// File: /workspace/github_cpp_makefile_star2_O2/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 0x2d59(%rip), %rsi      # 0x5044
callq 0x2130
movq %rax, %r14
leaq 0x2f28(%rip), %rdi      # 0x5222
callq 0x21a0
movq %r14, %rdi
callq 0x26bb
leaq 0x2f4d(%rip), %rdi      # 0x525b
callq 0x21a0
leaq -0x120(%rbp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x272f
movups (%rbx), %xmm0
movups 0xc(%rbx), %xmm1
movups %xmm1, 0xc(%rsp)
movups %xmm0, (%rsp)
movq %r14, %rdi
callq 0x277e
movq %rax, -0x60(%rbp)
leaq 0x2f52(%rip), %rdi      # 0x529a
callq 0x21a0
leaq -0x2a(%rbp), %r15
pushq $-0x2
popq %r12
movq %r14, -0x48(%rbp)
movq %r14, %rdi
leaq -0x278(%rbp), %rsi
callq 0x27ff
testl %eax, %eax
je 0x269f
leaq 0x2f64(%rip), %rdi      # 0x52db
callq 0x21a0
leaq -0xd0(%rbp), %rdi
movq %r14, %rsi
callq 0x272f
movups -0xd0(%rbp), %xmm0
movups -0xc4(%rbp), %xmm1
movups %xmm1, 0xc(%rsp)
movups %xmm0, (%rsp)
movq %r14, %rdi
callq 0x277e
movq %rax, %rbx
leaq 0x2f57(%rip), %rdi      # 0x530b
callq 0x21a0
leaq -0xb0(%rbp), %rdi
movq %r14, %rsi
callq 0x272f
movups -0xb0(%rbp), %xmm0
movups -0xa4(%rbp), %xmm1
movups %xmm1, 0xc(%rsp)
movups %xmm0, (%rsp)
movq %r14, %rdi
callq 0x277e
movl -0x9c(%rbp), %ecx
movq %rax, -0x78(%rbp)
addq %rcx, %rax
addq $-0x1c, %rax
movq %rax, -0x70(%rbp)
movl -0xa0(%rbp), %eax
movl $0x100, %ecx            # imm = 0x100
andl %ecx, %eax
movl %eax, -0x34(%rbp)
movl -0xbc(%rbp), %eax
movq %rbx, -0x58(%rbp)
addq %rbx, %rax
addq $-0x1c, %rax
movq %rax, -0x50(%rbp)
movl -0xc0(%rbp), %eax
andl %ecx, %eax
movl %eax, -0x30(%rbp)
movq %r15, %rdi
pushq $0x2
popq %rsi
pushq $0x1
popq %rdx
movq %r14, %rcx
callq 0x2190
testq %rax, %rax
pushq $0x1
popq %rbx
je 0x2359
movq %r14, %rdi
movq %r12, %rsi
movl %ebx, %edx
callq 0x2110
movzwl -0x2a(%rbp), %eax
cmpl $0x201, %eax            # imm = 0x201
je 0x24e0
cmpl $0x202, %eax            # imm = 0x202
jne 0x2359
leaq -0x90(%rbp), %rdi
pushq $0x10
popq %rsi
pushq $0x1
popq %rdx
movq %r14, %rcx
callq 0x2190
movzbl -0x88(%rbp), %ebx
leaq 0x2bb8(%rip), %rdi      # 0x5047
movl %ebx, %esi
xorl %eax, %eax
callq 0x2030
decl %ebx
movq -0x58(%rbp), %rdi
movq -0x50(%rbp), %rsi
movl %ebx, %edx
movl -0x30(%rbp), %ecx
callq 0x2856
movl -0x84(%rbp), %ebx
subl $0x1, %ebx
jb 0x242c
leaq -0x38(%rbp), %rdi
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r14, %rcx
callq 0x2190
movl -0x38(%rbp), %esi
leaq 0x2b84(%rip), %rdi      # 0x505b
xorl %eax, %eax
callq 0x2030
jmp 0x24b2
leaq -0x158(%rbp), %rdi
pushq $0x38
popq %rsi
pushq $0x1
popq %r12
movq %r12, %rdx
movq %r14, %rcx
callq 0x2190
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 0x2877
movzwl -0x156(%rbp), %esi
addq $-0x38, %rsi
movq %r14, %rdi
movl %ebx, %edx
callq 0x2110
movl -0x14c(%rbp), %ebx
leaq (,%rbx,4), %rdi
callq 0x2150
movq %rax, -0x40(%rbp)
movq %rax, %rdi
pushq $0x4
popq %rsi
movq %rbx, -0x80(%rbp)
movq %rbx, %rdx
movq %r14, %rcx
callq 0x2190
movl -0x154(%rbp), %ebx
subl -0x148(%rbp), %ebx
movq %rbx, %rdi
callq 0x2150
movq %rax, %r15
movq %rax, %rdi
movq %rbx, %rsi
movq %r12, %rdx
movq %r14, %rcx
callq 0x2190
leaq 0x8(%r15), %rax
movq %rax, -0x68(%rbp)
xorl %ebx, %ebx
cmpq -0x80(%rbp), %rbx
je 0x267d
movq -0x40(%rbp), %rax
movl (%rax,%rbx,4), %r12d
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq %rax, %r12
je 0x263f
leaq (%r15,%r12), %r13
leaq 0x2a83(%rip), %rdi      # 0x5061
movl %ebx, %esi
xorl %eax, %eax
callq 0x2030
movl 0x4(%r15,%r12), %edx
movq -0x78(%rbp), %rdi
movq -0x70(%rbp), %rsi
movl -0x34(%rbp), %ecx
callq 0x2856
testb $0x1, 0x2(%r15,%r12)
jne 0x2647
addq $0x8, %r13
leaq 0x2d2b(%rip), %rdi      # 0x533a
callq 0x21a0
movups -0x120(%rbp), %xmm0
movups -0x114(%rbp), %xmm1
movups %xmm1, 0xc(%rsp)
movups %xmm0, (%rsp)
movq %r13, %rdi
movq -0x60(%rbp), %rsi
callq 0x28b0
pushq $0xa
popq %rdi
callq 0x2180
incq %rbx
jmp 0x25b7
movl 0xc(%r13), %r14d
addq -0x68(%rbp), %r12
subq $0x1, %r14
jb 0x263f
movzwl (%r13), %eax
movzbl -0x1(%r12,%rax), %edx
movl -0x8(%r12,%rax), %esi
movl (%r12,%rax), %ecx
leaq 0x2a0a(%rip), %rdi      # 0x507a
xorl %eax, %eax
callq 0x2030
addq $0xc, %r12
jmp 0x264f
movq -0x40(%rbp), %rdi
callq 0x2140
movq %r15, %rdi
callq 0x2140
movq -0x48(%rbp), %r14
leaq -0x2a(%rbp), %r15
pushq $-0x2
popq %r12
jmp 0x242c
movq %r14, %rdi
callq 0x20f0
xorl %eax, %eax
addq $0x278, %rsp            # imm = 0x278
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq