forked from n64decomp/sm64
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtextconv.c
565 lines (485 loc) · 15.3 KB
/
textconv.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
#include <ctype.h>
#include <errno.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "hashtable.h"
#include "utf8.h"
#define ARRAY_COUNT(arr) (sizeof(arr) / sizeof(arr[0]))
#define INVALID_CHAR 0xFFFFFFFF
struct CharmapEntry
{
uint32_t unicode[3];
int length; // length of the unicode array. TODO: use dynamic memory allocation
int bytesCount;
uint8_t bytes[4]; // bytes to convert unicode array to, (e.g. 'A' = 0x0A)
};
static struct HashTable *charmap;
static void fatal_error(const char *msgfmt, ...)
{
va_list args;
fputs("error: ", stderr);
va_start(args, msgfmt);
vfprintf(stderr, msgfmt, args);
va_end(args);
fputc('\n', stderr);
exit(1);
}
static void parse_error(const char *filename, int lineNum, const char *msgfmt, ...)
{
va_list args;
fprintf(stderr, "%s: line %i: ", filename, lineNum);
va_start(args, msgfmt);
vfprintf(stderr, msgfmt, args);
va_end(args);
fputc('\n', stderr);
exit(1);
}
// Reads the whole file and returns a null-terminated buffer with its contents
void *read_text_file(const char *filename)
{
if (strcmp(filename, "-") != 0)
{
FILE *file = fopen(filename, "rb");
uint8_t *buffer;
size_t size;
if (file == NULL)
fatal_error("failed to open file '%s' for reading: %s", filename, strerror(errno));
// get size
fseek(file, 0, SEEK_END);
size = ftell(file);
// allocate buffer
buffer = malloc(size + 1);
if (buffer == NULL)
fatal_error("could not allocate buffer of size %u", (uint32_t)(size + 1));
// read file
fseek(file, 0, SEEK_SET);
if (fread(buffer, size, 1, file) != 1)
fatal_error("error reading from file '%s': %s", filename, strerror(errno));
// null-terminate the buffer
buffer[size] = 0;
fclose(file);
return buffer;
}
else
{
size_t size = 0;
size_t capacity = 1024;
uint8_t *buffer = malloc(capacity + 1);
if (buffer == NULL)
fatal_error("could not allocate buffer of size %u", (uint32_t)(capacity + 1));
for (;;)
{
size += fread(buffer + size, 1, capacity - size, stdin);
if (size == capacity)
{
capacity *= 2;
buffer = realloc(buffer, capacity + 1);
if (buffer == NULL)
fatal_error("could not allocate buffer of size %u", (uint32_t)(capacity + 1));
}
else if (feof(stdin))
{
break;
}
else
{
fatal_error("error reading from stdin: %s", strerror(errno));
}
}
// null-terminate the buffer
buffer[size] = 0;
return buffer;
}
}
static char *skip_whitespace(char *str)
{
while (isspace(*str))
str++;
return str;
}
// null terminates the current line and returns a pointer to the next line
static char *line_split(char *str)
{
while (*str != '\n')
{
if (*str == 0)
return str; // end of string
str++;
}
*str = 0; // terminate line
return str + 1;
}
static char *parse_number(const char *str, unsigned int *num)
{
char *endptr;
unsigned int n = strtol(str, &endptr, 0);
*num = n;
if (endptr > str)
return endptr;
else
return NULL;
}
static int is_identifier_char(char c)
{
return isalnum(c) || c == '_';
}
static uint32_t get_escape_char(int c)
{
const uint8_t escapeTable[] =
{
['0'] = '\0',
['a'] = '\a',
['b'] = '\b',
['f'] = '\f',
['n'] = '\n',
['r'] = '\r',
['t'] = '\t',
['v'] = '\v',
['\\'] = '\\',
['\''] = '\'',
['"'] = '"',
};
if ((unsigned int)c < ARRAY_COUNT(escapeTable) && (escapeTable[c] != 0 || c == '0'))
return escapeTable[c];
else
return INVALID_CHAR;
}
static void read_charmap(const char *filename)
{
char *filedata = read_text_file(filename);
char *line = filedata;
int lineNum = 1;
while (line[0] != 0)
{
char *nextLine = line_split(line);
struct CharmapEntry entry;
struct CharmapEntry *existing;
line = skip_whitespace(line);
if (line[0] != 0 && !(line[0] == '/' && line[1] == '/')) // ignore empty lines and comments
{
int len = 0;
/* Read Character */
// opening quote
if (*line != '\'')
parse_error(filename, lineNum, "expected '");
line++;
// perform analysis of charmap entry, we are in the quote
while(1)
{
if(*line == '\'')
{
line++;
break;
}
else if(len == ARRAY_COUNT(entry.unicode))
{
// TODO: Use dynamic memory allocation so this is unnecessary.
parse_error(filename, lineNum, "string limit exceeded");
}
else if (*line == '\\')
{
line++; // advance to get the character being escaped
if (*line == '\r')
line++;
if (*line == '\n')
{
// Backslash at end of line is ignored
continue;
}
entry.unicode[len] = get_escape_char(*line);
if (entry.unicode[len] == INVALID_CHAR)
parse_error(filename, lineNum, "unknown escape sequence \\%c", *line);
line++; // increment again to get past the escape sequence.
}
else
{
line = utf8_decode(line, &entry.unicode[len]);
if (line == NULL)
parse_error(filename, lineNum, "invalid UTF8");
}
len++;
}
entry.length = len;
// equals sign
line = skip_whitespace(line);
if (*line != '=')
parse_error(filename, lineNum, "expected = after character \\%c", *line);
line++;
entry.bytesCount = 0;
// value
while (1)
{
uint32_t value;
if (entry.bytesCount >= 4)
parse_error(filename, lineNum, "more than 4 values specified");
line = skip_whitespace(line);
line = parse_number(line, &value);
if (line == NULL)
parse_error(filename, lineNum, "expected number after =");
if (value > 0xFF)
parse_error(filename, lineNum, "0x%X is larger than 1 byte", value);
entry.bytes[entry.bytesCount] = value;
entry.bytesCount++;
line = skip_whitespace(line);
if (*line == 0)
break;
if (*line != ',')
parse_error(filename, lineNum, "junk at end of line");
line++;
}
existing = hashtable_query(charmap, &entry);
if (existing != NULL) {
const char *fmt = "0x%02X, ";
int fmtlen = 6;
char str[32];
int i;
for (i = 0; i < existing->bytesCount; i++) {
sprintf(&str[fmtlen * i], fmt, existing->bytes[i]);
}
str[fmtlen * i - 2] = '\0';
parse_error(filename, lineNum, "entry for character already exists (%s)", str);
} else {
hashtable_insert(charmap, &entry);
}
}
line = nextLine;
lineNum++;
}
free(filedata);
}
static int count_line_num(const char *start, const char *pos)
{
const char *c;
int lineNum = 1;
for (c = start; c < pos; c++)
{
if (*c == '\n')
lineNum++;
}
return lineNum;
}
static char *convert_string(char *pos, FILE *fout, const char *inputFileName, char *start, int uncompressed, int cnOneByte)
{
const struct CharmapEntry terminatorInput = {.unicode = {'\0'}, .length = 1};
struct CharmapEntry *terminator;
int hasString = 0;
int i;
while (1)
{
pos = skip_whitespace(pos);
if (*pos == ')')
{
if (hasString)
break;
else
parse_error(inputFileName, count_line_num(start, pos), "expected quoted string after '_('");
}
else if (*pos != '"')
parse_error(inputFileName, count_line_num(start, pos), "unexpected character '%c'", *pos);
pos++;
hasString = 1;
// convert quoted string
while (*pos != '"')
{
struct CharmapEntry input;
struct CharmapEntry *last_valid_entry = NULL;
struct CharmapEntry *entry;
uint32_t c;
int length = 0;
char* last_valid_pos = NULL;
// safely erase the unicode area before use
memset(input.unicode, 0, sizeof (input.unicode));
input.length = 0;
// Find a charmap entry of longest length possible starting from this position
while (*pos != '"')
{
if ((uncompressed && length == 1) || length == ARRAY_COUNT(entry->unicode))
{
// Stop searching after length 3; we only support strings of lengths up
// to that right now. Unless uncompressed is set, in which we ignore multi
// texts by discarding entries longer than 1.
break;
}
if (*pos == 0)
parse_error(inputFileName, count_line_num(start, pos), "EOF in string literal");
if (*pos == '\\')
{
pos++;
c = get_escape_char(*pos);
if (c == INVALID_CHAR)
parse_error(inputFileName, count_line_num(start, pos), "unknown escape sequence \\%c", *pos);
input.unicode[length] = c;
pos++;
}
else
{
pos = utf8_decode(pos, &input.unicode[length]);
if (pos == NULL)
parse_error(inputFileName, count_line_num(start, pos), "invalid unicode encountered in file");
}
length++;
input.length = length;
entry = hashtable_query(charmap, &input);
if (entry != NULL)
{
last_valid_entry = entry;
last_valid_pos = pos;
}
}
entry = last_valid_entry;
pos = last_valid_pos;
if (entry == NULL)
parse_error(inputFileName, count_line_num(start, pos), "no charmap entry for U+%X", input.unicode[0]);
for (i = 0; i < entry->bytesCount; i++) {
if (entry->bytesCount > 1 && cnOneByte && i % 2 == 0) {
continue;
}
fprintf(fout, "0x%02X,", entry->bytes[i]);
}
}
pos++; // skip over closing '"'
}
pos++; // skip over closing ')'
// use terminator \0 from charmap if provided, otherwise default 0xFF
terminator = hashtable_query(charmap, &terminatorInput);
if (terminator == NULL)
fputs("0xFF", fout);
else
{
for (i = 0; i < (cnOneByte ? 1 : terminator->bytesCount); i++)
fprintf(fout, "0x%02X,", terminator->bytes[i]);
}
return pos;
}
static void convert_file(const char *infilename, const char *outfilename)
{
char *in = read_text_file(infilename);
FILE *fout = strcmp(outfilename, "-") != 0 ? fopen(outfilename, "wb") : stdout;
if (fout == NULL)
fatal_error("failed to open file '%s' for writing: %s", strerror(errno));
char *start = in;
char *end = in;
char *pos = in;
while (1)
{
if (*pos == 0) // end of file
goto eof;
// check for comment
if (*pos == '/')
{
pos++;
// skip over // comment
if (*pos == '/')
{
pos++;
// skip over next newline
while (*pos != '\n')
{
if (*pos == 0)
goto eof;
pos++;
}
pos++;
}
// skip over /* */ comment
else if (*pos == '*')
{
pos++;
while (*pos != '*' && pos[1] != '/')
{
if (*pos == 0)
goto eof;
pos++;
}
pos += 2;
}
}
// skip over normal string literal
else if (*pos == '"')
{
pos++;
while (*pos != '"')
{
if (*pos == 0)
goto eof;
if (*pos == '\\')
pos++;
pos++;
}
pos++;
}
// check for _( sequence
else if ((*pos == '_') && (pos == in || !is_identifier_char(pos[-1])))
{
int uncompressed = 0;
int cnOneByte = 0;
end = pos;
pos++;
if (*pos == '_') // an extra _ signifies uncompressed strings. Enable uncompressed flag
{
pos++;
uncompressed = 1;
}
if (*pos == '%') // an extra % signifies a one-byte long characters on iQue instead of two-byte
{
pos++;
cnOneByte = 1;
}
if (*pos == '(')
{
pos++;
fwrite(start, end - start, 1, fout);
pos = convert_string(pos, fout, infilename, in, uncompressed, cnOneByte);
start = pos;
}
}
else
{
pos++;
}
}
eof:
fwrite(start, pos - start, 1, fout);
if (strcmp(outfilename, "-") != 0)
fclose(fout);
free(in);
}
static unsigned int charmap_hash(const void *value)
{
const struct CharmapEntry* entry = value;
unsigned int ret = 0;
for (int i = 0; i < entry->length; i++)
ret = ret * 17 + entry->unicode[i];
return ret;
}
static int charmap_cmp(const void *a, const void *b)
{
const struct CharmapEntry *ea = a;
const struct CharmapEntry *eb = b;
if (ea->length != eb->length)
return 0;
for(int i = 0; i < ea->length; i++)
if(ea->unicode[i] != eb->unicode[i])
return 0;
return 1;
}
static void usage(const char *execName)
{
fprintf(stderr, "Usage: %s CHARMAP INPUT OUTPUT\n", execName);
}
int main(int argc, char **argv)
{
if (argc != 4)
{
usage(argv[0]);
return 1;
}
charmap = hashtable_new(charmap_hash, charmap_cmp, 256, sizeof(struct CharmapEntry));
read_charmap(argv[1]);
convert_file(argv[2], argv[3]);
hashtable_free(charmap);
return 0;
}