// File: sisong[P]ApkDiffPatch/src/patch/Zipper.cpp
// Function: UnZipper_searchApkV2Sign
bool UnZipper_searchApkV2Sign(const hpatch_TStreamInput* stream,hpatch_StreamPos_t centralDirectory_pos,
                              ZipFilePos_t* v2sign_topPos,ZipFilePos_t* out_blockSize,bool* out_isHaveV3Sign){
    *v2sign_topPos=(ZipFilePos_t)centralDirectory_pos; //default not found
    if (out_blockSize) *out_blockSize=0;
    //nodePos_begin=v2sign_topPos-8;
    //sumNodeSize=blockSize-8-APKSigningTagLen;
    
    //tag
    const size_t APKSigningTagLen=16;
    const char* APKSigningTag="APK Sig Block 42";
    if (APKSigningTagLen>centralDirectory_pos) return true; //not found
    ZipFilePos_t APKSigningBlockTagPos=(ZipFilePos_t)centralDirectory_pos-APKSigningTagLen;
    TByte buf[APKSigningTagLen];
    check(stream->read(stream,APKSigningBlockTagPos,buf,buf+APKSigningTagLen));
    if (0!=memcmp(buf,APKSigningTag,APKSigningTagLen)) return true;//not found
    //bottom size
    check(8<=APKSigningBlockTagPos);
    ZipFilePos_t blockSizeBottomPos=APKSigningBlockTagPos-8;
    check(stream->read(stream,blockSizeBottomPos,buf,buf+8));
    hpatch_StreamPos_t blockSize=readUInt64(buf);
    //top
    check(blockSize+8<=centralDirectory_pos);
    ZipFilePos_t blockTopSizePos=(ZipFilePos_t)centralDirectory_pos-(ZipFilePos_t)blockSize-8;
    check(stream->read(stream,blockTopSizePos,buf,buf+8));
    check(blockSize==readUInt64(buf)); //check top size
    
    *v2sign_topPos=blockTopSizePos;
    if (out_blockSize) *out_blockSize=(ZipFilePos_t)blockSize;
    if (out_isHaveV3Sign){
        const hpatch_uint32_t kV3SignID=0xf05368c0;
        ZipFilePos_t IDNodePos;
        ZipFilePos_t IDNodeSize;
        if (!_searchID_in_block(stream,blockTopSizePos+8,(ZipFilePos_t)blockSize-8-APKSigningTagLen,
                                kV3SignID,&IDNodePos,&IDNodeSize)) return false; //error
        *out_isHaveV3Sign=(IDNodeSize>0);
    }
    return true;//found
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq -0x18(%rbp), %rax
movl %eax, %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
cmpq $0x0, -0x28(%rbp)
je 0x7e62c
movq -0x28(%rbp), %rax
movl $0x0, (%rax)
movq $0x10, -0x38(%rbp)
leaq 0x3533e(%rip), %rax     # 0xb3979
movq %rax, -0x40(%rbp)
movl $0x10, %eax
cmpq -0x18(%rbp), %rax
jbe 0x7e653
movb $0x1, -0x1(%rbp)
jmp 0x7e87d
movq -0x18(%rbp), %rax
movl %eax, %eax
subq $0x10, %rax
movl %eax, -0x44(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x10(%rbp), %rdi
movl -0x44(%rbp), %ecx
movl %ecx, %esi
leaq -0x60(%rbp), %rdx
leaq -0x60(%rbp), %rcx
addq $0x10, %rcx
callq *%rax
cmpl $0x0, %eax
jne 0x7e6a3
leaq 0x2ebe5(%rip), %rdi     # 0xad270
leaq 0x352f8(%rip), %rsi     # 0xb398a
movl $0xb3, %edx
leaq 0x35301(%rip), %rcx     # 0xb399f
callq 0x4270
leaq -0x60(%rbp), %rdi
movq -0x40(%rbp), %rsi
movl $0x10, %edx
callq 0x4190
movl %eax, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
je 0x7e6c6
movb $0x1, -0x1(%rbp)
jmp 0x7e87d
movl $0x8, %eax
cmpl -0x44(%rbp), %eax
jbe 0x7e6ef
leaq 0x2eb99(%rip), %rdi     # 0xad270
leaq 0x352ac(%rip), %rsi     # 0xb398a
movl $0xb6, %edx
leaq 0x352b5(%rip), %rcx     # 0xb399f
callq 0x4270
movl -0x44(%rbp), %eax
subl $0x8, %eax
movl %eax, -0x64(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x10(%rbp), %rdi
movl -0x64(%rbp), %ecx
movl %ecx, %esi
leaq -0x60(%rbp), %rdx
leaq -0x60(%rbp), %rcx
addq $0x8, %rcx
callq *%rax
cmpl $0x0, %eax
jne 0x7e73b
leaq 0x2eb4d(%rip), %rdi     # 0xad270
leaq 0x35260(%rip), %rsi     # 0xb398a
movl $0xb8, %edx
leaq 0x35269(%rip), %rcx     # 0xb399f
callq 0x4270
leaq -0x60(%rbp), %rdi
callq 0x7e890
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
addq $0x8, %rax
cmpq -0x18(%rbp), %rax
jbe 0x7e775
leaq 0x2eb13(%rip), %rdi     # 0xad270
leaq 0x35226(%rip), %rsi     # 0xb398a
movl $0xbb, %edx
leaq 0x3522f(%rip), %rcx     # 0xb399f
callq 0x4270
movq -0x18(%rbp), %rax
movq -0x70(%rbp), %rcx
subl %ecx, %eax
subl $0x8, %eax
movl %eax, -0x74(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x10(%rbp), %rdi
movl -0x74(%rbp), %ecx
movl %ecx, %esi
leaq -0x60(%rbp), %rdx
leaq -0x60(%rbp), %rcx
addq $0x8, %rcx
callq *%rax
cmpl $0x0, %eax
jne 0x7e7c8
leaq 0x2eac0(%rip), %rdi     # 0xad270
leaq 0x351d3(%rip), %rsi     # 0xb398a
movl $0xbd, %edx
leaq 0x351dc(%rip), %rcx     # 0xb399f
callq 0x4270
movq -0x70(%rbp), %rax
movq %rax, -0x88(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x7e890
movq %rax, %rcx
movq -0x88(%rbp), %rax
cmpq %rcx, %rax
je 0x7e80a
leaq 0x2ea7e(%rip), %rdi     # 0xad270
leaq 0x35191(%rip), %rsi     # 0xb398a
movl $0xbe, %edx
leaq 0x3519a(%rip), %rcx     # 0xb399f
callq 0x4270
movl -0x74(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
cmpq $0x0, -0x28(%rbp)
je 0x7e826
movq -0x70(%rbp), %rax
movl %eax, %ecx
movq -0x28(%rbp), %rax
movl %ecx, (%rax)
cmpq $0x0, -0x30(%rbp)
je 0x7e879
movl $0xf05368c0, -0x78(%rbp) # imm = 0xF05368C0
movq -0x10(%rbp), %rdi
movl -0x74(%rbp), %esi
addl $0x8, %esi
movq -0x70(%rbp), %rax
subl $0x8, %eax
movl %eax, %eax
subq $0x10, %rax
movl %eax, %edx
movl $0xf05368c0, %ecx       # imm = 0xF05368C0
leaq -0x7c(%rbp), %r8
leaq -0x80(%rbp), %r9
callq 0x7e8d0
testb $0x1, %al
jne 0x7e869
movb $0x0, -0x1(%rbp)
jmp 0x7e87d
cmpl $0x0, -0x80(%rbp)
seta %cl
movq -0x30(%rbp), %rax
andb $0x1, %cl
movb %cl, (%rax)
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x90, %rsp
popq %rbp
retq
nopl (%rax,%rax)