diff options
-rw-r--r-- | usr.bin/compress/compress.h | 8 | ||||
-rw-r--r-- | usr.bin/compress/gzopen.c | 39 | ||||
-rw-r--r-- | usr.bin/compress/main.c | 44 | ||||
-rw-r--r-- | usr.bin/compress/zopen.c | 42 |
4 files changed, 58 insertions, 75 deletions
diff --git a/usr.bin/compress/compress.h b/usr.bin/compress/compress.h index a5c3a72fa38..b9ce25d7ca2 100644 --- a/usr.bin/compress/compress.h +++ b/usr.bin/compress/compress.h @@ -1,4 +1,4 @@ -/* $OpenBSD: compress.h,v 1.4 2003/06/03 21:08:36 mickey Exp $ */ +/* $OpenBSD: compress.h,v 1.5 2003/07/11 02:31:18 millert Exp $ */ /* * Copyright (c) 1997 Michael Shalayeff @@ -37,21 +37,21 @@ extern const char main_rcsid[], z_rcsid[], gz_rcsid[], pkzip_rcsid[], pack_rcsid[], lzh_rcsid[]; extern int z_check_header(int, struct stat *, const char *); -extern void *z_open(int, const char *, int); +extern void *z_open(int, const char *, int, int); extern FILE *zopen(const char *, const char *,int); extern int zread(void *, char *, int); extern int zwrite(void *, const char *, int); extern int zclose(void *); extern int gz_check_header(int, struct stat *, const char *); -extern void *gz_open(int, const char *, int); +extern void *gz_open(int, const char *, int, int); extern int gz_read(void *, char *, int); extern int gz_write(void *, const char *, int); extern int gz_close(void *); extern int gz_flush(void *, int); extern int lzh_check_header(int, struct stat *, const char *); -extern void *lzh_open(int, const char *, int); +extern void *lzh_open(int, const char *, int, int); extern int lzh_read(void *, char *, int); extern int lzh_write(void *, const char *, int); extern int lzh_close(void *); diff --git a/usr.bin/compress/gzopen.c b/usr.bin/compress/gzopen.c index b73ed5d2422..c7c9e203ad3 100644 --- a/usr.bin/compress/gzopen.c +++ b/usr.bin/compress/gzopen.c @@ -1,4 +1,4 @@ -/* $OpenBSD: gzopen.c,v 1.11 2003/07/10 00:06:50 david Exp $ */ +/* $OpenBSD: gzopen.c,v 1.12 2003/07/11 02:31:18 millert Exp $ */ /* * Copyright (c) 1997 Michael Shalayeff @@ -59,7 +59,7 @@ */ const char gz_rcsid[] = - "$OpenBSD: gzopen.c,v 1.11 2003/07/10 00:06:50 david Exp $"; + "$OpenBSD: gzopen.c,v 1.12 2003/07/11 02:31:18 millert Exp $"; #include <sys/types.h> #include <sys/stat.h> @@ -97,26 +97,11 @@ static const u_char gz_magic[2] = {0x1f, 0x8b}; /* gzip magic header */ static int put_int32(gz_stream *, u_int32_t); static u_int32_t get_int32(gz_stream *); -static int get_header(gz_stream *); +static int get_header(gz_stream *, int); static int get_byte(gz_stream *); -int -gz_check_header(int fd, struct stat *sb, const char *ofn) -{ - int f; - u_char buf[sizeof(gz_magic)]; - off_t off = lseek(fd, 0, SEEK_CUR); - - f = (read(fd, buf, sizeof(buf)) == sizeof(buf) && - !memcmp(buf, gz_magic, sizeof(buf))); - - lseek (fd, off, SEEK_SET); - - return f; -} - void * -gz_open(int fd, const char *mode, int bits) +gz_open(int fd, const char *mode, int bits, int gotmagic) { gz_stream *s; @@ -177,7 +162,7 @@ gz_open(int fd, const char *mode, int bits) s = NULL; } } else { - if (get_header(s) != 0) { /* skip the .gz header */ + if (get_header(s, gotmagic) != 0) { /* skip the .gz header */ gz_close (s); s = NULL; } @@ -297,7 +282,7 @@ get_int32(gz_stream *s) } static int -get_header(gz_stream *s) +get_header(gz_stream *s, int gotmagic) { int method; /* method byte */ int flags; /* flags byte */ @@ -305,11 +290,13 @@ get_header(gz_stream *s) int c; /* Check the gzip magic header */ - for (len = 0; len < 2; len++) { - c = get_byte(s); - if (c != gz_magic[len]) { - errno = EFTYPE; - return -1; + if (!gotmagic) { + for (len = 0; len < 2; len++) { + c = get_byte(s); + if (c != gz_magic[len]) { + errno = EFTYPE; + return -1; + } } } diff --git a/usr.bin/compress/main.c b/usr.bin/compress/main.c index 3238f738a51..dfdfbb9f712 100644 --- a/usr.bin/compress/main.c +++ b/usr.bin/compress/main.c @@ -1,4 +1,4 @@ -/* $OpenBSD: main.c,v 1.32 2003/07/08 00:30:12 mickey Exp $ */ +/* $OpenBSD: main.c,v 1.33 2003/07/11 02:31:18 millert Exp $ */ static const char copyright[] = "@(#) Copyright (c) 1992, 1993\n\ @@ -35,7 +35,7 @@ static const char license[] = #if 0 static char sccsid[] = "@(#)compress.c 8.2 (Berkeley) 1/7/94"; #else -static const char main_rcsid[] = "$OpenBSD: main.c,v 1.32 2003/07/08 00:30:12 mickey Exp $"; +static const char main_rcsid[] = "$OpenBSD: main.c,v 1.33 2003/07/11 02:31:18 millert Exp $"; #endif #endif /* not lint */ @@ -65,23 +65,23 @@ extern char *__progname; const struct compressor { char *name; char *suffix; - int (*check_header)(int, struct stat *, const char *); - void *(*open)(int, const char *, int); + u_char *magic; + void *(*open)(int, const char *, int, int); int (*read)(void *, char *, int); int (*write)(void *, const char *, int); int (*close)(void *); } c_table[] = { #define M_COMPRESS (&c_table[0]) - { "compress", ".Z", z_check_header, z_open, zread, zwrite, zclose }, + { "compress", ".Z", "\037\235", z_open, zread, zwrite, zclose }, #define M_DEFLATE (&c_table[1]) - { "deflate", ".gz", gz_check_header, gz_open, gz_read, gz_write, gz_close }, + { "deflate", ".gz", "\037\213", gz_open, gz_read, gz_write, gz_close }, #if 0 #define M_LZH (&c_table[2]) - { "lzh", ".lzh", lzh_check_header, lzh_open, lzh_read, lzh_write, lzh_close }, + { "lzh", ".lzh", "\037\240", lzh_open, lzh_read, lzh_write, lzh_close }, #define M_ZIP (&c_table[3]) - { "zip", ".zip", zip_check_header, zip_open, zip_read, zip_write, zip_close }, + { "zip", ".zip", "PK", zip_open, zip_read, zip_write, zip_close }, #define M_PACK (&c_table[4]) - { "pack", ".pak",pak_check_header, pak_open, pak_read, pak_write, pak_close }, + { "pack", ".pak", "\037\036", pak_open, pak_read, pak_write, pak_close }, #endif { NULL } }; @@ -464,7 +464,7 @@ compress(const char *in, const char *out, const struct compressor *method, return (-1); } - if ((cookie = (*method->open)(ofd, "w", bits)) == NULL) { + if ((cookie = (*method->open)(ofd, "w", bits, 0)) == NULL) { if (verbose >= 0) warn("%s", in); (void) close(ofd); @@ -505,16 +505,16 @@ const struct compressor * check_method(int fd, struct stat *sb, const char *out) { const struct compressor *method; - - for (method = &c_table[0]; - method->name != NULL && !(*method->check_header)(fd, sb, out); - method++) - ; - - if (method->name == NULL) - method = NULL; - - return (method); + u_char magic[2]; + + if (read(fd, magic, sizeof(magic)) != 2) + return (NULL); + for (method = &c_table[0]; method->name != NULL; method++) { + if (magic[0] == method->magic[0] && + magic[1] == method->magic[1]) + return (method); + } + return (NULL); } int @@ -543,14 +543,14 @@ decompress(const char *in, const char *out, const struct compressor *method, return -1; } - if (!pipin && (method = check_method(ifd, sb, out)) == NULL) { + if ((method = check_method(ifd, sb, out)) == NULL) { if (verbose >= 0) warnx("%s: unrecognized file format", in); close (ifd); return -1; } - if ((cookie = (*method->open)(ifd, "r", bits)) == NULL) { + if ((cookie = (*method->open)(ifd, "r", bits, 1)) == NULL) { if (verbose >= 0) warn("%s", in); error++; diff --git a/usr.bin/compress/zopen.c b/usr.bin/compress/zopen.c index 24cfab4b5ab..9b26edcb206 100644 --- a/usr.bin/compress/zopen.c +++ b/usr.bin/compress/zopen.c @@ -1,4 +1,4 @@ -/* $OpenBSD: zopen.c,v 1.11 2003/06/22 15:22:43 deraadt Exp $ */ +/* $OpenBSD: zopen.c,v 1.12 2003/07/11 02:31:18 millert Exp $ */ /* $NetBSD: zopen.c,v 1.5 1995/03/26 09:44:53 glass Exp $ */ /*- @@ -40,7 +40,7 @@ static char sccsid[] = "@(#)zopen.c 8.1 (Berkeley) 6/27/93"; #else const char z_rcsid[] = - "$OpenBSD: zopen.c,v 1.11 2003/06/22 15:22:43 deraadt Exp $"; + "$OpenBSD: zopen.c,v 1.12 2003/07/11 02:31:18 millert Exp $"; #endif /*- @@ -104,7 +104,7 @@ struct s_zstate { int zs_fd; /* File stream for I/O */ char zs_mode; /* r or w */ enum { - S_START, S_MIDDLE, S_EOF + S_START, S_MAGIC, S_MIDDLE, S_EOF } zs_state; /* State of computation */ int zs_n_bits; /* Number of bits/code. */ int zs_maxbits; /* User settable max # bits/code. */ @@ -227,6 +227,8 @@ zwrite(void *cookie, const char *wbp, int num) count = num; bp = (u_char *)wbp; switch (zs->zs_state) { + case S_MAGIC: + return -1; case S_EOF: return 0; case S_START: @@ -468,6 +470,16 @@ zread(void *cookie, char *rbp, int num) case S_START: zs->zs_state = S_MIDDLE; zs->zs_bp = zs->zs_buf; + header[0] = header[1] = header[2] = '\0'; + read(zs->zs_fd, header, sizeof(header)); + break; + case S_MAGIC: + zs->zs_state = S_MIDDLE; + zs->zs_bp = zs->zs_buf; + header[0] = z_magic[0]; + header[1] = z_magic[1]; + header[2] = '\0'; + read(zs->zs_fd, &header[2], 1); break; case S_MIDDLE: goto middle; @@ -476,8 +488,7 @@ zread(void *cookie, char *rbp, int num) } /* Check the magic number */ - if (read(zs->zs_fd, header, sizeof(header)) != sizeof(header) || - memcmp(header, z_magic, sizeof(z_magic)) != 0) { + if (header[0] != z_magic[0] || header[1] != z_magic[1]) { errno = EFTYPE; return (-1); } @@ -706,7 +717,7 @@ zopen(const char *name, const char *mode, int bits) if ((fd = open(name, (*mode=='r'? O_RDONLY:O_WRONLY|O_CREAT), S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) == -1) return NULL; - if ((cookie = z_open(fd, mode, bits)) == NULL) { + if ((cookie = z_open(fd, mode, bits, 0)) == NULL) { close(fd); return NULL; } @@ -715,7 +726,7 @@ zopen(const char *name, const char *mode, int bits) } void * -z_open(int fd, const char *mode, int bits) +z_open(int fd, const char *mode, int bits, int gotmagic) { struct s_zstate *zs; @@ -740,7 +751,7 @@ z_open(int fd, const char *mode, int bits) zs->zs_checkpoint = CHECK_GAP; zs->zs_in_count = 1; /* Length of input. */ zs->zs_out_count = 0; /* # of codes output (for debugging).*/ - zs->zs_state = S_START; + zs->zs_state = gotmagic ? S_MAGIC : S_START; zs->zs_offset = 0; zs->zs_size = 0; zs->zs_mode = mode[0]; @@ -749,18 +760,3 @@ z_open(int fd, const char *mode, int bits) zs->zs_fd = fd; return zs; } - -int -z_check_header(int fd, struct stat *sb, const char *ofn) -{ - int f; - u_char buf[sizeof(z_magic)]; - off_t off = lseek(fd, 0, SEEK_CUR); - - f = (read(fd, buf, sizeof(buf)) == sizeof(buf) && - !memcmp(buf, z_magic, sizeof(buf))); - - lseek (fd, off, SEEK_SET); - - return f; -} |