mirror of https://github.com/pfusik/zlib6502.git
Command-line converter from GZIP to DEFLATE.
This commit is contained in:
parent
a9c38e537e
commit
ce3d27e006
82
deflater.c
82
deflater.c
|
@ -1,82 +0,0 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <zlib.h>
|
||||
|
||||
#define IN_SIZE_MAX 60000
|
||||
#define OUT_SIZE_MAX 60000
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
FILE* fp;
|
||||
char* inbuf;
|
||||
char* outbuf;
|
||||
size_t inlen;
|
||||
size_t outlen;
|
||||
z_stream stream;
|
||||
|
||||
/* check command line */
|
||||
if (argc != 3) {
|
||||
fprintf(stderr,
|
||||
"Compresses a file to DEFLATE format.\n"
|
||||
"24-08-2002, fox@scene.pl\n"
|
||||
"Usage: deflater input_file deflated_file\n"
|
||||
);
|
||||
return 3;
|
||||
}
|
||||
|
||||
/* alloc buffers */
|
||||
inbuf = malloc(IN_SIZE_MAX);
|
||||
outbuf = malloc(OUT_SIZE_MAX);
|
||||
if (inbuf == NULL || outbuf == NULL) {
|
||||
fprintf(stderr, "deflater: Out of memory!\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* read input file */
|
||||
fp = fopen(argv[1], "rb");
|
||||
if (fp == NULL) {
|
||||
perror(argv[1]);
|
||||
return 1;
|
||||
}
|
||||
inlen = fread(inbuf, 1, IN_SIZE_MAX, fp);
|
||||
fclose(fp);
|
||||
|
||||
/* compress */
|
||||
stream.next_in = inbuf;
|
||||
stream.avail_in = inlen;
|
||||
stream.next_out = outbuf;
|
||||
stream.avail_out = OUT_SIZE_MAX;
|
||||
stream.zalloc = (alloc_func) 0;
|
||||
stream.zfree = (free_func) 0;
|
||||
if (deflateInit2(&stream, Z_BEST_COMPRESSION, Z_DEFLATED,
|
||||
-MAX_WBITS, 9, Z_DEFAULT_STRATEGY) != Z_OK) {
|
||||
fprintf(stderr, "deflater: deflateInit2 failed\n");
|
||||
return 1;
|
||||
}
|
||||
if (deflate(&stream, Z_FINISH) != Z_STREAM_END) {
|
||||
fprintf(stderr, "deflater: deflate failed\n");
|
||||
return 1;
|
||||
}
|
||||
if (deflateEnd(&stream) != Z_OK) {
|
||||
fprintf(stderr, "deflater: deflateEnd failed\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* write output */
|
||||
fp = fopen(argv[2], "wb");
|
||||
if (fp == NULL) {
|
||||
perror(argv[2]);
|
||||
return 1;
|
||||
}
|
||||
outlen = fwrite(outbuf, 1, stream.total_out, fp);
|
||||
fclose(fp);
|
||||
if (outlen != stream.total_out) {
|
||||
perror(argv[2]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* display summary */
|
||||
printf("Compressed %s (%d bytes) to %s (%d bytes)\n",
|
||||
argv[1], inlen, argv[2], outlen);
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,78 @@
|
|||
// gzip2deflate by Piotr Fusik <fox@scene.pl>
|
||||
// http://xasm.atari.org
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#ifdef __WIN32
|
||||
#include <fcntl.h>
|
||||
#endif
|
||||
|
||||
static int get_byte(void)
|
||||
{
|
||||
int b = getchar();
|
||||
if (b == EOF) {
|
||||
fputs("gzip2deflate: unexpected end of stream\n", stderr);
|
||||
exit(1);
|
||||
}
|
||||
return b;
|
||||
}
|
||||
|
||||
static void skip_bytes(int n)
|
||||
{
|
||||
while (--n >= 0)
|
||||
get_byte();
|
||||
}
|
||||
|
||||
static void skip_string(void)
|
||||
{
|
||||
while (get_byte() != 0);
|
||||
}
|
||||
|
||||
int main (int argc, char *argv[])
|
||||
{
|
||||
int flg;
|
||||
char buf[8 + 4096];
|
||||
size_t len;
|
||||
#ifdef __WIN32
|
||||
_setmode(_fileno(stdin), _O_BINARY);
|
||||
_setmode(_fileno(stdout), _O_BINARY);
|
||||
#endif
|
||||
if (get_byte() != 0x1f || get_byte() != 0x8b || get_byte() != 8) {
|
||||
fputs("gzip2deflate: not a gzip file on stdin\n", stderr);
|
||||
return 1;
|
||||
}
|
||||
flg = get_byte();
|
||||
skip_bytes(6);
|
||||
if ((flg & 4) != 0) {
|
||||
int xlen = get_byte();
|
||||
xlen += get_byte() << 8;
|
||||
skip_bytes(xlen);
|
||||
}
|
||||
if ((flg & 8) != 0)
|
||||
skip_string();
|
||||
if ((flg & 16) != 0)
|
||||
skip_string();
|
||||
if ((flg & 2) != 0)
|
||||
skip_bytes(2);
|
||||
/* copy everything except the last 8 bytes */
|
||||
len = 0;
|
||||
for (;;) {
|
||||
len += fread(buf + len, 1, sizeof(buf) - len, stdin);
|
||||
if (len != sizeof(buf))
|
||||
break;
|
||||
fwrite(buf, 1, sizeof(buf) - 8, stdout);
|
||||
memcpy(buf, buf + sizeof(buf) - 8, 8);
|
||||
len = 8;
|
||||
}
|
||||
if (ferror(stdin)) {
|
||||
fputs("gzip2deflate: read error\n", stderr);
|
||||
return 1;
|
||||
}
|
||||
if (len < 8) {
|
||||
fputs("gzip2deflate: unexpected end of stream\n", stderr);
|
||||
return 1;
|
||||
}
|
||||
fwrite(buf, 1, len - 8, stdout);
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue