// File: /workspace/github_c_makefile_star2_O2/miniupnp[P]hfsplustools/src/hfsunhide.c
// Function: main
int main(int argc, char * * argv)
{
	FILE * f;
	const char * isofile = NULL;
	const char * path = NULL;
	int i;
	int doit = 0;
	int verbose = 0;
	unsigned char * catalog = NULL;
	int found = 0;
	int need_patching = 0;
	struct find_infos infos;
	int ret = 0;

	for(i = 1; i < argc; i++) {
		if(strcmp(argv[i], "-v")==0)
			verbose++;
		else if(strcmp(argv[i], "--doit")==0)
			doit = 1;
		else if(isofile == NULL)
			isofile = argv[i];
		else if(path == NULL)
			path = argv[i];
		else {
			fprintf(stderr, "unrecognized argument : %s\n", argv[i]);
			return 1;
		}
	}
	if(isofile == NULL || path == NULL) {
		fprintf(stderr, "Usage: %s [--doit] [-v] <image.iso> path/.../file.txt\n", argv[0]);
		return 1;
	}
	printf("isofile=%s path=%s\n", isofile, path);
	f = fopen(isofile, "rb");
	if(f == NULL) {
		fprintf(stderr, "cannot open %s for reading\n", isofile);
		return 2;
	}
	read_partition_map(f);
	if(hfs_part_sector == 0 && hfs_part_sector_count == 0) {
		printf("HFS/HFS+ partition not found\n");
		fclose(f);
		return 3;
	} else {
		const char * curpath;
		char pathelt[256];
		uint32_t folder_id;

		read_hfs_volume_header(f);
		//read_catalog(f);
		catalog = load_catalog(f);
		if(!print_node(catalog)) { /* header node */
			free(catalog);
			return 4;
		}
		/* find path/.../file.txt */
		curpath = path;
		folder_id = 2; /* root id */
		do {
			char * p;
			size_t len;
			p = strchr(curpath, '/');
			if(p) {
				len = p - curpath;
				if(len >= sizeof(pathelt))
					len = sizeof(pathelt) - 1;
				memcpy(pathelt, curpath, len);
				pathelt[len] = '\0';
				curpath = p + 1;
			} else {
				strncpy(pathelt, curpath, sizeof(pathelt));
				curpath = NULL;
			}
			if(!hfs_find(catalog, folder_id, pathelt, &infos)) {
				/* not found */
				break;
			}
			if(curpath) {
				folder_id = infos.folder_id;
				printf("Folder %s found : id=%u\n", pathelt, folder_id);
			} else {
				printf("Folder/file %s found ! flags=%04X\n",
				       path, readu16(infos.p + 56));
				found = 1;
				if(readu16(infos.p + 56) & 0x4000)
					need_patching = 1;
			}
		} while(curpath && !found);
	}
	fclose(f);
	if(!found) {
		fprintf(stderr, "Folder/file %s NOT FOUND !\n", path);
		ret = 42;
	} else if(!need_patching) {
		fprintf(stderr, "%s doesn't need patching\n", isofile);
		ret = 43;
	}
	//   kIsInvisible    = 0x4000,     /* Files and folders */
	if(doit && found && need_patching) {
		uint16_t HFSflags, newHFSflags;
		printf("unhidding %s\n", path);
		HFSflags = readu16(infos.p + 56);
		newHFSflags = HFSflags & ~0x4000;
		printf("flags %04X => %04X (offset %Xh in catalog)\n",
		       HFSflags, newHFSflags, (unsigned)(infos.p + 56 - catalog));
		writeu16(infos.p + 56, newHFSflags);
		printf("writing patched catalog\n");
		f = fopen(isofile, "r+b");
		if(f == NULL) {
			fprintf(stderr, "cannot open %s for writing\n", isofile);
			ret = 5;
		} else {
			if(save_catalog(f, catalog)) {
				printf("SUCCESS\n");
				ret = 0;
			} else {
				fprintf(stderr, "save_catalog() failed\n");
				ret = 6;
			}
		}
		fclose(f);
	}
	free(catalog);
	return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x148, %rsp            # imm = 0x148
movq %rsi, %r14
movslq %edi, %rbx
movq $0x0, 0x30(%rsp)
pushq $0x1
popq %r13
leaq 0x1d55(%rip), %r12      # 0x3004
pushq $0x1
popq %rbp
movq $0x0, 0x8(%rsp)
movq $0x0, 0x10(%rsp)
cmpq %rbx, %r13
jge 0x131e
movq (%r14,%r13,8), %r15
cmpb $0x2d, (%r15)
jne 0x12e1
cmpb $0x76, 0x1(%r15)
jne 0x12e1
cmpb $0x0, 0x2(%r15)
je 0x1319
movq %r15, %rdi
movq %r12, %rsi
callq 0x10d0
testl %eax, %eax
je 0x130b
cmpq $0x0, 0x10(%rsp)
je 0x1314
cmpq $0x0, 0x8(%rsp)
jne 0x15e3
movq %r15, 0x8(%rsp)
jmp 0x1319
movl %ebp, %eax
movq %rax, 0x30(%rsp)
jmp 0x1319
movq %r15, 0x10(%rsp)
incq %r13
jmp 0x12c4
movq 0x10(%rsp), %rbx
testq %rbx, %rbx
movq 0x8(%rsp), %r12
je 0x15ae
testq %r12, %r12
je 0x15ae
leaq 0x1d1d(%rip), %rdi      # 0x305e
movq %rbx, %rsi
movq %r12, %rdx
xorl %eax, %eax
callq 0x10b0
leaq 0x1d1d(%rip), %rsi      # 0x3072
movq %rbx, %rdi
callq 0x1140
testq %rax, %rax
je 0x15c4
movq %rax, %r14
movq %rax, %rdi
callq 0x2a13
leaq 0x4d5c(%rip), %rax      # 0x60d4
leaq 0x4d59(%rip), %rcx      # 0x60d8
movl (%rcx), %ecx
orl (%rax), %ecx
jne 0x13a0
leaq 0x1e08(%rip), %rdi      # 0x3194
callq 0x1070
movq %r14, %rdi
callq 0x1090
pushq $0x3
jmp 0x1600
movq %r14, %rdi
callq 0x179c
movq %r14, %rdi
callq 0x270b
movq %rax, %r15
movq %rax, %rdi
callq 0x1c83
testl %eax, %eax
je 0x146a
movq %r14, 0x38(%rsp)
pushq $0x2
popq %rbp
leaq 0x40(%rsp), %r14
movq %r12, %rbx
movl $0xff, %r12d
movq %r15, 0x28(%rsp)
movq %rbx, %rdi
pushq $0x2f
popq %rsi
callq 0x10a0
testq %rax, %rax
je 0x141f
movq %rax, %r13
movq %rax, %r15
subq %rbx, %r15
cmpq %r12, %r15
cmovaeq %r12, %r15
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x10f0
movb $0x0, 0x40(%rsp,%r15)
movq 0x28(%rsp), %r15
incq %r13
movq %r13, %rbx
jmp 0x1431
movl $0x100, %edx            # imm = 0x100
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1060
xorl %ebx, %ebx
movq %r15, %rdi
movl %ebp, %esi
movq %r14, %rdx
leaq 0x18(%rsp), %rcx
callq 0x216e
movl %eax, %ebp
testl %eax, %eax
je 0x1479
testq %rbx, %rbx
je 0x147e
movl 0x20(%rsp), %ebp
leaq 0x1c38(%rip), %rdi      # 0x3091
movq %r14, %rsi
movl %ebp, %edx
xorl %eax, %eax
callq 0x10b0
jmp 0x13de
movq %r15, %rdi
callq 0x1030
pushq $0x4
jmp 0x1600
xorl %r12d, %r12d
jmp 0x14bb
movq 0x18(%rsp), %rdi
addq $0x38, %rdi
callq 0x1787
movzwl %ax, %edx
leaq 0x1c14(%rip), %rdi      # 0x30aa
movq 0x8(%rsp), %rsi
xorl %eax, %eax
callq 0x10b0
movq 0x18(%rsp), %rdi
addq $0x38, %rdi
callq 0x1787
movl %eax, %r12d
shrl $0xe, %r12d
andl $0x1, %r12d
movq 0x38(%rsp), %rdi
callq 0x1090
testl %ebp, %ebp
je 0x1617
xorl %r14d, %r14d
testl %r12d, %r12d
je 0x1638
cmpl $0x0, 0x30(%rsp)
je 0x15a4
testl %ebp, %ebp
je 0x15a4
testl %r12d, %r12d
je 0x15a4
leaq 0x1c07(%rip), %rdi      # 0x3103
movq 0x8(%rsp), %rsi
xorl %eax, %eax
callq 0x10b0
movq 0x18(%rsp), %rdi
addq $0x38, %rdi
callq 0x1787
movl %eax, %ebx
movzwl %ax, %ebp
andl $0xbfff, %ebx           # imm = 0xBFFF
movl 0x18(%rsp), %ecx
subl %r15d, %ecx
addl $0x38, %ecx
leaq 0x1bdf(%rip), %rdi      # 0x3111
movl %ebp, %esi
movl %ebx, %edx
xorl %eax, %eax
callq 0x10b0
movq 0x18(%rsp), %rax
movb %bh, 0x38(%rax)
movb %bpl, 0x39(%rax)
leaq 0x1c24(%rip), %rdi      # 0x3174
callq 0x1070
leaq 0x1be1(%rip), %rsi      # 0x313d
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x1140
movq %r15, %rsi
movq %rax, %r15
testq %rax, %rax
je 0x165e
movq %r15, %rdi
callq 0x285a
testl %eax, %eax
je 0x167d
leaq 0x1bfd(%rip), %rdi      # 0x318c
callq 0x1070
xorl %r14d, %r14d
movq %r15, %rdi
callq 0x1090
movq 0x28(%rsp), %r15
movq %r15, %rdi
callq 0x1030
jmp 0x1602
movq 0x4a43(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
movq (%r14), %rdx
leaq 0x1a65(%rip), %rsi      # 0x3027
jmp 0x15f7
movq 0x4a2d(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x1aa0(%rip), %rsi      # 0x3075
movq %rbx, %rdx
xorl %eax, %eax
callq 0x10e0
pushq $0x2
jmp 0x1600
movq 0x4a0e(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x1a17(%rip), %rsi      # 0x300b
movq %r15, %rdx
xorl %eax, %eax
callq 0x10e0
pushq $0x1
popq %r14
movl %r14d, %eax
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x49da(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x1aa5(%rip), %rsi      # 0x30cd
movq 0x8(%rsp), %rdx
xorl %eax, %eax
callq 0x10e0
pushq $0x2a
jmp 0x1657
movq 0x49b9(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x1aa0(%rip), %rsi      # 0x30e9
movq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x10e0
pushq $0x2b
popq %r14
jmp 0x14d9
movq 0x4993(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x1ad2(%rip), %rsi      # 0x3141
movq %rbx, %rdx
xorl %eax, %eax
callq 0x10e0
pushq $0x5
jmp 0x169b
movq 0x4974(%rip), %rax      # 0x5ff8
movq (%rax), %rcx
leaq 0x1acf(%rip), %rdi      # 0x315d
pushq $0x16
popq %rsi
pushq $0x1
popq %rdx
callq 0x1160
pushq $0x6
popq %r14
jmp 0x1597
nop