// File: /workspace/github_c_makefile_star2_O1/bloomsource[P]dns_update/util.c
// Function: unpack_update_pkg
int unpack_update_pkg( char* passwd, int time_diff, char* pkg, int pkglen )
{
    aes_context ctx;
    
    upd_pack_hdr *hdr;
    int offset = 0;
    int left = pkglen;
    int proc_len;    
    char tmp[1024];
    unsigned char aes_key[32];
    unsigned char iv[16];
    unsigned int  checksum;
    unsigned int  curtime;
    unsigned int  timestamp;
    int iv_off = 0;
    
    
    
    if( pkglen < sizeof(upd_pack_hdr) + 1 )
    {
        write_log( "[WRN] update pack length too small!" );
        return -1;
    }
    
    crypto_derive_key( passwd, aes_key, 32 );
    md5( (unsigned char*)passwd, strlen( passwd ), iv );
    aes_setkey_enc( &ctx, aes_key, 256 );
    
    while( left )
    {
        proc_len = left > sizeof(tmp) ? sizeof(tmp) : left;
        aes_crypt_cfb128( &ctx, AES_DECRYPT, proc_len, &iv_off, iv, (unsigned char*)pkg + offset, (unsigned char*)tmp );
        
        memcpy( pkg + offset, tmp, proc_len );
        
        left -= proc_len;
        offset += proc_len;
    }
    
    
    
    hdr = (upd_pack_hdr*)pkg;
    
    curtime = time( NULL );
    timestamp = ntohl( hdr->time_stamp );
    
    checksum = ntohl( hdr->checksum );
    hdr->checksum = 0;
    
    if( checksum != crc32( 0, pkg, pkglen ) )
    {
        write_log( "[WRN] update pack checksum incorrected!" );
        return -1;
    }
    
    if( abs( curtime - timestamp ) > time_diff )
    {
        write_log( "[WRN] update pack timestamp out of range!" );
        return -1;
    }
    
    return 0;
    
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x688, %rsp            # imm = 0x688
movl $0x0, 0x14(%rsp)
cmpl $0xd, %ecx
jae 0x66cc
leaq 0x1dd6(%rip), %rdi      # 0x849d
jmp 0x67c9
movl %ecx, %ebp
movq %rdx, %r14
movq %rdi, %rbx
movl %esi, 0x10(%rsp)
movslq %ecx, %rax
movq %rax, 0x18(%rsp)
leaq 0x30(%rsp), %r15
movq %r15, %rsi
movl $0x20, %edx
callq 0x648c
movq %rbx, %rdi
callq 0x20f0
leaq 0x20(%rsp), %rdx
movq %rbx, %rdi
movl %eax, %esi
callq 0x35ab
leaq 0x50(%rsp), %rdi
movq %r15, %rsi
movl $0x100, %edx            # imm = 0x100
callq 0x67ec
xorl %ebx, %ebx
leaq 0x280(%rsp), %r15
movl $0x400, %eax            # imm = 0x400
cmpl %eax, %ebp
movl $0x400, %r12d           # imm = 0x400
cmovbl %ebp, %r12d
movl %ebx, %r13d
addq %r14, %r13
movq %r15, (%rsp)
leaq 0x50(%rsp), %rdi
xorl %esi, %esi
movl %r12d, %edx
leaq 0x14(%rsp), %rcx
leaq 0x20(%rsp), %r8
movq %r13, %r9
callq 0x751b
movq %r13, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x21c0
addl %r12d, %ebx
subl %r12d, %ebp
jne 0x6725
xorl %ebx, %ebx
xorl %edi, %edi
callq 0x21d0
movq %rax, %r15
movl 0x4(%r14), %r12d
movl 0x8(%r14), %ebp
bswapl %r12d
movl $0x0, 0x4(%r14)
xorl %edi, %edi
movq %r14, %rsi
movq 0x18(%rsp), %rdx
callq 0x75ec
cmpl %eax, %r12d
jne 0x67c2
bswapl %ebp
subl %ebp, %r15d
movl %r15d, %eax
negl %eax
cmovsl %r15d, %eax
cmpl 0x10(%rsp), %eax
jle 0x67d5
leaq 0x1d29(%rip), %rdi      # 0x84e9
jmp 0x67c9
leaq 0x1cf8(%rip), %rdi      # 0x84c1
xorl %eax, %eax
callq 0x5fec
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movl %ebx, %eax
addq $0x688, %rsp            # imm = 0x688
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)