xref: /aosp_15_r20/external/icu/libicu/cts_headers/uspoof_impl.h (revision 0e209d3975ff4a8c132096b14b0e9364a753506e)
1*0e209d39SAndroid Build Coastguard Worker // © 2016 and later: Unicode, Inc. and others.
2*0e209d39SAndroid Build Coastguard Worker // License & terms of use: http://www.unicode.org/copyright.html
3*0e209d39SAndroid Build Coastguard Worker /*
4*0e209d39SAndroid Build Coastguard Worker ***************************************************************************
5*0e209d39SAndroid Build Coastguard Worker * Copyright (C) 2008-2013, International Business Machines Corporation
6*0e209d39SAndroid Build Coastguard Worker * and others. All Rights Reserved.
7*0e209d39SAndroid Build Coastguard Worker ***************************************************************************
8*0e209d39SAndroid Build Coastguard Worker *
9*0e209d39SAndroid Build Coastguard Worker *  uspoof_impl.h
10*0e209d39SAndroid Build Coastguard Worker *
11*0e209d39SAndroid Build Coastguard Worker *    Implementation header for spoof detection
12*0e209d39SAndroid Build Coastguard Worker *
13*0e209d39SAndroid Build Coastguard Worker */
14*0e209d39SAndroid Build Coastguard Worker 
15*0e209d39SAndroid Build Coastguard Worker #ifndef USPOOFIM_H
16*0e209d39SAndroid Build Coastguard Worker #define USPOOFIM_H
17*0e209d39SAndroid Build Coastguard Worker 
18*0e209d39SAndroid Build Coastguard Worker #include "uassert.h"
19*0e209d39SAndroid Build Coastguard Worker #include "unicode/utypes.h"
20*0e209d39SAndroid Build Coastguard Worker #include "unicode/uspoof.h"
21*0e209d39SAndroid Build Coastguard Worker #include "unicode/uscript.h"
22*0e209d39SAndroid Build Coastguard Worker #include "unicode/udata.h"
23*0e209d39SAndroid Build Coastguard Worker #include "udataswp.h"
24*0e209d39SAndroid Build Coastguard Worker #include "utrie2.h"
25*0e209d39SAndroid Build Coastguard Worker 
26*0e209d39SAndroid Build Coastguard Worker #if !UCONFIG_NO_NORMALIZATION
27*0e209d39SAndroid Build Coastguard Worker 
28*0e209d39SAndroid Build Coastguard Worker #ifdef __cplusplus
29*0e209d39SAndroid Build Coastguard Worker 
30*0e209d39SAndroid Build Coastguard Worker #include "capi_helper.h"
31*0e209d39SAndroid Build Coastguard Worker #include "umutex.h"
32*0e209d39SAndroid Build Coastguard Worker 
33*0e209d39SAndroid Build Coastguard Worker U_NAMESPACE_BEGIN
34*0e209d39SAndroid Build Coastguard Worker 
35*0e209d39SAndroid Build Coastguard Worker // The maximum length (in UTF-16 UChars) of the skeleton replacement string resulting from
36*0e209d39SAndroid Build Coastguard Worker //   a single input code point.  This is function of the unicode.org data.
37*0e209d39SAndroid Build Coastguard Worker #define USPOOF_MAX_SKELETON_EXPANSION 20
38*0e209d39SAndroid Build Coastguard Worker 
39*0e209d39SAndroid Build Coastguard Worker // The default stack buffer size for copies or conversions or normalizations
40*0e209d39SAndroid Build Coastguard Worker // of input strings being checked.  (Used in multiple places.)
41*0e209d39SAndroid Build Coastguard Worker #define USPOOF_STACK_BUFFER_SIZE 100
42*0e209d39SAndroid Build Coastguard Worker 
43*0e209d39SAndroid Build Coastguard Worker // Magic number for sanity checking spoof data.
44*0e209d39SAndroid Build Coastguard Worker #define USPOOF_MAGIC 0x3845fdef
45*0e209d39SAndroid Build Coastguard Worker 
46*0e209d39SAndroid Build Coastguard Worker // Magic number for sanity checking spoof checkers.
47*0e209d39SAndroid Build Coastguard Worker #define USPOOF_CHECK_MAGIC 0x2734ecde
48*0e209d39SAndroid Build Coastguard Worker 
49*0e209d39SAndroid Build Coastguard Worker class ScriptSet;
50*0e209d39SAndroid Build Coastguard Worker class SpoofData;
51*0e209d39SAndroid Build Coastguard Worker struct SpoofDataHeader;
52*0e209d39SAndroid Build Coastguard Worker class ConfusableDataUtils;
53*0e209d39SAndroid Build Coastguard Worker 
54*0e209d39SAndroid Build Coastguard Worker /**
55*0e209d39SAndroid Build Coastguard Worker   *  Class SpoofImpl corresponds directly to the plain C API opaque type
56*0e209d39SAndroid Build Coastguard Worker   *  USpoofChecker.  One can be cast to the other.
57*0e209d39SAndroid Build Coastguard Worker   */
58*0e209d39SAndroid Build Coastguard Worker class SpoofImpl : public UObject,
59*0e209d39SAndroid Build Coastguard Worker         public IcuCApiHelper<USpoofChecker, SpoofImpl, USPOOF_MAGIC> {
60*0e209d39SAndroid Build Coastguard Worker public:
61*0e209d39SAndroid Build Coastguard Worker     SpoofImpl(SpoofData *data, UErrorCode& status);
62*0e209d39SAndroid Build Coastguard Worker     SpoofImpl(UErrorCode& status);
63*0e209d39SAndroid Build Coastguard Worker     SpoofImpl();
64*0e209d39SAndroid Build Coastguard Worker     void construct(UErrorCode& status);
65*0e209d39SAndroid Build Coastguard Worker     virtual ~SpoofImpl();
66*0e209d39SAndroid Build Coastguard Worker 
67*0e209d39SAndroid Build Coastguard Worker     /** Copy constructor, used by the user level uspoof_clone() function.
68*0e209d39SAndroid Build Coastguard Worker      */
69*0e209d39SAndroid Build Coastguard Worker     SpoofImpl(const SpoofImpl &src, UErrorCode &status);
70*0e209d39SAndroid Build Coastguard Worker 
71*0e209d39SAndroid Build Coastguard Worker     USpoofChecker *asUSpoofChecker();
72*0e209d39SAndroid Build Coastguard Worker     static SpoofImpl *validateThis(USpoofChecker *sc, UErrorCode &status);
73*0e209d39SAndroid Build Coastguard Worker     static const SpoofImpl *validateThis(const USpoofChecker *sc, UErrorCode &status);
74*0e209d39SAndroid Build Coastguard Worker 
75*0e209d39SAndroid Build Coastguard Worker     /** Set and Get AllowedLocales, implementations of the corresponding API */
76*0e209d39SAndroid Build Coastguard Worker     void setAllowedLocales(const char *localesList, UErrorCode &status);
77*0e209d39SAndroid Build Coastguard Worker     const char * getAllowedLocales(UErrorCode &status);
78*0e209d39SAndroid Build Coastguard Worker 
79*0e209d39SAndroid Build Coastguard Worker     // Add (union) to the UnicodeSet all of the characters for the scripts used for
80*0e209d39SAndroid Build Coastguard Worker     // the specified locale.  Part of the implementation of setAllowedLocales.
81*0e209d39SAndroid Build Coastguard Worker     void addScriptChars(const char *locale, UnicodeSet *allowedChars, UErrorCode &status);
82*0e209d39SAndroid Build Coastguard Worker 
83*0e209d39SAndroid Build Coastguard Worker     // Functions implementing the features of UTS 39 section 5.
84*0e209d39SAndroid Build Coastguard Worker     static void getAugmentedScriptSet(UChar32 codePoint, ScriptSet& result, UErrorCode& status);
85*0e209d39SAndroid Build Coastguard Worker     void getResolvedScriptSet(const UnicodeString& input, ScriptSet& result, UErrorCode& status) const;
86*0e209d39SAndroid Build Coastguard Worker     void getResolvedScriptSetWithout(const UnicodeString& input, UScriptCode script, ScriptSet& result, UErrorCode& status) const;
87*0e209d39SAndroid Build Coastguard Worker     void getNumerics(const UnicodeString& input, UnicodeSet& result, UErrorCode& status) const;
88*0e209d39SAndroid Build Coastguard Worker     URestrictionLevel getRestrictionLevel(const UnicodeString& input, UErrorCode& status) const;
89*0e209d39SAndroid Build Coastguard Worker 
90*0e209d39SAndroid Build Coastguard Worker     int32_t findHiddenOverlay(const UnicodeString& input, UErrorCode& status) const;
91*0e209d39SAndroid Build Coastguard Worker     bool isIllegalCombiningDotLeadCharacter(UChar32 cp) const;
92*0e209d39SAndroid Build Coastguard Worker 
93*0e209d39SAndroid Build Coastguard Worker     /** parse a hex number.  Untility used by the builders.   */
94*0e209d39SAndroid Build Coastguard Worker     static UChar32 ScanHex(const char16_t *s, int32_t start, int32_t limit, UErrorCode &status);
95*0e209d39SAndroid Build Coastguard Worker 
96*0e209d39SAndroid Build Coastguard Worker     static UClassID U_EXPORT2 getStaticClassID();
97*0e209d39SAndroid Build Coastguard Worker     virtual UClassID getDynamicClassID() const override;
98*0e209d39SAndroid Build Coastguard Worker 
99*0e209d39SAndroid Build Coastguard Worker     //
100*0e209d39SAndroid Build Coastguard Worker     // Data Members
101*0e209d39SAndroid Build Coastguard Worker     //
102*0e209d39SAndroid Build Coastguard Worker 
103*0e209d39SAndroid Build Coastguard Worker     int32_t           fChecks;            // Bit vector of checks to perform.
104*0e209d39SAndroid Build Coastguard Worker 
105*0e209d39SAndroid Build Coastguard Worker     SpoofData        *fSpoofData;
106*0e209d39SAndroid Build Coastguard Worker 
107*0e209d39SAndroid Build Coastguard Worker     const UnicodeSet *fAllowedCharsSet;   // The UnicodeSet of allowed characters.
108*0e209d39SAndroid Build Coastguard Worker                                           //   for this Spoof Checker.  Defaults to all chars.
109*0e209d39SAndroid Build Coastguard Worker 
110*0e209d39SAndroid Build Coastguard Worker     const char       *fAllowedLocales;    // The list of allowed locales.
111*0e209d39SAndroid Build Coastguard Worker     URestrictionLevel fRestrictionLevel;  // The maximum restriction level for an acceptable identifier.
112*0e209d39SAndroid Build Coastguard Worker };
113*0e209d39SAndroid Build Coastguard Worker 
114*0e209d39SAndroid Build Coastguard Worker /**
115*0e209d39SAndroid Build Coastguard Worker  *  Class CheckResult corresponds directly to the plain C API opaque type
116*0e209d39SAndroid Build Coastguard Worker  *  USpoofCheckResult.  One can be cast to the other.
117*0e209d39SAndroid Build Coastguard Worker  */
118*0e209d39SAndroid Build Coastguard Worker class CheckResult : public UObject,
119*0e209d39SAndroid Build Coastguard Worker         public IcuCApiHelper<USpoofCheckResult, CheckResult, USPOOF_CHECK_MAGIC> {
120*0e209d39SAndroid Build Coastguard Worker public:
121*0e209d39SAndroid Build Coastguard Worker     CheckResult();
122*0e209d39SAndroid Build Coastguard Worker     virtual ~CheckResult();
123*0e209d39SAndroid Build Coastguard Worker 
124*0e209d39SAndroid Build Coastguard Worker     USpoofCheckResult *asUSpoofCheckResult();
125*0e209d39SAndroid Build Coastguard Worker     static CheckResult *validateThis(USpoofCheckResult *ptr, UErrorCode &status);
126*0e209d39SAndroid Build Coastguard Worker     static const CheckResult *validateThis(const USpoofCheckResult *ptr, UErrorCode &status);
127*0e209d39SAndroid Build Coastguard Worker 
128*0e209d39SAndroid Build Coastguard Worker     void clear();
129*0e209d39SAndroid Build Coastguard Worker 
130*0e209d39SAndroid Build Coastguard Worker     // Used to convert this CheckResult to the older int32_t return value API
131*0e209d39SAndroid Build Coastguard Worker     int32_t toCombinedBitmask(int32_t expectedChecks);
132*0e209d39SAndroid Build Coastguard Worker 
133*0e209d39SAndroid Build Coastguard Worker     // Data Members
134*0e209d39SAndroid Build Coastguard Worker     int32_t fChecks;                       // Bit vector of checks that were failed.
135*0e209d39SAndroid Build Coastguard Worker     UnicodeSet fNumerics;                  // Set of numerics found in the string.
136*0e209d39SAndroid Build Coastguard Worker     URestrictionLevel fRestrictionLevel;   // The restriction level of the string.
137*0e209d39SAndroid Build Coastguard Worker };
138*0e209d39SAndroid Build Coastguard Worker 
139*0e209d39SAndroid Build Coastguard Worker 
140*0e209d39SAndroid Build Coastguard Worker //
141*0e209d39SAndroid Build Coastguard Worker //  Confusable Mappings Data Structures, version 2.0
142*0e209d39SAndroid Build Coastguard Worker //
143*0e209d39SAndroid Build Coastguard Worker //    For the confusable data, we are essentially implementing a map,
144*0e209d39SAndroid Build Coastguard Worker //       key:    a code point
145*0e209d39SAndroid Build Coastguard Worker //       value:  a string.  Most commonly one char in length, but can be more.
146*0e209d39SAndroid Build Coastguard Worker //
147*0e209d39SAndroid Build Coastguard Worker //    The keys are stored as a sorted array of 32 bit ints.
148*0e209d39SAndroid Build Coastguard Worker //             bits 0-23    a code point value
149*0e209d39SAndroid Build Coastguard Worker //             bits 24-31   length of value string, in UChars (between 1 and 256 UChars).
150*0e209d39SAndroid Build Coastguard Worker //        The key table is sorted in ascending code point order.  (not on the
151*0e209d39SAndroid Build Coastguard Worker //        32 bit int value, the flag bits do not participate in the sorting.)
152*0e209d39SAndroid Build Coastguard Worker //
153*0e209d39SAndroid Build Coastguard Worker //        Lookup is done by means of a binary search in the key table.
154*0e209d39SAndroid Build Coastguard Worker //
155*0e209d39SAndroid Build Coastguard Worker //    The corresponding values are kept in a parallel array of 16 bit ints.
156*0e209d39SAndroid Build Coastguard Worker //        If the value string is of length 1, it is literally in the value array.
157*0e209d39SAndroid Build Coastguard Worker //        For longer strings, the value array contains an index into the strings table.
158*0e209d39SAndroid Build Coastguard Worker //
159*0e209d39SAndroid Build Coastguard Worker //    String Table:
160*0e209d39SAndroid Build Coastguard Worker //       The strings table contains all of the value strings (those of length two or greater)
161*0e209d39SAndroid Build Coastguard Worker //       concatenated together into one long char16_t (UTF-16) array.
162*0e209d39SAndroid Build Coastguard Worker //
163*0e209d39SAndroid Build Coastguard Worker //       There is no nul character or other mark between adjacent strings.
164*0e209d39SAndroid Build Coastguard Worker //
165*0e209d39SAndroid Build Coastguard Worker //----------------------------------------------------------------------------
166*0e209d39SAndroid Build Coastguard Worker //
167*0e209d39SAndroid Build Coastguard Worker //  Changes from format version 1 to format version 2:
168*0e209d39SAndroid Build Coastguard Worker //      1) Removal of the whole-script confusable data tables.
169*0e209d39SAndroid Build Coastguard Worker //      2) Removal of the SL/SA/ML/MA and multi-table flags in the key bitmask.
170*0e209d39SAndroid Build Coastguard Worker //      3) Expansion of string length value in the key bitmask from 2 bits to 8 bits.
171*0e209d39SAndroid Build Coastguard Worker //      4) Removal of the string lengths table since 8 bits is sufficient for the
172*0e209d39SAndroid Build Coastguard Worker //         lengths of all entries in confusables.txt.
173*0e209d39SAndroid Build Coastguard Worker 
174*0e209d39SAndroid Build Coastguard Worker 
175*0e209d39SAndroid Build Coastguard Worker 
176*0e209d39SAndroid Build Coastguard Worker // Internal functions for manipulating confusable data table keys
177*0e209d39SAndroid Build Coastguard Worker #define USPOOF_CONFUSABLE_DATA_FORMAT_VERSION 2  // version for ICU 58
178*0e209d39SAndroid Build Coastguard Worker class ConfusableDataUtils {
179*0e209d39SAndroid Build Coastguard Worker public:
keyToCodePoint(int32_t key)180*0e209d39SAndroid Build Coastguard Worker     inline static UChar32 keyToCodePoint(int32_t key) {
181*0e209d39SAndroid Build Coastguard Worker         return key & 0x00ffffff;
182*0e209d39SAndroid Build Coastguard Worker     }
keyToLength(int32_t key)183*0e209d39SAndroid Build Coastguard Worker     inline static int32_t keyToLength(int32_t key) {
184*0e209d39SAndroid Build Coastguard Worker         return ((key & 0xff000000) >> 24) + 1;
185*0e209d39SAndroid Build Coastguard Worker     }
codePointAndLengthToKey(UChar32 codePoint,int32_t length)186*0e209d39SAndroid Build Coastguard Worker     inline static int32_t codePointAndLengthToKey(UChar32 codePoint, int32_t length) {
187*0e209d39SAndroid Build Coastguard Worker         U_ASSERT((codePoint & 0x00ffffff) == codePoint);
188*0e209d39SAndroid Build Coastguard Worker         U_ASSERT(length <= 256);
189*0e209d39SAndroid Build Coastguard Worker         return codePoint | ((length - 1) << 24);
190*0e209d39SAndroid Build Coastguard Worker     }
191*0e209d39SAndroid Build Coastguard Worker };
192*0e209d39SAndroid Build Coastguard Worker 
193*0e209d39SAndroid Build Coastguard Worker 
194*0e209d39SAndroid Build Coastguard Worker //-------------------------------------------------------------------------------------
195*0e209d39SAndroid Build Coastguard Worker //
196*0e209d39SAndroid Build Coastguard Worker //  SpoofData
197*0e209d39SAndroid Build Coastguard Worker //
198*0e209d39SAndroid Build Coastguard Worker //    A small class that wraps the raw (usually memory mapped) spoof data.
199*0e209d39SAndroid Build Coastguard Worker //    Serves two primary functions:
200*0e209d39SAndroid Build Coastguard Worker //      1.  Convenience.  Contains real pointers to the data, to avoid dealing with
201*0e209d39SAndroid Build Coastguard Worker //          the offsets in the raw data.
202*0e209d39SAndroid Build Coastguard Worker //      2.  Reference counting.  When a spoof checker is cloned, the raw data is shared
203*0e209d39SAndroid Build Coastguard Worker //          and must be retained until all checkers using the data are closed.
204*0e209d39SAndroid Build Coastguard Worker //    Nothing in this struct includes state that is specific to any particular
205*0e209d39SAndroid Build Coastguard Worker //    USpoofDetector object.
206*0e209d39SAndroid Build Coastguard Worker //
207*0e209d39SAndroid Build Coastguard Worker //---------------------------------------------------------------------------------------
208*0e209d39SAndroid Build Coastguard Worker class SpoofData: public UMemory {
209*0e209d39SAndroid Build Coastguard Worker   public:
210*0e209d39SAndroid Build Coastguard Worker     static SpoofData* getDefault(UErrorCode &status);   // Get standard ICU spoof data.
211*0e209d39SAndroid Build Coastguard Worker     static void releaseDefault();   // Cleanup reference to default spoof data.
212*0e209d39SAndroid Build Coastguard Worker 
213*0e209d39SAndroid Build Coastguard Worker     SpoofData(UErrorCode &status);   // Create new spoof data wrapper.
214*0e209d39SAndroid Build Coastguard Worker                                      // Only used when building new data from rules.
215*0e209d39SAndroid Build Coastguard Worker 
216*0e209d39SAndroid Build Coastguard Worker     // Constructor for use when creating from prebuilt default data.
217*0e209d39SAndroid Build Coastguard Worker     //   A UDataMemory is what the ICU internal data loading functions provide.
218*0e209d39SAndroid Build Coastguard Worker     //   The udm is adopted by the SpoofData.
219*0e209d39SAndroid Build Coastguard Worker     SpoofData(UDataMemory *udm, UErrorCode &status);
220*0e209d39SAndroid Build Coastguard Worker 
221*0e209d39SAndroid Build Coastguard Worker     // Constructor for use when creating from serialized data.
222*0e209d39SAndroid Build Coastguard Worker     //
223*0e209d39SAndroid Build Coastguard Worker     SpoofData(const void *serializedData, int32_t length, UErrorCode &status);
224*0e209d39SAndroid Build Coastguard Worker 
225*0e209d39SAndroid Build Coastguard Worker     //  Check raw Spoof Data Version compatibility.
226*0e209d39SAndroid Build Coastguard Worker     //  Return true it looks good.
227*0e209d39SAndroid Build Coastguard Worker     UBool validateDataVersion(UErrorCode &status) const;
228*0e209d39SAndroid Build Coastguard Worker 
229*0e209d39SAndroid Build Coastguard Worker     ~SpoofData();                    // Destructor not normally used.
230*0e209d39SAndroid Build Coastguard Worker                                      // Use removeReference() instead.
231*0e209d39SAndroid Build Coastguard Worker     // Reference Counting functions.
232*0e209d39SAndroid Build Coastguard Worker     //    Clone of a user-level spoof detector increments the ref count on the data.
233*0e209d39SAndroid Build Coastguard Worker     //    Close of a user-level spoof detector decrements the ref count.
234*0e209d39SAndroid Build Coastguard Worker     //    If the data is owned by us, it will be deleted when count goes to zero.
235*0e209d39SAndroid Build Coastguard Worker     SpoofData *addReference();
236*0e209d39SAndroid Build Coastguard Worker     void removeReference();
237*0e209d39SAndroid Build Coastguard Worker 
238*0e209d39SAndroid Build Coastguard Worker     // Reset all fields to an initial state.
239*0e209d39SAndroid Build Coastguard Worker     // Called from the top of all constructors.
240*0e209d39SAndroid Build Coastguard Worker     void reset();
241*0e209d39SAndroid Build Coastguard Worker 
242*0e209d39SAndroid Build Coastguard Worker     // Copy this instance's raw data buffer to the specified address.
243*0e209d39SAndroid Build Coastguard Worker     int32_t serialize(void *buf, int32_t capacity, UErrorCode &status) const;
244*0e209d39SAndroid Build Coastguard Worker 
245*0e209d39SAndroid Build Coastguard Worker     // Get the total number of bytes of data backed by this SpoofData.
246*0e209d39SAndroid Build Coastguard Worker     // Not to be confused with length, which returns the number of confusable entries.
247*0e209d39SAndroid Build Coastguard Worker     int32_t size() const;
248*0e209d39SAndroid Build Coastguard Worker 
249*0e209d39SAndroid Build Coastguard Worker     // Get the confusable skeleton transform for a single code point.
250*0e209d39SAndroid Build Coastguard Worker     // The result is a string with a length between 1 and 18 as of Unicode 9.
251*0e209d39SAndroid Build Coastguard Worker     // This is the main public endpoint for this class.
252*0e209d39SAndroid Build Coastguard Worker     // @return   The length in UTF-16 code units of the substitution string.
253*0e209d39SAndroid Build Coastguard Worker     int32_t confusableLookup(UChar32 inChar, UnicodeString &dest) const;
254*0e209d39SAndroid Build Coastguard Worker 
255*0e209d39SAndroid Build Coastguard Worker     // Get the number of confusable entries in this SpoofData.
256*0e209d39SAndroid Build Coastguard Worker     int32_t length() const;
257*0e209d39SAndroid Build Coastguard Worker 
258*0e209d39SAndroid Build Coastguard Worker     // Get the code point (key) at the specified index.
259*0e209d39SAndroid Build Coastguard Worker     UChar32 codePointAt(int32_t index) const;
260*0e209d39SAndroid Build Coastguard Worker 
261*0e209d39SAndroid Build Coastguard Worker     // Get the confusable skeleton (value) at the specified index.
262*0e209d39SAndroid Build Coastguard Worker     // Append it to the specified UnicodeString&.
263*0e209d39SAndroid Build Coastguard Worker     // @return   The length in UTF-16 code units of the skeleton string.
264*0e209d39SAndroid Build Coastguard Worker     int32_t appendValueTo(int32_t index, UnicodeString& dest) const;
265*0e209d39SAndroid Build Coastguard Worker 
266*0e209d39SAndroid Build Coastguard Worker   private:
267*0e209d39SAndroid Build Coastguard Worker     // Reserve space in the raw data.  For use by builder when putting together a
268*0e209d39SAndroid Build Coastguard Worker     //   new set of data.  Init the new storage to zero, to prevent inconsistent
269*0e209d39SAndroid Build Coastguard Worker     //   results if it is not all otherwise set by the requester.
270*0e209d39SAndroid Build Coastguard Worker     //  Return:
271*0e209d39SAndroid Build Coastguard Worker     //    pointer to the new space that was added by this function.
272*0e209d39SAndroid Build Coastguard Worker     void *reserveSpace(int32_t numBytes, UErrorCode &status);
273*0e209d39SAndroid Build Coastguard Worker 
274*0e209d39SAndroid Build Coastguard Worker     // initialize the pointers from this object to the raw data.
275*0e209d39SAndroid Build Coastguard Worker     void initPtrs(UErrorCode &status);
276*0e209d39SAndroid Build Coastguard Worker 
277*0e209d39SAndroid Build Coastguard Worker     SpoofDataHeader             *fRawData;          // Ptr to the raw memory-mapped data
278*0e209d39SAndroid Build Coastguard Worker     UBool                       fDataOwned;         // True if the raw data is owned, and needs
279*0e209d39SAndroid Build Coastguard Worker                                                     //  to be deleted when refcount goes to zero.
280*0e209d39SAndroid Build Coastguard Worker     UDataMemory                 *fUDM;              // If not nullptr, our data came from a
281*0e209d39SAndroid Build Coastguard Worker                                                     //   UDataMemory, which we must close when
282*0e209d39SAndroid Build Coastguard Worker                                                     //   we are done.
283*0e209d39SAndroid Build Coastguard Worker 
284*0e209d39SAndroid Build Coastguard Worker     uint32_t                    fMemLimit;          // Limit of available raw data space
285*0e209d39SAndroid Build Coastguard Worker     u_atomic_int32_t            fRefCount;
286*0e209d39SAndroid Build Coastguard Worker 
287*0e209d39SAndroid Build Coastguard Worker     // Confusable data
288*0e209d39SAndroid Build Coastguard Worker     int32_t                     *fCFUKeys;
289*0e209d39SAndroid Build Coastguard Worker     uint16_t                    *fCFUValues;
290*0e209d39SAndroid Build Coastguard Worker     char16_t                    *fCFUStrings;
291*0e209d39SAndroid Build Coastguard Worker 
292*0e209d39SAndroid Build Coastguard Worker     friend class ConfusabledataBuilder;
293*0e209d39SAndroid Build Coastguard Worker };
294*0e209d39SAndroid Build Coastguard Worker 
295*0e209d39SAndroid Build Coastguard Worker //---------------------------------------------------------------------------------------
296*0e209d39SAndroid Build Coastguard Worker //
297*0e209d39SAndroid Build Coastguard Worker //  Raw Binary Data Formats, as loaded from the ICU data file,
298*0e209d39SAndroid Build Coastguard Worker //    or as built by the builder.
299*0e209d39SAndroid Build Coastguard Worker //
300*0e209d39SAndroid Build Coastguard Worker //---------------------------------------------------------------------------------------
301*0e209d39SAndroid Build Coastguard Worker struct SpoofDataHeader {
302*0e209d39SAndroid Build Coastguard Worker     int32_t       fMagic;                // (0x3845fdef)
303*0e209d39SAndroid Build Coastguard Worker     uint8_t       fFormatVersion[4];     // Data Format. Same as the value in struct UDataInfo
304*0e209d39SAndroid Build Coastguard Worker                                          //   if there is one associated with this data.
305*0e209d39SAndroid Build Coastguard Worker     int32_t       fLength;               // Total length in bytes of this spoof data,
306*0e209d39SAndroid Build Coastguard Worker                                          //   including all sections, not just the header.
307*0e209d39SAndroid Build Coastguard Worker 
308*0e209d39SAndroid Build Coastguard Worker     // The following four sections refer to data representing the confusable data
309*0e209d39SAndroid Build Coastguard Worker     //   from the Unicode.org data from "confusables.txt"
310*0e209d39SAndroid Build Coastguard Worker 
311*0e209d39SAndroid Build Coastguard Worker     int32_t       fCFUKeys;               // byte offset to Keys table (from SpoofDataHeader *)
312*0e209d39SAndroid Build Coastguard Worker     int32_t       fCFUKeysSize;           // number of entries in keys table  (32 bits each)
313*0e209d39SAndroid Build Coastguard Worker 
314*0e209d39SAndroid Build Coastguard Worker     // TODO: change name to fCFUValues, for consistency.
315*0e209d39SAndroid Build Coastguard Worker     int32_t       fCFUStringIndex;        // byte offset to String Indexes table
316*0e209d39SAndroid Build Coastguard Worker     int32_t       fCFUStringIndexSize;    // number of entries in String Indexes table (16 bits each)
317*0e209d39SAndroid Build Coastguard Worker                                           //     (number of entries must be same as in Keys table
318*0e209d39SAndroid Build Coastguard Worker 
319*0e209d39SAndroid Build Coastguard Worker     int32_t       fCFUStringTable;        // byte offset of String table
320*0e209d39SAndroid Build Coastguard Worker     int32_t       fCFUStringTableLen;     // length of string table (in 16 bit UChars)
321*0e209d39SAndroid Build Coastguard Worker 
322*0e209d39SAndroid Build Coastguard Worker     // The following sections are for data from xidmodifications.txt
323*0e209d39SAndroid Build Coastguard Worker 
324*0e209d39SAndroid Build Coastguard Worker     int32_t       unused[15];              // Padding, Room for Expansion
325*0e209d39SAndroid Build Coastguard Worker };
326*0e209d39SAndroid Build Coastguard Worker 
327*0e209d39SAndroid Build Coastguard Worker 
328*0e209d39SAndroid Build Coastguard Worker 
329*0e209d39SAndroid Build Coastguard Worker U_NAMESPACE_END
330*0e209d39SAndroid Build Coastguard Worker #endif /* __cplusplus */
331*0e209d39SAndroid Build Coastguard Worker 
332*0e209d39SAndroid Build Coastguard Worker /**
333*0e209d39SAndroid Build Coastguard Worker   * Endianness swap function for binary spoof data.
334*0e209d39SAndroid Build Coastguard Worker   * @internal
335*0e209d39SAndroid Build Coastguard Worker   */
336*0e209d39SAndroid Build Coastguard Worker U_CAPI int32_t U_EXPORT2
337*0e209d39SAndroid Build Coastguard Worker uspoof_swap(const UDataSwapper *ds, const void *inData, int32_t length, void *outData,
338*0e209d39SAndroid Build Coastguard Worker             UErrorCode *status);
339*0e209d39SAndroid Build Coastguard Worker 
340*0e209d39SAndroid Build Coastguard Worker 
341*0e209d39SAndroid Build Coastguard Worker #endif
342*0e209d39SAndroid Build Coastguard Worker 
343*0e209d39SAndroid Build Coastguard Worker #endif  /* USPOOFIM_H */
344*0e209d39SAndroid Build Coastguard Worker 
345