1 // Copyright (C) 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 **********************************************************************
5 * Copyright (C) 2009-2016, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 **********************************************************************
12 //--------------------------------------------------------------------
14 // Tool for generating Unicode Confusable data files (.cfu files).
15 // .cfu files contain the compiled of the confusable data
16 // derived from the Unicode Consortium data described in
19 // Usage: gencfu [options] -r confusables-file.txt -o output-file.cfu
21 // options: -v verbose
24 // The input rule filew is are plain text files containing confusable character
25 // definitions in the input format defined by Unicode UAX39 for the files
26 // confusables.txt. This source (.txt) format
27 // is also accepted direaccepted by ICU spoof detedtors. The
28 // files must be encoded in utf-8 format, with or without a BOM.
30 // The script used to compile confusablesWholeScript.txt into the CFU file
31 // until the Unicode consortium deprecated it.
33 //--------------------------------------------------------------------
35 #include "unicode/utypes.h"
36 #include "unicode/unistr.h"
37 #include "unicode/uclean.h"
38 #include "unicode/udata.h"
39 #include "unicode/putil.h"
44 #include "uspoof_impl.h"
53 static char *progName;
54 static UOption options[]={
55 UOPTION_HELP_H, /* 0 */
56 UOPTION_HELP_QUESTION_MARK, /* 1 */
57 UOPTION_VERBOSE, /* 2 */
58 { "rules", NULL, NULL, NULL, 'r', UOPT_REQUIRES_ARG, 0 }, /* 3 */
59 { "wsrules", NULL, NULL, NULL, 'w', UOPT_REQUIRES_ARG, 0}, /* 4 */ // deprecated
60 { "out", NULL, NULL, NULL, 'o', UOPT_REQUIRES_ARG, 0 }, /* 5 */
61 UOPTION_ICUDATADIR, /* 6 */
62 UOPTION_DESTDIR, /* 7 */
63 UOPTION_COPYRIGHT, /* 8 */
64 UOPTION_QUIET, /* 9 */
67 void usageAndDie(int retCode) {
68 printf("Usage: %s [-v] [-options] -r confusablesRules.txt -o output-file\n", progName);
69 printf("\tRead in Unicode confusable character definitions and write out the binary data\n"
71 "\t-h or -? or --help this usage text\n"
72 "\t-V or --version show a version message\n"
73 "\t-c or --copyright include a copyright notice\n"
74 "\t-v or --verbose turn on verbose output\n"
75 "\t-q or --quiet do not display warnings and progress\n"
76 "\t-i or --icudatadir directory for locating any needed intermediate data files,\n"
77 "\t followed by path, defaults to %s\n"
78 "\t-d or --destdir destination directory, followed by the path\n",
79 u_getDataDirectory());
84 #if UCONFIG_NO_REGULAR_EXPRESSIONS || UCONFIG_NO_NORMALIZATION || UCONFIG_NO_FILE_IO
86 /* dummy UDataInfo cf. udata.h */
87 static UDataInfo dummyDataInfo = {
96 { 0, 0, 0, 0 }, /* dummy dataFormat */
97 { 0, 0, 0, 0 }, /* dummy formatVersion */
98 { 0, 0, 0, 0 } /* dummy dataVersion */
104 // Set up the ICU data header, defined in ucmndata.h
107 {sizeof(DataHeader), // Struct MappedData
111 { // struct UDataInfo
112 sizeof(UDataInfo), // size
119 { 0x43, 0x66, 0x75, 0x20 }, // dataFormat="Cfu "
120 { 0xff, 0, 0, 0 }, // formatVersion. Filled in later with values
121 // from the builder. The values declared
122 // here should never appear in any real data.
123 { 5, 1, 0, 0 } // dataVersion (Unicode version)
128 // Forward declaration for function for reading source files.
129 static const char *readFile(const char *fileName, int32_t *len);
131 //----------------------------------------------------------------------------
135 //----------------------------------------------------------------------------
136 int main(int argc, char **argv) {
137 UErrorCode status = U_ZERO_ERROR;
138 const char *confFileName;
139 const char *outFileName;
140 const char *outDir = NULL;
141 const char *copyright = NULL;
144 // Pick up and check the command line arguments,
145 // using the standard ICU tool utils option handling.
147 U_MAIN_INIT_ARGS(argc, argv);
149 argc=u_parseArgs(argc, argv, UPRV_LENGTHOF(options), options);
151 // Unrecognized option
152 fprintf(stderr, "error in command line argument \"%s\"\n", argv[-argc]);
153 usageAndDie(U_ILLEGAL_ARGUMENT_ERROR);
156 if(options[0].doesOccur || options[1].doesOccur) {
157 // -? or -h for help.
161 if (!(options[3].doesOccur && options[5].doesOccur)) {
162 fprintf(stderr, "confusables file and output file must all be specified.\n");
163 usageAndDie(U_ILLEGAL_ARGUMENT_ERROR);
165 confFileName = options[3].value;
166 outFileName = options[5].value;
168 if (options[6].doesOccur) {
169 u_setDataDirectory(options[6].value);
172 status = U_ZERO_ERROR;
174 /* Combine the directory with the file name */
175 if(options[7].doesOccur) {
176 outDir = options[7].value;
178 if (options[8].doesOccur) {
179 copyright = U_COPYRIGHT_STRING;
183 if (options[9].doesOccur) {
187 #if UCONFIG_NO_REGULAR_EXPRESSIONS || UCONFIG_NO_NORMALIZATION || UCONFIG_NO_FILE_IO
188 // spoof detection data file parsing is dependent on regular expressions.
189 // TODO: have the tool return an error status. Requires fixing the ICU data build
190 // so that it doesn't abort entirely on that error.
192 UNewDataMemory *pData;
195 /* write message with just the name */
196 sprintf(msg, "gencfu writes dummy %s because of UCONFIG_NO_REGULAR_EXPRESSIONS and/or UCONFIG_NO_NORMALIZATION and/or UCONFIG_NO_FILE_IO, see uconfig.h", outFileName);
197 fprintf(stderr, "%s\n", msg);
199 /* write the dummy data file */
200 pData = udata_create(outDir, NULL, outFileName, &dummyDataInfo, NULL, &status);
201 udata_writeBlock(pData, msg, strlen(msg));
202 udata_finish(pData, &status);
208 if (U_FAILURE(status)) {
209 fprintf(stderr, "%s: can not initialize ICU. status = %s\n",
210 argv[0], u_errorName(status));
213 status = U_ZERO_ERROR;
215 // Read in the confusables source file
217 int32_t confusablesLen = 0;
218 const char *confusables = readFile(confFileName, &confusablesLen);
219 if (confusables == NULL) {
220 printf("gencfu: error reading file \"%s\"\n", confFileName);
225 // Create the Spoof Detector from the source confusables files.
226 // This will compile the data.
228 UParseError parseError;
230 parseError.offset = 0;
232 USpoofChecker *sc = uspoof_openFromSource(confusables, confusablesLen,
234 &errType, &parseError, &status);
235 if (U_FAILURE(status)) {
236 fprintf(stderr, "gencfu: uspoof_openFromSource error \"%s\" at file %s, line %d, column %d\n",
237 u_errorName(status), confFileName, (int)parseError.line, (int)parseError.offset);
243 // Get the compiled rule data from the USpoofChecker.
245 uint32_t outDataSize;
247 outDataSize = uspoof_serialize(sc, NULL, 0, &status);
248 if (status != U_BUFFER_OVERFLOW_ERROR) {
249 fprintf(stderr, "gencfu: uspoof_serialize() returned %s\n", u_errorName(status));
252 status = U_ZERO_ERROR;
253 outData = new uint8_t[outDataSize];
254 uspoof_serialize(sc, outData, outDataSize, &status);
256 // Copy the data format version numbers from the spoof data header into the UDataMemory header.
258 uprv_memcpy(dh.info.formatVersion,
259 reinterpret_cast<SpoofDataHeader *>(outData)->fFormatVersion,
260 sizeof(dh.info.formatVersion));
263 // Create the output file
266 UNewDataMemory *pData;
267 pData = udata_create(outDir, NULL, outFileName, &(dh.info), copyright, &status);
268 if(U_FAILURE(status)) {
269 fprintf(stderr, "gencfu: Could not open output file \"%s\", \"%s\"\n",
270 outFileName, u_errorName(status));
275 // Write the data itself.
276 udata_writeBlock(pData, outData, outDataSize);
278 bytesWritten = udata_finish(pData, &status);
279 if(U_FAILURE(status)) {
280 fprintf(stderr, "gencfu: Error %d writing the output file\n", status);
284 if (bytesWritten != outDataSize) {
285 fprintf(stderr, "gencfu: Error writing to output file \"%s\"\n", outFileName);
291 delete [] confusables;
294 printf("gencfu: tool completed successfully.\n");
297 #endif // UCONFIG_NO_REGULAR_EXPRESSIONS
302 // Read in a confusables source file
304 static const char *readFile(const char *fileName, int32_t *len) {
309 file = fopen(fileName, "rb");
313 fseek(file, 0, SEEK_END);
314 fileSize = ftell(file);
315 fseek(file, 0, SEEK_SET);
316 result = new char[fileSize+10];
322 long t = fread(result, 1, fileSize, file);
329 *len = static_cast<int32_t>(fileSize);