// File: /workspace/github_cpp_makefile_star2_O3/hey-yahei[P]ConvNet/src/TarFile.cpp
// Function: TarFile::IsValidTarFile()
bool TarFile::IsValidTarFile()
{
	if (!file) return false;

	const int block_size{ 512 };
	unsigned char buf[block_size];
	tar_posix_header* header = (tar_posix_header*)buf;
	memset(buf, 0, block_size);

	fseek(file, 0, SEEK_END);
	size = ftell(file);
	fseek(file, 0, SEEK_SET);
	if (size % block_size != 0) {
		fprintf(stderr, "tar file size should be a multiple of 512 bytes: %d\n", size);
		return false;
	}

	size_t pos{ 0 };

	while (1) {
		size_t read_size = fread(buf, block_size, 1, file);
		if (read_size != 1) break;
		if (strncmp(header->magic, TMAGIC, 5)) break;

		pos += block_size;
		size_t file_size{ 0 };
		sscanf(header->size, "%lo", &file_size);
		size_t file_block_count = (file_size + block_size - 1) / block_size;

		switch (header->typeflag) {
		case '0': // intentionally dropping through
		case '\0':
			// normal file
			file_sizes.push_back(file_size);
			file_names.push_back(std::string(header->name));
			file_data_start_addrs.push_back(pos);
			break;
		case '1':
			// hard link
			break;
		case '2':
			// symbolic link
			break;
		case '3':
			// device file/special file
			break;
		case '4':
			// block device
			break;
		case '5':
			// directory
			break;
		case '6':
			// named pipe
			break;
		default:
			break;
		}

		pos += file_block_count * block_size;
		fseek(file, pos, SEEK_SET);
	}

	fseek(file, 0, SEEK_SET);

	return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x258, %rsp            # imm = 0x258
movq (%rdi), %r14
testq %r14, %r14
je 0x5350
movq %rdi, %rbx
leaq -0x280(%rbp), %rdi
movl $0x200, %edx            # imm = 0x200
xorl %esi, %esi
callq 0x20f0
movq %r14, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x21d0
movq (%rbx), %rdi
callq 0x2030
movq %rax, 0x8(%rbx)
movq (%rbx), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x21d0
movq 0x8(%rbx), %rdx
testl $0x1ff, %edx            # imm = 0x1FF
sete %r14b
jne 0x5368
movl %r14d, -0x3c(%rbp)
movq $0x0, -0x30(%rbp)
movq (%rbx), %rcx
leaq -0x280(%rbp), %rdi
movl $0x200, %esi            # imm = 0x200
movl $0x1, %edx
callq 0x2290
cmpq $0x1, %rax
jne 0x533e
leaq 0x28(%rbx), %rax
movq %rax, -0x50(%rbp)
leaq 0x10(%rbx), %rax
movq %rax, -0x58(%rbp)
leaq 0x40(%rbx), %rax
movq %rax, -0x48(%rbp)
leaq -0x280(%rbp), %r15
movl -0x17f(%rbp), %eax
movl $0x61747375, %ecx       # imm = 0x61747375
xorl %ecx, %eax
movzbl -0x17b(%rbp), %ecx
xorl $0x72, %ecx
orl %eax, %ecx
jne 0x533e
addq $0x200, -0x30(%rbp)     # imm = 0x200
movq $0x0, -0x38(%rbp)
leaq -0x204(%rbp), %rdi
leaq 0x1235(%rip), %rsi      # 0x6477
leaq -0x38(%rbp), %rdx
xorl %eax, %eax
callq 0x2170
movq -0x38(%rbp), %r14
movzbl -0x1e4(%rbp), %eax
cmpl $0x30, %eax
je 0x5265
testl %eax, %eax
jne 0x52fc
movq 0x30(%rbx), %rsi
cmpq 0x38(%rbx), %rsi
je 0x527c
movq %r14, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x30(%rbx)
jmp 0x5289
movq -0x50(%rbp), %rdi
leaq -0x38(%rbp), %rdx
callq 0x5784
leaq -0x68(%rbp), %r12
movq %r12, -0x78(%rbp)
movq %r15, %rdi
callq 0x20c0
leaq (%rax,%rbp), %rdx
addq $-0x280, %rdx           # imm = 0xFD80
leaq -0x78(%rbp), %r13
movq %r13, %rdi
movq %r15, %rsi
callq 0x58a6
movq -0x58(%rbp), %rdi
movq %r13, %rsi
callq 0x5944
movq -0x78(%rbp), %rdi
cmpq %r12, %rdi
je 0x52d4
movq -0x68(%rbp), %rsi
incq %rsi
callq 0x21b0
movq 0x48(%rbx), %rsi
cmpq 0x50(%rbx), %rsi
je 0x52ef
movq -0x30(%rbp), %rax
movq %rax, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x48(%rbx)
jmp 0x52fc
movq -0x48(%rbp), %rdi
leaq -0x30(%rbp), %rdx
callq 0x5784
addq $0x1ff, %r14            # imm = 0x1FF
andq $-0x200, %r14           # imm = 0xFE00
addq -0x30(%rbp), %r14
movq %r14, -0x30(%rbp)
movq (%rbx), %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x21d0
movq (%rbx), %rcx
movl $0x200, %esi            # imm = 0x200
movl $0x1, %edx
movq %r15, %rdi
callq 0x2290
cmpq $0x1, %rax
je 0x5205
movq (%rbx), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x21d0
movl -0x3c(%rbp), %r14d
jmp 0x5353
xorl %r14d, %r14d
movl %r14d, %eax
addq $0x258, %rsp            # imm = 0x258
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x3c61(%rip), %rax      # 0x8fd0
movq (%rax), %rdi
leaq 0x10c3(%rip), %rsi      # 0x643c
xorl %eax, %eax
callq 0x2260
jmp 0x5353
movq %rax, %rbx
movq -0x78(%rbp), %rdi
cmpq %r12, %rdi
je 0x539a
movq -0x68(%rbp), %rsi
incq %rsi
callq 0x21b0
movq %rbx, %rdi
callq 0x22e0