diff options
Diffstat (limited to '')
-rw-r--r-- | applypatch/applypatch_modes.cpp (renamed from applypatch/main.cpp) | 104 |
1 files changed, 54 insertions, 50 deletions
diff --git a/applypatch/main.cpp b/applypatch/applypatch_modes.cpp index 9013760c4..7b191a801 100644 --- a/applypatch/main.cpp +++ b/applypatch/applypatch_modes.cpp @@ -14,61 +14,72 @@ * limitations under the License. */ +#include "applypatch_modes.h" + #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <memory> +#include <string> #include <vector> -#include "applypatch.h" +#include <android-base/parseint.h> +#include <android-base/strings.h> +#include <openssl/sha.h> + +#include "applypatch/applypatch.h" #include "edify/expr.h" -#include "openssl/sha.h" -static int CheckMode(int argc, char** argv) { +static int CheckMode(int argc, const char** argv) { if (argc < 3) { return 2; } - return applypatch_check(argv[2], argc-3, argv+3); + std::vector<std::string> sha1; + for (int i = 3; i < argc; i++) { + sha1.push_back(argv[i]); + } + + return applypatch_check(argv[2], sha1); } -static int SpaceMode(int argc, char** argv) { +static int SpaceMode(int argc, const char** argv) { if (argc != 3) { return 2; } - char* endptr; - size_t bytes = strtol(argv[2], &endptr, 10); - if (bytes == 0 && endptr == argv[2]) { + + size_t bytes; + if (!android::base::ParseUint(argv[2], &bytes) || bytes == 0) { printf("can't parse \"%s\" as byte count\n\n", argv[2]); return 1; } return CacheSizeCheck(bytes); } -// Parse arguments (which should be of the form "<sha1>:<filename>" -// into the new parallel arrays *sha1s and *files.Returns true on -// success. -static bool ParsePatchArgs(int argc, char** argv, std::vector<char*>* sha1s, +// Parse arguments (which should be of the form "<sha1>:<filename>" into the +// new parallel arrays *sha1s and *files. Returns true on success. +static bool ParsePatchArgs(int argc, const char** argv, std::vector<std::string>* sha1s, std::vector<FileContents>* files) { - uint8_t digest[SHA_DIGEST_LENGTH]; - + if (sha1s == nullptr) { + return false; + } for (int i = 0; i < argc; ++i) { - char* colon = strchr(argv[i], ':'); - if (colon == nullptr) { - printf("no ':' in patch argument \"%s\"\n", argv[i]); + std::vector<std::string> pieces = android::base::Split(argv[i], ":"); + if (pieces.size() != 2) { + printf("failed to parse patch argument \"%s\"\n", argv[i]); return false; } - *colon = '\0'; - ++colon; - if (ParseSha1(argv[i], digest) != 0) { + + uint8_t digest[SHA_DIGEST_LENGTH]; + if (ParseSha1(pieces[0].c_str(), digest) != 0) { printf("failed to parse sha1 \"%s\"\n", argv[i]); return false; } - sha1s->push_back(argv[i]); + sha1s->push_back(pieces[0]); FileContents fc; - if (LoadFileContents(colon, &fc) != 0) { + if (LoadFileContents(pieces[1].c_str(), &fc) != 0) { return false; } files->push_back(std::move(fc)); @@ -81,20 +92,17 @@ static int FlashMode(const char* src_filename, const char* tgt_filename, return applypatch_flash(src_filename, tgt_filename, tgt_sha1, tgt_size); } -static int PatchMode(int argc, char** argv) { +static int PatchMode(int argc, const char** argv) { FileContents bonusFc; - Value bonusValue; - Value* bonus = nullptr; + Value bonus(VAL_INVALID, ""); if (argc >= 3 && strcmp(argv[1], "-b") == 0) { if (LoadFileContents(argv[2], &bonusFc) != 0) { printf("failed to load bonus file %s\n", argv[2]); return 1; } - bonus = &bonusValue; - bonus->type = VAL_BLOB; - bonus->size = bonusFc.data.size(); - bonus->data = reinterpret_cast<char*>(bonusFc.data.data()); + bonus.type = VAL_BLOB; + bonus.data = std::string(bonusFc.data.cbegin(), bonusFc.data.cend()); argc -= 2; argv += 2; } @@ -103,42 +111,38 @@ static int PatchMode(int argc, char** argv) { return 2; } - char* endptr; - size_t target_size = strtol(argv[4], &endptr, 10); - if (target_size == 0 && endptr == argv[4]) { + size_t target_size; + if (!android::base::ParseUint(argv[4], &target_size) || target_size == 0) { printf("can't parse \"%s\" as byte count\n\n", argv[4]); return 1; } // If no <src-sha1>:<patch> is provided, it is in flash mode. if (argc == 5) { - if (bonus != nullptr) { + if (bonus.type != VAL_INVALID) { printf("bonus file not supported in flash mode\n"); return 1; } return FlashMode(argv[1], argv[2], argv[3], target_size); } - std::vector<char*> sha1s; + + std::vector<std::string> sha1s; std::vector<FileContents> files; if (!ParsePatchArgs(argc-5, argv+5, &sha1s, &files)) { printf("failed to parse patch args\n"); return 1; } - std::vector<Value> patches(files.size()); - std::vector<Value*> patch_ptrs(files.size()); + + std::vector<std::unique_ptr<Value>> patches; for (size_t i = 0; i < files.size(); ++i) { - patches[i].type = VAL_BLOB; - patches[i].size = files[i].data.size(); - patches[i].data = reinterpret_cast<char*>(files[i].data.data()); - patch_ptrs[i] = &patches[i]; + patches.push_back(std::make_unique<Value>( + VAL_BLOB, std::string(files[i].data.cbegin(), files[i].data.cend()))); } - return applypatch(argv[1], argv[2], argv[3], target_size, - patch_ptrs.size(), sha1s.data(), - patch_ptrs.data(), bonus); + return applypatch(argv[1], argv[2], argv[3], target_size, sha1s, patches, &bonus); } -// This program applies binary patches to files in a way that is safe -// (the original file is not touched until we have the desired +// This program (applypatch) applies binary patches to files in a way that +// is safe (the original file is not touched until we have the desired // replacement for it) and idempotent (it's okay to run this program // multiple times). // @@ -160,11 +164,11 @@ static int PatchMode(int argc, char** argv) { // - otherwise, or if any error is encountered, exits with non-zero // status. // -// <src-file> (or <file> in check mode) may refer to an MTD partition +// <src-file> (or <file> in check mode) may refer to an EMMC partition // to read the source data. See the comments for the -// LoadMTDContents() function above for the format of such a filename. +// LoadPartitionContents() function for the format of such a filename. -int main(int argc, char** argv) { +int applypatch_modes(int argc, const char** argv) { if (argc < 2) { usage: printf( @@ -175,8 +179,8 @@ int main(int argc, char** argv) { " or %s -l\n" "\n" "Filenames may be of the form\n" - " MTD:<partition>:<len_1>:<sha1_1>:<len_2>:<sha1_2>:...\n" - "to specify reading from or writing to an MTD partition.\n\n", + " EMMC:<partition>:<len_1>:<sha1_1>:<len_2>:<sha1_2>:...\n" + "to specify reading from or writing to an EMMC partition.\n\n", argv[0], argv[0], argv[0], argv[0]); return 2; } |