lzop.c 29 KB

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