// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x1a0, %rsp            # imm = 0x1A0
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movl $0x0, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
movq $0x0, -0x40(%rbp)
movl $0x0, -0x44(%rbp)
movl $0x0, -0x48(%rbp)
movl $0x0, -0x5c(%rbp)
movl $0x1, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x8(%rbp), %eax
jge 0x13e0
movq -0x10(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x2cda(%rip), %rsi      # 0x4004
callq 0x10d0
cmpl $0x0, %eax
jne 0x1342
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x13d0
movq -0x10(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
leaq 0x2cb2(%rip), %rsi      # 0x4007
callq 0x10d0
cmpl $0x0, %eax
jne 0x1368
movl $0x1, -0x30(%rbp)
jmp 0x13ce
cmpq $0x0, -0x20(%rbp)
jne 0x1381
movq -0x10(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x20(%rbp)
jmp 0x13cc
cmpq $0x0, -0x28(%rbp)
jne 0x139a
movq -0x10(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x28(%rbp)
jmp 0x13ca
movq 0x5c57(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
leaq 0x2c57(%rip), %rsi      # 0x400e
movb $0x0, %al
callq 0x10e0
movl $0x1, -0x4(%rbp)
jmp 0x1823
jmp 0x13cc
jmp 0x13ce
jmp 0x13d0
jmp 0x13d2
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x130b
cmpq $0x0, -0x20(%rbp)
je 0x13ee
cmpq $0x0, -0x28(%rbp)
jne 0x1419
movq 0x5c03(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rdx
leaq 0x2c24(%rip), %rsi      # 0x402a
movb $0x0, %al
callq 0x10e0
movl $0x1, -0x4(%rbp)
jmp 0x1823
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
leaq 0x2c39(%rip), %rdi      # 0x4061
movb $0x0, %al
callq 0x10a0
movq -0x20(%rbp), %rdi
leaq 0x2c3b(%rip), %rsi      # 0x4075
callq 0x1130
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x1472
movq 0x5ba7(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
movq -0x20(%rbp), %rdx
leaq 0x2c19(%rip), %rsi      # 0x4078
movb $0x0, %al
callq 0x10e0
movl $0x2, -0x4(%rbp)
jmp 0x1823
movq -0x18(%rbp), %rdi
callq 0x3520
leaq 0x5c4a(%rip), %rax      # 0x70cc
cmpl $0x0, (%rax)
jne 0x14b6
leaq 0x5c42(%rip), %rax      # 0x70d0
cmpl $0x0, (%rax)
jne 0x14b6
leaq 0x2bfa(%rip), %rdi      # 0x4094
movb $0x0, %al
callq 0x10a0
movq -0x18(%rbp), %rdi
callq 0x1080
movl $0x3, -0x4(%rbp)
jmp 0x1823
movq -0x18(%rbp), %rdi
callq 0x19f0
movq -0x18(%rbp), %rdi
callq 0x30d0
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rdi
callq 0x20b0
cmpl $0x0, %eax
jne 0x14ef
movq -0x40(%rbp), %rdi
callq 0x1030
movl $0x4, -0x4(%rbp)
jmp 0x1823
movq -0x28(%rbp), %rax
movq %rax, -0x68(%rbp)
movl $0x2, -0x174(%rbp)
movq -0x68(%rbp), %rdi
movl $0x2f, %esi
callq 0x1090
movq %rax, -0x180(%rbp)
cmpq $0x0, -0x180(%rbp)
je 0x1584
movq -0x180(%rbp), %rax
movq -0x68(%rbp), %rcx
subq %rcx, %rax
movq %rax, -0x188(%rbp)
cmpq $0x100, -0x188(%rbp) # imm = 0x100
jb 0x154d
movq $0xff, -0x188(%rbp)
leaq -0x170(%rbp), %rdi
movq -0x68(%rbp), %rsi
movq -0x188(%rbp), %rdx
callq 0x10f0
movq -0x188(%rbp), %rax
movb $0x0, -0x170(%rbp,%rax)
movq -0x180(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
jmp 0x15a1
leaq -0x170(%rbp), %rdi
movq -0x68(%rbp), %rsi
movl $0x100, %edx            # imm = 0x100
callq 0x1060
movq $0x0, -0x68(%rbp)
movq -0x40(%rbp), %rdi
movl -0x174(%rbp), %esi
leaq -0x170(%rbp), %rdx
leaq -0x58(%rbp), %rcx
callq 0x2740
cmpl $0x0, %eax
jne 0x15c5
jmp 0x167a
cmpq $0x0, -0x68(%rbp)
je 0x15f2
movl -0x50(%rbp), %eax
movl %eax, -0x174(%rbp)
leaq -0x170(%rbp), %rsi
movl -0x174(%rbp), %edx
leaq 0x2ac9(%rip), %rdi      # 0x40b2
movb $0x0, %al
callq 0x10a0
jmp 0x164c
movq -0x28(%rbp), %rax
movq %rax, -0x198(%rbp)
movq -0x58(%rbp), %rdi
addq $0x38, %rdi
callq 0x1970
movq -0x198(%rbp), %rsi
movzwl %ax, %edx
leaq 0x2ab0(%rip), %rdi      # 0x40cb
movb $0x0, %al
callq 0x10a0
movl $0x1, -0x44(%rbp)
movq -0x58(%rbp), %rdi
addq $0x38, %rdi
callq 0x1970
movzwl %ax, %eax
andl $0x4000, %eax           # imm = 0x4000
cmpl $0x0, %eax
je 0x164a
movl $0x1, -0x48(%rbp)
jmp 0x164c
jmp 0x164e
xorl %eax, %eax
cmpq $0x0, -0x68(%rbp)
movb %al, -0x199(%rbp)
je 0x166c
cmpl $0x0, -0x44(%rbp)
setne %al
xorb $-0x1, %al
movb %al, -0x199(%rbp)
movb -0x199(%rbp), %al
testb $0x1, %al
jne 0x1501
jmp 0x167c
movq -0x18(%rbp), %rdi
callq 0x1080
cmpl $0x0, -0x44(%rbp)
jne 0x16b0
movq 0x5966(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
movq -0x28(%rbp), %rdx
leaq 0x2a4e(%rip), %rsi      # 0x40ee
movb $0x0, %al
callq 0x10e0
movl $0x2a, -0x5c(%rbp)
jmp 0x16db
cmpl $0x0, -0x48(%rbp)
jne 0x16d9
movq 0x593b(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
movq -0x20(%rbp), %rdx
leaq 0x2a3f(%rip), %rsi      # 0x410a
movb $0x0, %al
callq 0x10e0
movl $0x2b, -0x5c(%rbp)
jmp 0x16db
cmpl $0x0, -0x30(%rbp)
je 0x1814
cmpl $0x0, -0x44(%rbp)
je 0x1814
cmpl $0x0, -0x48(%rbp)
je 0x1814
movq -0x28(%rbp), %rsi
leaq 0x2a20(%rip), %rdi      # 0x4124
movb $0x0, %al
callq 0x10a0
movq -0x58(%rbp), %rdi
addq $0x38, %rdi
callq 0x1970
movw %ax, -0x18a(%rbp)
movzwl -0x18a(%rbp), %eax
andl $0xffffbfff, %eax       # imm = 0xFFFFBFFF
movw %ax, -0x18c(%rbp)
movzwl -0x18a(%rbp), %esi
movzwl -0x18c(%rbp), %edx
movq -0x58(%rbp), %rax
addq $0x38, %rax
movq -0x40(%rbp), %rcx
subq %rcx, %rax
movl %eax, %ecx
leaq 0x29da(%rip), %rdi      # 0x4132
movb $0x0, %al
callq 0x10a0
movq -0x58(%rbp), %rdi
addq $0x38, %rdi
movzwl -0x18c(%rbp), %esi
callq 0x1270
leaq 0x29e4(%rip), %rdi      # 0x415e
movb $0x0, %al
callq 0x10a0
movq -0x20(%rbp), %rdi
leaq 0x29eb(%rip), %rsi      # 0x4177
callq 0x1130
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x17c1
movq 0x5855(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
movq -0x20(%rbp), %rdx
leaq 0x29ca(%rip), %rsi      # 0x417b
movb $0x0, %al
callq 0x10e0
movl $0x5, -0x5c(%rbp)
jmp 0x180b
movq -0x18(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x3280
cmpl $0x0, %eax
je 0x17ea
leaq 0x29bd(%rip), %rdi      # 0x4197
movb $0x0, %al
callq 0x10a0
movl $0x0, -0x5c(%rbp)
jmp 0x1809
movq 0x5807(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
leaq 0x29a5(%rip), %rsi      # 0x41a0
movb $0x0, %al
callq 0x10e0
movl $0x6, -0x5c(%rbp)
jmp 0x180b
movq -0x18(%rbp), %rdi
callq 0x1080
movq -0x40(%rbp), %rdi
callq 0x1030
movl -0x5c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x1a0, %rsp            # imm = 0x1A0
popq %rbp
retq
nop