// File: /workspace/github_cpp_makefile_star2_O2/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
cmpq $0x0, 0x8(%rdx)
je 0x8279
movl %r8d, %ebp
movq %rcx, %r14
movq %rdx, %r12
movq %rsi, %rbx
movq %rdi, %r15
leaq 0x90(%rsp), %r13
movq %r13, %rdi
movq %rdx, %rsi
callq 0x4640
movq %r13, %rdi
callq 0x7f2c
movl %eax, %r13d
leaq 0x90(%rsp), %rdi
callq 0x41b0
testb %r13b, %r13b
je 0x827d
movq %r15, %rdi
movq %r12, %rsi
callq 0x4890
leaq 0xb0(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
callq 0x72d0
movq %r12, %rdi
callq 0x6fee
movq %rax, %r15
movq 0x6fd1(%rip), %rdi      # 0xefc8
leaq 0x26c2(%rip), %rsi      # 0xa6c0
callq 0x4400
movq %rax, %rdi
movq %r15, %rsi
callq 0x42d0
movq %rax, %rdi
callq 0x41d0
movq %r15, %rax
shrq $0x20, %rax
je 0x803d
movq 0x6fa2(%rip), %rdi      # 0xefc8
leaq 0x269f(%rip), %rsi      # 0xa6cc
callq 0x4400
xorl %r15d, %r15d
movq %rax, %rdi
callq 0x41d0
movq %rbx, %rdi
movl %r15d, %esi
callq 0x68b3
testb %al, %al
je 0x828b
testl %r15d, %r15d
je 0x828f
movl %ebp, 0x18(%rsp)
pushq $0x1
popq %rdi
leaq 0x58(%rsp), %r12
movl $0x5413, %esi           # imm = 0x5413
movq %r12, %rdx
xorl %eax, %eax
callq 0x4660
movzwl 0x2(%r12), %ebp
leaq 0x20(%rsp), %rdi
callq 0x93ac
leaq 0x10(%rsp), %rdi
callq 0x74b8
leaq 0x60(%rsp), %rdi
movq %r14, %rsi
callq 0x7cd0
cvtsi2ss %ebp, %xmm0
movss 0x25e5(%rip), %xmm1     # 0xa68c
movl %r15d, %eax
cvtsi2sd %rax, %xmm2
movsd %xmm2, 0x38(%rsp)
divss %xmm0, %xmm1
movss %xmm1, 0x1c(%rsp)
xorps %xmm0, %xmm0
movss %xmm0, 0x8(%rsp)
xorl %r15d, %r15d
movq 0x6ef6(%rip), %r12      # 0xefc8
leaq 0x40(%rsp), %r13
leaq 0x261d(%rip), %r14      # 0xa6fb
leaq 0xb0(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x7402
cmpl $0x3, %eax
je 0x8102
cmpl $0x2, %eax
je 0x82fa
xorl %ebp, %ebp
jmp 0x811c
movq %r12, %rdi
leaq 0x25e3(%rip), %rsi      # 0xa6ef
callq 0x4400
movb $0x1, %bpl
movq %rax, %rdi
callq 0x41d0
movq %r13, %rdi
callq 0x9450
leaq 0x10(%rsp), %rdi
movq %r13, %rsi
leaq 0x20(%rsp), %rdx
callq 0x75b6
testb %al, %al
je 0x82af
testb %bpl, %bpl
je 0x8158
leaq 0x10(%rsp), %rdi
movq %r13, %rsi
callq 0x7634
testb %al, %al
je 0x82af
leaq 0x60(%rsp), %rdi
movq %r13, %rsi
callq 0x7d98
testb %al, %al
je 0x82af
movq %r13, %rdi
callq 0x94b6
movl 0x50(%rsp), %edx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x6a96
testb %al, %al
je 0x8294
addl 0x30(%rsp), %r15d
andl $0x0, 0x30(%rsp)
cvtsi2sd %r15, %xmm0
divsd 0x38(%rsp), %xmm0
mulsd 0x24e7(%rip), %xmm0     # 0xa690
cmpb $0x0, 0x18(%rsp)
je 0x8213
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 0x81f6
movq %r12, %rdi
movq %r14, %rsi
callq 0x4400
movq %rax, %rdi
callq 0x42e0
movss 0x8(%rsp), %xmm0
jmp 0x81c0
movss %xmm0, 0x8(%rsp)
movss 0xc(%rsp), %xmm0
ucomiss 0x2483(%rip), %xmm0     # 0xa68c
jb 0x8213
movq %r12, %rdi
callq 0x41d0
movq %r13, %rdi
callq 0x945e
testb %bpl, %bpl
je 0x80de
leaq 0x60(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x7de8
movq %rax, %r14
testq %rax, %rax
je 0x8317
movl 0x40(%rsp), %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x6a96
movl %eax, %ebp
testb %al, %al
jne 0x826f
movq 0x6d6d(%rip), %rdi      # 0xefc8
leaq 0x2491(%rip), %rsi      # 0xa6f3
callq 0x4400
movq %rax, %rdi
callq 0x41d0
movq %r14, %rdi
callq 0x45a0
jmp 0x82bb
xorl %ebp, %ebp
jmp 0x82e6
xorl %ebp, %ebp
movq %rbx, %rdi
xorl %esi, %esi
callq 0x68b3
jmp 0x82e6
xorl %ebp, %ebp
jmp 0x82d9
movb $0x1, %bpl
jmp 0x82d9
movq 0x6d2d(%rip), %rdi      # 0xefc8
leaq 0x2451(%rip), %rsi      # 0xa6f3
callq 0x4400
movq %rax, %rdi
callq 0x41d0
leaq 0x40(%rsp), %rdi
callq 0x945e
xorl %ebp, %ebp
leaq 0x60(%rsp), %rdi
callq 0x7ef6
leaq 0x10(%rsp), %rdi
callq 0x7542
leaq 0x20(%rsp), %rdi
callq 0x93ba
leaq 0xb0(%rsp), %rdi
callq 0x7076
movl %ebp, %eax
addq $0x2f8, %rsp            # imm = 0x2F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x6cc7(%rip), %rdi      # 0xefc8
leaq 0x23db(%rip), %rsi      # 0xa6e3
callq 0x4400
movq %rax, %rdi
callq 0x41d0
jmp 0x82b9
movq 0x6caa(%rip), %rdi      # 0xefc8
leaq 0x23da(%rip), %rsi      # 0xa6ff
callq 0x4400
movq %rax, %rdi
callq 0x41d0
jmp 0x82b9
jmp 0x835a
jmp 0x8366
jmp 0x835a
movq %rax, %rbx
jmp 0x837d
movq %rax, %rbx
jmp 0x8387
jmp 0x835f
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0x41b0
jmp 0x839e
jmp 0x835a
movq %rax, %rbx
jmp 0x8373
movq %rax, %rbx
jmp 0x8391
jmp 0x8366
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x945e
leaq 0x60(%rsp), %rdi
callq 0x7ef6
leaq 0x10(%rsp), %rdi
callq 0x7542
leaq 0x20(%rsp), %rdi
callq 0x93ba
leaq 0xb0(%rsp), %rdi
callq 0x7076
movq %rbx, %rdi
callq 0x47a0