michael@0: /* michael@0: ********************************************************************** michael@0: * Copyright (C) 2009-2011, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: ********************************************************************** michael@0: * michael@0: * File gencfu.c michael@0: */ michael@0: michael@0: //-------------------------------------------------------------------- michael@0: // michael@0: // Tool for generating Unicode Confusable data files (.cfu files). michael@0: // .cfu files contain the compiled of the confusable data michael@0: // derived from the Unicode Consortium data described in michael@0: // Unicode UAX 39. michael@0: // michael@0: // Usage: gencfu [options] -r confusables-file.txt -w whole-script-confusables.txt -o output-file.cfu michael@0: // michael@0: // options: -v verbose michael@0: // -? or -h help michael@0: // michael@0: // The input rule filew is are plain text files containing confusable character michael@0: // definitions in the input format defined by Unicode UAX39 for the files michael@0: // confusables.txt and confusablesWholeScript.txt. This source (.txt) format michael@0: // is also accepted direaccepted by ICU spoof detedtors. The michael@0: // files must be encoded in utf-8 format, with or without a BOM. michael@0: // michael@0: //-------------------------------------------------------------------- michael@0: michael@0: #include "unicode/utypes.h" michael@0: #include "unicode/unistr.h" michael@0: #include "unicode/uclean.h" michael@0: #include "unicode/udata.h" michael@0: #include "unicode/putil.h" michael@0: michael@0: #include "uoptions.h" michael@0: #include "unewdata.h" michael@0: #include "ucmndata.h" michael@0: #include "uspoof_impl.h" michael@0: #include "cmemory.h" michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: U_NAMESPACE_USE michael@0: michael@0: static char *progName; michael@0: static UOption options[]={ michael@0: UOPTION_HELP_H, /* 0 */ michael@0: UOPTION_HELP_QUESTION_MARK, /* 1 */ michael@0: UOPTION_VERBOSE, /* 2 */ michael@0: { "rules", NULL, NULL, NULL, 'r', UOPT_REQUIRES_ARG, 0 }, /* 3 */ michael@0: { "wsrules", NULL, NULL, NULL, 'w', UOPT_REQUIRES_ARG, 0}, /* 4 */ michael@0: { "out", NULL, NULL, NULL, 'o', UOPT_REQUIRES_ARG, 0 }, /* 5 */ michael@0: UOPTION_ICUDATADIR, /* 6 */ michael@0: UOPTION_DESTDIR, /* 7 */ michael@0: UOPTION_COPYRIGHT, /* 8 */ michael@0: }; michael@0: michael@0: void usageAndDie(int retCode) { michael@0: printf("Usage: %s [-v] [-options] -r confusablesRules.txt -w wholeScriptConfusables.txt -o output-file\n", progName); michael@0: printf("\tRead in Unicode confusable character definitions and write out the binary data\n" michael@0: "options:\n" michael@0: "\t-h or -? or --help this usage text\n" michael@0: "\t-V or --version show a version message\n" michael@0: "\t-c or --copyright include a copyright notice\n" michael@0: "\t-v or --verbose turn on verbose output\n" michael@0: "\t-i or --icudatadir directory for locating any needed intermediate data files,\n" michael@0: "\t followed by path, defaults to %s\n" michael@0: "\t-d or --destdir destination directory, followed by the path\n", michael@0: u_getDataDirectory()); michael@0: exit (retCode); michael@0: } michael@0: michael@0: michael@0: #if UCONFIG_NO_REGULAR_EXPRESSIONS || UCONFIG_NO_NORMALIZATION || UCONFIG_NO_FILE_IO michael@0: michael@0: /* dummy UDataInfo cf. udata.h */ michael@0: static UDataInfo dummyDataInfo = { michael@0: sizeof(UDataInfo), michael@0: 0, michael@0: michael@0: U_IS_BIG_ENDIAN, michael@0: U_CHARSET_FAMILY, michael@0: U_SIZEOF_UCHAR, michael@0: 0, michael@0: michael@0: { 0, 0, 0, 0 }, /* dummy dataFormat */ michael@0: { 0, 0, 0, 0 }, /* dummy formatVersion */ michael@0: { 0, 0, 0, 0 } /* dummy dataVersion */ michael@0: }; michael@0: michael@0: #else michael@0: michael@0: // michael@0: // Set up the ICU data header, defined in ucmndata.h michael@0: // michael@0: DataHeader dh ={ michael@0: {sizeof(DataHeader), // Struct MappedData michael@0: 0xda, michael@0: 0x27}, michael@0: michael@0: { // struct UDataInfo michael@0: sizeof(UDataInfo), // size michael@0: 0, // reserved michael@0: U_IS_BIG_ENDIAN, michael@0: U_CHARSET_FAMILY, michael@0: U_SIZEOF_UCHAR, michael@0: 0, // reserved michael@0: michael@0: { 0x43, 0x66, 0x75, 0x20 }, // dataFormat="Cfu " michael@0: { 0xff, 0, 0, 0 }, // formatVersion. Filled in later with values michael@0: // from the builder. The values declared michael@0: // here should never appear in any real data. michael@0: { 5, 1, 0, 0 } // dataVersion (Unicode version) michael@0: }}; michael@0: michael@0: #endif michael@0: michael@0: // Forward declaration for function for reading source files. michael@0: static const char *readFile(const char *fileName, int32_t *len); michael@0: michael@0: //---------------------------------------------------------------------------- michael@0: // michael@0: // main for gencfu michael@0: // michael@0: //---------------------------------------------------------------------------- michael@0: int main(int argc, char **argv) { michael@0: UErrorCode status = U_ZERO_ERROR; michael@0: const char *confFileName; michael@0: const char *confWSFileName; michael@0: const char *outFileName; michael@0: const char *outDir = NULL; michael@0: const char *copyright = NULL; michael@0: michael@0: // michael@0: // Pick up and check the command line arguments, michael@0: // using the standard ICU tool utils option handling. michael@0: // michael@0: U_MAIN_INIT_ARGS(argc, argv); michael@0: progName = argv[0]; michael@0: argc=u_parseArgs(argc, argv, sizeof(options)/sizeof(options[0]), options); michael@0: if(argc<0) { michael@0: // Unrecognized option michael@0: fprintf(stderr, "error in command line argument \"%s\"\n", argv[-argc]); michael@0: usageAndDie(U_ILLEGAL_ARGUMENT_ERROR); michael@0: } michael@0: michael@0: if(options[0].doesOccur || options[1].doesOccur) { michael@0: // -? or -h for help. michael@0: usageAndDie(0); michael@0: } michael@0: michael@0: if (!(options[3].doesOccur && options[4].doesOccur && options[5].doesOccur)) { michael@0: fprintf(stderr, "confusables file, whole script confusables file and output file must all be specified.\n"); michael@0: usageAndDie(U_ILLEGAL_ARGUMENT_ERROR); michael@0: } michael@0: confFileName = options[3].value; michael@0: confWSFileName = options[4].value; michael@0: outFileName = options[5].value; michael@0: michael@0: if (options[6].doesOccur) { michael@0: u_setDataDirectory(options[6].value); michael@0: } michael@0: michael@0: status = U_ZERO_ERROR; michael@0: michael@0: /* Combine the directory with the file name */ michael@0: if(options[7].doesOccur) { michael@0: outDir = options[7].value; michael@0: } michael@0: if (options[8].doesOccur) { michael@0: copyright = U_COPYRIGHT_STRING; michael@0: } michael@0: michael@0: #if UCONFIG_NO_REGULAR_EXPRESSIONS || UCONFIG_NO_NORMALIZATION || UCONFIG_NO_FILE_IO michael@0: // spoof detection data file parsing is dependent on regular expressions. michael@0: // TODO: have the tool return an error status. Requires fixing the ICU data build michael@0: // so that it doesn't abort entirely on that error. michael@0: michael@0: UNewDataMemory *pData; michael@0: char msg[1024]; michael@0: michael@0: /* write message with just the name */ michael@0: 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: fprintf(stderr, "%s\n", msg); michael@0: michael@0: /* write the dummy data file */ michael@0: pData = udata_create(outDir, NULL, outFileName, &dummyDataInfo, NULL, &status); michael@0: udata_writeBlock(pData, msg, strlen(msg)); michael@0: udata_finish(pData, &status); michael@0: return (int)status; michael@0: michael@0: #else michael@0: /* Initialize ICU */ michael@0: u_init(&status); michael@0: if (U_FAILURE(status)) { michael@0: fprintf(stderr, "%s: can not initialize ICU. status = %s\n", michael@0: argv[0], u_errorName(status)); michael@0: exit(1); michael@0: } michael@0: status = U_ZERO_ERROR; michael@0: michael@0: // Read in the confusables source file michael@0: michael@0: int32_t confusablesLen = 0; michael@0: const char *confusables = readFile(confFileName, &confusablesLen); michael@0: if (confusables == NULL) { michael@0: printf("gencfu: error reading file \"%s\"\n", confFileName); michael@0: exit(-1); michael@0: } michael@0: michael@0: int32_t wsConfusablesLen = 0; michael@0: const char *wsConfsables = readFile(confWSFileName, &wsConfusablesLen); michael@0: if (wsConfsables == NULL) { michael@0: printf("gencfu: error reading file \"%s\"\n", confFileName); michael@0: exit(-1); michael@0: } michael@0: michael@0: // michael@0: // Create the Spoof Detector from the source confusables files. michael@0: // This will compile the data. michael@0: // michael@0: UParseError parseError; michael@0: parseError.line = 0; michael@0: parseError.offset = 0; michael@0: int32_t errType; michael@0: USpoofChecker *sc = uspoof_openFromSource(confusables, confusablesLen, michael@0: wsConfsables, wsConfusablesLen, michael@0: &errType, &parseError, &status); michael@0: if (U_FAILURE(status)) { michael@0: const char *errFile = michael@0: (errType == USPOOF_WHOLE_SCRIPT_CONFUSABLE)? confWSFileName : confFileName; michael@0: fprintf(stderr, "gencfu: uspoof_openFromSource error \"%s\" at file %s, line %d, column %d\n", michael@0: u_errorName(status), errFile, (int)parseError.line, (int)parseError.offset); michael@0: exit(status); michael@0: }; michael@0: michael@0: michael@0: // michael@0: // Get the compiled rule data from the USpoofChecker. michael@0: // michael@0: uint32_t outDataSize; michael@0: uint8_t *outData; michael@0: outDataSize = uspoof_serialize(sc, NULL, 0, &status); michael@0: if (status != U_BUFFER_OVERFLOW_ERROR) { michael@0: fprintf(stderr, "gencfu: uspoof_serialize() returned %s\n", u_errorName(status)); michael@0: exit(status); michael@0: } michael@0: status = U_ZERO_ERROR; michael@0: outData = new uint8_t[outDataSize]; michael@0: uspoof_serialize(sc, outData, outDataSize, &status); michael@0: michael@0: // Copy the data format version numbers from the spoof data header into the UDataMemory header. michael@0: michael@0: uprv_memcpy(dh.info.formatVersion, michael@0: reinterpret_cast(outData)->fFormatVersion, michael@0: sizeof(dh.info.formatVersion)); michael@0: michael@0: // michael@0: // Create the output file michael@0: // michael@0: size_t bytesWritten; michael@0: UNewDataMemory *pData; michael@0: pData = udata_create(outDir, NULL, outFileName, &(dh.info), copyright, &status); michael@0: if(U_FAILURE(status)) { michael@0: fprintf(stderr, "gencfu: Could not open output file \"%s\", \"%s\"\n", michael@0: outFileName, u_errorName(status)); michael@0: exit(status); michael@0: } michael@0: michael@0: michael@0: // Write the data itself. michael@0: udata_writeBlock(pData, outData, outDataSize); michael@0: // finish up michael@0: bytesWritten = udata_finish(pData, &status); michael@0: if(U_FAILURE(status)) { michael@0: fprintf(stderr, "gencfu: Error %d writing the output file\n", status); michael@0: exit(status); michael@0: } michael@0: michael@0: if (bytesWritten != outDataSize) { michael@0: fprintf(stderr, "gencfu: Error writing to output file \"%s\"\n", outFileName); michael@0: exit(-1); michael@0: } michael@0: michael@0: uspoof_close(sc); michael@0: delete [] outData; michael@0: delete [] confusables; michael@0: delete [] wsConfsables; michael@0: u_cleanup(); michael@0: printf("gencfu: tool completed successfully.\n"); michael@0: return 0; michael@0: #endif // UCONFIG_NO_REGULAR_EXPRESSIONS michael@0: } michael@0: michael@0: michael@0: // michael@0: // Read in a confusables source file michael@0: // michael@0: static const char *readFile(const char *fileName, int32_t *len) { michael@0: char *result; michael@0: long fileSize; michael@0: FILE *file; michael@0: michael@0: file = fopen(fileName, "rb"); michael@0: if( file == 0 ) { michael@0: return NULL; michael@0: } michael@0: fseek(file, 0, SEEK_END); michael@0: fileSize = ftell(file); michael@0: fseek(file, 0, SEEK_SET); michael@0: result = new char[fileSize+10]; michael@0: if (result==NULL) { michael@0: fclose(file); michael@0: return NULL; michael@0: } michael@0: michael@0: long t = fread(result, 1, fileSize, file); michael@0: if (t != fileSize) { michael@0: delete [] result; michael@0: fclose(file); michael@0: return NULL; michael@0: } michael@0: result[fileSize]=0; michael@0: *len = static_cast(fileSize); michael@0: fclose(file); michael@0: return result; michael@0: }