Move StringPool to libandroidfw
Test: verified affected tests pass
Bug: 232940948
Change-Id: I22089893d7e5013f759c39ce190bec07fa6435db
diff --git a/tools/aapt2/util/BigBuffer.cpp b/tools/aapt2/util/BigBuffer.cpp
deleted file mode 100644
index 75fa789..0000000
--- a/tools/aapt2/util/BigBuffer.cpp
+++ /dev/null
@@ -1,87 +0,0 @@
-/*
- * Copyright (C) 2015 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include "util/BigBuffer.h"
-
-#include <algorithm>
-#include <memory>
-#include <vector>
-
-#include "android-base/logging.h"
-
-namespace aapt {
-
-void* BigBuffer::NextBlockImpl(size_t size) {
- if (!blocks_.empty()) {
- Block& block = blocks_.back();
- if (block.block_size_ - block.size >= size) {
- void* out_buffer = block.buffer.get() + block.size;
- block.size += size;
- size_ += size;
- return out_buffer;
- }
- }
-
- const size_t actual_size = std::max(block_size_, size);
-
- Block block = {};
-
- // Zero-allocate the block's buffer.
- block.buffer = std::unique_ptr<uint8_t[]>(new uint8_t[actual_size]());
- CHECK(block.buffer);
-
- block.size = size;
- block.block_size_ = actual_size;
-
- blocks_.push_back(std::move(block));
- size_ += size;
- return blocks_.back().buffer.get();
-}
-
-void* BigBuffer::NextBlock(size_t* out_size) {
- if (!blocks_.empty()) {
- Block& block = blocks_.back();
- if (block.size != block.block_size_) {
- void* out_buffer = block.buffer.get() + block.size;
- size_t size = block.block_size_ - block.size;
- block.size = block.block_size_;
- size_ += size;
- *out_size = size;
- return out_buffer;
- }
- }
-
- // Zero-allocate the block's buffer.
- Block block = {};
- block.buffer = std::unique_ptr<uint8_t[]>(new uint8_t[block_size_]());
- CHECK(block.buffer);
- block.size = block_size_;
- block.block_size_ = block_size_;
- blocks_.push_back(std::move(block));
- size_ += block_size_;
- *out_size = block_size_;
- return blocks_.back().buffer.get();
-}
-
-std::string BigBuffer::to_string() const {
- std::string result;
- for (const Block& block : blocks_) {
- result.append(block.buffer.get(), block.buffer.get() + block.size);
- }
- return result;
-}
-
-} // namespace aapt
diff --git a/tools/aapt2/util/BigBuffer.h b/tools/aapt2/util/BigBuffer.h
deleted file mode 100644
index d4b3abc..0000000
--- a/tools/aapt2/util/BigBuffer.h
+++ /dev/null
@@ -1,189 +0,0 @@
-/*
- * Copyright (C) 2015 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef AAPT_BIG_BUFFER_H
-#define AAPT_BIG_BUFFER_H
-
-#include <cstring>
-#include <memory>
-#include <string>
-#include <type_traits>
-#include <vector>
-
-#include "android-base/logging.h"
-#include "android-base/macros.h"
-
-namespace aapt {
-
-/**
- * Inspired by protobuf's ZeroCopyOutputStream, offers blocks of memory
- * in which to write without knowing the full size of the entire payload.
- * This is essentially a list of memory blocks. As one fills up, another
- * block is allocated and appended to the end of the list.
- */
-class BigBuffer {
- public:
- /**
- * A contiguous block of allocated memory.
- */
- struct Block {
- /**
- * Pointer to the memory.
- */
- std::unique_ptr<uint8_t[]> buffer;
-
- /**
- * Size of memory that is currently occupied. The actual
- * allocation may be larger.
- */
- size_t size;
-
- private:
- friend class BigBuffer;
-
- /**
- * The size of the memory block allocation.
- */
- size_t block_size_;
- };
-
- typedef std::vector<Block>::const_iterator const_iterator;
-
- /**
- * Create a BigBuffer with block allocation sizes
- * of block_size.
- */
- explicit BigBuffer(size_t block_size);
-
- BigBuffer(BigBuffer&& rhs) noexcept;
-
- /**
- * Number of occupied bytes in all the allocated blocks.
- */
- size_t size() const;
-
- /**
- * Returns a pointer to an array of T, where T is
- * a POD type. The elements are zero-initialized.
- */
- template <typename T>
- T* NextBlock(size_t count = 1);
-
- /**
- * Returns the next block available and puts the size in out_count.
- * This is useful for grabbing blocks where the size doesn't matter.
- * Use BackUp() to give back any bytes that were not used.
- */
- void* NextBlock(size_t* out_count);
-
- /**
- * Backs up count bytes. This must only be called after NextBlock()
- * and can not be larger than sizeof(T) * count of the last NextBlock()
- * call.
- */
- void BackUp(size_t count);
-
- /**
- * Moves the specified BigBuffer into this one. When this method
- * returns, buffer is empty.
- */
- void AppendBuffer(BigBuffer&& buffer);
-
- /**
- * Pads the block with 'bytes' bytes of zero values.
- */
- void Pad(size_t bytes);
-
- /**
- * Pads the block so that it aligns on a 4 byte boundary.
- */
- void Align4();
-
- size_t block_size() const;
-
- const_iterator begin() const;
- const_iterator end() const;
-
- std::string to_string() const;
-
- private:
- DISALLOW_COPY_AND_ASSIGN(BigBuffer);
-
- /**
- * Returns a pointer to a buffer of the requested size.
- * The buffer is zero-initialized.
- */
- void* NextBlockImpl(size_t size);
-
- size_t block_size_;
- size_t size_;
- std::vector<Block> blocks_;
-};
-
-inline BigBuffer::BigBuffer(size_t block_size)
- : block_size_(block_size), size_(0) {}
-
-inline BigBuffer::BigBuffer(BigBuffer&& rhs) noexcept
- : block_size_(rhs.block_size_),
- size_(rhs.size_),
- blocks_(std::move(rhs.blocks_)) {}
-
-inline size_t BigBuffer::size() const { return size_; }
-
-inline size_t BigBuffer::block_size() const { return block_size_; }
-
-template <typename T>
-inline T* BigBuffer::NextBlock(size_t count) {
- static_assert(std::is_standard_layout<T>::value,
- "T must be standard_layout type");
- CHECK(count != 0);
- return reinterpret_cast<T*>(NextBlockImpl(sizeof(T) * count));
-}
-
-inline void BigBuffer::BackUp(size_t count) {
- Block& block = blocks_.back();
- block.size -= count;
- size_ -= count;
-}
-
-inline void BigBuffer::AppendBuffer(BigBuffer&& buffer) {
- std::move(buffer.blocks_.begin(), buffer.blocks_.end(),
- std::back_inserter(blocks_));
- size_ += buffer.size_;
- buffer.blocks_.clear();
- buffer.size_ = 0;
-}
-
-inline void BigBuffer::Pad(size_t bytes) { NextBlock<char>(bytes); }
-
-inline void BigBuffer::Align4() {
- const size_t unaligned = size_ % 4;
- if (unaligned != 0) {
- Pad(4 - unaligned);
- }
-}
-
-inline BigBuffer::const_iterator BigBuffer::begin() const {
- return blocks_.begin();
-}
-
-inline BigBuffer::const_iterator BigBuffer::end() const {
- return blocks_.end();
-}
-
-} // namespace aapt
-
-#endif // AAPT_BIG_BUFFER_H
diff --git a/tools/aapt2/util/BigBuffer_test.cpp b/tools/aapt2/util/BigBuffer_test.cpp
deleted file mode 100644
index 64dcc1d..0000000
--- a/tools/aapt2/util/BigBuffer_test.cpp
+++ /dev/null
@@ -1,100 +0,0 @@
-/*
- * Copyright (C) 2015 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include "util/BigBuffer.h"
-
-#include "test/Test.h"
-
-using ::testing::NotNull;
-
-namespace aapt {
-
-TEST(BigBufferTest, AllocateSingleBlock) {
- BigBuffer buffer(4);
-
- EXPECT_THAT(buffer.NextBlock<char>(2), NotNull());
- EXPECT_EQ(2u, buffer.size());
-}
-
-TEST(BigBufferTest, ReturnSameBlockIfNextAllocationFits) {
- BigBuffer buffer(16);
-
- char* b1 = buffer.NextBlock<char>(8);
- EXPECT_THAT(b1, NotNull());
-
- char* b2 = buffer.NextBlock<char>(4);
- EXPECT_THAT(b2, NotNull());
-
- EXPECT_EQ(b1 + 8, b2);
-}
-
-TEST(BigBufferTest, AllocateExactSizeBlockIfLargerThanBlockSize) {
- BigBuffer buffer(16);
-
- EXPECT_THAT(buffer.NextBlock<char>(32), NotNull());
- EXPECT_EQ(32u, buffer.size());
-}
-
-TEST(BigBufferTest, AppendAndMoveBlock) {
- BigBuffer buffer(16);
-
- uint32_t* b1 = buffer.NextBlock<uint32_t>();
- ASSERT_THAT(b1, NotNull());
- *b1 = 33;
-
- {
- BigBuffer buffer2(16);
- b1 = buffer2.NextBlock<uint32_t>();
- ASSERT_THAT(b1, NotNull());
- *b1 = 44;
-
- buffer.AppendBuffer(std::move(buffer2));
- EXPECT_EQ(0u, buffer2.size()); // NOLINT
- EXPECT_EQ(buffer2.begin(), buffer2.end());
- }
-
- EXPECT_EQ(2 * sizeof(uint32_t), buffer.size());
-
- auto b = buffer.begin();
- ASSERT_NE(b, buffer.end());
- ASSERT_EQ(sizeof(uint32_t), b->size);
- ASSERT_EQ(33u, *reinterpret_cast<uint32_t*>(b->buffer.get()));
- ++b;
-
- ASSERT_NE(b, buffer.end());
- ASSERT_EQ(sizeof(uint32_t), b->size);
- ASSERT_EQ(44u, *reinterpret_cast<uint32_t*>(b->buffer.get()));
- ++b;
-
- ASSERT_EQ(b, buffer.end());
-}
-
-TEST(BigBufferTest, PadAndAlignProperly) {
- BigBuffer buffer(16);
-
- ASSERT_THAT(buffer.NextBlock<char>(2), NotNull());
- ASSERT_EQ(2u, buffer.size());
- buffer.Pad(2);
- ASSERT_EQ(4u, buffer.size());
- buffer.Align4();
- ASSERT_EQ(4u, buffer.size());
- buffer.Pad(2);
- ASSERT_EQ(6u, buffer.size());
- buffer.Align4();
- ASSERT_EQ(8u, buffer.size());
-}
-
-} // namespace aapt
diff --git a/tools/aapt2/util/Files.cpp b/tools/aapt2/util/Files.cpp
index 3285d8b..5d5b7cd 100644
--- a/tools/aapt2/util/Files.cpp
+++ b/tools/aapt2/util/Files.cpp
@@ -333,9 +333,8 @@
if (ignore) {
if (chatty) {
- diag_->Warn(DiagMessage()
- << "skipping "
- << (type == FileType::kDirectory ? "dir '" : "file '")
+ diag_->Warn(android::DiagMessage()
+ << "skipping " << (type == FileType::kDirectory ? "dir '" : "file '")
<< filename << "' due to ignore pattern '" << token << "'");
}
return false;
@@ -345,11 +344,12 @@
}
std::optional<std::vector<std::string>> FindFiles(const android::StringPiece& path,
- IDiagnostics* diag, const FileFilter* filter) {
+ android::IDiagnostics* diag,
+ const FileFilter* filter) {
const std::string root_dir = path.to_string();
std::unique_ptr<DIR, decltype(closedir)*> d(opendir(root_dir.data()), closedir);
if (!d) {
- diag->Error(DiagMessage() << SystemErrorCodeToString(errno) << ": " << root_dir);
+ diag->Error(android::DiagMessage() << SystemErrorCodeToString(errno) << ": " << root_dir);
return {};
}
diff --git a/tools/aapt2/util/Files.h b/tools/aapt2/util/Files.h
index a2b1b58..ee95712 100644
--- a/tools/aapt2/util/Files.h
+++ b/tools/aapt2/util/Files.h
@@ -24,12 +24,11 @@
#include <vector>
#include "android-base/macros.h"
+#include "androidfw/IDiagnostics.h"
+#include "androidfw/Source.h"
#include "androidfw/StringPiece.h"
#include "utils/FileMap.h"
-#include "Diagnostics.h"
-#include "Source.h"
-
namespace aapt {
namespace file {
@@ -98,7 +97,8 @@
// Pattern format is specified in the FileFilter::SetPattern() method.
class FileFilter {
public:
- explicit FileFilter(IDiagnostics* diag) : diag_(diag) {}
+ explicit FileFilter(android::IDiagnostics* diag) : diag_(diag) {
+ }
// Patterns syntax:
// - Delimiter is :
@@ -120,14 +120,14 @@
private:
DISALLOW_COPY_AND_ASSIGN(FileFilter);
- IDiagnostics* diag_;
+ android::IDiagnostics* diag_;
std::vector<std::string> pattern_tokens_;
};
// Returns a list of files relative to the directory identified by `path`.
// An optional FileFilter filters out any files that don't pass.
std::optional<std::vector<std::string>> FindFiles(const android::StringPiece& path,
- IDiagnostics* diag,
+ android::IDiagnostics* diag,
const FileFilter* filter = nullptr);
} // namespace file
diff --git a/tools/aapt2/util/Util.cpp b/tools/aapt2/util/Util.cpp
index efbbf8e..9b7ebdd 100644
--- a/tools/aapt2/util/Util.cpp
+++ b/tools/aapt2/util/Util.cpp
@@ -23,11 +23,12 @@
#include "android-base/stringprintf.h"
#include "android-base/strings.h"
+#include "androidfw/BigBuffer.h"
#include "androidfw/StringPiece.h"
+#include "androidfw/Util.h"
#include "build/version.h"
#include "text/Unicode.h"
#include "text/Utf8Iterator.h"
-#include "util/BigBuffer.h"
#include "utils/Unicode.h"
using ::aapt::text::Utf8Iterator;
@@ -340,107 +341,6 @@
return true;
}
-std::string Utf8ToModifiedUtf8(const std::string& utf8) {
- // Java uses Modified UTF-8 which only supports the 1, 2, and 3 byte formats of UTF-8. To encode
- // 4 byte UTF-8 codepoints, Modified UTF-8 allows the use of surrogate pairs in the same format
- // of CESU-8 surrogate pairs. Calculate the size of the utf8 string with all 4 byte UTF-8
- // codepoints replaced with 2 3 byte surrogate pairs
- size_t modified_size = 0;
- const size_t size = utf8.size();
- for (size_t i = 0; i < size; i++) {
- if (((uint8_t) utf8[i] >> 4) == 0xF) {
- modified_size += 6;
- i += 3;
- } else {
- modified_size++;
- }
- }
-
- // Early out if no 4 byte codepoints are found
- if (size == modified_size) {
- return utf8;
- }
-
- std::string output;
- output.reserve(modified_size);
- for (size_t i = 0; i < size; i++) {
- if (((uint8_t) utf8[i] >> 4) == 0xF) {
- int32_t codepoint = utf32_from_utf8_at(utf8.data(), size, i, nullptr);
-
- // Calculate the high and low surrogates as UTF-16 would
- int32_t high = ((codepoint - 0x10000) / 0x400) + 0xD800;
- int32_t low = ((codepoint - 0x10000) % 0x400) + 0xDC00;
-
- // Encode each surrogate in UTF-8
- output.push_back((char) (0xE4 | ((high >> 12) & 0xF)));
- output.push_back((char) (0x80 | ((high >> 6) & 0x3F)));
- output.push_back((char) (0x80 | (high & 0x3F)));
- output.push_back((char) (0xE4 | ((low >> 12) & 0xF)));
- output.push_back((char) (0x80 | ((low >> 6) & 0x3F)));
- output.push_back((char) (0x80 | (low & 0x3F)));
- i += 3;
- } else {
- output.push_back(utf8[i]);
- }
- }
-
- return output;
-}
-
-std::string ModifiedUtf8ToUtf8(const std::string& modified_utf8) {
- // The UTF-8 representation will have a byte length less than or equal to the Modified UTF-8
- // representation.
- std::string output;
- output.reserve(modified_utf8.size());
-
- size_t index = 0;
- const size_t modified_size = modified_utf8.size();
- while (index < modified_size) {
- size_t next_index;
- int32_t high_surrogate = utf32_from_utf8_at(modified_utf8.data(), modified_size, index,
- &next_index);
- if (high_surrogate < 0) {
- return {};
- }
-
- // Check that the first codepoint is within the high surrogate range
- if (high_surrogate >= 0xD800 && high_surrogate <= 0xDB7F) {
- int32_t low_surrogate = utf32_from_utf8_at(modified_utf8.data(), modified_size, next_index,
- &next_index);
- if (low_surrogate < 0) {
- return {};
- }
-
- // Check that the second codepoint is within the low surrogate range
- if (low_surrogate >= 0xDC00 && low_surrogate <= 0xDFFF) {
- const char32_t codepoint = (char32_t) (((high_surrogate - 0xD800) * 0x400)
- + (low_surrogate - 0xDC00) + 0x10000);
-
- // The decoded codepoint should represent a 4 byte, UTF-8 character
- const size_t utf8_length = (size_t) utf32_to_utf8_length(&codepoint, 1);
- if (utf8_length != 4) {
- return {};
- }
-
- // Encode the UTF-8 representation of the codepoint into the string
- char* start = &output[output.size()];
- output.resize(output.size() + utf8_length);
- utf32_to_utf8((char32_t*) &codepoint, 1, start, utf8_length + 1);
-
- index = next_index;
- continue;
- }
- }
-
- // Append non-surrogate pairs to the output string
- for (size_t i = index; i < next_index; i++) {
- output.push_back(modified_utf8[i]);
- }
- index = next_index;
- }
- return output;
-}
-
std::u16string Utf8ToUtf16(const StringPiece& utf8) {
ssize_t utf16_length = utf8_to_utf16_length(
reinterpret_cast<const uint8_t*>(utf8.data()), utf8.length());
@@ -467,7 +367,7 @@
return utf8;
}
-bool WriteAll(std::ostream& out, const BigBuffer& buffer) {
+bool WriteAll(std::ostream& out, const android::BigBuffer& buffer) {
for (const auto& b : buffer) {
if (!out.write(reinterpret_cast<const char*>(b.buffer.get()), b.size)) {
return false;
@@ -476,17 +376,6 @@
return true;
}
-std::unique_ptr<uint8_t[]> Copy(const BigBuffer& buffer) {
- std::unique_ptr<uint8_t[]> data =
- std::unique_ptr<uint8_t[]>(new uint8_t[buffer.size()]);
- uint8_t* p = data.get();
- for (const auto& block : buffer) {
- memcpy(p, block.buffer.get(), block.size);
- p += block.size;
- }
- return data;
-}
-
typename Tokenizer::iterator& Tokenizer::iterator::operator++() {
const char* start = token_.end();
const char* end = str_.end();
@@ -553,19 +442,5 @@
return true;
}
-StringPiece16 GetString16(const android::ResStringPool& pool, size_t idx) {
- if (auto str = pool.stringAt(idx); str.ok()) {
- return *str;
- }
- return StringPiece16();
-}
-
-std::string GetString(const android::ResStringPool& pool, size_t idx) {
- if (auto str = pool.string8At(idx); str.ok()) {
- return ModifiedUtf8ToUtf8(str->to_string());
- }
- return Utf16ToUtf8(GetString16(pool, idx));
-}
-
} // namespace util
} // namespace aapt
diff --git a/tools/aapt2/util/Util.h b/tools/aapt2/util/Util.h
index c3efe6a..8d3b413 100644
--- a/tools/aapt2/util/Util.h
+++ b/tools/aapt2/util/Util.h
@@ -23,12 +23,11 @@
#include <string>
#include <vector>
+#include "androidfw/BigBuffer.h"
#include "androidfw/ResourceTypes.h"
#include "androidfw/StringPiece.h"
#include "utils/ByteOrder.h"
-#include "util/BigBuffer.h"
-
#ifdef _WIN32
// TODO(adamlesinski): remove once http://b/32447322 is resolved.
// utils/ByteOrder.h includes winsock2.h on WIN32,
@@ -149,15 +148,6 @@
};
}
-// Helper method to extract a UTF-16 string from a StringPool. If the string is stored as UTF-8,
-// the conversion to UTF-16 happens within ResStringPool.
-android::StringPiece16 GetString16(const android::ResStringPool& pool, size_t idx);
-
-// Helper method to extract a UTF-8 string from a StringPool. If the string is stored as UTF-16,
-// the conversion from UTF-16 to UTF-8 does not happen in ResStringPool and is done by this method,
-// which maintains no state or cache. This means we must return an std::string copy.
-std::string GetString(const android::ResStringPool& pool, size_t idx);
-
// Checks that the Java string format contains no non-positional arguments (arguments without
// explicitly specifying an index) when there are more than one argument. This is an error
// because translations may rearrange the order of the arguments in the string, which will
@@ -212,19 +202,8 @@
return error_.empty();
}
-// Converts a UTF8 string into Modified UTF8
-std::string Utf8ToModifiedUtf8(const std::string& utf8);
-std::string ModifiedUtf8ToUtf8(const std::string& modified_utf8);
-
-// Converts a UTF8 string to a UTF16 string.
-std::u16string Utf8ToUtf16(const android::StringPiece& utf8);
-std::string Utf16ToUtf8(const android::StringPiece16& utf16);
-
// Writes the entire BigBuffer to the output stream.
-bool WriteAll(std::ostream& out, const BigBuffer& buffer);
-
-// Copies the entire BigBuffer into a single buffer.
-std::unique_ptr<uint8_t[]> Copy(const BigBuffer& buffer);
+bool WriteAll(std::ostream& out, const android::BigBuffer& buffer);
// A Tokenizer implemented as an iterable collection. It does not allocate any memory on the heap
// nor use standard containers.
@@ -277,22 +256,6 @@
return Tokenizer(str, sep);
}
-inline uint16_t HostToDevice16(uint16_t value) {
- return htods(value);
-}
-
-inline uint32_t HostToDevice32(uint32_t value) {
- return htodl(value);
-}
-
-inline uint16_t DeviceToHost16(uint16_t value) {
- return dtohs(value);
-}
-
-inline uint32_t DeviceToHost32(uint32_t value) {
- return dtohl(value);
-}
-
// Given a path like: res/xml-sw600dp/foo.xml
//
// Extracts "res/xml-sw600dp/" into outPrefix.
@@ -305,13 +268,15 @@
} // namespace util
+} // namespace aapt
+
+namespace std {
// Stream operator for functions. Calls the function with the stream as an argument.
// In the aapt namespace for lookup.
inline ::std::ostream& operator<<(::std::ostream& out,
const ::std::function<::std::ostream&(::std::ostream&)>& f) {
return f(out);
}
-
-} // namespace aapt
+} // namespace std
#endif // AAPT_UTIL_H