intl/icu/source/common/ucmndata.c

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /*
michael@0 2 ******************************************************************************
michael@0 3 *
michael@0 4 * Copyright (C) 1999-2011, International Business Machines
michael@0 5 * Corporation and others. All Rights Reserved.
michael@0 6 *
michael@0 7 ******************************************************************************/
michael@0 8
michael@0 9
michael@0 10 /*------------------------------------------------------------------------------
michael@0 11 *
michael@0 12 * UCommonData An abstract interface for dealing with ICU Common Data Files.
michael@0 13 * ICU Common Data Files are a grouping of a number of individual
michael@0 14 * data items (resources, converters, tables, anything) into a
michael@0 15 * single file or dll. The combined format includes a table of
michael@0 16 * contents for locating the individual items by name.
michael@0 17 *
michael@0 18 * Two formats for the table of contents are supported, which is
michael@0 19 * why there is an abstract inteface involved.
michael@0 20 *
michael@0 21 */
michael@0 22
michael@0 23 #include "unicode/utypes.h"
michael@0 24 #include "unicode/udata.h"
michael@0 25 #include "cstring.h"
michael@0 26 #include "ucmndata.h"
michael@0 27 #include "udatamem.h"
michael@0 28
michael@0 29 #if defined(UDATA_DEBUG) || defined(UDATA_DEBUG_DUMP)
michael@0 30 # include <stdio.h>
michael@0 31 #endif
michael@0 32
michael@0 33 U_CFUNC uint16_t
michael@0 34 udata_getHeaderSize(const DataHeader *udh) {
michael@0 35 if(udh==NULL) {
michael@0 36 return 0;
michael@0 37 } else if(udh->info.isBigEndian==U_IS_BIG_ENDIAN) {
michael@0 38 /* same endianness */
michael@0 39 return udh->dataHeader.headerSize;
michael@0 40 } else {
michael@0 41 /* opposite endianness */
michael@0 42 uint16_t x=udh->dataHeader.headerSize;
michael@0 43 return (uint16_t)((x<<8)|(x>>8));
michael@0 44 }
michael@0 45 }
michael@0 46
michael@0 47 U_CFUNC uint16_t
michael@0 48 udata_getInfoSize(const UDataInfo *info) {
michael@0 49 if(info==NULL) {
michael@0 50 return 0;
michael@0 51 } else if(info->isBigEndian==U_IS_BIG_ENDIAN) {
michael@0 52 /* same endianness */
michael@0 53 return info->size;
michael@0 54 } else {
michael@0 55 /* opposite endianness */
michael@0 56 uint16_t x=info->size;
michael@0 57 return (uint16_t)((x<<8)|(x>>8));
michael@0 58 }
michael@0 59 }
michael@0 60
michael@0 61 /*-----------------------------------------------------------------------------*
michael@0 62 * *
michael@0 63 * Pointer TOCs. TODO: This form of table-of-contents should be removed *
michael@0 64 * because DLLs must be relocated on loading to correct the *
michael@0 65 * pointer values and this operation makes shared memory *
michael@0 66 * mapping of the data much less likely to work. *
michael@0 67 * *
michael@0 68 *-----------------------------------------------------------------------------*/
michael@0 69 typedef struct {
michael@0 70 const char *entryName;
michael@0 71 const DataHeader *pHeader;
michael@0 72 } PointerTOCEntry;
michael@0 73
michael@0 74
michael@0 75 typedef struct {
michael@0 76 uint32_t count;
michael@0 77 uint32_t reserved;
michael@0 78 PointerTOCEntry entry[2]; /* Actual size is from count. */
michael@0 79 } PointerTOC;
michael@0 80
michael@0 81
michael@0 82 /* definition of OffsetTOC struct types moved to ucmndata.h */
michael@0 83
michael@0 84 /*-----------------------------------------------------------------------------*
michael@0 85 * *
michael@0 86 * entry point lookup implementations *
michael@0 87 * *
michael@0 88 *-----------------------------------------------------------------------------*/
michael@0 89
michael@0 90 #ifndef MIN
michael@0 91 #define MIN(a,b) (((a)<(b)) ? (a) : (b))
michael@0 92 #endif
michael@0 93
michael@0 94 /**
michael@0 95 * Compare strings where we know the shared prefix length,
michael@0 96 * and advance the prefix length as we find that the strings share even more characters.
michael@0 97 */
michael@0 98 static int32_t
michael@0 99 strcmpAfterPrefix(const char *s1, const char *s2, int32_t *pPrefixLength) {
michael@0 100 int32_t pl=*pPrefixLength;
michael@0 101 int32_t cmp=0;
michael@0 102 s1+=pl;
michael@0 103 s2+=pl;
michael@0 104 for(;;) {
michael@0 105 int32_t c1=(uint8_t)*s1++;
michael@0 106 int32_t c2=(uint8_t)*s2++;
michael@0 107 cmp=c1-c2;
michael@0 108 if(cmp!=0 || c1==0) { /* different or done */
michael@0 109 break;
michael@0 110 }
michael@0 111 ++pl; /* increment shared same-prefix length */
michael@0 112 }
michael@0 113 *pPrefixLength=pl;
michael@0 114 return cmp;
michael@0 115 }
michael@0 116
michael@0 117 static int32_t
michael@0 118 offsetTOCPrefixBinarySearch(const char *s, const char *names,
michael@0 119 const UDataOffsetTOCEntry *toc, int32_t count) {
michael@0 120 int32_t start=0;
michael@0 121 int32_t limit=count;
michael@0 122 /*
michael@0 123 * Remember the shared prefix between s, start and limit,
michael@0 124 * and don't compare that shared prefix again.
michael@0 125 * The shared prefix should get longer as we narrow the [start, limit[ range.
michael@0 126 */
michael@0 127 int32_t startPrefixLength=0;
michael@0 128 int32_t limitPrefixLength=0;
michael@0 129 if(count==0) {
michael@0 130 return -1;
michael@0 131 }
michael@0 132 /*
michael@0 133 * Prime the prefix lengths so that we don't keep prefixLength at 0 until
michael@0 134 * both the start and limit indexes have moved.
michael@0 135 * At the same time, we find if s is one of the start and (limit-1) names,
michael@0 136 * and if not, exclude them from the actual binary search.
michael@0 137 */
michael@0 138 if(0==strcmpAfterPrefix(s, names+toc[0].nameOffset, &startPrefixLength)) {
michael@0 139 return 0;
michael@0 140 }
michael@0 141 ++start;
michael@0 142 --limit;
michael@0 143 if(0==strcmpAfterPrefix(s, names+toc[limit].nameOffset, &limitPrefixLength)) {
michael@0 144 return limit;
michael@0 145 }
michael@0 146 while(start<limit) {
michael@0 147 int32_t i=(start+limit)/2;
michael@0 148 int32_t prefixLength=MIN(startPrefixLength, limitPrefixLength);
michael@0 149 int32_t cmp=strcmpAfterPrefix(s, names+toc[i].nameOffset, &prefixLength);
michael@0 150 if(cmp<0) {
michael@0 151 limit=i;
michael@0 152 limitPrefixLength=prefixLength;
michael@0 153 } else if(cmp==0) {
michael@0 154 return i;
michael@0 155 } else {
michael@0 156 start=i+1;
michael@0 157 startPrefixLength=prefixLength;
michael@0 158 }
michael@0 159 }
michael@0 160 return -1;
michael@0 161 }
michael@0 162
michael@0 163 static int32_t
michael@0 164 pointerTOCPrefixBinarySearch(const char *s, const PointerTOCEntry *toc, int32_t count) {
michael@0 165 int32_t start=0;
michael@0 166 int32_t limit=count;
michael@0 167 /*
michael@0 168 * Remember the shared prefix between s, start and limit,
michael@0 169 * and don't compare that shared prefix again.
michael@0 170 * The shared prefix should get longer as we narrow the [start, limit[ range.
michael@0 171 */
michael@0 172 int32_t startPrefixLength=0;
michael@0 173 int32_t limitPrefixLength=0;
michael@0 174 if(count==0) {
michael@0 175 return -1;
michael@0 176 }
michael@0 177 /*
michael@0 178 * Prime the prefix lengths so that we don't keep prefixLength at 0 until
michael@0 179 * both the start and limit indexes have moved.
michael@0 180 * At the same time, we find if s is one of the start and (limit-1) names,
michael@0 181 * and if not, exclude them from the actual binary search.
michael@0 182 */
michael@0 183 if(0==strcmpAfterPrefix(s, toc[0].entryName, &startPrefixLength)) {
michael@0 184 return 0;
michael@0 185 }
michael@0 186 ++start;
michael@0 187 --limit;
michael@0 188 if(0==strcmpAfterPrefix(s, toc[limit].entryName, &limitPrefixLength)) {
michael@0 189 return limit;
michael@0 190 }
michael@0 191 while(start<limit) {
michael@0 192 int32_t i=(start+limit)/2;
michael@0 193 int32_t prefixLength=MIN(startPrefixLength, limitPrefixLength);
michael@0 194 int32_t cmp=strcmpAfterPrefix(s, toc[i].entryName, &prefixLength);
michael@0 195 if(cmp<0) {
michael@0 196 limit=i;
michael@0 197 limitPrefixLength=prefixLength;
michael@0 198 } else if(cmp==0) {
michael@0 199 return i;
michael@0 200 } else {
michael@0 201 start=i+1;
michael@0 202 startPrefixLength=prefixLength;
michael@0 203 }
michael@0 204 }
michael@0 205 return -1;
michael@0 206 }
michael@0 207
michael@0 208 static uint32_t offsetTOCEntryCount(const UDataMemory *pData) {
michael@0 209 int32_t retVal=0;
michael@0 210 const UDataOffsetTOC *toc = (UDataOffsetTOC *)pData->toc;
michael@0 211 if (toc != NULL) {
michael@0 212 retVal = toc->count;
michael@0 213 }
michael@0 214 return retVal;
michael@0 215 }
michael@0 216
michael@0 217 static const DataHeader *
michael@0 218 offsetTOCLookupFn(const UDataMemory *pData,
michael@0 219 const char *tocEntryName,
michael@0 220 int32_t *pLength,
michael@0 221 UErrorCode *pErrorCode) {
michael@0 222 const UDataOffsetTOC *toc = (UDataOffsetTOC *)pData->toc;
michael@0 223 if(toc!=NULL) {
michael@0 224 const char *base=(const char *)toc;
michael@0 225 int32_t number, count=(int32_t)toc->count;
michael@0 226
michael@0 227 /* perform a binary search for the data in the common data's table of contents */
michael@0 228 #if defined (UDATA_DEBUG_DUMP)
michael@0 229 /* list the contents of the TOC each time .. not recommended */
michael@0 230 for(number=0; number<count; ++number) {
michael@0 231 fprintf(stderr, "\tx%d: %s\n", number, &base[toc->entry[number].nameOffset]);
michael@0 232 }
michael@0 233 #endif
michael@0 234 number=offsetTOCPrefixBinarySearch(tocEntryName, base, toc->entry, count);
michael@0 235 if(number>=0) {
michael@0 236 /* found it */
michael@0 237 const UDataOffsetTOCEntry *entry=toc->entry+number;
michael@0 238 #ifdef UDATA_DEBUG
michael@0 239 fprintf(stderr, "%s: Found.\n", tocEntryName);
michael@0 240 #endif
michael@0 241 if((number+1) < count) {
michael@0 242 *pLength = (int32_t)(entry[1].dataOffset - entry->dataOffset);
michael@0 243 } else {
michael@0 244 *pLength = -1;
michael@0 245 }
michael@0 246 return (const DataHeader *)(base+entry->dataOffset);
michael@0 247 } else {
michael@0 248 #ifdef UDATA_DEBUG
michael@0 249 fprintf(stderr, "%s: Not found.\n", tocEntryName);
michael@0 250 #endif
michael@0 251 return NULL;
michael@0 252 }
michael@0 253 } else {
michael@0 254 #ifdef UDATA_DEBUG
michael@0 255 fprintf(stderr, "returning header\n");
michael@0 256 #endif
michael@0 257
michael@0 258 return pData->pHeader;
michael@0 259 }
michael@0 260 }
michael@0 261
michael@0 262
michael@0 263 static uint32_t pointerTOCEntryCount(const UDataMemory *pData) {
michael@0 264 const PointerTOC *toc = (PointerTOC *)pData->toc;
michael@0 265 return (uint32_t)((toc != NULL) ? (toc->count) : 0);
michael@0 266 }
michael@0 267
michael@0 268
michael@0 269 static const DataHeader *pointerTOCLookupFn(const UDataMemory *pData,
michael@0 270 const char *name,
michael@0 271 int32_t *pLength,
michael@0 272 UErrorCode *pErrorCode) {
michael@0 273 if(pData->toc!=NULL) {
michael@0 274 const PointerTOC *toc = (PointerTOC *)pData->toc;
michael@0 275 int32_t number, count=(int32_t)toc->count;
michael@0 276
michael@0 277 #if defined (UDATA_DEBUG_DUMP)
michael@0 278 /* list the contents of the TOC each time .. not recommended */
michael@0 279 for(number=0; number<count; ++number) {
michael@0 280 fprintf(stderr, "\tx%d: %s\n", number, toc->entry[number].entryName);
michael@0 281 }
michael@0 282 #endif
michael@0 283 number=pointerTOCPrefixBinarySearch(name, toc->entry, count);
michael@0 284 if(number>=0) {
michael@0 285 /* found it */
michael@0 286 #ifdef UDATA_DEBUG
michael@0 287 fprintf(stderr, "%s: Found.\n", toc->entry[number].entryName);
michael@0 288 #endif
michael@0 289 *pLength=-1;
michael@0 290 return UDataMemory_normalizeDataPointer(toc->entry[number].pHeader);
michael@0 291 } else {
michael@0 292 #ifdef UDATA_DEBUG
michael@0 293 fprintf(stderr, "%s: Not found.\n", name);
michael@0 294 #endif
michael@0 295 return NULL;
michael@0 296 }
michael@0 297 } else {
michael@0 298 return pData->pHeader;
michael@0 299 }
michael@0 300 }
michael@0 301
michael@0 302 static const commonDataFuncs CmnDFuncs = {offsetTOCLookupFn, offsetTOCEntryCount};
michael@0 303 static const commonDataFuncs ToCPFuncs = {pointerTOCLookupFn, pointerTOCEntryCount};
michael@0 304
michael@0 305
michael@0 306
michael@0 307 /*----------------------------------------------------------------------*
michael@0 308 * *
michael@0 309 * checkCommonData Validate the format of a common data file. *
michael@0 310 * Fill in the virtual function ptr based on TOC type *
michael@0 311 * If the data is invalid, close the UDataMemory *
michael@0 312 * and set the appropriate error code. *
michael@0 313 * *
michael@0 314 *----------------------------------------------------------------------*/
michael@0 315 U_CFUNC void udata_checkCommonData(UDataMemory *udm, UErrorCode *err) {
michael@0 316 if (U_FAILURE(*err)) {
michael@0 317 return;
michael@0 318 }
michael@0 319
michael@0 320 if(udm==NULL || udm->pHeader==NULL) {
michael@0 321 *err=U_INVALID_FORMAT_ERROR;
michael@0 322 } else if(!(udm->pHeader->dataHeader.magic1==0xda &&
michael@0 323 udm->pHeader->dataHeader.magic2==0x27 &&
michael@0 324 udm->pHeader->info.isBigEndian==U_IS_BIG_ENDIAN &&
michael@0 325 udm->pHeader->info.charsetFamily==U_CHARSET_FAMILY)
michael@0 326 ) {
michael@0 327 /* header not valid */
michael@0 328 *err=U_INVALID_FORMAT_ERROR;
michael@0 329 }
michael@0 330 else if (udm->pHeader->info.dataFormat[0]==0x43 &&
michael@0 331 udm->pHeader->info.dataFormat[1]==0x6d &&
michael@0 332 udm->pHeader->info.dataFormat[2]==0x6e &&
michael@0 333 udm->pHeader->info.dataFormat[3]==0x44 &&
michael@0 334 udm->pHeader->info.formatVersion[0]==1
michael@0 335 ) {
michael@0 336 /* dataFormat="CmnD" */
michael@0 337 udm->vFuncs = &CmnDFuncs;
michael@0 338 udm->toc=(const char *)udm->pHeader+udata_getHeaderSize(udm->pHeader);
michael@0 339 }
michael@0 340 else if(udm->pHeader->info.dataFormat[0]==0x54 &&
michael@0 341 udm->pHeader->info.dataFormat[1]==0x6f &&
michael@0 342 udm->pHeader->info.dataFormat[2]==0x43 &&
michael@0 343 udm->pHeader->info.dataFormat[3]==0x50 &&
michael@0 344 udm->pHeader->info.formatVersion[0]==1
michael@0 345 ) {
michael@0 346 /* dataFormat="ToCP" */
michael@0 347 udm->vFuncs = &ToCPFuncs;
michael@0 348 udm->toc=(const char *)udm->pHeader+udata_getHeaderSize(udm->pHeader);
michael@0 349 }
michael@0 350 else {
michael@0 351 /* dataFormat not recognized */
michael@0 352 *err=U_INVALID_FORMAT_ERROR;
michael@0 353 }
michael@0 354
michael@0 355 if (U_FAILURE(*err)) {
michael@0 356 /* If the data is no good and we memory-mapped it ourselves,
michael@0 357 * close the memory mapping so it doesn't leak. Note that this has
michael@0 358 * no effect on non-memory mapped data, other than clearing fields in udm.
michael@0 359 */
michael@0 360 udata_close(udm);
michael@0 361 }
michael@0 362 }
michael@0 363
michael@0 364 /*
michael@0 365 * TODO: Add a udata_swapPackageHeader() function that swaps an ICU .dat package
michael@0 366 * header but not its sub-items.
michael@0 367 * This function will be needed for automatic runtime swapping.
michael@0 368 * Sub-items should not be swapped to limit the swapping to the parts of the
michael@0 369 * package that are actually used.
michael@0 370 *
michael@0 371 * Since lengths of items are implicit in the order and offsets of their
michael@0 372 * ToC entries, and since offsets are relative to the start of the ToC,
michael@0 373 * a swapped version may need to generate a different data structure
michael@0 374 * with pointers to the original data items and with their lengths
michael@0 375 * (-1 for the last one if it is not known), and maybe even pointers to the
michael@0 376 * swapped versions of the items.
michael@0 377 * These pointers to swapped versions would establish a cache;
michael@0 378 * instead, each open data item could simply own the storage for its swapped
michael@0 379 * data. This fits better with the current design.
michael@0 380 *
michael@0 381 * markus 2003sep18 Jitterbug 2235
michael@0 382 */

mercurial