// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/psd.c
// Function: do_image_resource
static int do_image_resource(deark *c, lctx *d, zztype *zz)
{
	i64 resource_id;
	i64 block_data_len;
	struct rsrc_info ri;
	de_ucstring *blkname = NULL;
	struct de_fourcc sig4cc;
	const char *signame = "Photoshop";
	zztype czz;
	int retval = 0;

	// Check the "8BIM" (etc.) signature.
	// TODO: Maybe we should allow arbitrary signatures, but restricting it to
	// known signatures lets us know if the parser has gone off the rails.
	psd_read_fourcc_zz(c, d, zz, &sig4cc);
	if(sig4cc.id==CODE_8BIM) {
		;
	}
	else if(sig4cc.id==CODE_AgHg) { // Seen in Photoshop Elements files
		signame = "AgHg";
	}
	else if(sig4cc.id==CODE_DCSR) { // ExifTool says this exists
		signame = "DCSR";
	}
	else if(sig4cc.id==CODE_MeSa) { // Image Ready resource?
		signame = "MeSa";
	}
	else if(sig4cc.id==CODE_PHUT) { // PhotoDeluxe resource?
		signame = "PHUT";
	}
	else {
		de_warn(c, "Bad Photoshop resource block signature '%s' at %d",
			sig4cc.id_sanitized_sz, (int)zz->startpos);
		goto done;
	}

	resource_id = psd_getu16zz(zz);

	// Read resource block name. A "Pascal string" padded to an even number of bytes.
	blkname = ucstring_create(c);
	zz_init(&czz, zz);
	read_pascal_string_to_ucstring(c, d, blkname, &czz);
	zz->pos += de_pad_to_2(zz_used(&czz));

	block_data_len = psd_getu32zz(zz);

	// TODO: Are resource_ids "namespaced" based on the block signature?
	lookup_rsrc(sig4cc.id, (u16)resource_id, &ri);

	de_dbg(c, "%s rsrc 0x%04x (%s) pos=%d blkname=\"%s\" dpos=%d dlen=%d",
		signame, (int)resource_id, ri.idname, (int)zz->startpos,
		ucstring_getpsz(blkname), (int)zz->pos, (int)block_data_len);

	if(zz->pos+block_data_len > zz->endpos) {
		de_warn(c, "PSD rsrc exceeds its parent's bounds. Ends at %"I64_FMT
			", parent ends at %"I64_FMT".", zz->pos+block_data_len, zz->endpos);
	}

	de_dbg_indent(c, 1);
	if(ri.hfn) {
		zz_init_with_len(&czz, zz, block_data_len);
		ri.hfn(c, d, &czz, &ri);
	}
	else if(ri.flags&0x0004) {
		zz_init_with_len(&czz, zz, block_data_len);
		hrsrc_descriptor_with_version(c, d, &czz, &ri);
	}
	else if(c->debug_level>=2) {
		de_dbg_hexdump(c, c->infile, zz->pos, block_data_len, 256, NULL,
			(ri.flags&0x0010)?0x0:0x1);
	}
	de_dbg_indent(c, -1);

	zz->pos += de_pad_to_2(block_data_len);

	retval = 1;

done:
	if(blkname) ucstring_destroy(blkname);
	return retval;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xe0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq $0x0, -0x48(%rbp)
leaq 0xcaf4f(%rip), %rax     # 0x1be985
movq %rax, -0x80(%rbp)
movl $0x0, -0x9c(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
leaq -0x78(%rbp), %rcx
callq 0xf3d80
cmpl $0x3842494d, -0x74(%rbp) # imm = 0x3842494D
jne 0xf3a67
jmp 0xf3af0
cmpl $0x41674867, -0x74(%rbp) # imm = 0x41674867
jne 0xf3a7d
leaq 0xcaf18(%rip), %rax     # 0x1be98f
movq %rax, -0x80(%rbp)
jmp 0xf3aee
cmpl $0x44435352, -0x74(%rbp) # imm = 0x44435352
jne 0xf3a93
leaq 0xcaf07(%rip), %rax     # 0x1be994
movq %rax, -0x80(%rbp)
jmp 0xf3aec
cmpl $0x4d655361, -0x74(%rbp) # imm = 0x4D655361
jne 0xf3aa9
leaq 0xcaef6(%rip), %rax     # 0x1be999
movq %rax, -0x80(%rbp)
jmp 0xf3aea
cmpl $0x50485554, -0x74(%rbp) # imm = 0x50485554
jne 0xf3abf
leaq 0xcaee5(%rip), %rax     # 0x1be99e
movq %rax, -0x80(%rbp)
jmp 0xf3ae8
movq -0x8(%rbp), %rdi
leaq -0x78(%rbp), %rdx
addq $0x8, %rdx
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movl %eax, %ecx
leaq 0xcaec7(%rip), %rsi     # 0x1be9a3
movb $0x0, %al
callq 0x15bc10
jmp 0xf3d56
jmp 0xf3aea
jmp 0xf3aec
jmp 0xf3aee
jmp 0xf3af0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rsi
movq -0x10(%rbp), %rax
movl 0x4(%rax), %edx
callq 0xf3de0
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rdi
callq 0x1711d0
movq %rax, -0x48(%rbp)
movq -0x18(%rbp), %rsi
leaq -0x98(%rbp), %rdi
callq 0xf3580
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x48(%rbp), %rdx
leaq -0x98(%rbp), %rcx
callq 0xf3e30
leaq -0x98(%rbp), %rdi
callq 0xf3820
movq %rax, %rdi
callq 0x15cdf0
movq %rax, %rcx
movq -0x18(%rbp), %rax
addq (%rax), %rcx
movq %rcx, (%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rsi
movq -0x10(%rbp), %rax
movl 0x4(%rax), %edx
callq 0xf3330
movq %rax, -0x28(%rbp)
movl -0x74(%rbp), %edi
movq -0x20(%rbp), %rax
leaq -0x40(%rbp), %rdx
movzwl %ax, %esi
callq 0xf3ee0
movq -0x8(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq -0x80(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq -0x20(%rbp), %rax
movl %eax, -0xac(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movl %eax, -0xa0(%rbp)
movq -0x48(%rbp), %rdi
callq 0x172500
movq -0xc0(%rbp), %rdi
movq -0xb8(%rbp), %rdx
movl -0xac(%rbp), %ecx
movq -0xa8(%rbp), %r8
movl -0xa0(%rbp), %r9d
movq %rax, %r11
movq -0x18(%rbp), %rax
movq (%rax), %rax
movl %eax, %r10d
movq -0x28(%rbp), %rax
leaq 0xcadc8(%rip), %rsi     # 0x1be9d5
movq %r11, (%rsp)
movl %r10d, 0x8(%rsp)
movl %eax, 0x10(%rsp)
movb $0x0, %al
callq 0x15b0d0
movq -0x18(%rbp), %rax
movq (%rax), %rax
addq -0x28(%rbp), %rax
movq -0x18(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jle 0xf3c5b
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq (%rax), %rdx
addq -0x28(%rbp), %rdx
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rcx
leaq 0xcadb9(%rip), %rsi     # 0x1bea0d
movb $0x0, %al
callq 0x15bc10
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
cmpq $0x0, -0x30(%rbp)
je 0xf3ca2
movq -0x18(%rbp), %rsi
movq -0x28(%rbp), %rdx
leaq -0x98(%rbp), %rdi
callq 0xf3380
movq -0x30(%rbp), %rax
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
leaq -0x98(%rbp), %rdx
leaq -0x40(%rbp), %rcx
callq *%rax
jmp 0xf3d28
movl -0x3c(%rbp), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0xf3cdb
movq -0x18(%rbp), %rsi
movq -0x28(%rbp), %rdx
leaq -0x98(%rbp), %rdi
callq 0xf3380
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
leaq -0x98(%rbp), %rdx
leaq -0x40(%rbp), %rcx
callq 0xf4070
jmp 0xf3d26
movq -0x8(%rbp), %rax
cmpl $0x2, (%rax)
jl 0xf3d24
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x18(%rbp), %rax
movq (%rax), %rdx
movq -0x28(%rbp), %rcx
movl -0x3c(%rbp), %r9d
andl $0x10, %r9d
movl $0x1, %eax
xorl %r8d, %r8d
cmpl $0x0, %r9d
cmovnel %r8d, %eax
movl $0x100, %r8d            # imm = 0x100
xorl %r9d, %r9d
movl %eax, (%rsp)
callq 0x15b840
jmp 0xf3d26
jmp 0xf3d28
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x28(%rbp), %rdi
callq 0x15cdf0
movq %rax, %rcx
movq -0x18(%rbp), %rax
addq (%rax), %rcx
movq %rcx, (%rax)
movl $0x1, -0x9c(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0xf3d66
movq -0x48(%rbp), %rdi
callq 0x171750
movl -0x9c(%rbp), %eax
addq $0xe0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)