; Part of m_exe.inc
;
; This resident int21 shield, the only anti-TEU/BlastWave/UPC/Intruder/ENTPACK
; trick in Mess 1.31, is only used if you invoke Mess with the /T switch.
; However, it seems to cause incompatibility under NT. Why?
;

            ;  resident int21 shield
              mov     al,1
              org $-1
      mustteu db 1
              .IF al == 1
              jmp nexttrick3
              .ENDIF
              push es      ; adjust MCB
               mov ax,ds
               dec ax
               mov es,ax   ; this is my MCB segment
               mov ax, word ptr es:[3] ; number of paragraphs (exclusive MCB)
                                       ; which belongs to this MCB
              pop es
              sub ax, 33 ; resize to 33*16 bytes less (one extra for mcb)
              mov bx, ax
              mov ah, 4A
              int 21     ; resizing is necessary, for example for files
                         ; with 0FFFFh in maxmem-field in exe header..
                         ; in this case no memory is free otherwise

              mov ah,48
              mov bx,32    ; hook 32h*16 bytes = 800 bytes
              int 21
              jnc okseg21
              mov ax,4C01  ; terminate if error (for example, not enough memory)
              int 21
     okseg21:
              mov word ptr cs:[eoldint21+bp+2], ax
              push es
               push ds
                push cs
                pop ds
                mov si, offset enewint21
                 add si,bp
                mov cx, offset enewint21_e - offset enewint21
                xor di,di
                push ax
                 mov es,di
                 mov eax, dword ptr cs:[eoldint21+bp]
                 xchg eax, es:[21*4]
                 mov dword ptr cs:[eoldint21+bp], eax       ; hook int 21
                pop ax
                mov es,ax
                rep movsb
               pop ds
              pop es

              jmp nexttrick3

            ;  new int 21
   enewint21:
              push bp
               xor bp,bp
               sub bp,offset enewint21
               mov cs:[eoldbp2+bp],bp
               push bx
                mov bx,sp
                add bx,4  ; two pushes too much
                mov cs:[eoldsp+bp],bx
                xor dword ptr ss:[bx],12345678
       teuvar1a equ $-4
                xor dword ptr ss:[bx+4],56789ABC ; encrypt stack for teu
       teuvar2a equ $-4
               pop bx
               push fs
                push eax
                 xor ax,ax
                 mov fs,ax
                 mov eax, dword ptr cs:[eoldint21+bp]
                 xchg eax, fs:[21*4]
                 mov dword ptr cs:[eoldint21+bp], eax       ; unhook int 21
                pop eax
               pop fs
              pop bp
              int 21        ; CALL OLD INT 21 FROM ANOTHER LOCATION !!!!!!
              pushf
               push bp
                mov bp,0000
                org $-2
       eoldbp2  dw 0
                push bx
                 mov bx,cs:[eoldsp+bp]
                 xor dword ptr ss:[bx],12345678
        teuvar1b equ $-4
                 xor dword ptr ss:[bx+4],56789ABC  ; decrypt stack after teu
        teuvar2b equ $-4
                pop bx
                push fs
                 push eax
                  xor ax,ax
                  mov fs,ax
                  mov eax, dword ptr cs:[eoldint21+bp]
                  xchg eax, fs:[21*4]
                  mov dword ptr cs:[eoldint21+bp],eax       ; hook int 21 again
                 pop eax
                pop fs
               pop bp
              popf
              iret
    eoldint21 dw 0,0
       eoldsp dw 0
  enewint21_e equ $

            ;  timer/exe
            ; this does not belong to the teu trick, but is included for
            ; historical reasons, compare to scram02.asm :)
            
  nexttrick3:
              sti             ; otherwise crash!
              push ds
               lds bx, dword ptr cs:[etimer+bp]
               mov ax, ds:[bx]
      ethru4:  cmp ax, ds:[bx]

               jmp skipetimer
       etimer  dw 006c,0040    ; ofs:seg for LDS
   skipetimer:
               jz ethru4       ; timer: anti cup386 3,0, 3.2
              pop ds
              cli
