// File: /workspace/github_c_makefile_star2_O3/miniupnp[P]hfsplustools/src/hfscommon.c
// Function: read_partition_map
void read_partition_map(FILE * f)
{
	unsigned char buffer[SECTOR_SIZE];
	unsigned int i = 0;
	unsigned int count = 0;
	uint16_t block_size = 0;
	uint32_t number_of_blocks = 0;

#if 0
	fseek(f, PARTITION_MAP_OFFSET, SEEK_SET);
#else
	/* read the Driver Descriptor */
	fseek(f, 0, SEEK_SET);
	fread(buffer, 1, sizeof(buffer), f);
	hexdump(buffer, 32, 0);
	if(readu16(buffer) != 0x4552) {	// signature
		printf("bad signature for Driver Descriptor block\n");
	} else {
		unsigned int i, desc_count;
		printf("=== Driver Descriptor Block ===\n");
		block_size = readu16(buffer + 2);
		number_of_blocks = readu32(buffer + 4);
		printf("%lu blocks of %hu bytes. %lu bytes total.\n",
		       (unsigned long)number_of_blocks, block_size, (unsigned long)number_of_blocks * block_size);
		printf("Type %04hx Identifier %04hx Data %08x\n",
		       readu16(buffer+8), readu16(buffer+10), readu32(buffer+12));
		desc_count = readu16(buffer+16);
		printf("%u descriptors%s\n", desc_count, (desc_count == 0) ? "." : " :");
		for (i = 0; i < desc_count; i++) {
			printf(" #%02u : start block %u, block count %u, OS=%04x\n",
			       i, readu32(buffer+18+i*8), readu16(buffer+20+i*8),
			       readu32(buffer+22+i*8));
		}
	}
#endif

	do {
		unsigned int start, size;
		unsigned int start_l, size_l;
		fread(buffer, 1, sizeof(buffer), f);
		hexdump(buffer, 136, 0);
		printf("=== partition %u ===\n", i);
		if(readu16(buffer) != 0x504D) {	// signature
			printf("bad signature\n");
			break;
		}
		count = readu32(buffer + 4);
		printf("total number of partitions = %u\n", count);
		start = readu32(buffer + 8);
		size = readu32(buffer + 12);
		printf("starting sector=%u (offset %08x) size %u (%u bytes)\n",
			start, start * SECTOR_SIZE, size, size * SECTOR_SIZE);
		start_l = readu32(buffer + 80);
		size_l = readu32(buffer + 84);
		printf("logical starting sector=%u (offset %08x) size %u (%u bytes)\n",
			start_l, start_l * SECTOR_SIZE, size_l, size_l * SECTOR_SIZE);
		printf(" name = '%s' type = '%s'\n", buffer + 16, buffer + 48);
		if(strcmp((char *)buffer + 48, "Apple_HFS") == 0) {
			printf("---\n");
			hfs_part_sector = start;
			hfs_part_sector_count = size;
		}
		i++;
	} while(i < count);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x208, %rsp            # imm = 0x208
movq %rdi, %rbx
xorl %esi, %esi
xorl %edx, %edx
callq 0x1100
movq %rsp, %r14
movl $0x1, %esi
movl $0x200, %edx            # imm = 0x200
movq %r14, %rdi
movq %rbx, %rcx
callq 0x1070
movq %r14, %rdi
movl $0x20, %esi
xorl %edx, %edx
callq 0x19e8
movzwl (%r14), %eax
rolw $0x8, %ax
movzwl %ax, %eax
cmpl $0x4552, %eax           # imm = 0x4552
jne 0x2d38
leaq 0xf25(%rip), %rdi       # 0x3b7f
callq 0x1060
movzwl 0x2(%rsp), %eax
rolw $0x8, %ax
movzwl %ax, %edx
movl 0x4(%rsp), %esi
bswapl %esi
movq %rdx, %rcx
imulq %rsi, %rcx
leaq 0xb9d(%rip), %rdi       # 0x381c
xorl %eax, %eax
callq 0x1090
movzwl 0x8(%rsp), %eax
movzwl 0xa(%rsp), %ecx
rolw $0x8, %ax
rolw $0x8, %cx
movzwl %ax, %esi
movzwl %cx, %edx
movl 0xc(%rsp), %ecx
bswapl %ecx
leaq 0xb9c(%rip), %rdi       # 0x3847
xorl %eax, %eax
callq 0x1090
movzwl 0x10(%rsp), %eax
rolw $0x8, %ax
movzwl %ax, %r14d
testw %r14w, %r14w
leaq 0xbb6(%rip), %rax       # 0x3880
leaq 0xe4c(%rip), %rdx       # 0x3b1d
cmoveq %rax, %rdx
leaq 0xb92(%rip), %rdi       # 0x386e
movl %r14d, %esi
xorl %eax, %eax
callq 0x1090
testw %r14w, %r14w
je 0x2d44
leaq 0xb8f(%rip), %r15       # 0x3882
xorl %r12d, %r12d
movzbl 0x12(%rsp,%r12,8), %eax
shll $0x18, %eax
movzbl 0x13(%rsp,%r12,8), %edx
shll $0x10, %edx
orl %eax, %edx
movzwl 0x14(%rsp,%r12,8), %eax
rolw $0x8, %ax
movzwl %ax, %ecx
orl %ecx, %edx
movl 0x16(%rsp,%r12,8), %r8d
bswapl %r8d
movq %r15, %rdi
movl %r12d, %esi
xorl %eax, %eax
callq 0x1090
incq %r12
cmpq %r12, %r14
jne 0x2cf6
jmp 0x2d44
leaq 0xe60(%rip), %rdi       # 0x3b9f
callq 0x1060
movq %rsp, %r12
xorl %r14d, %r14d
movl $0x1, %esi
movl $0x200, %edx            # imm = 0x200
movq %r12, %rdi
movq %rbx, %rcx
callq 0x1070
movq %r12, %rdi
movl $0x88, %esi
xorl %edx, %edx
callq 0x19e8
leaq 0xb3f(%rip), %rdi       # 0x38b4
movl %r14d, %esi
xorl %eax, %eax
callq 0x1090
movzwl (%rsp), %eax
rolw $0x8, %ax
movzwl %ax, %eax
cmpl $0x504d, %eax           # imm = 0x504D
jne 0x2e61
movl 0x4(%rsp), %r15d
bswapl %r15d
leaq 0xb26(%rip), %rdi       # 0x38ca
movl %r15d, %esi
xorl %eax, %eax
callq 0x1090
movl 0x8(%rsp), %r13d
movl 0xc(%rsp), %ebp
bswapl %r13d
bswapl %ebp
movl %r13d, %edx
shll $0x9, %edx
movl %ebp, %r8d
shll $0x9, %r8d
leaq 0xb23(%rip), %rdi       # 0x38f3
movl %r13d, %esi
movl %ebp, %ecx
xorl %eax, %eax
callq 0x1090
movl 0x50(%rsp), %esi
movl 0x54(%rsp), %ecx
bswapl %esi
bswapl %ecx
movl %esi, %edx
shll $0x9, %edx
movl %ecx, %r8d
shll $0x9, %r8d
leaq 0xaf0(%rip), %rdi       # 0x38eb
xorl %eax, %eax
callq 0x1090
leaq 0xb1f(%rip), %rdi       # 0x3928
leaq 0x10(%rsp), %rsi
leaq 0x30(%rsp), %rdx
xorl %eax, %eax
callq 0x1090
movq 0x30(%rsp), %rax
movabsq $0x46485f656c707041, %rcx # imm = 0x46485F656C707041
xorq %rcx, %rax
movzwl 0x38(%rsp), %ecx
xorq $0x53, %rcx
orq %rax, %rcx
jne 0x2e53
leaq 0xd88(%rip), %rdi       # 0x3bc9
callq 0x1060
movl %r13d, 0x3277(%rip)     # 0x60c4
movl %ebp, 0x3275(%rip)      # 0x60c8
incl %r14d
cmpl %r15d, %r14d
jb 0x2d4a
jmp 0x2e6d
leaq 0xd65(%rip), %rdi       # 0x3bcd
callq 0x1060
addq $0x208, %rsp            # imm = 0x208
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq