-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileContent.cpp
71 lines (64 loc) · 1.83 KB
/
FileContent.cpp
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
//
// Created by Marian Sobczyk on 01.11.2015.
//
#include <stdio.h>
#include <openssl/aes.h>
#include <string.h>
#include "FileContent.h"
void FileContent::readFromPath(const char *path) {
FILE *input = fopen(path, "rb");
if (input == NULL) {
return;
}
fseek(input, 0L, SEEK_END);
filesize = (unsigned long) ftell(input);
if (encrypted) {
filesize -= AES_BLOCK_SIZE;
initVector = new unsigned char[AES_BLOCK_SIZE];
}
fseek(input, 0L, SEEK_SET);
content = new unsigned char[filesize];
unsigned char character;
if (encrypted) {
for (int i = 0; i < AES_BLOCK_SIZE; i++) {
fread(&character, 1, 1, input);
initVector[i] = character;
}
}
for (int i = 0; i < filesize; i++) {
fread(&character, 1, 1, input);
content[i] = character;
}
fclose(input);
}
void FileContent::saveInPath(const char *path) {
FILE *output = fopen(path, "wb");
if (output == NULL) {
return;
}
if (encrypted) {
for (int i = 0; i < AES_BLOCK_SIZE; i++) {
fwrite(&initVector[i], 1, 1, output);
}
}
for (int i = 0; i < filesize; i++) {
fwrite(&content[i], 1, 1, output);
}
fclose(output);
}
FileContent::FileContent(long size, bool encrypted) {
filesize = (unsigned long) size;
content = new unsigned char[size];
this->encrypted = encrypted;
initVector = new unsigned char[AES_BLOCK_SIZE];
}
FileContent::FileContent(bool encrypted) {
this->encrypted = encrypted;
initVector = new unsigned char[AES_BLOCK_SIZE];
}
FileContent::FileContent(unsigned char *content, unsigned long length, bool encrypted) {
this->content = new unsigned char[length];
memcpy(this->content, content, (size_t) length);
this->filesize = length;
this->encrypted = encrypted;
}