source: umon/main/zlib/gzio.c @ 87db514

Last change on this file since 87db514 was 87db514, checked in by Amar Takhar <amar@…>, on 04/16/15 at 19:26:21

Initial commit of the umon repository.

Prior to this three changes were made:

  • Remove umon_ prefix from parent directories.
  • Collapse main/target/ into main/
  • Remove ports/template/flashtest.scr.ucon script.
  • Property mode set to 100644
File size: 11.9 KB
Line 
1/* gzio.c -- IO on .gz files
2 * Copyright (C) 1995-1998 Jean-loup Gailly.
3 * For conditions of distribution and use, see copyright notice in zlib.h
4 *
5 * This has been seriously re-arranged for use within MicroMonitor.
6 * The majority of the rework is to strip out unnecessary parts because this
7 * will reside in flash space.  Also, there is no file system interface, all
8 * decompression is done from one point in memory to another point in memory.
9 */
10
11#include "config.h"
12#if INCLUDE_UNZIP
13#include "tfs.h"
14#include "cli.h"
15#include "tfsprivate.h"
16#include "genlib.h"
17#include "zutil.h"
18
19struct internal_state {int dummy;}; /* for buggy compilers */
20
21#ifndef Z_BUFSIZE
22#  ifdef MAXSEG_64K
23#    define Z_BUFSIZE 4096 /* minimize memory usage for 16-bit DOS */
24#  else
25#    define Z_BUFSIZE 16384
26#  endif
27#endif
28#ifndef Z_PRINTF_BUFSIZE
29#  define Z_PRINTF_BUFSIZE 4096
30#endif
31
32#define ALLOC(size) malloc(size)
33#define TRYFREE(p) {if (p) free((char *)p);}
34
35static int gz_magic[2] = {0x1f, 0x8b}; /* gzip magic header */
36
37/* gzip flag byte */
38#define ASCII_FLAG   0x01 /* bit 0 set: file probably ascii text */
39#define HEAD_CRC     0x02 /* bit 1 set: header CRC present */
40#define EXTRA_FIELD  0x04 /* bit 2 set: extra field present */
41#define ORIG_NAME    0x08 /* bit 3 set: original file name present */
42#define COMMENT      0x10 /* bit 4 set: file comment present */
43#define RESERVED     0xE0 /* bits 5..7: reserved */
44
45typedef struct gz_stream {
46    z_stream stream;
47    int      z_err;                     /* error code for last stream operation */
48    int      z_eof;                     /* set if end of input file */
49    Byte     *outbuf;           /* output buffer */
50    uLong    crc;                       /* crc32 of uncompressed data */
51    char     *msg;                      /* error message */
52    int      transparent;        /* 1 if input file is not a .gz file */
53    char     mode;                      /* 'w' or 'r' */
54} gz_stream;
55
56
57local int    get_byte     OF((gz_stream *s));
58local void   check_header OF((gz_stream *s));
59local int    destroy      OF((gz_stream *s));
60local uLong  getLong      OF((gz_stream *s));
61
62/* ===========================================================================
63        gzInit():
64        Rewritten (from gzopen()) to support only decompression of data in
65        memory.  The next_in member points directly to the compressed data and
66        avail_in is the number of bytes remaining.
67        Things get simpler because of the removal of the file system interface
68        as well as the "only decompression" mode.
69*/
70gz_stream *
71gzInit(unsigned char *compressed_data,int size)
72{
73    int err;
74    gz_stream *s;
75
76    s = (gz_stream *)ALLOC(sizeof(gz_stream));
77    if (!s)
78                return Z_NULL;
79
80    s->stream.zalloc = (alloc_func)0;
81    s->stream.zfree = (free_func)0;
82    s->stream.opaque = (voidpf)0;
83    s->stream.next_in = compressed_data;
84    s->stream.next_out = s->outbuf = Z_NULL;
85    s->stream.avail_in = size;
86        s->stream.avail_out = 0;
87    s->z_err = Z_OK;
88    s->z_eof = 0;
89    s->crc = zcrc32(0L, Z_NULL, 0);
90    s->msg = NULL;
91    s->transparent = 0;
92    s->mode = 'r';
93
94        err = inflateInit2(&(s->stream), -MAX_WBITS);
95        /* windowBits is passed < 0 to tell that there is no zlib header.
96         * Note that in this case inflate *requires* an extra "dummy" byte
97         * after the compressed stream in order to complete decompression and
98         * return Z_STREAM_END. Here the gzip CRC32 ensures that 4 bytes are
99         * present after the compressed stream.
100         */
101        if (err != Z_OK) {
102                destroy(s);
103                return((gz_stream *)Z_NULL);
104        }
105       
106    s->stream.avail_out = Z_BUFSIZE;
107        check_header(s); /* skip the .gz header */
108    return(s);
109}
110
111/* ===========================================================================
112        get_byte()
113        Simple now because we assume that avail_in is pointing to the number
114        of bytes in the source buffer that are remaining to be decompressed
115        and next_in is the pointer to the next byte to be retrieved from the
116        buffer.
117*/
118local int
119get_byte(s)
120gz_stream *s;
121{
122    if (s->z_eof)
123                return EOF;
124    if (s->stream.avail_in == 0) {
125                s->z_eof = 1;
126            return EOF;
127    }
128    s->stream.avail_in--;
129    return *(s->stream.next_in)++;
130}
131
132/* ===========================================================================
133        check_header():
134        Check the gzip header of a gz_stream opened for reading. Set the stream
135    mode to transparent if the gzip magic header is not present; set s->err
136    to Z_DATA_ERROR if the magic header is present but the rest of the header
137    is incorrect.
138    IN assertion: the stream s has already been created sucessfully;
139       s->stream.avail_in is zero for the first time, but may be non-zero
140       for concatenated .gz files.
141*/
142local void
143check_header(s)
144gz_stream *s;
145{
146    int method; /* method byte */
147    int flags;  /* flags byte */
148    uInt len;
149    int c;
150
151    /* Check the gzip magic header */
152    for (len = 0; len < 2; len++) {
153                c = get_byte(s);
154                if (c != gz_magic[len]) {
155                    if (len != 0) {
156                                s->stream.avail_in++;
157                                s->stream.next_in--;
158                        }
159                    if (c != EOF) {
160                                s->stream.avail_in++;
161                                s->stream.next_in--;
162                                s->transparent = 1;
163                    }
164                    s->z_err = s->stream.avail_in != 0 ? Z_OK : Z_STREAM_END;
165                    return;
166                }
167    }
168    method = get_byte(s);
169    flags = get_byte(s);
170    if (method != Z_DEFLATED || (flags & RESERVED) != 0) {
171                s->z_err = Z_DATA_ERROR;
172                return;
173    }
174
175    /* Discard time, xflags and OS code: */
176    for (len = 0; len < 6; len++)
177                (void)get_byte(s);
178
179    if ((flags & EXTRA_FIELD) != 0) { /* skip the extra field */
180                len  =  (uInt)get_byte(s);
181                len += ((uInt)get_byte(s))<<8;
182                /* len is garbage if EOF but the loop below will quit anyway */
183                while (len-- != 0 && get_byte(s) != EOF) ;
184    }
185    if ((flags & ORIG_NAME) != 0) { /* skip the original file name */
186                while ((c = get_byte(s)) != 0 && c != EOF) ;
187    }
188    if ((flags & COMMENT) != 0) {   /* skip the .gz file comment */
189                while ((c = get_byte(s)) != 0 && c != EOF) ;
190    }
191    if ((flags & HEAD_CRC) != 0) {  /* skip the header crc */
192                for (len = 0; len < 2; len++) (void)get_byte(s);
193    }
194    s->z_err = s->z_eof ? Z_DATA_ERROR : Z_OK;
195}
196
197/* ===========================================================================
198        destroy():
199        Cleanup then free the given gz_stream. Return a zlib error code.
200        Try freeing in the reverse order of allocations.
201*/
202local int
203destroy (s)
204gz_stream *s;
205{
206    int err = Z_OK;
207
208    if (!s) return Z_STREAM_ERROR;
209
210    TRYFREE(s->msg);
211
212    if (s->stream.state != NULL)
213            err = inflateEnd(&(s->stream));
214   
215    if (s->z_err < 0)
216                err = s->z_err;
217
218    TRYFREE(s->outbuf);
219    TRYFREE(s);
220    return err;
221}
222
223/* ===========================================================================
224        gzRead():
225        Reads the given number of uncompressed bytes from the compressed file.
226        gzRead returns the number of bytes actually read (0 for end of file).
227*/
228int ZEXPORT
229gzRead (gzFile file, voidp buf,unsigned len)
230{
231    gz_stream *s = (gz_stream*)file;
232    Bytef *start = (Bytef*)buf; /* starting point for crc computation */
233    Byte  *next_out; /* == stream.next_out but not forced far (for MSDOS) */
234
235    if (s == NULL || s->mode != 'r') return Z_STREAM_ERROR;
236
237    if (s->z_err == Z_DATA_ERROR || s->z_err == Z_ERRNO) return -1;
238    if (s->z_err == Z_STREAM_END) return 0;  /* EOF */
239
240    next_out = (Byte*)buf;
241    s->stream.next_out = (Bytef*)buf;
242    s->stream.avail_out = len;
243
244    while (s->stream.avail_out != 0) {
245                if (s->transparent) {
246                    /* Copy first the lookahead bytes: */
247                    uInt n = s->stream.avail_in;
248                    if (n > s->stream.avail_out) n = s->stream.avail_out;
249                    if (n > 0) {
250                                zmemcpy((char *)s->stream.next_out, (char *)s->stream.next_in, n);
251                                next_out += n;
252                                s->stream.next_out = next_out;
253                                s->stream.next_in   += n;
254                                s->stream.avail_out -= n;
255                                s->stream.avail_in  -= n;
256                    }
257                    if (s->stream.avail_out > 0) {
258                                int             i, c;
259
260                                for(i=0;i<(int)(s->stream.avail_out);i++) {
261                                        c = get_byte(s);
262                                        if (c == EOF)
263                                                break;
264                                        *next_out++ = (Byte)c;
265                                        s->stream.avail_out--;
266                                }
267                    }
268                    len -= s->stream.avail_out;
269                    s->stream.total_in  += (uLong)len;
270                    s->stream.total_out += (uLong)len;
271            if (len == 0)
272                                s->z_eof = 1;
273                    return (int)len;
274                }
275                s->z_err = inflate(&(s->stream), Z_NO_FLUSH);
276       
277                if (s->z_err == Z_STREAM_END) {
278                    /* Check CRC and original size */
279                    s->crc = zcrc32(s->crc, start, (uInt)(s->stream.next_out - start));
280                    start = s->stream.next_out;
281       
282                    if (getLong(s) != s->crc) {
283                        s->z_err = Z_DATA_ERROR;
284                    } else {
285                        (void)getLong(s);
286                        /* The uncompressed length returned by above getlong() may
287                         * be different from s->stream.total_out) in case of
288                         * concatenated .gz files. Check for such files:
289                         */
290                        check_header(s);
291                        if (s->z_err == Z_OK) {
292                            uLong total_in = s->stream.total_in;
293                            uLong total_out = s->stream.total_out;
294       
295                            inflateReset(&(s->stream));
296                            s->stream.total_in = total_in;
297                            s->stream.total_out = total_out;
298                            s->crc = zcrc32(0L, Z_NULL, 0);
299                        }
300                    }
301                }
302                if (s->z_err != Z_OK || s->z_eof)
303                        break;
304        }
305    s->crc = zcrc32(s->crc, start, (uInt)(s->stream.next_out - start));
306    return (int)(len - s->stream.avail_out);
307}
308
309/* ===========================================================================
310        getLong():
311        Reads a long in LSB order from the given gz_stream. Sets z_err in case
312        of error.
313*/
314local uLong
315getLong (s)
316gz_stream *s;
317{
318    uLong x = (uLong)get_byte(s);
319    int c;
320
321    x += ((uLong)get_byte(s))<<8;
322    x += ((uLong)get_byte(s))<<16;
323    c = get_byte(s);
324    if (c == EOF) s->z_err = Z_DATA_ERROR;
325    x += ((uLong)c)<<24;
326    return x;
327}
328
329/* ===========================================================================
330        unZip():
331        This is the front end to the whole zlib decompressor.
332        It is a chop-up of the original minigzip.c code that came with
333        the zlib source.
334*/
335int
336unZip(char *src, int srclen, char *dest, int destlen)
337{
338        int     len;
339        gz_stream       *s;
340
341        if ((s = gzInit((unsigned char *)src,srclen)) == Z_NULL) {
342                printf("gzInit(0x%lx,%d) failed!\n",(ulong)src,srclen);
343                return(-1);
344        }
345        len = gzRead(s,dest,destlen);
346        if (len < 0)
347                printf("gzRead() returned %d\n",len);
348
349    destroy(s);
350
351        if (len > 0) {
352                flushDcache(dest,len);
353                invalidateIcache(dest,len);
354        }
355
356        return(len);
357}
358
359char *UnzipHelp[] = {
360        "Decompress memory (or file) to some other block of memory.",
361        "-[v:] {src} [dest]",
362        "  src:  addr,len | filename",
363        "  dest: addr[,len]",
364        "Options:",
365        " -v{varname} place decompress size into shellvar",
366        0,
367};
368
369/* Unzip():
370 * Access ZLIB decompressor from monitor command line.
371 */
372int
373Unzip(int argc,char *argv[])
374{
375        int             opt, tot;
376        ulong   srclen, destlen;
377        char    *varname, *asc_src, *asc_dst, *comma, *src, *dest;
378
379        destlen = 99999999;
380        varname = asc_dst = (char *)0;
381        dest = (char *)getAppRamStart();
382
383        while((opt=getopt(argc,argv,"v:")) != -1) {
384                switch(opt) {
385                case 'v':
386                        varname = optarg;
387                        break;
388                default:
389                        return(CMD_PARAM_ERROR);
390                }
391        }
392
393        if (argc == optind+1) {
394                asc_src = argv[optind];
395        }
396        else if (argc == optind+2) {
397                asc_src = argv[optind];
398                asc_dst = argv[optind+1];
399        }
400        else {
401                return(CMD_PARAM_ERROR);
402        }
403
404        comma = strchr(asc_src,',');
405        if (comma) {
406                *comma = 0;
407                src = (char *)strtoul(asc_src,(char **)0,0);
408                srclen = strtoul(comma+1,(char **)0,0);
409        }
410        else {
411                TFILE *tfp;
412                tfp = tfsstat(asc_src);
413                if (!tfp) {
414                        printf("%s: file not found\n",asc_src);
415                        return(CMD_FAILURE);
416                }
417                src = TFS_BASE(tfp);
418                srclen = TFS_SIZE(tfp);
419        }
420
421        if (asc_dst) {
422                comma = strchr(asc_dst,',');
423                if (comma) {
424                        *comma = 0;
425                        destlen = strtoul(comma+1,(char **)0,0);
426                }
427                dest = (char *)strtoul(asc_dst,(char **)0,0);
428        }
429
430        tot = unZip(src,srclen,dest,destlen);
431        printf("Decompressed %ld bytes from 0x%lx to %d bytes at 0x%lx.\n",
432                srclen,(ulong)src,tot,(ulong)dest);
433
434        if (varname)
435                shell_sprintf(varname,"%d",tot);
436
437        return(0);
438}
439
440/* Front end to the rest of the unZip() stuff...
441        Return the size of the decompressed data or -1 if failure.
442        The same front API end is available if unpack is used instead of unzip.
443*/
444int
445decompress(char *src,int srclen, char *dest)
446{
447        return(unZip(src,srclen,dest,99999999));
448}
449#else
450int
451decompress(char *src,int srclen, char *dest)
452{
453        return(-1);
454}
455#endif
Note: See TracBrowser for help on using the repository browser.