Subversion Repositories Kolibri OS

Rev

Rev 7118 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3122 leency 1
; Kolibri kernel packer
2
; (C) copyright diamond 2006, 2007
3
;
4
; Disassemled and corrected in 2010 specially for FASM
5
;            by Marat Zakiyanov aka Mario79, aka Mario
6
;
7
;	This program is free software; you can redistribute it and/or modify
8
;	it under the terms of the GNU General Public License as published by
9
;	the Free Software Foundation; either version 2 of the License, or
10
;	(at your option) any later version.
11
;
12
;	This program is distributed in the hope that it will be useful,
13
;	but WITHOUT ANY WARRANTY; without even the implied warranty of
14
;	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
;	GNU General Public License for more details.
16
 
17
; Uses LZMA compression library by Igor Pavlov
18
; (for more information on LZMA and 7-Zip visit http://www.7-zip.org)
19
; (plain-C packer is ported by diamond)
20
 
21
;---------------------------------------------------------------------
22
use32
23
	org	0
24
 
25
	db 'MENUET01'
26
	dd 1
27
	dd START
28
	dd IM_END
29
	dd I_END
30
	dd stacktop
31
	dd 0	;params
32
	dd 0	;cur_dir_path
33
;---------------------------------------------------------------------
7118 dunkaist 34
include '../../../../macros.inc'
3122 leency 35
; do not touch "purge mov"!!!
36
purge mov ; for the correct patch of loader
37
; do not touch "purge mov"!!!
38
;*********************************************************************
39
die_with_err:
40
	pop	esi
41
@@:
42
	lodsb
43
	test	al,al
44
	jz	@f
45
 
46
	mov	cl,al
47
	mcall	63,1
48
	jmp	@b
49
;*********************************************************************
50
@@:
51
	mcall	63,,13
52
	mcall	,,10
53
	mcall	-1
54
;*********************************************************************
55
START:
56
	mcall	70,fn70_read
57
	cmp	eax,6
58
	jz	read_ok
59
;--------------------------------------
60
read_err:
61
	call	die_with_err
62
	db	'KerPack: cannot load kernel.mnt',0
63
;*********************************************************************
64
read_ok:
65
	push	18
66
	call	lzma_set_dict_size
67
; find jump to 32-bit code
68
	mov	edi,infile - 1
69
;--------------------------------------
70
@@:
71
	inc	edi
72
	cmp	dword [edi],0xE88EE08E	 ; mov fs,ax/mov gs,ax
73
	jnz	@b
74
 
75
	cmp	dword [edi+4],0x00BCD08E	; mov ss,ax/mov esp,00xxxxxx
76
	jnz	@b
77
 
78
	add	edi,11
79
	mov	[inptr],edi
80
	sub	edi, infile
81
	mov	[indelta],edi
82
	lea	eax,[ebx+0x10000]
83
	mov	dword [loader_patch3+2],eax
84
	sub	ebx,edi
85
	mov	[insize],ebx
86
	call	preprocess_calltrick2
87
 
88
	mov	al,[cti]
89
	mov	[loader_patch5-1],al
90
	mov	eax,[ctn]
91
	mov	[loader_patch4+1],eax
92
	mov	eax,[inptr]
93
	add	eax, outfile - infile + loader_size - 5
94
	push	workmem
95
	push	[insize]
96
	push	eax
97
	push	[inptr]
98
	call	lzma_compress
99
 
100
	add	eax, loader_size-5
101
	mov	[loader_patch1+6],eax
102
	add	eax,[indelta]
103
	mov	[outsize],eax
104
	mov	eax,[indelta]
105
	mov	ecx,dword [eax + outfile + loader_size - 4]
106
	bswap	ecx
107
	mov	[loader_patch2+4],ecx
108
	add	eax, 0x10000
109
	mov	[loader_patch1+1],eax
110
	mov	esi,infile
111
	mov	edi,outfile
112
	mov	ecx,[indelta]
113
	rep	movsb
114
 
115
	mov	esi,loader_start
116
	mov	ecx,loader_size
117
	rep	movsb
118
 
119
	mcall	70,fn70_write
120
	test	eax,eax
121
	jz	@f
122
 
123
	call	die_with_err
124
	db	'KerPack: cannot save kernel.mnt',0
125
;*********************************************************************
126
@@:
127
	call	die_with_err
128
	db	'KerPack: all is OK',0
129
;*********************************************************************
130
preprocess_calltrick2:
131
; input preprocessing
132
	mov	edi,ct1
133
	xor	eax,eax
134
	push	edi
135
	mov	ecx,256/4
136
	rep	stosd
137
 
138
	pop	edi
139
	mov	ecx,ebx
140
	mov	esi,[inptr]
141
	mov	ebx,inbuftmp
142
	xchg	eax,edx
143
;--------------------------------------
144
input_pre2:
145
	lodsb
146
;--------------------------------------
147
@@:
148
	cmp	al,0Fh
149
	jnz	ip1
150
 
151
	dec	ecx
152
	jz	input_pre_done2
153
 
154
	lodsb
155
	cmp	al,80h
156
	jb	@b
157
 
158
	cmp	al,90h
159
	jb	@f
160
;--------------------------------------
161
ip1:
162
	sub	al,0E8h
163
	cmp	al,1
164
	ja	input_pre_cont2
165
;--------------------------------------
166
@@:
167
	cmp	ecx,5
168
	jb	input_pre_done2
169
 
170
	lodsd
171
	add	eax,esi
172
	sub	eax,[inptr]
173
	cmp	eax,[insize]
174
	jae	xxx2
175
 
176
	cmp	eax,1000000h
177
	jae	xxx2
178
 
179
	sub	ecx,4
180
	xchg	al,ah
181
	rol	eax,16
182
	xchg	al,ah
183
	mov	[esi-4],eax
184
	inc	edx
185
	mov	[ebx],esi
186
	add	ebx,4
187
	jmp	input_pre_cont2
188
;*********************************************************************
189
xxx2:
190
	sub	esi,4
191
	movzx	eax,byte [esi]
192
	mov	byte [eax+edi],1
193
;--------------------------------------
194
input_pre_cont2:
195
	loop	input_pre2
196
;--------------------------------------
197
input_pre_done2:
198
	mov	[ctn],edx
199
	xor	eax,eax
200
	mov	ecx,256
201
	repnz	scasb
202
	jnz	pack_calltrick_done
203
 
204
	not	cl
205
	mov	[cti],cl
206
;--------------------------------------
207
@@:
208
	cmp	ebx,inbuftmp
209
	jz	pack_calltrick_done
210
 
211
	sub	ebx,4
212
	mov	eax,[ebx]
213
	mov	[eax-4],cl
214
	jmp	@b
215
;*********************************************************************
216
pack_calltrick_done:
217
	ret
218
;*********************************************************************
219
;lzma_compress:
220
include 'lzma_compress.inc'
221
;---------------------------------------------------------------------
222
;lzma_set_dict_size:
223
include 'lzma_set_dict_size.inc'
224
;---------------------------------------------------------------------
225
;
226
include 'loader_lzma.inc'
227
;*********************************************************************
228
fn70_read:
229
	dd	0
230
	dd	0
231
	dd	0
232
	dd	200*1024
233
	dd	infile
9587 vitalkrilo 234
filename db	'/sys/kernel.mnt',0
3122 leency 235
 
236
fn70_write:
237
	dd	2
238
	dd	0
239
	dd	0
240
outsize dd	?
241
	dd	outfile
242
	db	0
243
	dd	filename
244
;---------------------------------------------------------------------
245
align 4
246
LiteralNextStates:
247
db 0,0,0,0,1,2,3,4,5,6,4,5
248
MatchNextStates:
249
db 7,7,7,7,7,7,7,10,10,10,10,10
250
RepNextStates:
251
db 8,8,8,8,8,8,8,11,11,11,11,11
252
ShortRepNextStates:
253
db 9,9,9,9,9,9,9,11,11,11,11,11
254
;---------------------------------------------------------------------
255
;*********************************************************************
256
IM_END:
257
;*********************************************************************
258
;params:
259
;	rb 256
260
;---------------------------------------------------------------------
261
;cur_dir_path:
262
;	rb 4096
263
;---------------------------------------------------------------------
264
align 4
265
	rb 4096
266
stacktop:
267
;---------------------------------------------------------------------
268
align	4
269
inptr		dd ?
270
indelta		dd ?
271
insize		dd ?
272
ct1		rb 256
273
ctn		dd ?
274
cti		db ?
275
 
276
infile		rb 200*1024
277
inbuftmp	rb 200*1024
278
outfile		rb 200*1024
279
workmem		rb 6A8000h
280
;---------------------------------------------------------------------
281
; Compress data area start
282
;---------------------------------------------------------------------
283
align 4
284
_lenEncoder:
285
	rd 8451
286
;-----------------------------------------------------
287
_prices:
288
	rd 4384
289
	rd 17
290
;-----------------------------------------------------
291
_finished:		rb 1
292
_writeEndMark:		rb 1
293
_longestMatchWasFound:	rb 1
294
_previousByte:		rb 1
295
_longestMatchLength:	rd 1
296
;-----------------------------------------------------
297
g_FastPos:
298
	rb 1024
299
;-----------------------------------------------------
300
_posSlotPrices:
301
	rd 256
302
;-----------------------------------------------------
303
_isRep0Long:
304
	rd 192
305
;-----------------------------------------------------
306
distances:
307
	rd 274
308
;-----------------------------------------------------
309
_optimumCurrentIndex:	rd 1
310
_additionalOffset:	rd 1
311
;-----------------------------------------------------
312
_isRepG1:
313
	rd 12
314
;-----------------------------------------------------
315
_isMatch:
316
	rd 192
317
;-----------------------------------------------------
318
_alignPriceCount:	rd 1
319
_numLiteralContextBits:	rd 1
320
;-----------------------------------------------------
321
_literalEncoder:
322
	rd 114
323
;-----------------------------------------------------
324
nowPos64:
325
	rd 2
326
;-----------------------------------------------------
327
_distancesPrices:
328
	rd 512
329
;-----------------------------------------------------
330
_repDistances:
331
	rd 4
332
;-----------------------------------------------------
333
_posSlotEncoder:
334
	rd 1028
335
;-----------------------------------------------------
336
lastPosSlotFillingPos:
337
	rd 2
338
;-----------------------------------------------------
339
_numFastBytes:	rd 1
340
_posStateMask:	rd 1
341
;-----------------------------------------------------
342
_isRepG0:
343
	rd 12
344
;-----------------------------------------------------
345
_repMatchLenEncoder:
346
	rd 8451
347
	rd 4384
348
	rd 17
349
;-----------------------------------------------------
350
_isRepG2:
351
	rd 12
352
;-----------------------------------------------------
353
_dictionarySize:		rd 1
354
_numLiteralPosStateBits:	rd 1
355
_distTableSize:			rd 1
356
_optimumEndIndex:		rd 1
357
;-----------------------------------------------------
358
;static CState state
359
state.State:		rb 1
360
state.Prev1IsChar:	rb 1
361
state.Prev2:		rb 2
362
state.PosPrev2:		rd 1
363
state.BackPrev2:	rd 1
364
state.Price:		rd 1
365
state.PosPrev:		rd 1
366
state.BackPrev:		rd 1
367
state.Backs:
368
	rd 4
369
;----------------------------------------------------
370
	rd 40950
371
;-----------------------------------------------------
372
_alignPrices:
373
	rd 16
374
;-----------------------------------------------------
375
_isRep:
376
	rd 12
377
;-----------------------------------------------------
378
_posAlignEncoder:
379
	rd 256
380
;-----------------------------------------------------
381
i_01:	rd 1
382
;-----------------------------------------------------
383
_state:			rb 1
384
_cache:			rb 1
385
_state.Prev2:		rb 2
386
_posEncoders:		rd 1
387
_numPrevBits:		rd 1
388
_numPosBits:		rd 1
389
_posMask:		rd 1
390
_posStateBits:		rd 1
391
_range:			rd 1
392
_cacheSize:		rd 1
393
_cyclicBufferSize:	rd 1
394
;-----------------------------------------------------
395
low:
396
	rd 2
397
;-----------------------------------------------------
398
Models:
399
	rd 512
400
;-----------------------------------------------------
401
_matchMaxLen:	rd 1
402
pack_pos:	rd 1
403
_cutValue:	rd 1
404
_hash:		rd 1
405
;-----------------------------------------------------
406
crc_table:
407
	rd 256
408
;-----------------------------------------------------
409
_buffer:	rd 1
410
_pos:		rd 1
411
_streamPos:	rd 1
412
pack_length:	rd 1
413
;---------------------------------------------------------------------
414
; Compress data area end
415
;---------------------------------------------------------------------
416
I_END:
7118 dunkaist 417
;---------------------------------------------------------------------