// File: /workspace/github_cpp_makefile_star2_O1/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 $0x308, %rsp            # imm = 0x308
movq %rdx, %r12
movq 0x8(%rdx), %rdx
testq %rdx, %rdx
je 0x8f0a
movq %rdi, %r14
movq %rcx, 0x8(%rsp)
movl %r8d, 0x20(%rsp)
movq %rsi, %rbx
leaq 0x78(%rsp), %r15
movq %r15, -0x10(%r15)
movq (%r12), %rsi
addq %rsi, %rdx
leaq 0x68(%rsp), %r13
movq %r13, %rdi
callq 0x6a0e
movq %r13, %rdi
callq 0x8a34
movl %eax, %ebp
movq 0x68(%rsp), %rdi
cmpq %r15, %rdi
je 0x8b21
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x4450
testb %bpl, %bpl
je 0x8f11
movq (%r12), %rsi
movq 0x8(%r12), %rdx
movq %r14, %rdi
callq 0x4150
leaq 0xc0(%rsp), %r12
movq %r12, %rdi
movq %r14, %rsi
callq 0x7cec
movq %r12, %rdi
callq 0x794a
movq %rax, %r12
movq 0x7460(%rip), %rdi      # 0xffc0
leaq 0x2b49(%rip), %rsi      # 0xb6b0
movl $0xb, %edx
callq 0x44e0
movq 0x7448(%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 0x8c00
movq 0x7403(%rip), %r12      # 0xffc0
leaq 0x2af8(%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 0x73d0(%rip), %rdi      # 0xffc0
callq 0x4040
xorl %r12d, %r12d
movq %rax, %rdi
callq 0x42f0
movq %rbx, %rdi
movl %r12d, %esi
callq 0x6f51
testb %al, %al
je 0x8f22
testl %r12d, %r12d
je 0x8f29
leaq 0x88(%rsp), %r13
movl $0x5413, %esi           # imm = 0x5413
movl $0x1, %edi
movq %r13, %rdx
xorl %eax, %eax
callq 0x4660
movzwl 0x2(%r13), %ebp
leaq 0x38(%rsp), %rdi
callq 0xa378
leaq 0x18(%rsp), %rdi
callq 0x7f28
leaq 0x90(%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0x8744
cvtsi2ss %ebp, %xmm0
movss 0x2a09(%rip), %xmm1     # 0xb678
divss %xmm0, %xmm1
movss %xmm1, 0x24(%rsp)
movl %r12d, %eax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
movsd %xmm0, 0x30(%rsp)
xorpd %xmm0, %xmm0
movss %xmm0, 0x8(%rsp)
movq $0x0, 0x28(%rsp)
leaq 0x38(%rsp), %r12
leaq 0x50(%rsp), %r13
xorl %ebp, %ebp
leaq 0xc0(%rsp), %rdi
movq %r12, %rsi
callq 0x7e72
cmpl $0x3, %eax
je 0x8cc8
cmpl $0x2, %eax
jne 0x8d06
jmp 0x8f31
movl $0x3, %edx
movq %r14, %rdi
leaq 0x2a08(%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 0xa48e
leaq 0x18(%rsp), %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x8026
movl $0x1, %r15d
testb %al, %al
je 0x8e8b
testb $0x1, %bpl
je 0x8d47
leaq 0x18(%rsp), %rdi
movq %r13, %rsi
callq 0x80a4
testb %al, %al
je 0x8e8b
leaq 0x90(%rsp), %rdi
movq %r13, %rsi
callq 0x8832
testb %al, %al
je 0x8e8b
movq %r13, %rdi
callq 0xa516
movl 0x60(%rsp), %edx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x71c2
testb %al, %al
je 0x8e07
movq 0x28(%rsp), %rax
addl 0x48(%rsp), %eax
movq %rax, 0x28(%rsp)
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
movl $0x0, 0x48(%rsp)
divsd 0x30(%rsp), %xmm0
mulsd 0x28d6(%rip), %xmm0     # 0xb680
cmpb $0x0, 0x20(%rsp)
je 0x8e80
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x14(%rsp)
leaq 0x2925(%rip), %r15      # 0xb6eb
movss 0x8(%rsp), %xmm0
movss 0x24(%rsp), %xmm2
addss %xmm0, %xmm2
movss 0x14(%rsp), %xmm1
movss %xmm2, 0x8(%rsp)
ucomiss %xmm2, %xmm1
jbe 0x8e44
movl $0x3, %edx
movq %r14, %rdi
movq %r15, %rsi
callq 0x44e0
movq %r14, %rdi
callq 0x42f0
movss 0x8(%rsp), %xmm0
jmp 0x8dcc
movl $0x7, %edx
movq %r14, %rdi
leaq 0x28cd(%rip), %rsi      # 0xb6e3
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
jmp 0x8e8b
movss %xmm0, 0x8(%rsp)
movss 0x14(%rsp), %xmm0
ucomiss 0x2821(%rip), %xmm0     # 0xb678
jb 0x8e80
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
movzbl %bpl, %eax
andl $0x1, %eax
leal (%rax,%rax,2), %r15d
movq %r13, %rdi
callq 0xa4a2
testl %r15d, %r15d
je 0x8ca9
cmpl $0x3, %r15d
jne 0x8f9d
leaq 0x90(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0x8882
movq %rax, %r14
testq %rax, %rax
je 0x8f79
movl 0x50(%rsp), %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x71c2
movl %eax, %ebp
testb %al, %al
jne 0x8efd
movq 0x70e0(%rip), %rdi      # 0xffc0
leaq 0x27fc(%rip), %rsi      # 0xb6e3
movl $0x7, %edx
callq 0x44e0
movq 0x70c8(%rip), %rdi      # 0xffc0
callq 0x41e0
movq %r14, %rdi
callq 0x45c0
jmp 0x8f9f
xorl %ebp, %ebp
jmp 0x8fcd
xorl %ebp, %ebp
movq %rbx, %rdi
xorl %esi, %esi
callq 0x6f51
jmp 0x8fcd
xorl %ebp, %ebp
jmp 0x8fc0
movb $0x1, %bpl
jmp 0x8fc0
movq 0x7088(%rip), %rbx      # 0xffc0
leaq 0x2794(%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 0x7056(%rip), %rdi      # 0xffc0
callq 0x4040
movq %rax, %rdi
callq 0x42f0
jmp 0x8f9d
movq 0x7040(%rip), %rdi      # 0xffc0
leaq 0x2768(%rip), %rsi      # 0xb6ef
movl $0x11, %edx
callq 0x44e0
movq 0x7028(%rip), %rdi      # 0xffc0
callq 0x41e0
xorl %ebp, %ebp
leaq 0x90(%rsp), %rdi
callq 0x89f0
leaq 0x18(%rsp), %rdi
callq 0x7fb2
leaq 0x38(%rsp), %rdi
callq 0xa38c
leaq 0xc0(%rsp), %rdi
callq 0x79d4
movl %ebp, %eax
addq $0x308, %rsp            # imm = 0x308
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x9025
movq %rax, %r14
jmp 0x9035
movq %rax, %r14
jmp 0x903f
jmp 0x900f
jmp 0x9025
movq %rax, %r14
movq 0x68(%rsp), %rdi
cmpq %r15, %rdi
je 0x9056
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x4450
jmp 0x9056
jmp 0x9025
movq %rax, %r14
jmp 0x9049
jmp 0x9016
movq %rax, %r14
leaq 0x50(%rsp), %rdi
callq 0xa4a2
jmp 0x9028
movq %rax, %r14
leaq 0x90(%rsp), %rdi
callq 0x89f0
leaq 0x18(%rsp), %rdi
callq 0x7fb2
leaq 0x38(%rsp), %rdi
callq 0xa38c
leaq 0xc0(%rsp), %rdi
callq 0x79d4
movq %r14, %rdi
callq 0x47d0