// File: /workspace/github_c_makefile_star2_O3/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 $0x138, %rsp            # imm = 0x138
movq %rsi, %r14
cmpl $0x2, %edi
jl 0x1692
movl %edi, %ebx
movq $0x0, (%rsp)
movl $0x1, %ebp
leaq 0x1b2d(%rip), %r12      # 0x3004
xorl %r15d, %r15d
movl $0x0, 0xc(%rsp)
movq (%r14,%rbp,8), %r13
cmpb $0x2d, (%r13)
jne 0x14fb
cmpb $0x76, 0x1(%r13)
jne 0x14fb
cmpb $0x0, 0x2(%r13)
je 0x152d
movq %r13, %rdi
movq %r12, %rsi
callq 0x10d0
testl %eax, %eax
je 0x151f
cmpq $0x0, (%rsp)
je 0x1529
testq %r15, %r15
jne 0x1843
movq %r13, %r15
jmp 0x152d
movl $0x1, 0xc(%rsp)
jmp 0x152d
movq %r13, (%rsp)
incq %rbp
cmpq %rbp, %rbx
jne 0x14e2
movq (%rsp), %rbx
testq %rbx, %rbx
je 0x1692
testq %r15, %r15
je 0x1692
leaq 0x1b0c(%rip), %rdi      # 0x305e
movq %rbx, %rsi
movq %r15, %rdx
xorl %eax, %eax
callq 0x10b0
leaq 0x1b0c(%rip), %rsi      # 0x3072
movq %rbx, %rdi
callq 0x1140
testq %rax, %rax
je 0x17f5
movq %rax, %r14
movq %rax, %rdi
callq 0x2ad1
leaq 0x4b4b(%rip), %rax      # 0x60d4
leaq 0x4b48(%rip), %rcx      # 0x60d8
movl (%rcx), %ecx
orl (%rax), %ecx
jne 0x15b4
leaq 0x1bf7(%rip), %rdi      # 0x3194
callq 0x1070
movq %r14, %rdi
callq 0x1090
movl $0x3, %ebp
jmp 0x17e1
movq %r14, %rdi
callq 0x19dd
movq %r14, %rdi
callq 0x288c
movq %rax, %rbx
movq %rax, %rdi
callq 0x1e65
testl %eax, %eax
je 0x1680
movq %r14, 0x20(%rsp)
movq %rbx, 0x28(%rsp)
movq %r15, %rdi
movl $0x2f, %esi
callq 0x10a0
testq %rax, %rax
je 0x16a4
movq %rax, %rbx
movl $0x2, %ebp
leaq 0x30(%rsp), %r13
movq %r15, %r12
movq %rbx, %r14
subq %r12, %r14
movl $0xff, %eax
cmpq %rax, %r14
cmovaeq %rax, %r14
movq %r13, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x10f0
movb $0x0, 0x30(%rsp,%r14)
movq 0x28(%rsp), %r12
movq %r12, %rdi
movl %ebp, %esi
movq %r13, %rdx
leaq 0x10(%rsp), %rcx
callq 0x231d
testl %eax, %eax
je 0x1817
movq %rbx, %r12
incq %r12
movl 0x18(%rsp), %ebp
leaq 0x1a34(%rip), %rdi      # 0x3091
movq %r13, %rsi
movl %ebp, %edx
xorl %eax, %eax
callq 0x10b0
movq %r12, %rdi
movl $0x2f, %esi
callq 0x10a0
movq %rax, %rbx
testq %rax, %rax
jne 0x1607
jmp 0x16ac
movq %rbx, %rdi
callq 0x1030
movl $0x4, %ebp
jmp 0x17e1
movq %r14, %rdi
callq 0x11b2
movl $0x1, %ebp
jmp 0x17e1
movl $0x2, %ebp
movq %r15, %r12
leaq 0x30(%rsp), %rbx
movl $0x100, %edx            # imm = 0x100
movq %rbx, %rdi
movq %r12, %rsi
callq 0x1060
leaq 0x10(%rsp), %rcx
movq 0x28(%rsp), %r12
movq %r12, %rdi
movl %ebp, %esi
movq %rbx, %rdx
callq 0x231d
testl %eax, %eax
je 0x1817
movq 0x10(%rsp), %rdi
addq $0x38, %rdi
callq 0x19c8
movzwl %ax, %edx
leaq 0x19b2(%rip), %rdi      # 0x30aa
xorl %ebp, %ebp
movq %r15, %rsi
xorl %eax, %eax
callq 0x10b0
movq 0x10(%rsp), %rdi
addq $0x38, %rdi
callq 0x19c8
movl %eax, %ebx
movq 0x20(%rsp), %rdi
callq 0x1090
btl $0xe, %ebx
jae 0x1863
cmpl $0x0, 0xc(%rsp)
je 0x17d9
leaq 0x19c9(%rip), %rdi      # 0x3103
movq %r15, %rsi
xorl %eax, %eax
callq 0x10b0
movq 0x10(%rsp), %rdi
addq $0x38, %rdi
callq 0x19c8
movl %eax, %ebx
movzwl %ax, %ebp
andl $0xbfff, %ebx           # imm = 0xBFFF
movl 0x10(%rsp), %ecx
subl %r12d, %ecx
addl $0x38, %ecx
leaq 0x19a3(%rip), %rdi      # 0x3111
movl %ebp, %esi
movl %ebx, %edx
xorl %eax, %eax
callq 0x10b0
movq 0x10(%rsp), %rax
movb %bh, 0x38(%rax)
movb %bpl, 0x39(%rax)
leaq 0x19e8(%rip), %rdi      # 0x3174
callq 0x1070
leaq 0x19a5(%rip), %rsi      # 0x313d
movq (%rsp), %rbx
movq %rbx, %rdi
callq 0x1140
movq %rax, %r14
testq %rax, %rax
je 0x1889
movq %r14, %rdi
movq %r12, %rsi
callq 0x2974
testl %eax, %eax
je 0x18ae
leaq 0x19c2(%rip), %rdi      # 0x318c
callq 0x1070
xorl %ebp, %ebp
movq %r14, %rdi
callq 0x1090
movq %r12, %rdi
callq 0x1030
movl %ebp, %eax
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x47fc(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x186f(%rip), %rsi      # 0x3075
movq %rbx, %rdx
xorl %eax, %eax
callq 0x10e0
movl $0x2, %ebp
jmp 0x17e1
movq 0x20(%rsp), %rdi
callq 0x1090
movq 0x47d0(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x189b(%rip), %rsi      # 0x30cd
movq %r15, %rdx
xorl %eax, %eax
callq 0x10e0
movl $0x2a, %ebp
jmp 0x17d9
movq 0x47ae(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x17b7(%rip), %rsi      # 0x300b
movq %r13, %rdx
xorl %eax, %eax
callq 0x10e0
jmp 0x169a
movq 0x478e(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x1875(%rip), %rsi      # 0x30e9
movq (%rsp), %rdx
xorl %eax, %eax
callq 0x10e0
movl $0x2b, %ebp
jmp 0x17d9
movq 0x4768(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x18a7(%rip), %rsi      # 0x3141
movq %rbx, %rdx
xorl %eax, %eax
callq 0x10e0
movl $0x5, %ebp
jmp 0x17d1
callq 0x1190
movl $0x6, %ebp
jmp 0x17d1
nopl (%rax)