185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho/*
285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho***************************************************************************
38393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius* Copyright (C) 2008-2013, International Business Machines Corporation
485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho* and others. All Rights Reserved.
585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho***************************************************************************
685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho*
785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho*  uspoof_impl.h
885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho*
985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho*    Implemenation header for spoof detection
1085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho*
1185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho*/
1285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
1385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#ifndef USPOOFIM_H
1485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#define USPOOFIM_H
1585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
1685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#include "unicode/utypes.h"
1785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#include "unicode/uspoof.h"
1885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#include "unicode/uscript.h"
1985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#include "unicode/udata.h"
2085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
218393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius#include "utrie2.h"
2285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
2385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#if !UCONFIG_NO_NORMALIZATION
2485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
25103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius#ifdef __cplusplus
2627f654740f2a26ad62a5c155af9199af9e69b889claireho
2785bf2e2fbc60a9f938064abc8127d61da7d19882Claire HoU_NAMESPACE_BEGIN
2885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
2985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho// The maximium length (in UTF-16 UChars) of the skeleton replacement string resulting from
3085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//   a single input code point.  This is function of the unicode.org data.
3185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#define USPOOF_MAX_SKELETON_EXPANSION 20
3285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
3385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho// The default stack buffer size for copies or conversions or normalizations
3485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho// of input strings being checked.  (Used in multiple places.)
3585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#define USPOOF_STACK_BUFFER_SIZE 100
3685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
3785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho// Magic number for sanity checking spoof data.
3885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#define USPOOF_MAGIC 0x3845fdef
3985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
408393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Corneliusclass IdentifierInfo;
418393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Corneliusclass ScriptSet;
4285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Hoclass SpoofData;
4385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Hostruct SpoofDataHeader;
4485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Hostruct SpoofStringLengthsElement;
4585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
4685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho/**
4785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho  *  Class SpoofImpl corresponds directly to the plain C API opaque type
4885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho  *  USpoofChecker.  One can be cast to the other.
4985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho  */
5085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Hoclass SpoofImpl : public UObject  {
5185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Hopublic:
5285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho	SpoofImpl(SpoofData *data, UErrorCode &status);
5385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho	SpoofImpl();
5485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho	virtual ~SpoofImpl();
5585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
5685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    /** Copy constructor, used by the user level uspoof_clone() function.
5785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho     */
5885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    SpoofImpl(const SpoofImpl &src, UErrorCode &status);
5985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
6085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    static SpoofImpl *validateThis(USpoofChecker *sc, UErrorCode &status);
6185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    static const SpoofImpl *validateThis(const USpoofChecker *sc, UErrorCode &status);
6285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
6385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    /** Get the confusable skeleton transform for a single code point.
6485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho     *  The result is a string with a length between 1 and 18.
6585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho     *  @param    tableMask  bit flag specifying which confusable table to use.
6685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho     *                       One of USPOOF_SL_TABLE_FLAG, USPOOF_MA_TABLE_FLAG, etc.
6785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho     *  @return   The length in UTF-16 code units of the substition string.
6885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho     */
698393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius    int32_t confusableLookup(UChar32 inChar, int32_t tableMask, UnicodeString &destBuf) const;
7085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
7185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    /** Set and Get AllowedLocales, implementations of the corresponding API */
7285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    void setAllowedLocales(const char *localesList, UErrorCode &status);
7385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    const char * getAllowedLocales(UErrorCode &status);
7485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
7585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    // Add (union) to the UnicodeSet all of the characters for the scripts used for
7685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    // the specified locale.  Part of the implementation of setAllowedLocales.
7785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    void addScriptChars(const char *locale, UnicodeSet *allowedChars, UErrorCode &status);
7885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
7985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
8085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    /** parse a hex number.  Untility used by the builders.   */
8185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    static UChar32 ScanHex(const UChar *s, int32_t start, int32_t limit, UErrorCode &status);
8285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
8385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    // Implementation for Whole Script tests.
8485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    // Return the test bit flag to be ORed into the eventual user return value
8585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    //    if a Spoof opportunity is detected.
8685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    void wholeScriptCheck(
878393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius        const UnicodeString &text, ScriptSet *result, UErrorCode &status) const;
8885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
8985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    static UClassID U_EXPORT2 getStaticClassID(void);
9085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    virtual UClassID getDynamicClassID(void) const;
9185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
928393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius    // IdentifierInfo Cache. IdentifierInfo objects are somewhat expensive to create.
938393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius    //                       Maintain a one-element cache, which is sufficient to avoid repeatedly
948393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius    //                       creating new ones unless we get multi-thread concurrency in spoof
958393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius    //                       check operations, which should be statistically uncommon.
968393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius    IdentifierInfo *getIdentifierInfo(UErrorCode &status) const;
978393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius    void releaseIdentifierInfo(IdentifierInfo *idInfo) const;
988393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius
9985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    //
10085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    // Data Members
10185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    //
10285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
10385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    int32_t           fMagic;             // Internal sanity check.
10485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    int32_t           fChecks;            // Bit vector of checks to perform.
10585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
10685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    SpoofData        *fSpoofData;
10785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
10885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    const UnicodeSet *fAllowedCharsSet;   // The UnicodeSet of allowed characters.
10985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho                                          //   for this Spoof Checker.  Defaults to all chars.
11085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
11185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    const char       *fAllowedLocales;    // The list of allowed locales.
1128393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius    URestrictionLevel fRestrictionLevel;  // The maximum restriction level for an acceptable identifier.
1138393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius
1148393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius    IdentifierInfo    *fCachedIdentifierInfo;    // Do not use directly. See getIdentifierInfo().:w
11585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho};
11685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
11785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
11885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
11985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//
12085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//  Confusable Mappings Data Structures
12185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//
12285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//    For the confusable data, we are essentially implementing a map,
12385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//       key:    a code point
12485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//       value:  a string.  Most commonly one char in length, but can be more.
12585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//
12685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//    The keys are stored as a sorted array of 32 bit ints.
12785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//             bits 0-23    a code point value
12885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//             bits 24-31   flags
12985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//                24:  1 if entry applies to SL table
13085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//                25:  1 if entry applies to SA table
13185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//                26:  1 if entry applies to ML table
13285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//                27:  1 if entry applies to MA table
13385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//                28:  1 if there are multiple entries for this code point.
13485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//                29-30:  length of value string, in UChars.
13585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//                         values are (1, 2, 3, other)
13685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//        The key table is sorted in ascending code point order.  (not on the
13785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//        32 bit int value, the flag bits do not participate in the sorting.)
13885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//
13985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//        Lookup is done by means of a binary search in the key table.
14085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//
14185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//    The corresponding values are kept in a parallel array of 16 bit ints.
14285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//        If the value string is of length 1, it is literally in the value array.
14385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//        For longer strings, the value array contains an index into the strings table.
14485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//
14585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//    String Table:
14685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//       The strings table contains all of the value strings (those of length two or greater)
14785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//       concatentated together into one long UChar (UTF-16) array.
14885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//
14985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//       The array is arranged by length of the strings - all strings of the same length
15085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//       are stored together.  The sections are ordered by length of the strings -
15185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//       all two char strings first, followed by all of the three Char strings, etc.
15285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//
15385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//       There is no nul character or other mark between adjacent strings.
15485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//
15585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//    String Lengths table
15685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//       The length of strings from 1 to 3 is flagged in the key table.
15785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//       For strings of length 4 or longer, the string length table provides a
15885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//       mapping between an index into the string table and the corresponding length.
15985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//       Strings of these lengths are rare, so lookup time is not an issue.
16085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//       Each entry consists of
16185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//            uint16_t      index of the _last_ string with this length
16285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//            uint16_t      the length
16385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//
16485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
16585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho// Flag bits in the Key entries
16685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#define USPOOF_SL_TABLE_FLAG (1<<24)
16785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#define USPOOF_SA_TABLE_FLAG (1<<25)
16885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#define USPOOF_ML_TABLE_FLAG (1<<26)
16985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#define USPOOF_MA_TABLE_FLAG (1<<27)
17085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#define USPOOF_KEY_MULTIPLE_VALUES (1<<28)
17185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#define USPOOF_KEY_LENGTH_SHIFT 29
17285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#define USPOOF_KEY_LENGTH_FIELD(x) (((x)>>29) & 3)
17385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
17485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
17585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Hostruct SpoofStringLengthsElement {
17685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    uint16_t      fLastString;         // index in string table of last string with this length
17785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    uint16_t      fStrLength;           // Length of strings
17885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho};
17985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
18085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
18185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
18285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//-------------------------------------------------------------------------------------
18385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//
18485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//  SpoofData
18585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//
18685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//    A small class that wraps the raw (usually memory mapped) spoof data.
18785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//    Serves two primary functions:
18885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//      1.  Convenience.  Contains real pointers to the data, to avoid dealing with
18985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//          the offsets in the raw data.
19085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//      2.  Reference counting.  When a spoof checker is cloned, the raw data is shared
19185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//          and must be retained until all checkers using the data are closed.
19285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//    Nothing in this struct includes state that is specific to any particular
19385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//    USpoofDetector object.
19485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//
19585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//---------------------------------------------------------------------------------------
19685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Hoclass SpoofData: public UMemory {
19785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho  public:
19885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    static SpoofData *getDefault(UErrorCode &status);   // Load standard ICU spoof data.
19985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    SpoofData(UErrorCode &status);   // Create new spoof data wrapper.
20085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho                                     // Only used when building new data from rules.
20185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
20285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    // Constructor for use when creating from prebuilt default data.
20385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    //   A UDataMemory is what the ICU internal data loading functions provide.
20485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    //   The udm is adopted by the SpoofData.
20585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    SpoofData(UDataMemory *udm, UErrorCode &status);
20685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
20785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    // Constructor for use when creating from serialized data.
20885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    //
20985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    SpoofData(const void *serializedData, int32_t length, UErrorCode &status);
21085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
21185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    //  Check raw Spoof Data Version compatibility.
21285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    //  Return TRUE it looks good.
21385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    static UBool validateDataVersion(const SpoofDataHeader *rawData, UErrorCode &status);
21485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    ~SpoofData();                    // Destructor not normally used.
21585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho                                     // Use removeReference() instead.
21685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    // Reference Counting functions.
21785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    //    Clone of a user-level spoof detector increments the ref count on the data.
21885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    //    Close of a user-level spoof detector decrements the ref count.
21985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    //    If the data is owned by us, it will be deleted when count goes to zero.
22085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    SpoofData *addReference();
22185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    void removeReference();
22285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
22385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    // Reserve space in the raw data.  For use by builder when putting together a
22485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    //   new set of data.  Init the new storage to zero, to prevent inconsistent
22585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    //   results if it is not all otherwise set by the requester.
22685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    //  Return:
22785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    //    pointer to the new space that was added by this function.
22885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    void *reserveSpace(int32_t numBytes, UErrorCode &status);
22985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
23085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    // initialize the pointers from this object to the raw data.
23185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    void initPtrs(UErrorCode &status);
23285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
23385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    // Reset all fields to an initial state.
23485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    // Called from the top of all constructors.
23585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    void reset();
23685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
23785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    SpoofDataHeader             *fRawData;          // Ptr to the raw memory-mapped data
23885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    UBool                       fDataOwned;         // True if the raw data is owned, and needs
23985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho                                                    //  to be deleted when refcount goes to zero.
24085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    UDataMemory                 *fUDM;              // If not NULL, our data came from a
24185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho                                                    //   UDataMemory, which we must close when
242b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho                                                    //   we are done.
24385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
24485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    uint32_t                    fMemLimit;          // Limit of available raw data space
24559d709d503bab6e2b61931737e662dd293b40578ccornelius    u_atomic_int32_t            fRefCount;
24685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
24785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    // Confusable data
24885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    int32_t                     *fCFUKeys;
24985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    uint16_t                    *fCFUValues;
25085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    SpoofStringLengthsElement   *fCFUStringLengths;
25185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    UChar                       *fCFUStrings;
25285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
25385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    // Whole Script Confusable Data
25485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    UTrie2                      *fAnyCaseTrie;
25585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    UTrie2                      *fLowerCaseTrie;
25685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    ScriptSet                   *fScriptSets;
25785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    };
25885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
25985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
26085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//---------------------------------------------------------------------------------------
26185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//
26285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//  Raw Binary Data Formats, as loaded from the ICU data file,
26385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//    or as built by the builder.
26485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//
26585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//---------------------------------------------------------------------------------------
26685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Hostruct SpoofDataHeader {
26727f654740f2a26ad62a5c155af9199af9e69b889claireho    int32_t       fMagic;                // (0x3845fdef)
26885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    uint8_t       fFormatVersion[4];     // Data Format. Same as the value in struct UDataInfo
26985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho                                         //   if there is one associated with this data.
27085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    int32_t       fLength;               // Total lenght in bytes of this spoof data,
27185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho                                         //   including all sections, not just the header.
27285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
27385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    // The following four sections refer to data representing the confusable data
27485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    //   from the Unicode.org data from "confusables.txt"
27585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
27685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    int32_t       fCFUKeys;               // byte offset to Keys table (from SpoofDataHeader *)
27785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    int32_t       fCFUKeysSize;           // number of entries in keys table  (32 bits each)
27885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
27985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    // TODO: change name to fCFUValues, for consistency.
28085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    int32_t       fCFUStringIndex;        // byte offset to String Indexes table
28185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    int32_t       fCFUStringIndexSize;    // number of entries in String Indexes table (16 bits each)
28285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho                                          //     (number of entries must be same as in Keys table
28385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
28485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    int32_t       fCFUStringTable;        // byte offset of String table
28585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    int32_t       fCFUStringTableLen;     // length of string table (in 16 bit UChars)
28685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
28785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    int32_t       fCFUStringLengths;      // byte offset to String Lengths table
28885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    int32_t       fCFUStringLengthsSize;  // number of entries in lengths table. (2 x 16 bits each)
28985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
29085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
29185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    // The following sections are for data from confusablesWholeScript.txt
29285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
29385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    int32_t       fAnyCaseTrie;           // byte offset to the serialized Any Case Trie
29485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    int32_t       fAnyCaseTrieLength;     // Length (bytes) of the serialized Any Case Trie
29585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
29685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    int32_t       fLowerCaseTrie;         // byte offset to the serialized Lower Case Trie
29785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    int32_t       fLowerCaseTrieLength;   // Length (bytes) of the serialized Lower Case Trie
29885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
29985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    int32_t       fScriptSets;            // byte offset to array of ScriptSets
30085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    int32_t       fScriptSetsLength;      // Number of ScriptSets (24 bytes each)
30185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
30285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
30385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    // The following sections are for data from xidmodifications.txt
30485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
30585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
30685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    int32_t       unused[15];              // Padding, Room for Expansion
30785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
30885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho };
30985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
31085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
31185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
31285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
31385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//
31485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//  Structure for the Whole Script Confusable Data
31585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//    See Unicode UAX-39, Unicode Security Mechanisms, for a description of the
31685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//    Whole Script confusable data
31785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//
31885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//  The data provides mappings from code points to a set of scripts
31985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//    that contain characters that might be confused with the code point.
32085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//  There are two mappings, one for lower case only, and one for characters
32185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//    of any case.
32285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//
32385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//  The actual data consists of a utrie2 to map from a code point to an offset,
32485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//  and an array of UScriptSets (essentially bit maps) that is indexed
32585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//  by the offsets obtained from the Trie.
32685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//
32785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho//
32885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
32985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
33085bf2e2fbc60a9f938064abc8127d61da7d19882Claire HoU_NAMESPACE_END
331103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius#endif /* __cplusplus */
33285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
33385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho/**
33485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho  * Endianness swap function for binary spoof data.
33585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho  * @internal
33685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho  */
33785bf2e2fbc60a9f938064abc8127d61da7d19882Claire HoU_CAPI int32_t U_EXPORT2
33885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Houspoof_swap(const UDataSwapper *ds, const void *inData, int32_t length, void *outData,
33985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho            UErrorCode *status);
34085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
34185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
34285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#endif
34385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
34485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#endif  /* USPOOFIM_H */
34585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
346