// File: /workspace/github_cpp_makefile_star2_O1/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 0x950a
movq %rcx, %r15
movq %rdi, %r12
movq (%rsi), %rsi
callq 0x4150
movq 0x6f27(%rip), %r14      # 0xffc0
leaq 0x2661(%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 0x7015
testb %al, %al
je 0x950a
movl (%rsp), %ebp
testl %ebp, %ebp
je 0x9511
movl $0x0, 0x8(%rsp)
movq 0x6eab(%rip), %r14      # 0xffc0
leaq 0x261c(%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 0x7a28
leaq 0x10(%rsp), %rdi
callq 0x8110
leaq 0x30(%rsp), %rdi
movq %r15, %rsi
callq 0x85e4
movq $0x0, 0x20(%rsp)
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 0x24bd(%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 0x60(%rsp), %r12
leaq 0x48(%rsp), %r13
leaq 0x24fb(%rip), %rbp      # 0xb6eb
movq %rbx, %rdi
leaq 0x8(%rsp), %rsi
callq 0x7270
movq %rax, %r15
testq %rax, %rax
je 0x9553
movq %r12, %rdi
callq 0xa48e
movl 0x8(%rsp), %eax
movl %eax, 0x70(%rsp)
movq %r12, %rdi
movq %r15, %rsi
callq 0xa4dc
movq %r13, %rdi
callq 0xa378
leaq 0x10(%rsp), %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x81ae
movl $0x13, %edx
leaq 0x2512(%rip), %rsi      # 0xb75a
testb %al, %al
je 0x92f7
movl (%rsp), %eax
subl 0x58(%rsp), %eax
movl %eax, (%rsp)
cmpl $0xf, %eax
ja 0x927c
leaq 0x10(%rsp), %rdi
movq %r13, %rsi
callq 0x82cc
movl $0x12, %edx
leaq 0x24f6(%rip), %rsi      # 0xb76e
testb %al, %al
je 0x92f7
movq 0x20(%rsp), %r15
addl 0x58(%rsp), %r15d
xorps %xmm0, %xmm0
cvtsi2sd %r15, %xmm0
divsd 0x28(%rsp), %xmm0
mulsd 0x23e4(%rip), %xmm0     # 0xb680
cmpb $0x0, 0x18(%rsp)
movq %r15, 0x20(%rsp)
je 0x937f
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 0x9343
movl $0x3, %edx
movq %r14, %rdi
movq %rbp, %rsi
callq 0x44e0
movq %r14, %rdi
callq 0x42f0
movss 0x4(%rsp), %xmm0
jmp 0x92bc
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
movl $0x1, %r15d
leaq 0x80(%rsp), %rdi
movl $0x1, %esi
callq 0x7c98
jmp 0x9453
movss %xmm0, 0x4(%rsp)
movss 0xc(%rsp), %xmm0
ucomiss 0x2322(%rip), %xmm0     # 0xb678
jb 0x937f
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 0x30(%rsp), %rdi
movq %r12, %rsi
callq 0x8646
movl $0x7, %edx
leaq 0x234b(%rip), %rsi      # 0xb6e3
testb %al, %al
je 0x940c
leaq 0x80(%rsp), %rdi
movq %r13, %rsi
callq 0x7c22
cmpl $0x2, %eax
je 0x9400
xorl %r15d, %r15d
cmpl $0x3, %eax
jne 0x9453
movl $0x18, %edx
movq %r14, %rdi
leaq 0x23b5(%rip), %rsi      # 0xb781
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
movl $0x3, %r15d
movq %rax, %rdi
callq 0x42f0
jmp 0x9453
movl $0xa, %edx
leaq 0x238e(%rip), %rsi      # 0xb79a
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
movl $0x1, %r15d
leaq 0x80(%rsp), %rdi
movl $0x1, %esi
callq 0x7c98
movq %r13, %rdi
callq 0xa38c
movq %r12, %rdi
callq 0xa4a2
testl %r15d, %r15d
je 0x91f0
cmpl $0x3, %r15d
jne 0x95ab
leaq 0x8(%rsp), %rsi
movl $0x0, (%rsi)
movq %rbx, %rdi
callq 0x7270
movq %rax, %r14
leaq 0x30(%rsp), %rdi
movq %rax, %rsi
callq 0x86f2
cmpl $0x1, %eax
sete %bl
jne 0x95af
movq 0x6b14(%rip), %r15      # 0xffc0
leaq 0x230c(%rip), %rsi      # 0xb7bf
movl $0x1a, %edx
movq %r15, %rdi
callq 0x44e0
movq (%r15), %rax
addq -0x18(%rax), %r15
movq %r15, %rdi
movl $0xa, %esi
callq 0x4410
movsbl %al, %esi
movq 0x6ae2(%rip), %rdi      # 0xffc0
callq 0x4040
movq %rax, %rdi
callq 0x42f0
leaq 0x80(%rsp), %rdi
xorl %esi, %esi
callq 0x7c98
testq %r14, %r14
jne 0x960c
movb $0x1, %bl
jmp 0x9614
xorl %ebx, %ebx
jmp 0x9668
leaq 0x21fb(%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, %bl
jmp 0x9668
movq 0x6a66(%rip), %rbx      # 0xffc0
leaq 0x21e9(%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 0x6a34(%rip), %rdi      # 0xffc0
callq 0x4040
movq %rax, %rdi
callq 0x42f0
leaq 0x80(%rsp), %rdi
movl $0x1, %esi
callq 0x7c98
xorl %ebx, %ebx
jmp 0x9614
leaq 0x80(%rsp), %rdi
movl $0x1, %esi
callq 0x7c98
movq 0x69f8(%rip), %r15      # 0xffc0
leaq 0x21d6(%rip), %rsi      # 0xb7a5
movl $0x19, %edx
movq %r15, %rdi
callq 0x44e0
movq (%r15), %rax
addq -0x18(%rax), %r15
movq %r15, %rdi
movl $0xa, %esi
callq 0x4410
movsbl %al, %esi
movq 0x69c6(%rip), %rdi      # 0xffc0
callq 0x4040
movq %rax, %rdi
callq 0x42f0
testq %r14, %r14
je 0x95ab
movq %r14, %rdi
callq 0x45c0
leaq 0x30(%rsp), %rdi
callq 0x871e
leaq 0x10(%rsp), %rdi
callq 0x819a
leaq 0x6701(%rip), %rax      # 0xfd30
addq $0x10, %rax
leaq 0x2d8(%rsp), %rcx
movq %rax, -0x258(%rcx)
movq -0x10(%rcx), %rdi
cmpq %rcx, %rdi
je 0x965b
movq 0x2d8(%rsp), %rsi
incq %rsi
callq 0x4450
leaq 0x80(%rsp), %rdi
callq 0x79d4
movl %ebx, %eax
addq $0x2e8, %rsp            # imm = 0x2E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x9695
movq %rax, %rbx
jmp 0x96bd
movq %rax, %rbx
jmp 0x96c7
jmp 0x9695
jmp 0x9695
jmp 0x9690
jmp 0x9695
movq %rax, %rbx
jmp 0x96a9
movq %rax, %rbx
jmp 0x96b3
jmp 0x969c
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0xa38c
leaq 0x60(%rsp), %rdi
callq 0xa4a2
leaq 0x30(%rsp), %rdi
callq 0x871e
leaq 0x10(%rsp), %rdi
callq 0x819a
leaq 0x80(%rsp), %rdi
callq 0x96dc
movq %rbx, %rdi
callq 0x47d0