-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
153 lines (130 loc) · 4.09 KB
/
main.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
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
/*This encodes a binary payload using Run Length Encoding (RLE). Since it is specified
* that the message is binary, RLE is considered, which is particularly helpful when there are a number
* of repetitions of 0's or 1's. In the case of characters, probability based on the number of
* occurrences would have been assigned and a (Prefix)/Huffman code would have been considered.
* Input - messagePayload.txt path
* Output - Original binary message is decoded (can be written to a file if required).
*/
#include "main.h"
#include <fstream>
char* binary_payload_path; // Path to the directory containing the bytes.
// Message header map
std::map<string, string> Message::function()
{
std::map<string, string> header;
return header;
}
/*Method to read a binary payload from a text file. For the sake of convenience
* it is assumed that the message is contained in a text file.*/
vector<int> Message::returnArray(char *binary_payload_path)
{
int read_number;
vector<int> arr;
cout<<binary_payload_path << "\n";
std::ifstream in(binary_payload_path);
while (in >> read_number)
{
arr.push_back(read_number);
}
return arr;
}
vector<int> Codec::encode(unsigned long size, vector<int> &byte_stream)
{
int count =1;
vector<int> rle;
cout<<"\n";
for (int i = 0; i < size; i++)
{
cout<<byte_stream[i];
}
std::cout<<std::endl;
rle.push_back(byte_stream[0]); //The first element of the vector is the first digit of the bit stream
for (int j = 0; j<size ; j++)
{
if(byte_stream[j] == byte_stream[j+1])
{
count +=1;
}
else {
rle.push_back(count); // the count values are added to the vector
count = 1;
}
}
return rle;
}
/* Assign a 0 or 1 to every value alternately since each digit denotes a change in values 'n' times.
* It does not matter what it is initially. Now use the first byte that you stored in encoded_stream[0] to know
* the starting value and change it accordingly.
* */
vector<int> Codec::decode(unsigned long size, vector<int>& encoded_stream)
{
vector<int> original;
int first_digit = encoded_stream[0];
for (int i = 1; i < size; i++)
{
int n = encoded_stream[i];
if (first_digit ==0)
{
if(i%2==0)
{
for (int j=0; j<n; j++)
{
original.push_back(1);
}
}
else {
for (int j=0; j<n; j++)
{
original.push_back(0);
}
}
}
else {
if(i%2==0)
{
for (int j=0; j<n; j++)
{
original.push_back(0);
}
}
else {
for (int j=0; j<n; j++)
{
original.push_back(1);
}
}
}
}
cout<<"\n";
return original;
}
int main(int argc, char **argv)
{
Message m;
Codec c;
binary_payload_path = (char*) malloc(strlen(argv[1]));
strcpy(binary_payload_path, argv[1]);
// Read the binary stream from the text file and store it in a vector
vector<int> byte_stream = m.returnArray(binary_payload_path);
unsigned long byte_stream_size = byte_stream.size();
for (int i = 0; i < byte_stream.size(); i++)
{
cout<<byte_stream[i];
}
// Input the binary stream stored in the vector into the encoder and the encoded
// stream is stored in another vector
vector<int> encoded_stream = c.encode(byte_stream_size,byte_stream);
unsigned long encoded_stream_size = encoded_stream.size();
for (int j = 0; j < encoded_stream.size(); j++)
{
cout<<encoded_stream[j];
}
// Input the encoded stream into the decoder to get the decoded original message
vector<int> decoded_stream = c.decode(encoded_stream_size,encoded_stream);
for (int j = 0; j < decoded_stream.size(); j++)
{
cout<<decoded_stream[j];
}
free(binary_payload_path);
return 0;
}