// File: /workspace/github_c_makefile_star2_O3/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 0x4a7e
leaq 0x8(%rbx), %r15
leaq 0x90(%rsp), %rdi
movl $0x8, %edx
movq %r15, %rsi
callq 0x2260
cmpb $0x43, 0x8(%rbx)
jne 0x4a8c
cmpb $0x57, 0x9(%rbx)
jne 0x4a8c
cmpb $0x53, 0xa(%rbx)
jne 0x4a8c
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 0x190f(%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
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 0x49ad
leaq 0x18(%rsp), %r12
movq %rax, (%r12)
leaq 0x8(%rsp), %r13
movq %r13, %rdi
movq %r12, %rsi
callq 0x4cf6
movq 0x20(%rbx), %rsi
cmpq 0x28(%rbx), %rsi
je 0x49f3
movups 0x8(%rsp), %xmm0
movups %xmm0, (%rsi)
addq $0x10, 0x20(%rbx)
jmp 0x49fe
movq %r14, %rdi
movq %r13, %rdx
callq 0x4ba0
cmpw $0x0, 0x10(%rsp)
jne 0x49cf
leaq 0x8(%rsp), %rdi
callq 0x4ce0
movq 0x18(%rbx), %rax
movq 0x20(%rbx), %rcx
cmpq %rcx, %rax
je 0x4a49
cmpw $0x52, 0x8(%rax)
je 0x4a2a
addq $0x10, %rax
jmp 0x4a18
movq (%rax), %rcx
movq %rcx, 0x8(%rsp)
movw $0x52, 0x10(%rsp)
movl 0xa(%rax), %ecx
movzwl 0xe(%rax), %eax
movw %ax, 0x16(%rsp)
movl %ecx, 0x12(%rsp)
movq 0x8(%rsp), %rdi
callq 0x2ac4
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 0x17cc(%rip), %r15      # 0x6251
movl $0x18, %ebp
jmp 0x4aa0
movq %r15, %rdi
callq 0x46f8
leaq 0x17db(%rip), %r15      # 0x6276
movl $0x1c, %ebp
movl $0x18, %edi
callq 0x2100
movl %ebp, (%rax)
leaq 0x17b2(%rip), %rcx      # 0x6265
movq %rcx, 0x8(%rax)
movq %r15, 0x10(%rax)
leaq 0x42ce(%rip), %rsi      # 0x8d90
movq %rax, %rdi
xorl %edx, %edx
callq 0x22d0
jmp 0x4add
jmp 0x4add
movq %rax, %r15
jmp 0x4aed
jmp 0x4add
jmp 0x4add
jmp 0x4add
jmp 0x4add
movq %rax, %r15
leaq 0x90(%rsp), %rdi
callq 0x2090
movq (%r14), %rdi
testq %rdi, %rdi
je 0x4b01
movq 0x28(%rbx), %rsi
subq %rdi, %rsi
callq 0x21f0
movq %r15, %rdi
callq 0x2300
nop