// // 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) : mSorted(sorted), 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; } 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; } size_t strPos = 0; for (i=0; i 0x7fff ? sizeof(uint32_t) : sizeof(uint16_t); const size_t totalSize = lenSize + ((strSize+1)*sizeof(uint16_t)); ent.offset = strPos; uint16_t* dat = (uint16_t*)pool->editData(preSize + strPos + totalSize); if (dat == NULL) { fprintf(stderr, "ERROR: Out of memory for string pool\n"); return NO_MEMORY; } dat += (preSize+strPos)/sizeof(uint16_t); if (lenSize > sizeof(uint16_t)) { *dat = htods(0x8000 | ((strSize>>16)&0x7ffff)); dat++; } *dat++ = htods(strSize); strcpy16_htod(dat, ent.value); strPos += lenSize + (strSize+1)*sizeof(uint16_t); } // 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); } 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; }