lz4demo.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /*
  2. LZ4Demo - Demo CLI program using LZ4 compression
  3. Copyright (C) Yann Collet 2011-2012
  4. GPL v2 License
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License along
  14. with this program; if not, write to the Free Software Foundation, Inc.,
  15. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  16. You can contact the author at :
  17. - LZ4 homepage : http://fastcompression.blogspot.com/p/lz4.html
  18. - LZ4 source repository : http://code.google.com/p/lz4/
  19. */
  20. /*
  21. Note : this is *only* a demo program, an example to show how LZ4 can be used.
  22. It is not considered part of LZ4 compression library.
  23. The license of LZ4 is BSD.
  24. The license of the demo program is GPL.
  25. */
  26. //**************************************
  27. // Compiler Options
  28. //**************************************
  29. // Disable some Visual warning messages
  30. #define _CRT_SECURE_NO_WARNINGS
  31. #define _CRT_SECURE_NO_DEPRECATE // VS2005
  32. //****************************
  33. // Includes
  34. //****************************
  35. #include <stdio.h> // fprintf, fopen, fread, _fileno(?)
  36. #include <stdlib.h> // malloc
  37. #include <string.h> // strcmp
  38. #include <time.h> // clock
  39. #ifdef _WIN32
  40. #include <io.h> // _setmode
  41. #include <fcntl.h> // _O_BINARY
  42. #endif
  43. #include "lz4.h"
  44. #include "lz4hc.h"
  45. #include "bench.h"
  46. //**************************************
  47. // Compiler-specific functions
  48. //**************************************
  49. #define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
  50. #if defined(_MSC_VER) // Visual Studio
  51. #define swap32 _byteswap_ulong
  52. #elif GCC_VERSION >= 403
  53. #define swap32 __builtin_bswap32
  54. #else
  55. static inline unsigned int swap32(unsigned int x) {
  56. return ((x << 24) & 0xff000000 ) |
  57. ((x << 8) & 0x00ff0000 ) |
  58. ((x >> 8) & 0x0000ff00 ) |
  59. ((x >> 24) & 0x000000ff );
  60. }
  61. #endif
  62. //****************************
  63. // Constants
  64. //****************************
  65. #define COMPRESSOR_NAME "Compression CLI using LZ4 algorithm"
  66. #define COMPRESSOR_VERSION ""
  67. #define COMPILED __DATE__
  68. #define AUTHOR "Yann Collet"
  69. #define EXTENSION ".lz4"
  70. #define WELCOME_MESSAGE "*** %s %s, by %s (%s) ***\n", COMPRESSOR_NAME, COMPRESSOR_VERSION, AUTHOR, COMPILED
  71. #define CHUNKSIZE (8<<20) // 8 MB
  72. #define CACHELINE 64
  73. #define ARCHIVE_MAGICNUMBER 0x184C2102
  74. #define ARCHIVE_MAGICNUMBER_SIZE 4
  75. //**************************************
  76. // Architecture Macros
  77. //**************************************
  78. static const int one = 1;
  79. #define CPU_LITTLE_ENDIAN (*(char*)(&one))
  80. #define CPU_BIG_ENDIAN (!CPU_LITTLE_ENDIAN)
  81. #define LITTLE_ENDIAN32(i) if (CPU_BIG_ENDIAN) { i = swap32(i); }
  82. //**************************************
  83. // Macros
  84. //**************************************
  85. #define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
  86. //****************************
  87. // Functions
  88. //****************************
  89. int usage(char* exename)
  90. {
  91. DISPLAY( "Usage :\n");
  92. DISPLAY( " %s [arg] input output\n", exename);
  93. DISPLAY( "Arguments :\n");
  94. DISPLAY( " -c0: Fast compression (default) \n");
  95. DISPLAY( " -c1: High compression \n");
  96. DISPLAY( " -d : decompression \n");
  97. DISPLAY( " -b#: Benchmark files, using # compression level\n");
  98. DISPLAY( " -t : check compressed file \n");
  99. DISPLAY( " -h : help (this text)\n");
  100. DISPLAY( "input : can be 'stdin' (pipe) or a filename\n");
  101. DISPLAY( "output : can be 'stdout'(pipe) or a filename or 'null'\n");
  102. return 0;
  103. }
  104. int badusage(char* exename)
  105. {
  106. DISPLAY("Wrong parameters\n");
  107. usage(exename);
  108. return 0;
  109. }
  110. int get_fileHandle(char* input_filename, char* output_filename, FILE** pfinput, FILE** pfoutput)
  111. {
  112. char stdinmark[] = "stdin";
  113. char stdoutmark[] = "stdout";
  114. if (!strcmp (input_filename, stdinmark)) {
  115. DISPLAY( "Using stdin for input\n");
  116. *pfinput = stdin;
  117. #ifdef _WIN32 // Need to set stdin/stdout to binary mode specifically for windows
  118. _setmode( _fileno( stdin ), _O_BINARY );
  119. #endif
  120. } else {
  121. *pfinput = fopen( input_filename, "rb" );
  122. }
  123. if (!strcmp (output_filename, stdoutmark)) {
  124. DISPLAY( "Using stdout for output\n");
  125. *pfoutput = stdout;
  126. #ifdef _WIN32 // Need to set stdin/stdout to binary mode specifically for windows
  127. _setmode( _fileno( stdout ), _O_BINARY );
  128. #endif
  129. } else {
  130. *pfoutput = fopen( output_filename, "wb" );
  131. }
  132. if ( *pfinput==0 ) { DISPLAY( "Pb opening %s\n", input_filename); return 2; }
  133. if ( *pfoutput==0) { DISPLAY( "Pb opening %s\n", output_filename); return 3; }
  134. return 0;
  135. }
  136. int compress_file(char* input_filename, char* output_filename, int compressionlevel)
  137. {
  138. int (*compressionFunction)(const char*, char*, int);
  139. unsigned long long filesize = 0;
  140. unsigned long long compressedfilesize = ARCHIVE_MAGICNUMBER_SIZE;
  141. unsigned int u32var;
  142. char* in_buff;
  143. char* out_buff;
  144. FILE* finput;
  145. FILE* foutput;
  146. int r;
  147. int displayLevel = (compressionlevel>0);
  148. clock_t start, end;
  149. size_t sizeCheck;
  150. // Init
  151. switch (compressionlevel)
  152. {
  153. case 0 : compressionFunction = LZ4_compress; break;
  154. case 1 : compressionFunction = LZ4_compressHC; break;
  155. default : compressionFunction = LZ4_compress;
  156. }
  157. start = clock();
  158. r = get_fileHandle(input_filename, output_filename, &finput, &foutput);
  159. if (r) return r;
  160. // Allocate Memory
  161. in_buff = (char*)malloc(CHUNKSIZE);
  162. out_buff = (char*)malloc(LZ4_compressBound(CHUNKSIZE));
  163. if (!in_buff || !out_buff) { DISPLAY("Allocation error : not enough memory\n"); return 8; }
  164. // Write Archive Header
  165. u32var = ARCHIVE_MAGICNUMBER;
  166. LITTLE_ENDIAN32(u32var);
  167. *(unsigned int*)out_buff = u32var;
  168. sizeCheck = fwrite(out_buff, 1, ARCHIVE_MAGICNUMBER_SIZE, foutput);
  169. if (sizeCheck!=ARCHIVE_MAGICNUMBER_SIZE) { DISPLAY("write error\n"); return 10; }
  170. // Main Loop
  171. while (1)
  172. {
  173. int outSize;
  174. // Read Block
  175. int inSize = (int) fread(in_buff, (size_t)1, (size_t)CHUNKSIZE, finput);
  176. if( inSize<=0 ) break;
  177. filesize += inSize;
  178. if (displayLevel) DISPLAY("Read : %i MB \r", (int)(filesize>>20));
  179. // Compress Block
  180. outSize = compressionFunction(in_buff, out_buff+4, inSize);
  181. compressedfilesize += outSize+4;
  182. if (displayLevel) DISPLAY("Read : %i MB ==> %.2f%%\r", (int)(filesize>>20), (double)compressedfilesize/filesize*100);
  183. // Write Block
  184. LITTLE_ENDIAN32(outSize);
  185. * (unsigned int*) out_buff = outSize;
  186. LITTLE_ENDIAN32(outSize);
  187. sizeCheck = fwrite(out_buff, 1, outSize+4, foutput);
  188. if (sizeCheck!=(size_t)(outSize+4)) { DISPLAY("write error\n"); return 11; }
  189. }
  190. // Status
  191. end = clock();
  192. DISPLAY( "Compressed %llu bytes into %llu bytes ==> %.2f%%\n",
  193. (unsigned long long) filesize, (unsigned long long) compressedfilesize, (double)compressedfilesize/filesize*100);
  194. {
  195. double seconds = (double)(end - start)/CLOCKS_PER_SEC;
  196. DISPLAY( "Done in %.2f s ==> %.2f MB/s\n", seconds, (double)filesize / seconds / 1024 / 1024);
  197. }
  198. // Close & Free
  199. free(in_buff);
  200. free(out_buff);
  201. fclose(finput);
  202. fclose(foutput);
  203. return 0;
  204. }
  205. int decode_file(char* input_filename, char* output_filename)
  206. {
  207. unsigned long long filesize = 0;
  208. char* in_buff;
  209. char* out_buff;
  210. size_t uselessRet;
  211. int sinkint;
  212. unsigned int chunkSize;
  213. FILE* finput;
  214. FILE* foutput;
  215. clock_t start, end;
  216. int r;
  217. size_t sizeCheck;
  218. // Init
  219. start = clock();
  220. r = get_fileHandle(input_filename, output_filename, &finput, &foutput);
  221. if (r) return r;
  222. // Allocate Memory
  223. in_buff = (char*)malloc(LZ4_compressBound(CHUNKSIZE));
  224. out_buff = (char*)malloc(CHUNKSIZE);
  225. if (!in_buff || !out_buff) { DISPLAY("Allocation error : not enough memory\n"); return 7; }
  226. // Check Archive Header
  227. chunkSize = 0;
  228. uselessRet = fread(&chunkSize, 1, ARCHIVE_MAGICNUMBER_SIZE, finput);
  229. LITTLE_ENDIAN32(chunkSize);
  230. if (chunkSize != ARCHIVE_MAGICNUMBER) { DISPLAY("Unrecognized header : file cannot be decoded\n"); return 6; }
  231. // Main Loop
  232. while (1)
  233. {
  234. // Block Size
  235. uselessRet = fread(&chunkSize, 1, 4, finput);
  236. if( uselessRet==0 ) break; // Nothing to read : file read is completed
  237. LITTLE_ENDIAN32(chunkSize);
  238. if (chunkSize == ARCHIVE_MAGICNUMBER)
  239. continue; // appended compressed stream
  240. // Read Block
  241. uselessRet = fread(in_buff, 1, chunkSize, finput);
  242. // Decode Block
  243. sinkint = LZ4_uncompress_unknownOutputSize(in_buff, out_buff, chunkSize, CHUNKSIZE);
  244. if (sinkint < 0) { DISPLAY("Decoding Failed ! Corrupted input !\n"); return 9; }
  245. filesize += sinkint;
  246. // Write Block
  247. sizeCheck = fwrite(out_buff, 1, sinkint, foutput);
  248. if (sizeCheck != (size_t)sinkint) { DISPLAY("write error\n"); return 12; }
  249. }
  250. // Status
  251. end = clock();
  252. DISPLAY( "Successfully decoded %llu bytes \n", (unsigned long long)filesize);
  253. {
  254. double seconds = (double)(end - start)/CLOCKS_PER_SEC;
  255. DISPLAY( "Done in %.2f s ==> %.2f MB/s\n", seconds, (double)filesize / seconds / 1024 / 1024);
  256. }
  257. // Close & Free
  258. free(in_buff);
  259. free(out_buff);
  260. fclose(finput);
  261. fclose(foutput);
  262. return 0;
  263. }
  264. int main(int argc, char** argv)
  265. {
  266. int i,
  267. cLevel=0,
  268. decode=0,
  269. bench=0,
  270. filenamesStart=2;
  271. char* exename=argv[0];
  272. char* input_filename=0;
  273. char* output_filename=0;
  274. #ifdef _WIN32
  275. char nulmark[] = "nul";
  276. #else
  277. char nulmark[] = "/dev/null";
  278. #endif
  279. char nullinput[] = "null";
  280. // Welcome message
  281. DISPLAY( WELCOME_MESSAGE);
  282. if (argc<2) { badusage(exename); return 1; }
  283. for(i=1; i<argc; i++)
  284. {
  285. char* argument = argv[i];
  286. if(!argument) continue; // Protection if argument empty
  287. // Select command
  288. if (argument[0]=='-')
  289. {
  290. argument ++;
  291. // Display help on usage
  292. if ( argument[0] =='h' ) { usage(exename); return 0; }
  293. // Compression (default)
  294. if ( argument[0] =='c' ) { if (argument[1] >='0') cLevel=argument[1] - '0'; continue; }
  295. // Decoding
  296. if ( argument[0] =='d' ) { decode=1; continue; }
  297. // Bench
  298. if ( argument[0] =='b' ) { bench=1; if (argument[1] >= '0') cLevel=argument[1] - '0'; continue; }
  299. // Modify Block Size (benchmark only)
  300. if ( argument[0] =='B' ) { int B = argument[1] - '0'; int S = 1 << (10 + 2*B); BMK_SetBlocksize(S); continue; }
  301. // Modify Nb Iterations (benchmark only)
  302. if ( argument[0] =='i' ) { int iters = argument[1] - '0'; BMK_SetNbIterations(iters); continue; }
  303. // Pause at the end (benchmark only)
  304. if ( argument[0] =='p' ) { BMK_SetPause(); continue; }
  305. // Test
  306. if ( argument[0] =='t' ) { decode=1; output_filename=nulmark; continue; }
  307. }
  308. // first provided filename is input
  309. if (!input_filename) { input_filename=argument; filenamesStart=i; continue; }
  310. // second provided filename is output
  311. if (!output_filename)
  312. {
  313. output_filename=argument;
  314. if (!strcmp (output_filename, nullinput)) output_filename = nulmark;
  315. continue;
  316. }
  317. }
  318. // No input filename ==> Error
  319. if(!input_filename) { badusage(exename); return 1; }
  320. if (bench) return BMK_benchFile(argv+filenamesStart, argc-filenamesStart, cLevel);
  321. // No output filename ==> Error
  322. if (!output_filename) { badusage(exename); return 1; }
  323. if (decode) return decode_file(input_filename, output_filename);
  324. return compress_file(input_filename, output_filename, cLevel); // Compression is 'default' action
  325. }