lzop.c 26 KB

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