1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 *******************************************************************************
5 *
6 * Copyright (C) 1999-2016 International Business Machines
7 * Corporation and others. All Rights Reserved.
8 *
9 *******************************************************************************
10 * file name: gencnval.c
11 * encoding: UTF-8
12 * tab size: 8 (not used)
13 * indentation:4
14 *
15 * created on: 1999nov05
16 * created by: Markus W. Scherer
17 *
18 * This program reads convrtrs.txt and writes a memory-mappable
19 * converter name alias table to cnvalias.dat .
20 *
21 * This program currently writes version 2.1 of the data format. See
22 * ucnv_io.c for more details on the format. Note that version 2.1
23 * is written in such a way that a 2.0 reader will be able to use it,
24 * and a 2.1 reader will be able to read 2.0.
25 */
26
27 #include "unicode/utypes.h"
28 #include "unicode/putil.h"
29 #include "unicode/ucnv.h" /* ucnv_compareNames() */
30 #include "ucnv_io.h"
31 #include "cmemory.h"
32 #include "cstring.h"
33 #include "uinvchar.h"
34 #include "filestrm.h"
35 #include "toolutil.h"
36 #include "unicode/uclean.h"
37 #include "unewdata.h"
38 #include "uoptions.h"
39
40 #include <ctype.h>
41 #include <stdbool.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44
45 /* TODO: Need to check alias name length is less than UCNV_MAX_CONVERTER_NAME_LENGTH */
46
47 /* STRING_STORE_SIZE + TAG_STORE_SIZE <= ((2^16 - 1) * 2)
48 That is the maximum size for the string stores combined
49 because the strings are indexed at 16-bit boundaries by a
50 16-bit index, and there is only one section for the
51 strings.
52 */
53 #define STRING_STORE_SIZE 0x1FBFE /* 130046 */
54 #define TAG_STORE_SIZE 0x400 /* 1024 */
55
56 /* The combined tag and converter count can affect the number of lists
57 created. The size of all lists must be less than (2^17 - 1)
58 because the lists are indexed as a 16-bit array with a 16-bit index.
59 */
60 #define MAX_TAG_COUNT 0x3F /* 63 */
61 #define MAX_CONV_COUNT UCNV_CONVERTER_INDEX_MASK
62 #define MAX_ALIAS_COUNT 0xFFFF /* 65535 */
63
64 /* The maximum number of aliases that a standard tag/converter combination can have.
65 At this moment 6/18/2002, IANA has 12 names for ASCII. Don't go below 15 for
66 this value. I don't recommend more than 31 for this value.
67 */
68 #define MAX_TC_ALIAS_COUNT 0x1F /* 31 */
69
70 #define MAX_LINE_SIZE 0x7FFF /* 32767 */
71 #define MAX_LIST_SIZE 0xFFFF /* 65535 */
72
73 #define DATA_NAME "cnvalias"
74 #define DATA_TYPE "icu" /* ICU alias table */
75
76 #define ALL_TAG_STR "ALL"
77 #define ALL_TAG_NUM 1
78 #define EMPTY_TAG_NUM 0
79
80 /* UDataInfo cf. udata.h */
81 static const UDataInfo dataInfo={
82 sizeof(UDataInfo),
83 0,
84
85 U_IS_BIG_ENDIAN,
86 U_CHARSET_FAMILY,
87 sizeof(UChar),
88 0,
89
90 {0x43, 0x76, 0x41, 0x6c}, /* dataFormat="CvAl" */
91 {3, 0, 1, 0}, /* formatVersion */
92 {1, 4, 2, 0} /* dataVersion */
93 };
94
95 typedef struct {
96 char *store;
97 uint32_t top;
98 uint32_t max;
99 } StringBlock;
100
101 static char stringStore[STRING_STORE_SIZE];
102 static StringBlock stringBlock = { stringStore, 0, STRING_STORE_SIZE };
103
104 typedef struct {
105 uint16_t aliasCount;
106 uint16_t *aliases; /* Index into stringStore */
107 } AliasList;
108
109 typedef struct {
110 uint16_t converter; /* Index into stringStore */
111 uint16_t totalAliasCount; /* Total aliases in this column */
112 } Converter;
113
114 static Converter converters[MAX_CONV_COUNT];
115 static uint16_t converterCount=0;
116
117 static char tagStore[TAG_STORE_SIZE];
118 static StringBlock tagBlock = { tagStore, 0, TAG_STORE_SIZE };
119
120 typedef struct {
121 uint16_t tag; /* Index into tagStore */
122 uint16_t totalAliasCount; /* Total aliases in this row */
123 AliasList aliasList[MAX_CONV_COUNT];
124 } Tag;
125
126 /* Think of this as a 3D array. It's tagCount by converterCount by aliasCount */
127 static Tag tags[MAX_TAG_COUNT];
128 static uint16_t tagCount = 0;
129
130 /* Used for storing all aliases */
131 static uint16_t knownAliases[MAX_ALIAS_COUNT];
132 static uint16_t knownAliasesCount = 0;
133 /*static uint16_t duplicateKnownAliasesCount = 0;*/
134
135 /* Used for storing the lists section that point to aliases */
136 static uint16_t aliasLists[MAX_LIST_SIZE];
137 static uint16_t aliasListsSize = 0;
138
139 /* Were the standard tags declared before the aliases. */
140 static UBool standardTagsUsed = false;
141 static UBool verbose = false;
142 static UBool quiet = false;
143 static int lineNum = 1;
144
145 static UConverterAliasOptions tableOptions = {
146 UCNV_IO_STD_NORMALIZED,
147 1 /* containsCnvOptionInfo */
148 };
149
150
151 /**
152 * path to convrtrs.txt
153 */
154 const char *path;
155
156 /* prototypes --------------------------------------------------------------- */
157
158 static void
159 parseLine(const char *line);
160
161 static void
162 parseFile(FileStream *in);
163
164 static int32_t
165 chomp(char *line);
166
167 static void
168 addOfficialTaggedStandards(char *line, int32_t lineLen);
169
170 static uint16_t
171 addAlias(const char *alias, uint16_t standard, uint16_t converter, UBool defaultName);
172
173 static uint16_t
174 addConverter(const char *converter);
175
176 static char *
177 allocString(StringBlock *block, const char *s, int32_t length);
178
179 static uint16_t
180 addToKnownAliases(const char *alias);
181
182 static int
183 compareAliases(const void *alias1, const void *alias2);
184
185 static uint16_t
186 getTagNumber(const char *tag, uint16_t tagLen);
187
188 /*static void
189 addTaggedAlias(uint16_t tag, const char *alias, uint16_t converter);*/
190
191 static void
192 writeAliasTable(UNewDataMemory *out);
193
194 /* -------------------------------------------------------------------------- */
195
196 /* Presumes that you used allocString() */
197 #define GET_ALIAS_STR(index) (stringStore + ((size_t)(index) << 1))
198 #define GET_TAG_STR(index) (tagStore + ((size_t)(index) << 1))
199
200 /* Presumes that you used allocString() */
201 #define GET_ALIAS_NUM(str) ((uint16_t)((str - stringStore) >> 1))
202 #define GET_TAG_NUM(str) ((uint16_t)((str - tagStore) >> 1))
203
204 enum
205 {
206 HELP1,
207 HELP2,
208 VERBOSE,
209 COPYRIGHT,
210 DESTDIR,
211 SOURCEDIR,
212 QUIET
213 };
214
215 static UOption options[]={
216 UOPTION_HELP_H,
217 UOPTION_HELP_QUESTION_MARK,
218 UOPTION_VERBOSE,
219 UOPTION_COPYRIGHT,
220 UOPTION_DESTDIR,
221 UOPTION_SOURCEDIR,
222 UOPTION_QUIET
223 };
224
225 extern int
main(int argc,char * argv[])226 main(int argc, char* argv[]) {
227 int i, n;
228 char pathBuf[512];
229 FileStream *in;
230 UNewDataMemory *out;
231 UErrorCode errorCode=U_ZERO_ERROR;
232
233 U_MAIN_INIT_ARGS(argc, argv);
234
235 /* preset then read command line options */
236 options[DESTDIR].value=options[SOURCEDIR].value=u_getDataDirectory();
237 argc=u_parseArgs(argc, argv, UPRV_LENGTHOF(options), options);
238
239 /* error handling, printing usage message */
240 if(argc<0) {
241 fprintf(stderr,
242 "error in command line argument \"%s\"\n",
243 argv[-argc]);
244 }
245 if(argc<0 || options[HELP1].doesOccur || options[HELP2].doesOccur) {
246 fprintf(stderr,
247 "usage: %s [-options] [convrtrs.txt]\n"
248 "\tread convrtrs.txt and create " U_ICUDATA_NAME "_" DATA_NAME "." DATA_TYPE "\n"
249 "options:\n"
250 "\t-h or -? or --help this usage text\n"
251 "\t-v or --verbose prints out extra information about the alias table\n"
252 "\t-q or --quiet do not display warnings and progress\n"
253 "\t-c or --copyright include a copyright notice\n"
254 "\t-d or --destdir destination directory, followed by the path\n"
255 "\t-s or --sourcedir source directory, followed by the path\n",
256 argv[0]);
257 return argc<0 ? U_ILLEGAL_ARGUMENT_ERROR : U_ZERO_ERROR;
258 }
259
260 if(options[VERBOSE].doesOccur) {
261 verbose = true;
262 }
263
264 if(options[QUIET].doesOccur) {
265 quiet = true;
266 }
267
268 if (argc >= 2) {
269 path = argv[1];
270 } else {
271 path = "convrtrs.txt";
272 }
273
274 const char* sourcedir = options[SOURCEDIR].value;
275 if (sourcedir != NULL && *sourcedir != 0) {
276 char *end;
277 uprv_strcpy(pathBuf, sourcedir);
278 end = uprv_strchr(pathBuf, 0);
279 if(*(end-1)!=U_FILE_SEP_CHAR) {
280 *(end++)=U_FILE_SEP_CHAR;
281 }
282 uprv_strcpy(end, path);
283 path = pathBuf;
284 }
285
286 uprv_memset(stringStore, 0, sizeof(stringStore));
287 uprv_memset(tagStore, 0, sizeof(tagStore));
288 uprv_memset(converters, 0, sizeof(converters));
289 uprv_memset(tags, 0, sizeof(tags));
290 uprv_memset(aliasLists, 0, sizeof(aliasLists));
291 uprv_memset(knownAliases, 0, sizeof(aliasLists));
292
293
294 in=T_FileStream_open(path, "r");
295 if(in==NULL) {
296 fprintf(stderr, "gencnval: unable to open input file %s\n", path);
297 exit(U_FILE_ACCESS_ERROR);
298 }
299 parseFile(in);
300 T_FileStream_close(in);
301
302 /* create the output file */
303 out=udata_create(options[DESTDIR].value, DATA_TYPE, DATA_NAME, &dataInfo,
304 options[COPYRIGHT].doesOccur ? U_COPYRIGHT_STRING : NULL, &errorCode);
305 if(U_FAILURE(errorCode)) {
306 fprintf(stderr, "gencnval: unable to open output file - error %s\n", u_errorName(errorCode));
307 exit(errorCode);
308 }
309
310 /* write the table of aliases based on a tag/converter name combination */
311 writeAliasTable(out);
312
313 /* finish */
314 udata_finish(out, &errorCode);
315 if(U_FAILURE(errorCode)) {
316 fprintf(stderr, "gencnval: error finishing output file - %s\n", u_errorName(errorCode));
317 exit(errorCode);
318 }
319
320 /* clean up tags */
321 for (i = 0; i < MAX_TAG_COUNT; i++) {
322 for (n = 0; n < MAX_CONV_COUNT; n++) {
323 if (tags[i].aliasList[n].aliases!=NULL) {
324 uprv_free(tags[i].aliasList[n].aliases);
325 }
326 }
327 }
328
329 return 0;
330 }
331
332 static void
parseFile(FileStream * in)333 parseFile(FileStream *in) {
334 char line[MAX_LINE_SIZE];
335 char lastLine[MAX_LINE_SIZE];
336 int32_t lineSize = 0;
337 int32_t lastLineSize = 0;
338 UBool validParse = true;
339
340 lineNum = 0;
341
342 /* Add the empty tag, which is for untagged aliases */
343 getTagNumber("", 0);
344 getTagNumber(ALL_TAG_STR, 3);
345 allocString(&stringBlock, "", 0);
346
347 /* read the list of aliases */
348 while (validParse) {
349 validParse = false;
350
351 /* Read non-empty lines that don't start with a space character. */
352 while (T_FileStream_readLine(in, lastLine, MAX_LINE_SIZE) != NULL) {
353 lastLineSize = chomp(lastLine);
354 if (lineSize == 0 || (lastLineSize > 0 && isspace((int)*lastLine))) {
355 uprv_strcpy(line + lineSize, lastLine);
356 lineSize += lastLineSize;
357 } else if (lineSize > 0) {
358 validParse = true;
359 break;
360 }
361 lineNum++;
362 }
363
364 if (validParse || lineSize > 0) {
365 if (isspace((int)*line)) {
366 fprintf(stderr, "%s:%d: error: cannot start an alias with a space\n", path, lineNum-1);
367 exit(U_PARSE_ERROR);
368 } else if (line[0] == '{') {
369 if (!standardTagsUsed && line[lineSize - 1] != '}') {
370 fprintf(stderr, "%s:%d: error: alias needs to start with a converter name\n", path, lineNum);
371 exit(U_PARSE_ERROR);
372 }
373 addOfficialTaggedStandards(line, lineSize);
374 standardTagsUsed = true;
375 } else {
376 if (standardTagsUsed) {
377 parseLine(line);
378 }
379 else {
380 fprintf(stderr, "%s:%d: error: alias table needs to start a list of standard tags\n", path, lineNum);
381 exit(U_PARSE_ERROR);
382 }
383 }
384 /* Was the last line consumed */
385 if (lastLineSize > 0) {
386 uprv_strcpy(line, lastLine);
387 lineSize = lastLineSize;
388 }
389 else {
390 lineSize = 0;
391 }
392 }
393 lineNum++;
394 }
395 }
396
397 /* This works almost like the Perl chomp.
398 It removes the newlines, comments and trailing whitespace (not preceding whitespace).
399 */
400 static int32_t
chomp(char * line)401 chomp(char *line) {
402 char *s = line;
403 char *lastNonSpace = line;
404 while(*s!=0) {
405 /* truncate at a newline or a comment */
406 if(*s == '\r' || *s == '\n' || *s == '#') {
407 *s = 0;
408 break;
409 }
410 if (!isspace((int)*s)) {
411 lastNonSpace = s;
412 }
413 ++s;
414 }
415 if (lastNonSpace++ > line) {
416 *lastNonSpace = 0;
417 s = lastNonSpace;
418 }
419 return (int32_t)(s - line);
420 }
421
422 static void
parseLine(const char * line)423 parseLine(const char *line) {
424 uint16_t pos=0, start, limit, length, cnv;
425 char *converter, *alias;
426
427 /* skip leading white space */
428 /* There is no whitespace at the beginning anymore */
429 /* while(line[pos]!=0 && isspace(line[pos])) {
430 ++pos;
431 }
432 */
433
434 /* is there nothing on this line? */
435 if(line[pos]==0) {
436 return;
437 }
438
439 /* get the converter name */
440 start=pos;
441 while(line[pos]!=0 && !isspace((int)line[pos])) {
442 ++pos;
443 }
444 limit=pos;
445
446 /* store the converter name */
447 length=(uint16_t)(limit-start);
448 converter=allocString(&stringBlock, line+start, length);
449
450 /* add the converter to the converter table */
451 cnv=addConverter(converter);
452
453 /* The name itself may be tagged, so let's added it to the aliases list properly */
454 pos = start;
455
456 /* get all the real aliases */
457 for(;;) {
458
459 /* skip white space */
460 while(line[pos]!=0 && isspace((int)line[pos])) {
461 ++pos;
462 }
463
464 /* is there no more alias name on this line? */
465 if(line[pos]==0) {
466 break;
467 }
468
469 /* get an alias name */
470 start=pos;
471 while(line[pos]!=0 && line[pos]!='{' && !isspace((int)line[pos])) {
472 ++pos;
473 }
474 limit=pos;
475
476 /* store the alias name */
477 length=(uint16_t)(limit-start);
478 if (start == 0) {
479 /* add the converter as its own alias to the alias table */
480 alias = converter;
481 addAlias(alias, ALL_TAG_NUM, cnv, true);
482 }
483 else {
484 alias=allocString(&stringBlock, line+start, length);
485 addAlias(alias, ALL_TAG_NUM, cnv, false);
486 }
487 addToKnownAliases(alias);
488
489 /* add the alias/converter pair to the alias table */
490 /* addAlias(alias, 0, cnv, false);*/
491
492 /* skip whitespace */
493 while (line[pos] && isspace((int)line[pos])) {
494 ++pos;
495 }
496
497 /* handle tags if they are present */
498 if (line[pos] == '{') {
499 ++pos;
500 do {
501 start = pos;
502 while (line[pos] && line[pos] != '}' && !isspace((int)line[pos])) {
503 ++pos;
504 }
505 limit = pos;
506
507 if (start != limit) {
508 /* add the tag to the tag table */
509 uint16_t tag = getTagNumber(line + start, (uint16_t)(limit - start));
510 addAlias(alias, tag, cnv, (UBool)(line[limit-1] == '*'));
511 }
512
513 while (line[pos] && isspace((int)line[pos])) {
514 ++pos;
515 }
516 } while (line[pos] && line[pos] != '}');
517
518 if (line[pos] == '}') {
519 ++pos;
520 } else {
521 fprintf(stderr, "%s:%d: Unterminated tag list\n", path, lineNum);
522 exit(U_UNMATCHED_BRACES);
523 }
524 } else {
525 addAlias(alias, EMPTY_TAG_NUM, cnv, (UBool)(tags[0].aliasList[cnv].aliasCount == 0));
526 }
527 }
528 }
529
530 static uint16_t
getTagNumber(const char * tag,uint16_t tagLen)531 getTagNumber(const char *tag, uint16_t tagLen) {
532 char *atag;
533 uint16_t t;
534 UBool preferredName = ((tagLen > 0) ? (tag[tagLen - 1] == '*') : (false));
535
536 if (tagCount >= MAX_TAG_COUNT) {
537 fprintf(stderr, "%s:%d: too many tags\n", path, lineNum);
538 exit(U_BUFFER_OVERFLOW_ERROR);
539 }
540
541 if (preferredName) {
542 /* puts(tag);*/
543 tagLen--;
544 }
545
546 for (t = 0; t < tagCount; ++t) {
547 const char *currTag = GET_TAG_STR(tags[t].tag);
548 if (uprv_strlen(currTag) == tagLen && !uprv_strnicmp(currTag, tag, tagLen)) {
549 return t;
550 }
551 }
552
553 /* we need to add this tag */
554 if (tagCount >= MAX_TAG_COUNT) {
555 fprintf(stderr, "%s:%d: error: too many tags\n", path, lineNum);
556 exit(U_BUFFER_OVERFLOW_ERROR);
557 }
558
559 /* allocate a new entry in the tag table */
560 atag = allocString(&tagBlock, tag, tagLen);
561
562 if (standardTagsUsed) {
563 fprintf(stderr, "%s:%d: error: Tag \"%s\" is not declared at the beginning of the alias table.\n",
564 path, lineNum, atag);
565 exit(1);
566 }
567 else if (tagLen > 0 && strcmp(tag, ALL_TAG_STR) != 0) {
568 fprintf(stderr, "%s:%d: warning: Tag \"%s\" was added to the list of standards because it was not declared at beginning of the alias table.\n",
569 path, lineNum, atag);
570 }
571
572 /* add the tag to the tag table */
573 tags[tagCount].tag = GET_TAG_NUM(atag);
574 /* The aliasList should be set to 0's already */
575
576 return tagCount++;
577 }
578
579 /*static void
580 addTaggedAlias(uint16_t tag, const char *alias, uint16_t converter) {
581 tags[tag].aliases[converter] = alias;
582 }
583 */
584
585 static void
addOfficialTaggedStandards(char * line,int32_t lineLen)586 addOfficialTaggedStandards(char *line, int32_t lineLen) {
587 (void) lineLen; // suppress compiler warnings about unused variable
588 char *atag;
589 char *endTagExp;
590 char *tag;
591 static const char WHITESPACE[] = " \t";
592
593 if (tagCount > UCNV_NUM_RESERVED_TAGS) {
594 fprintf(stderr, "%s:%d: error: official tags already added\n", path, lineNum);
595 exit(U_BUFFER_OVERFLOW_ERROR);
596 }
597 tag = strchr(line, '{');
598 if (tag == NULL) {
599 /* Why were we called? */
600 fprintf(stderr, "%s:%d: error: Missing start of tag group\n", path, lineNum);
601 exit(U_PARSE_ERROR);
602 }
603 tag++;
604 endTagExp = strchr(tag, '}');
605 if (endTagExp == NULL) {
606 fprintf(stderr, "%s:%d: error: Missing end of tag group\n", path, lineNum);
607 exit(U_PARSE_ERROR);
608 }
609 endTagExp[0] = 0;
610
611 tag = strtok(tag, WHITESPACE);
612 while (tag != NULL) {
613 /* printf("Adding original tag \"%s\"\n", tag);*/
614
615 /* allocate a new entry in the tag table */
616 atag = allocString(&tagBlock, tag, -1);
617
618 /* add the tag to the tag table */
619 tags[tagCount++].tag = (uint16_t)((atag - tagStore) >> 1);
620
621 /* The aliasList should already be set to 0's */
622
623 /* Get next tag */
624 tag = strtok(NULL, WHITESPACE);
625 }
626 }
627
628 static uint16_t
addToKnownAliases(const char * alias)629 addToKnownAliases(const char *alias) {
630 /* uint32_t idx; */
631 /* strict matching */
632 /* for (idx = 0; idx < knownAliasesCount; idx++) {
633 uint16_t num = GET_ALIAS_NUM(alias);
634 if (knownAliases[idx] != num
635 && uprv_strcmp(alias, GET_ALIAS_STR(knownAliases[idx])) == 0)
636 {
637 fprintf(stderr, "%s:%d: warning: duplicate alias %s and %s found\n", path,
638 lineNum, alias, GET_ALIAS_STR(knownAliases[idx]));
639 duplicateKnownAliasesCount++;
640 break;
641 }
642 else if (knownAliases[idx] != num
643 && ucnv_compareNames(alias, GET_ALIAS_STR(knownAliases[idx])) == 0)
644 {
645 if (verbose) {
646 fprintf(stderr, "%s:%d: information: duplicate alias %s and %s found\n", path,
647 lineNum, alias, GET_ALIAS_STR(knownAliases[idx]));
648 }
649 duplicateKnownAliasesCount++;
650 break;
651 }
652 }
653 */
654 if (knownAliasesCount >= MAX_ALIAS_COUNT) {
655 fprintf(stderr, "%s:%d: warning: Too many aliases defined for all converters\n",
656 path, lineNum);
657 exit(U_BUFFER_OVERFLOW_ERROR);
658 }
659 /* TODO: We could try to unlist exact duplicates. */
660 return knownAliases[knownAliasesCount++] = GET_ALIAS_NUM(alias);
661 }
662
663 /*
664 @param standard When standard is 0, then it's the "empty" tag.
665 */
666 static uint16_t
addAlias(const char * alias,uint16_t standard,uint16_t converter,UBool defaultName)667 addAlias(const char *alias, uint16_t standard, uint16_t converter, UBool defaultName) {
668 uint32_t idx, idx2;
669 UBool startEmptyWithoutDefault = false;
670 AliasList *aliasList;
671
672 if(standard>=MAX_TAG_COUNT) {
673 fprintf(stderr, "%s:%d: error: too many standard tags\n", path, lineNum);
674 exit(U_BUFFER_OVERFLOW_ERROR);
675 }
676 if(converter>=MAX_CONV_COUNT) {
677 fprintf(stderr, "%s:%d: error: too many converter names\n", path, lineNum);
678 exit(U_BUFFER_OVERFLOW_ERROR);
679 }
680 aliasList = &tags[standard].aliasList[converter];
681
682 if (strchr(alias, '}')) {
683 fprintf(stderr, "%s:%d: error: unmatched } found\n", path,
684 lineNum);
685 }
686
687 if(aliasList->aliasCount + 1 >= MAX_TC_ALIAS_COUNT) {
688 fprintf(stderr, "%s:%d: error: too many aliases for alias %s and converter %s\n", path,
689 lineNum, alias, GET_ALIAS_STR(converters[converter].converter));
690 exit(U_BUFFER_OVERFLOW_ERROR);
691 }
692
693 /* Show this warning only once. All aliases are added to the "ALL" tag. */
694 if (standard == ALL_TAG_NUM && GET_ALIAS_STR(converters[converter].converter) != alias) {
695 /* Normally these option values are parsed at runtime, and they can
696 be discarded when the alias is a default converter. Options should
697 only be on a converter and not an alias. */
698 if (uprv_strchr(alias, UCNV_OPTION_SEP_CHAR) != 0)
699 {
700 fprintf(stderr, "warning(line %d): alias %s contains a \""UCNV_OPTION_SEP_STRING"\". Options are parsed at run-time and do not need to be in the alias table.\n",
701 lineNum, alias);
702 }
703 if (uprv_strchr(alias, UCNV_VALUE_SEP_CHAR) != 0)
704 {
705 fprintf(stderr, "warning(line %d): alias %s contains an \""UCNV_VALUE_SEP_STRING"\". Options are parsed at run-time and do not need to be in the alias table.\n",
706 lineNum, alias);
707 }
708 }
709
710 if (standard != ALL_TAG_NUM) {
711 /* Check for duplicate aliases for this tag on all converters */
712 for (idx = 0; idx < converterCount; idx++) {
713 for (idx2 = 0; idx2 < tags[standard].aliasList[idx].aliasCount; idx2++) {
714 uint16_t aliasNum = tags[standard].aliasList[idx].aliases[idx2];
715 if (aliasNum
716 && ucnv_compareNames(alias, GET_ALIAS_STR(aliasNum)) == 0)
717 {
718 if (idx == converter) {
719 /*
720 * (alias, standard) duplicates are harmless if they map to the same converter.
721 * Only print a warning in verbose mode, or if the alias is a precise duplicate,
722 * not just a lenient-match duplicate.
723 */
724 if (verbose || 0 == uprv_strcmp(alias, GET_ALIAS_STR(aliasNum))) {
725 fprintf(stderr, "%s:%d: warning: duplicate aliases %s and %s found for standard %s and converter %s\n", path,
726 lineNum, alias, GET_ALIAS_STR(aliasNum),
727 GET_TAG_STR(tags[standard].tag),
728 GET_ALIAS_STR(converters[converter].converter));
729 }
730 } else {
731 fprintf(stderr, "%s:%d: warning: duplicate aliases %s and %s found for standard tag %s between converter %s and converter %s\n", path,
732 lineNum, alias, GET_ALIAS_STR(aliasNum),
733 GET_TAG_STR(tags[standard].tag),
734 GET_ALIAS_STR(converters[converter].converter),
735 GET_ALIAS_STR(converters[idx].converter));
736 }
737 break;
738 }
739 }
740 }
741
742 /* Check for duplicate default aliases for this converter on all tags */
743 /* It's okay to have multiple standards prefer the same name */
744 /* if (verbose && !dupFound) {
745 for (idx = 0; idx < tagCount; idx++) {
746 if (tags[idx].aliasList[converter].aliases) {
747 uint16_t aliasNum = tags[idx].aliasList[converter].aliases[0];
748 if (aliasNum
749 && ucnv_compareNames(alias, GET_ALIAS_STR(aliasNum)) == 0)
750 {
751 fprintf(stderr, "%s:%d: warning: duplicate alias %s found for converter %s and standard tag %s\n", path,
752 lineNum, alias, GET_ALIAS_STR(converters[converter].converter), GET_TAG_STR(tags[standard].tag));
753 break;
754 }
755 }
756 }
757 }*/
758 }
759
760 if (aliasList->aliasCount <= 0) {
761 aliasList->aliasCount++;
762 startEmptyWithoutDefault = true;
763 }
764 aliasList->aliases = (uint16_t *)uprv_realloc(aliasList->aliases, (aliasList->aliasCount + 1) * sizeof(aliasList->aliases[0]));
765 if (startEmptyWithoutDefault) {
766 aliasList->aliases[0] = 0;
767 }
768 if (defaultName) {
769 if (aliasList->aliases[0] != 0) {
770 fprintf(stderr, "%s:%d: error: Alias %s and %s cannot both be the default alias for standard tag %s and converter %s\n", path,
771 lineNum,
772 alias,
773 GET_ALIAS_STR(aliasList->aliases[0]),
774 GET_TAG_STR(tags[standard].tag),
775 GET_ALIAS_STR(converters[converter].converter));
776 exit(U_PARSE_ERROR);
777 }
778 aliasList->aliases[0] = GET_ALIAS_NUM(alias);
779 } else {
780 aliasList->aliases[aliasList->aliasCount++] = GET_ALIAS_NUM(alias);
781 }
782 /* aliasList->converter = converter;*/
783
784 converters[converter].totalAliasCount++; /* One more to the column */
785 tags[standard].totalAliasCount++; /* One more to the row */
786
787 return aliasList->aliasCount;
788 }
789
790 static uint16_t
addConverter(const char * converter)791 addConverter(const char *converter) {
792 uint32_t idx;
793 if(converterCount>=MAX_CONV_COUNT) {
794 fprintf(stderr, "%s:%d: error: too many converters\n", path, lineNum);
795 exit(U_BUFFER_OVERFLOW_ERROR);
796 }
797
798 for (idx = 0; idx < converterCount; idx++) {
799 if (ucnv_compareNames(converter, GET_ALIAS_STR(converters[idx].converter)) == 0) {
800 fprintf(stderr, "%s:%d: error: duplicate converter %s found!\n", path, lineNum, converter);
801 exit(U_PARSE_ERROR);
802 break;
803 }
804 }
805
806 converters[converterCount].converter = GET_ALIAS_NUM(converter);
807 converters[converterCount].totalAliasCount = 0;
808
809 return converterCount++;
810 }
811
812 /* resolve this alias based on the prioritization of the standard tags. */
813 static void
resolveAliasToConverter(uint16_t alias,uint16_t * tagNum,uint16_t * converterNum)814 resolveAliasToConverter(uint16_t alias, uint16_t *tagNum, uint16_t *converterNum) {
815 uint16_t idx, idx2, idx3;
816
817 for (idx = UCNV_NUM_RESERVED_TAGS; idx < tagCount; idx++) {
818 for (idx2 = 0; idx2 < converterCount; idx2++) {
819 for (idx3 = 0; idx3 < tags[idx].aliasList[idx2].aliasCount; idx3++) {
820 uint16_t aliasNum = tags[idx].aliasList[idx2].aliases[idx3];
821 if (aliasNum == alias) {
822 *tagNum = idx;
823 *converterNum = idx2;
824 return;
825 }
826 }
827 }
828 }
829 /* Do the leftovers last, just in case */
830 /* There is no need to do the ALL tag */
831 idx = 0;
832 for (idx2 = 0; idx2 < converterCount; idx2++) {
833 for (idx3 = 0; idx3 < tags[idx].aliasList[idx2].aliasCount; idx3++) {
834 uint16_t aliasNum = tags[idx].aliasList[idx2].aliases[idx3];
835 if (aliasNum == alias) {
836 *tagNum = idx;
837 *converterNum = idx2;
838 return;
839 }
840 }
841 }
842 *tagNum = UINT16_MAX;
843 *converterNum = UINT16_MAX;
844 fprintf(stderr, "%s: warning: alias %s not found\n",
845 path,
846 GET_ALIAS_STR(alias));
847 return;
848 }
849
850 /* The knownAliases should be sorted before calling this function */
851 static uint32_t
resolveAliases(uint16_t * uniqueAliasArr,uint16_t * uniqueAliasToConverterArr,uint16_t aliasOffset)852 resolveAliases(uint16_t *uniqueAliasArr, uint16_t *uniqueAliasToConverterArr, uint16_t aliasOffset) {
853 uint32_t uniqueAliasIdx = 0;
854 uint32_t idx;
855 uint16_t currTagNum, oldTagNum;
856 uint16_t currConvNum, oldConvNum;
857 const char *lastName;
858
859 if (knownAliasesCount != 0) {
860 resolveAliasToConverter(knownAliases[0], &oldTagNum, &currConvNum);
861 uniqueAliasToConverterArr[uniqueAliasIdx] = currConvNum;
862 oldConvNum = currConvNum;
863 uniqueAliasArr[uniqueAliasIdx] = knownAliases[0] + aliasOffset;
864 uniqueAliasIdx++;
865 lastName = GET_ALIAS_STR(knownAliases[0]);
866
867 for (idx = 1; idx < knownAliasesCount; idx++) {
868 resolveAliasToConverter(knownAliases[idx], &currTagNum, &currConvNum);
869 if (ucnv_compareNames(lastName, GET_ALIAS_STR(knownAliases[idx])) == 0) {
870 /* duplicate found */
871 if ((currTagNum < oldTagNum && currTagNum >= UCNV_NUM_RESERVED_TAGS)
872 || oldTagNum == 0) {
873 oldTagNum = currTagNum;
874 uniqueAliasToConverterArr[uniqueAliasIdx - 1] = currConvNum;
875 uniqueAliasArr[uniqueAliasIdx - 1] = knownAliases[idx] + aliasOffset;
876 if (verbose) {
877 printf("using %s instead of %s -> %s",
878 GET_ALIAS_STR(knownAliases[idx]),
879 lastName,
880 GET_ALIAS_STR(converters[currConvNum].converter));
881 if (oldConvNum != currConvNum) {
882 printf(" (alias conflict)");
883 }
884 puts("");
885 }
886 }
887 else {
888 /* else ignore it */
889 if (verbose) {
890 printf("folding %s into %s -> %s",
891 GET_ALIAS_STR(knownAliases[idx]),
892 lastName,
893 GET_ALIAS_STR(converters[oldConvNum].converter));
894 if (oldConvNum != currConvNum) {
895 printf(" (alias conflict)");
896 }
897 puts("");
898 }
899 }
900 if (oldConvNum != currConvNum) {
901 uniqueAliasToConverterArr[uniqueAliasIdx - 1] |= UCNV_AMBIGUOUS_ALIAS_MAP_BIT;
902 }
903 }
904 else {
905 uniqueAliasToConverterArr[uniqueAliasIdx] = currConvNum;
906 oldConvNum = currConvNum;
907 uniqueAliasArr[uniqueAliasIdx] = knownAliases[idx] + aliasOffset;
908 uniqueAliasIdx++;
909 lastName = GET_ALIAS_STR(knownAliases[idx]);
910 oldTagNum = currTagNum;
911 /*printf("%s -> %s\n", GET_ALIAS_STR(knownAliases[idx]), GET_ALIAS_STR(converters[currConvNum].converter));*/
912 }
913 if (uprv_strchr(GET_ALIAS_STR(converters[currConvNum].converter), UCNV_OPTION_SEP_CHAR) != NULL) {
914 uniqueAliasToConverterArr[uniqueAliasIdx-1] |= UCNV_CONTAINS_OPTION_BIT;
915 }
916 }
917 }
918 return uniqueAliasIdx;
919 }
920
921 static void
createOneAliasList(uint16_t * aliasArrLists,uint32_t tag,uint32_t converter,uint16_t offset)922 createOneAliasList(uint16_t *aliasArrLists, uint32_t tag, uint32_t converter, uint16_t offset) {
923 uint32_t aliasNum;
924 AliasList *aliasList = &tags[tag].aliasList[converter];
925
926 if (aliasList->aliasCount == 0) {
927 aliasArrLists[tag*converterCount + converter] = 0;
928 }
929 else {
930 aliasLists[aliasListsSize++] = aliasList->aliasCount;
931
932 /* write into the array area a 1's based index. */
933 aliasArrLists[tag*converterCount + converter] = aliasListsSize;
934
935 /* printf("tag %s converter %s\n",
936 GET_TAG_STR(tags[tag].tag),
937 GET_ALIAS_STR(converters[converter].converter));*/
938 for (aliasNum = 0; aliasNum < aliasList->aliasCount; aliasNum++) {
939 uint16_t value;
940 /* printf(" %s\n",
941 GET_ALIAS_STR(aliasList->aliases[aliasNum]));*/
942 if (aliasList->aliases[aliasNum]) {
943 value = aliasList->aliases[aliasNum] + offset;
944 } else {
945 value = 0;
946 if (tag != 0 && !quiet) { /* Only show the warning when it's not the leftover tag. */
947 fprintf(stderr, "%s: warning: tag %s does not have a default alias for %s\n",
948 path,
949 GET_TAG_STR(tags[tag].tag),
950 GET_ALIAS_STR(converters[converter].converter));
951 }
952 }
953 aliasLists[aliasListsSize++] = value;
954 if (aliasListsSize >= MAX_LIST_SIZE) {
955 fprintf(stderr, "%s: error: Too many alias lists\n", path);
956 exit(U_BUFFER_OVERFLOW_ERROR);
957 }
958
959 }
960 }
961 }
962
963 static void
createNormalizedAliasStrings(char * normalizedStrings,const char * origStringBlock,int32_t stringBlockLength)964 createNormalizedAliasStrings(char *normalizedStrings, const char *origStringBlock, int32_t stringBlockLength) {
965 int32_t currStrLen;
966 uprv_memcpy(normalizedStrings, origStringBlock, stringBlockLength);
967 while ((currStrLen = (int32_t)uprv_strlen(origStringBlock)) < stringBlockLength) {
968 int32_t currStrSize = currStrLen + 1;
969 if (currStrLen > 0) {
970 int32_t normStrLen;
971 ucnv_io_stripForCompare(normalizedStrings, origStringBlock);
972 normStrLen = (int32_t)uprv_strlen(normalizedStrings);
973 if (normStrLen > 0) {
974 uprv_memset(normalizedStrings + normStrLen, 0, currStrSize - normStrLen);
975 }
976 }
977 stringBlockLength -= currStrSize;
978 normalizedStrings += currStrSize;
979 origStringBlock += currStrSize;
980 }
981 }
982
983 static void
writeAliasTable(UNewDataMemory * out)984 writeAliasTable(UNewDataMemory *out) {
985 uint32_t i, j;
986 uint32_t uniqueAliasesSize;
987 uint16_t aliasOffset = (uint16_t)(tagBlock.top/sizeof(uint16_t));
988 uint16_t *aliasArrLists = (uint16_t *)uprv_malloc(tagCount * converterCount * sizeof(uint16_t));
989 uint16_t *uniqueAliases = (uint16_t *)uprv_malloc(knownAliasesCount * sizeof(uint16_t));
990 uint16_t *uniqueAliasesToConverter = (uint16_t *)uprv_malloc(knownAliasesCount * sizeof(uint16_t));
991
992 qsort(knownAliases, knownAliasesCount, sizeof(knownAliases[0]), compareAliases);
993 uniqueAliasesSize = resolveAliases(uniqueAliases, uniqueAliasesToConverter, aliasOffset);
994
995 /* Array index starts at 1. aliasLists[0] is the size of the lists section. */
996 aliasListsSize = 0;
997
998 /* write the offsets of all the aliases lists in a 2D array, and create the lists. */
999 for (i = 0; i < tagCount; ++i) {
1000 for (j = 0; j < converterCount; ++j) {
1001 createOneAliasList(aliasArrLists, i, j, aliasOffset);
1002 }
1003 }
1004
1005 /* Write the size of the TOC */
1006 if (tableOptions.stringNormalizationType == UCNV_IO_UNNORMALIZED) {
1007 udata_write32(out, 8);
1008 }
1009 else {
1010 udata_write32(out, 9);
1011 }
1012
1013 /* Write the sizes of each section */
1014 /* All sizes are the number of uint16_t units, not bytes */
1015 udata_write32(out, converterCount);
1016 udata_write32(out, tagCount);
1017 udata_write32(out, uniqueAliasesSize); /* list of aliases */
1018 udata_write32(out, uniqueAliasesSize); /* The preresolved form of mapping an untagged the alias to a converter */
1019 udata_write32(out, tagCount * converterCount);
1020 udata_write32(out, aliasListsSize + 1);
1021 udata_write32(out, sizeof(tableOptions) / sizeof(uint16_t));
1022 udata_write32(out, (tagBlock.top + stringBlock.top) / sizeof(uint16_t));
1023 if (tableOptions.stringNormalizationType != UCNV_IO_UNNORMALIZED) {
1024 udata_write32(out, (tagBlock.top + stringBlock.top) / sizeof(uint16_t));
1025 }
1026
1027 /* write the table of converters */
1028 /* Think of this as the column headers */
1029 for(i=0; i<converterCount; ++i) {
1030 udata_write16(out, (uint16_t)(converters[i].converter + aliasOffset));
1031 }
1032
1033 /* write the table of tags */
1034 /* Think of this as the row headers */
1035 for(i=UCNV_NUM_RESERVED_TAGS; i<tagCount; ++i) {
1036 udata_write16(out, tags[i].tag);
1037 }
1038 /* The empty tag is considered the leftover list, and put that at the end of the priority list. */
1039 udata_write16(out, tags[EMPTY_TAG_NUM].tag);
1040 udata_write16(out, tags[ALL_TAG_NUM].tag);
1041
1042 /* Write the unique list of aliases */
1043 udata_writeBlock(out, uniqueAliases, uniqueAliasesSize * sizeof(uint16_t));
1044
1045 /* Write the unique list of aliases */
1046 udata_writeBlock(out, uniqueAliasesToConverter, uniqueAliasesSize * sizeof(uint16_t));
1047
1048 /* Write the array to the lists */
1049 udata_writeBlock(out, (const void *)(aliasArrLists + (2*converterCount)), (((tagCount - 2) * converterCount) * sizeof(uint16_t)));
1050 /* Now write the leftover part of the array for the EMPTY and ALL lists */
1051 udata_writeBlock(out, (const void *)aliasArrLists, (2 * converterCount * sizeof(uint16_t)));
1052
1053 /* Offset the next array to make the index start at 1. */
1054 udata_write16(out, 0xDEAD);
1055
1056 /* Write the lists */
1057 udata_writeBlock(out, (const void *)aliasLists, aliasListsSize * sizeof(uint16_t));
1058
1059 /* Write any options for the alias table. */
1060 udata_writeBlock(out, (const void *)&tableOptions, sizeof(tableOptions));
1061
1062 /* write the tags strings */
1063 udata_writeString(out, tagBlock.store, tagBlock.top);
1064
1065 /* write the aliases strings */
1066 udata_writeString(out, stringBlock.store, stringBlock.top);
1067
1068 /* write the normalized aliases strings */
1069 if (tableOptions.stringNormalizationType != UCNV_IO_UNNORMALIZED) {
1070 char *normalizedStrings = (char *)uprv_malloc(tagBlock.top + stringBlock.top);
1071 createNormalizedAliasStrings(normalizedStrings, tagBlock.store, tagBlock.top);
1072 createNormalizedAliasStrings(normalizedStrings + tagBlock.top, stringBlock.store, stringBlock.top);
1073
1074 /* Write out the complete normalized array. */
1075 udata_writeString(out, normalizedStrings, tagBlock.top + stringBlock.top);
1076 uprv_free(normalizedStrings);
1077 }
1078
1079 uprv_free(uniqueAliasesToConverter);
1080 uprv_free(uniqueAliases);
1081 uprv_free(aliasArrLists);
1082 }
1083
1084 static char *
allocString(StringBlock * block,const char * s,int32_t length)1085 allocString(StringBlock *block, const char *s, int32_t length) {
1086 uint32_t top;
1087 char *p;
1088
1089 if(length<0) {
1090 length=(int32_t)uprv_strlen(s);
1091 }
1092
1093 /*
1094 * add 1 for the terminating NUL
1095 * and round up (+1 &~1)
1096 * to keep the addresses on a 16-bit boundary
1097 */
1098 top=block->top + (uint32_t)((length + 1 + 1) & ~1);
1099
1100 if(top >= block->max) {
1101 fprintf(stderr, "%s:%d: error: out of memory\n", path, lineNum);
1102 exit(U_MEMORY_ALLOCATION_ERROR);
1103 }
1104
1105 /* get the pointer and copy the string */
1106 p = block->store + block->top;
1107 uprv_memcpy(p, s, length);
1108 p[length] = 0; /* NUL-terminate it */
1109 if((length & 1) == 0) {
1110 p[length + 1] = 0; /* set the padding byte */
1111 }
1112
1113 /* check for invariant characters now that we have a NUL-terminated string for easy output */
1114 if(!uprv_isInvariantString(p, length)) {
1115 fprintf(stderr, "%s:%d: error: the name %s contains not just invariant characters\n", path, lineNum, p);
1116 exit(U_INVALID_TABLE_FORMAT);
1117 }
1118
1119 block->top = top;
1120 return p;
1121 }
1122
1123 static int
compareAliases(const void * alias1,const void * alias2)1124 compareAliases(const void *alias1, const void *alias2) {
1125 /* Names like IBM850 and ibm-850 need to be sorted together */
1126 int result = ucnv_compareNames(GET_ALIAS_STR(*(uint16_t*)alias1), GET_ALIAS_STR(*(uint16_t*)alias2));
1127 if (!result) {
1128 /* Sort the shortest first */
1129 return (int)uprv_strlen(GET_ALIAS_STR(*(uint16_t*)alias1)) - (int)uprv_strlen(GET_ALIAS_STR(*(uint16_t*)alias2));
1130 }
1131 return result;
1132 }
1133
1134 /*
1135 * Hey, Emacs, please set the following:
1136 *
1137 * Local Variables:
1138 * indent-tabs-mode: nil
1139 * End:
1140 *
1141 */
1142
1143