lzop.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145
  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 (13 kb)"
  23. //config: default y
  24. //config: help
  25. //config: Lzop compression/decompresion.
  26. //config:
  27. //config:config UNLZOP
  28. //config: bool "unlzop (13 kb)"
  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 (13 kb)"
  35. //config: default n # INCOMPAT: upstream lzop does not provide such tool
  36. //config: help
  37. //config: Alias to "lzop -dc".
  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: "[-cfUvF] [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 -t Test integrity"
  81. //usage: "\n -v Verbose"
  82. //usage: "\n -F Don't verify checksum"
  83. #include "libbb.h"
  84. #include "common_bufsiz.h"
  85. #include "bb_archive.h"
  86. #include "liblzo_interface.h"
  87. /* lzo-2.03/src/lzo_ptr.h */
  88. #define pd(a,b) ((unsigned)((a)-(b)))
  89. #define lzo_version() LZO_VERSION
  90. #define lzo_sizeof_dict_t (sizeof(uint8_t*))
  91. /* lzo-2.03/include/lzo/lzo1x.h */
  92. #define LZO1X_1_MEM_COMPRESS (16384 * lzo_sizeof_dict_t)
  93. #define LZO1X_1_15_MEM_COMPRESS (32768 * lzo_sizeof_dict_t)
  94. #define LZO1X_999_MEM_COMPRESS (14 * 16384 * sizeof(short))
  95. /* lzo-2.03/src/lzo1x_oo.c */
  96. #define NO_LIT UINT_MAX
  97. /**********************************************************************/
  98. static void copy2(uint8_t* ip, const uint8_t* m_pos, unsigned off)
  99. {
  100. ip[0] = m_pos[0];
  101. if (off == 1)
  102. ip[1] = m_pos[0];
  103. else
  104. ip[1] = m_pos[1];
  105. }
  106. static void copy3(uint8_t* ip, const uint8_t* m_pos, unsigned off)
  107. {
  108. ip[0] = m_pos[0];
  109. if (off == 1) {
  110. ip[2] = ip[1] = m_pos[0];
  111. }
  112. else if (off == 2) {
  113. ip[1] = m_pos[1];
  114. ip[2] = m_pos[0];
  115. }
  116. else {
  117. ip[1] = m_pos[1];
  118. ip[2] = m_pos[2];
  119. }
  120. }
  121. /**********************************************************************/
  122. // optimize a block of data.
  123. /**********************************************************************/
  124. #define TEST_IP (ip < ip_end)
  125. #define TEST_OP (op <= op_end)
  126. static NOINLINE int lzo1x_optimize(uint8_t *in, unsigned in_len,
  127. uint8_t *out, unsigned *out_len /*, void* wrkmem */)
  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. /* used to have auxiliary fields here */
  398. /* Starting from here, the layout and endianness
  399. * are exactly in on-disk format.
  400. */
  401. uint16_t version_be16;
  402. uint16_t lib_version_be16;
  403. uint16_t version_needed_to_extract_be16;
  404. uint8_t method;
  405. uint8_t level;
  406. uint32_t flags32; /* be32 on disk, but we keep this field in native order */
  407. uint32_t mode_be32;
  408. uint32_t mtime_be32;
  409. uint32_t gmtdiff_be32;
  410. char len_and_name[1+255+1];
  411. } header_t;
  412. struct globals {
  413. /*const uint32_t *lzo_crc32_table;*/
  414. chksum_t chksum;
  415. } FIX_ALIASING;
  416. #define G (*(struct globals*)bb_common_bufsiz1)
  417. //#define G (*ptr_to_globals)
  418. #define INIT_G() do { \
  419. setup_common_bufsiz(); \
  420. /*SET_PTR_TO_GLOBALS(xzalloc(sizeof(G)));*/ \
  421. } while (0)
  422. /**********************************************************************/
  423. #define LZOP_VERSION 0x1010
  424. //#define LZOP_VERSION_STRING "1.01"
  425. //#define LZOP_VERSION_DATE "Apr 27th 2003"
  426. // lzop wants to be weird:
  427. // unlike all other compressosrs, its -k "keep" option is the default,
  428. // and -U is used to delete the source. We will invert the bit after getopt().
  429. #define OPTION_STRING "cfUvqdt123456789CFk"
  430. /* Note: must be kept in sync with archival/bbunzip.c */
  431. enum {
  432. OPT_STDOUT = (1 << 0),
  433. OPT_FORCE = (1 << 1),
  434. OPT_KEEP = (1 << 2),
  435. OPT_VERBOSE = (1 << 3),
  436. OPT_QUIET = (1 << 4),
  437. OPT_DECOMPRESS = (1 << 5),
  438. OPT_TEST = (1 << 6),
  439. OPT_1 = (1 << 7),
  440. OPT_2 = (1 << 8),
  441. OPT_3 = (1 << 9),
  442. OPT_4 = (1 << 10),
  443. OPT_5 = (1 << 11),
  444. OPT_6 = (1 << 12),
  445. OPT_7 = (1 << 13),
  446. OPT_8 = (1 << 14),
  447. OPT_9 = (1 << 15),
  448. OPT_C = (1 << 16),
  449. OPT_F = (1 << 17),
  450. OPT_k = (1 << 18),
  451. OPT_789 = OPT_7 | OPT_8 | OPT_9
  452. };
  453. /**********************************************************************/
  454. // adler32 checksum
  455. // adapted from free code by Mark Adler <madler@alumni.caltech.edu>
  456. // see http://www.zlib.org/
  457. /**********************************************************************/
  458. static FAST_FUNC uint32_t
  459. lzo_adler32(uint32_t adler, const uint8_t* buf, unsigned len)
  460. {
  461. enum {
  462. LZO_BASE = 65521, /* largest prime smaller than 65536 */
  463. /* NMAX is the largest n such that
  464. * 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
  465. LZO_NMAX = 5552,
  466. };
  467. uint32_t s1 = adler & 0xffff;
  468. uint32_t s2 = (adler >> 16) & 0xffff;
  469. unsigned k;
  470. if (buf == NULL)
  471. return 1;
  472. while (len > 0) {
  473. k = len < LZO_NMAX ? (unsigned) len : LZO_NMAX;
  474. len -= k;
  475. if (k != 0) do {
  476. s1 += *buf++;
  477. s2 += s1;
  478. } while (--k > 0);
  479. s1 %= LZO_BASE;
  480. s2 %= LZO_BASE;
  481. }
  482. return (s2 << 16) | s1;
  483. }
  484. static FAST_FUNC uint32_t
  485. lzo_crc32(uint32_t c, const uint8_t* buf, unsigned len)
  486. {
  487. //if (buf == NULL) - impossible
  488. // return 0;
  489. return ~crc32_block_endian0(~c, buf, len, global_crc32_table);
  490. }
  491. /**********************************************************************/
  492. static void init_chksum(void)
  493. {
  494. G.chksum.f_adler32 = ADLER32_INIT_VALUE;
  495. G.chksum.f_crc32 = CRC32_INIT_VALUE;
  496. }
  497. static void add_bytes_to_chksum(const void* buf, int cnt)
  498. {
  499. /* We need to handle the two checksums at once, because at the
  500. * beginning of the header, we don't know yet which one we'll
  501. * eventually need */
  502. G.chksum.f_adler32 = lzo_adler32(G.chksum.f_adler32, (const uint8_t*)buf, cnt);
  503. G.chksum.f_crc32 = lzo_crc32(G.chksum.f_crc32, (const uint8_t*)buf, cnt);
  504. }
  505. static uint32_t chksum_getresult(uint32_t h_flags32)
  506. {
  507. return (h_flags32 & F_H_CRC32) ? G.chksum.f_crc32 : G.chksum.f_adler32;
  508. }
  509. /**********************************************************************/
  510. static uint32_t read32(void)
  511. {
  512. uint32_t v;
  513. xread(0, &v, 4);
  514. return ntohl(v);
  515. }
  516. static void f_read(void* buf, int cnt)
  517. {
  518. xread(0, buf, cnt);
  519. add_bytes_to_chksum(buf, cnt);
  520. }
  521. //static int f_read8(void)
  522. //{
  523. // uint8_t v;
  524. // f_read(&v, 1);
  525. // return v;
  526. //}
  527. //static unsigned f_read16(void)
  528. //{
  529. // uint16_t v;
  530. // f_read(&v, 2);
  531. // return ntohs(v);
  532. //}
  533. static uint32_t f_read32(void)
  534. {
  535. uint32_t v;
  536. f_read(&v, 4);
  537. return ntohl(v);
  538. }
  539. static void write32(uint32_t v)
  540. {
  541. v = htonl(v);
  542. xwrite(1, &v, 4);
  543. }
  544. static void f_write(const void* buf, int cnt)
  545. {
  546. xwrite(1, buf, cnt);
  547. add_bytes_to_chksum(buf, cnt);
  548. }
  549. //static void f_write8(uint8_t v)
  550. //{
  551. // f_write(&v, 1);
  552. //}
  553. //static void f_write16(uint16_t v)
  554. //{
  555. // v = htons(v);
  556. // f_write(&v, 2);
  557. //}
  558. //static void f_write32(uint32_t v)
  559. //{
  560. // v = htonl(v);
  561. // f_write(&v, 4);
  562. //}
  563. /**********************************************************************/
  564. #define LZO_BLOCK_SIZE (256 * 1024l)
  565. #define MAX_BLOCK_SIZE (64 * 1024l * 1024l) /* DO NOT CHANGE */
  566. /* LZO may expand uncompressible data by a small amount */
  567. #define MAX_COMPRESSED_SIZE(x) ((x) + (x) / 16 + 64 + 3)
  568. /**********************************************************************/
  569. // compress a file
  570. /**********************************************************************/
  571. static NOINLINE int lzo_compress(const header_t *h)
  572. {
  573. unsigned block_size = LZO_BLOCK_SIZE;
  574. int r = 0; /* LZO_E_OK */
  575. uint8_t *const b1 = xzalloc(block_size);
  576. uint8_t *const b2 = xzalloc(MAX_COMPRESSED_SIZE(block_size));
  577. uint32_t d_adler32 = ADLER32_INIT_VALUE;
  578. uint32_t d_crc32 = CRC32_INIT_VALUE;
  579. uint8_t *wrk_mem = NULL;
  580. /* Only these methods are possible, see lzo_set_method():
  581. * -1: M_LZO1X_1_15
  582. * -2..6: M_LZO1X_1
  583. * -7..9: M_LZO1X_999 if ENABLE_LZOP_COMPR_HIGH
  584. */
  585. if (h->method == M_LZO1X_1)
  586. wrk_mem = xzalloc(LZO1X_1_MEM_COMPRESS);
  587. else /* check only if it's not the only possibility */
  588. IF_LZOP_COMPR_HIGH(if (h->method == M_LZO1X_1_15))
  589. wrk_mem = xzalloc(LZO1X_1_15_MEM_COMPRESS);
  590. #if ENABLE_LZOP_COMPR_HIGH
  591. else /* must be h->method == M_LZO1X_999 */
  592. wrk_mem = xzalloc(LZO1X_999_MEM_COMPRESS);
  593. #endif
  594. for (;;) {
  595. unsigned src_len, dst_len;
  596. int l;
  597. uint32_t wordbuf[6];
  598. uint32_t *wordptr = wordbuf;
  599. /* read a block */
  600. l = full_read(0, b1, block_size);
  601. src_len = (l > 0 ? l : 0);
  602. /* write uncompressed block size */
  603. /* exit if last block */
  604. if (src_len == 0) {
  605. write32(0);
  606. break;
  607. }
  608. *wordptr++ = htonl(src_len);
  609. /* compute checksum of uncompressed block */
  610. if (h->flags32 & F_ADLER32_D)
  611. d_adler32 = lzo_adler32(ADLER32_INIT_VALUE, b1, src_len);
  612. if (h->flags32 & F_CRC32_D)
  613. d_crc32 = lzo_crc32(CRC32_INIT_VALUE, b1, src_len);
  614. /* compress */
  615. if (h->method == M_LZO1X_1)
  616. r = lzo1x_1_compress(b1, src_len, b2, &dst_len, wrk_mem);
  617. else IF_LZOP_COMPR_HIGH(if (h->method == M_LZO1X_1_15))
  618. r = lzo1x_1_15_compress(b1, src_len, b2, &dst_len, wrk_mem);
  619. #if ENABLE_LZOP_COMPR_HIGH
  620. else /* must be h->method == M_LZO1X_999 */
  621. r = lzo1x_999_compress_level(b1, src_len, b2, &dst_len,
  622. wrk_mem, h->level);
  623. #endif
  624. if (r != 0) /* not LZO_E_OK */
  625. bb_error_msg_and_die("%s: %s", "internal error", "compression");
  626. /* write compressed block size */
  627. if (dst_len < src_len) {
  628. /* optimize */
  629. if (h->method == M_LZO1X_999) {
  630. unsigned new_len = src_len;
  631. r = lzo1x_optimize(b2, dst_len, b1, &new_len /*, NULL*/);
  632. if (r != 0 /*LZO_E_OK*/ || new_len != src_len)
  633. bb_error_msg_and_die("%s: %s", "internal error", "optimization");
  634. }
  635. *wordptr++ = htonl(dst_len);
  636. } else {
  637. /* data actually expanded => store data uncompressed */
  638. *wordptr++ = htonl(src_len);
  639. }
  640. /* write checksum of uncompressed block */
  641. if (h->flags32 & F_ADLER32_D)
  642. *wordptr++ = htonl(d_adler32);
  643. if (h->flags32 & F_CRC32_D)
  644. *wordptr++ = htonl(d_crc32);
  645. if (dst_len < src_len) {
  646. /* write checksum of compressed block */
  647. if (h->flags32 & F_ADLER32_C)
  648. *wordptr++ = htonl(lzo_adler32(ADLER32_INIT_VALUE, b2, dst_len));
  649. if (h->flags32 & F_CRC32_C)
  650. *wordptr++ = htonl(lzo_crc32(CRC32_INIT_VALUE, b2, dst_len));
  651. }
  652. xwrite(1, wordbuf, ((char*)wordptr) - ((char*)wordbuf));
  653. if (dst_len < src_len) {
  654. /* write compressed block data */
  655. xwrite(1, b2, dst_len);
  656. } else {
  657. /* write uncompressed block data */
  658. xwrite(1, b1, src_len);
  659. }
  660. // /* if full_read() was nevertheless "short", it was EOF */
  661. // if (src_len < block_size)
  662. // break;
  663. }
  664. free(wrk_mem);
  665. free(b1);
  666. free(b2);
  667. return 1;
  668. }
  669. static FAST_FUNC void lzo_check(
  670. uint32_t init,
  671. uint8_t* buf, unsigned len,
  672. uint32_t FAST_FUNC (*fn)(uint32_t, const uint8_t*, unsigned),
  673. uint32_t ref)
  674. {
  675. /* This function, by having the same order of parameters
  676. * as fn, and by being marked FAST_FUNC (same as fn),
  677. * saves a dozen bytes of code.
  678. */
  679. uint32_t c = fn(init, buf, len);
  680. if (c != ref)
  681. bb_simple_error_msg_and_die("checksum error");
  682. }
  683. /**********************************************************************/
  684. // decompress a file
  685. /**********************************************************************/
  686. // used to have "const header_t *h" parameter, but since it uses
  687. // only flags32 field, changed to receive only that.
  688. static NOINLINE int lzo_decompress(uint32_t h_flags32)
  689. {
  690. unsigned block_size = LZO_BLOCK_SIZE;
  691. int r;
  692. uint32_t src_len, dst_len;
  693. uint32_t c_adler32 = ADLER32_INIT_VALUE;
  694. uint32_t d_adler32 = ADLER32_INIT_VALUE;
  695. uint32_t c_crc32 = CRC32_INIT_VALUE, d_crc32 = CRC32_INIT_VALUE;
  696. uint8_t *b1;
  697. uint32_t mcs_block_size = MAX_COMPRESSED_SIZE(block_size);
  698. uint8_t *b2 = NULL;
  699. for (;;) {
  700. uint8_t *dst;
  701. /* read uncompressed block size */
  702. dst_len = read32();
  703. /* exit if last block */
  704. if (dst_len == 0)
  705. break;
  706. /* error if split file */
  707. if (dst_len == 0xffffffffL)
  708. /* should not happen - not yet implemented */
  709. bb_simple_error_msg_and_die("this file is a split lzop file");
  710. if (dst_len > MAX_BLOCK_SIZE)
  711. bb_simple_error_msg_and_die("corrupted data");
  712. /* read compressed block size */
  713. src_len = read32();
  714. if (src_len <= 0 || src_len > dst_len)
  715. bb_simple_error_msg_and_die("corrupted data");
  716. if (dst_len > block_size) {
  717. if (b2) {
  718. free(b2);
  719. b2 = NULL;
  720. }
  721. block_size = dst_len;
  722. mcs_block_size = MAX_COMPRESSED_SIZE(block_size);
  723. }
  724. /* read checksum of uncompressed block */
  725. if (h_flags32 & F_ADLER32_D)
  726. d_adler32 = read32();
  727. if (h_flags32 & F_CRC32_D)
  728. d_crc32 = read32();
  729. /* read checksum of compressed block */
  730. if (src_len < dst_len) {
  731. if (h_flags32 & F_ADLER32_C)
  732. c_adler32 = read32();
  733. if (h_flags32 & F_CRC32_C)
  734. c_crc32 = read32();
  735. }
  736. if (b2 == NULL)
  737. b2 = xzalloc(mcs_block_size);
  738. /* read the block into the end of our buffer */
  739. b1 = b2 + mcs_block_size - src_len;
  740. xread(0, b1, src_len);
  741. if (src_len < dst_len) {
  742. unsigned d = dst_len;
  743. if (!(option_mask32 & OPT_F)) {
  744. /* verify checksum of compressed block */
  745. if (h_flags32 & F_ADLER32_C)
  746. lzo_check(ADLER32_INIT_VALUE,
  747. b1, src_len,
  748. lzo_adler32, c_adler32);
  749. if (h_flags32 & F_CRC32_C)
  750. lzo_check(CRC32_INIT_VALUE,
  751. b1, src_len,
  752. lzo_crc32, c_crc32);
  753. }
  754. /* decompress */
  755. // if (option_mask32 & OPT_F)
  756. // r = lzo1x_decompress(b1, src_len, b2, &d /*, NULL*/);
  757. // else
  758. r = lzo1x_decompress_safe(b1, src_len, b2, &d /*, NULL*/);
  759. if (r != 0 /*LZO_E_OK*/ || dst_len != d) {
  760. bb_simple_error_msg_and_die("corrupted data");
  761. }
  762. dst = b2;
  763. } else {
  764. /* "stored" block => no decompression */
  765. dst = b1;
  766. }
  767. if (!(option_mask32 & OPT_F)) {
  768. /* verify checksum of uncompressed block */
  769. if (h_flags32 & F_ADLER32_D)
  770. lzo_check(ADLER32_INIT_VALUE,
  771. dst, dst_len,
  772. lzo_adler32, d_adler32);
  773. if (h_flags32 & F_CRC32_D)
  774. lzo_check(CRC32_INIT_VALUE,
  775. dst, dst_len,
  776. lzo_crc32, d_crc32);
  777. }
  778. /* write uncompressed block data */
  779. xwrite(1, dst, dst_len);
  780. }
  781. free(b2);
  782. return 1;
  783. }
  784. /**********************************************************************/
  785. // lzop file signature (shamelessly borrowed from PNG)
  786. /**********************************************************************/
  787. /*
  788. * The first nine bytes of a lzop file always contain the following values:
  789. *
  790. * 0 1 2 3 4 5 6 7 8
  791. * --- --- --- --- --- --- --- --- ---
  792. * (hex) 89 4c 5a 4f 00 0d 0a 1a 0a
  793. * (decimal) 137 76 90 79 0 13 10 26 10
  794. * (C notation - ASCII) \211 L Z O \0 \r \n \032 \n
  795. */
  796. /* (vda) comparison with lzop v1.02rc1 ("lzop -1 <FILE" cmd):
  797. * Only slight differences in header:
  798. * -00000000 89 4c 5a 4f 00 0d 0a 1a 0a 10 20 20 20 09 40 02
  799. * +00000000 89 4c 5a 4f 00 0d 0a 1a 0a 10 10 20 30 09 40 02
  800. * ^^^^^ ^^^^^
  801. * version lib_version
  802. * -00000010 01 03 00 00 0d 00 00 81 a4 49 f7 a6 3f 00 00 00
  803. * +00000010 01 03 00 00 01 00 00 00 00 00 00 00 00 00 00 00
  804. * ^^^^^^^^^^^ ^^^^^^^^^^^ ^^^^^^^^^^^
  805. * flags mode mtime
  806. * -00000020 00 00 2d 67 04 17 00 04 00 00 00 03 ed ec 9d 6d
  807. * +00000020 00 00 10 5f 00 c1 00 04 00 00 00 03 ed ec 9d 6d
  808. * ^^^^^^^^^^^
  809. * chksum
  810. * The rest is identical.
  811. */
  812. static const unsigned char lzop_magic[9] ALIGN1 = {
  813. 0x89, 0x4c, 0x5a, 0x4f, 0x00, 0x0d, 0x0a, 0x1a, 0x0a
  814. };
  815. /* This coding is derived from Alexander Lehmann's pngcheck code. */
  816. static void check_magic(void)
  817. {
  818. unsigned char magic[sizeof(lzop_magic)];
  819. xread(0, magic, sizeof(magic));
  820. if (memcmp(magic, lzop_magic, sizeof(lzop_magic)) != 0)
  821. bb_simple_error_msg_and_die("bad magic number");
  822. }
  823. /**********************************************************************/
  824. // lzop file header
  825. /**********************************************************************/
  826. static void write_header(header_t *h)
  827. {
  828. char *end;
  829. xwrite(1, lzop_magic, sizeof(lzop_magic));
  830. init_chksum();
  831. /* Our caller leaves name zero-filled, so len == 0 */
  832. end = h->len_and_name+1 + 0; /* 0 is strlen(h->len_and_name+1) */
  833. /* Store length byte */
  834. /*h->len_and_name[0] = end - (h->len_and_name+1); - zero already */
  835. f_write(&h->version_be16, end - (char*)&h->version_be16);
  836. h->flags32 = htonl(h->flags32); /* native endianness for lzo_compress() */
  837. write32(chksum_getresult(h->flags32));
  838. }
  839. static int read_header(header_t *h)
  840. {
  841. int l;
  842. uint32_t checksum;
  843. /* As it stands now, only h->flags32 is used by our caller.
  844. * Therefore we don't store many fields in h->FIELD.
  845. */
  846. unsigned h_version;
  847. unsigned h_version_needed_to_extract;
  848. init_chksum();
  849. /* We don't support versions < 0.94, since 0.94
  850. * came only 2 months after 0.90:
  851. * 0.90 (10 Aug 1997): First public release of lzop
  852. * 0.94 (15 Oct 1997): Header format change
  853. */
  854. /* Read up to and including name length byte */
  855. f_read(&h->version_be16, ((char*)&h->len_and_name[1]) - ((char*)&h->version_be16));
  856. h_version = htons(h->version_be16);
  857. if (h_version < 0x0940)
  858. return 3;
  859. h_version_needed_to_extract = htons(h->version_needed_to_extract_be16);
  860. if (h_version_needed_to_extract > LZOP_VERSION)
  861. return 16;
  862. if (h_version_needed_to_extract < 0x0940)
  863. return 3;
  864. if (h->method <= 0)
  865. return 14;
  866. /* former lzo_get_method(h): */
  867. if (h->method == M_LZO1X_1) {
  868. if (h->level == 0)
  869. h->level = 3;
  870. } else if (h->method == M_LZO1X_1_15) {
  871. if (h->level == 0)
  872. h->level = 1;
  873. } else if (h->method == M_LZO1X_999) {
  874. if (h->level == 0)
  875. h->level = 9;
  876. } else
  877. return -1; /* not a LZO method */
  878. /* check compression level */
  879. if (h->level < 1 || h->level > 9)
  880. return 15;
  881. h->flags32 = ntohl(h->flags32);
  882. if (h->flags32 & F_H_FILTER)
  883. return 16; /* filter not supported */
  884. /* check reserved flags */
  885. if (h->flags32 & F_RESERVED)
  886. return -13;
  887. l = h->len_and_name[0];
  888. if (l > 0)
  889. /* UNUSED */ f_read(h->len_and_name+1, l);
  890. /* UNUSED h->len_and_name[1+l] = 0; */
  891. checksum = chksum_getresult(h->flags32);
  892. if (read32() != checksum)
  893. return 2;
  894. /* skip extra field [not used yet] */
  895. if (h->flags32 & F_H_EXTRA_FIELD) {
  896. uint32_t extra_field_len;
  897. uint32_t extra_field_checksum;
  898. uint32_t k;
  899. char dummy;
  900. /* note: the checksum also covers the length */
  901. init_chksum();
  902. extra_field_len = f_read32();
  903. for (k = 0; k < extra_field_len; k++)
  904. f_read(&dummy, 1);
  905. checksum = chksum_getresult(h->flags32);
  906. extra_field_checksum = read32();
  907. if (extra_field_checksum != checksum)
  908. return 3;
  909. }
  910. return 0;
  911. }
  912. /**********************************************************************/
  913. // compress
  914. /**********************************************************************/
  915. static void lzo_set_method(header_t *h)
  916. {
  917. smallint level;
  918. /* levels 2..6 or none (defaults to level 3) */
  919. h->method = M_LZO1X_1;
  920. level = 5; /* levels 2-6 are actually the same */
  921. if (option_mask32 & OPT_1) {
  922. h->method = M_LZO1X_1_15;
  923. level = 1;
  924. }
  925. if (option_mask32 & OPT_789) {
  926. #if ENABLE_LZOP_COMPR_HIGH
  927. h->method = M_LZO1X_999;
  928. level = 9;
  929. if (option_mask32 & OPT_7)
  930. level = 7;
  931. else if (option_mask32 & OPT_8)
  932. level = 8;
  933. #else
  934. bb_simple_error_msg_and_die("high compression not compiled in");
  935. #endif
  936. }
  937. h->level = level;
  938. }
  939. static int do_lzo_compress(void)
  940. {
  941. header_t header;
  942. #define h (&header)
  943. memset(h, 0, sizeof(*h));
  944. lzo_set_method(h);
  945. h->version_be16 = htons(LZOP_VERSION & 0xffff);
  946. h->version_needed_to_extract_be16 = htons(0x0940);
  947. h->lib_version_be16 = htons(lzo_version() & 0xffff);
  948. h->flags32 = htonl((F_OS & F_OS_MASK) | (F_CS & F_CS_MASK));
  949. if (!(option_mask32 & OPT_F) || h->method == M_LZO1X_999) {
  950. h->flags32 |= htonl(F_ADLER32_D);
  951. if (option_mask32 & OPT_C)
  952. h->flags32 |= htonl(F_ADLER32_C);
  953. }
  954. /* write_header() also converts h->flags32 to native endianness */
  955. write_header(h);
  956. return lzo_compress(h);
  957. #undef h
  958. }
  959. /**********************************************************************/
  960. // decompress
  961. /**********************************************************************/
  962. static int do_lzo_decompress(void)
  963. {
  964. int r;
  965. header_t header;
  966. check_magic();
  967. r = read_header(&header);
  968. if (r != 0)
  969. bb_error_msg_and_die("header_error %d", r);
  970. return lzo_decompress(header.flags32);
  971. }
  972. static char* FAST_FUNC make_new_name_lzop(char *filename, const char *expected_ext UNUSED_PARAM)
  973. {
  974. if (option_mask32 & OPT_DECOMPRESS) {
  975. char *extension = strrchr(filename, '.');
  976. if (!extension || strcmp(extension + 1, "lzo") != 0)
  977. return xasprintf("%s.out", filename);
  978. *extension = '\0';
  979. return filename;
  980. }
  981. return xasprintf("%s.lzo", filename);
  982. }
  983. static IF_DESKTOP(long long) int FAST_FUNC pack_lzop(transformer_state_t *xstate UNUSED_PARAM)
  984. {
  985. if (option_mask32 & OPT_DECOMPRESS)
  986. return do_lzo_decompress();
  987. return do_lzo_compress();
  988. }
  989. int lzop_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  990. int lzop_main(int argc UNUSED_PARAM, char **argv)
  991. {
  992. INIT_G();
  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_new_table_le();
  1008. return bbunpack(argv, pack_lzop, make_new_name_lzop, /*unused:*/ NULL);
  1009. }