// File: /workspace/github_c_makefile_star2_O1/jsummers[P]deark/modules/zoo.c
// Function: de_run_zoo_z
static void de_run_zoo_z(deark *c, de_module_params *mparams)
{
	dbuf *outf = NULL;
	static const u8 archivehdr[34] = {0x5a,0x4f,0x4f,0x20,0x31,0x2e,0x32,0x30,0x20,0x41,
		0x72,0x63,0x68,0x69,0x76,0x65,0x2e,0x1a,0x00,0x00,0xdc,0xa7,0xc4,0xfd,0x22,0x00,
		0x00,0x00,0xde,0xff,0xff,0xff,0x01,0x01};
	struct zoo_z_ctx *zctx = NULL;
	int need_errmsg = 0;

	de_declare_fmtf(c, "Zoo Z, DOS-compatible");

	zctx = de_malloc(c, sizeof(struct zoo_z_ctx));
	if(dbuf_memcmp(c->infile, 0, "\xfe\x07\x01", 3)) {
		de_err(c, "File not in Zoo Z format, or not a supported version");
		goto done;
	}

	need_errmsg = 1;
	zctx->cmpr_len = de_getu32le(14);
	de_dbg(c, "compressed size: %"I64_FMT, zctx->cmpr_len);
	zctx->comment_len = de_getu16le(20);
	de_dbg(c, "comment: size=%d", (int)zctx->comment_len);

	// Figure out where everything will go.
	zctx->outf_member_pos = 34;
	zctx->outf_leader_pos = zctx->outf_member_pos + 52;
	zctx->outf_cmpr_pos = zctx->outf_leader_pos + 5;
	zctx->outf_comment_pos = zctx->outf_cmpr_pos + zctx->cmpr_len;
	zctx->outf_trailer_pos = zctx->outf_comment_pos + zctx->comment_len;
	zctx->inf_comment_pos = 36;
	zctx->inf_cmpr_pos = zctx->inf_comment_pos + zctx->comment_len;

	if(zctx->inf_comment_pos+zctx->comment_len > c->infile->len) goto done;
	if(zctx->inf_cmpr_pos+zctx->cmpr_len > c->infile->len) goto done;

	outf = dbuf_create_output_file(c, "zoo", NULL, 0);

	// Archive header
	dbuf_write(outf, archivehdr, 34);

	// Main member header
	dbuf_writeu32le(outf, ZOO_SIGNATURE);
	dbuf_writebyte(outf, 1); // "type"
	dbuf_copy(c->infile, 3, 1, outf); // packing method

	dbuf_writeu32le(outf, zctx->outf_trailer_pos);
	dbuf_writeu32le(outf, zctx->outf_cmpr_pos);

	// date, time, crc, sizeorig, sizenow, maj ver, min ver
	dbuf_copy(c->infile, 4, 16, outf);

	dbuf_writebyte(outf, 0); // "deleted" flag
	dbuf_writebyte(outf, 0); // file structure / reserved
	dbuf_writeu32le(outf, zctx->comment_len?zctx->outf_comment_pos:0);
	dbuf_writeu16le(outf, zctx->comment_len);
	dbuf_copy(c->infile, 22, 13, outf); // filename
	dbuf_writebyte(outf, 0x4f); // ??? This seems to be what Zoo does

	dbuf_write(outf, (const u8*)"@)#(\0", 5); // leader
	dbuf_copy(c->infile, zctx->inf_cmpr_pos, zctx->cmpr_len, outf); // cmpr data

	if(zctx->comment_len) {
		dbuf_copy(c->infile, zctx->inf_comment_pos, zctx->comment_len, outf);
	}

	dbuf_writeu32le(outf, ZOO_SIGNATURE);
	dbuf_write_zeroes(outf, 48);
	need_errmsg = 0;

done:
	dbuf_close(outf);
	if(need_errmsg) {
		de_err(c, "Conversion to Zoo format failed");
	}
	de_free(c, zctx);
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
leaq 0x62f4b(%rip), %rsi     # 0x144bb7
xorl %eax, %eax
callq 0xeb703
movl $0x48, %esi
movq %rbx, %rdi
callq 0xe969b
movq %rax, %r14
movq 0x18(%rbx), %rdi
leaq 0x62f3f(%rip), %rdx     # 0x144bcd
movl $0x3, %ecx
xorl %esi, %esi
callq 0xf025e
testl %eax, %eax
je 0xe1cb7
leaq 0x62f2c(%rip), %rsi     # 0x144bd1
xorl %r15d, %r15d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9df7
jmp 0xe1ebd
movq 0x18(%rbx), %rdi
movl $0xe, %esi
callq 0xef37e
movq %rax, 0x28(%r14)
leaq 0x3c5bb(%rip), %rsi     # 0x11e28b
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
movl $0x14, %esi
callq 0xef1ca
movq %rax, 0x10(%r14)
leaq 0x62f10(%rip), %rsi     # 0x144c06
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
movq $0x22, (%r14)
movq $0x56, 0x18(%r14)
movq $0x5b, 0x20(%r14)
movq 0x10(%r14), %rax
movq 0x28(%r14), %rcx
leaq 0x5b(%rcx), %rdx
movq %rdx, 0x8(%r14)
leaq 0x5b(%rax,%rcx), %rdx
movq %rdx, 0x30(%r14)
movq $0x24, 0x38(%r14)
leaq 0x24(%rax), %rdx
movq %rdx, 0x40(%r14)
movq 0x18(%rbx), %rsi
movq 0x18(%rsi), %rsi
cmpq %rsi, %rdx
setg %dl
addq %rcx, %rax
addq $0x24, %rax
cmpq %rsi, %rax
setg %al
orb %dl, %al
je 0xe1d7e
xorl %edi, %edi
callq 0xf0b13
leaq 0x62ea8(%rip), %rsi     # 0x144c17
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9df7
jmp 0xe1ec5
leaq 0x62883(%rip), %rsi     # 0x144608
xorl %r12d, %r12d
movq %rbx, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0xf037a
movq %rax, %r15
leaq 0x62ea2(%rip), %rsi     # 0x144c40
movl $0x22, %edx
movq %rax, %rdi
callq 0xefb82
movl $0xfdc4a7dc, %esi       # imm = 0xFDC4A7DC
movq %r15, %rdi
callq 0xf1645
movq %r15, %rdi
movl $0x1, %esi
callq 0xf11be
movq 0x18(%rbx), %rdi
movl $0x3, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0xefabc
movq 0x30(%r14), %rsi
movq %r15, %rdi
callq 0xf1645
movq 0x20(%r14), %rsi
movq %r15, %rdi
callq 0xf1645
movq 0x18(%rbx), %rdi
movl $0x4, %esi
movl $0x10, %edx
movq %r15, %rcx
callq 0xefabc
movq %r15, %rdi
xorl %esi, %esi
callq 0xf11be
movq %r15, %rdi
xorl %esi, %esi
callq 0xf11be
cmpq $0x0, 0x10(%r14)
je 0xe1e28
movq 0x8(%r14), %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0xf1645
movq 0x10(%r14), %rsi
movq %r15, %rdi
callq 0xf1598
movq 0x18(%rbx), %rdi
movl $0x16, %esi
movl $0xd, %edx
movq %r15, %rcx
callq 0xefabc
movq %r15, %rdi
movl $0x4f, %esi
callq 0xf11be
leaq 0x62df9(%rip), %rsi     # 0x144c62
movl $0x5, %edx
movq %r15, %rdi
callq 0xefb82
movq 0x18(%rbx), %rdi
movq 0x28(%r14), %rdx
movq 0x40(%r14), %rsi
movq %r15, %rcx
callq 0xefabc
movq 0x10(%r14), %rdx
testq %rdx, %rdx
je 0xe1ea3
movq 0x18(%rbx), %rdi
movq 0x38(%r14), %rsi
movq %r15, %rcx
callq 0xefabc
movl $0xfdc4a7dc, %esi       # imm = 0xFDC4A7DC
movq %r15, %rdi
callq 0xf1645
movl $0x30, %esi
movq %r15, %rdi
callq 0xf1346
movq %r15, %rdi
callq 0xf0b13
movq %rbx, %rdi
movq %r14, %rsi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
jmp 0xe9752