-
Notifications
You must be signed in to change notification settings - Fork 0
/
File.cc
137 lines (125 loc) · 2.98 KB
/
File.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include "File.h"
#include "stdio.h"
#include "cerrno"
#include "Exception.h"
#include "iostream"
#include "sys/stat.h"
#include "openssl/conf.h"
#include "openssl/evp.h"
#include "openssl/err.h"
using namespace std;
File::File(const char *path, const char *mode)
{
cout << "Opening file " << path << endl;
pFile = fopen(path, mode);
pFilePath = path;
pMode = mode;
if (!pFile)
{
throw Exception(FILE_OPEN_FAILED);
}
}
int File::read(int size, Buffer &buffer)
{
if (pFile == NULL)
{
throw Exception(INVALID_FILE);
}
int read = 0;
read = fread(buffer.buffer, 1, size, pFile);
// if read bytes is not equal to size, throw exception
if (read != size)
{
if (!feof(pFile))
{
throw Exception(READ_FAILED);
}
}
return read;
}
int File::write(int size, Buffer &buffer)
{
if (pFile == NULL)
{
throw Exception(INVALID_FILE);
}
int write = fwrite(buffer.buffer, 1, size, pFile);
// if write bytes is not equal to size, throw exception
if (write != size)
{
throw Exception(WRITE_FAILED);
}
return write;
}
void File::close()
{
if (pFile)
{
fclose(pFile);
}
return;
}
long File::size()
{
long fileSize = 0;
struct stat fileStats;
int ret = stat(pFilePath, &fileStats);
{
throw Exception(FAILED_TO_GET_FILE_INFORMATION);
}
fileSize = fileStats.st_size;
return fileSize;
}
int File::encryptBlock(void *ctx, int size, Buffer &inBuffer, Buffer &outBuffer, bool isFinal)
{
int len;
int ciphertextLength;
if (1 != EVP_EncryptUpdate((EVP_CIPHER_CTX *)ctx, outBuffer.buffer, &len, inBuffer.buffer, size))
{
throw Exception(ENC_ERROR);
}
ciphertextLength = len;
if (isFinal)
{
if (1 != EVP_EncryptFinal_ex((EVP_CIPHER_CTX *)ctx, outBuffer.buffer + len, &len))
{
throw Exception(ENC_ERROR);
}
ciphertextLength += len;
}
cout << "Encrypted " << ciphertextLength << " bytes" << endl;
BIO_dump_fp(stdout, (const char *)outBuffer.buffer, ciphertextLength);
cout << endl;
return ciphertextLength;
}
int File::decryptBlock(void *ctx, int size, Buffer &inBuffer, Buffer &outBuffer, bool isFinal)
{
int len;
int plaintext_len;
if (1 != EVP_DecryptUpdate((EVP_CIPHER_CTX *)ctx, outBuffer.buffer, &len, inBuffer.buffer, size))
{
throw Exception(DEC_ERROR);
}
plaintext_len = len;
if (isFinal)
{
if (1 != EVP_DecryptFinal_ex((EVP_CIPHER_CTX *)ctx, outBuffer.buffer + len, &len))
{
throw Exception(DEC_ERROR);
}
plaintext_len += len;
}
cout << "Decrypted " << plaintext_len << " bytes" << endl;
BIO_dump_fp(stdout, (const char *)outBuffer.buffer, plaintext_len);
cout << endl;
return plaintext_len;
}
File::~File()
{
if (pFile)
{
cout << "Closing file " << pFilePath << endl;
close();
pFile = NULL;
}
}