Loading Android.mk +3 −2 Original line number Diff line number Diff line Loading @@ -77,7 +77,8 @@ LOCAL_STATIC_LIBRARIES := \ libbatterymonitor \ libext4_utils_static \ libsparse_static \ libminzip \ libziparchive \ libotautil \ libmounts \ libz \ libminadbd \ Loading Loading @@ -150,7 +151,7 @@ LOCAL_CFLAGS := -Werror include $(BUILD_STATIC_LIBRARY) include $(LOCAL_PATH)/minui/Android.mk \ $(LOCAL_PATH)/minzip/Android.mk \ $(LOCAL_PATH)/otautil/Android.mk \ $(LOCAL_PATH)/minadbd/Android.mk \ $(LOCAL_PATH)/tests/Android.mk \ $(LOCAL_PATH)/tools/Android.mk \ Loading applypatch/Android.mk +0 −1 Original line number Diff line number Diff line Loading @@ -80,7 +80,6 @@ LOCAL_STATIC_LIBRARIES += \ libbase \ libedify \ libotafault \ libminzip \ libcrypto \ libbz LOCAL_SHARED_LIBRARIES += libbase libz libcutils libc Loading install.cpp +23 −22 Original line number Diff line number Diff line Loading @@ -32,13 +32,13 @@ #include <android-base/parseint.h> #include <android-base/stringprintf.h> #include <android-base/strings.h> #include <ziparchive/zip_archive.h> #include "common.h" #include "error_code.h" #include "install.h" #include "minui/minui.h" #include "minzip/SysUtil.h" #include "minzip/Zip.h" #include "otautil/SysUtil.h" #include "roots.h" #include "ui.h" #include "verifier.h" Loading Loading @@ -72,15 +72,17 @@ static int parse_build_number(const std::string& str) { } // Read the build.version.incremental of src/tgt from the metadata and log it to last_install. static void read_source_target_build(ZipArchive* zip, std::vector<std::string>& log_buffer) { const ZipEntry* meta_entry = mzFindZipEntry(zip, METADATA_PATH); if (meta_entry == nullptr) { static void read_source_target_build(ZipArchiveHandle zip, std::vector<std::string>& log_buffer) { ZipString metadata_path(METADATA_PATH); ZipEntry meta_entry; if (FindEntry(zip, metadata_path, &meta_entry) != 0) { LOG(ERROR) << "Failed to find " << METADATA_PATH << " in update package"; return; } std::string meta_data(meta_entry->uncompLen, '\0'); if (!mzReadZipEntry(zip, meta_entry, &meta_data[0], meta_entry->uncompLen)) { std::string meta_data(meta_entry.uncompressed_length, '\0'); if (ExtractToMemory(zip, &meta_entry, reinterpret_cast<uint8_t*>(&meta_data[0]), meta_entry.uncompressed_length) != 0) { LOG(ERROR) << "Failed to read metadata in update package"; return; } Loading Loading @@ -109,15 +111,14 @@ static void read_source_target_build(ZipArchive* zip, std::vector<std::string>& // If the package contains an update binary, extract it and run it. static int try_update_binary(const char* path, ZipArchive* zip, bool* wipe_cache, try_update_binary(const char* path, ZipArchiveHandle zip, bool* wipe_cache, std::vector<std::string>& log_buffer, int retry_count) { read_source_target_build(zip, log_buffer); const ZipEntry* binary_entry = mzFindZipEntry(zip, ASSUMED_UPDATE_BINARY_NAME); if (binary_entry == NULL) { mzCloseZipArchive(zip); ZipString binary_name(ASSUMED_UPDATE_BINARY_NAME); ZipEntry binary_entry; if (FindEntry(zip, binary_name, &binary_entry) != 0) { return INSTALL_CORRUPT; } Loading @@ -126,15 +127,14 @@ try_update_binary(const char* path, ZipArchive* zip, bool* wipe_cache, int fd = creat(binary, 0755); if (fd < 0) { PLOG(ERROR) << "Can't make " << binary; mzCloseZipArchive(zip); return INSTALL_ERROR; } bool ok = mzExtractZipEntryToFile(zip, binary_entry, fd); int error = ExtractEntryToFile(zip, &binary_entry, fd); close(fd); mzCloseZipArchive(zip); if (!ok) { LOG(ERROR) << "Can't copy " << ASSUMED_UPDATE_BINARY_NAME; if (error != 0) { LOG(ERROR) << "Can't copy " << ASSUMED_UPDATE_BINARY_NAME << " : " << ErrorCodeString(error); return INSTALL_ERROR; } Loading Loading @@ -326,13 +326,14 @@ really_install_package(const char *path, bool* wipe_cache, bool needs_mount, } // Try to open the package. ZipArchive zip; err = mzOpenZipArchive(map.addr, map.length, &zip); ZipArchiveHandle zip; err = OpenArchiveFromMemory(map.addr, map.length, path, &zip); if (err != 0) { LOG(ERROR) << "Can't open " << path; LOG(ERROR) << "Can't open " << path << " : " << ErrorCodeString(err); log_buffer.push_back(android::base::StringPrintf("error: %d", kZipOpenFailure)); sysReleaseMap(&map); CloseArchive(zip); return INSTALL_CORRUPT; } Loading @@ -342,12 +343,12 @@ really_install_package(const char *path, bool* wipe_cache, bool needs_mount, ui->Print("Retry attempt: %d\n", retry_count); } ui->SetEnableReboot(false); int result = try_update_binary(path, &zip, wipe_cache, log_buffer, retry_count); int result = try_update_binary(path, zip, wipe_cache, log_buffer, retry_count); ui->SetEnableReboot(true); ui->Print("\n"); sysReleaseMap(&map); CloseArchive(zip); return result; } Loading minzip/Android.mkdeleted 100644 → 0 +0 −23 Original line number Diff line number Diff line LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_SRC_FILES := \ Hash.cpp \ SysUtil.cpp \ DirUtil.cpp \ Inlines.c \ Zip.cpp LOCAL_C_INCLUDES := \ external/zlib \ external/safe-iop/include LOCAL_STATIC_LIBRARIES := libselinux libbase LOCAL_MODULE := libminzip LOCAL_CLANG := true LOCAL_CFLAGS += -Werror -Wall include $(BUILD_STATIC_LIBRARY) minzip/Bits.hdeleted 100644 → 0 +0 −357 Original line number Diff line number Diff line /* * Copyright 2006 The Android Open Source Project * * Some handy functions for manipulating bits and bytes. */ #ifndef _MINZIP_BITS #define _MINZIP_BITS #include "inline_magic.h" #include <stdlib.h> #include <string.h> /* * Get 1 byte. (Included to make the code more legible.) */ INLINE unsigned char get1(unsigned const char* pSrc) { return *pSrc; } /* * Get 2 big-endian bytes. */ INLINE unsigned short get2BE(unsigned char const* pSrc) { unsigned short result; result = *pSrc++ << 8; result |= *pSrc++; return result; } /* * Get 4 big-endian bytes. */ INLINE unsigned int get4BE(unsigned char const* pSrc) { unsigned int result; result = *pSrc++ << 24; result |= *pSrc++ << 16; result |= *pSrc++ << 8; result |= *pSrc++; return result; } /* * Get 8 big-endian bytes. */ INLINE unsigned long long get8BE(unsigned char const* pSrc) { unsigned long long result; result = (unsigned long long) *pSrc++ << 56; result |= (unsigned long long) *pSrc++ << 48; result |= (unsigned long long) *pSrc++ << 40; result |= (unsigned long long) *pSrc++ << 32; result |= (unsigned long long) *pSrc++ << 24; result |= (unsigned long long) *pSrc++ << 16; result |= (unsigned long long) *pSrc++ << 8; result |= (unsigned long long) *pSrc++; return result; } /* * Get 2 little-endian bytes. */ INLINE unsigned short get2LE(unsigned char const* pSrc) { unsigned short result; result = *pSrc++; result |= *pSrc++ << 8; return result; } /* * Get 4 little-endian bytes. */ INLINE unsigned int get4LE(unsigned char const* pSrc) { unsigned int result; result = *pSrc++; result |= *pSrc++ << 8; result |= *pSrc++ << 16; result |= *pSrc++ << 24; return result; } /* * Get 8 little-endian bytes. */ INLINE unsigned long long get8LE(unsigned char const* pSrc) { unsigned long long result; result = (unsigned long long) *pSrc++; result |= (unsigned long long) *pSrc++ << 8; result |= (unsigned long long) *pSrc++ << 16; result |= (unsigned long long) *pSrc++ << 24; result |= (unsigned long long) *pSrc++ << 32; result |= (unsigned long long) *pSrc++ << 40; result |= (unsigned long long) *pSrc++ << 48; result |= (unsigned long long) *pSrc++ << 56; return result; } /* * Grab 1 byte and advance the data pointer. */ INLINE unsigned char read1(unsigned const char** ppSrc) { return *(*ppSrc)++; } /* * Grab 2 big-endian bytes and advance the data pointer. */ INLINE unsigned short read2BE(unsigned char const** ppSrc) { unsigned short result; result = *(*ppSrc)++ << 8; result |= *(*ppSrc)++; return result; } /* * Grab 4 big-endian bytes and advance the data pointer. */ INLINE unsigned int read4BE(unsigned char const** ppSrc) { unsigned int result; result = *(*ppSrc)++ << 24; result |= *(*ppSrc)++ << 16; result |= *(*ppSrc)++ << 8; result |= *(*ppSrc)++; return result; } /* * Get 8 big-endian bytes. */ INLINE unsigned long long read8BE(unsigned char const** ppSrc) { unsigned long long result; result = (unsigned long long) *(*ppSrc)++ << 56; result |= (unsigned long long) *(*ppSrc)++ << 48; result |= (unsigned long long) *(*ppSrc)++ << 40; result |= (unsigned long long) *(*ppSrc)++ << 32; result |= (unsigned long long) *(*ppSrc)++ << 24; result |= (unsigned long long) *(*ppSrc)++ << 16; result |= (unsigned long long) *(*ppSrc)++ << 8; result |= (unsigned long long) *(*ppSrc)++; return result; } /* * Grab 2 little-endian bytes and advance the data pointer. */ INLINE unsigned short read2LE(unsigned char const** ppSrc) { unsigned short result; result = *(*ppSrc)++; result |= *(*ppSrc)++ << 8; return result; } /* * Grab 4 little-endian bytes and advance the data pointer. */ INLINE unsigned int read4LE(unsigned char const** ppSrc) { unsigned int result; result = *(*ppSrc)++; result |= *(*ppSrc)++ << 8; result |= *(*ppSrc)++ << 16; result |= *(*ppSrc)++ << 24; return result; } /* * Get 8 little-endian bytes. */ INLINE unsigned long long read8LE(unsigned char const** ppSrc) { unsigned long long result; result = (unsigned long long) *(*ppSrc)++; result |= (unsigned long long) *(*ppSrc)++ << 8; result |= (unsigned long long) *(*ppSrc)++ << 16; result |= (unsigned long long) *(*ppSrc)++ << 24; result |= (unsigned long long) *(*ppSrc)++ << 32; result |= (unsigned long long) *(*ppSrc)++ << 40; result |= (unsigned long long) *(*ppSrc)++ << 48; result |= (unsigned long long) *(*ppSrc)++ << 56; return result; } /* * Skip over a UTF-8 string. */ INLINE void skipUtf8String(unsigned char const** ppSrc) { unsigned int length = read4BE(ppSrc); (*ppSrc) += length; } /* * Read a UTF-8 string into a fixed-size buffer, and null-terminate it. * * Returns the length of the original string. */ INLINE int readUtf8String(unsigned char const** ppSrc, char* buf, size_t bufLen) { unsigned int length = read4BE(ppSrc); size_t copyLen = (length < bufLen) ? length : bufLen-1; memcpy(buf, *ppSrc, copyLen); buf[copyLen] = '\0'; (*ppSrc) += length; return length; } /* * Read a UTF-8 string into newly-allocated storage, and null-terminate it. * * Returns the string and its length. (The latter is probably unnecessary * for the way we're using UTF8.) */ INLINE char* readNewUtf8String(unsigned char const** ppSrc, size_t* pLength) { unsigned int length = read4BE(ppSrc); char* buf; buf = (char*) malloc(length+1); memcpy(buf, *ppSrc, length); buf[length] = '\0'; (*ppSrc) += length; *pLength = length; return buf; } /* * Set 1 byte. (Included to make the code more legible.) */ INLINE void set1(unsigned char* buf, unsigned char val) { *buf = (unsigned char)(val); } /* * Set 2 big-endian bytes. */ INLINE void set2BE(unsigned char* buf, unsigned short val) { *buf++ = (unsigned char)(val >> 8); *buf = (unsigned char)(val); } /* * Set 4 big-endian bytes. */ INLINE void set4BE(unsigned char* buf, unsigned int val) { *buf++ = (unsigned char)(val >> 24); *buf++ = (unsigned char)(val >> 16); *buf++ = (unsigned char)(val >> 8); *buf = (unsigned char)(val); } /* * Set 8 big-endian bytes. */ INLINE void set8BE(unsigned char* buf, unsigned long long val) { *buf++ = (unsigned char)(val >> 56); *buf++ = (unsigned char)(val >> 48); *buf++ = (unsigned char)(val >> 40); *buf++ = (unsigned char)(val >> 32); *buf++ = (unsigned char)(val >> 24); *buf++ = (unsigned char)(val >> 16); *buf++ = (unsigned char)(val >> 8); *buf = (unsigned char)(val); } /* * Set 2 little-endian bytes. */ INLINE void set2LE(unsigned char* buf, unsigned short val) { *buf++ = (unsigned char)(val); *buf = (unsigned char)(val >> 8); } /* * Set 4 little-endian bytes. */ INLINE void set4LE(unsigned char* buf, unsigned int val) { *buf++ = (unsigned char)(val); *buf++ = (unsigned char)(val >> 8); *buf++ = (unsigned char)(val >> 16); *buf = (unsigned char)(val >> 24); } /* * Set 8 little-endian bytes. */ INLINE void set8LE(unsigned char* buf, unsigned long long val) { *buf++ = (unsigned char)(val); *buf++ = (unsigned char)(val >> 8); *buf++ = (unsigned char)(val >> 16); *buf++ = (unsigned char)(val >> 24); *buf++ = (unsigned char)(val >> 32); *buf++ = (unsigned char)(val >> 40); *buf++ = (unsigned char)(val >> 48); *buf = (unsigned char)(val >> 56); } /* * Stuff a UTF-8 string into the buffer. */ INLINE void setUtf8String(unsigned char* buf, const unsigned char* str) { unsigned int strLen = strlen((const char*)str); set4BE(buf, strLen); memcpy(buf + sizeof(unsigned int), str, strLen); } #endif /*_MINZIP_BITS*/ Loading
Android.mk +3 −2 Original line number Diff line number Diff line Loading @@ -77,7 +77,8 @@ LOCAL_STATIC_LIBRARIES := \ libbatterymonitor \ libext4_utils_static \ libsparse_static \ libminzip \ libziparchive \ libotautil \ libmounts \ libz \ libminadbd \ Loading Loading @@ -150,7 +151,7 @@ LOCAL_CFLAGS := -Werror include $(BUILD_STATIC_LIBRARY) include $(LOCAL_PATH)/minui/Android.mk \ $(LOCAL_PATH)/minzip/Android.mk \ $(LOCAL_PATH)/otautil/Android.mk \ $(LOCAL_PATH)/minadbd/Android.mk \ $(LOCAL_PATH)/tests/Android.mk \ $(LOCAL_PATH)/tools/Android.mk \ Loading
applypatch/Android.mk +0 −1 Original line number Diff line number Diff line Loading @@ -80,7 +80,6 @@ LOCAL_STATIC_LIBRARIES += \ libbase \ libedify \ libotafault \ libminzip \ libcrypto \ libbz LOCAL_SHARED_LIBRARIES += libbase libz libcutils libc Loading
install.cpp +23 −22 Original line number Diff line number Diff line Loading @@ -32,13 +32,13 @@ #include <android-base/parseint.h> #include <android-base/stringprintf.h> #include <android-base/strings.h> #include <ziparchive/zip_archive.h> #include "common.h" #include "error_code.h" #include "install.h" #include "minui/minui.h" #include "minzip/SysUtil.h" #include "minzip/Zip.h" #include "otautil/SysUtil.h" #include "roots.h" #include "ui.h" #include "verifier.h" Loading Loading @@ -72,15 +72,17 @@ static int parse_build_number(const std::string& str) { } // Read the build.version.incremental of src/tgt from the metadata and log it to last_install. static void read_source_target_build(ZipArchive* zip, std::vector<std::string>& log_buffer) { const ZipEntry* meta_entry = mzFindZipEntry(zip, METADATA_PATH); if (meta_entry == nullptr) { static void read_source_target_build(ZipArchiveHandle zip, std::vector<std::string>& log_buffer) { ZipString metadata_path(METADATA_PATH); ZipEntry meta_entry; if (FindEntry(zip, metadata_path, &meta_entry) != 0) { LOG(ERROR) << "Failed to find " << METADATA_PATH << " in update package"; return; } std::string meta_data(meta_entry->uncompLen, '\0'); if (!mzReadZipEntry(zip, meta_entry, &meta_data[0], meta_entry->uncompLen)) { std::string meta_data(meta_entry.uncompressed_length, '\0'); if (ExtractToMemory(zip, &meta_entry, reinterpret_cast<uint8_t*>(&meta_data[0]), meta_entry.uncompressed_length) != 0) { LOG(ERROR) << "Failed to read metadata in update package"; return; } Loading Loading @@ -109,15 +111,14 @@ static void read_source_target_build(ZipArchive* zip, std::vector<std::string>& // If the package contains an update binary, extract it and run it. static int try_update_binary(const char* path, ZipArchive* zip, bool* wipe_cache, try_update_binary(const char* path, ZipArchiveHandle zip, bool* wipe_cache, std::vector<std::string>& log_buffer, int retry_count) { read_source_target_build(zip, log_buffer); const ZipEntry* binary_entry = mzFindZipEntry(zip, ASSUMED_UPDATE_BINARY_NAME); if (binary_entry == NULL) { mzCloseZipArchive(zip); ZipString binary_name(ASSUMED_UPDATE_BINARY_NAME); ZipEntry binary_entry; if (FindEntry(zip, binary_name, &binary_entry) != 0) { return INSTALL_CORRUPT; } Loading @@ -126,15 +127,14 @@ try_update_binary(const char* path, ZipArchive* zip, bool* wipe_cache, int fd = creat(binary, 0755); if (fd < 0) { PLOG(ERROR) << "Can't make " << binary; mzCloseZipArchive(zip); return INSTALL_ERROR; } bool ok = mzExtractZipEntryToFile(zip, binary_entry, fd); int error = ExtractEntryToFile(zip, &binary_entry, fd); close(fd); mzCloseZipArchive(zip); if (!ok) { LOG(ERROR) << "Can't copy " << ASSUMED_UPDATE_BINARY_NAME; if (error != 0) { LOG(ERROR) << "Can't copy " << ASSUMED_UPDATE_BINARY_NAME << " : " << ErrorCodeString(error); return INSTALL_ERROR; } Loading Loading @@ -326,13 +326,14 @@ really_install_package(const char *path, bool* wipe_cache, bool needs_mount, } // Try to open the package. ZipArchive zip; err = mzOpenZipArchive(map.addr, map.length, &zip); ZipArchiveHandle zip; err = OpenArchiveFromMemory(map.addr, map.length, path, &zip); if (err != 0) { LOG(ERROR) << "Can't open " << path; LOG(ERROR) << "Can't open " << path << " : " << ErrorCodeString(err); log_buffer.push_back(android::base::StringPrintf("error: %d", kZipOpenFailure)); sysReleaseMap(&map); CloseArchive(zip); return INSTALL_CORRUPT; } Loading @@ -342,12 +343,12 @@ really_install_package(const char *path, bool* wipe_cache, bool needs_mount, ui->Print("Retry attempt: %d\n", retry_count); } ui->SetEnableReboot(false); int result = try_update_binary(path, &zip, wipe_cache, log_buffer, retry_count); int result = try_update_binary(path, zip, wipe_cache, log_buffer, retry_count); ui->SetEnableReboot(true); ui->Print("\n"); sysReleaseMap(&map); CloseArchive(zip); return result; } Loading
minzip/Android.mkdeleted 100644 → 0 +0 −23 Original line number Diff line number Diff line LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_SRC_FILES := \ Hash.cpp \ SysUtil.cpp \ DirUtil.cpp \ Inlines.c \ Zip.cpp LOCAL_C_INCLUDES := \ external/zlib \ external/safe-iop/include LOCAL_STATIC_LIBRARIES := libselinux libbase LOCAL_MODULE := libminzip LOCAL_CLANG := true LOCAL_CFLAGS += -Werror -Wall include $(BUILD_STATIC_LIBRARY)
minzip/Bits.hdeleted 100644 → 0 +0 −357 Original line number Diff line number Diff line /* * Copyright 2006 The Android Open Source Project * * Some handy functions for manipulating bits and bytes. */ #ifndef _MINZIP_BITS #define _MINZIP_BITS #include "inline_magic.h" #include <stdlib.h> #include <string.h> /* * Get 1 byte. (Included to make the code more legible.) */ INLINE unsigned char get1(unsigned const char* pSrc) { return *pSrc; } /* * Get 2 big-endian bytes. */ INLINE unsigned short get2BE(unsigned char const* pSrc) { unsigned short result; result = *pSrc++ << 8; result |= *pSrc++; return result; } /* * Get 4 big-endian bytes. */ INLINE unsigned int get4BE(unsigned char const* pSrc) { unsigned int result; result = *pSrc++ << 24; result |= *pSrc++ << 16; result |= *pSrc++ << 8; result |= *pSrc++; return result; } /* * Get 8 big-endian bytes. */ INLINE unsigned long long get8BE(unsigned char const* pSrc) { unsigned long long result; result = (unsigned long long) *pSrc++ << 56; result |= (unsigned long long) *pSrc++ << 48; result |= (unsigned long long) *pSrc++ << 40; result |= (unsigned long long) *pSrc++ << 32; result |= (unsigned long long) *pSrc++ << 24; result |= (unsigned long long) *pSrc++ << 16; result |= (unsigned long long) *pSrc++ << 8; result |= (unsigned long long) *pSrc++; return result; } /* * Get 2 little-endian bytes. */ INLINE unsigned short get2LE(unsigned char const* pSrc) { unsigned short result; result = *pSrc++; result |= *pSrc++ << 8; return result; } /* * Get 4 little-endian bytes. */ INLINE unsigned int get4LE(unsigned char const* pSrc) { unsigned int result; result = *pSrc++; result |= *pSrc++ << 8; result |= *pSrc++ << 16; result |= *pSrc++ << 24; return result; } /* * Get 8 little-endian bytes. */ INLINE unsigned long long get8LE(unsigned char const* pSrc) { unsigned long long result; result = (unsigned long long) *pSrc++; result |= (unsigned long long) *pSrc++ << 8; result |= (unsigned long long) *pSrc++ << 16; result |= (unsigned long long) *pSrc++ << 24; result |= (unsigned long long) *pSrc++ << 32; result |= (unsigned long long) *pSrc++ << 40; result |= (unsigned long long) *pSrc++ << 48; result |= (unsigned long long) *pSrc++ << 56; return result; } /* * Grab 1 byte and advance the data pointer. */ INLINE unsigned char read1(unsigned const char** ppSrc) { return *(*ppSrc)++; } /* * Grab 2 big-endian bytes and advance the data pointer. */ INLINE unsigned short read2BE(unsigned char const** ppSrc) { unsigned short result; result = *(*ppSrc)++ << 8; result |= *(*ppSrc)++; return result; } /* * Grab 4 big-endian bytes and advance the data pointer. */ INLINE unsigned int read4BE(unsigned char const** ppSrc) { unsigned int result; result = *(*ppSrc)++ << 24; result |= *(*ppSrc)++ << 16; result |= *(*ppSrc)++ << 8; result |= *(*ppSrc)++; return result; } /* * Get 8 big-endian bytes. */ INLINE unsigned long long read8BE(unsigned char const** ppSrc) { unsigned long long result; result = (unsigned long long) *(*ppSrc)++ << 56; result |= (unsigned long long) *(*ppSrc)++ << 48; result |= (unsigned long long) *(*ppSrc)++ << 40; result |= (unsigned long long) *(*ppSrc)++ << 32; result |= (unsigned long long) *(*ppSrc)++ << 24; result |= (unsigned long long) *(*ppSrc)++ << 16; result |= (unsigned long long) *(*ppSrc)++ << 8; result |= (unsigned long long) *(*ppSrc)++; return result; } /* * Grab 2 little-endian bytes and advance the data pointer. */ INLINE unsigned short read2LE(unsigned char const** ppSrc) { unsigned short result; result = *(*ppSrc)++; result |= *(*ppSrc)++ << 8; return result; } /* * Grab 4 little-endian bytes and advance the data pointer. */ INLINE unsigned int read4LE(unsigned char const** ppSrc) { unsigned int result; result = *(*ppSrc)++; result |= *(*ppSrc)++ << 8; result |= *(*ppSrc)++ << 16; result |= *(*ppSrc)++ << 24; return result; } /* * Get 8 little-endian bytes. */ INLINE unsigned long long read8LE(unsigned char const** ppSrc) { unsigned long long result; result = (unsigned long long) *(*ppSrc)++; result |= (unsigned long long) *(*ppSrc)++ << 8; result |= (unsigned long long) *(*ppSrc)++ << 16; result |= (unsigned long long) *(*ppSrc)++ << 24; result |= (unsigned long long) *(*ppSrc)++ << 32; result |= (unsigned long long) *(*ppSrc)++ << 40; result |= (unsigned long long) *(*ppSrc)++ << 48; result |= (unsigned long long) *(*ppSrc)++ << 56; return result; } /* * Skip over a UTF-8 string. */ INLINE void skipUtf8String(unsigned char const** ppSrc) { unsigned int length = read4BE(ppSrc); (*ppSrc) += length; } /* * Read a UTF-8 string into a fixed-size buffer, and null-terminate it. * * Returns the length of the original string. */ INLINE int readUtf8String(unsigned char const** ppSrc, char* buf, size_t bufLen) { unsigned int length = read4BE(ppSrc); size_t copyLen = (length < bufLen) ? length : bufLen-1; memcpy(buf, *ppSrc, copyLen); buf[copyLen] = '\0'; (*ppSrc) += length; return length; } /* * Read a UTF-8 string into newly-allocated storage, and null-terminate it. * * Returns the string and its length. (The latter is probably unnecessary * for the way we're using UTF8.) */ INLINE char* readNewUtf8String(unsigned char const** ppSrc, size_t* pLength) { unsigned int length = read4BE(ppSrc); char* buf; buf = (char*) malloc(length+1); memcpy(buf, *ppSrc, length); buf[length] = '\0'; (*ppSrc) += length; *pLength = length; return buf; } /* * Set 1 byte. (Included to make the code more legible.) */ INLINE void set1(unsigned char* buf, unsigned char val) { *buf = (unsigned char)(val); } /* * Set 2 big-endian bytes. */ INLINE void set2BE(unsigned char* buf, unsigned short val) { *buf++ = (unsigned char)(val >> 8); *buf = (unsigned char)(val); } /* * Set 4 big-endian bytes. */ INLINE void set4BE(unsigned char* buf, unsigned int val) { *buf++ = (unsigned char)(val >> 24); *buf++ = (unsigned char)(val >> 16); *buf++ = (unsigned char)(val >> 8); *buf = (unsigned char)(val); } /* * Set 8 big-endian bytes. */ INLINE void set8BE(unsigned char* buf, unsigned long long val) { *buf++ = (unsigned char)(val >> 56); *buf++ = (unsigned char)(val >> 48); *buf++ = (unsigned char)(val >> 40); *buf++ = (unsigned char)(val >> 32); *buf++ = (unsigned char)(val >> 24); *buf++ = (unsigned char)(val >> 16); *buf++ = (unsigned char)(val >> 8); *buf = (unsigned char)(val); } /* * Set 2 little-endian bytes. */ INLINE void set2LE(unsigned char* buf, unsigned short val) { *buf++ = (unsigned char)(val); *buf = (unsigned char)(val >> 8); } /* * Set 4 little-endian bytes. */ INLINE void set4LE(unsigned char* buf, unsigned int val) { *buf++ = (unsigned char)(val); *buf++ = (unsigned char)(val >> 8); *buf++ = (unsigned char)(val >> 16); *buf = (unsigned char)(val >> 24); } /* * Set 8 little-endian bytes. */ INLINE void set8LE(unsigned char* buf, unsigned long long val) { *buf++ = (unsigned char)(val); *buf++ = (unsigned char)(val >> 8); *buf++ = (unsigned char)(val >> 16); *buf++ = (unsigned char)(val >> 24); *buf++ = (unsigned char)(val >> 32); *buf++ = (unsigned char)(val >> 40); *buf++ = (unsigned char)(val >> 48); *buf = (unsigned char)(val >> 56); } /* * Stuff a UTF-8 string into the buffer. */ INLINE void setUtf8String(unsigned char* buf, const unsigned char* str) { unsigned int strLen = strlen((const char*)str); set4BE(buf, strLen); memcpy(buf + sizeof(unsigned int), str, strLen); } #endif /*_MINZIP_BITS*/