michael@0: /* michael@0: ******************************************************************************* michael@0: * michael@0: * Copyright (C) 2000-2012, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: * michael@0: ******************************************************************************* michael@0: * michael@0: * File reslist.c michael@0: * michael@0: * Modification History: michael@0: * michael@0: * Date Name Description michael@0: * 02/21/00 weiv Creation. michael@0: ******************************************************************************* michael@0: */ michael@0: michael@0: #include michael@0: #include michael@0: #include "reslist.h" michael@0: #include "unewdata.h" michael@0: #include "unicode/ures.h" michael@0: #include "unicode/putil.h" michael@0: #include "errmsg.h" michael@0: michael@0: #include "uarrsort.h" michael@0: #include "uelement.h" michael@0: #include "uinvchar.h" michael@0: #include "ustr_imp.h" michael@0: #include "unicode/utf16.h" michael@0: /* michael@0: * Align binary data at a 16-byte offset from the start of the resource bundle, michael@0: * to be safe for any data type it may contain. michael@0: */ michael@0: #define BIN_ALIGNMENT 16 michael@0: michael@0: static UBool gIncludeCopyright = FALSE; michael@0: static UBool gUsePoolBundle = FALSE; michael@0: static int32_t gFormatVersion = 2; michael@0: michael@0: static UChar gEmptyString = 0; michael@0: michael@0: /* How do we store string values? */ michael@0: enum { michael@0: STRINGS_UTF16_V1, /* formatVersion 1: int length + UChars + NUL + padding to 4 bytes */ michael@0: STRINGS_UTF16_V2 /* formatVersion 2: optional length in 1..3 UChars + UChars + NUL */ michael@0: }; michael@0: michael@0: enum { michael@0: MAX_IMPLICIT_STRING_LENGTH = 40 /* do not store the length explicitly for such strings */ michael@0: }; michael@0: michael@0: /* michael@0: * res_none() returns the address of kNoResource, michael@0: * for use in non-error cases when no resource is to be added to the bundle. michael@0: * (NULL is used in error cases.) michael@0: */ michael@0: static const struct SResource kNoResource = { URES_NONE }; michael@0: michael@0: static UDataInfo dataInfo= { michael@0: sizeof(UDataInfo), michael@0: 0, michael@0: michael@0: U_IS_BIG_ENDIAN, michael@0: U_CHARSET_FAMILY, michael@0: sizeof(UChar), michael@0: 0, michael@0: michael@0: {0x52, 0x65, 0x73, 0x42}, /* dataFormat="ResB" */ michael@0: {1, 3, 0, 0}, /* formatVersion */ michael@0: {1, 4, 0, 0} /* dataVersion take a look at version inside parsed resb*/ michael@0: }; michael@0: michael@0: static const UVersionInfo gFormatVersions[3] = { /* indexed by a major-formatVersion integer */ michael@0: { 0, 0, 0, 0 }, michael@0: { 1, 3, 0, 0 }, michael@0: { 2, 0, 0, 0 } michael@0: }; michael@0: michael@0: static uint8_t calcPadding(uint32_t size) { michael@0: /* returns space we need to pad */ michael@0: return (uint8_t) ((size % sizeof(uint32_t)) ? (sizeof(uint32_t) - (size % sizeof(uint32_t))) : 0); michael@0: michael@0: } michael@0: michael@0: void setIncludeCopyright(UBool val){ michael@0: gIncludeCopyright=val; michael@0: } michael@0: michael@0: UBool getIncludeCopyright(void){ michael@0: return gIncludeCopyright; michael@0: } michael@0: michael@0: void setFormatVersion(int32_t formatVersion) { michael@0: gFormatVersion = formatVersion; michael@0: } michael@0: michael@0: void setUsePoolBundle(UBool use) { michael@0: gUsePoolBundle = use; michael@0: } michael@0: michael@0: static void michael@0: bundle_compactStrings(struct SRBRoot *bundle, UErrorCode *status); michael@0: michael@0: /* Writing Functions */ michael@0: michael@0: /* michael@0: * type_write16() functions write resource values into f16BitUnits michael@0: * and determine the resource item word, if possible. michael@0: */ michael@0: static void michael@0: res_write16(struct SRBRoot *bundle, struct SResource *res, michael@0: UErrorCode *status); michael@0: michael@0: /* michael@0: * type_preWrite() functions calculate ("preflight") and advance the *byteOffset michael@0: * by the size of their data in the binary file and michael@0: * determine the resource item word. michael@0: * Most type_preWrite() functions may add any number of bytes, but res_preWrite() michael@0: * will always pad it to a multiple of 4. michael@0: * The resource item type may be a related subtype of the fType. michael@0: * michael@0: * The type_preWrite() and type_write() functions start and end at the same michael@0: * byteOffset values. michael@0: * Prewriting allows bundle_write() to determine the root resource item word, michael@0: * before actually writing the bundle contents to the file, michael@0: * which is necessary because the root item is stored at the beginning. michael@0: */ michael@0: static void michael@0: res_preWrite(uint32_t *byteOffset, michael@0: struct SRBRoot *bundle, struct SResource *res, michael@0: UErrorCode *status); michael@0: michael@0: /* michael@0: * type_write() functions write their data to mem and update the byteOffset michael@0: * in parallel. michael@0: * (A kingdom for C++ and polymorphism...) michael@0: */ michael@0: static void michael@0: res_write(UNewDataMemory *mem, uint32_t *byteOffset, michael@0: struct SRBRoot *bundle, struct SResource *res, michael@0: UErrorCode *status); michael@0: michael@0: static uint16_t * michael@0: reserve16BitUnits(struct SRBRoot *bundle, int32_t length, UErrorCode *status) { michael@0: if (U_FAILURE(*status)) { michael@0: return NULL; michael@0: } michael@0: if ((bundle->f16BitUnitsLength + length) > bundle->f16BitUnitsCapacity) { michael@0: uint16_t *newUnits; michael@0: int32_t capacity = 2 * bundle->f16BitUnitsCapacity + length + 1024; michael@0: capacity &= ~1; /* ensures padding fits if f16BitUnitsLength needs it */ michael@0: newUnits = (uint16_t *)uprv_malloc(capacity * 2); michael@0: if (newUnits == NULL) { michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: return NULL; michael@0: } michael@0: if (bundle->f16BitUnitsLength > 0) { michael@0: uprv_memcpy(newUnits, bundle->f16BitUnits, bundle->f16BitUnitsLength * 2); michael@0: } else { michael@0: newUnits[0] = 0; michael@0: bundle->f16BitUnitsLength = 1; michael@0: } michael@0: uprv_free(bundle->f16BitUnits); michael@0: bundle->f16BitUnits = newUnits; michael@0: bundle->f16BitUnitsCapacity = capacity; michael@0: } michael@0: return bundle->f16BitUnits + bundle->f16BitUnitsLength; michael@0: } michael@0: michael@0: static int32_t michael@0: makeRes16(uint32_t resWord) { michael@0: uint32_t type, offset; michael@0: if (resWord == 0) { michael@0: return 0; /* empty string */ michael@0: } michael@0: type = RES_GET_TYPE(resWord); michael@0: offset = RES_GET_OFFSET(resWord); michael@0: if (type == URES_STRING_V2 && offset <= 0xffff) { michael@0: return (int32_t)offset; michael@0: } michael@0: return -1; michael@0: } michael@0: michael@0: static int32_t michael@0: mapKey(struct SRBRoot *bundle, int32_t oldpos) { michael@0: const KeyMapEntry *map = bundle->fKeyMap; michael@0: int32_t i, start, limit; michael@0: michael@0: /* do a binary search for the old, pre-bundle_compactKeys() key offset */ michael@0: start = bundle->fPoolBundleKeysCount; michael@0: limit = start + bundle->fKeysCount; michael@0: while (start < limit - 1) { michael@0: i = (start + limit) / 2; michael@0: if (oldpos < map[i].oldpos) { michael@0: limit = i; michael@0: } else { michael@0: start = i; michael@0: } michael@0: } michael@0: assert(oldpos == map[start].oldpos); michael@0: return map[start].newpos; michael@0: } michael@0: michael@0: static uint16_t michael@0: makeKey16(struct SRBRoot *bundle, int32_t key) { michael@0: if (key >= 0) { michael@0: return (uint16_t)key; michael@0: } else { michael@0: return (uint16_t)(key + bundle->fLocalKeyLimit); /* offset in the pool bundle */ michael@0: } michael@0: } michael@0: michael@0: /* michael@0: * Only called for UTF-16 v1 strings and duplicate UTF-16 v2 strings. michael@0: * For unique UTF-16 v2 strings, res_write16() sees fRes != RES_BOGUS michael@0: * and exits early. michael@0: */ michael@0: static void michael@0: string_write16(struct SRBRoot *bundle, struct SResource *res, UErrorCode *status) { michael@0: struct SResource *same; michael@0: if ((same = res->u.fString.fSame) != NULL) { michael@0: /* This is a duplicate. */ michael@0: if (same->fRes == RES_BOGUS) { michael@0: /* The original has not been visited yet. */ michael@0: string_write16(bundle, same, status); michael@0: } michael@0: res->fRes = same->fRes; michael@0: res->fWritten = same->fWritten; michael@0: } michael@0: } michael@0: michael@0: static void michael@0: array_write16(struct SRBRoot *bundle, struct SResource *res, michael@0: UErrorCode *status) { michael@0: struct SResource *current; michael@0: int32_t res16 = 0; michael@0: michael@0: if (U_FAILURE(*status)) { michael@0: return; michael@0: } michael@0: if (res->u.fArray.fCount == 0 && gFormatVersion > 1) { michael@0: res->fRes = URES_MAKE_EMPTY_RESOURCE(URES_ARRAY); michael@0: res->fWritten = TRUE; michael@0: return; michael@0: } michael@0: for (current = res->u.fArray.fFirst; current != NULL; current = current->fNext) { michael@0: res_write16(bundle, current, status); michael@0: res16 |= makeRes16(current->fRes); michael@0: } michael@0: if (U_SUCCESS(*status) && res->u.fArray.fCount <= 0xffff && res16 >= 0 && gFormatVersion > 1) { michael@0: uint16_t *p16 = reserve16BitUnits(bundle, 1 + res->u.fArray.fCount, status); michael@0: if (U_SUCCESS(*status)) { michael@0: res->fRes = URES_MAKE_RESOURCE(URES_ARRAY16, bundle->f16BitUnitsLength); michael@0: *p16++ = (uint16_t)res->u.fArray.fCount; michael@0: for (current = res->u.fArray.fFirst; current != NULL; current = current->fNext) { michael@0: *p16++ = (uint16_t)makeRes16(current->fRes); michael@0: } michael@0: bundle->f16BitUnitsLength += 1 + res->u.fArray.fCount; michael@0: res->fWritten = TRUE; michael@0: } michael@0: } michael@0: } michael@0: michael@0: static void michael@0: table_write16(struct SRBRoot *bundle, struct SResource *res, michael@0: UErrorCode *status) { michael@0: struct SResource *current; michael@0: int32_t maxKey = 0, maxPoolKey = 0x80000000; michael@0: int32_t res16 = 0; michael@0: UBool hasLocalKeys = FALSE, hasPoolKeys = FALSE; michael@0: michael@0: if (U_FAILURE(*status)) { michael@0: return; michael@0: } michael@0: if (res->u.fTable.fCount == 0 && gFormatVersion > 1) { michael@0: res->fRes = URES_MAKE_EMPTY_RESOURCE(URES_TABLE); michael@0: res->fWritten = TRUE; michael@0: return; michael@0: } michael@0: /* Find the smallest table type that fits the data. */ michael@0: for (current = res->u.fTable.fFirst; current != NULL; current = current->fNext) { michael@0: int32_t key; michael@0: res_write16(bundle, current, status); michael@0: if (bundle->fKeyMap == NULL) { michael@0: key = current->fKey; michael@0: } else { michael@0: key = current->fKey = mapKey(bundle, current->fKey); michael@0: } michael@0: if (key >= 0) { michael@0: hasLocalKeys = TRUE; michael@0: if (key > maxKey) { michael@0: maxKey = key; michael@0: } michael@0: } else { michael@0: hasPoolKeys = TRUE; michael@0: if (key > maxPoolKey) { michael@0: maxPoolKey = key; michael@0: } michael@0: } michael@0: res16 |= makeRes16(current->fRes); michael@0: } michael@0: if (U_FAILURE(*status)) { michael@0: return; michael@0: } michael@0: if(res->u.fTable.fCount > (uint32_t)bundle->fMaxTableLength) { michael@0: bundle->fMaxTableLength = res->u.fTable.fCount; michael@0: } michael@0: maxPoolKey &= 0x7fffffff; michael@0: if (res->u.fTable.fCount <= 0xffff && michael@0: (!hasLocalKeys || maxKey < bundle->fLocalKeyLimit) && michael@0: (!hasPoolKeys || maxPoolKey < (0x10000 - bundle->fLocalKeyLimit)) michael@0: ) { michael@0: if (res16 >= 0 && gFormatVersion > 1) { michael@0: uint16_t *p16 = reserve16BitUnits(bundle, 1 + res->u.fTable.fCount * 2, status); michael@0: if (U_SUCCESS(*status)) { michael@0: /* 16-bit count, key offsets and values */ michael@0: res->fRes = URES_MAKE_RESOURCE(URES_TABLE16, bundle->f16BitUnitsLength); michael@0: *p16++ = (uint16_t)res->u.fTable.fCount; michael@0: for (current = res->u.fTable.fFirst; current != NULL; current = current->fNext) { michael@0: *p16++ = makeKey16(bundle, current->fKey); michael@0: } michael@0: for (current = res->u.fTable.fFirst; current != NULL; current = current->fNext) { michael@0: *p16++ = (uint16_t)makeRes16(current->fRes); michael@0: } michael@0: bundle->f16BitUnitsLength += 1 + res->u.fTable.fCount * 2; michael@0: res->fWritten = TRUE; michael@0: } michael@0: } else { michael@0: /* 16-bit count, 16-bit key offsets, 32-bit values */ michael@0: res->u.fTable.fType = URES_TABLE; michael@0: } michael@0: } else { michael@0: /* 32-bit count, key offsets and values */ michael@0: res->u.fTable.fType = URES_TABLE32; michael@0: } michael@0: } michael@0: michael@0: static void michael@0: res_write16(struct SRBRoot *bundle, struct SResource *res, michael@0: UErrorCode *status) { michael@0: if (U_FAILURE(*status) || res == NULL) { michael@0: return; michael@0: } michael@0: if (res->fRes != RES_BOGUS) { michael@0: /* michael@0: * The resource item word was already precomputed, which means michael@0: * no further data needs to be written. michael@0: * This might be an integer, or an empty or UTF-16 v2 string, michael@0: * an empty binary, etc. michael@0: */ michael@0: return; michael@0: } michael@0: switch (res->fType) { michael@0: case URES_STRING: michael@0: string_write16(bundle, res, status); michael@0: break; michael@0: case URES_ARRAY: michael@0: array_write16(bundle, res, status); michael@0: break; michael@0: case URES_TABLE: michael@0: table_write16(bundle, res, status); michael@0: break; michael@0: default: michael@0: /* Only a few resource types write 16-bit units. */ michael@0: break; michael@0: } michael@0: } michael@0: michael@0: /* michael@0: * Only called for UTF-16 v1 strings. michael@0: * For UTF-16 v2 strings, res_preWrite() sees fRes != RES_BOGUS michael@0: * and exits early. michael@0: */ michael@0: static void michael@0: string_preWrite(uint32_t *byteOffset, michael@0: struct SRBRoot *bundle, struct SResource *res, michael@0: UErrorCode *status) { michael@0: /* Write the UTF-16 v1 string. */ michael@0: res->fRes = URES_MAKE_RESOURCE(URES_STRING, *byteOffset >> 2); michael@0: *byteOffset += 4 + (res->u.fString.fLength + 1) * U_SIZEOF_UCHAR; michael@0: } michael@0: michael@0: static void michael@0: bin_preWrite(uint32_t *byteOffset, michael@0: struct SRBRoot *bundle, struct SResource *res, michael@0: UErrorCode *status) { michael@0: uint32_t pad = 0; michael@0: uint32_t dataStart = *byteOffset + sizeof(res->u.fBinaryValue.fLength); michael@0: michael@0: if (dataStart % BIN_ALIGNMENT) { michael@0: pad = (BIN_ALIGNMENT - dataStart % BIN_ALIGNMENT); michael@0: *byteOffset += pad; /* pad == 4 or 8 or 12 */ michael@0: } michael@0: res->fRes = URES_MAKE_RESOURCE(URES_BINARY, *byteOffset >> 2); michael@0: *byteOffset += 4 + res->u.fBinaryValue.fLength; michael@0: } michael@0: michael@0: static void michael@0: array_preWrite(uint32_t *byteOffset, michael@0: struct SRBRoot *bundle, struct SResource *res, michael@0: UErrorCode *status) { michael@0: struct SResource *current; michael@0: michael@0: if (U_FAILURE(*status)) { michael@0: return; michael@0: } michael@0: for (current = res->u.fArray.fFirst; current != NULL; current = current->fNext) { michael@0: res_preWrite(byteOffset, bundle, current, status); michael@0: } michael@0: res->fRes = URES_MAKE_RESOURCE(URES_ARRAY, *byteOffset >> 2); michael@0: *byteOffset += (1 + res->u.fArray.fCount) * 4; michael@0: } michael@0: michael@0: static void michael@0: table_preWrite(uint32_t *byteOffset, michael@0: struct SRBRoot *bundle, struct SResource *res, michael@0: UErrorCode *status) { michael@0: struct SResource *current; michael@0: michael@0: if (U_FAILURE(*status)) { michael@0: return; michael@0: } michael@0: for (current = res->u.fTable.fFirst; current != NULL; current = current->fNext) { michael@0: res_preWrite(byteOffset, bundle, current, status); michael@0: } michael@0: if (res->u.fTable.fType == URES_TABLE) { michael@0: /* 16-bit count, 16-bit key offsets, 32-bit values */ michael@0: res->fRes = URES_MAKE_RESOURCE(URES_TABLE, *byteOffset >> 2); michael@0: *byteOffset += 2 + res->u.fTable.fCount * 6; michael@0: } else { michael@0: /* 32-bit count, key offsets and values */ michael@0: res->fRes = URES_MAKE_RESOURCE(URES_TABLE32, *byteOffset >> 2); michael@0: *byteOffset += 4 + res->u.fTable.fCount * 8; michael@0: } michael@0: } michael@0: michael@0: static void michael@0: res_preWrite(uint32_t *byteOffset, michael@0: struct SRBRoot *bundle, struct SResource *res, michael@0: UErrorCode *status) { michael@0: if (U_FAILURE(*status) || res == NULL) { michael@0: return; michael@0: } michael@0: if (res->fRes != RES_BOGUS) { michael@0: /* michael@0: * The resource item word was already precomputed, which means michael@0: * no further data needs to be written. michael@0: * This might be an integer, or an empty or UTF-16 v2 string, michael@0: * an empty binary, etc. michael@0: */ michael@0: return; michael@0: } michael@0: switch (res->fType) { michael@0: case URES_STRING: michael@0: string_preWrite(byteOffset, bundle, res, status); michael@0: break; michael@0: case URES_ALIAS: michael@0: res->fRes = URES_MAKE_RESOURCE(URES_ALIAS, *byteOffset >> 2); michael@0: *byteOffset += 4 + (res->u.fString.fLength + 1) * U_SIZEOF_UCHAR; michael@0: break; michael@0: case URES_INT_VECTOR: michael@0: if (res->u.fIntVector.fCount == 0 && gFormatVersion > 1) { michael@0: res->fRes = URES_MAKE_EMPTY_RESOURCE(URES_INT_VECTOR); michael@0: res->fWritten = TRUE; michael@0: } else { michael@0: res->fRes = URES_MAKE_RESOURCE(URES_INT_VECTOR, *byteOffset >> 2); michael@0: *byteOffset += (1 + res->u.fIntVector.fCount) * 4; michael@0: } michael@0: break; michael@0: case URES_BINARY: michael@0: bin_preWrite(byteOffset, bundle, res, status); michael@0: break; michael@0: case URES_INT: michael@0: break; michael@0: case URES_ARRAY: michael@0: array_preWrite(byteOffset, bundle, res, status); michael@0: break; michael@0: case URES_TABLE: michael@0: table_preWrite(byteOffset, bundle, res, status); michael@0: break; michael@0: default: michael@0: *status = U_INTERNAL_PROGRAM_ERROR; michael@0: break; michael@0: } michael@0: *byteOffset += calcPadding(*byteOffset); michael@0: } michael@0: michael@0: /* michael@0: * Only called for UTF-16 v1 strings. For UTF-16 v2 strings, michael@0: * res_write() sees fWritten and exits early. michael@0: */ michael@0: static void string_write(UNewDataMemory *mem, uint32_t *byteOffset, michael@0: struct SRBRoot *bundle, struct SResource *res, michael@0: UErrorCode *status) { michael@0: /* Write the UTF-16 v1 string. */ michael@0: int32_t length = res->u.fString.fLength; michael@0: udata_write32(mem, length); michael@0: udata_writeUString(mem, res->u.fString.fChars, length + 1); michael@0: *byteOffset += 4 + (length + 1) * U_SIZEOF_UCHAR; michael@0: res->fWritten = TRUE; michael@0: } michael@0: michael@0: static void alias_write(UNewDataMemory *mem, uint32_t *byteOffset, michael@0: struct SRBRoot *bundle, struct SResource *res, michael@0: UErrorCode *status) { michael@0: int32_t length = res->u.fString.fLength; michael@0: udata_write32(mem, length); michael@0: udata_writeUString(mem, res->u.fString.fChars, length + 1); michael@0: *byteOffset += 4 + (length + 1) * U_SIZEOF_UCHAR; michael@0: } michael@0: michael@0: static void array_write(UNewDataMemory *mem, uint32_t *byteOffset, michael@0: struct SRBRoot *bundle, struct SResource *res, michael@0: UErrorCode *status) { michael@0: uint32_t i; michael@0: michael@0: struct SResource *current = NULL; michael@0: michael@0: if (U_FAILURE(*status)) { michael@0: return; michael@0: } michael@0: for (i = 0, current = res->u.fArray.fFirst; current != NULL; ++i, current = current->fNext) { michael@0: res_write(mem, byteOffset, bundle, current, status); michael@0: } michael@0: assert(i == res->u.fArray.fCount); michael@0: michael@0: udata_write32(mem, res->u.fArray.fCount); michael@0: for (current = res->u.fArray.fFirst; current != NULL; current = current->fNext) { michael@0: udata_write32(mem, current->fRes); michael@0: } michael@0: *byteOffset += (1 + res->u.fArray.fCount) * 4; michael@0: } michael@0: michael@0: static void intvector_write(UNewDataMemory *mem, uint32_t *byteOffset, michael@0: struct SRBRoot *bundle, struct SResource *res, michael@0: UErrorCode *status) { michael@0: uint32_t i = 0; michael@0: udata_write32(mem, res->u.fIntVector.fCount); michael@0: for(i = 0; iu.fIntVector.fCount; i++) { michael@0: udata_write32(mem, res->u.fIntVector.fArray[i]); michael@0: } michael@0: *byteOffset += (1 + res->u.fIntVector.fCount) * 4; michael@0: } michael@0: michael@0: static void bin_write(UNewDataMemory *mem, uint32_t *byteOffset, michael@0: struct SRBRoot *bundle, struct SResource *res, michael@0: UErrorCode *status) { michael@0: uint32_t pad = 0; michael@0: uint32_t dataStart = *byteOffset + sizeof(res->u.fBinaryValue.fLength); michael@0: michael@0: if (dataStart % BIN_ALIGNMENT) { michael@0: pad = (BIN_ALIGNMENT - dataStart % BIN_ALIGNMENT); michael@0: udata_writePadding(mem, pad); /* pad == 4 or 8 or 12 */ michael@0: *byteOffset += pad; michael@0: } michael@0: michael@0: udata_write32(mem, res->u.fBinaryValue.fLength); michael@0: if (res->u.fBinaryValue.fLength > 0) { michael@0: udata_writeBlock(mem, res->u.fBinaryValue.fData, res->u.fBinaryValue.fLength); michael@0: } michael@0: *byteOffset += 4 + res->u.fBinaryValue.fLength; michael@0: } michael@0: michael@0: static void table_write(UNewDataMemory *mem, uint32_t *byteOffset, michael@0: struct SRBRoot *bundle, struct SResource *res, michael@0: UErrorCode *status) { michael@0: struct SResource *current; michael@0: uint32_t i; michael@0: michael@0: if (U_FAILURE(*status)) { michael@0: return; michael@0: } michael@0: for (i = 0, current = res->u.fTable.fFirst; current != NULL; ++i, current = current->fNext) { michael@0: assert(i < res->u.fTable.fCount); michael@0: res_write(mem, byteOffset, bundle, current, status); michael@0: } michael@0: assert(i == res->u.fTable.fCount); michael@0: michael@0: if(res->u.fTable.fType == URES_TABLE) { michael@0: udata_write16(mem, (uint16_t)res->u.fTable.fCount); michael@0: for (current = res->u.fTable.fFirst; current != NULL; current = current->fNext) { michael@0: udata_write16(mem, makeKey16(bundle, current->fKey)); michael@0: } michael@0: *byteOffset += (1 + res->u.fTable.fCount)* 2; michael@0: if ((res->u.fTable.fCount & 1) == 0) { michael@0: /* 16-bit count and even number of 16-bit key offsets need padding before 32-bit resource items */ michael@0: udata_writePadding(mem, 2); michael@0: *byteOffset += 2; michael@0: } michael@0: } else /* URES_TABLE32 */ { michael@0: udata_write32(mem, res->u.fTable.fCount); michael@0: for (current = res->u.fTable.fFirst; current != NULL; current = current->fNext) { michael@0: udata_write32(mem, (uint32_t)current->fKey); michael@0: } michael@0: *byteOffset += (1 + res->u.fTable.fCount)* 4; michael@0: } michael@0: for (current = res->u.fTable.fFirst; current != NULL; current = current->fNext) { michael@0: udata_write32(mem, current->fRes); michael@0: } michael@0: *byteOffset += res->u.fTable.fCount * 4; michael@0: } michael@0: michael@0: void res_write(UNewDataMemory *mem, uint32_t *byteOffset, michael@0: struct SRBRoot *bundle, struct SResource *res, michael@0: UErrorCode *status) { michael@0: uint8_t paddingSize; michael@0: michael@0: if (U_FAILURE(*status) || res == NULL) { michael@0: return; michael@0: } michael@0: if (res->fWritten) { michael@0: assert(res->fRes != RES_BOGUS); michael@0: return; michael@0: } michael@0: switch (res->fType) { michael@0: case URES_STRING: michael@0: string_write (mem, byteOffset, bundle, res, status); michael@0: break; michael@0: case URES_ALIAS: michael@0: alias_write (mem, byteOffset, bundle, res, status); michael@0: break; michael@0: case URES_INT_VECTOR: michael@0: intvector_write (mem, byteOffset, bundle, res, status); michael@0: break; michael@0: case URES_BINARY: michael@0: bin_write (mem, byteOffset, bundle, res, status); michael@0: break; michael@0: case URES_INT: michael@0: break; /* fRes was set by int_open() */ michael@0: case URES_ARRAY: michael@0: array_write (mem, byteOffset, bundle, res, status); michael@0: break; michael@0: case URES_TABLE: michael@0: table_write (mem, byteOffset, bundle, res, status); michael@0: break; michael@0: default: michael@0: *status = U_INTERNAL_PROGRAM_ERROR; michael@0: break; michael@0: } michael@0: paddingSize = calcPadding(*byteOffset); michael@0: if (paddingSize > 0) { michael@0: udata_writePadding(mem, paddingSize); michael@0: *byteOffset += paddingSize; michael@0: } michael@0: res->fWritten = TRUE; michael@0: } michael@0: michael@0: void bundle_write(struct SRBRoot *bundle, michael@0: const char *outputDir, const char *outputPkg, michael@0: char *writtenFilename, int writtenFilenameLen, michael@0: UErrorCode *status) { michael@0: UNewDataMemory *mem = NULL; michael@0: uint32_t byteOffset = 0; michael@0: uint32_t top, size; michael@0: char dataName[1024]; michael@0: int32_t indexes[URES_INDEX_TOP]; michael@0: michael@0: bundle_compactKeys(bundle, status); michael@0: /* michael@0: * Add padding bytes to fKeys so that fKeysTop is 4-aligned. michael@0: * Safe because the capacity is a multiple of 4. michael@0: */ michael@0: while (bundle->fKeysTop & 3) { michael@0: bundle->fKeys[bundle->fKeysTop++] = (char)0xaa; michael@0: } michael@0: /* michael@0: * In URES_TABLE, use all local key offsets that fit into 16 bits, michael@0: * and use the remaining 16-bit offsets for pool key offsets michael@0: * if there are any. michael@0: * If there are no local keys, then use the whole 16-bit space michael@0: * for pool key offsets. michael@0: * Note: This cannot be changed without changing the major formatVersion. michael@0: */ michael@0: if (bundle->fKeysBottom < bundle->fKeysTop) { michael@0: if (bundle->fKeysTop <= 0x10000) { michael@0: bundle->fLocalKeyLimit = bundle->fKeysTop; michael@0: } else { michael@0: bundle->fLocalKeyLimit = 0x10000; michael@0: } michael@0: } else { michael@0: bundle->fLocalKeyLimit = 0; michael@0: } michael@0: michael@0: bundle_compactStrings(bundle, status); michael@0: res_write16(bundle, bundle->fRoot, status); michael@0: if (bundle->f16BitUnitsLength & 1) { michael@0: bundle->f16BitUnits[bundle->f16BitUnitsLength++] = 0xaaaa; /* pad to multiple of 4 bytes */ michael@0: } michael@0: /* all keys have been mapped */ michael@0: uprv_free(bundle->fKeyMap); michael@0: bundle->fKeyMap = NULL; michael@0: michael@0: byteOffset = bundle->fKeysTop + bundle->f16BitUnitsLength * 2; michael@0: res_preWrite(&byteOffset, bundle, bundle->fRoot, status); michael@0: michael@0: /* total size including the root item */ michael@0: top = byteOffset; michael@0: michael@0: if (U_FAILURE(*status)) { michael@0: return; michael@0: } michael@0: michael@0: if (writtenFilename && writtenFilenameLen) { michael@0: *writtenFilename = 0; michael@0: } michael@0: michael@0: if (writtenFilename) { michael@0: int32_t off = 0, len = 0; michael@0: if (outputDir) { michael@0: len = (int32_t)uprv_strlen(outputDir); michael@0: if (len > writtenFilenameLen) { michael@0: len = writtenFilenameLen; michael@0: } michael@0: uprv_strncpy(writtenFilename, outputDir, len); michael@0: } michael@0: if (writtenFilenameLen -= len) { michael@0: off += len; michael@0: writtenFilename[off] = U_FILE_SEP_CHAR; michael@0: if (--writtenFilenameLen) { michael@0: ++off; michael@0: if(outputPkg != NULL) michael@0: { michael@0: uprv_strcpy(writtenFilename+off, outputPkg); michael@0: off += (int32_t)uprv_strlen(outputPkg); michael@0: writtenFilename[off] = '_'; michael@0: ++off; michael@0: } michael@0: michael@0: len = (int32_t)uprv_strlen(bundle->fLocale); michael@0: if (len > writtenFilenameLen) { michael@0: len = writtenFilenameLen; michael@0: } michael@0: uprv_strncpy(writtenFilename + off, bundle->fLocale, len); michael@0: if (writtenFilenameLen -= len) { michael@0: off += len; michael@0: len = 5; michael@0: if (len > writtenFilenameLen) { michael@0: len = writtenFilenameLen; michael@0: } michael@0: uprv_strncpy(writtenFilename + off, ".res", len); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: if(outputPkg) michael@0: { michael@0: uprv_strcpy(dataName, outputPkg); michael@0: uprv_strcat(dataName, "_"); michael@0: uprv_strcat(dataName, bundle->fLocale); michael@0: } michael@0: else michael@0: { michael@0: uprv_strcpy(dataName, bundle->fLocale); michael@0: } michael@0: michael@0: uprv_memcpy(dataInfo.formatVersion, gFormatVersions + gFormatVersion, sizeof(UVersionInfo)); michael@0: michael@0: mem = udata_create(outputDir, "res", dataName, &dataInfo, (gIncludeCopyright==TRUE)? U_COPYRIGHT_STRING:NULL, status); michael@0: if(U_FAILURE(*status)){ michael@0: return; michael@0: } michael@0: michael@0: /* write the root item */ michael@0: udata_write32(mem, bundle->fRoot->fRes); michael@0: michael@0: /* michael@0: * formatVersion 1.1 (ICU 2.8): michael@0: * write int32_t indexes[] after root and before the strings michael@0: * to make it easier to parse resource bundles in icuswap or from Java etc. michael@0: */ michael@0: uprv_memset(indexes, 0, sizeof(indexes)); michael@0: indexes[URES_INDEX_LENGTH]= bundle->fIndexLength; michael@0: indexes[URES_INDEX_KEYS_TOP]= bundle->fKeysTop>>2; michael@0: indexes[URES_INDEX_RESOURCES_TOP]= (int32_t)(top>>2); michael@0: indexes[URES_INDEX_BUNDLE_TOP]= indexes[URES_INDEX_RESOURCES_TOP]; michael@0: indexes[URES_INDEX_MAX_TABLE_LENGTH]= bundle->fMaxTableLength; michael@0: michael@0: /* michael@0: * formatVersion 1.2 (ICU 3.6): michael@0: * write indexes[URES_INDEX_ATTRIBUTES] with URES_ATT_NO_FALLBACK set or not set michael@0: * the memset() above initialized all indexes[] to 0 michael@0: */ michael@0: if (bundle->noFallback) { michael@0: indexes[URES_INDEX_ATTRIBUTES]=URES_ATT_NO_FALLBACK; michael@0: } michael@0: /* michael@0: * formatVersion 2.0 (ICU 4.4): michael@0: * more compact string value storage, optional pool bundle michael@0: */ michael@0: if (URES_INDEX_16BIT_TOP < bundle->fIndexLength) { michael@0: indexes[URES_INDEX_16BIT_TOP] = (bundle->fKeysTop>>2) + (bundle->f16BitUnitsLength>>1); michael@0: } michael@0: if (URES_INDEX_POOL_CHECKSUM < bundle->fIndexLength) { michael@0: if (bundle->fIsPoolBundle) { michael@0: indexes[URES_INDEX_ATTRIBUTES] |= URES_ATT_IS_POOL_BUNDLE | URES_ATT_NO_FALLBACK; michael@0: indexes[URES_INDEX_POOL_CHECKSUM] = michael@0: (int32_t)computeCRC((char *)(bundle->fKeys + bundle->fKeysBottom), michael@0: (uint32_t)(bundle->fKeysTop - bundle->fKeysBottom), michael@0: 0); michael@0: } else if (gUsePoolBundle) { michael@0: indexes[URES_INDEX_ATTRIBUTES] |= URES_ATT_USES_POOL_BUNDLE; michael@0: indexes[URES_INDEX_POOL_CHECKSUM] = bundle->fPoolChecksum; michael@0: } michael@0: } michael@0: michael@0: /* write the indexes[] */ michael@0: udata_writeBlock(mem, indexes, bundle->fIndexLength*4); michael@0: michael@0: /* write the table key strings */ michael@0: udata_writeBlock(mem, bundle->fKeys+bundle->fKeysBottom, michael@0: bundle->fKeysTop-bundle->fKeysBottom); michael@0: michael@0: /* write the v2 UTF-16 strings, URES_TABLE16 and URES_ARRAY16 */ michael@0: udata_writeBlock(mem, bundle->f16BitUnits, bundle->f16BitUnitsLength*2); michael@0: michael@0: /* write all of the bundle contents: the root item and its children */ michael@0: byteOffset = bundle->fKeysTop + bundle->f16BitUnitsLength * 2; michael@0: res_write(mem, &byteOffset, bundle, bundle->fRoot, status); michael@0: assert(byteOffset == top); michael@0: michael@0: size = udata_finish(mem, status); michael@0: if(top != size) { michael@0: fprintf(stderr, "genrb error: wrote %u bytes but counted %u\n", michael@0: (int)size, (int)top); michael@0: *status = U_INTERNAL_PROGRAM_ERROR; michael@0: } michael@0: } michael@0: michael@0: /* Opening Functions */ michael@0: michael@0: /* gcc 4.2 complained "no previous prototype for res_open" without this prototype... */ michael@0: struct SResource* res_open(struct SRBRoot *bundle, const char *tag, michael@0: const struct UString* comment, UErrorCode* status); michael@0: michael@0: struct SResource* res_open(struct SRBRoot *bundle, const char *tag, michael@0: const struct UString* comment, UErrorCode* status){ michael@0: struct SResource *res; michael@0: int32_t key = bundle_addtag(bundle, tag, status); michael@0: if (U_FAILURE(*status)) { michael@0: return NULL; michael@0: } michael@0: michael@0: res = (struct SResource *) uprv_malloc(sizeof(struct SResource)); michael@0: if (res == NULL) { michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: return NULL; michael@0: } michael@0: uprv_memset(res, 0, sizeof(struct SResource)); michael@0: res->fKey = key; michael@0: res->fRes = RES_BOGUS; michael@0: michael@0: ustr_init(&res->fComment); michael@0: if(comment != NULL){ michael@0: ustr_cpy(&res->fComment, comment, status); michael@0: if (U_FAILURE(*status)) { michael@0: res_close(res); michael@0: return NULL; michael@0: } michael@0: } michael@0: return res; michael@0: } michael@0: michael@0: struct SResource* res_none() { michael@0: return (struct SResource*)&kNoResource; michael@0: } michael@0: michael@0: struct SResource* table_open(struct SRBRoot *bundle, const char *tag, const struct UString* comment, UErrorCode *status) { michael@0: struct SResource *res = res_open(bundle, tag, comment, status); michael@0: if (U_FAILURE(*status)) { michael@0: return NULL; michael@0: } michael@0: res->fType = URES_TABLE; michael@0: res->u.fTable.fRoot = bundle; michael@0: return res; michael@0: } michael@0: michael@0: struct SResource* array_open(struct SRBRoot *bundle, const char *tag, const struct UString* comment, UErrorCode *status) { michael@0: struct SResource *res = res_open(bundle, tag, comment, status); michael@0: if (U_FAILURE(*status)) { michael@0: return NULL; michael@0: } michael@0: res->fType = URES_ARRAY; michael@0: return res; michael@0: } michael@0: michael@0: static int32_t U_CALLCONV michael@0: string_hash(const UElement key) { michael@0: const struct SResource *res = (struct SResource *)key.pointer; michael@0: return ustr_hashUCharsN(res->u.fString.fChars, res->u.fString.fLength); michael@0: } michael@0: michael@0: static UBool U_CALLCONV michael@0: string_comp(const UElement key1, const UElement key2) { michael@0: const struct SResource *res1 = (struct SResource *)key1.pointer; michael@0: const struct SResource *res2 = (struct SResource *)key2.pointer; michael@0: return 0 == u_strCompare(res1->u.fString.fChars, res1->u.fString.fLength, michael@0: res2->u.fString.fChars, res2->u.fString.fLength, michael@0: FALSE); michael@0: } michael@0: michael@0: struct SResource *string_open(struct SRBRoot *bundle, const char *tag, const UChar *value, int32_t len, const struct UString* comment, UErrorCode *status) { michael@0: struct SResource *res = res_open(bundle, tag, comment, status); michael@0: if (U_FAILURE(*status)) { michael@0: return NULL; michael@0: } michael@0: res->fType = URES_STRING; michael@0: michael@0: if (len == 0 && gFormatVersion > 1) { michael@0: res->u.fString.fChars = &gEmptyString; michael@0: res->fRes = 0; michael@0: res->fWritten = TRUE; michael@0: return res; michael@0: } michael@0: michael@0: res->u.fString.fLength = len; michael@0: michael@0: if (gFormatVersion > 1) { michael@0: /* check for duplicates */ michael@0: res->u.fString.fChars = (UChar *)value; michael@0: if (bundle->fStringSet == NULL) { michael@0: UErrorCode localStatus = U_ZERO_ERROR; /* if failure: just don't detect dups */ michael@0: bundle->fStringSet = uhash_open(string_hash, string_comp, string_comp, &localStatus); michael@0: } else { michael@0: res->u.fString.fSame = uhash_get(bundle->fStringSet, res); michael@0: } michael@0: } michael@0: if (res->u.fString.fSame == NULL) { michael@0: /* this is a new string */ michael@0: res->u.fString.fChars = (UChar *) uprv_malloc(sizeof(UChar) * (len + 1)); michael@0: michael@0: if (res->u.fString.fChars == NULL) { michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: uprv_free(res); michael@0: return NULL; michael@0: } michael@0: michael@0: uprv_memcpy(res->u.fString.fChars, value, sizeof(UChar) * len); michael@0: res->u.fString.fChars[len] = 0; michael@0: if (bundle->fStringSet != NULL) { michael@0: /* put it into the set for finding duplicates */ michael@0: uhash_put(bundle->fStringSet, res, res, status); michael@0: } michael@0: michael@0: if (bundle->fStringsForm != STRINGS_UTF16_V1) { michael@0: if (len <= MAX_IMPLICIT_STRING_LENGTH && !U16_IS_TRAIL(value[0]) && len == u_strlen(value)) { michael@0: /* michael@0: * This string will be stored without an explicit length. michael@0: * Runtime will detect !U16_IS_TRAIL(value[0]) and call u_strlen(). michael@0: */ michael@0: res->u.fString.fNumCharsForLength = 0; michael@0: } else if (len <= 0x3ee) { michael@0: res->u.fString.fNumCharsForLength = 1; michael@0: } else if (len <= 0xfffff) { michael@0: res->u.fString.fNumCharsForLength = 2; michael@0: } else { michael@0: res->u.fString.fNumCharsForLength = 3; michael@0: } michael@0: bundle->f16BitUnitsLength += res->u.fString.fNumCharsForLength + len + 1; /* +1 for the NUL */ michael@0: } michael@0: } else { michael@0: /* this is a duplicate of fSame */ michael@0: struct SResource *same = res->u.fString.fSame; michael@0: res->u.fString.fChars = same->u.fString.fChars; michael@0: } michael@0: return res; michael@0: } michael@0: michael@0: /* TODO: make alias_open and string_open use the same code */ michael@0: struct SResource *alias_open(struct SRBRoot *bundle, const char *tag, UChar *value, int32_t len, const struct UString* comment, UErrorCode *status) { michael@0: struct SResource *res = res_open(bundle, tag, comment, status); michael@0: if (U_FAILURE(*status)) { michael@0: return NULL; michael@0: } michael@0: res->fType = URES_ALIAS; michael@0: if (len == 0 && gFormatVersion > 1) { michael@0: res->u.fString.fChars = &gEmptyString; michael@0: res->fRes = URES_MAKE_EMPTY_RESOURCE(URES_ALIAS); michael@0: res->fWritten = TRUE; michael@0: return res; michael@0: } michael@0: michael@0: res->u.fString.fLength = len; michael@0: res->u.fString.fChars = (UChar *) uprv_malloc(sizeof(UChar) * (len + 1)); michael@0: if (res->u.fString.fChars == NULL) { michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: uprv_free(res); michael@0: return NULL; michael@0: } michael@0: uprv_memcpy(res->u.fString.fChars, value, sizeof(UChar) * (len + 1)); michael@0: return res; michael@0: } michael@0: michael@0: michael@0: struct SResource* intvector_open(struct SRBRoot *bundle, const char *tag, const struct UString* comment, UErrorCode *status) { michael@0: struct SResource *res = res_open(bundle, tag, comment, status); michael@0: if (U_FAILURE(*status)) { michael@0: return NULL; michael@0: } michael@0: res->fType = URES_INT_VECTOR; michael@0: michael@0: res->u.fIntVector.fCount = 0; michael@0: res->u.fIntVector.fArray = (uint32_t *) uprv_malloc(sizeof(uint32_t) * RESLIST_MAX_INT_VECTOR); michael@0: if (res->u.fIntVector.fArray == NULL) { michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: uprv_free(res); michael@0: return NULL; michael@0: } michael@0: return res; michael@0: } michael@0: michael@0: struct SResource *int_open(struct SRBRoot *bundle, const char *tag, int32_t value, const struct UString* comment, UErrorCode *status) { michael@0: struct SResource *res = res_open(bundle, tag, comment, status); michael@0: if (U_FAILURE(*status)) { michael@0: return NULL; michael@0: } michael@0: res->fType = URES_INT; michael@0: res->u.fIntValue.fValue = value; michael@0: res->fRes = URES_MAKE_RESOURCE(URES_INT, value & 0x0FFFFFFF); michael@0: res->fWritten = TRUE; michael@0: return res; michael@0: } michael@0: michael@0: 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: struct SResource *res = res_open(bundle, tag, comment, status); michael@0: if (U_FAILURE(*status)) { michael@0: return NULL; michael@0: } michael@0: res->fType = URES_BINARY; michael@0: michael@0: res->u.fBinaryValue.fLength = length; michael@0: res->u.fBinaryValue.fFileName = NULL; michael@0: if(fileName!=NULL && uprv_strcmp(fileName, "") !=0){ michael@0: res->u.fBinaryValue.fFileName = (char*) uprv_malloc(sizeof(char) * (uprv_strlen(fileName)+1)); michael@0: uprv_strcpy(res->u.fBinaryValue.fFileName,fileName); michael@0: } michael@0: if (length > 0) { michael@0: res->u.fBinaryValue.fData = (uint8_t *) uprv_malloc(sizeof(uint8_t) * length); michael@0: michael@0: if (res->u.fBinaryValue.fData == NULL) { michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: uprv_free(res); michael@0: return NULL; michael@0: } michael@0: michael@0: uprv_memcpy(res->u.fBinaryValue.fData, data, length); michael@0: } michael@0: else { michael@0: res->u.fBinaryValue.fData = NULL; michael@0: if (gFormatVersion > 1) { michael@0: res->fRes = URES_MAKE_EMPTY_RESOURCE(URES_BINARY); michael@0: res->fWritten = TRUE; michael@0: } michael@0: } michael@0: michael@0: return res; michael@0: } michael@0: michael@0: struct SRBRoot *bundle_open(const struct UString* comment, UBool isPoolBundle, UErrorCode *status) { michael@0: struct SRBRoot *bundle; michael@0: michael@0: if (U_FAILURE(*status)) { michael@0: return NULL; michael@0: } michael@0: michael@0: bundle = (struct SRBRoot *) uprv_malloc(sizeof(struct SRBRoot)); michael@0: if (bundle == NULL) { michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: return 0; michael@0: } michael@0: uprv_memset(bundle, 0, sizeof(struct SRBRoot)); michael@0: michael@0: bundle->fKeys = (char *) uprv_malloc(sizeof(char) * KEY_SPACE_SIZE); michael@0: bundle->fRoot = table_open(bundle, NULL, comment, status); michael@0: if (bundle->fKeys == NULL || bundle->fRoot == NULL || U_FAILURE(*status)) { michael@0: if (U_SUCCESS(*status)) { michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: } michael@0: bundle_close(bundle, status); michael@0: return NULL; michael@0: } michael@0: michael@0: bundle->fLocale = NULL; michael@0: bundle->fKeysCapacity = KEY_SPACE_SIZE; michael@0: /* formatVersion 1.1: start fKeysTop after the root item and indexes[] */ michael@0: bundle->fIsPoolBundle = isPoolBundle; michael@0: if (gUsePoolBundle || isPoolBundle) { michael@0: bundle->fIndexLength = URES_INDEX_POOL_CHECKSUM + 1; michael@0: } else if (gFormatVersion >= 2) { michael@0: bundle->fIndexLength = URES_INDEX_16BIT_TOP + 1; michael@0: } else /* formatVersion 1 */ { michael@0: bundle->fIndexLength = URES_INDEX_ATTRIBUTES + 1; michael@0: } michael@0: bundle->fKeysBottom = (1 /* root */ + bundle->fIndexLength) * 4; michael@0: uprv_memset(bundle->fKeys, 0, bundle->fKeysBottom); michael@0: bundle->fKeysTop = bundle->fKeysBottom; michael@0: michael@0: if (gFormatVersion == 1) { michael@0: bundle->fStringsForm = STRINGS_UTF16_V1; michael@0: } else { michael@0: bundle->fStringsForm = STRINGS_UTF16_V2; michael@0: } michael@0: michael@0: return bundle; michael@0: } michael@0: michael@0: /* Closing Functions */ michael@0: static void table_close(struct SResource *table) { michael@0: struct SResource *current = NULL; michael@0: struct SResource *prev = NULL; michael@0: michael@0: current = table->u.fTable.fFirst; michael@0: michael@0: while (current != NULL) { michael@0: prev = current; michael@0: current = current->fNext; michael@0: michael@0: res_close(prev); michael@0: } michael@0: michael@0: table->u.fTable.fFirst = NULL; michael@0: } michael@0: michael@0: static void array_close(struct SResource *array) { michael@0: struct SResource *current = NULL; michael@0: struct SResource *prev = NULL; michael@0: michael@0: if(array==NULL){ michael@0: return; michael@0: } michael@0: current = array->u.fArray.fFirst; michael@0: michael@0: while (current != NULL) { michael@0: prev = current; michael@0: current = current->fNext; michael@0: michael@0: res_close(prev); michael@0: } michael@0: array->u.fArray.fFirst = NULL; michael@0: } michael@0: michael@0: static void string_close(struct SResource *string) { michael@0: if (string->u.fString.fChars != NULL && michael@0: string->u.fString.fChars != &gEmptyString && michael@0: string->u.fString.fSame == NULL michael@0: ) { michael@0: uprv_free(string->u.fString.fChars); michael@0: string->u.fString.fChars =NULL; michael@0: } michael@0: } michael@0: michael@0: static void alias_close(struct SResource *alias) { michael@0: if (alias->u.fString.fChars != NULL) { michael@0: uprv_free(alias->u.fString.fChars); michael@0: alias->u.fString.fChars =NULL; michael@0: } michael@0: } michael@0: michael@0: static void intvector_close(struct SResource *intvector) { michael@0: if (intvector->u.fIntVector.fArray != NULL) { michael@0: uprv_free(intvector->u.fIntVector.fArray); michael@0: intvector->u.fIntVector.fArray =NULL; michael@0: } michael@0: } michael@0: michael@0: static void int_close(struct SResource *intres) { michael@0: /* Intentionally left blank */ michael@0: } michael@0: michael@0: static void bin_close(struct SResource *binres) { michael@0: if (binres->u.fBinaryValue.fData != NULL) { michael@0: uprv_free(binres->u.fBinaryValue.fData); michael@0: binres->u.fBinaryValue.fData = NULL; michael@0: } michael@0: if (binres->u.fBinaryValue.fFileName != NULL) { michael@0: uprv_free(binres->u.fBinaryValue.fFileName); michael@0: binres->u.fBinaryValue.fFileName = NULL; michael@0: } michael@0: } michael@0: michael@0: void res_close(struct SResource *res) { michael@0: if (res != NULL) { michael@0: switch(res->fType) { michael@0: case URES_STRING: michael@0: string_close(res); michael@0: break; michael@0: case URES_ALIAS: michael@0: alias_close(res); michael@0: break; michael@0: case URES_INT_VECTOR: michael@0: intvector_close(res); michael@0: break; michael@0: case URES_BINARY: michael@0: bin_close(res); michael@0: break; michael@0: case URES_INT: michael@0: int_close(res); michael@0: break; michael@0: case URES_ARRAY: michael@0: array_close(res); michael@0: break; michael@0: case URES_TABLE: michael@0: table_close(res); michael@0: break; michael@0: default: michael@0: /* Shouldn't happen */ michael@0: break; michael@0: } michael@0: michael@0: ustr_deinit(&res->fComment); michael@0: uprv_free(res); michael@0: } michael@0: } michael@0: michael@0: void bundle_close(struct SRBRoot *bundle, UErrorCode *status) { michael@0: res_close(bundle->fRoot); michael@0: uprv_free(bundle->fLocale); michael@0: uprv_free(bundle->fKeys); michael@0: uprv_free(bundle->fKeyMap); michael@0: uhash_close(bundle->fStringSet); michael@0: uprv_free(bundle->f16BitUnits); michael@0: uprv_free(bundle); michael@0: } michael@0: michael@0: void bundle_closeString(struct SRBRoot *bundle, struct SResource *string) { michael@0: if (bundle->fStringSet != NULL) { michael@0: uhash_remove(bundle->fStringSet, string); michael@0: } michael@0: string_close(string); michael@0: } michael@0: michael@0: /* Adding Functions */ michael@0: void table_add(struct SResource *table, struct SResource *res, int linenumber, UErrorCode *status) { michael@0: struct SResource *current = NULL; michael@0: struct SResource *prev = NULL; michael@0: struct SResTable *list; michael@0: const char *resKeyString; michael@0: michael@0: if (U_FAILURE(*status)) { michael@0: return; michael@0: } michael@0: if (res == &kNoResource) { michael@0: return; michael@0: } michael@0: michael@0: /* remember this linenumber to report to the user if there is a duplicate key */ michael@0: res->line = linenumber; michael@0: michael@0: /* here we need to traverse the list */ michael@0: list = &(table->u.fTable); michael@0: ++(list->fCount); michael@0: michael@0: /* is list still empty? */ michael@0: if (list->fFirst == NULL) { michael@0: list->fFirst = res; michael@0: res->fNext = NULL; michael@0: return; michael@0: } michael@0: michael@0: resKeyString = list->fRoot->fKeys + res->fKey; michael@0: michael@0: current = list->fFirst; michael@0: michael@0: while (current != NULL) { michael@0: const char *currentKeyString = list->fRoot->fKeys + current->fKey; michael@0: int diff; michael@0: /* michael@0: * formatVersion 1: compare key strings in native-charset order michael@0: * formatVersion 2 and up: compare key strings in ASCII order michael@0: */ michael@0: if (gFormatVersion == 1 || U_CHARSET_FAMILY == U_ASCII_FAMILY) { michael@0: diff = uprv_strcmp(currentKeyString, resKeyString); michael@0: } else { michael@0: diff = uprv_compareInvCharsAsAscii(currentKeyString, resKeyString); michael@0: } michael@0: if (diff < 0) { michael@0: prev = current; michael@0: current = current->fNext; michael@0: } else if (diff > 0) { michael@0: /* we're either in front of list, or in middle */ michael@0: if (prev == NULL) { michael@0: /* front of the list */ michael@0: list->fFirst = res; michael@0: } else { michael@0: /* middle of the list */ michael@0: prev->fNext = res; michael@0: } michael@0: michael@0: res->fNext = current; michael@0: return; michael@0: } else { michael@0: /* Key already exists! ERROR! */ michael@0: error(linenumber, "duplicate key '%s' in table, first appeared at line %d", currentKeyString, current->line); michael@0: *status = U_UNSUPPORTED_ERROR; michael@0: return; michael@0: } michael@0: } michael@0: michael@0: /* end of list */ michael@0: prev->fNext = res; michael@0: res->fNext = NULL; michael@0: } michael@0: michael@0: void array_add(struct SResource *array, struct SResource *res, UErrorCode *status) { michael@0: if (U_FAILURE(*status)) { michael@0: return; michael@0: } michael@0: michael@0: if (array->u.fArray.fFirst == NULL) { michael@0: array->u.fArray.fFirst = res; michael@0: array->u.fArray.fLast = res; michael@0: } else { michael@0: array->u.fArray.fLast->fNext = res; michael@0: array->u.fArray.fLast = res; michael@0: } michael@0: michael@0: (array->u.fArray.fCount)++; michael@0: } michael@0: michael@0: void intvector_add(struct SResource *intvector, int32_t value, UErrorCode *status) { michael@0: if (U_FAILURE(*status)) { michael@0: return; michael@0: } michael@0: michael@0: *(intvector->u.fIntVector.fArray + intvector->u.fIntVector.fCount) = value; michael@0: intvector->u.fIntVector.fCount++; michael@0: } michael@0: michael@0: /* Misc Functions */ michael@0: michael@0: void bundle_setlocale(struct SRBRoot *bundle, UChar *locale, UErrorCode *status) { michael@0: michael@0: if(U_FAILURE(*status)) { michael@0: return; michael@0: } michael@0: michael@0: if (bundle->fLocale!=NULL) { michael@0: uprv_free(bundle->fLocale); michael@0: } michael@0: michael@0: bundle->fLocale= (char*) uprv_malloc(sizeof(char) * (u_strlen(locale)+1)); michael@0: michael@0: if(bundle->fLocale == NULL) { michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: return; michael@0: } michael@0: michael@0: /*u_strcpy(bundle->fLocale, locale);*/ michael@0: u_UCharsToChars(locale, bundle->fLocale, u_strlen(locale)+1); michael@0: michael@0: } michael@0: michael@0: static const char * michael@0: getKeyString(const struct SRBRoot *bundle, int32_t key) { michael@0: if (key < 0) { michael@0: return bundle->fPoolBundleKeys + (key & 0x7fffffff); michael@0: } else { michael@0: return bundle->fKeys + key; michael@0: } michael@0: } michael@0: michael@0: const char * michael@0: res_getKeyString(const struct SRBRoot *bundle, const struct SResource *res, char temp[8]) { michael@0: if (res->fKey == -1) { michael@0: return NULL; michael@0: } michael@0: return getKeyString(bundle, res->fKey); michael@0: } michael@0: michael@0: const char * michael@0: bundle_getKeyBytes(struct SRBRoot *bundle, int32_t *pLength) { michael@0: *pLength = bundle->fKeysTop - bundle->fKeysBottom; michael@0: return bundle->fKeys + bundle->fKeysBottom; michael@0: } michael@0: michael@0: int32_t michael@0: bundle_addKeyBytes(struct SRBRoot *bundle, const char *keyBytes, int32_t length, UErrorCode *status) { michael@0: int32_t keypos; michael@0: michael@0: if (U_FAILURE(*status)) { michael@0: return -1; michael@0: } michael@0: if (length < 0 || (keyBytes == NULL && length != 0)) { michael@0: *status = U_ILLEGAL_ARGUMENT_ERROR; michael@0: return -1; michael@0: } michael@0: if (length == 0) { michael@0: return bundle->fKeysTop; michael@0: } michael@0: michael@0: keypos = bundle->fKeysTop; michael@0: bundle->fKeysTop += length; michael@0: if (bundle->fKeysTop >= bundle->fKeysCapacity) { michael@0: /* overflow - resize the keys buffer */ michael@0: bundle->fKeysCapacity += KEY_SPACE_SIZE; michael@0: bundle->fKeys = uprv_realloc(bundle->fKeys, bundle->fKeysCapacity); michael@0: if(bundle->fKeys == NULL) { michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: return -1; michael@0: } michael@0: } michael@0: michael@0: uprv_memcpy(bundle->fKeys + keypos, keyBytes, length); michael@0: michael@0: return keypos; michael@0: } michael@0: michael@0: int32_t michael@0: bundle_addtag(struct SRBRoot *bundle, const char *tag, UErrorCode *status) { michael@0: int32_t keypos; michael@0: michael@0: if (U_FAILURE(*status)) { michael@0: return -1; michael@0: } michael@0: michael@0: if (tag == NULL) { michael@0: /* no error: the root table and array items have no keys */ michael@0: return -1; michael@0: } michael@0: michael@0: keypos = bundle_addKeyBytes(bundle, tag, (int32_t)(uprv_strlen(tag) + 1), status); michael@0: if (U_SUCCESS(*status)) { michael@0: ++bundle->fKeysCount; michael@0: } michael@0: return keypos; michael@0: } michael@0: michael@0: static int32_t michael@0: compareInt32(int32_t lPos, int32_t rPos) { michael@0: /* michael@0: * Compare possibly-negative key offsets. Don't just return lPos - rPos michael@0: * because that is prone to negative-integer underflows. michael@0: */ michael@0: if (lPos < rPos) { michael@0: return -1; michael@0: } else if (lPos > rPos) { michael@0: return 1; michael@0: } else { michael@0: return 0; michael@0: } michael@0: } michael@0: michael@0: static int32_t U_CALLCONV michael@0: compareKeySuffixes(const void *context, const void *l, const void *r) { michael@0: const struct SRBRoot *bundle=(const struct SRBRoot *)context; michael@0: int32_t lPos = ((const KeyMapEntry *)l)->oldpos; michael@0: int32_t rPos = ((const KeyMapEntry *)r)->oldpos; michael@0: const char *lStart = getKeyString(bundle, lPos); michael@0: const char *lLimit = lStart; michael@0: const char *rStart = getKeyString(bundle, rPos); michael@0: const char *rLimit = rStart; michael@0: int32_t diff; michael@0: while (*lLimit != 0) { ++lLimit; } michael@0: while (*rLimit != 0) { ++rLimit; } michael@0: /* compare keys in reverse character order */ michael@0: while (lStart < lLimit && rStart < rLimit) { michael@0: diff = (int32_t)(uint8_t)*--lLimit - (int32_t)(uint8_t)*--rLimit; michael@0: if (diff != 0) { michael@0: return diff; michael@0: } michael@0: } michael@0: /* sort equal suffixes by descending key length */ michael@0: diff = (int32_t)(rLimit - rStart) - (int32_t)(lLimit - lStart); michael@0: if (diff != 0) { michael@0: return diff; michael@0: } michael@0: /* Sort pool bundle keys first (negative oldpos), and otherwise keys in parsing order. */ michael@0: return compareInt32(lPos, rPos); michael@0: } michael@0: michael@0: static int32_t U_CALLCONV michael@0: compareKeyNewpos(const void *context, const void *l, const void *r) { michael@0: return compareInt32(((const KeyMapEntry *)l)->newpos, ((const KeyMapEntry *)r)->newpos); michael@0: } michael@0: michael@0: static int32_t U_CALLCONV michael@0: compareKeyOldpos(const void *context, const void *l, const void *r) { michael@0: return compareInt32(((const KeyMapEntry *)l)->oldpos, ((const KeyMapEntry *)r)->oldpos); michael@0: } michael@0: michael@0: void michael@0: bundle_compactKeys(struct SRBRoot *bundle, UErrorCode *status) { michael@0: KeyMapEntry *map; michael@0: char *keys; michael@0: int32_t i; michael@0: int32_t keysCount = bundle->fPoolBundleKeysCount + bundle->fKeysCount; michael@0: if (U_FAILURE(*status) || bundle->fKeysCount == 0 || bundle->fKeyMap != NULL) { michael@0: return; michael@0: } michael@0: map = (KeyMapEntry *)uprv_malloc(keysCount * sizeof(KeyMapEntry)); michael@0: if (map == NULL) { michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: return; michael@0: } michael@0: keys = (char *)bundle->fPoolBundleKeys; michael@0: for (i = 0; i < bundle->fPoolBundleKeysCount; ++i) { michael@0: map[i].oldpos = michael@0: (int32_t)(keys - bundle->fPoolBundleKeys) | 0x80000000; /* negative oldpos */ michael@0: map[i].newpos = 0; michael@0: while (*keys != 0) { ++keys; } /* skip the key */ michael@0: ++keys; /* skip the NUL */ michael@0: } michael@0: keys = bundle->fKeys + bundle->fKeysBottom; michael@0: for (; i < keysCount; ++i) { michael@0: map[i].oldpos = (int32_t)(keys - bundle->fKeys); michael@0: map[i].newpos = 0; michael@0: while (*keys != 0) { ++keys; } /* skip the key */ michael@0: ++keys; /* skip the NUL */ michael@0: } michael@0: /* Sort the keys so that each one is immediately followed by all of its suffixes. */ michael@0: uprv_sortArray(map, keysCount, (int32_t)sizeof(KeyMapEntry), michael@0: compareKeySuffixes, bundle, FALSE, status); michael@0: /* michael@0: * Make suffixes point into earlier, longer strings that contain them michael@0: * and mark the old, now unused suffix bytes as deleted. michael@0: */ michael@0: if (U_SUCCESS(*status)) { michael@0: keys = bundle->fKeys; michael@0: for (i = 0; i < keysCount;) { michael@0: /* michael@0: * This key is not a suffix of the previous one; michael@0: * keep this one and delete the following ones that are michael@0: * suffixes of this one. michael@0: */ michael@0: const char *key; michael@0: const char *keyLimit; michael@0: int32_t j = i + 1; michael@0: map[i].newpos = map[i].oldpos; michael@0: if (j < keysCount && map[j].oldpos < 0) { michael@0: /* Key string from the pool bundle, do not delete. */ michael@0: i = j; michael@0: continue; michael@0: } michael@0: key = getKeyString(bundle, map[i].oldpos); michael@0: for (keyLimit = key; *keyLimit != 0; ++keyLimit) {} michael@0: for (; j < keysCount && map[j].oldpos >= 0; ++j) { michael@0: const char *k; michael@0: char *suffix; michael@0: const char *suffixLimit; michael@0: int32_t offset; michael@0: suffix = keys + map[j].oldpos; michael@0: for (suffixLimit = suffix; *suffixLimit != 0; ++suffixLimit) {} michael@0: offset = (int32_t)(keyLimit - key) - (suffixLimit - suffix); michael@0: if (offset < 0) { michael@0: break; /* suffix cannot be longer than the original */ michael@0: } michael@0: /* Is it a suffix of the earlier, longer key? */ michael@0: for (k = keyLimit; suffix < suffixLimit && *--k == *--suffixLimit;) {} michael@0: if (suffix == suffixLimit && *k == *suffixLimit) { michael@0: map[j].newpos = map[i].oldpos + offset; /* yes, point to the earlier key */ michael@0: /* mark the suffix as deleted */ michael@0: while (*suffix != 0) { *suffix++ = 1; } michael@0: *suffix = 1; michael@0: } else { michael@0: break; /* not a suffix, restart from here */ michael@0: } michael@0: } michael@0: i = j; michael@0: } michael@0: /* michael@0: * Re-sort by newpos, then modify the key characters array in-place michael@0: * to squeeze out unused bytes, and readjust the newpos offsets. michael@0: */ michael@0: uprv_sortArray(map, keysCount, (int32_t)sizeof(KeyMapEntry), michael@0: compareKeyNewpos, NULL, FALSE, status); michael@0: if (U_SUCCESS(*status)) { michael@0: int32_t oldpos, newpos, limit; michael@0: oldpos = newpos = bundle->fKeysBottom; michael@0: limit = bundle->fKeysTop; michael@0: /* skip key offsets that point into the pool bundle rather than this new bundle */ michael@0: for (i = 0; i < keysCount && map[i].newpos < 0; ++i) {} michael@0: if (i < keysCount) { michael@0: while (oldpos < limit) { michael@0: if (keys[oldpos] == 1) { michael@0: ++oldpos; /* skip unused bytes */ michael@0: } else { michael@0: /* adjust the new offsets for keys starting here */ michael@0: while (i < keysCount && map[i].newpos == oldpos) { michael@0: map[i++].newpos = newpos; michael@0: } michael@0: /* move the key characters to their new position */ michael@0: keys[newpos++] = keys[oldpos++]; michael@0: } michael@0: } michael@0: assert(i == keysCount); michael@0: } michael@0: bundle->fKeysTop = newpos; michael@0: /* Re-sort once more, by old offsets for binary searching. */ michael@0: uprv_sortArray(map, keysCount, (int32_t)sizeof(KeyMapEntry), michael@0: compareKeyOldpos, NULL, FALSE, status); michael@0: if (U_SUCCESS(*status)) { michael@0: /* key size reduction by limit - newpos */ michael@0: bundle->fKeyMap = map; michael@0: map = NULL; michael@0: } michael@0: } michael@0: } michael@0: uprv_free(map); michael@0: } michael@0: michael@0: static int32_t U_CALLCONV michael@0: compareStringSuffixes(const void *context, const void *l, const void *r) { michael@0: struct SResource *left = *((struct SResource **)l); michael@0: struct SResource *right = *((struct SResource **)r); michael@0: const UChar *lStart = left->u.fString.fChars; michael@0: const UChar *lLimit = lStart + left->u.fString.fLength; michael@0: const UChar *rStart = right->u.fString.fChars; michael@0: const UChar *rLimit = rStart + right->u.fString.fLength; michael@0: int32_t diff; michael@0: /* compare keys in reverse character order */ michael@0: while (lStart < lLimit && rStart < rLimit) { michael@0: diff = (int32_t)*--lLimit - (int32_t)*--rLimit; michael@0: if (diff != 0) { michael@0: return diff; michael@0: } michael@0: } michael@0: /* sort equal suffixes by descending string length */ michael@0: return right->u.fString.fLength - left->u.fString.fLength; michael@0: } michael@0: michael@0: static int32_t U_CALLCONV michael@0: compareStringLengths(const void *context, const void *l, const void *r) { michael@0: struct SResource *left = *((struct SResource **)l); michael@0: struct SResource *right = *((struct SResource **)r); michael@0: int32_t diff; michael@0: /* Make "is suffix of another string" compare greater than a non-suffix. */ michael@0: diff = (int)(left->u.fString.fSame != NULL) - (int)(right->u.fString.fSame != NULL); michael@0: if (diff != 0) { michael@0: return diff; michael@0: } michael@0: /* sort by ascending string length */ michael@0: return left->u.fString.fLength - right->u.fString.fLength; michael@0: } michael@0: michael@0: static int32_t michael@0: string_writeUTF16v2(struct SRBRoot *bundle, struct SResource *res, int32_t utf16Length) { michael@0: int32_t length = res->u.fString.fLength; michael@0: res->fRes = URES_MAKE_RESOURCE(URES_STRING_V2, utf16Length); michael@0: res->fWritten = TRUE; michael@0: switch(res->u.fString.fNumCharsForLength) { michael@0: case 0: michael@0: break; michael@0: case 1: michael@0: bundle->f16BitUnits[utf16Length++] = (uint16_t)(0xdc00 + length); michael@0: break; michael@0: case 2: michael@0: bundle->f16BitUnits[utf16Length] = (uint16_t)(0xdfef + (length >> 16)); michael@0: bundle->f16BitUnits[utf16Length + 1] = (uint16_t)length; michael@0: utf16Length += 2; michael@0: break; michael@0: case 3: michael@0: bundle->f16BitUnits[utf16Length] = 0xdfff; michael@0: bundle->f16BitUnits[utf16Length + 1] = (uint16_t)(length >> 16); michael@0: bundle->f16BitUnits[utf16Length + 2] = (uint16_t)length; michael@0: utf16Length += 3; michael@0: break; michael@0: default: michael@0: break; /* will not occur */ michael@0: } michael@0: u_memcpy(bundle->f16BitUnits + utf16Length, res->u.fString.fChars, length + 1); michael@0: return utf16Length + length + 1; michael@0: } michael@0: michael@0: static void michael@0: bundle_compactStrings(struct SRBRoot *bundle, UErrorCode *status) { michael@0: if (U_FAILURE(*status)) { michael@0: return; michael@0: } michael@0: switch(bundle->fStringsForm) { michael@0: case STRINGS_UTF16_V2: michael@0: if (bundle->f16BitUnitsLength > 0) { michael@0: struct SResource **array; michael@0: int32_t count = uhash_count(bundle->fStringSet); michael@0: int32_t i, pos; michael@0: /* michael@0: * Allocate enough space for the initial NUL and the UTF-16 v2 strings, michael@0: * and some extra for URES_TABLE16 and URES_ARRAY16 values. michael@0: * Round down to an even number. michael@0: */ michael@0: int32_t utf16Length = (bundle->f16BitUnitsLength + 20000) & ~1; michael@0: bundle->f16BitUnits = (UChar *)uprv_malloc(utf16Length * U_SIZEOF_UCHAR); michael@0: array = (struct SResource **)uprv_malloc(count * sizeof(struct SResource **)); michael@0: if (bundle->f16BitUnits == NULL || array == NULL) { michael@0: uprv_free(bundle->f16BitUnits); michael@0: bundle->f16BitUnits = NULL; michael@0: uprv_free(array); michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: return; michael@0: } michael@0: bundle->f16BitUnitsCapacity = utf16Length; michael@0: /* insert the initial NUL */ michael@0: bundle->f16BitUnits[0] = 0; michael@0: utf16Length = 1; michael@0: ++bundle->f16BitUnitsLength; michael@0: for (pos = -1, i = 0; i < count; ++i) { michael@0: array[i] = (struct SResource *)uhash_nextElement(bundle->fStringSet, &pos)->key.pointer; michael@0: } michael@0: /* Sort the strings so that each one is immediately followed by all of its suffixes. */ michael@0: uprv_sortArray(array, count, (int32_t)sizeof(struct SResource **), michael@0: compareStringSuffixes, NULL, FALSE, status); michael@0: /* michael@0: * Make suffixes point into earlier, longer strings that contain them. michael@0: * Temporarily use fSame and fSuffixOffset for suffix strings to michael@0: * refer to the remaining ones. michael@0: */ michael@0: if (U_SUCCESS(*status)) { michael@0: for (i = 0; i < count;) { michael@0: /* michael@0: * This string is not a suffix of the previous one; michael@0: * write this one and subsume the following ones that are michael@0: * suffixes of this one. michael@0: */ michael@0: struct SResource *res = array[i]; michael@0: const UChar *strLimit = res->u.fString.fChars + res->u.fString.fLength; michael@0: int32_t j; michael@0: for (j = i + 1; j < count; ++j) { michael@0: struct SResource *suffixRes = array[j]; michael@0: const UChar *s; michael@0: const UChar *suffix = suffixRes->u.fString.fChars; michael@0: const UChar *suffixLimit = suffix + suffixRes->u.fString.fLength; michael@0: int32_t offset = res->u.fString.fLength - suffixRes->u.fString.fLength; michael@0: if (offset < 0) { michael@0: break; /* suffix cannot be longer than the original */ michael@0: } michael@0: /* Is it a suffix of the earlier, longer key? */ michael@0: for (s = strLimit; suffix < suffixLimit && *--s == *--suffixLimit;) {} michael@0: if (suffix == suffixLimit && *s == *suffixLimit) { michael@0: if (suffixRes->u.fString.fNumCharsForLength == 0) { michael@0: /* yes, point to the earlier string */ michael@0: suffixRes->u.fString.fSame = res; michael@0: suffixRes->u.fString.fSuffixOffset = offset; michael@0: } else { michael@0: /* write the suffix by itself if we need explicit length */ michael@0: } michael@0: } else { michael@0: break; /* not a suffix, restart from here */ michael@0: } michael@0: } michael@0: i = j; michael@0: } michael@0: } michael@0: /* michael@0: * Re-sort the strings by ascending length (except suffixes last) michael@0: * to optimize for URES_TABLE16 and URES_ARRAY16: michael@0: * Keep as many as possible within reach of 16-bit offsets. michael@0: */ michael@0: uprv_sortArray(array, count, (int32_t)sizeof(struct SResource **), michael@0: compareStringLengths, NULL, FALSE, status); michael@0: if (U_SUCCESS(*status)) { michael@0: /* Write the non-suffix strings. */ michael@0: for (i = 0; i < count && array[i]->u.fString.fSame == NULL; ++i) { michael@0: utf16Length = string_writeUTF16v2(bundle, array[i], utf16Length); michael@0: } michael@0: /* Write the suffix strings. Make each point to the real string. */ michael@0: for (; i < count; ++i) { michael@0: struct SResource *res = array[i]; michael@0: struct SResource *same = res->u.fString.fSame; michael@0: res->fRes = same->fRes + same->u.fString.fNumCharsForLength + res->u.fString.fSuffixOffset; michael@0: res->u.fString.fSame = NULL; michael@0: res->fWritten = TRUE; michael@0: } michael@0: } michael@0: assert(utf16Length <= bundle->f16BitUnitsLength); michael@0: bundle->f16BitUnitsLength = utf16Length; michael@0: uprv_free(array); michael@0: } michael@0: break; michael@0: default: michael@0: break; michael@0: } michael@0: }