| 1 |
|
| /* |
| 2 |
|
| * escape_ansi.c |
| 3 |
|
| * ------------- |
| 4 |
|
| * Functions for escaping non-printable characters. |
| 5 |
|
| * |
| 6 |
|
| * |
| 7 |
|
| * Copyright 2008, 2009 Sebastien Aperghis-Tramoni |
| 8 |
|
| * |
| 9 |
|
| * This program is free software; you can redistribute it |
| 10 |
|
| * and/or modify it under the same terms as Perl itself. |
| 11 |
|
| */ |
| 12 |
|
| |
| 13 |
|
| /* standard includes */ |
| 14 |
|
| #include <stdlib.h> |
| 15 |
|
| |
| 16 |
|
| /* custom includes */ |
| 17 |
|
| #include "escape_chars.h" |
| 18 |
|
| |
| 19 |
|
| /* private macros */ |
| 20 |
|
| #define NON_PRINTABLE(x) ((x) <= '\x1F' || (x) == '\x7F') |
| 21 |
|
| #define CHAR_INDEX(x) ( (x) <= '\x1F' ? (int)((x) - '\x00') \ |
| 22 |
|
| : (x) == '\x7F' ? (int)((x) - '\x1F' + 1) \ |
| 23 |
|
| : (int)((x) - '\x1F' + 2) ) |
| 24 |
|
| |
| 25 |
|
| /* structures */ |
| 26 |
|
| struct { |
| 27 |
|
| const char code; |
| 28 |
|
| const char *name; |
| 29 |
|
| } charname[] = { |
| 30 |
|
| { '\x00', "NUL" }, /* "\\0" */ |
| 31 |
|
| { '\x01', "SOH" }, |
| 32 |
|
| { '\x02', "STX" }, |
| 33 |
|
| { '\x03', "ETX" }, |
| 34 |
|
| { '\x04', "EOT" }, |
| 35 |
|
| { '\x05', "ENQ" }, |
| 36 |
|
| { '\x06', "ACK" }, |
| 37 |
|
| { '\x07', "BEL" }, /* "\\a" */ |
| 38 |
|
| { '\x08', "BS" }, /* "\\b" */ |
| 39 |
|
| { '\x09', "HT" }, /* "\\t" */ |
| 40 |
|
| { '\x0A', "LF" }, /* "\\n" */ |
| 41 |
|
| { '\x0B', "VT" }, /* "\\v" */ |
| 42 |
|
| { '\x0C', "FF" }, /* "\\f" */ |
| 43 |
|
| { '\x0D', "CR" }, /* "\\r" */ |
| 44 |
|
| { '\x0E', "SO" }, |
| 45 |
|
| { '\x0F', "SI" }, |
| 46 |
|
| { '\x10', "DLE" }, |
| 47 |
|
| { '\x11', "DC1" }, |
| 48 |
|
| { '\x12', "DC2" }, |
| 49 |
|
| { '\x13', "DC3" }, |
| 50 |
|
| { '\x14', "DC4" }, |
| 51 |
|
| { '\x15', "NAK" }, |
| 52 |
|
| { '\x16', "SYN" }, |
| 53 |
|
| { '\x17', "ETB" }, |
| 54 |
|
| { '\x18', "CAN" }, |
| 55 |
|
| { '\x19', "EM" }, |
| 56 |
|
| { '\x1A', "SUB" }, |
| 57 |
|
| { '\x1B', "ESC" }, |
| 58 |
|
| { '\x1C', "FS" }, |
| 59 |
|
| { '\x1D', "GS" }, |
| 60 |
|
| { '\x1E', "RS" }, |
| 61 |
|
| { '\x1F', "US" }, |
| 62 |
|
| { '\x7F', "DEL" }, |
| 63 |
|
| { 0, "???" }, |
| 64 |
|
| }; |
| 65 |
|
| |
| 66 |
|
| |
| 67 |
|
| /* |
| 68 |
|
| * escape_non_printable_chars() |
| 69 |
|
| * -------------------------- |
| 70 |
|
| /*! Escape non-printable characters |
| 71 |
|
| * |
| 72 |
|
| * This function parse the given null-terminated string and looks for |
| 73 |
|
| * non-printable characters to escape. |
| 74 |
|
| * |
| 75 |
|
| * If there are any, a new string is allocated where each non-printable |
| 76 |
|
| * character is replaced with a human-readable representation. For example |
| 77 |
|
| * the character '\x1B' (ESC) is replace with "<ESC>". |
| 78 |
|
| * |
| 79 |
|
| * If there is no character to escape, the original string is returned. |
| 80 |
|
| * |
| 81 |
|
| * Typical usage: |
| 82 |
|
| * |
| 83 |
|
| * char input[] = ""; |
| 84 |
|
| * char *output; |
| 85 |
|
| * |
| 86 |
|
| * output = escape_non_printable_chars(input); |
| 87 |
|
| * ... |
| 88 |
|
| * if (output != input) |
| 89 |
|
| * free(output); |
| 90 |
|
| * |
| 91 |
|
| * |
| 92 |
|
| * @param input null-terminated string |
| 93 |
|
| * @return null-terminated string |
| 94 |
|
| * |
| 95 |
|
| */ |
| 96 |
6 |
| char * escape_non_printable_chars(const char *input) { |
| 97 |
|
| char *output; |
| 98 |
|
| const char *i; |
| 99 |
|
| const char *n; |
| 100 |
|
| char *o; |
| 101 |
6 |
| int len = 0; |
| 102 |
6 |
| int num = 0; |
| 103 |
|
| |
| 104 |
|
| /* first loop to calculate the number of characters to escape */ |
| 105 |
164 |
▶ | for (i=input; *i != '\0'; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i=input; *i != '\0'; i++) { |
158 |
6 |
164 |
|
| 106 |
158 |
| len++; |
| 107 |
|
| |
| 108 |
158 |
▶ | if (NON_PRINTABLE(*i)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (NON_PRINTABLE(*i)) |
136 |
22 |
158 |
| if (NON_PRINTABLE(*i)) |
0 |
136 |
136 |
|
| 109 |
22 |
| num++; |
| 110 |
|
| } |
| 111 |
|
| |
| 112 |
|
| /* if there's nothing to escape, just return the input string */ |
| 113 |
6 |
▶ | if (num == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (num == 0) |
0 |
6 |
6 |
|
| 114 |
0 |
| return (char *) input; |
| 115 |
|
| |
| 116 |
6 |
| output = (char *) malloc(len + 4*num); |
| 117 |
|
| |
| 118 |
|
| /* second loop to actually escape the non-printable characters */ |
| 119 |
164 |
▶ | for (i=input, o=output; *i != '\0'; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i=input, o=output; *i != '\0'; i++) { |
158 |
6 |
164 |
|
| 120 |
158 |
▶ | if (NON_PRINTABLE(*i)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (NON_PRINTABLE(*i)) { |
136 |
22 |
158 |
| if (NON_PRINTABLE(*i)) { |
0 |
136 |
136 |
|
| 121 |
22 |
| *o++ = '<'; |
| 122 |
|
| |
| 123 |
86 |
▶ | for (n=charname[CHAR_INDEX(*i)].name; *n != '\0'; n++) |
Branch: 3 branches
| Branch | True | False | Total |
| for (n=charname[CHAR_INDEX(*i)].name; *n != '\0'; n++) |
22 |
0 |
22 |
| for (n=charname[CHAR_INDEX(*i)].name; *n != '\0'; n++) |
0 |
0 |
0 |
| for (n=charname[CHAR_INDEX(*i)].name; *n != '\0'; n++) |
64 |
22 |
86 |
|
| 124 |
64 |
| *o++ = *n; |
| 125 |
|
| |
| 126 |
22 |
| *o++ = '>'; |
| 127 |
|
| } |
| 128 |
|
| else |
| 129 |
136 |
| *o++ = *i; |
| 130 |
|
| } |
| 131 |
|
| |
| 132 |
6 |
| *o = '\0'; |
| 133 |
|
| |
| 134 |
6 |
| return output; |
| 135 |
|
| } |