.286
.model tiny
.code
org 100h
start:
push cs
pop  ds
	cmp 	byte ptr cs:[80h], 0
	jnz	norm_work
	mov 	dx, offset usage
	mov 	ah, 09
	int	21h
	jmp	just_quit
norm_work:
	mov	di, offset CRKname
	mov	si, 82h
loop1:
	cmp	byte ptr ds:[si], 0dh
	jz	got_fname
	cmp	byte ptr ds:[si], 20h
	jz	got_fname
	movsb
	cmp	di, offset buffer
	jb	loop1
	
got_fname:
	mov	byte ptr ds:[di], 0		; Set ASCIIZ string
	mov	ax, 3d00h			;
	mov	dx, offset CRKname		; Opening CRK file
	int	21h				;

	jnc	$+5				; IF OK!
	jmp	error_file			; Its about long jump

	mov	bx, ax
	mov	hCRKfile, bx

	push	100h				; Reading Comment Line
	push	offset buffer			;
	push	bx				;
	call	readln				;

	mov 	dx, offset patching		; Writing that stuff
	mov 	ah, 09
	int	21h

	mov	cx, -1
	mov	di, offset Buffer
	xor	ax, ax
	repnz	scasb
	dec	di
	mov	byte ptr ds:[di], '$'
	mov 	dx, offset buffer
	mov 	ah, 09
	int	21h

	push	100h
	push	offset buffer
	push	hCRKfile
	call	readln

	cmp	word ptr buffer, 0a0dh
	jz	$+8
	mov	dx, offset ERRORInCrk
	jmp	bad

	push	100h
	push	offset buffer
	push	hCRKfile
	call	readln

	mov 	dx, offset tofp		; Writing type of patch stuff
	mov 	ah, 09
	int	21h

	mov	cx, -1
	mov	di, offset Buffer
	xor	ax, ax
	repnz	scasb
	dec	di
	mov	byte ptr ds:[di], '$'
	mov 	dx, offset buffer
	mov 	ah, 09
	int	21h

	push	15
	push	offset EXEname
	push	hCRKfile
	call	readln

	mov	cx, -1			; Convert to ASCIIz filename
	mov	di, offset EXEname
	mov	al, 0Dh
	repnz	scasb
	dec	di
	mov	byte ptr ds:[di], 0

	mov	ax, 3D02h
        mov 	dx, offset EXEname
        int	21h

	jnc	$+8				; IF OK!
	mov	dx, offset MustRun 
	jmp	bad				; Its about long jump
	mov	hEXEfile, ax

patch_file:
	push	100h
	push	offset buffer
	push	hCRKfile
	call	readln

	cmp	byte ptr buffer, 0
	jnz	$+5
	jmp	Patched_ok

	cmp	byte ptr buffer[8], ':'
	jz	$+8				; IF OK!
	mov	dx, offset MustRun 
	jmp	bad				; Its about long jump
	
	mov	di, offset buffer
	call	atob
	mov	ch, al
	call	atob
	mov 	cl, al
	call	atob
	mov	dh, al
	call	atob
	mov 	dl, al
	mov	bx, hEXEfile
	mov	ax, 4200h
	int	21h

	jnc	$+7				; IF OK!
	mov	dx, offset BadEXE 
	jmp	bad				; Its about long jump

	mov 	ah, 3fh
	mov	bx, hEXEfile
	mov	cx, 1
        mov	dx, offset EXEchar
        int	21h

        cmp	ax, 1
        jz	$+7
	mov	dx, offset BADexe
	jmp	bad				; Its about long jump

	mov	di, offset buffer[10]
	call	atob
	cmp	al, EXEchar
        jz	$+7
	mov	dx, offset BadEXE 
	jmp	bad				; Its about long jump

	mov	ax, 4201h
	mov	bx, hEXEfile
	xor	cx, cx
	mov	dx, -1
        int	21h

	mov	di, offset buffer[13]
	call	atob
	mov	EXEchar, al

	mov	ah, 40h
	mov	bx, hEXEfile
	mov	cx, 1
	mov 	dx, offset EXEchar
	int	21h

	cmp	ax, 01
	jnz	@@3
	jmp	patch_file
@@3:
	mov 	dx, offset BadEXE
	jmp	bad

Patched_ok:
	mov 	dx, offset PatOK
;--------- ERRORS ------------------
bad:
	mov	ah, 09
	int	21h
	jmp	good_quit
error_file:
	mov	cx, -1
	mov	di, offset CRKname
	xor	ax, ax
	repnz	scasb
	dec	di
	mov	byte ptr ds:[di], '$'
	mov 	dx, offset notfound
	mov 	ah, 09
	int	21h
	jmp	just_quit
good_quit:
	mov	bx, hEXEfile
	test	bx, bx
	jz	@@1
	mov	ah, 3eh
	int	21h
	jnc @@1
	mov 	dx, offset BadEXE
	mov 	ah, 09
	int	21h		
@@1:
	mov	bx, hCRKfile
	test	bx, bx
	jz	just_quit
	mov	ah, 3eh
	int	21h
just_quit:
	mov	ah, 4ch
	int	21h
;---------------------------------------------------------
; 	Stupid Procedures
; 
;---------------------------------------------------------
atob proc near	; di - Src 4 bytes string, out: ax - value
  push cx
  push dx

  xor  ax, ax
  xor  dx, dx
  mov  cx, 2
atow_calcloop:
  mov  dl, ds:[di]
  inc  di
  sub  dl, 30h
  cmp  dl, 9
  jna  atow_mul
  sub  dl, 7
atow_mul:
  shl  ax, 4
  add  ax, dx
  loop atow_calcloop

  pop  dx
  pop  cx
  ret
endp

readln proc near
readln_hFILE 	equ word ptr [bp+04]
readln_Buffer	equ word ptr [bp+06]
readln_BufLen	equ word ptr [bp+08]
  push	bp
  mov 	bp, sp
  pusha

  mov	di, readln_Buffer		; Destination
  mov	si, offset readln_chbuf
  mov	dx, offset readln_chbuf	; ReadBuffer
  mov	cx, 1			; Read only 1 character
  mov	bx, readln_hFile
  mov	ax, readln_buffer
  add	ax, readln_BufLen
  push 	ax
  pop	bp

readln_read_loop:
  mov	ah, 3fh
  int	21h
  cmp	ax, 1
  jnz	good_done
  movsb
  dec	si
  mov 	ax, bp
  dec	ax
  cmp	di, ax
  jae	good_done
  cmp	byte ptr readln_chbuf, 0Ah
  jnz	readln_read_loop

good_done:
  mov	byte ptr ds:[di], 0
  popa
  pop	bp
  ret	6
readln_chbuf   db ?
endp  

;---------------------  Parametrs ------------------------
usage   db 'Usage: do_crk.com <crack.crk>',13,10,'$'
MustRun db 'Patch file not found (must run from progs dir)',13,10,'$'
BadEXE  db 'Bad exe file (probably another version?)',13,10,'$'
ErrorInCRK db 'Error IN CRK file',13,10,'$'
PatOK	db 'File patched OK!',13,10,'$'
patching db 'Patching      - $'
tofp	 db 'Type of Patch - $'
notfound db 'File not found: '
hCRKfile dw 0
hEXEfile dw 0
EXEchar	db ?
CRKname	db 15 dup (?)
EXEname	db 15 dup (?)
buffer	db 100h dup (?)
end start