// File: renzibei[P]TSProxy/aes.cpp
// Function: encrypt(char*, int, char const*, int, char const (&) [16], char (&) [16], char (&) [16])
int encrypt(char *dst, int dst_size, const char *src, int src_size, const char (&key)[16], char (&random)[16], char (&mac)[16]) {
	int valid_size = (src_size >> 4) << 4;
	if (valid_size < src_size)
		valid_size += 16;
	if (valid_size < src_size + 2) {
		valid_size += 16;
	}
	// static char buf[constants::AES_MAX_DATA_LENGTH];
	
	if (valid_size > constants::AES_MAX_DATA_LENGTH) {
		LogHelper::log(Error, "data size should be no longer than: %lu, but size is: %d", constants::AES_MAX_DATA_LENGTH, valid_size);
		return -5;
	}
	
	if (dst_size < valid_size) {
		LogHelper::log(Error, "dst_size is less than valid size, dst_size: %d, valid_size: %d", dst_size, valid_size);
		return -5;
	}
	char *buf = new char[valid_size];
	memcpy(buf, src, src_size);
	
	//Padding
	memset(buf + src_size, 0, valid_size - src_size);
	buf[src_size] = 0xf;

	int retCode = encode(dst, dst_size, buf, valid_size, key, random, mac);
	delete[] buf;
	if (retCode != 0) {
		LogHelper::log(Error, "Fail to encrypt");
		return retCode;
	}
	
	return valid_size;
}

// Assembly:
subq $0x68, %rsp
movq 0x70(%rsp), %rax
movq %rdi, 0x58(%rsp)
movl %esi, 0x54(%rsp)
movq %rdx, 0x48(%rsp)
movl %ecx, 0x44(%rsp)
movq %r8, 0x38(%rsp)
movq %r9, 0x30(%rsp)
movl 0x44(%rsp), %eax
sarl $0x4, %eax
shll $0x4, %eax
movl %eax, 0x2c(%rsp)
movl 0x2c(%rsp), %eax
cmpl 0x44(%rsp), %eax
jge 0xe988
movl 0x2c(%rsp), %eax
addl $0x10, %eax
movl %eax, 0x2c(%rsp)
movl 0x2c(%rsp), %eax
movl 0x44(%rsp), %ecx
addl $0x2, %ecx
cmpl %ecx, %eax
jge 0xe9a2
movl 0x2c(%rsp), %eax
addl $0x10, %eax
movl %eax, 0x2c(%rsp)
movslq 0x2c(%rsp), %rax
cmpq $0xffcc, %rax           # imm = 0xFFCC
jbe 0xe9d5
movl 0x2c(%rsp), %ecx
xorl %edi, %edi
leaq 0x2602(%rip), %rsi      # 0x10fbe
movl $0xffcc, %edx           # imm = 0xFFCC
movb $0x0, %al
callq 0x28f0
movl $0xfffffffb, 0x64(%rsp) # imm = 0xFFFFFFFB
jmp 0xead4
movl 0x54(%rsp), %eax
cmpl 0x2c(%rsp), %eax
jge 0xea04
movl 0x54(%rsp), %edx
movl 0x2c(%rsp), %ecx
xorl %edi, %edi
leaq 0x2607(%rip), %rsi      # 0x10ff7
movb $0x0, %al
callq 0x28f0
movl $0xfffffffb, 0x64(%rsp) # imm = 0xFFFFFFFB
jmp 0xead4
movslq 0x2c(%rsp), %rdi
movq $-0x1, %rax
cmpq $0x0, %rdi
cmovlq %rax, %rdi
callq 0x2030
movq %rax, 0x20(%rsp)
movq 0x20(%rsp), %rdi
movq 0x48(%rsp), %rsi
movslq 0x44(%rsp), %rdx
callq 0x2180
movq 0x20(%rsp), %rdi
movslq 0x44(%rsp), %rax
addq %rax, %rdi
movl 0x2c(%rsp), %eax
subl 0x44(%rsp), %eax
movslq %eax, %rdx
xorl %esi, %esi
callq 0x2140
movq 0x20(%rsp), %rax
movslq 0x44(%rsp), %rcx
movb $0xf, (%rax,%rcx)
movq 0x58(%rsp), %rdi
movl 0x54(%rsp), %esi
movq 0x20(%rsp), %rdx
movl 0x2c(%rsp), %ecx
movq 0x38(%rsp), %r8
movq 0x30(%rsp), %r9
movq 0x70(%rsp), %rax
movq %rax, (%rsp)
callq 0xe1c0
movl %eax, 0x1c(%rsp)
movq 0x20(%rsp), %rax
movq %rax, 0x10(%rsp)
cmpq $0x0, %rax
je 0xeaab
movq 0x10(%rsp), %rdi
callq 0x2280
cmpl $0x0, 0x1c(%rsp)
je 0xeacc
xorl %edi, %edi
leaq 0x257b(%rip), %rsi      # 0x11036
movb $0x0, %al
callq 0x28f0
movl 0x1c(%rsp), %eax
movl %eax, 0x64(%rsp)
jmp 0xead4
movl 0x2c(%rsp), %eax
movl %eax, 0x64(%rsp)
movl 0x64(%rsp), %eax
addq $0x68, %rsp
retq
nopl (%rax)