summaryrefslogtreecommitdiff
path: root/xdelta3/xdelta3-internal.h
diff options
context:
space:
mode:
authorJoshua MacDonald <josh.macdonald@gmail.com>2015-03-10 22:18:40 -0700
committerJoshua MacDonald <josh.macdonald@gmail.com>2015-03-10 22:18:40 -0700
commit66204f902496f9c5de3a82e1309c84c9ea103983 (patch)
tree4a608ce1a5789fd1cb25c9a1673b0a284dc18bbd /xdelta3/xdelta3-internal.h
parenta2e999beec49f222ab30006121eb84d8f9548fbb (diff)
Functions out of macros for xd3_decode_offset, to address warnings created by new definition of xoff_t
Diffstat (limited to 'xdelta3/xdelta3-internal.h')
-rw-r--r--xdelta3/xdelta3-internal.h201
1 files changed, 201 insertions, 0 deletions
diff --git a/xdelta3/xdelta3-internal.h b/xdelta3/xdelta3-internal.h
index 35de56b..2e37768 100644
--- a/xdelta3/xdelta3-internal.h
+++ b/xdelta3/xdelta3-internal.h
@@ -46,6 +46,17 @@ xoff_t xd3_source_eof(const xd3_source *src);
46uint32_t xd3_large_cksum_update (uint32_t cksum, 46uint32_t xd3_large_cksum_update (uint32_t cksum,
47 const uint8_t *base, 47 const uint8_t *base,
48 usize_t look); 48 usize_t look);
49int xd3_emit_byte (xd3_stream *stream,
50 xd3_output **outputp,
51 uint8_t code);
52
53int xd3_emit_bytes (xd3_stream *stream,
54 xd3_output **outputp,
55 const uint8_t *base,
56 usize_t size);
57xd3_output* xd3_alloc_output (xd3_stream *stream,
58 xd3_output *old_output);
59
49int xd3_encode_init_full (xd3_stream *stream); 60int xd3_encode_init_full (xd3_stream *stream);
50size_t xd3_pow2_roundup (size_t x); 61size_t xd3_pow2_roundup (size_t x);
51int xd3_process_stream (int is_encode, 62int xd3_process_stream (int is_encode,
@@ -153,4 +164,194 @@ void xprintf(const char *fmt, ...) PRINTF_ATTRIBUTE(1,2);
153#define UINT64_MAX 18446744073709551615ULL 164#define UINT64_MAX 18446744073709551615ULL
154#endif 165#endif
155 166
167#define UINT32_OFLOW_MASK 0xfe000000U
168#define UINT64_OFLOW_MASK 0xfe00000000000000ULL
169
170/*********************************************************************
171 Integer encoder/decoder functions
172 **********************************************************************/
173
174/* Consume N bytes of input, only used by the decoder. */
175#define DECODE_INPUT(n) \
176 do { \
177 stream->total_in += (xoff_t) (n); \
178 stream->avail_in -= (n); \
179 stream->next_in += (n); \
180 } while (0)
181
182#define DECODE_INTEGER_TYPE(PART,OFLOW) \
183 while (stream->avail_in != 0) \
184 { \
185 usize_t next = stream->next_in[0]; \
186 \
187 DECODE_INPUT(1); \
188 \
189 if (PART & OFLOW) \
190 { \
191 stream->msg = "overflow in decode_integer"; \
192 return XD3_INVALID_INPUT; \
193 } \
194 \
195 PART = (PART << 7) | (next & 127); \
196 \
197 if ((next & 128) == 0) \
198 { \
199 (*val) = PART; \
200 PART = 0; \
201 return 0; \
202 } \
203 } \
204 \
205 stream->msg = "further input required"; \
206 return XD3_INPUT
207
208#define READ_INTEGER_TYPE(TYPE, OFLOW) \
209 TYPE val = 0; \
210 const uint8_t *inp = (*inpp); \
211 usize_t next; \
212 \
213 do \
214 { \
215 if (inp == max) \
216 { \
217 stream->msg = "end-of-input in read_integer"; \
218 return XD3_INVALID_INPUT; \
219 } \
220 \
221 if (val & OFLOW) \
222 { \
223 stream->msg = "overflow in read_intger"; \
224 return XD3_INVALID_INPUT; \
225 } \
226 \
227 next = (*inp++); \
228 val = (val << 7) | (next & 127); \
229 } \
230 while (next & 128); \
231 \
232 (*valp) = val; \
233 (*inpp) = inp; \
234 \
235 return 0
236
237#define EMIT_INTEGER_TYPE() \
238 /* max 64-bit value in base-7 encoding is 9.1 bytes */ \
239 uint8_t buf[10]; \
240 usize_t bufi = 10; \
241 \
242 /* This loop performs division and turns on all MSBs. */ \
243 do \
244 { \
245 buf[--bufi] = (num & 127) | 128; \
246 num >>= 7U; \
247 } \
248 while (num != 0); \
249 \
250 /* Turn off MSB of the last byte. */ \
251 buf[9] &= 127; \
252 \
253 return xd3_emit_bytes (stream, output, buf + bufi, 10 - bufi)
254
255#define IF_SIZEOF32(x) if (num < (1U << (7 * (x)))) return (x);
256#define IF_SIZEOF64(x) if (num < (1ULL << (7 * (x)))) return (x);
257
258#if USE_UINT32
259static inline uint32_t
260xd3_sizeof_uint32_t (uint32_t num)
261{
262 IF_SIZEOF32(1);
263 IF_SIZEOF32(2);
264 IF_SIZEOF32(3);
265 IF_SIZEOF32(4);
266 return 5;
267}
268
269static inline int
270xd3_decode_uint32_t (xd3_stream *stream, uint32_t *val)
271{ DECODE_INTEGER_TYPE (stream->dec_32part, UINT32_OFLOW_MASK); }
272
273static inline int
274xd3_read_uint32_t (xd3_stream *stream, const uint8_t **inpp,
275 const uint8_t *max, uint32_t *valp)
276{ READ_INTEGER_TYPE (uint32_t, UINT32_OFLOW_MASK); }
277
278#if XD3_ENCODER
279static inline int
280xd3_emit_uint32_t (xd3_stream *stream, xd3_output **output, uint32_t num)
281{ EMIT_INTEGER_TYPE (); }
282#endif
283#endif
284
285#if USE_UINT64
286static inline int
287xd3_decode_uint64_t (xd3_stream *stream, uint64_t *val)
288{ DECODE_INTEGER_TYPE (stream->dec_64part, UINT64_OFLOW_MASK); }
289
290#if XD3_ENCODER
291static inline int
292xd3_emit_uint64_t (xd3_stream *stream, xd3_output **output, uint64_t num)
293{ EMIT_INTEGER_TYPE (); }
294#endif
295
296/* These are tested but not used */
297#if REGRESSION_TEST
298static int
299xd3_read_uint64_t (xd3_stream *stream, const uint8_t **inpp,
300 const uint8_t *max, uint64_t *valp)
301{ READ_INTEGER_TYPE (uint64_t, UINT64_OFLOW_MASK); }
302
303static uint32_t
304xd3_sizeof_uint64_t (uint64_t num)
305{
306 IF_SIZEOF64(1);
307 IF_SIZEOF64(2);
308 IF_SIZEOF64(3);
309 IF_SIZEOF64(4);
310 IF_SIZEOF64(5);
311 IF_SIZEOF64(6);
312 IF_SIZEOF64(7);
313 IF_SIZEOF64(8);
314 IF_SIZEOF64(9);
315
316 return 10;
317}
318#endif
319
320#endif
321
322#if SIZEOF_USIZE_T == 4
323#define USIZE_T_MAX UINT32_MAX
324#define xd3_decode_size xd3_decode_uint32_t
325#define xd3_emit_size xd3_emit_uint32_t
326#define xd3_sizeof_size xd3_sizeof_uint32_t
327#define xd3_read_size xd3_read_uint32_t
328#elif SIZEOF_USIZE_T == 8
329#define USIZE_T_MAX UINT64_MAX
330#define xd3_decode_size xd3_decode_uint64_t
331#define xd3_emit_size xd3_emit_uint64_t
332#define xd3_sizeof_size xd3_sizeof_uint64_t
333#define xd3_read_size xd3_read_uint64_t
334#endif
335
336#if SIZEOF_XOFF_T == 4
337#define XOFF_T_MAX UINT32_MAX
338#define xd3_emit_offset xd3_emit_uint32_t
339static inline int
340xd3_decode_offset (xd3_stream *stream, xoff_t *val)
341{
342 return xd3_decode_uint32_t (stream, (uint32_t*) val);
343}
344#elif SIZEOF_XOFF_T == 8
345#define XOFF_T_MAX UINT64_MAX
346#define xd3_emit_offset xd3_emit_uint64_t
347static inline int
348xd3_decode_offset (xd3_stream *stream, xoff_t *val)
349{
350 return xd3_decode_uint64_t (stream, (uint64_t*) val);
351}
352#endif
353
354#define USIZE_T_OVERFLOW(a,b) ((USIZE_T_MAX - (usize_t) (a)) < (usize_t) (b))
355#define XOFF_T_OVERFLOW(a,b) ((XOFF_T_MAX - (xoff_t) (a)) < (xoff_t) (b))
356
156#endif // XDELTA3_INTERNAL_H__ 357#endif // XDELTA3_INTERNAL_H__