-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbytestream.cpp
More file actions
205 lines (183 loc) · 3.95 KB
/
bytestream.cpp
File metadata and controls
205 lines (183 loc) · 3.95 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include "headers.hpp"
using namespace std;
ByteStream::ByteStream()
{
this->values = {};
this->num_bytes = 0;
}
ByteStream::ByteStream(vector<Byte> bs)
{
this->values = bs;
this->num_bytes = bs.size();
}
ByteStream::ByteStream(unsigned int bs)
{
vector<Byte>bytes;
for(int i = 8; i<=32; i+=8)
{
unsigned int mask = (1LL<<i)-1;
unsigned char x = (bs & mask)>>(i-8);
bytes.push_back(Byte(x));
}
reverse(bytes.begin(), bytes.end());
this->values = bytes;
this->num_bytes = bytes.size();
}
ByteStream::ByteStream(string bs)
{
vector<Byte>bytes;
for(int i = 0; i<bs.length(); i+=2)
bytes.push_back(Byte(bs.substr(i,2)));
this->values = bytes;
this->num_bytes = bytes.size();
}
unsigned int ByteStream::getValue()
{
unsigned int value = 0;
for(int n = 0; n<this->num_bytes; n++)
{
unsigned int mask = (1<<((this->num_bytes-n-1)*8));
value += (mask * this->values[n].getValue());
}
return value;
}
string ByteStream::getString()
{
string ans = "";
for(Byte byte: this->values)
ans = ans + byte.getString();
return ans;
}
ByteStream ByteStream::byteStreamShift(int shift)
{
ByteStream ans = *this;
if(shift == 0)
return ans;
bool ret_value;
if(shift<0)
{
shift *= -1;
while(shift--)
{
ret_value = 0;
for(int n = 0; n<ans.num_bytes; n++)
{
bool LSB = ans.values[n].getBit(0);
ans.values[n] = ans.values[n].byteShift(-1);
ans.values[n] = ans.values[n]|(Byte(ret_value<<7));
ret_value = LSB;
}
}
}
else
{
while(shift--)
{
ret_value = 0;
for(int n = ans.num_bytes-1; n>=0; n--)
{
bool MSB = ans.values[n].getBit(7);
ans.values[n] = ans.values[n].byteShift(1);
ans.values[n] = ans.values[n]|Byte(ret_value);
ret_value = MSB;
}
}
}
return ans;
}
ByteStream ByteStream::byteStreamROR(int shift)
{
ByteStream ans = *this;
while(shift--)
{
bool LSB = ans.getByte(ans.num_bytes-1).getBit(0);
ans = ans.byteStreamShift(-1);
string temp = LSB?"80":"00";
for(int i = 0; i<num_bytes-1; i++)
temp += "00";
ans = ans | ByteStream(temp);
}
return ans;
}
ByteStream ByteStream::byteStreamROL(int shift)
{
ByteStream ans = *this;
while(shift--)
{
bool MSB = ans.getByte(0).getBit(7);
ans = ans.byteStreamShift(1);
string temp = MSB?"01":"00";
for(int i = 0; i<num_bytes-1; i++)
temp = "00" + temp;
ans = ans | ByteStream(temp);
}
return ans;
}
ByteStream ByteStream::operator|(const ByteStream &bs)
{
vector<Byte> ans_bytes;
for(int n = 0; n<this->num_bytes; n++)
ans_bytes.push_back(this->values[n]|bs.values[n]);
return ByteStream(ans_bytes);
}
ByteStream ByteStream::operator&(const ByteStream &bs)
{
vector<Byte> ans_bytes;
for(int n = 0; n<this->num_bytes; n++)
ans_bytes.push_back(this->values[n]&bs.values[n]);
return ByteStream(ans_bytes);
}
ByteStream ByteStream::operator^(const ByteStream &bs)
{
vector<Byte> ans_bytes;
for(int n = 0; n<this->num_bytes; n++)
ans_bytes.push_back(this->values[n]^bs.values[n]);
return ByteStream(ans_bytes);
}
ByteStream& ByteStream::operator=(const ByteStream &bs)
{
this->values = bs.values;
this->num_bytes = bs.num_bytes;
return *this;
}
unsigned int ByteStream::getNumBytes()
{
return this->num_bytes;
}
vector<Byte> ByteStream::getValues()
{
return this->values;
}
ByteStream ByteStream::getBytes(unsigned int start, unsigned int end)
{
vector<Byte> bytes;
for(int i = start; i<end; i++)
bytes.push_back(this->values[i]);
return ByteStream(bytes);
}
Byte ByteStream::getByte(unsigned int index)
{
return this->values[index];
}
void ByteStream::setBytes(unsigned int start, ByteStream bs)
{
for(int i = start; i<start+bs.values.size(); i++)
this->values[i] = bs.values[i-start];
}
bool ByteStream::getBit(unsigned int pos)
{
unsigned int x = pos/8, y = pos%8;
return this->values[num_bytes-x-1].getBit(y);
}
void ByteStream::printBits()
{
for(Byte byte: values)
byte.printBits();
cout<<endl;
}
ostream& operator<<(ostream &out, const ByteStream &bs)
{
for(Byte byte: bs.values)
out<<byte;
return out;
}