lzma_decode.S 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2008 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #define FIXED_PROPS
  19. #define LZMA_BASE_SIZE 1846
  20. #define LZMA_LIT_SIZE 768
  21. #define LZMA_PROPERTIES_SIZE 5
  22. #define kNumTopBits 24
  23. #define kTopValue (1 << kNumTopBits)
  24. #define kNumBitModelTotalBits 11
  25. #define kBitModelTotal (1 << kNumBitModelTotalBits)
  26. #define kNumMoveBits 5
  27. #define kNumPosBitsMax 4
  28. #define kNumPosStatesMax (1 << kNumPosBitsMax)
  29. #define kLenNumLowBits 3
  30. #define kLenNumLowSymbols (1 << kLenNumLowBits)
  31. #define kLenNumMidBits 3
  32. #define kLenNumMidSymbols (1 << kLenNumMidBits)
  33. #define kLenNumHighBits 8
  34. #define kLenNumHighSymbols (1 << kLenNumHighBits)
  35. #define LenChoice 0
  36. #define LenChoice2 (LenChoice + 1)
  37. #define LenLow (LenChoice2 + 1)
  38. #define LenMid (LenLow + (kNumPosStatesMax << kLenNumLowBits))
  39. #define LenHigh (LenMid + (kNumPosStatesMax << kLenNumMidBits))
  40. #define kNumLenProbs (LenHigh + kLenNumHighSymbols)
  41. #define kNumStates 12
  42. #define kNumLitStates 7
  43. #define kStartPosModelIndex 4
  44. #define kEndPosModelIndex 14
  45. #define kNumFullDistances (1 << (kEndPosModelIndex >> 1))
  46. #define kNumPosSlotBits 6
  47. #define kNumLenToPosStates 4
  48. #define kNumAlignBits 4
  49. #define kAlignTableSize (1 << kNumAlignBits)
  50. #define kMatchMinLen 2
  51. #define IsMatch 0
  52. #define IsRep (IsMatch + (kNumStates << kNumPosBitsMax))
  53. #define IsRepG0 (IsRep + kNumStates)
  54. #define IsRepG1 (IsRepG0 + kNumStates)
  55. #define IsRepG2 (IsRepG1 + kNumStates)
  56. #define IsRep0Long (IsRepG2 + kNumStates)
  57. #define PosSlot (IsRep0Long + (kNumStates << kNumPosBitsMax))
  58. #define SpecPos (PosSlot + (kNumLenToPosStates << kNumPosSlotBits))
  59. #define Align (SpecPos + kNumFullDistances - kEndPosModelIndex)
  60. #define LenCoder (Align + kAlignTableSize)
  61. #define RepLenCoder (LenCoder + kNumLenProbs)
  62. #define Literal (RepLenCoder + kNumLenProbs)
  63. #if 0
  64. DbgOut:
  65. pushf
  66. pushl %ebp
  67. pushl %edi
  68. pushl %esi
  69. pushl %edx
  70. pushl %ecx
  71. pushl %ebx
  72. pushl %eax
  73. call _DebugPrint
  74. popl %eax
  75. popl %ebx
  76. popl %ecx
  77. popl %edx
  78. popl %esi
  79. popl %edi
  80. popl %ebp
  81. popf
  82. ret
  83. /*
  84. * int LzmaDecodeProperties(CLzmaProperties *propsRes,
  85. * const unsigned char *propsData,
  86. * int size);
  87. */
  88. _LzmaDecodePropertiesA:
  89. movb (%edx), %dl
  90. xorl %ecx, %ecx
  91. 1:
  92. cmpb $45, %dl
  93. jb 2f
  94. incl %ecx
  95. subb $45, %dl
  96. jmp 1b
  97. 2:
  98. movl %ecx, 8(%eax) /* pb */
  99. xorl %ecx, %ecx
  100. 1:
  101. cmpb $9, %dl
  102. jb 2f
  103. incl %ecx
  104. subb $9, %dl
  105. 2:
  106. movl %ecx, 4(%eax) /* lp */
  107. movb %dl, %cl
  108. movl %ecx, (%eax) /* lc */
  109. #endif
  110. #ifndef ASM_FILE
  111. xorl %eax, %eax
  112. #endif
  113. ret
  114. #define out_size 8(%ebp)
  115. #define now_pos -4(%ebp)
  116. #define prev_byte -8(%ebp)
  117. #define range -12(%ebp)
  118. #define code -16(%ebp)
  119. #define state -20(%ebp)
  120. #define rep0 -24(%ebp)
  121. #define rep1 -28(%ebp)
  122. #define rep2 -32(%ebp)
  123. #define rep3 -36(%ebp)
  124. #ifdef FIXED_PROPS
  125. #define FIXED_LC 3
  126. #define FIXED_LP 0
  127. #define FIXED_PB 2
  128. #define POS_STATE_MASK ((1 << (FIXED_PB)) - 1)
  129. #define LIT_POS_MASK ((1 << (FIXED_LP)) - 1)
  130. #define LOCAL_SIZE 36
  131. #else
  132. #define lc (%ebx)
  133. #define lp 4(%ebx)
  134. #define pb 8(%ebx)
  135. #define probs 12(%ebx)
  136. #define pos_state_mask -40(%ebp)
  137. #define lit_pos_mask -44(%ebp)
  138. #define LOCAL_SIZE 44
  139. #endif
  140. RangeDecoderBitDecode:
  141. #ifdef FIXED_PROPS
  142. leal (%ebx, %eax, 4), %eax
  143. #else
  144. shll $2, %eax
  145. addl probs, %eax
  146. #endif
  147. movl %eax, %ecx
  148. movl (%ecx), %eax
  149. movl range, %edx
  150. shrl $kNumBitModelTotalBits, %edx
  151. mull %edx
  152. cmpl code, %eax
  153. jbe 1f
  154. movl %eax, range
  155. movl $kBitModelTotal, %edx
  156. subl (%ecx), %edx
  157. shrl $kNumMoveBits, %edx
  158. addl %edx, (%ecx)
  159. clc
  160. 3:
  161. pushf
  162. cmpl $kTopValue, range
  163. jnc 2f
  164. shll $8, code
  165. lodsb
  166. movb %al, code
  167. shll $8, range
  168. 2:
  169. popf
  170. ret
  171. 1:
  172. subl %eax, range
  173. subl %eax, code
  174. movl (%ecx), %edx
  175. shrl $kNumMoveBits, %edx
  176. subl %edx, (%ecx)
  177. stc
  178. jmp 3b
  179. RangeDecoderBitTreeDecode:
  180. RangeDecoderReverseBitTreeDecode:
  181. movzbl %cl, %ecx
  182. xorl %edx, %edx
  183. pushl %edx
  184. incl %edx
  185. pushl %edx
  186. 1:
  187. pushl %eax
  188. pushl %ecx
  189. pushl %edx
  190. addl %edx, %eax
  191. call RangeDecoderBitDecode
  192. popl %edx
  193. popl %ecx
  194. jnc 2f
  195. movl 4(%esp), %eax
  196. orl %eax, 8(%esp)
  197. stc
  198. 2:
  199. adcl %edx, %edx
  200. popl %eax
  201. shll $1, (%esp)
  202. loop 1b
  203. popl %ecx
  204. subl %ecx, %edx /* RangeDecoderBitTreeDecode */
  205. popl %ecx /* RangeDecoderReverseBitTreeDecode */
  206. ret
  207. LzmaLenDecode:
  208. pushl %eax
  209. addl $LenChoice, %eax
  210. call RangeDecoderBitDecode
  211. popl %eax
  212. jc 1f
  213. pushl $0
  214. movb $kLenNumLowBits, %cl
  215. addl $LenLow, %eax
  216. 2:
  217. movl 12(%esp), %edx
  218. shll %cl, %edx
  219. addl %edx, %eax
  220. 3:
  221. call RangeDecoderBitTreeDecode
  222. popl %eax
  223. addl %eax, %edx
  224. ret
  225. 1:
  226. pushl %eax
  227. addl $LenChoice2, %eax
  228. call RangeDecoderBitDecode
  229. popl %eax
  230. jc 1f
  231. pushl $kLenNumLowSymbols
  232. movb $kLenNumMidBits, %cl
  233. addl $LenMid, %eax
  234. jmp 2b
  235. 1:
  236. pushl $(kLenNumLowSymbols + kLenNumMidSymbols)
  237. addl $LenHigh, %eax
  238. movb $kLenNumHighBits, %cl
  239. jmp 3b
  240. WriteByte:
  241. movb %al, prev_byte
  242. stosb
  243. incl now_pos
  244. ret
  245. /*
  246. * int LzmaDecode(CLzmaDecoderState *vs,
  247. * const unsigned char *inStream,
  248. * unsigned char *outStream,
  249. * SizeT outSize);
  250. */
  251. _LzmaDecodeA:
  252. pushl %ebp
  253. movl %esp, %ebp
  254. subl $LOCAL_SIZE, %esp
  255. #ifndef ASM_FILE
  256. pushl %esi
  257. pushl %edi
  258. pushl %ebx
  259. movl %eax, %ebx
  260. movl %edx, %esi
  261. pushl %ecx
  262. #else
  263. pushl %edi
  264. #endif
  265. cld
  266. #ifdef FIXED_PROPS
  267. movl %ebx, %edi
  268. movl $(Literal + (LZMA_LIT_SIZE << (FIXED_LC + FIXED_LP))), %ecx
  269. #else
  270. movl $LZMA_LIT_SIZE, %eax
  271. movb lc, %cl
  272. addb lp, %cl
  273. shll %cl, %eax
  274. addl $Literal, %eax
  275. movl %eax, %ecx
  276. movl probs, %edi
  277. #endif
  278. movl $(kBitModelTotal >> 1), %eax
  279. rep
  280. stosl
  281. popl %edi
  282. xorl %eax, %eax
  283. movl %eax, now_pos
  284. movl %eax, prev_byte
  285. movl %eax, state
  286. incl %eax
  287. movl %eax, rep0
  288. movl %eax, rep1
  289. movl %eax, rep2
  290. movl %eax, rep3
  291. #ifndef FIXED_PROPS
  292. movl %eax, %edx
  293. movb pb, %cl
  294. shll %cl, %edx
  295. decl %edx
  296. movl %edx, pos_state_mask
  297. movl %eax, %edx
  298. movb lp, %cl
  299. shll %cl, %edx
  300. decl %edx
  301. movl %edx, lit_pos_mask;
  302. #endif
  303. /* RangeDecoderInit */
  304. negl %eax
  305. movl %eax, range
  306. incl %eax
  307. movb $5, %cl
  308. 1:
  309. shll $8, %eax
  310. lodsb
  311. loop 1b
  312. movl %eax, code
  313. lzma_decode_loop:
  314. movl now_pos, %eax
  315. cmpl out_size, %eax
  316. jb 1f
  317. #ifndef ASM_FILE
  318. xorl %eax, %eax
  319. popl %ebx
  320. popl %edi
  321. popl %esi
  322. #endif
  323. movl %ebp, %esp
  324. popl %ebp
  325. ret
  326. 1:
  327. #ifdef FIXED_PROPS
  328. andl $POS_STATE_MASK, %eax
  329. #else
  330. andl pos_state_mask, %eax
  331. #endif
  332. pushl %eax /* posState */
  333. movl state, %edx
  334. shll $kNumPosBitsMax, %edx
  335. addl %edx, %eax
  336. pushl %eax /* (state << kNumPosBitsMax) + posState */
  337. call RangeDecoderBitDecode
  338. jc 1f
  339. movl now_pos, %eax
  340. #ifdef FIXED_PROPS
  341. andl $LIT_POS_MASK, %eax
  342. shll $FIXED_LC, %eax
  343. movl prev_byte, %edx
  344. shrl $(8 - FIXED_LC), %edx
  345. #else
  346. andl lit_pos_mask, %eax
  347. movb lc, %cl
  348. shll %cl, %eax
  349. negb %cl
  350. addb $8, %cl
  351. movl prev_byte, %edx
  352. shrl %cl, %edx
  353. #endif
  354. addl %edx, %eax
  355. movl $LZMA_LIT_SIZE, %edx
  356. mull %edx
  357. addl $Literal, %eax
  358. pushl %eax
  359. incl %edx /* edx = 1 */
  360. movl rep0, %eax
  361. negl %eax
  362. pushl (%edi, %eax) /* matchByte */
  363. cmpb $kNumLitStates, state
  364. jb 5f
  365. /* LzmaLiteralDecodeMatch */
  366. 3:
  367. cmpl $0x100, %edx
  368. jae 4f
  369. xorl %eax, %eax
  370. shlb $1, (%esp)
  371. adcl %eax, %eax
  372. pushl %eax
  373. pushl %edx
  374. shll $8, %eax
  375. leal 0x100(%edx, %eax), %eax
  376. addl 12(%esp), %eax
  377. call RangeDecoderBitDecode
  378. setc %al
  379. popl %edx
  380. adcl %edx, %edx
  381. popl %ecx
  382. cmpb %cl, %al
  383. jz 3b
  384. 5:
  385. /* LzmaLiteralDecode */
  386. cmpl $0x100, %edx
  387. jae 4f
  388. pushl %edx
  389. movl %edx, %eax
  390. addl 8(%esp), %eax
  391. call RangeDecoderBitDecode
  392. popl %edx
  393. adcl %edx, %edx
  394. jmp 5b
  395. 4:
  396. addl $16, %esp
  397. movb %dl, %al
  398. call WriteByte
  399. movb state, %al
  400. cmpb $4, %al
  401. jae 2f
  402. xorb %al, %al
  403. jmp 3f
  404. 2:
  405. subb $3, %al
  406. cmpb $7, %al
  407. jb 3f
  408. subb $3, %al
  409. 3:
  410. movb %al, state
  411. jmp lzma_decode_loop
  412. 1:
  413. movl state, %eax
  414. addl $IsRep, %eax
  415. call RangeDecoderBitDecode
  416. jnc 1f
  417. movl state, %eax
  418. addl $IsRepG0, %eax
  419. call RangeDecoderBitDecode
  420. jc LOCAL(lzma_10a)
  421. movl (%esp), %eax
  422. addl $IsRep0Long, %eax
  423. call RangeDecoderBitDecode
  424. jc LOCAL(lzma_20a)
  425. cmpb $7, state
  426. movb $9, state
  427. jb LOCAL(lzma_100a)
  428. addb $2, state
  429. LOCAL(lzma_100a):
  430. movl $1, %ecx
  431. 3:
  432. movl rep0, %edx
  433. negl %edx
  434. 4:
  435. movb (%edi, %edx), %al
  436. call WriteByte
  437. loop 4b
  438. popl %eax
  439. popl %eax
  440. jmp lzma_decode_loop
  441. LOCAL(lzma_10a):
  442. movl state, %eax
  443. addl $IsRepG1, %eax
  444. call RangeDecoderBitDecode
  445. movl rep1, %edx
  446. jnc LOCAL(lzma_100b)
  447. movl state, %eax
  448. addl $IsRepG2, %eax
  449. call RangeDecoderBitDecode
  450. movl rep2, %edx
  451. jnc LOCAL(lzma_1000a)
  452. movl rep2, %edx
  453. xchgl rep3, %edx
  454. LOCAL(lzma_1000a):
  455. pushl rep1
  456. popl rep2
  457. LOCAL(lzma_100b):
  458. xchg rep0, %edx
  459. movl %edx, rep1
  460. LOCAL(lzma_20a):
  461. movl $RepLenCoder, %eax
  462. call LzmaLenDecode
  463. cmpb $7, state
  464. movb $8, state
  465. jb LOCAL(lzma_100c)
  466. addb $3, state
  467. LOCAL(lzma_100c):
  468. jmp 2f
  469. 1:
  470. movl rep0, %eax
  471. xchgl rep1, %eax
  472. xchgl rep2, %eax
  473. movl %eax, rep3
  474. cmpb $7, state
  475. movb $7, state
  476. jb LOCAL(lzma_10b)
  477. addb $3, state
  478. LOCAL(lzma_10b):
  479. movl $LenCoder, %eax
  480. call LzmaLenDecode
  481. pushl %edx
  482. movl $(kNumLenToPosStates - 1), %eax
  483. cmpl %eax, %edx
  484. jbe LOCAL(lzma_100d)
  485. movl %eax, %edx
  486. LOCAL(lzma_100d):
  487. movb $kNumPosSlotBits, %cl
  488. shll %cl, %edx
  489. leal PosSlot(%edx), %eax
  490. call RangeDecoderBitTreeDecode
  491. movl %edx, rep0
  492. cmpl $kStartPosModelIndex, %edx
  493. jb LOCAL(lzma_100e)
  494. movl %edx, %ecx
  495. shrl $1, %ecx
  496. decl %ecx
  497. movzbl %dl, %eax
  498. andb $1, %al
  499. orb $2, %al
  500. shll %cl, %eax
  501. movl %eax, rep0
  502. cmpl $kEndPosModelIndex, %edx
  503. jae LOCAL(lzma_200a)
  504. movl rep0, %eax
  505. addl $(SpecPos - 1), %eax
  506. subl %edx, %eax
  507. jmp LOCAL(lzma_300a)
  508. LOCAL(lzma_200a):
  509. subb $kNumAlignBits, %cl
  510. /* RangeDecoderDecodeDirectBits */
  511. xorl %edx, %edx
  512. LOCAL(lzma_1000b):
  513. shrl $1, range
  514. shll $1, %edx
  515. movl range, %eax
  516. cmpl %eax, code
  517. jb LOCAL(lzma_2000a)
  518. subl %eax, code
  519. orb $1, %dl
  520. LOCAL(lzma_2000a):
  521. cmpl $kTopValue, %eax
  522. jae LOCAL(lzma_3000a)
  523. shll $8, range
  524. shll $8, code
  525. lodsb
  526. movb %al, code
  527. LOCAL(lzma_3000a):
  528. loop LOCAL(lzma_1000b)
  529. movb $kNumAlignBits, %cl
  530. shll %cl, %edx
  531. addl %edx, rep0
  532. movl $Align, %eax
  533. LOCAL(lzma_300a):
  534. call RangeDecoderReverseBitTreeDecode
  535. addl %ecx, rep0
  536. LOCAL(lzma_100e):
  537. incl rep0
  538. popl %edx
  539. 2:
  540. addl $kMatchMinLen, %edx
  541. movl %edx, %ecx
  542. jmp 3b