summaryrefslogtreecommitdiffstats
path: root/applypatch
diff options
context:
space:
mode:
Diffstat (limited to 'applypatch')
-rw-r--r--applypatch/Android.mk103
-rw-r--r--applypatch/Makefile33
-rw-r--r--applypatch/applypatch.cpp1201
-rw-r--r--applypatch/applypatch_main.cpp28
-rw-r--r--applypatch/applypatch_modes.cpp (renamed from applypatch/main.cpp)104
-rw-r--r--applypatch/applypatch_modes.h22
-rw-r--r--applypatch/bsdiff.cpp410
-rw-r--r--applypatch/bspatch.cpp15
-rw-r--r--applypatch/freecache.cpp2
-rw-r--r--applypatch/imgdiff.cpp93
-rw-r--r--applypatch/imgpatch.cpp56
-rw-r--r--applypatch/include/applypatch/applypatch.h (renamed from applypatch/applypatch.h)26
-rw-r--r--applypatch/libimgpatch.pc6
-rw-r--r--applypatch/utils.cpp30
-rw-r--r--applypatch/utils.h9
15 files changed, 868 insertions, 1270 deletions
diff --git a/applypatch/Android.mk b/applypatch/Android.mk
index 887a570db..fa0fe8a37 100644
--- a/applypatch/Android.mk
+++ b/applypatch/Android.mk
@@ -14,61 +14,110 @@
LOCAL_PATH := $(call my-dir)
+# libapplypatch (static library)
+# ===============================
include $(CLEAR_VARS)
-
LOCAL_CLANG := true
-LOCAL_SRC_FILES := applypatch.cpp bspatch.cpp freecache.cpp imgpatch.cpp utils.cpp
+LOCAL_SRC_FILES := \
+ applypatch.cpp \
+ bspatch.cpp \
+ freecache.cpp \
+ imgpatch.cpp \
+ utils.cpp
LOCAL_MODULE := libapplypatch
LOCAL_MODULE_TAGS := eng
-LOCAL_C_INCLUDES += bootable/recovery
-LOCAL_STATIC_LIBRARIES += libbase libotafault libmtdutils libcrypto_static libbz libz
-
+LOCAL_C_INCLUDES += \
+ $(LOCAL_PATH)/include \
+ bootable/recovery
+LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
+LOCAL_STATIC_LIBRARIES += \
+ libotafault \
+ libbase \
+ libcrypto \
+ libbz \
+ libz
+LOCAL_CFLAGS := -Werror
include $(BUILD_STATIC_LIBRARY)
+# libimgpatch (static library)
+# ===============================
include $(CLEAR_VARS)
-
LOCAL_CLANG := true
LOCAL_SRC_FILES := bspatch.cpp imgpatch.cpp utils.cpp
LOCAL_MODULE := libimgpatch
-LOCAL_C_INCLUDES += bootable/recovery
+LOCAL_C_INCLUDES += \
+ $(LOCAL_PATH)/include \
+ bootable/recovery
LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
-LOCAL_STATIC_LIBRARIES += libcrypto_static libbz libz
-
+LOCAL_STATIC_LIBRARIES += libcrypto libbz libz
+LOCAL_CFLAGS := -Werror
include $(BUILD_STATIC_LIBRARY)
-ifeq ($(HOST_OS),linux)
+# libimgpatch (host static library)
+# ===============================
include $(CLEAR_VARS)
-
LOCAL_CLANG := true
LOCAL_SRC_FILES := bspatch.cpp imgpatch.cpp utils.cpp
LOCAL_MODULE := libimgpatch
-LOCAL_C_INCLUDES += bootable/recovery
+LOCAL_MODULE_HOST_OS := linux
+LOCAL_C_INCLUDES += \
+ $(LOCAL_PATH)/include \
+ bootable/recovery
LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
-LOCAL_STATIC_LIBRARIES += libcrypto_static libbz libz
-
+LOCAL_STATIC_LIBRARIES += libcrypto libbz libz
+LOCAL_CFLAGS := -Werror
include $(BUILD_HOST_STATIC_LIBRARY)
-endif # HOST_OS == linux
+# libapplypatch_modes (static library)
+# ===============================
include $(CLEAR_VARS)
+LOCAL_CLANG := true
+LOCAL_SRC_FILES := \
+ applypatch_modes.cpp
+LOCAL_MODULE := libapplypatch_modes
+LOCAL_C_INCLUDES := bootable/recovery
+LOCAL_STATIC_LIBRARIES := \
+ libapplypatch \
+ libbase \
+ libedify \
+ libcrypto
+LOCAL_CFLAGS := -Werror
+include $(BUILD_STATIC_LIBRARY)
+# applypatch (target executable)
+# ===============================
+include $(CLEAR_VARS)
LOCAL_CLANG := true
-LOCAL_SRC_FILES := main.cpp
+LOCAL_SRC_FILES := applypatch_main.cpp
LOCAL_MODULE := applypatch
-LOCAL_C_INCLUDES += bootable/recovery
-LOCAL_STATIC_LIBRARIES += libapplypatch libbase libotafault libmtdutils libcrypto_static libbz \
- libedify \
-
-LOCAL_SHARED_LIBRARIES += libz libcutils libc
-
+LOCAL_C_INCLUDES := bootable/recovery
+LOCAL_STATIC_LIBRARIES := \
+ libapplypatch_modes \
+ libapplypatch \
+ libbase \
+ libedify \
+ libotafault \
+ libcrypto \
+ libbz
+LOCAL_SHARED_LIBRARIES := \
+ libbase \
+ libz \
+ libcutils
+LOCAL_CFLAGS := -Werror
include $(BUILD_EXECUTABLE)
+# imgdiff (host static executable)
+# ===============================
include $(CLEAR_VARS)
-
LOCAL_CLANG := true
-LOCAL_SRC_FILES := imgdiff.cpp utils.cpp bsdiff.cpp
+LOCAL_SRC_FILES := imgdiff.cpp utils.cpp
LOCAL_MODULE := imgdiff
+LOCAL_STATIC_LIBRARIES += \
+ libbsdiff \
+ libbz \
+ libdivsufsort64 \
+ libdivsufsort \
+ libz
+LOCAL_CFLAGS := -Werror
LOCAL_FORCE_STATIC_EXECUTABLE := true
-LOCAL_C_INCLUDES += external/zlib external/bzip2
-LOCAL_STATIC_LIBRARIES += libz libbz
-
include $(BUILD_HOST_EXECUTABLE)
diff --git a/applypatch/Makefile b/applypatch/Makefile
new file mode 100644
index 000000000..fb4984303
--- /dev/null
+++ b/applypatch/Makefile
@@ -0,0 +1,33 @@
+# Copyright (C) 2016 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# This file is for building imgdiff in Chrome OS.
+
+CPPFLAGS += -iquote.. -Iinclude
+CXXFLAGS += -std=c++11 -O3 -Wall -Werror
+LDLIBS += -lbz2 -lz
+
+.PHONY: all clean
+
+all: imgdiff libimgpatch.a
+
+clean:
+ rm -f *.o imgdiff libimgpatch.a
+
+imgdiff: imgdiff.o bsdiff.o utils.o
+ $(CXX) $(CPPFLAGS) $(CXXFLAGS) $(LDLIBS) -o $@ $^
+
+libimgpatch.a utils.o: CXXFLAGS += -fPIC
+libimgpatch.a: imgpatch.o bspatch.o utils.o
+ ${AR} rcs $@ $^
diff --git a/applypatch/applypatch.cpp b/applypatch/applypatch.cpp
index 7985fc0c6..95389da6e 100644
--- a/applypatch/applypatch.cpp
+++ b/applypatch/applypatch.cpp
@@ -14,6 +14,8 @@
* limitations under the License.
*/
+#include "applypatch/applypatch.h"
+
#include <errno.h>
#include <fcntl.h>
#include <libgen.h>
@@ -27,17 +29,18 @@
#include <memory>
#include <string>
+#include <utility>
+#include <vector>
+#include <android-base/parseint.h>
#include <android-base/strings.h>
+#include <openssl/sha.h>
-#include "openssl/sha.h"
-#include "applypatch.h"
-#include "mtdutils/mtdutils.h"
#include "edify/expr.h"
#include "ota_io.h"
#include "print_sha1.h"
-static int LoadPartitionContents(const char* filename, FileContents* file);
+static int LoadPartitionContents(const std::string& filename, FileContents* file);
static ssize_t FileSink(const unsigned char* data, ssize_t len, void* token);
static int GenerateTarget(FileContents* source_file,
const Value* source_patch_value,
@@ -49,48 +52,39 @@ static int GenerateTarget(FileContents* source_file,
size_t target_size,
const Value* bonus_data);
-static bool mtd_partitions_scanned = false;
-
-// Read a file into memory; store the file contents and associated
-// metadata in *file.
-//
+// Read a file into memory; store the file contents and associated metadata in *file.
// Return 0 on success.
int LoadFileContents(const char* filename, FileContents* file) {
- // A special 'filename' beginning with "MTD:" or "EMMC:" means to
- // load the contents of a partition.
- if (strncmp(filename, "MTD:", 4) == 0 ||
- strncmp(filename, "EMMC:", 5) == 0) {
- return LoadPartitionContents(filename, file);
- }
+ // A special 'filename' beginning with "EMMC:" means to load the contents of a partition.
+ if (strncmp(filename, "EMMC:", 5) == 0) {
+ return LoadPartitionContents(filename, file);
+ }
- if (stat(filename, &file->st) != 0) {
- printf("failed to stat \"%s\": %s\n", filename, strerror(errno));
- return -1;
- }
+ if (stat(filename, &file->st) == -1) {
+ printf("failed to stat \"%s\": %s\n", filename, strerror(errno));
+ return -1;
+ }
- std::vector<unsigned char> data(file->st.st_size);
- FILE* f = ota_fopen(filename, "rb");
- if (f == NULL) {
- printf("failed to open \"%s\": %s\n", filename, strerror(errno));
- return -1;
- }
+ std::vector<unsigned char> data(file->st.st_size);
+ unique_file f(ota_fopen(filename, "rb"));
+ if (!f) {
+ printf("failed to open \"%s\": %s\n", filename, strerror(errno));
+ return -1;
+ }
- size_t bytes_read = ota_fread(data.data(), 1, data.size(), f);
- if (bytes_read != data.size()) {
- printf("short read of \"%s\" (%zu bytes of %zd)\n", filename, bytes_read, data.size());
- ota_fclose(f);
- return -1;
- }
- ota_fclose(f);
- file->data = std::move(data);
- SHA1(file->data.data(), file->data.size(), file->sha1);
- return 0;
+ size_t bytes_read = ota_fread(data.data(), 1, data.size(), f.get());
+ if (bytes_read != data.size()) {
+ printf("short read of \"%s\" (%zu bytes of %zu)\n", filename, bytes_read, data.size());
+ return -1;
+ }
+ file->data = std::move(data);
+ SHA1(file->data.data(), file->data.size(), file->sha1);
+ return 0;
}
-// Load the contents of an MTD or EMMC partition into the provided
+// Load the contents of an EMMC partition into the provided
// FileContents. filename should be a string of the form
-// "MTD:<partition_name>:<size_1>:<sha1_1>:<size_2>:<sha1_2>:..." (or
-// "EMMC:<partition_device>:..."). The smallest size_n bytes for
+// "EMMC:<partition_device>:...". The smallest size_n bytes for
// which that prefix of the partition contents has the corresponding
// sha1 hash will be loaded. It is acceptable for a size value to be
// repeated with different sha1s. Will return 0 on success.
@@ -102,380 +96,257 @@ int LoadFileContents(const char* filename, FileContents* file) {
// "end-of-file" marker), so the caller must specify the possible
// lengths and the hash of the data, and we'll do the load expecting
// to find one of those hashes.
-enum PartitionType { MTD, EMMC };
-
-static int LoadPartitionContents(const char* filename, FileContents* file) {
- std::string copy(filename);
- std::vector<std::string> pieces = android::base::Split(copy, ":");
- if (pieces.size() < 4 || pieces.size() % 2 != 0) {
- printf("LoadPartitionContents called with bad filename (%s)\n", filename);
- return -1;
- }
-
- enum PartitionType type;
- if (pieces[0] == "MTD") {
- type = MTD;
- } else if (pieces[0] == "EMMC") {
- type = EMMC;
- } else {
- printf("LoadPartitionContents called with bad filename (%s)\n", filename);
+static int LoadPartitionContents(const std::string& filename, FileContents* file) {
+ std::vector<std::string> pieces = android::base::Split(filename, ":");
+ if (pieces.size() < 4 || pieces.size() % 2 != 0 || pieces[0] != "EMMC") {
+ printf("LoadPartitionContents called with bad filename \"%s\"\n", filename.c_str());
+ return -1;
+ }
+
+ size_t pair_count = (pieces.size() - 2) / 2; // # of (size, sha1) pairs in filename
+ std::vector<std::pair<size_t, std::string>> pairs;
+ for (size_t i = 0; i < pair_count; ++i) {
+ size_t size;
+ if (!android::base::ParseUint(pieces[i * 2 + 2], &size) || size == 0) {
+ printf("LoadPartitionContents called with bad size \"%s\"\n", pieces[i * 2 + 2].c_str());
+ return -1;
+ }
+ pairs.push_back({ size, pieces[i * 2 + 3] });
+ }
+
+ // Sort the pairs array so that they are in order of increasing size.
+ std::sort(pairs.begin(), pairs.end());
+
+ const char* partition = pieces[1].c_str();
+ unique_file dev(ota_fopen(partition, "rb"));
+ if (!dev) {
+ printf("failed to open emmc partition \"%s\": %s\n", partition, strerror(errno));
+ return -1;
+ }
+
+ SHA_CTX sha_ctx;
+ SHA1_Init(&sha_ctx);
+
+ // Allocate enough memory to hold the largest size.
+ std::vector<unsigned char> buffer(pairs[pair_count - 1].first);
+ unsigned char* buffer_ptr = buffer.data();
+ size_t buffer_size = 0; // # bytes read so far
+ bool found = false;
+
+ for (const auto& pair : pairs) {
+ size_t current_size = pair.first;
+ const std::string& current_sha1 = pair.second;
+
+ // Read enough additional bytes to get us up to the next size. (Again,
+ // we're trying the possibilities in order of increasing size).
+ size_t next = current_size - buffer_size;
+ if (next > 0) {
+ size_t read = ota_fread(buffer_ptr, 1, next, dev.get());
+ if (next != read) {
+ printf("short read (%zu bytes of %zu) for partition \"%s\"\n", read, next, partition);
return -1;
- }
- const char* partition = pieces[1].c_str();
-
- size_t pairs = (pieces.size() - 2) / 2; // # of (size, sha1) pairs in filename
- std::vector<size_t> index(pairs);
- std::vector<size_t> size(pairs);
- std::vector<std::string> sha1sum(pairs);
-
- for (size_t i = 0; i < pairs; ++i) {
- size[i] = strtol(pieces[i*2+2].c_str(), NULL, 10);
- if (size[i] == 0) {
- printf("LoadPartitionContents called with bad size (%s)\n", filename);
- return -1;
- }
- sha1sum[i] = pieces[i*2+3].c_str();
- index[i] = i;
+ }
+ SHA1_Update(&sha_ctx, buffer_ptr, read);
+ buffer_size += read;
+ buffer_ptr += read;
}
- // Sort the index[] array so it indexes the pairs in order of increasing size.
- sort(index.begin(), index.end(),
- [&](const size_t& i, const size_t& j) {
- return (size[i] < size[j]);
- }
- );
-
- MtdReadContext* ctx = NULL;
- FILE* dev = NULL;
-
- switch (type) {
- case MTD: {
- if (!mtd_partitions_scanned) {
- mtd_scan_partitions();
- mtd_partitions_scanned = true;
- }
-
- const MtdPartition* mtd = mtd_find_partition_by_name(partition);
- if (mtd == NULL) {
- printf("mtd partition \"%s\" not found (loading %s)\n", partition, filename);
- return -1;
- }
-
- ctx = mtd_read_partition(mtd);
- if (ctx == NULL) {
- printf("failed to initialize read of mtd partition \"%s\"\n", partition);
- return -1;
- }
- break;
- }
-
- case EMMC:
- dev = ota_fopen(partition, "rb");
- if (dev == NULL) {
- printf("failed to open emmc partition \"%s\": %s\n", partition, strerror(errno));
- return -1;
- }
- }
+ // Duplicate the SHA context and finalize the duplicate so we can
+ // check it against this pair's expected hash.
+ SHA_CTX temp_ctx;
+ memcpy(&temp_ctx, &sha_ctx, sizeof(SHA_CTX));
+ uint8_t sha_so_far[SHA_DIGEST_LENGTH];
+ SHA1_Final(sha_so_far, &temp_ctx);
- SHA_CTX sha_ctx;
- SHA1_Init(&sha_ctx);
uint8_t parsed_sha[SHA_DIGEST_LENGTH];
-
- // Allocate enough memory to hold the largest size.
- std::vector<unsigned char> data(size[index[pairs-1]]);
- char* p = reinterpret_cast<char*>(data.data());
- size_t data_size = 0; // # bytes read so far
- bool found = false;
-
- for (size_t i = 0; i < pairs; ++i) {
- // Read enough additional bytes to get us up to the next size. (Again,
- // we're trying the possibilities in order of increasing size).
- size_t next = size[index[i]] - data_size;
- if (next > 0) {
- size_t read = 0;
- switch (type) {
- case MTD:
- read = mtd_read_data(ctx, p, next);
- break;
-
- case EMMC:
- read = ota_fread(p, 1, next, dev);
- break;
- }
- if (next != read) {
- printf("short read (%zu bytes of %zu) for partition \"%s\"\n",
- read, next, partition);
- return -1;
- }
- SHA1_Update(&sha_ctx, p, read);
- data_size += read;
- p += read;
- }
-
- // Duplicate the SHA context and finalize the duplicate so we can
- // check it against this pair's expected hash.
- SHA_CTX temp_ctx;
- memcpy(&temp_ctx, &sha_ctx, sizeof(SHA_CTX));
- uint8_t sha_so_far[SHA_DIGEST_LENGTH];
- SHA1_Final(sha_so_far, &temp_ctx);
-
- if (ParseSha1(sha1sum[index[i]].c_str(), parsed_sha) != 0) {
- printf("failed to parse sha1 %s in %s\n", sha1sum[index[i]].c_str(), filename);
- return -1;
- }
-
- if (memcmp(sha_so_far, parsed_sha, SHA_DIGEST_LENGTH) == 0) {
- // we have a match. stop reading the partition; we'll return
- // the data we've read so far.
- printf("partition read matched size %zu sha %s\n",
- size[index[i]], sha1sum[index[i]].c_str());
- found = true;
- break;
- }
+ if (ParseSha1(current_sha1.c_str(), parsed_sha) != 0) {
+ printf("failed to parse SHA-1 %s in %s\n", current_sha1.c_str(), filename.c_str());
+ return -1;
}
- switch (type) {
- case MTD:
- mtd_read_close(ctx);
- break;
-
- case EMMC:
- ota_fclose(dev);
- break;
+ if (memcmp(sha_so_far, parsed_sha, SHA_DIGEST_LENGTH) == 0) {
+ // We have a match. Stop reading the partition; we'll return the data we've read so far.
+ printf("partition read matched size %zu SHA-1 %s\n", current_size, current_sha1.c_str());
+ found = true;
+ break;
}
+ }
+ if (!found) {
+ // Ran off the end of the list of (size, sha1) pairs without finding a match.
+ printf("contents of partition \"%s\" didn't match %s\n", partition, filename.c_str());
+ return -1;
+ }
- if (!found) {
- // Ran off the end of the list of (size,sha1) pairs without finding a match.
- printf("contents of partition \"%s\" didn't match %s\n", partition, filename);
- return -1;
- }
-
- SHA1_Final(file->sha1, &sha_ctx);
+ SHA1_Final(file->sha1, &sha_ctx);
- data.resize(data_size);
- file->data = std::move(data);
- // Fake some stat() info.
- file->st.st_mode = 0644;
- file->st.st_uid = 0;
- file->st.st_gid = 0;
+ buffer.resize(buffer_size);
+ file->data = std::move(buffer);
+ // Fake some stat() info.
+ file->st.st_mode = 0644;
+ file->st.st_uid = 0;
+ file->st.st_gid = 0;
- return 0;
+ return 0;
}
// Save the contents of the given FileContents object under the given
// filename. Return 0 on success.
int SaveFileContents(const char* filename, const FileContents* file) {
- int fd = ota_open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_SYNC, S_IRUSR | S_IWUSR);
- if (fd < 0) {
- printf("failed to open \"%s\" for write: %s\n", filename, strerror(errno));
- return -1;
- }
+ unique_fd fd(ota_open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_SYNC, S_IRUSR | S_IWUSR));
+ if (fd == -1) {
+ printf("failed to open \"%s\" for write: %s\n", filename, strerror(errno));
+ return -1;
+ }
- ssize_t bytes_written = FileSink(file->data.data(), file->data.size(), &fd);
- if (bytes_written != static_cast<ssize_t>(file->data.size())) {
- printf("short write of \"%s\" (%zd bytes of %zu) (%s)\n",
- filename, bytes_written, file->data.size(), strerror(errno));
- ota_close(fd);
- return -1;
- }
- if (ota_fsync(fd) != 0) {
- printf("fsync of \"%s\" failed: %s\n", filename, strerror(errno));
- return -1;
- }
- if (ota_close(fd) != 0) {
- printf("close of \"%s\" failed: %s\n", filename, strerror(errno));
- return -1;
- }
+ ssize_t bytes_written = FileSink(file->data.data(), file->data.size(), &fd);
+ if (bytes_written != static_cast<ssize_t>(file->data.size())) {
+ printf("short write of \"%s\" (%zd bytes of %zu): %s\n", filename, bytes_written,
+ file->data.size(), strerror(errno));
+ return -1;
+ }
+ if (ota_fsync(fd) != 0) {
+ printf("fsync of \"%s\" failed: %s\n", filename, strerror(errno));
+ return -1;
+ }
+ if (ota_close(fd) != 0) {
+ printf("close of \"%s\" failed: %s\n", filename, strerror(errno));
+ return -1;
+ }
- if (chmod(filename, file->st.st_mode) != 0) {
- printf("chmod of \"%s\" failed: %s\n", filename, strerror(errno));
- return -1;
- }
- if (chown(filename, file->st.st_uid, file->st.st_gid) != 0) {
- printf("chown of \"%s\" failed: %s\n", filename, strerror(errno));
- return -1;
- }
+ if (chmod(filename, file->st.st_mode) != 0) {
+ printf("chmod of \"%s\" failed: %s\n", filename, strerror(errno));
+ return -1;
+ }
+ if (chown(filename, file->st.st_uid, file->st.st_gid) != 0) {
+ printf("chown of \"%s\" failed: %s\n", filename, strerror(errno));
+ return -1;
+ }
- return 0;
+ return 0;
}
// Write a memory buffer to 'target' partition, a string of the form
-// "MTD:<partition>[:...]" or "EMMC:<partition_device>[:...]". The target name
+// "EMMC:<partition_device>[:...]". The target name
// might contain multiple colons, but WriteToPartition() only uses the first
// two and ignores the rest. Return 0 on success.
-int WriteToPartition(const unsigned char* data, size_t len, const char* target) {
- std::string copy(target);
- std::vector<std::string> pieces = android::base::Split(copy, ":");
+int WriteToPartition(const unsigned char* data, size_t len, const std::string& target) {
+ std::vector<std::string> pieces = android::base::Split(target, ":");
+ if (pieces.size() < 2 || pieces[0] != "EMMC") {
+ printf("WriteToPartition called with bad target (%s)\n", target.c_str());
+ return -1;
+ }
- if (pieces.size() < 2) {
- printf("WriteToPartition called with bad target (%s)\n", target);
+ const char* partition = pieces[1].c_str();
+ unique_fd fd(ota_open(partition, O_RDWR));
+ if (fd == -1) {
+ printf("failed to open %s: %s\n", partition, strerror(errno));
+ return -1;
+ }
+
+ size_t start = 0;
+ bool success = false;
+ for (size_t attempt = 0; attempt < 2; ++attempt) {
+ if (TEMP_FAILURE_RETRY(lseek(fd, start, SEEK_SET)) == -1) {
+ printf("failed seek on %s: %s\n", partition, strerror(errno));
+ return -1;
+ }
+ while (start < len) {
+ size_t to_write = len - start;
+ if (to_write > 1 << 20) to_write = 1 << 20;
+
+ ssize_t written = TEMP_FAILURE_RETRY(ota_write(fd, data + start, to_write));
+ if (written == -1) {
+ printf("failed write writing to %s: %s\n", partition, strerror(errno));
return -1;
+ }
+ start += written;
}
- enum PartitionType type;
- if (pieces[0] == "MTD") {
- type = MTD;
- } else if (pieces[0] == "EMMC") {
- type = EMMC;
- } else {
- printf("WriteToPartition called with bad target (%s)\n", target);
- return -1;
+ if (ota_fsync(fd) != 0) {
+ printf("failed to sync to %s: %s\n", partition, strerror(errno));
+ return -1;
+ }
+ if (ota_close(fd) != 0) {
+ printf("failed to close %s: %s\n", partition, strerror(errno));
+ return -1;
}
- const char* partition = pieces[1].c_str();
-
- switch (type) {
- case MTD: {
- if (!mtd_partitions_scanned) {
- mtd_scan_partitions();
- mtd_partitions_scanned = true;
- }
-
- const MtdPartition* mtd = mtd_find_partition_by_name(partition);
- if (mtd == NULL) {
- printf("mtd partition \"%s\" not found for writing\n", partition);
- return -1;
- }
-
- MtdWriteContext* ctx = mtd_write_partition(mtd);
- if (ctx == NULL) {
- printf("failed to init mtd partition \"%s\" for writing\n", partition);
- return -1;
- }
-
- size_t written = mtd_write_data(ctx, reinterpret_cast<const char*>(data), len);
- if (written != len) {
- printf("only wrote %zu of %zu bytes to MTD %s\n", written, len, partition);
- mtd_write_close(ctx);
- return -1;
- }
-
- if (mtd_erase_blocks(ctx, -1) < 0) {
- printf("error finishing mtd write of %s\n", partition);
- mtd_write_close(ctx);
- return -1;
- }
-
- if (mtd_write_close(ctx)) {
- printf("error closing mtd write of %s\n", partition);
- return -1;
- }
- break;
- }
- case EMMC: {
- size_t start = 0;
- bool success = false;
- int fd = ota_open(partition, O_RDWR | O_SYNC);
- if (fd < 0) {
- printf("failed to open %s: %s\n", partition, strerror(errno));
- return -1;
- }
-
- for (size_t attempt = 0; attempt < 2; ++attempt) {
- if (TEMP_FAILURE_RETRY(lseek(fd, start, SEEK_SET)) == -1) {
- printf("failed seek on %s: %s\n", partition, strerror(errno));
- return -1;
- }
- while (start < len) {
- size_t to_write = len - start;
- if (to_write > 1<<20) to_write = 1<<20;
-
- ssize_t written = TEMP_FAILURE_RETRY(ota_write(fd, data+start, to_write));
- if (written == -1) {
- printf("failed write writing to %s: %s\n", partition, strerror(errno));
- return -1;
- }
- start += written;
- }
- if (ota_fsync(fd) != 0) {
- printf("failed to sync to %s (%s)\n", partition, strerror(errno));
- return -1;
- }
- if (ota_close(fd) != 0) {
- printf("failed to close %s (%s)\n", partition, strerror(errno));
- return -1;
- }
- fd = ota_open(partition, O_RDONLY);
- if (fd < 0) {
- printf("failed to reopen %s for verify (%s)\n", partition, strerror(errno));
- return -1;
- }
-
- // Drop caches so our subsequent verification read
- // won't just be reading the cache.
- sync();
- int dc = ota_open("/proc/sys/vm/drop_caches", O_WRONLY);
- if (TEMP_FAILURE_RETRY(ota_write(dc, "3\n", 2)) == -1) {
- printf("write to /proc/sys/vm/drop_caches failed: %s\n", strerror(errno));
- } else {
- printf(" caches dropped\n");
- }
- ota_close(dc);
- sleep(1);
-
- // verify
- if (TEMP_FAILURE_RETRY(lseek(fd, 0, SEEK_SET)) == -1) {
- printf("failed to seek back to beginning of %s: %s\n",
- partition, strerror(errno));
- return -1;
- }
- unsigned char buffer[4096];
- start = len;
- for (size_t p = 0; p < len; p += sizeof(buffer)) {
- size_t to_read = len - p;
- if (to_read > sizeof(buffer)) {
- to_read = sizeof(buffer);
- }
-
- size_t so_far = 0;
- while (so_far < to_read) {
- ssize_t read_count =
- TEMP_FAILURE_RETRY(ota_read(fd, buffer+so_far, to_read-so_far));
- if (read_count == -1) {
- printf("verify read error %s at %zu: %s\n",
- partition, p, strerror(errno));
- return -1;
- }
- if (static_cast<size_t>(read_count) < to_read) {
- printf("short verify read %s at %zu: %zd %zu %s\n",
- partition, p, read_count, to_read, strerror(errno));
- }
- so_far += read_count;
- }
-
- if (memcmp(buffer, data+p, to_read) != 0) {
- printf("verification failed starting at %zu\n", p);
- start = p;
- break;
- }
- }
-
- if (start == len) {
- printf("verification read succeeded (attempt %zu)\n", attempt+1);
- success = true;
- break;
- }
- }
-
- if (!success) {
- printf("failed to verify after all attempts\n");
- return -1;
- }
-
- if (ota_close(fd) != 0) {
- printf("error closing %s (%s)\n", partition, strerror(errno));
- return -1;
- }
- sync();
- break;
+ fd.reset(ota_open(partition, O_RDONLY));
+ if (fd == -1) {
+ printf("failed to reopen %s for verify: %s\n", partition, strerror(errno));
+ return -1;
+ }
+
+ // Drop caches so our subsequent verification read won't just be reading the cache.
+ sync();
+ unique_fd dc(ota_open("/proc/sys/vm/drop_caches", O_WRONLY));
+ if (TEMP_FAILURE_RETRY(ota_write(dc, "3\n", 2)) == -1) {
+ printf("write to /proc/sys/vm/drop_caches failed: %s\n", strerror(errno));
+ } else {
+ printf(" caches dropped\n");
+ }
+ ota_close(dc);
+ sleep(1);
+
+ // Verify.
+ if (TEMP_FAILURE_RETRY(lseek(fd, 0, SEEK_SET)) == -1) {
+ printf("failed to seek back to beginning of %s: %s\n", partition, strerror(errno));
+ return -1;
+ }
+
+ unsigned char buffer[4096];
+ start = len;
+ for (size_t p = 0; p < len; p += sizeof(buffer)) {
+ size_t to_read = len - p;
+ if (to_read > sizeof(buffer)) {
+ to_read = sizeof(buffer);
+ }
+
+ size_t so_far = 0;
+ while (so_far < to_read) {
+ ssize_t read_count = TEMP_FAILURE_RETRY(ota_read(fd, buffer + so_far, to_read - so_far));
+ if (read_count == -1) {
+ printf("verify read error %s at %zu: %s\n", partition, p, strerror(errno));
+ return -1;
+ } else if (read_count == 0) {
+ printf("verify read reached unexpected EOF, %s at %zu\n", partition, p);
+ return -1;
+ }
+ if (static_cast<size_t>(read_count) < to_read) {
+ printf("short verify read %s at %zu: %zd %zu\n", partition, p, read_count, to_read);
}
+ so_far += read_count;
+ }
+
+ if (memcmp(buffer, data + p, to_read) != 0) {
+ printf("verification failed starting at %zu\n", p);
+ start = p;
+ break;
+ }
}
- return 0;
-}
+ if (start == len) {
+ printf("verification read succeeded (attempt %zu)\n", attempt + 1);
+ success = true;
+ break;
+ }
+ }
+
+ if (!success) {
+ printf("failed to verify after all attempts\n");
+ return -1;
+ }
+ if (ota_close(fd) == -1) {
+ printf("error closing %s: %s\n", partition, strerror(errno));
+ return -1;
+ }
+ sync();
+
+ return 0;
+}
// Take a string 'str' of 40 hex digits and parse it into the 20
// byte array 'digest'. 'str' may contain only the digest or be of
@@ -509,52 +380,47 @@ int ParseSha1(const char* str, uint8_t* digest) {
// Search an array of sha1 strings for one matching the given sha1.
// Return the index of the match on success, or -1 if no match is
// found.
-int FindMatchingPatch(uint8_t* sha1, char* const * const patch_sha1_str,
- int num_patches) {
+int FindMatchingPatch(uint8_t* sha1, const std::vector<std::string>& patch_sha1_str) {
+ for (size_t i = 0; i < patch_sha1_str.size(); ++i) {
uint8_t patch_sha1[SHA_DIGEST_LENGTH];
- for (int i = 0; i < num_patches; ++i) {
- if (ParseSha1(patch_sha1_str[i], patch_sha1) == 0 &&
- memcmp(patch_sha1, sha1, SHA_DIGEST_LENGTH) == 0) {
- return i;
- }
+ if (ParseSha1(patch_sha1_str[i].c_str(), patch_sha1) == 0 &&
+ memcmp(patch_sha1, sha1, SHA_DIGEST_LENGTH) == 0) {
+ return i;
}
- return -1;
+ }
+ return -1;
}
// Returns 0 if the contents of the file (argv[2]) or the cached file
// match any of the sha1's on the command line (argv[3:]). Returns
// nonzero otherwise.
-int applypatch_check(const char* filename, int num_patches,
- char** const patch_sha1_str) {
- FileContents file;
-
- // It's okay to specify no sha1s; the check will pass if the
- // LoadFileContents is successful. (Useful for reading
- // partitions, where the filename encodes the sha1s; no need to
- // check them twice.)
- if (LoadFileContents(filename, &file) != 0 ||
- (num_patches > 0 &&
- FindMatchingPatch(file.sha1, patch_sha1_str, num_patches) < 0)) {
- printf("file \"%s\" doesn't have any of expected "
- "sha1 sums; checking cache\n", filename);
-
- // If the source file is missing or corrupted, it might be because
- // we were killed in the middle of patching it. A copy of it
- // should have been made in CACHE_TEMP_SOURCE. If that file
- // exists and matches the sha1 we're looking for, the check still
- // passes.
-
- if (LoadFileContents(CACHE_TEMP_SOURCE, &file) != 0) {
- printf("failed to load cache file\n");
- return 1;
- }
-
- if (FindMatchingPatch(file.sha1, patch_sha1_str, num_patches) < 0) {
- printf("cache bits don't match any sha1 for \"%s\"\n", filename);
- return 1;
- }
- }
- return 0;
+int applypatch_check(const char* filename, const std::vector<std::string>& patch_sha1_str) {
+ FileContents file;
+
+ // It's okay to specify no sha1s; the check will pass if the
+ // LoadFileContents is successful. (Useful for reading
+ // partitions, where the filename encodes the sha1s; no need to
+ // check them twice.)
+ if (LoadFileContents(filename, &file) != 0 ||
+ (!patch_sha1_str.empty() && FindMatchingPatch(file.sha1, patch_sha1_str) < 0)) {
+ printf("file \"%s\" doesn't have any of expected sha1 sums; checking cache\n", filename);
+
+ // If the source file is missing or corrupted, it might be because
+ // we were killed in the middle of patching it. A copy of it
+ // should have been made in CACHE_TEMP_SOURCE. If that file
+ // exists and matches the sha1 we're looking for, the check still
+ // passes.
+ if (LoadFileContents(CACHE_TEMP_SOURCE, &file) != 0) {
+ printf("failed to load cache file\n");
+ return 1;
+ }
+
+ if (FindMatchingPatch(file.sha1, patch_sha1_str) < 0) {
+ printf("cache bits don't match any sha1 for \"%s\"\n", filename);
+ return 1;
+ }
+ }
+ return 0;
}
int ShowLicenses() {
@@ -596,7 +462,7 @@ size_t FreeSpaceForFile(const char* filename) {
int CacheSizeCheck(size_t bytes) {
if (MakeFreeSpaceOnCache(bytes) < 0) {
- printf("unable to make %ld bytes available on /cache\n", (long)bytes);
+ printf("unable to make %zu bytes available on /cache\n", bytes);
return 1;
} else {
return 0;
@@ -633,10 +499,9 @@ int applypatch(const char* source_filename,
const char* target_filename,
const char* target_sha1_str,
size_t target_size,
- int num_patches,
- char** const patch_sha1_str,
- Value** patch_data,
- Value* bonus_data) {
+ const std::vector<std::string>& patch_sha1_str,
+ const std::vector<std::unique_ptr<Value>>& patch_data,
+ const Value* bonus_data) {
printf("patch %s: ", source_filename);
if (target_filename[0] == '-' && target_filename[1] == '\0') {
@@ -649,10 +514,8 @@ int applypatch(const char* source_filename,
return 1;
}
- FileContents copy_file;
FileContents source_file;
- const Value* source_patch_value = NULL;
- const Value* copy_patch_value = NULL;
+ const Value* source_patch_value = nullptr;
// We try to load the target file into the source_file object.
if (LoadFileContents(target_filename, &source_file) == 0) {
@@ -674,13 +537,15 @@ int applypatch(const char* source_filename,
}
if (!source_file.data.empty()) {
- int to_use = FindMatchingPatch(source_file.sha1, patch_sha1_str, num_patches);
+ int to_use = FindMatchingPatch(source_file.sha1, patch_sha1_str);
if (to_use >= 0) {
- source_patch_value = patch_data[to_use];
+ source_patch_value = patch_data[to_use].get();
}
}
- if (source_patch_value == NULL) {
+ FileContents copy_file;
+ const Value* copy_patch_value = nullptr;
+ if (source_patch_value == nullptr) {
source_file.data.clear();
printf("source file is bad; trying copy\n");
@@ -690,12 +555,12 @@ int applypatch(const char* source_filename,
return 1;
}
- int to_use = FindMatchingPatch(copy_file.sha1, patch_sha1_str, num_patches);
+ int to_use = FindMatchingPatch(copy_file.sha1, patch_sha1_str);
if (to_use >= 0) {
- copy_patch_value = patch_data[to_use];
+ copy_patch_value = patch_data[to_use].get();
}
- if (copy_patch_value == NULL) {
+ if (copy_patch_value == nullptr) {
// fail.
printf("copy file doesn't match source SHA-1s either\n");
return 1;
@@ -717,50 +582,49 @@ int applypatch(const char* source_filename,
*/
int applypatch_flash(const char* source_filename, const char* target_filename,
const char* target_sha1_str, size_t target_size) {
- printf("flash %s: ", target_filename);
-
- uint8_t target_sha1[SHA_DIGEST_LENGTH];
- if (ParseSha1(target_sha1_str, target_sha1) != 0) {
- printf("failed to parse tgt-sha1 \"%s\"\n", target_sha1_str);
- return 1;
- }
-
- FileContents source_file;
- std::string target_str(target_filename);
-
- std::vector<std::string> pieces = android::base::Split(target_str, ":");
- if (pieces.size() != 2 || (pieces[0] != "MTD" && pieces[0] != "EMMC")) {
- printf("invalid target name \"%s\"", target_filename);
- return 1;
- }
-
- // Load the target into the source_file object to see if already applied.
- pieces.push_back(std::to_string(target_size));
- pieces.push_back(target_sha1_str);
- std::string fullname = android::base::Join(pieces, ':');
- if (LoadPartitionContents(fullname.c_str(), &source_file) == 0 &&
- memcmp(source_file.sha1, target_sha1, SHA_DIGEST_LENGTH) == 0) {
- // The early-exit case: the image was already applied, this partition
- // has the desired hash, nothing for us to do.
- printf("already %s\n", short_sha1(target_sha1).c_str());
- return 0;
- }
-
- if (LoadFileContents(source_filename, &source_file) == 0) {
- if (memcmp(source_file.sha1, target_sha1, SHA_DIGEST_LENGTH) != 0) {
- // The source doesn't have desired checksum.
- printf("source \"%s\" doesn't have expected sha1 sum\n", source_filename);
- printf("expected: %s, found: %s\n", short_sha1(target_sha1).c_str(),
- short_sha1(source_file.sha1).c_str());
- return 1;
- }
- }
-
- if (WriteToPartition(source_file.data.data(), target_size, target_filename) != 0) {
- printf("write of copied data to %s failed\n", target_filename);
- return 1;
- }
+ printf("flash %s: ", target_filename);
+
+ uint8_t target_sha1[SHA_DIGEST_LENGTH];
+ if (ParseSha1(target_sha1_str, target_sha1) != 0) {
+ printf("failed to parse tgt-sha1 \"%s\"\n", target_sha1_str);
+ return 1;
+ }
+
+ std::string target_str(target_filename);
+ std::vector<std::string> pieces = android::base::Split(target_str, ":");
+ if (pieces.size() != 2 || pieces[0] != "EMMC") {
+ printf("invalid target name \"%s\"", target_filename);
+ return 1;
+ }
+
+ // Load the target into the source_file object to see if already applied.
+ pieces.push_back(std::to_string(target_size));
+ pieces.push_back(target_sha1_str);
+ std::string fullname = android::base::Join(pieces, ':');
+ FileContents source_file;
+ if (LoadPartitionContents(fullname, &source_file) == 0 &&
+ memcmp(source_file.sha1, target_sha1, SHA_DIGEST_LENGTH) == 0) {
+ // The early-exit case: the image was already applied, this partition
+ // has the desired hash, nothing for us to do.
+ printf("already %s\n", short_sha1(target_sha1).c_str());
return 0;
+ }
+
+ if (LoadFileContents(source_filename, &source_file) == 0) {
+ if (memcmp(source_file.sha1, target_sha1, SHA_DIGEST_LENGTH) != 0) {
+ // The source doesn't have desired checksum.
+ printf("source \"%s\" doesn't have expected sha1 sum\n", source_filename);
+ printf("expected: %s, found: %s\n", short_sha1(target_sha1).c_str(),
+ short_sha1(source_file.sha1).c_str());
+ return 1;
+ }
+ }
+
+ if (WriteToPartition(source_file.data.data(), target_size, target_filename) != 0) {
+ printf("write of copied data to %s failed\n", target_filename);
+ return 1;
+ }
+ return 0;
}
static int GenerateTarget(FileContents* source_file,
@@ -772,223 +636,214 @@ static int GenerateTarget(FileContents* source_file,
const uint8_t target_sha1[SHA_DIGEST_LENGTH],
size_t target_size,
const Value* bonus_data) {
- int retry = 1;
- SHA_CTX ctx;
- std::string memory_sink_str;
- FileContents* source_to_use;
- int made_copy = 0;
-
- bool target_is_partition = (strncmp(target_filename, "MTD:", 4) == 0 ||
- strncmp(target_filename, "EMMC:", 5) == 0);
- const std::string tmp_target_filename = std::string(target_filename) + ".patch";
-
- // assume that target_filename (eg "/system/app/Foo.apk") is located
- // on the same filesystem as its top-level directory ("/system").
- // We need something that exists for calling statfs().
- std::string target_fs = target_filename;
- auto slash_pos = target_fs.find('/', 1);
- if (slash_pos != std::string::npos) {
- target_fs.resize(slash_pos);
- }
-
- const Value* patch;
- if (source_patch_value != NULL) {
- source_to_use = source_file;
- patch = source_patch_value;
- } else {
- source_to_use = copy_file;
- patch = copy_patch_value;
- }
- if (patch->type != VAL_BLOB) {
- printf("patch is not a blob\n");
+ // assume that target_filename (eg "/system/app/Foo.apk") is located
+ // on the same filesystem as its top-level directory ("/system").
+ // We need something that exists for calling statfs().
+ std::string target_fs = target_filename;
+ auto slash_pos = target_fs.find('/', 1);
+ if (slash_pos != std::string::npos) {
+ target_fs.resize(slash_pos);
+ }
+
+ FileContents* source_to_use;
+ const Value* patch;
+ if (source_patch_value != nullptr) {
+ source_to_use = source_file;
+ patch = source_patch_value;
+ } else {
+ source_to_use = copy_file;
+ patch = copy_patch_value;
+ }
+
+ if (patch->type != VAL_BLOB) {
+ printf("patch is not a blob\n");
+ return 1;
+ }
+
+ const char* header = &patch->data[0];
+ size_t header_bytes_read = patch->data.size();
+ bool use_bsdiff = false;
+ if (header_bytes_read >= 8 && memcmp(header, "BSDIFF40", 8) == 0) {
+ use_bsdiff = true;
+ } else if (header_bytes_read >= 8 && memcmp(header, "IMGDIFF2", 8) == 0) {
+ use_bsdiff = false;
+ } else {
+ printf("Unknown patch file format\n");
+ return 1;
+ }
+
+ bool target_is_partition = (strncmp(target_filename, "EMMC:", 5) == 0);
+ const std::string tmp_target_filename = std::string(target_filename) + ".patch";
+
+ int retry = 1;
+ bool made_copy = false;
+ SHA_CTX ctx;
+ std::string memory_sink_str; // Don't need to reserve space.
+ do {
+ // Is there enough room in the target filesystem to hold the patched file?
+
+ if (target_is_partition) {
+ // If the target is a partition, we're actually going to
+ // write the output to /tmp and then copy it to the
+ // partition. statfs() always returns 0 blocks free for
+ // /tmp, so instead we'll just assume that /tmp has enough
+ // space to hold the file.
+
+ // We still write the original source to cache, in case
+ // the partition write is interrupted.
+ if (MakeFreeSpaceOnCache(source_file->data.size()) < 0) {
+ printf("not enough free space on /cache\n");
return 1;
- }
- char* header = patch->data;
- ssize_t header_bytes_read = patch->size;
- bool use_bsdiff = false;
- if (header_bytes_read >= 8 && memcmp(header, "BSDIFF40", 8) == 0) {
- use_bsdiff = true;
- } else if (header_bytes_read >= 8 && memcmp(header, "IMGDIFF2", 8) == 0) {
- use_bsdiff = false;
- } else {
- printf("Unknown patch file format\n");
+ }
+ if (SaveFileContents(CACHE_TEMP_SOURCE, source_file) < 0) {
+ printf("failed to back up source file\n");
return 1;
- }
-
- do {
- // Is there enough room in the target filesystem to hold the patched
- // file?
-
- if (target_is_partition) {
- // If the target is a partition, we're actually going to
- // write the output to /tmp and then copy it to the
- // partition. statfs() always returns 0 blocks free for
- // /tmp, so instead we'll just assume that /tmp has enough
- // space to hold the file.
-
- // We still write the original source to cache, in case
- // the partition write is interrupted.
- if (MakeFreeSpaceOnCache(source_file->data.size()) < 0) {
- printf("not enough free space on /cache\n");
- return 1;
- }
- if (SaveFileContents(CACHE_TEMP_SOURCE, source_file) < 0) {
- printf("failed to back up source file\n");
- return 1;
- }
- made_copy = 1;
- retry = 0;
- } else {
- int enough_space = 0;
- if (retry > 0) {
- size_t free_space = FreeSpaceForFile(target_fs.c_str());
- enough_space =
- (free_space > (256 << 10)) && // 256k (two-block) minimum
- (free_space > (target_size * 3 / 2)); // 50% margin of error
- if (!enough_space) {
- printf("target %zu bytes; free space %zu bytes; retry %d; enough %d\n",
- target_size, free_space, retry, enough_space);
- }
- }
-
- if (!enough_space) {
- retry = 0;
- }
-
- if (!enough_space && source_patch_value != NULL) {
- // Using the original source, but not enough free space. First
- // copy the source file to cache, then delete it from the original
- // location.
-
- if (strncmp(source_filename, "MTD:", 4) == 0 ||
- strncmp(source_filename, "EMMC:", 5) == 0) {
- // It's impossible to free space on the target filesystem by
- // deleting the source if the source is a partition. If
- // we're ever in a state where we need to do this, fail.
- printf("not enough free space for target but source is partition\n");
- return 1;
- }
-
- if (MakeFreeSpaceOnCache(source_file->data.size()) < 0) {
- printf("not enough free space on /cache\n");
- return 1;
- }
-
- if (SaveFileContents(CACHE_TEMP_SOURCE, source_file) < 0) {
- printf("failed to back up source file\n");
- return 1;
- }
- made_copy = 1;
- unlink(source_filename);
-
- size_t free_space = FreeSpaceForFile(target_fs.c_str());
- printf("(now %zu bytes free for target) ", free_space);
- }
- }
-
-
- SinkFn sink = NULL;
- void* token = NULL;
- int output_fd = -1;
- if (target_is_partition) {
- // We store the decoded output in memory.
- sink = MemorySink;
- token = &memory_sink_str;
- } else {
- // We write the decoded output to "<tgt-file>.patch".
- output_fd = ota_open(tmp_target_filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_SYNC,
- S_IRUSR | S_IWUSR);
- if (output_fd < 0) {
- printf("failed to open output file %s: %s\n", tmp_target_filename.c_str(),
- strerror(errno));
- return 1;
- }
- sink = FileSink;
- token = &output_fd;
+ }
+ made_copy = true;
+ retry = 0;
+ } else {
+ bool enough_space = false;
+ if (retry > 0) {
+ size_t free_space = FreeSpaceForFile(target_fs.c_str());
+ enough_space = (free_space > (256 << 10)) && // 256k (two-block) minimum
+ (free_space > (target_size * 3 / 2)); // 50% margin of error
+ if (!enough_space) {
+ printf("target %zu bytes; free space %zu bytes; retry %d; enough %d\n", target_size,
+ free_space, retry, enough_space);
}
-
-
- SHA1_Init(&ctx);
-
- int result;
- if (use_bsdiff) {
- result = ApplyBSDiffPatch(source_to_use->data.data(), source_to_use->data.size(),
- patch, 0, sink, token, &ctx);
- } else {
- result = ApplyImagePatch(source_to_use->data.data(), source_to_use->data.size(),
- patch, sink, token, &ctx, bonus_data);
+ }
+
+ if (!enough_space) {
+ retry = 0;
+ }
+
+ if (!enough_space && source_patch_value != nullptr) {
+ // Using the original source, but not enough free space. First
+ // copy the source file to cache, then delete it from the original
+ // location.
+
+ if (strncmp(source_filename, "EMMC:", 5) == 0) {
+ // It's impossible to free space on the target filesystem by
+ // deleting the source if the source is a partition. If
+ // we're ever in a state where we need to do this, fail.
+ printf("not enough free space for target but source is partition\n");
+ return 1;
}
- if (!target_is_partition) {
- if (ota_fsync(output_fd) != 0) {
- printf("failed to fsync file \"%s\" (%s)\n", tmp_target_filename.c_str(),
- strerror(errno));
- result = 1;
- }
- if (ota_close(output_fd) != 0) {
- printf("failed to close file \"%s\" (%s)\n", tmp_target_filename.c_str(),
- strerror(errno));
- result = 1;
- }
+ if (MakeFreeSpaceOnCache(source_file->data.size()) < 0) {
+ printf("not enough free space on /cache\n");
+ return 1;
}
- if (result != 0) {
- if (retry == 0) {
- printf("applying patch failed\n");
- return result != 0;
- } else {
- printf("applying patch failed; retrying\n");
- }
- if (!target_is_partition) {
- unlink(tmp_target_filename.c_str());
- }
- } else {
- // succeeded; no need to retry
- break;
+ if (SaveFileContents(CACHE_TEMP_SOURCE, source_file) < 0) {
+ printf("failed to back up source file\n");
+ return 1;
}
- } while (retry-- > 0);
+ made_copy = true;
+ unlink(source_filename);
- uint8_t current_target_sha1[SHA_DIGEST_LENGTH];
- SHA1_Final(current_target_sha1, &ctx);
- if (memcmp(current_target_sha1, target_sha1, SHA_DIGEST_LENGTH) != 0) {
- printf("patch did not produce expected sha1\n");
- return 1;
- } else {
- printf("now %s\n", short_sha1(target_sha1).c_str());
+ size_t free_space = FreeSpaceForFile(target_fs.c_str());
+ printf("(now %zu bytes free for target) ", free_space);
+ }
}
+ SinkFn sink = nullptr;
+ void* token = nullptr;
+ unique_fd output_fd;
if (target_is_partition) {
- // Copy the temp file to the partition.
- if (WriteToPartition(reinterpret_cast<const unsigned char*>(memory_sink_str.c_str()),
- memory_sink_str.size(), target_filename) != 0) {
- printf("write of patched data to %s failed\n", target_filename);
- return 1;
- }
+ // We store the decoded output in memory.
+ sink = MemorySink;
+ token = &memory_sink_str;
} else {
- // Give the .patch file the same owner, group, and mode of the
- // original source file.
- if (chmod(tmp_target_filename.c_str(), source_to_use->st.st_mode) != 0) {
- printf("chmod of \"%s\" failed: %s\n", tmp_target_filename.c_str(), strerror(errno));
- return 1;
- }
- if (chown(tmp_target_filename.c_str(), source_to_use->st.st_uid, source_to_use->st.st_gid) != 0) {
- printf("chown of \"%s\" failed: %s\n", tmp_target_filename.c_str(), strerror(errno));
- return 1;
- }
+ // We write the decoded output to "<tgt-file>.patch".
+ output_fd.reset(ota_open(tmp_target_filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_SYNC,
+ S_IRUSR | S_IWUSR));
+ if (output_fd == -1) {
+ printf("failed to open output file %s: %s\n", tmp_target_filename.c_str(), strerror(errno));
+ return 1;
+ }
+ sink = FileSink;
+ token = &output_fd;
+ }
- // Finally, rename the .patch file to replace the target file.
- if (rename(tmp_target_filename.c_str(), target_filename) != 0) {
- printf("rename of .patch to \"%s\" failed: %s\n", target_filename, strerror(errno));
- return 1;
- }
+ SHA1_Init(&ctx);
+
+ int result;
+ if (use_bsdiff) {
+ result = ApplyBSDiffPatch(source_to_use->data.data(), source_to_use->data.size(), patch, 0,
+ sink, token, &ctx);
+ } else {
+ result = ApplyImagePatch(source_to_use->data.data(), source_to_use->data.size(), patch, sink,
+ token, &ctx, bonus_data);
}
- // If this run of applypatch created the copy, and we're here, we
- // can delete it.
- if (made_copy) {
- unlink(CACHE_TEMP_SOURCE);
+ if (!target_is_partition) {
+ if (ota_fsync(output_fd) != 0) {
+ printf("failed to fsync file \"%s\": %s\n", tmp_target_filename.c_str(), strerror(errno));
+ result = 1;
+ }
+ if (ota_close(output_fd) != 0) {
+ printf("failed to close file \"%s\": %s\n", tmp_target_filename.c_str(), strerror(errno));
+ result = 1;
+ }
}
- // Success!
- return 0;
+ if (result != 0) {
+ if (retry == 0) {
+ printf("applying patch failed\n");
+ return 1;
+ } else {
+ printf("applying patch failed; retrying\n");
+ }
+ if (!target_is_partition) {
+ unlink(tmp_target_filename.c_str());
+ }
+ } else {
+ // succeeded; no need to retry
+ break;
+ }
+ } while (retry-- > 0);
+
+ uint8_t current_target_sha1[SHA_DIGEST_LENGTH];
+ SHA1_Final(current_target_sha1, &ctx);
+ if (memcmp(current_target_sha1, target_sha1, SHA_DIGEST_LENGTH) != 0) {
+ printf("patch did not produce expected sha1\n");
+ return 1;
+ } else {
+ printf("now %s\n", short_sha1(target_sha1).c_str());
+ }
+
+ if (target_is_partition) {
+ // Copy the temp file to the partition.
+ if (WriteToPartition(reinterpret_cast<const unsigned char*>(memory_sink_str.c_str()),
+ memory_sink_str.size(), target_filename) != 0) {
+ printf("write of patched data to %s failed\n", target_filename);
+ return 1;
+ }
+ } else {
+ // Give the .patch file the same owner, group, and mode of the original source file.
+ if (chmod(tmp_target_filename.c_str(), source_to_use->st.st_mode) != 0) {
+ printf("chmod of \"%s\" failed: %s\n", tmp_target_filename.c_str(), strerror(errno));
+ return 1;
+ }
+ if (chown(tmp_target_filename.c_str(), source_to_use->st.st_uid,
+ source_to_use->st.st_gid) != 0) {
+ printf("chown of \"%s\" failed: %s\n", tmp_target_filename.c_str(), strerror(errno));
+ return 1;
+ }
+
+ // Finally, rename the .patch file to replace the target file.
+ if (rename(tmp_target_filename.c_str(), target_filename) != 0) {
+ printf("rename of .patch to \"%s\" failed: %s\n", target_filename, strerror(errno));
+ return 1;
+ }
+ }
+
+ // If this run of applypatch created the copy, and we're here, we can delete it.
+ if (made_copy) {
+ unlink(CACHE_TEMP_SOURCE);
+ }
+
+ // Success!
+ return 0;
}
diff --git a/applypatch/applypatch_main.cpp b/applypatch/applypatch_main.cpp
new file mode 100644
index 000000000..197077c93
--- /dev/null
+++ b/applypatch/applypatch_main.cpp
@@ -0,0 +1,28 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "applypatch_modes.h"
+
+// 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).
+//
+// See the comments to applypatch_modes() function.
+
+int main(int argc, char** argv) {
+ return applypatch_modes(argc, const_cast<const char**>(argv));
+}
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;
}
diff --git a/applypatch/applypatch_modes.h b/applypatch/applypatch_modes.h
new file mode 100644
index 000000000..3d9d08df5
--- /dev/null
+++ b/applypatch/applypatch_modes.h
@@ -0,0 +1,22 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef _APPLYPATCH_MODES_H
+#define _APPLYPATCH_MODES_H
+
+int applypatch_modes(int argc, const char** argv);
+
+#endif // _APPLYPATCH_MODES_H
diff --git a/applypatch/bsdiff.cpp b/applypatch/bsdiff.cpp
deleted file mode 100644
index 55dbe5cf1..000000000
--- a/applypatch/bsdiff.cpp
+++ /dev/null
@@ -1,410 +0,0 @@
-/*
- * Copyright (C) 2009 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-/*
- * Most of this code comes from bsdiff.c from the bsdiff-4.3
- * distribution, which is:
- */
-
-/*-
- * Copyright 2003-2005 Colin Percival
- * All rights reserved
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted providing that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
- * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
- * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
- * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- */
-
-#include <sys/types.h>
-
-#include <bzlib.h>
-#include <err.h>
-#include <fcntl.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-
-#define MIN(x,y) (((x)<(y)) ? (x) : (y))
-
-static void split(off_t *I,off_t *V,off_t start,off_t len,off_t h)
-{
- off_t i,j,k,x,tmp,jj,kk;
-
- if(len<16) {
- for(k=start;k<start+len;k+=j) {
- j=1;x=V[I[k]+h];
- for(i=1;k+i<start+len;i++) {
- if(V[I[k+i]+h]<x) {
- x=V[I[k+i]+h];
- j=0;
- };
- if(V[I[k+i]+h]==x) {
- tmp=I[k+j];I[k+j]=I[k+i];I[k+i]=tmp;
- j++;
- };
- };
- for(i=0;i<j;i++) V[I[k+i]]=k+j-1;
- if(j==1) I[k]=-1;
- };
- return;
- };
-
- x=V[I[start+len/2]+h];
- jj=0;kk=0;
- for(i=start;i<start+len;i++) {
- if(V[I[i]+h]<x) jj++;
- if(V[I[i]+h]==x) kk++;
- };
- jj+=start;kk+=jj;
-
- i=start;j=0;k=0;
- while(i<jj) {
- if(V[I[i]+h]<x) {
- i++;
- } else if(V[I[i]+h]==x) {
- tmp=I[i];I[i]=I[jj+j];I[jj+j]=tmp;
- j++;
- } else {
- tmp=I[i];I[i]=I[kk+k];I[kk+k]=tmp;
- k++;
- };
- };
-
- while(jj+j<kk) {
- if(V[I[jj+j]+h]==x) {
- j++;
- } else {
- tmp=I[jj+j];I[jj+j]=I[kk+k];I[kk+k]=tmp;
- k++;
- };
- };
-
- if(jj>start) split(I,V,start,jj-start,h);
-
- for(i=0;i<kk-jj;i++) V[I[jj+i]]=kk-1;
- if(jj==kk-1) I[jj]=-1;
-
- if(start+len>kk) split(I,V,kk,start+len-kk,h);
-}
-
-static void qsufsort(off_t *I,off_t *V,u_char *old,off_t oldsize)
-{
- off_t buckets[256];
- off_t i,h,len;
-
- for(i=0;i<256;i++) buckets[i]=0;
- for(i=0;i<oldsize;i++) buckets[old[i]]++;
- for(i=1;i<256;i++) buckets[i]+=buckets[i-1];
- for(i=255;i>0;i--) buckets[i]=buckets[i-1];
- buckets[0]=0;
-
- for(i=0;i<oldsize;i++) I[++buckets[old[i]]]=i;
- I[0]=oldsize;
- for(i=0;i<oldsize;i++) V[i]=buckets[old[i]];
- V[oldsize]=0;
- for(i=1;i<256;i++) if(buckets[i]==buckets[i-1]+1) I[buckets[i]]=-1;
- I[0]=-1;
-
- for(h=1;I[0]!=-(oldsize+1);h+=h) {
- len=0;
- for(i=0;i<oldsize+1;) {
- if(I[i]<0) {
- len-=I[i];
- i-=I[i];
- } else {
- if(len) I[i-len]=-len;
- len=V[I[i]]+1-i;
- split(I,V,i,len,h);
- i+=len;
- len=0;
- };
- };
- if(len) I[i-len]=-len;
- };
-
- for(i=0;i<oldsize+1;i++) I[V[i]]=i;
-}
-
-static off_t matchlen(u_char *olddata,off_t oldsize,u_char *newdata,off_t newsize)
-{
- off_t i;
-
- for(i=0;(i<oldsize)&&(i<newsize);i++)
- if(olddata[i]!=newdata[i]) break;
-
- return i;
-}
-
-static off_t search(off_t *I,u_char *old,off_t oldsize,
- u_char *newdata,off_t newsize,off_t st,off_t en,off_t *pos)
-{
- off_t x,y;
-
- if(en-st<2) {
- x=matchlen(old+I[st],oldsize-I[st],newdata,newsize);
- y=matchlen(old+I[en],oldsize-I[en],newdata,newsize);
-
- if(x>y) {
- *pos=I[st];
- return x;
- } else {
- *pos=I[en];
- return y;
- }
- };
-
- x=st+(en-st)/2;
- if(memcmp(old+I[x],newdata,MIN(oldsize-I[x],newsize))<0) {
- return search(I,old,oldsize,newdata,newsize,x,en,pos);
- } else {
- return search(I,old,oldsize,newdata,newsize,st,x,pos);
- };
-}
-
-static void offtout(off_t x,u_char *buf)
-{
- off_t y;
-
- if(x<0) y=-x; else y=x;
-
- buf[0]=y%256;y-=buf[0];
- y=y/256;buf[1]=y%256;y-=buf[1];
- y=y/256;buf[2]=y%256;y-=buf[2];
- y=y/256;buf[3]=y%256;y-=buf[3];
- y=y/256;buf[4]=y%256;y-=buf[4];
- y=y/256;buf[5]=y%256;y-=buf[5];
- y=y/256;buf[6]=y%256;y-=buf[6];
- y=y/256;buf[7]=y%256;
-
- if(x<0) buf[7]|=0x80;
-}
-
-// This is main() from bsdiff.c, with the following changes:
-//
-// - old, oldsize, newdata, newsize are arguments; we don't load this
-// data from files. old and newdata are owned by the caller; we
-// don't free them at the end.
-//
-// - the "I" block of memory is owned by the caller, who passes a
-// pointer to *I, which can be NULL. This way if we call
-// bsdiff() multiple times with the same 'old' data, we only do
-// the qsufsort() step the first time.
-//
-int bsdiff(u_char* old, off_t oldsize, off_t** IP, u_char* newdata, off_t newsize,
- const char* patch_filename)
-{
- int fd;
- off_t *I;
- off_t scan,pos,len;
- off_t lastscan,lastpos,lastoffset;
- off_t oldscore,scsc;
- off_t s,Sf,lenf,Sb,lenb;
- off_t overlap,Ss,lens;
- off_t i;
- off_t dblen,eblen;
- u_char *db,*eb;
- u_char buf[8];
- u_char header[32];
- FILE * pf;
- BZFILE * pfbz2;
- int bz2err;
-
- if (*IP == NULL) {
- off_t* V;
- *IP = reinterpret_cast<off_t*>(malloc((oldsize+1) * sizeof(off_t)));
- V = reinterpret_cast<off_t*>(malloc((oldsize+1) * sizeof(off_t)));
- qsufsort(*IP, V, old, oldsize);
- free(V);
- }
- I = *IP;
-
- if(((db=reinterpret_cast<u_char*>(malloc(newsize+1)))==NULL) ||
- ((eb=reinterpret_cast<u_char*>(malloc(newsize+1)))==NULL)) err(1,NULL);
- dblen=0;
- eblen=0;
-
- /* Create the patch file */
- if ((pf = fopen(patch_filename, "w")) == NULL)
- err(1, "%s", patch_filename);
-
- /* Header is
- 0 8 "BSDIFF40"
- 8 8 length of bzip2ed ctrl block
- 16 8 length of bzip2ed diff block
- 24 8 length of new file */
- /* File is
- 0 32 Header
- 32 ?? Bzip2ed ctrl block
- ?? ?? Bzip2ed diff block
- ?? ?? Bzip2ed extra block */
- memcpy(header,"BSDIFF40",8);
- offtout(0, header + 8);
- offtout(0, header + 16);
- offtout(newsize, header + 24);
- if (fwrite(header, 32, 1, pf) != 1)
- err(1, "fwrite(%s)", patch_filename);
-
- /* Compute the differences, writing ctrl as we go */
- if ((pfbz2 = BZ2_bzWriteOpen(&bz2err, pf, 9, 0, 0)) == NULL)
- errx(1, "BZ2_bzWriteOpen, bz2err = %d", bz2err);
- scan=0;len=0;
- lastscan=0;lastpos=0;lastoffset=0;
- while(scan<newsize) {
- oldscore=0;
-
- for(scsc=scan+=len;scan<newsize;scan++) {
- len=search(I,old,oldsize,newdata+scan,newsize-scan,
- 0,oldsize,&pos);
-
- for(;scsc<scan+len;scsc++)
- if((scsc+lastoffset<oldsize) &&
- (old[scsc+lastoffset] == newdata[scsc]))
- oldscore++;
-
- if(((len==oldscore) && (len!=0)) ||
- (len>oldscore+8)) break;
-
- if((scan+lastoffset<oldsize) &&
- (old[scan+lastoffset] == newdata[scan]))
- oldscore--;
- };
-
- if((len!=oldscore) || (scan==newsize)) {
- s=0;Sf=0;lenf=0;
- for(i=0;(lastscan+i<scan)&&(lastpos+i<oldsize);) {
- if(old[lastpos+i]==newdata[lastscan+i]) s++;
- i++;
- if(s*2-i>Sf*2-lenf) { Sf=s; lenf=i; };
- };
-
- lenb=0;
- if(scan<newsize) {
- s=0;Sb=0;
- for(i=1;(scan>=lastscan+i)&&(pos>=i);i++) {
- if(old[pos-i]==newdata[scan-i]) s++;
- if(s*2-i>Sb*2-lenb) { Sb=s; lenb=i; };
- };
- };
-
- if(lastscan+lenf>scan-lenb) {
- overlap=(lastscan+lenf)-(scan-lenb);
- s=0;Ss=0;lens=0;
- for(i=0;i<overlap;i++) {
- if(newdata[lastscan+lenf-overlap+i]==
- old[lastpos+lenf-overlap+i]) s++;
- if(newdata[scan-lenb+i]==
- old[pos-lenb+i]) s--;
- if(s>Ss) { Ss=s; lens=i+1; };
- };
-
- lenf+=lens-overlap;
- lenb-=lens;
- };
-
- for(i=0;i<lenf;i++)
- db[dblen+i]=newdata[lastscan+i]-old[lastpos+i];
- for(i=0;i<(scan-lenb)-(lastscan+lenf);i++)
- eb[eblen+i]=newdata[lastscan+lenf+i];
-
- dblen+=lenf;
- eblen+=(scan-lenb)-(lastscan+lenf);
-
- offtout(lenf,buf);
- BZ2_bzWrite(&bz2err, pfbz2, buf, 8);
- if (bz2err != BZ_OK)
- errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);
-
- offtout((scan-lenb)-(lastscan+lenf),buf);
- BZ2_bzWrite(&bz2err, pfbz2, buf, 8);
- if (bz2err != BZ_OK)
- errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);
-
- offtout((pos-lenb)-(lastpos+lenf),buf);
- BZ2_bzWrite(&bz2err, pfbz2, buf, 8);
- if (bz2err != BZ_OK)
- errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);
-
- lastscan=scan-lenb;
- lastpos=pos-lenb;
- lastoffset=pos-scan;
- };
- };
- BZ2_bzWriteClose(&bz2err, pfbz2, 0, NULL, NULL);
- if (bz2err != BZ_OK)
- errx(1, "BZ2_bzWriteClose, bz2err = %d", bz2err);
-
- /* Compute size of compressed ctrl data */
- if ((len = ftello(pf)) == -1)
- err(1, "ftello");
- offtout(len-32, header + 8);
-
- /* Write compressed diff data */
- if ((pfbz2 = BZ2_bzWriteOpen(&bz2err, pf, 9, 0, 0)) == NULL)
- errx(1, "BZ2_bzWriteOpen, bz2err = %d", bz2err);
- BZ2_bzWrite(&bz2err, pfbz2, db, dblen);
- if (bz2err != BZ_OK)
- errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);
- BZ2_bzWriteClose(&bz2err, pfbz2, 0, NULL, NULL);
- if (bz2err != BZ_OK)
- errx(1, "BZ2_bzWriteClose, bz2err = %d", bz2err);
-
- /* Compute size of compressed diff data */
- if ((newsize = ftello(pf)) == -1)
- err(1, "ftello");
- offtout(newsize - len, header + 16);
-
- /* Write compressed extra data */
- if ((pfbz2 = BZ2_bzWriteOpen(&bz2err, pf, 9, 0, 0)) == NULL)
- errx(1, "BZ2_bzWriteOpen, bz2err = %d", bz2err);
- BZ2_bzWrite(&bz2err, pfbz2, eb, eblen);
- if (bz2err != BZ_OK)
- errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);
- BZ2_bzWriteClose(&bz2err, pfbz2, 0, NULL, NULL);
- if (bz2err != BZ_OK)
- errx(1, "BZ2_bzWriteClose, bz2err = %d", bz2err);
-
- /* Seek to the beginning, write the header, and close the file */
- if (fseeko(pf, 0, SEEK_SET))
- err(1, "fseeko");
- if (fwrite(header, 32, 1, pf) != 1)
- err(1, "fwrite(%s)", patch_filename);
- if (fclose(pf))
- err(1, "fclose");
-
- /* Free the memory we used */
- free(db);
- free(eb);
-
- return 0;
-}
diff --git a/applypatch/bspatch.cpp b/applypatch/bspatch.cpp
index ebb55f1d1..eb45e9ce9 100644
--- a/applypatch/bspatch.cpp
+++ b/applypatch/bspatch.cpp
@@ -30,7 +30,7 @@
#include <bzlib.h>
#include "openssl/sha.h"
-#include "applypatch.h"
+#include "applypatch/applypatch.h"
void ShowBSDiffLicense() {
puts("The bsdiff library used herein is:\n"
@@ -64,7 +64,7 @@ void ShowBSDiffLicense() {
);
}
-static off_t offtin(u_char *buf)
+static off_t offtin(const u_char *buf)
{
off_t y;
@@ -130,7 +130,7 @@ int ApplyBSDiffPatchMem(const unsigned char* old_data, ssize_t old_size,
// from oldfile to x bytes from the diff block; copy y bytes from the
// extra block; seek forwards in oldfile by z bytes".
- unsigned char* header = (unsigned char*) patch->data + patch_offset;
+ const unsigned char* header = reinterpret_cast<const unsigned char*>(&patch->data[patch_offset]);
if (memcmp(header, "BSDIFF40", 8) != 0) {
printf("corrupt bsdiff patch file header (magic number)\n");
return 1;
@@ -149,7 +149,7 @@ int ApplyBSDiffPatchMem(const unsigned char* old_data, ssize_t old_size,
int bzerr;
bz_stream cstream;
- cstream.next_in = patch->data + patch_offset + 32;
+ cstream.next_in = const_cast<char*>(&patch->data[patch_offset + 32]);
cstream.avail_in = ctrl_len;
cstream.bzalloc = NULL;
cstream.bzfree = NULL;
@@ -159,7 +159,7 @@ int ApplyBSDiffPatchMem(const unsigned char* old_data, ssize_t old_size,
}
bz_stream dstream;
- dstream.next_in = patch->data + patch_offset + 32 + ctrl_len;
+ dstream.next_in = const_cast<char*>(&patch->data[patch_offset + 32 + ctrl_len]);
dstream.avail_in = data_len;
dstream.bzalloc = NULL;
dstream.bzfree = NULL;
@@ -169,8 +169,8 @@ int ApplyBSDiffPatchMem(const unsigned char* old_data, ssize_t old_size,
}
bz_stream estream;
- estream.next_in = patch->data + patch_offset + 32 + ctrl_len + data_len;
- estream.avail_in = patch->size - (patch_offset + 32 + ctrl_len + data_len);
+ estream.next_in = const_cast<char*>(&patch->data[patch_offset + 32 + ctrl_len + data_len]);
+ estream.avail_in = patch->data.size() - (patch_offset + 32 + ctrl_len + data_len);
estream.bzalloc = NULL;
estream.bzfree = NULL;
estream.opaque = NULL;
@@ -182,7 +182,6 @@ int ApplyBSDiffPatchMem(const unsigned char* old_data, ssize_t old_size,
off_t oldpos = 0, newpos = 0;
off_t ctrl[3];
- off_t len_read;
int i;
unsigned char buf[24];
while (newpos < new_size) {
diff --git a/applypatch/freecache.cpp b/applypatch/freecache.cpp
index c84f42797..331cae265 100644
--- a/applypatch/freecache.cpp
+++ b/applypatch/freecache.cpp
@@ -32,7 +32,7 @@
#include <android-base/parseint.h>
#include <android-base/stringprintf.h>
-#include "applypatch.h"
+#include "applypatch/applypatch.h"
static int EliminateOpenFiles(std::set<std::string>* files) {
std::unique_ptr<DIR, decltype(&closedir)> d(opendir("/proc"), closedir);
diff --git a/applypatch/imgdiff.cpp b/applypatch/imgdiff.cpp
index f22502e38..528daf113 100644
--- a/applypatch/imgdiff.cpp
+++ b/applypatch/imgdiff.cpp
@@ -130,6 +130,8 @@
#include <unistd.h>
#include <sys/types.h>
+#include <bsdiff.h>
+
#include "zlib.h"
#include "imgdiff.h"
#include "utils.h"
@@ -144,8 +146,6 @@ typedef struct {
size_t source_start;
size_t source_len;
- off_t* I; // used by bsdiff
-
// --- for CHUNK_DEFLATE chunks only: ---
// original (compressed) deflate data
@@ -179,10 +179,6 @@ static int fileentry_compare(const void* a, const void* b) {
}
}
-// from bsdiff.c
-int bsdiff(u_char* old, off_t oldsize, off_t** IP, u_char* newdata, off_t newsize,
- const char* patch_filename);
-
unsigned char* ReadZip(const char* filename,
int* num_chunks, ImageChunk** chunks,
int include_pseudo_chunk) {
@@ -193,7 +189,7 @@ unsigned char* ReadZip(const char* filename,
}
size_t sz = static_cast<size_t>(st.st_size);
- unsigned char* img = reinterpret_cast<unsigned char*>(malloc(sz));
+ unsigned char* img = static_cast<unsigned char*>(malloc(sz));
FILE* f = fopen(filename, "rb");
if (fread(img, 1, sz, f) != sz) {
printf("failed to read \"%s\" %s\n", filename, strerror(errno));
@@ -220,7 +216,7 @@ unsigned char* ReadZip(const char* filename,
int cdcount = Read2(img+i+8);
int cdoffset = Read4(img+i+16);
- ZipFileEntry* temp_entries = reinterpret_cast<ZipFileEntry*>(malloc(
+ ZipFileEntry* temp_entries = static_cast<ZipFileEntry*>(malloc(
cdcount * sizeof(ZipFileEntry)));
int entrycount = 0;
@@ -228,6 +224,7 @@ unsigned char* ReadZip(const char* filename,
for (i = 0; i < cdcount; ++i) {
if (!(cd[0] == 0x50 && cd[1] == 0x4b && cd[2] == 0x01 && cd[3] == 0x02)) {
printf("bad central directory entry %d\n", i);
+ free(temp_entries);
return NULL;
}
@@ -238,7 +235,7 @@ unsigned char* ReadZip(const char* filename,
int mlen = Read2(cd+32); // file comment len
int hoffset = Read4(cd+42); // local header offset
- char* filename = reinterpret_cast<char*>(malloc(nlen+1));
+ char* filename = static_cast<char*>(malloc(nlen+1));
memcpy(filename, cd+46, nlen);
filename[nlen] = '\0';
@@ -287,7 +284,7 @@ unsigned char* ReadZip(const char* filename,
#endif
*num_chunks = 0;
- *chunks = reinterpret_cast<ImageChunk*>(malloc((entrycount*2+2) * sizeof(ImageChunk)));
+ *chunks = static_cast<ImageChunk*>(malloc((entrycount*2+2) * sizeof(ImageChunk)));
ImageChunk* curr = *chunks;
if (include_pseudo_chunk) {
@@ -296,7 +293,6 @@ unsigned char* ReadZip(const char* filename,
curr->len = st.st_size;
curr->data = img;
curr->filename = NULL;
- curr->I = NULL;
++curr;
++*num_chunks;
}
@@ -311,10 +307,9 @@ unsigned char* ReadZip(const char* filename,
curr->deflate_len = temp_entries[nextentry].deflate_len;
curr->deflate_data = img + pos;
curr->filename = temp_entries[nextentry].filename;
- curr->I = NULL;
curr->len = temp_entries[nextentry].uncomp_len;
- curr->data = reinterpret_cast<unsigned char*>(malloc(curr->len));
+ curr->data = static_cast<unsigned char*>(malloc(curr->len));
z_stream strm;
strm.zalloc = Z_NULL;
@@ -326,6 +321,10 @@ unsigned char* ReadZip(const char* filename,
// -15 means we are decoding a 'raw' deflate stream; zlib will
// not expect zlib headers.
int ret = inflateInit2(&strm, -15);
+ if (ret < 0) {
+ printf("failed to initialize inflate: %d\n", ret);
+ return NULL;
+ }
strm.avail_out = curr->len;
strm.next_out = curr->data;
@@ -356,7 +355,6 @@ unsigned char* ReadZip(const char* filename,
}
curr->data = img + pos;
curr->filename = NULL;
- curr->I = NULL;
pos += curr->len;
++*num_chunks;
@@ -385,7 +383,7 @@ unsigned char* ReadImage(const char* filename,
}
size_t sz = static_cast<size_t>(st.st_size);
- unsigned char* img = reinterpret_cast<unsigned char*>(malloc(sz + 4));
+ unsigned char* img = static_cast<unsigned char*>(malloc(sz + 4));
FILE* f = fopen(filename, "rb");
if (fread(img, 1, sz, f) != sz) {
printf("failed to read \"%s\" %s\n", filename, strerror(errno));
@@ -407,7 +405,6 @@ unsigned char* ReadImage(const char* filename,
while (pos < sz) {
unsigned char* p = img+pos;
- bool processed_deflate = false;
if (sz - pos >= 4 &&
p[0] == 0x1f && p[1] == 0x8b &&
p[2] == 0x08 && // deflate compression
@@ -416,7 +413,7 @@ unsigned char* ReadImage(const char* filename,
size_t chunk_offset = pos;
*num_chunks += 3;
- *chunks = reinterpret_cast<ImageChunk*>(realloc(*chunks,
+ *chunks = static_cast<ImageChunk*>(realloc(*chunks,
*num_chunks * sizeof(ImageChunk)));
ImageChunk* curr = *chunks + (*num_chunks-3);
@@ -425,7 +422,6 @@ unsigned char* ReadImage(const char* filename,
curr->type = CHUNK_NORMAL;
curr->len = GZIP_HEADER_LEN;
curr->data = p;
- curr->I = NULL;
pos += curr->len;
p += curr->len;
@@ -433,7 +429,6 @@ unsigned char* ReadImage(const char* filename,
curr->type = CHUNK_DEFLATE;
curr->filename = NULL;
- curr->I = NULL;
// We must decompress this chunk in order to discover where it
// ends, and so we can put the uncompressed data and its length
@@ -441,7 +436,7 @@ unsigned char* ReadImage(const char* filename,
size_t allocated = 32768;
curr->len = 0;
- curr->data = reinterpret_cast<unsigned char*>(malloc(allocated));
+ curr->data = static_cast<unsigned char*>(malloc(allocated));
curr->start = pos;
curr->deflate_data = p;
@@ -455,34 +450,37 @@ unsigned char* ReadImage(const char* filename,
// -15 means we are decoding a 'raw' deflate stream; zlib will
// not expect zlib headers.
int ret = inflateInit2(&strm, -15);
+ if (ret < 0) {
+ printf("failed to initialize inflate: %d\n", ret);
+ return NULL;
+ }
do {
strm.avail_out = allocated - curr->len;
strm.next_out = curr->data + curr->len;
ret = inflate(&strm, Z_NO_FLUSH);
if (ret < 0) {
- if (!processed_deflate) {
- // This is the first chunk, assume that it's just a spurious
- // gzip header instead of a real one.
- break;
- }
- printf("Error: inflate failed [%s] at file offset [%zu]\n"
- "imgdiff only supports gzip kernel compression,"
- " did you try CONFIG_KERNEL_LZO?\n",
+ printf("Warning: inflate failed [%s] at offset [%zu],"
+ " treating as a normal chunk\n",
strm.msg, chunk_offset);
- free(img);
- return NULL;
+ break;
}
curr->len = allocated - strm.avail_out;
if (strm.avail_out == 0) {
allocated *= 2;
- curr->data = reinterpret_cast<unsigned char*>(realloc(curr->data, allocated));
+ curr->data = static_cast<unsigned char*>(realloc(curr->data, allocated));
}
- processed_deflate = true;
} while (ret != Z_STREAM_END);
curr->deflate_len = sz - strm.avail_in - pos;
inflateEnd(&strm);
+
+ if (ret < 0) {
+ free(curr->data);
+ *num_chunks -= 2;
+ continue;
+ }
+
pos += curr->deflate_len;
p += curr->deflate_len;
++curr;
@@ -493,7 +491,6 @@ unsigned char* ReadImage(const char* filename,
curr->start = pos;
curr->len = GZIP_FOOTER_LEN;
curr->data = img+pos;
- curr->I = NULL;
pos += curr->len;
p += curr->len;
@@ -514,10 +511,9 @@ unsigned char* ReadImage(const char* filename,
// Reallocate the list for every chunk; we expect the number of
// chunks to be small (5 for typical boot and recovery images).
++*num_chunks;
- *chunks = reinterpret_cast<ImageChunk*>(realloc(*chunks, *num_chunks * sizeof(ImageChunk)));
+ *chunks = static_cast<ImageChunk*>(realloc(*chunks, *num_chunks * sizeof(ImageChunk)));
ImageChunk* curr = *chunks + (*num_chunks-1);
curr->start = pos;
- curr->I = NULL;
// 'pos' is not the offset of the start of a gzip chunk, so scan
// forward until we find a gzip header.
@@ -565,10 +561,18 @@ int TryReconstruction(ImageChunk* chunk, unsigned char* out) {
int ret;
ret = deflateInit2(&strm, chunk->level, chunk->method, chunk->windowBits,
chunk->memLevel, chunk->strategy);
+ if (ret < 0) {
+ printf("failed to initialize deflate: %d\n", ret);
+ return -1;
+ }
do {
strm.avail_out = BUFFER_SIZE;
strm.next_out = out;
ret = deflate(&strm, Z_FINISH);
+ if (ret < 0) {
+ printf("failed to deflate: %d\n", ret);
+ return -1;
+ }
size_t have = BUFFER_SIZE - strm.avail_out;
if (memcmp(out, chunk->deflate_data+p, have) != 0) {
@@ -598,8 +602,7 @@ int ReconstructDeflateChunk(ImageChunk* chunk) {
return -1;
}
- size_t p = 0;
- unsigned char* out = reinterpret_cast<unsigned char*>(malloc(BUFFER_SIZE));
+ unsigned char* out = static_cast<unsigned char*>(malloc(BUFFER_SIZE));
// We only check two combinations of encoder parameters: level 6
// (the default) and level 9 (the maximum).
@@ -645,7 +648,7 @@ unsigned char* MakePatch(ImageChunk* src, ImageChunk* tgt, size_t* size) {
close(fd); // temporary file is created and we don't need its file
// descriptor
- int r = bsdiff(src->data, src->len, &(src->I), tgt->data, tgt->len, ptemp);
+ int r = bsdiff::bsdiff(src->data, src->len, tgt->data, tgt->len, ptemp);
if (r != 0) {
printf("bsdiff() failed: %d\n", r);
return NULL;
@@ -660,7 +663,7 @@ unsigned char* MakePatch(ImageChunk* src, ImageChunk* tgt, size_t* size) {
size_t sz = static_cast<size_t>(st.st_size);
// TODO: Memory leak on error return.
- unsigned char* data = reinterpret_cast<unsigned char*>(malloc(sz));
+ unsigned char* data = static_cast<unsigned char*>(malloc(sz));
if (tgt->type == CHUNK_NORMAL && tgt->len <= sz) {
unlink(ptemp);
@@ -827,7 +830,7 @@ int main(int argc, char** argv) {
return 1;
}
bonus_size = st.st_size;
- bonus_data = reinterpret_cast<unsigned char*>(malloc(bonus_size));
+ bonus_data = static_cast<unsigned char*>(malloc(bonus_size));
FILE* f = fopen(argv[2], "rb");
if (f == NULL) {
printf("failed to open bonus file %s: %s\n", argv[2], strerror(errno));
@@ -844,7 +847,6 @@ int main(int argc, char** argv) {
}
if (argc != 4) {
- usage:
printf("usage: %s [-z] [-b <bonus-file>] <src-img> <tgt-img> <patch-file>\n",
argv[0]);
return 2;
@@ -974,9 +976,9 @@ int main(int argc, char** argv) {
DumpChunks(src_chunks, num_src_chunks);
printf("Construct patches for %d chunks...\n", num_tgt_chunks);
- unsigned char** patch_data = reinterpret_cast<unsigned char**>(malloc(
+ unsigned char** patch_data = static_cast<unsigned char**>(malloc(
num_tgt_chunks * sizeof(unsigned char*)));
- size_t* patch_size = reinterpret_cast<size_t*>(malloc(num_tgt_chunks * sizeof(size_t)));
+ size_t* patch_size = static_cast<size_t*>(malloc(num_tgt_chunks * sizeof(size_t)));
for (i = 0; i < num_tgt_chunks; ++i) {
if (zip_mode) {
ImageChunk* src;
@@ -990,7 +992,7 @@ int main(int argc, char** argv) {
} else {
if (i == 1 && bonus_data) {
printf(" using %zu bytes of bonus data for chunk %d\n", bonus_size, i);
- src_chunks[i].data = reinterpret_cast<unsigned char*>(realloc(src_chunks[i].data,
+ src_chunks[i].data = static_cast<unsigned char*>(realloc(src_chunks[i].data,
src_chunks[i].len + bonus_size));
memcpy(src_chunks[i].data+src_chunks[i].len, bonus_data, bonus_size);
src_chunks[i].len += bonus_size;
@@ -1077,6 +1079,9 @@ int main(int argc, char** argv) {
}
}
+ free(patch_data);
+ free(patch_size);
+
fclose(f);
return 0;
diff --git a/applypatch/imgpatch.cpp b/applypatch/imgpatch.cpp
index d175d6385..1c4409e36 100644
--- a/applypatch/imgpatch.cpp
+++ b/applypatch/imgpatch.cpp
@@ -24,21 +24,22 @@
#include <unistd.h>
#include <string.h>
+#include <string>
#include <vector>
#include "zlib.h"
#include "openssl/sha.h"
-#include "applypatch.h"
+#include "applypatch/applypatch.h"
#include "imgdiff.h"
#include "utils.h"
int ApplyImagePatch(const unsigned char* old_data, ssize_t old_size,
const unsigned char* patch_data, ssize_t patch_size,
SinkFn sink, void* token) {
- Value patch = {VAL_BLOB, patch_size,
- reinterpret_cast<char*>(const_cast<unsigned char*>(patch_data))};
- return ApplyImagePatch(
- old_data, old_size, &patch, sink, token, nullptr, nullptr);
+ 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);
}
/*
@@ -51,9 +52,7 @@ int ApplyImagePatch(const unsigned char* old_data, ssize_t old_size,
const Value* patch,
SinkFn sink, void* token, SHA_CTX* ctx,
const Value* bonus_data) {
- ssize_t pos = 12;
- char* header = patch->data;
- if (patch->size < 12) {
+ if (patch->data.size() < 12) {
printf("patch too short to contain header\n");
return -1;
}
@@ -61,6 +60,8 @@ int ApplyImagePatch(const unsigned char* old_data, ssize_t old_size,
// IMGDIFF2 uses CHUNK_NORMAL, CHUNK_DEFLATE, and CHUNK_RAW.
// (IMGDIFF1, which is no longer supported, used CHUNK_NORMAL and
// CHUNK_GZIP.)
+ size_t pos = 12;
+ const char* header = &patch->data[0];
if (memcmp(header, "IMGDIFF2", 8) != 0) {
printf("corrupt patch file header (magic number)\n");
return -1;
@@ -68,20 +69,19 @@ int ApplyImagePatch(const unsigned char* old_data, ssize_t old_size,
int num_chunks = Read4(header+8);
- int i;
- for (i = 0; i < num_chunks; ++i) {
+ for (int i = 0; i < num_chunks; ++i) {
// each chunk's header record starts with 4 bytes.
- if (pos + 4 > patch->size) {
+ if (pos + 4 > patch->data.size()) {
printf("failed to read chunk %d record\n", i);
return -1;
}
- int type = Read4(patch->data + pos);
+ int type = Read4(&patch->data[pos]);
pos += 4;
if (type == CHUNK_NORMAL) {
- char* normal_header = patch->data + pos;
+ const char* normal_header = &patch->data[pos];
pos += 24;
- if (pos > patch->size) {
+ if (pos > patch->data.size()) {
printf("failed to read chunk %d normal header data\n", i);
return -1;
}
@@ -97,21 +97,21 @@ int ApplyImagePatch(const unsigned char* old_data, ssize_t old_size,
ApplyBSDiffPatch(old_data + src_start, src_len,
patch, patch_offset, sink, token, ctx);
} else if (type == CHUNK_RAW) {
- char* raw_header = patch->data + pos;
+ const char* raw_header = &patch->data[pos];
pos += 4;
- if (pos > patch->size) {
+ if (pos > patch->data.size()) {
printf("failed to read chunk %d raw header data\n", i);
return -1;
}
ssize_t data_len = Read4(raw_header);
- if (pos + data_len > patch->size) {
+ if (pos + data_len > patch->data.size()) {
printf("failed to read chunk %d raw data\n", i);
return -1;
}
- if (ctx) SHA1_Update(ctx, patch->data + pos, data_len);
- if (sink((unsigned char*)patch->data + pos,
+ 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) {
printf("failed to write chunk %d raw data\n", i);
return -1;
@@ -119,9 +119,9 @@ int ApplyImagePatch(const unsigned char* old_data, ssize_t old_size,
pos += data_len;
} else if (type == CHUNK_DEFLATE) {
// deflate chunks have an additional 60 bytes in their chunk header.
- char* deflate_header = patch->data + pos;
+ const char* deflate_header = &patch->data[pos];
pos += 60;
- if (pos > patch->size) {
+ if (pos > patch->data.size()) {
printf("failed to read chunk %d deflate header data\n", i);
return -1;
}
@@ -130,6 +130,7 @@ int ApplyImagePatch(const unsigned char* old_data, ssize_t old_size,
size_t src_len = Read8(deflate_header+8);
size_t patch_offset = Read8(deflate_header+16);
size_t expanded_len = Read8(deflate_header+24);
+ size_t target_len = Read8(deflate_header+32);
int level = Read4(deflate_header+40);
int method = Read4(deflate_header+44);
int windowBits = Read4(deflate_header+48);
@@ -148,7 +149,7 @@ int ApplyImagePatch(const unsigned char* old_data, ssize_t old_size,
// the patch was constructed with bonus data. The
// deflation will come up 'bonus_size' bytes short; these
// must be appended from the bonus_data value.
- size_t bonus_size = (i == 1 && bonus_data != NULL) ? bonus_data->size : 0;
+ size_t bonus_size = (i == 1 && bonus_data != NULL) ? bonus_data->data.size() : 0;
std::vector<unsigned char> expanded_source(expanded_len);
@@ -188,7 +189,7 @@ int ApplyImagePatch(const unsigned char* old_data, ssize_t old_size,
if (bonus_size) {
memcpy(expanded_source.data() + (expanded_len - bonus_size),
- bonus_data->data, bonus_size);
+ &bonus_data->data[0], bonus_size);
}
}
@@ -200,6 +201,11 @@ int ApplyImagePatch(const unsigned char* old_data, ssize_t old_size,
&uncompressed_target_data) != 0) {
return -1;
}
+ if (uncompressed_target_data.size() != target_len) {
+ printf("expected target len to be %zu, but it's %zu\n",
+ target_len, uncompressed_target_data.size());
+ return -1;
+ }
// Now compress the target data and append it to the output.
@@ -231,8 +237,8 @@ int ApplyImagePatch(const unsigned char* old_data, ssize_t old_size,
ssize_t have = temp_data.size() - strm.avail_out;
if (sink(temp_data.data(), have, token) != have) {
- printf("failed to write %ld compressed bytes to output\n",
- (long)have);
+ printf("failed to write %zd compressed bytes to output\n",
+ have);
return -1;
}
if (ctx) SHA1_Update(ctx, temp_data.data(), have);
diff --git a/applypatch/applypatch.h b/applypatch/include/applypatch/applypatch.h
index f392c5534..ca3dafbc9 100644
--- a/applypatch/applypatch.h
+++ b/applypatch/include/applypatch/applypatch.h
@@ -17,11 +17,15 @@
#ifndef _APPLYPATCH_H
#define _APPLYPATCH_H
+#include <stdint.h>
#include <sys/stat.h>
+#include <memory>
+#include <string>
#include <vector>
-#include "openssl/sha.h"
+#include <openssl/sha.h>
+
#include "edify/expr.h"
struct FileContents {
@@ -39,31 +43,27 @@ struct FileContents {
typedef ssize_t (*SinkFn)(const unsigned char*, ssize_t, void*);
-// applypatch.c
+// applypatch.cpp
int ShowLicenses();
size_t FreeSpaceForFile(const char* filename);
int CacheSizeCheck(size_t bytes);
int ParseSha1(const char* str, uint8_t* digest);
-int applypatch_flash(const char* source_filename, const char* target_filename,
- const char* target_sha1_str, size_t target_size);
int applypatch(const char* source_filename,
const char* target_filename,
const char* target_sha1_str,
size_t target_size,
- int num_patches,
- char** const patch_sha1_str,
- Value** patch_data,
- Value* bonus_data);
+ const std::vector<std::string>& patch_sha1_str,
+ const std::vector<std::unique_ptr<Value>>& patch_data,
+ const Value* bonus_data);
int applypatch_check(const char* filename,
- int num_patches,
- char** const patch_sha1_str);
+ const std::vector<std::string>& patch_sha1_str);
+int applypatch_flash(const char* source_filename, const char* target_filename,
+ const char* target_sha1_str, size_t target_size);
int LoadFileContents(const char* filename, FileContents* file);
int SaveFileContents(const char* filename, const FileContents* file);
-void FreeFileContents(FileContents* file);
-int FindMatchingPatch(uint8_t* sha1, char* const * const patch_sha1_str,
- int num_patches);
+int FindMatchingPatch(uint8_t* sha1, const std::vector<std::string>& patch_sha1_str);
// bsdiff.cpp
void ShowBSDiffLicense();
diff --git a/applypatch/libimgpatch.pc b/applypatch/libimgpatch.pc
new file mode 100644
index 000000000..e5002934f
--- /dev/null
+++ b/applypatch/libimgpatch.pc
@@ -0,0 +1,6 @@
+# This file is for libimgpatch in Chrome OS.
+
+Name: libimgpatch
+Description: Apply imgdiff patch
+Version: 0.0.1
+Libs: -limgpatch -lbz2 -lz
diff --git a/applypatch/utils.cpp b/applypatch/utils.cpp
index 4a80be75f..450dc8d76 100644
--- a/applypatch/utils.cpp
+++ b/applypatch/utils.cpp
@@ -27,7 +27,7 @@ void Write4(int value, FILE* f) {
}
/** Write an 8-byte value to f in little-endian order. */
-void Write8(long long value, FILE* f) {
+void Write8(int64_t value, FILE* f) {
fputc(value & 0xff, f);
fputc((value >> 8) & 0xff, f);
fputc((value >> 16) & 0xff, f);
@@ -38,28 +38,28 @@ void Write8(long long value, FILE* f) {
fputc((value >> 56) & 0xff, f);
}
-int Read2(void* pv) {
- unsigned char* p = reinterpret_cast<unsigned char*>(pv);
+int Read2(const void* pv) {
+ const unsigned char* p = reinterpret_cast<const unsigned char*>(pv);
return (int)(((unsigned int)p[1] << 8) |
(unsigned int)p[0]);
}
-int Read4(void* pv) {
- unsigned char* p = reinterpret_cast<unsigned char*>(pv);
+int Read4(const void* pv) {
+ const unsigned char* p = reinterpret_cast<const unsigned char*>(pv);
return (int)(((unsigned int)p[3] << 24) |
((unsigned int)p[2] << 16) |
((unsigned int)p[1] << 8) |
(unsigned int)p[0]);
}
-long long Read8(void* pv) {
- unsigned char* p = reinterpret_cast<unsigned char*>(pv);
- return (long long)(((unsigned long long)p[7] << 56) |
- ((unsigned long long)p[6] << 48) |
- ((unsigned long long)p[5] << 40) |
- ((unsigned long long)p[4] << 32) |
- ((unsigned long long)p[3] << 24) |
- ((unsigned long long)p[2] << 16) |
- ((unsigned long long)p[1] << 8) |
- (unsigned long long)p[0]);
+int64_t Read8(const void* pv) {
+ const unsigned char* p = reinterpret_cast<const unsigned char*>(pv);
+ return (int64_t)(((uint64_t)p[7] << 56) |
+ ((uint64_t)p[6] << 48) |
+ ((uint64_t)p[5] << 40) |
+ ((uint64_t)p[4] << 32) |
+ ((uint64_t)p[3] << 24) |
+ ((uint64_t)p[2] << 16) |
+ ((uint64_t)p[1] << 8) |
+ (uint64_t)p[0]);
}
diff --git a/applypatch/utils.h b/applypatch/utils.h
index bc97f1720..c7c8e90e2 100644
--- a/applypatch/utils.h
+++ b/applypatch/utils.h
@@ -17,14 +17,15 @@
#ifndef _BUILD_TOOLS_APPLYPATCH_UTILS_H
#define _BUILD_TOOLS_APPLYPATCH_UTILS_H
+#include <inttypes.h>
#include <stdio.h>
// Read and write little-endian values of various sizes.
void Write4(int value, FILE* f);
-void Write8(long long value, FILE* f);
-int Read2(void* p);
-int Read4(void* p);
-long long Read8(void* p);
+void Write8(int64_t value, FILE* f);
+int Read2(const void* p);
+int Read4(const void* p);
+int64_t Read8(const void* p);
#endif // _BUILD_TOOLS_APPLYPATCH_UTILS_H