// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/binscii.c
// Function: do_binscii_header2
static void do_binscii_header2(deark *c, struct binscii_ctx *d)
{
	i64 pos;
	u32 hdr_crc_calc;
	u8 is_first_seg;
	struct binscii_segment *seg = &d->cur_seg;
	de_ucstring *fn = NULL;

	// Some fields we process for all segments.
	// Others we only process only for the first segment of a file
	// (or we process them differently).

	// TODO: Does the fn length use d->bmap, or is the coding fixed as
	// 'A'=1, 'B'==2, ... ?
	// (Some BinSCII decoders do it one way, some do it the other.)
	if(d->linebuf[0]>=64+1 && d->linebuf[0]<=64+15) {
		seg->fn_len = (i64)d->linebuf[0] - 64;
	}
	else {
		binscii_set_generic_error(c, d);
		goto done;
	}
	fn = ucstring_create(c);
	ucstring_append_bytes(fn, &d->linebuf[1],
		seg->fn_len, 0, DE_ENCODING_ASCII);
	de_dbg(c, "filename: \"%s\"", ucstring_getpsz_d(fn));

	dbuf_empty(d->tmpdbuf);
	binscii_decode(c, d, &d->linebuf[16], 9, d->tmpdbuf);

	pos = 0;
	seg->orig_len = dbuf_getint_ext(d->tmpdbuf, pos, 3, 1, 0);
	de_dbg(c, "orig len: %"I64_FMT, seg->orig_len);
	pos += 3;

	seg->offset = dbuf_getint_ext(d->tmpdbuf, pos, 3, 1, 0);
	de_dbg(c, "seg offset: %"I64_FMT, seg->offset);
	pos += 3;

	is_first_seg = (seg->offset==0);

	if(is_first_seg) {
		// If we're already in the middle of a file, close it.
		if(d->cur_md) {
			binscii_close_cur_file(c, d);
			if(d->errflag) goto done;
		}

		// Open a new file
		d->cur_md = binscii_create_md(c);
	}

	if(!d->cur_md) {
		binscii_set_generic_error(c, d);
		goto done;
	}

	// After this point, we can freely use both cur_md and cur_seg.

	if(is_first_seg) {
		d->cur_md->orig_len = seg->orig_len;
	}

	if(is_first_seg) {
		// TODO: Better decoding & use of file attributes
		seg->acmode = dbuf_getbyte_p(d->tmpdbuf, &pos);
		de_dbg(c, "access mode: 0x%02x", (UI)seg->acmode);
		seg->filetype = dbuf_getbyte_p(d->tmpdbuf, &pos);
		de_dbg(c, "file type: 0x%02x", (UI)seg->filetype);
		seg->auxtype = (UI)dbuf_getu16le_p(d->tmpdbuf, &pos);
		de_dbg(c, "aux file type: 0x%04x", (UI)seg->auxtype);
		seg->storetype = dbuf_getbyte_p(d->tmpdbuf, &pos);
		de_dbg(c, "storage type: 0x%02x", (UI)seg->storetype);
		seg->size_in_blocks = dbuf_getu16le_p(d->tmpdbuf, &pos);
		de_dbg(c, "orig len in blocks: %"I64_FMT, seg->size_in_blocks);
	}
	else {
		pos += 7;
	}

	if(is_first_seg) {
		seg->crdate_raw = (UI)dbuf_getu16le_p(d->tmpdbuf, &pos);
		seg->crtime_raw = (UI)dbuf_getu16le_p(d->tmpdbuf, &pos);
		de_prodos_datetime_to_timestamp(&d->cur_md->create_time, seg->crdate_raw, seg->crtime_raw);
		dbg_timestamp(c, &d->cur_md->create_time, "create time");
		seg->moddate_raw = (UI)dbuf_getu16le_p(d->tmpdbuf, &pos);
		seg->modtime_raw = (UI)dbuf_getu16le_p(d->tmpdbuf, &pos);
		de_prodos_datetime_to_timestamp(&d->cur_md->mod_time, seg->moddate_raw, seg->modtime_raw);
		dbg_timestamp(c, &d->cur_md->mod_time, "mod time");
	}
	else {
		pos += 8;
	}

	seg->segment_len = dbuf_getint_ext(d->tmpdbuf, pos, 3, 1, 0);
	de_dbg(c, "seg len: %"I64_FMT, seg->segment_len);
	pos += 3;

	seg->hdr_crc_reported = (u32)dbuf_getu16le_p(d->tmpdbuf, &pos);
	de_dbg(c, "header crc (reported): 0x%04x", (UI)seg->hdr_crc_reported);
	de_crcobj_reset(d->crco_header);
	de_crcobj_addslice(d->crco_header, d->tmpdbuf, 0, 24);
	hdr_crc_calc = de_crcobj_getval(d->crco_header);
	de_dbg(c, "header crc (calculated): 0x%04x", (UI)hdr_crc_calc);

	if(hdr_crc_calc!=seg->hdr_crc_reported) {
		de_err(c, "Header CRC check failed for segment at %"I64_FMT, d->cur_seg.pos);
		d->errflag = 1;
		goto done;
	}

	if(seg->offset != d->cur_md->nbytes_written) {
		binscii_set_generic_error(c, d);
		goto done;
	}

	if(is_first_seg) {
		if(!d->cur_md->fn) {
			d->cur_md->fn = ucstring_clone(fn);
		}
		if(!d->cur_md->outf) {
			binscii_create_output_file(c, d);
		}
	}

	d->cur_md->seg_count++;
done:
	ucstring_destroy(fn);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x10(%rbp), %rax
addq $0xc0, %rax
movq %rax, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movq -0x10(%rbp), %rax
movzbl 0x40(%rax), %eax
cmpl $0x41, %eax
jl 0x35738
movq -0x10(%rbp), %rax
movzbl 0x40(%rax), %eax
cmpl $0x4f, %eax
jg 0x35738
movq -0x10(%rbp), %rax
movzbl 0x40(%rax), %eax
movl %eax, %ecx
subq $0x40, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x8(%rax)
jmp 0x3574a
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x35ef0
jmp 0x35c9b
movq -0x8(%rbp), %rdi
callq 0x1711d0
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
movq -0x10(%rbp), %rsi
addq $0x40, %rsi
addq $0x1, %rsi
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rdx
xorl %ecx, %ecx
movl $0x1, %r8d
callq 0x171fa0
movq -0x8(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rdi
callq 0x172650
movq -0x38(%rbp), %rdi
movq %rax, %rdx
leaq 0x191de8(%rip), %rsi    # 0x1c7583
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x169310
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x10(%rbp), %rdx
addq $0x40, %rdx
addq $0x10, %rdx
movq -0x10(%rbp), %rax
movq 0x20(%rax), %r8
movl $0x9, %ecx
callq 0x35f20
movq $0x0, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
movq -0x18(%rbp), %rsi
movl $0x3, %edx
movl $0x1, %ecx
xorl %r8d, %r8d
callq 0x165a80
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rdx
leaq 0x16b77e(%rip), %rsi    # 0x1a0f97
movb $0x0, %al
callq 0x15b0d0
movq -0x18(%rbp), %rax
addq $0x3, %rax
movq %rax, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
movq -0x18(%rbp), %rsi
movl $0x3, %edx
movl $0x1, %ecx
xorl %r8d, %r8d
callq 0x165a80
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rdx
leaq 0x16b73d(%rip), %rsi    # 0x1a0fa5
movb $0x0, %al
callq 0x15b0d0
movq -0x18(%rbp), %rax
addq $0x3, %rax
movq %rax, -0x18(%rbp)
movq -0x28(%rbp), %rax
cmpq $0x0, 0x18(%rax)
sete %al
andb $0x1, %al
movzbl %al, %eax
movb %al, -0x1d(%rbp)
cmpb $0x0, -0x1d(%rbp)
je 0x358d0
movq -0x10(%rbp), %rax
cmpq $0x0, (%rax)
je 0x358bd
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x355f0
movq -0x10(%rbp), %rax
cmpb $0x0, 0xc(%rax)
je 0x358bb
jmp 0x35c9b
jmp 0x358bd
movq -0x8(%rbp), %rdi
callq 0x36010
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movq -0x10(%rbp), %rax
cmpq $0x0, (%rax)
jne 0x358ec
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x35ef0
jmp 0x35c9b
cmpb $0x0, -0x1d(%rbp)
je 0x35905
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rcx
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rcx, 0x8(%rax)
cmpb $0x0, -0x1d(%rbp)
je 0x35a16
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
leaq -0x18(%rbp), %rsi
callq 0x165170
movb %al, %cl
movq -0x28(%rbp), %rax
movb %cl, 0x20(%rax)
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rax
movzbl 0x20(%rax), %edx
leaq 0x16b679(%rip), %rsi    # 0x1a0fb5
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
leaq -0x18(%rbp), %rsi
callq 0x165170
movb %al, %cl
movq -0x28(%rbp), %rax
movb %cl, 0x21(%rax)
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rax
movzbl 0x21(%rax), %edx
leaq 0x16b659(%rip), %rsi    # 0x1a0fc9
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
leaq -0x18(%rbp), %rsi
callq 0x1652c0
movl %eax, %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x24(%rax)
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rax
movl 0x24(%rax), %edx
leaq 0x16b638(%rip), %rsi    # 0x1a0fdb
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
leaq -0x18(%rbp), %rsi
callq 0x165170
movb %al, %cl
movq -0x28(%rbp), %rax
movb %cl, 0x28(%rax)
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rax
movzbl 0x28(%rax), %edx
leaq 0x16b61a(%rip), %rsi    # 0x1a0ff1
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
leaq -0x18(%rbp), %rsi
callq 0x1652c0
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rax
movq 0x30(%rax), %rdx
leaq 0x16b5f9(%rip), %rsi    # 0x1a1006
movb $0x0, %al
callq 0x15b0d0
jmp 0x35a22
movq -0x18(%rbp), %rax
addq $0x7, %rax
movq %rax, -0x18(%rbp)
cmpb $0x0, -0x1d(%rbp)
je 0x35b10
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
leaq -0x18(%rbp), %rsi
callq 0x1652c0
movl %eax, %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x38(%rax)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
leaq -0x18(%rbp), %rsi
callq 0x1652c0
movl %eax, %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x3c(%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
addq $0x38, %rdi
movq -0x28(%rbp), %rax
movl 0x38(%rax), %eax
movl %eax, %esi
movq -0x28(%rbp), %rax
movl 0x3c(%rax), %eax
movl %eax, %edx
callq 0x15db40
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rsi
addq $0x38, %rsi
leaq 0x185484(%rip), %rdx    # 0x1baf1c
callq 0x36040
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
leaq -0x18(%rbp), %rsi
callq 0x1652c0
movl %eax, %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x40(%rax)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
leaq -0x18(%rbp), %rsi
callq 0x1652c0
movl %eax, %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x44(%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
addq $0x28, %rdi
movq -0x28(%rbp), %rax
movl 0x40(%rax), %eax
movl %eax, %esi
movq -0x28(%rbp), %rax
movl 0x44(%rax), %eax
movl %eax, %edx
callq 0x15db40
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rsi
addq $0x28, %rsi
leaq 0x184069(%rip), %rdx    # 0x1b9b72
callq 0x36040
jmp 0x35b1c
movq -0x18(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
movq -0x18(%rbp), %rsi
movl $0x3, %edx
movl $0x1, %ecx
xorl %r8d, %r8d
callq 0x165a80
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x48(%rax)
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rax
movq 0x48(%rax), %rdx
leaq 0x16b4c6(%rip), %rsi    # 0x1a101e
movb $0x0, %al
callq 0x15b0d0
movq -0x18(%rbp), %rax
addq $0x3, %rax
movq %rax, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
leaq -0x18(%rbp), %rsi
callq 0x1652c0
movl %eax, %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x50(%rax)
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rax
movl 0x50(%rax), %edx
leaq 0x1825a2(%rip), %rsi    # 0x1b8139
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdi
callq 0x15f250
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rsi
xorl %eax, %eax
movl %eax, %edx
movl $0x18, %ecx
callq 0x15f500
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdi
callq 0x15f340
movl %eax, -0x1c(%rbp)
movq -0x8(%rbp), %rdi
movl -0x1c(%rbp), %edx
leaq 0x18548a(%rip), %rsi    # 0x1bb071
movb $0x0, %al
callq 0x15b0d0
movl -0x1c(%rbp), %eax
movq -0x28(%rbp), %rcx
cmpl 0x50(%rcx), %eax
je 0x35c21
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0xc0(%rax), %rdx
leaq 0x16b41b(%rip), %rsi    # 0x1a102b
movb $0x0, %al
callq 0x15bfb0
movq -0x10(%rbp), %rax
movb $0x1, 0xc(%rax)
jmp 0x35c9b
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x10(%rbp), %rcx
movq (%rcx), %rcx
cmpq 0x10(%rcx), %rax
je 0x35c45
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x35ef0
jmp 0x35c9b
cmpb $0x0, -0x1d(%rbp)
je 0x35c8d
movq -0x10(%rbp), %rax
movq (%rax), %rax
cmpq $0x0, 0x18(%rax)
jne 0x35c70
movq -0x30(%rbp), %rdi
callq 0x171700
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rcx, 0x18(%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rax
cmpq $0x0, 0x20(%rax)
jne 0x35c8b
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x36090
jmp 0x35c8d
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
movq -0x30(%rbp), %rdi
callq 0x171750
addq $0x40, %rsp
popq %rbp
retq
nopw (%rax,%rax)