// File: /workspace/github_cpp_makefile_star2_O1/renzibei[P]TSProxy/aes.cpp
// Function: encode(char*, int, char const*, int, char const (&) [16], char (&) [16], char (&) [16])
int encode(char *dst, int dst_size, const char *src, int src_size, const char (&key)[16], char (&random)[16], char (&mac)[16]) {
	if(src_size == 0 || src_size % 16 != 0) {
		// printf("data len should be 16*n");
		LogHelper::log(Error, "src len should be 16*n, but src_size: %d", src_size);
		return -1;
	}
	if (dst_size < src_size) {
		LogHelper::log(Error, "dst_size should be at least the same as src_size, but dst_size: %, src_size: %d", dst_size, src_size);
		return -2;
	}

	memcpy(dst, src, src_size);

	srand((unsigned int)time((time_t *)NULL));
	// 生成随机计数器初始值
	// char* copy = (char*) malloc((16) * sizeof(char));
	char copy[16];
	int i, j;
	for(i=0; i<16; i++){
		copy[i] = random[i] = (char)rand();
	}
	// 计算h
	// char* h = (char*) malloc((16) * sizeof(char));
	char h[16];
	// strCopy(h, copy);
	memcpy(h, copy, 16);
	aes(h, key);

	// 加密 len/16 组数据，得到密文
	char ctr[16];
	for(i=0; i< (src_size >> 4); i++){
		ctrAdd(copy);	// 计数器 +1
		// char* ctr = (char*) malloc((16) * sizeof(char));
		memcpy(ctr, copy, sizeof(ctr));
		// strCopy(ctr, copy);
		aes(ctr, key);
		// 对该组数据的16个字节分别进行异或操作
		for(j=0; j<16; j++){
			dst[(i << 4) + j] = dst[ (i << 4) + j] ^ ctr[j];
		}
	}

	// 计算 mac 值
	compute_hh((Uchar16 &)h);
	// 第一次GF乘法
	mult_h((Uchar16 &)random, (Uchar16 &)mac);
	// 对每组密文进行一次
	char lastResult[16];
	for(i=0; i< (src_size >> 4); i++){
		// char* lastResult = (char*) malloc((16) * sizeof(char));
		// strCopy(lastResult, mac);
		memcpy(lastResult, mac, sizeof(lastResult));
		// 对该组密文的16个字节分别进行异或操作
		for(j=0; j<16; j++){
			lastResult[j] = lastResult[j] ^ dst[ (i << 4) + j];
		}
		mult_h((Uchar16 &)lastResult, (Uchar16 &)mac);
	}
	// 与h进行异或操作
	for(j=0; j<16; j++){
		mac[j] = mac[j] ^ h[j];
	}
	// printf("finish encode\n");
	LogHelper::log(Debug, "finish encode");
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %ecx, %ebp
movl %esi, %eax
testl %ecx, %ecx
setne %cl
testb $0xf, %bpl
sete %sil
testb %sil, %cl
je 0x67ea
cmpl %ebp, %eax
jge 0x6806
leaq 0x2c3b(%rip), %rsi      # 0x940e
xorl %edi, %edi
movl %eax, %edx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x273c
movl $0xfffffffe, %ebx       # imm = 0xFFFFFFFE
jmp 0x6939
leaq 0x2bf4(%rip), %rsi      # 0x93e5
xorl %edi, %edi
movl %ebp, %edx
xorl %eax, %eax
callq 0x273c
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x6939
movq %r9, %r15
movq %r8, %r12
movq %rdi, %rbx
movslq %ebp, %rax
movq %rdx, %rsi
movq %rax, %rdx
callq 0x2150
xorl %r14d, %r14d
xorl %edi, %edi
callq 0x2190
movl %eax, %edi
callq 0x21c0
callq 0x2090
movb %al, (%r15,%r14)
movb %al, 0x10(%rsp,%r14)
incq %r14
cmpq $0x10, %r14
jne 0x682e
movaps 0x10(%rsp), %xmm0
leaq 0x40(%rsp), %rdi
movaps %xmm0, (%rdi)
movq %r12, %rsi
callq 0x6288
sarl $0x4, %ebp
movl %ebp, %r14d
movl %ebp, 0xc(%rsp)
testl %ebp, %ebp
jle 0x68ba
xorl %ebp, %ebp
leaq 0x30(%rsp), %r13
movl $0xf, %eax
movl %eax, %edx
movb 0x10(%rsp,%rdx), %cl
incb %cl
movb %cl, 0x10(%rsp,%rdx)
subl $0x1, %eax
jb 0x6889
testb %cl, %cl
je 0x6874
movaps 0x10(%rsp), %xmm0
movaps %xmm0, 0x30(%rsp)
movq %r13, %rdi
movq %r12, %rsi
callq 0x6288
movq %rbp, %rax
shlq $0x4, %rax
movups (%rbx,%rax), %xmm0
xorps 0x30(%rsp), %xmm0
movups %xmm0, (%rbx,%rax)
incq %rbp
cmpq %r14, %rbp
jne 0x686f
leaq 0x40(%rsp), %rdi
callq 0x6720
movq %r15, %rdi
movq 0x90(%rsp), %r13
movq %r13, %rsi
callq 0x694a
cmpl $0x0, 0xc(%rsp)
jle 0x6915
shlq $0x4, %r14
xorl %r12d, %r12d
leaq 0x20(%rsp), %r15
movups (%r13), %xmm0
movaps %xmm0, 0x20(%rsp)
movups (%rbx,%r12), %xmm1
xorps %xmm0, %xmm1
movaps %xmm1, 0x20(%rsp)
movq %r15, %rdi
movq %r13, %rsi
callq 0x694a
addq $0x10, %r12
cmpq %r12, %r14
jne 0x68ea
movups (%r13), %xmm0
xorps 0x40(%rsp), %xmm0
movups %xmm0, (%r13)
leaq 0x2b33(%rip), %rsi      # 0x945e
xorl %ebx, %ebx
movl $0x3, %edi
xorl %eax, %eax
callq 0x273c
movl %ebx, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq