// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/modules/arc.c
// Function: parse_member_sequence
static void parse_member_sequence(deark *c, lctx *d, i64 pos1, i64 len, int nesting_level,
	member_cb_type member_cbfn)
{
	struct member_parser_data *mpd = NULL;
	int member_idx = 0;
	i64 pos = pos1;

	mpd = de_malloc(c, sizeof(struct member_parser_data));

	while(1) {
		if(pos+2 > pos1+len) break;
		de_zeromem(mpd, sizeof(struct member_parser_data));
		mpd->nesting_level = nesting_level;
		mpd->member_idx = member_idx++;
		mpd->member_pos = pos;

		mpd->magic = de_getbyte_p(&pos);

		if(mpd->magic==0xfe && d->fmt==FMT_PAK16SFX) {
			// Some hacks here. We're not setting all the fields correctly,
			// just the ones we need.
			mpd->cmpr_data_len = (i64)de_getbyte_p(&pos);
			mpd->cmpr_data_pos = pos;
			mpd->member_len = 2+mpd->cmpr_data_len;
			member_cbfn(c, d, mpd);
			pos = mpd->member_pos + mpd->member_len;
			continue;
		}
		else if(mpd->magic!=d->sig_byte) {
			mpd->member_len = 1;
			mpd->cmpr_data_pos = mpd->member_pos; // dummy value
			member_cbfn(c, d, mpd);
			break;
		}
		mpd->cmpr_meth = de_getbyte_p(&pos);
		mpd->cmpr_meth_masked = mpd->cmpr_meth & 0x7f;

		if(mpd->cmpr_meth_masked==0x00 || mpd->cmpr_meth==0x1f) { // end of archive/dir
			mpd->member_len = 2;
			mpd->cmpr_data_pos = mpd->member_pos+2; // dummy value
			member_cbfn(c, d, mpd);
			break;
		}

		if(d->fmt==FMT_ARCMAC) {
			u8 magic2;

			// TODO: Check for EOF?
			pos += 57; // Skip remainder of 59-byte ArcMac preheader
			magic2 = de_getbyte_p(&pos);
			if(magic2 != 0x1a) { // Error
				// TODO: Call member_cbfn()?
				break;
			}

			// Read the "real" compression method field (should be the same?).
			mpd->cmpr_meth = de_getbyte_p(&pos);
			mpd->cmpr_meth_masked = mpd->cmpr_meth & 0x7f;
		}

		pos += 13;
		mpd->cmpr_data_len = de_getu32le_p(&pos);
		pos += 2+2+2;
		if(mpd->cmpr_meth_masked!=0x01) {
			pos += 4; // original size
		}
		if(mpd->cmpr_meth & 0x80) {
			pos += 12; // Spark-specific data
		}

		mpd->cmpr_data_pos = pos;
		mpd->member_len = mpd->cmpr_data_pos + mpd->cmpr_data_len - mpd->member_pos;
		member_cbfn(c, d, mpd);

		pos = mpd->member_pos + mpd->member_len;
	}

	de_free(c, mpd);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %r9, 0x18(%rsp)
movl %r8d, 0x14(%rsp)
movq %rcx, %rbx
movq %rdx, %rbp
movq %rsi, %r15
movq %rdi, %r14
leaq 0x8(%rsp), %rax
movq %rdx, (%rax)
pushq $0x30
popq %rsi
callq 0xdecaf
movq %rax, %r12
addq %rbp, %rbx
movq %rbx, 0x20(%rsp)
movq %rax, %r13
addq $0x8, %r13
xorl %ebx, %ebx
leaq 0x2(%rbp), %rax
cmpq 0x20(%rsp), %rax
jg 0x268c7
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%r13)
movups %xmm0, (%r13)
andq $0x0, 0x20(%r13)
movl 0x14(%rsp), %eax
movl %eax, (%r12)
movl %ebx, 0x4(%r12)
movq %rbp, 0x10(%r12)
movq 0x18(%r14), %rdi
leaq 0x8(%rsp), %rbp
movq %rbp, %rsi
callq 0xe3f2a
movb %al, 0x8(%r12)
cmpb $-0x2, %al
jne 0x26793
cmpl $0x4, (%r15)
jne 0x26793
movq 0x18(%r14), %rdi
movq %rbp, %rsi
callq 0xe3f2a
movzbl %al, %eax
movq %rax, 0x28(%r12)
movq 0x8(%rsp), %rcx
movq %rcx, 0x20(%r12)
addq $0x2, %rax
jmp 0x2686b
cmpb 0x24(%r15), %al
jne 0x268a7
movq 0x18(%r14), %rdi
movq %rbp, %rsi
callq 0xe3f2a
movb %al, 0x9(%r12)
movl %eax, %ecx
andb $0x7f, %cl
movb %cl, 0xa(%r12)
testb %al, %al
je 0x26893
movzbl %al, %eax
cmpl $0x80, %eax
je 0x26893
cmpl $0x1f, %eax
je 0x26893
cmpl $0x3, (%r15)
jne 0x26814
addq $0x39, 0x8(%rsp)
movq 0x18(%r14), %rdi
movq %rbp, %rsi
callq 0xe3f2a
cmpb $0x1a, %al
jne 0x268c7
movq 0x18(%r14), %rdi
leaq 0x8(%rsp), %rbp
movq %rbp, %rsi
callq 0xe3f2a
movb %al, 0x9(%r12)
andb $0x7f, %al
movb %al, 0xa(%r12)
addq $0xd, 0x8(%rsp)
movq 0x18(%r14), %rdi
movq %rbp, %rsi
callq 0xe40bf
movq %rax, 0x28(%r12)
movq 0x8(%rsp), %rdx
leaq 0x6(%rdx), %rcx
movq %rcx, 0x8(%rsp)
cmpb $0x1, 0xa(%r12)
je 0x2684d
addq $0xa, %rdx
movq %rdx, 0x8(%rsp)
movq %rdx, %rcx
cmpb $0x0, 0x9(%r12)
jns 0x2685e
addq $0xc, %rcx
movq %rcx, 0x8(%rsp)
movq %rcx, 0x20(%r12)
addq %rcx, %rax
subq 0x10(%r12), %rax
movq %rax, 0x18(%r12)
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq *0x18(%rsp)
movq 0x18(%r12), %rbp
addq 0x10(%r12), %rbp
movq %rbp, 0x8(%rsp)
incl %ebx
jmp 0x26719
movq $0x2, 0x18(%r12)
movq 0x10(%r12), %rax
addq $0x2, %rax
jmp 0x268b5
movq $0x1, 0x18(%r12)
movq 0x10(%r12), %rax
movq %rax, 0x20(%r12)
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq *0x18(%rsp)
movq %r14, %rdi
movq %r12, %rsi
callq 0xded32
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq