Advertisement
cielavenir

zzstd update

Dec 2nd, 2015
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.36 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <errno.h>
  5.  
  6. #if !defined(FEOS)
  7. #include "../lib/zstd/zstd_buffered.h"
  8.  
  9. static int encode_file(FILE *in, FILE *out,int level){
  10.     size_t r;
  11.     ZBUFF_CCtx *ctx;
  12.     char *src, *buf = NULL;
  13.     size_t n, k, count_in = 0, count_out, offset = 0, buf_size, frame_size;
  14.  
  15.     ctx = ZBUFF_createCCtx();
  16.     if (!ctx) {
  17.         fprintf(stderr,"Failed to create context\n");
  18.         return 1;
  19.     }
  20.     r = 1;
  21.  
  22.     n = ZBUFF_compressInit(ctx,level);
  23.     if (ZBUFF_isError(n)) {
  24.         fprintf(stderr,"Compression init failed: error %zd\n", n);
  25.         goto cleanup;
  26.     }
  27.  
  28.     buf_size = ZBUFF_recommendedCInSize();
  29.     src = malloc(buf_size);
  30.     if (!src) {
  31.         fprintf(stderr,"Not enough memory\n");
  32.         goto cleanup;
  33.     }
  34.  
  35.     frame_size = ZBUFF_recommendedCOutSize();
  36.     buf = malloc(frame_size);
  37.     if (!buf) {
  38.         fprintf(stderr,"Not enough memory\n");
  39.         goto cleanup;
  40.     }
  41.  
  42.     for (;;) {
  43.         if(offset==0){
  44.             k = fread(src+offset, 1, buf_size-offset, in);
  45.             //if (k == 0) {
  46.             //  break;
  47.             //}
  48.             count_in += k;
  49.         }
  50.  
  51.         size_t dstsize=frame_size,srcsize=k;
  52.         n = ZBUFF_compressContinue(ctx, buf, &dstsize, src+offset, &srcsize);
  53.         if (ZBUFF_isError(n)) {
  54.             fprintf(stderr,"Compression failed: error %zd\n", n);
  55.             goto cleanup;
  56.         }
  57.         k-=srcsize;
  58.  
  59.         offset += srcsize;
  60.         count_out += dstsize;
  61.         if (fwrite(buf, 1, dstsize, out) < dstsize) {
  62.             goto cleanup;
  63.         }
  64.         if(dstsize==0)break;
  65.         if(offset==buf_size)offset=0;
  66.     }
  67.  
  68.     for (;;) {
  69.         size_t dstsize=frame_size;
  70.         n = ZBUFF_compressEnd(ctx, buf, &dstsize);
  71.         if (ZBUFF_isError(n)) {
  72.             goto cleanup;
  73.         }
  74.  
  75.         count_out += dstsize;
  76.         if (fwrite(buf, 1, dstsize, out) < dstsize) {
  77.             goto cleanup;
  78.         }
  79.         if(n==0)break;
  80.     }
  81.  
  82.     //*size_in = count_in;
  83.     //*size_out = count_out;
  84.     r = 0;
  85.  cleanup:
  86.     if (ctx)
  87.         ZBUFF_freeCCtx(ctx);
  88.     free(src);
  89.     free(buf);
  90.     return r;
  91. }
  92.  
  93. static int decode_file(FILE *in, FILE *out){
  94.     size_t r;
  95.     ZBUFF_DCtx *ctx;
  96.     char *src, *buf = NULL;
  97.     size_t n = 0, k, count_in = 0, count_out = 0, offset = 0, frame_size, buf_size;
  98.     int i = -1;
  99.  
  100.     ctx = ZBUFF_createDCtx();
  101.     if (!ctx) {
  102.         fprintf(stderr,"Failed to create context\n");
  103.         return 1;
  104.     }
  105.     r = 1;
  106.  
  107.     //ZBUFF_resetDCtx(ctx);
  108.     n = ZBUFF_decompressInit(ctx);
  109.     if (ZBUFF_isError(n)) {
  110.         fprintf(stderr,"Compression init failed: error %zd\n", n);
  111.         goto cleanup;
  112.     }
  113.  
  114.     frame_size = ZBUFF_recommendedDInSize();
  115.     src = malloc(frame_size);
  116.     if (!src) {
  117.         fprintf(stderr,"Not enough memory\n");
  118.         goto cleanup;
  119.     }
  120.  
  121.     buf_size = ZBUFF_recommendedDOutSize();
  122.     buf = malloc(buf_size);
  123.     if (!buf) {
  124.         fprintf(stderr,"Not enough memory\n");
  125.         goto cleanup;
  126.     }
  127.  
  128.     for (;;) {
  129.         if(offset==0){
  130.             k = fread(src+offset, 1, buf_size-offset, in);
  131.             //if (k == 0) {
  132.             //  break;
  133.             //}
  134.             count_in += k;
  135.         }
  136.  
  137.         size_t dstsize=frame_size,srcsize=k;
  138.         n = ZBUFF_decompressContinue(ctx, buf, &dstsize, src+offset, &srcsize);
  139.         if (ZBUFF_isError(n)) {
  140.             fprintf(stderr,"Decompression failed: error %zd\n", n);
  141.             goto cleanup;
  142.         }
  143.         k-=srcsize;
  144.  
  145.         offset += srcsize;
  146.         count_out += dstsize;
  147.         if (fwrite(buf, 1, dstsize, out) < dstsize) {
  148.             goto cleanup;
  149.         }
  150.         if(n==0)break;
  151.         if(offset==buf_size)offset=0;
  152.     }
  153.  
  154.     //*size_in = count_in;
  155.     //*size_out = count_out;
  156.     r = 0;
  157.  cleanup:
  158.     if (ctx)
  159.         ZBUFF_freeDCtx(ctx);
  160.     free(src);
  161.     free(buf);
  162.     return r;
  163. }
  164.  
  165. int main(const int argc, const char **argv){
  166.     char mode,level;
  167.     if(argc<2)goto argerror;
  168.     mode=argv[1][0],level=argv[1][1];
  169.     if(!mode)goto argerror;
  170.     if(mode=='-')mode=argv[1][1],level=argv[1][2];
  171.     if(mode!='e'&&mode!='c'&&mode!='d')goto argerror;
  172.     if(isatty(fileno(stdin))&&isatty(fileno(stdout)))goto argerror;
  173.  
  174.     int i=mode=='d'?decode_file(stdin,stdout):encode_file(stdin,stdout,level?(level-'0')*2:8);
  175.  
  176.     /*switch(i){
  177.         case 0: fprintf(stderr,"Everything is Ok\n");break;
  178.         case 1: fprintf(stderr,"Unexpected EOF\n");break;
  179.         case 2: fprintf(stderr,"File Corrupted\n");break;
  180.         case 3: fprintf(stderr,"Decode Error\n");break;
  181.         case 4: fprintf(stderr,"Format Unsupported\n");break;
  182.         default: fprintf(stderr,"Unknown Error\n");
  183.     }*/
  184.     return i;
  185.  
  186. argerror:
  187.     fprintf(stderr,"zzstd -[c|d] < in > out\ncompression levels are \"0123456789:\" (0-20 step 2)\n");
  188.     return -1;
  189. }
  190.  
  191. #else
  192.  
  193. int zzstd(const int argc, const char** argv){
  194.     fprintf(stderr,"sorry: clz() instruction is not supported, thus cannot be used on this platform.\n");
  195.     return -1;
  196. }
  197.  
  198. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement