// File: /workspace/github_cpp_makefile_star2_O3/luigix25[P]SSLP/library/SendReceiveFile.cpp
// Function: SendFile(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>&, NetSocket&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>&, char const*, bool)
bool SendFile(string& path,NetSocket& receiverSocket,string &filename,const char *key_path,bool progressBar){

	if(filename.size() == 0){
		return false;
	}

	uint32_t size;

	bool return_value = checkValidityFilename(filename);
	if(!return_value){
		size = 0;
		sendIntHMAC(receiverSocket,size);
		return false;	
	}

	path+= filename;
	ReadFileManager fm(path);
	uint64_t size_64 = fm.size_file();			//32 bit ok
	cout<<"File size: "<<size_64<<endl;	


	if(size_64 > UINT32_MAX){					//check for overflow
		//Bigger than 4GiBs
		cout<<"File bigger than 4GiBs"<<endl;
		size = 0;
	} else {
		size = (uint32_t)size_64;
	}

	if(!sendIntHMAC(receiverSocket,size)) return false;		//32 bit ok

	if(size == 0){				//file non esistente
		return true;
	}

	//For the progressbar
	struct winsize w;
    ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);

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

	completeFileSize = size;


	Chunk c;
	file_status status;
	bool last = false;

	EncryptManager em;

 	char* digest;
 
	RSASignManager sign(key_path);

	while(true){
	
		status = fm.read(c);

		if(status == FILE_ERROR){
			cout<<"[ERROR]File"<<endl;
			return false;
		} else if(status == END_OF_FILE){
			cout<<"EOF"<<endl;
			last = true;
		}


		EncryptedChunk ec;

		if(!em.EncryptUpdate(ec,c)){
			//cout<<"HANDLE ERROR"<<endl;
			return false;
		}	


		if(last){
			if(!em.EncryptFinal(ec)){
				return false;
			}


		}

		if(!sign.RSAUpdate(ec)){						//hash on plaintext
			return false;
		}


		if(!sendDataHMAC(receiverSocket,ec.getCipherText(),ec.size)){			//aggiorno il nonce
			cout<<"[ERROR]"<<endl;
			return false;
		}

		sentBytes += c.size;
		c.size = 0;

		currentPercentage = (double)sentBytes/(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;
			}
		}
		
		//delete[] ec.ciphertext;


		if(last)
			break;

	}

	uint32_t sign_len;

	digest = sign.RSAFinal(sign_len);

	if(digest == NULL){
		cout<<"[ERROR] SignFinal"<<endl;
		return false;
	}	

	if(!sendDataHMAC(receiverSocket,digest,sign_len)){		
		cout<<"[ERROR]"<<endl;
		delete[] digest;
		return false;
	}

	delete[] digest;

	return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2f8, %rsp            # imm = 0x2F8
