📄 mszip.c
字号:
ZIPNEEDBITS(e) } while ((e = (t = t->v.t + ((uint32_t)b & Zipmask[e]))->e) > 16); ZIPDUMPBITS(t->b) if (w >= CAB_BLOCKMAX) break; if (e == 16) /* then it's a literal */ CAB(outbuf)[w++] = (uint8_t)t->v.n; else /* it's an EOB or a length */ { /* exit if end of block */ if(e == 15) break; /* get length of block to copy */ ZIPNEEDBITS(e) n = t->v.n + ((uint32_t)b & Zipmask[e]); ZIPDUMPBITS(e); /* decode distance of block to copy */ ZIPNEEDBITS((uint32_t)bd) if ((e = (t = td + ((uint32_t)b & md))->e) > 16) do { if (e == 99) return 1; ZIPDUMPBITS(t->b) e -= 16; ZIPNEEDBITS(e) } while ((e = (t = t->v.t + ((uint32_t)b & Zipmask[e]))->e) > 16); ZIPDUMPBITS(t->b) ZIPNEEDBITS(e) d = w - t->v.n - ((uint32_t)b & Zipmask[e]); ZIPDUMPBITS(e) do { n -= (e = (e = ZIPWSIZE - ((d &= ZIPWSIZE-1) > w ? d : w)) > n ?n:e); do { CAB(outbuf)[w++] = CAB(outbuf)[d++]; } while (--e); } while (n); } } /* restore the globals from the locals */ ZIP(window_posn) = w; /* restore global window pointer */ ZIP(bb) = b; /* restore global bit buffer */ ZIP(bk) = k; /* done */ return 0;}/* "decompress" an inflated type 0 (stored) block. */static int32_t Zipinflate_stored(struct decomp_state *decomp_state){ uint32_t n; /* number of bytes in block */ uint32_t w; /* current window position */ register uint32_t b; /* bit buffer */ register uint32_t k; /* number of bits in bit buffer */ /* make local copies of globals */ b = ZIP(bb); /* initialize bit buffer */ k = ZIP(bk); w = ZIP(window_posn); /* initialize window position */ /* go to byte boundary */ n = k & 7; ZIPDUMPBITS(n); /* get the length and its complement */ ZIPNEEDBITS(16) n = ((uint32_t)b & 0xffff); ZIPDUMPBITS(16) ZIPNEEDBITS(16) if (n != (uint32_t)((~b) & 0xffff)) return 1; /* error in compressed data */ ZIPDUMPBITS(16) /* read and output the compressed data */ while(n--) { ZIPNEEDBITS(8) CAB(outbuf)[w++] = (uint8_t)b; ZIPDUMPBITS(8) } /* restore the globals from the locals */ ZIP(window_posn) = w; /* restore global window pointer */ ZIP(bb) = b; /* restore global bit buffer */ ZIP(bk) = k; return 0;}static int32_t Zipinflate_fixed(struct decomp_state *decomp_state){ struct Ziphuft *fixed_tl; struct Ziphuft *fixed_td; int32_t fixed_bl, fixed_bd; int32_t i; /* temporary variable */ uint32_t *l; l = ZIP(ll); /* literal table */ for(i = 0; i < 144; i++) l[i] = 8; for(; i < 256; i++) l[i] = 9; for(; i < 280; i++) l[i] = 7; for(; i < 288; i++) /* make a complete, but wrong code set */ l[i] = 8; fixed_bl = 7; if((i = Ziphuft_build(decomp_state, l, 288, 257, Zipcplens, Zipcplext, &fixed_tl, &fixed_bl))) return i; /* distance table */ for(i = 0; i < 30; i++) /* make an incomplete code set */ l[i] = 5; fixed_bd = 5; if((i = Ziphuft_build(decomp_state, l, 30, 0, Zipcpdist, Zipcpdext, &fixed_td, &fixed_bd)) > 1) { Ziphuft_free(fixed_tl); return i; } /* decompress until an end-of-block code */ i = Zipinflate_codes(decomp_state, fixed_tl, fixed_td, fixed_bl, fixed_bd); Ziphuft_free(fixed_td); Ziphuft_free(fixed_tl); return i;}/* decompress an inflated type 2 (dynamic Huffman codes) block. */static int32_t Zipinflate_dynamic(struct decomp_state *decomp_state){ int32_t i; /* temporary variables */ uint32_t j; uint32_t *ll; uint32_t l; /* last length */ uint32_t m; /* mask for bit lengths table */ uint32_t n; /* number of lengths to get */ struct Ziphuft *tl; /* literal/length code table */ struct Ziphuft *td; /* distance code table */ int32_t bl; /* lookup bits for tl */ int32_t bd; /* lookup bits for td */ uint32_t nb; /* number of bit length codes */ uint32_t nl; /* number of literal/length codes */ uint32_t nd; /* number of distance codes */ register uint32_t b; /* bit buffer */ register uint32_t k; /* number of bits in bit buffer */ /* make local bit buffer */ b = ZIP(bb); k = ZIP(bk); ll = ZIP(ll); /* read in table lengths */ ZIPNEEDBITS(5) nl = 257 + ((uint32_t)b & 0x1f); /* number of literal/length codes */ ZIPDUMPBITS(5) ZIPNEEDBITS(5) nd = 1 + ((uint32_t)b & 0x1f); /* number of distance codes */ ZIPDUMPBITS(5) ZIPNEEDBITS(4) nb = 4 + ((uint32_t)b & 0xf); /* number of bit length codes */ ZIPDUMPBITS(4) if(nl > 288 || nd > 32) return 1; /* bad lengths */ /* read in bit-length-code lengths */ for(j = 0; j < nb; j++) { ZIPNEEDBITS(3) ll[Zipborder[j]] = (uint32_t)b & 7; ZIPDUMPBITS(3) } for(; j < 19; j++) ll[Zipborder[j]] = 0; /* build decoding table for trees--single level, 7 bit lookup */ bl = 7; if((i = Ziphuft_build(decomp_state, ll, 19, 19, NULL, NULL, &tl, &bl)) != 0) { if(i == 1) Ziphuft_free(tl); return i; /* incomplete code set */ } /* read in literal and distance code lengths */ n = nl + nd; m = Zipmask[bl]; i = l = 0; while((uint32_t)i < n) { ZIPNEEDBITS((uint32_t)bl) j = (td = tl + ((uint32_t)b & m))->b; ZIPDUMPBITS(j) j = td->v.n; if (j < 16) /* length of code in bits (0..15) */ ll[i++] = l = j; /* save last length in l */ else if (j == 16) /* repeat last length 3 to 6 times */ { ZIPNEEDBITS(2) j = 3 + ((uint32_t)b & 3); ZIPDUMPBITS(2) if((uint32_t)i + j > n) return 1; while (j--) ll[i++] = l; } else if (j == 17) /* 3 to 10 zero length codes */ { ZIPNEEDBITS(3) j = 3 + ((uint32_t)b & 7); ZIPDUMPBITS(3) if ((uint32_t)i + j > n) return 1; while (j--) ll[i++] = 0; l = 0; } else /* j == 18: 11 to 138 zero length codes */ { ZIPNEEDBITS(7) j = 11 + ((uint32_t)b & 0x7f); ZIPDUMPBITS(7) if ((uint32_t)i + j > n) return 1; while (j--) ll[i++] = 0; l = 0; } } /* free decoding table for trees */ Ziphuft_free(tl); /* restore the global bit buffer */ ZIP(bb) = b; ZIP(bk) = k; /* build the decoding tables for literal/length and distance codes */ bl = ZIPLBITS; if((i = Ziphuft_build(decomp_state, ll, nl, 257, Zipcplens, Zipcplext, &tl, &bl)) != 0) { if(i == 1) Ziphuft_free(tl); return i; /* incomplete code set */ } bd = ZIPDBITS; Ziphuft_build(decomp_state, ll + nl, nd, 0, Zipcpdist, Zipcpdext, &td, &bd); /* decompress until an end-of-block code */ if(Zipinflate_codes(decomp_state, tl, td, bl, bd)) return 1; /* free the decoding tables, return */ Ziphuft_free(tl); Ziphuft_free(td); return 0;}/* e == last block flag */static int32_t Zipinflate_block(struct decomp_state *decomp_state, int32_t *e){ /* decompress an inflated block */ uint32_t t; /* block type */ register uint32_t b; /* bit buffer */ register uint32_t k; /* number of bits in bit buffer */ DEBUG(10,("Zipinflate_block\n")); /* make local bit buffer */ b = ZIP(bb); k = ZIP(bk); /* read in last block bit */ ZIPNEEDBITS(1) *e = (int32_t)b & 1; ZIPDUMPBITS(1) /* read in block type */ ZIPNEEDBITS(2) t = (uint32_t)b & 3; ZIPDUMPBITS(2) /* restore the global bit buffer */ ZIP(bb) = b; ZIP(bk) = k; DEBUG(10,("inflate type %d\n", t)); /* inflate that block type */ if(t == 2) return Zipinflate_dynamic(decomp_state); if(t == 0) return Zipinflate_stored(decomp_state); if(t == 1) return Zipinflate_fixed(decomp_state); /* bad block type */ return 2;}_PUBLIC_ struct decomp_state *ZIPdecomp_state(TALLOC_CTX *mem_ctx){ return talloc_zero(mem_ctx, struct decomp_state);}int ZIPdecompress(struct decomp_state *decomp_state, DATA_BLOB *inbuf, DATA_BLOB *outbuf){ int32_t e = 0;/* last block flag */ ZIP(inpos) = CAB(inbuf); ZIP(bb) = ZIP(bk) = ZIP(window_posn) = 0; if (inbuf->length > sizeof(decomp_state->inbuf)) return DECR_INPUT; if (outbuf->length > sizeof(decomp_state->outbuf)) return DECR_OUTPUT; if (outbuf->length > ZIPWSIZE) return DECR_DATAFORMAT; memcpy(decomp_state->inbuf, inbuf->data, inbuf->length); /* CK = Chris Kirmse, official Microsoft purloiner */ if (ZIP(inpos)[0] != 'C' || ZIP(inpos)[1] != 'K') return DECR_ILLEGALDATA; ZIP(inpos) += 2; while (!e) { if (Zipinflate_block(decomp_state, &e)) { return DECR_ILLEGALDATA; } } memcpy(outbuf->data, decomp_state->outbuf, outbuf->length); return DECR_OK;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -