// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x6d0, %rsp            # imm = 0x6D0
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
movl $0x0, -0x264(%rbp)
movl -0x24(%rbp), %eax
movl %eax, -0x268(%rbp)
movl $0x0, -0x6b0(%rbp)
movslq -0x24(%rbp), %rax
cmpq $0xd, %rax
jae 0xa17a
leaq 0x3441(%rip), %rdi      # 0xd5a8
movb $0x0, %al
callq 0x9780
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xa362
movq -0x10(%rbp), %rdi
leaq -0x690(%rbp), %rsi
movl $0x20, %edx
callq 0x9df0
movq -0x10(%rbp), %rax
movq %rax, -0x6b8(%rbp)
movq -0x10(%rbp), %rdi
callq 0x20e0
movq -0x6b8(%rbp), %rdi
movl %eax, %esi
leaq -0x6a0(%rbp), %rdx
callq 0x4c70
leaq -0x690(%rbp), %rsi
leaq -0x258(%rbp), %rdi
movl $0x100, %edx            # imm = 0x100
callq 0xa370
cmpl $0x0, -0x268(%rbp)
je 0xa29c
movslq -0x268(%rbp), %rax
cmpq $0x400, %rax            # imm = 0x400
jbe 0xa1fa
movl $0x400, %eax            # imm = 0x400
movq %rax, -0x6c0(%rbp)
jmp 0xa208
movslq -0x268(%rbp), %rax
movq %rax, -0x6c0(%rbp)
movq -0x6c0(%rbp), %rax
movl %eax, -0x26c(%rbp)
movl -0x26c(%rbp), %edx
leaq -0x6a0(%rbp), %r8
movq -0x20(%rbp), %r9
movslq -0x264(%rbp), %rax
addq %rax, %r9
leaq -0x670(%rbp), %rax
leaq -0x258(%rbp), %rdi
xorl %esi, %esi
leaq -0x6b0(%rbp), %rcx
movq %rax, (%rsp)
callq 0xc180
movq -0x20(%rbp), %rdi
movslq -0x264(%rbp), %rax
addq %rax, %rdi
leaq -0x670(%rbp), %rsi
movslq -0x26c(%rbp), %rdx
callq 0x21c0
movl -0x26c(%rbp), %ecx
movl -0x268(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x268(%rbp)
movl -0x26c(%rbp), %eax
addl -0x264(%rbp), %eax
movl %eax, -0x264(%rbp)
jmp 0xa1d0
movq -0x20(%rbp), %rax
movq %rax, -0x260(%rbp)
xorl %eax, %eax
movl %eax, %edi
callq 0x21d0
movl %eax, -0x6a8(%rbp)
movq -0x260(%rbp), %rax
movl 0x8(%rax), %edi
callq 0x22b0
movl %eax, -0x6ac(%rbp)
movq -0x260(%rbp), %rax
movl 0x4(%rax), %edi
callq 0x22b0
movl %eax, -0x6a4(%rbp)
movq -0x260(%rbp), %rax
movl $0x0, 0x4(%rax)
movl -0x6a4(%rbp), %eax
movl %eax, -0x6c4(%rbp)
movq -0x20(%rbp), %rsi
movslq -0x24(%rbp), %rdx
xorl %edi, %edi
callq 0xc2d0
movl %eax, %ecx
movl -0x6c4(%rbp), %eax
cmpl %ecx, %eax
je 0xa32c
leaq 0x32b0(%rip), %rdi      # 0xd5cc
movb $0x0, %al
callq 0x9780
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xa362
movl -0x6a8(%rbp), %eax
subl -0x6ac(%rbp), %eax
movl %eax, %ecx
negl %ecx
cmovnsl %ecx, %eax
cmpl -0x14(%rbp), %eax
jle 0xa35b
leaq 0x32a9(%rip), %rdi      # 0xd5f4
movb $0x0, %al
callq 0x9780
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xa362
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x6d0, %rsp            # imm = 0x6D0
popq %rbp
retq
nop