// File: /workspace/github_c_makefile_star2_O1/FirestonnneTFM[P]tfm_key_parser/src/swf_file.cpp
// Function: swf_file::swf_file(char*)
swf_file::swf_file(char *fname) {
	std::ifstream file;
	file.open(fname, std::ios::in | std::ios::binary | std::ios::ate);
	int fsize = file.tellg();
	file.close();
	
	file.open(fname, std::ios::in | std::ios::binary);
	if (! file.is_open())
		fatal("Could not open file");
	file.read((char*)&header, sizeof(cwf_header));
	if (! header.is_sig_valid()) {
		header.print();
		fatal("Header is not valid");
	}
	fsize -= sizeof(cwf_header);
	char *in_data = new char[fsize];
	file.read(in_data, fsize);
	file.close();
	handle = new uint8_t[header.file_size];
	
	z_stream stream;
	stream.zalloc = Z_NULL;
	stream.zfree = Z_NULL;
	stream.opaque = Z_NULL;
	stream.avail_in = (uInt)fsize;
	stream.next_in = (Bytef*)in_data;
	stream.avail_out = header.file_size;
	stream.next_out = (Bytef*)handle;

	inflateInit(&stream);
	inflate(&stream, Z_NO_FLUSH);
	inflateEnd(&stream);

	// read tags
	uint8_t *raw = handle;
	uint16_t bit_size = *raw >> 3;
	bit_size = bit_size * 4 + 5;
	for ( ; bit_size < 200; bit_size -= 8) {
		++raw;
	}
	// frame rate and frame count
	raw += 4;
	for ( ; ; ) {
		swf_tag tag(&raw);
		tags.push_back(tag);
		if (tag.kind == 0)
			break;
	}
	swf_tag abc_tag;
	get_tags(82, &abc_tag, 1);
	abc = abc_file::factory_from_bytes(abc_tag.get_handle());
	/*
	do {
		
		uint16_t header_0 = conv_copy<uint16_t>(raw);
		uint16_t tag_type = header_0 >> 6;
		uint32_t tag_len = header_0 & 63;
		if (tag_len == 0x3f) {
			tag_len = conv_copy<uint32_t>(swf);
		}
		if (tag_type == 87) {
			uint16_t tag = conv_copy<uint16_t>(swf);
			// skip ahead 4 bytes (reserved data)
			btags[i] = swf + 4;
			btag_lens[i] = tag_len - 6;
			// undo the read (length is not from this point)
			swf -= 2;
			++i;
			if (tag != i)
				throw "Tag and position mismatch";
		}
		if (tag_type == 76) {
			sym = swf;
		}
		swf += tag_len;
	} while (sym == nullptr || i < 12);
	*/

	delete [] in_data;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x298, %rsp            # imm = 0x298
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x18(%rdi), %r14
xorps %xmm0, %xmm0
movups %xmm0, 0x18(%rdi)
movq $0x0, 0x28(%rdi)
leaq 0x90(%rsp), %rdi
callq 0x2220
leaq 0x90(%rsp), %rdi
movq %r15, %rsi
movl $0xe, %edx
callq 0x2130
leaq 0x90(%rsp), %rdi
callq 0x22f0
movq %rax, %r12
leaq 0x90(%rsp), %rdi
callq 0x2070
leaq 0x90(%rsp), %rdi
movq %r15, %rsi
movl $0xc, %edx
callq 0x2130
leaq 0x108(%rsp), %rdi
callq 0x2080
testb %al, %al
je 0x4a91
leaq 0x8(%rbx), %r15
leaq 0x90(%rsp), %rdi
movl $0x8, %edx
movq %r15, %rsi
callq 0x2260
cmpb $0x43, 0x8(%rbx)
jne 0x4a9f
cmpb $0x57, 0x9(%rbx)
jne 0x4a9f
cmpb $0x53, 0xa(%rbx)
jne 0x4a9f
addl $-0x8, %r12d
movslq %r12d, %r13
movq %r13, %rdi
callq 0x2030
movq %rax, %r15
leaq 0x90(%rsp), %rdi
movq %rax, %rsi
movq %r13, %rdx
callq 0x2260
leaq 0x90(%rsp), %rdi
callq 0x2070
movl 0xc(%rbx), %r13d
movq %r13, %rdi
callq 0x2030
movq %rax, (%rbx)
xorps %xmm0, %xmm0
leaq 0x20(%rsp), %rdi
movups %xmm0, 0x40(%rdi)
movq $0x0, 0x50(%rdi)
movl %r12d, 0x8(%rdi)
movq %r15, (%rdi)
movl %r13d, 0x20(%rdi)
movq %rax, 0x18(%rdi)
leaq 0x1937(%rip), %rsi      # 0x628a
movl $0x70, %edx
callq 0x21a0
leaq 0x20(%rsp), %rdi
xorl %esi, %esi
callq 0x2170
leaq 0x20(%rsp), %rdi
callq 0x2210
movq (%rbx), %rax
movq %rax, 0x18(%rsp)
movzbl (%rax), %ecx
shrl %ecx
andl $0x7c, %ecx
addl $0x5, %ecx
addq $0x4, %rax
addl $0xfff8, %ecx           # imm = 0xFFF8
movzwl %cx, %ecx
incq %rax
cmpl $0xc8, %ecx
jb 0x498a
leaq 0x18(%rsp), %r12
movq %rax, (%r12)
leaq 0x8(%rsp), %r13
movq %r13, %rdi
movq %r12, %rsi
callq 0x4d16
movq 0x20(%rbx), %rsi
cmpq 0x28(%rbx), %rsi
je 0x49d0
movups 0x8(%rsp), %xmm0
movups %xmm0, (%rsi)
addq $0x10, 0x20(%rbx)
jmp 0x49db
movq %r14, %rdi
movq %r13, %rdx
callq 0x4bbe
cmpw $0x0, 0x10(%rsp)
jne 0x49ac
leaq 0x8(%rsp), %rdi
callq 0x4d00
movq 0x18(%rbx), %rax
movq 0x20(%rbx), %rcx
cmpq %rcx, %rax
je 0x4a5c
xorl %edx, %edx
movq (%rax), %rsi
movzwl 0x8(%rax), %edi
movzwl 0xe(%rax), %r8d
movw %r8w, 0x4(%rsp)
movl 0xa(%rax), %r8d
movl %r8d, (%rsp)
cmpw $0x52, %di
jne 0x4a4c
movl %edx, %r8d
shlq $0x4, %r8
movq %rsi, 0x8(%rsp)
movw %di, 0x10(%rsp,%r8)
movl (%rsp), %esi
movl %esi, 0x12(%rsp,%r8)
movzwl 0x4(%rsp), %esi
movw %si, 0x16(%rsp,%r8)
testl %edx, %edx
leal 0x1(%rdx), %edx
sete %sil
jmp 0x4a4e
xorl %esi, %esi
testb %sil, %sil
jne 0x4a5c
addq $0x10, %rax
cmpq %rcx, %rax
jne 0x49fc
movq 0x8(%rsp), %rdi
callq 0x2ab8
movq %rax, 0x10(%rbx)
movq %r15, %rdi
callq 0x2270
leaq 0x90(%rsp), %rdi
callq 0x2090
addq $0x298, %rsp            # imm = 0x298
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x17b9(%rip), %r15      # 0x6251
movl $0x18, %ebp
jmp 0x4ab3
movq %r15, %rdi
callq 0x46d0
leaq 0x17c8(%rip), %r15      # 0x6276
movl $0x1c, %ebp
movl $0x18, %edi
callq 0x2100
movl %ebp, (%rax)
leaq 0x179f(%rip), %rcx      # 0x6265
movq %rcx, 0x8(%rax)
movq %r15, 0x10(%rax)
leaq 0x42bb(%rip), %rsi      # 0x8d90
movq %rax, %rdi
xorl %edx, %edx
callq 0x22d0
jmp 0x4af0
jmp 0x4af0
movq %rax, %r15
jmp 0x4b00
jmp 0x4af0
jmp 0x4af0
jmp 0x4af0
jmp 0x4af0
movq %rax, %r15
leaq 0x90(%rsp), %rdi
callq 0x2090
movq (%r14), %rdi
testq %rdi, %rdi
je 0x4b14
movq 0x28(%rbx), %rsi
subq %rdi, %rsi
callq 0x21f0
movq %r15, %rdi
callq 0x2300