// File: /workspace/github_cpp_makefile_star2_O2/luigix25[P]SSLP/library/SendReceiveFile.cpp
// Function: ReceiveFile(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>&, NetSocket&, PublicKey&, bool)
bool ReceiveFile(string & path, string& filename, NetSocket & senderSocket,PublicKey &key,bool progressBar){

	//handle get
	if(filename.size() == 0)
		return false;


	path += filename;
	cout<<"[LOG] File Path: "<<path<<endl;

	uint32_t file_size;
	uint32_t completeFileSize;
	if(!recvIntHMAC(senderSocket,(int32_t&)file_size)) return false;

	if(file_size == 0){				//file non esistente
		cout<<"[LOG] File does not exist or too big"<<endl;
		return true;
	}

	completeFileSize = file_size;

	char *recvd_data;
	int len = 0;
	file_status status;

	cout<<"[LOG] File Size: "<<file_size<<endl;
	WriteFileManager fm(path,file_size);
	DecryptManager dm;
	RSAVerifyManager verify(key);


	struct winsize w;
    ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);

    float currentPercentage = 0;
    uint32_t receivedBytes = 0;
    float percentageStep = 100.0f / w.ws_col;
    float lastPercentage = 0.0f;

    while(true){

		recvd_data = recvDataHMAC(senderSocket,len);
		
		if(recvd_data == NULL){
			cout << "recvd_data NULL" <<endl;
			fm.finalize(true);
			return false;
		} 

		EncryptedChunk ec;
		ec.size  = len;
		ec.setCipherText(recvd_data);

		Chunk c;

		if(!dm.DecryptUpdate(c,ec)){
			cout<<"DecryptUpdate error"<<endl;
			fm.finalize(true);
			return false;		
		}

		file_size-= (c.size);


		if(file_size < AES_BLOCK){			//ultimo chunk

			if(!dm.DecryptFinal(c)){
				cout<<"DecryptFinal error"<<endl;
				fm.finalize(true);
				return false;
			}

		}

		receivedBytes += c.size;

		currentPercentage = (double)receivedBytes/(double)completeFileSize * 100;

		if(progressBar){

			while(lastPercentage + percentageStep < currentPercentage){
				cout << "\u25A0"<<flush;
				lastPercentage+=percentageStep;
			}

			/*int col,row;
			cout << "\033[" << col << ";" << row << "H";*/

			if(currentPercentage  >= 100){
				cout << endl;
			}
		}

		if(!verify.RSAUpdate(ec)){
			cout<<"[ERROR]"<<endl;
			fm.finalize(true);


			return false;
		}	

		status = fm.write(c);

		if(status == END_OF_FILE){
			cout<<"[LOG] Transfer Completed"<<endl;
			break;
		} else if(status == FILE_ERROR){
			cout << "FILE_ERROR" << endl;
			fm.finalize(true);
			return false;
		}
	}

	len = 0;
	char *recvd_digest = recvDataHMAC(senderSocket,len);	

	int result;
	result = verify.RSAFinal(recvd_digest);

	if(result != 1){
		fm.finalize(true);						//error
		cout<<"[ERROR] File Verification"<<endl;
		delete[] recvd_digest;
		return false;
	} else {
		cout<<"[LOG] File Verification OK"<<endl;
		fm.finalize(false);
	}
	
	delete[] recvd_digest;

	return true;

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2e8, %rsp            # imm = 0x2E8
movl %r8d, 0x18(%rsp)
cmpq $0x0, 0x8(%rsi)
je 0x8621
movq %rcx, %r14
movq %rdx, %rbx
movq %rdi, %r15
callq 0x4890
movq 0x6bec(%rip), %rdi      # 0xefc8
leaq 0x232e(%rip), %rsi      # 0xa711
callq 0x4400
movq %rax, %rdi
movq %r15, %rsi
callq 0x43d0
movq %rax, %rdi
callq 0x41d0
movq %rsp, %rsi
movq %rbx, %rdi
callq 0x6975
testb %al, %al
je 0x8621
movl (%rsp), %ebp
testl %ebp, %ebp
je 0x8628
andl $0x0, 0x8(%rsp)
movq 0x6ba3(%rip), %rdi      # 0xefc8
leaq 0x231c(%rip), %rsi      # 0xa748
callq 0x4400
movl (%rsp), %esi
movq %rax, %rdi
callq 0x42d0
movq %rax, %rdi
callq 0x41d0
movl (%rsp), %edx
leaq 0x80(%rsp), %rdi
movq %r15, %rsi
callq 0x70a8
leaq 0x10(%rsp), %rdi
callq 0x76a0
movq %rbx, 0x20(%rsp)
leaq 0x60(%rsp), %rdi
movq %r14, %rsi
callq 0x7b6e
xorl %ebx, %ebx
pushq $0x1
popq %rdi
leaq 0x78(%rsp), %r14
movl $0x5413, %esi           # imm = 0x5413
movq %r14, %rdx
xorl %eax, %eax
callq 0x4660
movzwl 0x2(%r14), %eax
cvtsi2ss %eax, %xmm0
movss 0x21ef(%rip), %xmm1     # 0xa68c
divss %xmm0, %xmm1
movss %xmm1, 0x1c(%rsp)
movl %ebp, %eax
cvtsi2sd %rax, %xmm0
movsd %xmm0, 0x28(%rsp)
xorpd %xmm0, %xmm0
movss %xmm0, 0x4(%rsp)
leaq 0x48(%rsp), %r12
leaq 0x30(%rsp), %r13
movq 0x6af9(%rip), %r15      # 0xefc8
leaq 0x2225(%rip), %r14      # 0xa6fb
movq 0x20(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x6b42
movq %rax, %rbp
testq %rax, %rax
je 0x864b
movq %r12, %rdi
callq 0x9450
movl 0x8(%rsp), %eax
movl %eax, 0x58(%rsp)
movq %r12, %rdi
movq %rbp, %rsi
callq 0x9496
movq %r13, %rdi
callq 0x93ac
leaq 0x10(%rsp), %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x773e
testb %al, %al
je 0x867b
movl 0x40(%rsp), %eax
movl (%rsp), %ecx
subl %eax, %ecx
movl %ecx, (%rsp)
cmpl $0xf, %ecx
ja 0x8556
leaq 0x10(%rsp), %rdi
movq %r13, %rsi
callq 0x785c
testb %al, %al
je 0x8735
movl 0x40(%rsp), %eax
addl %eax, %ebx
cvtsi2sd %rbx, %xmm0
divsd 0x28(%rsp), %xmm0
mulsd 0x2125(%rip), %xmm0     # 0xa690
cmpb $0x0, 0x18(%rsp)
je 0x85d5
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0xc(%rsp)
movss 0x4(%rsp), %xmm0
movss 0x1c(%rsp), %xmm2
addss %xmm0, %xmm2
movss 0xc(%rsp), %xmm1
movss %xmm2, 0x4(%rsp)
ucomiss %xmm2, %xmm1
jbe 0x85b8
movq %r15, %rdi
movq %r14, %rsi
callq 0x4400
movq %rax, %rdi
callq 0x42e0
movss 0x4(%rsp), %xmm0
jmp 0x8582
movss %xmm0, 0x4(%rsp)
movss 0xc(%rsp), %xmm0
ucomiss 0x20c1(%rip), %xmm0     # 0xa68c
jb 0x85d5
movq %r15, %rdi
callq 0x41d0
leaq 0x60(%rsp), %rdi
movq %r12, %rsi
callq 0x7bd0
testb %al, %al
je 0x8687
leaq 0x80(%rsp), %rdi
movq %r13, %rsi
callq 0x720a
cmpl $0x2, %eax
je 0x8693
cmpl $0x3, %eax
je 0x869f
movq %r13, %rdi
callq 0x93ba
movq %r12, %rdi
callq 0x945e
jmp 0x84d6
xorl %ebp, %ebp
jmp 0x8797
movq 0x6999(%rip), %rdi      # 0xefc8
leaq 0x20ed(%rip), %rsi      # 0xa723
callq 0x4400
movq %rax, %rdi
callq 0x41d0
movb $0x1, %bpl
jmp 0x8797
movq 0x6976(%rip), %rdi      # 0xefc8
leaq 0x2101(%rip), %rsi      # 0xa75a
callq 0x4400
movq %rax, %rdi
callq 0x41d0
leaq 0x80(%rsp), %rdi
pushq $0x1
popq %rsi
callq 0x727e
jmp 0x8774
leaq 0x20e8(%rip), %rsi      # 0xa76a
jmp 0x873c
leaq 0x2065(%rip), %rsi      # 0xa6f3
jmp 0x873c
leaq 0x2110(%rip), %rsi      # 0xa7aa
jmp 0x873c
movq 0x6922(%rip), %rdi      # 0xefc8
leaq 0x20e4(%rip), %rsi      # 0xa791
callq 0x4400
movq %rax, %rdi
callq 0x41d0
leaq 0x30(%rsp), %rdi
callq 0x93ba
leaq 0x48(%rsp), %rdi
callq 0x945e
leaq 0x8(%rsp), %rsi
andl $0x0, (%rsi)
movq 0x20(%rsp), %rdi
callq 0x6b42
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
movq %rax, %rsi
callq 0x7c7c
cmpl $0x1, %eax
jne 0x87ab
movq 0x68c8(%rip), %rdi      # 0xefc8
leaq 0x20c8(%rip), %rsi      # 0xa7cf
callq 0x4400
movq %rax, %rdi
callq 0x41d0
leaq 0x80(%rsp), %rdi
xorl %esi, %esi
callq 0x727e
movb $0x1, %bpl
testq %rbx, %rbx
je 0x8776
movq %rbx, %rdi
callq 0x45a0
jmp 0x8776
leaq 0x2042(%rip), %rsi      # 0xa77e
movq 0x6885(%rip), %rdi      # 0xefc8
callq 0x4400
movq %rax, %rdi
callq 0x41d0
leaq 0x80(%rsp), %rdi
pushq $0x1
popq %rsi
callq 0x727e
leaq 0x30(%rsp), %rdi
callq 0x93ba
leaq 0x48(%rsp), %rdi
callq 0x945e
xorl %ebp, %ebp
leaq 0x60(%rsp), %rdi
callq 0x7ca8
leaq 0x10(%rsp), %rdi
callq 0x772a
leaq 0x80(%rsp), %rdi
callq 0x8848
movl %ebp, %eax
addq $0x2e8, %rsp            # imm = 0x2E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x80(%rsp), %rdi
pushq $0x1
popq %rsi
callq 0x727e
movq 0x6806(%rip), %rdi      # 0xefc8
leaq 0x1fec(%rip), %rsi      # 0xa7b5
callq 0x4400
movq %rax, %rdi
callq 0x41d0
testq %rbx, %rbx
je 0x8774
movq %rbx, %rdi
callq 0x45a0
jmp 0x8774
jmp 0x8800
jmp 0x8800
jmp 0x8800
movq %rax, %rbx
jmp 0x8828
movq %rax, %rbx
jmp 0x8832
jmp 0x8807
jmp 0x87f9
movq %rax, %rbx
jmp 0x8814
jmp 0x8800
movq %rax, %rbx
jmp 0x881e
jmp 0x8807
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x93ba
leaq 0x48(%rsp), %rdi
callq 0x945e
leaq 0x60(%rsp), %rdi
callq 0x7ca8
leaq 0x10(%rsp), %rdi
callq 0x772a
leaq 0x80(%rsp), %rdi
callq 0x8848
movq %rbx, %rdi
callq 0x47a0
nop