📄 inflate.c
字号:
/* inflate that block type */ if (t == 2) return inflate_dynamic(); if (t == 0) return inflate_stored(); if (t == 1) return inflate_fixed();DEBUG_ZIP(">"); /* bad block type */ return 2;}/****************************************************************************//* FUNCTION *//* *//* inflate *//* *//* DESCRIPTION *//* decompress an inflated entry *//* *//* CALLED BY *//* *//* CALLS *//* *//* INPUTS *//* *//* OUTPUTS *//* *//* STATUS *//* *//* HISTORY *//* *//* NAME DATE REMARKS *//* *//* yskim 20-06-2000 Created initial version 1.0 *//* *//****************************************************************************/STATIC int inflate(){ int e; /* last block flag */ int r; /* result code */ unsigned h; /* maximum struct huft's malloc'ed */// void *ptr; /* initialize window, bit buffer */ wp = 0; bk = 0; bb = 0; /* decompress until the last block */ h = 0; do { hufts = 0; //gzip_mark(&ptr); if ((r = inflate_block(&e)) != 0) { //gzip_release(&ptr); DEBUG_ZIP("r=inflate_block() = %d\n",r); return r; } //gzip_release(&ptr); if (hufts > h) h = hufts;DEBUG_ZIP("block flag = %d\n", e); } while (!e);DEBUG_ZIP("last block = %d\n", e); /* Undo too mu8 lookahead. The next read will be byte aligned so we * can discard unused bits in the last meaningful byte. */ while (bk >= 8) { bk -= 8; inptr--;DEBUG_ZIP("bk = %d, inptr = %d\n", bk, (int)inptr); } /* flush out slide */ flush_output(wp); /* return success */#if DEBUG_BIOS_ZIP printf("[ERROR]: <%u> ", h);#endif /* DEBUG */ return 0;}/****************************************************************************//* FUNCTION *//* *//* makecrc *//* *//* DESCRIPTION *//* Code to compute the CRC-32 table. Borrowed from *//* gzip-1.0.3/makecrc.c *//* *//* *//* CALLED BY *//* *//* CALLS *//* *//* INPUTS *//* *//* OUTPUTS *//* *//* STATUS *//* *//* HISTORY *//* *//* NAME DATE REMARKS *//* *//* yskim 20-06-2000 Created initial version 1.0 *//* *//****************************************************************************/static void makecrc(void){ /* Not copyrighted 1990 Mark Adler */ unsigned long c; /* crc shift register */ unsigned long e; /* polynomial exclusive-or pattern */ int i; /* counter for all possible eight bit values */ int k; /* byte being shifted into crc apparatus */ /* terms of polynomial defining this crc (except x^32): */ static int p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26}; /* Make exclusive-or pattern from polynomial */ e = 0; for (i = 0; i < sizeof(p)/sizeof(int); i++) e |= 1L << (31 - p[i]); crc_32_tab[0] = 0; for (i = 1; i < 256; i++) { c = 0; for (k = i | 256; k != 1; k >>= 1) { c = c & 1 ? (c >> 1) ^ e : c >> 1; if (k & 1) c ^= e; } crc_32_tab[i] = c; }}/****************************************************************************//* FUNCTION *//* *//* gunzip *//* *//* DESCRIPTION *//* Do the uncompression *//* *//* CALLED BY *//* *//* CALLS *//* *//* INPUTS *//* *//* OUTPUTS *//* *//* STATUS *//* *//* HISTORY *//* *//* NAME DATE REMARKS *//* *//* yskim 20-06-2000 Created initial version 1.0 *//* *//****************************************************************************/static int gunzip(void){ u8 flags; unsigned char magic[2]; /* magic header */ char method; u32 orig_crc; /* original crc */ u32 orig_len; /* original uncompressed length */ int res; orig_crc = 0; orig_len = 0; magic[0] = (unsigned char)get_byte(); magic[1] = (unsigned char)get_byte(); method = (unsigned char)get_byte(); if (magic[0] != 037 || ((magic[1] != 0213) && (magic[1] != 0236))) { printf("magic[0]=%x, magic[1]=%x\n", magic[0], magic[1]); error("bad gzip magic numbers"); return -1; } /* We only support method #8, DEFLATED */ if (method != 8) { error("internal error, invalid method"); return -1; } flags = (u8)get_byte(); if ((flags & ENCRYPTED) != 0) { error("Input is encrypted\n"); return -1; } if ((flags & CONTINUATION) != 0) { error("Multi part input\n"); return -1; } if ((flags & RESERVED) != 0) { error("Input has invalid flags\n"); return -1; } (void)get_byte(); /* Get timestamp */ (void)((u32)get_byte() << 8); (void)((u32)get_byte() << 16); (void)((u32)get_byte() << 24); (void)get_byte(); /* Ignore extra flags for the moment */ (void)get_byte(); /* Ignore OS type for the moment */ if ((flags & EXTRA_FIELD) != 0) { unsigned len = (unsigned)get_byte(); len |= ((unsigned)get_byte())<<8; while (len--) (void)get_byte(); } /* Get original file name if it was truncated */ if ((flags & ORIG_NAME) != 0) { /* Discard the old name */ while (get_byte() != 0) /* null */ ; } /* Discard file comment if any */ if ((flags & COMMENT) != 0) { while (get_byte() != 0) /* null */ ; } /* Decompress */ if ((res = inflate())) { switch (res) { case 0: break; case 1: error("invalid compressed format (err=1)"); break; case 2: error("invalid compressed format (err=2)"); break; default: error("invalid compressed format (other)"); } return -1; } /* Get the crc and original length */ /* crc32 (see algorithm.doc) * uncompressed input size modulo 2^32 */ orig_crc = (u32) get_byte(); orig_crc |= (u32) get_byte() << 8; orig_crc |= (u32) get_byte() << 16; orig_crc |= (u32) get_byte() << 24; orig_len = (u32)(get_byte()); orig_len |= (u32)(get_byte() << 8); orig_len |= (u32)(get_byte() << 16);#if 0 /* 06-04-2001 khjung */ orig_len |= (u32)(get_byte() << 24);#endif /* Validate decompression */ if (orig_crc != CRC_VALUE) { error("crc error"); return -1; } if (orig_len != bytes_out) { error("length error"); return -1; } return 0;}u32 decompress_kernel(u32 decomp_base, u32 gzip_base, u32 gzip_end){ output_data = (u8 *)decomp_base; /* base of decompressed image */ free_mem_ptr = gzip_base; /* base of compressed image */ free_mem_ptr_end = gzip_end; /* end of compressed image */ crc = (u32)0xffffffffL; /* shift register contents */ DEBUG_ZIP("_kernel(): output_data = 0x%x\n", (int)output_data); DEBUG_ZIP("_kernel(): free_mem_ptr = 0x%x\n", (int)free_mem_ptr); DEBUG_ZIP("_kernel(): free_mem_end = 0x%x\n", (int)free_mem_ptr_end); inbuf = (u8 *)free_mem_ptr; inptr = 0; makecrc(); printf("Uncompressing utlinux..."); gunzip(); return output_ptr;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -