// File: /workspace/github_cpp_makefile_star2_O3/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)
movq %rdx, %rbx
movq 0x8(%rsi), %rdx
testq %rdx, %rdx
je 0x92d3
movq %rcx, %r15
movq %rdi, %r12
movq (%rsi), %rsi
callq 0x4150
movq 0x6fb8(%rip), %r14      # 0xffc0
leaq 0x26f2(%rip), %rsi      # 0xb701
movl $0x11, %edx
movq %r14, %rdi
callq 0x44e0
movq (%r12), %rsi
movq 0x8(%r12), %rdx
movq %r14, %rdi
callq 0x44e0
movq %rax, %r13
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x4410
movsbl %al, %esi
movq %r13, %rdi
callq 0x4040
movq %rax, %rdi
callq 0x42f0
movq %rsp, %rsi
movq %rbx, %rdi
callq 0x7003
testb %al, %al
je 0x92d3
movl (%rsp), %ebp
testl %ebp, %ebp
je 0x92da
movl $0x0, 0x8(%rsp)
movq 0x6f3c(%rip), %r14      # 0xffc0
leaq 0x26ad(%rip), %rsi      # 0xb738
movl $0x11, %edx
movq %r14, %rdi
callq 0x44e0
movl (%rsp), %esi
movq %r14, %rdi
callq 0x42e0
movq %rax, %r13
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x4410
movsbl %al, %esi
movq %r13, %rdi
callq 0x4040
movq %rax, %rdi
callq 0x42f0
movl (%rsp), %edx
leaq 0x80(%rsp), %rdi
movq %r12, %rsi
callq 0x79b4
leaq 0x10(%rsp), %rdi
callq 0x809c
movq %rbx, 0x20(%rsp)
leaq 0x60(%rsp), %rdi
movq %r15, %rsi
callq 0x8570
xorl %ebx, %ebx
leaq 0x78(%rsp), %r15
movl $0x5413, %esi           # imm = 0x5413
movl $0x1, %edi
movq %r15, %rdx
xorl %eax, %eax
callq 0x4660
movzwl 0x2(%r15), %eax
cvtsi2ss %eax, %xmm0
movss 0x2550(%rip), %xmm1     # 0xb678
divss %xmm0, %xmm1
movss %xmm1, 0x1c(%rsp)
movl %ebp, %eax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
movsd %xmm0, 0x28(%rsp)
xorpd %xmm0, %xmm0
movss %xmm0, 0x4(%rsp)
leaq 0x48(%rsp), %r13
leaq 0x30(%rsp), %r15
leaq 0x258e(%rip), %r12      # 0xb6eb
movq 0x20(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x7250
movq %rax, %rbp
testq %rax, %rax
je 0x931d
movq %r13, %rdi
callq 0xa3c2
movl 0x8(%rsp), %eax
movl %eax, 0x58(%rsp)
movq %r13, %rdi
movq %rbp, %rsi
callq 0xa410
movq %r15, %rdi
callq 0xa2d0
leaq 0x10(%rsp), %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x813a
testb %al, %al
je 0x937a
movl 0x40(%rsp), %eax
movl (%rsp), %ecx
subl %eax, %ecx
movl %ecx, (%rsp)
cmpl $0xf, %ecx
ja 0x91dd
leaq 0x10(%rsp), %rdi
movq %r15, %rsi
callq 0x8258
testb %al, %al
je 0x949d
movl 0x40(%rsp), %eax
addl %eax, %ebx
xorps %xmm0, %xmm0
cvtsi2sd %rbx, %xmm0
divsd 0x28(%rsp), %xmm0
mulsd 0x248b(%rip), %xmm0     # 0xb680
cmpb $0x0, 0x18(%rsp)
je 0x9287
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 0x924b
movl $0x3, %edx
movq %r14, %rdi
movq %r12, %rsi
callq 0x44e0
movq %r14, %rdi
callq 0x42f0
movss 0x4(%rsp), %xmm0
jmp 0x9210
movss %xmm0, 0x4(%rsp)
movss 0xc(%rsp), %xmm0
ucomiss 0x241a(%rip), %xmm0     # 0xb678
jb 0x9287
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x4410
movsbl %al, %esi
movq %r14, %rdi
callq 0x4040
movq %rax, %rdi
callq 0x42f0
leaq 0x60(%rsp), %rdi
movq %r13, %rsi
callq 0x85d2
testb %al, %al
je 0x938b
leaq 0x80(%rsp), %rdi
movq %r15, %rsi
callq 0x7bae
cmpl $0x2, %eax
je 0x948f
cmpl $0x3, %eax
je 0x939c
movq %r15, %rdi
callq 0xa2e4
movq %r13, %rdi
callq 0xa3d6
jmp 0x915d
xorl %ebp, %ebp
jmp 0x955f
leaq 0x2432(%rip), %rsi      # 0xb713
movl $0x24, %edx
movq %r14, %rdi
callq 0x44e0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x4410
movsbl %al, %esi
movq %r14, %rdi
callq 0x4040
movq %rax, %rdi
callq 0x42f0
movb $0x1, %bpl
jmp 0x955f
movq 0x6c9c(%rip), %rbx      # 0xffc0
leaq 0x241f(%rip), %rsi      # 0xb74a
movl $0xf, %edx
movq %rbx, %rdi
callq 0x44e0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x4410
movsbl %al, %esi
movq 0x6c6a(%rip), %rdi      # 0xffc0
callq 0x4040
movq %rax, %rdi
callq 0x42f0
leaq 0x80(%rsp), %rdi
movl $0x1, %esi
callq 0x7c24
jmp 0x9509
movl $0x13, %edx
leaq 0x23d4(%rip), %rsi      # 0xb75a
jmp 0x94a9
movl $0x7, %edx
leaq 0x234c(%rip), %rsi      # 0xb6e3
jmp 0x94a9
movq 0x6c1d(%rip), %r14      # 0xffc0
leaq 0x23d7(%rip), %rsi      # 0xb781
movl $0x18, %edx
movq %r14, %rdi
callq 0x44e0
movq (%r14), %rax
addq -0x18(%rax), %r14
movq %r14, %rdi
movl $0xa, %esi
callq 0x4410
movsbl %al, %esi
movq 0x6beb(%rip), %rdi      # 0xffc0
callq 0x4040
movq %rax, %rdi
callq 0x42f0
leaq 0x30(%rsp), %rdi
callq 0xa2e4
leaq 0x48(%rsp), %rdi
callq 0xa3d6
leaq 0x8(%rsp), %rsi
movl $0x0, (%rsi)
movq 0x20(%rsp), %rdi
callq 0x7250
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
movq %rax, %rsi
callq 0x867e
cmpl $0x1, %eax
jne 0x9573
movq 0x6b95(%rip), %r14      # 0xffc0
leaq 0x238d(%rip), %rsi      # 0xb7bf
movl $0x1a, %edx
movq %r14, %rdi
callq 0x44e0
movq (%r14), %rax
addq -0x18(%rax), %r14
movq %r14, %rdi
movl $0xa, %esi
callq 0x4410
movsbl %al, %esi
movq 0x6b63(%rip), %rdi      # 0xffc0
callq 0x4040
movq %rax, %rdi
callq 0x42f0
leaq 0x80(%rsp), %rdi
xorl %esi, %esi
callq 0x7c24
movb $0x1, %bpl
testq %rbx, %rbx
je 0x950b
movq %rbx, %rdi
callq 0x45c0
jmp 0x950b
movl $0xa, %edx
leaq 0x22ff(%rip), %rsi      # 0xb79a
jmp 0x94a9
movl $0x12, %edx
leaq 0x22c5(%rip), %rsi      # 0xb76e
movq 0x6b10(%rip), %rbx      # 0xffc0
movq %rbx, %rdi
callq 0x44e0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x4410
movsbl %al, %esi
movq 0x6aea(%rip), %rdi      # 0xffc0
callq 0x4040
movq %rax, %rdi
callq 0x42f0
leaq 0x80(%rsp), %rdi
movl $0x1, %esi
callq 0x7c24
leaq 0x30(%rsp), %rdi
callq 0xa2e4
leaq 0x48(%rsp), %rdi
callq 0xa3d6
xorl %ebp, %ebp
leaq 0x60(%rsp), %rdi
callq 0x86aa
leaq 0x10(%rsp), %rdi
callq 0x8126
leaq 0x680a(%rip), %rax      # 0xfd30
addq $0x10, %rax
leaq 0x2d8(%rsp), %rcx
movq %rax, -0x258(%rcx)
movq -0x10(%rcx), %rdi
cmpq %rcx, %rdi
je 0x9552
movq 0x2d8(%rsp), %rsi
incq %rsi
callq 0x4450
leaq 0x80(%rsp), %rdi
callq 0x795e
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
movl $0x1, %esi
callq 0x7c24
movq 0x6a34(%rip), %r14      # 0xffc0
leaq 0x2212(%rip), %rsi      # 0xb7a5
movl $0x19, %edx
movq %r14, %rdi
callq 0x44e0
movq (%r14), %rax
addq -0x18(%rax), %r14
movq %r14, %rdi
movl $0xa, %esi
callq 0x4410
movsbl %al, %esi
movq 0x6a02(%rip), %rdi      # 0xffc0
callq 0x4040
movq %rax, %rdi
callq 0x42f0
testq %rbx, %rbx
je 0x9509
movq %rbx, %rdi
callq 0x45c0
jmp 0x9509
jmp 0x95fc
jmp 0x95fc
movq %rax, %rbx
jmp 0x9624
movq %rax, %rbx
jmp 0x962e
jmp 0x95fc
jmp 0x9603
jmp 0x95fc
jmp 0x95f7
movq %rax, %rbx
jmp 0x9610
movq %rax, %rbx
jmp 0x961a
jmp 0x9603
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0xa2e4
leaq 0x48(%rsp), %rdi
callq 0xa3d6
leaq 0x60(%rsp), %rdi
callq 0x86aa
leaq 0x10(%rsp), %rdi
callq 0x8126
leaq 0x80(%rsp), %rdi
callq 0x9644
movq %rbx, %rdi
callq 0x47d0
nop