~ubuntu-branches/debian/squeeze/stella/squeeze

« back to all changes in this revision

Viewing changes to src/zlib/minigzip.c

  • Committer: Bazaar Package Importer
  • Author(s): Stephen Kitt
  • Date: 2010-07-12 23:49:36 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20100712234936-juawrr3etzhr2qpv
Tags: 3.1.2-1
* New maintainer (closes: #532039).
* New upstream version (closes: #461121):
  - includes launcher (closes: #396058).
* Fix the reference to the X Window System in the description (closes:
  #411815).
* Move to main, DFSG-free ROMs are available (see README.Debian).
* Enhance the package description.
* Drop the libslang2-dev dependency (closes: #560274).
* Remove the Encoding entry from stella.desktop.
* Avoid ignoring errors when cleaning.
* Add ${misc:Depends} to the package dependencies.
* Provide a doc-base file to install the documentation using doc-base.
* Switch to debhelper 7 with a simplified rules file.
* Use autotools-dev to provide updated configuration files.
* Update to Standards-Version 3.9.0:
  - Move to menu section Applications/Emulators.
  - Move the homepage declaration.
* Re-write the manpage.

Show diffs side-by-side

added added

removed removed

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