Last active
June 30, 2018 03:29
-
-
Save hirokuma/0f038adc04c1ec38ba6c01f8b2db4fb5 to your computer and use it in GitHub Desktop.
zlib compress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdint.h> | |
#include <string.h> | |
#include <zlib.h> | |
#include <assert.h> | |
#include "hirokuma.h" // data: hirokuma_png[] | |
// size: hirokuma_png_len = 8080 | |
#include "hirokumaz.h" // data: hirokuma_z[] | |
// size: hirokuma_z_len = 8091 | |
#define M_CHUNK (1024) | |
static void comp(void) | |
{ | |
printf("exec: %s\n", __func__); | |
int retval; | |
z_stream zstm; | |
const uint8_t *p_in; | |
uint8_t buf_out[M_CHUNK]; | |
zstm.zalloc = Z_NULL; | |
zstm.zfree = Z_NULL; | |
zstm.opaque = Z_NULL; | |
retval = deflateInit(&zstm, Z_DEFAULT_COMPRESSION); | |
assert(retval == Z_OK); | |
FILE *fp = fopen("hirokuma.z", "w"); | |
assert(fp != NULL); | |
size_t blk = (hirokuma_png_len + M_CHUNK - 1) / M_CHUNK; | |
zstm.next_in = (uint8_t *)hirokuma_png; | |
for (size_t lp = 0; lp < blk; lp++) { | |
zstm.avail_in = (lp != blk - 1) ? M_CHUNK : hirokuma_png_len - M_CHUNK * lp; | |
do { | |
zstm.avail_out = sizeof(buf_out); | |
zstm.next_out = buf_out; | |
retval = deflate(&zstm, (lp != blk - 1) ? Z_NO_FLUSH : Z_FINISH); | |
if (retval != Z_OK) { | |
printf("retval=%d\n", retval); | |
} | |
assert(retval != Z_STREAM_ERROR); | |
fwrite(buf_out, 1, M_CHUNK - zstm.avail_out, fp); | |
} while (zstm.avail_out == 0); | |
} | |
fclose(fp); | |
deflateEnd(&zstm); | |
} | |
static void decomp(void) | |
{ | |
printf("exec: %s\n", __func__); | |
int retval; | |
z_stream zstm; | |
uint8_t buf_out[M_CHUNK]; | |
zstm.zalloc = Z_NULL; | |
zstm.zfree = Z_NULL; | |
zstm.opaque = Z_NULL; | |
retval = inflateInit(&zstm); | |
assert(retval == Z_OK); | |
FILE *fp = fopen("hirokuma.z.png", "w"); | |
assert(fp != NULL); | |
size_t blk = (hirokuma_z_len + M_CHUNK - 1) / M_CHUNK; | |
zstm.next_in = (uint8_t *)hirokuma_z; | |
for (size_t lp = 0; lp < blk; lp++) { | |
zstm.avail_in = (lp != blk - 1) ? M_CHUNK : hirokuma_z_len - M_CHUNK * lp; | |
do { | |
zstm.avail_out = sizeof(buf_out); | |
zstm.next_out = buf_out; | |
retval = inflate(&zstm, Z_NO_FLUSH); | |
if (retval != Z_OK) { | |
printf("retval=%d\n", retval); | |
} | |
assert(retval != Z_STREAM_ERROR); | |
fwrite(buf_out, 1, M_CHUNK - zstm.avail_out, fp); | |
} while (zstm.avail_out == 0); | |
} | |
fclose(fp); | |
inflateEnd(&zstm); | |
} | |
int main(void) | |
{ | |
comp(); | |
decomp(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment