// File: sisong[P]ApkDiffPatch/HDiffPatch/libHDiffPatch/HDiff/diff.cpp
// Function: resave_compressed_diff(hpatch_TStreamInput const*, hpatch_TDecompress*, hpatch_TStreamOutput const*, hdiff_TCompress const*, unsigned long long)
void resave_compressed_diff(const hpatch_TStreamInput*  in_diff,
                            hpatch_TDecompress*         decompressPlugin,
                            const hpatch_TStreamOutput* out_diff,
                            const hdiff_TCompress*      compressPlugin,
                            hpatch_StreamPos_t          out_diff_curPos){
    _THDiffzHead              head;
    hpatch_compressedDiffInfo diffInfo;
    assert(in_diff!=0);
    assert(in_diff->read!=0);
    assert(out_diff!=0);
    assert(out_diff->write!=0);
    
    {//read head
        checki(read_diffz_head(&diffInfo,&head,in_diff),
               "resave_compressed_diff() read_diffz_head() error!");
        checki((decompressPlugin!=0)||(diffInfo.compressedCount<=0),
               "resave_compressed_diff() decompressPlugin null error!");
        if ((decompressPlugin)&&(diffInfo.compressedCount>0)){
            checki(decompressPlugin->is_can_open(diffInfo.compressType),
                   "resave_compressed_diff() decompressPlugin cannot open compressed data error!");
        }
    }
    
    TDiffStream outDiff(out_diff,out_diff_curPos);
    {//type
        std::vector<TByte> out_type;
        _outType(out_type,compressPlugin);
        outDiff.pushBack(out_type.data(),out_type.size());
    }
    {//copy other
        TStreamClip clip(in_diff,head.typesEndPos,head.compressSizeBeginPos);
        outDiff.pushStream(&clip);
    }
    outDiff.packUInt(head.cover_buf_size);
    TPlaceholder compress_cover_buf_sizePos=
        outDiff.packUInt_pos(compressPlugin?head.cover_buf_size:0);//compress_cover_buf size
    outDiff.packUInt(head.rle_ctrlBuf_size);//rle_ctrlBuf size
    TPlaceholder compress_rle_ctrlBuf_sizePos=
        outDiff.packUInt_pos(compressPlugin?head.rle_ctrlBuf_size:0);//compress_rle_ctrlBuf size
    outDiff.packUInt(head.rle_codeBuf_size);//rle_codeBuf size
    TPlaceholder compress_rle_codeBuf_sizePos=
        outDiff.packUInt_pos(compressPlugin?head.rle_codeBuf_size:0);//compress_rle_codeBuf size
    outDiff.packUInt(head.newDataDiff_size);
    TPlaceholder compress_newDataDiff_sizePos=
        outDiff.packUInt_pos(compressPlugin?head.newDataDiff_size:0);//compress_newDataDiff size
    
    {//save covers
        TStreamClip clip(in_diff,head.headEndPos,head.coverEndPos,
                         (head.compress_cover_buf_size>0)?decompressPlugin:0,head.cover_buf_size);
        outDiff.pushStream(&clip,compressPlugin,compress_cover_buf_sizePos);
    }
    hpatch_StreamPos_t diffPos0=head.coverEndPos;
    {//save rle ctrl
        bool isCompressed=(head.compress_rle_ctrlBuf_size>0);
        hpatch_StreamPos_t bufSize=isCompressed?head.compress_rle_ctrlBuf_size:head.rle_ctrlBuf_size;
        TStreamClip clip(in_diff,diffPos0,diffPos0+bufSize,
                         isCompressed?decompressPlugin:0,head.rle_ctrlBuf_size);
        outDiff.pushStream(&clip,compressPlugin,compress_rle_ctrlBuf_sizePos);
        diffPos0+=bufSize;
    }
    {//save rle code
        bool isCompressed=(head.compress_rle_codeBuf_size>0);
        hpatch_StreamPos_t bufSize=isCompressed?head.compress_rle_codeBuf_size:head.rle_codeBuf_size;
        TStreamClip clip(in_diff,diffPos0,diffPos0+bufSize,
                         isCompressed?decompressPlugin:0,head.rle_codeBuf_size);
        outDiff.pushStream(&clip,compressPlugin,compress_rle_codeBuf_sizePos);
        diffPos0+=bufSize;
    }
    {//save newDataDiff
        bool isCompressed=(head.compress_newDataDiff_size>0);
        hpatch_StreamPos_t bufSize=isCompressed?head.compress_newDataDiff_size:head.newDataDiff_size;
        TStreamClip clip(in_diff,diffPos0,diffPos0+bufSize,
                         isCompressed?decompressPlugin:0,head.newDataDiff_size);
        outDiff.pushStream(&clip,compressPlugin,compress_newDataDiff_sizePos);
        diffPos0+=bufSize;
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x530, %rsp            # imm = 0x530
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
cmpq $0x0, -0x8(%rbp)
je 0x1b9b8
jmp 0x1b9d7
leaq 0x9347d(%rip), %rdi     # 0xaee3c
leaq 0x933bc(%rip), %rsi     # 0xaed82
movl $0x563, %edx            # imm = 0x563
leaq 0x93475(%rip), %rcx     # 0xaee47
callq 0x4270
movq -0x8(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x1b9e4
jmp 0x1ba03
leaq 0x934f6(%rip), %rdi     # 0xaeee1
leaq 0x93390(%rip), %rsi     # 0xaed82
movl $0x564, %edx            # imm = 0x564
leaq 0x93449(%rip), %rcx     # 0xaee47
callq 0x4270
cmpq $0x0, -0x18(%rbp)
je 0x1ba0c
jmp 0x1ba2b
leaq 0x934df(%rip), %rdi     # 0xaeef2
leaq 0x93368(%rip), %rsi     # 0xaed82
movl $0x565, %edx            # imm = 0x565
leaq 0x93421(%rip), %rcx     # 0xaee47
callq 0x4270
movq -0x18(%rbp), %rax
cmpq $0x0, 0x18(%rax)
je 0x1ba38
jmp 0x1ba57
leaq 0x934bf(%rip), %rdi     # 0xaeefe
leaq 0x9333c(%rip), %rsi     # 0xaed82
movl $0x566, %edx            # imm = 0x566
leaq 0x933f5(%rip), %rcx     # 0xaee47
callq 0x4270
movq -0x8(%rbp), %rdx
leaq -0x1a8(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x87b50
cmpl $0x0, %eax
jne 0x1bad5
movl $0x10, %edi
callq 0x41e0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x408(%rbp)
leaq 0x93480(%rip), %rsi     # 0xaef11
callq 0x4140
jmp 0x1ba98
movq -0x408(%rbp), %rdi
movq 0xca54a(%rip), %rsi     # 0xe5ff0
movq 0xca513(%rip), %rdx     # 0xe5fc0
callq 0x4630
movq -0x408(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1b0(%rbp)
movl %eax, -0x1b4(%rbp)
callq 0x42e0
jmp 0x1c31b
cmpq $0x0, -0x10(%rbp)
jne 0x1bb47
cmpl $0x0, -0x198(%rbp)
jbe 0x1bb47
movl $0x10, %edi
callq 0x41e0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x410(%rbp)
leaq 0x93440(%rip), %rsi     # 0xaef43
callq 0x4140
jmp 0x1bb0a
movq -0x410(%rbp), %rdi
movq 0xca4d8(%rip), %rsi     # 0xe5ff0
movq 0xca4a1(%rip), %rdx     # 0xe5fc0
callq 0x4630
movq -0x410(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1b0(%rbp)
movl %eax, -0x1b4(%rbp)
callq 0x42e0
jmp 0x1c31b
cmpq $0x0, -0x10(%rbp)
je 0x1bbd8
cmpl $0x0, -0x198(%rbp)
jbe 0x1bbd8
movq -0x10(%rbp), %rax
movq (%rax), %rax
leaq -0x1a8(%rbp), %rdi
addq $0x14, %rdi
callq *%rax
cmpl $0x0, %eax
jne 0x1bbd6
movl $0x10, %edi
callq 0x41e0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x418(%rbp)
leaq 0x933e7(%rip), %rsi     # 0xaef79
callq 0x4140
jmp 0x1bb99
movq -0x418(%rbp), %rdi
movq 0xca449(%rip), %rsi     # 0xe5ff0
movq 0xca412(%rip), %rdx     # 0xe5fc0
callq 0x4630
movq -0x418(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1b0(%rbp)
movl %eax, -0x1b4(%rbp)
callq 0x42e0
jmp 0x1c31b
jmp 0x1bbd8
movq -0x18(%rbp), %rsi
movq -0x28(%rbp), %rdx
leaq -0x1e0(%rbp), %rdi
callq 0x4f3c0
leaq -0x1f8(%rbp), %rdi
movq %rdi, -0x420(%rbp)
callq 0xbe70
movq -0x420(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq 0xca967(%rip), %rdx     # 0xe6578
callq 0x1c330
jmp 0x1bc18
leaq -0x1f8(%rbp), %rdi
movq %rdi, -0x430(%rbp)
callq 0xbb60
movq -0x430(%rbp), %rdi
movq %rax, -0x428(%rbp)
callq 0xbe90
movq -0x428(%rbp), %rsi
movq %rax, %rdx
leaq -0x1e0(%rbp), %rdi
callq 0x4f450
jmp 0x1bc56
leaq -0x1f8(%rbp), %rdi
callq 0xbb80
movq -0x8(%rbp), %rsi
movq -0x48(%rbp), %rdx
movq -0x40(%rbp), %rcx
xorl %eax, %eax
movl %eax, %r9d
leaq -0x248(%rbp), %rdi
movq %r9, %r8
callq 0x29e00
jmp 0x1bc84
leaq -0x1e0(%rbp), %rdi
leaq -0x248(%rbp), %rsi
callq 0x29e50
jmp 0x1bc99
leaq -0x248(%rbp), %rdi
callq 0x29e80
movq -0x88(%rbp), %rsi
leaq -0x1e0(%rbp), %rdi
callq 0x4f510
jmp 0x1bcba
cmpq $0x0, -0x20(%rbp)
je 0x1bcd1
movq -0x88(%rbp), %rax
movq %rax, -0x438(%rbp)
jmp 0x1bcdc
xorl %eax, %eax
movq %rax, -0x438(%rbp)
jmp 0x1bcdc
movq -0x438(%rbp), %rsi
leaq -0x1e0(%rbp), %rdi
callq 0x29eb0
movq %rdx, -0x448(%rbp)
movq %rax, -0x440(%rbp)
jmp 0x1bcff
movq -0x448(%rbp), %rax
movq -0x440(%rbp), %rcx
movq %rcx, -0x258(%rbp)
movq %rax, -0x250(%rbp)
movq -0x78(%rbp), %rsi
leaq -0x1e0(%rbp), %rdi
callq 0x4f510
jmp 0x1bd2d
cmpq $0x0, -0x20(%rbp)
je 0x1bd41
movq -0x78(%rbp), %rax
movq %rax, -0x450(%rbp)
jmp 0x1bd4c
xorl %eax, %eax
movq %rax, -0x450(%rbp)
jmp 0x1bd4c
movq -0x450(%rbp), %rsi
leaq -0x1e0(%rbp), %rdi
callq 0x29eb0
movq %rdx, -0x460(%rbp)
movq %rax, -0x458(%rbp)
jmp 0x1bd6f
movq -0x460(%rbp), %rax
movq -0x458(%rbp), %rcx
movq %rcx, -0x268(%rbp)
movq %rax, -0x260(%rbp)
movq -0x68(%rbp), %rsi
leaq -0x1e0(%rbp), %rdi
callq 0x4f510
jmp 0x1bd9d
cmpq $0x0, -0x20(%rbp)
je 0x1bdb1
movq -0x68(%rbp), %rax
movq %rax, -0x468(%rbp)
jmp 0x1bdbc
xorl %eax, %eax
movq %rax, -0x468(%rbp)
jmp 0x1bdbc
movq -0x468(%rbp), %rsi
leaq -0x1e0(%rbp), %rdi
callq 0x29eb0
movq %rdx, -0x478(%rbp)
movq %rax, -0x470(%rbp)
jmp 0x1bddf
movq -0x478(%rbp), %rax
movq -0x470(%rbp), %rcx
movq %rcx, -0x278(%rbp)
movq %rax, -0x270(%rbp)
movq -0x58(%rbp), %rsi
leaq -0x1e0(%rbp), %rdi
callq 0x4f510
jmp 0x1be0d
cmpq $0x0, -0x20(%rbp)
je 0x1be21
movq -0x58(%rbp), %rax
movq %rax, -0x480(%rbp)
jmp 0x1be2c
xorl %eax, %eax
movq %rax, -0x480(%rbp)
jmp 0x1be2c
movq -0x480(%rbp), %rsi
leaq -0x1e0(%rbp), %rdi
callq 0x29eb0
movq %rdx, -0x490(%rbp)
movq %rax, -0x488(%rbp)
jmp 0x1be4f
movq -0x490(%rbp), %rax
movq -0x488(%rbp), %rcx
movq %rcx, -0x288(%rbp)
movq %rax, -0x280(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x4a8(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x4a0(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x498(%rbp)
cmpq $0x0, -0x80(%rbp)
jbe 0x1bea0
movq -0x10(%rbp), %rax
movq %rax, -0x4b0(%rbp)
jmp 0x1beab
xorl %eax, %eax
movq %rax, -0x4b0(%rbp)
jmp 0x1beab
movq -0x498(%rbp), %rcx
movq -0x4a0(%rbp), %rdx
movq -0x4a8(%rbp), %rsi
movq -0x4b0(%rbp), %r8
movq -0x88(%rbp), %r9
leaq -0x2d8(%rbp), %rdi
callq 0x29e00
jmp 0x1bedc
movq -0x20(%rbp), %rdx
xorl %r8d, %r8d
movl %r8d, %r9d
leaq -0x1e0(%rbp), %rdi
leaq -0x2d8(%rbp), %rsi
leaq -0x258(%rbp), %rcx
callq 0x4fd10
jmp 0x1bf02
leaq -0x2d8(%rbp), %rdi
callq 0x29e80
movq -0x30(%rbp), %rax
movq %rax, -0x2e0(%rbp)
cmpq $0x0, -0x70(%rbp)
seta %al
andb $0x1, %al
movb %al, -0x2e1(%rbp)
testb $0x1, -0x2e1(%rbp)
je 0x1bf3f
movq -0x70(%rbp), %rax
movq %rax, -0x4b8(%rbp)
jmp 0x1bf4a
movq -0x78(%rbp), %rax
movq %rax, -0x4b8(%rbp)
movq -0x4b8(%rbp), %rax
movq %rax, -0x2f0(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x4d0(%rbp)
movq -0x2e0(%rbp), %rax
movq %rax, -0x4c8(%rbp)
movq -0x2e0(%rbp), %rax
addq -0x2f0(%rbp), %rax
movq %rax, -0x4c0(%rbp)
testb $0x1, -0x2e1(%rbp)
je 0x1bf9c
movq -0x10(%rbp), %rax
movq %rax, -0x4d8(%rbp)
jmp 0x1bfa7
xorl %eax, %eax
movq %rax, -0x4d8(%rbp)
jmp 0x1bfa7
movq -0x4c0(%rbp), %rcx
movq -0x4c8(%rbp), %rdx
movq -0x4d0(%rbp), %rsi
movq -0x4d8(%rbp), %r8
movq -0x78(%rbp), %r9
leaq -0x340(%rbp), %rdi
callq 0x29e00
jmp 0x1bfd5
movq -0x20(%rbp), %rdx
xorl %r8d, %r8d
movl %r8d, %r9d
leaq -0x1e0(%rbp), %rdi
leaq -0x340(%rbp), %rsi
leaq -0x268(%rbp), %rcx
callq 0x4fd10
jmp 0x1bffb
movq -0x2f0(%rbp), %rax
addq -0x2e0(%rbp), %rax
movq %rax, -0x2e0(%rbp)
leaq -0x340(%rbp), %rdi
callq 0x29e80
cmpq $0x0, -0x60(%rbp)
seta %al
andb $0x1, %al
movb %al, -0x341(%rbp)
testb $0x1, -0x341(%rbp)
je 0x1c042
movq -0x60(%rbp), %rax
movq %rax, -0x4e0(%rbp)
jmp 0x1c04d
movq -0x68(%rbp), %rax
movq %rax, -0x4e0(%rbp)
movq -0x4e0(%rbp), %rax
movq %rax, -0x350(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x4f8(%rbp)
movq -0x2e0(%rbp), %rax
movq %rax, -0x4f0(%rbp)
movq -0x2e0(%rbp), %rax
addq -0x350(%rbp), %rax
movq %rax, -0x4e8(%rbp)
testb $0x1, -0x341(%rbp)
je 0x1c09f
movq -0x10(%rbp), %rax
movq %rax, -0x500(%rbp)
jmp 0x1c0aa
xorl %eax, %eax
movq %rax, -0x500(%rbp)
jmp 0x1c0aa
movq -0x4e8(%rbp), %rcx
movq -0x4f0(%rbp), %rdx
movq -0x4f8(%rbp), %rsi
movq -0x500(%rbp), %r8
movq -0x68(%rbp), %r9
leaq -0x3a0(%rbp), %rdi
callq 0x29e00
jmp 0x1c0d8
movq -0x20(%rbp), %rdx
xorl %r8d, %r8d
movl %r8d, %r9d
leaq -0x1e0(%rbp), %rdi
leaq -0x3a0(%rbp), %rsi
leaq -0x278(%rbp), %rcx
callq 0x4fd10
jmp 0x1c0fe
movq -0x350(%rbp), %rax
addq -0x2e0(%rbp), %rax
movq %rax, -0x2e0(%rbp)
leaq -0x3a0(%rbp), %rdi
callq 0x29e80
cmpq $0x0, -0x50(%rbp)
seta %al
andb $0x1, %al
movb %al, -0x3a1(%rbp)
testb $0x1, -0x3a1(%rbp)
je 0x1c145
movq -0x50(%rbp), %rax
movq %rax, -0x508(%rbp)
jmp 0x1c150
movq -0x58(%rbp), %rax
movq %rax, -0x508(%rbp)
movq -0x508(%rbp), %rax
movq %rax, -0x3b0(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x520(%rbp)
movq -0x2e0(%rbp), %rax
movq %rax, -0x518(%rbp)
movq -0x2e0(%rbp), %rax
addq -0x3b0(%rbp), %rax
movq %rax, -0x510(%rbp)
testb $0x1, -0x3a1(%rbp)
je 0x1c1a2
movq -0x10(%rbp), %rax
movq %rax, -0x528(%rbp)
jmp 0x1c1ad
xorl %eax, %eax
movq %rax, -0x528(%rbp)
jmp 0x1c1ad
movq -0x510(%rbp), %rcx
movq -0x518(%rbp), %rdx
movq -0x520(%rbp), %rsi
movq -0x528(%rbp), %r8
movq -0x58(%rbp), %r9
leaq -0x400(%rbp), %rdi
callq 0x29e00
jmp 0x1c1db
movq -0x20(%rbp), %rdx
xorl %r8d, %r8d
movl %r8d, %r9d
leaq -0x1e0(%rbp), %rdi
leaq -0x400(%rbp), %rsi
leaq -0x288(%rbp), %rcx
callq 0x4fd10
jmp 0x1c201
movq -0x3b0(%rbp), %rax
addq -0x2e0(%rbp), %rax
movq %rax, -0x2e0(%rbp)
leaq -0x400(%rbp), %rdi
callq 0x29e80
leaq -0x1e0(%rbp), %rdi
callq 0x4f430
addq $0x530, %rsp            # imm = 0x530
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1b0(%rbp)
movl %eax, -0x1b4(%rbp)
leaq -0x1f8(%rbp), %rdi
callq 0xbb80
jmp 0x1c30f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1b0(%rbp)
movl %eax, -0x1b4(%rbp)
jmp 0x1c30f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1b0(%rbp)
movl %eax, -0x1b4(%rbp)
leaq -0x248(%rbp), %rdi
callq 0x29e80
jmp 0x1c30f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1b0(%rbp)
movl %eax, -0x1b4(%rbp)
leaq -0x2d8(%rbp), %rdi
callq 0x29e80
jmp 0x1c30f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1b0(%rbp)
movl %eax, -0x1b4(%rbp)
leaq -0x340(%rbp), %rdi
callq 0x29e80
jmp 0x1c30f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1b0(%rbp)
movl %eax, -0x1b4(%rbp)
leaq -0x3a0(%rbp), %rdi
callq 0x29e80
jmp 0x1c30f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1b0(%rbp)
movl %eax, -0x1b4(%rbp)
leaq -0x400(%rbp), %rdi
callq 0x29e80
leaq -0x1e0(%rbp), %rdi
callq 0x4f430
movq -0x1b0(%rbp), %rdi
callq 0x4680
nopw (%rax,%rax)