// File: renzibei[P]TSProxy/aes.cpp
// Function: testEncodeAndDecode()
int testEncodeAndDecode() {
	char key[16], random[16], mac[16];
	for (int i = 0; i < 16; ++i) {
		key[i] = rand() & 0xff;
	}
	char text[30] = "12345678901234566543210987654";
	for (int i = 0; i < sizeof(text) - 1; i++) {
		text[i] -= '0';
	}
	char buf[60] = {0};
	int codeLen = encrypt(buf, sizeof(buf), text, sizeof(text) - 1, key, random, mac);
	if (codeLen < 0) {
		printf("encode error, ret :%d", codeLen);
		return codeLen;
	}
	printf("after encode:\n");
	
	for(int i = 0; i < codeLen; ++i) {
		printf("%u ", buf[i]);
	}
	printf("\n");
	memset(text, 0, sizeof(text));
	// memset(random, 0, 2);
	codeLen = decrypt(text, sizeof(text), buf, codeLen, key, random, mac);
	if (codeLen < 0) {
		printf("decode error, ret :%d", codeLen);
		return codeLen;
	}
	printf("after decode:\n");
	for(int i = 0; i < codeLen; ++i) {
		printf("%u ", text[i]);
	}
	printf("\n");
	return 0;
}

// Assembly:
subq $0xc8, %rsp
movl $0x0, 0x8c(%rsp)
cmpl $0x10, 0x8c(%rsp)
jge 0xed8a
callq 0x20a0
andl $0xff, %eax
movb %al, %cl
movslq 0x8c(%rsp), %rax
movb %cl, 0xb0(%rsp,%rax)
movl 0x8c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x8c(%rsp)
jmp 0xed52
movq 0x23cf(%rip), %rax      # 0x11160
movq %rax, 0x60(%rsp)
movq 0x23cb(%rip), %rax      # 0x11168
movq %rax, 0x68(%rsp)
movq 0x23c7(%rip), %rax      # 0x11170
movq %rax, 0x70(%rsp)
movl 0x23c4(%rip), %eax      # 0x11178
movl %eax, 0x78(%rsp)
movw 0x23bd(%rip), %ax       # 0x1117c
movw %ax, 0x7c(%rsp)
movl $0x0, 0x5c(%rsp)
movslq 0x5c(%rsp), %rax
cmpq $0x1d, %rax
jae 0xedf5
movslq 0x5c(%rsp), %rax
movsbl 0x60(%rsp,%rax), %ecx
subl $0x30, %ecx
movb %cl, 0x60(%rsp,%rax)
movl 0x5c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x5c(%rsp)
jmp 0xedcc
leaq 0x20(%rsp), %rdi
xorl %esi, %esi
movl $0x3c, %edx
callq 0x2140
leaq 0x20(%rsp), %rdi
leaq 0x60(%rsp), %rdx
movl $0x3c, %esi
movl $0x1d, %ecx
leaq 0xb0(%rsp), %r8
leaq 0xa0(%rsp), %r9
leaq 0x90(%rsp), %rax
movq %rax, (%rsp)
callq 0xe940
movl %eax, 0x1c(%rsp)
cmpl $0x0, 0x1c(%rsp)
jge 0xee68
movl 0x1c(%rsp), %esi
leaq 0x22ba(%rip), %rdi      # 0x1110b
movb $0x0, %al
callq 0x2050
movl 0x1c(%rsp), %eax
movl %eax, 0xc4(%rsp)
jmp 0xef88
leaq 0x22b2(%rip), %rdi      # 0x11121
movb $0x0, %al
callq 0x2050
movl $0x0, 0x18(%rsp)
movl 0x18(%rsp), %eax
cmpl 0x1c(%rsp), %eax
jge 0xeead
movslq 0x18(%rsp), %rax
movsbl 0x20(%rsp,%rax), %esi
leaq 0x2297(%rip), %rdi      # 0x11130
movb $0x0, %al
callq 0x2050
movl 0x18(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x18(%rsp)
jmp 0xee7e
leaq 0x1834(%rip), %rdi      # 0x106e8
movb $0x0, %al
callq 0x2050
leaq 0x60(%rsp), %rdi
xorl %esi, %esi
movl $0x1e, %edx
callq 0x2140
leaq 0x60(%rsp), %rdi
leaq 0x20(%rsp), %rdx
movl 0x1c(%rsp), %ecx
movl $0x1e, %esi
leaq 0xb0(%rsp), %r8
leaq 0xa0(%rsp), %r9
leaq 0x90(%rsp), %rax
movq %rax, (%rsp)
callq 0xeae0
movl %eax, 0x1c(%rsp)
cmpl $0x0, 0x1c(%rsp)
jge 0xef2a
movl 0x1c(%rsp), %esi
leaq 0x221e(%rip), %rdi      # 0x11134
movb $0x0, %al
callq 0x2050
movl 0x1c(%rsp), %eax
movl %eax, 0xc4(%rsp)
jmp 0xef88
leaq 0x2219(%rip), %rdi      # 0x1114a
movb $0x0, %al
callq 0x2050
movl $0x0, 0x14(%rsp)
movl 0x14(%rsp), %eax
cmpl 0x1c(%rsp), %eax
jge 0xef6f
movslq 0x14(%rsp), %rax
movsbl 0x60(%rsp,%rax), %esi
leaq 0x21d5(%rip), %rdi      # 0x11130
movb $0x0, %al
callq 0x2050
movl 0x14(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x14(%rsp)
jmp 0xef40
leaq 0x1772(%rip), %rdi      # 0x106e8
movb $0x0, %al
callq 0x2050
movl $0x0, 0xc4(%rsp)
movl 0xc4(%rsp), %eax
addq $0xc8, %rsp
retq
nopw (%rax,%rax)