include	clib.inc
include	io.inc
include	string.inc
include	iost.inc
ifdef __TE__
include	dobj.inc
include	tinfo.inc
endif
ifdef __TVEXE__
	extrn tvmem_size: DWORD
	extrn tvmem_offs: DWORD
endif
ifdef __ZIP__
	extrn odecrypt: NEAR
endif
	public	ofread
	public	oupdate

_TEXT	SEGMENT

ifdef __TE__

	public	tiofread

tiofread PROC
	mov	ax,STDI.ios_l
	cmp     STDI.ios_c,0
	je	tiofread_read
	inc	STDI.ios_l
	inc	ax
    tiofread_read:
	call	tigetline
	jc 	tiofread_eof
	stom	STDI.ios_bp
	push	cx
	invoke	strlen, dx::ax
	pop	cx
	inc	ax
	mov	STDI.ios_c,ax
	xor	ax,ax
	mov	STDI.ios_i,ax
	inc	ax
	ret
    tiofread_eof:
	xor	ax,ax
	ret
tiofread ENDP

endif

ofread	PROC	DIST
	push	si
	push	di
	mov	si,STDI.ios_flag
  ifdef __TVEXE__
	test	si,IO_MEMREAD
	jnz	ofread_memory
  endif
  ifdef __TE__
	test	si,IO_LINEBUF
	jnz	ofread_linebuf
  endif
	mov	di,STDI.ios_c
	sub	di,STDI.ios_i
	jnz	ofread_copy
    ofread_read:
	xor	ax,ax
	mov	STDI.ios_i,ax
	mov	STDI.ios_c,di
	mov	cx,STDI.ios_size
	sub	cx,di
	push	ds
	mov	bx,STDI.ios_file
	lds	dx,STDI
	add	dx,di
	mov	ax,3F00h
	int	21h
	pop	ds
	jc	ofread_error
	add	STDI.ios_c,ax
	add	ax,di
	and	si,IO_UPDTOTAL or IO_USECRC or IO_USEUPD or IO_CRYPT
	jnz	ofread_user
    ofread_end:
	test	ax,ax
	pop	di
	pop	si
	ret
  ifdef __TVEXE__
    ofread_memory:
	call	iomemread
	jmp	ofread_end
  endif
  ifdef __TE__
    ofread_linebuf:
	call	tiofread
	jmp	ofread_end
  endif
    ofread_user:
  ifdef __ZIP__
	test	si,IO_CRYPT
	jz	@F
	call	odecrypt
    @@:
  endif
	test	si,IO_UPDTOTAL
	jz	ofread_crc
	add	word ptr STDI.ios_total,ax
	adc	word ptr STDI.ios_total+2,0
    ofread_crc:
	test	si,IO_USECRC
	jz	ofread_progress
	mov	dx,di
	mov	si,OSTDI
	call	oupdcrc
	mov	si,STDI.ios_flag
    ofread_progress:
	test	si,IO_USEUPD
	jz	ofread_end
	push	ax
	push	0
	call	oupdate
	dec	ax
	pop	ax
	jnz	ofread_error
	jmp	ofread_end
    ofread_copy:
	cmp	di,STDI.ios_c
	je	ofread_eof
	lodm	STDI.ios_bp
	push	dx
	push	ax
	push	dx
	add	ax,STDI.ios_i
	push	ax
	push	di
	call	memcpy
	jmp	ofread_read
    ofread_error:
	call	@dosmaper
	or	STDI.ios_flag,IO_ERROR
    ofread_eof:
	xor	ax,ax
	jmp	ofread_end
ofread	ENDP

ifdef __TVEXE__

iomemread PROC
  ifdef __386__
	mov	eax,tvmem_size
	cmp	eax,STDI.ios_offset
	jbe     iomemread_02
	mov	eax,STDI.ios_offset
	push	STDI.ios_bp
	mov	dx,ax
	and	dx,000Fh
	and	al,0F0h
	shl	eax,12
	mov	ax,dx
	push	eax
	mov	ecx,STDI.ios_offset
	xor	eax,eax
	mov	STDI.ios_i,ax
	cmp	ax,STDI.ios_c
	mov	ax,STDI.ios_size
	mov	STDI.ios_c,ax
	jz	@F
	add	ecx,eax
	cmp	ecx,tvmem_size
	jbe	@F
	sub	ecx,eax
	mov	eax,tvmem_size
	sub	eax,ecx
	mov	STDI.ios_c,ax
	add	ecx,eax
      @@:
	mov	STDI.ios_offset,ecx
	push	STDI.ios_c
	call	memcpy
	mov	ax,1
	ret
    iomemread_02:
	mov	STDI.ios_offset,eax
  else
	lodm	tvmem_size
	cmp	dx,word ptr STDI.ios_offset+2
	jne	@F
	cmp	ax,word ptr STDI.ios_offset
      @@:
	jbe     iomemread_02
	pushm	STDI.ios_bp
	lodm	STDI.ios_offset
	mov	bx,ax
	and	bx,000Fh
	shl	dx,12
	shr	ax,4
	or	dx,ax
	push	dx
	push	bx
	xor	ax,ax
	mov	STDI.ios_i,ax
	cmp	ax,STDI.ios_c
	mov	dx,word ptr STDI.ios_offset+2
	mov	cx,word ptr STDI.ios_offset
	mov	ax,STDI.ios_size
	mov	STDI.ios_c,ax
	jz	@F
	add	cx,ax
	adc	dx,0
	cmp	dx,word ptr tvmem_size+2
	jbe	@F
	mov	dx,word ptr STDI.ios_offset+2
	mov	cx,word ptr STDI.ios_offset
	mov	ax,word ptr tvmem_size
	mov	bx,word ptr tvmem_size+2
	sub	ax,cx
	sbb	bx,dx
	mov	STDI.ios_c,ax
	add	cx,ax
	adc	dx,bx
      @@:
	mov	word ptr STDI.ios_offset,cx
	mov	word ptr STDI.ios_offset+2,dx
	push	STDI.ios_c
	call	memcpy
	mov	ax,1
	ret
    iomemread_02:
	stom	STDI.ios_offset
  endif
	xor	ax,ax
	mov	STDI.ios_i,ax
	mov	STDI.ios_c,ax
	ret
iomemread ENDP

endif

_TEXT	ENDS

_DATA	SEGMENT

oupdate	PF	0

_DATA	ENDS

	END
