// File: /workspace/github_cpp_makefile_star2_O2/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 0x4eba
movq %rdi, %rbx
leaq -0x280(%rbp), %rdi
movl $0x200, %edx            # imm = 0x200
xorl %esi, %esi
callq 0x20e0
pushq $0x2
popq %rdx
movq %r14, %rdi
xorl %esi, %esi
callq 0x21a0
movq (%rbx), %rdi
callq 0x2030
movq %rax, 0x8(%rbx)
movq (%rbx), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x21a0
movq 0x8(%rbx), %rdx
testw $0x1ff, %dx             # imm = 0x1FF
sete %r14b
jne 0x4ed2
movl %r14d, -0x3c(%rbp)
leaq -0x38(%rbp), %rax
andq $0x0, (%rax)
leaq 0x28(%rbx), %rax
movq %rax, -0x60(%rbp)
leaq 0x10(%rbx), %rax
movq %rax, -0x58(%rbp)
leaq 0x40(%rbx), %rax
movq %rax, -0x50(%rbp)
leaq -0x280(%rbp), %r12
leaq -0x48(%rbp), %r14
leaq -0x80(%rbp), %r15
movq (%rbx), %rcx
movl $0x200, %esi            # imm = 0x200
movq %r12, %rdi
pushq $0x1
popq %rdx
callq 0x2270
cmpq $0x1, %rax
jne 0x4ea8
leaq -0x17f(%rbp), %rdi
leaq 0x166e(%rip), %rsi      # 0x6468
pushq $0x5
popq %rdx
callq 0x2180
testl %eax, %eax
jne 0x4ea8
addq $0x200, -0x38(%rbp)     # imm = 0x200
andq $0x0, -0x48(%rbp)
leaq -0x204(%rbp), %rdi
leaq 0x1649(%rip), %rsi      # 0x646e
movq %r14, %rdx
xorl %eax, %eax
callq 0x2130
movq -0x48(%rbp), %r13
movl $0x1ff, %eax            # imm = 0x1FF
addq %rax, %r13
movzbl -0x1e4(%rbp), %eax
cmpl $0x30, %eax
je 0x4e4b
testl %eax, %eax
jne 0x4e87
movq -0x60(%rbp), %rdi
movq %r14, %rsi
callq 0x50e2
movq %r15, %rdi
movq %r12, %rsi
leaq -0x29(%rbp), %rdx
callq 0x510a
movq -0x58(%rbp), %rdi
movq %r15, %rsi
callq 0x5430
movq %r15, %rdi
callq 0x20b0
movq -0x50(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x50e2
andq $-0x200, %r13           # imm = 0xFE00
addq -0x38(%rbp), %r13
movq %r13, -0x38(%rbp)
movq (%rbx), %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0x21a0
jmp 0x4dcf
movq (%rbx), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x21a0
movl -0x3c(%rbp), %r14d
jmp 0x4ebd
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 0x40f7(%rip), %rax      # 0x8fd0
movq (%rax), %rdi
leaq 0x1550(%rip), %rsi      # 0x6433
xorl %eax, %eax
callq 0x2240
jmp 0x4ebd
movq %rax, %rbx
leaq -0x80(%rbp), %rdi
callq 0x20b0
movq %rbx, %rdi
callq 0x22b0