// File: /workspace/github_cpp_makefile_star2_O2/irakr[P]Minimal-File-Compression-Tool/src/compression-driver.cpp
// Function: CompressionDriver::compress(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>&)
void CompressionDriver :: compress(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 + ".compressed";

	// TODO ...
	// Open file named 'file'
	// Pass the handle to specific encode() function
	// encode() should return back the compressed file handle
	std::fstream inputf;
	inputf.open(ifilename, std::fstream::in | std::fstream::binary);
	if(!inputf.is_open()) {
		Logf(this, "[Error] The file \"%s\" does not exist.", ifilename.c_str());
		throw "fstream::open() failed.";
	}

	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* compressed_buff = m_Compressor->encoder().encode(in_data_byte_buff, &file_size);
	if(!compressed_buff) {
	    // TODO... Cleanup code like should be placed somewhere else systematically.
	    Logf(this, "Error: File too small to be compressed.");
	    delete[] in_data_byte_buff;
	    inputf.close();
	    return;
	}
	std::cout << "Compressed 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*)compressed_buff,
		file_size);

	inputf.close();
	outputf.close();
}

// Assembly:
pushq %r15
pushq %r14
pushq %rbx
subq $0x450, %rsp            # imm = 0x450
cmpq $0x0, 0x38(%rdi)
je 0x3a55
movq %rsi, %r14
movq %rdi, %rbx
movq 0x5712(%rip), %rdi      # 0x8fc0
leaq 0x18b1(%rip), %rsi      # 0x5166
callq 0x21c0
movq %rax, %rdi
movq %r14, %rsi
callq 0x21a0
movq %rax, %rdi
callq 0x2100
leaq 0x189f(%rip), %rdx      # 0x5173
leaq 0x10(%rsp), %rdi
movq %r14, %rsi
callq 0x3243
leaq 0x30(%rsp), %rdi
callq 0x21f0
leaq 0x30(%rsp), %rdi
pushq $0xc
popq %rdx
movq %r14, %rsi
callq 0x2270
leaq 0xb0(%rsp), %rdi
callq 0x2080
testb %al, %al
je 0x3a62
leaq 0x30(%rsp), %rdi
callq 0x3ac3
movq %rax, %r14
movq %rax, 0x8(%rsp)
movq 0x5697(%rip), %rdi      # 0x8fc0
leaq 0x1867(%rip), %rsi      # 0x5197
callq 0x21c0
movq %rax, %rdi
movq %r14, %rsi
callq 0x21b0
leaq 0x1857(%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 0x40(%rax), %rdi
movq (%rdi), %rax
leaq 0x8(%rsp), %rdx
movq %r15, %rsi
callq *(%rax)
movq %rax, %rbx
testq %rax, %rax
je 0x3a2f
movq 0x561e(%rip), %rdi      # 0x8fc0
leaq 0x17fc(%rip), %rsi      # 0x51a5
callq 0x21c0
movq 0x8(%rsp), %rsi
movq %rax, %rdi
callq 0x21b0
leaq 0x17dc(%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 0x3a41
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
pushq $0x8
popq %rdi
callq 0x2110
leaq 0x170e(%rip), %rcx      # 0x517f
movq %rcx, (%rax)
movq 0x552d(%rip), %rsi      # 0x8fa8
movq %rax, %rdi
xorl %edx, %edx
callq 0x22f0
jmp 0x3aa4
movq %rax, %rbx
jmp 0x3ab1
jmp 0x3aa4
jmp 0x3aa4
movq %rax, %rbx
leaq 0x240(%rsp), %rdi
callq 0x2070
jmp 0x3aa7
jmp 0x3aa4
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x2070
leaq 0x10(%rsp), %rdi
callq 0x20f0
movq %rbx, %rdi
callq 0x2310