Advertisement
cielavenir

zstd compression

Sep 24th, 2015
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <errno.h>
  5.  
  6. #include "../lib/zstd/zstd_static.h"
  7. #include "../lib/zstd/zstd.c" //lol
  8.  
  9. //MAX 512KB?
  10. #define BUF_SIZE (1<<(8+(11)))
  11.  
  12. static int encode_file(FILE *in, FILE *out,int level){
  13. size_t r;
  14. ZSTD_Cctx *ctx;
  15. char *src, *buf = NULL;
  16. size_t size, n, k, count_in = 0, count_out, offset = 0, frame_size;
  17.  
  18. ctx = ZSTD_createCCtx();
  19. if (!ctx) {
  20. fprintf(stderr,"Failed to create context\n");
  21. return 1;
  22. }
  23. r = 1;
  24.  
  25. src = malloc(BUF_SIZE);
  26. if (!src) {
  27. fprintf(stderr,"Not enough memory\n");
  28. goto cleanup;
  29. }
  30.  
  31. frame_size = ZSTD_compressBound(BUF_SIZE);
  32. size = frame_size;
  33. buf = malloc(size);
  34. if (!buf) {
  35. fprintf(stderr,"Not enough memory\n");
  36. goto cleanup;
  37. }
  38.  
  39. n = offset = count_out = ZSTD_compressBegin(ctx, buf, size);
  40. if (ZSTD_isError(n)) {
  41. fprintf(stderr,"Failed to start compression: error %zd\n", n);
  42. goto cleanup;
  43. }
  44.  
  45. for (;;) {
  46. k = fread(src, 1, BUF_SIZE, in);
  47. if (k == 0) {
  48. break;
  49. }
  50. count_in += k;
  51.  
  52. n = ZSTD_compressContinue(ctx, buf + offset, size - offset, src, k);
  53. if (ZSTD_isError(n)) {
  54. fprintf(stderr,"Compression failed: error %zd\n", n);
  55. goto cleanup;
  56. }
  57.  
  58. offset += n;
  59. count_out += n;
  60. if (size - offset < frame_size) {
  61.  
  62. k = fwrite(buf, 1, offset, out);
  63. if (k < offset) {
  64. goto cleanup;
  65. }
  66.  
  67. offset = 0;
  68. }
  69. }
  70.  
  71. n = ZSTD_compressEnd(ctx, buf + offset, size - offset);
  72. if (ZSTD_isError(n)) {
  73. goto cleanup;
  74. }
  75.  
  76. offset += n;
  77. count_out += n;
  78.  
  79. k = fwrite(buf, 1, offset, out);
  80. if (k < offset) {
  81. goto cleanup;
  82. }
  83.  
  84. //*size_in = count_in;
  85. //*size_out = count_out;
  86. r = 0;
  87. cleanup:
  88. if (ctx)
  89. ZSTD_freeCCtx(ctx);
  90. free(src);
  91. free(buf);
  92. return r;
  93. }
  94.  
  95. static int decode_file(FILE *in, FILE *out){
  96. size_t r;
  97. ZSTD_Dctx *ctx;
  98. char *src, *buf = NULL;
  99. size_t size = BUF_SIZE*2, n = 0, k, count_in = 0, count_out = 0, offset = 0;
  100. int i = -1;
  101.  
  102. ctx = ZSTD_createDCtx();
  103. if (!ctx) {
  104. fprintf(stderr,"Failed to create context\n");
  105. return 1;
  106. }
  107. ZSTD_resetDCtx(ctx);
  108. r = 1;
  109.  
  110. src = malloc(BUF_SIZE);
  111. if (!src) {
  112. fprintf(stderr,"Not enough memory\n");
  113. goto cleanup;
  114. }
  115. buf = malloc(size);
  116. if (!buf) {
  117. fprintf(stderr,"Not enough memory\n");
  118. goto cleanup;
  119. }
  120.  
  121. for (;;) {
  122. size_t srcsize=ZSTD_nextSrcSizeToDecompress(ctx);
  123. if(!srcsize){
  124. ZSTD_resetDCtx(ctx);
  125. srcsize=ZSTD_nextSrcSizeToDecompress(ctx);
  126. }
  127. k = fread(src, 1, srcsize, in);
  128. if (k == 0) {
  129. break;
  130. }
  131.  
  132. count_in += k;
  133. n = ZSTD_decompressContinue(ctx, buf+offset, size-offset, src, k);
  134. if (ZSTD_isError(n)) {
  135. fprintf(stderr,"Decompression failed: error %zd\n", n);
  136. goto cleanup;
  137. }
  138. count_out += n;
  139.  
  140. k = fwrite(buf+offset, 1, n, out);
  141. if (k < n) {
  142. goto cleanup;
  143. }
  144. offset += n;
  145. i++;
  146. if(i==8){
  147. i=0;
  148. offset=0;
  149. }
  150. }
  151.  
  152. //*size_in = count_in;
  153. //*size_out = count_out;
  154. r = 0;
  155. cleanup:
  156. if (ctx)
  157. ZSTD_freeDCtx(ctx);
  158. free(src);
  159. free(buf);
  160. return r;
  161. }
  162.  
  163. int zzstd(const int argc, const char **argv){
  164. char mode,level;
  165. if(argc<2)goto argerror;
  166. mode=argv[1][0],level=argv[1][1];
  167. if(!mode)goto argerror;
  168. if(mode=='-')mode=argv[1][1],level=argv[1][2];
  169. if(mode!='e'&&mode!='c'&&mode!='d')goto argerror;
  170. if(isatty(fileno(stdin))&&isatty(fileno(stdout)))goto argerror;
  171.  
  172. int i=mode=='d'?decode_file(stdin,stdout):encode_file(stdin,stdout,level?level-'0':1);
  173.  
  174. /*switch(i){
  175. case 0: fprintf(stderr,"Everything is Ok\n");break;
  176. case 1: fprintf(stderr,"Unexpected EOF\n");break;
  177. case 2: fprintf(stderr,"File Corrupted\n");break;
  178. case 3: fprintf(stderr,"Decode Error\n");break;
  179. case 4: fprintf(stderr,"Format Unsupported\n");break;
  180. default: fprintf(stderr,"Unknown Error\n");
  181. }*/
  182. return i;
  183.  
  184. argerror:
  185. fprintf(stderr,"zzstd -[c|d] < in > out\n");
  186. return -1;
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement