// File: /workspace/github_c_makefile_star2_O2/evilwombat[P]gopro-fw-tools/h4-section-patch.c
// Function: parse_firmware
int parse_firmware(unsigned char *buf, int size, struct section_info *output, unsigned int max_sections)
{
	unsigned int section_offset, num = 0;
	unsigned int global_header_crc, global_actual_crc;
	int length;
	int offset = 0;
	
	if (size < GLOBAL_HEADER_SIZE) {
		printf("Invalid firmware size: %d\n", size);
		return -1;
	}
	
	global_header_crc = read_word_le(buf, 0);
	global_actual_crc = get_global_crc(buf, size);
	
	printf("Global header CRC: %08x\n", global_header_crc);
	printf("Global actual CRC: %08x (%s)\n", global_actual_crc,
		(global_header_crc == global_actual_crc) ? "OK" : "MISMATCH!");
	
	if (global_header_crc != global_actual_crc) {
		printf("DANGER!!! Firmware global CRC does not match the CRC listed in the header!\n");
		printf("This is a bad thing. This firmware looks invalid.\n");
		return -1;
	}
	
	while (1) {
		if (num >= max_sections) {
			printf("Firmware contains more than %d sections. Something must be wrong.\n", max_sections);
			return -1;
		}
		
		offset = find_magic(buf, size, offset);
		if (offset < 0) {
			return num;
		}
	
		offset -= 28;
		output[num].header_crc = read_word_le(buf, offset);
		offset += 4;

		output[num].version = read_word_le(buf, offset);
		offset += 4;

		output[num].build_date = read_word_le(buf, offset);
		offset += 4;
		
		length = read_word_le(buf, offset);
		offset += 4;
		offset += 4;

		output[num].flags = read_word_le(buf, offset);
		offset += 4;

		output[num].magic = read_word_le(buf, offset);
		offset += 4;

		offset += 0x100 - 28;

		section_offset = offset;

		output[num].length = length;
		output[num].offset = section_offset;
		
		if (length < 0)
			continue;

		output[num].actual_crc = crc32(buf + section_offset, length);

		if (output[num].header_crc != output[num].actual_crc) {
			printf("WARNING!!! CRC MISMATCH WHILE PARSING SECTION %d\n", num);
			printf("Header CRC = %08x, Actual CRC = %08x\n",
			       output[num].header_crc, output[num].actual_crc);
			return -1;
		}
		
		offset += length;
		
		num++;
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %esi, %r12d
cmpl $0xdf, %esi
jg 0x140f
leaq 0xcc4(%rip), %rdi       # 0x20c4
movl %r12d, %esi
xorl %eax, %eax
callq 0x1080
jmp 0x162b
movl %ecx, %ebp
movq %rdx, %r14
movq %rdi, %r15
movl (%rdi), %r13d
movl %r12d, %esi
callq 0x13ac
movl %eax, %ebx
leaq 0xcb4(%rip), %rdi       # 0x20df
movl %r13d, %esi
xorl %eax, %eax
callq 0x1080
cmpl %ebx, %r13d
leaq 0xc78(%rip), %rax       # 0x20b7
leaq 0xc74(%rip), %rdx       # 0x20ba
cmoveq %rax, %rdx
leaq 0xca7(%rip), %rdi       # 0x20f8
movl %ebx, %esi
xorl %eax, %eax
callq 0x1080
cmpl %ebx, %r13d
jne 0x1613
movq %r14, 0x20(%rsp)
movl %r12d, %ebx
leaq 0xb92(%rip), %r13       # 0x2004
pushq $0x1
popq %r12
movabsq $0x100000000, %r14      # imm = 0x100000000
movabsq $-0x1700000000, %r8     # imm = 0xFFFFFFE900000000
movabsq $-0x1300000000, %r9     # imm = 0xFFFFFFED00000000
movabsq $-0xf00000000, %r10     # imm = 0xFFFFFFF100000000
movabsq $-0x700000000, %r11     # imm = 0xFFFFFFF900000000
xorl %ecx, %ecx
xorl %eax, %eax
cmpl %ebp, %eax
je 0x163d
movl %ebp, 0xc(%rsp)
movl %eax, %edx
shlq $0x5, %rdx
addq 0x20(%rsp), %rdx
movq %rdx, 0x18(%rsp)
movl %ecx, %ebp
movq %rbp, %rsi
shlq $0x20, %rsi
xorl %ecx, %ecx
cmpq %rbx, %rbp
jae 0x162e
movb (%r15,%rbp), %dl
movl %ecx, %ecx
movslq (%r13,%rcx,4), %rcx
addq %r13, %rcx
jmpq *%rcx
xorl %ecx, %ecx
cmpb $-0x70, %dl
sete %cl
jmp 0x1516
xorl %ecx, %ecx
cmpb $0x24, %dl
sete %cl
leal (%rcx,%rcx,2), %ecx
jmp 0x1516
xorl %ecx, %ecx
cmpb $-0x5d, %dl
jne 0x1516
jmp 0x1525
xorl %ecx, %ecx
cmpb $-0x15, %dl
sete %cl
addl %ecx, %ecx
cmpb $-0x70, %dl
cmovel %r12d, %ecx
addq %r14, %rsi
incq %rbp
jmp 0x14d3
movabsq $-0x1b00000000, %rcx    # imm = 0xFFFFFFE500000000
addq %rsi, %rcx
sarq $0x20, %rcx
movl (%r15,%rcx), %ecx
movq 0x18(%rsp), %rdx
movl %ecx, (%rdx)
leaq (%rsi,%r8), %rcx
sarq $0x20, %rcx
movl (%r15,%rcx), %ecx
movl %ecx, 0x8(%rdx)
leaq (%rsi,%r9), %rcx
sarq $0x20, %rcx
movl (%r15,%rcx), %ecx
movl %ecx, 0xc(%rdx)
leaq (%rsi,%r10), %rcx
sarq $0x20, %rcx
movl (%r15,%rcx), %edi
leaq (%rsi,%r11), %rcx
sarq $0x20, %rcx
movl (%r15,%rcx), %ecx
movl %ecx, 0x10(%rdx)
movabsq $-0x300000000, %rcx     # imm = 0xFFFFFFFD00000000
addq %rcx, %rsi
sarq $0x20, %rsi
movl (%r15,%rsi), %ecx
movl %ecx, 0x14(%rdx)
leal 0xe5(%rbp), %ecx
movl %edi, 0x1c(%rdx)
movl %ecx, 0x18(%rdx)
movq %rdi, 0x10(%rsp)
testl %edi, %edi
js 0x14c8
movl %eax, 0x8(%rsp)
leal 0xe5(%rbp), %edi
addq %r15, %rdi
movq 0x10(%rsp), %rsi
callq 0x1ba4
movq 0x18(%rsp), %rcx
movl %eax, 0x4(%rcx)
cmpl %eax, (%rcx)
jne 0x164b
movq 0x10(%rsp), %rax
leal (%rax,%rbp), %ecx
addl $0xe5, %ecx
movl 0x8(%rsp), %eax
incl %eax
movl 0xc(%rsp), %ebp
movabsq $-0x1700000000, %r8     # imm = 0xFFFFFFE900000000
movabsq $-0x1300000000, %r9     # imm = 0xFFFFFFED00000000
movabsq $-0xf00000000, %r10     # imm = 0xFFFFFFF100000000
movabsq $-0x700000000, %r11     # imm = 0xFFFFFFF900000000
jmp 0x14ac
leaq 0xfd9(%rip), %rdi       # 0x25f3
callq 0x1050
leaq 0x1018(%rip), %rdi      # 0x263e
callq 0x1050
pushq $-0x1
popq %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xad2(%rip), %rdi       # 0x2116
movl %ebp, %esi
jmp 0x1403
leaq 0xb07(%rip), %rdi       # 0x2159
movl 0x8(%rsp), %esi
xorl %eax, %eax
movq %rcx, %rbx
callq 0x1080
movl (%rbx), %esi
movl 0x4(%rbx), %edx
leaq 0xb1f(%rip), %rdi       # 0x218b
xorl %eax, %eax
callq 0x1080
jmp 0x162b