lzop.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122
  1. /*
  2. This file is part of the lzop file compressor.
  3. Copyright (C) 1996..2003 Markus Franz Xaver Johannes Oberhumer
  4. All Rights Reserved.
  5. Markus F.X.J. Oberhumer <markus@oberhumer.com>
  6. http://www.oberhumer.com/opensource/lzop/
  7. lzop and the LZO library are free software; you can redistribute them
  8. and/or modify them under the terms of the GNU General Public License as
  9. published by the Free Software Foundation; either version 2 of
  10. the License, or (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; see the file COPYING.
  17. If not, write to the Free Software Foundation, Inc.,
  18. 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. "Minimalized" for busybox by Alain Knaff
  20. */
  21. //config:config LZOP
  22. //config: bool "lzop"
  23. //config: default y
  24. //config: help
  25. //config: Lzop compression/decompresion.
  26. //config:
  27. //config:config LZOP_COMPR_HIGH
  28. //config: bool "lzop compression levels 7,8,9 (not very useful)"
  29. //config: default n
  30. //config: depends on LZOP
  31. //config: help
  32. //config: High levels (7,8,9) of lzop compression. These levels
  33. //config: are actually slower than gzip at equivalent compression ratios
  34. //config: and take up 3.2K of code.
  35. //applet:IF_LZOP(APPLET(lzop, BB_DIR_BIN, BB_SUID_DROP))
  36. //applet:IF_LZOP(APPLET_ODDNAME(lzopcat, lzop, BB_DIR_USR_BIN, BB_SUID_DROP, lzopcat))
  37. //applet:IF_LZOP(APPLET_ODDNAME(unlzop, lzop, BB_DIR_USR_BIN, BB_SUID_DROP, unlzop))
  38. //kbuild:lib-$(CONFIG_LZOP) += lzop.o
  39. //usage:#define lzop_trivial_usage
  40. //usage: "[-cfvd123456789CF] [FILE]..."
  41. //usage:#define lzop_full_usage "\n\n"
  42. //usage: " -1..9 Compression level"
  43. //usage: "\n -d Decompress"
  44. //usage: "\n -c Write to stdout"
  45. //usage: "\n -f Force"
  46. //usage: "\n -v Verbose"
  47. //usage: "\n -F Don't store or verify checksum"
  48. //usage: "\n -C Also write checksum of compressed block"
  49. //usage:
  50. //usage:#define lzopcat_trivial_usage
  51. //usage: "[-vCF] [FILE]..."
  52. //usage:#define lzopcat_full_usage "\n\n"
  53. //usage: " -v Verbose"
  54. //usage: "\n -F Don't store or verify checksum"
  55. //usage:
  56. //usage:#define unlzop_trivial_usage
  57. //usage: "[-cfvCF] [FILE]..."
  58. //usage:#define unlzop_full_usage "\n\n"
  59. //usage: " -c Write to stdout"
  60. //usage: "\n -f Force"
  61. //usage: "\n -v Verbose"
  62. //usage: "\n -F Don't store or verify checksum"
  63. #include "libbb.h"
  64. #include "common_bufsiz.h"
  65. #include "bb_archive.h"
  66. #include "liblzo_interface.h"
  67. /* lzo-2.03/src/lzo_ptr.h */
  68. #define pd(a,b) ((unsigned)((a)-(b)))
  69. #define lzo_version() LZO_VERSION
  70. #define lzo_sizeof_dict_t (sizeof(uint8_t*))
  71. /* lzo-2.03/include/lzo/lzo1x.h */
  72. #define LZO1X_1_MEM_COMPRESS (16384 * lzo_sizeof_dict_t)
  73. #define LZO1X_1_15_MEM_COMPRESS (32768 * lzo_sizeof_dict_t)
  74. #define LZO1X_999_MEM_COMPRESS (14 * 16384 * sizeof(short))
  75. /* lzo-2.03/src/lzo1x_oo.c */
  76. #define NO_LIT UINT_MAX
  77. /**********************************************************************/
  78. static void copy2(uint8_t* ip, const uint8_t* m_pos, unsigned off)
  79. {
  80. ip[0] = m_pos[0];
  81. if (off == 1)
  82. ip[1] = m_pos[0];
  83. else
  84. ip[1] = m_pos[1];
  85. }
  86. static void copy3(uint8_t* ip, const uint8_t* m_pos, unsigned off)
  87. {
  88. ip[0] = m_pos[0];
  89. if (off == 1) {
  90. ip[2] = ip[1] = m_pos[0];
  91. }
  92. else if (off == 2) {
  93. ip[1] = m_pos[1];
  94. ip[2] = m_pos[0];
  95. }
  96. else {
  97. ip[1] = m_pos[1];
  98. ip[2] = m_pos[2];
  99. }
  100. }
  101. /**********************************************************************/
  102. // optimize a block of data.
  103. /**********************************************************************/
  104. #define TEST_IP (ip < ip_end)
  105. #define TEST_OP (op <= op_end)
  106. static NOINLINE int lzo1x_optimize(uint8_t *in, unsigned in_len,
  107. uint8_t *out, unsigned *out_len,
  108. void* wrkmem UNUSED_PARAM)
  109. {
  110. uint8_t* op;
  111. uint8_t* ip;
  112. unsigned t;
  113. uint8_t* m_pos;
  114. uint8_t* const ip_end = in + in_len;
  115. uint8_t* const op_end = out + *out_len;
  116. uint8_t* litp = NULL;
  117. unsigned lit = 0;
  118. unsigned next_lit = NO_LIT;
  119. unsigned nl;
  120. unsigned long o_m1_a = 0, o_m1_b = 0, o_m2 = 0, o_m3_a = 0, o_m3_b = 0;
  121. // LZO_UNUSED(wrkmem);
  122. *out_len = 0;
  123. op = out;
  124. ip = in;
  125. if (*ip > 17) {
  126. t = *ip++ - 17;
  127. if (t < 4)
  128. goto match_next;
  129. goto first_literal_run;
  130. }
  131. while (TEST_IP && TEST_OP) {
  132. t = *ip++;
  133. if (t >= 16)
  134. goto match;
  135. /* a literal run */
  136. litp = ip - 1;
  137. if (t == 0) {
  138. t = 15;
  139. while (*ip == 0)
  140. t += 255, ip++;
  141. t += *ip++;
  142. }
  143. lit = t + 3;
  144. /* copy literals */
  145. copy_literal_run:
  146. *op++ = *ip++;
  147. *op++ = *ip++;
  148. *op++ = *ip++;
  149. first_literal_run:
  150. do *op++ = *ip++; while (--t > 0);
  151. t = *ip++;
  152. if (t >= 16)
  153. goto match;
  154. #if defined(LZO1X)
  155. m_pos = op - 1 - 0x800;
  156. #elif defined(LZO1Y)
  157. m_pos = op - 1 - 0x400;
  158. #endif
  159. m_pos -= t >> 2;
  160. m_pos -= *ip++ << 2;
  161. *op++ = *m_pos++;
  162. *op++ = *m_pos++;
  163. *op++ = *m_pos++;
  164. lit = 0;
  165. goto match_done;
  166. /* handle matches */
  167. do {
  168. if (t < 16) { /* a M1 match */
  169. m_pos = op - 1;
  170. m_pos -= t >> 2;
  171. m_pos -= *ip++ << 2;
  172. if (litp == NULL)
  173. goto copy_m1;
  174. nl = ip[-2] & 3;
  175. /* test if a match follows */
  176. if (nl == 0 && lit == 1 && ip[0] >= 16) {
  177. next_lit = nl;
  178. /* adjust length of previous short run */
  179. lit += 2;
  180. *litp = (unsigned char)((*litp & ~3) | lit);
  181. /* copy over the 2 literals that replace the match */
  182. copy2(ip-2, m_pos, pd(op, m_pos));
  183. o_m1_a++;
  184. }
  185. /* test if a literal run follows */
  186. else
  187. if (nl == 0
  188. && ip[0] < 16
  189. && ip[0] != 0
  190. && (lit + 2 + ip[0] < 16)
  191. ) {
  192. t = *ip++;
  193. /* remove short run */
  194. *litp &= ~3;
  195. /* copy over the 2 literals that replace the match */
  196. copy2(ip-3+1, m_pos, pd(op, m_pos));
  197. /* move literals 1 byte ahead */
  198. litp += 2;
  199. if (lit > 0)
  200. memmove(litp+1, litp, lit);
  201. /* insert new length of long literal run */
  202. lit += 2 + t + 3;
  203. *litp = (unsigned char)(lit - 3);
  204. o_m1_b++;
  205. *op++ = *m_pos++;
  206. *op++ = *m_pos++;
  207. goto copy_literal_run;
  208. }
  209. copy_m1:
  210. *op++ = *m_pos++;
  211. *op++ = *m_pos++;
  212. } else {
  213. match:
  214. if (t >= 64) { /* a M2 match */
  215. m_pos = op - 1;
  216. #if defined(LZO1X)
  217. m_pos -= (t >> 2) & 7;
  218. m_pos -= *ip++ << 3;
  219. t = (t >> 5) - 1;
  220. #elif defined(LZO1Y)
  221. m_pos -= (t >> 2) & 3;
  222. m_pos -= *ip++ << 2;
  223. t = (t >> 4) - 3;
  224. #endif
  225. if (litp == NULL)
  226. goto copy_m;
  227. nl = ip[-2] & 3;
  228. /* test if in beetween two long literal runs */
  229. if (t == 1 && lit > 3 && nl == 0
  230. && ip[0] < 16 && ip[0] != 0 && (lit + 3 + ip[0] < 16)
  231. ) {
  232. t = *ip++;
  233. /* copy over the 3 literals that replace the match */
  234. copy3(ip-1-2, m_pos, pd(op, m_pos));
  235. /* set new length of previous literal run */
  236. lit += 3 + t + 3;
  237. *litp = (unsigned char)(lit - 3);
  238. o_m2++;
  239. *op++ = *m_pos++;
  240. *op++ = *m_pos++;
  241. *op++ = *m_pos++;
  242. goto copy_literal_run;
  243. }
  244. } else {
  245. if (t >= 32) { /* a M3 match */
  246. t &= 31;
  247. if (t == 0) {
  248. t = 31;
  249. while (*ip == 0)
  250. t += 255, ip++;
  251. t += *ip++;
  252. }
  253. m_pos = op - 1;
  254. m_pos -= *ip++ >> 2;
  255. m_pos -= *ip++ << 6;
  256. } else { /* a M4 match */
  257. m_pos = op;
  258. m_pos -= (t & 8) << 11;
  259. t &= 7;
  260. if (t == 0) {
  261. t = 7;
  262. while (*ip == 0)
  263. t += 255, ip++;
  264. t += *ip++;
  265. }
  266. m_pos -= *ip++ >> 2;
  267. m_pos -= *ip++ << 6;
  268. if (m_pos == op)
  269. goto eof_found;
  270. m_pos -= 0x4000;
  271. }
  272. if (litp == NULL)
  273. goto copy_m;
  274. nl = ip[-2] & 3;
  275. /* test if in beetween two matches */
  276. if (t == 1 && lit == 0 && nl == 0 && ip[0] >= 16) {
  277. next_lit = nl;
  278. /* make a previous short run */
  279. lit += 3;
  280. *litp = (unsigned char)((*litp & ~3) | lit);
  281. /* copy over the 3 literals that replace the match */
  282. copy3(ip-3, m_pos, pd(op, m_pos));
  283. o_m3_a++;
  284. }
  285. /* test if a literal run follows */
  286. else if (t == 1 && lit <= 3 && nl == 0
  287. && ip[0] < 16 && ip[0] != 0 && (lit + 3 + ip[0] < 16)
  288. ) {
  289. t = *ip++;
  290. /* remove short run */
  291. *litp &= ~3;
  292. /* copy over the 3 literals that replace the match */
  293. copy3(ip-4+1, m_pos, pd(op, m_pos));
  294. /* move literals 1 byte ahead */
  295. litp += 2;
  296. if (lit > 0)
  297. memmove(litp+1,litp,lit);
  298. /* insert new length of long literal run */
  299. lit += 3 + t + 3;
  300. *litp = (unsigned char)(lit - 3);
  301. o_m3_b++;
  302. *op++ = *m_pos++;
  303. *op++ = *m_pos++;
  304. *op++ = *m_pos++;
  305. goto copy_literal_run;
  306. }
  307. }
  308. copy_m:
  309. *op++ = *m_pos++;
  310. *op++ = *m_pos++;
  311. do *op++ = *m_pos++; while (--t > 0);
  312. }
  313. match_done:
  314. if (next_lit == NO_LIT) {
  315. t = ip[-2] & 3;
  316. lit = t;
  317. litp = ip - 2;
  318. }
  319. else
  320. t = next_lit;
  321. next_lit = NO_LIT;
  322. if (t == 0)
  323. break;
  324. /* copy literals */
  325. match_next:
  326. do *op++ = *ip++; while (--t > 0);
  327. t = *ip++;
  328. } while (TEST_IP && TEST_OP);
  329. }
  330. /* no EOF code was found */
  331. *out_len = pd(op, out);
  332. return LZO_E_EOF_NOT_FOUND;
  333. eof_found:
  334. // LZO_UNUSED(o_m1_a); LZO_UNUSED(o_m1_b); LZO_UNUSED(o_m2);
  335. // LZO_UNUSED(o_m3_a); LZO_UNUSED(o_m3_b);
  336. *out_len = pd(op, out);
  337. return (ip == ip_end ? LZO_E_OK :
  338. (ip < ip_end ? LZO_E_INPUT_NOT_CONSUMED : LZO_E_INPUT_OVERRUN));
  339. }
  340. /**********************************************************************/
  341. #define F_OS F_OS_UNIX
  342. #define F_CS F_CS_NATIVE
  343. /**********************************************************************/
  344. #define ADLER32_INIT_VALUE 1
  345. #define CRC32_INIT_VALUE 0
  346. /**********************************************************************/
  347. enum {
  348. M_LZO1X_1 = 1,
  349. M_LZO1X_1_15 = 2,
  350. M_LZO1X_999 = 3,
  351. };
  352. /**********************************************************************/
  353. /* header flags */
  354. #define F_ADLER32_D 0x00000001L
  355. #define F_ADLER32_C 0x00000002L
  356. #define F_H_EXTRA_FIELD 0x00000040L
  357. #define F_H_GMTDIFF 0x00000080L
  358. #define F_CRC32_D 0x00000100L
  359. #define F_CRC32_C 0x00000200L
  360. #define F_H_FILTER 0x00000800L
  361. #define F_H_CRC32 0x00001000L
  362. #define F_MASK 0x00003FFFL
  363. /* operating system & file system that created the file [mostly unused] */
  364. #define F_OS_UNIX 0x03000000L
  365. #define F_OS_SHIFT 24
  366. #define F_OS_MASK 0xff000000L
  367. /* character set for file name encoding [mostly unused] */
  368. #define F_CS_NATIVE 0x00000000L
  369. #define F_CS_SHIFT 20
  370. #define F_CS_MASK 0x00f00000L
  371. /* these bits must be zero */
  372. #define F_RESERVED ((F_MASK | F_OS_MASK | F_CS_MASK) ^ 0xffffffffL)
  373. typedef struct chksum_t {
  374. uint32_t f_adler32;
  375. uint32_t f_crc32;
  376. } chksum_t;
  377. typedef struct header_t {
  378. unsigned version;
  379. unsigned lib_version;
  380. unsigned version_needed_to_extract;
  381. uint32_t flags;
  382. uint32_t mode;
  383. uint32_t mtime;
  384. uint32_t gmtdiff;
  385. uint32_t header_checksum;
  386. uint32_t extra_field_len;
  387. uint32_t extra_field_checksum;
  388. unsigned char method;
  389. unsigned char level;
  390. /* info */
  391. char name[255+1];
  392. } header_t;
  393. struct globals {
  394. /*const uint32_t *lzo_crc32_table;*/
  395. chksum_t chksum_in;
  396. chksum_t chksum_out;
  397. } FIX_ALIASING;
  398. #define G (*(struct globals*)bb_common_bufsiz1)
  399. #define INIT_G() do { setup_common_bufsiz(); } while (0)
  400. //#define G (*ptr_to_globals)
  401. //#define INIT_G() do {
  402. // SET_PTR_TO_GLOBALS(xzalloc(sizeof(G)));
  403. //} while (0)
  404. /**********************************************************************/
  405. #define LZOP_VERSION 0x1010
  406. //#define LZOP_VERSION_STRING "1.01"
  407. //#define LZOP_VERSION_DATE "Apr 27th 2003"
  408. #define OPTION_STRING "cfvqdt123456789CF"
  409. /* Note: must be kept in sync with archival/bbunzip.c */
  410. enum {
  411. OPT_STDOUT = (1 << 0),
  412. OPT_FORCE = (1 << 1),
  413. OPT_VERBOSE = (1 << 2),
  414. OPT_QUIET = (1 << 3),
  415. OPT_DECOMPRESS = (1 << 4),
  416. OPT_TEST = (1 << 5),
  417. OPT_1 = (1 << 6),
  418. OPT_2 = (1 << 7),
  419. OPT_3 = (1 << 8),
  420. OPT_4 = (1 << 9),
  421. OPT_5 = (1 << 10),
  422. OPT_6 = (1 << 11),
  423. OPT_789 = (7 << 12),
  424. OPT_7 = (1 << 13),
  425. OPT_8 = (1 << 14),
  426. OPT_C = (1 << 15),
  427. OPT_F = (1 << 16),
  428. };
  429. /**********************************************************************/
  430. // adler32 checksum
  431. // adapted from free code by Mark Adler <madler@alumni.caltech.edu>
  432. // see http://www.zlib.org/
  433. /**********************************************************************/
  434. static FAST_FUNC uint32_t
  435. lzo_adler32(uint32_t adler, const uint8_t* buf, unsigned len)
  436. {
  437. enum {
  438. LZO_BASE = 65521, /* largest prime smaller than 65536 */
  439. /* NMAX is the largest n such that
  440. * 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
  441. LZO_NMAX = 5552,
  442. };
  443. uint32_t s1 = adler & 0xffff;
  444. uint32_t s2 = (adler >> 16) & 0xffff;
  445. unsigned k;
  446. if (buf == NULL)
  447. return 1;
  448. while (len > 0) {
  449. k = len < LZO_NMAX ? (unsigned) len : LZO_NMAX;
  450. len -= k;
  451. if (k != 0) do {
  452. s1 += *buf++;
  453. s2 += s1;
  454. } while (--k > 0);
  455. s1 %= LZO_BASE;
  456. s2 %= LZO_BASE;
  457. }
  458. return (s2 << 16) | s1;
  459. }
  460. static FAST_FUNC uint32_t
  461. lzo_crc32(uint32_t c, const uint8_t* buf, unsigned len)
  462. {
  463. //if (buf == NULL) - impossible
  464. // return 0;
  465. return ~crc32_block_endian0(~c, buf, len, global_crc32_table);
  466. }
  467. /**********************************************************************/
  468. static void init_chksum(chksum_t *ct)
  469. {
  470. ct->f_adler32 = ADLER32_INIT_VALUE;
  471. ct->f_crc32 = CRC32_INIT_VALUE;
  472. }
  473. static void add_bytes_to_chksum(chksum_t *ct, const void* buf, int cnt)
  474. {
  475. /* We need to handle the two checksums at once, because at the
  476. * beginning of the header, we don't know yet which one we'll
  477. * eventually need */
  478. ct->f_adler32 = lzo_adler32(ct->f_adler32, (const uint8_t*)buf, cnt);
  479. ct->f_crc32 = lzo_crc32(ct->f_crc32, (const uint8_t*)buf, cnt);
  480. }
  481. static uint32_t chksum_getresult(chksum_t *ct, const header_t *h)
  482. {
  483. return (h->flags & F_H_CRC32) ? ct->f_crc32 : ct->f_adler32;
  484. }
  485. /**********************************************************************/
  486. static uint32_t read32(void)
  487. {
  488. uint32_t v;
  489. xread(0, &v, 4);
  490. return ntohl(v);
  491. }
  492. static void write32(uint32_t v)
  493. {
  494. v = htonl(v);
  495. xwrite(1, &v, 4);
  496. }
  497. static void f_write(const void* buf, int cnt)
  498. {
  499. xwrite(1, buf, cnt);
  500. add_bytes_to_chksum(&G.chksum_out, buf, cnt);
  501. }
  502. static void f_read(void* buf, int cnt)
  503. {
  504. xread(0, buf, cnt);
  505. add_bytes_to_chksum(&G.chksum_in, buf, cnt);
  506. }
  507. static int f_read8(void)
  508. {
  509. uint8_t v;
  510. f_read(&v, 1);
  511. return v;
  512. }
  513. static void f_write8(uint8_t v)
  514. {
  515. f_write(&v, 1);
  516. }
  517. static unsigned f_read16(void)
  518. {
  519. uint16_t v;
  520. f_read(&v, 2);
  521. return ntohs(v);
  522. }
  523. static void f_write16(uint16_t v)
  524. {
  525. v = htons(v);
  526. f_write(&v, 2);
  527. }
  528. static uint32_t f_read32(void)
  529. {
  530. uint32_t v;
  531. f_read(&v, 4);
  532. return ntohl(v);
  533. }
  534. static void f_write32(uint32_t v)
  535. {
  536. v = htonl(v);
  537. f_write(&v, 4);
  538. }
  539. /**********************************************************************/
  540. static int lzo_get_method(header_t *h)
  541. {
  542. /* check method */
  543. if (h->method == M_LZO1X_1) {
  544. if (h->level == 0)
  545. h->level = 3;
  546. } else if (h->method == M_LZO1X_1_15) {
  547. if (h->level == 0)
  548. h->level = 1;
  549. } else if (h->method == M_LZO1X_999) {
  550. if (h->level == 0)
  551. h->level = 9;
  552. } else
  553. return -1; /* not a LZO method */
  554. /* check compression level */
  555. if (h->level < 1 || h->level > 9)
  556. return 15;
  557. return 0;
  558. }
  559. /**********************************************************************/
  560. #define LZO_BLOCK_SIZE (256 * 1024l)
  561. #define MAX_BLOCK_SIZE (64 * 1024l * 1024l) /* DO NOT CHANGE */
  562. /* LZO may expand uncompressible data by a small amount */
  563. #define MAX_COMPRESSED_SIZE(x) ((x) + (x) / 16 + 64 + 3)
  564. /**********************************************************************/
  565. // compress a file
  566. /**********************************************************************/
  567. static NOINLINE int lzo_compress(const header_t *h)
  568. {
  569. unsigned block_size = LZO_BLOCK_SIZE;
  570. int r = 0; /* LZO_E_OK */
  571. uint8_t *const b1 = xzalloc(block_size);
  572. uint8_t *const b2 = xzalloc(MAX_COMPRESSED_SIZE(block_size));
  573. unsigned src_len = 0, dst_len = 0;
  574. uint32_t d_adler32 = ADLER32_INIT_VALUE;
  575. uint32_t d_crc32 = CRC32_INIT_VALUE;
  576. int l;
  577. uint8_t *wrk_mem = NULL;
  578. if (h->method == M_LZO1X_1)
  579. wrk_mem = xzalloc(LZO1X_1_MEM_COMPRESS);
  580. else if (h->method == M_LZO1X_1_15)
  581. wrk_mem = xzalloc(LZO1X_1_15_MEM_COMPRESS);
  582. else if (h->method == M_LZO1X_999)
  583. wrk_mem = xzalloc(LZO1X_999_MEM_COMPRESS);
  584. for (;;) {
  585. /* read a block */
  586. l = full_read(0, b1, block_size);
  587. src_len = (l > 0 ? l : 0);
  588. /* write uncompressed block size */
  589. write32(src_len);
  590. /* exit if last block */
  591. if (src_len == 0)
  592. break;
  593. /* compute checksum of uncompressed block */
  594. if (h->flags & F_ADLER32_D)
  595. d_adler32 = lzo_adler32(ADLER32_INIT_VALUE, b1, src_len);
  596. if (h->flags & F_CRC32_D)
  597. d_crc32 = lzo_crc32(CRC32_INIT_VALUE, b1, src_len);
  598. /* compress */
  599. if (h->method == M_LZO1X_1)
  600. r = lzo1x_1_compress(b1, src_len, b2, &dst_len, wrk_mem);
  601. else if (h->method == M_LZO1X_1_15)
  602. r = lzo1x_1_15_compress(b1, src_len, b2, &dst_len, wrk_mem);
  603. #if ENABLE_LZOP_COMPR_HIGH
  604. else if (h->method == M_LZO1X_999)
  605. r = lzo1x_999_compress_level(b1, src_len, b2, &dst_len,
  606. wrk_mem, h->level);
  607. #endif
  608. else
  609. bb_error_msg_and_die("internal error");
  610. if (r != 0) /* not LZO_E_OK */
  611. bb_error_msg_and_die("internal error - compression failed");
  612. /* write compressed block size */
  613. if (dst_len < src_len) {
  614. /* optimize */
  615. if (h->method == M_LZO1X_999) {
  616. unsigned new_len = src_len;
  617. r = lzo1x_optimize(b2, dst_len, b1, &new_len, NULL);
  618. if (r != 0 /*LZO_E_OK*/ || new_len != src_len)
  619. bb_error_msg_and_die("internal error - optimization failed");
  620. }
  621. write32(dst_len);
  622. } else {
  623. /* data actually expanded => store data uncompressed */
  624. write32(src_len);
  625. }
  626. /* write checksum of uncompressed block */
  627. if (h->flags & F_ADLER32_D)
  628. write32(d_adler32);
  629. if (h->flags & F_CRC32_D)
  630. write32(d_crc32);
  631. if (dst_len < src_len) {
  632. /* write checksum of compressed block */
  633. if (h->flags & F_ADLER32_C)
  634. write32(lzo_adler32(ADLER32_INIT_VALUE, b2, dst_len));
  635. if (h->flags & F_CRC32_C)
  636. write32(lzo_crc32(CRC32_INIT_VALUE, b2, dst_len));
  637. /* write compressed block data */
  638. xwrite(1, b2, dst_len);
  639. } else {
  640. /* write uncompressed block data */
  641. xwrite(1, b1, src_len);
  642. }
  643. }
  644. free(wrk_mem);
  645. free(b1);
  646. free(b2);
  647. return 1;
  648. }
  649. static FAST_FUNC void lzo_check(
  650. uint32_t init,
  651. uint8_t* buf, unsigned len,
  652. uint32_t FAST_FUNC (*fn)(uint32_t, const uint8_t*, unsigned),
  653. uint32_t ref)
  654. {
  655. /* This function, by having the same order of parameters
  656. * as fn, and by being marked FAST_FUNC (same as fn),
  657. * saves a dozen bytes of code.
  658. */
  659. uint32_t c = fn(init, buf, len);
  660. if (c != ref)
  661. bb_error_msg_and_die("checksum error");
  662. }
  663. /**********************************************************************/
  664. // decompress a file
  665. /**********************************************************************/
  666. static NOINLINE int lzo_decompress(const header_t *h)
  667. {
  668. unsigned block_size = LZO_BLOCK_SIZE;
  669. int r;
  670. uint32_t src_len, dst_len;
  671. uint32_t c_adler32 = ADLER32_INIT_VALUE;
  672. uint32_t d_adler32 = ADLER32_INIT_VALUE;
  673. uint32_t c_crc32 = CRC32_INIT_VALUE, d_crc32 = CRC32_INIT_VALUE;
  674. uint8_t *b1;
  675. uint32_t mcs_block_size = MAX_COMPRESSED_SIZE(block_size);
  676. uint8_t *b2 = NULL;
  677. for (;;) {
  678. uint8_t *dst;
  679. /* read uncompressed block size */
  680. dst_len = read32();
  681. /* exit if last block */
  682. if (dst_len == 0)
  683. break;
  684. /* error if split file */
  685. if (dst_len == 0xffffffffL)
  686. /* should not happen - not yet implemented */
  687. bb_error_msg_and_die("this file is a split lzop file");
  688. if (dst_len > MAX_BLOCK_SIZE)
  689. bb_error_msg_and_die("corrupted data");
  690. /* read compressed block size */
  691. src_len = read32();
  692. if (src_len <= 0 || src_len > dst_len)
  693. bb_error_msg_and_die("corrupted data");
  694. if (dst_len > block_size) {
  695. if (b2) {
  696. free(b2);
  697. b2 = NULL;
  698. }
  699. block_size = dst_len;
  700. mcs_block_size = MAX_COMPRESSED_SIZE(block_size);
  701. }
  702. /* read checksum of uncompressed block */
  703. if (h->flags & F_ADLER32_D)
  704. d_adler32 = read32();
  705. if (h->flags & F_CRC32_D)
  706. d_crc32 = read32();
  707. /* read checksum of compressed block */
  708. if (src_len < dst_len) {
  709. if (h->flags & F_ADLER32_C)
  710. c_adler32 = read32();
  711. if (h->flags & F_CRC32_C)
  712. c_crc32 = read32();
  713. }
  714. if (b2 == NULL)
  715. b2 = xzalloc(mcs_block_size);
  716. /* read the block into the end of our buffer */
  717. b1 = b2 + mcs_block_size - src_len;
  718. xread(0, b1, src_len);
  719. if (src_len < dst_len) {
  720. unsigned d = dst_len;
  721. if (!(option_mask32 & OPT_F)) {
  722. /* verify checksum of compressed block */
  723. if (h->flags & F_ADLER32_C)
  724. lzo_check(ADLER32_INIT_VALUE,
  725. b1, src_len,
  726. lzo_adler32, c_adler32);
  727. if (h->flags & F_CRC32_C)
  728. lzo_check(CRC32_INIT_VALUE,
  729. b1, src_len,
  730. lzo_crc32, c_crc32);
  731. }
  732. /* decompress */
  733. // if (option_mask32 & OPT_F)
  734. // r = lzo1x_decompress(b1, src_len, b2, &d, NULL);
  735. // else
  736. r = lzo1x_decompress_safe(b1, src_len, b2, &d, NULL);
  737. if (r != 0 /*LZO_E_OK*/ || dst_len != d) {
  738. bb_error_msg_and_die("corrupted data");
  739. }
  740. dst = b2;
  741. } else {
  742. /* "stored" block => no decompression */
  743. dst = b1;
  744. }
  745. if (!(option_mask32 & OPT_F)) {
  746. /* verify checksum of uncompressed block */
  747. if (h->flags & F_ADLER32_D)
  748. lzo_check(ADLER32_INIT_VALUE,
  749. dst, dst_len,
  750. lzo_adler32, d_adler32);
  751. if (h->flags & F_CRC32_D)
  752. lzo_check(CRC32_INIT_VALUE,
  753. dst, dst_len,
  754. lzo_crc32, d_crc32);
  755. }
  756. /* write uncompressed block data */
  757. xwrite(1, dst, dst_len);
  758. }
  759. free(b2);
  760. return 1;
  761. }
  762. /**********************************************************************/
  763. // lzop file signature (shamelessly borrowed from PNG)
  764. /**********************************************************************/
  765. /*
  766. * The first nine bytes of a lzop file always contain the following values:
  767. *
  768. * 0 1 2 3 4 5 6 7 8
  769. * --- --- --- --- --- --- --- --- ---
  770. * (hex) 89 4c 5a 4f 00 0d 0a 1a 0a
  771. * (decimal) 137 76 90 79 0 13 10 26 10
  772. * (C notation - ASCII) \211 L Z O \0 \r \n \032 \n
  773. */
  774. /* (vda) comparison with lzop v1.02rc1 ("lzop -1 <FILE" cmd):
  775. * Only slight differences in header:
  776. * -00000000 89 4c 5a 4f 00 0d 0a 1a 0a 10 20 20 20 09 40 02
  777. * +00000000 89 4c 5a 4f 00 0d 0a 1a 0a 10 10 20 30 09 40 02
  778. * ^^^^^ ^^^^^
  779. * version lib_version
  780. * -00000010 01 03 00 00 0d 00 00 81 a4 49 f7 a6 3f 00 00 00
  781. * +00000010 01 03 00 00 01 00 00 00 00 00 00 00 00 00 00 00
  782. * ^^^^^^^^^^^ ^^^^^^^^^^^ ^^^^^^^^^^^
  783. * flags mode mtime
  784. * -00000020 00 00 2d 67 04 17 00 04 00 00 00 03 ed ec 9d 6d
  785. * +00000020 00 00 10 5f 00 c1 00 04 00 00 00 03 ed ec 9d 6d
  786. * ^^^^^^^^^^^
  787. * chksum_out
  788. * The rest is identical.
  789. */
  790. static const unsigned char lzop_magic[9] ALIGN1 = {
  791. 0x89, 0x4c, 0x5a, 0x4f, 0x00, 0x0d, 0x0a, 0x1a, 0x0a
  792. };
  793. /* This coding is derived from Alexander Lehmann's pngcheck code. */
  794. static void check_magic(void)
  795. {
  796. unsigned char magic[sizeof(lzop_magic)];
  797. xread(0, magic, sizeof(magic));
  798. if (memcmp(magic, lzop_magic, sizeof(lzop_magic)) != 0)
  799. bb_error_msg_and_die("bad magic number");
  800. }
  801. /**********************************************************************/
  802. // lzop file header
  803. /**********************************************************************/
  804. static void write_header(const header_t *h)
  805. {
  806. int l;
  807. xwrite(1, lzop_magic, sizeof(lzop_magic));
  808. init_chksum(&G.chksum_out);
  809. f_write16(h->version);
  810. f_write16(h->lib_version);
  811. f_write16(h->version_needed_to_extract);
  812. f_write8(h->method);
  813. f_write8(h->level);
  814. f_write32(h->flags);
  815. f_write32(h->mode);
  816. f_write32(h->mtime);
  817. f_write32(h->gmtdiff);
  818. l = (int) strlen(h->name);
  819. f_write8(l);
  820. if (l)
  821. f_write(h->name, l);
  822. f_write32(chksum_getresult(&G.chksum_out, h));
  823. }
  824. static int read_header(header_t *h)
  825. {
  826. int r;
  827. int l;
  828. uint32_t checksum;
  829. memset(h, 0, sizeof(*h));
  830. h->version_needed_to_extract = 0x0900; /* first lzop version */
  831. h->level = 0;
  832. init_chksum(&G.chksum_in);
  833. h->version = f_read16();
  834. if (h->version < 0x0900)
  835. return 3;
  836. h->lib_version = f_read16();
  837. if (h->version >= 0x0940) {
  838. h->version_needed_to_extract = f_read16();
  839. if (h->version_needed_to_extract > LZOP_VERSION)
  840. return 16;
  841. if (h->version_needed_to_extract < 0x0900)
  842. return 3;
  843. }
  844. h->method = f_read8();
  845. if (h->version >= 0x0940)
  846. h->level = f_read8();
  847. h->flags = f_read32();
  848. if (h->flags & F_H_FILTER)
  849. return 16; /* filter not supported */
  850. h->mode = f_read32();
  851. h->mtime = f_read32();
  852. if (h->version >= 0x0940)
  853. h->gmtdiff = f_read32();
  854. l = f_read8();
  855. if (l > 0)
  856. f_read(h->name, l);
  857. h->name[l] = 0;
  858. checksum = chksum_getresult(&G.chksum_in, h);
  859. h->header_checksum = f_read32();
  860. if (h->header_checksum != checksum)
  861. return 2;
  862. if (h->method <= 0)
  863. return 14;
  864. r = lzo_get_method(h);
  865. if (r != 0)
  866. return r;
  867. /* check reserved flags */
  868. if (h->flags & F_RESERVED)
  869. return -13;
  870. /* skip extra field [not used yet] */
  871. if (h->flags & F_H_EXTRA_FIELD) {
  872. uint32_t k;
  873. /* note: the checksum also covers the length */
  874. init_chksum(&G.chksum_in);
  875. h->extra_field_len = f_read32();
  876. for (k = 0; k < h->extra_field_len; k++)
  877. f_read8();
  878. checksum = chksum_getresult(&G.chksum_in, h);
  879. h->extra_field_checksum = f_read32();
  880. if (h->extra_field_checksum != checksum)
  881. return 3;
  882. }
  883. return 0;
  884. }
  885. static void p_header(header_t *h)
  886. {
  887. int r;
  888. r = read_header(h);
  889. if (r == 0)
  890. return;
  891. bb_error_msg_and_die("header_error %d", r);
  892. }
  893. /**********************************************************************/
  894. // compress
  895. /**********************************************************************/
  896. static void lzo_set_method(header_t *h)
  897. {
  898. int level = 1;
  899. if (option_mask32 & OPT_1) {
  900. h->method = M_LZO1X_1_15;
  901. } else if (option_mask32 & OPT_789) {
  902. #if ENABLE_LZOP_COMPR_HIGH
  903. h->method = M_LZO1X_999;
  904. if (option_mask32 & OPT_7)
  905. level = 7;
  906. else if (option_mask32 & OPT_8)
  907. level = 8;
  908. else
  909. level = 9;
  910. #else
  911. bb_error_msg_and_die("high compression not compiled in");
  912. #endif
  913. } else { /* levels 2..6 or none (defaults to level 3) */
  914. h->method = M_LZO1X_1;
  915. level = 5; /* levels 2-6 are actually the same */
  916. }
  917. h->level = level;
  918. }
  919. static int do_lzo_compress(void)
  920. {
  921. header_t header;
  922. #define h (&header)
  923. memset(h, 0, sizeof(*h));
  924. lzo_set_method(h);
  925. h->version = (LZOP_VERSION & 0xffff);
  926. h->version_needed_to_extract = 0x0940;
  927. h->lib_version = lzo_version() & 0xffff;
  928. h->flags = (F_OS & F_OS_MASK) | (F_CS & F_CS_MASK);
  929. if (!(option_mask32 & OPT_F) || h->method == M_LZO1X_999) {
  930. h->flags |= F_ADLER32_D;
  931. if (option_mask32 & OPT_C)
  932. h->flags |= F_ADLER32_C;
  933. }
  934. write_header(h);
  935. return lzo_compress(h);
  936. #undef h
  937. }
  938. /**********************************************************************/
  939. // decompress
  940. /**********************************************************************/
  941. static int do_lzo_decompress(void)
  942. {
  943. header_t header;
  944. check_magic();
  945. p_header(&header);
  946. return lzo_decompress(&header);
  947. }
  948. static char* FAST_FUNC make_new_name_lzop(char *filename, const char *expected_ext UNUSED_PARAM)
  949. {
  950. if (option_mask32 & OPT_DECOMPRESS) {
  951. char *extension = strrchr(filename, '.');
  952. if (!extension || strcmp(extension + 1, "lzo") != 0)
  953. return xasprintf("%s.out", filename);
  954. *extension = '\0';
  955. return filename;
  956. }
  957. return xasprintf("%s.lzo", filename);
  958. }
  959. static IF_DESKTOP(long long) int FAST_FUNC pack_lzop(transformer_state_t *xstate UNUSED_PARAM)
  960. {
  961. if (option_mask32 & OPT_DECOMPRESS)
  962. return do_lzo_decompress();
  963. return do_lzo_compress();
  964. }
  965. int lzop_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  966. int lzop_main(int argc UNUSED_PARAM, char **argv)
  967. {
  968. getopt32(argv, OPTION_STRING);
  969. argv += optind;
  970. /* lzopcat? */
  971. if (applet_name[4] == 'c')
  972. option_mask32 |= (OPT_STDOUT | OPT_DECOMPRESS);
  973. /* unlzop? */
  974. if (applet_name[4] == 'o')
  975. option_mask32 |= OPT_DECOMPRESS;
  976. global_crc32_table = crc32_filltable(NULL, 0);
  977. return bbunpack(argv, pack_lzop, make_new_name_lzop, /*unused:*/ NULL);
  978. }