summaryrefslogtreecommitdiff
path: root/xdelta3/xdelta3-djw.h
diff options
context:
space:
mode:
authorjosh.macdonald <jmacd@users.noreply.github.com>2008-09-17 03:32:47 +0000
committerjosh.macdonald <jmacd@users.noreply.github.com>2008-09-17 03:32:47 +0000
commit0445e4a19055823f52ad799d0472b7331cedcf57 (patch)
tree424f9f8b157e38d47b1a9aff9f19558ca2ef07eb /xdelta3/xdelta3-djw.h
parent3f45014f9ed4132a7f388a034237e9ab93fe7ffe (diff)
Compile with
-Wconversion -Wsign-compare -Wextra -Wno-unused-parameter in C and C++.
Diffstat (limited to 'xdelta3/xdelta3-djw.h')
-rw-r--r--xdelta3/xdelta3-djw.h115
1 files changed, 60 insertions, 55 deletions
diff --git a/xdelta3/xdelta3-djw.h b/xdelta3/xdelta3-djw.h
index 24f5b81..53de230 100644
--- a/xdelta3/xdelta3-djw.h
+++ b/xdelta3/xdelta3-djw.h
@@ -53,56 +53,56 @@
53 * the other-format tests, including RFC1950 and the RFC1950+MTF 53 * the other-format tests, including RFC1950 and the RFC1950+MTF
54 * tests. */ 54 * tests. */
55 55
56#define DJW_MAX_CODELEN 20 /* Maximum length of an alphabet code. */ 56#define DJW_MAX_CODELEN 20U /* Maximum length of an alphabet code. */
57 57
58/* Code lengths are themselves code-length encoded, so the total number of 58/* Code lengths are themselves code-length encoded, so the total number of
59 * codes is: [RUN_0, RUN_1, 1-DJW_MAX_CODELEN] */ 59 * codes is: [RUN_0, RUN_1, 1-DJW_MAX_CODELEN] */
60#define DJW_TOTAL_CODES (DJW_MAX_CODELEN+2) 60#define DJW_TOTAL_CODES (DJW_MAX_CODELEN+2)
61 61
62#define RUN_0 0 /* Symbols used in MTF+1/2 coding. */ 62#define RUN_0 0U /* Symbols used in MTF+1/2 coding. */
63#define RUN_1 1 63#define RUN_1 1U
64 64
65/* Number of code lengths always encoded (djw_encode_basic array) */ 65/* Number of code lengths always encoded (djw_encode_basic array) */
66#define DJW_BASIC_CODES 5 66#define DJW_BASIC_CODES 5U
67#define DJW_RUN_CODES 2 /* Number of run codes */ 67#define DJW_RUN_CODES 2U /* Number of run codes */
68 68
69/* Offset of extra codes */ 69/* Offset of extra codes */
70#define DJW_EXTRA_12OFFSET (DJW_BASIC_CODES + DJW_RUN_CODES) 70#define DJW_EXTRA_12OFFSET (DJW_BASIC_CODES + DJW_RUN_CODES)
71 71
72/* Number of optionally encoded code lengths (djw_encode_extra array) */ 72/* Number of optionally encoded code lengths (djw_encode_extra array) */
73#define DJW_EXTRA_CODES 15 73#define DJW_EXTRA_CODES 15U
74 74
75/* Number of bits to code [0-DJW_EXTRA_CODES] */ 75/* Number of bits to code [0-DJW_EXTRA_CODES] */
76#define DJW_EXTRA_CODE_BITS 4 76#define DJW_EXTRA_CODE_BITS 4U
77 77
78#define DJW_MAX_GROUPS 8 /* Max number of group coding tables */ 78#define DJW_MAX_GROUPS 8U /* Max number of group coding tables */
79#define DJW_GROUP_BITS 3 /* Number of bits to code [1-DJW_MAX_GROUPS] */ 79#define DJW_GROUP_BITS 3U /* Number of bits to code [1-DJW_MAX_GROUPS] */
80 80
81#define DJW_SECTORSZ_MULT 5 /* Multiplier for encoded sectorsz */ 81#define DJW_SECTORSZ_MULT 5U /* Multiplier for encoded sectorsz */
82#define DJW_SECTORSZ_BITS 5 /* Number of bits to code group size */ 82#define DJW_SECTORSZ_BITS 5U /* Number of bits to code group size */
83#define DJW_SECTORSZ_MAX ((1 << DJW_SECTORSZ_BITS) * DJW_SECTORSZ_MULT) 83#define DJW_SECTORSZ_MAX ((1U << DJW_SECTORSZ_BITS) * DJW_SECTORSZ_MULT)
84 84
85/* Maximum number of iterations to find group tables. */ 85/* Maximum number of iterations to find group tables. */
86#define DJW_MAX_ITER 6 86#define DJW_MAX_ITER 6U
87/* Minimum number of bits an iteration must reduce coding by. */ 87/* Minimum number of bits an iteration must reduce coding by. */
88#define DJW_MIN_IMPROVEMENT 20 88#define DJW_MIN_IMPROVEMENT 20U
89 89
90/* Maximum code length of a prefix code length */ 90/* Maximum code length of a prefix code length */
91#define DJW_MAX_CLCLEN 15 91#define DJW_MAX_CLCLEN 15U
92 92
93/* Number of bits to code [0-DJW_MAX_CLCLEN] */ 93/* Number of bits to code [0-DJW_MAX_CLCLEN] */
94#define DJW_CLCLEN_BITS 4 94#define DJW_CLCLEN_BITS 4U
95 95
96#define DJW_MAX_GBCLEN 7 /* Maximum code length of a group selector */ 96#define DJW_MAX_GBCLEN 7U /* Maximum code length of a group selector */
97 97
98/* Number of bits to code [0-DJW_MAX_GBCLEN] 98/* Number of bits to code [0-DJW_MAX_GBCLEN]
99 * TODO: Actually, should never have zero code lengths here, or else a group 99 * TODO: Actually, should never have zero code lengths here, or else a group
100 * went unused. Write a test for this: if a group goes unused, eliminate 100 * went unused. Write a test for this: if a group goes unused, eliminate
101 * it? */ 101 * it? */
102#define DJW_GBCLEN_BITS 3 102#define DJW_GBCLEN_BITS 3U
103 103
104/* It has to save at least this many bits... */ 104/* It has to save at least this many bits... */
105#define EFFICIENCY_BITS 16 105#define EFFICIENCY_BITS 16U
106 106
107typedef struct _djw_stream djw_stream; 107typedef struct _djw_stream djw_stream;
108typedef struct _djw_heapen djw_heapen; 108typedef struct _djw_heapen djw_heapen;
@@ -226,7 +226,7 @@ static int xd3_decode_huff (xd3_stream *stream,
226static djw_stream* 226static djw_stream*
227djw_alloc (xd3_stream *stream) 227djw_alloc (xd3_stream *stream)
228{ 228{
229 return xd3_alloc (stream, sizeof (djw_stream), 1); 229 return (djw_stream*) xd3_alloc (stream, sizeof (djw_stream), 1);
230} 230}
231 231
232static void 232static void
@@ -361,7 +361,7 @@ djw_update_1_2 (int *mtf_run, usize_t *mtf_i,
361static void 361static void
362djw_init_clen_mtf_1_2 (uint8_t *clmtf) 362djw_init_clen_mtf_1_2 (uint8_t *clmtf)
363{ 363{
364 int i, cl_i = 0; 364 usize_t i, cl_i = 0;
365 365
366 clmtf[cl_i++] = 0; 366 clmtf[cl_i++] = 0;
367 for (i = 0; i < DJW_BASIC_CODES; i += 1) 367 for (i = 0; i < DJW_BASIC_CODES; i += 1)
@@ -379,7 +379,7 @@ djw_init_clen_mtf_1_2 (uint8_t *clmtf)
379/*********************************************************************/ 379/*********************************************************************/
380#if XD3_ENCODER 380#if XD3_ENCODER
381static usize_t 381static usize_t
382djw_build_prefix (const djw_weight *freq, uint8_t *clen, int asize, int maxlen) 382djw_build_prefix (const djw_weight *freq, uint8_t *clen, usize_t asize, usize_t maxlen)
383{ 383{
384 /* Heap with 0th entry unused, prefix tree with up to ALPHABET_SIZE-1 384 /* Heap with 0th entry unused, prefix tree with up to ALPHABET_SIZE-1
385 * internal nodes, never more than ALPHABET_SIZE entries actually in the 385 * internal nodes, never more than ALPHABET_SIZE entries actually in the
@@ -391,9 +391,9 @@ djw_build_prefix (const djw_weight *freq, uint8_t *clen, int asize, int maxlen)
391 391
392 usize_t heap_last; /* Index of the last _valid_ heap entry. */ 392 usize_t heap_last; /* Index of the last _valid_ heap entry. */
393 usize_t ents_size; /* Number of entries, including 0th fake entry */ 393 usize_t ents_size; /* Number of entries, including 0th fake entry */
394 int overflow; /* Number of code lengths that overflow */ 394 usize_t overflow; /* Number of code lengths that overflow */
395 uint32_t total_bits; 395 uint32_t total_bits;
396 int i; 396 usize_t i;
397 397
398 IF_DEBUG (uint32_t first_bits = 0); 398 IF_DEBUG (uint32_t first_bits = 0);
399 399
@@ -466,11 +466,11 @@ djw_build_prefix (const djw_weight *freq, uint8_t *clen, int asize, int maxlen)
466 /* Now compute prefix code lengths, counting parents. */ 466 /* Now compute prefix code lengths, counting parents. */
467 for (i = 1; i < asize+1; i += 1) 467 for (i = 1; i < asize+1; i += 1)
468 { 468 {
469 int b = 0; 469 usize_t b = 0;
470 470
471 if (ents[i].freq != 0) 471 if (ents[i].freq != 0)
472 { 472 {
473 int p = i; 473 usize_t p = i;
474 474
475 while ((p = ents[p].parent) != 0) { b += 1; } 475 while ((p = ents[p].parent) != 0) { b += 1; }
476 476
@@ -507,11 +507,11 @@ djw_build_prefix (const djw_weight *freq, uint8_t *clen, int asize, int maxlen)
507} 507}
508 508
509static void 509static void
510djw_build_codes (usize_t *codes, const uint8_t *clen, int asize, int abs_max) 510djw_build_codes (usize_t *codes, const uint8_t *clen, usize_t asize, usize_t abs_max)
511{ 511{
512 int i, l; 512 usize_t i, l;
513 int min_clen = DJW_MAX_CODELEN; 513 usize_t min_clen = DJW_MAX_CODELEN;
514 int max_clen = 0; 514 usize_t max_clen = 0;
515 usize_t code = 0; 515 usize_t code = 0;
516 516
517 /* Find the min and max code length */ 517 /* Find the min and max code length */
@@ -522,7 +522,7 @@ djw_build_codes (usize_t *codes, const uint8_t *clen, int asize, int abs_max)
522 min_clen = clen[i]; 522 min_clen = clen[i];
523 } 523 }
524 524
525 max_clen = max (max_clen, (int) clen[i]); 525 max_clen = max (max_clen, (usize_t) clen[i]);
526 } 526 }
527 527
528 XD3_ASSERT (max_clen <= abs_max); 528 XD3_ASSERT (max_clen <= abs_max);
@@ -558,7 +558,7 @@ djw_compute_mtf_1_2 (djw_prefix *prefix,
558 djw_weight *freq_out, 558 djw_weight *freq_out,
559 usize_t nsym) 559 usize_t nsym)
560{ 560{
561 int i, j, k; 561 size_t i, j, k;
562 usize_t sym; 562 usize_t sym;
563 usize_t size = prefix->scount; 563 usize_t size = prefix->scount;
564 usize_t mtf_i = 0; 564 usize_t mtf_i = 0;
@@ -640,11 +640,11 @@ djw_count_freqs (djw_weight *freq, xd3_output *input)
640} 640}
641 641
642static void 642static void
643djw_compute_multi_prefix (int groups, 643djw_compute_multi_prefix (usize_t groups,
644 uint8_t clen[DJW_MAX_GROUPS][ALPHABET_SIZE], 644 uint8_t clen[DJW_MAX_GROUPS][ALPHABET_SIZE],
645 djw_prefix *prefix) 645 djw_prefix *prefix)
646{ 646{
647 int gp, i; 647 usize_t gp, i;
648 648
649 prefix->scount = ALPHABET_SIZE; 649 prefix->scount = ALPHABET_SIZE;
650 memcpy (prefix->symbol, clen[0], ALPHABET_SIZE); 650 memcpy (prefix->symbol, clen[0], ALPHABET_SIZE);
@@ -680,7 +680,8 @@ djw_encode_prefix (xd3_stream *stream,
680 bit_state *bstate, 680 bit_state *bstate,
681 djw_prefix *prefix) 681 djw_prefix *prefix)
682{ 682{
683 int ret, i; 683 int ret;
684 size_t i;
684 usize_t num_to_encode; 685 usize_t num_to_encode;
685 djw_weight clfreq[DJW_TOTAL_CODES]; 686 djw_weight clfreq[DJW_TOTAL_CODES];
686 uint8_t clclen[DJW_TOTAL_CODES]; 687 uint8_t clclen[DJW_TOTAL_CODES];
@@ -761,7 +762,7 @@ xd3_encode_howmany_groups (xd3_stream *stream,
761 762
762 if (cfg->ngroups != 0) 763 if (cfg->ngroups != 0)
763 { 764 {
764 if (cfg->ngroups < 0 || cfg->ngroups > DJW_MAX_GROUPS) 765 if (cfg->ngroups > DJW_MAX_GROUPS)
765 { 766 {
766 stream->msg = "invalid secondary encoder group number"; 767 stream->msg = "invalid secondary encoder group number";
767 return XD3_INTERNAL; 768 return XD3_INTERNAL;
@@ -960,17 +961,17 @@ xd3_encode_huff (xd3_stream *stream,
960 djw_weight evolve_freq[DJW_MAX_GROUPS][ALPHABET_SIZE]; 961 djw_weight evolve_freq[DJW_MAX_GROUPS][ALPHABET_SIZE];
961 uint8_t evolve_clen[DJW_MAX_GROUPS][ALPHABET_SIZE]; 962 uint8_t evolve_clen[DJW_MAX_GROUPS][ALPHABET_SIZE];
962 djw_weight left = input_bytes; 963 djw_weight left = input_bytes;
963 int gp; 964 usize_t gp;
964 int niter = 0; 965 usize_t niter = 0;
965 usize_t select_bits; 966 usize_t select_bits;
966 usize_t sym1 = 0, sym2 = 0, s; 967 usize_t sym1 = 0, sym2 = 0, s;
967 usize_t gcost[DJW_MAX_GROUPS]; 968 usize_t gcost[DJW_MAX_GROUPS];
968 usize_t gbest_code[DJW_MAX_GROUPS+2]; 969 usize_t gbest_code[DJW_MAX_GROUPS+2];
969 uint8_t gbest_clen[DJW_MAX_GROUPS+2]; 970 uint8_t gbest_clen[DJW_MAX_GROUPS+2];
970 usize_t gbest_max = 1 + (input_bytes - 1) / sector_size; 971 usize_t gbest_max = 1 + (input_bytes - 1) / sector_size;
971 int best_bits = 0; 972 usize_t best_bits = 0;
972 usize_t gbest_no; 973 usize_t gbest_no;
973 usize_t gpcnt; 974 usize_t gpcnt;
974 const uint8_t *p; 975 const uint8_t *p;
975 IF_DEBUG1 (usize_t gcount[DJW_MAX_GROUPS]); 976 IF_DEBUG1 (usize_t gcount[DJW_MAX_GROUPS]);
976 977
@@ -985,7 +986,7 @@ xd3_encode_huff (xd3_stream *stream,
985 /* Dynamic allocation. */ 986 /* Dynamic allocation. */
986 if (gbest == NULL) 987 if (gbest == NULL)
987 { 988 {
988 if ((gbest = xd3_alloc (stream, gbest_max, 1)) == NULL) 989 if ((gbest = (uint8_t*) xd3_alloc (stream, gbest_max, 1)) == NULL)
989 { 990 {
990 ret = ENOMEM; 991 ret = ENOMEM;
991 goto failure; 992 goto failure;
@@ -994,7 +995,7 @@ xd3_encode_huff (xd3_stream *stream,
994 995
995 if (gbest_mtf == NULL) 996 if (gbest_mtf == NULL)
996 { 997 {
997 if ((gbest_mtf = xd3_alloc (stream, gbest_max, 1)) == NULL) 998 if ((gbest_mtf = (uint8_t*) xd3_alloc (stream, gbest_max, 1)) == NULL)
998 { 999 {
999 ret = ENOMEM; 1000 ret = ENOMEM;
1000 goto failure; 1001 goto failure;
@@ -1079,7 +1080,7 @@ xd3_encode_huff (xd3_stream *stream,
1079 1080
1080 /* Check end-of-input-page. */ 1081 /* Check end-of-input-page. */
1081# define GP_PAGE() \ 1082# define GP_PAGE() \
1082 if (++p - in->base == in->next) \ 1083 if ((usize_t)(++p - in->base) == in->next) \
1083 { \ 1084 { \
1084 in = in->next_page; \ 1085 in = in->next_page; \
1085 if (in == NULL) { break; } \ 1086 if (in == NULL) { break; } \
@@ -1090,10 +1091,14 @@ xd3_encode_huff (xd3_stream *stream,
1090 } 1091 }
1091 1092
1092 /* Find min cost group for this sector */ 1093 /* Find min cost group for this sector */
1093 best = -1U; 1094 best = USIZE_T_MAX;
1094 for (gp = 0; gp < groups; gp += 1) 1095 for (gp = 0; gp < groups; gp += 1)
1095 { 1096 {
1096 if (gcost[gp] < best) { best = gcost[gp]; winner = gp; } 1097 if (gcost[gp] < best)
1098 {
1099 best = gcost[gp];
1100 winner = gp;
1101 }
1097 } 1102 }
1098 1103
1099 XD3_ASSERT(gbest_no < gbest_max); 1104 XD3_ASSERT(gbest_no < gbest_max);
@@ -1343,12 +1348,12 @@ djw_build_decoder (xd3_stream *stream,
1343 usize_t *min_clenp, 1348 usize_t *min_clenp,
1344 usize_t *max_clenp) 1349 usize_t *max_clenp)
1345{ 1350{
1346 int i, l; 1351 usize_t i, l;
1347 const uint8_t *ci; 1352 const uint8_t *ci;
1348 usize_t nr_clen [DJW_TOTAL_CODES]; 1353 usize_t nr_clen [DJW_TOTAL_CODES];
1349 usize_t tmp_base[DJW_TOTAL_CODES]; 1354 usize_t tmp_base[DJW_TOTAL_CODES];
1350 int min_clen; 1355 usize_t min_clen;
1351 int max_clen; 1356 usize_t max_clen;
1352 1357
1353 /* Assumption: the two temporary arrays are large enough to hold abs_max. */ 1358 /* Assumption: the two temporary arrays are large enough to hold abs_max. */
1354 XD3_ASSERT (abs_max <= DJW_MAX_CODELEN); 1359 XD3_ASSERT (abs_max <= DJW_MAX_CODELEN);
@@ -1488,7 +1493,7 @@ djw_decode_clclen (xd3_stream *stream,
1488 int ret; 1493 int ret;
1489 uint8_t cl_clen[DJW_TOTAL_CODES]; 1494 uint8_t cl_clen[DJW_TOTAL_CODES];
1490 usize_t num_codes, value; 1495 usize_t num_codes, value;
1491 int i; 1496 usize_t i;
1492 1497
1493 /* How many extra code lengths to encode. */ 1498 /* How many extra code lengths to encode. */
1494 if ((ret = xd3_decode_bits (stream, bstate, input, 1499 if ((ret = xd3_decode_bits (stream, bstate, input,
@@ -1743,7 +1748,7 @@ xd3_decode_huff (xd3_stream *stream,
1743 sel_mtf[gp] = gp; 1748 sel_mtf[gp] = gp;
1744 } 1749 }
1745 1750
1746 if ((sel_group = xd3_alloc (stream, sectors, 1)) == NULL) 1751 if ((sel_group = (uint8_t*) xd3_alloc (stream, sectors, 1)) == NULL)
1747 { 1752 {
1748 ret = ENOMEM; 1753 ret = ENOMEM;
1749 goto fail; 1754 goto fail;