// File: /workspace/github_c_makefile_star2_O2/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
andl $0x0, 0x14(%rsp)
cmpl $0xc, %ecx
ja 0x5efa
leaq 0x15a8(%rip), %rdi      # 0x749d
jmp 0x5ff7
movl %ecx, %ebp
movq %rdx, %r14
movq %rdi, %rbx
movl %esi, 0x10(%rsp)
movslq %ecx, %rax
movq %rax, 0x18(%rsp)
leaq 0x30(%rsp), %r15
pushq $0x20
popq %rdx
movq %r15, %rsi
callq 0x5d08
movq %rbx, %rdi
callq 0x20f0
leaq 0x20(%rsp), %rdx
movq %rbx, %rdi
movl %eax, %esi
callq 0x3577
leaq 0x50(%rsp), %rdi
movq %r15, %rsi
movl $0x100, %edx            # imm = 0x100
callq 0x6018
xorl %ebx, %ebx
leaq 0x280(%rsp), %r15
testl %ebp, %ebp
je 0x5fa5
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 0x6c61
movq %r13, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x21b0
subl %r12d, %ebp
addl %ebx, %r12d
movl %r12d, %ebx
jmp 0x5f51
xorl %r12d, %r12d
xorl %edi, %edi
callq 0x21c0
movq %rax, %rbx
movl 0x4(%r14), %r15d
movl 0x8(%r14), %ebp
bswapl %r15d
andl $0x0, 0x4(%r14)
xorl %edi, %edi
movq %r14, %rsi
movq 0x18(%rsp), %rdx
callq 0x6d24
cmpl %eax, %r15d
jne 0x5ff0
bswapl %ebp
subl %ebp, %ebx
movl %ebx, %eax
negl %eax
cmovsl %ebx, %eax
cmpl 0x10(%rsp), %eax
jle 0x6002
leaq 0x14fb(%rip), %rdi      # 0x74e9
jmp 0x5ff7
leaq 0x14ca(%rip), %rdi      # 0x74c1
xorl %eax, %eax
callq 0x58d1
pushq $-0x1
popq %r12
movl %r12d, %eax
addq $0x688, %rsp            # imm = 0x688
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop