"Fossies" - the Fresh Open Source Software Archive

Member "xdelta3-3.0.11/xdelta3-internal.h" (3 Nov 2015, 13849 Bytes) of package /linux/misc/xdelta3-3.0.11.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "xdelta3-internal.h" see the Fossies "Dox" file reference documentation.

    1 /* xdelta3 - delta compression tools and library
    2  * Copyright (C) 2011, 2012, 2013, 2014, 2015 Joshua P. MacDonald
    3  *
    4  *  This program is free software; you can redistribute it and/or modify
    5  *  it under the terms of the GNU General Public License as published by
    6  *  the Free Software Foundation; either version 2 of the License, or
    7  *  (at your option) any later version.
    8  *
    9  *  This program is distributed in the hope that it will be useful,
   10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
   11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   12  *  GNU General Public License for more details.
   13  *
   14  *  You should have received a copy of the GNU General Public License
   15  *  along with this program; if not, write to the Free Software
   16  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   17  */
   18 #ifndef XDELTA3_INTERNAL_H__
   19 #define XDELTA3_INTERNAL_H__
   20 
   21 #include "xdelta3.h"
   22 
   23 typedef struct _main_file        main_file;
   24 typedef struct _main_extcomp     main_extcomp;
   25 
   26 void main_buffree (void *ptr);
   27 void* main_bufalloc (size_t size);
   28 void main_file_init (main_file *xfile);
   29 int main_file_close (main_file *xfile);
   30 void main_file_cleanup (main_file *xfile);
   31 int main_file_isopen (main_file *xfile);
   32 int main_file_open (main_file *xfile, const char* name, int mode);
   33 int main_file_exists (main_file *xfile);
   34 int xd3_whole_append_window (xd3_stream *stream);
   35 int xd3_main_cmdline (int argc, char **argv);
   36 int main_file_read (main_file  *ifile,
   37             uint8_t    *buf,
   38             size_t     size,
   39             size_t    *nread,
   40             const char *msg);
   41 int main_file_write (main_file *ofile, uint8_t *buf, 
   42              usize_t size, const char *msg);
   43 int test_compare_files (const char* f0, const char* f1);
   44 usize_t xd3_bytes_on_srcblk (xd3_source *src, xoff_t blkno);
   45 xoff_t xd3_source_eof(const xd3_source *src);
   46 uint32_t xd3_large_cksum_update (uint32_t cksum,
   47                  const uint8_t *base,
   48                  usize_t look);
   49 int xd3_emit_byte (xd3_stream  *stream,
   50            xd3_output **outputp,
   51            uint8_t      code);
   52 
   53 int xd3_emit_bytes (xd3_stream     *stream,
   54             xd3_output    **outputp,
   55             const uint8_t  *base,
   56             usize_t          size);
   57 xd3_output* xd3_alloc_output (xd3_stream *stream,
   58                   xd3_output *old_output);
   59 
   60 int xd3_encode_init_full (xd3_stream *stream);
   61 size_t xd3_pow2_roundup (size_t x);
   62 int xd3_process_stream (int            is_encode,
   63             xd3_stream    *stream,
   64             int          (*func) (xd3_stream *),
   65             int            close_stream,
   66             const uint8_t *input,
   67             usize_t        input_size,
   68             uint8_t       *output,
   69             usize_t       *output_size,
   70             usize_t        output_size_max);
   71 
   72 #if PYTHON_MODULE || SWIG_MODULE || NOT_MAIN
   73 int xd3_main_cmdline (int argc, char **argv);
   74 #endif
   75 
   76 /* main_file->mode values */
   77 typedef enum
   78 {
   79   XO_READ  = 0,
   80   XO_WRITE = 1
   81 } main_file_modes;
   82 
   83 #ifndef XD3_POSIX
   84 #define XD3_POSIX 0
   85 #endif
   86 #ifndef XD3_STDIO
   87 #define XD3_STDIO 0
   88 #endif
   89 #ifndef XD3_WIN32
   90 #define XD3_WIN32 0
   91 #endif
   92 #ifndef NOT_MAIN
   93 #define NOT_MAIN 0
   94 #endif
   95 
   96 /* If none are set, default to posix. */
   97 #if (XD3_POSIX + XD3_STDIO + XD3_WIN32) == 0
   98 #undef XD3_POSIX
   99 #define XD3_POSIX 1
  100 #endif
  101 
  102 struct _main_file
  103 {
  104 #if XD3_WIN32
  105   HANDLE              file;
  106 #elif XD3_STDIO
  107   FILE               *file;
  108 #elif XD3_POSIX
  109   int                 file;
  110 #endif
  111 
  112   int                 mode;          /* XO_READ and XO_WRITE */
  113   const char         *filename;      /* File name or /dev/stdin,
  114                       * /dev/stdout, /dev/stderr. */
  115   char               *filename_copy; /* File name or /dev/stdin,
  116                       * /dev/stdout, /dev/stderr. */
  117   const char         *realname;      /* File name or /dev/stdin,
  118                       * /dev/stdout, /dev/stderr. */
  119   const main_extcomp *compressor;    /* External compression struct. */
  120   int                 flags;         /* RD_FIRST, RD_NONEXTERNAL, ... */
  121   xoff_t              nread;         /* for input position */
  122   xoff_t              nwrite;        /* for output position */
  123   uint8_t            *snprintf_buf;  /* internal snprintf() use */
  124   int                 size_known;    /* Set by main_set_souze */
  125   xoff_t              source_position;  /* for avoiding seek in getblk_func */
  126   int                 seek_failed;   /* after seek fails once, try FIFO */
  127 };
  128 
  129 #ifdef _WIN32
  130 #define vsnprintf_func _vsnprintf
  131 #define snprintf_func _snprintf
  132 #else
  133 #define vsnprintf_func vsnprintf
  134 #define snprintf_func snprintf
  135 #endif
  136 #define short_sprintf(sb,fmt,...) \
  137   snprintf_func((sb).buf,sizeof((sb).buf),fmt,__VA_ARGS__)
  138 
  139 /* Type used for short snprintf calls. */
  140 typedef struct {
  141   char buf[48];
  142 } shortbuf;
  143 
  144 /* Prior to SVN 303 this function was only defined in DJGPP and WIN32
  145  * environments and other platforms would use the builtin snprintf()
  146  * with an arrangement of macros below.  In OS X 10.6, Apply made
  147  * snprintf() a macro, which defeated those macros (since snprintf
  148  * would be evaluated before its argument macros were expanded,
  149  * therefore always define xsnprintf_func. */
  150 #undef PRINTF_ATTRIBUTE
  151 #ifdef __GNUC__
  152 /* Let's just assume no one uses gcc 2.x! */
  153 #define PRINTF_ATTRIBUTE(x,y) __attribute__ ((__format__ (__printf__, x, y)))
  154 #else
  155 #define PRINTF_ATTRIBUTE(x,y)
  156 #endif
  157 
  158 /* Underlying xprintf() */
  159 int xsnprintf_func (char *str, int n, const char *fmt, ...)
  160   PRINTF_ATTRIBUTE(3,4);
  161 
  162 /* XPR(NT "", ...) (used by main) prefixes an "xdelta3: " to the output. */
  163 void xprintf(const char *fmt, ...) PRINTF_ATTRIBUTE(1,2);
  164 #define XPR xprintf
  165 #define NT "xdelta3: "
  166 #define NTR ""
  167 
  168 #ifndef UINT32_MAX
  169 #define UINT32_MAX 4294967295U
  170 #endif
  171 
  172 #ifndef UINT64_MAX
  173 #define UINT64_MAX 18446744073709551615ULL
  174 #endif
  175 
  176 #define UINT32_OFLOW_MASK 0xfe000000U
  177 #define UINT64_OFLOW_MASK 0xfe00000000000000ULL
  178 
  179 /*********************************************************************
  180  Integer encoder/decoder functions
  181  **********************************************************************/
  182 
  183 /* Consume N bytes of input, only used by the decoder. */
  184 #define DECODE_INPUT(n)             \
  185   do {                              \
  186   stream->total_in += (xoff_t) (n); \
  187   stream->avail_in -= (n);          \
  188   stream->next_in  += (n);          \
  189   } while (0)
  190 
  191 #define DECODE_INTEGER_TYPE(PART,OFLOW)                                \
  192   while (stream->avail_in != 0)                                        \
  193     {                                                                  \
  194       usize_t next = stream->next_in[0];                               \
  195                                                                        \
  196       DECODE_INPUT(1);                                                 \
  197                                                                        \
  198       if (PART & OFLOW)                                                \
  199     {                                                              \
  200       stream->msg = "overflow in decode_integer";                  \
  201       return XD3_INVALID_INPUT;                                    \
  202     }                                                              \
  203                                                                        \
  204       PART = (PART << 7) | (next & 127);                               \
  205                                                                        \
  206       if ((next & 128) == 0)                                           \
  207     {                                                              \
  208       (*val) = PART;                                               \
  209       PART = 0;                                                    \
  210       return 0;                                                    \
  211     }                                                              \
  212     }                                                                  \
  213                                                                        \
  214   stream->msg = "further input required";                              \
  215   return XD3_INPUT
  216 
  217 #define READ_INTEGER_TYPE(TYPE, OFLOW)                                 \
  218   TYPE val = 0;                                                        \
  219   const uint8_t *inp = (*inpp);                                        \
  220   usize_t next;                                                        \
  221                                                                        \
  222   do                                                                   \
  223     {                                                                  \
  224       if (inp == maxp)                             \
  225     {                                                              \
  226       stream->msg = "end-of-input in read_integer";                \
  227       return XD3_INVALID_INPUT;                                    \
  228     }                                                              \
  229                                                                        \
  230       if (val & OFLOW)                                                 \
  231     {                                                              \
  232       stream->msg = "overflow in read_intger";                     \
  233       return XD3_INVALID_INPUT;                                    \
  234     }                                                              \
  235                                                                        \
  236       next = (*inp++);                                                 \
  237       val  = (val << 7) | (next & 127);                                \
  238     }                                                                  \
  239   while (next & 128);                                                  \
  240                                                                        \
  241   (*valp) = val;                                                       \
  242   (*inpp) = inp;                                                       \
  243                                                                        \
  244   return 0
  245 
  246 #define EMIT_INTEGER_TYPE()                                            \
  247   /* max 64-bit value in base-7 encoding is 9.1 bytes */               \
  248   uint8_t buf[10];                                                     \
  249   usize_t  bufi = 10;                                                  \
  250                                                                        \
  251   /* This loop performs division and turns on all MSBs. */             \
  252   do                                                                   \
  253     {                                                                  \
  254       buf[--bufi] = (num & 127) | 128;                                 \
  255       num >>= 7U;                                                      \
  256     }                                                                  \
  257   while (num != 0);                                                    \
  258                                                                        \
  259   /* Turn off MSB of the last byte. */                                 \
  260   buf[9] &= 127;                                                       \
  261                                                                        \
  262   return xd3_emit_bytes (stream, output, buf + bufi, 10 - bufi)
  263 
  264 #define IF_SIZEOF32(x) if (num < (1U   << (7 * (x)))) return (x);
  265 #define IF_SIZEOF64(x) if (num < (1ULL << (7 * (x)))) return (x);
  266 
  267 #if USE_UINT32
  268 static inline uint32_t
  269 xd3_sizeof_uint32_t (uint32_t num)
  270 {
  271   IF_SIZEOF32(1);
  272   IF_SIZEOF32(2);
  273   IF_SIZEOF32(3);
  274   IF_SIZEOF32(4);
  275   return 5;
  276 }
  277 
  278 static inline int
  279 xd3_decode_uint32_t (xd3_stream *stream, uint32_t *val)
  280 { DECODE_INTEGER_TYPE (stream->dec_32part, UINT32_OFLOW_MASK); }
  281 
  282 static inline int
  283 xd3_read_uint32_t (xd3_stream *stream, const uint8_t **inpp,
  284            const uint8_t *maxp, uint32_t *valp)
  285 { READ_INTEGER_TYPE (uint32_t, UINT32_OFLOW_MASK); }
  286 
  287 #if XD3_ENCODER
  288 static inline int
  289 xd3_emit_uint32_t (xd3_stream *stream, xd3_output **output, uint32_t num)
  290 { EMIT_INTEGER_TYPE (); }
  291 #endif
  292 #endif
  293 
  294 #if USE_UINT64
  295 static inline int
  296 xd3_decode_uint64_t (xd3_stream *stream, uint64_t *val)
  297 { DECODE_INTEGER_TYPE (stream->dec_64part, UINT64_OFLOW_MASK); }
  298 
  299 #if XD3_ENCODER
  300 static inline int
  301 xd3_emit_uint64_t (xd3_stream *stream, xd3_output **output, uint64_t num)
  302 { EMIT_INTEGER_TYPE (); }
  303 #endif
  304 
  305 /* These are tested but not used */
  306 #if REGRESSION_TEST
  307 static int
  308 xd3_read_uint64_t (xd3_stream *stream, const uint8_t **inpp,
  309            const uint8_t *maxp, uint64_t *valp)
  310 { READ_INTEGER_TYPE (uint64_t, UINT64_OFLOW_MASK); }
  311 
  312 static uint32_t
  313 xd3_sizeof_uint64_t (uint64_t num)
  314 {
  315   IF_SIZEOF64(1);
  316   IF_SIZEOF64(2);
  317   IF_SIZEOF64(3);
  318   IF_SIZEOF64(4);
  319   IF_SIZEOF64(5);
  320   IF_SIZEOF64(6);
  321   IF_SIZEOF64(7);
  322   IF_SIZEOF64(8);
  323   IF_SIZEOF64(9);
  324 
  325   return 10;
  326 }
  327 #endif
  328 
  329 #endif
  330 
  331 #if SIZEOF_USIZE_T == 4
  332 #define USIZE_T_MAX        UINT32_MAX
  333 #define USIZE_T_MAXBLKSZ   0x80000000U
  334 #define xd3_decode_size   xd3_decode_uint32_t
  335 #define xd3_emit_size     xd3_emit_uint32_t
  336 #define xd3_sizeof_size   xd3_sizeof_uint32_t
  337 #define xd3_read_size     xd3_read_uint32_t
  338 #elif SIZEOF_USIZE_T == 8
  339 #define USIZE_T_MAX        UINT64_MAX
  340 #define USIZE_T_MAXBLKSZ   0x8000000000000000ULL
  341 #define xd3_decode_size   xd3_decode_uint64_t
  342 #define xd3_emit_size     xd3_emit_uint64_t
  343 #define xd3_sizeof_size   xd3_sizeof_uint64_t
  344 #define xd3_read_size     xd3_read_uint64_t
  345 #endif
  346 
  347 #if SIZEOF_XOFF_T == 4
  348 #define XOFF_T_MAX        UINT32_MAX
  349 #define xd3_emit_offset   xd3_emit_uint32_t
  350 static inline int
  351 xd3_decode_offset (xd3_stream *stream, xoff_t *val)
  352 {
  353   return xd3_decode_uint32_t (stream, (uint32_t*) val);
  354 }
  355 #elif SIZEOF_XOFF_T == 8
  356 #define XOFF_T_MAX        UINT64_MAX
  357 #define xd3_emit_offset   xd3_emit_uint64_t
  358 static inline int
  359 xd3_decode_offset (xd3_stream *stream, xoff_t *val)
  360 {
  361   return xd3_decode_uint64_t (stream, (uint64_t*) val);
  362 }
  363 #endif
  364 
  365 #define USIZE_T_OVERFLOW(a,b) ((USIZE_T_MAX - (usize_t) (a)) < (usize_t) (b))
  366 #define XOFF_T_OVERFLOW(a,b) ((XOFF_T_MAX - (xoff_t) (a)) < (xoff_t) (b))
  367 
  368 #define MAX_LRU_SIZE 32U
  369 #define XD3_MINSRCWINSZ (XD3_ALLOCSIZE * MAX_LRU_SIZE)
  370 #define XD3_MAXSRCWINSZ (1ULL << 31)
  371 
  372 #endif // XDELTA3_INTERNAL_H__