xz_dec_bcj.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. /*
  2. * Branch/Call/Jump (BCJ) filter decoders
  3. *
  4. * Authors: Lasse Collin <lasse.collin@tukaani.org>
  5. * Igor Pavlov <http://7-zip.org/>
  6. *
  7. * This file has been put into the public domain.
  8. * You can do whatever you want with this file.
  9. */
  10. #include "xz_private.h"
  11. /*
  12. * The rest of the file is inside this ifdef. It makes things a little more
  13. * convenient when building without support for any BCJ filters.
  14. */
  15. #ifdef XZ_DEC_BCJ
  16. struct xz_dec_bcj {
  17. /* Type of the BCJ filter being used */
  18. enum {
  19. BCJ_X86 = 4, /* x86 or x86-64 */
  20. BCJ_POWERPC = 5, /* Big endian only */
  21. BCJ_IA64 = 6, /* Big or little endian */
  22. BCJ_ARM = 7, /* Little endian only */
  23. BCJ_ARMTHUMB = 8, /* Little endian only */
  24. BCJ_SPARC = 9 /* Big or little endian */
  25. } type;
  26. /*
  27. * Return value of the next filter in the chain. We need to preserve
  28. * this information across calls, because we must not call the next
  29. * filter anymore once it has returned XZ_STREAM_END.
  30. */
  31. enum xz_ret ret;
  32. /* True if we are operating in single-call mode. */
  33. bool single_call;
  34. /*
  35. * Absolute position relative to the beginning of the uncompressed
  36. * data (in a single .xz Block). We care only about the lowest 32
  37. * bits so this doesn't need to be uint64_t even with big files.
  38. */
  39. uint32_t pos;
  40. /* x86 filter state */
  41. uint32_t x86_prev_mask;
  42. /* Temporary space to hold the variables from struct xz_buf */
  43. uint8_t *out;
  44. size_t out_pos;
  45. size_t out_size;
  46. struct {
  47. /* Amount of already filtered data in the beginning of buf */
  48. size_t filtered;
  49. /* Total amount of data currently stored in buf */
  50. size_t size;
  51. /*
  52. * Buffer to hold a mix of filtered and unfiltered data. This
  53. * needs to be big enough to hold Alignment + 2 * Look-ahead:
  54. *
  55. * Type Alignment Look-ahead
  56. * x86 1 4
  57. * PowerPC 4 0
  58. * IA-64 16 0
  59. * ARM 4 0
  60. * ARM-Thumb 2 2
  61. * SPARC 4 0
  62. */
  63. uint8_t buf[16];
  64. } temp;
  65. };
  66. #ifdef XZ_DEC_X86
  67. /*
  68. * This is macro used to test the most significant byte of a memory address
  69. * in an x86 instruction.
  70. */
  71. #define bcj_x86_test_msbyte(b) ((b) == 0x00 || (b) == 0xFF)
  72. static noinline_for_stack size_t XZ_FUNC bcj_x86(
  73. struct xz_dec_bcj *s, uint8_t *buf, size_t size)
  74. {
  75. static const bool mask_to_allowed_status[8]
  76. = { true, true, true, false, true, false, false, false };
  77. static const uint8_t mask_to_bit_num[8] = { 0, 1, 2, 2, 3, 3, 3, 3 };
  78. size_t i;
  79. size_t prev_pos = (size_t)-1;
  80. uint32_t prev_mask = s->x86_prev_mask;
  81. uint32_t src;
  82. uint32_t dest;
  83. uint32_t j;
  84. uint8_t b;
  85. if (size <= 4)
  86. return 0;
  87. size -= 4;
  88. for (i = 0; i < size; ++i) {
  89. if ((buf[i] & 0xFE) != 0xE8)
  90. continue;
  91. prev_pos = i - prev_pos;
  92. if (prev_pos > 3) {
  93. prev_mask = 0;
  94. } else {
  95. prev_mask = (prev_mask << (prev_pos - 1)) & 7;
  96. if (prev_mask != 0) {
  97. b = buf[i + 4 - mask_to_bit_num[prev_mask]];
  98. if (!mask_to_allowed_status[prev_mask]
  99. || bcj_x86_test_msbyte(b)) {
  100. prev_pos = i;
  101. prev_mask = (prev_mask << 1) | 1;
  102. continue;
  103. }
  104. }
  105. }
  106. prev_pos = i;
  107. if (bcj_x86_test_msbyte(buf[i + 4])) {
  108. src = get_unaligned_le32(buf + i + 1);
  109. while (true) {
  110. dest = src - (s->pos + (uint32_t)i + 5);
  111. if (prev_mask == 0)
  112. break;
  113. j = mask_to_bit_num[prev_mask] * 8;
  114. b = (uint8_t)(dest >> (24 - j));
  115. if (!bcj_x86_test_msbyte(b))
  116. break;
  117. src = dest ^ (((uint32_t)1 << (32 - j)) - 1);
  118. }
  119. dest &= 0x01FFFFFF;
  120. dest |= (uint32_t)0 - (dest & 0x01000000);
  121. put_unaligned_le32(dest, buf + i + 1);
  122. i += 4;
  123. } else {
  124. prev_mask = (prev_mask << 1) | 1;
  125. }
  126. }
  127. prev_pos = i - prev_pos;
  128. s->x86_prev_mask = prev_pos > 3 ? 0 : prev_mask << (prev_pos - 1);
  129. return i;
  130. }
  131. #endif
  132. #ifdef XZ_DEC_POWERPC
  133. static noinline_for_stack size_t XZ_FUNC bcj_powerpc(
  134. struct xz_dec_bcj *s, uint8_t *buf, size_t size)
  135. {
  136. size_t i;
  137. uint32_t instr;
  138. for (i = 0; i + 4 <= size; i += 4) {
  139. instr = get_unaligned_be32(buf + i);
  140. if ((instr & 0xFC000003) == 0x48000001) {
  141. instr &= 0x03FFFFFC;
  142. instr -= s->pos + (uint32_t)i;
  143. instr &= 0x03FFFFFC;
  144. instr |= 0x48000001;
  145. put_unaligned_be32(instr, buf + i);
  146. }
  147. }
  148. return i;
  149. }
  150. #endif
  151. #ifdef XZ_DEC_IA64
  152. static noinline_for_stack size_t XZ_FUNC bcj_ia64(
  153. struct xz_dec_bcj *s, uint8_t *buf, size_t size)
  154. {
  155. static const uint8_t branch_table[32] = {
  156. 0, 0, 0, 0, 0, 0, 0, 0,
  157. 0, 0, 0, 0, 0, 0, 0, 0,
  158. 4, 4, 6, 6, 0, 0, 7, 7,
  159. 4, 4, 0, 0, 4, 4, 0, 0
  160. };
  161. /*
  162. * The local variables take a little bit stack space, but it's less
  163. * than what LZMA2 decoder takes, so it doesn't make sense to reduce
  164. * stack usage here without doing that for the LZMA2 decoder too.
  165. */
  166. /* Loop counters */
  167. size_t i;
  168. size_t j;
  169. /* Instruction slot (0, 1, or 2) in the 128-bit instruction word */
  170. uint32_t slot;
  171. /* Bitwise offset of the instruction indicated by slot */
  172. uint32_t bit_pos;
  173. /* bit_pos split into byte and bit parts */
  174. uint32_t byte_pos;
  175. uint32_t bit_res;
  176. /* Address part of an instruction */
  177. uint32_t addr;
  178. /* Mask used to detect which instructions to convert */
  179. uint32_t mask;
  180. /* 41-bit instruction stored somewhere in the lowest 48 bits */
  181. uint64_t instr;
  182. /* Instruction normalized with bit_res for easier manipulation */
  183. uint64_t norm;
  184. for (i = 0; i + 16 <= size; i += 16) {
  185. mask = branch_table[buf[i] & 0x1F];
  186. for (slot = 0, bit_pos = 5; slot < 3; ++slot, bit_pos += 41) {
  187. if (((mask >> slot) & 1) == 0)
  188. continue;
  189. byte_pos = bit_pos >> 3;
  190. bit_res = bit_pos & 7;
  191. instr = 0;
  192. for (j = 0; j < 6; ++j)
  193. instr |= (uint64_t)(buf[i + j + byte_pos])
  194. << (8 * j);
  195. norm = instr >> bit_res;
  196. if (((norm >> 37) & 0x0F) == 0x05
  197. && ((norm >> 9) & 0x07) == 0) {
  198. addr = (norm >> 13) & 0x0FFFFF;
  199. addr |= ((uint32_t)(norm >> 36) & 1) << 20;
  200. addr <<= 4;
  201. addr -= s->pos + (uint32_t)i;
  202. addr >>= 4;
  203. norm &= ~((uint64_t)0x8FFFFF << 13);
  204. norm |= (uint64_t)(addr & 0x0FFFFF) << 13;
  205. norm |= (uint64_t)(addr & 0x100000)
  206. << (36 - 20);
  207. instr &= (1 << bit_res) - 1;
  208. instr |= norm << bit_res;
  209. for (j = 0; j < 6; j++)
  210. buf[i + j + byte_pos]
  211. = (uint8_t)(instr >> (8 * j));
  212. }
  213. }
  214. }
  215. return i;
  216. }
  217. #endif
  218. #ifdef XZ_DEC_ARM
  219. static noinline_for_stack size_t XZ_FUNC bcj_arm(
  220. struct xz_dec_bcj *s, uint8_t *buf, size_t size)
  221. {
  222. size_t i;
  223. uint32_t addr;
  224. for (i = 0; i + 4 <= size; i += 4) {
  225. if (buf[i + 3] == 0xEB) {
  226. addr = (uint32_t)buf[i] | ((uint32_t)buf[i + 1] << 8)
  227. | ((uint32_t)buf[i + 2] << 16);
  228. addr <<= 2;
  229. addr -= s->pos + (uint32_t)i + 8;
  230. addr >>= 2;
  231. buf[i] = (uint8_t)addr;
  232. buf[i + 1] = (uint8_t)(addr >> 8);
  233. buf[i + 2] = (uint8_t)(addr >> 16);
  234. }
  235. }
  236. return i;
  237. }
  238. #endif
  239. #ifdef XZ_DEC_ARMTHUMB
  240. static noinline_for_stack size_t XZ_FUNC bcj_armthumb(
  241. struct xz_dec_bcj *s, uint8_t *buf, size_t size)
  242. {
  243. size_t i;
  244. uint32_t addr;
  245. for (i = 0; i + 4 <= size; i += 2) {
  246. if ((buf[i + 1] & 0xF8) == 0xF0
  247. && (buf[i + 3] & 0xF8) == 0xF8) {
  248. addr = (((uint32_t)buf[i + 1] & 0x07) << 19)
  249. | ((uint32_t)buf[i] << 11)
  250. | (((uint32_t)buf[i + 3] & 0x07) << 8)
  251. | (uint32_t)buf[i + 2];
  252. addr <<= 1;
  253. addr -= s->pos + (uint32_t)i + 4;
  254. addr >>= 1;
  255. buf[i + 1] = (uint8_t)(0xF0 | ((addr >> 19) & 0x07));
  256. buf[i] = (uint8_t)(addr >> 11);
  257. buf[i + 3] = (uint8_t)(0xF8 | ((addr >> 8) & 0x07));
  258. buf[i + 2] = (uint8_t)addr;
  259. i += 2;
  260. }
  261. }
  262. return i;
  263. }
  264. #endif
  265. #ifdef XZ_DEC_SPARC
  266. static noinline_for_stack size_t XZ_FUNC bcj_sparc(
  267. struct xz_dec_bcj *s, uint8_t *buf, size_t size)
  268. {
  269. size_t i;
  270. uint32_t instr;
  271. for (i = 0; i + 4 <= size; i += 4) {
  272. instr = get_unaligned_be32(buf + i);
  273. if ((instr >> 22) == 0x100 || (instr >> 22) == 0x1FF) {
  274. instr <<= 2;
  275. instr -= s->pos + (uint32_t)i;
  276. instr >>= 2;
  277. instr = ((uint32_t)0x40000000 - (instr & 0x400000))
  278. | 0x40000000 | (instr & 0x3FFFFF);
  279. put_unaligned_be32(instr, buf + i);
  280. }
  281. }
  282. return i;
  283. }
  284. #endif
  285. /*
  286. * Apply the selected BCJ filter. Update *pos and s->pos to match the amount
  287. * of data that got filtered.
  288. *
  289. * NOTE: This is implemented as a switch statement to avoid using function
  290. * pointers, which could be problematic in the kernel boot code, which must
  291. * avoid pointers to static data (at least on x86).
  292. */
  293. static void XZ_FUNC bcj_apply(struct xz_dec_bcj *s,
  294. uint8_t *buf, size_t *pos, size_t size)
  295. {
  296. size_t filtered;
  297. buf += *pos;
  298. size -= *pos;
  299. switch (s->type) {
  300. #ifdef XZ_DEC_X86
  301. case BCJ_X86:
  302. filtered = bcj_x86(s, buf, size);
  303. break;
  304. #endif
  305. #ifdef XZ_DEC_POWERPC
  306. case BCJ_POWERPC:
  307. filtered = bcj_powerpc(s, buf, size);
  308. break;
  309. #endif
  310. #ifdef XZ_DEC_IA64
  311. case BCJ_IA64:
  312. filtered = bcj_ia64(s, buf, size);
  313. break;
  314. #endif
  315. #ifdef XZ_DEC_ARM
  316. case BCJ_ARM:
  317. filtered = bcj_arm(s, buf, size);
  318. break;
  319. #endif
  320. #ifdef XZ_DEC_ARMTHUMB
  321. case BCJ_ARMTHUMB:
  322. filtered = bcj_armthumb(s, buf, size);
  323. break;
  324. #endif
  325. #ifdef XZ_DEC_SPARC
  326. case BCJ_SPARC:
  327. filtered = bcj_sparc(s, buf, size);
  328. break;
  329. #endif
  330. default:
  331. /* Never reached but silence compiler warnings. */
  332. filtered = 0;
  333. break;
  334. }
  335. *pos += filtered;
  336. s->pos += filtered;
  337. }
  338. /*
  339. * Flush pending filtered data from temp to the output buffer.
  340. * Move the remaining mixture of possibly filtered and unfiltered
  341. * data to the beginning of temp.
  342. */
  343. static void XZ_FUNC bcj_flush(struct xz_dec_bcj *s, struct xz_buf *b)
  344. {
  345. size_t copy_size;
  346. copy_size = min_t(size_t, s->temp.filtered, b->out_size - b->out_pos);
  347. memcpy(b->out + b->out_pos, s->temp.buf, copy_size);
  348. b->out_pos += copy_size;
  349. s->temp.filtered -= copy_size;
  350. s->temp.size -= copy_size;
  351. memmove(s->temp.buf, s->temp.buf + copy_size, s->temp.size);
  352. }
  353. /*
  354. * The BCJ filter functions are primitive in sense that they process the
  355. * data in chunks of 1-16 bytes. To hide this issue, this function does
  356. * some buffering.
  357. */
  358. XZ_EXTERN enum xz_ret XZ_FUNC xz_dec_bcj_run(struct xz_dec_bcj *s,
  359. struct xz_dec_lzma2 *lzma2, struct xz_buf *b)
  360. {
  361. size_t out_start;
  362. /*
  363. * Flush pending already filtered data to the output buffer. Return
  364. * immediatelly if we couldn't flush everything, or if the next
  365. * filter in the chain had already returned XZ_STREAM_END.
  366. */
  367. if (s->temp.filtered > 0) {
  368. bcj_flush(s, b);
  369. if (s->temp.filtered > 0)
  370. return XZ_OK;
  371. if (s->ret == XZ_STREAM_END)
  372. return XZ_STREAM_END;
  373. }
  374. /*
  375. * If we have more output space than what is currently pending in
  376. * temp, copy the unfiltered data from temp to the output buffer
  377. * and try to fill the output buffer by decoding more data from the
  378. * next filter in the chain. Apply the BCJ filter on the new data
  379. * in the output buffer. If everything cannot be filtered, copy it
  380. * to temp and rewind the output buffer position accordingly.
  381. */
  382. if (s->temp.size < b->out_size - b->out_pos) {
  383. out_start = b->out_pos;
  384. memcpy(b->out + b->out_pos, s->temp.buf, s->temp.size);
  385. b->out_pos += s->temp.size;
  386. s->ret = xz_dec_lzma2_run(lzma2, b);
  387. if (s->ret != XZ_STREAM_END
  388. && (s->ret != XZ_OK || s->single_call))
  389. return s->ret;
  390. bcj_apply(s, b->out, &out_start, b->out_pos);
  391. /*
  392. * As an exception, if the next filter returned XZ_STREAM_END,
  393. * we can do that too, since the last few bytes that remain
  394. * unfiltered are meant to remain unfiltered.
  395. */
  396. if (s->ret == XZ_STREAM_END)
  397. return XZ_STREAM_END;
  398. s->temp.size = b->out_pos - out_start;
  399. b->out_pos -= s->temp.size;
  400. memcpy(s->temp.buf, b->out + b->out_pos, s->temp.size);
  401. }
  402. /*
  403. * If we have unfiltered data in temp, try to fill by decoding more
  404. * data from the next filter. Apply the BCJ filter on temp. Then we
  405. * hopefully can fill the actual output buffer by copying filtered
  406. * data from temp. A mix of filtered and unfiltered data may be left
  407. * in temp; it will be taken care on the next call to this function.
  408. */
  409. if (s->temp.size > 0) {
  410. /* Make b->out{,_pos,_size} temporarily point to s->temp. */
  411. s->out = b->out;
  412. s->out_pos = b->out_pos;
  413. s->out_size = b->out_size;
  414. b->out = s->temp.buf;
  415. b->out_pos = s->temp.size;
  416. b->out_size = sizeof(s->temp.buf);
  417. s->ret = xz_dec_lzma2_run(lzma2, b);
  418. s->temp.size = b->out_pos;
  419. b->out = s->out;
  420. b->out_pos = s->out_pos;
  421. b->out_size = s->out_size;
  422. if (s->ret != XZ_OK && s->ret != XZ_STREAM_END)
  423. return s->ret;
  424. bcj_apply(s, s->temp.buf, &s->temp.filtered, s->temp.size);
  425. /*
  426. * If the next filter returned XZ_STREAM_END, we mark that
  427. * everything is filtered, since the last unfiltered bytes
  428. * of the stream are meant to be left as is.
  429. */
  430. if (s->ret == XZ_STREAM_END)
  431. s->temp.filtered = s->temp.size;
  432. bcj_flush(s, b);
  433. if (s->temp.filtered > 0)
  434. return XZ_OK;
  435. }
  436. return s->ret;
  437. }
  438. XZ_EXTERN struct xz_dec_bcj * XZ_FUNC xz_dec_bcj_create(bool single_call)
  439. {
  440. struct xz_dec_bcj *s = kmalloc(sizeof(*s), GFP_KERNEL);
  441. if (s != NULL)
  442. s->single_call = single_call;
  443. return s;
  444. }
  445. XZ_EXTERN enum xz_ret XZ_FUNC xz_dec_bcj_reset(
  446. struct xz_dec_bcj *s, uint8_t id)
  447. {
  448. switch (id) {
  449. #ifdef XZ_DEC_X86
  450. case BCJ_X86:
  451. #endif
  452. #ifdef XZ_DEC_POWERPC
  453. case BCJ_POWERPC:
  454. #endif
  455. #ifdef XZ_DEC_IA64
  456. case BCJ_IA64:
  457. #endif
  458. #ifdef XZ_DEC_ARM
  459. case BCJ_ARM:
  460. #endif
  461. #ifdef XZ_DEC_ARMTHUMB
  462. case BCJ_ARMTHUMB:
  463. #endif
  464. #ifdef XZ_DEC_SPARC
  465. case BCJ_SPARC:
  466. #endif
  467. break;
  468. default:
  469. /* Unsupported Filter ID */
  470. return XZ_OPTIONS_ERROR;
  471. }
  472. s->type = id;
  473. s->ret = XZ_OK;
  474. s->pos = 0;
  475. s->x86_prev_mask = 0;
  476. s->temp.filtered = 0;
  477. s->temp.size = 0;
  478. return XZ_OK;
  479. }
  480. #endif