summaryrefslogtreecommitdiffstats
path: root/applypatch
diff options
context:
space:
mode:
authorTianjie Xu <xunchang@google.com>2016-10-18 02:15:29 +0200
committerandroid-build-merger <android-build-merger@google.com>2016-10-18 02:15:29 +0200
commit430b7ae4699eb3685dcc0f7c736a9aebd76829a8 (patch)
treee237b84e2e6f4cd6a562f88ab3e491eca8767e9f /applypatch
parentMerge "init: move healthd to late-init" (diff)
parentMerge "Change StringValue to use std::string" (diff)
downloadandroid_bootable_recovery-430b7ae4699eb3685dcc0f7c736a9aebd76829a8.tar
android_bootable_recovery-430b7ae4699eb3685dcc0f7c736a9aebd76829a8.tar.gz
android_bootable_recovery-430b7ae4699eb3685dcc0f7c736a9aebd76829a8.tar.bz2
android_bootable_recovery-430b7ae4699eb3685dcc0f7c736a9aebd76829a8.tar.lz
android_bootable_recovery-430b7ae4699eb3685dcc0f7c736a9aebd76829a8.tar.xz
android_bootable_recovery-430b7ae4699eb3685dcc0f7c736a9aebd76829a8.tar.zst
android_bootable_recovery-430b7ae4699eb3685dcc0f7c736a9aebd76829a8.zip
Diffstat (limited to 'applypatch')
-rw-r--r--applypatch/applypatch.cpp28
-rw-r--r--applypatch/bspatch.cpp12
-rw-r--r--applypatch/imgpatch.cpp44
-rw-r--r--applypatch/include/applypatch/applypatch.h10
-rw-r--r--applypatch/main.cpp42
-rw-r--r--applypatch/utils.cpp12
-rw-r--r--applypatch/utils.h6
7 files changed, 75 insertions, 79 deletions
diff --git a/applypatch/applypatch.cpp b/applypatch/applypatch.cpp
index e52ef99dc..cf155607b 100644
--- a/applypatch/applypatch.cpp
+++ b/applypatch/applypatch.cpp
@@ -408,11 +408,10 @@ int ParseSha1(const char* str, uint8_t* digest) {
// Search an array of sha1 strings for one matching the given sha1.
// Return the index of the match on success, or -1 if no match is
// found.
-int FindMatchingPatch(uint8_t* sha1, char* const * const patch_sha1_str,
- int num_patches) {
- uint8_t patch_sha1[SHA_DIGEST_LENGTH];
- for (int i = 0; i < num_patches; ++i) {
- if (ParseSha1(patch_sha1_str[i], patch_sha1) == 0 &&
+int FindMatchingPatch(uint8_t* sha1, const std::vector<std::string>& patch_sha1_str) {
+ for (size_t i = 0; i < patch_sha1_str.size(); ++i) {
+ uint8_t patch_sha1[SHA_DIGEST_LENGTH];
+ if (ParseSha1(patch_sha1_str[i].c_str(), patch_sha1) == 0 &&
memcmp(patch_sha1, sha1, SHA_DIGEST_LENGTH) == 0) {
return i;
}
@@ -423,8 +422,7 @@ int FindMatchingPatch(uint8_t* sha1, char* const * const patch_sha1_str,
// Returns 0 if the contents of the file (argv[2]) or the cached file
// match any of the sha1's on the command line (argv[3:]). Returns
// nonzero otherwise.
-int applypatch_check(const char* filename, int num_patches,
- char** const patch_sha1_str) {
+int applypatch_check(const char* filename, const std::vector<std::string>& patch_sha1_str) {
FileContents file;
// It's okay to specify no sha1s; the check will pass if the
@@ -432,8 +430,7 @@ int applypatch_check(const char* filename, int num_patches,
// partitions, where the filename encodes the sha1s; no need to
// check them twice.)
if (LoadFileContents(filename, &file) != 0 ||
- (num_patches > 0 &&
- FindMatchingPatch(file.sha1, patch_sha1_str, num_patches) < 0)) {
+ FindMatchingPatch(file.sha1, patch_sha1_str) < 0) {
printf("file \"%s\" doesn't have any of expected "
"sha1 sums; checking cache\n", filename);
@@ -448,7 +445,7 @@ int applypatch_check(const char* filename, int num_patches,
return 1;
}
- if (FindMatchingPatch(file.sha1, patch_sha1_str, num_patches) < 0) {
+ if (FindMatchingPatch(file.sha1, patch_sha1_str) < 0) {
printf("cache bits don't match any sha1 for \"%s\"\n", filename);
return 1;
}
@@ -532,8 +529,7 @@ int applypatch(const char* source_filename,
const char* target_filename,
const char* target_sha1_str,
size_t target_size,
- int num_patches,
- char** const patch_sha1_str,
+ const std::vector<std::string>& patch_sha1_str,
Value** patch_data,
Value* bonus_data) {
printf("patch %s: ", source_filename);
@@ -573,7 +569,7 @@ int applypatch(const char* source_filename,
}
if (!source_file.data.empty()) {
- int to_use = FindMatchingPatch(source_file.sha1, patch_sha1_str, num_patches);
+ int to_use = FindMatchingPatch(source_file.sha1, patch_sha1_str);
if (to_use >= 0) {
source_patch_value = patch_data[to_use];
}
@@ -589,7 +585,7 @@ int applypatch(const char* source_filename,
return 1;
}
- int to_use = FindMatchingPatch(copy_file.sha1, patch_sha1_str, num_patches);
+ int to_use = FindMatchingPatch(copy_file.sha1, patch_sha1_str);
if (to_use >= 0) {
copy_patch_value = patch_data[to_use];
}
@@ -701,8 +697,8 @@ static int GenerateTarget(FileContents* source_file,
printf("patch is not a blob\n");
return 1;
}
- char* header = patch->data;
- ssize_t header_bytes_read = patch->size;
+ const char* header = &patch->data[0];
+ size_t header_bytes_read = patch->data.size();
bool use_bsdiff = false;
if (header_bytes_read >= 8 && memcmp(header, "BSDIFF40", 8) == 0) {
use_bsdiff = true;
diff --git a/applypatch/bspatch.cpp b/applypatch/bspatch.cpp
index a4945da28..eb45e9ce9 100644
--- a/applypatch/bspatch.cpp
+++ b/applypatch/bspatch.cpp
@@ -64,7 +64,7 @@ void ShowBSDiffLicense() {
);
}
-static off_t offtin(u_char *buf)
+static off_t offtin(const u_char *buf)
{
off_t y;
@@ -130,7 +130,7 @@ int ApplyBSDiffPatchMem(const unsigned char* old_data, ssize_t old_size,
// from oldfile to x bytes from the diff block; copy y bytes from the
// extra block; seek forwards in oldfile by z bytes".
- unsigned char* header = (unsigned char*) patch->data + patch_offset;
+ const unsigned char* header = reinterpret_cast<const unsigned char*>(&patch->data[patch_offset]);
if (memcmp(header, "BSDIFF40", 8) != 0) {
printf("corrupt bsdiff patch file header (magic number)\n");
return 1;
@@ -149,7 +149,7 @@ int ApplyBSDiffPatchMem(const unsigned char* old_data, ssize_t old_size,
int bzerr;
bz_stream cstream;
- cstream.next_in = patch->data + patch_offset + 32;
+ cstream.next_in = const_cast<char*>(&patch->data[patch_offset + 32]);
cstream.avail_in = ctrl_len;
cstream.bzalloc = NULL;
cstream.bzfree = NULL;
@@ -159,7 +159,7 @@ int ApplyBSDiffPatchMem(const unsigned char* old_data, ssize_t old_size,
}
bz_stream dstream;
- dstream.next_in = patch->data + patch_offset + 32 + ctrl_len;
+ dstream.next_in = const_cast<char*>(&patch->data[patch_offset + 32 + ctrl_len]);
dstream.avail_in = data_len;
dstream.bzalloc = NULL;
dstream.bzfree = NULL;
@@ -169,8 +169,8 @@ int ApplyBSDiffPatchMem(const unsigned char* old_data, ssize_t old_size,
}
bz_stream estream;
- estream.next_in = patch->data + patch_offset + 32 + ctrl_len + data_len;
- estream.avail_in = patch->size - (patch_offset + 32 + ctrl_len + data_len);
+ estream.next_in = const_cast<char*>(&patch->data[patch_offset + 32 + ctrl_len + data_len]);
+ estream.avail_in = patch->data.size() - (patch_offset + 32 + ctrl_len + data_len);
estream.bzalloc = NULL;
estream.bzfree = NULL;
estream.opaque = NULL;
diff --git a/applypatch/imgpatch.cpp b/applypatch/imgpatch.cpp
index 0c06d6b1e..1c4409e36 100644
--- a/applypatch/imgpatch.cpp
+++ b/applypatch/imgpatch.cpp
@@ -24,6 +24,7 @@
#include <unistd.h>
#include <string.h>
+#include <string>
#include <vector>
#include "zlib.h"
@@ -35,10 +36,10 @@
int ApplyImagePatch(const unsigned char* old_data, ssize_t old_size,
const unsigned char* patch_data, ssize_t patch_size,
SinkFn sink, void* token) {
- Value patch = {VAL_BLOB, patch_size,
- reinterpret_cast<char*>(const_cast<unsigned char*>(patch_data))};
- return ApplyImagePatch(
- old_data, old_size, &patch, sink, token, nullptr, nullptr);
+ Value patch(VAL_BLOB, std::string(
+ reinterpret_cast<const char*>(patch_data), patch_size));
+
+ return ApplyImagePatch(old_data, old_size, &patch, sink, token, nullptr, nullptr);
}
/*
@@ -51,9 +52,7 @@ int ApplyImagePatch(const unsigned char* old_data, ssize_t old_size,
const Value* patch,
SinkFn sink, void* token, SHA_CTX* ctx,
const Value* bonus_data) {
- ssize_t pos = 12;
- char* header = patch->data;
- if (patch->size < 12) {
+ if (patch->data.size() < 12) {
printf("patch too short to contain header\n");
return -1;
}
@@ -61,6 +60,8 @@ int ApplyImagePatch(const unsigned char* old_data, ssize_t old_size,
// IMGDIFF2 uses CHUNK_NORMAL, CHUNK_DEFLATE, and CHUNK_RAW.
// (IMGDIFF1, which is no longer supported, used CHUNK_NORMAL and
// CHUNK_GZIP.)
+ size_t pos = 12;
+ const char* header = &patch->data[0];
if (memcmp(header, "IMGDIFF2", 8) != 0) {
printf("corrupt patch file header (magic number)\n");
return -1;
@@ -68,20 +69,19 @@ int ApplyImagePatch(const unsigned char* old_data, ssize_t old_size,
int num_chunks = Read4(header+8);
- int i;
- for (i = 0; i < num_chunks; ++i) {
+ for (int i = 0; i < num_chunks; ++i) {
// each chunk's header record starts with 4 bytes.
- if (pos + 4 > patch->size) {
+ if (pos + 4 > patch->data.size()) {
printf("failed to read chunk %d record\n", i);
return -1;
}
- int type = Read4(patch->data + pos);
+ int type = Read4(&patch->data[pos]);
pos += 4;
if (type == CHUNK_NORMAL) {
- char* normal_header = patch->data + pos;
+ const char* normal_header = &patch->data[pos];
pos += 24;
- if (pos > patch->size) {
+ if (pos > patch->data.size()) {
printf("failed to read chunk %d normal header data\n", i);
return -1;
}
@@ -97,21 +97,21 @@ int ApplyImagePatch(const unsigned char* old_data, ssize_t old_size,
ApplyBSDiffPatch(old_data + src_start, src_len,
patch, patch_offset, sink, token, ctx);
} else if (type == CHUNK_RAW) {
- char* raw_header = patch->data + pos;
+ const char* raw_header = &patch->data[pos];
pos += 4;
- if (pos > patch->size) {
+ if (pos > patch->data.size()) {
printf("failed to read chunk %d raw header data\n", i);
return -1;
}
ssize_t data_len = Read4(raw_header);
- if (pos + data_len > patch->size) {
+ if (pos + data_len > patch->data.size()) {
printf("failed to read chunk %d raw data\n", i);
return -1;
}
- if (ctx) SHA1_Update(ctx, patch->data + pos, data_len);
- if (sink((unsigned char*)patch->data + pos,
+ if (ctx) SHA1_Update(ctx, &patch->data[pos], data_len);
+ if (sink(reinterpret_cast<const unsigned char*>(&patch->data[pos]),
data_len, token) != data_len) {
printf("failed to write chunk %d raw data\n", i);
return -1;
@@ -119,9 +119,9 @@ int ApplyImagePatch(const unsigned char* old_data, ssize_t old_size,
pos += data_len;
} else if (type == CHUNK_DEFLATE) {
// deflate chunks have an additional 60 bytes in their chunk header.
- char* deflate_header = patch->data + pos;
+ const char* deflate_header = &patch->data[pos];
pos += 60;
- if (pos > patch->size) {
+ if (pos > patch->data.size()) {
printf("failed to read chunk %d deflate header data\n", i);
return -1;
}
@@ -149,7 +149,7 @@ int ApplyImagePatch(const unsigned char* old_data, ssize_t old_size,
// the patch was constructed with bonus data. The
// deflation will come up 'bonus_size' bytes short; these
// must be appended from the bonus_data value.
- size_t bonus_size = (i == 1 && bonus_data != NULL) ? bonus_data->size : 0;
+ size_t bonus_size = (i == 1 && bonus_data != NULL) ? bonus_data->data.size() : 0;
std::vector<unsigned char> expanded_source(expanded_len);
@@ -189,7 +189,7 @@ int ApplyImagePatch(const unsigned char* old_data, ssize_t old_size,
if (bonus_size) {
memcpy(expanded_source.data() + (expanded_len - bonus_size),
- bonus_data->data, bonus_size);
+ &bonus_data->data[0], bonus_size);
}
}
diff --git a/applypatch/include/applypatch/applypatch.h b/applypatch/include/applypatch/applypatch.h
index 9ee39d293..80d681638 100644
--- a/applypatch/include/applypatch/applypatch.h
+++ b/applypatch/include/applypatch/applypatch.h
@@ -20,6 +20,7 @@
#include <stdint.h>
#include <sys/stat.h>
+#include <string>
#include <vector>
#include "openssl/sha.h"
@@ -52,19 +53,16 @@ int applypatch(const char* source_filename,
const char* target_filename,
const char* target_sha1_str,
size_t target_size,
- int num_patches,
- char** const patch_sha1_str,
+ const std::vector<std::string>& patch_sha1_str,
Value** patch_data,
Value* bonus_data);
int applypatch_check(const char* filename,
- int num_patches,
- char** const patch_sha1_str);
+ const std::vector<std::string>& patch_sha1_str);
int LoadFileContents(const char* filename, FileContents* file);
int SaveFileContents(const char* filename, const FileContents* file);
void FreeFileContents(FileContents* file);
-int FindMatchingPatch(uint8_t* sha1, char* const * const patch_sha1_str,
- int num_patches);
+int FindMatchingPatch(uint8_t* sha1, const std::vector<std::string>& patch_sha1_str);
// bsdiff.cpp
void ShowBSDiffLicense();
diff --git a/applypatch/main.cpp b/applypatch/main.cpp
index 1968ae48f..a3a45d06f 100644
--- a/applypatch/main.cpp
+++ b/applypatch/main.cpp
@@ -20,6 +20,7 @@
#include <unistd.h>
#include <memory>
+#include <string>
#include <vector>
#include "applypatch/applypatch.h"
@@ -30,7 +31,12 @@ static int CheckMode(int argc, char** argv) {
if (argc < 3) {
return 2;
}
- return applypatch_check(argv[2], argc-3, argv+3);
+ std::vector<std::string> sha1;
+ for (int i = 3; i < argc; i++) {
+ sha1.push_back(argv[i]);
+ }
+
+ return applypatch_check(argv[2], sha1);
}
static int SpaceMode(int argc, char** argv) {
@@ -49,11 +55,13 @@ static int SpaceMode(int argc, char** argv) {
// Parse arguments (which should be of the form "<sha1>:<filename>"
// into the new parallel arrays *sha1s and *files.Returns true on
// success.
-static bool ParsePatchArgs(int argc, char** argv, std::vector<char*>* sha1s,
+static bool ParsePatchArgs(int argc, char** argv, std::vector<std::string>* sha1s,
std::vector<FileContents>* files) {
- uint8_t digest[SHA_DIGEST_LENGTH];
-
+ if (sha1s == nullptr) {
+ return false;
+ }
for (int i = 0; i < argc; ++i) {
+ uint8_t digest[SHA_DIGEST_LENGTH];
char* colon = strchr(argv[i], ':');
if (colon == nullptr) {
printf("no ':' in patch argument \"%s\"\n", argv[i]);
@@ -83,18 +91,15 @@ static int FlashMode(const char* src_filename, const char* tgt_filename,
static int PatchMode(int argc, char** argv) {
FileContents bonusFc;
- Value bonusValue;
- Value* bonus = nullptr;
+ Value bonus(VAL_INVALID, "");
if (argc >= 3 && strcmp(argv[1], "-b") == 0) {
if (LoadFileContents(argv[2], &bonusFc) != 0) {
printf("failed to load bonus file %s\n", argv[2]);
return 1;
}
- bonus = &bonusValue;
- bonus->type = VAL_BLOB;
- bonus->size = bonusFc.data.size();
- bonus->data = reinterpret_cast<char*>(bonusFc.data.data());
+ bonus.type = VAL_BLOB;
+ bonus.data = reinterpret_cast<const char*>(bonusFc.data.data());
argc -= 2;
argv += 2;
}
@@ -112,29 +117,26 @@ static int PatchMode(int argc, char** argv) {
// If no <src-sha1>:<patch> is provided, it is in flash mode.
if (argc == 5) {
- if (bonus != nullptr) {
+ if (bonus.type != VAL_INVALID) {
printf("bonus file not supported in flash mode\n");
return 1;
}
return FlashMode(argv[1], argv[2], argv[3], target_size);
}
- std::vector<char*> sha1s;
+ std::vector<std::string> sha1s;
std::vector<FileContents> files;
if (!ParsePatchArgs(argc-5, argv+5, &sha1s, &files)) {
printf("failed to parse patch args\n");
return 1;
}
- std::vector<Value> patches(files.size());
- std::vector<Value*> patch_ptrs(files.size());
+ std::vector<Value> patches;
+ std::vector<Value*> patch_ptrs;
for (size_t i = 0; i < files.size(); ++i) {
- patches[i].type = VAL_BLOB;
- patches[i].size = files[i].data.size();
- patches[i].data = reinterpret_cast<char*>(files[i].data.data());
- patch_ptrs[i] = &patches[i];
+ patches.push_back(Value(VAL_BLOB, reinterpret_cast<const char*>(files[i].data.data())));
+ patch_ptrs.push_back(&patches[i]);
}
return applypatch(argv[1], argv[2], argv[3], target_size,
- patch_ptrs.size(), sha1s.data(),
- patch_ptrs.data(), bonus);
+ sha1s, patch_ptrs.data(), &bonus);
}
// This program applies binary patches to files in a way that is safe
diff --git a/applypatch/utils.cpp b/applypatch/utils.cpp
index fef250f01..450dc8d76 100644
--- a/applypatch/utils.cpp
+++ b/applypatch/utils.cpp
@@ -38,22 +38,22 @@ void Write8(int64_t value, FILE* f) {
fputc((value >> 56) & 0xff, f);
}
-int Read2(void* pv) {
- unsigned char* p = reinterpret_cast<unsigned char*>(pv);
+int Read2(const void* pv) {
+ const unsigned char* p = reinterpret_cast<const unsigned char*>(pv);
return (int)(((unsigned int)p[1] << 8) |
(unsigned int)p[0]);
}
-int Read4(void* pv) {
- unsigned char* p = reinterpret_cast<unsigned char*>(pv);
+int Read4(const void* pv) {
+ const unsigned char* p = reinterpret_cast<const unsigned char*>(pv);
return (int)(((unsigned int)p[3] << 24) |
((unsigned int)p[2] << 16) |
((unsigned int)p[1] << 8) |
(unsigned int)p[0]);
}
-int64_t Read8(void* pv) {
- unsigned char* p = reinterpret_cast<unsigned char*>(pv);
+int64_t Read8(const void* pv) {
+ const unsigned char* p = reinterpret_cast<const unsigned char*>(pv);
return (int64_t)(((uint64_t)p[7] << 56) |
((uint64_t)p[6] << 48) |
((uint64_t)p[5] << 40) |
diff --git a/applypatch/utils.h b/applypatch/utils.h
index 1c34edd97..c7c8e90e2 100644
--- a/applypatch/utils.h
+++ b/applypatch/utils.h
@@ -24,8 +24,8 @@
void Write4(int value, FILE* f);
void Write8(int64_t value, FILE* f);
-int Read2(void* p);
-int Read4(void* p);
-int64_t Read8(void* p);
+int Read2(const void* p);
+int Read4(const void* p);
+int64_t Read8(const void* p);
#endif // _BUILD_TOOLS_APPLYPATCH_UTILS_H