// File: /workspace/github_cpp_makefile_star2_O1/bluesky466[P]ResourcesArscDemo/main.cpp
// Function: main
int main(int argc, char *argv[]) {
	FILE* pFile = fopen(argv[1], "rb");

	printf("=====================RES_TABLE_TYPE=====================\n");
	readResTableHeader(pFile);

	printf("=====================RES_STRING_POOL_TYPE=====================\n");
	struct ResStringPool_header globalStringPoolHeader = readResStringPoolHeader(pFile);
	unsigned char* pGlobalStrings = readStringsFromStringPool(pFile, globalStringPoolHeader);
	struct ResTable_package packageHeader;

	printf("=====================RES_TABLE_PACKAGE_TYPE=====================\n");
	while(readResTablePackageHeader(pFile, &packageHeader)) {
		printf("=====================types=====================\n");
		struct ResStringPool_header typeStringPoolHeader = readResStringPoolHeader(pFile);
		unsigned char* pTypeStrings = readStringsFromStringPool(pFile, typeStringPoolHeader);

		printf("=====================keys=====================\n");
		struct ResStringPool_header keyStringPoolHeader = readResStringPoolHeader(pFile);
		unsigned char* pKeyStrings = readStringsFromStringPool(pFile, keyStringPoolHeader);

		/*
		struct ResChunk_header chunkHeader;
		uint8_t id;
		while(fread((void*)&chunkHeader, sizeof(struct ResChunk_header), 1, pFile)
				&& chunkHeader.type != RES_TABLE_PACKAGE_TYPE) {
			fread((void*)&id, sizeof(uint8_t), 1, pFile);
			printf("0x%x, %d\n", chunkHeader.type, id);
			fseek(pFile, chunkHeader.size - sizeof(struct ResChunk_header) - sizeof(uint8_t), SEEK_CUR);
		}
		continue;
		*/

		struct ResTable_type typeHeader;
		struct ResTable_typeSpec typeSpecHeader;
		uint32_t config;
		uint16_t type;
		while(fread((void*)&type, sizeof(u_int16_t), 1, pFile) != 0) {
			fseek(pFile, -sizeof(uint16_t), SEEK_CUR);
			if(RES_TABLE_TYPE_SPEC_TYPE == type) {
				fread((void*)&typeSpecHeader, sizeof(struct ResTable_typeSpec), 1, pFile);
				printf("type: id=0x%x,name=", typeSpecHeader.id);
				printStringFromStringsPool(
						(uint32_t*)pTypeStrings,
						(char*)pTypeStrings + typeStringPoolHeader.stringsStart - sizeof(struct ResStringPool_header),
						typeSpecHeader.id - 1,
						typeStringPoolHeader.flags & ResStringPool_header::UTF8_FLAG
				);

				for(int i = 0 ; i < typeSpecHeader.entryCount ; i++) {
					fread((void*)&config, sizeof(uint32_t), 1, pFile);
					printf("0x%x\n",config);
				}
			} else if(RES_TABLE_TYPE_TYPE == type) {
				fread((void*)&typeHeader, sizeof(struct ResTable_type), 1, pFile);
				printConfig(typeHeader.config);

				// 实际struct ResTable_type的大小可能不同sdk版本不一样,所以typeHeader.header.headerSize才是真正的头部大小
				fseek(pFile, typeHeader.header.headerSize -  sizeof(struct ResTable_type), SEEK_CUR);;

				uint32_t* pOffsets = (uint32_t*)malloc(typeHeader.entryCount * sizeof(uint32_t));
				fread((void*)pOffsets, sizeof(uint32_t), typeHeader.entryCount, pFile);

				unsigned char* pData = (unsigned char*)malloc(typeHeader.header.size - typeHeader.entriesStart);
				fread((void*)pData, typeHeader.header.size - typeHeader.entriesStart, 1, pFile);

				for(int i = 0 ; i< typeHeader.entryCount ; i++) {
					uint32_t offset = *(pOffsets + i);
					if(offset == ResTable_type::NO_ENTRY) {
						continue;
					}
					struct ResTable_entry* pEntry = (struct ResTable_entry*)(pData + offset);
					printf("entryIndex: 0x%x, key :\n", i);
					printStringFromStringsPool(
							(uint32_t*)pKeyStrings,
							(char*)pKeyStrings + keyStringPoolHeader.stringsStart - sizeof(struct ResStringPool_header),
							pEntry->key.index,
							keyStringPoolHeader.flags & ResStringPool_header::UTF8_FLAG
					);
					if(pEntry->flags & ResTable_entry::FLAG_COMPLEX) {
						struct ResTable_map_entry* pMapEntry = (struct ResTable_map_entry*)(pData + offset);
						for(int i = 0; i <pMapEntry->count ; i++) {
							struct ResTable_map* pMap =
							   	(struct ResTable_map*)(pData + offset + pMapEntry->size + i * sizeof(struct ResTable_map));
							printf("\tname:0x%08x, valueType:%u, value:%u\n",
									pMap->name.ident, pMap->value.dataType, pMap->value.data);
						}
					} else {
						struct Res_value* pValue = (struct Res_value*)((unsigned char*)pEntry + sizeof(struct ResTable_entry));
						printf("value :\n");
						printValue(pValue, globalStringPoolHeader, pGlobalStrings);
						printf("\n");
					}
				}
				free(pOffsets);
				free(pData);
			} else {
				break;
			}
		}
	}

	fclose(pFile);
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x278, %rsp            # imm = 0x278
movq 0x8(%rsi), %rdi
leaq 0x2d79(%rip), %rsi      # 0x5044
callq 0x2120
movq %rax, %r14
leaq 0x2f72(%rip), %rdi      # 0x524c
callq 0x2170
movq %r14, %rdi
callq 0x2739
leaq 0x2f97(%rip), %rdi      # 0x5285
callq 0x2170
leaq -0x120(%rbp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x27b8
movups (%rbx), %xmm0
movups 0xc(%rbx), %xmm1
movups %xmm1, 0xc(%rsp)
movups %xmm0, (%rsp)
movq %r14, %rdi
callq 0x280d
movq %rax, -0x78(%rbp)
leaq 0x2f9c(%rip), %rdi      # 0x52c4
callq 0x2170
leaq -0x278(%rbp), %rsi
movq %r14, %rdi
callq 0x2897
testl %eax, %eax
je 0x271d
leaq -0x2a(%rbp), %r15
leaq -0x30(%rbp), %r12
leaq 0x2d08(%rip), %r13      # 0x505b
movq %r14, -0x60(%rbp)
leaq 0x2fa7(%rip), %rdi      # 0x5305
callq 0x2170
leaq -0xd8(%rbp), %rdi
movq %r14, %rsi
callq 0x27b8
movups -0xd8(%rbp), %xmm0
movups -0xcc(%rbp), %xmm1
movups %xmm1, 0xc(%rsp)
movups %xmm0, (%rsp)
movq %r14, %rdi
callq 0x280d
movq %rax, -0x48(%rbp)
leaq 0x2f99(%rip), %rdi      # 0x5335
callq 0x2170
leaq -0xb8(%rbp), %rdi
movq %r14, %rsi
callq 0x27b8
movups -0xb8(%rbp), %xmm0
movups -0xac(%rbp), %xmm1
movups %xmm1, 0xc(%rsp)
movups %xmm0, (%rsp)
movq %r14, %rdi
callq 0x280d
movq %rax, -0x50(%rbp)
movl $0x2, %esi
movl $0x1, %edx
movq %r15, %rdi
movq %r14, %rcx
callq 0x2160
testq %rax, %rax
je 0x2706
movl -0xa8(%rbp), %eax
movl %eax, -0x40(%rbp)
movl -0xa4(%rbp), %eax
movq -0x50(%rbp), %rcx
addq %rcx, %rax
addq $-0x1a, %rax
movq %rax, -0x80(%rbp)
movl -0xc8(%rbp), %eax
movl %eax, -0x3c(%rbp)
movl -0xc4(%rbp), %eax
movq -0x48(%rbp), %rcx
addq %rcx, %rax
addq $-0x1a, %rax
movq %rax, -0x68(%rbp)
movq %r14, %rdi
movq $-0x2, %rsi
movl $0x1, %edx
callq 0x2100
movzwl -0x2a(%rbp), %eax
cmpl $0x201, %eax            # imm = 0x201
je 0x24a9
cmpl $0x202, %eax            # imm = 0x202
jne 0x2706
movl $0x10, %esi
movl $0x1, %edx
leaq -0x98(%rbp), %rdi
movq %r14, %rcx
callq 0x2160
movzbl -0x90(%rbp), %ebx
leaq 0x2bc9(%rip), %rdi      # 0x5047
movl %ebx, %esi
xorl %eax, %eax
callq 0x2030
decl %ebx
movq -0x48(%rbp), %rax
movl (%rax,%rbx,4), %edi
addq -0x68(%rbp), %rdi
btl $0x8, -0x3c(%rbp)
jb 0x26b3
callq 0x2ad0
jmp 0x26b8
movl $0x38, %esi
movl $0x1, %edx
leaq -0x158(%rbp), %rdi
movq %r14, %rcx
callq 0x2160
leaq -0x144(%rbp), %rax
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movaps %xmm0, -0x100(%rbp)
movaps %xmm1, -0xf0(%rbp)
movl 0x20(%rax), %eax
movl %eax, -0xe0(%rbp)
movl -0xe0(%rbp), %eax
movl %eax, 0x20(%rsp)
movaps -0x100(%rbp), %xmm0
movaps -0xf0(%rbp), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
callq 0x2913
movzwl -0x156(%rbp), %esi
addq $-0x38, %rsi
movq %r14, %rdi
movl $0x1, %edx
callq 0x2100
movl -0x14c(%rbp), %r15d
leaq (,%r15,4), %rdi
callq 0x2140
movl $0x4, %esi
movq %rax, -0x58(%rbp)
movq %rax, %rdi
movq %r15, %rdx
movq %r14, %rcx
callq 0x2160
movl -0x154(%rbp), %ebx
subl -0x148(%rbp), %ebx
movq %rbx, %rdi
callq 0x2140
movl $0x1, %edx
movq %rax, -0x38(%rbp)
movq %rax, %rdi
movq %rbx, %rsi
movq %r14, %rcx
callq 0x2160
movq %r15, -0x88(%rbp)
testq %r15, %r15
je 0x268c
movq -0x38(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x70(%rbp)
xorl %ebx, %ebx
movq -0x58(%rbp), %rax
movl (%rax,%rbx,4), %r15d
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq %rax, %r15
je 0x267c
leaq 0x2aab(%rip), %rdi      # 0x5061
movl %ebx, %esi
xorl %eax, %eax
callq 0x2030
movq -0x38(%rbp), %rax
movl 0x4(%rax,%r15), %eax
movq -0x50(%rbp), %rcx
movl (%rcx,%rax,4), %edi
addq -0x80(%rbp), %rdi
btl $0x8, -0x40(%rbp)
jb 0x25e1
callq 0x2ad0
jmp 0x25e6
callq 0x2170
movq -0x38(%rbp), %rax
leaq (%rax,%r15), %r14
testb $0x1, 0x2(%r14)
jne 0x2634
addq $0x8, %r14
leaq 0x2d64(%rip), %rdi      # 0x5364
callq 0x2170
movups -0x120(%rbp), %xmm0
movups -0x114(%rbp), %xmm1
movups %xmm1, 0xc(%rsp)
movups %xmm0, (%rsp)
movq %r14, %rdi
movq -0x78(%rbp), %rsi
callq 0x2955
movl $0xa, %edi
callq 0x2150
jmp 0x267c
movl 0xc(%r14), %eax
testq %rax, %rax
leaq 0x2a38(%rip), %r13      # 0x507a
je 0x267c
movzwl (%r14), %ecx
addq -0x70(%rbp), %r15
addq %rcx, %r15
shlq $0x2, %rax
leaq (%rax,%rax,2), %r14
xorl %r12d, %r12d
movzbl -0x1(%r15,%r12), %edx
movl -0x8(%r15,%r12), %esi
movl (%r15,%r12), %ecx
movq %r13, %rdi
xorl %eax, %eax
callq 0x2030
addq $0xc, %r12
cmpq %r12, %r14
jne 0x265a
incq %rbx
cmpq -0x88(%rbp), %rbx
jne 0x2599
movq -0x58(%rbp), %rdi
callq 0x2130
movq -0x38(%rbp), %rdi
callq 0x2130
movq -0x60(%rbp), %r14
leaq -0x2a(%rbp), %r15
leaq -0x30(%rbp), %r12
leaq 0x29aa(%rip), %r13      # 0x505b
jmp 0x26e8
callq 0x2170
movl -0x8c(%rbp), %ebx
testl %ebx, %ebx
je 0x26e8
movl $0x4, %esi
movl $0x1, %edx
movq %r12, %rdi
movq %r14, %rcx
callq 0x2160
movl -0x30(%rbp), %esi
movq %r13, %rdi
xorl %eax, %eax
callq 0x2030
decl %ebx
jne 0x26c2
movl $0x2, %esi
movl $0x1, %edx
movq %r15, %rdi
movq %r14, %rcx
callq 0x2160
testq %rax, %rax
jne 0x242d
movq %r14, %rdi
leaq -0x278(%rbp), %rsi
callq 0x2897
testl %eax, %eax
jne 0x2357
movq %r14, %rdi
callq 0x20e0
xorl %eax, %eax
addq $0x278, %rsp            # imm = 0x278
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq