include	clib.inc
ifdef __ZIP__
include dos.inc
include iost.inc
include wsub.inc
include fblk.inc
include string.inc
include unzip.inc

_TEXT	SEGMENT

zip_copylocal proc dist pascal public uses si di exact_match:byte
local	extsize_local:word
local	offset_local:dword
	invoke	strlen, addr __outpath
	mov	di,ax
	xor	ax,ax
	mov	si,ax
	mov	[bp-6],ax
	dec	ax
	mov	[bp-2],ax
	mov	[bp-4],ax
    copylocal_loop:
	mov	ax,SIZE S_ZEND
	call	oread
	jnz	copylocal_test_local
    copylocal_error:
	mov	ax,-1
    copylocal_toend:
	jmp	copylocal_end
    copylocal_test_local:
	cmp	word ptr es:[bx],ZIPHEADERID
	jne	copylocal_error
	mov	ax,si
	cmp	word ptr es:[bx+2],ZIPLOCALID
	jne	copylocal_toend
	mov	ax,SIZE S_LZIP
	add	ax,di
    copylocal_oread:
	call	oread
	jz	copylocal_error
	mov	dx,word ptr es:[bx.lz_csize+2]
	mov	ax,SIZE S_LZIP
	add	ax,es:[bx].lz_fnsize
	cmp	cx,ax
	jb	copylocal_oread
	add	ax,es:[bx].lz_extsize
	add	ax,word ptr es:[bx.lz_csize]
	adc	dx,0
	push	dx
	push	ax
	test	si,si
	jnz	copylocal_copy
	mov	ax,es:[bx].lz_fnsize
	cmp	exact_match,0
	je	copylocal_subdir
	cmp	di,ax
	je	copylocal_compare
    copylocal_copy:
	call	ocopy
	jz	copylocal_error
    copylocal_next:
	jmp	copylocal_loop
    copylocal_subdir:
	cmp	ax,di
	jbe	copylocal_copy
    copylocal_compare:
	push	bx
	add	bx,SIZE S_LZIP
	invoke	strnicmp, addr __outpath, es::bx, di
	pop	bx
	jnz	copylocal_copy
    copylocal_found:
	inc	si
	mov	ax,es:[bx].lz_extsize
	mov	[bp-6],ax
	mov	ax,es:[bx].lz_fnsize
	push	ax
	add	bx,SIZE S_LZIP
	and	ax,01FFh
	invoke	memcpy, addr entryname, es::bx, ax
	pop	bx
	add	bx,ax
	mov	byte ptr es:[bx],0
	lodm	STDO.ios_total
	add	ax,STDO.ios_i
	adc	dx,0
	mov	[bp-2],dx
	mov	[bp-4],ax
	pop	cx
	pop	bx
	add	ax,cx
	adc	dx,bx
	invoke	oseek, dx::ax, SEEK_SET
	jnz	copylocal_next
	mov	ax,-1
    copylocal_end:
	mov	dx,[bp-2]
	mov	cx,[bp-4]
	mov	bx,[bp-6]
	ret
zip_copylocal endp

zip_copycentral proc dist pascal public uses si di loffset:dword,
	lsize:dword, exact_match:byte
	invoke	strlen, addr __outpath
	mov	di,ax
	xor	si,si
    copycentral_loop:
	mov	ax,SIZE S_ZEND
	call	oread
	jnz	copycentral_03
    copycentral_ioerror:
	mov	si,-1
    copycentral_end:
	mov	ax,si
	ret
    copycentral_03:
	cmp	es:[bx.cz_pkzip],ZIPHEADERID	; 'PK'	4B50h
	jne	copycentral_ioerror
	cmp	es:[bx.cz_zipid],ZIPCENTRALID	; 1,2	0201h
	jne	copycentral_end
	mov	ax,SIZE S_CZIP
	call	oread
	jz	copycentral_ioerror
	mov	ax,SIZE S_CZIP
	add	ax,es:[bx.cz_fnsize]
	call	oread
	jz	copycentral_ioerror
	mov	ax,SIZE S_CZIP		; Central directory
	add	ax,es:[bx].cz_fnsize	; file name length (*this)
	add	ax,es:[bx].cz_extsize
	add	ax,es:[bx].cz_cmtsize
	push	0
	push	ax			; = size of this record
  ifdef __386__
	mov	eax,loffset		; Update local offset if above
	cmp	es:[bx.cz_off_local],eax
	jb	copycentral_06
	mov	eax,lsize
	sub	es:[bx.cz_off_local],eax
  else
	mov	ax,word ptr loffset+2	; Update local offset if above
	cmp	word ptr es:[bx.cz_off_local+2],ax
	jb	copycentral_06
	ja	copycentral_05
	mov	ax,word ptr loffset
	cmp	word ptr es:[bx.cz_off_local],ax
	jb	copycentral_06
    copycentral_05:
	lodm	lsize
	sub	word ptr es:[bx.cz_off_local],ax
	sbb	word ptr es:[bx.cz_off_local+2],dx
  endif
    copycentral_06:
	test	si,si
	jnz	copycentral_copy	; already found -- deleted
	mov	ax,es:[bx].cz_fnsize
	cmp	exact_match,0
	je	copycentral_subdir
	cmp	di,ax
	jne	copycentral_copy
    copycentral_subdir:
	cmp	di,ax
	ja	copycentral_copy
	add	bx,SIZE S_CZIP
	invoke	strnicmp, addr __outpath, es::bx, di
	test	ax,ax
	jz	copycentral_delete
    copycentral_copy:
	call	ocopy
	jz	copycentral_error
    copycentral_next:
	jmp	copycentral_loop
    copycentral_delete:
	inc	si
	push	SEEK_CUR
	call	oseek
	jnz	copycentral_next
    copycentral_error:
	jmp	copycentral_ioerror
zip_copycentral endp

_TEXT	ENDS
endif
	END
