// File: /workspace/github_c_makefile_star2_O3/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 $0xc, %ecx
ja 0x6640
leaq 0x1e62(%rip), %rdi      # 0x849d
jmp 0x673b
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 0x6400
movq %rbx, %rdi
callq 0x20f0
leaq 0x20(%rsp), %rdx
movq %rbx, %rdi
movl %eax, %esi
callq 0x3595
leaq 0x50(%rsp), %rdi
movq %r15, %rsi
movl $0x100, %edx            # imm = 0x100
callq 0x6760
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 0x748b
movq %r13, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x21b0
addl %r12d, %ebx
subl %r12d, %ebp
jne 0x6699
xorl %r12d, %r12d
xorl %edi, %edi
callq 0x21c0
movq %rax, %rbx
movl 0x4(%r14), %r15d
movl 0x8(%r14), %ebp
bswapl %r15d
movl $0x0, 0x4(%r14)
xorl %edi, %edi
movq %r14, %rsi
movq 0x18(%rsp), %rdx
callq 0x755c
cmpl %eax, %r15d
jne 0x6734
bswapl %ebp
subl %ebp, %ebx
movl %ebx, %eax
negl %eax
cmovsl %ebx, %eax
cmpl 0x10(%rsp), %eax
jle 0x6748
leaq 0x1db7(%rip), %rdi      # 0x84e9
jmp 0x673b
leaq 0x1d86(%rip), %rdi      # 0x84c1
xorl %eax, %eax
callq 0x5f60
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movl %r12d, %eax
addq $0x688, %rsp            # imm = 0x688
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)