// File: /workspace/github_c_makefile_star2_O3/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, %ebp
cmpl $0xdf, %esi
jg 0x140b
leaq 0xcc7(%rip), %rdi       # 0x20c4
movl %ebp, %esi
xorl %eax, %eax
callq 0x1080
jmp 0x164a
movl %ecx, %r15d
movq %rdx, %rbx
movl (%rdi), %r12d
movq %rdi, 0x20(%rsp)
movl %ebp, %esi
callq 0x13aa
movl %eax, %r13d
leaq 0xcb5(%rip), %rdi       # 0x20df
movl %r12d, %esi
xorl %eax, %eax
callq 0x1080
cmpl %r13d, %r12d
leaq 0xc79(%rip), %rax       # 0x20b7
leaq 0xc75(%rip), %rdx       # 0x20ba
cmoveq %rax, %rdx
leaq 0xca8(%rip), %rdi       # 0x20f8
movl %r13d, %esi
xorl %eax, %eax
callq 0x1080
cmpl %r13d, %r12d
jne 0x1632
movq %rbx, 0x18(%rsp)
testl %r15d, %r15d
je 0x1623
movl %ebp, %r14d
negl %r14d
xorl %eax, %eax
leaq 0xb84(%rip), %rbx       # 0x2004
movl $0x1, %r13d
movabsq $0x100000000, %r12      # imm = 0x100000000
movabsq $-0x1b00000000, %r10    # imm = 0xFFFFFFE500000000
movabsq $-0x1700000000, %r11    # imm = 0xFFFFFFE900000000
xorl %ecx, %ecx
movl %r15d, 0x4(%rsp)
movq %rax, %rdx
shlq $0x5, %rdx
addq 0x18(%rsp), %rdx
movl %ecx, %r15d
movq 0x20(%rsp), %r8
movq %rdx, 0x10(%rsp)
cmpl %ebp, %r15d
jge 0x164f
movl %r15d, %r9d
leaq (%r8,%r9), %rcx
shlq $0x20, %r9
addl $0xe5, %r15d
xorl %edx, %edx
movb (%rcx), %sil
movl %edx, %edx
movslq (%rbx,%rdx,4), %rdx
addq %rbx, %rdx
jmpq *%rdx
xorl %edx, %edx
cmpb $-0x70, %sil
sete %dl
jmp 0x151d
xorl %edx, %edx
cmpb $0x24, %sil
sete %dl
leal (%rdx,%rdx,2), %edx
jmp 0x151d
xorl %edx, %edx
cmpb $-0x5d, %sil
jne 0x151d
jmp 0x1546
xorl %edx, %edx
cmpb $-0x15, %sil
sete %dl
addl %edx, %edx
cmpb $-0x70, %sil
cmovel %r13d, %edx
addq %r12, %r9
incq %rcx
leal (%r14,%r15), %esi
incl %esi
movl %r15d, %edi
incl %edi
movl %edi, %r15d
cmpl $0xe5, %esi
jne 0x14e1
jmp 0x164f
leaq (%r9,%r10), %rcx
sarq $0x20, %rcx
movl (%r8,%rcx), %ecx
movq 0x10(%rsp), %rdx
movl %ecx, (%rdx)
leaq (%r9,%r11), %rcx
sarq $0x20, %rcx
movl (%r8,%rcx), %ecx
movl %ecx, 0x8(%rdx)
movabsq $-0x1300000000, %rcx    # imm = 0xFFFFFFED00000000
addq %r9, %rcx
sarq $0x20, %rcx
movl (%r8,%rcx), %ecx
movl %ecx, 0xc(%rdx)
movabsq $-0xf00000000, %rcx     # imm = 0xFFFFFFF100000000
addq %r9, %rcx
sarq $0x20, %rcx
movl (%r8,%rcx), %esi
movabsq $-0x700000000, %rcx     # imm = 0xFFFFFFF900000000
addq %r9, %rcx
sarq $0x20, %rcx
movl (%r8,%rcx), %ecx
movl %ecx, 0x10(%rdx)
movabsq $-0x300000000, %rcx     # imm = 0xFFFFFFFD00000000
addq %rcx, %r9
sarq $0x20, %r9
movl (%r8,%r9), %ecx
movl %ecx, 0x14(%rdx)
movl %esi, 0x1c(%rdx)
movl %r15d, 0x18(%rdx)
testl %esi, %esi
js 0x14c4
movq %rax, 0x8(%rsp)
movl %r15d, %edi
addq %r8, %rdi
movl %esi, (%rsp)
callq 0x1bf4
movq 0x10(%rsp), %rcx
movl %eax, 0x4(%rcx)
cmpl %eax, (%rcx)
jne 0x165e
movl (%rsp), %ecx
addl %r15d, %ecx
movq 0x8(%rsp), %rax
incq %rax
movl 0x4(%rsp), %r15d
cmpl %r15d, %eax
movabsq $-0x1b00000000, %r10    # imm = 0xFFFFFFE500000000
movabsq $-0x1700000000, %r11    # imm = 0xFFFFFFE900000000
jne 0x14ab
leaq 0xaec(%rip), %rdi       # 0x2116
movl %r15d, %esi
jmp 0x13ff
leaq 0xfba(%rip), %rdi       # 0x25f3
callq 0x1050
leaq 0xff9(%rip), %rdi       # 0x263e
callq 0x1050
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xaf4(%rip), %rdi       # 0x2159
movq 0x8(%rsp), %rsi
xorl %eax, %eax
movq %rcx, %rbx
callq 0x1080
movl (%rbx), %esi
movl 0x4(%rbx), %edx
leaq 0xb0b(%rip), %rdi       # 0x218b
xorl %eax, %eax
callq 0x1080
jmp 0x164a