From 6572ef6e1e4822f785b7139ef166586c444dc01e Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Sat, 19 Oct 2019 18:52:41 +0200 Subject: [PATCH] gzip: code shrink huft_build() still has way too many params function old new delta lit - 94 +94 dist - 94 +94 huft_build 1058 1054 -4 inflate_block 1281 1254 -27 cpdext 30 - -30 cplext 31 - -31 cpdist 60 - -60 cplens 62 - -62 ------------------------------------------------------------------------------ (add/remove: 2/4 grow/shrink: 0/2 up/down: 188/-214) Total: -26 bytes Signed-off-by: Denys Vlasenko --- archival/libarchive/decompress_gunzip.c | 55 ++++++++++++------------- 1 file changed, 26 insertions(+), 29 deletions(-) diff --git a/archival/libarchive/decompress_gunzip.c b/archival/libarchive/decompress_gunzip.c index e01687000..530661e15 100644 --- a/archival/libarchive/decompress_gunzip.c +++ b/archival/libarchive/decompress_gunzip.c @@ -184,29 +184,26 @@ static const uint16_t mask_bits[] ALIGN2 = { 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff }; -/* Copy lengths for literal codes 257..285 */ -static const uint16_t cplens[] ALIGN2 = { - 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, - 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0 +/* Put lengths/offsets and extra bits in a struct of arrays + * to make calls to huft_build() have one fewer parameter. + */ +struct cp_ext { + uint16_t cp[31]; + uint8_t ext[31]; }; - +/* Copy lengths and extra bits for literal codes 257..285 */ /* note: see note #13 above about the 258 in this list. */ -/* Extra bits for literal codes 257..285 */ -static const uint8_t cplext[] ALIGN1 = { - 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, - 5, 5, 5, 0, 99, 99 -}; /* 99 == invalid */ - -/* Copy offsets for distance codes 0..29 */ -static const uint16_t cpdist[] ALIGN2 = { - 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, - 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577 +static const struct cp_ext lit = { + /*257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 */ + /*0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 */ + { 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0 }, + { 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 99, 99 } /* 99 == invalid */ }; - -/* Extra bits for distance codes */ -static const uint8_t cpdext[] ALIGN1 = { - 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, - 11, 11, 12, 12, 13, 13 +/* Copy offsets and extra bits for distance codes 0..29 */ +static const struct cp_ext dist = { + /*0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 */ + { 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577 }, + { 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13 } }; /* Tables for deflate from PKZIP's appnote.txt. */ @@ -288,8 +285,8 @@ static unsigned fill_bitbuffer(STATE_PARAM unsigned bitbuffer, unsigned *current * result: starting table */ static huft_t* huft_build(const unsigned *b, const unsigned n, - const unsigned s, const uint16_t *d, - const uint8_t *e, unsigned *m) + const unsigned s, const struct cp_ext *cp_ext, + unsigned *m) { unsigned a; /* counter for codes of length k */ unsigned c[BMAX + 1]; /* bit length count table */ @@ -447,8 +444,8 @@ static huft_t* huft_build(const unsigned *b, const unsigned n, r.e = (unsigned char) (*p < 256 ? 16 : 15); /* 256 is EOB code */ r.v.n = (unsigned short) (*p++); /* simple code is just the value */ } else { - r.e = (unsigned char) e[*p - s]; /* non-simple--look up in lists */ - r.v.n = d[*p++ - s]; + r.e = (unsigned char) cp_ext->ext[*p - s]; /* non-simple--look up in lists */ + r.v.n = cp_ext->cp[*p++ - s]; } /* fill code-like entries with r */ @@ -777,14 +774,14 @@ static int inflate_block(STATE_PARAM smallint *e) for (; i < 288; i++) /* make a complete, but wrong code set */ ll[i] = 8; bl = 7; - inflate_codes_tl = huft_build(ll, 288, 257, cplens, cplext, &bl); + inflate_codes_tl = huft_build(ll, 288, 257, &lit, &bl); /* huft_build() never returns error here - we use known data */ /* set up distance table */ for (i = 0; i < 30; i++) /* make an incomplete code set */ ll[i] = 5; bd = 5; - inflate_codes_td = huft_build(ll, 30, 0, cpdist, cpdext, &bd); + inflate_codes_td = huft_build(ll, 30, 0, &dist, &bd); /* set up data for inflate_codes() */ inflate_codes_setup(PASS_STATE bl, bd); @@ -850,7 +847,7 @@ static int inflate_block(STATE_PARAM smallint *e) /* build decoding table for trees - single level, 7 bit lookup */ bl = 7; - inflate_codes_tl = huft_build(ll, 19, 19, NULL, NULL, &bl); + inflate_codes_tl = huft_build(ll, 19, 19, NULL, &bl); if (!inflate_codes_tl) { abort_unzip(PASS_STATE_ONLY); /* incomplete code set */ } @@ -915,12 +912,12 @@ static int inflate_block(STATE_PARAM smallint *e) /* build the decoding tables for literal/length and distance codes */ bl = lbits; - inflate_codes_tl = huft_build(ll, nl, 257, cplens, cplext, &bl); + inflate_codes_tl = huft_build(ll, nl, 257, &lit, &bl); if (!inflate_codes_tl) { abort_unzip(PASS_STATE_ONLY); } bd = dbits; - inflate_codes_td = huft_build(ll + nl, nd, 0, cpdist, cpdext, &bd); + inflate_codes_td = huft_build(ll + nl, nd, 0, &dist, &bd); if (!inflate_codes_td) { abort_unzip(PASS_STATE_ONLY); }