intl/icu/source/tools/genrb/reslist.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /*
michael@0 2 *******************************************************************************
michael@0 3 *
michael@0 4 * Copyright (C) 2000-2012, International Business Machines
michael@0 5 * Corporation and others. All Rights Reserved.
michael@0 6 *
michael@0 7 *******************************************************************************
michael@0 8 *
michael@0 9 * File reslist.c
michael@0 10 *
michael@0 11 * Modification History:
michael@0 12 *
michael@0 13 * Date Name Description
michael@0 14 * 02/21/00 weiv Creation.
michael@0 15 *******************************************************************************
michael@0 16 */
michael@0 17
michael@0 18 #include <assert.h>
michael@0 19 #include <stdio.h>
michael@0 20 #include "reslist.h"
michael@0 21 #include "unewdata.h"
michael@0 22 #include "unicode/ures.h"
michael@0 23 #include "unicode/putil.h"
michael@0 24 #include "errmsg.h"
michael@0 25
michael@0 26 #include "uarrsort.h"
michael@0 27 #include "uelement.h"
michael@0 28 #include "uinvchar.h"
michael@0 29 #include "ustr_imp.h"
michael@0 30 #include "unicode/utf16.h"
michael@0 31 /*
michael@0 32 * Align binary data at a 16-byte offset from the start of the resource bundle,
michael@0 33 * to be safe for any data type it may contain.
michael@0 34 */
michael@0 35 #define BIN_ALIGNMENT 16
michael@0 36
michael@0 37 static UBool gIncludeCopyright = FALSE;
michael@0 38 static UBool gUsePoolBundle = FALSE;
michael@0 39 static int32_t gFormatVersion = 2;
michael@0 40
michael@0 41 static UChar gEmptyString = 0;
michael@0 42
michael@0 43 /* How do we store string values? */
michael@0 44 enum {
michael@0 45 STRINGS_UTF16_V1, /* formatVersion 1: int length + UChars + NUL + padding to 4 bytes */
michael@0 46 STRINGS_UTF16_V2 /* formatVersion 2: optional length in 1..3 UChars + UChars + NUL */
michael@0 47 };
michael@0 48
michael@0 49 enum {
michael@0 50 MAX_IMPLICIT_STRING_LENGTH = 40 /* do not store the length explicitly for such strings */
michael@0 51 };
michael@0 52
michael@0 53 /*
michael@0 54 * res_none() returns the address of kNoResource,
michael@0 55 * for use in non-error cases when no resource is to be added to the bundle.
michael@0 56 * (NULL is used in error cases.)
michael@0 57 */
michael@0 58 static const struct SResource kNoResource = { URES_NONE };
michael@0 59
michael@0 60 static UDataInfo dataInfo= {
michael@0 61 sizeof(UDataInfo),
michael@0 62 0,
michael@0 63
michael@0 64 U_IS_BIG_ENDIAN,
michael@0 65 U_CHARSET_FAMILY,
michael@0 66 sizeof(UChar),
michael@0 67 0,
michael@0 68
michael@0 69 {0x52, 0x65, 0x73, 0x42}, /* dataFormat="ResB" */
michael@0 70 {1, 3, 0, 0}, /* formatVersion */
michael@0 71 {1, 4, 0, 0} /* dataVersion take a look at version inside parsed resb*/
michael@0 72 };
michael@0 73
michael@0 74 static const UVersionInfo gFormatVersions[3] = { /* indexed by a major-formatVersion integer */
michael@0 75 { 0, 0, 0, 0 },
michael@0 76 { 1, 3, 0, 0 },
michael@0 77 { 2, 0, 0, 0 }
michael@0 78 };
michael@0 79
michael@0 80 static uint8_t calcPadding(uint32_t size) {
michael@0 81 /* returns space we need to pad */
michael@0 82 return (uint8_t) ((size % sizeof(uint32_t)) ? (sizeof(uint32_t) - (size % sizeof(uint32_t))) : 0);
michael@0 83
michael@0 84 }
michael@0 85
michael@0 86 void setIncludeCopyright(UBool val){
michael@0 87 gIncludeCopyright=val;
michael@0 88 }
michael@0 89
michael@0 90 UBool getIncludeCopyright(void){
michael@0 91 return gIncludeCopyright;
michael@0 92 }
michael@0 93
michael@0 94 void setFormatVersion(int32_t formatVersion) {
michael@0 95 gFormatVersion = formatVersion;
michael@0 96 }
michael@0 97
michael@0 98 void setUsePoolBundle(UBool use) {
michael@0 99 gUsePoolBundle = use;
michael@0 100 }
michael@0 101
michael@0 102 static void
michael@0 103 bundle_compactStrings(struct SRBRoot *bundle, UErrorCode *status);
michael@0 104
michael@0 105 /* Writing Functions */
michael@0 106
michael@0 107 /*
michael@0 108 * type_write16() functions write resource values into f16BitUnits
michael@0 109 * and determine the resource item word, if possible.
michael@0 110 */
michael@0 111 static void
michael@0 112 res_write16(struct SRBRoot *bundle, struct SResource *res,
michael@0 113 UErrorCode *status);
michael@0 114
michael@0 115 /*
michael@0 116 * type_preWrite() functions calculate ("preflight") and advance the *byteOffset
michael@0 117 * by the size of their data in the binary file and
michael@0 118 * determine the resource item word.
michael@0 119 * Most type_preWrite() functions may add any number of bytes, but res_preWrite()
michael@0 120 * will always pad it to a multiple of 4.
michael@0 121 * The resource item type may be a related subtype of the fType.
michael@0 122 *
michael@0 123 * The type_preWrite() and type_write() functions start and end at the same
michael@0 124 * byteOffset values.
michael@0 125 * Prewriting allows bundle_write() to determine the root resource item word,
michael@0 126 * before actually writing the bundle contents to the file,
michael@0 127 * which is necessary because the root item is stored at the beginning.
michael@0 128 */
michael@0 129 static void
michael@0 130 res_preWrite(uint32_t *byteOffset,
michael@0 131 struct SRBRoot *bundle, struct SResource *res,
michael@0 132 UErrorCode *status);
michael@0 133
michael@0 134 /*
michael@0 135 * type_write() functions write their data to mem and update the byteOffset
michael@0 136 * in parallel.
michael@0 137 * (A kingdom for C++ and polymorphism...)
michael@0 138 */
michael@0 139 static void
michael@0 140 res_write(UNewDataMemory *mem, uint32_t *byteOffset,
michael@0 141 struct SRBRoot *bundle, struct SResource *res,
michael@0 142 UErrorCode *status);
michael@0 143
michael@0 144 static uint16_t *
michael@0 145 reserve16BitUnits(struct SRBRoot *bundle, int32_t length, UErrorCode *status) {
michael@0 146 if (U_FAILURE(*status)) {
michael@0 147 return NULL;
michael@0 148 }
michael@0 149 if ((bundle->f16BitUnitsLength + length) > bundle->f16BitUnitsCapacity) {
michael@0 150 uint16_t *newUnits;
michael@0 151 int32_t capacity = 2 * bundle->f16BitUnitsCapacity + length + 1024;
michael@0 152 capacity &= ~1; /* ensures padding fits if f16BitUnitsLength needs it */
michael@0 153 newUnits = (uint16_t *)uprv_malloc(capacity * 2);
michael@0 154 if (newUnits == NULL) {
michael@0 155 *status = U_MEMORY_ALLOCATION_ERROR;
michael@0 156 return NULL;
michael@0 157 }
michael@0 158 if (bundle->f16BitUnitsLength > 0) {
michael@0 159 uprv_memcpy(newUnits, bundle->f16BitUnits, bundle->f16BitUnitsLength * 2);
michael@0 160 } else {
michael@0 161 newUnits[0] = 0;
michael@0 162 bundle->f16BitUnitsLength = 1;
michael@0 163 }
michael@0 164 uprv_free(bundle->f16BitUnits);
michael@0 165 bundle->f16BitUnits = newUnits;
michael@0 166 bundle->f16BitUnitsCapacity = capacity;
michael@0 167 }
michael@0 168 return bundle->f16BitUnits + bundle->f16BitUnitsLength;
michael@0 169 }
michael@0 170
michael@0 171 static int32_t
michael@0 172 makeRes16(uint32_t resWord) {
michael@0 173 uint32_t type, offset;
michael@0 174 if (resWord == 0) {
michael@0 175 return 0; /* empty string */
michael@0 176 }
michael@0 177 type = RES_GET_TYPE(resWord);
michael@0 178 offset = RES_GET_OFFSET(resWord);
michael@0 179 if (type == URES_STRING_V2 && offset <= 0xffff) {
michael@0 180 return (int32_t)offset;
michael@0 181 }
michael@0 182 return -1;
michael@0 183 }
michael@0 184
michael@0 185 static int32_t
michael@0 186 mapKey(struct SRBRoot *bundle, int32_t oldpos) {
michael@0 187 const KeyMapEntry *map = bundle->fKeyMap;
michael@0 188 int32_t i, start, limit;
michael@0 189
michael@0 190 /* do a binary search for the old, pre-bundle_compactKeys() key offset */
michael@0 191 start = bundle->fPoolBundleKeysCount;
michael@0 192 limit = start + bundle->fKeysCount;
michael@0 193 while (start < limit - 1) {
michael@0 194 i = (start + limit) / 2;
michael@0 195 if (oldpos < map[i].oldpos) {
michael@0 196 limit = i;
michael@0 197 } else {
michael@0 198 start = i;
michael@0 199 }
michael@0 200 }
michael@0 201 assert(oldpos == map[start].oldpos);
michael@0 202 return map[start].newpos;
michael@0 203 }
michael@0 204
michael@0 205 static uint16_t
michael@0 206 makeKey16(struct SRBRoot *bundle, int32_t key) {
michael@0 207 if (key >= 0) {
michael@0 208 return (uint16_t)key;
michael@0 209 } else {
michael@0 210 return (uint16_t)(key + bundle->fLocalKeyLimit); /* offset in the pool bundle */
michael@0 211 }
michael@0 212 }
michael@0 213
michael@0 214 /*
michael@0 215 * Only called for UTF-16 v1 strings and duplicate UTF-16 v2 strings.
michael@0 216 * For unique UTF-16 v2 strings, res_write16() sees fRes != RES_BOGUS
michael@0 217 * and exits early.
michael@0 218 */
michael@0 219 static void
michael@0 220 string_write16(struct SRBRoot *bundle, struct SResource *res, UErrorCode *status) {
michael@0 221 struct SResource *same;
michael@0 222 if ((same = res->u.fString.fSame) != NULL) {
michael@0 223 /* This is a duplicate. */
michael@0 224 if (same->fRes == RES_BOGUS) {
michael@0 225 /* The original has not been visited yet. */
michael@0 226 string_write16(bundle, same, status);
michael@0 227 }
michael@0 228 res->fRes = same->fRes;
michael@0 229 res->fWritten = same->fWritten;
michael@0 230 }
michael@0 231 }
michael@0 232
michael@0 233 static void
michael@0 234 array_write16(struct SRBRoot *bundle, struct SResource *res,
michael@0 235 UErrorCode *status) {
michael@0 236 struct SResource *current;
michael@0 237 int32_t res16 = 0;
michael@0 238
michael@0 239 if (U_FAILURE(*status)) {
michael@0 240 return;
michael@0 241 }
michael@0 242 if (res->u.fArray.fCount == 0 && gFormatVersion > 1) {
michael@0 243 res->fRes = URES_MAKE_EMPTY_RESOURCE(URES_ARRAY);
michael@0 244 res->fWritten = TRUE;
michael@0 245 return;
michael@0 246 }
michael@0 247 for (current = res->u.fArray.fFirst; current != NULL; current = current->fNext) {
michael@0 248 res_write16(bundle, current, status);
michael@0 249 res16 |= makeRes16(current->fRes);
michael@0 250 }
michael@0 251 if (U_SUCCESS(*status) && res->u.fArray.fCount <= 0xffff && res16 >= 0 && gFormatVersion > 1) {
michael@0 252 uint16_t *p16 = reserve16BitUnits(bundle, 1 + res->u.fArray.fCount, status);
michael@0 253 if (U_SUCCESS(*status)) {
michael@0 254 res->fRes = URES_MAKE_RESOURCE(URES_ARRAY16, bundle->f16BitUnitsLength);
michael@0 255 *p16++ = (uint16_t)res->u.fArray.fCount;
michael@0 256 for (current = res->u.fArray.fFirst; current != NULL; current = current->fNext) {
michael@0 257 *p16++ = (uint16_t)makeRes16(current->fRes);
michael@0 258 }
michael@0 259 bundle->f16BitUnitsLength += 1 + res->u.fArray.fCount;
michael@0 260 res->fWritten = TRUE;
michael@0 261 }
michael@0 262 }
michael@0 263 }
michael@0 264
michael@0 265 static void
michael@0 266 table_write16(struct SRBRoot *bundle, struct SResource *res,
michael@0 267 UErrorCode *status) {
michael@0 268 struct SResource *current;
michael@0 269 int32_t maxKey = 0, maxPoolKey = 0x80000000;
michael@0 270 int32_t res16 = 0;
michael@0 271 UBool hasLocalKeys = FALSE, hasPoolKeys = FALSE;
michael@0 272
michael@0 273 if (U_FAILURE(*status)) {
michael@0 274 return;
michael@0 275 }
michael@0 276 if (res->u.fTable.fCount == 0 && gFormatVersion > 1) {
michael@0 277 res->fRes = URES_MAKE_EMPTY_RESOURCE(URES_TABLE);
michael@0 278 res->fWritten = TRUE;
michael@0 279 return;
michael@0 280 }
michael@0 281 /* Find the smallest table type that fits the data. */
michael@0 282 for (current = res->u.fTable.fFirst; current != NULL; current = current->fNext) {
michael@0 283 int32_t key;
michael@0 284 res_write16(bundle, current, status);
michael@0 285 if (bundle->fKeyMap == NULL) {
michael@0 286 key = current->fKey;
michael@0 287 } else {
michael@0 288 key = current->fKey = mapKey(bundle, current->fKey);
michael@0 289 }
michael@0 290 if (key >= 0) {
michael@0 291 hasLocalKeys = TRUE;
michael@0 292 if (key > maxKey) {
michael@0 293 maxKey = key;
michael@0 294 }
michael@0 295 } else {
michael@0 296 hasPoolKeys = TRUE;
michael@0 297 if (key > maxPoolKey) {
michael@0 298 maxPoolKey = key;
michael@0 299 }
michael@0 300 }
michael@0 301 res16 |= makeRes16(current->fRes);
michael@0 302 }
michael@0 303 if (U_FAILURE(*status)) {
michael@0 304 return;
michael@0 305 }
michael@0 306 if(res->u.fTable.fCount > (uint32_t)bundle->fMaxTableLength) {
michael@0 307 bundle->fMaxTableLength = res->u.fTable.fCount;
michael@0 308 }
michael@0 309 maxPoolKey &= 0x7fffffff;
michael@0 310 if (res->u.fTable.fCount <= 0xffff &&
michael@0 311 (!hasLocalKeys || maxKey < bundle->fLocalKeyLimit) &&
michael@0 312 (!hasPoolKeys || maxPoolKey < (0x10000 - bundle->fLocalKeyLimit))
michael@0 313 ) {
michael@0 314 if (res16 >= 0 && gFormatVersion > 1) {
michael@0 315 uint16_t *p16 = reserve16BitUnits(bundle, 1 + res->u.fTable.fCount * 2, status);
michael@0 316 if (U_SUCCESS(*status)) {
michael@0 317 /* 16-bit count, key offsets and values */
michael@0 318 res->fRes = URES_MAKE_RESOURCE(URES_TABLE16, bundle->f16BitUnitsLength);
michael@0 319 *p16++ = (uint16_t)res->u.fTable.fCount;
michael@0 320 for (current = res->u.fTable.fFirst; current != NULL; current = current->fNext) {
michael@0 321 *p16++ = makeKey16(bundle, current->fKey);
michael@0 322 }
michael@0 323 for (current = res->u.fTable.fFirst; current != NULL; current = current->fNext) {
michael@0 324 *p16++ = (uint16_t)makeRes16(current->fRes);
michael@0 325 }
michael@0 326 bundle->f16BitUnitsLength += 1 + res->u.fTable.fCount * 2;
michael@0 327 res->fWritten = TRUE;
michael@0 328 }
michael@0 329 } else {
michael@0 330 /* 16-bit count, 16-bit key offsets, 32-bit values */
michael@0 331 res->u.fTable.fType = URES_TABLE;
michael@0 332 }
michael@0 333 } else {
michael@0 334 /* 32-bit count, key offsets and values */
michael@0 335 res->u.fTable.fType = URES_TABLE32;
michael@0 336 }
michael@0 337 }
michael@0 338
michael@0 339 static void
michael@0 340 res_write16(struct SRBRoot *bundle, struct SResource *res,
michael@0 341 UErrorCode *status) {
michael@0 342 if (U_FAILURE(*status) || res == NULL) {
michael@0 343 return;
michael@0 344 }
michael@0 345 if (res->fRes != RES_BOGUS) {
michael@0 346 /*
michael@0 347 * The resource item word was already precomputed, which means
michael@0 348 * no further data needs to be written.
michael@0 349 * This might be an integer, or an empty or UTF-16 v2 string,
michael@0 350 * an empty binary, etc.
michael@0 351 */
michael@0 352 return;
michael@0 353 }
michael@0 354 switch (res->fType) {
michael@0 355 case URES_STRING:
michael@0 356 string_write16(bundle, res, status);
michael@0 357 break;
michael@0 358 case URES_ARRAY:
michael@0 359 array_write16(bundle, res, status);
michael@0 360 break;
michael@0 361 case URES_TABLE:
michael@0 362 table_write16(bundle, res, status);
michael@0 363 break;
michael@0 364 default:
michael@0 365 /* Only a few resource types write 16-bit units. */
michael@0 366 break;
michael@0 367 }
michael@0 368 }
michael@0 369
michael@0 370 /*
michael@0 371 * Only called for UTF-16 v1 strings.
michael@0 372 * For UTF-16 v2 strings, res_preWrite() sees fRes != RES_BOGUS
michael@0 373 * and exits early.
michael@0 374 */
michael@0 375 static void
michael@0 376 string_preWrite(uint32_t *byteOffset,
michael@0 377 struct SRBRoot *bundle, struct SResource *res,
michael@0 378 UErrorCode *status) {
michael@0 379 /* Write the UTF-16 v1 string. */
michael@0 380 res->fRes = URES_MAKE_RESOURCE(URES_STRING, *byteOffset >> 2);
michael@0 381 *byteOffset += 4 + (res->u.fString.fLength + 1) * U_SIZEOF_UCHAR;
michael@0 382 }
michael@0 383
michael@0 384 static void
michael@0 385 bin_preWrite(uint32_t *byteOffset,
michael@0 386 struct SRBRoot *bundle, struct SResource *res,
michael@0 387 UErrorCode *status) {
michael@0 388 uint32_t pad = 0;
michael@0 389 uint32_t dataStart = *byteOffset + sizeof(res->u.fBinaryValue.fLength);
michael@0 390
michael@0 391 if (dataStart % BIN_ALIGNMENT) {
michael@0 392 pad = (BIN_ALIGNMENT - dataStart % BIN_ALIGNMENT);
michael@0 393 *byteOffset += pad; /* pad == 4 or 8 or 12 */
michael@0 394 }
michael@0 395 res->fRes = URES_MAKE_RESOURCE(URES_BINARY, *byteOffset >> 2);
michael@0 396 *byteOffset += 4 + res->u.fBinaryValue.fLength;
michael@0 397 }
michael@0 398
michael@0 399 static void
michael@0 400 array_preWrite(uint32_t *byteOffset,
michael@0 401 struct SRBRoot *bundle, struct SResource *res,
michael@0 402 UErrorCode *status) {
michael@0 403 struct SResource *current;
michael@0 404
michael@0 405 if (U_FAILURE(*status)) {
michael@0 406 return;
michael@0 407 }
michael@0 408 for (current = res->u.fArray.fFirst; current != NULL; current = current->fNext) {
michael@0 409 res_preWrite(byteOffset, bundle, current, status);
michael@0 410 }
michael@0 411 res->fRes = URES_MAKE_RESOURCE(URES_ARRAY, *byteOffset >> 2);
michael@0 412 *byteOffset += (1 + res->u.fArray.fCount) * 4;
michael@0 413 }
michael@0 414
michael@0 415 static void
michael@0 416 table_preWrite(uint32_t *byteOffset,
michael@0 417 struct SRBRoot *bundle, struct SResource *res,
michael@0 418 UErrorCode *status) {
michael@0 419 struct SResource *current;
michael@0 420
michael@0 421 if (U_FAILURE(*status)) {
michael@0 422 return;
michael@0 423 }
michael@0 424 for (current = res->u.fTable.fFirst; current != NULL; current = current->fNext) {
michael@0 425 res_preWrite(byteOffset, bundle, current, status);
michael@0 426 }
michael@0 427 if (res->u.fTable.fType == URES_TABLE) {
michael@0 428 /* 16-bit count, 16-bit key offsets, 32-bit values */
michael@0 429 res->fRes = URES_MAKE_RESOURCE(URES_TABLE, *byteOffset >> 2);
michael@0 430 *byteOffset += 2 + res->u.fTable.fCount * 6;
michael@0 431 } else {
michael@0 432 /* 32-bit count, key offsets and values */
michael@0 433 res->fRes = URES_MAKE_RESOURCE(URES_TABLE32, *byteOffset >> 2);
michael@0 434 *byteOffset += 4 + res->u.fTable.fCount * 8;
michael@0 435 }
michael@0 436 }
michael@0 437
michael@0 438 static void
michael@0 439 res_preWrite(uint32_t *byteOffset,
michael@0 440 struct SRBRoot *bundle, struct SResource *res,
michael@0 441 UErrorCode *status) {
michael@0 442 if (U_FAILURE(*status) || res == NULL) {
michael@0 443 return;
michael@0 444 }
michael@0 445 if (res->fRes != RES_BOGUS) {
michael@0 446 /*
michael@0 447 * The resource item word was already precomputed, which means
michael@0 448 * no further data needs to be written.
michael@0 449 * This might be an integer, or an empty or UTF-16 v2 string,
michael@0 450 * an empty binary, etc.
michael@0 451 */
michael@0 452 return;
michael@0 453 }
michael@0 454 switch (res->fType) {
michael@0 455 case URES_STRING:
michael@0 456 string_preWrite(byteOffset, bundle, res, status);
michael@0 457 break;
michael@0 458 case URES_ALIAS:
michael@0 459 res->fRes = URES_MAKE_RESOURCE(URES_ALIAS, *byteOffset >> 2);
michael@0 460 *byteOffset += 4 + (res->u.fString.fLength + 1) * U_SIZEOF_UCHAR;
michael@0 461 break;
michael@0 462 case URES_INT_VECTOR:
michael@0 463 if (res->u.fIntVector.fCount == 0 && gFormatVersion > 1) {
michael@0 464 res->fRes = URES_MAKE_EMPTY_RESOURCE(URES_INT_VECTOR);
michael@0 465 res->fWritten = TRUE;
michael@0 466 } else {
michael@0 467 res->fRes = URES_MAKE_RESOURCE(URES_INT_VECTOR, *byteOffset >> 2);
michael@0 468 *byteOffset += (1 + res->u.fIntVector.fCount) * 4;
michael@0 469 }
michael@0 470 break;
michael@0 471 case URES_BINARY:
michael@0 472 bin_preWrite(byteOffset, bundle, res, status);
michael@0 473 break;
michael@0 474 case URES_INT:
michael@0 475 break;
michael@0 476 case URES_ARRAY:
michael@0 477 array_preWrite(byteOffset, bundle, res, status);
michael@0 478 break;
michael@0 479 case URES_TABLE:
michael@0 480 table_preWrite(byteOffset, bundle, res, status);
michael@0 481 break;
michael@0 482 default:
michael@0 483 *status = U_INTERNAL_PROGRAM_ERROR;
michael@0 484 break;
michael@0 485 }
michael@0 486 *byteOffset += calcPadding(*byteOffset);
michael@0 487 }
michael@0 488
michael@0 489 /*
michael@0 490 * Only called for UTF-16 v1 strings. For UTF-16 v2 strings,
michael@0 491 * res_write() sees fWritten and exits early.
michael@0 492 */
michael@0 493 static void string_write(UNewDataMemory *mem, uint32_t *byteOffset,
michael@0 494 struct SRBRoot *bundle, struct SResource *res,
michael@0 495 UErrorCode *status) {
michael@0 496 /* Write the UTF-16 v1 string. */
michael@0 497 int32_t length = res->u.fString.fLength;
michael@0 498 udata_write32(mem, length);
michael@0 499 udata_writeUString(mem, res->u.fString.fChars, length + 1);
michael@0 500 *byteOffset += 4 + (length + 1) * U_SIZEOF_UCHAR;
michael@0 501 res->fWritten = TRUE;
michael@0 502 }
michael@0 503
michael@0 504 static void alias_write(UNewDataMemory *mem, uint32_t *byteOffset,
michael@0 505 struct SRBRoot *bundle, struct SResource *res,
michael@0 506 UErrorCode *status) {
michael@0 507 int32_t length = res->u.fString.fLength;
michael@0 508 udata_write32(mem, length);
michael@0 509 udata_writeUString(mem, res->u.fString.fChars, length + 1);
michael@0 510 *byteOffset += 4 + (length + 1) * U_SIZEOF_UCHAR;
michael@0 511 }
michael@0 512
michael@0 513 static void array_write(UNewDataMemory *mem, uint32_t *byteOffset,
michael@0 514 struct SRBRoot *bundle, struct SResource *res,
michael@0 515 UErrorCode *status) {
michael@0 516 uint32_t i;
michael@0 517
michael@0 518 struct SResource *current = NULL;
michael@0 519
michael@0 520 if (U_FAILURE(*status)) {
michael@0 521 return;
michael@0 522 }
michael@0 523 for (i = 0, current = res->u.fArray.fFirst; current != NULL; ++i, current = current->fNext) {
michael@0 524 res_write(mem, byteOffset, bundle, current, status);
michael@0 525 }
michael@0 526 assert(i == res->u.fArray.fCount);
michael@0 527
michael@0 528 udata_write32(mem, res->u.fArray.fCount);
michael@0 529 for (current = res->u.fArray.fFirst; current != NULL; current = current->fNext) {
michael@0 530 udata_write32(mem, current->fRes);
michael@0 531 }
michael@0 532 *byteOffset += (1 + res->u.fArray.fCount) * 4;
michael@0 533 }
michael@0 534
michael@0 535 static void intvector_write(UNewDataMemory *mem, uint32_t *byteOffset,
michael@0 536 struct SRBRoot *bundle, struct SResource *res,
michael@0 537 UErrorCode *status) {
michael@0 538 uint32_t i = 0;
michael@0 539 udata_write32(mem, res->u.fIntVector.fCount);
michael@0 540 for(i = 0; i<res->u.fIntVector.fCount; i++) {
michael@0 541 udata_write32(mem, res->u.fIntVector.fArray[i]);
michael@0 542 }
michael@0 543 *byteOffset += (1 + res->u.fIntVector.fCount) * 4;
michael@0 544 }
michael@0 545
michael@0 546 static void bin_write(UNewDataMemory *mem, uint32_t *byteOffset,
michael@0 547 struct SRBRoot *bundle, struct SResource *res,
michael@0 548 UErrorCode *status) {
michael@0 549 uint32_t pad = 0;
michael@0 550 uint32_t dataStart = *byteOffset + sizeof(res->u.fBinaryValue.fLength);
michael@0 551
michael@0 552 if (dataStart % BIN_ALIGNMENT) {
michael@0 553 pad = (BIN_ALIGNMENT - dataStart % BIN_ALIGNMENT);
michael@0 554 udata_writePadding(mem, pad); /* pad == 4 or 8 or 12 */
michael@0 555 *byteOffset += pad;
michael@0 556 }
michael@0 557
michael@0 558 udata_write32(mem, res->u.fBinaryValue.fLength);
michael@0 559 if (res->u.fBinaryValue.fLength > 0) {
michael@0 560 udata_writeBlock(mem, res->u.fBinaryValue.fData, res->u.fBinaryValue.fLength);
michael@0 561 }
michael@0 562 *byteOffset += 4 + res->u.fBinaryValue.fLength;
michael@0 563 }
michael@0 564
michael@0 565 static void table_write(UNewDataMemory *mem, uint32_t *byteOffset,
michael@0 566 struct SRBRoot *bundle, struct SResource *res,
michael@0 567 UErrorCode *status) {
michael@0 568 struct SResource *current;
michael@0 569 uint32_t i;
michael@0 570
michael@0 571 if (U_FAILURE(*status)) {
michael@0 572 return;
michael@0 573 }
michael@0 574 for (i = 0, current = res->u.fTable.fFirst; current != NULL; ++i, current = current->fNext) {
michael@0 575 assert(i < res->u.fTable.fCount);
michael@0 576 res_write(mem, byteOffset, bundle, current, status);
michael@0 577 }
michael@0 578 assert(i == res->u.fTable.fCount);
michael@0 579
michael@0 580 if(res->u.fTable.fType == URES_TABLE) {
michael@0 581 udata_write16(mem, (uint16_t)res->u.fTable.fCount);
michael@0 582 for (current = res->u.fTable.fFirst; current != NULL; current = current->fNext) {
michael@0 583 udata_write16(mem, makeKey16(bundle, current->fKey));
michael@0 584 }
michael@0 585 *byteOffset += (1 + res->u.fTable.fCount)* 2;
michael@0 586 if ((res->u.fTable.fCount & 1) == 0) {
michael@0 587 /* 16-bit count and even number of 16-bit key offsets need padding before 32-bit resource items */
michael@0 588 udata_writePadding(mem, 2);
michael@0 589 *byteOffset += 2;
michael@0 590 }
michael@0 591 } else /* URES_TABLE32 */ {
michael@0 592 udata_write32(mem, res->u.fTable.fCount);
michael@0 593 for (current = res->u.fTable.fFirst; current != NULL; current = current->fNext) {
michael@0 594 udata_write32(mem, (uint32_t)current->fKey);
michael@0 595 }
michael@0 596 *byteOffset += (1 + res->u.fTable.fCount)* 4;
michael@0 597 }
michael@0 598 for (current = res->u.fTable.fFirst; current != NULL; current = current->fNext) {
michael@0 599 udata_write32(mem, current->fRes);
michael@0 600 }
michael@0 601 *byteOffset += res->u.fTable.fCount * 4;
michael@0 602 }
michael@0 603
michael@0 604 void res_write(UNewDataMemory *mem, uint32_t *byteOffset,
michael@0 605 struct SRBRoot *bundle, struct SResource *res,
michael@0 606 UErrorCode *status) {
michael@0 607 uint8_t paddingSize;
michael@0 608
michael@0 609 if (U_FAILURE(*status) || res == NULL) {
michael@0 610 return;
michael@0 611 }
michael@0 612 if (res->fWritten) {
michael@0 613 assert(res->fRes != RES_BOGUS);
michael@0 614 return;
michael@0 615 }
michael@0 616 switch (res->fType) {
michael@0 617 case URES_STRING:
michael@0 618 string_write (mem, byteOffset, bundle, res, status);
michael@0 619 break;
michael@0 620 case URES_ALIAS:
michael@0 621 alias_write (mem, byteOffset, bundle, res, status);
michael@0 622 break;
michael@0 623 case URES_INT_VECTOR:
michael@0 624 intvector_write (mem, byteOffset, bundle, res, status);
michael@0 625 break;
michael@0 626 case URES_BINARY:
michael@0 627 bin_write (mem, byteOffset, bundle, res, status);
michael@0 628 break;
michael@0 629 case URES_INT:
michael@0 630 break; /* fRes was set by int_open() */
michael@0 631 case URES_ARRAY:
michael@0 632 array_write (mem, byteOffset, bundle, res, status);
michael@0 633 break;
michael@0 634 case URES_TABLE:
michael@0 635 table_write (mem, byteOffset, bundle, res, status);
michael@0 636 break;
michael@0 637 default:
michael@0 638 *status = U_INTERNAL_PROGRAM_ERROR;
michael@0 639 break;
michael@0 640 }
michael@0 641 paddingSize = calcPadding(*byteOffset);
michael@0 642 if (paddingSize > 0) {
michael@0 643 udata_writePadding(mem, paddingSize);
michael@0 644 *byteOffset += paddingSize;
michael@0 645 }
michael@0 646 res->fWritten = TRUE;
michael@0 647 }
michael@0 648
michael@0 649 void bundle_write(struct SRBRoot *bundle,
michael@0 650 const char *outputDir, const char *outputPkg,
michael@0 651 char *writtenFilename, int writtenFilenameLen,
michael@0 652 UErrorCode *status) {
michael@0 653 UNewDataMemory *mem = NULL;
michael@0 654 uint32_t byteOffset = 0;
michael@0 655 uint32_t top, size;
michael@0 656 char dataName[1024];
michael@0 657 int32_t indexes[URES_INDEX_TOP];
michael@0 658
michael@0 659 bundle_compactKeys(bundle, status);
michael@0 660 /*
michael@0 661 * Add padding bytes to fKeys so that fKeysTop is 4-aligned.
michael@0 662 * Safe because the capacity is a multiple of 4.
michael@0 663 */
michael@0 664 while (bundle->fKeysTop & 3) {
michael@0 665 bundle->fKeys[bundle->fKeysTop++] = (char)0xaa;
michael@0 666 }
michael@0 667 /*
michael@0 668 * In URES_TABLE, use all local key offsets that fit into 16 bits,
michael@0 669 * and use the remaining 16-bit offsets for pool key offsets
michael@0 670 * if there are any.
michael@0 671 * If there are no local keys, then use the whole 16-bit space
michael@0 672 * for pool key offsets.
michael@0 673 * Note: This cannot be changed without changing the major formatVersion.
michael@0 674 */
michael@0 675 if (bundle->fKeysBottom < bundle->fKeysTop) {
michael@0 676 if (bundle->fKeysTop <= 0x10000) {
michael@0 677 bundle->fLocalKeyLimit = bundle->fKeysTop;
michael@0 678 } else {
michael@0 679 bundle->fLocalKeyLimit = 0x10000;
michael@0 680 }
michael@0 681 } else {
michael@0 682 bundle->fLocalKeyLimit = 0;
michael@0 683 }
michael@0 684
michael@0 685 bundle_compactStrings(bundle, status);
michael@0 686 res_write16(bundle, bundle->fRoot, status);
michael@0 687 if (bundle->f16BitUnitsLength & 1) {
michael@0 688 bundle->f16BitUnits[bundle->f16BitUnitsLength++] = 0xaaaa; /* pad to multiple of 4 bytes */
michael@0 689 }
michael@0 690 /* all keys have been mapped */
michael@0 691 uprv_free(bundle->fKeyMap);
michael@0 692 bundle->fKeyMap = NULL;
michael@0 693
michael@0 694 byteOffset = bundle->fKeysTop + bundle->f16BitUnitsLength * 2;
michael@0 695 res_preWrite(&byteOffset, bundle, bundle->fRoot, status);
michael@0 696
michael@0 697 /* total size including the root item */
michael@0 698 top = byteOffset;
michael@0 699
michael@0 700 if (U_FAILURE(*status)) {
michael@0 701 return;
michael@0 702 }
michael@0 703
michael@0 704 if (writtenFilename && writtenFilenameLen) {
michael@0 705 *writtenFilename = 0;
michael@0 706 }
michael@0 707
michael@0 708 if (writtenFilename) {
michael@0 709 int32_t off = 0, len = 0;
michael@0 710 if (outputDir) {
michael@0 711 len = (int32_t)uprv_strlen(outputDir);
michael@0 712 if (len > writtenFilenameLen) {
michael@0 713 len = writtenFilenameLen;
michael@0 714 }
michael@0 715 uprv_strncpy(writtenFilename, outputDir, len);
michael@0 716 }
michael@0 717 if (writtenFilenameLen -= len) {
michael@0 718 off += len;
michael@0 719 writtenFilename[off] = U_FILE_SEP_CHAR;
michael@0 720 if (--writtenFilenameLen) {
michael@0 721 ++off;
michael@0 722 if(outputPkg != NULL)
michael@0 723 {
michael@0 724 uprv_strcpy(writtenFilename+off, outputPkg);
michael@0 725 off += (int32_t)uprv_strlen(outputPkg);
michael@0 726 writtenFilename[off] = '_';
michael@0 727 ++off;
michael@0 728 }
michael@0 729
michael@0 730 len = (int32_t)uprv_strlen(bundle->fLocale);
michael@0 731 if (len > writtenFilenameLen) {
michael@0 732 len = writtenFilenameLen;
michael@0 733 }
michael@0 734 uprv_strncpy(writtenFilename + off, bundle->fLocale, len);
michael@0 735 if (writtenFilenameLen -= len) {
michael@0 736 off += len;
michael@0 737 len = 5;
michael@0 738 if (len > writtenFilenameLen) {
michael@0 739 len = writtenFilenameLen;
michael@0 740 }
michael@0 741 uprv_strncpy(writtenFilename + off, ".res", len);
michael@0 742 }
michael@0 743 }
michael@0 744 }
michael@0 745 }
michael@0 746
michael@0 747 if(outputPkg)
michael@0 748 {
michael@0 749 uprv_strcpy(dataName, outputPkg);
michael@0 750 uprv_strcat(dataName, "_");
michael@0 751 uprv_strcat(dataName, bundle->fLocale);
michael@0 752 }
michael@0 753 else
michael@0 754 {
michael@0 755 uprv_strcpy(dataName, bundle->fLocale);
michael@0 756 }
michael@0 757
michael@0 758 uprv_memcpy(dataInfo.formatVersion, gFormatVersions + gFormatVersion, sizeof(UVersionInfo));
michael@0 759
michael@0 760 mem = udata_create(outputDir, "res", dataName, &dataInfo, (gIncludeCopyright==TRUE)? U_COPYRIGHT_STRING:NULL, status);
michael@0 761 if(U_FAILURE(*status)){
michael@0 762 return;
michael@0 763 }
michael@0 764
michael@0 765 /* write the root item */
michael@0 766 udata_write32(mem, bundle->fRoot->fRes);
michael@0 767
michael@0 768 /*
michael@0 769 * formatVersion 1.1 (ICU 2.8):
michael@0 770 * write int32_t indexes[] after root and before the strings
michael@0 771 * to make it easier to parse resource bundles in icuswap or from Java etc.
michael@0 772 */
michael@0 773 uprv_memset(indexes, 0, sizeof(indexes));
michael@0 774 indexes[URES_INDEX_LENGTH]= bundle->fIndexLength;
michael@0 775 indexes[URES_INDEX_KEYS_TOP]= bundle->fKeysTop>>2;
michael@0 776 indexes[URES_INDEX_RESOURCES_TOP]= (int32_t)(top>>2);
michael@0 777 indexes[URES_INDEX_BUNDLE_TOP]= indexes[URES_INDEX_RESOURCES_TOP];
michael@0 778 indexes[URES_INDEX_MAX_TABLE_LENGTH]= bundle->fMaxTableLength;
michael@0 779
michael@0 780 /*
michael@0 781 * formatVersion 1.2 (ICU 3.6):
michael@0 782 * write indexes[URES_INDEX_ATTRIBUTES] with URES_ATT_NO_FALLBACK set or not set
michael@0 783 * the memset() above initialized all indexes[] to 0
michael@0 784 */
michael@0 785 if (bundle->noFallback) {
michael@0 786 indexes[URES_INDEX_ATTRIBUTES]=URES_ATT_NO_FALLBACK;
michael@0 787 }
michael@0 788 /*
michael@0 789 * formatVersion 2.0 (ICU 4.4):
michael@0 790 * more compact string value storage, optional pool bundle
michael@0 791 */
michael@0 792 if (URES_INDEX_16BIT_TOP < bundle->fIndexLength) {
michael@0 793 indexes[URES_INDEX_16BIT_TOP] = (bundle->fKeysTop>>2) + (bundle->f16BitUnitsLength>>1);
michael@0 794 }
michael@0 795 if (URES_INDEX_POOL_CHECKSUM < bundle->fIndexLength) {
michael@0 796 if (bundle->fIsPoolBundle) {
michael@0 797 indexes[URES_INDEX_ATTRIBUTES] |= URES_ATT_IS_POOL_BUNDLE | URES_ATT_NO_FALLBACK;
michael@0 798 indexes[URES_INDEX_POOL_CHECKSUM] =
michael@0 799 (int32_t)computeCRC((char *)(bundle->fKeys + bundle->fKeysBottom),
michael@0 800 (uint32_t)(bundle->fKeysTop - bundle->fKeysBottom),
michael@0 801 0);
michael@0 802 } else if (gUsePoolBundle) {
michael@0 803 indexes[URES_INDEX_ATTRIBUTES] |= URES_ATT_USES_POOL_BUNDLE;
michael@0 804 indexes[URES_INDEX_POOL_CHECKSUM] = bundle->fPoolChecksum;
michael@0 805 }
michael@0 806 }
michael@0 807
michael@0 808 /* write the indexes[] */
michael@0 809 udata_writeBlock(mem, indexes, bundle->fIndexLength*4);
michael@0 810
michael@0 811 /* write the table key strings */
michael@0 812 udata_writeBlock(mem, bundle->fKeys+bundle->fKeysBottom,
michael@0 813 bundle->fKeysTop-bundle->fKeysBottom);
michael@0 814
michael@0 815 /* write the v2 UTF-16 strings, URES_TABLE16 and URES_ARRAY16 */
michael@0 816 udata_writeBlock(mem, bundle->f16BitUnits, bundle->f16BitUnitsLength*2);
michael@0 817
michael@0 818 /* write all of the bundle contents: the root item and its children */
michael@0 819 byteOffset = bundle->fKeysTop + bundle->f16BitUnitsLength * 2;
michael@0 820 res_write(mem, &byteOffset, bundle, bundle->fRoot, status);
michael@0 821 assert(byteOffset == top);
michael@0 822
michael@0 823 size = udata_finish(mem, status);
michael@0 824 if(top != size) {
michael@0 825 fprintf(stderr, "genrb error: wrote %u bytes but counted %u\n",
michael@0 826 (int)size, (int)top);
michael@0 827 *status = U_INTERNAL_PROGRAM_ERROR;
michael@0 828 }
michael@0 829 }
michael@0 830
michael@0 831 /* Opening Functions */
michael@0 832
michael@0 833 /* gcc 4.2 complained "no previous prototype for res_open" without this prototype... */
michael@0 834 struct SResource* res_open(struct SRBRoot *bundle, const char *tag,
michael@0 835 const struct UString* comment, UErrorCode* status);
michael@0 836
michael@0 837 struct SResource* res_open(struct SRBRoot *bundle, const char *tag,
michael@0 838 const struct UString* comment, UErrorCode* status){
michael@0 839 struct SResource *res;
michael@0 840 int32_t key = bundle_addtag(bundle, tag, status);
michael@0 841 if (U_FAILURE(*status)) {
michael@0 842 return NULL;
michael@0 843 }
michael@0 844
michael@0 845 res = (struct SResource *) uprv_malloc(sizeof(struct SResource));
michael@0 846 if (res == NULL) {
michael@0 847 *status = U_MEMORY_ALLOCATION_ERROR;
michael@0 848 return NULL;
michael@0 849 }
michael@0 850 uprv_memset(res, 0, sizeof(struct SResource));
michael@0 851 res->fKey = key;
michael@0 852 res->fRes = RES_BOGUS;
michael@0 853
michael@0 854 ustr_init(&res->fComment);
michael@0 855 if(comment != NULL){
michael@0 856 ustr_cpy(&res->fComment, comment, status);
michael@0 857 if (U_FAILURE(*status)) {
michael@0 858 res_close(res);
michael@0 859 return NULL;
michael@0 860 }
michael@0 861 }
michael@0 862 return res;
michael@0 863 }
michael@0 864
michael@0 865 struct SResource* res_none() {
michael@0 866 return (struct SResource*)&kNoResource;
michael@0 867 }
michael@0 868
michael@0 869 struct SResource* table_open(struct SRBRoot *bundle, const char *tag, const struct UString* comment, UErrorCode *status) {
michael@0 870 struct SResource *res = res_open(bundle, tag, comment, status);
michael@0 871 if (U_FAILURE(*status)) {
michael@0 872 return NULL;
michael@0 873 }
michael@0 874 res->fType = URES_TABLE;
michael@0 875 res->u.fTable.fRoot = bundle;
michael@0 876 return res;
michael@0 877 }
michael@0 878
michael@0 879 struct SResource* array_open(struct SRBRoot *bundle, const char *tag, const struct UString* comment, UErrorCode *status) {
michael@0 880 struct SResource *res = res_open(bundle, tag, comment, status);
michael@0 881 if (U_FAILURE(*status)) {
michael@0 882 return NULL;
michael@0 883 }
michael@0 884 res->fType = URES_ARRAY;
michael@0 885 return res;
michael@0 886 }
michael@0 887
michael@0 888 static int32_t U_CALLCONV
michael@0 889 string_hash(const UElement key) {
michael@0 890 const struct SResource *res = (struct SResource *)key.pointer;
michael@0 891 return ustr_hashUCharsN(res->u.fString.fChars, res->u.fString.fLength);
michael@0 892 }
michael@0 893
michael@0 894 static UBool U_CALLCONV
michael@0 895 string_comp(const UElement key1, const UElement key2) {
michael@0 896 const struct SResource *res1 = (struct SResource *)key1.pointer;
michael@0 897 const struct SResource *res2 = (struct SResource *)key2.pointer;
michael@0 898 return 0 == u_strCompare(res1->u.fString.fChars, res1->u.fString.fLength,
michael@0 899 res2->u.fString.fChars, res2->u.fString.fLength,
michael@0 900 FALSE);
michael@0 901 }
michael@0 902
michael@0 903 struct SResource *string_open(struct SRBRoot *bundle, const char *tag, const UChar *value, int32_t len, const struct UString* comment, UErrorCode *status) {
michael@0 904 struct SResource *res = res_open(bundle, tag, comment, status);
michael@0 905 if (U_FAILURE(*status)) {
michael@0 906 return NULL;
michael@0 907 }
michael@0 908 res->fType = URES_STRING;
michael@0 909
michael@0 910 if (len == 0 && gFormatVersion > 1) {
michael@0 911 res->u.fString.fChars = &gEmptyString;
michael@0 912 res->fRes = 0;
michael@0 913 res->fWritten = TRUE;
michael@0 914 return res;
michael@0 915 }
michael@0 916
michael@0 917 res->u.fString.fLength = len;
michael@0 918
michael@0 919 if (gFormatVersion > 1) {
michael@0 920 /* check for duplicates */
michael@0 921 res->u.fString.fChars = (UChar *)value;
michael@0 922 if (bundle->fStringSet == NULL) {
michael@0 923 UErrorCode localStatus = U_ZERO_ERROR; /* if failure: just don't detect dups */
michael@0 924 bundle->fStringSet = uhash_open(string_hash, string_comp, string_comp, &localStatus);
michael@0 925 } else {
michael@0 926 res->u.fString.fSame = uhash_get(bundle->fStringSet, res);
michael@0 927 }
michael@0 928 }
michael@0 929 if (res->u.fString.fSame == NULL) {
michael@0 930 /* this is a new string */
michael@0 931 res->u.fString.fChars = (UChar *) uprv_malloc(sizeof(UChar) * (len + 1));
michael@0 932
michael@0 933 if (res->u.fString.fChars == NULL) {
michael@0 934 *status = U_MEMORY_ALLOCATION_ERROR;
michael@0 935 uprv_free(res);
michael@0 936 return NULL;
michael@0 937 }
michael@0 938
michael@0 939 uprv_memcpy(res->u.fString.fChars, value, sizeof(UChar) * len);
michael@0 940 res->u.fString.fChars[len] = 0;
michael@0 941 if (bundle->fStringSet != NULL) {
michael@0 942 /* put it into the set for finding duplicates */
michael@0 943 uhash_put(bundle->fStringSet, res, res, status);
michael@0 944 }
michael@0 945
michael@0 946 if (bundle->fStringsForm != STRINGS_UTF16_V1) {
michael@0 947 if (len <= MAX_IMPLICIT_STRING_LENGTH && !U16_IS_TRAIL(value[0]) && len == u_strlen(value)) {
michael@0 948 /*
michael@0 949 * This string will be stored without an explicit length.
michael@0 950 * Runtime will detect !U16_IS_TRAIL(value[0]) and call u_strlen().
michael@0 951 */
michael@0 952 res->u.fString.fNumCharsForLength = 0;
michael@0 953 } else if (len <= 0x3ee) {
michael@0 954 res->u.fString.fNumCharsForLength = 1;
michael@0 955 } else if (len <= 0xfffff) {
michael@0 956 res->u.fString.fNumCharsForLength = 2;
michael@0 957 } else {
michael@0 958 res->u.fString.fNumCharsForLength = 3;
michael@0 959 }
michael@0 960 bundle->f16BitUnitsLength += res->u.fString.fNumCharsForLength + len + 1; /* +1 for the NUL */
michael@0 961 }
michael@0 962 } else {
michael@0 963 /* this is a duplicate of fSame */
michael@0 964 struct SResource *same = res->u.fString.fSame;
michael@0 965 res->u.fString.fChars = same->u.fString.fChars;
michael@0 966 }
michael@0 967 return res;
michael@0 968 }
michael@0 969
michael@0 970 /* TODO: make alias_open and string_open use the same code */
michael@0 971 struct SResource *alias_open(struct SRBRoot *bundle, const char *tag, UChar *value, int32_t len, const struct UString* comment, UErrorCode *status) {
michael@0 972 struct SResource *res = res_open(bundle, tag, comment, status);
michael@0 973 if (U_FAILURE(*status)) {
michael@0 974 return NULL;
michael@0 975 }
michael@0 976 res->fType = URES_ALIAS;
michael@0 977 if (len == 0 && gFormatVersion > 1) {
michael@0 978 res->u.fString.fChars = &gEmptyString;
michael@0 979 res->fRes = URES_MAKE_EMPTY_RESOURCE(URES_ALIAS);
michael@0 980 res->fWritten = TRUE;
michael@0 981 return res;
michael@0 982 }
michael@0 983
michael@0 984 res->u.fString.fLength = len;
michael@0 985 res->u.fString.fChars = (UChar *) uprv_malloc(sizeof(UChar) * (len + 1));
michael@0 986 if (res->u.fString.fChars == NULL) {
michael@0 987 *status = U_MEMORY_ALLOCATION_ERROR;
michael@0 988 uprv_free(res);
michael@0 989 return NULL;
michael@0 990 }
michael@0 991 uprv_memcpy(res->u.fString.fChars, value, sizeof(UChar) * (len + 1));
michael@0 992 return res;
michael@0 993 }
michael@0 994
michael@0 995
michael@0 996 struct SResource* intvector_open(struct SRBRoot *bundle, const char *tag, const struct UString* comment, UErrorCode *status) {
michael@0 997 struct SResource *res = res_open(bundle, tag, comment, status);
michael@0 998 if (U_FAILURE(*status)) {
michael@0 999 return NULL;
michael@0 1000 }
michael@0 1001 res->fType = URES_INT_VECTOR;
michael@0 1002
michael@0 1003 res->u.fIntVector.fCount = 0;
michael@0 1004 res->u.fIntVector.fArray = (uint32_t *) uprv_malloc(sizeof(uint32_t) * RESLIST_MAX_INT_VECTOR);
michael@0 1005 if (res->u.fIntVector.fArray == NULL) {
michael@0 1006 *status = U_MEMORY_ALLOCATION_ERROR;
michael@0 1007 uprv_free(res);
michael@0 1008 return NULL;
michael@0 1009 }
michael@0 1010 return res;
michael@0 1011 }
michael@0 1012
michael@0 1013 struct SResource *int_open(struct SRBRoot *bundle, const char *tag, int32_t value, const struct UString* comment, UErrorCode *status) {
michael@0 1014 struct SResource *res = res_open(bundle, tag, comment, status);
michael@0 1015 if (U_FAILURE(*status)) {
michael@0 1016 return NULL;
michael@0 1017 }
michael@0 1018 res->fType = URES_INT;
michael@0 1019 res->u.fIntValue.fValue = value;
michael@0 1020 res->fRes = URES_MAKE_RESOURCE(URES_INT, value & 0x0FFFFFFF);
michael@0 1021 res->fWritten = TRUE;
michael@0 1022 return res;
michael@0 1023 }
michael@0 1024
michael@0 1025 struct SResource *bin_open(struct SRBRoot *bundle, const char *tag, uint32_t length, uint8_t *data, const char* fileName, const struct UString* comment, UErrorCode *status) {
michael@0 1026 struct SResource *res = res_open(bundle, tag, comment, status);
michael@0 1027 if (U_FAILURE(*status)) {
michael@0 1028 return NULL;
michael@0 1029 }
michael@0 1030 res->fType = URES_BINARY;
michael@0 1031
michael@0 1032 res->u.fBinaryValue.fLength = length;
michael@0 1033 res->u.fBinaryValue.fFileName = NULL;
michael@0 1034 if(fileName!=NULL && uprv_strcmp(fileName, "") !=0){
michael@0 1035 res->u.fBinaryValue.fFileName = (char*) uprv_malloc(sizeof(char) * (uprv_strlen(fileName)+1));
michael@0 1036 uprv_strcpy(res->u.fBinaryValue.fFileName,fileName);
michael@0 1037 }
michael@0 1038 if (length > 0) {
michael@0 1039 res->u.fBinaryValue.fData = (uint8_t *) uprv_malloc(sizeof(uint8_t) * length);
michael@0 1040
michael@0 1041 if (res->u.fBinaryValue.fData == NULL) {
michael@0 1042 *status = U_MEMORY_ALLOCATION_ERROR;
michael@0 1043 uprv_free(res);
michael@0 1044 return NULL;
michael@0 1045 }
michael@0 1046
michael@0 1047 uprv_memcpy(res->u.fBinaryValue.fData, data, length);
michael@0 1048 }
michael@0 1049 else {
michael@0 1050 res->u.fBinaryValue.fData = NULL;
michael@0 1051 if (gFormatVersion > 1) {
michael@0 1052 res->fRes = URES_MAKE_EMPTY_RESOURCE(URES_BINARY);
michael@0 1053 res->fWritten = TRUE;
michael@0 1054 }
michael@0 1055 }
michael@0 1056
michael@0 1057 return res;
michael@0 1058 }
michael@0 1059
michael@0 1060 struct SRBRoot *bundle_open(const struct UString* comment, UBool isPoolBundle, UErrorCode *status) {
michael@0 1061 struct SRBRoot *bundle;
michael@0 1062
michael@0 1063 if (U_FAILURE(*status)) {
michael@0 1064 return NULL;
michael@0 1065 }
michael@0 1066
michael@0 1067 bundle = (struct SRBRoot *) uprv_malloc(sizeof(struct SRBRoot));
michael@0 1068 if (bundle == NULL) {
michael@0 1069 *status = U_MEMORY_ALLOCATION_ERROR;
michael@0 1070 return 0;
michael@0 1071 }
michael@0 1072 uprv_memset(bundle, 0, sizeof(struct SRBRoot));
michael@0 1073
michael@0 1074 bundle->fKeys = (char *) uprv_malloc(sizeof(char) * KEY_SPACE_SIZE);
michael@0 1075 bundle->fRoot = table_open(bundle, NULL, comment, status);
michael@0 1076 if (bundle->fKeys == NULL || bundle->fRoot == NULL || U_FAILURE(*status)) {
michael@0 1077 if (U_SUCCESS(*status)) {
michael@0 1078 *status = U_MEMORY_ALLOCATION_ERROR;
michael@0 1079 }
michael@0 1080 bundle_close(bundle, status);
michael@0 1081 return NULL;
michael@0 1082 }
michael@0 1083
michael@0 1084 bundle->fLocale = NULL;
michael@0 1085 bundle->fKeysCapacity = KEY_SPACE_SIZE;
michael@0 1086 /* formatVersion 1.1: start fKeysTop after the root item and indexes[] */
michael@0 1087 bundle->fIsPoolBundle = isPoolBundle;
michael@0 1088 if (gUsePoolBundle || isPoolBundle) {
michael@0 1089 bundle->fIndexLength = URES_INDEX_POOL_CHECKSUM + 1;
michael@0 1090 } else if (gFormatVersion >= 2) {
michael@0 1091 bundle->fIndexLength = URES_INDEX_16BIT_TOP + 1;
michael@0 1092 } else /* formatVersion 1 */ {
michael@0 1093 bundle->fIndexLength = URES_INDEX_ATTRIBUTES + 1;
michael@0 1094 }
michael@0 1095 bundle->fKeysBottom = (1 /* root */ + bundle->fIndexLength) * 4;
michael@0 1096 uprv_memset(bundle->fKeys, 0, bundle->fKeysBottom);
michael@0 1097 bundle->fKeysTop = bundle->fKeysBottom;
michael@0 1098
michael@0 1099 if (gFormatVersion == 1) {
michael@0 1100 bundle->fStringsForm = STRINGS_UTF16_V1;
michael@0 1101 } else {
michael@0 1102 bundle->fStringsForm = STRINGS_UTF16_V2;
michael@0 1103 }
michael@0 1104
michael@0 1105 return bundle;
michael@0 1106 }
michael@0 1107
michael@0 1108 /* Closing Functions */
michael@0 1109 static void table_close(struct SResource *table) {
michael@0 1110 struct SResource *current = NULL;
michael@0 1111 struct SResource *prev = NULL;
michael@0 1112
michael@0 1113 current = table->u.fTable.fFirst;
michael@0 1114
michael@0 1115 while (current != NULL) {
michael@0 1116 prev = current;
michael@0 1117 current = current->fNext;
michael@0 1118
michael@0 1119 res_close(prev);
michael@0 1120 }
michael@0 1121
michael@0 1122 table->u.fTable.fFirst = NULL;
michael@0 1123 }
michael@0 1124
michael@0 1125 static void array_close(struct SResource *array) {
michael@0 1126 struct SResource *current = NULL;
michael@0 1127 struct SResource *prev = NULL;
michael@0 1128
michael@0 1129 if(array==NULL){
michael@0 1130 return;
michael@0 1131 }
michael@0 1132 current = array->u.fArray.fFirst;
michael@0 1133
michael@0 1134 while (current != NULL) {
michael@0 1135 prev = current;
michael@0 1136 current = current->fNext;
michael@0 1137
michael@0 1138 res_close(prev);
michael@0 1139 }
michael@0 1140 array->u.fArray.fFirst = NULL;
michael@0 1141 }
michael@0 1142
michael@0 1143 static void string_close(struct SResource *string) {
michael@0 1144 if (string->u.fString.fChars != NULL &&
michael@0 1145 string->u.fString.fChars != &gEmptyString &&
michael@0 1146 string->u.fString.fSame == NULL
michael@0 1147 ) {
michael@0 1148 uprv_free(string->u.fString.fChars);
michael@0 1149 string->u.fString.fChars =NULL;
michael@0 1150 }
michael@0 1151 }
michael@0 1152
michael@0 1153 static void alias_close(struct SResource *alias) {
michael@0 1154 if (alias->u.fString.fChars != NULL) {
michael@0 1155 uprv_free(alias->u.fString.fChars);
michael@0 1156 alias->u.fString.fChars =NULL;
michael@0 1157 }
michael@0 1158 }
michael@0 1159
michael@0 1160 static void intvector_close(struct SResource *intvector) {
michael@0 1161 if (intvector->u.fIntVector.fArray != NULL) {
michael@0 1162 uprv_free(intvector->u.fIntVector.fArray);
michael@0 1163 intvector->u.fIntVector.fArray =NULL;
michael@0 1164 }
michael@0 1165 }
michael@0 1166
michael@0 1167 static void int_close(struct SResource *intres) {
michael@0 1168 /* Intentionally left blank */
michael@0 1169 }
michael@0 1170
michael@0 1171 static void bin_close(struct SResource *binres) {
michael@0 1172 if (binres->u.fBinaryValue.fData != NULL) {
michael@0 1173 uprv_free(binres->u.fBinaryValue.fData);
michael@0 1174 binres->u.fBinaryValue.fData = NULL;
michael@0 1175 }
michael@0 1176 if (binres->u.fBinaryValue.fFileName != NULL) {
michael@0 1177 uprv_free(binres->u.fBinaryValue.fFileName);
michael@0 1178 binres->u.fBinaryValue.fFileName = NULL;
michael@0 1179 }
michael@0 1180 }
michael@0 1181
michael@0 1182 void res_close(struct SResource *res) {
michael@0 1183 if (res != NULL) {
michael@0 1184 switch(res->fType) {
michael@0 1185 case URES_STRING:
michael@0 1186 string_close(res);
michael@0 1187 break;
michael@0 1188 case URES_ALIAS:
michael@0 1189 alias_close(res);
michael@0 1190 break;
michael@0 1191 case URES_INT_VECTOR:
michael@0 1192 intvector_close(res);
michael@0 1193 break;
michael@0 1194 case URES_BINARY:
michael@0 1195 bin_close(res);
michael@0 1196 break;
michael@0 1197 case URES_INT:
michael@0 1198 int_close(res);
michael@0 1199 break;
michael@0 1200 case URES_ARRAY:
michael@0 1201 array_close(res);
michael@0 1202 break;
michael@0 1203 case URES_TABLE:
michael@0 1204 table_close(res);
michael@0 1205 break;
michael@0 1206 default:
michael@0 1207 /* Shouldn't happen */
michael@0 1208 break;
michael@0 1209 }
michael@0 1210
michael@0 1211 ustr_deinit(&res->fComment);
michael@0 1212 uprv_free(res);
michael@0 1213 }
michael@0 1214 }
michael@0 1215
michael@0 1216 void bundle_close(struct SRBRoot *bundle, UErrorCode *status) {
michael@0 1217 res_close(bundle->fRoot);
michael@0 1218 uprv_free(bundle->fLocale);
michael@0 1219 uprv_free(bundle->fKeys);
michael@0 1220 uprv_free(bundle->fKeyMap);
michael@0 1221 uhash_close(bundle->fStringSet);
michael@0 1222 uprv_free(bundle->f16BitUnits);
michael@0 1223 uprv_free(bundle);
michael@0 1224 }
michael@0 1225
michael@0 1226 void bundle_closeString(struct SRBRoot *bundle, struct SResource *string) {
michael@0 1227 if (bundle->fStringSet != NULL) {
michael@0 1228 uhash_remove(bundle->fStringSet, string);
michael@0 1229 }
michael@0 1230 string_close(string);
michael@0 1231 }
michael@0 1232
michael@0 1233 /* Adding Functions */
michael@0 1234 void table_add(struct SResource *table, struct SResource *res, int linenumber, UErrorCode *status) {
michael@0 1235 struct SResource *current = NULL;
michael@0 1236 struct SResource *prev = NULL;
michael@0 1237 struct SResTable *list;
michael@0 1238 const char *resKeyString;
michael@0 1239
michael@0 1240 if (U_FAILURE(*status)) {
michael@0 1241 return;
michael@0 1242 }
michael@0 1243 if (res == &kNoResource) {
michael@0 1244 return;
michael@0 1245 }
michael@0 1246
michael@0 1247 /* remember this linenumber to report to the user if there is a duplicate key */
michael@0 1248 res->line = linenumber;
michael@0 1249
michael@0 1250 /* here we need to traverse the list */
michael@0 1251 list = &(table->u.fTable);
michael@0 1252 ++(list->fCount);
michael@0 1253
michael@0 1254 /* is list still empty? */
michael@0 1255 if (list->fFirst == NULL) {
michael@0 1256 list->fFirst = res;
michael@0 1257 res->fNext = NULL;
michael@0 1258 return;
michael@0 1259 }
michael@0 1260
michael@0 1261 resKeyString = list->fRoot->fKeys + res->fKey;
michael@0 1262
michael@0 1263 current = list->fFirst;
michael@0 1264
michael@0 1265 while (current != NULL) {
michael@0 1266 const char *currentKeyString = list->fRoot->fKeys + current->fKey;
michael@0 1267 int diff;
michael@0 1268 /*
michael@0 1269 * formatVersion 1: compare key strings in native-charset order
michael@0 1270 * formatVersion 2 and up: compare key strings in ASCII order
michael@0 1271 */
michael@0 1272 if (gFormatVersion == 1 || U_CHARSET_FAMILY == U_ASCII_FAMILY) {
michael@0 1273 diff = uprv_strcmp(currentKeyString, resKeyString);
michael@0 1274 } else {
michael@0 1275 diff = uprv_compareInvCharsAsAscii(currentKeyString, resKeyString);
michael@0 1276 }
michael@0 1277 if (diff < 0) {
michael@0 1278 prev = current;
michael@0 1279 current = current->fNext;
michael@0 1280 } else if (diff > 0) {
michael@0 1281 /* we're either in front of list, or in middle */
michael@0 1282 if (prev == NULL) {
michael@0 1283 /* front of the list */
michael@0 1284 list->fFirst = res;
michael@0 1285 } else {
michael@0 1286 /* middle of the list */
michael@0 1287 prev->fNext = res;
michael@0 1288 }
michael@0 1289
michael@0 1290 res->fNext = current;
michael@0 1291 return;
michael@0 1292 } else {
michael@0 1293 /* Key already exists! ERROR! */
michael@0 1294 error(linenumber, "duplicate key '%s' in table, first appeared at line %d", currentKeyString, current->line);
michael@0 1295 *status = U_UNSUPPORTED_ERROR;
michael@0 1296 return;
michael@0 1297 }
michael@0 1298 }
michael@0 1299
michael@0 1300 /* end of list */
michael@0 1301 prev->fNext = res;
michael@0 1302 res->fNext = NULL;
michael@0 1303 }
michael@0 1304
michael@0 1305 void array_add(struct SResource *array, struct SResource *res, UErrorCode *status) {
michael@0 1306 if (U_FAILURE(*status)) {
michael@0 1307 return;
michael@0 1308 }
michael@0 1309
michael@0 1310 if (array->u.fArray.fFirst == NULL) {
michael@0 1311 array->u.fArray.fFirst = res;
michael@0 1312 array->u.fArray.fLast = res;
michael@0 1313 } else {
michael@0 1314 array->u.fArray.fLast->fNext = res;
michael@0 1315 array->u.fArray.fLast = res;
michael@0 1316 }
michael@0 1317
michael@0 1318 (array->u.fArray.fCount)++;
michael@0 1319 }
michael@0 1320
michael@0 1321 void intvector_add(struct SResource *intvector, int32_t value, UErrorCode *status) {
michael@0 1322 if (U_FAILURE(*status)) {
michael@0 1323 return;
michael@0 1324 }
michael@0 1325
michael@0 1326 *(intvector->u.fIntVector.fArray + intvector->u.fIntVector.fCount) = value;
michael@0 1327 intvector->u.fIntVector.fCount++;
michael@0 1328 }
michael@0 1329
michael@0 1330 /* Misc Functions */
michael@0 1331
michael@0 1332 void bundle_setlocale(struct SRBRoot *bundle, UChar *locale, UErrorCode *status) {
michael@0 1333
michael@0 1334 if(U_FAILURE(*status)) {
michael@0 1335 return;
michael@0 1336 }
michael@0 1337
michael@0 1338 if (bundle->fLocale!=NULL) {
michael@0 1339 uprv_free(bundle->fLocale);
michael@0 1340 }
michael@0 1341
michael@0 1342 bundle->fLocale= (char*) uprv_malloc(sizeof(char) * (u_strlen(locale)+1));
michael@0 1343
michael@0 1344 if(bundle->fLocale == NULL) {
michael@0 1345 *status = U_MEMORY_ALLOCATION_ERROR;
michael@0 1346 return;
michael@0 1347 }
michael@0 1348
michael@0 1349 /*u_strcpy(bundle->fLocale, locale);*/
michael@0 1350 u_UCharsToChars(locale, bundle->fLocale, u_strlen(locale)+1);
michael@0 1351
michael@0 1352 }
michael@0 1353
michael@0 1354 static const char *
michael@0 1355 getKeyString(const struct SRBRoot *bundle, int32_t key) {
michael@0 1356 if (key < 0) {
michael@0 1357 return bundle->fPoolBundleKeys + (key & 0x7fffffff);
michael@0 1358 } else {
michael@0 1359 return bundle->fKeys + key;
michael@0 1360 }
michael@0 1361 }
michael@0 1362
michael@0 1363 const char *
michael@0 1364 res_getKeyString(const struct SRBRoot *bundle, const struct SResource *res, char temp[8]) {
michael@0 1365 if (res->fKey == -1) {
michael@0 1366 return NULL;
michael@0 1367 }
michael@0 1368 return getKeyString(bundle, res->fKey);
michael@0 1369 }
michael@0 1370
michael@0 1371 const char *
michael@0 1372 bundle_getKeyBytes(struct SRBRoot *bundle, int32_t *pLength) {
michael@0 1373 *pLength = bundle->fKeysTop - bundle->fKeysBottom;
michael@0 1374 return bundle->fKeys + bundle->fKeysBottom;
michael@0 1375 }
michael@0 1376
michael@0 1377 int32_t
michael@0 1378 bundle_addKeyBytes(struct SRBRoot *bundle, const char *keyBytes, int32_t length, UErrorCode *status) {
michael@0 1379 int32_t keypos;
michael@0 1380
michael@0 1381 if (U_FAILURE(*status)) {
michael@0 1382 return -1;
michael@0 1383 }
michael@0 1384 if (length < 0 || (keyBytes == NULL && length != 0)) {
michael@0 1385 *status = U_ILLEGAL_ARGUMENT_ERROR;
michael@0 1386 return -1;
michael@0 1387 }
michael@0 1388 if (length == 0) {
michael@0 1389 return bundle->fKeysTop;
michael@0 1390 }
michael@0 1391
michael@0 1392 keypos = bundle->fKeysTop;
michael@0 1393 bundle->fKeysTop += length;
michael@0 1394 if (bundle->fKeysTop >= bundle->fKeysCapacity) {
michael@0 1395 /* overflow - resize the keys buffer */
michael@0 1396 bundle->fKeysCapacity += KEY_SPACE_SIZE;
michael@0 1397 bundle->fKeys = uprv_realloc(bundle->fKeys, bundle->fKeysCapacity);
michael@0 1398 if(bundle->fKeys == NULL) {
michael@0 1399 *status = U_MEMORY_ALLOCATION_ERROR;
michael@0 1400 return -1;
michael@0 1401 }
michael@0 1402 }
michael@0 1403
michael@0 1404 uprv_memcpy(bundle->fKeys + keypos, keyBytes, length);
michael@0 1405
michael@0 1406 return keypos;
michael@0 1407 }
michael@0 1408
michael@0 1409 int32_t
michael@0 1410 bundle_addtag(struct SRBRoot *bundle, const char *tag, UErrorCode *status) {
michael@0 1411 int32_t keypos;
michael@0 1412
michael@0 1413 if (U_FAILURE(*status)) {
michael@0 1414 return -1;
michael@0 1415 }
michael@0 1416
michael@0 1417 if (tag == NULL) {
michael@0 1418 /* no error: the root table and array items have no keys */
michael@0 1419 return -1;
michael@0 1420 }
michael@0 1421
michael@0 1422 keypos = bundle_addKeyBytes(bundle, tag, (int32_t)(uprv_strlen(tag) + 1), status);
michael@0 1423 if (U_SUCCESS(*status)) {
michael@0 1424 ++bundle->fKeysCount;
michael@0 1425 }
michael@0 1426 return keypos;
michael@0 1427 }
michael@0 1428
michael@0 1429 static int32_t
michael@0 1430 compareInt32(int32_t lPos, int32_t rPos) {
michael@0 1431 /*
michael@0 1432 * Compare possibly-negative key offsets. Don't just return lPos - rPos
michael@0 1433 * because that is prone to negative-integer underflows.
michael@0 1434 */
michael@0 1435 if (lPos < rPos) {
michael@0 1436 return -1;
michael@0 1437 } else if (lPos > rPos) {
michael@0 1438 return 1;
michael@0 1439 } else {
michael@0 1440 return 0;
michael@0 1441 }
michael@0 1442 }
michael@0 1443
michael@0 1444 static int32_t U_CALLCONV
michael@0 1445 compareKeySuffixes(const void *context, const void *l, const void *r) {
michael@0 1446 const struct SRBRoot *bundle=(const struct SRBRoot *)context;
michael@0 1447 int32_t lPos = ((const KeyMapEntry *)l)->oldpos;
michael@0 1448 int32_t rPos = ((const KeyMapEntry *)r)->oldpos;
michael@0 1449 const char *lStart = getKeyString(bundle, lPos);
michael@0 1450 const char *lLimit = lStart;
michael@0 1451 const char *rStart = getKeyString(bundle, rPos);
michael@0 1452 const char *rLimit = rStart;
michael@0 1453 int32_t diff;
michael@0 1454 while (*lLimit != 0) { ++lLimit; }
michael@0 1455 while (*rLimit != 0) { ++rLimit; }
michael@0 1456 /* compare keys in reverse character order */
michael@0 1457 while (lStart < lLimit && rStart < rLimit) {
michael@0 1458 diff = (int32_t)(uint8_t)*--lLimit - (int32_t)(uint8_t)*--rLimit;
michael@0 1459 if (diff != 0) {
michael@0 1460 return diff;
michael@0 1461 }
michael@0 1462 }
michael@0 1463 /* sort equal suffixes by descending key length */
michael@0 1464 diff = (int32_t)(rLimit - rStart) - (int32_t)(lLimit - lStart);
michael@0 1465 if (diff != 0) {
michael@0 1466 return diff;
michael@0 1467 }
michael@0 1468 /* Sort pool bundle keys first (negative oldpos), and otherwise keys in parsing order. */
michael@0 1469 return compareInt32(lPos, rPos);
michael@0 1470 }
michael@0 1471
michael@0 1472 static int32_t U_CALLCONV
michael@0 1473 compareKeyNewpos(const void *context, const void *l, const void *r) {
michael@0 1474 return compareInt32(((const KeyMapEntry *)l)->newpos, ((const KeyMapEntry *)r)->newpos);
michael@0 1475 }
michael@0 1476
michael@0 1477 static int32_t U_CALLCONV
michael@0 1478 compareKeyOldpos(const void *context, const void *l, const void *r) {
michael@0 1479 return compareInt32(((const KeyMapEntry *)l)->oldpos, ((const KeyMapEntry *)r)->oldpos);
michael@0 1480 }
michael@0 1481
michael@0 1482 void
michael@0 1483 bundle_compactKeys(struct SRBRoot *bundle, UErrorCode *status) {
michael@0 1484 KeyMapEntry *map;
michael@0 1485 char *keys;
michael@0 1486 int32_t i;
michael@0 1487 int32_t keysCount = bundle->fPoolBundleKeysCount + bundle->fKeysCount;
michael@0 1488 if (U_FAILURE(*status) || bundle->fKeysCount == 0 || bundle->fKeyMap != NULL) {
michael@0 1489 return;
michael@0 1490 }
michael@0 1491 map = (KeyMapEntry *)uprv_malloc(keysCount * sizeof(KeyMapEntry));
michael@0 1492 if (map == NULL) {
michael@0 1493 *status = U_MEMORY_ALLOCATION_ERROR;
michael@0 1494 return;
michael@0 1495 }
michael@0 1496 keys = (char *)bundle->fPoolBundleKeys;
michael@0 1497 for (i = 0; i < bundle->fPoolBundleKeysCount; ++i) {
michael@0 1498 map[i].oldpos =
michael@0 1499 (int32_t)(keys - bundle->fPoolBundleKeys) | 0x80000000; /* negative oldpos */
michael@0 1500 map[i].newpos = 0;
michael@0 1501 while (*keys != 0) { ++keys; } /* skip the key */
michael@0 1502 ++keys; /* skip the NUL */
michael@0 1503 }
michael@0 1504 keys = bundle->fKeys + bundle->fKeysBottom;
michael@0 1505 for (; i < keysCount; ++i) {
michael@0 1506 map[i].oldpos = (int32_t)(keys - bundle->fKeys);
michael@0 1507 map[i].newpos = 0;
michael@0 1508 while (*keys != 0) { ++keys; } /* skip the key */
michael@0 1509 ++keys; /* skip the NUL */
michael@0 1510 }
michael@0 1511 /* Sort the keys so that each one is immediately followed by all of its suffixes. */
michael@0 1512 uprv_sortArray(map, keysCount, (int32_t)sizeof(KeyMapEntry),
michael@0 1513 compareKeySuffixes, bundle, FALSE, status);
michael@0 1514 /*
michael@0 1515 * Make suffixes point into earlier, longer strings that contain them
michael@0 1516 * and mark the old, now unused suffix bytes as deleted.
michael@0 1517 */
michael@0 1518 if (U_SUCCESS(*status)) {
michael@0 1519 keys = bundle->fKeys;
michael@0 1520 for (i = 0; i < keysCount;) {
michael@0 1521 /*
michael@0 1522 * This key is not a suffix of the previous one;
michael@0 1523 * keep this one and delete the following ones that are
michael@0 1524 * suffixes of this one.
michael@0 1525 */
michael@0 1526 const char *key;
michael@0 1527 const char *keyLimit;
michael@0 1528 int32_t j = i + 1;
michael@0 1529 map[i].newpos = map[i].oldpos;
michael@0 1530 if (j < keysCount && map[j].oldpos < 0) {
michael@0 1531 /* Key string from the pool bundle, do not delete. */
michael@0 1532 i = j;
michael@0 1533 continue;
michael@0 1534 }
michael@0 1535 key = getKeyString(bundle, map[i].oldpos);
michael@0 1536 for (keyLimit = key; *keyLimit != 0; ++keyLimit) {}
michael@0 1537 for (; j < keysCount && map[j].oldpos >= 0; ++j) {
michael@0 1538 const char *k;
michael@0 1539 char *suffix;
michael@0 1540 const char *suffixLimit;
michael@0 1541 int32_t offset;
michael@0 1542 suffix = keys + map[j].oldpos;
michael@0 1543 for (suffixLimit = suffix; *suffixLimit != 0; ++suffixLimit) {}
michael@0 1544 offset = (int32_t)(keyLimit - key) - (suffixLimit - suffix);
michael@0 1545 if (offset < 0) {
michael@0 1546 break; /* suffix cannot be longer than the original */
michael@0 1547 }
michael@0 1548 /* Is it a suffix of the earlier, longer key? */
michael@0 1549 for (k = keyLimit; suffix < suffixLimit && *--k == *--suffixLimit;) {}
michael@0 1550 if (suffix == suffixLimit && *k == *suffixLimit) {
michael@0 1551 map[j].newpos = map[i].oldpos + offset; /* yes, point to the earlier key */
michael@0 1552 /* mark the suffix as deleted */
michael@0 1553 while (*suffix != 0) { *suffix++ = 1; }
michael@0 1554 *suffix = 1;
michael@0 1555 } else {
michael@0 1556 break; /* not a suffix, restart from here */
michael@0 1557 }
michael@0 1558 }
michael@0 1559 i = j;
michael@0 1560 }
michael@0 1561 /*
michael@0 1562 * Re-sort by newpos, then modify the key characters array in-place
michael@0 1563 * to squeeze out unused bytes, and readjust the newpos offsets.
michael@0 1564 */
michael@0 1565 uprv_sortArray(map, keysCount, (int32_t)sizeof(KeyMapEntry),
michael@0 1566 compareKeyNewpos, NULL, FALSE, status);
michael@0 1567 if (U_SUCCESS(*status)) {
michael@0 1568 int32_t oldpos, newpos, limit;
michael@0 1569 oldpos = newpos = bundle->fKeysBottom;
michael@0 1570 limit = bundle->fKeysTop;
michael@0 1571 /* skip key offsets that point into the pool bundle rather than this new bundle */
michael@0 1572 for (i = 0; i < keysCount && map[i].newpos < 0; ++i) {}
michael@0 1573 if (i < keysCount) {
michael@0 1574 while (oldpos < limit) {
michael@0 1575 if (keys[oldpos] == 1) {
michael@0 1576 ++oldpos; /* skip unused bytes */
michael@0 1577 } else {
michael@0 1578 /* adjust the new offsets for keys starting here */
michael@0 1579 while (i < keysCount && map[i].newpos == oldpos) {
michael@0 1580 map[i++].newpos = newpos;
michael@0 1581 }
michael@0 1582 /* move the key characters to their new position */
michael@0 1583 keys[newpos++] = keys[oldpos++];
michael@0 1584 }
michael@0 1585 }
michael@0 1586 assert(i == keysCount);
michael@0 1587 }
michael@0 1588 bundle->fKeysTop = newpos;
michael@0 1589 /* Re-sort once more, by old offsets for binary searching. */
michael@0 1590 uprv_sortArray(map, keysCount, (int32_t)sizeof(KeyMapEntry),
michael@0 1591 compareKeyOldpos, NULL, FALSE, status);
michael@0 1592 if (U_SUCCESS(*status)) {
michael@0 1593 /* key size reduction by limit - newpos */
michael@0 1594 bundle->fKeyMap = map;
michael@0 1595 map = NULL;
michael@0 1596 }
michael@0 1597 }
michael@0 1598 }
michael@0 1599 uprv_free(map);
michael@0 1600 }
michael@0 1601
michael@0 1602 static int32_t U_CALLCONV
michael@0 1603 compareStringSuffixes(const void *context, const void *l, const void *r) {
michael@0 1604 struct SResource *left = *((struct SResource **)l);
michael@0 1605 struct SResource *right = *((struct SResource **)r);
michael@0 1606 const UChar *lStart = left->u.fString.fChars;
michael@0 1607 const UChar *lLimit = lStart + left->u.fString.fLength;
michael@0 1608 const UChar *rStart = right->u.fString.fChars;
michael@0 1609 const UChar *rLimit = rStart + right->u.fString.fLength;
michael@0 1610 int32_t diff;
michael@0 1611 /* compare keys in reverse character order */
michael@0 1612 while (lStart < lLimit && rStart < rLimit) {
michael@0 1613 diff = (int32_t)*--lLimit - (int32_t)*--rLimit;
michael@0 1614 if (diff != 0) {
michael@0 1615 return diff;
michael@0 1616 }
michael@0 1617 }
michael@0 1618 /* sort equal suffixes by descending string length */
michael@0 1619 return right->u.fString.fLength - left->u.fString.fLength;
michael@0 1620 }
michael@0 1621
michael@0 1622 static int32_t U_CALLCONV
michael@0 1623 compareStringLengths(const void *context, const void *l, const void *r) {
michael@0 1624 struct SResource *left = *((struct SResource **)l);
michael@0 1625 struct SResource *right = *((struct SResource **)r);
michael@0 1626 int32_t diff;
michael@0 1627 /* Make "is suffix of another string" compare greater than a non-suffix. */
michael@0 1628 diff = (int)(left->u.fString.fSame != NULL) - (int)(right->u.fString.fSame != NULL);
michael@0 1629 if (diff != 0) {
michael@0 1630 return diff;
michael@0 1631 }
michael@0 1632 /* sort by ascending string length */
michael@0 1633 return left->u.fString.fLength - right->u.fString.fLength;
michael@0 1634 }
michael@0 1635
michael@0 1636 static int32_t
michael@0 1637 string_writeUTF16v2(struct SRBRoot *bundle, struct SResource *res, int32_t utf16Length) {
michael@0 1638 int32_t length = res->u.fString.fLength;
michael@0 1639 res->fRes = URES_MAKE_RESOURCE(URES_STRING_V2, utf16Length);
michael@0 1640 res->fWritten = TRUE;
michael@0 1641 switch(res->u.fString.fNumCharsForLength) {
michael@0 1642 case 0:
michael@0 1643 break;
michael@0 1644 case 1:
michael@0 1645 bundle->f16BitUnits[utf16Length++] = (uint16_t)(0xdc00 + length);
michael@0 1646 break;
michael@0 1647 case 2:
michael@0 1648 bundle->f16BitUnits[utf16Length] = (uint16_t)(0xdfef + (length >> 16));
michael@0 1649 bundle->f16BitUnits[utf16Length + 1] = (uint16_t)length;
michael@0 1650 utf16Length += 2;
michael@0 1651 break;
michael@0 1652 case 3:
michael@0 1653 bundle->f16BitUnits[utf16Length] = 0xdfff;
michael@0 1654 bundle->f16BitUnits[utf16Length + 1] = (uint16_t)(length >> 16);
michael@0 1655 bundle->f16BitUnits[utf16Length + 2] = (uint16_t)length;
michael@0 1656 utf16Length += 3;
michael@0 1657 break;
michael@0 1658 default:
michael@0 1659 break; /* will not occur */
michael@0 1660 }
michael@0 1661 u_memcpy(bundle->f16BitUnits + utf16Length, res->u.fString.fChars, length + 1);
michael@0 1662 return utf16Length + length + 1;
michael@0 1663 }
michael@0 1664
michael@0 1665 static void
michael@0 1666 bundle_compactStrings(struct SRBRoot *bundle, UErrorCode *status) {
michael@0 1667 if (U_FAILURE(*status)) {
michael@0 1668 return;
michael@0 1669 }
michael@0 1670 switch(bundle->fStringsForm) {
michael@0 1671 case STRINGS_UTF16_V2:
michael@0 1672 if (bundle->f16BitUnitsLength > 0) {
michael@0 1673 struct SResource **array;
michael@0 1674 int32_t count = uhash_count(bundle->fStringSet);
michael@0 1675 int32_t i, pos;
michael@0 1676 /*
michael@0 1677 * Allocate enough space for the initial NUL and the UTF-16 v2 strings,
michael@0 1678 * and some extra for URES_TABLE16 and URES_ARRAY16 values.
michael@0 1679 * Round down to an even number.
michael@0 1680 */
michael@0 1681 int32_t utf16Length = (bundle->f16BitUnitsLength + 20000) & ~1;
michael@0 1682 bundle->f16BitUnits = (UChar *)uprv_malloc(utf16Length * U_SIZEOF_UCHAR);
michael@0 1683 array = (struct SResource **)uprv_malloc(count * sizeof(struct SResource **));
michael@0 1684 if (bundle->f16BitUnits == NULL || array == NULL) {
michael@0 1685 uprv_free(bundle->f16BitUnits);
michael@0 1686 bundle->f16BitUnits = NULL;
michael@0 1687 uprv_free(array);
michael@0 1688 *status = U_MEMORY_ALLOCATION_ERROR;
michael@0 1689 return;
michael@0 1690 }
michael@0 1691 bundle->f16BitUnitsCapacity = utf16Length;
michael@0 1692 /* insert the initial NUL */
michael@0 1693 bundle->f16BitUnits[0] = 0;
michael@0 1694 utf16Length = 1;
michael@0 1695 ++bundle->f16BitUnitsLength;
michael@0 1696 for (pos = -1, i = 0; i < count; ++i) {
michael@0 1697 array[i] = (struct SResource *)uhash_nextElement(bundle->fStringSet, &pos)->key.pointer;
michael@0 1698 }
michael@0 1699 /* Sort the strings so that each one is immediately followed by all of its suffixes. */
michael@0 1700 uprv_sortArray(array, count, (int32_t)sizeof(struct SResource **),
michael@0 1701 compareStringSuffixes, NULL, FALSE, status);
michael@0 1702 /*
michael@0 1703 * Make suffixes point into earlier, longer strings that contain them.
michael@0 1704 * Temporarily use fSame and fSuffixOffset for suffix strings to
michael@0 1705 * refer to the remaining ones.
michael@0 1706 */
michael@0 1707 if (U_SUCCESS(*status)) {
michael@0 1708 for (i = 0; i < count;) {
michael@0 1709 /*
michael@0 1710 * This string is not a suffix of the previous one;
michael@0 1711 * write this one and subsume the following ones that are
michael@0 1712 * suffixes of this one.
michael@0 1713 */
michael@0 1714 struct SResource *res = array[i];
michael@0 1715 const UChar *strLimit = res->u.fString.fChars + res->u.fString.fLength;
michael@0 1716 int32_t j;
michael@0 1717 for (j = i + 1; j < count; ++j) {
michael@0 1718 struct SResource *suffixRes = array[j];
michael@0 1719 const UChar *s;
michael@0 1720 const UChar *suffix = suffixRes->u.fString.fChars;
michael@0 1721 const UChar *suffixLimit = suffix + suffixRes->u.fString.fLength;
michael@0 1722 int32_t offset = res->u.fString.fLength - suffixRes->u.fString.fLength;
michael@0 1723 if (offset < 0) {
michael@0 1724 break; /* suffix cannot be longer than the original */
michael@0 1725 }
michael@0 1726 /* Is it a suffix of the earlier, longer key? */
michael@0 1727 for (s = strLimit; suffix < suffixLimit && *--s == *--suffixLimit;) {}
michael@0 1728 if (suffix == suffixLimit && *s == *suffixLimit) {
michael@0 1729 if (suffixRes->u.fString.fNumCharsForLength == 0) {
michael@0 1730 /* yes, point to the earlier string */
michael@0 1731 suffixRes->u.fString.fSame = res;
michael@0 1732 suffixRes->u.fString.fSuffixOffset = offset;
michael@0 1733 } else {
michael@0 1734 /* write the suffix by itself if we need explicit length */
michael@0 1735 }
michael@0 1736 } else {
michael@0 1737 break; /* not a suffix, restart from here */
michael@0 1738 }
michael@0 1739 }
michael@0 1740 i = j;
michael@0 1741 }
michael@0 1742 }
michael@0 1743 /*
michael@0 1744 * Re-sort the strings by ascending length (except suffixes last)
michael@0 1745 * to optimize for URES_TABLE16 and URES_ARRAY16:
michael@0 1746 * Keep as many as possible within reach of 16-bit offsets.
michael@0 1747 */
michael@0 1748 uprv_sortArray(array, count, (int32_t)sizeof(struct SResource **),
michael@0 1749 compareStringLengths, NULL, FALSE, status);
michael@0 1750 if (U_SUCCESS(*status)) {
michael@0 1751 /* Write the non-suffix strings. */
michael@0 1752 for (i = 0; i < count && array[i]->u.fString.fSame == NULL; ++i) {
michael@0 1753 utf16Length = string_writeUTF16v2(bundle, array[i], utf16Length);
michael@0 1754 }
michael@0 1755 /* Write the suffix strings. Make each point to the real string. */
michael@0 1756 for (; i < count; ++i) {
michael@0 1757 struct SResource *res = array[i];
michael@0 1758 struct SResource *same = res->u.fString.fSame;
michael@0 1759 res->fRes = same->fRes + same->u.fString.fNumCharsForLength + res->u.fString.fSuffixOffset;
michael@0 1760 res->u.fString.fSame = NULL;
michael@0 1761 res->fWritten = TRUE;
michael@0 1762 }
michael@0 1763 }
michael@0 1764 assert(utf16Length <= bundle->f16BitUnitsLength);
michael@0 1765 bundle->f16BitUnitsLength = utf16Length;
michael@0 1766 uprv_free(array);
michael@0 1767 }
michael@0 1768 break;
michael@0 1769 default:
michael@0 1770 break;
michael@0 1771 }
michael@0 1772 }

mercurial