Skip to content

Instantly share code, notes, and snippets.

@vtermanis
Last active December 20, 2016 12:24
Show Gist options
  • Save vtermanis/70cf54aad096e8c870eedf59efad228e to your computer and use it in GitHub Desktop.
Save vtermanis/70cf54aad096e8c870eedf59efad228e to your computer and use it in GitHub Desktop.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "lz4frame_static.h"
/******************************************************************************/
static LZ4F_preferences_t prefs_defaults = {{0, 0, 0, 0, 0, {0}}, 0, 0, {0}};
static const char short_input[] = "abcdefghijklmnopqrstuvwxyz0123456789";
#define BAIL_ON_LZ4_ERROR(code) {\
size_t __err = (code);\
if (LZ4F_isError(__err)) {\
printf("LZ4 error [%zd] %s\n", __err, LZ4F_getErrorName(__err));\
goto bail;\
}\
}
#define BAIL_ON_NULL(result) \
if (NULL == (result)) {\
goto bail;\
}
#define FREE_AND_NULL(ptr) free(ptr); ptr = NULL
/******************************************************************************/
int main(void) {
LZ4F_preferences_t prefs = prefs_defaults;
LZ4F_compressionContext_t ctx = NULL;
size_t output_len = 15; // maximum header size
// NOTE: just used for chunks written by begin/update/end, not full output
char* output = NULL;
BAIL_ON_LZ4_ERROR(LZ4F_createCompressionContext(&ctx, LZ4F_VERSION));
BAIL_ON_NULL(output = malloc(sizeof(char) * output_len));
BAIL_ON_LZ4_ERROR(LZ4F_compressBegin(ctx, output, output_len, &prefs));
FREE_AND_NULL(output);
BAIL_ON_LZ4_ERROR(output_len = LZ4F_compressBound(strlen(short_input),
&prefs));
printf("update output length: %zu\n", output_len);
BAIL_ON_NULL(output = malloc(sizeof(char) * output_len));
BAIL_ON_LZ4_ERROR(LZ4F_compressUpdate(ctx, output, output_len, short_input,
strlen(short_input), NULL));
FREE_AND_NULL(output);
BAIL_ON_LZ4_ERROR(output_len = LZ4F_compressBound(0, &prefs));
printf("end output length: %zu\n", output_len);
BAIL_ON_NULL(output = malloc(sizeof(char) * output_len));
BAIL_ON_LZ4_ERROR(LZ4F_compressEnd(ctx, output, output_len, NULL));
FREE_AND_NULL(output);
BAIL_ON_LZ4_ERROR(LZ4F_freeCompressionContext(ctx));
return 0;
bail:
free(output);
LZ4F_freeCompressionContext(ctx);
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment