From 8b0b0f1f02f7990177e0da89eb471da2ced13f4c Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Thu, 19 Apr 2018 21:02:13 -0700 Subject: applypatch: Drop the SHA_CTX parameter in Apply{BSDiff,Image}Patch. As they're accepting the SinkFn callback, it makes more sense to leave the work to their callers. Test: mmma -j bootable/recovery Test: Run recovery_component_test on marlin. Test: No other active user of the two functions. Change-Id: I8d67b38ce037925442296f136b483e0c71983777 --- applypatch/applypatch.cpp | 15 +++++++-------- applypatch/bspatch.cpp | 10 ++-------- applypatch/imgpatch.cpp | 19 +++++++------------ applypatch/include/applypatch/applypatch.h | 12 ++++++------ 4 files changed, 22 insertions(+), 34 deletions(-) (limited to 'applypatch') diff --git a/applypatch/applypatch.cpp b/applypatch/applypatch.cpp index 120add6d6..7104abd67 100644 --- a/applypatch/applypatch.cpp +++ b/applypatch/applypatch.cpp @@ -627,21 +627,20 @@ static int GenerateTarget(const FileContents& source_file, const std::unique_ptr // We store the decoded output in memory. std::string memory_sink_str; // Don't need to reserve space. - SinkFn sink = [&memory_sink_str](const unsigned char* data, size_t len) { + SHA_CTX ctx; + SHA1_Init(&ctx); + SinkFn sink = [&memory_sink_str, &ctx](const unsigned char* data, size_t len) { + SHA1_Update(&ctx, data, len); memory_sink_str.append(reinterpret_cast(data), len); return len; }; - SHA_CTX ctx; - SHA1_Init(&ctx); - int result; if (use_bsdiff) { - result = - ApplyBSDiffPatch(source_file.data.data(), source_file.data.size(), *patch, 0, sink, &ctx); + result = ApplyBSDiffPatch(source_file.data.data(), source_file.data.size(), *patch, 0, sink); } else { - result = ApplyImagePatch(source_file.data.data(), source_file.data.size(), *patch, sink, &ctx, - bonus_data); + result = + ApplyImagePatch(source_file.data.data(), source_file.data.size(), *patch, sink, bonus_data); } if (result != 0) { diff --git a/applypatch/bspatch.cpp b/applypatch/bspatch.cpp index 912dbbdd8..ba33c3a9c 100644 --- a/applypatch/bspatch.cpp +++ b/applypatch/bspatch.cpp @@ -66,18 +66,12 @@ void ShowBSDiffLicense() { } int ApplyBSDiffPatch(const unsigned char* old_data, size_t old_size, const Value& patch, - size_t patch_offset, SinkFn sink, SHA_CTX* ctx) { - auto sha_sink = [&sink, &ctx](const uint8_t* data, size_t len) { - len = sink(data, len); - if (ctx) SHA1_Update(ctx, data, len); - return len; - }; - + size_t patch_offset, SinkFn sink) { CHECK_LE(patch_offset, patch.data.size()); int result = bsdiff::bspatch(old_data, old_size, reinterpret_cast(&patch.data[patch_offset]), - patch.data.size() - patch_offset, sha_sink); + patch.data.size() - patch_offset, sink); if (result != 0) { LOG(ERROR) << "bspatch failed, result: " << result; // print SHA1 of the patch in the case of a data error. diff --git a/applypatch/imgpatch.cpp b/applypatch/imgpatch.cpp index 3682d6115..2e4faaadf 100644 --- a/applypatch/imgpatch.cpp +++ b/applypatch/imgpatch.cpp @@ -51,7 +51,7 @@ 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(Read8(deflate_header + 32)); int level = Read4(deflate_header + 40); int method = Read4(deflate_header + 44); @@ -77,7 +77,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 +102,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) { @@ -135,11 +133,11 @@ static bool ApplyBSDiffPatchAndStreamOutput(const uint8_t* src_data, size_t src_ 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(patch_data), patch_size)); - return ApplyImagePatch(old_data, old_size, patch, sink, nullptr, nullptr); + 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,7 +178,7 @@ 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; } @@ -198,9 +196,6 @@ 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(patch_header + pos), data_len) != data_len) { printf("failed to write chunk %d raw data\n", i); return -1; @@ -276,7 +271,7 @@ int ApplyImagePatch(const unsigned char* old_data, size_t old_size, const Value& } 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; } diff --git a/applypatch/include/applypatch/applypatch.h b/applypatch/include/applypatch/applypatch.h index 021a28d05..77125f9de 100644 --- a/applypatch/include/applypatch/applypatch.h +++ b/applypatch/include/applypatch/applypatch.h @@ -63,18 +63,18 @@ int SaveFileContents(const char* filename, const FileContents* file); void ShowBSDiffLicense(); // Applies the bsdiff-patch given in 'patch' (from offset 'patch_offset' to the end) to the source -// data given by (old_data, old_size). Writes the patched output through the given 'sink', and -// updates the SHA-1 context with the output data. Returns 0 on success. +// data given by (old_data, old_size). Writes the patched output through the given 'sink'. Returns +// 0 on success. int ApplyBSDiffPatch(const unsigned char* old_data, size_t old_size, const Value& patch, - size_t patch_offset, SinkFn sink, SHA_CTX* ctx); + size_t patch_offset, SinkFn sink); // imgpatch.cpp // Applies the imgdiff-patch given in 'patch' to the source data given by (old_data, old_size), with -// the optional bonus data. Writes the patched output through the given 'sink', and updates the -// SHA-1 context with the output data. Returns 0 on success. +// the optional bonus data. Writes the patched output through the given 'sink'. Returns 0 on +// success. 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); // freecache.cpp -- cgit v1.2.3