-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathloader_lzma.asm
More file actions
404 lines (384 loc) · 8.01 KB
/
loader_lzma.asm
File metadata and controls
404 lines (384 loc) · 8.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
loader_start:
; start address; this code will be injected after the init code
; (some commands below "B32" in the kernel)
mov edi, 0x280000
lea ebx, [edi+loader_size1+16]
lea edx, [ebx+4]
loader_patch1:
mov esi, 0 ; will be patched: start address to copy
mov ecx, 0 ; will be patched: size of data to copy
push esi
rep movsb
jmp edx
loader_size1 = $ - loader_start
loader_patch2:
dd 0x280000 + loader_size
dd 0 ; will be patched: start value for code
; (LZMA-specific)
dd -1
dd _RangeDecoderBitDecode_edx - loader_start + 0x280000
dd _RangeDecoderBitDecode - loader_start + 0x280000
RangeDecoderBitDecode equ dword [ebx]
RangeDecoderBitDecode_edx equ dword [ebx-4]
code_ equ ebx-12
range equ ebx-8
rep1 equ ebx-28
rep2 equ ebx-24
rep3 equ ebx-20
inptr_ldr equ ebx-16
pb equ 0 ; pos state bits
lp equ 0 ; literal pos state bits
lc equ 3 ; literal context bits
posStateMask equ ((1 shl pb)-1)
literalPosMask equ ((1 shl lp)-1)
kNumPosBitsMax = 4
kNumPosStatesMax = (1 shl kNumPosBitsMax)
kLenNumLowBits = 3
kLenNumLowSymbols = (1 shl kLenNumLowBits)
kLenNumMidBits = 3
kLenNumMidSymbols = (1 shl kLenNumMidBits)
kLenNumHighBits = 8
kLenNumHighSymbols = (1 shl kLenNumHighBits)
LenChoice = 0
LenChoice2 = 1
LenLow = 2
LenMid = (LenLow + (kNumPosStatesMax shl kLenNumLowBits))
LenHigh = (LenMid + (kNumPosStatesMax shl kLenNumMidBits))
kNumLenProbs = (LenHigh + kLenNumHighSymbols)
kNumStates = 12
kNumLitStates = 7
kStartPosModelIndex = 4
kEndPosModelIndex = 14
kNumFullDistances = (1 shl (kEndPosModelIndex/2))
kNumPosSlotBits = 6
kNumLenToPosStates = 4
kNumAlignBits = 4
kAlignTableSize = (1 shl kNumAlignBits)
kMatchMinLen = 2
IsMatch = 0
IsRep = 0xC0 ; (IsMatch + (kNumStates shl kNumPosBitsMax))
IsRepG0 = 0xCC ; (IsRep + kNumStates)
IsRepG1 = 0xD8 ; (IsRepG0 + kNumStates)
IsRepG2 = 0xE4 ; (IsRepG1 + kNumStates)
IsRep0Long = 0xF0 ; (IsRepG2 + kNumStates)
PosSlot = 0x1B0 ; (IsRep0Long + (kNumStates shl kNumPosBitsMax))
SpecPos = 0x2B0 ; (PosSlot + (kNumLenToPosStates shl kNumPosSlotBits))
Align_ = 0x322 ; (SpecPos + kNumFullDistances - kEndPosModelIndex)
Lencoder = 0x332 ; (Align_ + kAlignTableSize)
RepLencoder = 0x534 ; (Lencoder + kNumLenProbs)
Literal = 0x736 ; (RepLencoder + kNumLenProbs)
LZMA_BASE_SIZE = 1846 ; must be ==Literal
LZMA_LIT_SIZE = 768
kNumTopBits = 24
kTopValue = (1 shl kNumTopBits)
kNumBitModelTotalBits = 11
kBitModelTotal = (1 shl kNumBitModelTotalBits)
kNumMoveBits = 5
uninit_base = 2C0000h
p = uninit_base
unpacker:
xor ebp, ebp
xor eax, eax
dec eax
lea edi, [rep1]
stosd
stosd
stosd
xchg eax, esi
; mov ecx, Literal + (LZMA_LIT_SIZE shl (lc+lp))
mov ch, (Literal + (LZMA_LIT_SIZE shl (lc+lp)) + 0xFF) shr 8
mov eax, kBitModelTotal/2
mov edi, p
rep stosd
pop edi
push edi
.main_loop:
..loader_patch3:
cmp edi, dword 0 ; will be patched: end of data to unpack
jae .main_loop_done
if posStateMask
mov edx, edi
and edx, posStateMask
else
xor edx, edx
end if
push eax ; al = previous byte
lea eax, [ebp + ((p+IsMatch*4) shr (kNumPosBitsMax+2))]
shl eax, kNumPosBitsMax+2
if posStateMask
call RangeDecoderBitDecode_edx
else
call RangeDecoderBitDecode
end if
pop eax
jc .1
movzx eax, al
if literalPosMask
mov ah, dl
and ah, literalPosMask
end if
if ((LZMA_LIT_SIZE*4) and ((1 shl (8-lc)) - 1)) <> 0
shr eax, 8-lc
imul eax, LZMA_LIT_SIZE*4
else
and al, not ((1 shl (8-lc)) - 1)
imul eax, (LZMA_LIT_SIZE*4) shr (8-lc)
end if
add eax, p+Literal*4
mov dl, 1
cmp ebp, kNumLitStates
jb .literal
mov cl, [edi + esi]
.lx0:
add cl, cl
adc dh, 1
call RangeDecoderBitDecode_edx
adc dl, dl
jc .lx1
xor dh, dl
test dh, 1
mov dh, 0
jnz .lx0
.literal:
@@:
call RangeDecoderBitDecode_edx
adc dl, dl
jnc @b
.lx1:
mov eax, ebp
cmp al, 4
jb @f
cmp al, 10
mov al, 3
jb @f
mov al, 6
@@: sub ebp, eax
xchg eax, edx
.stosb_main_loop:
stosb
jmp .main_loop
.1:
lea eax, [p + IsRep*4 + ebp*4]
call RangeDecoderBitDecode
jnc .10
add eax, (IsRepG0 - IsRep)*4 ;lea eax, [p + IsRepG0*4 + ebp*4]
call RangeDecoderBitDecode
jc .111
mov eax, ebp
shl eax, kNumPosBitsMax+2
add eax, p + IsRep0Long*4
call RangeDecoderBitDecode_edx
jc .1101
cmp ebp, 7
sbb ebp, ebp
lea ebp, [ebp+ebp+11]
mov al, [edi + esi]
jmp .stosb_main_loop
.111:
add eax, (IsRepG1 - IsRepG0) * 4 ;lea eax, [p + IsRepG1*4 + ebp*4]
call RangeDecoderBitDecode
xchg esi, [rep1]
jnc @f
add eax, (IsRepG2 - IsRepG1) * 4 ;lea eax, [p + IsRepG2*4 + ebp*4]
call RangeDecoderBitDecode
xchg esi, [rep2]
jnc @f
xchg esi, [rep3]
@@:
.1101:
mov eax, p + RepLencoder*4
call LzmaLenDecode
push 8
jmp .rmu
.10:
xchg esi, [rep1]
xchg esi, [rep2]
mov [rep3], esi
mov eax, p + Lencoder*4
call LzmaLenDecode
push kNumLenToPosStates-1
pop edx
cmp edx, ecx
jb @f
mov edx, ecx
@@:
push ecx
push kNumPosSlotBits
pop ecx
mov eax, p+PosSlot*4
shl edx, cl
call RangeDecoderBitTreeDecode
mov esi, ecx
cmp ecx, kStartPosModelIndex
jb .l6
mov edx, ecx
xor eax, eax
shr ecx, 1
adc al, 2
dec ecx
shl eax, cl
mov esi, eax
sub eax, edx
lea eax, [p + (SpecPos - 1)*4 + eax*4]
cmp edx, kEndPosModelIndex
jb .l59
; call RangeDecoderDecodeDirectBits
;RangeDecoderDecodeDirectBits:
xor eax, eax
.l:
shr dword [range], 1
add eax, eax
mov edx, [code_]
sub edx, [range]
jb @f
mov [code_], edx
add al, 1 shl kNumAlignBits
@@:
call update_decoder
dec ecx
cmp ecx, kNumAlignBits
jnz .l
; ret
add esi, eax
mov eax, p+Align_*4
.l59:
; call RangeDecoderReverseBitTreeDecode_addesi
;_RangeDecoderReverseBitTreeDecode_addesi:
; in: eax->probs,ecx=numLevels
; out: esi+=length; destroys edx
push edi
xor edx, edx
inc edx
mov edi, edx
@@:
call RangeDecoderBitDecode_edx
jnc .591
add esi, edi
stc
.591:
adc edx, edx
add edi, edi
loop @b
pop edi
; ret
.l6:
pop ecx
not esi
push 7
.rmu:
cmp ebp, 7
pop ebp
jb @f
add ebp, 3
@@:
.repmovsb:
inc ecx
push esi
add esi, edi
rep movsb
lodsb
pop esi
jmp .stosb_main_loop
.main_loop_done:
include 'calltrick2.asm'
ret
_RangeDecoderBitDecode:
; in: eax->prob
; out: CF=bit
push edx
mov edx, [range]
shr edx, kNumBitModelTotalBits
imul edx, [eax]
cmp [code_], edx
jae .ae
mov [range], edx
mov edx, kBitModelTotal
sub edx, [eax]
shr edx, kNumMoveBits
add [eax], edx
.n:
pushfd
call update_decoder
popfd
pop edx
ret
.ae:
sub [range], edx
sub [code_], edx
mov edx, [eax]
shr edx, kNumMoveBits
sub [eax], edx
stc
jmp .n
update_decoder:
cmp byte [range+3], 0 ;cmp dword [range], kTopValue
jnz @f ;jae @f
shl dword [range], 8
shl dword [code_], 8
push eax
mov eax, [inptr_ldr]
mov al, [eax]
inc dword [inptr_ldr]
mov byte [code_], al
pop eax
@@: ret
_RangeDecoderBitDecode_edx:
push eax
lea eax, [eax+edx*4]
call RangeDecoderBitDecode
pop eax
ret
LzmaLenDecode:
; in: eax->prob, edx=posState
; out: ecx=len
; LenChoice==0
; add eax, LenChoice*4
if kLenNumMidBits <> kLenNumLowBits
error in optimization
end if
mov cl, kLenNumMidBits
call RangeDecoderBitDecode
jnc .0
add eax, (LenChoice2-LenChoice)*4
call RangeDecoderBitDecode
jc @f
if (kLenNumMidBits <> 3) | (LenMid-LenChoice2 > 0x7F + kLenNumMidBits)
shl edx, cl
add edx, LenMid-LenChoice2
else
lea edx, [ecx + edx*8 - kLenNumMidBits + LenMid-LenChoice2]
end if
push kLenNumLowSymbols
jmp RangeDecoderBitTreeDecode.1
@@:
mov edx, LenHigh-LenChoice2
mov cl, kLenNumHighBits
push kLenNumLowSymbols + kLenNumMidSymbols
jmp RangeDecoderBitTreeDecode.1
.0:
shl edx, cl
if LenLow = 2
inc edx
inc edx
else
add edx, LenLow
end if
RangeDecoderBitTreeDecode:
; in: eax+edx*4->probs,ecx=numLevels
; out: ecx=length; destroys edx
push 0
.1:
lea eax, [eax+edx*4]
xor edx, edx
inc edx
push ecx
@@:
call RangeDecoderBitDecode_edx
adc edx, edx
loop @b
pop ecx
btc edx, ecx
pop ecx
add ecx, edx
ret
loader_size = $ - loader_start