6pack.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. /*
  2. 6PACK - file compressor using FastLZ (lightning-fast compression library)
  3. Copyright (C) 2007 Ariya Hidayat (ariya@kde.org)
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. */
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #define SIXPACK_VERSION_MAJOR 0
  24. #define SIXPACK_VERSION_MINOR 1
  25. #define SIXPACK_VERSION_REVISION 0
  26. #define SIXPACK_VERSION_STRING "snapshot 20070615"
  27. #include "fastlz.h"
  28. #undef PATH_SEPARATOR
  29. #if defined(MSDOS) || defined(__MSDOS__) || defined(MSDOS)
  30. #define PATH_SEPARATOR '\\'
  31. #endif
  32. #if defined(WIN32) || defined(__NT__) || defined(_WIN32) || defined(__WIN32__)
  33. #define PATH_SEPARATOR '\\'
  34. #if defined(__BORLANDC__) || defined(_MSC_VER)
  35. #define inline __inline
  36. #endif
  37. #endif
  38. #ifndef PATH_SEPARATOR
  39. #define PATH_SEPARATOR '/'
  40. #endif
  41. #undef SIXPACK_BENCHMARK_WIN32
  42. #if defined(WIN32) || defined(__NT__) || defined(_WIN32) || defined(__WIN32__)
  43. #if defined(_MSC_VER) || defined(__GNUC__)
  44. #define SIXPACK_BENCHMARK_WIN32
  45. #include <windows.h>
  46. #endif
  47. #endif
  48. /* magic identifier for 6pack file */
  49. static unsigned char sixpack_magic[8] = {137, '6', 'P', 'K', 13, 10, 26, 10};
  50. #define BLOCK_SIZE (2*64*1024)
  51. /* prototypes */
  52. static inline unsigned long update_adler32(unsigned long checksum, const void *buf, int len);
  53. void usage(void);
  54. int detect_magic(FILE *f);
  55. void write_magic(FILE *f);
  56. void write_chunk_header(FILE* f, int id, int options, unsigned long size,
  57. unsigned long checksum, unsigned long extra);
  58. unsigned long block_compress(const unsigned char* input, unsigned long length, unsigned char* output);
  59. int pack_file_compressed(const char* input_file, int method, int level, FILE* f);
  60. int pack_file(int compress_level, const char* input_file, const char* output_file);
  61. /* for Adler-32 checksum algorithm, see RFC 1950 Section 8.2 */
  62. #define ADLER32_BASE 65521
  63. static inline unsigned long update_adler32(unsigned long checksum, const void *buf, int len)
  64. {
  65. const unsigned char* ptr = (const unsigned char*)buf;
  66. unsigned long s1 = checksum & 0xffff;
  67. unsigned long s2 = (checksum >> 16) & 0xffff;
  68. while(len>0)
  69. {
  70. unsigned k = len < 5552 ? len : 5552;
  71. len -= k;
  72. while(k >= 8)
  73. {
  74. s1 += *ptr++; s2 += s1;
  75. s1 += *ptr++; s2 += s1;
  76. s1 += *ptr++; s2 += s1;
  77. s1 += *ptr++; s2 += s1;
  78. s1 += *ptr++; s2 += s1;
  79. s1 += *ptr++; s2 += s1;
  80. s1 += *ptr++; s2 += s1;
  81. s1 += *ptr++; s2 += s1;
  82. k -= 8;
  83. }
  84. while(k-- > 0)
  85. {
  86. s1 += *ptr++; s2 += s1;
  87. }
  88. s1 = s1 % ADLER32_BASE;
  89. s2 = s2 % ADLER32_BASE;
  90. }
  91. return (s2 << 16) + s1;
  92. }
  93. void usage(void)
  94. {
  95. printf("6pack: high-speed file compression tool\n");
  96. printf("Copyright (C) 2007 Ariya Hidayat (ariya@kde.org)\n");
  97. printf("\n");
  98. printf("Usage: 6pack [options] input-file output-file\n");
  99. printf("\n");
  100. printf("Options:\n");
  101. printf(" -1 compress faster\n");
  102. printf(" -2 compress better\n");
  103. printf(" -v show program version\n");
  104. #ifdef SIXPACK_BENCHMARK_WIN32
  105. printf(" -mem check in-memory compression speed\n");
  106. #endif
  107. printf("\n");
  108. }
  109. /* return non-zero if magic sequence is detected */
  110. /* warning: reset the read pointer to the beginning of the file */
  111. int detect_magic(FILE *f)
  112. {
  113. unsigned char buffer[8];
  114. size_t bytes_read;
  115. int c;
  116. fseek(f, SEEK_SET, 0);
  117. bytes_read = fread(buffer, 1, 8, f);
  118. fseek(f, SEEK_SET, 0);
  119. if(bytes_read < 8)
  120. return 0;
  121. for(c = 0; c < 8; c++)
  122. if(buffer[c] != sixpack_magic[c])
  123. return 0;
  124. return -1;
  125. }
  126. void write_magic(FILE *f)
  127. {
  128. fwrite(sixpack_magic, 8, 1, f);
  129. }
  130. void write_chunk_header(FILE* f, int id, int options, unsigned long size,
  131. unsigned long checksum, unsigned long extra)
  132. {
  133. unsigned char buffer[16];
  134. buffer[0] = id & 255;
  135. buffer[1] = id >> 8;
  136. buffer[2] = options & 255;
  137. buffer[3] = options >> 8;
  138. buffer[4] = size & 255;
  139. buffer[5] = (size >> 8) & 255;
  140. buffer[6] = (size >> 16) & 255;
  141. buffer[7] = (size >> 24) & 255;
  142. buffer[8] = checksum & 255;
  143. buffer[9] = (checksum >> 8) & 255;
  144. buffer[10] = (checksum >> 16) & 255;
  145. buffer[11] = (checksum >> 24) & 255;
  146. buffer[12] = extra & 255;
  147. buffer[13] = (extra >> 8) & 255;
  148. buffer[14] = (extra >> 16) & 255;
  149. buffer[15] = (extra >> 24) & 255;
  150. fwrite(buffer, 16, 1, f);
  151. }
  152. int pack_file_compressed(const char* input_file, int method, int level, FILE* f)
  153. {
  154. FILE* in;
  155. unsigned long fsize;
  156. unsigned long checksum;
  157. const char* shown_name;
  158. unsigned char buffer[BLOCK_SIZE];
  159. unsigned char result[BLOCK_SIZE*2]; /* FIXME twice is too large */
  160. unsigned char progress[20];
  161. int c;
  162. unsigned long percent;
  163. unsigned long total_read;
  164. unsigned long total_compressed;
  165. int chunk_size;
  166. /* sanity check */
  167. in = fopen(input_file, "rb");
  168. if(!in)
  169. {
  170. printf("Error: could not open %s\n", input_file);
  171. return -1;
  172. }
  173. /* find size of the file */
  174. fseek(in, 0, SEEK_END);
  175. fsize = ftell(in);
  176. fseek(in, 0, SEEK_SET);
  177. /* already a 6pack archive? */
  178. if(detect_magic(in))
  179. {
  180. printf("Error: file %s is already a 6pack archive!\n", input_file);
  181. fclose(in);
  182. return -1;
  183. }
  184. /* truncate directory prefix, e.g. "foo/bar/FILE.txt" becomes "FILE.txt" */
  185. shown_name = input_file + strlen(input_file) - 1;
  186. while(shown_name > input_file)
  187. if(*(shown_name-1) == PATH_SEPARATOR)
  188. break;
  189. else
  190. shown_name--;
  191. /* chunk for File Entry */
  192. buffer[0] = fsize & 255;
  193. buffer[1] = (fsize >> 8) & 255;
  194. buffer[2] = (fsize >> 16) & 255;
  195. buffer[3] = (fsize >> 24) & 255;
  196. #if 0
  197. buffer[4] = (fsize >> 32) & 255;
  198. buffer[5] = (fsize >> 40) & 255;
  199. buffer[6] = (fsize >> 48) & 255;
  200. buffer[7] = (fsize >> 56) & 255;
  201. #else
  202. /* because fsize is only 32-bit */
  203. buffer[4] = 0;
  204. buffer[5] = 0;
  205. buffer[6] = 0;
  206. buffer[7] = 0;
  207. #endif
  208. buffer[8] = (strlen(shown_name)+1) & 255;
  209. buffer[9] = (strlen(shown_name)+1) >> 8;
  210. checksum = 1L;
  211. checksum = update_adler32(checksum, buffer, 10);
  212. checksum = update_adler32(checksum, shown_name, strlen(shown_name)+1);
  213. write_chunk_header(f, 1, 0, 10+strlen(shown_name)+1, checksum, 0);
  214. fwrite(buffer, 10, 1, f);
  215. fwrite(shown_name, strlen(shown_name)+1, 1, f);
  216. total_compressed = 16 + 10 + strlen(shown_name)+1;
  217. /* for progress status */
  218. memset(progress, ' ', 20);
  219. if(strlen(shown_name) < 16)
  220. for(c = 0; c < (int)strlen(shown_name); c++)
  221. progress[c] = shown_name[c];
  222. else
  223. {
  224. for(c = 0; c < 13; c++)
  225. progress[c] = shown_name[c];
  226. progress[13] = '.';
  227. progress[14] = '.';
  228. progress[15] = ' ';
  229. }
  230. progress[16] = '[';
  231. progress[17] = 0;
  232. printf("%s", progress);
  233. for(c = 0; c < 50; c++)
  234. printf(".");
  235. printf("]\r");
  236. printf("%s", progress);
  237. /* read file and place in archive */
  238. total_read = 0;
  239. percent = 0;
  240. for(;;)
  241. {
  242. int compress_method = method;
  243. int last_percent = (int)percent;
  244. size_t bytes_read = fread(buffer, 1, BLOCK_SIZE, in);
  245. if(bytes_read == 0)
  246. break;
  247. total_read += bytes_read;
  248. /* for progress */
  249. if(fsize < (1<<24))
  250. percent = total_read * 100 / fsize;
  251. else
  252. percent = total_read/256 * 100 / (fsize >>8);
  253. percent >>= 1;
  254. while(last_percent < (int)percent)
  255. {
  256. printf("#");
  257. last_percent++;
  258. }
  259. /* too small, don't bother to compress */
  260. if(bytes_read < 32)
  261. compress_method = 0;
  262. /* write to output */
  263. switch(compress_method)
  264. {
  265. /* FastLZ */
  266. case 1:
  267. chunk_size = fastlz_compress_level(level, buffer, bytes_read, result);
  268. checksum = update_adler32(1L, result, chunk_size);
  269. write_chunk_header(f, 17, 1, chunk_size, checksum, bytes_read);
  270. fwrite(result, 1, chunk_size, f);
  271. total_compressed += 16;
  272. total_compressed += chunk_size;
  273. break;
  274. /* uncompressed, also fallback method */
  275. case 0:
  276. default:
  277. checksum = 1L;
  278. checksum = update_adler32(checksum, buffer, bytes_read);
  279. write_chunk_header(f, 17, 0, bytes_read, checksum, bytes_read);
  280. fwrite(buffer, 1, bytes_read, f);
  281. total_compressed += 16;
  282. total_compressed += bytes_read;
  283. break;
  284. }
  285. }
  286. fclose(in);
  287. if(total_read != fsize)
  288. {
  289. printf("\n");
  290. printf("Error: reading %s failed!\n", input_file);
  291. return -1;
  292. }
  293. else
  294. {
  295. printf("] ");
  296. if(total_compressed < fsize)
  297. {
  298. if(fsize < (1<<20))
  299. percent = total_compressed * 1000 / fsize;
  300. else
  301. percent = total_compressed/256 * 1000 / (fsize >>8);
  302. percent = 1000 - percent;
  303. printf("%2d.%d%% saved", (int)percent/10, (int)percent%10);
  304. }
  305. printf("\n");
  306. }
  307. return 0;
  308. }
  309. int pack_file(int compress_level, const char* input_file, const char* output_file)
  310. {
  311. FILE* f;
  312. int result;
  313. f = fopen(output_file, "rb");
  314. if(f)
  315. {
  316. fclose(f);
  317. printf("Error: file %s already exists. Aborted.\n\n", output_file);
  318. return -1;
  319. }
  320. f = fopen(output_file, "wb");
  321. if(!f)
  322. {
  323. printf("Error: could not create %s. Aborted.\n\n", output_file);
  324. return -1;
  325. }
  326. write_magic(f);
  327. result = pack_file_compressed(input_file, 1, compress_level, f);
  328. fclose(f);
  329. return result;
  330. }
  331. #ifdef SIXPACK_BENCHMARK_WIN32
  332. int benchmark_speed(int compress_level, const char* input_file);
  333. int benchmark_speed(int compress_level, const char* input_file)
  334. {
  335. FILE* in;
  336. unsigned long fsize;
  337. unsigned long maxout;
  338. const char* shown_name;
  339. unsigned char* buffer;
  340. unsigned char* result;
  341. size_t bytes_read;
  342. /* sanity check */
  343. in = fopen(input_file, "rb");
  344. if(!in)
  345. {
  346. printf("Error: could not open %s\n", input_file);
  347. return -1;
  348. }
  349. /* find size of the file */
  350. fseek(in, 0, SEEK_END);
  351. fsize = ftell(in);
  352. fseek(in, 0, SEEK_SET);
  353. /* already a 6pack archive? */
  354. if(detect_magic(in))
  355. {
  356. printf("Error: no benchmark for 6pack archive!\n");
  357. fclose(in);
  358. return -1;
  359. }
  360. /* truncate directory prefix, e.g. "foo/bar/FILE.txt" becomes "FILE.txt" */
  361. shown_name = input_file + strlen(input_file) - 1;
  362. while(shown_name > input_file)
  363. if(*(shown_name-1) == PATH_SEPARATOR)
  364. break;
  365. else
  366. shown_name--;
  367. maxout = 1.05 * fsize;
  368. maxout = (maxout < 66) ? 66 : maxout;
  369. buffer = (unsigned char*)malloc(fsize);
  370. result = (unsigned char*)malloc(maxout);
  371. if(!buffer || !result)
  372. {
  373. printf("Error: not enough memory!\n");
  374. free(buffer);
  375. free(result);
  376. fclose(in);
  377. return -1;
  378. }
  379. printf("Reading source file....\n");
  380. bytes_read = fread(buffer, 1, fsize, in);
  381. if(bytes_read != fsize)
  382. {
  383. printf("Error reading file %s!\n", shown_name);
  384. printf("Read %d bytes, expecting %d bytes\n", bytes_read, fsize);
  385. free(buffer);
  386. free(result);
  387. fclose(in);
  388. return -1;
  389. }
  390. /* shamelessly copied from QuickLZ 1.20 test program */
  391. {
  392. unsigned int j, y;
  393. size_t i, u = 0;
  394. double mbs, fastest;
  395. unsigned long compressed_size;
  396. printf("Setting HIGH_PRIORITY_CLASS...\n");
  397. SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS);
  398. printf("Benchmarking FastLZ Level %d, please wait...\n", compress_level);
  399. i = bytes_read;
  400. fastest = 0.0;
  401. for (j = 0; j < 3; j++)
  402. {
  403. y = 0;
  404. mbs = GetTickCount();
  405. while(GetTickCount() == mbs);
  406. mbs = GetTickCount();
  407. while(GetTickCount() - mbs < 3000) /* 1% accuracy with 18.2 timer */
  408. {
  409. u = fastlz_compress_level(compress_level, buffer, bytes_read, result);
  410. y++;
  411. }
  412. mbs = ((double)i*(double)y)/((double)(GetTickCount() - mbs)/1000.)/1000000.;
  413. /*printf(" %.1f Mbyte/s ", mbs);*/
  414. if (fastest < mbs)
  415. fastest = mbs;
  416. }
  417. printf("\nCompressed %d bytes into %d bytes (%.1f%%) at %.1f Mbyte/s.\n", (unsigned int)i, (unsigned int)u, (double)u/(double)i*100., fastest);
  418. #if 1
  419. fastest = 0.0;
  420. compressed_size = u;
  421. for (j = 0; j < 3; j++)
  422. {
  423. y = 0;
  424. mbs = GetTickCount();
  425. while(GetTickCount() == mbs);
  426. mbs = GetTickCount();
  427. while(GetTickCount() - mbs < 3000) /* 1% accuracy with 18.2 timer */
  428. {
  429. u = fastlz_decompress(result, compressed_size, buffer, bytes_read);
  430. y++;
  431. }
  432. mbs = ((double)i*(double)y)/((double)(GetTickCount() - mbs)/1000.)/1000000.;
  433. /*printf(" %.1f Mbyte/s ", mbs);*/
  434. if (fastest < mbs)
  435. fastest = mbs;
  436. }
  437. printf("\nDecompressed at %.1f Mbyte/s.\n\n(1 MB = 1000000 byte)\n", fastest);
  438. #endif
  439. }
  440. fclose(in);
  441. return 0;
  442. }
  443. #endif /* SIXPACK_BENCHMARK_WIN32 */
  444. int main(int argc, char** argv)
  445. {
  446. int i;
  447. int compress_level;
  448. int benchmark;
  449. char* input_file;
  450. char* output_file;
  451. /* show help with no argument at all*/
  452. if(argc == 1)
  453. {
  454. usage();
  455. return 0;
  456. }
  457. /* default compression level, not the fastest */
  458. compress_level = 2;
  459. /* do benchmark only when explicitly specified */
  460. benchmark = 0;
  461. /* no file is specified */
  462. input_file = 0;
  463. output_file = 0;
  464. for(i = 1; i <= argc; i++)
  465. {
  466. char* argument = argv[i];
  467. if(!argument)
  468. continue;
  469. /* display help on usage */
  470. if(!strcmp(argument, "-h") || !strcmp(argument, "--help"))
  471. {
  472. usage();
  473. return 0;
  474. }
  475. /* check for version information */
  476. if(!strcmp(argument, "-v") || !strcmp(argument, "--version"))
  477. {
  478. printf("6pack: high-speed file compression tool\n");
  479. printf("Version %s (using FastLZ %s)\n",
  480. SIXPACK_VERSION_STRING, FASTLZ_VERSION_STRING);
  481. printf("Copyright (C) 2007 Ariya Hidayat (ariya@kde.org)\n");
  482. printf("\n");
  483. return 0;
  484. }
  485. /* test compression speed? */
  486. if(!strcmp(argument, "-mem"))
  487. {
  488. benchmark = 1;
  489. continue;
  490. }
  491. /* compression level */
  492. if(!strcmp(argument, "-1") || !strcmp(argument, "--fastest"))
  493. {
  494. compress_level = 1;
  495. continue;
  496. }
  497. if(!strcmp(argument, "-2"))
  498. {
  499. compress_level = 2;
  500. continue;
  501. }
  502. /* unknown option */
  503. if(argument[0] == '-')
  504. {
  505. printf("Error: unknown option %s\n\n", argument);
  506. printf("To get help on usage:\n");
  507. printf(" 6pack --help\n\n");
  508. return -1;
  509. }
  510. /* first specified file is input */
  511. if(!input_file)
  512. {
  513. input_file = argument;
  514. continue;
  515. }
  516. /* next specified file is output */
  517. if(!output_file)
  518. {
  519. output_file = argument;
  520. continue;
  521. }
  522. /* files are already specified */
  523. printf("Error: unknown option %s\n\n", argument);
  524. printf("To get help on usage:\n");
  525. printf(" 6pack --help\n\n");
  526. return -1;
  527. }
  528. if(!input_file)
  529. {
  530. printf("Error: input file is not specified.\n\n");
  531. printf("To get help on usage:\n");
  532. printf(" 6pack --help\n\n");
  533. return -1;
  534. }
  535. if(!output_file && !benchmark)
  536. {
  537. printf("Error: output file is not specified.\n\n");
  538. printf("To get help on usage:\n");
  539. printf(" 6pack --help\n\n");
  540. return -1;
  541. }
  542. #ifdef SIXPACK_BENCHMARK_WIN32
  543. if(benchmark)
  544. return benchmark_speed(compress_level, input_file);
  545. else
  546. #endif
  547. return pack_file(compress_level, input_file, output_file);
  548. /* unreachable */
  549. return 0;
  550. }