movl %r8d, 0x18(%rsp)
movq %rdx, %r12
movq 0x8(%rdx), %rdx
testq %rdx, %rdx
je 0x8e25
movq %rcx, %r15
movq %rsi, %rbx
movq %rdi, %r14
leaq 0x68(%rsp), %rbp
movq %rbp, -0x10(%rbp)
movq (%r12), %rsi
addq %rsi, %rdx
leaq 0x58(%rsp), %r13
movq %r13, %rdi
callq 0x69fa
movq %r13, %rdi
callq 0x89c0
movl %eax, %r13d
movq 0x58(%rsp), %rdi
cmpq %rbp, %rdi
je 0x8aac
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x4450
testb %r13b, %r13b
je 0x8e2c
movq (%r12), %rsi
movq 0x8(%r12), %rdx
movq %r14, %rdi
callq 0x4150
leaq 0xb0(%rsp), %r12
movq %r12, %rdi
movq %r14, %rsi
callq 0x7c78
movq %r12, %rdi
callq 0x78d4
movq %rax, %r12
movq 0x74d5(%rip), %rdi      # 0xffc0
leaq 0x2bbe(%rip), %rsi      # 0xb6b0
movl $0xb, %edx
callq 0x44e0
movq 0x74bd(%rip), %r14      # 0xffc0
movq %r14, %rdi
movq %r12, %rsi
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
movq %r12, %rax
shrq $0x20, %rax
je 0x8b8b
movq 0x7478(%rip), %r12      # 0xffc0
leaq 0x2b6d(%rip), %rsi      # 0xb6bc
movl $0x16, %edx
movq %r12, %rdi
callq 0x44e0
movq (%r12), %rax
addq -0x18(%rax), %r12
movq %r12, %rdi
movl $0xa, %esi
callq 0x4410
movsbl %al, %esi
movq 0x7445(%rip), %rdi      # 0xffc0
callq 0x4040
xorl %r12d, %r12d
movq %rax, %rdi
callq 0x42f0
movq %rbx, %rdi
movl %r12d, %esi
callq 0x6f3f
testb %al, %al
je 0x8e3d
testl %r12d, %r12d
je 0x8e41
leaq 0x78(%rsp), %r13
movl $0x5413, %esi           # imm = 0x5413
movl $0x1, %edi
movq %r13, %rdx
xorl %eax, %eax
callq 0x4660
movzwl 0x2(%r13), %ebp
leaq 0x20(%rsp), %rdi
callq 0xa2d0
leaq 0x10(%rsp), %rdi
callq 0x7eb4
leaq 0x80(%rsp), %rdi
movq %r15, %rsi
callq 0x86d0
cvtsi2ss %ebp, %xmm0
movss 0x2a83(%rip), %xmm1     # 0xb678
divss %xmm0, %xmm1
movss %xmm1, 0x1c(%rsp)
movl %r12d, %eax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
movsd %xmm0, 0x38(%rsp)
xorpd %xmm0, %xmm0
movss %xmm0, 0x8(%rsp)
xorl %r12d, %r12d
leaq 0x40(%rsp), %r13
leaq 0x2ac2(%rip), %r15      # 0xb6eb
leaq 0xb0(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x7dfe
cmpl $0x3, %eax
je 0x8c4d
cmpl $0x2, %eax
je 0x8eda
xorl %ebp, %ebp
jmp 0x8c8b
movl $0x3, %edx
movq %r14, %rdi
leaq 0x2a83(%rip), %rsi      # 0xb6df
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
movb $0x1, %bpl
movq %rax, %rdi
callq 0x42f0
movq %r13, %rdi
callq 0xa3c2
leaq 0x10(%rsp), %rdi
movq %r13, %rsi
leaq 0x20(%rsp), %rdx
callq 0x7fb2
testb %al, %al
je 0x8e8c
testb %bpl, %bpl
je 0x8cc7
leaq 0x10(%rsp), %rdi
movq %r13, %rsi
callq 0x8030
testb %al, %al
je 0x8e8c
leaq 0x80(%rsp), %rdi
movq %r13, %rsi
callq 0x87be
testb %al, %al
je 0x8e8c
movq %r13, %rdi
callq 0xa43e
movl 0x50(%rsp), %edx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x71a2
testb %al, %al
je 0x8e46
addl 0x30(%rsp), %r12d
xorps %xmm0, %xmm0
cvtsi2sd %r12, %xmm0
movl $0x0, 0x30(%rsp)
divsd 0x38(%rsp), %xmm0
mulsd 0x295f(%rip), %xmm0     # 0xb680
cmpb $0x0, 0x18(%rsp)
je 0x8db3
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0xc(%rsp)
movss 0x8(%rsp), %xmm0
movss 0x1c(%rsp), %xmm2
addss %xmm0, %xmm2
movss 0xc(%rsp), %xmm1
movss %xmm2, 0x8(%rsp)
ucomiss %xmm2, %xmm1
jbe 0x8d77
movl $0x3, %edx
movq %r14, %rdi
movq %r15, %rsi
callq 0x44e0
movq %r14, %rdi
callq 0x42f0
movss 0x8(%rsp), %xmm0
jmp 0x8d3c
movss %xmm0, 0x8(%rsp)
movss 0xc(%rsp), %xmm0
ucomiss 0x28ee(%rip), %xmm0     # 0xb678
jb 0x8db3
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
movq %r13, %rdi
callq 0xa3d6
testb %bpl, %bpl
je 0x8c29
leaq 0x80(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x880e
movq %rax, %r14
testq %rax, %rax
je 0x8f25
movl 0x40(%rsp), %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x71a2
movl %eax, %ebp
testb %al, %al
jne 0x8e1b
movq 0x71c2(%rip), %rdi      # 0xffc0
leaq 0x28de(%rip), %rsi      # 0xb6e3
movl $0x7, %edx
callq 0x44e0
movq 0x71aa(%rip), %rdi      # 0xffc0
callq 0x41e0
movq %r14, %rdi
callq 0x45c0
jmp 0x8e98
xorl %ebp, %ebp
jmp 0x8ec6
xorl %ebp, %ebp
movq %rbx, %rdi
xorl %esi, %esi
callq 0x6f3f
jmp 0x8ec6
xorl %ebp, %ebp
jmp 0x8eb9
movb $0x1, %bpl
jmp 0x8eb9
movq 0x7173(%rip), %rbx      # 0xffc0
leaq 0x288f(%rip), %rsi      # 0xb6e3
movl $0x7, %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 0x7141(%rip), %rdi      # 0xffc0
callq 0x4040
movq %rax, %rdi
callq 0x42f0
leaq 0x40(%rsp), %rdi
callq 0xa3d6
xorl %ebp, %ebp
leaq 0x80(%rsp), %rdi
callq 0x897c
leaq 0x10(%rsp), %rdi
callq 0x7f3e
leaq 0x20(%rsp), %rdi
callq 0xa2e4
leaq 0xb0(%rsp), %rdi
callq 0x795e
movl %ebp, %eax
addq $0x2f8, %rsp            # imm = 0x2F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x70df(%rip), %rbx      # 0xffc0
leaq 0x27eb(%rip), %rsi      # 0xb6d3
movl $0xb, %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 0x70ad(%rip), %rdi      # 0xffc0
callq 0x4040
movq %rax, %rdi
callq 0x42f0
jmp 0x8e96
movq 0x7094(%rip), %rdi      # 0xffc0
leaq 0x27bc(%rip), %rsi      # 0xb6ef
movl $0x11, %edx
callq 0x44e0
movq 0x707c(%rip), %rdi      # 0xffc0
callq 0x41e0
jmp 0x8e96
jmp 0x8f7e
jmp 0x8f7e
jmp 0x8f8a
movq %rax, %rbx
jmp 0x8fa4
movq %rax, %rbx
jmp 0x8fae
jmp 0x8f83
movq %rax, %rbx
movq 0x58(%rsp), %rdi
cmpq %rbp, %rdi
je 0x8fc5
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x4450
jmp 0x8fc5
jmp 0x8f7e
movq %rax, %rbx
jmp 0x8f97
movq %rax, %rbx
jmp 0x8fb8
jmp 0x8f8a
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0xa3d6
leaq 0x80(%rsp), %rdi
callq 0x897c
leaq 0x10(%rsp), %rdi
callq 0x7f3e
leaq 0x20(%rsp), %rdi
callq 0xa2e4
leaq 0xb0(%rsp), %rdi
callq 0x795e
movq %rbx, %rdi
callq 0x47d0