// File: /workspace/github_c_makefile_star2_O2/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, %r14
leaq 0x18(%rdi), %rbx
xorps %xmm0, %xmm0
movups %xmm0, 0x18(%rdi)
andq $0x0, 0x28(%rdi)
leaq 0x90(%rsp), %rdi
callq 0x21e0
leaq 0x90(%rsp), %rdi
pushq $0xe
popq %rdx
movq %r15, %rsi
callq 0x2110
leaq 0x90(%rsp), %rdi
callq 0x22a0
movq %rax, %r12
leaq 0x90(%rsp), %rdi
callq 0x2060
leaq 0x90(%rsp), %rdi
pushq $0xc
popq %rdx
movq %r15, %rsi
callq 0x2110
leaq 0x108(%rsp), %rdi
callq 0x2070
testb %al, %al
je 0x48b0
leaq 0x8(%r14), %r15
leaq 0x90(%rsp), %rdi
pushq $0x8
popq %rdx
movq %r15, %rsi
callq 0x2210
movq %r15, %rdi
callq 0x46bc
testb %al, %al
je 0x48bb
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 0x2210
leaq 0x90(%rsp), %rdi
callq 0x2060
movl 0xc(%r14), %r13d
movq %r13, %rdi
callq 0x2030
movq %rax, (%r14)
xorps %xmm0, %xmm0
leaq 0x20(%rsp), %rdi
movups %xmm0, 0x40(%rdi)
andq $0x0, 0x50(%rdi)
movl %r12d, 0x8(%rdi)
movq %r15, (%rdi)
movl %r13d, 0x20(%rdi)
movq %rax, 0x18(%rdi)
leaq 0x1a9d(%rip), %rsi      # 0x628a
pushq $0x70
popq %rdx
callq 0x2170
leaq 0x20(%rsp), %rdi
xorl %esi, %esi
callq 0x2140
leaq 0x20(%rsp), %rdi
callq 0x21d0
movq (%r14), %rax
movzbl (%rax), %ecx
shrl %ecx
andl $0x7c, %ecx
addl $0x5, %ecx
addq $0x4, %rax
movzwl %cx, %ecx
cmpl $0xc7, %ecx
ja 0x4830
addl $-0x8, %ecx
incq %rax
jmp 0x481d
leaq 0x18(%rsp), %r12
movq %rax, (%r12)
leaq 0x8(%rsp), %r13
movq %r13, %rdi
movq %r12, %rsi
callq 0x4b56
movq %rbx, %rdi
movq %r13, %rsi
callq 0x49ae
cmpw $0x0, 0x10(%rsp)
jne 0x483e
leaq 0x8(%rsp), %rdi
callq 0x4b48
pushq $0x52
popq %rsi
leaq 0x8(%rsp), %r12
pushq $0x1
popq %rcx
movq %r14, %rdi
movq %r12, %rdx
callq 0x4928
movq (%r12), %rdi
callq 0x2a0c
movq %rax, 0x10(%r14)
movq %r15, %rdi
callq 0x2220
leaq 0x90(%rsp), %rdi
callq 0x2080
addq $0x298, %rsp            # imm = 0x298
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x199a(%rip), %r14      # 0x6251
pushq $0x18
jmp 0x48cc
movq %r15, %rdi
callq 0x4628
leaq 0x19ac(%rip), %r14      # 0x6276
pushq $0x1c
popq %rbp
pushq $0x18
popq %rdi
callq 0x20f0
movl %ebp, (%rax)
leaq 0x1987(%rip), %rcx      # 0x6265
movq %rcx, 0x8(%rax)
movq %r14, 0x10(%rax)
leaq 0x44a3(%rip), %rsi      # 0x8d90
movq %rax, %rdi
xorl %edx, %edx
callq 0x2280
jmp 0x4908
jmp 0x4908
movq %rax, %r14
jmp 0x4918
jmp 0x4908
jmp 0x4908
jmp 0x4908
jmp 0x4908
movq %rax, %r14
leaq 0x90(%rsp), %rdi
callq 0x2080
movq %rbx, %rdi
callq 0x49cc
movq %r14, %rdi
callq 0x22b0