Minor clean-ups.
* Track the length of the dex file in RawDexFile. This can be used to
improve its assertions.
* Move a sanity check for MmapCloser into the constructor.
* Add a TODO for moving the base64 openers into test code.
* Make the base64 decoder assume C strings, mention this in a comment.
Change-Id: Ib4d5a43d23ebe8819032763c42c536c8b826723a
diff --git a/src/base64.cc b/src/base64.cc
index e745a62..7c09688 100644
--- a/src/base64.cc
+++ b/src/base64.cc
@@ -32,11 +32,11 @@
255, 255, 255, 255
};
-byte* DecodeBase64(const char* src, size_t size, size_t* dst_size) {
+byte* DecodeBase64(const char* src, size_t* dst_size) {
std::vector<byte> tmp;
unsigned long t = 0, y = 0;
int g = 3;
- for (size_t i = 0; i < size; ++i) {
+ for (size_t i = 0; src[i] != '\0'; ++i) {
byte c = kMap[src[i] & 0xFF];
if (c == 255) continue;
// the final = symbols are read and used to trim the remaining bytes
diff --git a/src/base64.h b/src/base64.h
index 320148d..160046d 100644
--- a/src/base64.h
+++ b/src/base64.h
@@ -7,7 +7,8 @@
namespace art {
-byte* DecodeBase64(const char* src, size_t size, size_t* dst_size);
+// Decodes a C string with base64 encoded data.
+byte* DecodeBase64(const char* src, size_t* dst_size);
} // namespace art
diff --git a/src/dex_file.h b/src/dex_file.h
index 61add38..3545c07 100644
--- a/src/dex_file.h
+++ b/src/dex_file.h
@@ -17,6 +17,7 @@
// Opens a .dex file from a base64 encoded array. Returns NULL on
// failure.
+ // TODO: move this into the DexFile unit test
static DexFile* OpenBase64(const char* base64);
// Opens a .dex file from a RawDexFile. Takes ownership of the
diff --git a/src/raw_dex_file.cc b/src/raw_dex_file.cc
index 6f1703f..9994b9f 100644
--- a/src/raw_dex_file.cc
+++ b/src/raw_dex_file.cc
@@ -22,9 +22,10 @@
// Helper class to deallocate mmap-backed .dex files.
class MmapCloser : public RawDexFile::Closer {
public:
- MmapCloser(void* addr, size_t length) : addr_(addr), length_(length) {};
+ MmapCloser(void* addr, size_t length) : addr_(addr), length_(length) {
+ CHECK(addr != NULL);
+ };
virtual ~MmapCloser() {
- CHECK(addr_ != NULL);
if (munmap(addr_, length_) == -1) {
LG << "munmap: " << strerror(errno); // TODO: PLOG
}
@@ -47,7 +48,7 @@
RawDexFile* RawDexFile::OpenFile(const char* filename) {
CHECK(filename != NULL);
- int fd = open(filename, O_RDONLY);
+ int fd = open(filename, O_RDONLY); // TODO: scoped_fd
if (fd == -1) {
LG << "open: " << strerror(errno); // TODO: PLOG
return NULL;
@@ -69,22 +70,23 @@
close(fd);
byte* dex_file = reinterpret_cast<byte*>(addr);
Closer* closer = new MmapCloser(addr, length);
- return Open(dex_file, closer);
+ return Open(dex_file, length, closer);
}
RawDexFile* RawDexFile::OpenBase64(const char* base64) {
CHECK(base64 != NULL);
- size_t size = strlen(base64);
- byte* dex_file = DecodeBase64(base64, size, NULL);
+ size_t length;
+ byte* dex_file = DecodeBase64(base64, &length);
if (dex_file == NULL) {
return NULL;
}
RawDexFile::Closer* closer = new PtrCloser(dex_file);
- return Open(dex_file, closer);
+ return Open(dex_file, length, closer);
}
-RawDexFile* RawDexFile::Open(const byte* dex_file, Closer* closer) {
- scoped_ptr<RawDexFile> raw(new RawDexFile(dex_file, closer));
+RawDexFile* RawDexFile::Open(const byte* dex_file, size_t length,
+ Closer* closer) {
+ scoped_ptr<RawDexFile> raw(new RawDexFile(dex_file, length, closer));
if (!raw->Init()) {
return NULL;
} else {
diff --git a/src/raw_dex_file.h b/src/raw_dex_file.h
index ea2ac4d..7e158e4 100644
--- a/src/raw_dex_file.h
+++ b/src/raw_dex_file.h
@@ -153,10 +153,11 @@
static RawDexFile* OpenFile(const char* filename);
// Opens a .dex file from a base64 encoded array.
+ // TODO: move this into the RawDexFile unit test
static RawDexFile* OpenBase64(const char* base64);
// Opens a .dex file at a the given address.
- static RawDexFile* Open(const byte* dex_file, Closer* closer);
+ static RawDexFile* Open(const byte* dex_file, size_t length, Closer* closer);
// Closes a .dex file.
virtual ~RawDexFile();
@@ -348,8 +349,9 @@
}
private:
- RawDexFile(const byte* addr, Closer* closer)
+ RawDexFile(const byte* addr, size_t length, Closer* closer)
: base_(addr),
+ length_(length),
closer_(closer),
header_(0),
string_ids_(0),
@@ -381,6 +383,9 @@
// The base address of the memory mapping.
const byte* base_;
+ // The size of the underlying memory allocation in bytes.
+ size_t length_;
+
// Helper object to free the underlying allocation.
scoped_ptr<Closer> closer_;