From 24aad163bc88cb10d2275385e9afc3de7f342d65 Mon Sep 17 00:00:00 2001 From: Adam Lesinski Date: Fri, 24 Apr 2015 19:19:30 -0700 Subject: Add namespace handling in attribute values Previously, you could only reference namespace prefixes in attribute names: & cmp ); } -static uint8_t* encodeLength(uint8_t* data, size_t length) { - if (length > 0x7fu) { - *data++ = 0x80u | (0x000000ffu & (length >> 8)); +template +static T* encodeLength(T* data, size_t length) { + static_assert(std::is_integral::value, "wat."); + + constexpr size_t kMask = 1 << ((sizeof(T) * 8) - 1); + constexpr size_t kMaxSize = kMask - 1; + if (length > kMaxSize) { + *data++ = kMask | (kMaxSize & (length >> (sizeof(T) * 8))); } - *data++ = 0x000000ffu & length; + *data++ = length; return data; } -static size_t encodedLengthByteCount(size_t length) { - return length > 0x7fu ? 2 : 1; +template +static size_t encodedLengthUnits(size_t length) { + static_assert(std::is_integral::value, "wat."); + + constexpr size_t kMask = 1 << ((sizeof(T) * 8) - 1); + constexpr size_t kMaxSize = kMask - 1; + return length > kMaxSize ? 2 : 1; } -bool StringPool::flattenUtf8(BigBuffer* out, const StringPool& pool) { + +bool StringPool::flatten(BigBuffer* out, const StringPool& pool, bool utf8) { const size_t startIndex = out->size(); android::ResStringPool_header* header = out->nextBlock(); header->header.type = android::RES_STRING_POOL_TYPE; header->header.headerSize = sizeof(*header); header->stringCount = pool.size(); - header->flags |= android::ResStringPool_header::UTF8_FLAG; + if (utf8) { + header->flags |= android::ResStringPool_header::UTF8_FLAG; + } uint32_t* indices = pool.size() != 0 ? out->nextBlock(pool.size()) : nullptr; @@ -300,25 +313,31 @@ bool StringPool::flattenUtf8(BigBuffer* out, const StringPool& pool) { *indices = out->size() - beforeStringsIndex; indices++; - std::string encoded = util::utf16ToUtf8(entry->value); + if (utf8) { + std::string encoded = util::utf16ToUtf8(entry->value); + + const size_t totalSize = encodedLengthUnits(entry->value.size()) + + encodedLengthUnits(encoded.length()) + + encoded.size() + 1; - const size_t stringByteLength = sizeof(char) * encoded.length(); - const size_t totalSize = encodedLengthByteCount(entry->value.size()) - + encodedLengthByteCount(encoded.length()) - + stringByteLength - + sizeof(char); + char* data = out->nextBlock(totalSize); - uint8_t* data = out->nextBlock(totalSize); + // First encode the actual UTF16 string length. + data = encodeLength(data, entry->value.size()); - // First encode the actual UTF16 string length. - data = encodeLength(data, entry->value.size()); + // Now encode the size of the converted UTF8 string. + data = encodeLength(data, encoded.length()); + strncpy(data, encoded.data(), encoded.size()); + } else { + const size_t totalSize = encodedLengthUnits(entry->value.size()) + + entry->value.size() + 1; - // Now encode the size of the converted UTF8 string. - data = encodeLength(data, encoded.length()); + char16_t* data = out->nextBlock(totalSize); - memcpy(data, encoded.data(), stringByteLength); - data += stringByteLength; - *data = 0; + // Encode the actual UTF16 string length. + data = encodeLength(data, entry->value.size()); + strncpy16(data, entry->value.data(), entry->value.size()); + } } out->align4(); @@ -364,4 +383,12 @@ bool StringPool::flattenUtf8(BigBuffer* out, const StringPool& pool) { return true; } +bool StringPool::flattenUtf8(BigBuffer* out, const StringPool& pool) { + return flatten(out, pool, true); +} + +bool StringPool::flattenUtf16(BigBuffer* out, const StringPool& pool) { + return flatten(out, pool, false); +} + } // namespace aapt -- cgit v1.2.3