From: Mark Adler Date: Wed, 12 Oct 2016 05:15:50 +0000 (-0700) Subject: Clean up type conversions. X-Git-Tag: 1.9.9-b1~741 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f9bb580bcba2e94eb1c931d0d8d9146d86704557;p=thirdparty%2Fzlib-ng.git Clean up type conversions. Based on upstream 7096424f23df1b1813237fb5f8bc8f34cfcedd0c, but modified heavily to match zlib-ng. --- diff --git a/deflate.c b/deflate.c index d3661c48a..c9a220bee 100644 --- a/deflate.c +++ b/deflate.c @@ -80,7 +80,7 @@ static block_state deflate_huff (deflate_state *s, int flush); static void lm_init (deflate_state *s); static void putShortMSB (deflate_state *s, uint16_t b); ZLIB_INTERNAL void flush_pending (z_stream *strm); -ZLIB_INTERNAL int read_buf (z_stream *strm, unsigned char *buf, unsigned size); +ZLIB_INTERNAL unsigned read_buf (z_stream *strm, unsigned char *buf, unsigned size); extern void crc_reset(deflate_state *const s); extern void crc_finalize(deflate_state *const s); @@ -223,16 +223,16 @@ int ZEXPORT deflateInit2_(z_stream *strm, int level, int method, int windowBits, s->wrap = wrap; s->gzhead = NULL; - s->w_bits = windowBits; + s->w_bits = (unsigned int)windowBits; s->w_size = 1 << s->w_bits; s->w_mask = s->w_size - 1; #ifdef X86_SSE4_2_CRC_HASH if (x86_cpu_has_sse42) - s->hash_bits = 15; + s->hash_bits = (unsigned int)15; else #endif - s->hash_bits = memLevel + 7; + s->hash_bits = (unsigned int)memLevel + 7; s->hash_size = 1 << s->hash_bits; s->hash_mask = s->hash_size - 1; @@ -456,10 +456,10 @@ int ZEXPORT deflateTune(z_stream *strm, int good_length, int max_lazy, int nice_ if (strm == NULL || strm->state == NULL) return Z_STREAM_ERROR; s = strm->state; - s->good_match = good_length; - s->max_lazy_match = max_lazy; + s->good_match = (unsigned int)good_length; + s->max_lazy_match = (unsigned int)max_lazy; s->nice_match = nice_length; - s->max_chain_length = max_chain; + s->max_chain_length = (unsigned int)max_chain; return Z_OK; } @@ -967,7 +967,7 @@ int ZEXPORT deflateCopy(z_stream *dest, z_stream *source) { * allocating a large strm->next_in buffer and copying from it. * (See also flush_pending()). */ -ZLIB_INTERNAL int read_buf(z_stream *strm, unsigned char *buf, unsigned size) { +ZLIB_INTERNAL unsigned read_buf(z_stream *strm, unsigned char *buf, unsigned size) { uint32_t len = strm->avail_in; if (len > size) @@ -990,7 +990,7 @@ ZLIB_INTERNAL int read_buf(z_stream *strm, unsigned char *buf, unsigned size) { strm->next_in += len; strm->total_in += len; - return (int)len; + return len; } /* =========================================================================== @@ -1270,7 +1270,7 @@ static block_state deflate_stored(deflate_state *s, int flush) { s->lookahead = 0; /* Emit a stored block if pending_buf will be full: */ - max_start = s->block_start + max_block_size; + max_start = max_block_size + (unsigned long)s->block_start; if (s->strstart == 0 || (unsigned long)s->strstart >= max_start) { /* strstart == 0 is possible when wraparound on 16-bit machine */ s->lookahead = (unsigned int)(s->strstart - max_start); @@ -1332,7 +1332,7 @@ static block_state deflate_rle(deflate_state *s, int flush) { prev == *++scan && prev == *++scan && prev == *++scan && prev == *++scan && scan < strend); - s->match_length = MAX_MATCH - (int)(strend - scan); + s->match_length = MAX_MATCH - (unsigned int)(strend - scan); if (s->match_length > s->lookahead) s->match_length = s->lookahead; } diff --git a/deflate.h b/deflate.h index 3711d0ba2..5d122d803 100644 --- a/deflate.h +++ b/deflate.h @@ -290,7 +290,7 @@ typedef enum { /* Output a byte on the stream. * IN assertion: there is enough room in pending_buf. */ -#define put_byte(s, c) {s->pending_buf[s->pending++] = (c);} +#define put_byte(s, c) {s->pending_buf[s->pending++] = (unsigned char)(c);} /* =========================================================================== * Output a short LSB first on the stream. @@ -365,8 +365,8 @@ void ZLIB_INTERNAL bi_windup(deflate_state *s); flush = (s->last_lit == s->lit_bufsize-1); \ } # define _tr_tally_dist(s, distance, length, flush) \ - { unsigned char len = (length); \ - uint16_t dist = (distance); \ + { unsigned char len = (unsigned char)(length); \ + uint16_t dist = (uint16_t)(distance); \ s->d_buf[s->last_lit] = dist; \ s->l_buf[s->last_lit++] = len; \ dist--; \ @@ -429,7 +429,7 @@ void send_bits(deflate_state *s, int value, int length); #define send_bits(s, value, length) \ { int len = length;\ if (s->bi_valid > (int)Buf_size - len) {\ - int val = value;\ + int val = (int)value;\ s->bi_buf |= (uint16_t)val << s->bi_valid;\ put_short(s, s->bi_buf);\ s->bi_buf = (uint16_t)val >> (Buf_size - s->bi_valid);\ diff --git a/gzread.c b/gzread.c index 970b64917..1b1417ca2 100644 --- a/gzread.c +++ b/gzread.c @@ -18,14 +18,14 @@ static int gz_skip(gz_statep, z_off64_t); This function needs to loop on read(), since read() is not guaranteed to read the number of bytes requested, depending on the type of descriptor. */ static int gz_load(gz_statep state, unsigned char *buf, unsigned len, unsigned *have) { - int ret; + ssize_t ret; *have = 0; do { ret = read(state->fd, buf + *have, len - *have); if (ret <= 0) break; - *have += ret; + *have += (unsigned)ret; } while (*have < len); if (ret < 0) { gz_error(state, Z_ERRNO, zstrerror()); @@ -402,7 +402,7 @@ int ZEXPORT gzungetc(int c, gzFile file) { if (state->x.have == 0) { state->x.have = 1; state->x.next = state->out + (state->size << 1) - 1; - state->x.next[0] = c; + state->x.next[0] = (unsigned char)c; state->x.pos--; state->past = 0; return c; @@ -424,7 +424,7 @@ int ZEXPORT gzungetc(int c, gzFile file) { } state->x.have++; state->x.next--; - state->x.next[0] = c; + state->x.next[0] = (unsigned char)c; state->x.pos--; state->past = 0; return c; diff --git a/gzwrite.c b/gzwrite.c index e4860342a..6f632f173 100644 --- a/gzwrite.c +++ b/gzwrite.c @@ -67,7 +67,8 @@ static int gz_init(gz_statep state) { is true, then simply write to the output file without compressing, and ignore flush. */ static int gz_comp(gz_statep state, int flush) { - int ret, got; + int ret; + ssize_t got; unsigned have; z_stream *strm = &(state->strm); @@ -93,7 +94,7 @@ static int gz_comp(gz_statep state, int flush) { doing Z_FINISH then don't write until we get to Z_STREAM_END */ if (strm->avail_out == 0 || (flush != Z_NO_FLUSH && (flush != Z_FINISH || ret == Z_STREAM_END))) { have = (unsigned)(strm->next_out - state->x.next); - if (have && ((got = write(state->fd, state->x.next, have)) < 0 || (unsigned)got != have)) { + if (have && ((got = write(state->fd, state->x.next, (unsigned long)have)) < 0 || (unsigned)got != have)) { gz_error(state, Z_ERRNO, zstrerror()); return -1; } @@ -256,7 +257,7 @@ int ZEXPORT gzputc(gzFile file, int c) { strm->next_in = state->in; have = (unsigned)((strm->next_in + strm->avail_in) - state->in); if (have < state->size) { - state->in[have] = c; + state->in[have] = (unsigned char)c; strm->avail_in++; state->x.pos++; return c & 0xff; @@ -264,7 +265,7 @@ int ZEXPORT gzputc(gzFile file, int c) { } /* no room in buffer or not initialized, use gz_write() */ - buf[0] = c; + buf[0] = (unsigned char)c; if (gzwrite(file, buf, 1) != 1) return -1; return c & 0xff; @@ -283,7 +284,8 @@ int ZEXPORT gzputs(gzFile file, const char *str) { /* -- see zlib.h -- */ int ZEXPORTVA gzvprintf(gzFile file, const char *format, va_list va) { - int len, left; + int len; + unsigned left; char *next; gz_statep state; z_stream *strm; @@ -314,16 +316,16 @@ int ZEXPORTVA gzvprintf(gzFile file, const char *format, va_list va) { be state->size bytes available after the current contents */ if (strm->avail_in == 0) strm->next_in = state->in; - next = (char *)(strm->next_in + strm->avail_in); + next = (char *)(state->in + (strm->next_in - state->in) + strm->avail_in); next[state->size - 1] = 0; len = vsnprintf(next, state->size, format, va); /* check that printf() results fit in buffer */ - if (len == 0 || len >= state->size || next[state->size - 1] != 0) + if (len == 0 || (unsigned)len >= state->size || next[state->size - 1] != 0) return 0; /* update buffer and position, compress first half if past that */ - strm->avail_in += len; + strm->avail_in += (unsigned)len; state->x.pos += len; if (strm->avail_in >= state->size) { left = strm->avail_in - state->size; @@ -334,7 +336,7 @@ int ZEXPORTVA gzvprintf(gzFile file, const char *format, va_list va) { strm->next_in = state->in; strm->avail_in = left; } - return (int)len; + return len; } int ZEXPORTVA gzprintf(gzFile file, const char *format, ...) { diff --git a/infback.c b/infback.c index 4fc783fd3..49c90cc16 100644 --- a/infback.c +++ b/infback.c @@ -46,7 +46,7 @@ int ZEXPORT inflateBackInit_(z_stream *strm, int windowBits, unsigned char *wind Tracev((stderr, "inflate: allocated\n")); strm->state = (struct internal_state *)state; state->dmax = 32768U; - state->wbits = windowBits; + state->wbits = (uInt)windowBits; state->wsize = 1U << windowBits; state->window = window; state->wnext = 0; diff --git a/inflate.c b/inflate.c index 17c7239d6..1f715db56 100644 --- a/inflate.c +++ b/inflate.c @@ -214,11 +214,11 @@ int ZEXPORT inflatePrime(z_stream *strm, int bits, int value) { state->bits = 0; return Z_OK; } - if (bits > 16 || state->bits + bits > 32) + if (bits > 16 || state->bits + (unsigned int)bits > 32) return Z_STREAM_ERROR; value &= (1L << bits) - 1; state->hold += (unsigned)value << state->bits; - state->bits += bits; + state->bits += (unsigned int)bits; return Z_OK; } @@ -735,7 +735,7 @@ int ZEXPORT inflate(z_stream *strm, int flush) { do { len = (unsigned)(next[copy++]); if (state->head != NULL && state->head->name != NULL && state->length < state->head->name_max) - state->head->name[state->length++] = len; + state->head->name[state->length++] = (unsigned char)len; } while (len && copy < have); if ((state->flags & 0x0200) && (state->wrap & 4)) state->check = crc32(state->check, next, copy); @@ -756,7 +756,7 @@ int ZEXPORT inflate(z_stream *strm, int flush) { len = (unsigned)(next[copy++]); if (state->head != NULL && state->head->comment != NULL && state->length < state->head->comm_max) - state->head->comment[state->length++] = len; + state->head->comment[state->length++] = (unsigned char)len; } while (len && copy < have); if ((state->flags & 0x0200) && (state->wrap & 4)) state->check = crc32(state->check, next, copy); @@ -1219,7 +1219,7 @@ int ZEXPORT inflate(z_stream *strm, int flush) { state->total += out; if ((state->wrap & 4) && out) strm->adler = state->check = UPDATE(state->check, strm->next_out - out, out); - strm->data_type = state->bits + (state->last ? 64 : 0) + + strm->data_type = (int)state->bits + (state->last ? 64 : 0) + (state->mode == TYPE ? 128 : 0) + (state->mode == LEN_ || state->mode == COPY_ ? 256 : 0); if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK) ret = Z_BUF_ERROR; @@ -1451,6 +1451,7 @@ int ZEXPORT inflateUndermine(z_stream *strm, int subvert) { state->sane = !subvert; return Z_OK; #else + (void)subvert; state->sane = 1; return Z_DATA_ERROR; #endif diff --git a/trees.c b/trees.c index 40b0428c7..a0a7d89c8 100644 --- a/trees.c +++ b/trees.c @@ -442,9 +442,9 @@ static void gen_bitlen(deflate_state *s, tree_desc *desc) { if (n >= base) xbits = extra[n-base]; f = tree[n].Freq; - s->opt_len += (unsigned long)f * (bits + xbits); + s->opt_len += (unsigned long)f * (unsigned int)(bits + xbits); if (stree) - s->static_len += (unsigned long)f * (stree[n].Len + xbits); + s->static_len += (unsigned long)f * (unsigned int)(stree[n].Len + xbits); } if (overflow == 0) return; @@ -479,7 +479,7 @@ static void gen_bitlen(deflate_state *s, tree_desc *desc) { continue; if (tree[m].Len != bits) { Trace((stderr, "code %d bits %d->%u\n", m, tree[m].Len, bits)); - s->opt_len += (long)((bits - tree[m].Len) * tree[m].Freq); + s->opt_len += (unsigned long)((bits - tree[m].Len) * tree[m].Freq); tree[m].Len = (uint16_t)bits; } n--; @@ -500,7 +500,7 @@ static void gen_codes(ct_data *tree, int max_code, uint16_t *bl_count) { /* max_code: largest code with non zero frequency */ /* bl_count: number of codes at each bit length */ uint16_t next_code[MAX_BITS+1]; /* next code value for each bit length */ - uint16_t code = 0; /* running code value */ + unsigned int code = 0; /* running code value */ int bits; /* bit index */ int n; /* code index */ @@ -508,7 +508,8 @@ static void gen_codes(ct_data *tree, int max_code, uint16_t *bl_count) { * without bit reversal. */ for (bits = 1; bits <= MAX_BITS; bits++) { - next_code[bits] = code = (code + bl_count[bits-1]) << 1; + code = (code + bl_count[bits-1]) << 1; + next_code[bits] = (uint16_t)code; } /* Check that the bit counts in bl_count are consistent. The last code * must be all ones. @@ -521,7 +522,7 @@ static void gen_codes(ct_data *tree, int max_code, uint16_t *bl_count) { if (len == 0) continue; /* Now reverse the bits */ - tree[n].Code = bi_reverse(next_code[len]++, len); + tree[n].Code = (uint16_t)bi_reverse(next_code[len]++, len); Tracecv(tree != static_ltree, (stderr, "\nn %3d %c l %2d c %4x (%x) ", n, (isgraph(n) ? n : ' '), len, tree[n].Code, next_code[len]-1)); @@ -752,7 +753,7 @@ static int build_bl_tree(deflate_state *s) { break; } /* Update opt_len to include the bit length tree and counts */ - s->opt_len += 3*(max_blindex+1) + 5+5+4; + s->opt_len += 3*((unsigned long)max_blindex+1) + 5+5+4; Tracev((stderr, "\ndyn trees: dyn %lu, stat %lu", s->opt_len, s->static_len)); return max_blindex; @@ -997,7 +998,7 @@ static void compress_block(deflate_state *s, const ct_data *ltree, const ct_data send_code(s, code, dtree); /* send the distance code */ extra = extra_dbits[code]; if (extra != 0) { - dist -= base_dist[code]; + dist -= (unsigned int)base_dist[code]; send_bits(s, dist, extra); /* send the extra distance bits */ } } /* literal or match pair ? */ diff --git a/zutil.c b/zutil.c index e46277b87..a5dc4aa42 100644 --- a/zutil.c +++ b/zutil.c @@ -11,16 +11,17 @@ #endif const char * const z_errmsg[10] = { -"need dictionary", /* Z_NEED_DICT 2 */ -"stream end", /* Z_STREAM_END 1 */ -"", /* Z_OK 0 */ -"file error", /* Z_ERRNO (-1) */ -"stream error", /* Z_STREAM_ERROR (-2) */ -"data error", /* Z_DATA_ERROR (-3) */ -"insufficient memory", /* Z_MEM_ERROR (-4) */ -"buffer error", /* Z_BUF_ERROR (-5) */ -"incompatible version",/* Z_VERSION_ERROR (-6) */ -""}; + (const char *)"need dictionary", /* Z_NEED_DICT 2 */ + (const char *)"stream end", /* Z_STREAM_END 1 */ + (const char *)"", /* Z_OK 0 */ + (const char *)"file error", /* Z_ERRNO (-1) */ + (const char *)"stream error", /* Z_STREAM_ERROR (-2) */ + (const char *)"data error", /* Z_DATA_ERROR (-3) */ + (const char *)"insufficient memory", /* Z_MEM_ERROR (-4) */ + (const char *)"buffer error", /* Z_BUF_ERROR (-5) */ + (const char *)"incompatible version",/* Z_VERSION_ERROR (-6) */ + (const char *)"" +}; const char zlibng_string[] = " zlib-ng 1.9.9 forked from zlib 1.2.8 ";