lzop.c 30 KB

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