// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/tar.c
// Function: read_phys_member_header
static int read_phys_member_header(deark *c, lctx *d,
	struct phys_member_data *pmd, i64 pos1)
{
	i64 n;
	int ret;
	i64 pos = pos1;
	de_ucstring *tmpstr = NULL;
	int retval = 0;
	int saved_indent_level;
	u8 hdrblock[512];

	de_dbg_indent_save(c, &saved_indent_level);

	de_dbg(c, "physical archive member header at %"I64_FMT, pos1);
	de_dbg_indent(c, 1);

	// Look ahead to try to figure out some things about the format of this member.

	de_read(hdrblock, pos1, 512);
	calc_checksum(c, d, pmd, hdrblock);

	if(pmd->checksum_calc==8*32 && de_is_all_zeroes(&hdrblock[148], 8)) {
		// "The end of the archive is indicated by two records consisting
		// entirely of zero bytes."
		// Most tar programs seem to stop at the first "zero block", so that's
		// what we'll do.
		de_dbg(c, "[trailer record]");
		d->found_trailer = 1;
		goto done;
	}

	pmd->linkflag = hdrblock[156];

	if(!de_memcmp(&hdrblock[257], (const void*)"ustar  \0", 8)) {
		pmd->fmt = TARFMT_GNU;
	}
	else if(!de_memcmp(&hdrblock[257], (const void*)"ustar\0", 6)) {
		pmd->fmt = TARFMT_POSIX;
	}
	else if(!de_memcmp(&hdrblock[508], (const void*)"tar\0", 4)) {
		pmd->fmt = TARFMT_STAR;
	}

	de_dbg(c, "tar format: %s", get_fmt_name(pmd->fmt));

	pmd->name = ucstring_create(c);
	dbuf_read_to_ucstring(c->infile, pos, 100, pmd->name, DE_CONVFLAG_STOP_AT_NUL,
		d->input_encoding);
	pos += 100;
	de_dbg(c, "name: \"%s\"", ucstring_getpsz_d(pmd->name));

	ret = read_ascii_octal_number(c->infile, pos, 8, &pmd->mode);
	if(ret) {
		de_dbg(c, "mode: octal(%06o)", (unsigned int)pmd->mode);
	}
	pos += 8;

	ret = read_ascii_octal_number(c->infile, pos, 8, &n);
	if(ret) {
		de_dbg(c, "uid: %"I64_FMT, n);
	}
	pos += 8;
	ret = read_ascii_octal_number(c->infile, pos, 8, &n);
	if(ret) {
		de_dbg(c, "gid: %"I64_FMT, n);
	}
	pos += 8;

	ret = read_ascii_octal_number(c->infile, pos, 12, &pmd->filesize);
	de_sanitize_length(&pmd->filesize);
	if(!ret) goto done;
	pos += 12;
	de_dbg(c, "size: %"I64_FMT, pmd->filesize);
	pmd->file_data_pos = pos1 + 512;

	read_12char_timestamp(c, pmd, pos, DE_TIMESTAMPIDX_MODIFY, "mtime");
	pos += 12;

	(void)read_ascii_octal_number(c->infile, pos, 8, &pmd->checksum);
	de_dbg(c, "header checksum (reported): %"I64_FMT, pmd->checksum);
	de_dbg(c, "header checksum (calculated): %"I64_FMT, pmd->checksum_calc);
	if(pmd->checksum != pmd->checksum_calc) {
		de_err(c, "%s: Header checksum failed: reported=%"I64_FMT", calculated=%"I64_FMT,
			ucstring_getpsz_d(pmd->name), pmd->checksum, pmd->checksum_calc);
	}
	pos += 8;

	// linkflag already set, above
	de_dbg(c, "linkflag/typeflag: 0x%02x ('%c')", (unsigned int)pmd->linkflag,
		de_byte_to_printable_char(pmd->linkflag));
	pos += 1;

	if(de_getbyte(pos)!=0) {
		pmd->linkname = dbuf_read_string(c->infile, pos, 100, 100, DE_CONVFLAG_STOP_AT_NUL,
			d->input_encoding);
		de_dbg(c, "linkname: \"%s\"", ucstring_getpsz_d(pmd->linkname->str));
	}
	pos += 100;

	tmpstr = ucstring_create(c);

