2 **********************************************************************
3 * Copyright (C) 2009-2010, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
10 //--------------------------------------------------------------------
12 // Tool for generating Unicode Confusable data files (.cfu files).
13 // .cfu files contain the compiled of the confusable data
14 // derived from the Unicode Consortium data described in
17 // Usage: gencfu [options] -r confusables-file.txt -w whole-script-confusables.txt -o output-file.cfu
19 // options: -v verbose
22 // The input rule filew is are plain text files containing confusable character
23 // definitions in the input format defined by Unicode UAX39 for the files
24 // confusables.txt and confusablesWholeScript.txt. This source (.txt) format
25 // is also accepted direaccepted by ICU spoof detedtors. The
26 // files must be encoded in utf-8 format, with or without a BOM.
28 //--------------------------------------------------------------------
30 #include "unicode/utypes.h"
31 #include "unicode/unistr.h"
32 #include "unicode/uclean.h"
33 #include "unicode/udata.h"
34 #include "unicode/putil.h"
39 #include "uspoof_impl.h"
48 static char *progName
;
49 static UOption options
[]={
50 UOPTION_HELP_H
, /* 0 */
51 UOPTION_HELP_QUESTION_MARK
, /* 1 */
52 UOPTION_VERBOSE
, /* 2 */
53 { "rules", NULL
, NULL
, NULL
, 'r', UOPT_REQUIRES_ARG
, 0 }, /* 3 */
54 { "wsrules", NULL
, NULL
, NULL
, 'w', UOPT_REQUIRES_ARG
, 0}, /* 4 */
55 { "out", NULL
, NULL
, NULL
, 'o', UOPT_REQUIRES_ARG
, 0 }, /* 5 */
56 UOPTION_ICUDATADIR
, /* 6 */
57 UOPTION_DESTDIR
, /* 7 */
58 UOPTION_COPYRIGHT
, /* 8 */
61 void usageAndDie(int retCode
) {
62 printf("Usage: %s [-v] [-options] -r confusablesRules.txt -w wholeScriptConfusables.txt -o output-file\n", progName
);
63 printf("\tRead in Unicode confusable character definitions and write out the binary data\n"
65 "\t-h or -? or --help this usage text\n"
66 "\t-V or --version show a version message\n"
67 "\t-c or --copyright include a copyright notice\n"
68 "\t-v or --verbose turn on verbose output\n"
69 "\t-i or --icudatadir directory for locating any needed intermediate data files,\n"
70 "\t followed by path, defaults to %s\n"
71 "\t-d or --destdir destination directory, followed by the path\n",
72 u_getDataDirectory());
77 #if UCONFIG_NO_REGULAR_EXPRESSIONS || UCONFIG_NO_NORMALIZATION || UCONFIG_NO_FILE_IO
79 /* dummy UDataInfo cf. udata.h */
80 static UDataInfo dummyDataInfo
= {
89 { 0, 0, 0, 0 }, /* dummy dataFormat */
90 { 0, 0, 0, 0 }, /* dummy formatVersion */
91 { 0, 0, 0, 0 } /* dummy dataVersion */
97 // Set up the ICU data header, defined in ucmndata.h
100 {sizeof(DataHeader
), // Struct MappedData
104 { // struct UDataInfo
105 sizeof(UDataInfo
), // size
112 { 0x43, 0x66, 0x75, 0x20 }, // dataFormat="Cfu "
113 { 0xff, 0, 0, 0 }, // formatVersion. Filled in later with values
114 // from the builder. The values declared
115 // here should never appear in any real data.
116 { 5, 1, 0, 0 } // dataVersion (Unicode version)
121 // Forward declaration for function for reading source files.
122 static const char *readFile(const char *fileName
, int32_t *len
);
124 //----------------------------------------------------------------------------
128 //----------------------------------------------------------------------------
129 int main(int argc
, char **argv
) {
130 UErrorCode status
= U_ZERO_ERROR
;
131 const char *confFileName
;
132 const char *confWSFileName
;
133 const char *outFileName
;
134 const char *outDir
= NULL
;
135 const char *copyright
= NULL
;
138 // Pick up and check the command line arguments,
139 // using the standard ICU tool utils option handling.
141 U_MAIN_INIT_ARGS(argc
, argv
);
143 argc
=u_parseArgs(argc
, argv
, sizeof(options
)/sizeof(options
[0]), options
);
145 // Unrecognized option
146 fprintf(stderr
, "error in command line argument \"%s\"\n", argv
[-argc
]);
147 usageAndDie(U_ILLEGAL_ARGUMENT_ERROR
);
150 if(options
[0].doesOccur
|| options
[1].doesOccur
) {
151 // -? or -h for help.
155 if (!(options
[3].doesOccur
&& options
[4].doesOccur
&& options
[5].doesOccur
)) {
156 fprintf(stderr
, "confusables file, whole script confusables file and output file must all be specified.\n");
157 usageAndDie(U_ILLEGAL_ARGUMENT_ERROR
);
159 confFileName
= options
[3].value
;
160 confWSFileName
= options
[4].value
;
161 outFileName
= options
[5].value
;
163 if (options
[6].doesOccur
) {
164 u_setDataDirectory(options
[6].value
);
167 status
= U_ZERO_ERROR
;
169 /* Combine the directory with the file name */
170 if(options
[7].doesOccur
) {
171 outDir
= options
[7].value
;
173 if (options
[8].doesOccur
) {
174 copyright
= U_COPYRIGHT_STRING
;
177 #if UCONFIG_NO_REGULAR_EXPRESSIONS || UCONFIG_NO_NORMALIZATION || UCONFIG_NO_FILE_IO
178 // spoof detection data file parsing is dependent on regular expressions.
179 // TODO: have the tool return an error status. Requires fixing the ICU data build
180 // so that it doesn't abort entirely on that error.
182 UNewDataMemory
*pData
;
185 /* write message with just the name */
186 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
);
187 fprintf(stderr
, "%s\n", msg
);
189 /* write the dummy data file */
190 pData
= udata_create(outDir
, NULL
, outFileName
, &dummyDataInfo
, NULL
, &status
);
191 udata_writeBlock(pData
, msg
, strlen(msg
));
192 udata_finish(pData
, &status
);
198 if (U_FAILURE(status
)) {
199 fprintf(stderr
, "%s: can not initialize ICU. status = %s\n",
200 argv
[0], u_errorName(status
));
203 status
= U_ZERO_ERROR
;
205 // Read in the confusables source file
207 int32_t confusablesLen
= 0;
208 const char *confusables
= readFile(confFileName
, &confusablesLen
);
209 if (confusables
== NULL
) {
210 printf("gencfu: error reading file \"%s\"\n", confFileName
);
214 int32_t wsConfusablesLen
= 0;
215 const char *wsConfsables
= readFile(confWSFileName
, &wsConfusablesLen
);
216 if (wsConfsables
== NULL
) {
217 printf("gencfu: error reading file \"%s\"\n", confFileName
);
222 // Create the Spoof Detector from the source confusables files.
223 // This will compile the data.
225 UParseError parseError
;
227 parseError
.offset
= 0;
229 USpoofChecker
*sc
= uspoof_openFromSource(confusables
, confusablesLen
,
230 wsConfsables
, wsConfusablesLen
,
231 &errType
, &parseError
, &status
);
232 if (U_FAILURE(status
)) {
233 const char *errFile
=
234 (errType
== USPOOF_WHOLE_SCRIPT_CONFUSABLE
)? confWSFileName
: confFileName
;
235 fprintf(stderr
, "gencfu: uspoof_openFromSource error \"%s\" at file %s, line %d, column %d\n",
236 u_errorName(status
), errFile
, (int)parseError
.line
, (int)parseError
.offset
);
242 // Get the compiled rule data from the USpoofChecker.
244 uint32_t outDataSize
;
246 outDataSize
= uspoof_serialize(sc
, NULL
, 0, &status
);
247 if (status
!= U_BUFFER_OVERFLOW_ERROR
) {
248 fprintf(stderr
, "gencfu: uspoof_serialize() returned %s\n", u_errorName(status
));
251 status
= U_ZERO_ERROR
;
252 outData
= new uint8_t[outDataSize
];
253 uspoof_serialize(sc
, outData
, outDataSize
, &status
);
255 // Copy the data format version numbers from the spoof data header into the UDataMemory header.
257 uprv_memcpy(dh
.info
.formatVersion
,
258 reinterpret_cast<SpoofDataHeader
*>(outData
)->fFormatVersion
,
259 sizeof(dh
.info
.formatVersion
));
262 // Create the output file
265 UNewDataMemory
*pData
;
266 pData
= udata_create(outDir
, NULL
, outFileName
, &(dh
.info
), copyright
, &status
);
267 if(U_FAILURE(status
)) {
268 fprintf(stderr
, "gencfu: Could not open output file \"%s\", \"%s\"\n",
269 outFileName
, u_errorName(status
));
274 // Write the data itself.
275 udata_writeBlock(pData
, outData
, outDataSize
);
277 bytesWritten
= udata_finish(pData
, &status
);
278 if(U_FAILURE(status
)) {
279 fprintf(stderr
, "gencfu: Error %d writing the output file\n", status
);
283 if (bytesWritten
!= outDataSize
) {
284 fprintf(stderr
, "gencfu: Error writing to output file \"%s\"\n", outFileName
);
293 printf("gencfu: tool completed successfully.\n");
295 #endif // UCONFIG_NO_REGULAR_EXPRESSIONS
300 // Read in a confusables source file
302 static const char *readFile(const char *fileName
, int32_t *len
) {
307 file
= fopen(fileName
, "rb");
311 fseek(file
, 0, SEEK_END
);
312 fileSize
= ftell(file
);
313 fseek(file
, 0, SEEK_SET
);
314 result
= new char[fileSize
+10];
319 long t
= fread(result
, 1, fileSize
, file
);
326 *len
= static_cast<int32_t>(fileSize
);