;
; ‘âàãªâãà  ¤«ï ä㭪樨 70
;

struc sinfo
{
	.subfnc_name     dd 0
	.pos_in_file     dd 0
	.reserved        dd 0
	.bytes_to_read   dd 0
	.pbuffer	 dd 0
	.null            db 0
	.pname           dd 0
}




;
;  â ¡«¨æë §­ ç¥­¨© 
;
;  +----+-------------+-----------------+
;  | in | Variable    | Variable        |
;  | de | name        | string          |
;  | x  |             |                 |
;  |    |             |                 |
;  +----+-------------+-----------------+
;  |    |             |                 |
;  |    |             |                 |
;  | 1  | UIN         | 'XXXXX..XX',0   |
;  |    |             |                 |
;  +----+-------------+-----------------+
;  |                  |                 |
;
;  § £à㧪  int ¯®ª  ­¥ ॠ«¨§®¢ ­ 
;
;  +----+-------------+-----------------+
;  | in | Variable    | Variable        |
;  | de | name        | int             |
;  | x  |             |                 |
;  |    |             |                 |
;  +----+-------------+-----------------+
;  |    |             |                 |
;  |    |             |                 |
;  | 1  |    BUFFSIZE |  XXXXXXXX       |
;  |    |             |                 |
;  +----+-------------+-----------------+
;  |                  |                 |
;
;


TABLE_SIZE	equ 16
VNAME_LEN	equ 8
VAR_LEN         equ 16

;
; Ž¯¨á ­¨¥ â ¡«¨æë §­ ç¥­¨©

virtual at 0
  vartable:
	.uin db VAR_LEN dup ?
	.pass db VAR_LEN dup ?
	.icqip db VAR_LEN dup ?
 	


end virtual


;
; Š®¤ë ®è¨¡®ª ä ©«®¢®© á¨á⥬ë
;

FIO_SUCCESS		equ 0
FIO_UNSUPPORTED		equ 2
FIO_UNKNOWNFS		equ 3
FIO_FILENOTFOUND	equ 5
FIO_EOF			equ 6
FIO_BADPOINTER		equ 7
FIO_DISKFULL		equ 8
FIO_FATDAMAGED		equ 9
FIO_DENIED		equ 10
FIO_ERRORDEVICE		equ 11


IOBUFF_SIZE	equ 128




;
; ¬ ªà®á ¤«ï ¯®¨áª  í«¥¬¥­â  ¢ áâப¥ 
; ¢®§¢à é ¥â ¢ eax ­®¬¥à í«¥¬¥­â  ¨«¨ -1 ¥á«¨ ­¥ ­ ©¤¥­
macro findchar string, len, char
{
   local ..fc_endstr, ..fc_end

   push ebx
   push ecx
   push edi


   mov edi, string
   mov ecx, len
   mov ebx, ecx
   cld
   mov al, char
   repne scasb
   jcxz ..fc_endstr

   sub ebx, ecx  ; ®¬¥à =
   mov eax, ebx
   jmp ..fc_end

 ..fc_endstr:
   mov eax, -1


 ..fc_end:
   pop edi
   pop ecx
   pop ebx

}

;
; Œ ªà®á ¤«ï ¯®¨áª  í«¥¬¥­â  áâப¨, ®â«¨ç î饣®áï ®â 
; § ¤ ­­®£®

macro findother string, len, char
{
   local ..fc_endstr, ..fc_end

   push ebx
   push ecx
   push edi


   mov edi, string
   mov ecx, len
   mov ebx, ecx
   cld
   mov al, char
   repe scasb    ; …᫨ ᨬ¢®« ­¥ char - ¢ë室¨¬
   jcxz ..fc_endstr   ; áâப  ¨§ char 

   sub ebx, ecx   ; ¢ ebx - ­®¬¥à í«¥¬¥­â  ®â«¨ç­®£® ®â char
   mov eax, ebx
   jmp ..fc_end

 ..fc_endstr:
   mov eax, -1


 ..fc_end:
   pop edi
   pop ecx
   pop ebx
}

;
; Œ ªà®á ¤«ï ª®¯¨à®¢ ­¨ï áâப
;
macro mstrcpy from, to, leng
{

   push ecx
   push esi
   push edi

   mov ecx, leng
   mov esi, from
   mov edi, to
   cld
   rep movsb

   pop edi
   pop esi
   pop ecx

}


