// File: sisong[P]ApkDiffPatch/src/patch/Zipper.cpp
// Function: UnZipper_openStream
bool UnZipper_openStream(UnZipper* self,const hpatch_TStreamInput* zipFileStream,
                         bool isDataNormalized,bool isFileDataOffsetMatch,bool isSaved_fileEntryOffsets){
    check(_UnZipper_open_begin(self));
    self->stream=zipFileStream;
    self->_isSaved_fileEntryOffsets=isSaved_fileEntryOffsets;
    //_sreachVCE
    ZipFilePos_t endCentralDirectory_pos=0;
    ZipFilePos_t centralDirectory_pos=0;
    uint32_t     fileCount=0;
    ZipFilePos_t v2sign_topPos=0;
    check(_UnZipper_searchEndCentralDirectory(self,&endCentralDirectory_pos));
    check(_UnZipper_searchCentralDirectory(self,endCentralDirectory_pos,&centralDirectory_pos,&fileCount));
    check(UnZipper_searchApkV2Sign(self->stream,centralDirectory_pos,&v2sign_topPos,0,&self->_isHaveV3Sign));

    // now don't know fileDataEndPos value
    const ZipFilePos_t kNullSize=1024*4;
    ZipFilePos_t ripe_fileDataEndPos=(v2sign_topPos>kNullSize)?(v2sign_topPos-kNullSize):0;
    assert(self->_cache_fvce==0);
    for (int i=0; i<2; ++i){ //try 2 times
        check(_UnZipper_open_fvce(self,(ZipFilePos_t)self->stream->streamSize-ripe_fileDataEndPos,fileCount));
        self->_vce=self->_cache_fvce+(v2sign_topPos-ripe_fileDataEndPos);
        self->_centralDirectory=self->_vce+(centralDirectory_pos-v2sign_topPos);
        self->_endCentralDirectory=self->_vce+(endCentralDirectory_pos-v2sign_topPos);
        
        check(UnZipper_fileData_read(self,ripe_fileDataEndPos,self->_cache_fvce,self->_cache_fvce+self->_fvce_size));
        ZipFilePos_t fileDataEndPos;
        check(_UnZipper_vce_normalized(self,isFileDataOffsetMatch,&fileDataEndPos));
        if (fileDataEndPos>=ripe_fileDataEndPos){ //ok
            ZipFilePos_t nullSize=(fileDataEndPos-ripe_fileDataEndPos);
            if (nullSize>0){//need move data
                self->_vce-=nullSize;
                self->_centralDirectory-=nullSize;
                self->_endCentralDirectory-=nullSize;
                self->_fvce_size-=nullSize;
                memmove(self->_cache_fvce,self->_cache_fvce+nullSize,self->_fvce_size);
            }
            break;
        }else{ //continue
            _UnZipper_close_fvce(self);
            ripe_fileDataEndPos=fileDataEndPos;
        }
    }
    check(self->_cache_fvce!=0);
    self->_isDataNormalized=isDataNormalized;
    self->_isFileDataOffsetMatch=isFileDataOffsetMatch;
    return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movb %r8b, %al
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
andb $0x1, %dl
movb %dl, -0x11(%rbp)
andb $0x1, %cl
movb %cl, -0x12(%rbp)
andb $0x1, %al
movb %al, -0x13(%rbp)
movq -0x8(%rbp), %rdi
callq 0x7f880
testb $0x1, %al
jne 0x7f4d0
leaq 0x2ddb8(%rip), %rdi     # 0xad270
leaq 0x344cb(%rip), %rsi     # 0xb398a
movl $0x1e2, %edx            # imm = 0x1E2
leaq 0x345ae(%rip), %rcx     # 0xb3a79
callq 0x4270
movq -0x10(%rbp), %rcx
movq -0x8(%rbp), %rax
movq %rcx, (%rax)
movb -0x13(%rbp), %cl
movq -0x8(%rbp), %rax
andb $0x1, %cl
movb %cl, 0x88(%rax)
movl $0x0, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
movl $0x0, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x18(%rbp), %rsi
callq 0x7f920
testb $0x1, %al
jne 0x7f537
leaq 0x2dd51(%rip), %rdi     # 0xad270
leaq 0x34464(%rip), %rsi     # 0xb398a
movl $0x1ea, %edx            # imm = 0x1EA
leaq 0x34547(%rip), %rcx     # 0xb3a79
callq 0x4270
movq -0x8(%rbp), %rdi
movl -0x18(%rbp), %esi
leaq -0x1c(%rbp), %rdx
leaq -0x20(%rbp), %rcx
callq 0x7fa60
testb $0x1, %al
jne 0x7f56e
leaq 0x2dd1a(%rip), %rdi     # 0xad270
leaq 0x3442d(%rip), %rsi     # 0xb398a
movl $0x1eb, %edx            # imm = 0x1EB
leaq 0x34510(%rip), %rcx     # 0xb3a79
callq 0x4270
movq -0x8(%rbp), %rax
movq (%rax), %rdi
movl -0x1c(%rbp), %eax
movl %eax, %esi
movq -0x8(%rbp), %r8
addq $0x8b, %r8
leaq -0x24(%rbp), %rdx
xorl %eax, %eax
movl %eax, %ecx
callq 0x7e5f0
testb $0x1, %al
jne 0x7f5b5
leaq 0x2dcd3(%rip), %rdi     # 0xad270
leaq 0x343e6(%rip), %rsi     # 0xb398a
movl $0x1ec, %edx            # imm = 0x1EC
leaq 0x344c9(%rip), %rcx     # 0xb3a79
callq 0x4270
movl $0x1000, -0x28(%rbp)    # imm = 0x1000
cmpl $0x1000, -0x24(%rbp)    # imm = 0x1000
jbe 0x7f5d2
movl -0x24(%rbp), %eax
subl $0x1000, %eax           # imm = 0x1000
movl %eax, -0x3c(%rbp)
jmp 0x7f5d9
xorl %eax, %eax
movl %eax, -0x3c(%rbp)
jmp 0x7f5d9
movl -0x3c(%rbp), %eax
movl %eax, -0x2c(%rbp)
movq -0x8(%rbp), %rax
cmpq $0x0, 0xa0(%rax)
jne 0x7f5ef
jmp 0x7f60e
leaq 0x344d7(%rip), %rdi     # 0xb3acd
leaq 0x3438d(%rip), %rsi     # 0xb398a
movl $0x1f1, %edx            # imm = 0x1F1
leaq 0x34470(%rip), %rcx     # 0xb3a79
callq 0x4270
movl $0x0, -0x30(%rbp)
cmpl $0x2, -0x30(%rbp)
jge 0x7f81e
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq 0x8(%rax), %rax
movl %eax, %esi
subl -0x2c(%rbp), %esi
movl -0x20(%rbp), %edx
callq 0x7fb00
testb $0x1, %al
jne 0x7f65e
leaq 0x2dc2a(%rip), %rdi     # 0xad270
leaq 0x3433d(%rip), %rsi     # 0xb398a
movl $0x1f3, %edx            # imm = 0x1F3
leaq 0x34420(%rip), %rcx     # 0xb3a79
callq 0x4270
movq -0x8(%rbp), %rax
movq 0xa0(%rax), %rcx
movl -0x24(%rbp), %eax
subl -0x2c(%rbp), %eax
movl %eax, %eax
addq %rax, %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x98(%rax)
movq -0x8(%rbp), %rax
movq 0x98(%rax), %rcx
movl -0x1c(%rbp), %eax
subl -0x24(%rbp), %eax
movl %eax, %eax
addq %rax, %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x50(%rax)
movq -0x8(%rbp), %rax
movq 0x98(%rax), %rcx
movl -0x18(%rbp), %eax
subl -0x24(%rbp), %eax
movl %eax, %eax
addq %rax, %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x48(%rax)
movq -0x8(%rbp), %rdi
movl -0x2c(%rbp), %esi
movq -0x8(%rbp), %rax
movq 0xa0(%rax), %rdx
movq -0x8(%rbp), %rax
movq 0xa0(%rax), %rcx
movq -0x8(%rbp), %rax
movl 0xa8(%rax), %eax
addq %rax, %rcx
callq 0x7fcb0
testb $0x1, %al
jne 0x7f70d
leaq 0x2db7b(%rip), %rdi     # 0xad270
leaq 0x3428e(%rip), %rsi     # 0xb398a
movl $0x1f8, %edx            # imm = 0x1F8
leaq 0x34371(%rip), %rcx     # 0xb3a79
callq 0x4270
movq -0x8(%rbp), %rdi
movb -0x12(%rbp), %al
leaq -0x34(%rbp), %rdx
andb $0x1, %al
movzbl %al, %esi
callq 0x7fd00
testb $0x1, %al
jne 0x7f745
leaq 0x2db43(%rip), %rdi     # 0xad270
leaq 0x34256(%rip), %rsi     # 0xb398a
movl $0x1fa, %edx            # imm = 0x1FA
leaq 0x34339(%rip), %rcx     # 0xb3a79
callq 0x4270
movl -0x34(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jb 0x7f7ff
movl -0x34(%rbp), %eax
subl -0x2c(%rbp), %eax
movl %eax, -0x38(%rbp)
cmpl $0x0, -0x38(%rbp)
jbe 0x7f7fd
movl -0x38(%rbp), %edx
movq -0x8(%rbp), %rax
movq 0x98(%rax), %rcx
movl %edx, %edx
movl %edx, %esi
xorl %edx, %edx
subq %rsi, %rdx
addq %rdx, %rcx
movq %rcx, 0x98(%rax)
movl -0x38(%rbp), %edx
movq -0x8(%rbp), %rax
movq 0x50(%rax), %rcx
movl %edx, %edx
movl %edx, %esi
xorl %edx, %edx
subq %rsi, %rdx
addq %rdx, %rcx
movq %rcx, 0x50(%rax)
movl -0x38(%rbp), %edx
movq -0x8(%rbp), %rax
movq 0x48(%rax), %rcx
movl %edx, %edx
movl %edx, %esi
xorl %edx, %edx
subq %rsi, %rdx
addq %rdx, %rcx
movq %rcx, 0x48(%rax)
movl -0x38(%rbp), %edx
movq -0x8(%rbp), %rax
movl 0xa8(%rax), %ecx
subl %edx, %ecx
movl %ecx, 0xa8(%rax)
movq -0x8(%rbp), %rax
movq 0xa0(%rax), %rdi
movq -0x8(%rbp), %rax
movq 0xa0(%rax), %rsi
movl -0x38(%rbp), %eax
addq %rax, %rsi
movq -0x8(%rbp), %rax
movl 0xa8(%rax), %eax
movl %eax, %edx
callq 0x45c0
jmp 0x7f81e
movq -0x8(%rbp), %rdi
callq 0x7f430
movl -0x34(%rbp), %eax
movl %eax, -0x2c(%rbp)
jmp 0x7f810
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x7f615
movq -0x8(%rbp), %rax
cmpq $0x0, 0xa0(%rax)
jne 0x7f84b
leaq 0x2da3d(%rip), %rdi     # 0xad270
leaq 0x34150(%rip), %rsi     # 0xb398a
movl $0x20a, %edx            # imm = 0x20A
leaq 0x34233(%rip), %rcx     # 0xb3a79
callq 0x4270
movb -0x11(%rbp), %cl
movq -0x8(%rbp), %rax
andb $0x1, %cl
movb %cl, 0x89(%rax)
movb -0x12(%rbp), %cl
movq -0x8(%rbp), %rax
andb $0x1, %cl
movb %cl, 0x8a(%rax)
movb $0x1, %al
andb $0x1, %al
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)