summaryrefslogtreecommitdiff
path: root/xdelta3.h
diff options
context:
space:
mode:
Diffstat (limited to 'xdelta3.h')
-rw-r--r--xdelta3.h269
1 files changed, 90 insertions, 179 deletions
diff --git a/xdelta3.h b/xdelta3.h
index 0c1ae0d..d6471fa 100644
--- a/xdelta3.h
+++ b/xdelta3.h
@@ -1,5 +1,6 @@
/* xdelta 3 - delta compression tools and library
- * Copyright (C) Joshua P. MacDonald
+ * Copyright (C) 2001, 2003, 2004, 2005, 2006, 2007,
+ * 2008, 2009, 2010, 2011, 2012, 2013. Joshua P. MacDonald
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -16,7 +17,7 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
-/* To learn more about Xdelta, start by reading xdelta3.c. If you are
+/* To know more about Xdelta, start by reading xdelta3.c. If you are
* ready to use the API, continue reading here. There are two
* interfaces -- xd3_encode_input and xd3_decode_input -- plus a dozen
* or so related calls. This interface is styled after Zlib. */
@@ -24,7 +25,7 @@
#ifndef _XDELTA3_H_
#define _XDELTA3_H_
-#define _POSIX_SOURCE 200112L
+#define _POSIX_SOURCE
#define _ISOC99_SOURCE
#define _C99_SOURCE
@@ -70,7 +71,7 @@
*
* 8-16MB is reasonable, probably don't need to go larger. */
#ifndef XD3_HARDMAXWINSIZE
-#define XD3_HARDMAXWINSIZE (1U<<26)
+#define XD3_HARDMAXWINSIZE (1U<<24)
#endif
/* The IOPT_SIZE value sets the size of a buffer used to batch
* overlapping copy instructions before they are optimized by picking
@@ -86,7 +87,8 @@
#define XD3_DEFAULT_SPREVSZ (1U<<18)
#endif
-/* The default compression level */
+/* The default compression level
+ */
#ifndef XD3_DEFAULT_LEVEL
#define XD3_DEFAULT_LEVEL 3
#endif
@@ -99,12 +101,6 @@
#define XD3_USE_LARGEFILE64 1
#endif
-/* The source window size is limited to 2GB unless
- * XD3_USE_LARGESIZET is defined to 1. */
-#ifndef XD3_USE_LARGESIZET
-#define XD3_USE_LARGESIZET 1
-#endif
-
/* Sizes and addresses within VCDIFF windows are represented as usize_t
*
* For source-file offsets and total file sizes, total input and
@@ -113,141 +109,68 @@
* the 32bit boundary [xdelta3-test.h]).
*/
#ifndef _WIN32
-#define __STDC_FORMAT_MACROS
-#include <inttypes.h>
#include <stdint.h>
-#else /* WIN32 case */
+#else
#define WIN32_LEAN_AND_MEAN
-
-#ifndef WINVER
#if XD3_USE_LARGEFILE64
-/* 64 bit file offsets: uses GetFileSizeEx and SetFilePointerEx. */
+/* 64 bit file offsets: uses GetFileSizeEx and SetFilePointerEx.
+ * requires Win2000 or newer version of WinNT */
#define WINVER 0x0500
#define _WIN32_WINNT 0x0500
-#else /* xoff_t is 32bit */
-/* 32 bit file offsets: uses GetFileSize and SetFilePointer. */
+#else
+/* 32 bit (DWORD) file offsets: uses GetFileSize and
+ * SetFilePointer. compatible with win9x-me and WinNT4 */
#define WINVER 0x0400
#define _WIN32_WINNT 0x0400
-#endif /* if XD3_USE_LARGEFILE64 */
-#endif /* ifndef WINVER */
-
+#endif
#include <windows.h>
-
-/* _MSV_VER is defined by Microsoft tools, not by Mingw32 */
#ifdef _MSC_VER
+#define inline
typedef signed int ssize_t;
#if _MSC_VER < 1600
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned long uint32_t;
typedef ULONGLONG uint64_t;
-#else /* _MSC_VER >= 1600 */
+#else
/* For MSVC10 and above */
#include <stdint.h>
-#endif /* _MSC_VER < 1600 */
-#else /* _MSC_VER not defined */
-/* Mingw32 */
+#endif
+#else
+/* mingw32, lcc and watcom provide a proper header */
#include <stdint.h>
-#endif /* _MSC_VER defined */
+#endif
+#endif
-#endif /* _WIN32 defined */
+typedef uint32_t usize_t;
-/* Settings based on the size of xoff_t (32 vs 64 file offsets) */
#if XD3_USE_LARGEFILE64
-/* xoff_t is a 64-bit type */
-#define __USE_FILE_OFFSET64 1 /* GLIBC: for 64bit fileops. */
-
+#define __USE_FILE_OFFSET64 1 /* GLIBC: for 64bit fileops, ... ? */
#ifndef _LARGEFILE_SOURCE
#define _LARGEFILE_SOURCE
#endif
-
#ifndef _FILE_OFFSET_BITS
#define _FILE_OFFSET_BITS 64
#endif
-/* Set a xoff_t typedef and the "Q" printf insert. */
-#if defined(_WIN32)
typedef uint64_t xoff_t;
-/* Note: The following generates benign warnings in a mingw
- * cross-compiler */
-#define Q "I64"
-#elif SIZEOF_UNSIGNED_LONG == 8
-typedef unsigned long xoff_t;
-#define Q "l"
-#elif SIZEOF_SIZE_T == 8
-typedef size_t xoff_t;
+#define SIZEOF_XOFF_T 8
+#define SIZEOF_USIZE_T 4
+#ifndef WIN32
+#if SIZEOF_SIZE_T == 8
#define Q "z"
-#elif SIZEOF_UNSIGNED_LONG_LONG == 8
-typedef unsigned long long xoff_t;
+#else
#define Q "ll"
-#endif /* typedef and #define Q */
-
-#define SIZEOF_XOFF_T 8
-
-#else /* XD3_USE_LARGEFILE64 == 0 */
-
-#if SIZEOF_UNSIGNED_INT == 4
-typedef unsigned int xoff_t;
-#elif SIZEOF_UNSIGNED_LONG == 4
-typedef unsigned long xoff_t;
+#endif
+#else
+#define Q "I64"
+#endif
#else
typedef uint32_t xoff_t;
-#endif /* xoff_t is 32 bits */
-
#define SIZEOF_XOFF_T 4
-#define Q
-#endif /* 64 vs 32 bit xoff_t */
-
-/* Settings based on the size of usize_t (32 and 64 bit window size) */
-#if XD3_USE_LARGESIZET
-
-/* Set a usize_ttypedef and the "W" printf insert. */
-#if defined(_WIN32)
-typedef uint64_t usize_t;
-/* Note: The following generates benign warnings in a mingw
- * cross-compiler */
-#define W "I64"
-#elif SIZEOF_UNSIGNED_LONG == 8
-typedef unsigned long usize_t;
-#define W "l"
-#elif SIZEOF_SIZE_T == 8
-typedef size_t usize_t;
-#define W "z"
-#elif SIZEOF_UNSIGNED_LONG_LONG == 8
-typedef unsigned long long usize_t;
-#define W "ll"
-#endif /* typedef and #define W */
-
-#define SIZEOF_USIZE_T 8
-
-#else /* XD3_USE_LARGESIZET == 0 */
-
-#if SIZEOF_UNSIGNED_INT == 4
-typedef unsigned int usize_t;
-#elif SIZEOF_UNSIGNED_LONG == 4
-typedef unsigned long usize_t;
-#else
-typedef uint32_t usize_t;
-#endif /* usize_t is 32 bits */
-
#define SIZEOF_USIZE_T 4
-#define W
-
-#endif /* 64 vs 32 bit usize_t */
-
-/* Settings based on the size of size_t (the system-provided,
- * usually-but-maybe-not an unsigned type) */
-#if SIZEOF_SIZE_T == 4
-#define Z "z"
-#elif SIZEOF_SIZE_T == 8
-#ifdef _WIN32
-#define Z "I64"
-#else /* !_WIN32 */
-#define Z "z"
-#endif /* Windows or not */
-#else
-#error Bad configure script
-#endif /* size_t printf flags */
+#define Q
+#endif
#define USE_UINT32 (SIZEOF_USIZE_T == 4 || \
SIZEOF_XOFF_T == 4 || REGRESSION_TEST)
@@ -297,10 +220,6 @@ typedef uint32_t usize_t;
#define SWIG_MODULE 0
#endif
-#ifndef NOT_MAIN
-#define NOT_MAIN 0
-#endif
-
/* There are three string matching functions supplied: one fast, one
* slow (default), and one soft-configurable. To disable any of
* these, use the following definitions. */
@@ -327,6 +246,12 @@ typedef uint32_t usize_t;
#include <stdio.h>
#endif
+/* XPRINT. Debug output and VCDIFF_TOOLS functions report to stderr.
+ * I have used an irregular style to abbreviate [fprintf(stderr, "] as
+ * [DP(RINT "]. */
+#define DP fprintf
+#define RINT stderr,
+
typedef struct _xd3_stream xd3_stream;
typedef struct _xd3_source xd3_source;
typedef struct _xd3_hash_cfg xd3_hash_cfg;
@@ -365,54 +290,31 @@ typedef int (xd3_getblk_func) (xd3_stream *stream,
xd3_source *source,
xoff_t blkno);
-typedef const xd3_dinst* (xd3_code_table_func) (void);
-
-
-#ifdef _WIN32
-#define vsnprintf_func _vsnprintf
-#define snprintf_func _snprintf
-#else
-#define vsnprintf_func vsnprintf
-#define snprintf_func snprintf
-#endif
-#define short_sprintf(sb,fmt,...) \
- snprintf_func((sb).buf,sizeof((sb).buf),fmt,__VA_ARGS__)
-
-/* Type used for short snprintf calls. */
-typedef struct {
- char buf[48];
-} shortbuf;
+/* These are internal functions to delay construction of encoding
+ * tables and support alternate code tables. See the comments & code
+ * enabled by GENERIC_ENCODE_TABLES. */
-#ifndef PRINTF_ATTRIBUTE
-#define PRINTF_ATTRIBUTE(x,y) __attribute__ ((__format__ (__printf__, x, y)))
-#endif
+typedef const xd3_dinst* (xd3_code_table_func) (void);
+typedef int (xd3_comp_table_func) (xd3_stream *stream,
+ const uint8_t **data,
+ usize_t *size);
-/* Underlying xprintf() */
-int xsnprintf_func (char *str, size_t n, const char *fmt, ...)
- PRINTF_ATTRIBUTE(3,4);
-/* XPR(NT "", ...) (used by main) prefixes an "xdelta3: " to the output. */
-void xprintf(const char *fmt, ...) PRINTF_ATTRIBUTE(1,2);
-#define XPR xprintf
-#define NT "xdelta3: "
-#define NTR ""
-/* DP(RINT ...) */
-#define DP xprintf
-#define RINT ""
#if XD3_DEBUG
-#define XD3_ASSERT(x) \
- do { \
- if (! (x)) { \
- DP(RINT "%s:%d: XD3 assertion failed: %s\n", \
- __FILE__, __LINE__, #x); \
- abort (); } } while (0)
+#define XD3_ASSERT(x) \
+ do { if (! (x)) { DP(RINT "%s:%d: XD3 assertion failed: %s\n", __FILE__, __LINE__, #x); \
+ abort (); } } while (0)
#else
#define XD3_ASSERT(x) (void)0
#endif /* XD3_DEBUG */
-#define xd3_max(x,y) ((x) < (y) ? (y) : (x))
-#define xd3_min(x,y) ((x) < (y) ? (x) : (y))
+#ifndef max
+#define max(x,y) ((x) < (y) ? (y) : (x))
+#endif
+#ifndef min
+#define min(x,y) ((x) < (y) ? (x) : (y))
+#endif
/****************************************************************
PUBLIC ENUMS
@@ -447,7 +349,7 @@ typedef enum {
XD3_INVALID_INPUT = -17712, /* invalid input/decoder error */
XD3_NOSECOND = -17713, /* when secondary compression finds no
improvement. */
- XD3_UNIMPLEMENTED = -17714 /* currently VCD_TARGET, VCD_CODETABLE */
+ XD3_UNIMPLEMENTED = -17714 /* currently VCD_TARGET */
} xd3_rvalues;
/* special values in config->flags */
@@ -483,6 +385,9 @@ typedef enum
XD3_ADLER32_NOVER = (1 << 11), /* disable checksum verification in
the decoder. */
+ XD3_ALT_CODE_TABLE = (1 << 12), /* for testing th
+ e alternate code table encoding. */
+
XD3_NOCOMPRESS = (1 << 13), /* disable ordinary data
* compression feature, only search
* the source, not the target. */
@@ -654,9 +559,9 @@ struct _xd3_dinst
/* the decoded form of a single (half) instruction. */
struct _xd3_hinst
{
- uint8_t type;
- usize_t size;
- usize_t addr;
+ uint8_t type;
+ uint32_t size; /* TODO: why decode breaks if this is usize_t? */
+ uint32_t addr; /* TODO: why decode breaks if this is usize_t? */
};
/* the form of a whole-file instruction */
@@ -683,7 +588,7 @@ struct _xd3_desect
{
const uint8_t *buf;
const uint8_t *buf_max;
- usize_t size;
+ uint32_t size; /* TODO: why decode breaks if this is usize_t? */
usize_t pos;
/* used in xdelta3-decode.h */
@@ -731,13 +636,9 @@ struct _xd3_smatcher
/* hash table size & power-of-two hash function. */
struct _xd3_hash_cfg
{
- usize_t size; // Number of buckets
- usize_t shift;
- usize_t mask;
- usize_t look; // How wide is this checksum
- usize_t multiplier; // K * powers[0]
- usize_t *powers; // Array of [0,look) where powers[look-1] == 1
- // and powers[N] = powers[N+1]*K (Rabin-Karp)
+ usize_t size;
+ usize_t shift;
+ usize_t mask;
};
/* the sprev list */
@@ -797,7 +698,7 @@ struct _xd3_config
xd3_alloc_func *alloc;
xd3_free_func *freef;
void *opaque; /* Not used. */
- uint32_t flags; /* stream->flags are initialized
+ int flags; /* stream->flags are initialized
* from xd3_config & never
* modified by the library. Use
* xd3_set_flags to modify flags
@@ -840,8 +741,8 @@ struct _xd3_source
usize_t srclen; /* length of this source window */
xoff_t srcbase; /* offset of this source window
in the source itself */
- usize_t shiftby; /* for power-of-two blocksizes */
- usize_t maskby; /* for power-of-two blocksizes */
+ int shiftby; /* for power-of-two blocksizes */
+ int maskby; /* for power-of-two blocksizes */
xoff_t cpyoff_blocks; /* offset of dec_cpyoff in blocks */
usize_t cpyoff_blkoff; /* offset of copy window in
blocks, remainder */
@@ -852,6 +753,10 @@ struct _xd3_source
xoff_t max_blkno; /* Maximum block, if eof is known,
* otherwise, equals frontier_blkno
* (initially 0). */
+ xoff_t frontier_blkno; /* If eof is unknown, the next
+ * source position to be read.
+ * Otherwise, equal to
+ * max_blkno. */
usize_t onlastblk; /* Number of bytes on max_blkno */
int eof_known; /* Set to true when the first
* partial block is read. */
@@ -898,7 +803,7 @@ struct _xd3_stream
xd3_free_func *free; /* free function */
void* opaque; /* private data object passed to
alloc, free, and getblk */
- uint32_t flags; /* various options */
+ int flags; /* various options */
/* secondary compressor configuration */
xd3_sec_cfg sec_data; /* Secondary compressor config: data */
@@ -996,11 +901,13 @@ struct _xd3_stream
usize_t dec_secondid; /* Optional secondary compressor ID. */
- usize_t dec_codetblsz; /* Optional code table: length. */
+ /* TODO: why decode breaks if this is usize_t? */
+ uint32_t dec_codetblsz; /* Optional code table: length. */
uint8_t *dec_codetbl; /* Optional code table: storage. */
usize_t dec_codetblbytes; /* Optional code table: position. */
- usize_t dec_appheadsz; /* Optional application header:
+ /* TODO: why decode breaks if this is usize_t? */
+ uint32_t dec_appheadsz; /* Optional application header:
size. */
uint8_t *dec_appheader; /* Optional application header:
storage */
@@ -1011,12 +918,15 @@ struct _xd3_stream
uint8_t dec_cksum[4]; /* Optional checksum: storage. */
uint32_t dec_adler32; /* Optional checksum: value. */
- usize_t dec_cpylen; /* length of copy window
+ /* TODO: why decode breaks if this is usize_t? */
+ uint32_t dec_cpylen; /* length of copy window
(VCD_SOURCE or VCD_TARGET) */
xoff_t dec_cpyoff; /* offset of copy window
(VCD_SOURCE or VCD_TARGET) */
- usize_t dec_enclen; /* length of delta encoding */
- usize_t dec_tgtlen; /* length of target window */
+ /* TODO: why decode breaks if this is usize_t? */
+ uint32_t dec_enclen; /* length of delta encoding */
+ /* TODO: why decode breaks if this is usize_t? */
+ uint32_t dec_tgtlen; /* length of target window */
#if USE_UINT64
uint64_t dec_64part; /* part of a decoded uint64_t */
@@ -1065,6 +975,7 @@ struct _xd3_stream
xd3_desect data_sect;
xd3_code_table_func *code_table_func;
+ xd3_comp_table_func *comp_table_func;
const xd3_dinst *code_table;
const xd3_code_table_desc *code_table_desc;
xd3_dinst *code_table_alloc;
@@ -1321,7 +1232,7 @@ const char* xd3_strerror (int ret);
specified flags. */
static inline
void xd3_init_config (xd3_config *config,
- uint32_t flags)
+ int flags)
{
memset (config, 0, sizeof (*config));
config->flags = flags;
@@ -1384,7 +1295,7 @@ usize_t xd3_encoder_srclen (xd3_stream *stream) {
/* Checks for legal flag changes. */
static inline
-void xd3_set_flags (xd3_stream *stream, uint32_t flags)
+void xd3_set_flags (xd3_stream *stream, int flags)
{
/* The bitwise difference should contain only XD3_FLUSH or
XD3_SKIP_WINDOW */
@@ -1409,8 +1320,8 @@ void xd3_blksize_div (const xoff_t offset,
const xd3_source *source,
xoff_t *blkno,
usize_t *blkoff) {
- *blkno = offset >> source->shiftby;
- *blkoff = offset & source->maskby;
+ *blkno = (xoff_t) (offset >> source->shiftby);
+ *blkoff = (usize_t) (offset & source->maskby);
XD3_ASSERT (*blkoff < source->blksize);
}