;
; ˆ­¨æ¨ «¨§¨àã¥â â ¡«¨æë 
;

;  inittables:
;
;
;   mstrcpy name1, nvtable, VNAME_LEN
;   mstrcpy name2, (nvtable + NAME_LEN), VNAME_LEN
;   mstrcpy name3, (nvtable + NAME_LEN * 2), VNAME_LEN
;
;
;
;  ret   


;                 
; § ¯®«­ï¥â â ¡«¨æë §­ ç¥­¨ï¬¨
; IN eax - ASCIIZ ¨¬ï ä ©« 
; OUT eax - १ã«ìâ â ç⥭¨ï
; ¥á«¨ १ã«ìâ â -1, ä®à¬ â ä ©«  ­¥¯à ¢¨«ì­ë©
;
  parseconf:
   push edi
   push esi
   ;push eax
   push ebx
   push ecx
   push edx

   mov [strnum], dword 0
   
   ;
   ; à®ç¨â âì ¯®áâà®ç­® ª®­ä¨£
   ; ¥á«¨ áâப  ­ ç¨­ ¥âáï á ;, #  - ª®¬¬¥­â à¨©
   ; ”®à¬ â UIN="1234567890"
   ; PASS="******" ¨ â.¤.

   ;
   ; ‘¡à®á ᬥ饭¨ï
   mov [shift], dword 0

   mov esi, eax
   
 pc_still:

   mov edx, esi
   mov ecx, IOBUFF_SIZE
   mov ebx, iobuff

   call getstr

   inc [strnum]

   push eax

   ;
   ;à®¢¥àª  ¯®«ã祭­®© áâப¨
   ;
   movzx eax, byte [iobuff]

   test eax, eax
   jz pc_next

   cmp al, '#'
   jz pc_next

   cmp al, ';'
   jz pc_next

   ;
   ;  ©â¨ ¨¬ï ¯¥à¥¬¥­­®© 
   ;
   findother iobuff, ebx, ' '
   cmp eax, -1
   jz pc_next
    
   mov [stnpos], eax  ;­ ç «® ¨¬¥­¨

   ;
   ; ­ ©â¨ =
   ;
   mov ecx, ebx   ; ˆáª âì ®â ­ ©¤¥­­®£® ᨬ¢®« 
   sub ecx, eax   ;

   mov edi, iobuff
   add edi, eax

   findchar edi, ecx, '='

   cmp eax, -1
   jz pc_badformat

   mov edi, [stnpos]
   add eax, edi   ; ¢ eax - ᬥ饭¨¥ ®â ­ ç «  áâப¨
   mov [eqpos], eax

   mov ecx, ebx
   sub ecx, eax

   ;
   ; ¯à®¢¥à¨âì "
   ;
   mov dl, [iobuff + eax]
   cmp dl, '"'
   jnz pc_badformat
   ;
   ; ­ ©â¨ § ªà뢠îéãî "
   ;
   mov edi, iobuff
   add edi, eax

   inc edi

   findchar edi, ecx, '"'

   cmp eax, -1
   jz pc_badformat

   inc eax

   mov edx, [eqpos]
   add eax, edx
   mov [edvpos], eax

   ;
   ; “áâ ­®¢¨âì §­ ç¥­¨¥
   ;
   ; „®¡ ¢¨âì § ¢¥àè î騥 0

   mov eax, [stnpos]
   dec eax


   mov ebx, [eqpos]
   mov ecx, ebx
   dec ecx            ; ª®«¨ç¥á⢮ ᨬ¢®«®¢ ¤® =
   inc ebx            ; à®¯ãáâ¨âì "

   mov [iobuff + ecx], byte 0

   mov edx, [edvpos]
   dec edx

   mov [iobuff + edx], byte 0

   lea eax, [iobuff + eax]
   lea ebx, [iobuff + ebx]
   call setavar

   jmp pc_next




 pc_badformat:
   pop eax

   mov ebx, [strnum]
   jmp pc_err


 pc_next:
   pop eax

   cmp eax, FIO_EOF
   jz pc_eof
   cmp eax, FIO_SUCCESS
   jnz pc_err

 jmp pc_still



 pc_eof:
 pc_err:
   pop edx
   pop ecx
   pop ebx
   ;pop eax
   pop esi
   pop edi
  ret