	if(c->debug_level>=2) {
		ucstring_empty(tmpstr);
		dbuf_read_to_ucstring(c->infile, pos, 8, tmpstr, 0,
			DE_EXTENC_MAKE(DE_ENCODING_ASCII, DE_ENCSUBTYPE_PRINTABLE));
		de_dbg2(c, "magic/version: \"%s\"", ucstring_getpsz_d(tmpstr));
	}
	pos += 6; // magic
	pos += 2; // version

	if(pmd->fmt==TARFMT_POSIX || pmd->fmt==TARFMT_GNU) {
		ucstring_empty(tmpstr);
		dbuf_read_to_ucstring(c->infile, pos, 32, tmpstr, DE_CONVFLAG_STOP_AT_NUL,
			DE_ENCODING_ASCII);
		de_dbg(c, "uname: \"%s\"", ucstring_getpsz(tmpstr));
	}
	pos += 32;

	if(pmd->fmt==TARFMT_POSIX || pmd->fmt==TARFMT_GNU) {
		ucstring_empty(tmpstr);
		dbuf_read_to_ucstring(c->infile, pos, 32, tmpstr, DE_CONVFLAG_STOP_AT_NUL,
			DE_ENCODING_ASCII);
		de_dbg(c, "gname: \"%s\"", ucstring_getpsz(tmpstr));
	}
	pos += 32;

	pos += 8; // devmajor
	pos += 8; // devminor

	// TODO?: There are various dialect-specific fields after this point, more of
	// which might be worth supporting.

	// TODO?: Some (rare?) GNU files have atime/ctime fields here, but is it
	// worth trying to detect them?
	// And "star" files can have atime/ctime fields at offset 476.

	if((pmd->fmt==TARFMT_POSIX || pmd->fmt==TARFMT_STAR) && (de_getbyte(pos)!=0)) {
		// This field might only be 131 bytes, instead of 155. Let's hope that
		// it's NUL terminated in that case.
		pmd->prefix = ucstring_create(c);
		dbuf_read_to_ucstring(c->infile, pos, 155, pmd->prefix,
			DE_CONVFLAG_STOP_AT_NUL, d->input_encoding);
		de_dbg(c, "prefix: \"%s\"", ucstring_getpsz_d(pmd->prefix));
		//pos += 155;
	}

	retval = 1;

done:
	ucstring_destroy(tmpstr);

