// File: 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
movq %rsp, %rbp
subq $0x410, %rsp            # imm = 0x410
movb %r8b, %al
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
andb $0x1, %al
movb %al, -0x29(%rbp)
movq -0x20(%rbp), %rdi
callq 0x4350
cmpq $0x0, %rax
jne 0xb49b
movb $0x0, -0x1(%rbp)
jmp 0xbc30
movq -0x20(%rbp), %rsi
leaq -0x58(%rbp), %rdi
movq %rdi, -0x360(%rbp)
callq 0x41f0
movq -0x360(%rbp), %rdi
callq 0xb3e0
movb %al, -0x355(%rbp)
jmp 0xb4c3
leaq -0x58(%rbp), %rdi
callq 0x42b0
movb -0x355(%rbp), %al
andb $0x1, %al
movb %al, -0x31(%rbp)
testb $0x1, -0x31(%rbp)
jne 0xb513
movl $0x0, -0x30(%rbp)
movq -0x18(%rbp), %rdi
movl -0x30(%rbp), %esi
callq 0x8480
movb $0x0, -0x1(%rbp)
jmp 0xbc30
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x58(%rbp), %rdi
callq 0x42b0
jmp 0xbc3e
movq -0x20(%rbp), %rsi
movq -0x10(%rbp), %rdi
callq 0x46c0
movq -0x10(%rbp), %rsi
leaq -0x2b0(%rbp), %rdi
movq %rdi, -0x370(%rbp)
callq 0x9ef0
movq -0x370(%rbp), %rdi
callq 0x9940
movq %rax, -0x368(%rbp)
jmp 0xb54c
movq -0x368(%rbp), %rax
movq %rax, -0x2b8(%rbp)
movq 0x7a57(%rip), %rdi      # 0x12fb8
leaq 0x3148(%rip), %rsi      # 0xe6b0
callq 0x44a0
movq %rax, -0x378(%rbp)
jmp 0xb576
movq -0x378(%rbp), %rdi
movq -0x2b8(%rbp), %rsi
callq 0x4110
movq %rax, -0x380(%rbp)
jmp 0xb592
movq -0x380(%rbp), %rdi
movq 0x7a08(%rip), %rsi      # 0x12fa8
callq 0x4500
jmp 0xb5a7
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq %rax, -0x2b8(%rbp)
jbe 0xb600
movq 0x79fc(%rip), %rdi      # 0x12fb8
leaq 0x30f9(%rip), %rsi      # 0xe6bc
callq 0x44a0
movq %rax, -0x388(%rbp)
jmp 0xb5d1
movq -0x388(%rbp), %rdi
movq 0x79c9(%rip), %rsi      # 0x12fa8
callq 0x4500
jmp 0xb5e6
movl $0x0, -0x30(%rbp)
jmp 0xb60a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0xbc22
movq -0x2b8(%rbp), %rax
movl %eax, -0x30(%rbp)
movq -0x18(%rbp), %rdi
movl -0x30(%rbp), %esi
callq 0x8480
movb %al, -0x389(%rbp)
jmp 0xb61e
movb -0x389(%rbp), %al
testb $0x1, %al
jne 0xb63d
jmp 0xb62a
movb $0x0, -0x1(%rbp)
movl $0x1, -0x2bc(%rbp)
jmp 0xbc14
cmpl $0x0, -0x30(%rbp)
jne 0xb656
movb $0x1, -0x1(%rbp)
movl $0x1, -0x2bc(%rbp)
jmp 0xbc14
xorl %eax, %eax
movl $0x1, %edi
movl $0x5413, %esi           # imm = 0x5413
leaq -0x2c4(%rbp), %rdx
callq 0x4790
movl $0x0, -0x2c8(%rbp)
movl $0x0, -0x2cc(%rbp)
movzwl -0x2c2(%rbp), %eax
cvtsi2ss %eax, %xmm1
movss 0x2fe3(%rip), %xmm0     # 0xe678
divss %xmm1, %xmm0
movss %xmm0, -0x2d0(%rbp)
movl $0x0, -0x2d4(%rbp)
movl -0x30(%rbp), %eax
movl %eax, -0x2d8(%rbp)
leaq -0x2f0(%rbp), %rdi
callq 0xd6b0
jmp 0xb6c2
movb $0x0, -0x2f5(%rbp)
leaq -0x300(%rbp), %rdi
callq 0xa300
jmp 0xb6d7
movq -0x28(%rbp), %rsi
leaq -0x338(%rbp), %rdi
callq 0xaff0
jmp 0xb6e9
jmp 0xb6eb
jmp 0xb6ed
leaq -0x2b0(%rbp), %rdi
leaq -0x2f0(%rbp), %rsi
callq 0xa140
movl %eax, -0x390(%rbp)
jmp 0xb708
movl -0x390(%rbp), %eax
movl %eax, -0x2f4(%rbp)
cmpl $0x2, -0x2f4(%rbp)
jne 0xb794
movq 0x7894(%rip), %rdi      # 0x12fb8
leaq 0x2fa8(%rip), %rsi      # 0xe6d3
callq 0x44a0
movq %rax, -0x398(%rbp)
jmp 0xb739
movq -0x398(%rbp), %rdi
movq 0x7861(%rip), %rsi      # 0x12fa8
callq 0x4500
jmp 0xb74e
movb $0x0, -0x1(%rbp)
movl $0x1, -0x2bc(%rbp)
jmp 0xbbc8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0xbc06
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0xbbfa
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0xbbee
cmpl $0x3, -0x2f4(%rbp)
jne 0xb7d5
movq 0x7814(%rip), %rdi      # 0x12fb8
leaq 0x2f34(%rip), %rsi      # 0xe6df
callq 0x44a0
movq %rax, -0x3a0(%rbp)
jmp 0xb7b9
movq -0x3a0(%rbp), %rdi
movq 0x77e1(%rip), %rsi      # 0x12fa8
callq 0x4500
jmp 0xb7ce
movb $0x1, -0x2f5(%rbp)
jmp 0xb7d7
leaq -0x350(%rbp), %rdi
callq 0xd860
jmp 0xb7e5
leaq -0x300(%rbp), %rdi
leaq -0x350(%rbp), %rsi
leaq -0x2f0(%rbp), %rdx
callq 0xa490
movb %al, -0x3a1(%rbp)
jmp 0xb807
movb -0x3a1(%rbp), %al
testb $0x1, %al
jne 0xb843
jmp 0xb813
movb $0x0, -0x1(%rbp)
movl $0x1, -0x2bc(%rbp)
jmp 0xba64
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x350(%rbp), %rdi
callq 0xd880
jmp 0xbbee
testb $0x1, -0x2f5(%rbp)
je 0xb888
leaq -0x300(%rbp), %rdi
leaq -0x350(%rbp), %rsi
callq 0xa550
movb %al, -0x3a2(%rbp)
jmp 0xb867
movb -0x3a2(%rbp), %al
testb $0x1, %al
jne 0xb886
jmp 0xb873
movb $0x0, -0x1(%rbp)
movl $0x1, -0x2bc(%rbp)
jmp 0xba64
jmp 0xb888
leaq -0x338(%rbp), %rdi
leaq -0x350(%rbp), %rsi
callq 0xb170
movb %al, -0x3a3(%rbp)
jmp 0xb8a3
movb -0x3a3(%rbp), %al
testb $0x1, %al
jne 0xb8c2
jmp 0xb8af
movb $0x0, -0x1(%rbp)
movl $0x1, -0x2bc(%rbp)
jmp 0xba64
movq -0x18(%rbp), %rax
movq %rax, -0x3b8(%rbp)
leaq -0x350(%rbp), %rdi
callq 0xd970
movq %rax, -0x3b0(%rbp)
jmp 0xb8e2
movq -0x3b0(%rbp), %rsi
movq -0x3b8(%rbp), %rdi
movl -0x340(%rbp), %edx
callq 0x87a0
movb %al, -0x3b9(%rbp)
jmp 0xb903
movb -0x3b9(%rbp), %al
testb $0x1, %al
jne 0xb953
jmp 0xb90f
movq 0x76a2(%rip), %rdi      # 0x12fb8
leaq 0x2dc6(%rip), %rsi      # 0xe6e3
callq 0x44a0
movq %rax, -0x3c8(%rbp)
jmp 0xb92b
movq -0x3c8(%rbp), %rdi
movq 0x766f(%rip), %rsi      # 0x12fa8
callq 0x4500
jmp 0xb940
movb $0x0, -0x1(%rbp)
movl $0x1, -0x2bc(%rbp)
jmp 0xba64
movl -0x2e0(%rbp), %ecx
movl -0x2cc(%rbp), %eax
addl %ecx, %eax
movl %eax, -0x2cc(%rbp)
movl $0x0, -0x2e0(%rbp)
movl -0x2cc(%rbp), %eax
cvtsi2sd %rax, %xmm0
movl -0x2d8(%rbp), %eax
cvtsi2sd %rax, %xmm1
divsd %xmm1, %xmm0
movsd 0x2ced(%rip), %xmm1     # 0xe680
mulsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x2c8(%rbp)
testb $0x1, -0x29(%rbp)
je 0xba45
jmp 0xb9af
movss -0x2d4(%rbp), %xmm1
addss -0x2d0(%rbp), %xmm1
movss -0x2c8(%rbp), %xmm0
ucomiss %xmm1, %xmm0
jbe 0xba17
movq 0x75e5(%rip), %rdi      # 0x12fb8
leaq 0x2d11(%rip), %rsi      # 0xe6eb
callq 0x44a0
movq %rax, -0x3d0(%rbp)
jmp 0xb9e8
movq -0x3d0(%rbp), %rdi
movq 0x75ca(%rip), %rsi      # 0x12fc0
callq 0x4500
jmp 0xb9fd
movss -0x2d0(%rbp), %xmm0
addss -0x2d4(%rbp), %xmm0
movss %xmm0, -0x2d4(%rbp)
jmp 0xb9af
movss -0x2c8(%rbp), %xmm0
movss 0x2c51(%rip), %xmm1     # 0xe678
ucomiss %xmm1, %xmm0
jb 0xba43
movq 0x7585(%rip), %rdi      # 0x12fb8
movq 0x756e(%rip), %rsi      # 0x12fa8
callq 0x4500
jmp 0xba41
jmp 0xba43
jmp 0xba45
testb $0x1, -0x2f5(%rbp)
je 0xba5a
movl $0x3, -0x2bc(%rbp)
jmp 0xba64
movl $0x0, -0x2bc(%rbp)
leaq -0x350(%rbp), %rdi
callq 0xd880
movl -0x2bc(%rbp), %eax
movl %eax, -0x3d4(%rbp)
testl %eax, %eax
je 0xba92
jmp 0xba82
movl -0x3d4(%rbp), %eax
subl $0x3, %eax
je 0xba97
jmp 0xbbc8
jmp 0xb6eb
leaq -0x338(%rbp), %rdi
leaq -0x354(%rbp), %rsi
callq 0xb1f0
movq %rax, -0x3e0(%rbp)
jmp 0xbab3
movq -0x3e0(%rbp), %rax
movq %rax, -0x308(%rbp)
cmpq $0x0, -0x308(%rbp)
jne 0xbb0f
movq 0x74e6(%rip), %rdi      # 0x12fb8
leaq 0x2c16(%rip), %rsi      # 0xe6ef
callq 0x44a0
movq %rax, -0x3e8(%rbp)
jmp 0xbae7
movq -0x3e8(%rbp), %rdi
movq 0x74b3(%rip), %rsi      # 0x12fa8
callq 0x4500
jmp 0xbafc
movb $0x0, -0x1(%rbp)
movl $0x1, -0x2bc(%rbp)
jmp 0xbbc8
movq -0x18(%rbp), %rdi
movq -0x308(%rbp), %rsi
movl -0x354(%rbp), %edx
callq 0x87a0
movb %al, -0x3e9(%rbp)
jmp 0xbb2d
movb -0x3e9(%rbp), %al
testb $0x1, %al
jne 0xbb9a
jmp 0xbb39
movq 0x7478(%rip), %rdi      # 0x12fb8
leaq 0x2b9c(%rip), %rsi      # 0xe6e3
callq 0x44a0
movq %rax, -0x3f8(%rbp)
jmp 0xbb55
movq -0x3f8(%rbp), %rdi
movq 0x7445(%rip), %rsi      # 0x12fa8
callq 0x4500
jmp 0xbb6a
movq -0x308(%rbp), %rax
movq %rax, -0x400(%rbp)
cmpq $0x0, %rax
je 0xbb8a
movq -0x400(%rbp), %rdi
callq 0x46d0
movb $0x0, -0x1(%rbp)
movl $0x1, -0x2bc(%rbp)
jmp 0xbbc8
movq -0x308(%rbp), %rax
movq %rax, -0x408(%rbp)
cmpq $0x0, %rax
je 0xbbba
movq -0x408(%rbp), %rdi
callq 0x46d0
movb $0x1, -0x1(%rbp)
movl $0x1, -0x2bc(%rbp)
leaq -0x338(%rbp), %rdi
callq 0xb370
leaq -0x300(%rbp), %rdi
callq 0xa3d0
leaq -0x2f0(%rbp), %rdi
callq 0xd6d0
jmp 0xbc14
leaq -0x338(%rbp), %rdi
callq 0xb370
leaq -0x300(%rbp), %rdi
callq 0xa3d0
leaq -0x2f0(%rbp), %rdi
callq 0xd6d0
jmp 0xbc22
leaq -0x2b0(%rbp), %rdi
callq 0xc460
jmp 0xbc30
leaq -0x2b0(%rbp), %rdi
callq 0xc460
jmp 0xbc3e
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x410, %rsp            # imm = 0x410
popq %rbp
retq
movq -0x60(%rbp), %rdi
callq 0x4900
nopw (%rax,%rax)