; ®¨áª ¢ â ¡«¨æ¥ ¯¥à¥¬¥­­®© ¨ ãáâ ­®¢ª  ¥ñ §­ ç¥­¨ï
; IN eax - ­ §¢ ­¨¥ ¯¥à¥¬¥­­®© 㪠§ â¥«ì ­  ASCIIZ
; IN ebx - §­ ç¥­¨¥ ¯¥à¥¬¥­­®© 㪠§ â¥«ì ­  ASCIIZ
; OUT eax -१ã«ìâ â 0 = OK, -1 = ­¥â ¢ â ¡«¨æ¥ ¯¥à¥¬¥­­ëå
; OUT § ¯®«­ï¥â £«®¡ «ì­ãî â ¡«¨æã
  setavar:
   ;push ebx
   push ecx
   push edx
   push esi
   push edi
   push ebx

   ;
   ; Ž¯à¥¤¥«¨âì ¤«¨­ã áâப¨ - ­ §¢ ­¨¥ ¯¥à¥¬¥­­®©
   ;
   mov edi, eax
   push eax

   mov ecx, VNAME_LEN

   xor eax, eax ;ˆé¥¬ \0
   cld
   repne scasb

   mov eax, VNAME_LEN
   sub eax, ecx       ; ‚ ecx - ®áâ â®ª ¤® ¬ ªá¨¬ «ì­®£® à §¬¥à  áâப¨
   mov ebx, eax


   pop eax            
   ;
   ; ˆáª âì ¢ â ¡«¨æ¥ ¯®¤å®¤ï饥 ¨¬ï
   ;
   xor edx, edx         ;index

 sv_next:
   mov ecx, ebx
   push eax
   mov esi, eax
   mov edi, nvtable
   mov eax, edx
   imul eax, VNAME_LEN   ;offset
   add edi, eax
   pop eax
   cld
  
   repe cmpsb
   jz sv_match

 sv_inc:
   inc edx
   cmp edx, TABLE_SIZE
   jae  sv_fail
   jmp sv_next

 sv_match:
   cmp ebx, VNAME_LEN    ;‚ ebx - ¤«¨­  ¨á室­®© áâà®çª¨
   jz sv_match2

   push eax
   mov edi, nvtable
   mov eax, edx
   imul eax, VNAME_LEN   ;offset
   add edi, eax
   pop eax

   cmp [edi + ebx], byte 0 ; …᫨ ¨¬ï ¢ â ¡«¨æ¥ ª®à®ç¥ ¬ ªá¨¬ «ì­®£®,
   jnz sv_inc                  ; § ª ­ç¨¢ ¥âáï 0

 sv_match2:
   pop edi                    ; 
   push edi
   ;
   ; Ž¯à¥¤¥«ïâì ¤«¨­ã áâப¨ - ¯¥à¥¬¥­­ ï
   ;
   xor eax, eax
   mov ecx, VAR_LEN
   cld
   repne scasb

   mov eax, VAR_LEN
   sub eax, ecx
   mov ecx, eax
   mov ebx, eax

   ;
   ; Š®¯¨à®¢ âì ¯¥à¥¬¥­­ãî ¢ â ¡«¨æã
   ;

   pop esi
   push esi

   mov eax, VAR_LEN
   imul eax, edx
   mov edi, vtable
   add edi, eax
   cld
   rep movsb

   ;
   ; …᫨ áâப  ª®à®ç¥ ¯®«ï ¢ â ¡«¨æ¥, § ¯¨á âì ¢ ª®­¥æ 0
   ;
   cmp ebx, VAR_LEN
   jz sv_end
   mov [edi + ebx], byte 0



 sv_end:
   xor eax, eax
   jmp sv_fin

 sv_fail:
   mov eax, -1

 sv_fin:

   pop ebx
   pop edi
   pop esi
   pop edx
   pop ecx
   ;pop ebx
  ret





