home *** CD-ROM | disk | FTP | other *** search
/ The Equalizer BBS / equalizer-bbs-collection_2004.zip / equalizer-bbs-collection / DEMOSCENE-STUFF / BKISSSRC.ZIP / JLIBIO.ASM < prev    next >
Assembly Source File  |  1994-02-14  |  30KB  |  620 lines

  1. ;this is for the C-version of the library interface
  2.  
  3. LOCALS
  4. JUMPS
  5. .286
  6.  
  7. IFDEF s
  8.   DISPLAY "JLBIO Small Model"
  9.   .model small
  10. ELSE
  11.   IFDEF c
  12.     DISPLAY "JLIBIO Compact Model"
  13.     .model compact
  14.   ELSE
  15.      IFDEF l
  16.        DISPLAY "JLIBIO Large Model"
  17.       .model large
  18.      ELSE
  19.        DISPLAY "WARNING: Model was not defined at the command line."
  20.        DISPLAY "         Include in TASM commandline either:  /ds, /dc, or /dl"
  21.        DISPLAY "         Using default small model. ie:  /ds"
  22.        .model small
  23.       ENDIF
  24.   ENDIF
  25. ENDIF
  26.  
  27. public _jlib_open_library
  28. public _jlib_close_library
  29. public _jlib_open_file
  30. public _jlib_close_file
  31. public _jlib_ftell
  32. public _jlib_filelength
  33. public _jlib_fseek
  34. public _jlib_read_file
  35.  
  36. MAJORVER = 1         ;\ file format version 1.3
  37. MINORVER = 3         ;/
  38. MAXFILES = 50        ;maximum number of files allowed in library
  39. SLIDINGREQUEST = 0   ;enable this to allow sliding requests (see read_file)
  40.  
  41.  
  42. ;structures
  43. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  44. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  45. LibFile        STRUC
  46.                Fileattr    DB 0
  47.                Filetime    DW 0
  48.                Filedate    DW 0
  49.                Filesize    DD 0
  50.                Filename    DB 13 DUP (0)
  51.                Fileoffset  DD 0
  52.                CurPos      DD 0              ;\ not stored in file
  53.                Opened      DB 0              ;/
  54. LibFile        ENDS
  55.  
  56.  
  57. ;macros
  58. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  59. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  60. MAKEUPPERCASE  macro LETTER
  61.                LOCAL @@NotLower
  62.                cmp LETTER,'a'
  63.                jb @@NotLower
  64.                cmp LETTER,'z'
  65.                ja @@NotLower
  66.                sub LETTER,'a'-'A'
  67. @@NotLower:    endm
  68.  
  69.  
  70. .data
  71. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  72. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  73. LibHandle   DW -1                       ;handle for library
  74. NumFiles    DW 0                        ;number of files in library
  75. FileStats   LibFile MAXFILES DUP (<>)   ;file info structures of each file
  76. Wildcard    DB 13 DUP (0)               ;\   last defined wildcard
  77. MasterAttr  DW 0                        ; \  original search attribute
  78. HandleNext  DB 0                        ;  > if next FindNext should be done
  79. LastMatch   DW 0                        ; /  number of last match
  80. MasterWild  DB 80 DUP (0)               ;/   original search wildcard
  81. TempBuffer  DB 13 DUP (0)               ;temporary I/O buffer
  82. HeaderOffset DD 0                       ;offset to start of JLIB file
  83.  
  84.  
  85. .code
  86. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  87. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  88. ;capitalize filename, make sure there's a period, only allow 8 characters
  89. ;for the base name and 3 characters for the extension, doesn't allow
  90. ;characters after an asterick
  91. ;
  92. ;           DS:SI ==> ASCIIZ filename to parse
  93. ;           ES:DI ==> buffer to store parsed filename
  94. ;
  95. ;returns:   ZF set if no filename passed (null name)
  96. ;
  97. PROC        FixFileName
  98.             push si di ax bx dx              ;save what we'll modify
  99.             mov dx,si                        ;DS:DX ==> first byte of filename
  100.             cmp byte ptr ds:[si],0           ;\ quit with ZF set, if there's
  101.             jz @@Quit                        ;/ nothing given to us
  102. @@FindEnd:  inc si                           ;\
  103.             cmp byte ptr ds:[si],0           ; > seek to the end of the name
  104.             jnz @@FindEnd                    ;/
  105.             std                              ;\
  106. @@FindStart:lodsb                            ; \
  107.             cmp si,dx                        ;  \
  108.             jz @@GotStart                    ;   | seek back to
  109.             cmp al,'\'                       ;   | the start of
  110.             jz @@IncOne                      ;   | the base name
  111.             cmp al,':'                       ;  /
  112.             jnz @@FindStart                  ; /
  113. @@IncOne:   add si,2                         ;/
  114. @@GotStart: cld                              ;go forward, now
  115.             mov ah,8                         ;only copy 8 bytes for the base
  116. @@Basename: mov al,byte ptr ds:[si]          ;\
  117.             or al,al                         ; \ load a byte, handling it
  118.             jz @@AddDot                      ; / if it's a NULL
  119.             inc si                           ;/
  120.             cmp al,'.'                       ;if we've found a period, start
  121.             jz @@AddDot                      ;  working on the extension
  122.             MAKEUPPERCASE al                 ;\
  123.             mov byte ptr es:[di],al          ; > make uppercase and store
  124.             inc di                           ;/
  125.             cmp al,'*'                       ;if we've found an asterick, then
  126.             jz @@Ignore                      ;  ignore all up to period or end
  127.             dec ah                           ;\ only copy 8 characters
  128.             jnz @@Basename                   ;/
  129. @@Ignore:   mov al,byte ptr ds:[si]          ;\
  130.             or al,al                         ; \
  131.             jz @@AddDot                      ;  \ skip until a NULL or a
  132.             inc si                           ;  / period is encountered
  133.             cmp al,'.'                       ; /
  134.             jnz @@Ignore                     ;/
  135. @@AddDot:   mov byte ptr es:[di],'.'         ;\ tack on a period
  136.             inc di                           ;/
  137.             mov ah,3                         ;only copy 3 bytes for the ext
  138. @@Extension:lodsb                            ;\  load a byte and handle
  139.             or al,al                         ; > it if it's a NULL
  140.             jz @@Done                        ;/
  141.             MAKEUPPERCASE al                 ;\
  142.             mov byte ptr es:[di],al          ; > make uppercase and store it
  143.             inc di                           ;/
  144.             cmp al,'*'                       ;\ if we copied a '*', then there
  145.             jz @@Done                        ;/ shouldn't be anything after it
  146.             dec ah                           ;\ only copy 3 characters
  147.             jnz @@Extension                  ;/
  148. @@Done:     mov byte ptr es:[di],0           ;\ terminate it with a NULL
  149.             or al,1               ;clear ZF  ;/
  150. @@Quit:     pop dx bx ax di si               ;restore everything
  151.             ret                              ;return
  152. ENDP        FixFileName
  153. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  154. ;locates a file structure that matches identically to a specified file
  155. ;
  156. ;           ES:DI ==> ASCIIZ filename to match
  157. ;
  158. ;returns:   DS:SI ==> appropiate structure
  159. ;           AX = file number (0 based)
  160. ;           carry set if not found
  161. ;
  162. PROC        MatchFile
  163.             push bx cx dx
  164.             mov ax,seg FileStats             ;\
  165.             mov ds,ax                        ; > DS:SI ==> File structures
  166.             mov si,offset FileStats          ;/
  167.             mov cx,[NumFiles]                ;CX = number of files to search
  168.             xor ax,ax                        ;current file number
  169. @@NewFile:  xor bx,bx
  170. @@Compare:  mov dl,byte ptr ds:[si+bx].Filename
  171.             or dl,dl
  172.             jz @@GotIt
  173.             cmp dl,byte ptr es:[di+bx]
  174.             jnz @@Next
  175.             inc bx
  176.             jmp @@Compare
  177. @@Next:     add si,size LibFile
  178.             inc ax
  179.             loop @@NewFile
  180.             stc                              ;signal error (CF = 1)
  181.             jmp @@Quit
  182. @@GotIt:    clc                              ;signal success (CF = 0)
  183. @@Quit:     pop dx cx bx
  184.             ret
  185. ENDP        MatchFile
  186. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  187. ;
  188. ;     extern int jlib_open_library (char far *file)
  189. ;
  190. ;Sets the library that will be used for all subsequent JLIB file I/O accesses.
  191. ;This procedure will close previous library, if one is still loaded.
  192. ;
  193. ;returns:   0 = success
  194. ;           -1 = error
  195. ;
  196. PROC        _jlib_open_library
  197.             ARG file:dword
  198.             push bp                          ;preserve caller's stack frame
  199.             mov bp,sp                        ;use BP instead of SP
  200.             push si di ds                    ;save what we'll modify
  201.  
  202.             cmp [LibHandle],-1               ;is a library already loaded?
  203.             jz @@LoadIt                      ;  no -- don't need to close it
  204.             mov ah,3Eh                       ;\
  205.             mov bx,[LibHandle]               ; \ close the library file
  206.             int 21h                          ; /
  207.             mov [LibHandle],-1               ;/
  208.  
  209.             ;*** Reading of the JLIB file header ***
  210. @@LoadIt:   push ds                          ;\
  211.             mov ax,3D00h                     ; \
  212.             lds dx,[file]                    ;  \
  213.             int 21h                          ;   > try to open the library
  214.             pop ds                           ;  /
  215.             jc @@Error                       ; /
  216.             mov [LibHandle],ax               ;/
  217. ;-------- 1.3 changes start here
  218.             mov ax,-4                        ;\
  219.             cwd                              ; \
  220.             mov cx,dx                        ;  \
  221.             mov dx,ax                        ;   > seek to 4 bytes before end
  222.             mov ax,4202h                     ;  /
  223.             mov bx,[LibHandle]               ; /
  224.             int 21h                          ;/
  225.             mov ah,3Fh                       ;\
  226.             mov bx,[LibHandle]               ; \
  227.             mov cx,4                         ;  > read in the offset to start
  228.             mov dx,offset HeaderOffset       ; /
  229.             int 21h                          ;/
  230.             mov ax,4202h                     ;\
  231.             mov bx,[LibHandle]               ; \
  232.             xor cx,cx                        ;  > find out length of total file
  233.             xor dx,dx                        ; /
  234.             int 21h                          ;/
  235.             sub ax,word ptr [HeaderOffset+0] ;\
  236.             sbb dx,word ptr [HeaderOffset+2] ; \ compute offset to start of JLIB
  237.             mov word ptr [HeaderOffset+0],ax ; /
  238.             mov word ptr [HeaderOffset+2],dx ;/
  239.             mov ax,4200h                     ;\
  240.             mov bx,[LibHandle]               ; \
  241.             mov cx,word ptr [HeaderOffset+2] ;  > seek to start of JLIB
  242.             mov dx,word ptr [HeaderOffset+0] ; /
  243.             int 21h                          ;/
  244. ;-------- 1.3 changes end here
  245.             mov ah,3Fh                       ;\
  246.             mov bx,[LibHandle]               ; \
  247.             mov cx,6                         ;  \ read in signature and
  248.             mov dx,offset TempBuffer         ;  / file version number
  249.             int 21h                          ; /
  250.             jc @@Error                       ;/
  251.             cmp word ptr [TempBuffer],'LJ'   ;\
  252.             jnz @@Error                      ; \
  253.             cmp word ptr [TempBuffer+2],'bi' ;  \ check the signarure and
  254.             jnz @@Error                      ;  / file version number
  255.             cmp word ptr [TempBuffer+4],MINORVER*256+MAJORVER
  256.             jnz @@Error                      ;/
  257.             mov ah,3Fh                       ;\
  258.             mov bx,[LibHandle]               ; \
  259.             mov cx,2                         ;  \ read in number of files
  260.             mov dx,offset NumFiles           ;  / contained in the library
  261.             int 21h                          ; /
  262.             jc @@Error                       ;/
  263.             mov cx,[NumFiles]                ;\
  264.             cmp cx,MAXFILES                  ; > check the limit of files
  265.             ja @@Error                       ;/
  266.             mov ah,3Fh                       ;\
  267.             mov bx,[LibHandle]               ; \
  268.             mov cx,4                         ;  \ read in the offset to
  269.             mov dx,offset TempBuffer         ;  / the directory structures
  270.             int 21h                          ; /
  271.             jc @@Error                       ;/
  272.             mov ax,4200h                     ;\
  273.             mov bx,[LibHandle]               ; \
  274.             mov cx,word ptr [TempBuffer+2]   ;  \ seek to the start of the
  275.             mov dx,word ptr [TempBuffer+0]   ;  / directory structures
  276. ;-------- 1.3 changes start here
  277.             add dx,word ptr [HeaderOffset+0]
  278.             adc cx,word ptr [HeaderOffset+2]
  279. ;-------- 1.3 changes end here
  280.             int 21h                          ; /
  281.             jc @@Error                       ;/
  282.             mov cx,[NumFiles]                ;\
  283.             mov dx,offset FileStats          ; \
  284. @@Read:     push cx dx                       ;  \
  285.             mov ah,3Fh                       ;   |
  286.             mov bx,[LibHandle]               ;   |
  287.             mov cx,size LibFile-5            ;   | read in the directory
  288.             int 21h                          ;   | information for each file
  289.             jb @@Error                       ;   |
  290.             pop dx cx                        ;   |
  291.             mov si,dx                        ;   |
  292.             mov ds:[si].Opened,0             ;  /
  293.             add dx,size LibFile              ; /
  294.             loop @@Read                      ;/
  295.  
  296.             xor ax,ax                        ;indicate success
  297.             jmp @@Done                       ;return back to main program
  298. @@Error:    cmp [LibHandle],-1               ;do we need to close the library?
  299.             jz @@Error2                      ;  no -- then don't
  300.             mov ah,3Eh                       ;\
  301.             mov bx,[LibHandle]               ; \ close the library file
  302.             int 21h                          ; /
  303.             mov [LibHandle],-1               ;/
  304. @@Error2:   mov ax,-1                        ;indicate failure
  305. @@Done:     pop ds di si bp                  ;restore caller's stack frame
  306.             ret                              ;return
  307. ENDP        _jlib_open_library
  308. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  309. ;
  310. ;     extern int jlib_close_library (void)
  311. ;
  312. ; Closes the current library file
  313. ;
  314. ; Returns 0 on success, -1 on failure
  315. ;
  316. PROC        _jlib_close_library
  317.             push si di                       ;save what we'll modify
  318.             cmp [LibHandle],-1               ;is a library already loaded?
  319.             jz @@Error                       ;  no -- don't need to close it
  320.             mov ah,3Eh                       ;\
  321.             mov bx,[LibHandle]               ; \ close the library file
  322.             int 21h                          ; /
  323.             mov [LibHandle],-1               ;/
  324.             xor ax,ax                        ;indicate success
  325.             jmp short @@Done                 ;return
  326. @@Error:    mov ax,-1                        ;indicate error
  327. @@Done:     pop di si                        ;restore what we saved
  328.             ret                              ;return
  329. ENDP        _jlib_close_library
  330. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  331. ;
  332. ;     extern int jlib_open_file (char far *file)
  333. ;
  334. ; Opens a file that is stored in the library file for read-only
  335. ; access
  336. ;
  337. ; Returns file handle on success, -1 on failure
  338. ;
  339. PROC        _jlib_open_file
  340.             ARG file:dword
  341.             push bp                          ;preserve caller's stack frame
  342.             mov bp,sp                        ;use BP instead of SP
  343.             push si di ds                    ;save what we'll modify
  344.  
  345.             cmp [LibHandle],-1               ;is a library already loaded?
  346.             jz @@Error                       ;  no -- return error
  347.  
  348.             lds si,[file]                    ;DS:SI ==> source filename
  349.             mov ax,@data                     ;\
  350.             mov es,ax                        ; > ES:DI ==> target buffer
  351.             mov di,offset TempBuffer         ;/
  352.             call FixFileName                 ;parse the filename
  353.             jz @@Error                       ;if nothing passed, return error
  354.             call MatchFile                   ;match the filename
  355.             jc @@Error                       ;if nothing matched, return error
  356.  
  357.             pop ds
  358.             push ds
  359.             cmp [si].Opened,0                ;is the file already "opened"?
  360.             jnz @@Error                      ;  yes -- return error
  361.             mov [si].Opened,1                ;say the file is now opened
  362.             mov word ptr [si+0].CurPos,0     ;\ move the file position to
  363.             mov word ptr [si+2].CurPos,0     ;/ the beginning of the file
  364.             jmp @@Quit                       ;return
  365.  
  366. @@Error:    mov ax,-1                        ;indicate error
  367. @@Quit:     pop ds di si bp                  ;restore the caller's stack frame
  368.             ret                              ;return
  369. ENDP        _jlib_open_file
  370. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  371. ;
  372. ;   extern long jlib_close_file (int handle)
  373. ;
  374. ; Closes the file associated with the specified handle
  375. ;
  376. ; Returns 0 on success, -1 on failure
  377. ;
  378. PROC        _jlib_close_file
  379. ARG         handle:word
  380.             push bp                          ;preserve caller's stack frame
  381.             mov bp,sp                        ;use BP instead of SP
  382.             push si di                       ;save SI and DI, too
  383.  
  384.             cmp [LibHandle],-1               ;is a library already loaded?
  385.             jz @@Error                       ;  no -- return error
  386.             mov ax,[NumFiles]                ;\
  387.             cmp [handle],ax                  ; > if the handle is out of range
  388.             jae @@Error                      ;/  then return an error
  389.             mov ax,size LibFile              ;\
  390.             mul [handle]                     ; \ DS:SI ==> file information
  391.             mov si,offset FileStats          ; /
  392.             add si,ax                        ;/
  393.             cmp ds:[si].Opened,0             ;is the file opened yet?
  394.             jz @@Error                       ;  no -- return error
  395.  
  396.             mov ds:[si].Opened,0             ;close the file
  397.             jmp @@Done                       ;return
  398.  
  399. @@Error:    mov ax,-1                        ;\ indicate error
  400. @@Done:     pop di si bp                     ;restore caller's stack frame
  401.             ret                              ;return
  402. ENDP        _jlib_close_file
  403. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  404. ;
  405. ;   extern long jlib_ftell (int handle)
  406. ;
  407. ; Returns file pointer position on success, -1 on failure
  408. ;
  409. PROC        _jlib_ftell
  410. ARG         handle:word
  411.             push bp                          ;preserve caller's stack frame
  412.             mov bp,sp                        ;use BP instead of SP
  413.             push si di                       ;save SI and DI, too
  414.  
  415.             cmp [LibHandle],-1               ;is a library already loaded?
  416.             jz @@Error                       ;  no -- return error
  417.             mov ax,[NumFiles]                ;\
  418.             cmp [handle],ax                  ; > if the handle is out of range
  419.             jae @@Error                      ;/  then return an error
  420.             mov ax,size LibFile              ;\
  421.             mul [handle]                     ; \ DS:SI ==> file information
  422.             mov si,offset FileStats          ; /
  423.             add si,ax                        ;/
  424.             cmp ds:[si].Opened,0             ;is the file opened yet?
  425.             jz @@Error                       ;  no -- return error
  426.  
  427.             mov ax,word ptr [si+0].CurPos    ;\ DX:AX ==> current position
  428.             mov dx,word ptr [si+2].CurPos    ;/
  429.             jmp @@Done                       ;return
  430.  
  431. @@Error:    mov ax,-1                        ;\ indicate error
  432.             mov dx,-1                        ;/
  433. @@Done:     pop di si bp                     ;restore caller's stack frame
  434.             ret                              ;return
  435. ENDP        _jlib_ftell
  436. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  437. ;
  438. ;   extern long jlib_filelength (int handle)
  439. ;
  440. ; Returns the length of the file associated with the specified handle
  441. ;
  442. ; Returns file length on success, -1 on failure
  443. ;
  444. PROC        _jlib_filelength
  445. ARG         handle:word
  446.             push bp                          ;preserve caller's stack frame
  447.             mov bp,sp                        ;use BP instead of SP
  448.             push si di                       ;save SI and DI, too
  449.  
  450.             cmp [LibHandle],-1               ;is a library already loaded?
  451.             jz @@Error                       ;  no -- return error
  452.             mov ax,[NumFiles]                ;\
  453.             cmp [handle],ax                  ; > if the handle is out of range
  454.             jae @@Error                      ;/  then return an error
  455.             mov ax,size LibFile              ;\
  456.             mul [handle]                     ; \ DS:SI ==> file information
  457.             mov si,offset FileStats          ; /
  458.             add si,ax                        ;/
  459.             cmp ds:[si].Opened,0             ;is the file opened yet?
  460.             jz @@Error                       ;  no -- return error
  461.  
  462.             mov ax,word ptr [si+0].Filesize  ;\ DX:AX ==> current position
  463.             mov dx,word ptr [si+2].Filesize  ;/
  464.             jmp @@Done                       ;return
  465.  
  466. @@Error:    mov ax,-1                        ;\ indicate error
  467.             mov dx,-1                        ;/
  468. @@Done:     pop di si bp                     ;restore caller's stack frame
  469.             ret                              ;return
  470. ENDP        _jlib_filelength
  471. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  472. ;
  473. ;   extern long jlib_fseek (int handle, long position, char method_code)
  474. ;
  475. ; Moves the file pointer according to the position and method code
  476. ;
  477. ; Returns file pointer position on success, -1 on failure
  478. ;
  479. PROC        _jlib_fseek
  480. ARG         handle:word,position:dword,method_code:byte
  481.             push bp                          ;preserve caller's stack frame
  482.             mov bp,sp                        ;use BP insead of SP
  483.             push si di                       ;save SI and DI, too
  484.  
  485.             cmp [LibHandle],-1               ;is a library already loaded?
  486.             jz @@Error                       ;  no -- return error
  487.             mov ax,[NumFiles]                ;\
  488.             cmp [handle],ax                  ; > if the handle is out of range
  489.             jae @@Error                      ;/  then return an error
  490.             mov ax,size LibFile              ;\
  491.             mul [handle]                     ; \ DS:SI ==> file information
  492.             mov si,offset FileStats          ; /
  493.             add si,ax                        ;/
  494.             cmp ds:[si].Opened,0             ;is the file opened yet?
  495.             jz @@Error                       ;  no -- return error
  496.  
  497.             cmp al,0                         ;\
  498.             jnz @@Method1                    ; \
  499. @@Method0:  xor ax,ax                        ;  > mode 0: start from beginning
  500.             xor dx,dx                        ; /
  501.             jmp @@AddOffset                  ;/
  502. @@Method1:  cmp al,1                         ;\
  503.             jnz @@Method2                    ; \
  504.             mov ax,word ptr [si+0].CurPos    ;  > mode 1: start from location
  505.             mov dx,word ptr [si+2].CurPos    ; /
  506.             jmp @@AddOffset                  ;/
  507. @@Method2:  cmp al,2                         ;\
  508.             jnz @@Error                      ; \ mode 2: start from end
  509.             mov ax,word ptr [si+0].Filesize  ; /
  510.             mov dx,word ptr [si+2].Filesize  ;/
  511.  
  512. @@AddOffset:add ax,word ptr [position+0]     ;\ add in the offset
  513.             adc ax,word ptr [position+2]     ;/
  514.             jc @@Error                       ;signal error, if we overflowed
  515.             cmp dx,word ptr [si+2].Filesize  ;\
  516.             ja @@Error                       ; \  make sure new position is
  517.             jb @@Okay                        ;  > less than or equal to the
  518.             cmp ax,word ptr [si+0].Filesize  ; /  length of the file
  519.             jae @@Error                      ;/
  520. @@Okay:     mov word ptr [si+0].CurPos,ax    ;\ save the new position
  521.             mov word ptr [si+2].CurPos,dx    ;/
  522.             jmp @@Done                       ;return with the current position
  523. @@Error:    mov ax,-1                        ;\ indicate error
  524.             mov dx,-1                        ;/
  525. @@Done:     pop di si bp                     ;restore caller's stack frame
  526.             ret                              ;return
  527. ENDP        _jlib_fseek
  528. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  529. ;
  530. ;     extern int jlib_read_file (int handle, char far *buffer, int count)
  531. ;
  532. ;reads a block of count bytes from the file specified by the handle into the
  533. ;specified buffer
  534. ;
  535. ;note:  If 'SLIDINGREQUEST' is nonzero and a value of 'count' is passed that
  536. ;       will go past the end of the logical end of file, it will be forced
  537. ;       down to the maximum number that will not go past the end.  If it is
  538. ;       not defined, then requests that would go past the end will be rejected
  539. ;       and an error will be returned
  540. ;also:  Note that this function returns 0 on failure, while most of the others
  541. ;       return -1 on failure
  542. ;
  543. ;Returns number of bytes read on success, or 0 on failure
  544. ;
  545. PROC        _jlib_read_file
  546.             ARG handle:word,buffer:dword,count:word
  547.             push bp                          ;preserve caller's stack frame
  548.             mov bp,sp                        ;use BP instead of SP
  549.             push si di ds                    ;save what we'll modify
  550.  
  551.             cmp [LibHandle],-1               ;is a library already loaded?
  552.             jz @@Error                       ;  no -- return error
  553.             mov ax,size LibFile              ;\
  554.             mul [handle]                     ; \ DS:SI ==> file information
  555.             mov si,offset FileStats          ; /
  556.             add si,ax                        ;/
  557.             cmp ds:[si].Opened,0             ;is the file opened yet?
  558.             jz @@Error                       ;  no -- return error
  559.  
  560.             mov ax,4200h                     ;\
  561.             mov bx,[LibHandle]               ; \
  562.             mov dx,word ptr [si+0].Fileoffset;  \
  563.             mov cx,word ptr [si+2].Fileoffset;   \ seek to where we're
  564.             add dx,word ptr [si+0].CurPos    ;   / supposed to be
  565.             adc cx,word ptr [si+2].CurPos    ;  /
  566. ;-------- 1.3 changes start here
  567.             add dx,word ptr [HeaderOffset+0]
  568.             adc cx,word ptr [HeaderOffset+2]
  569. ;-------- 1.3 changes end here
  570.             int 21h                          ; /
  571.             jc @@Error                       ;/
  572.  
  573.             mov ax,word ptr [si+0].Filesize  ;\
  574.             mov dx,word ptr [si+2].Filesize  ; \
  575.             sub ax,word ptr [si+0].CurPos    ;  | figure out the maximum no.
  576.             sbb dx,word ptr [si+2].CurPos    ;  | of bytes that could be read
  577.             mov cx,[count]                   ;  | and compare the request with
  578.             or dx,dx                         ;  | it, forcing to the maximum
  579.             jnz @@Okay                       ;  | allowed if it will go past
  580.             cmp ax,cx                        ;  | the end of the file.
  581. IF SLIDINGREQUEST NE 0
  582.             jae @@Okay                       ; /
  583.             mov cx,ax                        ;/
  584. ELSE
  585.             jb @@Error
  586. ENDIF
  587.  
  588. @@Okay:     push ds                          ;\
  589.             mov ah,3Fh                       ; \
  590.             mov bx,[LibHandle]               ;  \
  591.             lds dx,[buffer]                  ;   \ actually do the read
  592.             int 21h                          ;  /
  593.             pop ds                           ; /
  594.             jc @@Error                       ;/
  595.             mov di,ax                        ;DI = number of bytes read
  596.  
  597.             mov ax,4201h                     ;\
  598.             mov bx,[LibHandle]               ; \
  599.             xor cx,cx                        ;  \
  600.             xor dx,dx                        ;   \
  601.             int 21h                          ;    \ find out where we are now
  602.             jc @@Error                       ;    / and save it
  603.             sub ax,word ptr [si+0].Fileoffset;   /
  604.             sbb dx,word ptr [si+2].Fileoffset;  /
  605. ;-------- 1.3 changes start here
  606.             sub ax,word ptr [HeaderOffset+0]
  607.             sbb dx,word ptr [HeaderOffset+2]
  608. ;-------- 1.3 changes end here
  609.             mov word ptr [si+0].CurPos,ax    ; /
  610.             mov word ptr [si+2].CurPos,dx    ;/
  611.  
  612.             mov ax,di                        ;return the number of bytes read
  613.             jmp @@Done                       ;return
  614. @@Error:    xor ax,ax                        ;signal error
  615. @@Done:     pop ds di si bp                  ;restore caller's stack frame
  616.             ret                              ;return
  617. ENDP        _jlib_read_file
  618. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  619.             END
  620.