Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <errno.h>
- #if !defined(FEOS)
- #include "../lib/zstd/zstd_buffered.h"
- static int encode_file(FILE *in, FILE *out,int level){
- size_t r;
- ZBUFF_CCtx *ctx;
- char *src, *buf = NULL;
- size_t n, k, count_in = 0, count_out, offset = 0, buf_size, frame_size;
- ctx = ZBUFF_createCCtx();
- if (!ctx) {
- fprintf(stderr,"Failed to create context\n");
- return 1;
- }
- r = 1;
- n = ZBUFF_compressInit(ctx,level);
- if (ZBUFF_isError(n)) {
- fprintf(stderr,"Compression init failed: error %zd\n", n);
- goto cleanup;
- }
- buf_size = ZBUFF_recommendedCInSize();
- src = malloc(buf_size);
- if (!src) {
- fprintf(stderr,"Not enough memory\n");
- goto cleanup;
- }
- frame_size = ZBUFF_recommendedCOutSize();
- buf = malloc(frame_size);
- if (!buf) {
- fprintf(stderr,"Not enough memory\n");
- goto cleanup;
- }
- for (;;) {
- if(offset==0){
- k = fread(src+offset, 1, buf_size-offset, in);
- //if (k == 0) {
- // break;
- //}
- count_in += k;
- }
- size_t dstsize=frame_size,srcsize=k;
- n = ZBUFF_compressContinue(ctx, buf, &dstsize, src+offset, &srcsize);
- if (ZBUFF_isError(n)) {
- fprintf(stderr,"Compression failed: error %zd\n", n);
- goto cleanup;
- }
- k-=srcsize;
- offset += srcsize;
- count_out += dstsize;
- if (fwrite(buf, 1, dstsize, out) < dstsize) {
- goto cleanup;
- }
- if(dstsize==0)break;
- if(offset==buf_size)offset=0;
- }
- for (;;) {
- size_t dstsize=frame_size;
- n = ZBUFF_compressEnd(ctx, buf, &dstsize);
- if (ZBUFF_isError(n)) {
- goto cleanup;
- }
- count_out += dstsize;
- if (fwrite(buf, 1, dstsize, out) < dstsize) {
- goto cleanup;
- }
- if(n==0)break;
- }
- //*size_in = count_in;
- //*size_out = count_out;
- r = 0;
- cleanup:
- if (ctx)
- ZBUFF_freeCCtx(ctx);
- free(src);
- free(buf);
- return r;
- }
- static int decode_file(FILE *in, FILE *out){
- size_t r;
- ZBUFF_DCtx *ctx;
- char *src, *buf = NULL;
- size_t n = 0, k, count_in = 0, count_out = 0, offset = 0, frame_size, buf_size;
- int i = -1;
- ctx = ZBUFF_createDCtx();
- if (!ctx) {
- fprintf(stderr,"Failed to create context\n");
- return 1;
- }
- r = 1;
- //ZBUFF_resetDCtx(ctx);
- n = ZBUFF_decompressInit(ctx);
- if (ZBUFF_isError(n)) {
- fprintf(stderr,"Compression init failed: error %zd\n", n);
- goto cleanup;
- }
- frame_size = ZBUFF_recommendedDInSize();
- src = malloc(frame_size);
- if (!src) {
- fprintf(stderr,"Not enough memory\n");
- goto cleanup;
- }
- buf_size = ZBUFF_recommendedDOutSize();
- buf = malloc(buf_size);
- if (!buf) {
- fprintf(stderr,"Not enough memory\n");
- goto cleanup;
- }
- for (;;) {
- if(offset==0){
- k = fread(src+offset, 1, buf_size-offset, in);
- //if (k == 0) {
- // break;
- //}
- count_in += k;
- }
- size_t dstsize=frame_size,srcsize=k;
- n = ZBUFF_decompressContinue(ctx, buf, &dstsize, src+offset, &srcsize);
- if (ZBUFF_isError(n)) {
- fprintf(stderr,"Decompression failed: error %zd\n", n);
- goto cleanup;
- }
- k-=srcsize;
- offset += srcsize;
- count_out += dstsize;
- if (fwrite(buf, 1, dstsize, out) < dstsize) {
- goto cleanup;
- }
- if(n==0)break;
- if(offset==buf_size)offset=0;
- }
- //*size_in = count_in;
- //*size_out = count_out;
- r = 0;
- cleanup:
- if (ctx)
- ZBUFF_freeDCtx(ctx);
- free(src);
- free(buf);
- return r;
- }
- int main(const int argc, const char **argv){
- char mode,level;
- if(argc<2)goto argerror;
- mode=argv[1][0],level=argv[1][1];
- if(!mode)goto argerror;
- if(mode=='-')mode=argv[1][1],level=argv[1][2];
- if(mode!='e'&&mode!='c'&&mode!='d')goto argerror;
- if(isatty(fileno(stdin))&&isatty(fileno(stdout)))goto argerror;
- int i=mode=='d'?decode_file(stdin,stdout):encode_file(stdin,stdout,level?(level-'0')*2:8);
- /*switch(i){
- case 0: fprintf(stderr,"Everything is Ok\n");break;
- case 1: fprintf(stderr,"Unexpected EOF\n");break;
- case 2: fprintf(stderr,"File Corrupted\n");break;
- case 3: fprintf(stderr,"Decode Error\n");break;
- case 4: fprintf(stderr,"Format Unsupported\n");break;
- default: fprintf(stderr,"Unknown Error\n");
- }*/
- return i;
- argerror:
- fprintf(stderr,"zzstd -[c|d] < in > out\ncompression levels are \"0123456789:\" (0-20 step 2)\n");
- return -1;
- }
- #else
- int zzstd(const int argc, const char** argv){
- fprintf(stderr,"sorry: clz() instruction is not supported, thus cannot be used on this platform.\n");
- return -1;
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement