intl/icu/source/tools/gencfu/gencfu.cpp

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 /*
michael@0 2 **********************************************************************
michael@0 3 * Copyright (C) 2009-2011, International Business Machines
michael@0 4 * Corporation and others. All Rights Reserved.
michael@0 5 **********************************************************************
michael@0 6 *
michael@0 7 * File gencfu.c
michael@0 8 */
michael@0 9
michael@0 10 //--------------------------------------------------------------------
michael@0 11 //
michael@0 12 // Tool for generating Unicode Confusable data files (.cfu files).
michael@0 13 // .cfu files contain the compiled of the confusable data
michael@0 14 // derived from the Unicode Consortium data described in
michael@0 15 // Unicode UAX 39.
michael@0 16 //
michael@0 17 // Usage: gencfu [options] -r confusables-file.txt -w whole-script-confusables.txt -o output-file.cfu
michael@0 18 //
michael@0 19 // options: -v verbose
michael@0 20 // -? or -h help
michael@0 21 //
michael@0 22 // The input rule filew is are plain text files containing confusable character
michael@0 23 // definitions in the input format defined by Unicode UAX39 for the files
michael@0 24 // confusables.txt and confusablesWholeScript.txt. This source (.txt) format
michael@0 25 // is also accepted direaccepted by ICU spoof detedtors. The
michael@0 26 // files must be encoded in utf-8 format, with or without a BOM.
michael@0 27 //
michael@0 28 //--------------------------------------------------------------------
michael@0 29
michael@0 30 #include "unicode/utypes.h"
michael@0 31 #include "unicode/unistr.h"
michael@0 32 #include "unicode/uclean.h"
michael@0 33 #include "unicode/udata.h"
michael@0 34 #include "unicode/putil.h"
michael@0 35
michael@0 36 #include "uoptions.h"
michael@0 37 #include "unewdata.h"
michael@0 38 #include "ucmndata.h"
michael@0 39 #include "uspoof_impl.h"
michael@0 40 #include "cmemory.h"
michael@0 41
michael@0 42 #include <stdio.h>
michael@0 43 #include <stdlib.h>
michael@0 44 #include <string.h>
michael@0 45
michael@0 46 U_NAMESPACE_USE
michael@0 47
michael@0 48 static char *progName;
michael@0 49 static UOption options[]={
michael@0 50 UOPTION_HELP_H, /* 0 */
michael@0 51 UOPTION_HELP_QUESTION_MARK, /* 1 */
michael@0 52 UOPTION_VERBOSE, /* 2 */
michael@0 53 { "rules", NULL, NULL, NULL, 'r', UOPT_REQUIRES_ARG, 0 }, /* 3 */
michael@0 54 { "wsrules", NULL, NULL, NULL, 'w', UOPT_REQUIRES_ARG, 0}, /* 4 */
michael@0 55 { "out", NULL, NULL, NULL, 'o', UOPT_REQUIRES_ARG, 0 }, /* 5 */
michael@0 56 UOPTION_ICUDATADIR, /* 6 */
michael@0 57 UOPTION_DESTDIR, /* 7 */
michael@0 58 UOPTION_COPYRIGHT, /* 8 */
michael@0 59 };
michael@0 60
michael@0 61 void usageAndDie(int retCode) {
michael@0 62 printf("Usage: %s [-v] [-options] -r confusablesRules.txt -w wholeScriptConfusables.txt -o output-file\n", progName);
michael@0 63 printf("\tRead in Unicode confusable character definitions and write out the binary data\n"
michael@0 64 "options:\n"
michael@0 65 "\t-h or -? or --help this usage text\n"
michael@0 66 "\t-V or --version show a version message\n"
michael@0 67 "\t-c or --copyright include a copyright notice\n"
michael@0 68 "\t-v or --verbose turn on verbose output\n"
michael@0 69 "\t-i or --icudatadir directory for locating any needed intermediate data files,\n"
michael@0 70 "\t followed by path, defaults to %s\n"
michael@0 71 "\t-d or --destdir destination directory, followed by the path\n",
michael@0 72 u_getDataDirectory());
michael@0 73 exit (retCode);
michael@0 74 }
michael@0 75
michael@0 76
michael@0 77 #if UCONFIG_NO_REGULAR_EXPRESSIONS || UCONFIG_NO_NORMALIZATION || UCONFIG_NO_FILE_IO
michael@0 78
michael@0 79 /* dummy UDataInfo cf. udata.h */
michael@0 80 static UDataInfo dummyDataInfo = {
michael@0 81 sizeof(UDataInfo),
michael@0 82 0,
michael@0 83
michael@0 84 U_IS_BIG_ENDIAN,
michael@0 85 U_CHARSET_FAMILY,
michael@0 86 U_SIZEOF_UCHAR,
michael@0 87 0,
michael@0 88
michael@0 89 { 0, 0, 0, 0 }, /* dummy dataFormat */
michael@0 90 { 0, 0, 0, 0 }, /* dummy formatVersion */
michael@0 91 { 0, 0, 0, 0 } /* dummy dataVersion */
michael@0 92 };
michael@0 93
michael@0 94 #else
michael@0 95
michael@0 96 //
michael@0 97 // Set up the ICU data header, defined in ucmndata.h
michael@0 98 //
michael@0 99 DataHeader dh ={
michael@0 100 {sizeof(DataHeader), // Struct MappedData
michael@0 101 0xda,
michael@0 102 0x27},
michael@0 103
michael@0 104 { // struct UDataInfo
michael@0 105 sizeof(UDataInfo), // size
michael@0 106 0, // reserved
michael@0 107 U_IS_BIG_ENDIAN,
michael@0 108 U_CHARSET_FAMILY,
michael@0 109 U_SIZEOF_UCHAR,
michael@0 110 0, // reserved
michael@0 111
michael@0 112 { 0x43, 0x66, 0x75, 0x20 }, // dataFormat="Cfu "
michael@0 113 { 0xff, 0, 0, 0 }, // formatVersion. Filled in later with values
michael@0 114 // from the builder. The values declared
michael@0 115 // here should never appear in any real data.
michael@0 116 { 5, 1, 0, 0 } // dataVersion (Unicode version)
michael@0 117 }};
michael@0 118
michael@0 119 #endif
michael@0 120
michael@0 121 // Forward declaration for function for reading source files.
michael@0 122 static const char *readFile(const char *fileName, int32_t *len);
michael@0 123
michael@0 124 //----------------------------------------------------------------------------
michael@0 125 //
michael@0 126 // main for gencfu
michael@0 127 //
michael@0 128 //----------------------------------------------------------------------------
michael@0 129 int main(int argc, char **argv) {
michael@0 130 UErrorCode status = U_ZERO_ERROR;
michael@0 131 const char *confFileName;
michael@0 132 const char *confWSFileName;
michael@0 133 const char *outFileName;
michael@0 134 const char *outDir = NULL;
michael@0 135 const char *copyright = NULL;
michael@0 136
michael@0 137 //
michael@0 138 // Pick up and check the command line arguments,
michael@0 139 // using the standard ICU tool utils option handling.
michael@0 140 //
michael@0 141 U_MAIN_INIT_ARGS(argc, argv);
michael@0 142 progName = argv[0];
michael@0 143 argc=u_parseArgs(argc, argv, sizeof(options)/sizeof(options[0]), options);
michael@0 144 if(argc<0) {
michael@0 145 // Unrecognized option
michael@0 146 fprintf(stderr, "error in command line argument \"%s\"\n", argv[-argc]);
michael@0 147 usageAndDie(U_ILLEGAL_ARGUMENT_ERROR);
michael@0 148 }
michael@0 149
michael@0 150 if(options[0].doesOccur || options[1].doesOccur) {
michael@0 151 // -? or -h for help.
michael@0 152 usageAndDie(0);
michael@0 153 }
michael@0 154
michael@0 155 if (!(options[3].doesOccur && options[4].doesOccur && options[5].doesOccur)) {
michael@0 156 fprintf(stderr, "confusables file, whole script confusables file and output file must all be specified.\n");
michael@0 157 usageAndDie(U_ILLEGAL_ARGUMENT_ERROR);
michael@0 158 }
michael@0 159 confFileName = options[3].value;
michael@0 160 confWSFileName = options[4].value;
michael@0 161 outFileName = options[5].value;
michael@0 162
michael@0 163 if (options[6].doesOccur) {
michael@0 164 u_setDataDirectory(options[6].value);
michael@0 165 }
michael@0 166
michael@0 167 status = U_ZERO_ERROR;
michael@0 168
michael@0 169 /* Combine the directory with the file name */
michael@0 170 if(options[7].doesOccur) {
michael@0 171 outDir = options[7].value;
michael@0 172 }
michael@0 173 if (options[8].doesOccur) {
michael@0 174 copyright = U_COPYRIGHT_STRING;
michael@0 175 }
michael@0 176
michael@0 177 #if UCONFIG_NO_REGULAR_EXPRESSIONS || UCONFIG_NO_NORMALIZATION || UCONFIG_NO_FILE_IO
michael@0 178 // spoof detection data file parsing is dependent on regular expressions.
michael@0 179 // TODO: have the tool return an error status. Requires fixing the ICU data build
michael@0 180 // so that it doesn't abort entirely on that error.
michael@0 181
michael@0 182 UNewDataMemory *pData;
michael@0 183 char msg[1024];
michael@0 184
michael@0 185 /* write message with just the name */
michael@0 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);
michael@0 187 fprintf(stderr, "%s\n", msg);
michael@0 188
michael@0 189 /* write the dummy data file */
michael@0 190 pData = udata_create(outDir, NULL, outFileName, &dummyDataInfo, NULL, &status);
michael@0 191 udata_writeBlock(pData, msg, strlen(msg));
michael@0 192 udata_finish(pData, &status);
michael@0 193 return (int)status;
michael@0 194
michael@0 195 #else
michael@0 196 /* Initialize ICU */
michael@0 197 u_init(&status);
michael@0 198 if (U_FAILURE(status)) {
michael@0 199 fprintf(stderr, "%s: can not initialize ICU. status = %s\n",
michael@0 200 argv[0], u_errorName(status));
michael@0 201 exit(1);
michael@0 202 }
michael@0 203 status = U_ZERO_ERROR;
michael@0 204
michael@0 205 // Read in the confusables source file
michael@0 206
michael@0 207 int32_t confusablesLen = 0;
michael@0 208 const char *confusables = readFile(confFileName, &confusablesLen);
michael@0 209 if (confusables == NULL) {
michael@0 210 printf("gencfu: error reading file \"%s\"\n", confFileName);
michael@0 211 exit(-1);
michael@0 212 }
michael@0 213
michael@0 214 int32_t wsConfusablesLen = 0;
michael@0 215 const char *wsConfsables = readFile(confWSFileName, &wsConfusablesLen);
michael@0 216 if (wsConfsables == NULL) {
michael@0 217 printf("gencfu: error reading file \"%s\"\n", confFileName);
michael@0 218 exit(-1);
michael@0 219 }
michael@0 220
michael@0 221 //
michael@0 222 // Create the Spoof Detector from the source confusables files.
michael@0 223 // This will compile the data.
michael@0 224 //
michael@0 225 UParseError parseError;
michael@0 226 parseError.line = 0;
michael@0 227 parseError.offset = 0;
michael@0 228 int32_t errType;
michael@0 229 USpoofChecker *sc = uspoof_openFromSource(confusables, confusablesLen,
michael@0 230 wsConfsables, wsConfusablesLen,
michael@0 231 &errType, &parseError, &status);
michael@0 232 if (U_FAILURE(status)) {
michael@0 233 const char *errFile =
michael@0 234 (errType == USPOOF_WHOLE_SCRIPT_CONFUSABLE)? confWSFileName : confFileName;
michael@0 235 fprintf(stderr, "gencfu: uspoof_openFromSource error \"%s\" at file %s, line %d, column %d\n",
michael@0 236 u_errorName(status), errFile, (int)parseError.line, (int)parseError.offset);
michael@0 237 exit(status);
michael@0 238 };
michael@0 239
michael@0 240
michael@0 241 //
michael@0 242 // Get the compiled rule data from the USpoofChecker.
michael@0 243 //
michael@0 244 uint32_t outDataSize;
michael@0 245 uint8_t *outData;
michael@0 246 outDataSize = uspoof_serialize(sc, NULL, 0, &status);
michael@0 247 if (status != U_BUFFER_OVERFLOW_ERROR) {
michael@0 248 fprintf(stderr, "gencfu: uspoof_serialize() returned %s\n", u_errorName(status));
michael@0 249 exit(status);
michael@0 250 }
michael@0 251 status = U_ZERO_ERROR;
michael@0 252 outData = new uint8_t[outDataSize];
michael@0 253 uspoof_serialize(sc, outData, outDataSize, &status);
michael@0 254
michael@0 255 // Copy the data format version numbers from the spoof data header into the UDataMemory header.
michael@0 256
michael@0 257 uprv_memcpy(dh.info.formatVersion,
michael@0 258 reinterpret_cast<SpoofDataHeader *>(outData)->fFormatVersion,
michael@0 259 sizeof(dh.info.formatVersion));
michael@0 260
michael@0 261 //
michael@0 262 // Create the output file
michael@0 263 //
michael@0 264 size_t bytesWritten;
michael@0 265 UNewDataMemory *pData;
michael@0 266 pData = udata_create(outDir, NULL, outFileName, &(dh.info), copyright, &status);
michael@0 267 if(U_FAILURE(status)) {
michael@0 268 fprintf(stderr, "gencfu: Could not open output file \"%s\", \"%s\"\n",
michael@0 269 outFileName, u_errorName(status));
michael@0 270 exit(status);
michael@0 271 }
michael@0 272
michael@0 273
michael@0 274 // Write the data itself.
michael@0 275 udata_writeBlock(pData, outData, outDataSize);
michael@0 276 // finish up
michael@0 277 bytesWritten = udata_finish(pData, &status);
michael@0 278 if(U_FAILURE(status)) {
michael@0 279 fprintf(stderr, "gencfu: Error %d writing the output file\n", status);
michael@0 280 exit(status);
michael@0 281 }
michael@0 282
michael@0 283 if (bytesWritten != outDataSize) {
michael@0 284 fprintf(stderr, "gencfu: Error writing to output file \"%s\"\n", outFileName);
michael@0 285 exit(-1);
michael@0 286 }
michael@0 287
michael@0 288 uspoof_close(sc);
michael@0 289 delete [] outData;
michael@0 290 delete [] confusables;
michael@0 291 delete [] wsConfsables;
michael@0 292 u_cleanup();
michael@0 293 printf("gencfu: tool completed successfully.\n");
michael@0 294 return 0;
michael@0 295 #endif // UCONFIG_NO_REGULAR_EXPRESSIONS
michael@0 296 }
michael@0 297
michael@0 298
michael@0 299 //
michael@0 300 // Read in a confusables source file
michael@0 301 //
michael@0 302 static const char *readFile(const char *fileName, int32_t *len) {
michael@0 303 char *result;
michael@0 304 long fileSize;
michael@0 305 FILE *file;
michael@0 306
michael@0 307 file = fopen(fileName, "rb");
michael@0 308 if( file == 0 ) {
michael@0 309 return NULL;
michael@0 310 }
michael@0 311 fseek(file, 0, SEEK_END);
michael@0 312 fileSize = ftell(file);
michael@0 313 fseek(file, 0, SEEK_SET);
michael@0 314 result = new char[fileSize+10];
michael@0 315 if (result==NULL) {
michael@0 316 fclose(file);
michael@0 317 return NULL;
michael@0 318 }
michael@0 319
michael@0 320 long t = fread(result, 1, fileSize, file);
michael@0 321 if (t != fileSize) {
michael@0 322 delete [] result;
michael@0 323 fclose(file);
michael@0 324 return NULL;
michael@0 325 }
michael@0 326 result[fileSize]=0;
michael@0 327 *len = static_cast<int32_t>(fileSize);
michael@0 328 fclose(file);
michael@0 329 return result;
michael@0 330 }

mercurial