// File: /workspace/github_c_makefile_star2_O1/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 $0x158, %rsp            # imm = 0x158
movq %rsi, %r14
xorl %ebx, %ebx
cmpl $0x2, %edi
jl 0x153a
movq $0x0, 0x10(%rsp)
movl %edi, %ebx
movl $0x1, %r15d
xorl %r13d, %r13d
xorl %r12d, %r12d
movq (%r14,%r15,8), %rbp
movq %rbp, %rdi
leaq 0x1b20(%rip), %rsi      # 0x3004
callq 0x10d0
testl %eax, %eax
je 0x1522
movq %rbp, %rdi
leaq 0x1b10(%rip), %rsi      # 0x3007
callq 0x10d0
testl %eax, %eax
je 0x1513
testq %r13, %r13
je 0x151f
testq %r12, %r12
jne 0x18de
movq %rbp, %r12
jmp 0x1522
movl $0x1, %eax
movq %rax, 0x10(%rsp)
jmp 0x1522
movq %rbp, %r13
incq %r15
cmpq %r15, %rbx
jne 0x14d6
cmpl $0x0, 0x10(%rsp)
setne %bl
movq %r13, %r15
movq %r12, %r13
jmp 0x1540
xorl %r13d, %r13d
xorl %r15d, %r15d
testq %r15, %r15
je 0x1839
testq %r13, %r13
je 0x1839
leaq 0x1b08(%rip), %rdi      # 0x3061
movq %r15, %rsi
movq %r13, %rdx
xorl %eax, %eax
callq 0x10b0
leaq 0x1b08(%rip), %rsi      # 0x3075
movq %r15, %rdi
callq 0x1140
testq %rax, %rax
je 0x1848
movq %rax, %r12
movq %rax, %rdi
callq 0x2b6f
leaq 0x4b44(%rip), %rax      # 0x60d4
leaq 0x4b41(%rip), %rcx      # 0x60d8
movl (%rcx), %ecx
orl (%rax), %ecx
jne 0x15bb
leaq 0x1bf3(%rip), %rdi      # 0x3197
callq 0x1070
movq %r12, %rdi
callq 0x1090
movl $0x3, %ebp
jmp 0x1825
movq %r12, %rdi
callq 0x1a40
movq %r12, %rdi
callq 0x2935
movq %rax, %r14
movq %rax, %rdi
callq 0x1ec7
movl %eax, 0x24(%rsp)
testl %eax, %eax
movq %r14, 0x18(%rsp)
je 0x171b
movq %r12, 0x40(%rsp)
movq %rbx, 0x10(%rsp)
movq %r15, 0x48(%rsp)
movq %r13, %r15
movl $0x2, %r13d
leaq 0x50(%rsp), %rbp
xorl %ebx, %ebx
movl $0x0, 0xc(%rsp)
movq %r15, 0x38(%rsp)
movq %r15, %rdi
movl $0x2f, %esi
callq 0x10a0
testq %rax, %rax
je 0x1656
movq %rax, %r14
movq %rax, %r12
subq %r15, %r12
movl $0xff, %eax
cmpq %rax, %r12
cmovaeq %rax, %r12
movq %rbp, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x10f0
movb $0x0, 0x50(%rsp,%r12)
incq %r14
movq %r14, %r15
jmp 0x1669
movl $0x100, %edx            # imm = 0x100
movq %rbp, %rdi
movq %r15, %rsi
callq 0x1060
xorl %r15d, %r15d
movq 0x18(%rsp), %rdi
movl %r13d, %esi
movq %rbp, %rdx
leaq 0x28(%rsp), %rcx
callq 0x2389
movl %eax, %r12d
testl %eax, %eax
je 0x16eb
testq %r15, %r15
je 0x16a5
movl 0x30(%rsp), %r13d
leaq 0x19fe(%rip), %rdi      # 0x3094
movq %rbp, %rsi
movl %r13d, %edx
xorl %eax, %eax
callq 0x10b0
jmp 0x16eb
movq 0x28(%rsp), %rdi
addq $0x38, %rdi
callq 0x1a2b
movzwl %ax, %edx
leaq 0x19f0(%rip), %rdi      # 0x30ad
movq 0x38(%rsp), %rsi
xorl %eax, %eax
callq 0x10b0
movq 0x28(%rsp), %rdi
addq $0x38, %rdi
callq 0x1a2b
btl $0xe, %eax
movl $0x1, %ebx
movl 0xc(%rsp), %eax
cmovbl %ebx, %eax
movl %eax, 0xc(%rsp)
testl %r12d, %r12d
je 0x16fd
testq %r15, %r15
je 0x16fd
testl %ebx, %ebx
je 0x1613
testl %ebx, %ebx
setne %r14b
xorl %ebp, %ebp
movq 0x48(%rsp), %r15
movq 0x10(%rsp), %rbx
movq 0x38(%rsp), %r13
movq 0x40(%rsp), %r12
jmp 0x1733
movq %r14, %rdi
callq 0x1030
movl $0x4, %ebp
xorl %r14d, %r14d
movl $0x0, 0xc(%rsp)
cmpl $0x0, 0x24(%rsp)
je 0x1825
movq %r12, %rdi
callq 0x1090
testb %r14b, %r14b
je 0x186a
xorl %ebp, %ebp
movl 0xc(%rsp), %r12d
testl %r12d, %r12d
je 0x1894
andb %r14b, %bl
cmpb $0x1, %bl
jne 0x181b
testl %r12d, %r12d
je 0x181b
leaq 0x198b(%rip), %rdi      # 0x3106
movq %r13, %rsi
xorl %eax, %eax
callq 0x10b0
movq 0x28(%rsp), %rdi
addq $0x38, %rdi
callq 0x1a2b
movl %eax, %ebx
movzwl %ax, %ebp
andl $0xbfff, %ebx           # imm = 0xBFFF
movl 0x28(%rsp), %ecx
movq 0x18(%rsp), %r12
subl %r12d, %ecx
addl $0x38, %ecx
leaq 0x1960(%rip), %rdi      # 0x3114
movl %ebp, %esi
movl %ebx, %edx
xorl %eax, %eax
callq 0x10b0
movq 0x28(%rsp), %rax
movb %bh, 0x38(%rax)
movb %bpl, 0x39(%rax)
leaq 0x19a5(%rip), %rdi      # 0x3177
callq 0x1070
leaq 0x1962(%rip), %rsi      # 0x3140
movq %r15, %rdi
callq 0x1140
movq %rax, %r14
testq %rax, %rax
je 0x18b9
movq %r14, %rdi
movq %r12, %rsi
callq 0x2a1d
testl %eax, %eax
je 0x18fe
leaq 0x1983(%rip), %rdi      # 0x318f
callq 0x1070
xorl %ebp, %ebp
movq %r14, %rdi
callq 0x1090
movq 0x18(%rsp), %rdi
callq 0x1030
movl %ebp, %eax
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rdi
callq 0x11b2
movl $0x1, %ebp
jmp 0x1825
movq 0x47a9(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x181f(%rip), %rsi      # 0x3078
movq %r15, %rdx
xorl %eax, %eax
callq 0x10e0
movl $0x2, %ebp
jmp 0x1825
movq 0x4787(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x1855(%rip), %rsi      # 0x30d0
movq %r13, %rdx
xorl %eax, %eax
callq 0x10e0
movl $0x2a, %ebp
movl 0xc(%rsp), %r12d
jmp 0x175f
movq 0x475d(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x1847(%rip), %rsi      # 0x30ec
movq %r15, %rdx
xorl %eax, %eax
callq 0x10e0
movl $0x2b, %ebp
jmp 0x175f
movq 0x4738(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x187a(%rip), %rsi      # 0x3144
movq %r15, %rdx
xorl %eax, %eax
callq 0x10e0
movl $0x5, %ebp
jmp 0x1813
movq 0x4713(%rip), %rax      # 0x5ff8
movq (%rax), %rdi
leaq 0x171f(%rip), %rsi      # 0x300e
movq %rbp, %rdx
xorl %eax, %eax
callq 0x10e0
jmp 0x1841
callq 0x1190
movl $0x6, %ebp
jmp 0x1813
nopl (%rax)