// File: /workspace/github_cpp_makefile_star2_O2/irakr[P]Minimal-File-Compression-Tool/src/compression-driver.cpp
// Function: CompressionDriver::decompress(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>&)
void CompressionDriver :: decompress(std::string &ifilename) {
	Log(this, "Started...");
	if(!m_Compressor) {
		Log(this, "[Fatal Error] No compression algorithm registered.");
		return; // FIXME... Throw an exception from here
	}

	std::cout << "Input File: " << ifilename << std::endl;

	// Output filename
	// (XXX) For now this is the auto generated output file name
	std::string ofilename = ifilename + ".orig";

	// TODO ...
	// Open file named 'file'
	// Pass the handle to specific decode() function
	// decode() should return back the decompressed file handle
	std::fstream inputf(ifilename, std::fstream::in | std::fstream::binary);
	if(!inputf.is_open()) {
		std::cerr << "[Error] The file \"" << ifilename << "\" does not exist." << std::endl;
		return;
	}
	
	fsize_t file_size = getFileSize(inputf);
    std::cout << "Size: " << file_size << " bytes" << std::endl;
    
	// Read bytes from input file
	byte *in_data_byte_buff = new byte[file_size + 1];
	inputf.read((char*)in_data_byte_buff, file_size);
	in_data_byte_buff[file_size] = 0; // Append null at the end because read() doesn't
	//std::cout << "file_buff: " << in_data_byte_buff << std::endl;

	// Get the compressed byte stream
	byte* decompressed_buff = m_Compressor->decoder().decode(in_data_byte_buff, &file_size);
	if(!decompressed_buff) {
	    // TODO... Cleanup code like should be placed somewhere else systematically.
	    Logf(this, "Error: Invalid file format.");
	    delete[] in_data_byte_buff;
	    inputf.close();
	    return;
	}
	std::cout << "Decompressed size = " << file_size << " bytes" << std::endl;

	// Open output file. The size will be obviously less than or impossibly
	// equal to the input file size;
	// Then write the compressed byte stream to it.
	std::fstream outputf;
	outputf.open(ofilename, std::fstream::out | std::fstream::binary);
	outputf.write((const char*)decompressed_buff,
		file_size);

	inputf.close();
	outputf.close();
}

// Assembly:
pushq %r15
pushq %r14
pushq %rbx
subq $0x450, %rsp            # imm = 0x450
cmpq $0x0, 0x38(%rdi)
je 0x3d32
movq %rsi, %r14
movq %rdi, %rbx
movq 0x5462(%rip), %rdi      # 0x8fc0
leaq 0x1601(%rip), %rsi      # 0x5166
callq 0x21c0
movq %rax, %rdi
movq %r14, %rsi
callq 0x21a0
movq %rax, %rdi
callq 0x2100
leaq 0x1634(%rip), %rdx      # 0x51b8
leaq 0x10(%rsp), %rdi
movq %r14, %rsi
callq 0x3243
leaq 0x30(%rsp), %rdi
pushq $0xc
popq %rdx
movq %r14, %rsi
callq 0x22e0
leaq 0xb0(%rsp), %rdi
callq 0x2080
testb %al, %al
je 0x3cd5
leaq 0x30(%rsp), %rdi
callq 0x3ac3
movq %rax, %r14
movq %rax, 0x8(%rsp)
movq 0x53f1(%rip), %rdi      # 0x8fc0
leaq 0x15c1(%rip), %rsi      # 0x5197
callq 0x21c0
movq %rax, %rdi
movq %r14, %rsi
callq 0x21b0
leaq 0x15b1(%rip), %rsi      # 0x519e
movq %rax, %rdi
callq 0x21c0
movq %rax, %rdi
callq 0x2100
movq %r14, %rdi
incq %rdi
callq 0x2030
movq %rax, %r15
leaq 0x30(%rsp), %rdi
movq %rax, %rsi
movq %r14, %rdx
callq 0x2210
movb $0x0, (%r15,%r14)
movq 0x38(%rbx), %rax
movq 0x48(%rax), %rdi
movq (%rdi), %rax
leaq 0x8(%rsp), %rdx
movq %r15, %rsi
callq *(%rax)
movq %rax, %rbx
testq %rax, %rax
je 0x3d0c
movq 0x5378(%rip), %rdi      # 0x8fc0
leaq 0x1594(%rip), %rsi      # 0x51e3
callq 0x21c0
movq 0x8(%rsp), %rsi
movq %rax, %rdi
callq 0x21b0
leaq 0x1536(%rip), %rsi      # 0x519e
movq %rax, %rdi
callq 0x21c0
movq %rax, %rdi
callq 0x2100
leaq 0x240(%rsp), %rdi
callq 0x21f0
leaq 0x240(%rsp), %rdi
leaq 0x10(%rsp), %rsi
pushq $0x14
popq %rdx
callq 0x2270
leaq 0x250(%rsp), %rdi
movq 0x8(%rsp), %rdx
movq %rbx, %rsi
callq 0x2240
leaq 0x30(%rsp), %rdi
callq 0x2040
leaq 0x240(%rsp), %rdi
callq 0x2040
leaq 0x240(%rsp), %rdi
callq 0x2070
jmp 0x3d1e
movq 0x5314(%rip), %rdi      # 0x8ff0
leaq 0x14db(%rip), %rsi      # 0x51be
callq 0x21c0
movq %rax, %rdi
movq %r14, %rsi
callq 0x21a0
leaq 0x14d7(%rip), %rsi      # 0x51d1
movq %rax, %rdi
callq 0x21c0
movq %rax, %rdi
callq 0x2100
jmp 0x3d1e
movq %r15, %rdi
callq 0x2220
leaq 0x30(%rsp), %rdi
callq 0x2040
leaq 0x30(%rsp), %rdi
callq 0x2070
leaq 0x10(%rsp), %rdi
callq 0x20f0
addq $0x450, %rsp            # imm = 0x450
popq %rbx
popq %r14
popq %r15
retq
jmp 0x3d5e
jmp 0x3d5e
movq %rax, %rbx
jmp 0x3d6b
movq %rax, %rbx
leaq 0x240(%rsp), %rdi
callq 0x2070
jmp 0x3d61
jmp 0x3d5e
jmp 0x3d5e
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x2070
leaq 0x10(%rsp), %rdi
callq 0x20f0
movq %rbx, %rdi
callq 0x2310
nop