// File: /workspace/github_c_makefile_star2_O3/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 0x63e4c(%rip), %rsi     # 0x145097
xorl %eax, %eax
callq 0xeb152
movl $0x48, %esi
movq %rbx, %rdi
callq 0xe8b2b
movq %rax, %r14
movq 0x18(%rbx), %rdi
leaq 0x63e40(%rip), %rdx     # 0x1450ad
movl $0x3, %ecx
xorl %esi, %esi
callq 0xefc85
testl %eax, %eax
je 0xe1296
leaq 0x63e2d(%rip), %rsi     # 0x1450b1
xorl %r15d, %r15d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9295
jmp 0xe149c
movq 0x18(%rbx), %rdi
movl $0xe, %esi
callq 0xeed61
movq %rax, 0x28(%r14)
leaq 0x3d0ec(%rip), %rsi     # 0x11e39b
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
movl $0x14, %esi
callq 0xeebc8
movq %rax, 0x10(%r14)
leaq 0x63e11(%rip), %rsi     # 0x1450e6
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
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 0xe135d
xorl %edi, %edi
callq 0xf052b
leaq 0x63da9(%rip), %rsi     # 0x1450f7
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9295
jmp 0xe14a4
leaq 0x63784(%rip), %rsi     # 0x144ae8
xorl %r12d, %r12d
movq %rbx, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0xefda1
movq %rax, %r15
leaq 0x63da3(%rip), %rsi     # 0x145120
movl $0x22, %edx
movq %rax, %rdi
callq 0xef547
movl $0xfdc4a7dc, %esi       # imm = 0xFDC4A7DC
movq %r15, %rdi
callq 0xf1042
movq %r15, %rdi
movl $0x1, %esi
callq 0xf0bd6
movq 0x18(%rbx), %rdi
movl $0x3, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0xef481
movq 0x30(%r14), %rsi
movq %r15, %rdi
callq 0xf1042
movq 0x20(%r14), %rsi
movq %r15, %rdi
callq 0xf1042
movq 0x18(%rbx), %rdi
movl $0x4, %esi
movl $0x10, %edx
movq %r15, %rcx
callq 0xef481
movq %r15, %rdi
xorl %esi, %esi
callq 0xf0bd6
movq %r15, %rdi
xorl %esi, %esi
callq 0xf0bd6
cmpq $0x0, 0x10(%r14)
je 0xe1407
movq 0x8(%r14), %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0xf1042
movq 0x10(%r14), %rsi
movq %r15, %rdi
callq 0xf0f95
movq 0x18(%rbx), %rdi
movl $0x16, %esi
movl $0xd, %edx
movq %r15, %rcx
callq 0xef481
movq %r15, %rdi
movl $0x4f, %esi
callq 0xf0bd6
leaq 0x63cfa(%rip), %rsi     # 0x145142
movl $0x5, %edx
movq %r15, %rdi
callq 0xef547
movq 0x18(%rbx), %rdi
movq 0x28(%r14), %rdx
movq 0x40(%r14), %rsi
movq %r15, %rcx
callq 0xef481
movq 0x10(%r14), %rdx
testq %rdx, %rdx
je 0xe1482
movq 0x18(%rbx), %rdi
movq 0x38(%r14), %rsi
movq %r15, %rcx
callq 0xef481
movl $0xfdc4a7dc, %esi       # imm = 0xFDC4A7DC
movq %r15, %rdi
callq 0xf1042
movl $0x30, %esi
movq %r15, %rdi
callq 0xf0d35
movq %r15, %rdi
callq 0xf052b
movq %rbx, %rdi
movq %r14, %rsi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
jmp 0xe8be2