diff options
Diffstat (limited to '')
-rw-r--r-- | applypatch/main.cpp (renamed from applypatch/main.c) | 115 |
1 files changed, 53 insertions, 62 deletions
diff --git a/applypatch/main.c b/applypatch/main.cpp index 8e9fe80ef..7606d5d3c 100644 --- a/applypatch/main.c +++ b/applypatch/main.cpp @@ -19,18 +19,21 @@ #include <string.h> #include <unistd.h> +#include <memory> +#include <vector> + #include "applypatch.h" #include "edify/expr.h" -#include "mincrypt/sha.h" +#include "openssl/sha.h" -int CheckMode(int argc, char** argv) { +static int CheckMode(int argc, char** argv) { if (argc < 3) { return 2; } return applypatch_check(argv[2], argc-3, argv+3); } -int SpaceMode(int argc, char** argv) { +static int SpaceMode(int argc, char** argv) { if (argc != 3) { return 2; } @@ -45,19 +48,13 @@ int SpaceMode(int argc, char** argv) { // Parse arguments (which should be of the form "<sha1>" or // "<sha1>:<filename>" into the new parallel arrays *sha1s and -// *patches (loading file contents into the patches). Returns 0 on +// *patches (loading file contents into the patches). Returns true on // success. -static int ParsePatchArgs(int argc, char** argv, - char*** sha1s, Value*** patches, int* num_patches) { - *num_patches = argc; - *sha1s = malloc(*num_patches * sizeof(char*)); - *patches = malloc(*num_patches * sizeof(Value*)); - memset(*patches, 0, *num_patches * sizeof(Value*)); - - uint8_t digest[SHA_DIGEST_SIZE]; +static bool ParsePatchArgs(int argc, char** argv, std::vector<char*>* sha1s, + std::vector<std::unique_ptr<Value, decltype(&FreeValue)>>* patches) { + uint8_t digest[SHA_DIGEST_LENGTH]; - int i; - for (i = 0; i < *num_patches; ++i) { + for (int i = 0; i < argc; ++i) { char* colon = strchr(argv[i], ':'); if (colon != NULL) { *colon = '\0'; @@ -66,56 +63,49 @@ static int ParsePatchArgs(int argc, char** argv, if (ParseSha1(argv[i], digest) != 0) { printf("failed to parse sha1 \"%s\"\n", argv[i]); - return -1; + return false; } - (*sha1s)[i] = argv[i]; + sha1s->push_back(argv[i]); if (colon == NULL) { - (*patches)[i] = NULL; + patches->emplace_back(nullptr, FreeValue); } else { FileContents fc; if (LoadFileContents(colon, &fc) != 0) { - goto abort; + return false; } - (*patches)[i] = malloc(sizeof(Value)); - (*patches)[i]->type = VAL_BLOB; - (*patches)[i]->size = fc.size; - (*patches)[i]->data = (char*)fc.data; + std::unique_ptr<Value, decltype(&FreeValue)> value(new Value, FreeValue); + value->type = VAL_BLOB; + value->size = fc.size; + value->data = reinterpret_cast<char*>(fc.data); + patches->push_back(std::move(value)); } } + return true; +} - return 0; - - abort: - for (i = 0; i < *num_patches; ++i) { - Value* p = (*patches)[i]; - if (p != NULL) { - free(p->data); - free(p); - } - } - free(*sha1s); - free(*patches); - return -1; +static int FlashMode(const char* src_filename, const char* tgt_filename, + const char* tgt_sha1, size_t tgt_size) { + return applypatch_flash(src_filename, tgt_filename, tgt_sha1, tgt_size); } -int PatchMode(int argc, char** argv) { - Value* bonus = NULL; +static int PatchMode(int argc, char** argv) { + std::unique_ptr<Value, decltype(&FreeValue)> bonus(nullptr, FreeValue); if (argc >= 3 && strcmp(argv[1], "-b") == 0) { FileContents fc; if (LoadFileContents(argv[2], &fc) != 0) { printf("failed to load bonus file %s\n", argv[2]); return 1; } - bonus = malloc(sizeof(Value)); + bonus.reset(new Value); bonus->type = VAL_BLOB; bonus->size = fc.size; - bonus->data = (char*)fc.data; + bonus->data = reinterpret_cast<char*>(fc.data); argc -= 2; argv += 2; } - if (argc < 6) { + if (argc < 4) { return 2; } @@ -126,33 +116,30 @@ int PatchMode(int argc, char** argv) { return 1; } - char** sha1s; - Value** patches; - int num_patches; - if (ParsePatchArgs(argc-5, argv+5, &sha1s, &patches, &num_patches) != 0) { - printf("failed to parse patch args\n"); - return 1; + // If no <src-sha1>:<patch> is provided, it is in flash mode. + if (argc == 5) { + if (bonus != NULL) { + printf("bonus file not supported in flash mode\n"); + return 1; + } + return FlashMode(argv[1], argv[2], argv[3], target_size); } - int result = applypatch(argv[1], argv[2], argv[3], target_size, - num_patches, sha1s, patches, bonus); - int i; - for (i = 0; i < num_patches; ++i) { - Value* p = patches[i]; - if (p != NULL) { - free(p->data); - free(p); - } - } - if (bonus) { - free(bonus->data); - free(bonus); + std::vector<char*> sha1s; + std::vector<std::unique_ptr<Value, decltype(&FreeValue)>> patches; + if (!ParsePatchArgs(argc-5, argv+5, &sha1s, &patches)) { + printf("failed to parse patch args\n"); + return 1; } - free(sha1s); - free(patches); - return result; + std::vector<Value*> patch_ptrs; + for (const auto& p : patches) { + patch_ptrs.push_back(p.get()); + } + return applypatch(argv[1], argv[2], argv[3], target_size, + patch_ptrs.size(), sha1s.data(), + patch_ptrs.data(), bonus.get()); } // This program applies binary patches to files in a way that is safe @@ -163,6 +150,10 @@ int PatchMode(int argc, char** argv) { // - if the sha1 hash of <tgt-file> is <tgt-sha1>, does nothing and exits // successfully. // +// - otherwise, if no <src-sha1>:<patch> is provided, flashes <tgt-file> with +// <src-file>. <tgt-file> must be a partition name, while <src-file> must +// be a regular image file. <src-file> will not be deleted on success. +// // - otherwise, if the sha1 hash of <src-file> is <src-sha1>, applies the // bsdiff <patch> to <src-file> to produce a new file (the type of patch // is automatically detected from the file header). If that new |