// // Copyright 2006 The Android Open Source Project // // Build resource files from raw assets. // #include "StringPool.h" #include #define NOISY(x) //x void strcpy16_htod(uint16_t* dst, const uint16_t* src) { while (*src) { char16_t s = htods(*src); *dst++ = s; src++; } *dst = 0; } void printStringPool(const ResStringPool* pool) { const size_t NS = pool->size(); for (size_t s=0; sstringAt(s, &len)).string()); } } StringPool::StringPool(bool sorted, bool utf8) : mSorted(sorted), mUTF8(utf8), mValues(-1), mIdents(-1) { } ssize_t StringPool::add(const String16& value, bool mergeDuplicates) { return add(String16(), value, mergeDuplicates); } ssize_t StringPool::add(const String16& value, const Vector& spans) { ssize_t res = add(String16(), value, false); if (res >= 0) { addStyleSpans(res, spans); } return res; } ssize_t StringPool::add(const String16& ident, const String16& value, bool mergeDuplicates) { if (ident.size() > 0) { ssize_t idx = mIdents.valueFor(ident); if (idx >= 0) { fprintf(stderr, "ERROR: Duplicate string identifier %s\n", String8(mEntries[idx].value).string()); return UNKNOWN_ERROR; } } ssize_t vidx = mValues.indexOfKey(value); ssize_t pos = vidx >= 0 ? mValues.valueAt(vidx) : -1; ssize_t eidx = pos >= 0 ? mEntryArray.itemAt(pos) : -1; if (eidx < 0) { eidx = mEntries.add(entry(value)); if (eidx < 0) { fprintf(stderr, "Failure adding string %s\n", String8(value).string()); return eidx; } } const bool first = vidx < 0; if (first || !mergeDuplicates) { pos = mEntryArray.add(eidx); if (first) { vidx = mValues.add(value, pos); const size_t N = mEntryArrayToValues.size(); for (size_t i=0; i= vidx) { e++; } } } mEntryArrayToValues.add(vidx); if (!mSorted) { entry& ent = mEntries.editItemAt(eidx); ent.indices.add(pos); } } if (ident.size() > 0) { mIdents.add(ident, vidx); } NOISY(printf("Adding string %s to pool: pos=%d eidx=%d vidx=%d\n", String8(value).string(), pos, eidx, vidx)); return pos; } status_t StringPool::addStyleSpan(size_t idx, const String16& name, uint32_t start, uint32_t end) { entry_style_span span; span.name = name; span.span.firstChar = start; span.span.lastChar = end; return addStyleSpan(idx, span); } status_t StringPool::addStyleSpans(size_t idx, const Vector& spans) { const size_t N=spans.size(); for (size_t i=0; i StringPool::createStringBlock() { sp pool = new AaptFile(String8(), AaptGroupEntry(), String8()); status_t err = writeStringBlock(pool); return err == NO_ERROR ? pool : NULL; } #define ENCODE_LENGTH(str, chrsz, strSize) \ { \ size_t maxMask = 1 << ((chrsz*8)-1); \ size_t maxSize = maxMask-1; \ if (strSize > maxSize) { \ *str++ = maxMask | ((strSize>>(chrsz*8))&maxSize); \ } \ *str++ = strSize; \ } status_t StringPool::writeStringBlock(const sp& pool) { // Allow appending. Sorry this is a little wacky. if (pool->getSize() > 0) { sp block = createStringBlock(); if (block == NULL) { return UNKNOWN_ERROR; } ssize_t res = pool->writeData(block->getData(), block->getSize()); return (res >= 0) ? (status_t)NO_ERROR : res; } // First we need to add all style span names to the string pool. // We do this now (instead of when the span is added) so that these // will appear at the end of the pool, not disrupting the order // our client placed their own strings in it. const size_t STYLES = mEntryStyleArray.size(); size_t i; for (i=0; ieditData(preSize) == NULL) { fprintf(stderr, "ERROR: Out of memory for string pool\n"); return NO_MEMORY; } const size_t charSize = mUTF8 ? sizeof(uint8_t) : sizeof(char16_t); size_t strPos = 0; for (i=0; i (size_t)(1<<((charSize*8)-1))-1 ? charSize*2 : charSize; String8 encStr; if (mUTF8) { encStr = String8(ent.value); } const size_t encSize = mUTF8 ? encStr.size() : 0; const size_t encLenSize = mUTF8 ? (encSize > (size_t)(1<<((charSize*8)-1))-1 ? charSize*2 : charSize) : 0; ent.offset = strPos; const size_t totalSize = lenSize + encLenSize + ((mUTF8 ? encSize : strSize)+1)*charSize; void* dat = (void*)pool->editData(preSize + strPos + totalSize); if (dat == NULL) { fprintf(stderr, "ERROR: Out of memory for string pool\n"); return NO_MEMORY; } dat = (uint8_t*)dat + preSize + strPos; if (mUTF8) { uint8_t* strings = (uint8_t*)dat; ENCODE_LENGTH(strings, sizeof(uint8_t), strSize) ENCODE_LENGTH(strings, sizeof(uint8_t), encSize) strncpy((char*)strings, encStr, encSize+1); } else { uint16_t* strings = (uint16_t*)dat; ENCODE_LENGTH(strings, sizeof(uint16_t), strSize) strcpy16_htod(strings, ent.value); } strPos += totalSize; } // Pad ending string position up to a uint32_t boundary. if (strPos&0x3) { size_t padPos = ((strPos+3)&~0x3); uint8_t* dat = (uint8_t*)pool->editData(preSize + padPos); if (dat == NULL) { fprintf(stderr, "ERROR: Out of memory padding string pool\n"); return NO_MEMORY; } memset(dat+preSize+strPos, 0, padPos-strPos); strPos = padPos; } // Build the pool of style spans. size_t styPos = strPos; for (i=0; ieditData(preSize + styPos + totalSize); if (dat == NULL) { fprintf(stderr, "ERROR: Out of memory for string styles\n"); return NO_MEMORY; } ResStringPool_span* span = (ResStringPool_span*)(dat+preSize+styPos); for (size_t i=0; iname.index = htodl(ent.spans[i].span.name.index); span->firstChar = htodl(ent.spans[i].span.firstChar); span->lastChar = htodl(ent.spans[i].span.lastChar); span++; } span->name.index = htodl(ResStringPool_span::END); styPos += totalSize; } if (STYLES > 0) { // Add full terminator at the end (when reading we validate that // the end of the pool is fully terminated to simplify error // checking). size_t extra = sizeof(ResStringPool_span)-sizeof(ResStringPool_ref); uint8_t* dat = (uint8_t*)pool->editData(preSize + styPos + extra); if (dat == NULL) { fprintf(stderr, "ERROR: Out of memory for string styles\n"); return NO_MEMORY; } uint32_t* p = (uint32_t*)(dat+preSize+styPos); while (extra > 0) { *p++ = htodl(ResStringPool_span::END); extra -= sizeof(uint32_t); } styPos += extra; } // Write header. ResStringPool_header* header = (ResStringPool_header*)pool->padData(sizeof(uint32_t)); if (header == NULL) { fprintf(stderr, "ERROR: Out of memory for string pool\n"); return NO_MEMORY; } memset(header, 0, sizeof(*header)); header->header.type = htods(RES_STRING_POOL_TYPE); header->header.headerSize = htods(sizeof(*header)); header->header.size = htodl(pool->getSize()); header->stringCount = htodl(ENTRIES); header->styleCount = htodl(STYLES); if (mSorted) { header->flags |= htodl(ResStringPool_header::SORTED_FLAG); } if (mUTF8) { header->flags |= htodl(ResStringPool_header::UTF8_FLAG); } header->stringsStart = htodl(preSize); header->stylesStart = htodl(STYLES > 0 ? (preSize+strPos) : 0); // Write string index array. uint32_t* index = (uint32_t*)(header+1); if (mSorted) { for (i=0; i(entryAt(i)); ent.indices.clear(); ent.indices.add(i); *index++ = htodl(ent.offset); } } else { for (i=0; i* indices = offsetsForString(val); ssize_t res = indices != NULL && indices->size() > 0 ? indices->itemAt(0) : -1; NOISY(printf("Offset for string %s: %d (%s)\n", String8(val).string(), res, res >= 0 ? String8(mEntries[mEntryArray[res]].value).string() : String8())); return res; } const Vector* StringPool::offsetsForString(const String16& val) const { ssize_t pos = mValues.valueFor(val); if (pos < 0) { return NULL; } return &mEntries[mEntryArray[pos]].indices; }