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>
- #include "../lib/zstd/zstd_static.h"
- #include "../lib/zstd/zstd.c" //lol
- //MAX 512KB?
- #define BUF_SIZE (1<<(8+(11)))
- static int encode_file(FILE *in, FILE *out,int level){
- size_t r;
- ZSTD_Cctx *ctx;
- char *src, *buf = NULL;
- size_t size, n, k, count_in = 0, count_out, offset = 0, frame_size;
- ctx = ZSTD_createCCtx();
- if (!ctx) {
- fprintf(stderr,"Failed to create context\n");
- return 1;
- }
- r = 1;
- src = malloc(BUF_SIZE);
- if (!src) {
- fprintf(stderr,"Not enough memory\n");
- goto cleanup;
- }
- frame_size = ZSTD_compressBound(BUF_SIZE);
- size = frame_size;
- buf = malloc(size);
- if (!buf) {
- fprintf(stderr,"Not enough memory\n");
- goto cleanup;
- }
- n = offset = count_out = ZSTD_compressBegin(ctx, buf, size);
- if (ZSTD_isError(n)) {
- fprintf(stderr,"Failed to start compression: error %zd\n", n);
- goto cleanup;
- }
- for (;;) {
- k = fread(src, 1, BUF_SIZE, in);
- if (k == 0) {
- break;
- }
- count_in += k;
- n = ZSTD_compressContinue(ctx, buf + offset, size - offset, src, k);
- if (ZSTD_isError(n)) {
- fprintf(stderr,"Compression failed: error %zd\n", n);
- goto cleanup;
- }
- offset += n;
- count_out += n;
- if (size - offset < frame_size) {
- k = fwrite(buf, 1, offset, out);
- if (k < offset) {
- goto cleanup;
- }
- offset = 0;
- }
- }
- n = ZSTD_compressEnd(ctx, buf + offset, size - offset);
- if (ZSTD_isError(n)) {
- goto cleanup;
- }
- offset += n;
- count_out += n;
- k = fwrite(buf, 1, offset, out);
- if (k < offset) {
- goto cleanup;
- }
- //*size_in = count_in;
- //*size_out = count_out;
- r = 0;
- cleanup:
- if (ctx)
- ZSTD_freeCCtx(ctx);
- free(src);
- free(buf);
- return r;
- }
- static int decode_file(FILE *in, FILE *out){
- size_t r;
- ZSTD_Dctx *ctx;
- char *src, *buf = NULL;
- size_t size = BUF_SIZE*2, n = 0, k, count_in = 0, count_out = 0, offset = 0;
- int i = -1;
- ctx = ZSTD_createDCtx();
- if (!ctx) {
- fprintf(stderr,"Failed to create context\n");
- return 1;
- }
- ZSTD_resetDCtx(ctx);
- r = 1;
- src = malloc(BUF_SIZE);
- if (!src) {
- fprintf(stderr,"Not enough memory\n");
- goto cleanup;
- }
- buf = malloc(size);
- if (!buf) {
- fprintf(stderr,"Not enough memory\n");
- goto cleanup;
- }
- for (;;) {
- size_t srcsize=ZSTD_nextSrcSizeToDecompress(ctx);
- if(!srcsize){
- ZSTD_resetDCtx(ctx);
- srcsize=ZSTD_nextSrcSizeToDecompress(ctx);
- }
- k = fread(src, 1, srcsize, in);
- if (k == 0) {
- break;
- }
- count_in += k;
- n = ZSTD_decompressContinue(ctx, buf+offset, size-offset, src, k);
- if (ZSTD_isError(n)) {
- fprintf(stderr,"Decompression failed: error %zd\n", n);
- goto cleanup;
- }
- count_out += n;
- k = fwrite(buf+offset, 1, n, out);
- if (k < n) {
- goto cleanup;
- }
- offset += n;
- i++;
- if(i==8){
- i=0;
- offset=0;
- }
- }
- //*size_in = count_in;
- //*size_out = count_out;
- r = 0;
- cleanup:
- if (ctx)
- ZSTD_freeDCtx(ctx);
- free(src);
- free(buf);
- return r;
- }
- int zzstd(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':1);
- /*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\n");
- return -1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement