Skip to content

Instantly share code, notes, and snippets.

@ccoreghaesm ccoreghaesm/main.c
Created May 10, 2019

Embed
What would you like to do?
zlib - iostream.ir
#include "zlib.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define CHUNK 251904
int compressing(FILE *source, FILE *dest, int level) {
int return_;
int flush;
int have;
z_stream stream;
unsigned char input[CHUNK];
unsigned char output[CHUNK];
stream.zalloc = Z_NULL;
stream.zfree = Z_NULL;
stream.opaque = Z_NULL;
return_ = deflateInit(&stream, level);
if (return_ != Z_OK)
return return_;
do {
stream.avail_in = fread(input, 1, CHUNK, source);
if (ferror(source)) {
deflateEnd(&stream);
return Z_ERRNO;
}
flush = feof(source) ? Z_FINISH : Z_NO_FLUSH;
stream.next_in = input;
do {
stream.avail_out = CHUNK;
stream.next_out = output;
return_ = deflate(&stream, flush);
assert(return_ != Z_STREAM_ERROR);
have = CHUNK - stream.avail_out;
if (fwrite(output, 1, have, dest) != have || ferror(dest)) {
deflateEnd(&stream);
return Z_ERRNO;
}
} while (stream.avail_out == 0);
assert(stream.avail_in == 0);
} while (flush != Z_FINISH);
assert(return_ == Z_STREAM_END);
deflateEnd(&stream);
return Z_OK;
}
int decompress(FILE *source, FILE *dest) {
int return_;
unsigned have;
z_stream stream;
unsigned char input[CHUNK];
unsigned char output[CHUNK];
stream.zalloc = Z_NULL;
stream.zfree = Z_NULL;
stream.opaque = Z_NULL;
stream.avail_in = 0;
stream.next_in = Z_NULL;
return_ = inflateInit(&stream);
if (return_ != Z_OK)
return return_;
do {
stream.avail_in = fread(input, 1, CHUNK, source);
if (ferror(source)) {
inflateEnd(&stream);
return Z_ERRNO;
}
if (stream.avail_in == 0)
break;
stream.next_in = input;
do {
stream.avail_out = CHUNK;
stream.next_out = output;
return_ = inflate(&stream, Z_NO_FLUSH);
assert(return_ != Z_STREAM_ERROR);
switch (return_) {
case Z_NEED_DICT:
return_ = Z_DATA_ERROR;
case Z_DATA_ERROR:
case Z_MEM_ERROR:
inflateEnd(&stream);
return return_;
}
have = CHUNK - stream.avail_out;
if (fwrite(output, 1, have, dest) != have || ferror(dest)) {
inflateEnd(&stream);
return Z_ERRNO;
}
} while (stream.avail_out == 0);
} while (return_ != Z_STREAM_END);
inflateEnd(&stream);
return (return_ == Z_STREAM_END) ? Z_OK : Z_DATA_ERROR;
}
void zlibError(int return_) {
fprintf(stderr, "ZLIB ERROR: ");
switch (return_) {
case Z_ERRNO:
if (ferror(stdin))
fprintf(stderr, "ERROR READING stdin.\n");
if (ferror(stdout))
fprintf(stderr, "ERROR WRITING stdout.\n");
break;
case Z_STREAM_ERROR:
fprintf(stderr, "INVALID COMPRESSION LEVEL.\n");
break;
case Z_DATA_ERROR:
fprintf(stderr, "INVALID OR INCOMPLETE deflate() DATA.\n");
break;
case Z_MEM_ERROR:
fprintf(stderr, "OUT OF MEMORY.\n");
break;
case Z_VERSION_ERROR:
fprintf(stderr, "zlib VERSION MISMATCH.\n");
}
}
int main(int argc, char **argv) {
int return_;
if (argc == 1) {
return_ = compressing(stdin, stdout, 9);
if (return_ != Z_OK)
zlibError(return_);
return return_;
} else if (argc == 2 && strcmp(argv[1], "-d") == 0) {
return_ = decompress(stdin, stdout);
if (return_ != Z_OK)
zlibError(return_);
return return_;
} else {
fprintf(stderr, "zlib Usage: PROGRAMM [-d] < SOURCE > DEST\n");
return EXIT_FAILURE;
}
return EXIT_FAILURE;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.