3
0

lzop.c 27 KB

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