diff options
Diffstat (limited to 'applypatch')
-rw-r--r-- | applypatch/applypatch.cpp | 24 | ||||
-rw-r--r-- | applypatch/bspatch.cpp | 8 | ||||
-rw-r--r-- | applypatch/imgpatch.cpp | 13 | ||||
-rw-r--r-- | applypatch/include/applypatch/applypatch.h | 7 | ||||
-rw-r--r-- | applypatch/include/applypatch/imgpatch.h | 6 |
5 files changed, 28 insertions, 30 deletions
diff --git a/applypatch/applypatch.cpp b/applypatch/applypatch.cpp index 06b8e3120..51bf3932a 100644 --- a/applypatch/applypatch.cpp +++ b/applypatch/applypatch.cpp @@ -27,6 +27,7 @@ #include <sys/types.h> #include <unistd.h> +#include <functional> #include <memory> #include <string> #include <utility> @@ -42,7 +43,7 @@ #include "print_sha1.h" static int LoadPartitionContents(const std::string& filename, FileContents* file); -static size_t FileSink(const unsigned char* data, size_t len, void* token); +static size_t FileSink(const unsigned char* data, size_t len, int fd); static int GenerateTarget(const FileContents& source_file, const std::unique_ptr<Value>& patch, const std::string& target_filename, const uint8_t target_sha1[SHA_DIGEST_LENGTH], const Value* bonus_data); @@ -194,7 +195,7 @@ int SaveFileContents(const char* filename, const FileContents* file) { return -1; } - size_t bytes_written = FileSink(file->data.data(), file->data.size(), &fd); + size_t bytes_written = FileSink(file->data.data(), file->data.size(), fd); if (bytes_written != file->data.size()) { printf("short write of \"%s\" (%zd bytes of %zu): %s\n", filename, bytes_written, file->data.size(), strerror(errno)); @@ -433,8 +434,7 @@ int ShowLicenses() { return 0; } -static size_t FileSink(const unsigned char* data, size_t len, void* token) { - int fd = *static_cast<int*>(token); +static size_t FileSink(const unsigned char* data, size_t len, int fd) { size_t done = 0; while (done < len) { ssize_t wrote = TEMP_FAILURE_RETRY(ota_write(fd, data + done, len - done)); @@ -447,12 +447,6 @@ static size_t FileSink(const unsigned char* data, size_t len, void* token) { return done; } -size_t MemorySink(const unsigned char* data, size_t len, void* token) { - std::string* s = static_cast<std::string*>(token); - s->append(reinterpret_cast<const char*>(data), len); - return len; -} - // Return the amount of free space (in bytes) on the filesystem // containing filename. filename must exist. Return -1 on error. size_t FreeSpaceForFile(const char* filename) { @@ -646,9 +640,11 @@ static int GenerateTarget(const FileContents& source_file, const std::unique_ptr } // We store the decoded output in memory. - SinkFn sink = MemorySink; std::string memory_sink_str; // Don't need to reserve space. - void* token = &memory_sink_str; + SinkFn sink = [&memory_sink_str](const unsigned char* data, size_t len) { + memory_sink_str.append(reinterpret_cast<const char*>(data), len); + return len; + }; SHA_CTX ctx; SHA1_Init(&ctx); @@ -656,10 +652,10 @@ static int GenerateTarget(const FileContents& source_file, const std::unique_ptr int result; if (use_bsdiff) { result = ApplyBSDiffPatch(source_file.data.data(), source_file.data.size(), patch.get(), 0, - sink, token, &ctx); + sink, &ctx); } else { result = ApplyImagePatch(source_file.data.data(), source_file.data.size(), patch.get(), sink, - token, &ctx, bonus_data); + &ctx, bonus_data); } if (result != 0) { diff --git a/applypatch/bspatch.cpp b/applypatch/bspatch.cpp index 8ef7a068d..f75a2c680 100644 --- a/applypatch/bspatch.cpp +++ b/applypatch/bspatch.cpp @@ -24,9 +24,9 @@ #include <sys/types.h> #include <bspatch.h> +#include <openssl/sha.h> #include "applypatch/applypatch.h" -#include "openssl/sha.h" void ShowBSDiffLicense() { puts("The bsdiff library used herein is:\n" @@ -61,9 +61,9 @@ void ShowBSDiffLicense() { } int ApplyBSDiffPatch(const unsigned char* old_data, size_t old_size, const Value* patch, - size_t patch_offset, SinkFn sink, void* token, SHA_CTX* ctx) { - auto sha_sink = [&](const uint8_t* data, size_t len) { - len = sink(data, len, token); + 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; }; diff --git a/applypatch/imgpatch.cpp b/applypatch/imgpatch.cpp index 7a88ffbbc..7d8b7361c 100644 --- a/applypatch/imgpatch.cpp +++ b/applypatch/imgpatch.cpp @@ -44,10 +44,10 @@ static inline int32_t Read4(const void *address) { } int ApplyImagePatch(const unsigned char* old_data, size_t old_size, const unsigned char* patch_data, - size_t patch_size, SinkFn sink, void* token) { + 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, token, nullptr, nullptr); + return ApplyImagePatch(old_data, old_size, &patch, sink, nullptr, nullptr); } /* @@ -57,7 +57,7 @@ int ApplyImagePatch(const unsigned char* old_data, size_t old_size, const unsign * Return 0 on success. */ int ApplyImagePatch(const unsigned char* old_data, size_t old_size, const Value* patch, SinkFn sink, - void* token, SHA_CTX* ctx, const Value* bonus_data) { + SHA_CTX* ctx, const Value* bonus_data) { if (patch->data.size() < 12) { printf("patch too short to contain header\n"); return -1; @@ -100,7 +100,7 @@ int ApplyImagePatch(const unsigned char* old_data, size_t old_size, const Value* printf("source data too short\n"); return -1; } - ApplyBSDiffPatch(old_data + src_start, src_len, patch, patch_offset, sink, token, ctx); + ApplyBSDiffPatch(old_data + src_start, src_len, patch, patch_offset, sink, ctx); } else if (type == CHUNK_RAW) { const char* raw_header = &patch->data[pos]; pos += 4; @@ -116,8 +116,7 @@ int ApplyImagePatch(const unsigned char* old_data, size_t old_size, const Value* return -1; } 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) { + if (sink(reinterpret_cast<const unsigned char*>(&patch->data[pos]), data_len) != data_len) { printf("failed to write chunk %d raw data\n", i); return -1; } @@ -241,7 +240,7 @@ int ApplyImagePatch(const unsigned char* old_data, size_t old_size, const Value* ret = deflate(&strm, Z_FINISH); size_t have = temp_data.size() - strm.avail_out; - if (sink(temp_data.data(), have, token) != have) { + if (sink(temp_data.data(), have) != have) { printf("failed to write %zd compressed bytes to output\n", have); return -1; } diff --git a/applypatch/include/applypatch/applypatch.h b/applypatch/include/applypatch/applypatch.h index 52fb58287..da55432d5 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 <functional> #include <memory> #include <string> #include <vector> @@ -41,7 +42,7 @@ struct FileContents { // and use it as the source instead. #define CACHE_TEMP_SOURCE "/cache/saved.file" -using SinkFn = size_t (*)(const unsigned char*, size_t, void*); +using SinkFn = std::function<size_t(const unsigned char*, size_t)>; // applypatch.cpp int ShowLicenses(); @@ -67,13 +68,13 @@ int SaveFileContents(const char* filename, const FileContents* file); // bspatch.cpp void ShowBSDiffLicense(); int ApplyBSDiffPatch(const unsigned char* old_data, size_t old_size, const Value* patch, - size_t patch_offset, SinkFn sink, void* token, SHA_CTX* ctx); + size_t patch_offset, SinkFn sink, SHA_CTX* ctx); int ApplyBSDiffPatchMem(const unsigned char* old_data, size_t old_size, const Value* patch, size_t patch_offset, std::vector<unsigned char>* new_data); // imgpatch.cpp int ApplyImagePatch(const unsigned char* old_data, size_t old_size, const Value* patch, SinkFn sink, - void* token, SHA_CTX* ctx, const Value* bonus_data); + SHA_CTX* ctx, const Value* bonus_data); // freecache.cpp int MakeFreeSpaceOnCache(size_t bytes_needed); diff --git a/applypatch/include/applypatch/imgpatch.h b/applypatch/include/applypatch/imgpatch.h index 16e4c4f57..07c66094f 100644 --- a/applypatch/include/applypatch/imgpatch.h +++ b/applypatch/include/applypatch/imgpatch.h @@ -19,9 +19,11 @@ #include <sys/types.h> -using SinkFn = size_t (*)(const unsigned char*, size_t, void*); +#include <functional> + +using SinkFn = std::function<size_t(const unsigned char*, size_t)>; int ApplyImagePatch(const unsigned char* old_data, size_t old_size, const unsigned char* patch_data, - size_t patch_size, SinkFn sink, void* token); + size_t patch_size, SinkFn sink); #endif // _APPLYPATCH_IMGPATCH_H |