summaryrefslogtreecommitdiffstats
path: root/applypatch/imgpatch.cpp
diff options
context:
space:
mode:
authorTao Bao <tbao@google.com>2016-06-14 03:29:09 +0200
committerandroid-build-merger <android-build-merger@google.com>2016-06-14 03:29:09 +0200
commit2a4386fa7bd7275ecdb1ca218873a8aeb5bccaca (patch)
treeb2291966128c5882d09faf4b1792e1c655b9f910 /applypatch/imgpatch.cpp
parentMerge "recovery: Track the name change for wipe_ab." into nyc-mr1-dev (diff)
parentapplypatch: Don't call inflate() when it expects zero-length output. (diff)
downloadandroid_bootable_recovery-2a4386fa7bd7275ecdb1ca218873a8aeb5bccaca.tar
android_bootable_recovery-2a4386fa7bd7275ecdb1ca218873a8aeb5bccaca.tar.gz
android_bootable_recovery-2a4386fa7bd7275ecdb1ca218873a8aeb5bccaca.tar.bz2
android_bootable_recovery-2a4386fa7bd7275ecdb1ca218873a8aeb5bccaca.tar.lz
android_bootable_recovery-2a4386fa7bd7275ecdb1ca218873a8aeb5bccaca.tar.xz
android_bootable_recovery-2a4386fa7bd7275ecdb1ca218873a8aeb5bccaca.tar.zst
android_bootable_recovery-2a4386fa7bd7275ecdb1ca218873a8aeb5bccaca.zip
Diffstat (limited to 'applypatch/imgpatch.cpp')
-rw-r--r--applypatch/imgpatch.cpp123
1 files changed, 66 insertions, 57 deletions
diff --git a/applypatch/imgpatch.cpp b/applypatch/imgpatch.cpp
index 8824038ea..d175d6385 100644
--- a/applypatch/imgpatch.cpp
+++ b/applypatch/imgpatch.cpp
@@ -151,40 +151,45 @@ int ApplyImagePatch(const unsigned char* old_data, ssize_t old_size,
size_t bonus_size = (i == 1 && bonus_data != NULL) ? bonus_data->size : 0;
std::vector<unsigned char> expanded_source(expanded_len);
- z_stream strm;
- strm.zalloc = Z_NULL;
- strm.zfree = Z_NULL;
- strm.opaque = Z_NULL;
- strm.avail_in = src_len;
- strm.next_in = (unsigned char*)(old_data + src_start);
- strm.avail_out = expanded_len;
- strm.next_out = expanded_source.data();
-
- int ret;
- ret = inflateInit2(&strm, -15);
- if (ret != Z_OK) {
- printf("failed to init source inflation: %d\n", ret);
- return -1;
- }
- // Because we've provided enough room to accommodate the output
- // data, we expect one call to inflate() to suffice.
- ret = inflate(&strm, Z_SYNC_FLUSH);
- if (ret != Z_STREAM_END) {
- printf("source inflation returned %d\n", ret);
- return -1;
- }
- // We should have filled the output buffer exactly, except
- // for the bonus_size.
- if (strm.avail_out != bonus_size) {
- printf("source inflation short by %zu bytes\n", strm.avail_out-bonus_size);
- return -1;
- }
- inflateEnd(&strm);
+ // inflate() doesn't like strm.next_out being a nullptr even with
+ // avail_out being zero (Z_STREAM_ERROR).
+ if (expanded_len != 0) {
+ z_stream strm;
+ strm.zalloc = Z_NULL;
+ strm.zfree = Z_NULL;
+ strm.opaque = Z_NULL;
+ strm.avail_in = src_len;
+ strm.next_in = (unsigned char*)(old_data + src_start);
+ strm.avail_out = expanded_len;
+ strm.next_out = expanded_source.data();
+
+ int ret;
+ ret = inflateInit2(&strm, -15);
+ if (ret != Z_OK) {
+ printf("failed to init source inflation: %d\n", ret);
+ return -1;
+ }
+
+ // Because we've provided enough room to accommodate the output
+ // data, we expect one call to inflate() to suffice.
+ ret = inflate(&strm, Z_SYNC_FLUSH);
+ if (ret != Z_STREAM_END) {
+ printf("source inflation returned %d\n", ret);
+ return -1;
+ }
+ // We should have filled the output buffer exactly, except
+ // for the bonus_size.
+ if (strm.avail_out != bonus_size) {
+ printf("source inflation short by %zu bytes\n", strm.avail_out-bonus_size);
+ return -1;
+ }
+ inflateEnd(&strm);
- if (bonus_size) {
- memcpy(expanded_source.data() + (expanded_len - bonus_size),
- bonus_data->data, bonus_size);
+ if (bonus_size) {
+ memcpy(expanded_source.data() + (expanded_len - bonus_size),
+ bonus_data->data, bonus_size);
+ }
}
// Next, apply the bsdiff patch (in memory) to the uncompressed
@@ -203,33 +208,37 @@ int ApplyImagePatch(const unsigned char* old_data, ssize_t old_size,
if (expanded_source.size() < 32768U) {
expanded_source.resize(32768U);
}
- std::vector<unsigned char>& temp_data = expanded_source;
-
- // now the deflate stream
- strm.zalloc = Z_NULL;
- strm.zfree = Z_NULL;
- strm.opaque = Z_NULL;
- strm.avail_in = uncompressed_target_data.size();
- strm.next_in = uncompressed_target_data.data();
- ret = deflateInit2(&strm, level, method, windowBits, memLevel, strategy);
- if (ret != Z_OK) {
- printf("failed to init uncompressed data deflation: %d\n", ret);
- return -1;
- }
- do {
- strm.avail_out = temp_data.size();
- strm.next_out = temp_data.data();
- ret = deflate(&strm, Z_FINISH);
- ssize_t have = temp_data.size() - strm.avail_out;
-
- if (sink(temp_data.data(), have, token) != have) {
- printf("failed to write %ld compressed bytes to output\n",
- (long)have);
+
+ {
+ std::vector<unsigned char>& temp_data = expanded_source;
+
+ // now the deflate stream
+ z_stream strm;
+ strm.zalloc = Z_NULL;
+ strm.zfree = Z_NULL;
+ strm.opaque = Z_NULL;
+ strm.avail_in = uncompressed_target_data.size();
+ strm.next_in = uncompressed_target_data.data();
+ int ret = deflateInit2(&strm, level, method, windowBits, memLevel, strategy);
+ if (ret != Z_OK) {
+ printf("failed to init uncompressed data deflation: %d\n", ret);
return -1;
}
- if (ctx) SHA1_Update(ctx, temp_data.data(), have);
- } while (ret != Z_STREAM_END);
- deflateEnd(&strm);
+ do {
+ strm.avail_out = temp_data.size();
+ strm.next_out = temp_data.data();
+ ret = deflate(&strm, Z_FINISH);
+ ssize_t have = temp_data.size() - strm.avail_out;
+
+ if (sink(temp_data.data(), have, token) != have) {
+ printf("failed to write %ld compressed bytes to output\n",
+ (long)have);
+ return -1;
+ }
+ if (ctx) SHA1_Update(ctx, temp_data.data(), have);
+ } while (ret != Z_STREAM_END);
+ deflateEnd(&strm);
+ }
} else {
printf("patch chunk %d is unknown type %d\n", i, type);
return -1;