2 * gunzip65 - a gunzip utility for 6502-based machines.
4 * Piotr Fusik <fox@scene.pl>
6 * This should be considered as a test of my zlib-compatible library
7 * rather than a real application.
8 * It's not user-friendly, fault-tolerant, whatever.
9 * However, it really works for real GZIP files, provided they are small
10 * enough to fit in buffer[] (after decompression!).
19 * Emulate inflatemem() if using original zlib.
20 * As you can see, this program is quite portable.
22 unsigned inflatemem(char* dest, const char* source)
26 stream.next_in = (Bytef*) source;
27 stream.avail_in = 65535;
29 stream.next_out = dest;
30 stream.avail_out = 65535;
32 stream.zalloc = (alloc_func) 0;
33 stream.zfree = (free_func) 0;
35 inflateInit2(&stream, -MAX_WBITS);
36 inflate(&stream, Z_FINISH);
39 return stream.total_out;
44 * Structure of a GZIP file:
47 * Offset 0: Signature (2 bytes: 0x1f, 0x8b)
48 * Offset 2: Compression method (1 byte: 8 == "deflate")
49 * Offset 3: Flags (1 byte: see below)
50 * Offset 4: File date and time (4 bytes)
51 * Offset 8: Extra flags (1 byte)
52 * Offset 9: Target OS (1 byte: DOS, Amiga, Unix, etc.)
53 * if (flags & FEXTRA) { 2 bytes of length, then length bytes }
54 * if (flags & FNAME) { ASCIIZ filename }
55 * if (flags & FCOMMENT) { ASCIIZ comment }
56 * if (flags & FHCRC) { 2 bytes of CRC }
58 * 2. Deflate compressed data.
61 * Offset 0: CRC-32 (4 bytes)
62 * Offset 4: uncompressed file length (4 bytes)
65 /* Flags in the GZIP header. */
66 #define FTEXT 1 /* Extra text */
67 #define FHCRC 2 /* Header CRC */
68 #define FEXTRA 4 /* Extra field */
69 #define FNAME 8 /* File name */
70 #define FCOMMENT 16 /* File comment */
73 * We read whole GZIP file into this buffer.
74 * Then we use this buffer for the decompressed data.
76 static unsigned char buffer[26000];
79 * Get a 16-bit little-endian unsigned number, using unsigned char* p.
80 * On many machines this could be (*(unsigned short*) p),
81 * but I really like portability. :-)
83 #define GET_WORD(p) (*(p) + ((unsigned) (p)[1] << 8))
85 /* Likewise, for a 32-bit number. */
86 #define GET_LONG(p) (GET_WORD(p) + ((unsigned long) GET_WORD(p + 2) << 16))
89 * Uncompress a GZIP file.
90 * On entry, buffer[] should contain the whole GZIP file contents,
91 * and the argument complen should be equal to the length of the GZIP file.
92 * On return, buffer[] contains the uncompressed data, and the returned
93 * value is the length of the uncompressed data.
95 unsigned uncompress_buffer(unsigned complen)
103 /* check GZIP signature */
104 if (buffer[0] != 0x1f || buffer[1] != 0x8b) {
105 puts("Not GZIP format");
109 /* check compression method (it is always (?) "deflate") */
110 if (buffer[2] != 8) {
111 puts("Unsupported compression method");
115 /* get CRC from GZIP trailer */
116 crc = GET_LONG(buffer + complen - 8);
118 /* get uncompressed length from GZIP trailer */
119 unclen = GET_LONG(buffer + complen - 4);
120 if (unclen > sizeof(buffer)) {
121 puts("Uncompressed size too big");
125 /* skip extra field, file name, comment and crc */
127 if (buffer[3] & FEXTRA)
128 ptr = buffer + 12 + GET_WORD(buffer + 10);
129 if (buffer[3] & FNAME)
131 if (buffer[3] & FCOMMENT)
133 if (buffer[3] & FHCRC)
137 * calculate length of raw "deflate" data
138 * (without the GZIP header and 8-byte trailer)
140 complen -= (ptr - buffer) + 8;
143 * We will move the compressed data to the end of buffer[].
144 * Thus the compressed data and the decompressed data (written from
145 * the beginning of buffer[]) may overlap, as long as the decompressed
146 * data doesn't go further than unread compressed data.
147 * ptr2 points to the beginning of compressed data at the end
150 ptr2 = buffer + sizeof(buffer) - complen;
151 /* move the compressed data to end of buffer[] */
152 memmove(ptr2, ptr, complen);
155 puts("Inflating...");
156 unclen2 = inflatemem(buffer, ptr2);
158 /* verify uncompressed length */
159 if (unclen2 != (unsigned) unclen) {
160 puts("Uncompressed size does not match");
165 puts("Calculating CRC...");
166 if (crc32(crc32(0L, Z_NULL, 0), buffer, unclen2) != crc) {
167 puts("CRC mismatch");
171 /* return number of uncompressed bytes */
176 * Get a filename from standard input.
178 char* get_fname(void)
180 static char filename[100];
182 fgets(filename, sizeof(filename), stdin);
183 len = strlen(filename);
184 if (len >= 1 && filename[len - 1] == '\n')
185 filename[len - 1] = '\0';
195 puts("GZIP file name:");
196 fp = fopen(get_fname(), "rb");
198 puts("Can't open GZIP file");
201 length = fread(buffer, 1, sizeof(buffer), fp);
203 if (length == sizeof(buffer)) {
204 puts("File is too long");
209 length = uncompress_buffer(length);
213 /* write uncompressed file */
214 puts("Uncompressed file name:");
215 fp = fopen(get_fname(), "wb");
217 puts("Can't create output file");
220 if (fwrite(buffer, 1, length, fp) != length) {
221 puts("Error while writing output file");