/* gunzip.c * PUBLIC DOMAIN - Jon Mayo - August 30, 2006 * dumps the Deflate stream from a GZIP stream (RFC 1951) */ /* TODO: * accumulate crc16 for the header (FHCRC) */ #include #include #define FTEXT 1 #define FHCRC 2 #define FEXTRA 4 #define FNAME 8 #define FCOMMENT 16 int main() { unsigned char header[10]; unsigned char buf[333]; int buflen; if(fread(header, sizeof header, 1, stdin)!=1) { perror("header"); return EXIT_FAILURE; } if(header[0]!=31 && header[1]!=139) { fprintf(stderr, "Not a GZIP\n"); return EXIT_FAILURE; } if(header[3] & FEXTRA) { unsigned char len_data[2]; unsigned char *data; unsigned fextra_len; if(fread(len_data, sizeof len_data, 1, stdin)!=1) { perror("fextra"); return EXIT_FAILURE; } fextra_len=len_data[0] | len_data[1]<<8; data=malloc(fextra_len); if(fread(data, fextra_len, 1, stdin)!=1) { perror("fextra"); return EXIT_FAILURE; } free(data); fprintf(stderr, "FEXTRA %d\n", fextra_len); } if(header[3] & FNAME) { unsigned char name[64]; int i; int ch; int fname_len=0; for(i=0;i0) { fwrite(buf, 1, buflen, stdout); } if(buflen<0) { perror("fread()"); return EXIT_FAILURE; } return 0; }