tizen 2.4 release
[external/xdelta3.git] / xdelta3-internal.h
1 /* xdelta3 - delta compression tools and library
2  * Copyright (C) 2011, 2012 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_encode_init_full (xd3_stream *stream);
50 size_t xd3_pow2_roundup (size_t x);
51 int xd3_process_stream (int            is_encode,
52                         xd3_stream    *stream,
53                         int          (*func) (xd3_stream *),
54                         int            close_stream,
55                         const uint8_t *input,
56                         usize_t        input_size,
57                         uint8_t       *output,
58                         usize_t       *output_size,
59                         usize_t        output_size_max);
60
61 #if PYTHON_MODULE || SWIG_MODULE || NOT_MAIN
62 int xd3_main_cmdline (int argc, char **argv);
63 #endif
64
65 /* main_file->mode values */
66 typedef enum
67 {
68   XO_READ  = 0,
69   XO_WRITE = 1
70 } main_file_modes;
71
72 struct _main_file
73 {
74 #if XD3_STDIO
75   FILE               *file;
76 #elif XD3_POSIX
77   int                 file;
78 #elif XD3_WIN32
79   HANDLE              file;
80 #endif
81
82   int                 mode;          /* XO_READ and XO_WRITE */
83   const char         *filename;      /* File name or /dev/stdin,
84                                       * /dev/stdout, /dev/stderr. */
85   char               *filename_copy; /* File name or /dev/stdin,
86                                       * /dev/stdout, /dev/stderr. */
87   const char         *realname;      /* File name or /dev/stdin,
88                                       * /dev/stdout, /dev/stderr. */
89   const main_extcomp *compressor;    /* External compression struct. */
90   int                 flags;         /* RD_FIRST, RD_NONEXTERNAL, ... */
91   xoff_t              nread;         /* for input position */
92   xoff_t              nwrite;        /* for output position */
93   uint8_t            *snprintf_buf;  /* internal snprintf() use */
94   int                 size_known;    /* Set by main_set_souze */
95   xoff_t              source_position;  /* for avoiding seek in getblk_func */
96   int                 seek_failed;   /* after seek fails once, try FIFO */
97 };
98
99 /* According to the internet, Windows vsnprintf() differs from most
100  * Unix implementations regarding the terminating 0 when the boundary
101  * condition is met. It doesn't matter here, we don't rely on the
102  * trailing 0.  Besides, both Windows and DJGPP vsnprintf return -1
103  * upon truncation, which isn't C99 compliant. To overcome this,
104  * recent MinGW runtimes provided their own vsnprintf (notice the
105  * absence of the '_' prefix) but they were initially buggy.  So,
106  * always use the native '_'-prefixed version with Win32. */
107 #ifdef _WIN32
108 #define vsnprintf_func(str,size,fmt,args) \
109   _vsnprintf_s(str,size,size-1,fmt,args)
110 #define snprintf_func(str,size,fmt,...) \
111   _snprintf_s(str,size,size-1,fmt,__VA_ARGS__)
112 #else
113 #define vsnprintf_func vsnprintf
114 #define snprintf_func snprintf
115 #endif
116 #define short_sprintf(sb,fmt,...) \
117   snprintf_func((sb).buf,sizeof((sb).buf),fmt,__VA_ARGS__)
118
119 /* Type used for short snprintf calls. */
120 typedef struct {
121   char buf[48];
122 } shortbuf;
123
124 /* Prior to SVN 303 this function was only defined in DJGPP and WIN32
125  * environments and other platforms would use the builtin snprintf()
126  * with an arrangement of macros below.  In OS X 10.6, Apply made
127  * snprintf() a macro, which defeated those macros (since snprintf
128  * would be evaluated before its argument macros were expanded,
129  * therefore always define xsnprintf_func. */
130 #undef PRINTF_ATTRIBUTE
131 #ifdef __GNUC__
132 /* Let's just assume no one uses gcc 2.x! */
133 #define PRINTF_ATTRIBUTE(x,y) __attribute__ ((__format__ (__printf__, x, y)))
134 #else
135 #define PRINTF_ATTRIBUTE(x,y)
136 #endif
137
138 /* Underlying xprintf() */
139 int xsnprintf_func (char *str, int n, const char *fmt, ...)
140   PRINTF_ATTRIBUTE(3,4);
141
142 /* XPR(NT "", ...) (used by main) prefixes an "xdelta3: " to the output. */
143 void xprintf(const char *fmt, ...) PRINTF_ATTRIBUTE(1,2);
144 #define XPR xprintf
145 #define NT "xdelta3: "
146 #define NTR ""
147
148 #ifndef UINT32_MAX
149 #define UINT32_MAX 4294967295U
150 #endif
151
152 #ifndef UINT64_MAX
153 #define UINT64_MAX 18446744073709551615ULL
154 #endif
155
156 #endif // XDELTA3_INTERNAL_H__