// File: renzibei[P]TSProxy/aes.cpp
// Function: decode(char*, int, char const*, int, char const (&) [16], char const (&) [16], char const (&) [16])
int decode(char *dst, int dst_size, const char *src, int src_size, const char (&key)[16], const char (&random)[16], const char (&mac)[16]) {
	// 利用计数器初始值计算h
	// char* copy = (char*) malloc((16) * sizeof(char));

	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);

	char copy[16];
	// strCopy(copy, random);
	memcpy(copy, random, sizeof(copy));
	int i, j;
	// 计算h
	// char* h = (char*) malloc((16) * sizeof(char));
	char h[16];
	// strCopy(h, copy);
	memcpy(h, copy, sizeof(h));
	aes(h, key);

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

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

// Assembly:
subq $0x98, %rsp
movq 0xa0(%rsp), %rax
movq %rdi, 0x88(%rsp)
movl %esi, 0x84(%rsp)
movq %rdx, 0x78(%rsp)
movl %ecx, 0x74(%rsp)
movq %r8, 0x68(%rsp)
movq %r9, 0x60(%rsp)
cmpl $0x0, 0x74(%rsp)
je 0xe659
movl 0x74(%rsp), %eax
movl $0x10, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
je 0xe67d
movl 0x74(%rsp), %edx
xorl %edi, %edi
leaq 0x28b4(%rip), %rsi      # 0x10f1a
movb $0x0, %al
callq 0x28f0
movl $0xffffffff, 0x94(%rsp) # imm = 0xFFFFFFFF
jmp 0xe927
movl 0x84(%rsp), %eax
cmpl 0x74(%rsp), %eax
jge 0xe6b5
movl 0x84(%rsp), %edx
movl 0x74(%rsp), %ecx
xorl %edi, %edi
leaq 0x28a5(%rip), %rsi      # 0x10f43
movb $0x0, %al
callq 0x28f0
movl $0xfffffffe, 0x94(%rsp) # imm = 0xFFFFFFFE
jmp 0xe927
movq 0x88(%rsp), %rdi
movq 0x78(%rsp), %rsi
movslq 0x74(%rsp), %rdx
callq 0x2180
movq 0x60(%rsp), %rax
movq (%rax), %rcx
movq %rcx, 0x50(%rsp)
movq 0x8(%rax), %rax
movq %rax, 0x58(%rsp)
movq 0x50(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x58(%rsp), %rax
movq %rax, 0x38(%rsp)
leaq 0x30(%rsp), %rdi
movq 0x68(%rsp), %rsi
callq 0xd930
leaq 0x30(%rsp), %rdi
callq 0xe060
movq 0x60(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0xe520
movl $0x0, 0x4c(%rsp)
movl 0x4c(%rsp), %eax
movl 0x74(%rsp), %ecx
sarl $0x4, %ecx
cmpl %ecx, %eax
jge 0xe7b9
movq 0x20(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x28(%rsp), %rax
movq %rax, 0x18(%rsp)
movl $0x0, 0x48(%rsp)
cmpl $0x10, 0x48(%rsp)
jge 0xe79a
movslq 0x48(%rsp), %rax
movsbl 0x10(%rsp,%rax), %eax
movq 0x88(%rsp), %rcx
movl 0x4c(%rsp), %edx
shll $0x4, %edx
addl 0x48(%rsp), %edx
movslq %edx, %rdx
movsbl (%rcx,%rdx), %ecx
xorl %ecx, %eax
movb %al, %cl
movslq 0x48(%rsp), %rax
movb %cl, 0x10(%rsp,%rax)
movl 0x48(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x48(%rsp)
jmp 0xe755
leaq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0xe520
movl 0x4c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4c(%rsp)
jmp 0xe726
movl $0x0, 0x48(%rsp)
cmpl $0x10, 0x48(%rsp)
jge 0xe7f6
movslq 0x48(%rsp), %rax
movsbl 0x20(%rsp,%rax), %eax
movslq 0x48(%rsp), %rcx
movsbl 0x30(%rsp,%rcx), %ecx
xorl %ecx, %eax
movb %al, %cl
movslq 0x48(%rsp), %rax
movb %cl, 0x20(%rsp,%rax)
movl 0x48(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x48(%rsp)
jmp 0xe7c1
movl $0x0, 0x48(%rsp)
cmpl $0x10, 0x48(%rsp)
jge 0xe856
movslq 0x48(%rsp), %rax
movsbl 0x20(%rsp,%rax), %eax
movq 0xa0(%rsp), %rcx
movslq 0x48(%rsp), %rdx
movsbl (%rcx,%rdx), %ecx
cmpl %ecx, %eax
je 0xe847
movl $0x1, %edi
leaq 0x2771(%rip), %rsi      # 0x10fa1
movb $0x0, %al
callq 0x28f0
movl $0xfffffffd, 0x94(%rsp) # imm = 0xFFFFFFFD
jmp 0xe927
jmp 0xe849
movl 0x48(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x48(%rsp)
jmp 0xe7fe
movl $0x3, %edi
leaq 0x274f(%rip), %rsi      # 0x10fb1
movb $0x0, %al
callq 0x28f0
movl $0x0, 0x4c(%rsp)
movl 0x4c(%rsp), %eax
movl 0x74(%rsp), %ecx
sarl $0x4, %ecx
cmpl %ecx, %eax
jge 0xe91c
leaq 0x50(%rsp), %rdi
callq 0xe4c0
movq 0x50(%rsp), %rax
movq %rax, (%rsp)
movq 0x58(%rsp), %rax
movq %rax, 0x8(%rsp)
movq %rsp, %rdi
movq 0x68(%rsp), %rsi
callq 0xd930
movl $0x0, 0x48(%rsp)
cmpl $0x10, 0x48(%rsp)
jge 0xe90a
movq 0x88(%rsp), %rax
movl 0x4c(%rsp), %ecx
shll $0x4, %ecx
addl 0x48(%rsp), %ecx
movslq %ecx, %rcx
movsbl (%rax,%rcx), %eax
movslq 0x48(%rsp), %rcx
movsbl (%rsp,%rcx), %ecx
xorl %ecx, %eax
movb %al, %dl
movq 0x88(%rsp), %rax
movl 0x4c(%rsp), %ecx
shll $0x4, %ecx
addl 0x48(%rsp), %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movl 0x48(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x48(%rsp)
jmp 0xe8b6
jmp 0xe90c
movl 0x4c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4c(%rsp)
jmp 0xe871
movl $0x0, 0x94(%rsp)
movl 0x94(%rsp), %eax
addq $0x98, %rsp
retq
nopw %cs:(%rax,%rax)