From f281b4233dd4848eb37d9c0da89bfc8417e5cb80 Mon Sep 17 00:00:00 2001 From: josch Date: Tue, 4 Nov 2014 11:02:33 +0100 Subject: [PATCH] add diff and patch functions for xdelta3 --- CMakeLists.txt | 2 +- utils/xdelta3.c | 115 ++++++++++++++++++++++++++++++++++++++++++++++++ utils/xdelta3.h | 27 ++++++++++++ 3 files changed, 143 insertions(+), 1 deletion(-) create mode 100644 utils/xdelta3.c create mode 100644 utils/xdelta3.h diff --git a/CMakeLists.txt b/CMakeLists.txt index d54cd64..adca72c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,7 +12,7 @@ set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--no-as-needed") add_library(mfapi SHARED mfapi/mfconn.c mfapi/file.c mfapi/folder.c mfapi/apicalls/file_get_info.c mfapi/apicalls/user_get_info.c mfapi/apicalls/file_get_links.c mfapi/apicalls/user_session.c mfapi/apicalls/folder_get_info.c mfapi/apicalls/folder_create.c mfapi/apicalls/folder_get_content.c mfapi/apicalls/folder_delete.c mfapi/apicalls/device_get_status.c mfapi/apicalls/device_get_changes.c) -add_library(mfutils SHARED utils/http.c utils/json.c utils/strings.c utils/stringv.c) +add_library(mfutils SHARED utils/http.c utils/json.c utils/strings.c utils/stringv.c utils/xdelta3.c) add_executable(mediafire-shell mfshell/main.c mfshell/mfshell.c mfshell/commands/folder.c mfshell/commands/auth.c mfshell/commands/chdir.c mfshell/commands/debug.c mfshell/commands/file.c mfshell/commands/get.c mfshell/commands/help.c mfshell/commands/host.c mfshell/commands/lcd.c mfshell/commands/links.c mfshell/commands/list.c mfshell/commands/lpwd.c mfshell/commands/mkdir.c mfshell/commands/pwd.c mfshell/commands/whoami.c mfshell/commands/rmdir.c mfshell/commands/status.c mfshell/commands/changes.c mfshell/config.c mfshell/options.c) target_link_libraries(mediafire-shell curl ssl crypto jansson mfapi mfutils) diff --git a/utils/xdelta3.c b/utils/xdelta3.c new file mode 100644 index 0000000..6fda254 --- /dev/null +++ b/utils/xdelta3.c @@ -0,0 +1,115 @@ +/* + * below file was taken from xdelta3 sources: + * xdelta3-3.0.8-dfsg/examples/encode_decode_test.c + * and modified a bit. + * + * xdelta3 is copyright 2007 Ralf Junker and released under the terms of the + * GPL2+ + */ + +// +// Permission to distribute this example by +// Copyright (C) 2007 Ralf Junker +// Ralf Junker +// http://www.yunqa.de/delphi/ + +//--------------------------------------------------------------------------- + +#define _POSIX_SOURCE +#include +#include +#include +#include +#include +#undef _POSIX_SOURCE +#include "../3rdparty/xdelta3-3.0.8/xdelta3.h" +#include "../3rdparty/xdelta3-3.0.8/xdelta3.c" + +//--------------------------------------------------------------------------- +static int code(int encode, FILE * InFile, FILE * SrcFile, FILE * OutFile, + unsigned int BufSize) +{ + int r, + ret; + struct stat statbuf; + + xd3_stream stream; + xd3_config config; + xd3_source source; + void *Input_Buf; + unsigned int Input_Buf_Read; + + if (BufSize < XD3_ALLOCSIZE) + BufSize = XD3_ALLOCSIZE; + memset(&stream, 0, sizeof(stream)); + memset(&source, 0, sizeof(source)); + xd3_init_config(&config, XD3_ADLER32); + config.winsize = BufSize; + xd3_config_stream(&stream, &config); + r = fstat(fileno(SrcFile), &statbuf); + if (r) + return r; + source.blksize = BufSize; + source.curblk = malloc(source.blksize); + + /* Load 1st block of stream. */ + r = fseek(SrcFile, 0, SEEK_SET); + if (r) + return r; + source.onblk = fread((void *)source.curblk, 1, source.blksize, SrcFile); + source.curblkno = 0; + + /* Set the stream. */ + xd3_set_source(&stream, &source); + Input_Buf = malloc(BufSize); + fseek(InFile, 0, SEEK_SET); + + do { + Input_Buf_Read = fread(Input_Buf, 1, BufSize, InFile); + if (Input_Buf_Read < BufSize) { + xd3_set_flags(&stream, XD3_FLUSH | stream.flags); + } + xd3_avail_input(&stream, Input_Buf, Input_Buf_Read); + for (;;) { + if (encode) + ret = xd3_encode_input(&stream); + else + ret = xd3_decode_input(&stream); + if (ret == XD3_INPUT) { + break; + } else if (ret == XD3_OUTPUT) { + r = fwrite(stream.next_out, 1, stream.avail_out, OutFile); + if (r != (int)stream.avail_out) + return r; + xd3_consume_output(&stream); + } else if (ret == XD3_GETSRCBLK) { + r = fseek(SrcFile, source.blksize * source.getblkno, SEEK_SET); + if (r) + return r; + source.onblk = + fread((void *)source.curblk, 1, source.blksize, SrcFile); + source.curblkno = source.getblkno; + } else if (ret == XD3_GOTHEADER || ret == XD3_WINSTART + || ret == XD3_WINFINISH) { + } else { + fprintf(stderr, "!!! INVALID %s %d !!!\n", stream.msg, ret); + return ret; + } + } + } while (Input_Buf_Read == BufSize); + free(Input_Buf); + free((void *)source.curblk); + xd3_close_stream(&stream); + xd3_free_stream(&stream); + return 0; +} + +int xdelta3_diff(FILE *old, FILE *new, FILE *diff) +{ + return code(1, new, old, diff, 0x1000); +} + +int xdelta3_patch(FILE *old, FILE *diff, FILE *new) +{ + return code(0, diff, old, new, 0x1000); +} diff --git a/utils/xdelta3.h b/utils/xdelta3.h new file mode 100644 index 0000000..03b6645 --- /dev/null +++ b/utils/xdelta3.h @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2014 Johannes Schauer + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2, as published by + * the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#ifndef _MFSHELL_XDELTA3_H_ +#define _MFSHELL_XDELTA3_H_ + +#include + +int xdelta3_diff(FILE *old, FILE *new, FILE *diff); +int xdelta3_patch(FILE *old, FILE *diff, FILE *new); + +#endif