// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/tar.c
// Function: read_exthdr_item
static int read_exthdr_item(deark *c, lctx *d, struct phys_member_data *pmd,
	struct extattr_data *ea,
	i64 pos1, i64 max_len, i64 *bytes_consumed)
{
	struct exthdr_item *ehi = NULL;
	int retval = 0;
	int ret;
	int saved_indent_level;
	i64 offs;
	i64 n;
	enum {
		STATE_LOOKING_FOR_LEN, STATE_READING_LEN,
		STATE_LOOKING_FOR_NAME, STATE_READING_NAME, STATE_DONE
	} state;

	state = STATE_LOOKING_FOR_LEN;
	de_dbg_indent_save(c, &saved_indent_level);
	de_dbg(c, "extended header field at %"I64_FMT, pos1);
	de_dbg_indent(c, 1);

	ehi = de_malloc(c, sizeof(struct exthdr_item));
	ehi->base_pos = pos1;

	if(max_len<1) {
		goto done;
	}

	// Parse one header item. We will read the initial "length" field
	// immediately, because it is needed for proper parsing.
	// For the name and value fields, we only record their location and size.
	for(offs=0; ; offs++) {
		u8 ch;
		int is_whitespace;

		if(state==STATE_DONE) break;

		if(offs>=max_len) goto done;

		// If we know the reported length of this item, enforce it.
		if(state>STATE_READING_LEN && offs>=ehi->fieldlen) goto done;

		ch = de_getbyte(pos1+offs);
		is_whitespace = (ch==' ' || ch==0x09);

		if(state==STATE_LOOKING_FOR_LEN) {
			if(is_whitespace) continue;
			ehi->fieldlen_offs = offs;
			state = STATE_READING_LEN;
		}
		else if(state==STATE_READING_LEN) {
			if(is_whitespace) {
				ehi->fieldlen_len = offs - ehi->fieldlen_offs;
				ret = dbuf_read_ascii_number(c->infile,
					pos1+ehi->fieldlen_offs, ehi->fieldlen_len, 10, &ehi->fieldlen);
				if(!ret) {
					goto done;
				}
				de_dbg(c, "length: %d", (int)ehi->fieldlen);
				if(ehi->fieldlen > max_len) {
					goto done;
				}
				state = STATE_LOOKING_FOR_NAME;
			}
		}
		else if(state==STATE_LOOKING_FOR_NAME) {
			if(is_whitespace) continue;
			ehi->name_offs = offs;
			state = STATE_READING_NAME;
		}
		else if(state==STATE_READING_NAME) {
			if(ch=='=') {
				ehi->name_len = offs - ehi->name_offs;
				ehi->val_offs = offs+1;
				ehi->val_len = ehi->fieldlen - offs - 2;
				if(ehi->val_len<0) goto done;
				state = STATE_DONE;
			}
		}
	}

	// Sanity check: The item must end with a newline
	if(de_getbyte(pos1+ehi->fieldlen-1) != 0x0a) {
		goto done;
	}

	n = de_min_int(ehi->name_len, 256);
	ehi->name = dbuf_read_string(c->infile, pos1+ehi->name_offs,
		n, n, 0, DE_ENCODING_UTF8);
	de_dbg(c, "keyword: \"%s\"", ucstring_getpsz_d(ehi->name->str));

	n = de_min_int(ehi->val_len, 65536);
	ehi->value = dbuf_read_string(c->infile, pos1+ehi->val_offs,
		n, n, 0, DE_ENCODING_UTF8);de_dbg(c, "value: \"%s\"", ucstring_getpsz_d(ehi->value->str));

	if(!de_strncmp(ehi->name->sz, "GNU.sparse.", 11)) {
		ea->main_file_is_special = 1;
	}

	if(!de_strcmp(ehi->name->sz, "path") ||
		!de_strcmp(ehi->name->sz, "GNU.sparse.name"))
	{
		if(!ea->alt_name) ea->alt_name = ucstring_create(c);
		ucstring_empty(ea->alt_name);
		ucstring_append_ucstring(ea->alt_name, ehi->value->str);
	}
	else if(!de_strcmp(ehi->name->sz, "linkpath")) {
		if(!ea->linkname) ea->linkname = ucstring_create(c);
		ucstring_empty(ea->linkname);
		ucstring_append_ucstring(ea->linkname, ehi->value->str);
	}
	else if(!de_strcmp(ehi->name->sz, "mtime")) {
		do_exthdr_timestamp(c, d, pmd, ehi, ea, DE_TIMESTAMPIDX_MODIFY, "mod time");
	}
	else if(!de_strcmp(ehi->name->sz, "atime")) {
		do_exthdr_timestamp(c, d, pmd, ehi, ea, DE_TIMESTAMPIDX_ACCESS, "access time");
	}
	else if(!de_strcmp(ehi->name->sz, "ctime")) {
		do_exthdr_timestamp(c, d, pmd, ehi, ea, DE_TIMESTAMPIDX_ATTRCHANGE, "attrib-change time");
	}
	else if(!de_strcmp(ehi->name->sz, "LIBARCHIVE.creationtime")) {
		do_exthdr_timestamp(c, d, pmd, ehi, ea, DE_TIMESTAMPIDX_CREATE, "create time");
	}
	else if(!de_strcmp(ehi->name->sz, "size")) {
		if(ehi->val_len==0) {
			ea->has_alt_size = 0;
		}
		else {
			ea->has_alt_size = 1;
			ea->alt_size = de_strtoll(ehi->value->sz, NULL, 10);
		}
	}
	// TODO: "hdrcharset"

	*bytes_consumed = ehi->fieldlen;
	retval = 1;

done:
	if(!retval) {
		de_warn(c, "Failed to parse extended header at %"I64_FMT, pos1);
	}
	if(ehi) {
		de_destroy_stringreaderdata(c, ehi->name);
		de_destroy_stringreaderdata(c, ehi->value);
		de_free(c, ehi);
	}
	de_dbg_indent_restore(c, saved_indent_level);
	return retval;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
movl $0x0, -0x5c(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x44(%rbp), %rsi
callq 0x15b580
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rdx
leaq 0x9d1db(%rip), %rsi     # 0x1c75b4
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rdi
movl $0x50, %esi
callq 0x15b780
movq %rax, -0x38(%rbp)
movq -0x28(%rbp), %rcx
movq -0x38(%rbp), %rax
movq %rcx, (%rax)
cmpq $0x1, -0x30(%rbp)
jge 0x12a417
jmp 0x12a9d5
movq $0x0, -0x50(%rbp)
cmpl $0x4, -0x5c(%rbp)
jne 0x12a42a
jmp 0x12a5fb
movq -0x50(%rbp), %rax
cmpq -0x30(%rbp), %rax
jl 0x12a439
jmp 0x12a9d5
cmpl $0x1, -0x5c(%rbp)
jbe 0x12a452
movq -0x50(%rbp), %rax
movq -0x38(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jl 0x12a452
jmp 0x12a9d5
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x28(%rbp), %rsi
addq -0x50(%rbp), %rsi
callq 0x165060
movb %al, -0x5d(%rbp)
movzbl -0x5d(%rbp), %ecx
movb $0x1, %al
cmpl $0x20, %ecx
movb %al, -0x65(%rbp)
je 0x12a485
movzbl -0x5d(%rbp), %eax
cmpl $0x9, %eax
sete %al
movb %al, -0x65(%rbp)
movb -0x65(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x64(%rbp)
cmpl $0x0, -0x5c(%rbp)
jne 0x12a4b9
cmpl $0x0, -0x64(%rbp)
je 0x12a4a1
jmp 0x12a5ea
movq -0x50(%rbp), %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x10(%rax)
movl $0x1, -0x5c(%rbp)
jmp 0x12a5e8
cmpl $0x1, -0x5c(%rbp)
jne 0x12a558
cmpl $0x0, -0x64(%rbp)
je 0x12a553
movq -0x50(%rbp), %rcx
movq -0x38(%rbp), %rax
subq 0x10(%rax), %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x28(%rbp), %rsi
movq -0x38(%rbp), %rax
addq 0x10(%rax), %rsi
movq -0x38(%rbp), %rax
movq 0x18(%rax), %rdx
movq -0x38(%rbp), %r8
addq $0x8, %r8
movl $0xa, %ecx
callq 0x165f70
movl %eax, -0x40(%rbp)
cmpl $0x0, -0x40(%rbp)
jne 0x12a51d
jmp 0x12a9d5
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movl %eax, %edx
leaq 0x8c978(%rip), %rsi     # 0x1b6eaa
movb $0x0, %al
callq 0x15b0d0
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
cmpq -0x30(%rbp), %rax
jle 0x12a54c
jmp 0x12a9d5
movl $0x2, -0x5c(%rbp)
jmp 0x12a5e6
cmpl $0x2, -0x5c(%rbp)
jne 0x12a57e
cmpl $0x0, -0x64(%rbp)
je 0x12a569
jmp 0x12a5ea
movq -0x50(%rbp), %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x20(%rax)
movl $0x3, -0x5c(%rbp)
jmp 0x12a5e4
cmpl $0x3, -0x5c(%rbp)
jne 0x12a5e2
movzbl -0x5d(%rbp), %eax
cmpl $0x3d, %eax
jne 0x12a5e0
movq -0x50(%rbp), %rcx
movq -0x38(%rbp), %rax
subq 0x20(%rax), %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x50(%rbp), %rcx
addq $0x1, %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rcx
subq -0x50(%rbp), %rcx
subq $0x2, %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x38(%rbp), %rax
cmpq $0x0, 0x38(%rax)
jge 0x12a5d9
jmp 0x12a9d5
movl $0x4, -0x5c(%rbp)
jmp 0x12a5e2
jmp 0x12a5e4
jmp 0x12a5e6
jmp 0x12a5e8
jmp 0x12a5ea
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0x12a41f
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x28(%rbp), %rsi
movq -0x38(%rbp), %rax
addq 0x8(%rax), %rsi
subq $0x1, %rsi
callq 0x165060
movzbl %al, %eax
cmpl $0xa, %eax
je 0x12a625
jmp 0x12a9d5
movq -0x38(%rbp), %rax
movq 0x28(%rax), %rdi
movl $0x100, %esi            # imm = 0x100
callq 0x15ccf0
movq %rax, -0x58(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x28(%rbp), %rsi
movq -0x38(%rbp), %rax
addq 0x20(%rax), %rsi
movq -0x58(%rbp), %rdx
movq -0x58(%rbp), %rcx
xorl %r8d, %r8d
movl $0x2, %r9d
callq 0x1664c0
movq %rax, %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x40(%rax)
movq -0x8(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x38(%rbp), %rax
movq 0x40(%rax), %rax
movq 0x18(%rax), %rdi
callq 0x172650
movq -0x78(%rbp), %rdi
movq %rax, %rdx
leaq 0x9cf3a(%rip), %rsi     # 0x1c75d1
movb $0x0, %al
callq 0x15b0d0
movq -0x38(%rbp), %rax
movq 0x38(%rax), %rdi
movl $0x10000, %esi          # imm = 0x10000
callq 0x15ccf0
movq %rax, -0x58(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x28(%rbp), %rsi
movq -0x38(%rbp), %rax
addq 0x30(%rax), %rsi
movq -0x58(%rbp), %rdx
movq -0x58(%rbp), %rcx
xorl %r8d, %r8d
movl $0x2, %r9d
callq 0x1664c0
movq %rax, %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x48(%rax)
movq -0x8(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x38(%rbp), %rax
movq 0x48(%rax), %rax
movq 0x18(%rax), %rdi
callq 0x172650
movq -0x70(%rbp), %rdi
movq %rax, %rdx
leaq 0x7411b(%rip), %rsi     # 0x19e82b
movb $0x0, %al
callq 0x15b0d0
movq -0x38(%rbp), %rax
movq 0x40(%rax), %rax
movq 0x8(%rax), %rdi
leaq 0x9ceb5(%rip), %rsi     # 0x1c75df
movl $0xb, %edx
callq 0x19050
cmpl $0x0, %eax
jne 0x12a741
movq -0x20(%rbp), %rax
movb $0x1, 0x10(%rax)
movq -0x38(%rbp), %rax
movq 0x40(%rax), %rax
movq 0x8(%rax), %rdi
leaq 0x94552(%rip), %rsi     # 0x1beca6
callq 0x191a0
cmpl $0x0, %eax
je 0x12a77b
movq -0x38(%rbp), %rax
movq 0x40(%rax), %rax
movq 0x8(%rax), %rdi
leaq 0x9ce7a(%rip), %rsi     # 0x1c75eb
callq 0x191a0
cmpl $0x0, %eax
jne 0x12a7c1
movq -0x20(%rbp), %rax
cmpq $0x0, (%rax)
jne 0x12a798
movq -0x8(%rbp), %rdi
callq 0x1711d0
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, (%rax)
movq -0x20(%rbp), %rax
movq (%rax), %rdi
callq 0x171210
movq -0x20(%rbp), %rax
movq (%rax), %rdi
movq -0x38(%rbp), %rax
movq 0x48(%rax), %rax
movq 0x18(%rax), %rsi
callq 0x171460
jmp 0x12a9bf
movq -0x38(%rbp), %rax
movq 0x40(%rax), %rax
movq 0x8(%rax), %rdi
leaq 0x9ce27(%rip), %rsi     # 0x1c75fb
callq 0x191a0
cmpl $0x0, %eax
jne 0x12a828
movq -0x20(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jne 0x12a7fd
movq -0x8(%rbp), %rdi
callq 0x1711d0
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x171210
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x38(%rbp), %rax
movq 0x48(%rax), %rax
movq 0x18(%rax), %rsi
callq 0x171460
jmp 0x12a9bd
movq -0x38(%rbp), %rax
movq 0x40(%rax), %rax
movq 0x8(%rax), %rdi
leaq 0x9cc20(%rip), %rsi     # 0x1c745b
callq 0x191a0
cmpl $0x0, %eax
jne 0x12a871
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
movq -0x38(%rbp), %rcx
movq -0x20(%rbp), %r8
xorl %r9d, %r9d
leaq 0x8f30f(%rip), %rax     # 0x1b9b72
movq %rax, (%rsp)
callq 0x12aa40
jmp 0x12a9bb
movq -0x38(%rbp), %rax
movq 0x40(%rax), %rax
movq 0x8(%rax), %rdi
leaq 0x9cd80(%rip), %rsi     # 0x1c7604
callq 0x191a0
cmpl $0x0, %eax
jne 0x12a8bd
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
movq -0x38(%rbp), %rcx
movq -0x20(%rbp), %r8
movl $0x2, %r9d
leaq 0x8d5c5(%rip), %rax     # 0x1b7e74
movq %rax, (%rsp)
callq 0x12aa40
jmp 0x12a9b9
movq -0x38(%rbp), %rax
movq 0x40(%rax), %rax
movq 0x8(%rax), %rdi
leaq 0x9cd3a(%rip), %rsi     # 0x1c760a
callq 0x191a0
cmpl $0x0, %eax
jne 0x12a909
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
movq -0x38(%rbp), %rcx
movq -0x20(%rbp), %r8
movl $0x3, %r9d
leaq 0x9cd15(%rip), %rax     # 0x1c7610
movq %rax, (%rsp)
callq 0x12aa40
jmp 0x12a9b7
movq -0x38(%rbp), %rax
movq 0x40(%rax), %rax
movq 0x8(%rax), %rdi
leaq 0x9cd07(%rip), %rsi     # 0x1c7623
callq 0x191a0
cmpl $0x0, %eax
jne 0x12a952
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
movq -0x38(%rbp), %rcx
movq -0x20(%rbp), %r8
movl $0x1, %r9d
leaq 0x905d5(%rip), %rax     # 0x1baf1c
movq %rax, (%rsp)
callq 0x12aa40
jmp 0x12a9b5
movq -0x38(%rbp), %rax
movq 0x40(%rax), %rax
movq 0x8(%rax), %rdi
leaq 0x78750(%rip), %rsi     # 0x1a30b5
callq 0x191a0
cmpl $0x0, %eax
jne 0x12a9b3
movq -0x38(%rbp), %rax
cmpq $0x0, 0x38(%rax)
jne 0x12a984
movq -0x20(%rbp), %rax
movb $0x0, 0x11(%rax)
jmp 0x12a9b1
movq -0x20(%rbp), %rax
movb $0x1, 0x11(%rax)
movq -0x38(%rbp), %rax
movq 0x48(%rax), %rax
movq 0x8(%rax), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0xa, %edx
callq 0x18fa00
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x18(%rax)
jmp 0x12a9b3
jmp 0x12a9b5
jmp 0x12a9b7
jmp 0x12a9b9
jmp 0x12a9bb
jmp 0x12a9bd
jmp 0x12a9bf
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rcx
movq 0x10(%rbp), %rax
movq %rcx, (%rax)
movl $0x1, -0x3c(%rbp)
cmpl $0x0, -0x3c(%rbp)
jne 0x12a9f1
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rdx
leaq 0x9cc51(%rip), %rsi     # 0x1c763b
movb $0x0, %al
callq 0x15bc10
cmpq $0x0, -0x38(%rbp)
je 0x12aa27
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rax
movq 0x40(%rax), %rsi
callq 0x166830
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rax
movq 0x48(%rax), %rsi
callq 0x166830
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x15b820
movq -0x8(%rbp), %rdi
movl -0x44(%rbp), %esi
callq 0x15b5a0
movl -0x3c(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq
nop