-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathencode.h
163 lines (136 loc) · 6 KB
/
encode.h
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#ifndef ENCODE_H_
#define ENCODE_H_
#include "binaryio.h"
#include <filesystem>
#include <iostream>
#include <string>
#include <bitset>
#include <vector>
#include <fstream>
//This performs the actual encoding
void encodeImage(fs::path & inputPath, fs::path & outputPath, std::string encodeMessage)
{
//Create and read files to buffer
std::vector<std::bitset<sizeof(unsigned char)*8>> bytesOfData;
bytesOfData = readBinaryToBitSet(inputPath);
//Creates and reserve 15 bytes to store the header of the file
std::vector<std::bitset<sizeof(unsigned char)*8>> headerData;
headerData.reserve(HEADER_SIZE_IN_BYTES);
//Reads the first 15 bytes of the header into the 'headerData' buffer
for(unsigned i = 0; i < HEADER_SIZE_IN_BYTES; i++)
{
headerData.emplace_back(bytesOfData[i]);
}
//This iterates over every byte in the 'bytesOfData' buffer. For every 4 bytes, we can encode 1 byte of the message.
unsigned iterationString = 0;
unsigned shiftOfByte = 0;
for(auto & b : bytesOfData)
{
//Stores the byte of the encode message into a bitset
std::bitset<sizeof(unsigned char)*8> c(encodeMessage[iterationString]);
//Sets the first bit of the iterate bytesOfData to match the (first bit + shiftOfByte) bit of the message character
if(c.test(shiftOfByte))
b.set(0);
else
b.reset(0);
//Sets the second bit of the iterate bytesOfData to match the (second bit + shiftOfByte) bit of the message character
if(c.test(shiftOfByte+1))
b.set(1);
else
b.reset(1);
//This performs a loop, this number is going to be
/*
Times looping | shiftOfByte value
0 0
1 2
2 4
3 6
4 8
5 0
6 2
7 4
8 6
9 8
10 0
and so on...
*/
shiftOfByte+=2;
if(shiftOfByte == 8)
{
shiftOfByte = 0;
iterationString = (iterationString + 1)%encodeMessage.size();
}
}
//Writes the header data...
writeBinary(outputPath, headerData);
//Writes the encoded message, but first it need to get rid of the first 15 bytes, because they are the header data
//So creates a new vector, now without the 15 bytes headerData
std::vector<std::bitset<sizeof(unsigned char)*8>> bytesOfDataWithoutHeader;
//Reserve the size of bytesOfData - 15 bytes of header data
bytesOfDataWithoutHeader.reserve(bytesOfData.size()-headerData.size());
//Copy the files ***THIS NEEDS TO BE CHANGE, probrably really bad approach***
std::copy(bytesOfData.begin()+HEADER_SIZE_IN_BYTES, bytesOfData.end(), back_inserter(bytesOfDataWithoutHeader));
//Writes the rest of the file
writeBinary(outputPath, bytesOfDataWithoutHeader);
return;
}
//THIS IS JUST A TEST, DO NOT USE IT
//This is for encoding files into .ppm images
/*
void encodeFileIntoImage(fs::path & inputImagePath, fs::path & inputFilePath, fs::path & outputImagePath)
{
//If file does not exists, just return; (i need to change these from void to bool, so that we can know if it was successful of fail
if(!fs::exists(inputFilePath))
return;
//Create a buffer and read input image to buffer
std::vector<std::bitset<sizeof(unsigned char)*8>> bytesOfDataImage;
bytesOfDataImage = readBinaryToBitSet(inputPath);
//Creates and reserve 15 bytes to store the header of the input image
std::vector<std::bitset<sizeof(unsigned char)*8>> headerData;
headerData.reserve(HEADER_SIZE_IN_BYTES);
//Reads the first 15 bytes of the header into the 'headerData' buffer
for(unsigned i = 0; i < HEADER_SIZE_IN_BYTES; i++)
{
headerData.emplace_back(bytesOfData[i]);
}
//Create a buffer and read input file to this buffer
std::vector<std::bitset<sizeof(unsigned char)*8>> bytesOfDataFile;
bytesOfDataFile = readBinaryToBitSet(inputPath);
//This iterates over every byte in the 'bytesOfDataImage' buffer. For every 4 bytes, we can encode 1 byte of the input file.
unsigned iterationFile = 0;
unsigned shiftOfByte = 0;
for(auto & b : bytesOfData)
{
//Stores the byte of the encode message into a bitset
std::bitset<sizeof(unsigned char)*8> c(bytesOfDataFile[iterationFile]);
//Sets the first bit of the iterate bytesOfDataImage to match the (first bit + shiftOfByte) bit of the input file character
if(c.test(shiftOfByte))
b.set(0);
else
b.reset(0);
//Sets the second bit of the iterate bytesOfDataImage to match the (second bit + shiftOfByte) bit of the input file character
if(c.test(shiftOfByte+1))
b.set(1);
else
b.reset(1);
shiftOfByte+=2;
if(shiftOfByte == 8)
{
shiftOfByte = 0;
iterationFile = (iterationFile + 1)%bytesOfDataFile.size();
}
}
//Writes the header data...
writeBinary(outputPath, headerData);
//Writes the encoded message, but first it need to get rid of the first 15 bytes, because they are the header data
//So creates a new vector, now without the 15 bytes headerData
std::vector<std::bitset<sizeof(unsigned char)*8>> bytesOfDataWithoutHeader;
//Reserve the size of bytesOfData - 15 bytes of header data
bytesOfDataWithoutHeader.reserve(bytesOfDataImage.size()-headerData.size());
//Copy the files ***THIS NEEDS TO BE CHANGE, probrably really bad approach***
std::copy(bytesOfDataImage.begin()+HEADER_SIZE_IN_BYTES, bytesOfDataImage.end(), back_inserter(bytesOfDataWithoutHeader));
//Writes the rest of the file
writeBinary(outputPath, bytesOfDataWithoutHeader);
return;
}*/
#endif