ADD mk_delta /tota-upg/mk_delta/
ADD scripts /tota-upg/scripts/
ADD recovery /tota-upg/recovery/
-COPY --from=build /usr/local/bin/ss_bsdiff /usr/local/bin/ss_bspatch /usr/local/bin/
+COPY --from=build /usr/local/bin/ss_bsdiff /usr/local/bin/
COPY --from=build /usr/local/lib/liblzma-tool.so.* /usr/local/lib
RUN apt-get update && \
apt-get install -y --no-install-recommends libbrotli1 libdivsufsort3 python-is-python2 python2 python-apt python3-apt python3 python3-pip aria2 p7zip-full attr tar file sudo git && rm -rf /var/lib/apt/lists/*
PROJECT(ss_bsdiff C)
SET(ss_bsdiff_SRCS ss_bsdiff.c)
-SET(ss_bspatch_SRCS
- ss_bspatch_common.c
- ss_bspatch.c
-)
INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/bsdiff)
ADD_EXECUTABLE(ss_bsdiff ${ss_bsdiff_SRCS})
TARGET_LINK_LIBRARIES(ss_bsdiff ${${PROJECT_NAME}_pkgs_LDFLAGS} "-g" "-pthread")
INSTALL(TARGETS ss_bsdiff DESTINATION bin)
-
-ADD_EXECUTABLE(ss_bspatch ${ss_bspatch_SRCS})
-TARGET_LINK_LIBRARIES(ss_bspatch ${${PROJECT_NAME}_pkgs_LDFLAGS} "-g" "-pthread")
-INSTALL(TARGETS ss_bspatch DESTINATION bin)
+++ /dev/null
-/*
- * libtota
- *
- * Copyright (c) 2022 Samsung Electronics Co., Ltd.
- *
- * 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 <assert.h>
-#include <stdio.h>
-#include <stddef.h>
-#include <string.h>
-#include <errno.h>
-#include <brotli/decode.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <sys/mman.h>
-#include <unistd.h>
-#include "fota_log.h"
-
-#define PF_OK 0
-#define PF_ERROR_OPEN_FILE 1
-#define PF_ERROR_MMAP 2
-#define PF_ERROR_INVALID_PATCH_FILE 3
-#define PF_ERROR_DECOMPRESSION 4
-
-#define BUFF_IN_LEN 4096
-#define BUFF_OUT_LEN 4096
-#define SSINT_LEN 8
-#define BLOCK_COUNT_REPORT 5000
-
-const char SSDIFF_MAGIC[] = "SSDIFF40";
-
-struct bs_data {
- int src_fd, dest_fd, patch_fd;
- void *src_ptr, *dest_ptr, *patch_ptr;
- size_t src_len, dest_len, patch_len;
- unsigned char buff_in[BUFF_IN_LEN];
- unsigned char buff_out[BUFF_IN_LEN];
- uint8_t *dest_pos;
- uint8_t *src_pos;
- size_t available_in, available_out;
- const uint8_t *compressed_pos;
- uint8_t *decompressed_pos;
- size_t total_size;
- BrotliDecoderState *bstate;
-};
-
-static void free_data(struct bs_data *data)
-{
- if (data == NULL)
- return;
-
- if (data->src_ptr) munmap(data->src_ptr, data->src_len);
- if (data->dest_ptr) munmap(data->dest_ptr, data->dest_len);
- if (data->patch_ptr) munmap(data->patch_ptr, data->patch_len);
-
- if (data->src_fd) close(data->src_fd);
- if (data->patch_fd) close(data->patch_fd);
- if (data->dest_fd) close(data->dest_fd);
-}
-
-static int open_file(char *file_name, int mode)
-{
- assert(file_name);
- int fd = open(file_name, mode, S_IWUSR | S_IRUSR);
- if (fd < 0)
- LOGE("Open file %s error: %m (%d)\n", file_name, errno);
- return fd;
-}
-
-static size_t get_file_len(int fd)
-{
- assert(fd >= 0);
- size_t result = lseek(fd, 0, SEEK_END);
- lseek(fd, 0, SEEK_SET);
- return result;
-}
-
-
-static size_t decompress_bytes(struct bs_data *data, size_t keep_offset)
-{
- assert(data);
- if (keep_offset > 0) {
- memcpy(data->buff_out, data->buff_out + sizeof(data->buff_out) - keep_offset, keep_offset);
- }
- data->decompressed_pos = data->buff_out + keep_offset;
- data->available_out = sizeof(data->buff_out) - keep_offset;
-
- BrotliDecoderResult result = BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT;
-
- result = BrotliDecoderDecompressStream(data->bstate,
- &data->available_in,
- &data->compressed_pos,
- &data->available_out,
- &data->decompressed_pos,
- &data->total_size);
-
- if (result == BROTLI_DECODER_RESULT_ERROR) {
- LOGE("Decoder error\n");
- return PF_ERROR_DECOMPRESSION;
- } else if (result == BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT) {
- LOGE("Invalid source file\n");
- return PF_ERROR_DECOMPRESSION;
- }
-
- return PF_OK;
-}
-
-static int open_files(struct bs_data *data, char *source_file, size_t source_size, char *dest_file, size_t dest_size, char *patch_file)
-{
- assert(data);
- assert(source_file);
- assert(dest_file);
- assert(patch_file);
-
- data->src_fd = open_file(source_file, O_RDONLY);
- data->patch_fd = open_file(patch_file, O_RDONLY);
- data->dest_fd = open_file(dest_file, O_RDWR);
- if (data->src_fd < 0 ||
- data->patch_fd < 0 ||
- data->dest_fd < 0)
- return PF_ERROR_OPEN_FILE;
-
- data->src_len = source_size;
- data->patch_len = get_file_len(data->patch_fd);
- data->dest_len = dest_size;
-
- data->src_ptr = mmap(NULL, data->src_len, PROT_READ, MAP_PRIVATE, data->src_fd, 0);
- if (data->src_ptr == MAP_FAILED) {
- LOGE("mmap source file error: %m (%d)", errno);
- return PF_ERROR_MMAP;
- }
-
- data->patch_ptr = mmap(NULL, data->patch_len, PROT_READ, MAP_PRIVATE, data->patch_fd, 0);
- if (data->patch_ptr == MAP_FAILED) {
- LOGE("mmap patch file error: %m (%d)", errno);
- return PF_ERROR_MMAP;
- }
-
- data->dest_ptr = mmap(NULL, data->dest_len, PROT_WRITE, MAP_SHARED, data->dest_fd, 0);
- if (data->dest_ptr == MAP_FAILED) {
- LOGE("mmap destination error: %m (%d)\n", errno);
- return PF_ERROR_MMAP;
- }
-
- data->compressed_pos = data->patch_ptr;
- data->available_in = data->patch_len;
-
- return PF_OK;
-}
-
-static void init_data(struct bs_data *data)
-{
- assert(data);
-
- data->src_fd = -1;
- data->patch_fd = -1;
- data->dest_fd = -1;
- data->src_ptr = NULL;
- data->dest_ptr = NULL;
- data->patch_ptr = NULL;
- data->src_len = 0;
- data->dest_len = 0;
- data->patch_len = 0;
- data->available_in = 0;
- data->compressed_pos = 0;
- data->available_out = 0;
- data->decompressed_pos = 0;
- data->bstate = BrotliDecoderCreateInstance(NULL, NULL, NULL);
-}
-
-static int64_t parse_ssint(unsigned char *buff)
-{
- assert(buff);
- /*
- * From bsdiff 4.0 documentation:
- *
- * INTEGER type:
- *
- * offset size data type value
- * 0 1 byte x0
- * 1 1 byte x1
- * 2 1 byte x2
- * 3 1 byte x3
- * 4 1 byte x4
- * 5 1 byte x5
- * 6 1 byte x6
- * 7 1 byte x7 + 128 * s
- *
- * The values x0, x2, x2, x3, x4, x5, x6 are between 0 and 255 (inclusive).
- * The value x7 is between 0 and 127 (inclusive). The value s is 0 or 1.
- *
- * The INTEGER is parsed as:
- * (x0 + x1 * 256 + x2 * 256^2 + x3 * 256^3 + x4 * 256^4 +
- * x5 * 256^5 + x6 * 256^6 + x7 * 256^7) * (-1)^s
- *
- * (In other words, an INTEGER is a 64-byte signed integer in sign-magnitude
- * format, stored in little-endian byte order.)
- */
- int64_t result = *(int64_t*)buff & 0x7fffffff;
- if ((buff[7] & 0x80) != 0)
- result = -result;
-
- return result;
-}
-
-int read_header(struct bs_data *data, uint8_t **buff_out_pos)
-{
- assert(data);
- assert(buff_out_pos);
-
- *buff_out_pos = data->buff_out;
-
- if (*buff_out_pos + sizeof(SSDIFF_MAGIC) > data->decompressed_pos ||
- memcmp(data->buff_out, SSDIFF_MAGIC, sizeof(SSDIFF_MAGIC) - 1) != 0) {
- LOGE("Invalid patch file\n");
- return PF_ERROR_INVALID_PATCH_FILE;
- } else {
- LOGL(LOG_SSENGINE, "Looks like SSDIFF\n");
- }
-
- *buff_out_pos += sizeof(SSDIFF_MAGIC) - 1;
-
- if (*buff_out_pos + SSINT_LEN > data->decompressed_pos) {
- decompress_bytes(data, data->decompressed_pos - *buff_out_pos);
- *buff_out_pos = data->buff_out;
- }
-
- size_t target_size = parse_ssint(*buff_out_pos);
- LOGL(LOG_SSENGINE, "target_size: 0x%lx (%ld)\n", target_size, target_size);
-
- if (target_size != data->dest_len) {
- LOGE("Declared target size differs from that read from the patch\n");
- return PF_ERROR_INVALID_PATCH_FILE;
- }
-
- *buff_out_pos += SSINT_LEN;
-
- return PF_OK;
-}
-
-int apply_patch_brotli(char *source_file, size_t source_size, char *dest_file, size_t dest_size, char *patch_file)
-{
- assert(source_file);
- assert(dest_file);
- assert(patch_file);
-
- int result;
- uint64_t blocks = 0;
- struct bs_data data;
-
- init_data(&data);
-
- if ((result = open_files(&data, source_file, source_size, dest_file, dest_size, patch_file)) != PF_OK)
- goto exit;
-
- if ((result = decompress_bytes(&data, 0)) != PF_OK)
- goto exit;
-
- uint8_t *buff_out_pos;
-
- if ((result = read_header(&data, &buff_out_pos)) != PF_OK)
- goto exit;
-
- uint64_t total_write = 0;
-
- while (total_write < data.dest_len) {
- /*
- * Make sure we can read the block header
- */
- if (buff_out_pos + 4*8 > data.decompressed_pos) {
- if ((result = decompress_bytes(&data, data.decompressed_pos - buff_out_pos)) != PF_OK)
- goto exit;
- buff_out_pos = data.buff_out;
- }
-
- /*
- * Read the block header
- */
- int64_t diff_len = parse_ssint(buff_out_pos+0*8);
- int64_t extra_len = parse_ssint(buff_out_pos+1*8);
- int64_t old_pos = parse_ssint(buff_out_pos+2*8);
- int64_t new_pos = parse_ssint(buff_out_pos+3*8);
- buff_out_pos += 4*8;
-
- /*
- * Prepare pointers
- */
- data.dest_pos = data.dest_ptr + new_pos;
- data.src_pos = data.src_ptr + old_pos;
- /*
- * Read diff data
- */
- int64_t write = 0;
- while (write < diff_len) {
- if (buff_out_pos >= data.decompressed_pos) {
- if ((result = decompress_bytes(&data, 0)) != PF_OK)
- goto exit;
- buff_out_pos = data.buff_out;
- }
- while (write < diff_len && buff_out_pos < data.decompressed_pos) {
- *data.dest_pos = *(uint8_t*)(data.src_pos) + *(uint8_t*)buff_out_pos;
- data.dest_pos++;
- data.src_pos++;
- buff_out_pos++;
- write++;
- }
- }
- total_write += write;
- /*
- * Read extra data
- */
- write = 0;
- while (write < extra_len) {
- if (buff_out_pos >= data.decompressed_pos) {
- if ((result = decompress_bytes(&data, 0)) != PF_OK)
- goto exit;
- buff_out_pos = data.buff_out;
- }
- int64_t chunk_size = extra_len - write;
- if (buff_out_pos + chunk_size > data.decompressed_pos) {
- chunk_size = data.decompressed_pos - buff_out_pos;
- }
- memcpy(data.dest_pos, buff_out_pos, chunk_size);
- data.dest_pos += chunk_size;
- buff_out_pos += chunk_size;
- write += chunk_size;
- }
- total_write += write;
-
- blocks++;
- if (blocks % BLOCK_COUNT_REPORT == 0) {
- printf("Number of processed patch blocks: %lld\n", blocks);
- }
- }
-
- result = PF_OK;
-
-exit:
- printf("Total processed blocks: %lld\n", blocks);
- free_data(&data);
- return result;
-}
+++ /dev/null
-/*
- * libtota
- *
- * Copyright (c) 2022 Samsung Electronics Co., Ltd.
- *
- * 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.
- */
-#pragma once
-
-#include <unistd.h>
-
-extern int apply_patch_brotli(char *source_file, size_t source_size, char *dest_file, size_t dest_size, char *patch_file);
+++ /dev/null
-/*-
- * Copyright 2003-2005 Colin Percival
- * Copyright 2012 Matthew Endsley
- * 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.
- *
- * Modifications are made in reimplementing suffix sort array generation
- * and how the data is read and written to.Iterative part replaced the
- * recursive implementation to avoid buffer overflow problems
- */
-#define _CRT_SECURE_NO_WARNINGS
-#include <err.h>
-#include <unistd.h>
-#include <fcntl.h>
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include <Alloc.h>
-#include <7zFile.h>
-#include <7zVersion.h>
-#include <LzmaDec.h>
-#include <LzmaEnc.h>
-
-#include "ss_bspatch_common.h"
-
-const char *kCantReadMessage = "Can not read input file";
-const char *kCantWriteMessage = "Can not write output file";
-const char *kCantAllocateMessage = "Can not allocate memory";
-const char *kDataErrorMessage = "Data error";
-
-int PrintError(char *buffer, const char *message, int buf_size)
-{
- snprintf(buffer + strlen(buffer), buf_size - strlen(buffer),
- "\nError: %s\n", message);
- return 1;
-}
-
-int PrintErrorNumber(char *buffer, SRes val, int buf_size)
-{
- snprintf(buffer + strlen(buffer), buf_size - strlen(buffer),
- "\nError code: %x\n", (unsigned)val);
- return 1;
-}
-
-int PrintUserError(char *buffer, int buf_size)
-{
- return PrintError(buffer, "Incorrect command", buf_size);
-}
-
-int main2(int numArgs, const char *args[], char *rs, int rs_size)
-{
- CFileSeqInStream inStream;
- CFileOutStream outStream;
- int res, fd;
- int encodeMode;
- unsigned char *buf_res = NULL;
- unsigned char *new_data;
- ssize_t new_size;
-
- FileSeqInStream_CreateVTable(&inStream);
- File_Construct(&inStream.file);
-
- FileOutStream_CreateVTable(&outStream);
- //File_Construct(&outStream.file);
-
- encodeMode = 0;
-
- size_t t4 = sizeof(UInt32);
- size_t t8 = sizeof(UInt64);
- if (t4 != 4 || t8 != 8)
- return PrintError(rs, "Incorrect UInt32 or UInt64", rs_size);
-
- if (InFile_Open(&inStream.file, args[3]) != 0)
- return PrintError(rs, "Can not open input file", rs_size);
-
- if (encodeMode) {
- UInt64 fileSize;
- File_GetLength(&inStream.file, &fileSize);
- //res = Encode(&outStream.s, &inStream.s, fileSize, rs);
- } else {
- UInt64 unpackSize, i;
- CLzmaDec state;
- unsigned char header[LZMA_PROPS_SIZE + 8];
- RINOK(SeqInStream_Read(&inStream.s, header, sizeof(header)));
- unpackSize = 0;
- for (i = 0; i < 8; i++)
- unpackSize += (UInt64)header[LZMA_PROPS_SIZE + i] << (i * 8);
- buf_res = (unsigned char *)malloc(unpackSize);
- if (!buf_res)
- return PrintError(rs, "Failed to allocate memory", rs_size);
- memset(buf_res, 0x0, unpackSize);
- LzmaDec_Construct(&state);
- RINOK(LzmaDec_Allocate(&state, header, LZMA_PROPS_SIZE, &g_Alloc));
- res = Decode2(&state, &outStream.s, &inStream.s, &unpackSize, buf_res);
- LzmaDec_Free(&state, &g_Alloc);
- File_Close(&inStream.file);
- if (apply_patch(args[1], buf_res, &new_data, &new_size) != 0) {
- if (new_data)
- free(new_data);
- return 1;
- }
- if (((fd = open(args[2], O_CREAT | O_TRUNC | O_WRONLY, 0666)) < 0) ||
- (write(fd, new_data, new_size) != new_size) || (close(fd) == -1))
- err(1, "%s", args[2]);
- if (res != SZ_OK) {
- free(new_data);
- free(buf_res);
- if (res == SZ_ERROR_MEM)
- return PrintError(rs, kCantAllocateMessage, rs_size);
- else if (res == SZ_ERROR_DATA)
- return PrintError(rs, kDataErrorMessage, rs_size);
- else if (res == SZ_ERROR_WRITE)
- return PrintError(rs, kCantWriteMessage, rs_size);
- else if (res == SZ_ERROR_READ)
- return PrintError(rs, kCantReadMessage, rs_size);
- return PrintErrorNumber(rs, res, rs_size);
- }
- free(new_data);
- free(buf_res);
- return 0;
- }
- return 1;
-}
-
-int main(int numArgs, const char *args[])
-{
- char rs[800] = { 0 };
- if (numArgs != 4)
- errx(1, "ss_bspatch Version 1.0\nUsage: ss_bspatch oldfile newfile patchfile\n");
- int res = main2(numArgs, args, rs, sizeof(rs));
- fputs(rs, stdout);
- return res;
-}
+++ /dev/null
-/*-
- * Copyright 2003-2005 Colin Percival
- * Copyright 2012 Matthew Endsley
- * 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.
- *
- * Modifications are made in reimplementing suffix sort array generation
- * and how the data is read and written to.Iterative part replaced the
- * recursive implementation to avoid buffer overflow problems
- */
-//#define ZLIB_MOD //not stable yet.
-//#define MAX_MATCH_SIZE // define ( MAX_MATCH_SIZE or CONST_MEMORY_USAGE ) or ( none of them )
-#define CONST_MEMORY_USAGE (64*1024) //tests show smallest time when using 64 kb
-#define PATCH_FILE_FORMAT_MOD
-#define BSDIFF_HEADER "BSDIFF40"
-#define SSDIFF_HEADER "SSDIFF40"
-//#define MULTI_THREADING
-#include <stdbool.h>
-#include <unistd.h>
-#include <fcntl.h>
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include <Alloc.h>
-#include <7zFile.h>
-#include <7zVersion.h>
-#include <LzmaDec.h>
-#include <LzmaEnc.h>
-
-static void *SzAlloc(void *p, size_t size)
-{
- p = p;
- return MyAlloc(size);
-}
-
-static void SzFree(void *p, void *address)
-{
- p = p;
- MyFree(address);
-}
-ISzAlloc g_Alloc = { SzAlloc, SzFree };
-
-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;
-}
-
-#define IN_BUF_SIZE (1 << 16)
-#define OUT_BUF_SIZE (1 << 16)
-
-SRes Decode2(CLzmaDec *state, ISeqOutStream *outStream, ISeqInStream *inStream,
- UInt64 *unpackSize, unsigned char *dec_data)
-{
- int thereIsSize = (*unpackSize != (UInt64)(Int64) - 1);
- UInt64 offset = 0;
- Byte inBuf[IN_BUF_SIZE];
- Byte outBuf[OUT_BUF_SIZE];
- size_t inPos = 0, inSize = 0, outPos = 0;
-
- LzmaDec_Init(state);
-
- offset = 0;
-
- for (;;) {
- if (inPos == inSize) {
- inSize = IN_BUF_SIZE;
- RINOK(inStream->Read(inStream, inBuf, &inSize));
- inPos = 0;
- }
-
- SRes res;
- SizeT inProcessed = inSize - inPos;
- SizeT outProcessed = OUT_BUF_SIZE - outPos;
- ELzmaFinishMode finishMode = LZMA_FINISH_ANY;
- ELzmaStatus status;
-
- if (thereIsSize && outProcessed > *unpackSize) {
- outProcessed = (SizeT) * unpackSize;
- finishMode = LZMA_FINISH_END;
- }
-
- res = LzmaDec_DecodeToBuf(state, outBuf + outPos, &outProcessed,
- inBuf + inPos, &inProcessed, finishMode, &status);
- inPos += inProcessed;
- outPos += outProcessed;
- *unpackSize -= outProcessed;
- memcpy(dec_data + offset, outBuf, outProcessed);
- offset += outProcessed;
-
- outPos = 0;
-
- if ((res != SZ_OK) || (thereIsSize && *unpackSize == 0))
- return res;
-
- if (inProcessed == 0 && outProcessed == 0) {
- if (thereIsSize || status != LZMA_STATUS_FINISHED_WITH_MARK)
- return SZ_ERROR_DATA;
- return res;
- }
- }
-}
-
-int apply_patch(const char *oldfile, unsigned char *patch_buffer, unsigned char **dest_buf, ssize_t *dest_size)
-{
- int fd = -1, result = 0;
- off_t oldsize, newsize;
- u_char header[16], buf[8];
- u_char *old = NULL;
- off_t oldpos, newpos;
- off_t ctrl[4]; /////////////////////////////////////THREAD
- off_t total_write; /////////////////////////////////////////THREAD
- off_t j;
- off_t memory_usage = CONST_MEMORY_USAGE;
- off_t match_size;
- off_t patch_buffer_offset = 0;
- bool flag;
-
- /*
- File 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".
- */
- // Read header
- if (patch_buffer)
- memcpy(header, patch_buffer, 16);
- else {
- printf("%s().%d Corrupt decoded patch buffer\n", __FUNCTION__, __LINE__);
- return 1;
- }
-
- /* Check for appropriate magic */
- if (memcmp(header, BSDIFF_HEADER, 8) != 0 && memcmp(header, SSDIFF_HEADER, 8) != 0) {
- printf("%s().%d Patch buffer header corrupt\n", __FUNCTION__, __LINE__);
- return 1;
- }
-
- /* Read lengths from header */
- newsize = offtin(header + 8);
-
- if ((newsize < 0)) {
- printf("%s().%d Patch buffer corrupt\n", __FUNCTION__, __LINE__);
- return 1;
- }
-
- /* Cset patch_buffer_offset at the right place */
- patch_buffer_offset += 16;
-
- if (((fd = open(oldfile, O_RDONLY, 0)) < 0) ||
- ((oldsize = lseek(fd, 0, SEEK_END)) == -1) ||
- ((old = malloc(memory_usage + 1)) == NULL) ||
- (lseek(fd, 0, SEEK_SET) != 0)) {
- printf("Corruption in old file %s\n", oldfile);
- result = 1;
- goto Cleanup;
- }
-
- if ((*dest_buf = malloc(newsize + 1)) == NULL) {
- printf("Corruption in old file %s\n", oldfile);
- result = 1;
- goto Cleanup;
- }
- oldpos = 0;
- newpos = 0;
-
- total_write = 0;
-
- while (total_write != newsize) {
- /* Read control data */
- for (j = 0; j <= 3; j++) {
- memcpy(buf, patch_buffer + patch_buffer_offset, 8);
- patch_buffer_offset += 8;
- ctrl[j] = offtin(buf);
- };
-
- total_write += (ctrl[0] + ctrl[1]);
- newpos = ctrl[3];
- oldpos = ctrl[2];
-
- //////////////////////////////////////////////////////////////////////////////////
- flag = true;
- match_size = ctrl[0];
- while (flag == true) {
- if (match_size <= memory_usage) {
- if (pread(fd, old, match_size, oldpos) != match_size) {
- printf("Corruption in old file %s\n", oldfile);
- result = 1;
- goto Cleanup;
- }
- if (newpos + match_size > newsize) {
- printf("%s().%d Corrupt patch\n", __FUNCTION__, __LINE__);
- result = 1;
- goto Cleanup;
- }
- memcpy((*dest_buf) + newpos, patch_buffer + patch_buffer_offset, match_size);
- patch_buffer_offset += match_size;
- for (j = 0; j < match_size; j++)
- (*dest_buf)[newpos + j] += old[j];
- newpos += match_size;
- flag = false;
- } else {
- if (pread(fd, old, memory_usage, oldpos) != memory_usage) {
- printf("%s().%d Corruption in old file %s\n", __FUNCTION__, __LINE__ , oldfile);
- result = 1;
- goto Cleanup;
- }
- if (newpos + memory_usage > newsize) {
- printf("%s().%d Corrupt patch\n", __FUNCTION__, __LINE__);
- result = 1;
- goto Cleanup;
- }
- memcpy((*dest_buf) + newpos, patch_buffer + patch_buffer_offset, memory_usage);
- patch_buffer_offset += memory_usage;
- for (j = 0; j < memory_usage; j++)
- (*dest_buf)[newpos + j] += old[j];
- match_size -= memory_usage;
- oldpos += memory_usage;
- newpos += memory_usage;
- }
- }
-
- ////////////////////////////////////////////////////////////////////////////////////////
- /* Sanity-check */
- if (newpos + ctrl[1] > newsize) {
- printf("%s().%d Corrupt patch\n", __FUNCTION__, __LINE__);
- result = 1;
- goto Cleanup;
- }
- /* Read extra string */
- memcpy((*dest_buf) + newpos, patch_buffer + patch_buffer_offset, ctrl[1]);
- patch_buffer_offset += ctrl[1];
- };
- *dest_size = newsize;
-Cleanup:
- //close old file
- if (fd >= 0)
- close(fd);
- if (old)
- free(old);
- return result;
-}
+++ /dev/null
-#ifndef _SS_BSPATCH_COMMON_H
-#define _SS_BSPATCH_COMMON_H 1
-
-#include <Alloc.h>
-#include <7zFile.h>
-#include <7zVersion.h>
-#include <LzmaDec.h>
-#include <LzmaEnc.h>
-
-extern ISzAlloc g_Alloc;
-
-SRes Decode2(CLzmaDec *state, ISeqOutStream *outStream, ISeqInStream *inStream,
- UInt64 *unpackSize, unsigned char *dec_data);
-
-int apply_patch(const char *oldfile, unsigned char *patch_buffer, unsigned char **dest_buf, ssize_t *dest_size);
-
-#endif /* _SS_BSPATCH_COMMON_H */
COMMON_BIN_PATH = "../../common/bin/"
DIFF_UTIL = "/usr/local/bin/ss_bsdiff"
-DIFFPATCH_UTIL = "/usr/local/bin/ss_bspatch"
ZIPUTIL = "7z -mf=off a "
NEW_FILES_PATH = "run/upgrade-sysroot"
NEW_FILES_ZIP_NAME = "system.7z"
def main():
global DIFF_UTIL
- global DIFFPATCH_UTIL
logging.basicConfig(filename=LOGFILE, level=logging.DEBUG)
if not (os.path.isfile(DIFF_UTIL) and os.access(DIFF_UTIL, os.X_OK)):
DIFF_UTIL = os.path.join(COMMON_BIN_PATH, DIFF_UTIL)
- DIFFPATCH_UTIL = os.path.join(COMMON_BIN_PATH, DIFFPATCH_UTIL)
if not (os.path.isfile(DIFF_UTIL) and os.access(DIFF_UTIL, os.X_OK)):
print("Diff Util Does NOT exist -- ABORT", file=sys.stderr)
logging.info('Diff Util Does NOT exist -- ABORT')
if __name__ == '__main__':
- main()
\ No newline at end of file
+ main()