| 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 |
/** |
| 11 |
* Helper utility that generates conversion tables. |
| 12 |
*/ |
| 13 |
|
| 14 |
#include <stdio.h> |
| 15 |
#include <string.h> |
| 16 |
#include <math.h> |
| 17 |
#include <libgen.h> |
| 18 |
|
| 19 |
#include "builtin_table.h" |
| 20 |
#include "parse_table.h" |
| 21 |
#include "table.h" |
| 22 |
#include "wrapped.h" |
| 23 |
|
| 24 |
/** |
| 25 |
* Escapes a string for this particular case. |
| 26 |
* |
| 27 |
* @param in |
| 28 |
* |
| 29 |
* @return |
| 30 |
*/ |
| 31 |
static char *escape_string(char *in) |
| 32 |
{ |
| 33 |
char *ret, *work; |
| 34 |
|
| 35 |
ret = work = wrapped_malloc((strlen(in) * 2) + 1); |
| 36 |
while (*in != '\0') { |
| 37 |
switch (*in) { |
| 38 |
case '"': |
| 39 |
*work++ = '\\'; |
| 40 |
*work++ = *in++; |
| 41 |
break; |
| 42 |
|
| 43 |
case '\\': |
| 44 |
// leave \u and \U alone |
| 45 |
if (*(in + 1) != 'u' && *(in + 1) != 'U') { |
| 46 |
*work++ = '\\'; |
| 47 |
} |
| 48 |
*work++ = *in++; |
| 49 |
break; |
| 50 |
|
| 51 |
default: |
| 52 |
*work++ = *in++; |
| 53 |
break; |
| 54 |
} |
| 55 |
} |
| 56 |
*work = '\0'; |
| 57 |
|
| 58 |
return ret; |
| 59 |
} |
| 60 |
|
| 61 |
static void generate_loader(char *filename) |
| 62 |
{ |
| 63 |
table_t *table; |
| 64 |
table_t *hold_table; |
| 65 |
int i; |
| 66 |
|
| 67 |
hold_table = parse_table(filename); |
| 68 |
|
| 69 |
table = table_resize(hold_table, hold_table->max_key + 1, 1); |
| 70 |
|
| 71 |
printf( |
| 72 |
"\n" |
| 73 |
"/**\n" |
| 74 |
" * Generated from %s\n" |
| 75 |
" */\n\n", |
| 76 |
basename(filename) |
| 77 |
); |
| 78 |
|
| 79 |
// |
| 80 |
|
| 81 |
printf( |
| 82 |
"static table_row_t builtin_NEW_rows[%d] = {\n", |
| 83 |
table->used |
| 84 |
); |
| 85 |
|
| 86 |
for (i = 0; i < table->length; i++) { |
| 87 |
if (table->rows[i].key == 0) { |
| 88 |
continue; |
| 89 |
} |
| 90 |
|
| 91 |
printf( |
| 92 |
" { " |
| 93 |
".key = 0x%04x, " |
| 94 |
".data = \"%s\" " |
| 95 |
"},\n", |
| 96 |
table->rows[i].key, |
| 97 |
escape_string(table->rows[i].data) |
| 98 |
); |
| 99 |
} |
| 100 |
|
| 101 |
printf( |
| 102 |
"};\n\n" |
| 103 |
); |
| 104 |
|
| 105 |
// |
| 106 |
|
| 107 |
printf( |
| 108 |
"static table_t builtin_NEW_table = {\n" |
| 109 |
" .length = %d,\n" |
| 110 |
" .used = %d,\n" |
| 111 |
" .max_data_length = %d,\n" |
| 112 |
" .max_key = 0x%04x,\n" |
| 113 |
" .hits = 0,\n" |
| 114 |
" .misses = 0,\n" |
| 115 |
" .seeks = 0,\n" |
| 116 |
" .overwrites = %d,\n" |
| 117 |
" .use_hash = 0,\n" |
| 118 |
" .builtin = 1,\n" |
| 119 |
" .rows = builtin_NEW_rows,\n", |
| 120 |
table->used, |
| 121 |
table->used, |
| 122 |
table->max_data_length, |
| 123 |
table->max_key, |
| 124 |
table->overwrites |
| 125 |
); |
| 126 |
|
| 127 |
if (table->default_translation == NULL) { |
| 128 |
printf( |
| 129 |
" .default_translation = NULL,\n" |
| 130 |
); |
| 131 |
} else { |
| 132 |
printf( |
| 133 |
" .default_translation = \"%s\",\n", |
| 134 |
table->default_translation |
| 135 |
); |
| 136 |
} |
| 137 |
|
| 138 |
printf( |
| 139 |
"};\n\n" |
| 140 |
); |
| 141 |
|
| 142 |
// |
| 143 |
|
| 144 |
printf( |
| 145 |
"table_t *load_builtin_NEW_table(void)\n" |
| 146 |
"{\n" |
| 147 |
" return table_resize(&builtin_NEW_table, %d, 1);\n" |
| 148 |
"}\n", |
| 149 |
((int) ceil(table->used / BUILTIN_TABLE_MULTIPLE) + 1) * BUILTIN_TABLE_MULTIPLE |
| 150 |
); |
| 151 |
} |
| 152 |
|
| 153 |
int main(int argc, char **argv) |
| 154 |
{ |
| 155 |
if (argc == 1) { |
| 156 |
fprintf(stderr, "please specify a file to operate on\n"); |
| 157 |
return -1; |
| 158 |
} |
| 159 |
|
| 160 |
generate_loader(argv[1]); |
| 161 |
|
| 162 |
return 0; |
| 163 |
} |