	de_dbg_indent_restore(c, saved_indent_level);
	return retval;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x2a0, %rsp            # imm = 0x2A0
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movl $0x0, -0x44(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x15b580
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rdx
leaq 0x9dd95(%rip), %rsi     # 0x1c7403
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x250(%rbp), %rsi
movq -0x20(%rbp), %rdx
movl $0x200, %ecx            # imm = 0x200
callq 0x164d10
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
leaq -0x250(%rbp), %rcx
callq 0x12a0a0
movq -0x18(%rbp), %rax
cmpq $0x100, 0x28(%rax)      # imm = 0x100
jne 0x129705
leaq -0x250(%rbp), %rdi
addq $0x94, %rdi
movl $0x8, %esi
callq 0x169cd0
cmpl $0x0, %eax
je 0x129705
movq -0x8(%rbp), %rdi
leaq 0x9dd3b(%rip), %rsi     # 0x1c7429
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
movl $0x1, 0x4(%rax)
jmp 0x129d76
movb -0x1b4(%rbp), %cl
movq -0x18(%rbp), %rax
movb %cl, 0x4(%rax)
leaq -0x250(%rbp), %rax
addq $0x101, %rax            # imm = 0x101
movq (%rax), %rax
movabsq $0x20207261747375, %rcx # imm = 0x20207261747375
subq %rcx, %rax
setne %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x129746
movq -0x18(%rbp), %rax
movl $0x2, (%rax)
jmp 0x1297a3
leaq -0x250(%rbp), %rdi
addq $0x101, %rdi            # imm = 0x101
leaq 0x9df16(%rip), %rsi     # 0x1c7671
movl $0x6, %edx
callq 0x19170
cmpl $0x0, %eax
jne 0x129776
movq -0x18(%rbp), %rax
movl $0x1, (%rax)
jmp 0x1297a1
leaq -0x250(%rbp), %rax
addq $0x1fc, %rax            # imm = 0x1FC
movl (%rax), %eax
subl $0x726174, %eax         # imm = 0x726174
setne %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x12979f
movq -0x18(%rbp), %rax
movl $0x3, (%rax)
jmp 0x1297a1
jmp 0x1297a3
movq -0x8(%rbp), %rax
movq %rax, -0x260(%rbp)
movq -0x18(%rbp), %rax
movl (%rax), %edi
callq 0x12a160
movq -0x260(%rbp), %rdi
movq %rax, %rdx
leaq 0x9dc70(%rip), %rsi     # 0x1c743a
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
callq 0x1711d0
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rcx
movq -0x10(%rbp), %rax
movl (%rax), %r9d
movl $0x64, %edx
movl $0x1, %r8d
callq 0x1669f0
movq -0x38(%rbp), %rax
addq $0x64, %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x258(%rbp)
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rdi
callq 0x172650
movq -0x258(%rbp), %rdi
movq %rax, %rdx
leaq 0x8498b(%rip), %rsi     # 0x1ae1d0
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
movq -0x18(%rbp), %rcx
addq $0x8, %rcx
movl $0x8, %edx
callq 0x12a1c0
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
je 0x12988f
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movl %eax, %edx
leaq 0x7485c(%rip), %rsi     # 0x19e0e4
movb $0x0, %al
callq 0x15b0d0
movq -0x38(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
movl $0x8, %edx
leaq -0x28(%rbp), %rcx
callq 0x12a1c0
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
je 0x1298d4
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rdx
leaq 0x9db7c(%rip), %rsi     # 0x1c7449
movb $0x0, %al
callq 0x15b0d0
movq -0x38(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
movl $0x8, %edx
leaq -0x28(%rbp), %rcx
callq 0x12a1c0
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
je 0x129919
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rdx
leaq 0x9db40(%rip), %rsi     # 0x1c7452
movb $0x0, %al
callq 0x15b0d0
movq -0x38(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
movq -0x18(%rbp), %rcx
addq $0x18, %rcx
movl $0xc, %edx
callq 0x12a1c0
movl %eax, -0x2c(%rbp)
movq -0x18(%rbp), %rdi
addq $0x18, %rdi
callq 0x15f830
cmpl $0x0, -0x2c(%rbp)
jne 0x12995e
jmp 0x129d76
movq -0x38(%rbp), %rax
addq $0xc, %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rdx
leaq 0x9b9f1(%rip), %rsi     # 0x1c536e
movb $0x0, %al
callq 0x15b0d0
movq -0x20(%rbp), %rcx
addq $0x200, %rcx            # imm = 0x200
movq -0x18(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x38(%rbp), %rdx
xorl %ecx, %ecx
leaq 0x9daaf(%rip), %r8      # 0x1c745b
callq 0x12a2c0
movq -0x38(%rbp), %rax
addq $0xc, %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
movq -0x18(%rbp), %rcx
addq $0x20, %rcx
movl $0x8, %edx
callq 0x12a1c0
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rdx
leaq 0x9da73(%rip), %rsi     # 0x1c7461
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rdx
leaq 0x9da79(%rip), %rsi     # 0x1c7481
movb $0x0, %al
callq 0x15b0d0
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x18(%rbp), %rcx
cmpq 0x28(%rcx), %rax
je 0x129a61
movq -0x8(%rbp), %rax
movq %rax, -0x268(%rbp)
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rdi
callq 0x172650
movq -0x268(%rbp), %rdi
movq %rax, %rdx
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rcx
movq -0x18(%rbp), %rax
movq 0x28(%rax), %r8
leaq 0x9da49(%rip), %rsi     # 0x1c74a3
movb $0x0, %al
callq 0x15bfb0
movq -0x38(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x278(%rbp)
movq -0x18(%rbp), %rax
movzbl 0x4(%rax), %eax
movl %eax, -0x26c(%rbp)
movq -0x18(%rbp), %rax
movzbl 0x4(%rax), %edi
callq 0x161a80
movq -0x278(%rbp), %rdi
movl -0x26c(%rbp), %edx
movsbl %al, %ecx
leaq 0x9da32(%rip), %rsi     # 0x1c74dc
movb $0x0, %al
callq 0x15b0d0
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
callq 0x165060
movzbl %al, %eax
cmpl $0x0, %eax
je 0x129b3b
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
movq -0x10(%rbp), %rax
movl (%rax), %r9d
movl $0x64, %ecx
movl $0x1, %r8d
movq %rcx, %rdx
callq 0x1664c0
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x8(%rbp), %rax
movq %rax, -0x280(%rbp)
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rax
movq 0x18(%rax), %rdi
callq 0x172650
movq -0x280(%rbp), %rdi
movq %rax, %rdx
leaq 0x9da3b(%rip), %rsi     # 0x1c756f
movb $0x0, %al
callq 0x15b0d0
movq -0x38(%rbp), %rax
addq $0x64, %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
callq 0x1711d0
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rax
cmpl $0x2, (%rax)
jl 0x129bb5
movq -0x40(%rbp), %rdi
callq 0x171210
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
movq -0x40(%rbp), %rcx
movl $0x8, %edx
xorl %r8d, %r8d
movl $0x401, %r9d            # imm = 0x401
callq 0x1669f0
movq -0x8(%rbp), %rax
movq %rax, -0x288(%rbp)
movq -0x40(%rbp), %rdi
callq 0x172650
movq -0x288(%rbp), %rdi
movq %rax, %rdx
leaq 0x9d94f(%rip), %rsi     # 0x1c74fd
movb $0x0, %al
callq 0x15b350
movq -0x38(%rbp), %rax
addq $0x6, %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x38(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x1, (%rax)
je 0x129bdf
movq -0x18(%rbp), %rax
cmpl $0x2, (%rax)
jne 0x129c37
movq -0x40(%rbp), %rdi
callq 0x171210
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
movq -0x40(%rbp), %rcx
movl $0x20, %edx
movl $0x1, %r9d
movl %r9d, %r8d
callq 0x1669f0
movq -0x8(%rbp), %rax
movq %rax, -0x290(%rbp)
movq -0x40(%rbp), %rdi
callq 0x172500
movq -0x290(%rbp), %rdi
movq %rax, %rdx
leaq 0x9d8e1(%rip), %rsi     # 0x1c7511
movb $0x0, %al
callq 0x15b0d0
movq -0x38(%rbp), %rax
addq $0x20, %rax
movq %rax, -0x38(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x1, (%rax)
je 0x129c55
movq -0x18(%rbp), %rax
cmpl $0x2, (%rax)
jne 0x129cad
movq -0x40(%rbp), %rdi
callq 0x171210
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
movq -0x40(%rbp), %rcx
movl $0x20, %edx
movl $0x1, %r9d
movl %r9d, %r8d
callq 0x1669f0
movq -0x8(%rbp), %rax
movq %rax, -0x298(%rbp)
movq -0x40(%rbp), %rdi
callq 0x172500
movq -0x298(%rbp), %rdi
movq %rax, %rdx
leaq 0x9d877(%rip), %rsi     # 0x1c751d
movb $0x0, %al
callq 0x15b0d0
movq -0x38(%rbp), %rax
addq $0x20, %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x38(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x1, (%rax)
je 0x129ce7
movq -0x18(%rbp), %rax
cmpl $0x3, (%rax)
jne 0x129d6f
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
callq 0x165060
movzbl %al, %eax
cmpl $0x0, %eax
je 0x129d6f
movq -0x8(%rbp), %rdi
callq 0x1711d0
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x40(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rcx
movq -0x10(%rbp), %rax
movl (%rax), %r9d
movl $0x9b, %edx
movl $0x1, %r8d
callq 0x1669f0
movq -0x8(%rbp), %rax
movq %rax, -0x2a0(%rbp)
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rdi
callq 0x172650
movq -0x2a0(%rbp), %rdi
movq %rax, %rdx
leaq 0x9d7c1(%rip), %rsi     # 0x1c7529
movb $0x0, %al
callq 0x15b0d0
movl $0x1, -0x44(%rbp)
movq -0x40(%rbp), %rdi
callq 0x171750
movq -0x8(%rbp), %rdi
movl -0x48(%rbp), %esi
callq 0x15b5a0
movl -0x44(%rbp), %eax
addq $0x2a0, %rsp            # imm = 0x2A0
popq %rbp
retq
nopw (%rax,%rax)