- unsigned char* ptr;
- unsigned long crc;
- unsigned long unclen;
- void* ptr2;
- unsigned unclen2;
-
- /* check GZIP signature */
- if (buffer[0] != 0x1f || buffer[1] != 0x8b) {
- puts("Not GZIP format");
- return 0;
- }
-
- /* check compression method (it is always (?) "deflate") */
- if (buffer[2] != 8) {
- puts("Unsupported compression method");
- return 0;
- }
-
- /* get CRC from GZIP trailer */
- crc = GET_LONG(buffer + complen - 8);
-
- /* get uncompressed length from GZIP trailer */
- unclen = GET_LONG(buffer + complen - 4);
- if (unclen > sizeof(buffer)) {
- puts("Uncompressed size too big");
- return 0;
- }
-
- /* skip extra field, file name, comment and crc */
- ptr = buffer + 10;
- if (buffer[3] & FEXTRA)
- ptr = buffer + 12 + GET_WORD(buffer + 10);
- if (buffer[3] & FNAME)
- while (*ptr++ != 0);
- if (buffer[3] & FCOMMENT)
- while (*ptr++ != 0);
- if (buffer[3] & FHCRC)
- ptr += 2;
-
- /*
- * calculate length of raw "deflate" data
- * (without the GZIP header and 8-byte trailer)
- */
- complen -= (ptr - buffer) + 8;
-
- /*
- * We will move the compressed data to the end of buffer[].
- * Thus the compressed data and the decompressed data (written from
- * the beginning of buffer[]) may overlap, as long as the decompressed
- * data doesn't go further than unread compressed data.
- * ptr2 points to the beginning of compressed data at the end
- * of buffer[].
- */
- ptr2 = buffer + sizeof(buffer) - complen;
- /* move the compressed data to end of buffer[] */
- memmove(ptr2, ptr, complen);
-
- /* uncompress */
- puts("Inflating...");
- unclen2 = inflatemem(buffer, ptr2);
-
- /* verify uncompressed length */
- if (unclen2 != (unsigned) unclen) {
- puts("Uncompressed size does not match");
- return 0;
- }
-
- /* verify CRC */
- puts("Calculating CRC...");
- if (crc32(crc32(0L, Z_NULL, 0), buffer, unclen2) != crc) {
- puts("CRC mismatch");
- return 0;
- }
-
- /* return number of uncompressed bytes */
- return unclen2;
+ unsigned char* ptr;
+ unsigned long crc;
+ unsigned long unclen;
+ void* ptr2;
+ unsigned unclen2;
+
+ /* check GZIP signature */
+ if (buffer[0] != 0x1f || buffer[1] != 0x8b) {
+ puts("Not GZIP format");
+ return 0;
+ }
+
+ /* check compression method (it is always (?) "deflate") */
+ if (buffer[2] != 8) {
+ puts("Unsupported compression method");
+ return 0;
+ }
+
+ /* get CRC from GZIP trailer */
+ crc = GET_LONG(buffer + complen - 8);
+
+ /* get uncompressed length from GZIP trailer */
+ unclen = GET_LONG(buffer + complen - 4);
+ if (unclen > sizeof(buffer)) {
+ puts("Uncompressed size too big");
+ return 0;
+ }
+
+ /* skip extra field, file name, comment and crc */
+ ptr = buffer + 10;
+ if (buffer[3] & FEXTRA)
+ ptr = buffer + 12 + GET_WORD(buffer + 10);
+ if (buffer[3] & FNAME)
+ while (*ptr++ != 0);
+ if (buffer[3] & FCOMMENT)
+ while (*ptr++ != 0);
+ if (buffer[3] & FHCRC)
+ ptr += 2;
+
+ /*
+ ** calculate length of raw "deflate" data
+ ** (without the GZIP header and 8-byte trailer)
+ */
+ complen -= (ptr - buffer) + 8;
+
+ /*
+ ** We will move the compressed data to the end of buffer[].
+ ** Thus the compressed data and the decompressed data (written from
+ ** the beginning of buffer[]) may overlap, as long as the decompressed
+ ** data doesn't go further than unread compressed data.
+ ** ptr2 points to the beginning of compressed data at the end
+ ** of buffer[].
+ */
+ ptr2 = buffer + sizeof(buffer) - complen;
+ /* move the compressed data to end of buffer[] */
+ memmove(ptr2, ptr, complen);
+
+ /* uncompress */
+ puts("Inflating...");
+ unclen2 = inflatemem(buffer, ptr2);
+
+ /* verify uncompressed length */
+ if (unclen2 != (unsigned) unclen) {
+ puts("Uncompressed size does not match");
+ return 0;
+ }
+
+ /* verify CRC */
+ puts("Calculating CRC...");
+ if (crc32(crc32(0L, Z_NULL, 0), buffer, unclen2) != crc) {
+ puts("CRC mismatch");
+ return 0;
+ }
+
+ /* return number of uncompressed bytes */
+ return unclen2;