diff options
Diffstat (limited to 'applypatch/imgpatch.cpp')
-rw-r--r-- | applypatch/imgpatch.cpp | 42 |
1 files changed, 22 insertions, 20 deletions
diff --git a/applypatch/imgpatch.cpp b/applypatch/imgpatch.cpp index 3682d6115..e6be39a2f 100644 --- a/applypatch/imgpatch.cpp +++ b/applypatch/imgpatch.cpp @@ -38,6 +38,7 @@ #include <zlib.h> #include "edify/expr.h" +#include "otautil/print_sha1.h" static inline int64_t Read8(const void *address) { return android::base::get_unaligned<int64_t>(address); @@ -51,8 +52,9 @@ static inline int32_t Read4(const void *address) { // patched data and stream the deflated data to output. static bool ApplyBSDiffPatchAndStreamOutput(const uint8_t* src_data, size_t src_len, const Value& patch, size_t patch_offset, - const char* deflate_header, SinkFn sink, SHA_CTX* ctx) { + const char* deflate_header, SinkFn sink) { size_t expected_target_length = static_cast<size_t>(Read8(deflate_header + 32)); + CHECK_GT(expected_target_length, 0); int level = Read4(deflate_header + 40); int method = Read4(deflate_header + 44); int window_bits = Read4(deflate_header + 48); @@ -77,7 +79,7 @@ static bool ApplyBSDiffPatchAndStreamOutput(const uint8_t* src_data, size_t src_ size_t total_written = 0; static constexpr size_t buffer_size = 32768; auto compression_sink = [&strm, &actual_target_length, &expected_target_length, &total_written, - &ret, &ctx, &sink](const uint8_t* data, size_t len) -> size_t { + &ret, &sink](const uint8_t* data, size_t len) -> size_t { // The input patch length for an update never exceeds INT_MAX. strm.avail_in = len; strm.next_in = data; @@ -102,15 +104,13 @@ static bool ApplyBSDiffPatchAndStreamOutput(const uint8_t* src_data, size_t src_ LOG(ERROR) << "Failed to write " << have << " compressed bytes to output."; return 0; } - if (ctx) SHA1_Update(ctx, buffer.data(), have); } while ((strm.avail_in != 0 || strm.avail_out == 0) && ret != Z_STREAM_END); actual_target_length += len; return len; }; - int bspatch_result = - ApplyBSDiffPatch(src_data, src_len, patch, patch_offset, compression_sink, nullptr); + int bspatch_result = ApplyBSDiffPatch(src_data, src_len, patch, patch_offset, compression_sink); deflateEnd(&strm); if (bspatch_result != 0) { @@ -127,19 +127,20 @@ static bool ApplyBSDiffPatchAndStreamOutput(const uint8_t* src_data, size_t src_ << actual_target_length; return false; } - LOG(DEBUG) << "bspatch writes " << total_written << " bytes in total to streaming output."; + LOG(DEBUG) << "bspatch wrote " << total_written << " bytes in total to streaming output."; return true; } int ApplyImagePatch(const unsigned char* old_data, size_t old_size, const unsigned char* patch_data, size_t patch_size, SinkFn sink) { - Value patch(VAL_BLOB, std::string(reinterpret_cast<const char*>(patch_data), patch_size)); - return ApplyImagePatch(old_data, old_size, patch, sink, nullptr, nullptr); + Value patch(Value::Type::BLOB, + std::string(reinterpret_cast<const char*>(patch_data), patch_size)); + return ApplyImagePatch(old_data, old_size, patch, sink, nullptr); } int ApplyImagePatch(const unsigned char* old_data, size_t old_size, const Value& patch, SinkFn sink, - SHA_CTX* ctx, const Value* bonus_data) { + const Value* bonus_data) { if (patch.data.size() < 12) { printf("patch too short to contain header\n"); return -1; @@ -180,10 +181,12 @@ int ApplyImagePatch(const unsigned char* old_data, size_t old_size, const Value& printf("source data too short\n"); return -1; } - if (ApplyBSDiffPatch(old_data + src_start, src_len, patch, patch_offset, sink, ctx) != 0) { + if (ApplyBSDiffPatch(old_data + src_start, src_len, patch, patch_offset, sink) != 0) { printf("Failed to apply bsdiff patch.\n"); return -1; } + + LOG(DEBUG) << "Processed chunk type normal"; } else if (type == CHUNK_RAW) { const char* raw_header = patch_header + pos; pos += 4; @@ -198,14 +201,13 @@ int ApplyImagePatch(const unsigned char* old_data, size_t old_size, const Value& printf("failed to read chunk %d raw data\n", i); return -1; } - if (ctx) { - SHA1_Update(ctx, patch_header + pos, data_len); - } if (sink(reinterpret_cast<const unsigned char*>(patch_header + pos), data_len) != data_len) { printf("failed to write chunk %d raw data\n", i); return -1; } pos += data_len; + + LOG(DEBUG) << "Processed chunk type raw"; } else if (type == CHUNK_DEFLATE) { // deflate chunks have an additional 60 bytes in their chunk header. const char* deflate_header = patch_header + pos; @@ -228,11 +230,10 @@ int ApplyImagePatch(const unsigned char* old_data, size_t old_size, const Value& // Decompress the source data; the chunk header tells us exactly // how big we expect it to be when decompressed. - // Note: expanded_len will include the bonus data size if - // 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->data.size() : 0; + // Note: expanded_len will include the bonus data size if 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 != nullptr) ? bonus_data->data.size() : 0; std::vector<unsigned char> expanded_source(expanded_len); @@ -270,17 +271,18 @@ int ApplyImagePatch(const unsigned char* old_data, size_t old_size, const Value& inflateEnd(&strm); if (bonus_size) { - memcpy(expanded_source.data() + (expanded_len - bonus_size), &bonus_data->data[0], + memcpy(expanded_source.data() + (expanded_len - bonus_size), bonus_data->data.data(), bonus_size); } } if (!ApplyBSDiffPatchAndStreamOutput(expanded_source.data(), expanded_len, patch, - patch_offset, deflate_header, sink, ctx)) { + patch_offset, deflate_header, sink)) { LOG(ERROR) << "Fail to apply streaming bspatch."; return -1; } + LOG(DEBUG) << "Processed chunk type deflate"; } else { printf("patch chunk %d is unknown type %d\n", i, type); return -1; |