| 1 |
/** |
| 2 |
* This file is part of the Detox package. |
| 3 |
* |
| 4 |
* Copyright (c) Doug Harple <[email protected]> |
| 5 |
* |
| 6 |
* For the full copyright and license information, please view the LICENSE |
| 7 |
* file that was distributed with this source code. |
| 8 |
*/ |
| 9 |
|
| 10 |
#include "config.h" |
| 11 |
|
| 12 |
#include <stdio.h> |
| 13 |
#include <stdlib.h> |
| 14 |
#include <string.h> |
| 15 |
#include <ctype.h> |
| 16 |
|
| 17 |
#include "file.h" |
| 18 |
#include "filter.h" |
| 19 |
#include "parse_options.h" |
| 20 |
#include "sequence.h" |
| 21 |
#include "table.h" |
| 22 |
#include "table_dump.h" |
| 23 |
#include "wrapped.h" |
| 24 |
|
| 25 |
#define MAX_LENGTH 16 |
| 26 |
|
| 27 |
/** |
| 28 |
* Generated from c_escape.tbl |
| 29 |
*/ |
| 30 |
static table_row_t builtin_c_escape_rows[36] = { |
| 31 |
{ .key = 0x0001, .data = "\\x01" }, |
| 32 |
{ .key = 0x0002, .data = "\\x02" }, |
| 33 |
{ .key = 0x0003, .data = "\\x03" }, |
| 34 |
{ .key = 0x0004, .data = "\\x04" }, |
| 35 |
{ .key = 0x0005, .data = "\\x05" }, |
| 36 |
{ .key = 0x0006, .data = "\\x06" }, |
| 37 |
{ .key = 0x0007, .data = "\\a" }, |
| 38 |
{ .key = 0x0008, .data = "\\b" }, |
| 39 |
{ .key = 0x0009, .data = "\\t" }, |
| 40 |
{ .key = 0x000a, .data = "\\n" }, |
| 41 |
{ .key = 0x000b, .data = "\\v" }, |
| 42 |
{ .key = 0x000c, .data = "\\f" }, |
| 43 |
{ .key = 0x000d, .data = "\\r" }, |
| 44 |
{ .key = 0x000e, .data = "\\x0e" }, |
| 45 |
{ .key = 0x000f, .data = "\\x0f" }, |
| 46 |
{ .key = 0x0010, .data = "\\x10" }, |
| 47 |
{ .key = 0x0011, .data = "\\x11" }, |
| 48 |
{ .key = 0x0012, .data = "\\x12" }, |
| 49 |
{ .key = 0x0013, .data = "\\x13" }, |
| 50 |
{ .key = 0x0014, .data = "\\x14" }, |
| 51 |
{ .key = 0x0015, .data = "\\x15" }, |
| 52 |
{ .key = 0x0016, .data = "\\x16" }, |
| 53 |
{ .key = 0x0017, .data = "\\x17" }, |
| 54 |
{ .key = 0x0018, .data = "\\x18" }, |
| 55 |
{ .key = 0x0019, .data = "\\x19" }, |
| 56 |
{ .key = 0x001a, .data = "\\x1a" }, |
| 57 |
{ .key = 0x001b, .data = "\\x1b" }, |
| 58 |
{ .key = 0x001c, .data = "\\x1c" }, |
| 59 |
{ .key = 0x001d, .data = "\\x1d" }, |
| 60 |
{ .key = 0x001e, .data = "\\x1e" }, |
| 61 |
{ .key = 0x001f, .data = "\\x1f" }, |
| 62 |
{ .key = 0x0022, .data = "\\\"" }, |
| 63 |
{ .key = 0x0027, .data = "\\'" }, |
| 64 |
{ .key = 0x003f, .data = "\\?" }, |
| 65 |
{ .key = 0x005c, .data = "\\\\" }, |
| 66 |
{ .key = 0x007f, .data = "\\x7f" }, |
| 67 |
}; |
| 68 |
|
| 69 |
static table_t builtin_c_escape_table = { |
| 70 |
.length = 36, |
| 71 |
.used = 36, |
| 72 |
.max_data_length = 4, |
| 73 |
.max_key = 0x007f, |
| 74 |
.hits = 0, |
| 75 |
.misses = 0, |
| 76 |
.seeks = 0, |
| 77 |
.overwrites = 0, |
| 78 |
.use_hash = 0, |
| 79 |
.builtin = 1, |
| 80 |
.rows = builtin_c_escape_rows, |
| 81 |
.default_translation = NULL, |
| 82 |
}; |
| 83 |
|
| 84 |
/** |
| 85 |
* Generates a translation table that escapes everything not between 0x20 and |
| 86 |
* 0x7E (inclusive). |
| 87 |
* |
| 88 |
* For characters 0x01-0x1F and 0x7F, a \x escape sequence is used. This could |
| 89 |
* be improved, e.g. use \n instead of \x0A and \t instead of \x09. |
| 90 |
* |
| 91 |
* For characters 0x80- |
| 92 |
*/ |
| 93 |
table_t *generate_c_escape_table(void) |
| 94 |
{ |
| 95 |
table_t *table; |
| 96 |
char *work; |
| 97 |
int i; |
| 98 |
|
| 99 |
work = wrapped_malloc(MAX_LENGTH); |
| 100 |
|
| 101 |
table = table_resize(&builtin_c_escape_table, 0x110000, 1); |
| 102 |
|
| 103 |
for (i = 0x20; i < 0x7F; i++) { |
| 104 |
if (table_get(table, i) == NULL) { |
| 105 |
snprintf(work, MAX_LENGTH, "%c", i); |
| 106 |
table_put(table, i, work); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
for (i = 0x80; i < 0x10000; i++) { |
| 111 |
snprintf(work, MAX_LENGTH, "\\u%04X", i); |
| 112 |
table_put(table, i, work); |
| 113 |
} |
| 114 |
|
| 115 |
for (; i < 0x110000; i++) { |
| 116 |
snprintf(work, MAX_LENGTH, "\\U%08X", i); |
| 117 |
table_put(table, i, work); |
| 118 |
} |
| 119 |
|
| 120 |
free(work); |
| 121 |
|
| 122 |
#ifdef DEBUG |
| 123 |
table_reset(table); |
| 124 |
#endif |
| 125 |
|
| 126 |
return table; |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Reads from stdin and escapes everything so that: |
| 131 |
* |
| 132 |
* a) all Unicode is obvious, and |
| 133 |
* b) the output is safe to use in a C string. |
| 134 |
*/ |
| 135 |
int main(void) |
| 136 |
{ |
| 137 |
table_t *table; |
| 138 |
options_t *options; |
| 139 |
sequence_t *sequence; |
| 140 |
filter_t *filter; |
| 141 |
|
| 142 |
table = generate_c_escape_table(); |
| 143 |
|
| 144 |
options = options_init(); |
| 145 |
|
| 146 |
options->sequence_to_use = sequence = sequence_init("utf_8-escape"); |
| 147 |
sequence->source_filename = wrapped_strdup(__FILE__); |
| 148 |
|
| 149 |
sequence->filters = filter = filter_init(FILTER_UTF_8); |
| 150 |
filter->filename = wrapped_strdup(__FILE__); |
| 151 |
filter->table = table; |
| 152 |
|
| 153 |
parse_inline(NULL, NULL, options); |
| 154 |
|
| 155 |
#ifdef DEBUG |
| 156 |
printf("\n\n"); |
| 157 |
table_stats(table); |
| 158 |
table_free(table); |
| 159 |
#endif |
| 160 |
} |