source: svn/newcon3bcm2_21bu/dst/dlib/src/ZLIB/minigzip.c

Last change on this file was 76, checked in by megakiss, 10 years ago

1W 대기전력을 만족시키기 위하여 POWEROFF시 튜너를 Standby 상태로 함

  • Property svn:executable set to *
File size: 8.2 KB
Line 
1/* minigzip.c -- simulate gzip using the zlib compression library
2 * Copyright (C) 1995-1998 Jean-loup Gailly.
3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */
5
6/*
7 * minigzip is a minimal implementation of the gzip utility. This is
8 * only an example of using zlib and isn't meant to replace the
9 * full-featured gzip. No attempt is made to deal with file systems
10 * limiting names to 14 or 8+3 characters, etc... Error checking is
11 * very limited. So use minigzip only for testing; use gzip for the
12 * real thing. On MSDOS, use only on file names without extension
13 * or in pipe mode.
14 */
15
16/* @(#) $Id: //suprahd/releases/suprahd_163/suprahd_ztvapp640_163/drivers/graphics/ZLIB/minigzip.c#1 $
17 * $Revision: #1 $
18 * $DateTime: 2006/02/24 17:51:46 $
19 * $Change: 42566 $
20 * $Author: pryush.sharma $
21*/
22
23
24#include <stdio.h>
25#include "zlib.h"
26
27#ifdef STDC
28#  include <string.h>
29#  include <stdlib.h>
30#else
31   extern void exit  OF((int));
32#endif
33
34#ifdef USE_MMAP
35#  include <sys/types.h>
36#  include <sys/mman.h>
37#  include <sys/stat.h>
38#endif
39
40#if defined(MSDOS) || defined(OS2) || defined(WIN32)
41#  include <fcntl.h>
42#  include <io.h>
43#  define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
44#else
45#  define SET_BINARY_MODE(file)
46#endif
47
48#ifdef VMS
49#  define unlink delete
50#  define GZ_SUFFIX "-gz"
51#endif
52#ifdef RISCOS
53#  define unlink remove
54#  define GZ_SUFFIX "-gz"
55#  define fileno(file) file->__file
56#endif
57#if defined(__MWERKS__) && __dest_os != __be_os && __dest_os != __win32_os
58#  include <unix.h> /* for fileno */
59#endif
60
61#ifndef WIN32 /* unlink already in stdio.h for WIN32 */
62  extern int unlink OF((const char *));
63#endif
64
65#ifndef GZ_SUFFIX
66#  define GZ_SUFFIX ".gz"
67#endif
68#define SUFFIX_LEN (sizeof(GZ_SUFFIX)-1)
69
70#define BUFLEN      16384
71#define MAX_NAME_LEN 1024
72
73#ifdef MAXSEG_64K
74#  define local static
75   /* Needed for systems with limitation on stack size. */
76#else
77#  define local
78#endif
79
80char *prog;
81
82void error            OF((const char *msg));
83void gz_compress      OF((FILE   *in, gzFile out));
84#ifdef USE_MMAP
85int  gz_compress_mmap OF((FILE   *in, gzFile out));
86#endif
87void gz_uncompress    OF((gzFile in, FILE   *out));
88void file_compress    OF((char  *file, char *mode));
89void file_uncompress  OF((char  *file));
90int  main             OF((int argc, char *argv[]));
91
92/* ===========================================================================
93 * Display error message and exit
94 */
95void error(msg)
96    const char *msg;
97{
98    fprintf(stderr, "%s: %s\n", prog, msg);
99    exit(1);
100}
101
102/* ===========================================================================
103 * Compress input to output then close both files.
104 */
105
106void gz_compress(in, out)
107    FILE   *in;
108    gzFile out;
109{
110    local char buf[BUFLEN];
111    int len;
112    int err;
113
114#ifdef USE_MMAP
115    /* Try first compressing with mmap. If mmap fails (minigzip used in a
116     * pipe), use the normal fread loop.
117     */
118    if (gz_compress_mmap(in, out) == Z_OK) return;
119#endif
120    for (;;) {
121        len = fread(buf, 1, sizeof(buf), in);
122        if (ferror(in)) {
123            perror("fread");
124            exit(1);
125        }
126        if (len == 0) break;
127
128        if (gzwrite(out, buf, (unsigned)len) != len) error(gzerror(out, &err));
129    }
130    fclose(in);
131    if (gzclose(out) != Z_OK) error("failed gzclose");
132}
133
134#ifdef USE_MMAP /* MMAP version, Miguel Albrecht <malbrech@eso.org> */
135
136/* Try compressing the input file at once using mmap. Return Z_OK if
137 * if success, Z_ERRNO otherwise.
138 */
139int gz_compress_mmap(in, out)
140    FILE   *in;
141    gzFile out;
142{
143    int len;
144    int err;
145    int ifd = fileno(in);
146    caddr_t buf;    /* mmap'ed buffer for the entire input file */
147    off_t buf_len;  /* length of the input file */
148    struct stat sb;
149
150    /* Determine the size of the file, needed for mmap: */
151    if (fstat(ifd, &sb) < 0) return Z_ERRNO;
152    buf_len = sb.st_size;
153    if (buf_len <= 0) return Z_ERRNO;
154
155    /* Now do the actual mmap: */
156    buf = mmap((caddr_t) 0, buf_len, PROT_READ, MAP_SHARED, ifd, (off_t)0); 
157    if (buf == (caddr_t)(-1)) return Z_ERRNO;
158
159    /* Compress the whole file at once: */
160    len = gzwrite(out, (char *)buf, (unsigned)buf_len);
161
162    if (len != (int)buf_len) error(gzerror(out, &err));
163
164    munmap(buf, buf_len);
165    fclose(in);
166    if (gzclose(out) != Z_OK) error("failed gzclose");
167    return Z_OK;
168}
169#endif /* USE_MMAP */
170
171/* ===========================================================================
172 * Uncompress input to output then close both files.
173 */
174void gz_uncompress(in, out)
175    gzFile in;
176    FILE   *out;
177{
178    local char buf[BUFLEN];
179    int len;
180    int err;
181
182    for (;;) {
183        len = gzread(in, buf, sizeof(buf));
184        if (len < 0) error (gzerror(in, &err));
185        if (len == 0) break;
186
187        if ((int)fwrite(buf, 1, (unsigned)len, out) != len) {
188            error("failed fwrite");
189        }
190    }
191    if (fclose(out)) error("failed fclose");
192
193    if (gzclose(in) != Z_OK) error("failed gzclose");
194}
195
196
197/* ===========================================================================
198 * Compress the given file: create a corresponding .gz file and remove the
199 * original.
200 */
201void file_compress(file, mode)
202    char  *file;
203    char  *mode;
204{
205    local char outfile[MAX_NAME_LEN];
206    FILE  *in;
207    gzFile out;
208
209    strcpy(outfile, file);
210    strcat(outfile, GZ_SUFFIX);
211
212    in = fopen(file, "rb");
213    if (in == NULL) {
214        perror(file);
215        exit(1);
216    }
217    out = gzopen(outfile, mode);
218    if (out == NULL) {
219        fprintf(stderr, "%s: can't gzopen %s\n", prog, outfile);
220        exit(1);
221    }
222    gz_compress(in, out);
223
224    unlink(file);
225}
226
227
228/* ===========================================================================
229 * Uncompress the given file and remove the original.
230 */
231void file_uncompress(file)
232    char  *file;
233{
234    local char buf[MAX_NAME_LEN];
235    char *infile, *outfile;
236    FILE  *out;
237    gzFile in;
238    int len = strlen(file);
239
240    strcpy(buf, file);
241
242    if (len > (int)SUFFIX_LEN && strcmp(file+len-SUFFIX_LEN, GZ_SUFFIX) == 0) {
243        infile = file;
244        outfile = buf;
245        outfile[len-3] = '\0';
246    } else {
247        outfile = file;
248        infile = buf;
249        strcat(infile, GZ_SUFFIX);
250    }
251    in = gzopen(infile, "rb");
252    if (in == NULL) {
253        fprintf(stderr, "%s: can't gzopen %s\n", prog, infile);
254        exit(1);
255    }
256    out = fopen(outfile, "wb");
257    if (out == NULL) {
258        perror(file);
259        exit(1);
260    }
261
262    gz_uncompress(in, out);
263
264    unlink(infile);
265}
266
267
268/* ===========================================================================
269 * Usage:  minigzip [-d] [-f] [-h] [-1 to -9] [files...]
270 *   -d : decompress
271 *   -f : compress with Z_FILTERED
272 *   -h : compress with Z_HUFFMAN_ONLY
273 *   -1 to -9 : compression level
274 */
275
276int main(argc, argv)
277    int argc;
278    char *argv[];
279{
280    int uncompr = 0;
281    gzFile file;
282    char outmode[20];
283
284    strcpy(outmode, "wb6 ");
285
286    prog = argv[0];
287    argc--, argv++;
288
289    while (argc > 0) {
290      if (strcmp(*argv, "-d") == 0)
291        uncompr = 1;
292      else if (strcmp(*argv, "-f") == 0)
293        outmode[3] = 'f';
294      else if (strcmp(*argv, "-h") == 0)
295        outmode[3] = 'h';
296      else if ((*argv)[0] == '-' && (*argv)[1] >= '1' && (*argv)[1] <= '9' &&
297               (*argv)[2] == 0)
298        outmode[2] = (*argv)[1];
299      else
300        break;
301      argc--, argv++;
302    }
303    if (argc == 0) {
304        SET_BINARY_MODE(stdin);
305        SET_BINARY_MODE(stdout);
306        if (uncompr) {
307            file = gzdopen(fileno(stdin), "rb");
308            if (file == NULL) error("can't gzdopen stdin");
309            gz_uncompress(file, stdout);
310        } else {
311            file = gzdopen(fileno(stdout), outmode);
312            if (file == NULL) error("can't gzdopen stdout");
313            gz_compress(stdin, file);
314        }
315    } else {
316        do {
317            if (uncompr) {
318                file_uncompress(*argv);
319            } else {
320                file_compress(*argv, outmode);
321            }
322        } while (argv++, --argc);
323    }
324    exit(0);
325    return 0; /* to avoid warning */
326}
Note: See TracBrowser for help on using the repository browser.