-
Notifications
You must be signed in to change notification settings - Fork 12
/
Blob.h
198 lines (155 loc) · 3.04 KB
/
Blob.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
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
#pragma once
//-----------------------------------------------------------------------------
//
template < int _bits >
class Blob
{
private:
uint8_t bytes[(_bits+7)/8];
public:
Blob()
{
for(size_t i = 0; i < sizeof(bytes); i++)
{
bytes[i] = 0;
}
}
Blob ( int x )
{
for(size_t i = 0; i < sizeof(bytes); i++)
{
bytes[i] = 0;
}
*(int*)bytes = x;
}
Blob ( const Blob & k )
{
for(size_t i = 0; i < sizeof(bytes); i++)
{
bytes[i] = k.bytes[i];
}
}
Blob & operator = ( const Blob & k )
{
for(size_t i = 0; i < sizeof(bytes); i++)
{
bytes[i] = k.bytes[i];
}
return *this;
}
Blob ( uint64_t a, uint64_t b )
{
uint64_t t[2] = {a,b};
set(&t,16);
}
void set ( const void * blob, size_t len )
{
const uint8_t * k = (const uint8_t*)blob;
len = len > sizeof(bytes) ? sizeof(bytes) : len;
for(size_t i = 0; i < len; i++)
{
bytes[i] = k[i];
}
for(size_t i = len; i < sizeof(bytes); i++)
{
bytes[i] = 0;
}
}
uint8_t & operator [] ( int i )
{
return bytes[i];
}
const uint8_t & operator [] ( int i ) const
{
return bytes[i];
}
//----------
// boolean operations
bool operator < ( const Blob & k ) const
{
for(size_t i = 0; i < sizeof(bytes); i++)
{
if(bytes[i] < k.bytes[i]) return true;
if(bytes[i] > k.bytes[i]) return false;
}
return false;
}
bool operator == ( const Blob & k ) const
{
for(size_t i = 0; i < sizeof(bytes); i++)
{
if(bytes[i] != k.bytes[i]) return false;
}
return true;
}
bool operator != ( const Blob & k ) const
{
return !(*this == k);
}
//----------
// bitwise operations
Blob operator ^ ( const Blob & k ) const
{
Blob t;
for(size_t i = 0; i < sizeof(bytes); i++)
{
t.bytes[i] = bytes[i] ^ k.bytes[i];
}
return t;
}
Blob & operator ^= ( const Blob & k )
{
for(size_t i = 0; i < sizeof(bytes); i++)
{
bytes[i] ^= k.bytes[i];
}
return *this;
}
int operator & ( int x )
{
return (*(int*)bytes) & x;
}
Blob & operator &= ( const Blob & k )
{
for(size_t i = 0; i < sizeof(bytes); i++)
{
bytes[i] &= k.bytes[i];
}
}
Blob operator << ( int c )
{
Blob t = *this;
lshift(&t.bytes[0],sizeof(bytes),c);
return t;
}
Blob operator >> ( int c )
{
Blob t = *this;
rshift(&t.bytes[0],sizeof(bytes),c);
return t;
}
Blob & operator <<= ( int c )
{
lshift(&bytes[0],sizeof(bytes),c);
return *this;
}
Blob & operator >>= ( int c )
{
rshift(&bytes[0],sizeof(bytes),c);
return *this;
}
void print_as_hex () {
for (int i=0;i<sizeof(bytes);i++)
printf("%02x",bytes[i]);
printf("\n"); // nl ok
}
int bits () {
return _bits;
}
//----------
};
typedef Blob<128> uint128_t;
typedef Blob<96> uint96_t;
typedef Blob<256> uint256_t;
//-----------------------------------------------------------------------------
/* vim: set sts=2 sw=2 et: */