// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/modules/aldus.c
// Function: aldus_main
static void aldus_main(deark *c, de_module_params *mparams, UI fmtcode)
{
	lctx *d = NULL;
	i64 pos;
	i64 n;
	u8 frag_flag = 0;

	d = de_malloc(c, sizeof(lctx));
	d->fmtcode = fmtcode;
	d->rfork.is_rsrc_fork = 1;
	d->dfork.forkname = "data";
	d->rfork.forkname = "resource";

	d->advf = de_advfile_create(c);
	d->advf->userdata = (void*)d;
	d->advf->enable_wbuffer = 1;

	// For lack of a better idea, this is how we distinguish Mac and PC formats.
	if(d->fmtcode==1 && de_getbyte(60)!=0 && de_getbyte(64)!=0) {
		d->has_4cc_codes = 1;
	}

	d->input_encoding = de_get_input_encoding(c, NULL,
		(d->has_4cc_codes?DE_ENCODING_MACROMAN:DE_ENCODING_WINDOWS1252));

	d->fork1pos = de_getu16be(16);
	de_dbg(c, "first fork pos: %"I64_FMT, d->fork1pos);
	if(d->fork1pos<100) {
		d->need_errmsg = 1;
		goto done;
	}

	d->fname = dbuf_read_string(c->infile, 18, 32, 32, DE_CONVFLAG_STOP_AT_NUL,
		d->input_encoding);
	de_dbg(c, "filename: \"%s\"", ucstring_getpsz_d(d->fname->str));

	pos = 50;
	d->total_orig_len = de_getu32be_p(&pos);
	de_dbg(c, "orig len (sum of all forks): %"I64_FMT, d->total_orig_len);

	read_timestamp(c, d); // offset 54 or 68

	if(d->has_4cc_codes) {
		read_typecreator_codes(c, d, 60);
	}

	n = de_getu32be(78);
	if(n != d->fork1pos) {
		// The 2-byte field at offset 16, and the 4(?)-byte field at offset 78,
		// are always the same. Either 100 or 200.
		// So I don't know which is the one I want.
		d->need_errmsg = 1;
		goto done;
	}

	if(d->fmtcode==1) {
		d->no_dfork_flag = de_getbyte(82);
	}

	if(d->fmtcode==2) {
		frag_flag = de_getbyte(98);
	}
	if(frag_flag) {
		de_err(c, "Unsupported type of file (fragmented?)");
		goto done;
	}

	if(d->no_dfork_flag) {
		d->rfork.pos = d->fork1pos;
		aldus_read_fork(c, d, &d->rfork);
		if(d->errflag) goto done;
	}
	else {
		// We're assuming that if both forks exist, the data fork is always
		// first.
		d->dfork.pos = d->fork1pos;
		aldus_read_fork(c, d, &d->dfork);
		if(d->errflag) goto done;

		if(d->found_rsrc_fork) {
			aldus_read_fork(c, d, &d->rfork);
			if(d->errflag) goto done;
		}
	}

	ucstring_append_ucstring(d->advf->filename, d->fname->str);
	d->advf->original_filename_flag = 1;

	de_advfile_set_orig_filename(d->advf, d->fname->sz,
		d->fname->sz_strlen);

	if(!d->dfork.fork_exists && !d->rfork.fork_exists) goto done;

	if(d->dfork.fork_exists) {
		d->advf->mainfork.fork_exists = 1;
		d->advf->mainfork.fork_len = d->dfork.orig_len;
	}
	if(d->rfork.fork_exists) {
		d->advf->rsrcfork.fork_exists = 1;
		d->advf->rsrcfork.fork_len = d->rfork.orig_len;
	}

	d->advf->writefork_cbfn = my_advfile_cbfn;

	de_advfile_run(d->advf);

done:
	if(d) {
		if(d->need_errmsg) {
			de_err(c, "Bad or unsupported file");
		}
		de_advfile_destroy(d->advf);
		destroy_fork_contents(c, &d->dfork);
		destroy_fork_contents(c, &d->rfork);
		de_destroy_stringreaderdata(c, d->fname);
		de_free(c, d);
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %esi, %ebp
movq %rdi, %rbx
movl $0x110, %esi            # imm = 0x110
callq 0xdecaf
movq %rax, %r14
movl %ebp, (%rax)
movb $0x1, %bpl
movb %bpl, 0x91(%rax)
leaq 0xf49af(%rip), %rax     # 0x12d031
movq %rax, 0xe8(%r14)
leaq 0xeafdd(%rip), %rax     # 0x12366d
movq %rax, 0xa8(%r14)
movq %rbx, %rdi
callq 0xf07cc
movq %rax, 0x20(%r14)
movq %r14, 0x8(%rax)
movb %bpl, 0x78(%rax)
cmpl $0x1, (%r14)
jne 0x386d6
movq 0x18(%rbx), %rdi
pushq $0x3c
popq %rsi
callq 0xe3eb4
testb %al, %al
je 0x386d6
movq 0x18(%rbx), %rdi
pushq $0x40
popq %rsi
callq 0xe3eb4
testb %al, %al
je 0x386d6
movb $0x1, 0xa(%r14)
leaq 0x90(%r14), %r15
leaq 0xd0(%r14), %r12
xorl %eax, %eax
cmpb $0x0, 0xa(%r14)
setne %al
leal 0x9(,%rax,8), %edx
movq %rbx, %rdi
xorl %esi, %esi
callq 0xe08d2
movl %eax, 0x4(%r14)
movq 0x18(%rbx), %rdi
pushq $0x10
popq %rsi
callq 0xe3f46
movq %rax, 0x10(%r14)
leaq 0xd3729(%rip), %rsi     # 0x10be43
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
cmpq $0x63, 0x10(%r14)
jle 0x38934
movq 0x18(%rbx), %rdi
movl 0x4(%r14), %r9d
pushq $0x12
popq %rsi
pushq $0x20
popq %rcx
pushq $0x1
popq %r8
movq %rcx, %rdx
callq 0xe495c
movq %rax, 0x28(%r14)
movq 0x18(%rax), %rdi
callq 0xeb877
leaq 0xf62fe(%rip), %rsi     # 0x12ea5e
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
leaq 0x8(%rsp), %rsi
movq $0x32, (%rsi)
movq 0x18(%rbx), %rdi
callq 0xe4071
movq %rax, 0x18(%r14)
leaq 0xd36ca(%rip), %rsi     # 0x10be57
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
movq 0x20(%r14), %rax
movq 0x20(%rax), %r13
addq $0x18, %r13
movl (%r14), %eax
cmpl $0x1, %eax
je 0x387dc
cmpl $0x2, %eax
jne 0x38847
movq 0x18(%rbx), %rdi
pushq $0x36
popq %rsi
callq 0xe409f
movl $0x83ad2181, %ecx       # imm = 0x83AD2181
cmpq %rcx, %rax
jl 0x38847
movabsq $-0x83ad2180, %rcx      # imm = 0xFFFFFFFF7C52DE80
addq %rcx, %rax
jmp 0x38811
cmpb $0x0, 0xa(%r14)
movq 0x18(%rbx), %rdi
je 0x38804
pushq $0x44
popq %rsi
callq 0xe404f
cmpq $0x1000001, %rax        # imm = 0x1000001
jl 0x38847
movq %rax, %rdi
movq %r13, %rsi
callq 0xe0102
jmp 0x3881e
pushq $0x36
popq %rsi
callq 0xe40f2
testq %rax, %rax
jle 0x38847
movq %rax, %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0xe002e
leaq 0x10(%rsp), %rbp
pushq $0x40
popq %rdx
movq %r13, %rdi
movq %rbp, %rsi
xorl %ecx, %ecx
callq 0xe0644
leaq 0xd367d(%rip), %rsi     # 0x10beb7
movq %rbx, %rdi
movq %rbp, %rdx
xorl %eax, %eax
callq 0xde896
cmpb $0x0, 0xa(%r14)
je 0x388db
movq 0x18(%rbx), %rdi
leaq 0x30(%r14), %rdx
pushq $0x3c
popq %rsi
pushq $0x4
popq %rbp
movl %ebp, %ecx
xorl %r8d, %r8d
callq 0xe657c
leaq 0x40(%r14), %rdx
leaq 0xcf03b(%rip), %rsi     # 0x1078b0
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
movq 0x20(%r14), %rax
movl 0x30(%r14), %ecx
movl %ecx, 0x90(%rax)
movq 0x20(%r14), %rax
movb $0x1, %r13b
movb %r13b, 0x7b(%rax)
movq 0x18(%rbx), %rdi
leaq 0x60(%r14), %rdx
pushq $0x40
popq %rsi
movl %ebp, %ecx
xorl %r8d, %r8d
callq 0xe657c
movq %r14, %rdx
addq $0x70, %rdx
leaq 0xee758(%rip), %rsi     # 0x127013
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
movq 0x20(%r14), %rax
movl 0x60(%r14), %ecx
movl %ecx, 0x94(%rax)
movq 0x20(%r14), %rax
movb %r13b, 0x7c(%rax)
movq 0x18(%rbx), %rdi
pushq $0x4e
popq %rsi
callq 0xe404f
cmpq 0x10(%r14), %rax
jne 0x38934
movl (%r14), %eax
cmpl $0x1, %eax
jne 0x38908
movq 0x18(%rbx), %rdi
pushq $0x52
popq %rsi
callq 0xe3eb4
movb %al, 0xb(%r14)
movl (%r14), %eax
cmpl $0x2, %eax
jne 0x38996
movq 0x18(%rbx), %rdi
pushq $0x62
popq %rsi
callq 0xe3eb4
testb %al, %al
je 0x38996
leaq 0xd3550(%rip), %rsi     # 0x10be78
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf2a5
jmp 0x38939
movb $0x1, 0x9(%r14)
cmpb $0x0, 0x9(%r14)
je 0x38951
leaq 0xd3558(%rip), %rsi     # 0x10be9f
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf2a5
movq 0x20(%r14), %rdi
callq 0xf0812
movq %rbx, %rdi
movq %r12, %rsi
callq 0x38d31
movq %rbx, %rdi
movq %r15, %rsi
callq 0x38d31
movq 0x28(%r14), %rsi
movq %rbx, %rdi
callq 0xe4b94
movq %rbx, %rdi
movq %r14, %rsi
callq 0xded32
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpb $0x0, 0xb(%r14)
movq 0x10(%r14), %rax
je 0x38a18
movq %rax, 0x98(%r14)
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x38a7a
cmpb $0x0, 0x8(%r14)
jne 0x38939
movq 0x20(%r14), %rax
movq 0x28(%r14), %rcx
movq 0x68(%rax), %rdi
movq 0x18(%rcx), %rsi
callq 0xead71
movq 0x20(%r14), %rdi
movb $0x1, 0x79(%rdi)
movq 0x28(%r14), %rax
movq 0x8(%rax), %rsi
movq 0x10(%rax), %rdx
callq 0xf0864
cmpb $0x0, 0xd0(%r14)
je 0x38a48
movq 0x20(%r14), %rdi
movb $0x1, 0x10(%rdi)
movq 0xe0(%r14), %rax
movq %rax, 0x18(%rdi)
cmpb $0x0, 0x90(%r14)
jne 0x38a56
jmp 0x38a65
movq %rax, 0xd8(%r14)
movq %rbx, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x38a7a
cmpb $0x0, 0x8(%r14)
jne 0x38939
cmpb $0x0, 0xc(%r14)
jne 0x389a8
jmp 0x389c1
cmpb $0x0, (%r15)
je 0x38939
movq 0x20(%r14), %rdi
movb $0x1, 0x38(%rdi)
movq 0xa0(%r14), %rax
movq %rax, 0x40(%rdi)
leaq 0x291(%rip), %rax       # 0x38cfd
movq %rax, 0x60(%rdi)
callq 0xf08e8
jmp 0x38939