-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfifo.cpp
298 lines (231 loc) · 5.75 KB
/
fifo.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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/* SOTPET - Simple One-Trick Pony Encryption Tool */
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include "fifo.hpp"
/*
class BufSet
{
public:
uint8_t *buf;
int32_t buflen;
int32_t reqdpos;
*/
BufSet::BufSet(const uint8_t *magic, int32_t magiclen, int32_t pos)
{
this->buf = new uint8_t[magiclen];
memcpy(this->buf, magic, magiclen);
this->buflen = magiclen;
this->reqdpos = pos;
}
BufSet::~BufSet()
{
//delete this->buf;
}
/*
class FIFO
{
protected:
uint8_t *buf;
int32_t buflen;
int32_t rptr;
int32_t wptr;
*/
FIFO::FIFO(uint32_t len)
{
this->buf = new uint8_t[len];
this->buflen = len;
this->rptr = 0;
this->wptr = 0;
}
void FIFO::reset()
{
this->rptr = 0;
this->wptr = 0;
}
FIFO::~FIFO()
{
delete this->buf;
}
int32_t FIFO::push(const uint8_t *inbuf, int32_t inbuflen)
{
int32_t n=0;
while(inbuflen--) { this->push(*inbuf++); n++; }
return n;
}
void FIFO::push(uint8_t c)
{
int32_t oldwptr;
this->buf[this->wptr] = c;
oldwptr = this->wptr;
this->wptr = (this->wptr+1) % this->buflen;
/* if full, just overwrite */
/* if(oldwptr==rptr) do nothing */
if((oldwptr==this->buflen-1 && this->rptr==0) || (this->rptr==oldwptr+1))
this->rptr = (this->wptr+1) % this->buflen;
/* wptr pulls rptr behind */
}
int32_t FIFO::pop(uint8_t *outbuf, int32_t outbuflen)
{
int16_t c;
int32_t n=0;
while(outbuflen-- > 0)
{
c = this->pop();
if(c<0)
break;
*outbuf++=c;
n++;
}
return n;
}
int32_t FIFO::mcpy(uint8_t *outbuf, int32_t outbuflen, int32_t off)
{
int32_t n=0;
off = (off + this->rptr) % this->buflen;
while(outbuflen-- > 0)
{
*outbuf++ = this->buf[off];
n++;
}
return n;
}
int16_t FIFO::pop()
{
int16_t r;
if(this->wptr==this->rptr)
return -1;
r = this->buf[this->rptr];
this->rptr = (this->rptr+1) % this->buflen;
return r;
}
void FIFO::pop(int32_t poplen)
{
//this->rptr = (this->rptr+poplen) % this->buflen;
while(poplen-- > 0 && this->rptr != this->wptr)
this->rptr = (this->rptr+1) % this->buflen;
}
int32_t FIFO::getvlen()
{
if(this->wptr >= this->rptr)
return this->wptr - this->rptr;
return this->wptr - this->rptr + this->buflen;
}
uint8_t FIFO::getvbyte(int32_t p)
{
p = (p + this->rptr) % this->buflen;
return this->buf[p];
}
void FIFO::putvbyte(uint8_t c, int32_t p)
{
p = (p + this->rptr) % this->buflen;
this->buf[p] = c;
}
bool FIFO::checkmagic(const uint8_t *needle, int32_t needlelen, int32_t off /* =0 */)
{
int32_t rp = (this->rptr + off) % this->buflen;
while(needlelen>0 && *needle == this->buf[rp])
{
needlelen--;
needle++;
rp = (rp + 1) % this->buflen;
}
return !needlelen;
}
/*
int32_t findmagicbackwards(const uint8_t *needle, uint32_t needlelen, const uint8_t *haystack, uint32_t haystacklen)
{
int32_t i,j;
for(i=haystacklen-needlelen; i>=0; i--)
{
for(j=0; j<(int32_t)needlelen; j++)
{
if(needle[j]!=haystack[i+j])
break;
}
if(j>=(int32_t)needlelen)
return i;
}
return -1;
}
*/
int32_t FIFO::findmagicbackwards(const uint8_t *needle, int32_t needlelen, int32_t haystacklen /* =0 */)
{
int32_t i,j;
if(haystacklen==0)
haystacklen = this->getvlen();
else
if(haystacklen<0)
haystacklen = this->getvlen()+haystacklen+1; /* haystacklen is negative, so blen-haystacklen+1 */
for(i=haystacklen-needlelen; i>=0; i--)
{
for(j=0; j<(int32_t)needlelen; j++)
{
if(needle[j]!=this->getvbyte(i+j))
break;
}
if(j>=(int32_t)needlelen)
return (i>=this->rptr) ? (i-this->rptr) : (i-this->rptr+this->buflen);
}
return -1;
}
int32_t FIFO::getp(int32_t rel /* =-1 */)
{
if(rel==-1)
return this->rptr;
rel = this->rptr - rel;
if(rel<0)
rel += this->buflen;
return rel;
}
void FIFO::registermagic_reset()
{
this->mg.clear();
}
void FIFO::registermagic_add(const uint8_t *needle, int32_t needlelen, int32_t reqdpos)
{
this->mg.push_back(BufSet(needle, needlelen, reqdpos));
}
void FIFO::registermagic_setsize(uint32_t sz)
{
this->wslen = sz;
}
bool FIFO::registermagic_detect()
{
if(this->mg.empty())
return false;
for (std::vector<BufSet>::iterator i = this->mg.begin(); i != this->mg.end(); ++i)
{
for(int n=0; n<i->buflen; n++)
if(this->getvbyte(i->reqdpos + n) != i->buf[n])
return false;
}
return true;
}
void FIFO::registermagic_wsget(uint8_t *ws, int32_t wslen /* =0 */)
{
if(wslen==0)
wslen = this->wslen;
for(int n=0; n<wslen; n++)
ws[n] = this->getvbyte(n);
}
#include <cstdio>
void FIFO::dump(int32_t mx, int32_t my /* =INT32_MAX */)
{
fprintf(stderr, "rptr=%d wptr=%d buflen=%d\n", this->rptr, this->wptr, this->buflen);
for(int y=0; y<my; y++)
{
int dy = y*mx;
fprintf(stderr, "%04x ", dy);
for(int x=0; x<mx; x++)
{
int dx = dy+x;
if(dx>this->getvlen())
goto b2;
fprintf(stderr, "%02x ", this->getvbyte(dy+x));
}
fputc('\n', stderr);
}
b2:
fputc('\n', stderr);
}