Merge "applypatch: Drop the SHA_CTX parameter in Apply{BSDiff,Image}Patch." am: bcb015239c
am: 4cf3bc1875
Change-Id: I211d0252ae33cc8fc203360de8d982c2a3b597ae
This commit is contained in:
@@ -627,21 +627,20 @@ static int GenerateTarget(const FileContents& source_file, const std::unique_ptr
|
|||||||
|
|
||||||
// We store the decoded output in memory.
|
// We store the decoded output in memory.
|
||||||
std::string memory_sink_str; // Don't need to reserve space.
|
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<const char*>(data), len);
|
memory_sink_str.append(reinterpret_cast<const char*>(data), len);
|
||||||
return len;
|
return len;
|
||||||
};
|
};
|
||||||
|
|
||||||
SHA_CTX ctx;
|
|
||||||
SHA1_Init(&ctx);
|
|
||||||
|
|
||||||
int result;
|
int result;
|
||||||
if (use_bsdiff) {
|
if (use_bsdiff) {
|
||||||
result =
|
result = ApplyBSDiffPatch(source_file.data.data(), source_file.data.size(), *patch, 0, sink);
|
||||||
ApplyBSDiffPatch(source_file.data.data(), source_file.data.size(), *patch, 0, sink, &ctx);
|
|
||||||
} else {
|
} else {
|
||||||
result = ApplyImagePatch(source_file.data.data(), source_file.data.size(), *patch, sink, &ctx,
|
result =
|
||||||
bonus_data);
|
ApplyImagePatch(source_file.data.data(), source_file.data.size(), *patch, sink, bonus_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (result != 0) {
|
if (result != 0) {
|
||||||
|
|||||||
@@ -66,18 +66,12 @@ void ShowBSDiffLicense() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int ApplyBSDiffPatch(const unsigned char* old_data, size_t old_size, const Value& patch,
|
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) {
|
||||||
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;
|
|
||||||
};
|
|
||||||
|
|
||||||
CHECK_LE(patch_offset, patch.data.size());
|
CHECK_LE(patch_offset, patch.data.size());
|
||||||
|
|
||||||
int result = bsdiff::bspatch(old_data, old_size,
|
int result = bsdiff::bspatch(old_data, old_size,
|
||||||
reinterpret_cast<const uint8_t*>(&patch.data[patch_offset]),
|
reinterpret_cast<const uint8_t*>(&patch.data[patch_offset]),
|
||||||
patch.data.size() - patch_offset, sha_sink);
|
patch.data.size() - patch_offset, sink);
|
||||||
if (result != 0) {
|
if (result != 0) {
|
||||||
LOG(ERROR) << "bspatch failed, result: " << result;
|
LOG(ERROR) << "bspatch failed, result: " << result;
|
||||||
// print SHA1 of the patch in the case of a data error.
|
// print SHA1 of the patch in the case of a data error.
|
||||||
|
|||||||
+7
-12
@@ -51,7 +51,7 @@ static inline int32_t Read4(const void *address) {
|
|||||||
// patched data and stream the deflated data to output.
|
// patched data and stream the deflated data to output.
|
||||||
static bool ApplyBSDiffPatchAndStreamOutput(const uint8_t* src_data, size_t src_len,
|
static bool ApplyBSDiffPatchAndStreamOutput(const uint8_t* src_data, size_t src_len,
|
||||||
const Value& patch, size_t patch_offset,
|
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));
|
size_t expected_target_length = static_cast<size_t>(Read8(deflate_header + 32));
|
||||||
int level = Read4(deflate_header + 40);
|
int level = Read4(deflate_header + 40);
|
||||||
int method = Read4(deflate_header + 44);
|
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;
|
size_t total_written = 0;
|
||||||
static constexpr size_t buffer_size = 32768;
|
static constexpr size_t buffer_size = 32768;
|
||||||
auto compression_sink = [&strm, &actual_target_length, &expected_target_length, &total_written,
|
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.
|
// The input patch length for an update never exceeds INT_MAX.
|
||||||
strm.avail_in = len;
|
strm.avail_in = len;
|
||||||
strm.next_in = data;
|
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.";
|
LOG(ERROR) << "Failed to write " << have << " compressed bytes to output.";
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
if (ctx) SHA1_Update(ctx, buffer.data(), have);
|
|
||||||
} while ((strm.avail_in != 0 || strm.avail_out == 0) && ret != Z_STREAM_END);
|
} while ((strm.avail_in != 0 || strm.avail_out == 0) && ret != Z_STREAM_END);
|
||||||
|
|
||||||
actual_target_length += len;
|
actual_target_length += len;
|
||||||
return len;
|
return len;
|
||||||
};
|
};
|
||||||
|
|
||||||
int bspatch_result =
|
int bspatch_result = ApplyBSDiffPatch(src_data, src_len, patch, patch_offset, compression_sink);
|
||||||
ApplyBSDiffPatch(src_data, src_len, patch, patch_offset, compression_sink, nullptr);
|
|
||||||
deflateEnd(&strm);
|
deflateEnd(&strm);
|
||||||
|
|
||||||
if (bspatch_result != 0) {
|
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,
|
int ApplyImagePatch(const unsigned char* old_data, size_t old_size, const unsigned char* patch_data,
|
||||||
size_t patch_size, SinkFn sink) {
|
size_t patch_size, SinkFn sink) {
|
||||||
Value patch(VAL_BLOB, std::string(reinterpret_cast<const char*>(patch_data), patch_size));
|
Value patch(VAL_BLOB, std::string(reinterpret_cast<const char*>(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,
|
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) {
|
if (patch.data.size() < 12) {
|
||||||
printf("patch too short to contain header\n");
|
printf("patch too short to contain header\n");
|
||||||
return -1;
|
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");
|
printf("source data too short\n");
|
||||||
return -1;
|
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");
|
printf("Failed to apply bsdiff patch.\n");
|
||||||
return -1;
|
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);
|
printf("failed to read chunk %d raw data\n", i);
|
||||||
return -1;
|
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) {
|
if (sink(reinterpret_cast<const unsigned char*>(patch_header + pos), data_len) != data_len) {
|
||||||
printf("failed to write chunk %d raw data\n", i);
|
printf("failed to write chunk %d raw data\n", i);
|
||||||
return -1;
|
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,
|
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.";
|
LOG(ERROR) << "Fail to apply streaming bspatch.";
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -63,18 +63,18 @@ int SaveFileContents(const char* filename, const FileContents* file);
|
|||||||
void ShowBSDiffLicense();
|
void ShowBSDiffLicense();
|
||||||
|
|
||||||
// Applies the bsdiff-patch given in 'patch' (from offset 'patch_offset' to the end) to the source
|
// 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
|
// data given by (old_data, old_size). Writes the patched output through the given 'sink'. Returns
|
||||||
// updates the SHA-1 context with the output data. Returns 0 on success.
|
// 0 on success.
|
||||||
int ApplyBSDiffPatch(const unsigned char* old_data, size_t old_size, const Value& patch,
|
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
|
// imgpatch.cpp
|
||||||
|
|
||||||
// Applies the imgdiff-patch given in 'patch' to the source data given by (old_data, old_size), with
|
// 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
|
// the optional bonus data. Writes the patched output through the given 'sink'. Returns 0 on
|
||||||
// SHA-1 context with the output data. Returns 0 on success.
|
// success.
|
||||||
int ApplyImagePatch(const unsigned char* old_data, size_t old_size, const Value& patch, SinkFn sink,
|
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
|
// freecache.cpp
|
||||||
|
|
||||||
|
|||||||
@@ -1429,7 +1429,7 @@ static int PerformCommandDiff(CommandParameters& params) {
|
|||||||
if (ApplyImagePatch(params.buffer.data(), blocks * BLOCKSIZE, patch_value,
|
if (ApplyImagePatch(params.buffer.data(), blocks * BLOCKSIZE, patch_value,
|
||||||
std::bind(&RangeSinkWriter::Write, &writer, std::placeholders::_1,
|
std::bind(&RangeSinkWriter::Write, &writer, std::placeholders::_1,
|
||||||
std::placeholders::_2),
|
std::placeholders::_2),
|
||||||
nullptr, nullptr) != 0) {
|
nullptr) != 0) {
|
||||||
LOG(ERROR) << "Failed to apply image patch.";
|
LOG(ERROR) << "Failed to apply image patch.";
|
||||||
failure_type = kPatchApplicationFailure;
|
failure_type = kPatchApplicationFailure;
|
||||||
return -1;
|
return -1;
|
||||||
@@ -1437,8 +1437,7 @@ static int PerformCommandDiff(CommandParameters& params) {
|
|||||||
} else {
|
} else {
|
||||||
if (ApplyBSDiffPatch(params.buffer.data(), blocks * BLOCKSIZE, patch_value, 0,
|
if (ApplyBSDiffPatch(params.buffer.data(), blocks * BLOCKSIZE, patch_value, 0,
|
||||||
std::bind(&RangeSinkWriter::Write, &writer, std::placeholders::_1,
|
std::bind(&RangeSinkWriter::Write, &writer, std::placeholders::_1,
|
||||||
std::placeholders::_2),
|
std::placeholders::_2)) != 0) {
|
||||||
nullptr) != 0) {
|
|
||||||
LOG(ERROR) << "Failed to apply bsdiff patch.";
|
LOG(ERROR) << "Failed to apply bsdiff patch.";
|
||||||
failure_type = kPatchApplicationFailure;
|
failure_type = kPatchApplicationFailure;
|
||||||
return -1;
|
return -1;
|
||||||
|
|||||||
Reference in New Issue
Block a user