;
; —⥭¨¥ ASCIIZ áâப¨ ¨§ ä ©« 
;  IN ebx - 㪠§ â¥«ì ­  ¡ãä¥à
;  ecx - à §¬¥à ¡ãä¥à 
;  edx - 㪠§ â¥«ì ­  áâப㠨¬ï ä ©« 
;  OUT ebx - ¤«¨­  áâப¨
;  eax - १ã«ìâ â ç⥭¨ï

  getstr:
   ;push eax
   ;push ebx
   push ecx
   push edx
   push esi

   ;xor edx, edx
   xor esi, esi

 gs_read:
   ;
   ;  ‡ ¯®«­ï¥¬ áâàãªâãàã
   ;
   mov [finfo.subfnc_name], 0
   mov eax, [shift]
   mov [finfo.pos_in_file], eax
   mov [finfo.bytes_to_read], ecx
   mov [finfo.pbuffer], ebx
   mov [finfo.pname], edx

   push ebx

   ;
   ; —¨â¥¬
   ;
   ;mov eax, 70
   ;mov ebx, finfo
   ;int 40h
   mcall 70, finfo

   mov ecx, ebx    ; ‚ ebx ª®«¨ç¥á⢮ ¯à®ç⥭­ëå ¡ ©â

   pop ebx

   ;
   ; à®¢¥à¨âì १ã«ìâ â ç⥭¨ï - ¥á«¨ ­¥ EOF ¨ 0, 
   ; ¢ë室¨¬
   cmp eax, FIO_EOF
   jz gs_loop
   cmp eax, 0
   jz gs_loop

   jmp gs_ok


   ;
   ; Ž¡à ¡®âª  ¯®«ã祭­®£® ¡«®ª 
   ;
 gs_loop:
   mov dl, [ebx + esi]
   cmp dl, 0Ah          ;cr
   jz gs_cr
   inc esi
   cmp esi, ecx
   jnb gs_err 
   jmp gs_loop

 gs_err:
   ;
   ; ‚ ¡ãä¥à¥ ­¥â ᨬ¢®«  ¯¥à¥­®á  áâப¨, â.¥. áâப  ᫨誮¬ ¤«¨­­ ï
   ; Žâ¡à á뢠¥¬ ¢á¥ ¤® ¡«¨¦ ©è¥£® ᨬ¢®«  ¯¥à¥­®á  áâப¨
   ; ¥á«¨ ª®­¥æ ä ©«   - ¢ë室¨¬
   cmp eax, FIO_EOF
   jz gs_endf
   add [shift], ecx
   jmp gs_read


 gs_endf:
   xor ebx, ebx
   jmp gs_ok

 gs_cr:
   ;
   ; ‘¡à®á¨âì १ã«ìâ â ç⥭¨ï
   ;
   xor eax, eax

   mov dl, [ebx + esi - 1]
   cmp dl, 0Dh ;le
   jz gs_le

   mov [ebx + esi], byte 0
   mov ebx, esi


   inc esi
   add [shift], esi

   jmp gs_ok

 gs_le:
   mov [ebx + esi - 1], byte 0
   mov [ebx + esi], byte 0
   lea ebx, [esi - 1]

   inc esi
   add [shift], esi

 gs_ok:


   pop esi
   pop edx
   pop ecx
   ;pop ebx
   ;pop eax
  ret


;
; ”ã­ªæ¨ï ¤«ï ¢ë¢®¤  § £à㦥­­®© ¨­äë 
;

 showcfg:
   push eax
   push ebx
   push ecx
   push edx
   push edi


   xor edx, edx ; áç¥â稪

  sc_loop:

   cmp edx, TABLE_SIZE
   jnb sc_end

   ;
   ; ‘ª®¯¨à®¢ âì ¢ ¡ãä¥à ¨¬ï ¨ §­ ç¥­¨¥ ¯¥à¥¬¥­­®©
   ;
   mov eax, VNAME_LEN
   imul eax, edx
   lea eax, [nvtable + eax]

   mov cl, [eax]
   cmp cl, byte 0
   jz sc_next

   push eax
   call strlen

   mov ecx, eax
   pop eax

   mov ebx, cfgbuff

   call strcpy

   mov [cfgbuff + ecx], ':'

   lea ebx, [cfgbuff + ecx + 1]

   mov eax, VAR_LEN
   imul eax, edx
   lea eax, [vtable + eax]

   push eax
   call strlen

   mov ecx, eax
   pop eax

   call strcpy

   mov [ebx + ecx], byte 0

   mov eax, cfgbuff
   xor ebx, ebx
   call writemsg

  sc_next:

   inc edx

  jmp sc_loop



  sc_end:
   pop edi
   pop edx
   pop ecx
   pop ebx
   pop eax

 ret