6unpack.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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 "0.1.0"
  27. #include "fastlz.h"
  28. #if defined(WIN32) || defined(__NT__) || defined(_WIN32) || defined(__WIN32__)
  29. #if defined(__BORLANDC__) || defined(_MSC_VER)
  30. #define inline __inline
  31. #endif
  32. #endif
  33. /* magic identifier for 6pack file */
  34. static unsigned char sixpack_magic[8] = {137, '6', 'P', 'K', 13, 10, 26, 10};
  35. #define BLOCK_SIZE 65536
  36. /* prototypes */
  37. static inline unsigned long update_adler32(unsigned long checksum, const void *buf, int len);
  38. void usage(void);
  39. int detect_magic(FILE *f);
  40. static inline unsigned long readU16(const unsigned char* ptr);
  41. static inline unsigned long readU32(const unsigned char* ptr);
  42. void read_chunk_header(FILE* f, int* id, int* options, unsigned long* size,
  43. unsigned long* checksum, unsigned long* extra);
  44. int unpack_file(const char* archive_file);
  45. /* for Adler-32 checksum algorithm, see RFC 1950 Section 8.2 */
  46. #define ADLER32_BASE 65521
  47. static inline unsigned long update_adler32(unsigned long checksum, const void *buf, int len)
  48. {
  49. const unsigned char* ptr = (const unsigned char*)buf;
  50. unsigned long s1 = checksum & 0xffff;
  51. unsigned long s2 = (checksum >> 16) & 0xffff;
  52. while(len>0)
  53. {
  54. unsigned k = len < 5552 ? len : 5552;
  55. len -= k;
  56. while(k >= 8)
  57. {
  58. s1 += *ptr++; s2 += s1;
  59. s1 += *ptr++; s2 += s1;
  60. s1 += *ptr++; s2 += s1;
  61. s1 += *ptr++; s2 += s1;
  62. s1 += *ptr++; s2 += s1;
  63. s1 += *ptr++; s2 += s1;
  64. s1 += *ptr++; s2 += s1;
  65. s1 += *ptr++; s2 += s1;
  66. k -= 8;
  67. }
  68. while(k-- > 0)
  69. {
  70. s1 += *ptr++; s2 += s1;
  71. }
  72. s1 = s1 % ADLER32_BASE;
  73. s2 = s2 % ADLER32_BASE;
  74. }
  75. return (s2 << 16) + s1;
  76. }
  77. void usage(void)
  78. {
  79. printf("6unpack: uncompress 6pack archive\n");
  80. printf("Copyright (C) 2007 Ariya Hidayat (ariya@kde.org)\n");
  81. printf("\n");
  82. printf("Usage: 6unpack archive-file\n");
  83. printf("\n");
  84. }
  85. /* return non-zero if magic sequence is detected */
  86. /* warning: reset the read pointer to the beginning of the file */
  87. int detect_magic(FILE *f)
  88. {
  89. unsigned char buffer[8];
  90. size_t bytes_read;
  91. int c;
  92. fseek(f, SEEK_SET, 0);
  93. bytes_read = fread(buffer, 1, 8, f);
  94. fseek(f, SEEK_SET, 0);
  95. if(bytes_read < 8)
  96. return 0;
  97. for(c = 0; c < 8; c++)
  98. if(buffer[c] != sixpack_magic[c])
  99. return 0;
  100. return -1;
  101. }
  102. static inline unsigned long readU16( const unsigned char* ptr )
  103. {
  104. return ptr[0]+(ptr[1]<<8);
  105. }
  106. static inline unsigned long readU32( const unsigned char* ptr )
  107. {
  108. return ptr[0]+(ptr[1]<<8)+(ptr[2]<<16)+(ptr[3]<<24);
  109. }
  110. void read_chunk_header(FILE* f, int* id, int* options, unsigned long* size,
  111. unsigned long* checksum, unsigned long* extra)
  112. {
  113. unsigned char buffer[16];
  114. fread(buffer, 1, 16, f);
  115. *id = readU16(buffer) & 0xffff;
  116. *options = readU16(buffer+2) & 0xffff;
  117. *size = readU32(buffer+4) & 0xffffffff;
  118. *checksum = readU32(buffer+8) & 0xffffffff;
  119. *extra = readU32(buffer+12) & 0xffffffff;
  120. }
  121. int unpack_file(const char* input_file)
  122. {
  123. FILE* in;
  124. unsigned long fsize;
  125. int c;
  126. unsigned long percent;
  127. unsigned char progress[20];
  128. int chunk_id;
  129. int chunk_options;
  130. unsigned long chunk_size;
  131. unsigned long chunk_checksum;
  132. unsigned long chunk_extra;
  133. unsigned char buffer[BLOCK_SIZE];
  134. unsigned long checksum;
  135. unsigned long decompressed_size;
  136. unsigned long total_extracted;
  137. int name_length;
  138. char* output_file;
  139. FILE* f;
  140. unsigned char* compressed_buffer;
  141. unsigned char* decompressed_buffer;
  142. unsigned long compressed_bufsize;
  143. unsigned long decompressed_bufsize;
  144. /* sanity check */
  145. in = fopen(input_file, "rb");
  146. if(!in)
  147. {
  148. printf("Error: could not open %s\n", input_file);
  149. return -1;
  150. }
  151. /* find size of the file */
  152. fseek(in, 0, SEEK_END);
  153. fsize = ftell(in);
  154. fseek(in, 0, SEEK_SET);
  155. /* not a 6pack archive? */
  156. if(!detect_magic(in))
  157. {
  158. fclose(in);
  159. printf("Error: file %s is not a 6pack archive!\n", input_file);
  160. return -1;
  161. }
  162. printf("Archive: %s", input_file);
  163. /* position of first chunk */
  164. fseek(in, 8, SEEK_SET);
  165. /* initialize */
  166. output_file = 0;
  167. f = 0;
  168. total_extracted = 0;
  169. decompressed_size = 0;
  170. percent = 0;
  171. compressed_buffer = 0;
  172. decompressed_buffer = 0;
  173. compressed_bufsize = 0;
  174. decompressed_bufsize = 0;
  175. /* main loop */
  176. for(;;)
  177. {
  178. /* end of file? */
  179. size_t pos = ftell(in);
  180. if(pos >= fsize)
  181. break;
  182. read_chunk_header(in, &chunk_id, &chunk_options,
  183. &chunk_size, &chunk_checksum, &chunk_extra);
  184. if((chunk_id == 1) && (chunk_size > 10) && (chunk_size < BLOCK_SIZE))
  185. {
  186. /* close current file, if any */
  187. printf("\n");
  188. free(output_file);
  189. output_file = 0;
  190. if(f)
  191. fclose(f);
  192. /* file entry */
  193. fread(buffer, 1, chunk_size, in);
  194. checksum = update_adler32(1L, buffer, chunk_size);
  195. if(checksum != chunk_checksum)
  196. {
  197. free(output_file);
  198. output_file = 0;
  199. fclose(in);
  200. printf("\nError: checksum mismatch!\n");
  201. printf("Got %08lX Expecting %08lX\n", checksum, chunk_checksum);
  202. return -1;
  203. }
  204. decompressed_size = readU32(buffer);
  205. total_extracted = 0;
  206. percent = 0;
  207. /* get file to extract */
  208. name_length = (int)readU16(buffer+8);
  209. if(name_length > (int)chunk_size - 10)
  210. name_length = chunk_size - 10;
  211. output_file = (char*)malloc(name_length+1);
  212. memset(output_file, 0, name_length+1);
  213. for(c = 0; c < name_length; c++)
  214. output_file[c] = buffer[10+c];
  215. /* check if already exists */
  216. f = fopen(output_file, "rb");
  217. if(f)
  218. {
  219. fclose(f);
  220. printf("File %s already exists. Skipped.\n", output_file);
  221. free(output_file);
  222. output_file = 0;
  223. f = 0;
  224. }
  225. else
  226. {
  227. /* create the file */
  228. f = fopen(output_file, "wb");
  229. if(!f)
  230. {
  231. printf("Can't create file %s. Skipped.\n", output_file);
  232. free(output_file);
  233. output_file = 0;
  234. f = 0;
  235. }
  236. else
  237. {
  238. /* for progress status */
  239. printf("\n");
  240. memset(progress, ' ', 20);
  241. if(strlen(output_file) < 16)
  242. for(c = 0; c < (int)strlen(output_file); c++)
  243. progress[c] = output_file[c];
  244. else
  245. {
  246. for(c = 0; c < 13; c++)
  247. progress[c] = output_file[c];
  248. progress[13] = '.';
  249. progress[14] = '.';
  250. progress[15] = ' ';
  251. }
  252. progress[16] = '[';
  253. progress[17] = 0;
  254. printf("%s", progress);
  255. for(c = 0; c < 50; c++)
  256. printf(".");
  257. printf("]\r");
  258. printf("%s", progress);
  259. }
  260. }
  261. }
  262. if((chunk_id == 17) && f && output_file && decompressed_size)
  263. {
  264. unsigned long remaining;
  265. /* uncompressed */
  266. switch(chunk_options)
  267. {
  268. /* stored, simply copy to output */
  269. case 0:
  270. /* read one block at at time, write and update checksum */
  271. total_extracted += chunk_size;
  272. remaining = chunk_size;
  273. checksum = 1L;
  274. for(;;)
  275. {
  276. unsigned long r = (BLOCK_SIZE < remaining) ? BLOCK_SIZE: remaining;
  277. size_t bytes_read = fread(buffer, 1, r, in);
  278. if(bytes_read == 0)
  279. break;
  280. fwrite(buffer, 1, bytes_read, f);
  281. checksum = update_adler32(checksum, buffer, bytes_read);
  282. remaining -= bytes_read;
  283. }
  284. /* verify everything is written correctly */
  285. if(checksum != chunk_checksum)
  286. {
  287. fclose(f);
  288. f = 0;
  289. free(output_file);
  290. output_file = 0;
  291. printf("\nError: checksum mismatch. Aborted.\n");
  292. printf("Got %08lX Expecting %08lX\n", checksum, chunk_checksum);
  293. }
  294. break;
  295. /* compressed using FastLZ */
  296. case 1:
  297. /* enlarge input buffer if necessary */
  298. if(chunk_size > compressed_bufsize)
  299. {
  300. compressed_bufsize = chunk_size;
  301. free(compressed_buffer);
  302. compressed_buffer = (unsigned char*)malloc(compressed_bufsize);
  303. }
  304. /* enlarge output buffer if necessary */
  305. if(chunk_extra > decompressed_bufsize)
  306. {
  307. decompressed_bufsize = chunk_extra;
  308. free(decompressed_buffer);
  309. decompressed_buffer = (unsigned char*)malloc(decompressed_bufsize);
  310. }
  311. /* read and check checksum */
  312. fread(compressed_buffer, 1, chunk_size, in);
  313. checksum = update_adler32(1L, compressed_buffer, chunk_size);
  314. total_extracted += chunk_extra;
  315. /* verify that the chunk data is correct */
  316. if(checksum != chunk_checksum)
  317. {
  318. fclose(f);
  319. f = 0;
  320. free(output_file);
  321. output_file = 0;
  322. printf("\nError: checksum mismatch. Skipped.\n");
  323. printf("Got %08lX Expecting %08lX\n", checksum, chunk_checksum);
  324. }
  325. else
  326. {
  327. /* decompress and verify */
  328. remaining = fastlz_decompress(compressed_buffer, chunk_size, decompressed_buffer, chunk_extra);
  329. if(remaining != chunk_extra)
  330. {
  331. fclose(f);
  332. f = 0;
  333. free(output_file);
  334. output_file = 0;
  335. printf("\nError: decompression failed. Skipped.\n");
  336. }
  337. else
  338. fwrite(decompressed_buffer, 1, chunk_extra, f);
  339. }
  340. break;
  341. default:
  342. printf("\nError: unknown compression method (%d)\n", chunk_options);
  343. fclose(f);
  344. f = 0;
  345. free(output_file);
  346. output_file = 0;
  347. break;
  348. }
  349. /* for progress, if everything is fine */
  350. if(f)
  351. {
  352. int last_percent = (int)percent;
  353. if(decompressed_size < (1<<24))
  354. percent = total_extracted * 100 / decompressed_size;
  355. else
  356. percent = total_extracted / 256 * 100 / (decompressed_size >>8);
  357. percent >>= 1;
  358. while(last_percent < (int)percent)
  359. {
  360. printf("#");
  361. last_percent++;
  362. }
  363. }
  364. }
  365. /* position of next chunk */
  366. fseek(in, pos + 16 + chunk_size, SEEK_SET);
  367. }
  368. printf("\n\n");
  369. /* free allocated stuff */
  370. free(compressed_buffer);
  371. free(decompressed_buffer);
  372. free(output_file);
  373. /* close working files */
  374. if(f)
  375. fclose(f);
  376. fclose(in);
  377. /* so far so good */
  378. return 0;
  379. }
  380. int main(int argc, char** argv)
  381. {
  382. int i;
  383. const char* archive_file;
  384. /* show help with no argument at all*/
  385. if(argc == 1)
  386. {
  387. usage();
  388. return 0;
  389. }
  390. /* check for help on usage */
  391. for(i = 1; i <= argc; i++)
  392. if(argv[i])
  393. if(!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help"))
  394. {
  395. usage();
  396. return 0;
  397. }
  398. /* check for version information */
  399. for(i = 1; i <= argc; i++)
  400. if(argv[i])
  401. if(!strcmp(argv[i], "-v") || !strcmp(argv[i], "--version"))
  402. {
  403. printf("6unpack: high-speed file compression tool\n");
  404. printf("Version %s (using FastLZ %s)\n",
  405. SIXPACK_VERSION_STRING, FASTLZ_VERSION_STRING);
  406. printf("Copyright (C) 2007 Ariya Hidayat (ariya@kde.org)\n");
  407. printf("\n");
  408. return 0;
  409. }
  410. /* needs at least two arguments */
  411. if(argc <= 1)
  412. {
  413. usage();
  414. return 0;
  415. }
  416. archive_file = argv[1];
  417. return unpack_file(archive_file);
  418. }