summaryrefslogtreecommitdiffstats
path: root/applypatch
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--applypatch/Android.mk166
-rw-r--r--applypatch/Makefile33
-rw-r--r--applypatch/applypatch.cpp1056
-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.cpp205
-rw-r--r--applypatch/freecache.cpp2
-rw-r--r--applypatch/imgdiff.cpp1329
-rw-r--r--applypatch/imgdiff_main.cpp21
-rw-r--r--applypatch/imgpatch.cpp415
-rw-r--r--applypatch/include/applypatch/applypatch.h (renamed from applypatch/applypatch.h)27
-rw-r--r--applypatch/include/applypatch/imgdiff.h (renamed from applypatch/imgdiff.h)21
-rw-r--r--applypatch/include/applypatch/imgpatch.h10
-rw-r--r--applypatch/libimgpatch.pc6
-rw-r--r--applypatch/utils.cpp65
-rw-r--r--private/install.h (renamed from applypatch/utils.h)19
18 files changed, 1663 insertions, 2276 deletions
diff --git a/applypatch/Android.mk b/applypatch/Android.mk
index 2f96f0ab7..9cbe3e2a9 100644
--- a/applypatch/Android.mk
+++ b/applypatch/Android.mk
@@ -31,6 +31,7 @@ $(foreach board_define,$(BOARD_RECOVERY_DEFINES), \
)
LOCAL_C_INCLUDES += \
+ $(LOCAL_PATH)/include \
external/bzip2 \
external/zlib \
$(commands_recovery_local_path)
@@ -42,27 +43,99 @@ LOCAL_MODULE_TAGS := eng
LOCAL_C_INCLUDES += $(RECOVERY_PATH)
LOCAL_STATIC_LIBRARIES += libbase libotafault libmtdutils libcrypto_static libbz libz
+# libapplypatch (static library)
+# ===============================
+include $(CLEAR_VARS)
+LOCAL_SRC_FILES := \
+ applypatch.cpp \
+ bspatch.cpp \
+ freecache.cpp \
+ imgpatch.cpp
+LOCAL_MODULE := libapplypatch
+LOCAL_MODULE_TAGS := eng
+LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
+LOCAL_C_INCLUDES := \
+ $(LOCAL_PATH)/include \
+ $(commands_recovery_local_path)
+LOCAL_STATIC_LIBRARIES := \
+ libotafault \
+ libbase \
+ libcrypto \
+ libbspatch \
+ libbz \
+ libz
+LOCAL_WHOLE_STATIC_LIBRARIES += libmtdutils
+LOCAL_CFLAGS := \
+ -DZLIB_CONST \
+ -Werror
include $(BUILD_STATIC_LIBRARY)
+# libimgpatch (static library)
+# ===============================
include $(CLEAR_VARS)
-ifeq ($(HOST_OS),linux)
-include $(CLEAR_VARS)
+LOCAL_SRC_FILES := \
+ bspatch.cpp \
+ imgpatch.cpp
+LOCAL_MODULE := libimgpatch
+LOCAL_C_INCLUDES := \
+ $(LOCAL_PATH)/include \
+ $(commands_recovery_local_path)
+LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
+LOCAL_STATIC_LIBRARIES := \
+ libcrypto \
+ libbspatch \
+ libbase \
+ libbz \
+ libz
+LOCAL_CFLAGS := \
+ -DZLIB_CONST \
+ -Werror
+include $(BUILD_STATIC_LIBRARY)
-LOCAL_CLANG := true
-LOCAL_SRC_FILES := bspatch.cpp imgpatch.cpp utils.cpp
+# libimgpatch (host static library)
+# ===============================
+include $(CLEAR_VARS)
+LOCAL_SRC_FILES := \
+ bspatch.cpp \
+ imgpatch.cpp
LOCAL_MODULE := libimgpatch
LOCAL_C_INCLUDES += $(RECOVERY_PATH)
+LOCAL_MODULE_HOST_OS := linux
+LOCAL_C_INCLUDES := \
+ $(LOCAL_PATH)/include \
+ $(commands_recovery_local_path)
LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
-LOCAL_STATIC_LIBRARIES += libcrypto_static libbz libz
-
+LOCAL_STATIC_LIBRARIES := \
+ libcrypto \
+ libbspatch \
+ libbase \
+ libbz \
+ libz
+LOCAL_CFLAGS := \
+ -DZLIB_CONST \
+ -Werror
include $(BUILD_HOST_STATIC_LIBRARY)
-endif # HOST_OS == linux
+# libapplypatch_modes (static library)
+# ===============================
include $(CLEAR_VARS)
+LOCAL_SRC_FILES := \
+ applypatch_modes.cpp
+LOCAL_MODULE := libapplypatch_modes
+LOCAL_C_INCLUDES := $(commands_recovery_local_path)
+LOCAL_STATIC_LIBRARIES := \
+ libapplypatch \
+ libbase \
+ libedify \
+ libcrypto
+LOCAL_CFLAGS := -Werror
+include $(BUILD_STATIC_LIBRARY)
-LOCAL_CLANG := true
-LOCAL_SRC_FILES := main.cpp
+# applypatch (target executable)
+# ===============================
+include $(CLEAR_VARS)
+LOCAL_SRC_FILES := applypatch_main.cpp
LOCAL_MODULE := applypatch
LOCAL_C_INCLUDES += $(RECOVERY_PATH)
LOCAL_STATIC_LIBRARIES += libapplypatch libbase libotafault libmtdutils libcrypto_static libbz \
@@ -70,15 +143,78 @@ LOCAL_STATIC_LIBRARIES += libapplypatch libbase libotafault libmtdutils libcrypt
LOCAL_SHARED_LIBRARIES += libz libcutils libc
+LOCAL_C_INCLUDES := $(commands_recovery_local_path)
+LOCAL_STATIC_LIBRARIES := \
+ libapplypatch_modes \
+ libapplypatch \
+ libbase \
+ libedify \
+ libotafault \
+ libcrypto \
+ libbspatch \
+ libbz
+LOCAL_SHARED_LIBRARIES := \
+ libbase \
+ libz \
+ libcutils
+LOCAL_CFLAGS := -Werror
include $(BUILD_EXECUTABLE)
+libimgdiff_src_files := imgdiff.cpp
+
+# libbsdiff is compiled with -D_FILE_OFFSET_BITS=64.
+libimgdiff_cflags := \
+ -Werror \
+ -D_FILE_OFFSET_BITS=64
+
+libimgdiff_static_libraries := \
+ libbsdiff \
+ libdivsufsort \
+ libdivsufsort64 \
+ libziparchive \
+ libutils \
+ liblog \
+ libbase \
+ libz
+
+# libimgdiff (static library)
+# ===============================
include $(CLEAR_VARS)
+LOCAL_SRC_FILES := \
+ $(libimgdiff_src_files)
+LOCAL_MODULE := libimgdiff
+LOCAL_CFLAGS := \
+ $(libimgdiff_cflags)
+LOCAL_STATIC_LIBRARIES := \
+ $(libimgdiff_static_libraries)
+LOCAL_C_INCLUDES := \
+ $(LOCAL_PATH)/include
+LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
+include $(BUILD_STATIC_LIBRARY)
-LOCAL_CLANG := true
-LOCAL_SRC_FILES := imgdiff.cpp utils.cpp bsdiff.cpp
-LOCAL_MODULE := imgdiff
-LOCAL_FORCE_STATIC_EXECUTABLE := true
-LOCAL_C_INCLUDES += external/zlib external/bzip2
-LOCAL_STATIC_LIBRARIES += libz libbz
+# libimgdiff (host static library)
+# ===============================
+include $(CLEAR_VARS)
+LOCAL_SRC_FILES := \
+ $(libimgdiff_src_files)
+LOCAL_MODULE := libimgdiff
+LOCAL_CFLAGS := \
+ $(libimgdiff_cflags)
+LOCAL_STATIC_LIBRARIES := \
+ $(libimgdiff_static_libraries)
+LOCAL_C_INCLUDES := \
+ $(LOCAL_PATH)/include
+LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
+include $(BUILD_HOST_STATIC_LIBRARY)
+# imgdiff (host static executable)
+# ===============================
+include $(CLEAR_VARS)
+LOCAL_SRC_FILES := imgdiff_main.cpp
+LOCAL_MODULE := imgdiff
+LOCAL_CFLAGS := -Werror
+LOCAL_STATIC_LIBRARIES := \
+ libimgdiff \
+ $(libimgdiff_static_libraries) \
+ 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 c2fe11b3b..54c37eb34 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,72 +29,65 @@
#include <memory>
#include <string>
+#include <utility>
+#include <vector>
+#include <android-base/logging.h>
+#include <android-base/parseint.h>
#include <android-base/strings.h>
+#include <openssl/sha.h>
-#include "openssl/sha.h"
-#include "applypatch.h"
#include "bmlutils/bmlutils.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,
- FileContents* copy_file,
- const Value* copy_patch_value,
- const char* source_filename,
- const char* target_filename,
- const uint8_t target_sha1[SHA_DIGEST_LENGTH],
- size_t target_size,
- const Value* bonus_data);
+static int GenerateTarget(const FileContents& source_file, const std::unique_ptr<Value>& patch,
+ const std::string& target_filename,
+ const uint8_t target_sha1[SHA_DIGEST_LENGTH], const Value* bonus_data);
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 ||
- strncmp(filename, "BML:", 4) == 0) {
- return LoadPartitionContents(filename, file);
- }
-
- if (stat(filename, &file->st) != 0) {
- printf("failed to stat \"%s\": %s\n", filename, strerror(errno));
- return -1;
- }
+ // 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 ||
+ strncmp(filename, "BML:", 4) == 0) {
+ return LoadPartitionContents(filename, file);
+ }
+
+ 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.
@@ -106,223 +101,166 @@ int LoadFileContents(const char* filename, FileContents* file) {
// 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 if (pieces[0] == "BML") {
- 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) {
+ printf("LoadPartitionContents called with bad filename \"%s\"\n", filename.c_str());
+ return -1;
+ }
+
+ enum PartitionType type;
+ if (pieces[0] == "MTD") {
+ type = MTD;
+ } else if (pieces[0] == "EMMC") {
+ type = EMMC;
+ } else if (pieces[0] == "BML") {
+ type = EMMC;
+ } else {
+ 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;
+ }
+ SHA1_Update(&sha_ctx, buffer_ptr, read);
+ buffer_size += read;
+ buffer_ptr += read;
}
- const char* partition = pieces[1].c_str();
if (pieces[0] == "BML") {
- if (strcmp(partition, "boot") == 0) {
- partition = BOARD_BML_BOOT;
- } else if (strcmp(partition, "recovery") == 0) {
- partition = BOARD_BML_RECOVERY;
- }
+ if (strcmp(partition, "boot") == 0) {
+ partition = BOARD_BML_BOOT;
+ } else if (strcmp(partition, "recovery") == 0) {
+ partition = BOARD_BML_RECOVERY;
+ }
}
- 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;
- }
-
- // 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;
- }
+ // 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);
- 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;
- }
- }
-
- 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) {
+int WriteToPartition(const unsigned char* data, size_t len, const std::string& target) {
std::string copy(target);
std::vector<std::string> pieces = android::base::Split(copy, ":");
if (pieces.size() < 2) {
- printf("WriteToPartition called with bad target (%s)\n", target);
+ printf("WriteToPartition called with bad target (%s)\n", target.c_str());
return -1;
}
@@ -334,7 +272,7 @@ int WriteToPartition(const unsigned char* data, size_t len, const char* target)
} else if (pieces[0] == "BML") {
type = EMMC;
} else {
- printf("WriteToPartition called with bad target (%s)\n", target);
+ printf("WriteToPartition called with bad target (%s)\n", target.c_str());
return -1;
}
@@ -358,7 +296,7 @@ int WriteToPartition(const unsigned char* data, size_t len, const char* target)
}
if (partition == NULL) {
- printf("bad partition target name \"%s\"\n", target);
+ printf("bad partition target name \"%s\"\n", target.c_str());
return -1;
}
@@ -404,7 +342,7 @@ int WriteToPartition(const unsigned char* data, size_t len, const char* target)
case EMMC: {
size_t start = 0;
bool success = false;
- int fd = ota_open(partition, O_RDWR | O_SYNC);
+ unique_fd fd(ota_open(partition, O_RDWR | O_SYNC));
if (fd < 0) {
printf("failed to open %s: %s\n", partition, strerror(errno));
return -1;
@@ -434,7 +372,7 @@ int WriteToPartition(const unsigned char* data, size_t len, const char* target)
printf("failed to close %s (%s)\n", partition, strerror(errno));
return -1;
}
- fd = ota_open(partition, O_RDONLY);
+ unique_fd fd(ota_open(partition, O_RDONLY));
if (fd < 0) {
printf("failed to reopen %s for verify (%s)\n", partition, strerror(errno));
return -1;
@@ -443,7 +381,7 @@ int WriteToPartition(const unsigned char* data, size_t len, const char* target)
// 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);
+ 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 {
@@ -513,7 +451,6 @@ int WriteToPartition(const unsigned char* data, size_t len, const char* target)
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
// the form "<digest>:<anything>". Return 0 on success, -1 on any
@@ -546,52 +483,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) {
+static 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() {
@@ -633,116 +565,97 @@ 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;
}
}
-// This function 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).
+// This function applies binary patches to EMMC target 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).
//
-// - if the sha1 hash of <target_filename> is <target_sha1_string>,
-// does nothing and exits successfully.
+// - If the SHA-1 hash of <target_filename> is <target_sha1_string>, does nothing and exits
+// successfully.
//
-// - otherwise, if the sha1 hash of <source_filename> is one of the
-// entries in <patch_sha1_str>, the corresponding patch from
-// <patch_data> (which must be a VAL_BLOB) is applied to produce a
-// new file (the type of patch is automatically detected from the
-// blob data). If that new file has sha1 hash <target_sha1_str>,
-// moves it to replace <target_filename>, and exits successfully.
-// Note that if <source_filename> and <target_filename> are not the
-// same, <source_filename> is NOT deleted on success.
-// <target_filename> may be the string "-" to mean "the same as
-// source_filename".
+// - Otherwise, if the SHA-1 hash of <source_filename> is one of the entries in <patch_sha1_str>,
+// the corresponding patch from <patch_data> (which must be a VAL_BLOB) is applied to produce a
+// new file (the type of patch is automatically detected from the blob data). If that new file
+// has SHA-1 hash <target_sha1_str>, moves it to replace <target_filename>, and exits
+// successfully. Note that if <source_filename> and <target_filename> are not the same,
+// <source_filename> is NOT deleted on success. <target_filename> may be the string "-" to mean
+// "the same as <source_filename>".
//
-// - otherwise, or if any error is encountered, exits with non-zero
-// status.
+// - Otherwise, or if any error is encountered, exits with non-zero status.
//
-// <source_filename> may refer to a partition to read the source data.
-// See the comments for the LoadPartitionContents() function above
-// for the format of such a filename.
-
-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) {
- printf("patch %s: ", source_filename);
-
- if (target_filename[0] == '-' && target_filename[1] == '\0') {
- target_filename = source_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 copy_file;
- FileContents source_file;
- const Value* source_patch_value = NULL;
- const Value* copy_patch_value = NULL;
-
- // We try to load the target file into the source_file object.
- if (LoadFileContents(target_filename, &source_file) == 0) {
- if (memcmp(source_file.sha1, target_sha1, SHA_DIGEST_LENGTH) == 0) {
- // The early-exit case: the patch was already applied, this file
- // has the desired hash, nothing for us to do.
- printf("already %s\n", short_sha1(target_sha1).c_str());
- return 0;
- }
- }
-
- if (source_file.data.empty() ||
- (target_filename != source_filename &&
- strcmp(target_filename, source_filename) != 0)) {
- // Need to load the source file: either we failed to load the
- // target file, or we did but it's different from the source file.
- source_file.data.clear();
- LoadFileContents(source_filename, &source_file);
- }
-
- if (!source_file.data.empty()) {
- int to_use = FindMatchingPatch(source_file.sha1, patch_sha1_str, num_patches);
- if (to_use >= 0) {
- source_patch_value = patch_data[to_use];
- }
- }
-
- if (source_patch_value == NULL) {
- source_file.data.clear();
- printf("source file is bad; trying copy\n");
-
- if (LoadFileContents(CACHE_TEMP_SOURCE, &copy_file) < 0) {
- // fail.
- printf("failed to read copy file\n");
- return 1;
- }
-
- int to_use = FindMatchingPatch(copy_file.sha1, patch_sha1_str, num_patches);
- if (to_use >= 0) {
- copy_patch_value = patch_data[to_use];
- }
-
- if (copy_patch_value == NULL) {
- // fail.
- printf("copy file doesn't match source SHA-1s either\n");
- return 1;
- }
- }
-
- return GenerateTarget(&source_file, source_patch_value,
- &copy_file, copy_patch_value,
- source_filename, target_filename,
- target_sha1, target_size, bonus_data);
+// <source_filename> must refer to an EMMC partition to read the source data. See the comments for
+// the LoadPartitionContents() function above for the format of such a filename. <target_size> has
+// become obsolete since we have dropped the support for patching non-EMMC targets (EMMC targets
+// have the size embedded in the filename).
+int applypatch(const char* source_filename, const char* target_filename,
+ const char* target_sha1_str, size_t target_size __unused,
+ 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') {
+ target_filename = source_filename;
+ }
+
+ if (strncmp(target_filename, "EMMC:", 5) != 0) {
+ printf("Supporting patching EMMC targets only.\n");
+ return 1;
+ }
+
+ 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;
+ }
+
+ // We try to load the target file into the source_file object.
+ FileContents source_file;
+ if (LoadFileContents(target_filename, &source_file) == 0) {
+ if (memcmp(source_file.sha1, target_sha1, SHA_DIGEST_LENGTH) == 0) {
+ // The early-exit case: the patch was already applied, this file has the desired hash, nothing
+ // for us to do.
+ printf("already %s\n", short_sha1(target_sha1).c_str());
+ return 0;
+ }
+ }
+
+ if (source_file.data.empty() ||
+ (target_filename != source_filename && strcmp(target_filename, source_filename) != 0)) {
+ // Need to load the source file: either we failed to load the target file, or we did but it's
+ // different from the expected.
+ source_file.data.clear();
+ LoadFileContents(source_filename, &source_file);
+ }
+
+ if (!source_file.data.empty()) {
+ int to_use = FindMatchingPatch(source_file.sha1, patch_sha1_str);
+ if (to_use != -1) {
+ return GenerateTarget(source_file, patch_data[to_use], target_filename, target_sha1,
+ bonus_data);
+ }
+ }
+
+ printf("source file is bad; trying copy\n");
+
+ FileContents copy_file;
+ if (LoadFileContents(CACHE_TEMP_SOURCE, &copy_file) < 0) {
+ printf("failed to read copy file\n");
+ return 1;
+ }
+
+ int to_use = FindMatchingPatch(copy_file.sha1, patch_sha1_str);
+ if (to_use == -1) {
+ printf("copy file doesn't match source SHA-1s either\n");
+ return 1;
+ }
+
+ return GenerateTarget(copy_file, patch_data[to_use], target_filename, target_sha1, bonus_data);
}
/*
@@ -754,281 +667,124 @@ 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,
- const Value* source_patch_value,
- FileContents* copy_file,
- const Value* copy_patch_value,
- const char* source_filename,
- const char* target_filename,
- 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 ||
- strncmp(target_filename, "BML:", 4) == 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");
- 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");
- 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 ||
- strncmp(source_filename, "BML:", 4) == 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;
- }
-
-
- 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 (!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 (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;
- }
- } 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;
+static int GenerateTarget(const FileContents& source_file, const std::unique_ptr<Value>& patch,
+ const std::string& target_filename,
+ const uint8_t target_sha1[SHA_DIGEST_LENGTH], const Value* bonus_data) {
+ 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;
+ }
+
+ CHECK(android::base::StartsWith(target_filename, "EMMC:"));
+
+ // 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;
+ }
+
+ // We store the decoded output in memory.
+ SinkFn sink = MemorySink;
+ std::string memory_sink_str; // Don't need to reserve space.
+ void* token = &memory_sink_str;
+
+ SHA_CTX ctx;
+ SHA1_Init(&ctx);
+
+ int result;
+ if (use_bsdiff) {
+ result = ApplyBSDiffPatch(source_file.data.data(), source_file.data.size(), patch.get(), 0,
+ sink, token, &ctx);
+ } else {
+ result = ApplyImagePatch(source_file.data.data(), source_file.data.size(), patch.get(), sink,
+ token, &ctx, bonus_data);
+ }
+
+ if (result != 0) {
+ printf("applying patch failed\n");
+ return 1;
+ }
+
+ 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());
+ }
+
+ // Write back 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.c_str());
+ return 1;
+ }
+
+ // Delete the backup copy of the source.
+ 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..9920c2be1 100644
--- a/applypatch/bspatch.cpp
+++ b/applypatch/bspatch.cpp
@@ -21,16 +21,12 @@
// notice.
#include <stdio.h>
-#include <sys/stat.h>
#include <sys/types.h>
-#include <errno.h>
-#include <unistd.h>
-#include <string.h>
-#include <bzlib.h>
+#include <bspatch.h>
+#include "applypatch/applypatch.h"
#include "openssl/sha.h"
-#include "applypatch.h"
void ShowBSDiffLicense() {
puts("The bsdiff library used herein is:\n"
@@ -64,184 +60,25 @@ void ShowBSDiffLicense() {
);
}
-static off_t offtin(u_char *buf)
-{
- off_t y;
-
- y=buf[7]&0x7F;
- y=y*256;y+=buf[6];
- y=y*256;y+=buf[5];
- y=y*256;y+=buf[4];
- y=y*256;y+=buf[3];
- y=y*256;y+=buf[2];
- y=y*256;y+=buf[1];
- y=y*256;y+=buf[0];
-
- if(buf[7]&0x80) y=-y;
-
- return y;
-}
-
-int FillBuffer(unsigned char* buffer, int size, bz_stream* stream) {
- stream->next_out = (char*)buffer;
- stream->avail_out = size;
- while (stream->avail_out > 0) {
- int bzerr = BZ2_bzDecompress(stream);
- if (bzerr != BZ_OK && bzerr != BZ_STREAM_END) {
- printf("bz error %d decompressing\n", bzerr);
- return -1;
- }
- if (stream->avail_out > 0) {
- printf("need %d more bytes\n", stream->avail_out);
- }
- }
- return 0;
-}
-
-int ApplyBSDiffPatch(const unsigned char* old_data, ssize_t old_size,
- const Value* patch, ssize_t patch_offset,
- SinkFn sink, void* token, SHA_CTX* ctx) {
-
- std::vector<unsigned char> new_data;
- if (ApplyBSDiffPatchMem(old_data, old_size, patch, patch_offset, &new_data) != 0) {
- return -1;
- }
-
- if (sink(new_data.data(), new_data.size(), token) < static_cast<ssize_t>(new_data.size())) {
- printf("short write of output: %d (%s)\n", errno, strerror(errno));
- return 1;
- }
- if (ctx) SHA1_Update(ctx, new_data.data(), new_data.size());
- return 0;
+int ApplyBSDiffPatch(const unsigned char* old_data, ssize_t old_size, const Value* patch,
+ ssize_t patch_offset, SinkFn sink, void* token, SHA_CTX* ctx) {
+ auto sha_sink = [&](const uint8_t* data, size_t len) {
+ len = sink(data, len, token);
+ if (ctx) SHA1_Update(ctx, data, len);
+ return len;
+ };
+ return bsdiff::bspatch(old_data, old_size,
+ reinterpret_cast<const uint8_t*>(&patch->data[patch_offset]),
+ patch->data.size(), sha_sink);
}
-int ApplyBSDiffPatchMem(const unsigned char* old_data, ssize_t old_size,
- const Value* patch, ssize_t patch_offset,
- std::vector<unsigned char>* new_data) {
- // Patch data format:
- // 0 8 "BSDIFF40"
- // 8 8 X
- // 16 8 Y
- // 24 8 sizeof(newfile)
- // 32 X bzip2(control block)
- // 32+X Y bzip2(diff block)
- // 32+X+Y ??? bzip2(extra block)
- // with control block a set of triples (x,y,z) meaning "add x bytes
- // 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;
- if (memcmp(header, "BSDIFF40", 8) != 0) {
- printf("corrupt bsdiff patch file header (magic number)\n");
- return 1;
- }
-
- ssize_t ctrl_len, data_len, new_size;
- ctrl_len = offtin(header+8);
- data_len = offtin(header+16);
- new_size = offtin(header+24);
-
- if (ctrl_len < 0 || data_len < 0 || new_size < 0) {
- printf("corrupt patch file header (data lengths)\n");
- return 1;
- }
-
- int bzerr;
-
- bz_stream cstream;
- cstream.next_in = patch->data + patch_offset + 32;
- cstream.avail_in = ctrl_len;
- cstream.bzalloc = NULL;
- cstream.bzfree = NULL;
- cstream.opaque = NULL;
- if ((bzerr = BZ2_bzDecompressInit(&cstream, 0, 0)) != BZ_OK) {
- printf("failed to bzinit control stream (%d)\n", bzerr);
- }
-
- bz_stream dstream;
- dstream.next_in = patch->data + patch_offset + 32 + ctrl_len;
- dstream.avail_in = data_len;
- dstream.bzalloc = NULL;
- dstream.bzfree = NULL;
- dstream.opaque = NULL;
- if ((bzerr = BZ2_bzDecompressInit(&dstream, 0, 0)) != BZ_OK) {
- printf("failed to bzinit diff stream (%d)\n", bzerr);
- }
-
- 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.bzalloc = NULL;
- estream.bzfree = NULL;
- estream.opaque = NULL;
- if ((bzerr = BZ2_bzDecompressInit(&estream, 0, 0)) != BZ_OK) {
- printf("failed to bzinit extra stream (%d)\n", bzerr);
- }
-
- new_data->resize(new_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) {
- // Read control data
- if (FillBuffer(buf, 24, &cstream) != 0) {
- printf("error while reading control stream\n");
- return 1;
- }
- ctrl[0] = offtin(buf);
- ctrl[1] = offtin(buf+8);
- ctrl[2] = offtin(buf+16);
-
- if (ctrl[0] < 0 || ctrl[1] < 0) {
- printf("corrupt patch (negative byte counts)\n");
- return 1;
- }
-
- // Sanity check
- if (newpos + ctrl[0] > new_size) {
- printf("corrupt patch (new file overrun)\n");
- return 1;
- }
-
- // Read diff string
- if (FillBuffer(new_data->data() + newpos, ctrl[0], &dstream) != 0) {
- printf("error while reading diff stream\n");
- return 1;
- }
-
- // Add old data to diff string
- for (i = 0; i < ctrl[0]; ++i) {
- if ((oldpos+i >= 0) && (oldpos+i < old_size)) {
- (*new_data)[newpos+i] += old_data[oldpos+i];
- }
- }
-
- // Adjust pointers
- newpos += ctrl[0];
- oldpos += ctrl[0];
-
- // Sanity check
- if (newpos + ctrl[1] > new_size) {
- printf("corrupt patch (new file overrun)\n");
- return 1;
- }
-
- // Read extra string
- if (FillBuffer(new_data->data() + newpos, ctrl[1], &estream) != 0) {
- printf("error while reading extra stream\n");
- return 1;
- }
-
- // Adjust pointers
- newpos += ctrl[1];
- oldpos += ctrl[2];
- }
-
- BZ2_bzDecompressEnd(&cstream);
- BZ2_bzDecompressEnd(&dstream);
- BZ2_bzDecompressEnd(&estream);
- return 0;
+int ApplyBSDiffPatchMem(const unsigned char* old_data, ssize_t old_size, const Value* patch,
+ ssize_t patch_offset, std::vector<unsigned char>* new_data) {
+ auto vector_sink = [new_data](const uint8_t* data, size_t len) {
+ new_data->insert(new_data->end(), data, data + len);
+ return len;
+ };
+ return bsdiff::bspatch(old_data, old_size,
+ reinterpret_cast<const uint8_t*>(&patch->data[patch_offset]),
+ patch->data.size(), vector_sink);
}
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..41d73ab98 100644
--- a/applypatch/imgdiff.cpp
+++ b/applypatch/imgdiff.cpp
@@ -121,618 +121,687 @@
* information that is stored on the system partition.
*/
+#include "applypatch/imgdiff.h"
+
#include <errno.h>
-#include <inttypes.h>
+#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
-#include <unistd.h>
#include <sys/types.h>
+#include <unistd.h>
-#include "zlib.h"
-#include "imgdiff.h"
-#include "utils.h"
+#include <algorithm>
+#include <string>
+#include <vector>
-typedef struct {
- int type; // CHUNK_NORMAL, CHUNK_DEFLATE
- size_t start; // offset of chunk in original image file
+#include <android-base/file.h>
+#include <android-base/logging.h>
+#include <android-base/memory.h>
+#include <android-base/unique_fd.h>
+#include <ziparchive/zip_archive.h>
- size_t len;
- unsigned char* data; // data to be patched (uncompressed, for deflate chunks)
+#include <bsdiff.h>
+#include <zlib.h>
- size_t source_start;
- size_t source_len;
+using android::base::get_unaligned;
- off_t* I; // used by bsdiff
+static constexpr auto BUFFER_SIZE = 0x8000;
- // --- for CHUNK_DEFLATE chunks only: ---
+// If we use this function to write the offset and length (type size_t), their values should not
+// exceed 2^63; because the signed bit will be casted away.
+static inline bool Write8(int fd, int64_t value) {
+ return android::base::WriteFully(fd, &value, sizeof(int64_t));
+}
+
+// Similarly, the value should not exceed 2^31 if we are casting from size_t (e.g. target chunk
+// size).
+static inline bool Write4(int fd, int32_t value) {
+ return android::base::WriteFully(fd, &value, sizeof(int32_t));
+}
+
+class ImageChunk {
+ public:
+ static constexpr auto WINDOWBITS = -15; // 32kb window; negative to indicate a raw stream.
+ static constexpr auto MEMLEVEL = 8; // the default value.
+ static constexpr auto METHOD = Z_DEFLATED;
+ static constexpr auto STRATEGY = Z_DEFAULT_STRATEGY;
+
+ ImageChunk(int type, size_t start, const std::vector<uint8_t>* file_content, size_t raw_data_len)
+ : type_(type),
+ start_(start),
+ input_file_ptr_(file_content),
+ raw_data_len_(raw_data_len),
+ compress_level_(6),
+ source_start_(0),
+ source_len_(0),
+ source_uncompressed_len_(0) {
+ CHECK(file_content != nullptr) << "input file container can't be nullptr";
+ }
+
+ int GetType() const {
+ return type_;
+ }
+ size_t GetRawDataLength() const {
+ return raw_data_len_;
+ }
+ const std::string& GetEntryName() const {
+ return entry_name_;
+ }
+
+ // CHUNK_DEFLATE will return the uncompressed data for diff, while other types will simply return
+ // the raw data.
+ const uint8_t * DataForPatch() const;
+ size_t DataLengthForPatch() const;
+
+ void Dump() const {
+ printf("type %d start %zu len %zu\n", type_, start_, DataLengthForPatch());
+ }
- // original (compressed) deflate data
- size_t deflate_len;
- unsigned char* deflate_data;
+ void SetSourceInfo(const ImageChunk& other);
+ void SetEntryName(std::string entryname);
+ void SetUncompressedData(std::vector<uint8_t> data);
+ bool SetBonusData(const std::vector<uint8_t>& bonus_data);
- char* filename; // used for zip entries
+ bool operator==(const ImageChunk& other) const;
+ bool operator!=(const ImageChunk& other) const {
+ return !(*this == other);
+ }
+
+ size_t GetHeaderSize(size_t patch_size) const;
+ // Return the offset of the next patch into the patch data.
+ size_t WriteHeaderToFd(int fd, const std::vector<uint8_t>& patch, size_t offset);
+
+ /*
+ * Cause a gzip chunk to be treated as a normal chunk (ie, as a blob
+ * of uninterpreted data). The resulting patch will likely be about
+ * as big as the target file, but it lets us handle the case of images
+ * where some gzip chunks are reconstructible but others aren't (by
+ * treating the ones that aren't as normal chunks).
+ */
+ void ChangeDeflateChunkToNormal();
+ bool ChangeChunkToRaw(size_t patch_size);
+
+ /*
+ * Verify that we can reproduce exactly the same compressed data that
+ * we started with. Sets the level, method, windowBits, memLevel, and
+ * strategy fields in the chunk to the encoding parameters needed to
+ * produce the right output.
+ */
+ bool ReconstructDeflateChunk();
+ bool IsAdjacentNormal(const ImageChunk& other) const;
+ void MergeAdjacentNormal(const ImageChunk& other);
+
+ private:
+ int type_; // CHUNK_NORMAL, CHUNK_DEFLATE, CHUNK_RAW
+ size_t start_; // offset of chunk in the original input file
+ const std::vector<uint8_t>* input_file_ptr_; // ptr to the full content of original input file
+ size_t raw_data_len_;
+
+ // --- for CHUNK_DEFLATE chunks only: ---
+ std::vector<uint8_t> uncompressed_data_;
+ std::string entry_name_; // used for zip entries
// deflate encoder parameters
- int level, method, windowBits, memLevel, strategy;
-
- size_t source_uncompressed_len;
-} ImageChunk;
-
-typedef struct {
- int data_offset;
- int deflate_len;
- int uncomp_len;
- char* filename;
-} ZipFileEntry;
-
-static int fileentry_compare(const void* a, const void* b) {
- int ao = ((ZipFileEntry*)a)->data_offset;
- int bo = ((ZipFileEntry*)b)->data_offset;
- if (ao < bo) {
- return -1;
- } else if (ao > bo) {
- return 1;
- } else {
- return 0;
+ int compress_level_;
+
+ size_t source_start_;
+ size_t source_len_;
+ size_t source_uncompressed_len_;
+
+ const uint8_t* GetRawData() const;
+ bool TryReconstruction(int level);
+};
+
+const uint8_t* ImageChunk::GetRawData() const {
+ CHECK_LE(start_ + raw_data_len_, input_file_ptr_->size());
+ return input_file_ptr_->data() + start_;
+}
+
+const uint8_t * ImageChunk::DataForPatch() const {
+ if (type_ == CHUNK_DEFLATE) {
+ return uncompressed_data_.data();
}
+ return GetRawData();
}
-// from bsdiff.c
-int bsdiff(u_char* old, off_t oldsize, off_t** IP, u_char* newdata, off_t newsize,
- const char* patch_filename);
+size_t ImageChunk::DataLengthForPatch() const {
+ if (type_ == CHUNK_DEFLATE) {
+ return uncompressed_data_.size();
+ }
+ return raw_data_len_;
+}
-unsigned char* ReadZip(const char* filename,
- int* num_chunks, ImageChunk** chunks,
- int include_pseudo_chunk) {
- struct stat st;
- if (stat(filename, &st) != 0) {
- printf("failed to stat \"%s\": %s\n", filename, strerror(errno));
- return NULL;
+bool ImageChunk::operator==(const ImageChunk& other) const {
+ if (type_ != other.type_) {
+ return false;
}
+ return (raw_data_len_ == other.raw_data_len_ &&
+ memcmp(GetRawData(), other.GetRawData(), raw_data_len_) == 0);
+}
- size_t sz = static_cast<size_t>(st.st_size);
- unsigned char* img = reinterpret_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));
- fclose(f);
- return NULL;
+void ImageChunk::SetSourceInfo(const ImageChunk& src) {
+ source_start_ = src.start_;
+ if (type_ == CHUNK_NORMAL) {
+ source_len_ = src.raw_data_len_;
+ } else if (type_ == CHUNK_DEFLATE) {
+ source_len_ = src.raw_data_len_;
+ source_uncompressed_len_ = src.uncompressed_data_.size();
}
- fclose(f);
+}
+
+void ImageChunk::SetEntryName(std::string entryname) {
+ entry_name_ = std::move(entryname);
+}
- // look for the end-of-central-directory record.
+void ImageChunk::SetUncompressedData(std::vector<uint8_t> data) {
+ uncompressed_data_ = std::move(data);
+}
- int i;
- for (i = st.st_size-20; i >= 0 && i > st.st_size - 65600; --i) {
- if (img[i] == 0x50 && img[i+1] == 0x4b &&
- img[i+2] == 0x05 && img[i+3] == 0x06) {
- break;
- }
- }
- // double-check: this archive consists of a single "disk"
- if (!(img[i+4] == 0 && img[i+5] == 0 && img[i+6] == 0 && img[i+7] == 0)) {
- printf("can't process multi-disk archive\n");
- return NULL;
+bool ImageChunk::SetBonusData(const std::vector<uint8_t>& bonus_data) {
+ if (type_ != CHUNK_DEFLATE) {
+ return false;
}
+ uncompressed_data_.insert(uncompressed_data_.end(), bonus_data.begin(), bonus_data.end());
+ return true;
+}
- int cdcount = Read2(img+i+8);
- int cdoffset = Read4(img+i+16);
+// Convert CHUNK_NORMAL & CHUNK_DEFLATE to CHUNK_RAW if the target size is
+// smaller. Also take the header size into account during size comparison.
+bool ImageChunk::ChangeChunkToRaw(size_t patch_size) {
+ if (type_ == CHUNK_RAW) {
+ return true;
+ } else if (type_ == CHUNK_NORMAL && (raw_data_len_ <= 160 || raw_data_len_ < patch_size)) {
+ type_ = CHUNK_RAW;
+ return true;
+ }
+ return false;
+}
- ZipFileEntry* temp_entries = reinterpret_cast<ZipFileEntry*>(malloc(
- cdcount * sizeof(ZipFileEntry)));
- int entrycount = 0;
+void ImageChunk::ChangeDeflateChunkToNormal() {
+ if (type_ != CHUNK_DEFLATE) return;
+ type_ = CHUNK_NORMAL;
+ entry_name_.clear();
+ uncompressed_data_.clear();
+}
- unsigned char* cd = img+cdoffset;
- 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);
- return NULL;
- }
+// Header size:
+// header_type 4 bytes
+// CHUNK_NORMAL 8*3 = 24 bytes
+// CHUNK_DEFLATE 8*5 + 4*5 = 60 bytes
+// CHUNK_RAW 4 bytes + patch_size
+size_t ImageChunk::GetHeaderSize(size_t patch_size) const {
+ switch (type_) {
+ case CHUNK_NORMAL:
+ return 4 + 8 * 3;
+ case CHUNK_DEFLATE:
+ return 4 + 8 * 5 + 4 * 5;
+ case CHUNK_RAW:
+ return 4 + 4 + patch_size;
+ default:
+ CHECK(false) << "unexpected chunk type: " << type_; // Should not reach here.
+ return 0;
+ }
+}
- int clen = Read4(cd+20); // compressed len
- int ulen = Read4(cd+24); // uncompressed len
- int nlen = Read2(cd+28); // filename len
- int xlen = Read2(cd+30); // extra field len
- int mlen = Read2(cd+32); // file comment len
- int hoffset = Read4(cd+42); // local header offset
+size_t ImageChunk::WriteHeaderToFd(int fd, const std::vector<uint8_t>& patch, size_t offset) {
+ Write4(fd, type_);
+ switch (type_) {
+ case CHUNK_NORMAL:
+ printf("normal (%10zu, %10zu) %10zu\n", start_, raw_data_len_, patch.size());
+ Write8(fd, static_cast<int64_t>(source_start_));
+ Write8(fd, static_cast<int64_t>(source_len_));
+ Write8(fd, static_cast<int64_t>(offset));
+ return offset + patch.size();
+ case CHUNK_DEFLATE:
+ printf("deflate (%10zu, %10zu) %10zu %s\n", start_, raw_data_len_, patch.size(),
+ entry_name_.c_str());
+ Write8(fd, static_cast<int64_t>(source_start_));
+ Write8(fd, static_cast<int64_t>(source_len_));
+ Write8(fd, static_cast<int64_t>(offset));
+ Write8(fd, static_cast<int64_t>(source_uncompressed_len_));
+ Write8(fd, static_cast<int64_t>(uncompressed_data_.size()));
+ Write4(fd, compress_level_);
+ Write4(fd, METHOD);
+ Write4(fd, WINDOWBITS);
+ Write4(fd, MEMLEVEL);
+ Write4(fd, STRATEGY);
+ return offset + patch.size();
+ case CHUNK_RAW:
+ printf("raw (%10zu, %10zu)\n", start_, raw_data_len_);
+ Write4(fd, static_cast<int32_t>(patch.size()));
+ if (!android::base::WriteFully(fd, patch.data(), patch.size())) {
+ CHECK(false) << "failed to write " << patch.size() <<" bytes patch";
+ }
+ return offset;
+ default:
+ CHECK(false) << "unexpected chunk type: " << type_;
+ return offset;
+ }
+}
- char* filename = reinterpret_cast<char*>(malloc(nlen+1));
- memcpy(filename, cd+46, nlen);
- filename[nlen] = '\0';
+bool ImageChunk::IsAdjacentNormal(const ImageChunk& other) const {
+ if (type_ != CHUNK_NORMAL || other.type_ != CHUNK_NORMAL) {
+ return false;
+ }
+ return (other.start_ == start_ + raw_data_len_);
+}
- int method = Read2(cd+10);
+void ImageChunk::MergeAdjacentNormal(const ImageChunk& other) {
+ CHECK(IsAdjacentNormal(other));
+ raw_data_len_ = raw_data_len_ + other.raw_data_len_;
+}
- cd += 46 + nlen + xlen + mlen;
+bool ImageChunk::ReconstructDeflateChunk() {
+ if (type_ != CHUNK_DEFLATE) {
+ printf("attempt to reconstruct non-deflate chunk\n");
+ return false;
+ }
- if (method != 8) { // 8 == deflate
- free(filename);
- continue;
+ // We only check two combinations of encoder parameters: level 6
+ // (the default) and level 9 (the maximum).
+ for (int level = 6; level <= 9; level += 3) {
+ if (TryReconstruction(level)) {
+ compress_level_ = level;
+ return true;
}
+ }
+
+ return false;
+}
- unsigned char* lh = img + hoffset;
+/*
+ * Takes the uncompressed data stored in the chunk, compresses it
+ * using the zlib parameters stored in the chunk, and checks that it
+ * matches exactly the compressed data we started with (also stored in
+ * the chunk).
+ */
+bool ImageChunk::TryReconstruction(int level) {
+ z_stream strm;
+ strm.zalloc = Z_NULL;
+ strm.zfree = Z_NULL;
+ strm.opaque = Z_NULL;
+ strm.avail_in = uncompressed_data_.size();
+ strm.next_in = uncompressed_data_.data();
+ int ret = deflateInit2(&strm, level, METHOD, WINDOWBITS, MEMLEVEL, STRATEGY);
+ if (ret < 0) {
+ printf("failed to initialize deflate: %d\n", ret);
+ return false;
+ }
- if (!(lh[0] == 0x50 && lh[1] == 0x4b && lh[2] == 0x03 && lh[3] == 0x04)) {
- printf("bad local file header entry %d\n", i);
- return NULL;
+ std::vector<uint8_t> buffer(BUFFER_SIZE);
+ size_t offset = 0;
+ do {
+ strm.avail_out = buffer.size();
+ strm.next_out = buffer.data();
+ ret = deflate(&strm, Z_FINISH);
+ if (ret < 0) {
+ printf("failed to deflate: %d\n", ret);
+ return false;
}
- if (Read2(lh+26) != nlen || memcmp(lh+30, filename, nlen) != 0) {
- printf("central dir filename doesn't match local header\n");
- return NULL;
+ size_t compressed_size = buffer.size() - strm.avail_out;
+ if (memcmp(buffer.data(), input_file_ptr_->data() + start_ + offset, compressed_size) != 0) {
+ // mismatch; data isn't the same.
+ deflateEnd(&strm);
+ return false;
}
+ offset += compressed_size;
+ } while (ret != Z_STREAM_END);
+ deflateEnd(&strm);
- xlen = Read2(lh+28); // extra field len; might be different from CD entry?
+ if (offset != raw_data_len_) {
+ // mismatch; ran out of data before we should have.
+ return false;
+ }
+ return true;
+}
- temp_entries[entrycount].data_offset = hoffset+30+nlen+xlen;
- temp_entries[entrycount].deflate_len = clen;
- temp_entries[entrycount].uncomp_len = ulen;
- temp_entries[entrycount].filename = filename;
- ++entrycount;
+// EOCD record
+// offset 0: signature 0x06054b50, 4 bytes
+// offset 4: number of this disk, 2 bytes
+// ...
+// offset 20: comment length, 2 bytes
+// offset 22: comment, n bytes
+static bool GetZipFileSize(const std::vector<uint8_t>& zip_file, size_t* input_file_size) {
+ if (zip_file.size() < 22) {
+ printf("file is too small to be a zip file\n");
+ return false;
}
- qsort(temp_entries, entrycount, sizeof(ZipFileEntry), fileentry_compare);
-
-#if 0
- printf("found %d deflated entries\n", entrycount);
- for (i = 0; i < entrycount; ++i) {
- printf("off %10d len %10d unlen %10d %p %s\n",
- temp_entries[i].data_offset,
- temp_entries[i].deflate_len,
- temp_entries[i].uncomp_len,
- temp_entries[i].filename,
- temp_entries[i].filename);
+ // Look for End of central directory record of the zip file, and calculate the actual
+ // zip_file size.
+ for (int i = zip_file.size() - 22; i >= 0; i--) {
+ if (zip_file[i] == 0x50) {
+ if (get_unaligned<uint32_t>(&zip_file[i]) == 0x06054b50) {
+ // double-check: this archive consists of a single "disk".
+ CHECK_EQ(get_unaligned<uint16_t>(&zip_file[i + 4]), 0);
+
+ uint16_t comment_length = get_unaligned<uint16_t>(&zip_file[i + 20]);
+ size_t file_size = i + 22 + comment_length;
+ CHECK_LE(file_size, zip_file.size());
+ *input_file_size = file_size;
+ return true;
+ }
+ }
}
-#endif
- *num_chunks = 0;
- *chunks = reinterpret_cast<ImageChunk*>(malloc((entrycount*2+2) * sizeof(ImageChunk)));
- ImageChunk* curr = *chunks;
+ // EOCD not found, this file is likely not a valid zip file.
+ return false;
+}
+
+static bool ReadZip(const char* filename, std::vector<ImageChunk>* chunks,
+ std::vector<uint8_t>* zip_file, bool include_pseudo_chunk) {
+ CHECK(chunks != nullptr && zip_file != nullptr);
- if (include_pseudo_chunk) {
- curr->type = CHUNK_NORMAL;
- curr->start = 0;
- curr->len = st.st_size;
- curr->data = img;
- curr->filename = NULL;
- curr->I = NULL;
- ++curr;
- ++*num_chunks;
+ android::base::unique_fd fd(open(filename, O_RDONLY));
+ if (fd == -1) {
+ printf("failed to open \"%s\" %s\n", filename, strerror(errno));
+ return false;
+ }
+ struct stat st;
+ if (fstat(fd, &st) != 0) {
+ printf("failed to stat \"%s\": %s\n", filename, strerror(errno));
+ return false;
}
- int pos = 0;
- int nextentry = 0;
+ size_t sz = static_cast<size_t>(st.st_size);
+ zip_file->resize(sz);
+ if (!android::base::ReadFully(fd, zip_file->data(), sz)) {
+ printf("failed to read \"%s\" %s\n", filename, strerror(errno));
+ return false;
+ }
+ fd.reset();
- while (pos < st.st_size) {
- if (nextentry < entrycount && pos == temp_entries[nextentry].data_offset) {
- curr->type = CHUNK_DEFLATE;
- curr->start = pos;
- curr->deflate_len = temp_entries[nextentry].deflate_len;
- curr->deflate_data = img + pos;
- curr->filename = temp_entries[nextentry].filename;
- curr->I = NULL;
+ // Trim the trailing zeros before we pass the file to ziparchive handler.
+ size_t zipfile_size;
+ if (!GetZipFileSize(*zip_file, &zipfile_size)) {
+ printf("failed to parse the actual size of %s\n", filename);
+ return false;
+ }
+ ZipArchiveHandle handle;
+ int err = OpenArchiveFromMemory(zip_file->data(), zipfile_size, filename, &handle);
+ if (err != 0) {
+ printf("failed to open zip file %s: %s\n", filename, ErrorCodeString(err));
+ CloseArchive(handle);
+ return false;
+ }
- curr->len = temp_entries[nextentry].uncomp_len;
- curr->data = reinterpret_cast<unsigned char*>(malloc(curr->len));
+ // Create a list of deflated zip entries, sorted by offset.
+ std::vector<std::pair<std::string, ZipEntry>> temp_entries;
+ void* cookie;
+ int ret = StartIteration(handle, &cookie, nullptr, nullptr);
+ if (ret != 0) {
+ printf("failed to iterate over entries in %s: %s\n", filename, ErrorCodeString(ret));
+ CloseArchive(handle);
+ return false;
+ }
- z_stream strm;
- strm.zalloc = Z_NULL;
- strm.zfree = Z_NULL;
- strm.opaque = Z_NULL;
- strm.avail_in = curr->deflate_len;
- strm.next_in = curr->deflate_data;
+ ZipString name;
+ ZipEntry entry;
+ while ((ret = Next(cookie, &entry, &name)) == 0) {
+ if (entry.method == kCompressDeflated) {
+ std::string entryname(name.name, name.name + name.name_length);
+ temp_entries.push_back(std::make_pair(entryname, entry));
+ }
+ }
- // -15 means we are decoding a 'raw' deflate stream; zlib will
- // not expect zlib headers.
- int ret = inflateInit2(&strm, -15);
+ if (ret != -1) {
+ printf("Error while iterating over zip entries: %s\n", ErrorCodeString(ret));
+ CloseArchive(handle);
+ return false;
+ }
+ std::sort(temp_entries.begin(), temp_entries.end(),
+ [](auto& entry1, auto& entry2) {
+ return entry1.second.offset < entry2.second.offset;
+ });
- strm.avail_out = curr->len;
- strm.next_out = curr->data;
- ret = inflate(&strm, Z_NO_FLUSH);
- if (ret != Z_STREAM_END) {
- printf("failed to inflate \"%s\"; %d\n", curr->filename, ret);
- return NULL;
+ EndIteration(cookie);
+
+ if (include_pseudo_chunk) {
+ chunks->emplace_back(CHUNK_NORMAL, 0, zip_file, zip_file->size());
+ }
+
+ size_t pos = 0;
+ size_t nextentry = 0;
+ while (pos < zip_file->size()) {
+ if (nextentry < temp_entries.size() &&
+ static_cast<off64_t>(pos) == temp_entries[nextentry].second.offset) {
+ // compose the next deflate chunk.
+ std::string entryname = temp_entries[nextentry].first;
+ size_t uncompressed_len = temp_entries[nextentry].second.uncompressed_length;
+ std::vector<uint8_t> uncompressed_data(uncompressed_len);
+ if ((ret = ExtractToMemory(handle, &temp_entries[nextentry].second, uncompressed_data.data(),
+ uncompressed_len)) != 0) {
+ printf("failed to extract %s with size %zu: %s\n", entryname.c_str(), uncompressed_len,
+ ErrorCodeString(ret));
+ CloseArchive(handle);
+ return false;
}
- inflateEnd(&strm);
+ size_t compressed_len = temp_entries[nextentry].second.compressed_length;
+ ImageChunk curr(CHUNK_DEFLATE, pos, zip_file, compressed_len);
+ curr.SetEntryName(std::move(entryname));
+ curr.SetUncompressedData(std::move(uncompressed_data));
+ chunks->push_back(curr);
- pos += curr->deflate_len;
+ pos += compressed_len;
++nextentry;
- ++*num_chunks;
- ++curr;
continue;
}
- // use a normal chunk to take all the data up to the start of the
- // next deflate section.
-
- curr->type = CHUNK_NORMAL;
- curr->start = pos;
- if (nextentry < entrycount) {
- curr->len = temp_entries[nextentry].data_offset - pos;
+ // Use a normal chunk to take all the data up to the start of the next deflate section.
+ size_t raw_data_len;
+ if (nextentry < temp_entries.size()) {
+ raw_data_len = temp_entries[nextentry].second.offset - pos;
} else {
- curr->len = st.st_size - pos;
+ raw_data_len = zip_file->size() - pos;
}
- curr->data = img + pos;
- curr->filename = NULL;
- curr->I = NULL;
- pos += curr->len;
+ chunks->emplace_back(CHUNK_NORMAL, pos, zip_file, raw_data_len);
- ++*num_chunks;
- ++curr;
+ pos += raw_data_len;
}
- free(temp_entries);
- return img;
+ CloseArchive(handle);
+ return true;
}
-/*
- * Read the given file and break it up into chunks, putting the number
- * of chunks and their info in *num_chunks and **chunks,
- * respectively. Returns a malloc'd block of memory containing the
- * contents of the file; various pointers in the output chunk array
- * will point into this block of memory. The caller should free the
- * return value when done with all the chunks. Returns NULL on
- * failure.
- */
-unsigned char* ReadImage(const char* filename,
- int* num_chunks, ImageChunk** chunks) {
+// Read the given file and break it up into chunks, and putting the data in to a vector.
+static bool ReadImage(const char* filename, std::vector<ImageChunk>* chunks,
+ std::vector<uint8_t>* img) {
+ CHECK(chunks != nullptr && img != nullptr);
+
+ android::base::unique_fd fd(open(filename, O_RDONLY));
+ if (fd == -1) {
+ printf("failed to open \"%s\" %s\n", filename, strerror(errno));
+ return false;
+ }
struct stat st;
- if (stat(filename, &st) != 0) {
+ if (fstat(fd, &st) != 0) {
printf("failed to stat \"%s\": %s\n", filename, strerror(errno));
- return NULL;
+ return false;
}
size_t sz = static_cast<size_t>(st.st_size);
- unsigned char* img = reinterpret_cast<unsigned char*>(malloc(sz + 4));
- FILE* f = fopen(filename, "rb");
- if (fread(img, 1, sz, f) != sz) {
+ img->resize(sz);
+ if (!android::base::ReadFully(fd, img->data(), sz)) {
printf("failed to read \"%s\" %s\n", filename, strerror(errno));
- fclose(f);
- return NULL;
+ return false;
}
- fclose(f);
-
- // append 4 zero bytes to the data so we can always search for the
- // four-byte string 1f8b0800 starting at any point in the actual
- // file data, without special-casing the end of the data.
- memset(img+sz, 0, 4);
size_t pos = 0;
- *num_chunks = 0;
- *chunks = NULL;
-
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
- p[3] == 0x00) { // no header flags
+ // 0x00 no header flags, 0x08 deflate compression, 0x1f8b gzip magic number
+ if (sz - pos >= 4 && get_unaligned<uint32_t>(img->data() + pos) == 0x00088b1f) {
// 'pos' is the offset of the start of a gzip chunk.
size_t chunk_offset = pos;
- *num_chunks += 3;
- *chunks = reinterpret_cast<ImageChunk*>(realloc(*chunks,
- *num_chunks * sizeof(ImageChunk)));
- ImageChunk* curr = *chunks + (*num_chunks-3);
-
- // create a normal chunk for the header.
- curr->start = pos;
- curr->type = CHUNK_NORMAL;
- curr->len = GZIP_HEADER_LEN;
- curr->data = p;
- curr->I = NULL;
-
- pos += curr->len;
- p += curr->len;
- ++curr;
-
- curr->type = CHUNK_DEFLATE;
- curr->filename = NULL;
- curr->I = NULL;
+ // The remaining data is too small to be a gzip chunk; treat them as a normal chunk.
+ if (sz - pos < GZIP_HEADER_LEN + GZIP_FOOTER_LEN) {
+ chunks->emplace_back(CHUNK_NORMAL, pos, img, sz - pos);
+ break;
+ }
- // We must decompress this chunk in order to discover where it
- // ends, and so we can put the uncompressed data and its length
- // into curr->data and curr->len.
+ // We need three chunks for the deflated image in total, one normal chunk for the header,
+ // one deflated chunk for the body, and another normal chunk for the footer.
+ chunks->emplace_back(CHUNK_NORMAL, pos, img, GZIP_HEADER_LEN);
+ pos += GZIP_HEADER_LEN;
- size_t allocated = 32768;
- curr->len = 0;
- curr->data = reinterpret_cast<unsigned char*>(malloc(allocated));
- curr->start = pos;
- curr->deflate_data = p;
+ // We must decompress this chunk in order to discover where it ends, and so we can update
+ // the uncompressed_data of the image body and its length.
z_stream strm;
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.avail_in = sz - pos;
- strm.next_in = p;
+ strm.next_in = img->data() + pos;
// -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 false;
+ }
+ size_t allocated = BUFFER_SIZE;
+ std::vector<uint8_t> uncompressed_data(allocated);
+ size_t uncompressed_len = 0, raw_data_len = 0;
do {
- strm.avail_out = allocated - curr->len;
- strm.next_out = curr->data + curr->len;
+ strm.avail_out = allocated - uncompressed_len;
+ strm.next_out = uncompressed_data.data() + uncompressed_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;
+ uncompressed_len = allocated - strm.avail_out;
if (strm.avail_out == 0) {
allocated *= 2;
- curr->data = reinterpret_cast<unsigned char*>(realloc(curr->data, allocated));
+ uncompressed_data.resize(allocated);
}
- processed_deflate = true;
} while (ret != Z_STREAM_END);
- curr->deflate_len = sz - strm.avail_in - pos;
+ raw_data_len = sz - strm.avail_in - pos;
inflateEnd(&strm);
- pos += curr->deflate_len;
- p += curr->deflate_len;
- ++curr;
- // create a normal chunk for the footer
+ if (ret < 0) {
+ continue;
+ }
+
+ ImageChunk body(CHUNK_DEFLATE, pos, img, raw_data_len);
+ uncompressed_data.resize(uncompressed_len);
+ body.SetUncompressedData(std::move(uncompressed_data));
+ chunks->push_back(body);
- curr->type = CHUNK_NORMAL;
- curr->start = pos;
- curr->len = GZIP_FOOTER_LEN;
- curr->data = img+pos;
- curr->I = NULL;
+ pos += raw_data_len;
- pos += curr->len;
- p += curr->len;
- ++curr;
+ // create a normal chunk for the footer
+ chunks->emplace_back(CHUNK_NORMAL, pos, img, GZIP_FOOTER_LEN);
+
+ pos += GZIP_FOOTER_LEN;
// The footer (that we just skipped over) contains the size of
// the uncompressed data. Double-check to make sure that it
// matches the size of the data we got when we actually did
// the decompression.
- size_t footer_size = Read4(p-4);
- if (footer_size != curr[-2].len) {
- printf("Error: footer size %zu != decompressed size %zu\n",
- footer_size, curr[-2].len);
- free(img);
- return NULL;
+ size_t footer_size = get_unaligned<uint32_t>(img->data() + pos - 4);
+ if (footer_size != body.DataLengthForPatch()) {
+ printf("Error: footer size %zu != decompressed size %zu\n", footer_size,
+ body.GetRawDataLength());
+ return false;
}
} else {
- // 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)));
- 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.
- curr->type = CHUNK_NORMAL;
- curr->data = p;
-
- for (curr->len = 0; curr->len < (sz - pos); ++curr->len) {
- if (p[curr->len] == 0x1f &&
- p[curr->len+1] == 0x8b &&
- p[curr->len+2] == 0x08 &&
- p[curr->len+3] == 0x00) {
+ // Use a normal chunk to take all the contents until the next gzip chunk (or EOF); we expect
+ // the number of chunks to be small (5 for typical boot and recovery images).
+
+ // Scan forward until we find a gzip header.
+ size_t data_len = 0;
+ while (data_len + pos < sz) {
+ if (data_len + pos + 4 <= sz &&
+ get_unaligned<uint32_t>(img->data() + pos + data_len) == 0x00088b1f) {
break;
}
+ data_len++;
}
- pos += curr->len;
- }
- }
+ chunks->emplace_back(CHUNK_NORMAL, pos, img, data_len);
- return img;
-}
-
-#define BUFFER_SIZE 32768
-
-/*
- * Takes the uncompressed data stored in the chunk, compresses it
- * using the zlib parameters stored in the chunk, and checks that it
- * matches exactly the compressed data we started with (also stored in
- * the chunk). Return 0 on success.
- */
-int TryReconstruction(ImageChunk* chunk, unsigned char* out) {
- size_t p = 0;
-
-#if 0
- printf("trying %d %d %d %d %d\n",
- chunk->level, chunk->method, chunk->windowBits,
- chunk->memLevel, chunk->strategy);
-#endif
-
- z_stream strm;
- strm.zalloc = Z_NULL;
- strm.zfree = Z_NULL;
- strm.opaque = Z_NULL;
- strm.avail_in = chunk->len;
- strm.next_in = chunk->data;
- int ret;
- ret = deflateInit2(&strm, chunk->level, chunk->method, chunk->windowBits,
- chunk->memLevel, chunk->strategy);
- do {
- strm.avail_out = BUFFER_SIZE;
- strm.next_out = out;
- ret = deflate(&strm, Z_FINISH);
- size_t have = BUFFER_SIZE - strm.avail_out;
-
- if (memcmp(out, chunk->deflate_data+p, have) != 0) {
- // mismatch; data isn't the same.
- deflateEnd(&strm);
- return -1;
+ pos += data_len;
}
- p += have;
- } while (ret != Z_STREAM_END);
- deflateEnd(&strm);
- if (p != chunk->deflate_len) {
- // mismatch; ran out of data before we should have.
- return -1;
- }
- return 0;
-}
-
-/*
- * Verify that we can reproduce exactly the same compressed data that
- * we started with. Sets the level, method, windowBits, memLevel, and
- * strategy fields in the chunk to the encoding parameters needed to
- * produce the right output. Returns 0 on success.
- */
-int ReconstructDeflateChunk(ImageChunk* chunk) {
- if (chunk->type != CHUNK_DEFLATE) {
- printf("attempt to reconstruct non-deflate chunk\n");
- return -1;
}
- size_t p = 0;
- unsigned char* out = reinterpret_cast<unsigned char*>(malloc(BUFFER_SIZE));
-
- // We only check two combinations of encoder parameters: level 6
- // (the default) and level 9 (the maximum).
- for (chunk->level = 6; chunk->level <= 9; chunk->level += 3) {
- chunk->windowBits = -15; // 32kb window; negative to indicate a raw stream.
- chunk->memLevel = 8; // the default value.
- chunk->method = Z_DEFLATED;
- chunk->strategy = Z_DEFAULT_STRATEGY;
-
- if (TryReconstruction(chunk, out) == 0) {
- free(out);
- return 0;
- }
- }
-
- free(out);
- return -1;
+ return true;
}
/*
- * Given source and target chunks, compute a bsdiff patch between them
- * by running bsdiff in a subprocess. Return the patch data, placing
- * its length in *size. Return NULL on failure. We expect the bsdiff
- * program to be in the path.
+ * Given source and target chunks, compute a bsdiff patch between them.
+ * Store the result in the patch_data.
+ * |bsdiff_cache| can be used to cache the suffix array if the same |src| chunk
+ * is used repeatedly, pass nullptr if not needed.
*/
-unsigned char* MakePatch(ImageChunk* src, ImageChunk* tgt, size_t* size) {
- if (tgt->type == CHUNK_NORMAL) {
- if (tgt->len <= 160) {
- tgt->type = CHUNK_RAW;
- *size = tgt->len;
- return tgt->data;
- }
+static bool MakePatch(const ImageChunk* src, ImageChunk* tgt, std::vector<uint8_t>* patch_data,
+ saidx_t** bsdiff_cache) {
+ if (tgt->ChangeChunkToRaw(0)) {
+ size_t patch_size = tgt->DataLengthForPatch();
+ patch_data->resize(patch_size);
+ std::copy(tgt->DataForPatch(), tgt->DataForPatch() + patch_size, patch_data->begin());
+ return true;
}
+#if defined(__ANDROID__)
+ char ptemp[] = "/data/local/tmp/imgdiff-patch-XXXXXX";
+#else
char ptemp[] = "/tmp/imgdiff-patch-XXXXXX";
- int fd = mkstemp(ptemp);
+#endif
+ int fd = mkstemp(ptemp);
if (fd == -1) {
- printf("MakePatch failed to create a temporary file: %s\n",
- strerror(errno));
- return NULL;
+ printf("MakePatch failed to create a temporary file: %s\n", strerror(errno));
+ return false;
}
- close(fd); // temporary file is created and we don't need its file
- // descriptor
+ close(fd);
- int r = bsdiff(src->data, src->len, &(src->I), tgt->data, tgt->len, ptemp);
+ int r = bsdiff::bsdiff(src->DataForPatch(), src->DataLengthForPatch(), tgt->DataForPatch(),
+ tgt->DataLengthForPatch(), ptemp, bsdiff_cache);
if (r != 0) {
printf("bsdiff() failed: %d\n", r);
- return NULL;
+ return false;
}
+ android::base::unique_fd patch_fd(open(ptemp, O_RDONLY));
+ if (patch_fd == -1) {
+ printf("failed to open %s: %s\n", ptemp, strerror(errno));
+ return false;
+ }
struct stat st;
- if (stat(ptemp, &st) != 0) {
- printf("failed to stat patch file %s: %s\n",
- ptemp, strerror(errno));
- return NULL;
+ if (fstat(patch_fd, &st) != 0) {
+ printf("failed to stat patch file %s: %s\n", ptemp, strerror(errno));
+ return false;
}
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));
-
- if (tgt->type == CHUNK_NORMAL && tgt->len <= sz) {
+ // Change the chunk type to raw if the patch takes less space that way.
+ if (tgt->ChangeChunkToRaw(sz)) {
unlink(ptemp);
-
- tgt->type = CHUNK_RAW;
- *size = tgt->len;
- return tgt->data;
- }
-
- *size = sz;
-
- FILE* f = fopen(ptemp, "rb");
- if (f == NULL) {
- printf("failed to open patch %s: %s\n", ptemp, strerror(errno));
- return NULL;
+ size_t patch_size = tgt->DataLengthForPatch();
+ patch_data->resize(patch_size);
+ std::copy(tgt->DataForPatch(), tgt->DataForPatch() + patch_size, patch_data->begin());
+ return true;
}
- if (fread(data, 1, sz, f) != sz) {
- printf("failed to read patch %s: %s\n", ptemp, strerror(errno));
- return NULL;
+ patch_data->resize(sz);
+ if (!android::base::ReadFully(patch_fd, patch_data->data(), sz)) {
+ printf("failed to read \"%s\" %s\n", ptemp, strerror(errno));
+ return false;
}
- fclose(f);
unlink(ptemp);
+ tgt->SetSourceInfo(*src);
- tgt->source_start = src->start;
- switch (tgt->type) {
- case CHUNK_NORMAL:
- tgt->source_len = src->len;
- break;
- case CHUNK_DEFLATE:
- tgt->source_len = src->deflate_len;
- tgt->source_uncompressed_len = src->len;
- break;
- }
-
- return data;
-}
-
-/*
- * Cause a gzip chunk to be treated as a normal chunk (ie, as a blob
- * of uninterpreted data). The resulting patch will likely be about
- * as big as the target file, but it lets us handle the case of images
- * where some gzip chunks are reconstructible but others aren't (by
- * treating the ones that aren't as normal chunks).
- */
-void ChangeDeflateChunkToNormal(ImageChunk* ch) {
- if (ch->type != CHUNK_DEFLATE) return;
- ch->type = CHUNK_NORMAL;
- free(ch->data);
- ch->data = ch->deflate_data;
- ch->len = ch->deflate_len;
-}
-
-/*
- * Return true if the data in the chunk is identical (including the
- * compressed representation, for gzip chunks).
- */
-int AreChunksEqual(ImageChunk* a, ImageChunk* b) {
- if (a->type != b->type) return 0;
-
- switch (a->type) {
- case CHUNK_NORMAL:
- return a->len == b->len && memcmp(a->data, b->data, a->len) == 0;
-
- case CHUNK_DEFLATE:
- return a->deflate_len == b->deflate_len &&
- memcmp(a->deflate_data, b->deflate_data, a->deflate_len) == 0;
-
- default:
- printf("unknown chunk type %d\n", a->type);
- return 0;
- }
+ return true;
}
/*
@@ -740,137 +809,103 @@ int AreChunksEqual(ImageChunk* a, ImageChunk* b) {
* a single chunk. (Such runs can be produced when deflate chunks are
* changed to normal chunks.)
*/
-void MergeAdjacentNormalChunks(ImageChunk* chunks, int* num_chunks) {
- int out = 0;
- int in_start = 0, in_end;
- while (in_start < *num_chunks) {
- if (chunks[in_start].type != CHUNK_NORMAL) {
- in_end = in_start+1;
- } else {
- // in_start is a normal chunk. Look for a run of normal chunks
- // that constitute a solid block of data (ie, each chunk begins
- // where the previous one ended).
- for (in_end = in_start+1;
- in_end < *num_chunks && chunks[in_end].type == CHUNK_NORMAL &&
- (chunks[in_end].start ==
- chunks[in_end-1].start + chunks[in_end-1].len &&
- chunks[in_end].data ==
- chunks[in_end-1].data + chunks[in_end-1].len);
- ++in_end);
+static void MergeAdjacentNormalChunks(std::vector<ImageChunk>* chunks) {
+ size_t merged_last = 0, cur = 0;
+ while (cur < chunks->size()) {
+ // Look for normal chunks adjacent to the current one. If such chunk exists, extend the
+ // length of the current normal chunk.
+ size_t to_check = cur + 1;
+ while (to_check < chunks->size() && chunks->at(cur).IsAdjacentNormal(chunks->at(to_check))) {
+ chunks->at(cur).MergeAdjacentNormal(chunks->at(to_check));
+ to_check++;
}
- if (in_end == in_start+1) {
-#if 0
- printf("chunk %d is now %d\n", in_start, out);
-#endif
- if (out != in_start) {
- memcpy(chunks+out, chunks+in_start, sizeof(ImageChunk));
- }
- } else {
-#if 0
- printf("collapse normal chunks %d-%d into %d\n", in_start, in_end-1, out);
-#endif
-
- // Merge chunks [in_start, in_end-1] into one chunk. Since the
- // data member of each chunk is just a pointer into an in-memory
- // copy of the file, this can be done without recopying (the
- // output chunk has the first chunk's start location and data
- // pointer, and length equal to the sum of the input chunk
- // lengths).
- chunks[out].type = CHUNK_NORMAL;
- chunks[out].start = chunks[in_start].start;
- chunks[out].data = chunks[in_start].data;
- chunks[out].len = chunks[in_end-1].len +
- (chunks[in_end-1].start - chunks[in_start].start);
+ if (merged_last != cur) {
+ chunks->at(merged_last) = std::move(chunks->at(cur));
}
-
- ++out;
- in_start = in_end;
+ merged_last++;
+ cur = to_check;
+ }
+ if (merged_last < chunks->size()) {
+ chunks->erase(chunks->begin() + merged_last, chunks->end());
}
- *num_chunks = out;
}
-ImageChunk* FindChunkByName(const char* name,
- ImageChunk* chunks, int num_chunks) {
- int i;
- for (i = 0; i < num_chunks; ++i) {
- if (chunks[i].type == CHUNK_DEFLATE && chunks[i].filename &&
- strcmp(name, chunks[i].filename) == 0) {
- return chunks+i;
+static ImageChunk* FindChunkByName(const std::string& name, std::vector<ImageChunk>& chunks) {
+ for (size_t i = 0; i < chunks.size(); ++i) {
+ if (chunks[i].GetType() == CHUNK_DEFLATE && chunks[i].GetEntryName() == name) {
+ return &chunks[i];
}
}
- return NULL;
+ return nullptr;
}
-void DumpChunks(ImageChunk* chunks, int num_chunks) {
- for (int i = 0; i < num_chunks; ++i) {
- printf("chunk %d: type %d start %zu len %zu\n",
- i, chunks[i].type, chunks[i].start, chunks[i].len);
- }
+static void DumpChunks(const std::vector<ImageChunk>& chunks) {
+ for (size_t i = 0; i < chunks.size(); ++i) {
+ printf("chunk %zu: ", i);
+ chunks[i].Dump();
+ }
}
-int main(int argc, char** argv) {
- int zip_mode = 0;
+int imgdiff(int argc, const char** argv) {
+ bool zip_mode = false;
if (argc >= 2 && strcmp(argv[1], "-z") == 0) {
- zip_mode = 1;
+ zip_mode = true;
--argc;
++argv;
}
- size_t bonus_size = 0;
- unsigned char* bonus_data = NULL;
+ std::vector<uint8_t> bonus_data;
if (argc >= 3 && strcmp(argv[1], "-b") == 0) {
- struct stat st;
- if (stat(argv[2], &st) != 0) {
- printf("failed to stat bonus file %s: %s\n", argv[2], strerror(errno));
+ android::base::unique_fd fd(open(argv[2], O_RDONLY));
+ if (fd == -1) {
+ printf("failed to open bonus file %s: %s\n", argv[2], strerror(errno));
return 1;
}
- bonus_size = st.st_size;
- bonus_data = reinterpret_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));
+ struct stat st;
+ if (fstat(fd, &st) != 0) {
+ printf("failed to stat bonus file %s: %s\n", argv[2], strerror(errno));
return 1;
}
- if (fread(bonus_data, 1, bonus_size, f) != bonus_size) {
+
+ size_t bonus_size = st.st_size;
+ bonus_data.resize(bonus_size);
+ if (!android::base::ReadFully(fd, bonus_data.data(), bonus_size)) {
printf("failed to read bonus file %s: %s\n", argv[2], strerror(errno));
return 1;
}
- fclose(f);
argc -= 2;
argv += 2;
}
if (argc != 4) {
- usage:
printf("usage: %s [-z] [-b <bonus-file>] <src-img> <tgt-img> <patch-file>\n",
argv[0]);
return 2;
}
- int num_src_chunks;
- ImageChunk* src_chunks;
- int num_tgt_chunks;
- ImageChunk* tgt_chunks;
- int i;
+ std::vector<ImageChunk> src_chunks;
+ std::vector<ImageChunk> tgt_chunks;
+ std::vector<uint8_t> src_file;
+ std::vector<uint8_t> tgt_file;
if (zip_mode) {
- if (ReadZip(argv[1], &num_src_chunks, &src_chunks, 1) == NULL) {
+ if (!ReadZip(argv[1], &src_chunks, &src_file, true)) {
printf("failed to break apart source zip file\n");
return 1;
}
- if (ReadZip(argv[2], &num_tgt_chunks, &tgt_chunks, 0) == NULL) {
+ if (!ReadZip(argv[2], &tgt_chunks, &tgt_file, false)) {
printf("failed to break apart target zip file\n");
return 1;
}
} else {
- if (ReadImage(argv[1], &num_src_chunks, &src_chunks) == NULL) {
+ if (!ReadImage(argv[1], &src_chunks, &src_file)) {
printf("failed to break apart source image\n");
return 1;
}
- if (ReadImage(argv[2], &num_tgt_chunks, &tgt_chunks) == NULL) {
+ if (!ReadImage(argv[2], &tgt_chunks, &tgt_file)) {
printf("failed to break apart target image\n");
return 1;
}
@@ -878,51 +913,47 @@ int main(int argc, char** argv) {
// Verify that the source and target images have the same chunk
// structure (ie, the same sequence of deflate and normal chunks).
- if (!zip_mode) {
- // Merge the gzip header and footer in with any adjacent
- // normal chunks.
- MergeAdjacentNormalChunks(tgt_chunks, &num_tgt_chunks);
- MergeAdjacentNormalChunks(src_chunks, &num_src_chunks);
- }
+ // Merge the gzip header and footer in with any adjacent normal chunks.
+ MergeAdjacentNormalChunks(&tgt_chunks);
+ MergeAdjacentNormalChunks(&src_chunks);
- if (num_src_chunks != num_tgt_chunks) {
+ if (src_chunks.size() != tgt_chunks.size()) {
printf("source and target don't have same number of chunks!\n");
printf("source chunks:\n");
- DumpChunks(src_chunks, num_src_chunks);
+ DumpChunks(src_chunks);
printf("target chunks:\n");
- DumpChunks(tgt_chunks, num_tgt_chunks);
+ DumpChunks(tgt_chunks);
return 1;
}
- for (i = 0; i < num_src_chunks; ++i) {
- if (src_chunks[i].type != tgt_chunks[i].type) {
- printf("source and target don't have same chunk "
- "structure! (chunk %d)\n", i);
+ for (size_t i = 0; i < src_chunks.size(); ++i) {
+ if (src_chunks[i].GetType() != tgt_chunks[i].GetType()) {
+ printf("source and target don't have same chunk structure! (chunk %zu)\n", i);
printf("source chunks:\n");
- DumpChunks(src_chunks, num_src_chunks);
+ DumpChunks(src_chunks);
printf("target chunks:\n");
- DumpChunks(tgt_chunks, num_tgt_chunks);
+ DumpChunks(tgt_chunks);
return 1;
}
}
}
- for (i = 0; i < num_tgt_chunks; ++i) {
- if (tgt_chunks[i].type == CHUNK_DEFLATE) {
+ for (size_t i = 0; i < tgt_chunks.size(); ++i) {
+ if (tgt_chunks[i].GetType() == CHUNK_DEFLATE) {
// Confirm that given the uncompressed chunk data in the target, we
// can recompress it and get exactly the same bits as are in the
// input target image. If this fails, treat the chunk as a normal
// non-deflated chunk.
- if (ReconstructDeflateChunk(tgt_chunks+i) < 0) {
- printf("failed to reconstruct target deflate chunk %d [%s]; "
- "treating as normal\n", i, tgt_chunks[i].filename);
- ChangeDeflateChunkToNormal(tgt_chunks+i);
+ if (!tgt_chunks[i].ReconstructDeflateChunk()) {
+ printf("failed to reconstruct target deflate chunk %zu [%s]; treating as normal\n", i,
+ tgt_chunks[i].GetEntryName().c_str());
+ tgt_chunks[i].ChangeDeflateChunkToNormal();
if (zip_mode) {
- ImageChunk* src = FindChunkByName(tgt_chunks[i].filename, src_chunks, num_src_chunks);
- if (src) {
- ChangeDeflateChunkToNormal(src);
+ ImageChunk* src = FindChunkByName(tgt_chunks[i].GetEntryName(), src_chunks);
+ if (src != nullptr) {
+ src->ChangeDeflateChunkToNormal();
}
} else {
- ChangeDeflateChunkToNormal(src_chunks+i);
+ src_chunks[i].ChangeDeflateChunkToNormal();
}
continue;
}
@@ -935,16 +966,16 @@ int main(int argc, char** argv) {
// data.
ImageChunk* src;
if (zip_mode) {
- src = FindChunkByName(tgt_chunks[i].filename, src_chunks, num_src_chunks);
+ src = FindChunkByName(tgt_chunks[i].GetEntryName(), src_chunks);
} else {
- src = src_chunks+i;
+ src = &src_chunks[i];
}
- if (src == NULL || AreChunksEqual(tgt_chunks+i, src)) {
- ChangeDeflateChunkToNormal(tgt_chunks+i);
- if (src) {
- ChangeDeflateChunkToNormal(src);
- }
+ if (src == nullptr) {
+ tgt_chunks[i].ChangeDeflateChunkToNormal();
+ } else if (tgt_chunks[i] == *src) {
+ tgt_chunks[i].ChangeDeflateChunkToNormal();
+ src->ChangeDeflateChunkToNormal();
}
}
}
@@ -954,14 +985,15 @@ int main(int argc, char** argv) {
// For zips, we only need to do this to the target: deflated
// chunks are matched via filename, and normal chunks are patched
// using the entire source file as the source.
- MergeAdjacentNormalChunks(tgt_chunks, &num_tgt_chunks);
+ MergeAdjacentNormalChunks(&tgt_chunks);
+
} else {
// For images, we need to maintain the parallel structure of the
// chunk lists, so do the merging in both the source and target
// lists.
- MergeAdjacentNormalChunks(tgt_chunks, &num_tgt_chunks);
- MergeAdjacentNormalChunks(src_chunks, &num_src_chunks);
- if (num_src_chunks != num_tgt_chunks) {
+ MergeAdjacentNormalChunks(&tgt_chunks);
+ MergeAdjacentNormalChunks(&src_chunks);
+ if (src_chunks.size() != tgt_chunks.size()) {
// This shouldn't happen.
printf("merging normal chunks went awry\n");
return 1;
@@ -971,35 +1003,43 @@ int main(int argc, char** argv) {
// Compute bsdiff patches for each chunk's data (the uncompressed
// data, in the case of deflate chunks).
- DumpChunks(src_chunks, num_src_chunks);
+ DumpChunks(src_chunks);
- printf("Construct patches for %d chunks...\n", num_tgt_chunks);
- unsigned char** patch_data = reinterpret_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)));
- for (i = 0; i < num_tgt_chunks; ++i) {
+ printf("Construct patches for %zu chunks...\n", tgt_chunks.size());
+ std::vector<std::vector<uint8_t>> patch_data(tgt_chunks.size());
+ saidx_t* bsdiff_cache = nullptr;
+ for (size_t i = 0; i < tgt_chunks.size(); ++i) {
if (zip_mode) {
ImageChunk* src;
- if (tgt_chunks[i].type == CHUNK_DEFLATE &&
- (src = FindChunkByName(tgt_chunks[i].filename, src_chunks,
- num_src_chunks))) {
- patch_data[i] = MakePatch(src, tgt_chunks+i, patch_size+i);
+ if (tgt_chunks[i].GetType() == CHUNK_DEFLATE &&
+ (src = FindChunkByName(tgt_chunks[i].GetEntryName(), src_chunks))) {
+ if (!MakePatch(src, &tgt_chunks[i], &patch_data[i], nullptr)) {
+ printf("Failed to generate patch for target chunk %zu: ", i);
+ return 1;
+ }
} else {
- patch_data[i] = MakePatch(src_chunks, tgt_chunks+i, patch_size+i);
+ if (!MakePatch(&src_chunks[0], &tgt_chunks[i], &patch_data[i], &bsdiff_cache)) {
+ printf("Failed to generate patch for target chunk %zu: ", i);
+ return 1;
+ }
}
} 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].len + bonus_size));
- memcpy(src_chunks[i].data+src_chunks[i].len, bonus_data, bonus_size);
- src_chunks[i].len += bonus_size;
- }
-
- patch_data[i] = MakePatch(src_chunks+i, tgt_chunks+i, patch_size+i);
+ if (i == 1 && !bonus_data.empty()) {
+ printf(" using %zu bytes of bonus data for chunk %zu\n", bonus_data.size(), i);
+ src_chunks[i].SetBonusData(bonus_data);
+ }
+
+ if (!MakePatch(&src_chunks[i], &tgt_chunks[i], &patch_data[i], nullptr)) {
+ printf("Failed to generate patch for target chunk %zu: ", i);
+ return 1;
+ }
}
- printf("patch %3d is %zu bytes (of %zu)\n",
- i, patch_size[i], tgt_chunks[i].source_len);
+ printf("patch %3zu is %zu bytes (of %zu)\n", i, patch_data[i].size(),
+ src_chunks[i].GetRawDataLength());
+ }
+
+ if (bsdiff_cache != nullptr) {
+ free(bsdiff_cache);
}
// Figure out how big the imgdiff file header is going to be, so
@@ -1007,77 +1047,38 @@ int main(int argc, char** argv) {
// within the file.
size_t total_header_size = 12;
- for (i = 0; i < num_tgt_chunks; ++i) {
- total_header_size += 4;
- switch (tgt_chunks[i].type) {
- case CHUNK_NORMAL:
- total_header_size += 8*3;
- break;
- case CHUNK_DEFLATE:
- total_header_size += 8*5 + 4*5;
- break;
- case CHUNK_RAW:
- total_header_size += 4 + patch_size[i];
- break;
- }
+ for (size_t i = 0; i < tgt_chunks.size(); ++i) {
+ total_header_size += tgt_chunks[i].GetHeaderSize(patch_data[i].size());
}
size_t offset = total_header_size;
- FILE* f = fopen(argv[3], "wb");
+ android::base::unique_fd patch_fd(open(argv[3], O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR));
+ if (patch_fd == -1) {
+ printf("failed to open \"%s\": %s\n", argv[3], strerror(errno));
+ return 1;
+ }
// Write out the headers.
-
- fwrite("IMGDIFF2", 1, 8, f);
- Write4(num_tgt_chunks, f);
- for (i = 0; i < num_tgt_chunks; ++i) {
- Write4(tgt_chunks[i].type, f);
-
- switch (tgt_chunks[i].type) {
- case CHUNK_NORMAL:
- printf("chunk %3d: normal (%10zu, %10zu) %10zu\n", i,
- tgt_chunks[i].start, tgt_chunks[i].len, patch_size[i]);
- Write8(tgt_chunks[i].source_start, f);
- Write8(tgt_chunks[i].source_len, f);
- Write8(offset, f);
- offset += patch_size[i];
- break;
-
- case CHUNK_DEFLATE:
- printf("chunk %3d: deflate (%10zu, %10zu) %10zu %s\n", i,
- tgt_chunks[i].start, tgt_chunks[i].deflate_len, patch_size[i],
- tgt_chunks[i].filename);
- Write8(tgt_chunks[i].source_start, f);
- Write8(tgt_chunks[i].source_len, f);
- Write8(offset, f);
- Write8(tgt_chunks[i].source_uncompressed_len, f);
- Write8(tgt_chunks[i].len, f);
- Write4(tgt_chunks[i].level, f);
- Write4(tgt_chunks[i].method, f);
- Write4(tgt_chunks[i].windowBits, f);
- Write4(tgt_chunks[i].memLevel, f);
- Write4(tgt_chunks[i].strategy, f);
- offset += patch_size[i];
- break;
-
- case CHUNK_RAW:
- printf("chunk %3d: raw (%10zu, %10zu)\n", i,
- tgt_chunks[i].start, tgt_chunks[i].len);
- Write4(patch_size[i], f);
- fwrite(patch_data[i], 1, patch_size[i], f);
- break;
- }
+ if (!android::base::WriteStringToFd("IMGDIFF2", patch_fd)) {
+ printf("failed to write \"IMGDIFF2\" to \"%s\": %s\n", argv[3], strerror(errno));
+ return 1;
+ }
+ Write4(patch_fd, static_cast<int32_t>(tgt_chunks.size()));
+ for (size_t i = 0; i < tgt_chunks.size(); ++i) {
+ printf("chunk %zu: ", i);
+ offset = tgt_chunks[i].WriteHeaderToFd(patch_fd, patch_data[i], offset);
}
// Append each chunk's bsdiff patch, in order.
-
- for (i = 0; i < num_tgt_chunks; ++i) {
- if (tgt_chunks[i].type != CHUNK_RAW) {
- fwrite(patch_data[i], 1, patch_size[i], f);
+ for (size_t i = 0; i < tgt_chunks.size(); ++i) {
+ if (tgt_chunks[i].GetType() != CHUNK_RAW) {
+ if (!android::base::WriteFully(patch_fd, patch_data[i].data(), patch_data[i].size())) {
+ CHECK(false) << "failed to write " << patch_data[i].size() << " bytes patch for chunk "
+ << i;
+ }
}
}
- fclose(f);
-
return 0;
}
diff --git a/applypatch/imgdiff_main.cpp b/applypatch/imgdiff_main.cpp
new file mode 100644
index 000000000..7d5bdf9aa
--- /dev/null
+++ b/applypatch/imgdiff_main.cpp
@@ -0,0 +1,21 @@
+/*
+ * 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/imgdiff.h"
+
+int main(int argc, char** argv) {
+ return imgdiff(argc, const_cast<const char**>(argv));
+}
diff --git a/applypatch/imgpatch.cpp b/applypatch/imgpatch.cpp
index d175d6385..adcc61fd6 100644
--- a/applypatch/imgpatch.cpp
+++ b/applypatch/imgpatch.cpp
@@ -14,31 +14,41 @@
* limitations under the License.
*/
-// See imgdiff.c in this directory for a description of the patch file
+// See imgdiff.cpp in this directory for a description of the patch file
// format.
+#include <applypatch/imgpatch.h>
+
+#include <errno.h>
#include <stdio.h>
+#include <string.h>
#include <sys/cdefs.h>
#include <sys/stat.h>
-#include <errno.h>
#include <unistd.h>
-#include <string.h>
+#include <string>
#include <vector>
-#include "zlib.h"
-#include "openssl/sha.h"
-#include "applypatch.h"
-#include "imgdiff.h"
-#include "utils.h"
+#include <applypatch/applypatch.h>
+#include <applypatch/imgdiff.h>
+#include <android-base/memory.h>
+#include <openssl/sha.h>
+#include <zlib.h>
+
+static inline int64_t Read8(const void *address) {
+ return android::base::get_unaligned<int64_t>(address);
+}
+
+static inline int32_t Read4(const void *address) {
+ return android::base::get_unaligned<int32_t>(address);
+}
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);
}
/*
@@ -47,203 +57,204 @@ int ApplyImagePatch(const unsigned char* old_data, ssize_t old_size,
* file, and update the SHA context with the output data as well.
* Return 0 on success.
*/
-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) {
- printf("patch too short to contain header\n");
- return -1;
+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) {
+ if (patch->data.size() < 12) {
+ printf("patch too short to contain header\n");
+ return -1;
+ }
+
+ // 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;
+ }
+
+ int num_chunks = Read4(header + 8);
+
+ for (int i = 0; i < num_chunks; ++i) {
+ // each chunk's header record starts with 4 bytes.
+ if (pos + 4 > patch->data.size()) {
+ printf("failed to read chunk %d record\n", i);
+ return -1;
}
+ int type = Read4(&patch->data[pos]);
+ pos += 4;
- // IMGDIFF2 uses CHUNK_NORMAL, CHUNK_DEFLATE, and CHUNK_RAW.
- // (IMGDIFF1, which is no longer supported, used CHUNK_NORMAL and
- // CHUNK_GZIP.)
- if (memcmp(header, "IMGDIFF2", 8) != 0) {
- printf("corrupt patch file header (magic number)\n");
+ if (type == CHUNK_NORMAL) {
+ const char* normal_header = &patch->data[pos];
+ pos += 24;
+ if (pos > patch->data.size()) {
+ printf("failed to read chunk %d normal header data\n", i);
return -1;
- }
+ }
- int num_chunks = Read4(header+8);
+ size_t src_start = static_cast<size_t>(Read8(normal_header));
+ size_t src_len = static_cast<size_t>(Read8(normal_header + 8));
+ size_t patch_offset = static_cast<size_t>(Read8(normal_header + 16));
- int i;
- for (i = 0; i < num_chunks; ++i) {
- // each chunk's header record starts with 4 bytes.
- if (pos + 4 > patch->size) {
- printf("failed to read chunk %d record\n", i);
- return -1;
+ if (src_start + src_len > static_cast<size_t>(old_size)) {
+ printf("source data too short\n");
+ return -1;
+ }
+ ApplyBSDiffPatch(old_data + src_start, src_len, patch, patch_offset, sink, token, ctx);
+ } else if (type == CHUNK_RAW) {
+ const char* raw_header = &patch->data[pos];
+ pos += 4;
+ 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->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(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;
+ }
+ pos += data_len;
+ } else if (type == CHUNK_DEFLATE) {
+ // deflate chunks have an additional 60 bytes in their chunk header.
+ const char* deflate_header = &patch->data[pos];
+ pos += 60;
+ if (pos > patch->data.size()) {
+ printf("failed to read chunk %d deflate header data\n", i);
+ return -1;
+ }
+
+ size_t src_start = static_cast<size_t>(Read8(deflate_header));
+ size_t src_len = static_cast<size_t>(Read8(deflate_header + 8));
+ size_t patch_offset = static_cast<size_t>(Read8(deflate_header + 16));
+ size_t expanded_len = static_cast<size_t>(Read8(deflate_header + 24));
+ size_t target_len = static_cast<size_t>(Read8(deflate_header + 32));
+ int level = Read4(deflate_header + 40);
+ int method = Read4(deflate_header + 44);
+ int windowBits = Read4(deflate_header + 48);
+ int memLevel = Read4(deflate_header + 52);
+ int strategy = Read4(deflate_header + 56);
+
+ if (src_start + src_len > static_cast<size_t>(old_size)) {
+ printf("source data too short\n");
+ return -1;
+ }
+
+ // Decompress the source data; the chunk header tells us exactly
+ // how big we expect it to be when decompressed.
+
+ // Note: expanded_len will include the bonus data size if
+ // 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->data.size() : 0;
+
+ std::vector<unsigned char> expanded_source(expanded_len);
+
+ // inflate() doesn't like strm.next_out being a nullptr even with
+ // avail_out being zero (Z_STREAM_ERROR).
+ if (expanded_len != 0) {
+ z_stream strm;
+ strm.zalloc = Z_NULL;
+ strm.zfree = Z_NULL;
+ strm.opaque = Z_NULL;
+ strm.avail_in = src_len;
+ strm.next_in = old_data + src_start;
+ strm.avail_out = expanded_len;
+ strm.next_out = expanded_source.data();
+
+ int ret = inflateInit2(&strm, -15);
+ if (ret != Z_OK) {
+ printf("failed to init source inflation: %d\n", ret);
+ return -1;
}
- int type = Read4(patch->data + pos);
- pos += 4;
-
- if (type == CHUNK_NORMAL) {
- char* normal_header = patch->data + pos;
- pos += 24;
- if (pos > patch->size) {
- printf("failed to read chunk %d normal header data\n", i);
- return -1;
- }
-
- size_t src_start = Read8(normal_header);
- size_t src_len = Read8(normal_header+8);
- size_t patch_offset = Read8(normal_header+16);
-
- if (src_start + src_len > static_cast<size_t>(old_size)) {
- printf("source data too short\n");
- return -1;
- }
- ApplyBSDiffPatch(old_data + src_start, src_len,
- patch, patch_offset, sink, token, ctx);
- } else if (type == CHUNK_RAW) {
- char* raw_header = patch->data + pos;
- pos += 4;
- if (pos > patch->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) {
- 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,
- data_len, token) != data_len) {
- printf("failed to write chunk %d raw data\n", i);
- return -1;
- }
- 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;
- pos += 60;
- if (pos > patch->size) {
- printf("failed to read chunk %d deflate header data\n", i);
- return -1;
- }
-
- size_t src_start = Read8(deflate_header);
- size_t src_len = Read8(deflate_header+8);
- size_t patch_offset = Read8(deflate_header+16);
- size_t expanded_len = Read8(deflate_header+24);
- int level = Read4(deflate_header+40);
- int method = Read4(deflate_header+44);
- int windowBits = Read4(deflate_header+48);
- int memLevel = Read4(deflate_header+52);
- int strategy = Read4(deflate_header+56);
-
- if (src_start + src_len > static_cast<size_t>(old_size)) {
- printf("source data too short\n");
- return -1;
- }
-
- // Decompress the source data; the chunk header tells us exactly
- // how big we expect it to be when decompressed.
-
- // Note: expanded_len will include the bonus data size if
- // 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;
-
- std::vector<unsigned char> expanded_source(expanded_len);
-
- // inflate() doesn't like strm.next_out being a nullptr even with
- // avail_out being zero (Z_STREAM_ERROR).
- if (expanded_len != 0) {
- z_stream strm;
- strm.zalloc = Z_NULL;
- strm.zfree = Z_NULL;
- strm.opaque = Z_NULL;
- strm.avail_in = src_len;
- strm.next_in = (unsigned char*)(old_data + src_start);
- strm.avail_out = expanded_len;
- strm.next_out = expanded_source.data();
-
- int ret;
- ret = inflateInit2(&strm, -15);
- if (ret != Z_OK) {
- printf("failed to init source inflation: %d\n", ret);
- return -1;
- }
-
- // Because we've provided enough room to accommodate the output
- // data, we expect one call to inflate() to suffice.
- ret = inflate(&strm, Z_SYNC_FLUSH);
- if (ret != Z_STREAM_END) {
- printf("source inflation returned %d\n", ret);
- return -1;
- }
- // We should have filled the output buffer exactly, except
- // for the bonus_size.
- if (strm.avail_out != bonus_size) {
- printf("source inflation short by %zu bytes\n", strm.avail_out-bonus_size);
- return -1;
- }
- inflateEnd(&strm);
-
- if (bonus_size) {
- memcpy(expanded_source.data() + (expanded_len - bonus_size),
- bonus_data->data, bonus_size);
- }
- }
-
- // Next, apply the bsdiff patch (in memory) to the uncompressed
- // data.
- std::vector<unsigned char> uncompressed_target_data;
- if (ApplyBSDiffPatchMem(expanded_source.data(), expanded_len,
- patch, patch_offset,
- &uncompressed_target_data) != 0) {
- return -1;
- }
-
- // Now compress the target data and append it to the output.
-
- // we're done with the expanded_source data buffer, so we'll
- // reuse that memory to receive the output of deflate.
- if (expanded_source.size() < 32768U) {
- expanded_source.resize(32768U);
- }
-
- {
- std::vector<unsigned char>& temp_data = expanded_source;
-
- // now the deflate stream
- z_stream strm;
- strm.zalloc = Z_NULL;
- strm.zfree = Z_NULL;
- strm.opaque = Z_NULL;
- strm.avail_in = uncompressed_target_data.size();
- strm.next_in = uncompressed_target_data.data();
- int ret = deflateInit2(&strm, level, method, windowBits, memLevel, strategy);
- if (ret != Z_OK) {
- printf("failed to init uncompressed data deflation: %d\n", ret);
- return -1;
- }
- do {
- strm.avail_out = temp_data.size();
- strm.next_out = temp_data.data();
- ret = deflate(&strm, Z_FINISH);
- 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);
- return -1;
- }
- if (ctx) SHA1_Update(ctx, temp_data.data(), have);
- } while (ret != Z_STREAM_END);
- deflateEnd(&strm);
- }
- } else {
- printf("patch chunk %d is unknown type %d\n", i, type);
- return -1;
+
+ // Because we've provided enough room to accommodate the output
+ // data, we expect one call to inflate() to suffice.
+ ret = inflate(&strm, Z_SYNC_FLUSH);
+ if (ret != Z_STREAM_END) {
+ printf("source inflation returned %d\n", ret);
+ return -1;
+ }
+ // We should have filled the output buffer exactly, except
+ // for the bonus_size.
+ if (strm.avail_out != bonus_size) {
+ printf("source inflation short by %zu bytes\n", strm.avail_out - bonus_size);
+ return -1;
}
+ inflateEnd(&strm);
+
+ if (bonus_size) {
+ memcpy(expanded_source.data() + (expanded_len - bonus_size), &bonus_data->data[0],
+ bonus_size);
+ }
+ }
+
+ // Next, apply the bsdiff patch (in memory) to the uncompressed data.
+ std::vector<unsigned char> uncompressed_target_data;
+ // TODO(senj): Remove the only usage of ApplyBSDiffPatchMem here,
+ // replace it with ApplyBSDiffPatch with a custom sink function that
+ // wraps the given sink function to stream output to save memory.
+ if (ApplyBSDiffPatchMem(expanded_source.data(), expanded_len, patch, patch_offset,
+ &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.
+
+ // we're done with the expanded_source data buffer, so we'll
+ // reuse that memory to receive the output of deflate.
+ if (expanded_source.size() < 32768U) {
+ expanded_source.resize(32768U);
+ }
+
+ {
+ std::vector<unsigned char>& temp_data = expanded_source;
+
+ // now the deflate stream
+ z_stream strm;
+ strm.zalloc = Z_NULL;
+ strm.zfree = Z_NULL;
+ strm.opaque = Z_NULL;
+ strm.avail_in = uncompressed_target_data.size();
+ strm.next_in = uncompressed_target_data.data();
+ int ret = deflateInit2(&strm, level, method, windowBits, memLevel, strategy);
+ if (ret != Z_OK) {
+ printf("failed to init uncompressed data deflation: %d\n", ret);
+ return -1;
+ }
+ do {
+ strm.avail_out = temp_data.size();
+ strm.next_out = temp_data.data();
+ ret = deflate(&strm, Z_FINISH);
+ ssize_t have = temp_data.size() - strm.avail_out;
+
+ if (sink(temp_data.data(), have, token) != have) {
+ printf("failed to write %zd compressed bytes to output\n", have);
+ return -1;
+ }
+ if (ctx) SHA1_Update(ctx, temp_data.data(), have);
+ } while (ret != Z_STREAM_END);
+ deflateEnd(&strm);
+ }
+ } else {
+ printf("patch chunk %d is unknown type %d\n", i, type);
+ return -1;
}
+ }
- return 0;
+ return 0;
}
diff --git a/applypatch/applypatch.h b/applypatch/include/applypatch/applypatch.h
index f392c5534..4489decb6 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,33 +43,28 @@ 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);
-// bsdiff.cpp
+// bspatch.cpp
void ShowBSDiffLicense();
int ApplyBSDiffPatch(const unsigned char* old_data, ssize_t old_size,
const Value* patch, ssize_t patch_offset,
diff --git a/applypatch/imgdiff.h b/applypatch/include/applypatch/imgdiff.h
index f2069b4f3..22cbd4fa0 100644
--- a/applypatch/imgdiff.h
+++ b/applypatch/include/applypatch/imgdiff.h
@@ -14,17 +14,26 @@
* limitations under the License.
*/
+#ifndef _APPLYPATCH_IMGDIFF_H
+#define _APPLYPATCH_IMGDIFF_H
+
+#include <stddef.h>
+
// Image patch chunk types
-#define CHUNK_NORMAL 0
-#define CHUNK_GZIP 1 // version 1 only
-#define CHUNK_DEFLATE 2 // version 2 only
-#define CHUNK_RAW 3 // version 2 only
+#define CHUNK_NORMAL 0
+#define CHUNK_GZIP 1 // version 1 only
+#define CHUNK_DEFLATE 2 // version 2 only
+#define CHUNK_RAW 3 // version 2 only
// The gzip header size is actually variable, but we currently don't
// support gzipped data with any of the optional fields, so for now it
// will always be ten bytes. See RFC 1952 for the definition of the
// gzip format.
-#define GZIP_HEADER_LEN 10
+static constexpr size_t GZIP_HEADER_LEN = 10;
// The gzip footer size really is fixed.
-#define GZIP_FOOTER_LEN 8
+static constexpr size_t GZIP_FOOTER_LEN = 8;
+
+int imgdiff(int argc, const char** argv);
+
+#endif // _APPLYPATCH_IMGDIFF_H
diff --git a/applypatch/include/applypatch/imgpatch.h b/applypatch/include/applypatch/imgpatch.h
index 64d9aa9eb..6549f79f0 100644
--- a/applypatch/include/applypatch/imgpatch.h
+++ b/applypatch/include/applypatch/imgpatch.h
@@ -14,13 +14,15 @@
* limitations under the License.
*/
-#ifndef _IMGPATCH_H
-#define _IMGPATCH_H
+#ifndef _APPLYPATCH_IMGPATCH_H
+#define _APPLYPATCH_IMGPATCH_H
-typedef ssize_t (*SinkFn)(const unsigned char*, ssize_t, void*);
+#include <sys/types.h>
+
+using SinkFn = ssize_t (*)(const unsigned char*, ssize_t, void*);
int ApplyImagePatch(const unsigned char* old_data, ssize_t old_size,
const unsigned char* patch_data, ssize_t patch_size,
SinkFn sink, void* token);
-#endif //_IMGPATCH_H
+#endif // _APPLYPATCH_IMGPATCH_H
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
deleted file mode 100644
index 4a80be75f..000000000
--- a/applypatch/utils.cpp
+++ /dev/null
@@ -1,65 +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.
- */
-
-#include <stdio.h>
-
-#include "utils.h"
-
-/** Write a 4-byte value to f in little-endian order. */
-void Write4(int value, FILE* f) {
- fputc(value & 0xff, f);
- fputc((value >> 8) & 0xff, f);
- fputc((value >> 16) & 0xff, f);
- fputc((value >> 24) & 0xff, f);
-}
-
-/** Write an 8-byte value to f in little-endian order. */
-void Write8(long long value, FILE* f) {
- fputc(value & 0xff, f);
- fputc((value >> 8) & 0xff, f);
- fputc((value >> 16) & 0xff, f);
- fputc((value >> 24) & 0xff, f);
- fputc((value >> 32) & 0xff, f);
- fputc((value >> 40) & 0xff, f);
- fputc((value >> 48) & 0xff, f);
- fputc((value >> 56) & 0xff, f);
-}
-
-int Read2(void* pv) {
- unsigned char* p = reinterpret_cast<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);
- 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]);
-}
diff --git a/applypatch/utils.h b/private/install.h
index bc97f1720..12d303b01 100644
--- a/applypatch/utils.h
+++ b/private/install.h
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2009 The Android Open Source Project
+ * Copyright (C) 2017 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.
@@ -14,17 +14,14 @@
* limitations under the License.
*/
-#ifndef _BUILD_TOOLS_APPLYPATCH_UTILS_H
-#define _BUILD_TOOLS_APPLYPATCH_UTILS_H
+// Private headers exposed for testing purpose only.
-#include <stdio.h>
+#pragma once
-// Read and write little-endian values of various sizes.
+#include <string>
+#include <vector>
-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);
+#include <ziparchive/zip_archive.h>
-#endif // _BUILD_TOOLS_APPLYPATCH_UTILS_H
+int update_binary_command(const std::string& path, ZipArchiveHandle zip, int retry_count,
+ int status_fd, std::vector<std::string>* cmd);