forked from rokath/tcobs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcobs.c
407 lines (375 loc) · 15.8 KB
/
tcobs.c
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/*! \file tcobs.c
\author Thomas.Hoehenleitner [at] seerose.net
\details See ./TCOBSSpecification.md.
*******************************************************************************/
#include <stdint.h>
#include <stddef.h>
#include "tcobs.h"
//! ASSERT checks for a true condition, otherwise stop. (used for testing)
#define ASSERT( condition ) do{ if( !(condition) ){ for(;;){} } }while(0);
//! OUTB writes a non-sigil byte to output and increments offset.
//! If offset reaches 31, a NOP sigil byte is inserted and offset is then set to 0.
#define OUTB( by ) do{ \
*o++ = by; \
offset++; \
ASSERT( offset <= 31 ); \
if( offset == 31 ){ \
*o++ = N | 31; \
offset = 0; \
} \
} while( 0 );
//! OUT_zeroSigil writes one of the sigil bytes Z1, Z3, Z3
//! according to zeroCount and sets zeroCount=0 and offset=0.
#define OUT_zeroSigil do{ \
ASSERT( b_1 == 0 ); \
ASSERT( (fullCount|reptCount) == 0 ); \
ASSERT( 1 <= zeroCount && zeroCount <= 3 ); \
ASSERT( offset <= 31 ); \
*o++ = (zeroCount << 5) | offset; \
offset = 0; \
zeroCount = 0; \
}while(0);
//! OUT_fullSigil writes one of the sigil bytes F2, F3, F4
//! according to fullCount and sets fullCount=0 and offset=0.
#define OUT_fullSigil do{ \
ASSERT( b_1 == 0xFF ); \
ASSERT( (zeroCount|reptCount) == 0 ); \
ASSERT( 2 <= fullCount && fullCount <= 4 ); \
ASSERT( offset <= 31 ); \
*o++ = 0x80 | (fullCount << 5) | offset; \
offset = 0; \
fullCount = 0; \
}while(0);
//! OUT_reptSigil writes one of the sigil bytes R2, R3, R4
//! according to reptCount and sets reptCount=0 and offset=0.
//! If offset is bigger than 7 a NOP sigil byte is inserted.
#define OUT_reptSigil do{ \
ASSERT( (zeroCount|fullCount) == 0 ); \
ASSERT( 2 <= reptCount && reptCount <= 4 ); \
ASSERT( offset <= 31 ); \
if( offset > 7 ){ \
*o++ = N | offset; \
offset = 0; \
} \
*o++ = ((reptCount-1) << 3) | offset; \
offset = 0; \
reptCount = 0; \
}while(0);
#define N 0xA0 //!< sigil byte 0x101ooooo, offset 0-31
#define Z1 0x20 //!< sigil byte 0x001ooooo, offset 0-31
#define Z2 0x40 //!< sigil byte 0x010ooooo, offset 0-31
#define Z3 0x60 //!< sigil byte 0x011ooooo, offset 0-31
#define F2 0xC0 //!< sigil byte 0x110ooooo, offset 0-31
#define F3 0xE0 //!< sigil byte 0x111ooooo, offset 0-31
#define F4 0x80 //!< sigil byte 0x100ooooo, offset 0-31
#define R2 0x08 //!< sigil byte 0x00001ooo, offset 0-7
#define R3 0x10 //!< sigil byte 0x00010ooo, offset 0-7
#define R4 0x18 //!< sigil byte 0x00011ooo, offset 0-7
size_t TCOBSEncode( void * restrict output, const void * restrict input, size_t length){
uint8_t* o = output; // write pointer
uint8_t* out = output;
uint8_t const * i = input; // read pointer
uint8_t const * limit = input + length; // read limit
uint8_t zeroCount = 0; // counts zero bytes 1-3 for Z1-Z3
uint8_t fullCount = 0; // counts 0xFF bytes 1-4 for FF and F2-F4
uint8_t reptCount = 0; // counts repeat bytes 1-4 for !00 and R2-R4,
uint8_t b_1 = 0; // previous byte
uint8_t b = 0; // current byte
uint8_t offset = 0; // link to next sigil or buffer start looking backwards
// comment syntax:
// Sigil bytes chaining is done with offset and not shown explicitly.
// All left from comma is already written to o and if, only partially shown.
// n is 0...3|4 and m is n+1, representing count number.
// zn, fn, rn right from comma is count in variables, if not shown, then 0.
// At any moment only one of the 3 counters can be different from 0.
// Zn, Fn, Rn, Nn are (written) sigil bytes.
// Between comma and dot are the 2 values b_1 and b.
// 3 dots ... means it is unknown if bytes follow.
// !00 is a byte != 00.
// !FF is a byte != FF.
// aa is not 00 and not FF and all aa in a row are equal.
// xx yy and zz are any bytes.
// Invalid b_1 and b are displayed as --.
if( length >= 2 ){ // , -- --. xx yy ...
b = *i++; // , -- xx, yy ...
for(;;){ // , zn|fn|rn -- xx, yy ...
b_1 = b; // , xx --. yy ...
b = *i++; // , xx yy, ...
if( limit - i > 0 ){ // , xx yy. zz ...
// , z0 00 00. -> , z1 -- 00.
// , z0 00 00. 00 -> , z2 -- 00.
if( (b_1 | b) == 0){ // , zn 00 00. zz ...
zeroCount++; // , zm -- 00. zz ...
if( zeroCount == 2 ){ // , z2 -- 00. zz ...
zeroCount = 3; // , z3 -- --. zz ...
OUT_zeroSigil // Z3, -- --. zz ...
b = *i++; // , -- zz. ...
if( limit - i == 0 ){ // , -- zz.
goto lastByte;
}
// , -- xx. yy ...
}
continue; // , zn -- xx. yy ...
}
// , f0 FF FF. -> , f1 -- FF.
// , f0 FF FF. FF -> , f2 -- FF.
// , f0 FF FF. FF FF -> , f3 -- FF.
if( (b_1 & b) == 0xFF ){ // , fn FF FF. zz ...
fullCount++; // , fm -- FF. zz ...
if( fullCount == 3 ){ // , f3 -- FF. zz ...
fullCount = 4; // , f4 -- --. zz ...
OUT_fullSigil // F4, -- --. zz ...
b = *i++; // , -- zz. ...
if( limit - i == 0 ){ // , -- zz.
goto lastByte;
}
// , -- xx. yy ...
}
continue; // , fn -- xx. yy ...
}
// , r0 aa aa. -> , r1 -- aa.
// , r0 aa aa. aa -> , r2 -- aa.
// , r0 aa aa. aa aa -> , r3 -- aa.
// , r0 aa aa. aa aa aa -> , r4 -- aa.
if( b_1 == b ){ // , rn aa aa. xx ...
ASSERT( b_1 != 0 );
ASSERT( b_1 != 0xFF );
reptCount++; // , rm -- aa. xx ...
if( reptCount == 4 ){ // , r4 -- aa. xx ...
OUTB( b ) // aa, r4 -- --. xx ...
OUT_reptSigil // aa R4, -- --. xx ...
b = *i++; // , -- xx. ...
if( limit - i == 0 ){ // , -- xx.
goto lastByte;
}
// , -- xx. yy ...
}
continue; // , r1|r2|r3 -- aa. yy ...
} // OR // , -- xx. yy ...
// , zn|fn|rn xx yy. zz ... (at this point is b_1 != b)
// handle counts
if( zeroCount ) { // , z1|z2 00 aa. xx ...
ASSERT( 1 <= zeroCount && zeroCount <= 2 )
ASSERT( b_1 == 0 )
ASSERT( b_1 != b )
zeroCount++; // , z2|z3 -- aa. xx ...
OUT_zeroSigil // Z2|Z3, -- aa. xx ...
continue;
}
if( fullCount ) { // , f1|f2|f3 FF !FF. xx ...
ASSERT( 1 <= fullCount && fullCount <= 3 )
ASSERT( b_1 == 0xFF )
ASSERT( b_1 != b )
fullCount++; // , f2|f3|f4 -- !FF. xx ...
OUT_fullSigil // Fn, -- !FF. xx ...
continue;
}
if( reptCount ) { // , r1|r2|r3 aa !aa. xx ...
ASSERT( 1 <= reptCount && reptCount <= 3 )
ASSERT( b_1 != 0 )
ASSERT( b_1 != 0xFF )
ASSERT( b_1 != b )
if( reptCount == 1 ){ // , r1 aa !aa. xx ...
reptCount = 0; // clear
OUTB( b_1 ) // aa, r0 aa !aa. xx ...
OUTB( b_1 ) // aa aa, -- !aa. xx ...
continue;
}
*o++ = b_1; // aa, r2|r3 -- !aa. xx ...
offset++;
OUT_reptSigil // aa R1|R2|R3, -- !aa. xx ...
continue;
}
// at this point all counts are 0, b_1 != b and b_1 = xx, b == yy
ASSERT( zeroCount == 0 )
ASSERT( fullCount == 0 )
ASSERT( reptCount == 0 )
ASSERT( b_1 != b )
// , xx yy. zz ...
if( b_1 == 0 ){ // , 00 !00. xx ...
ASSERT( b != 0 )
zeroCount++; // , z1 -- !00. xx ...
OUT_zeroSigil
continue; // Z1, -- !00. xx ...
}
if( b_1 == 0xFF ){ // , FF !FF. xx ...
ASSERT( b != 0xFF )
OUTB( 0xFF ); // FF, -- !FF. xx ...
continue;
}
// , aa xx. yy ...
ASSERT( 1 <= b_1 && b_1 <= 0xFE )
OUTB( b_1 ) // aa, -- xx. yy ...
continue;
}else{ // last 2 bytes
// , zn|fn|rn xx yy.
if( (zeroCount | fullCount | reptCount) == 0){ // , xx yy.
if( b_1 == 0 && b == 0 ){ // , 00 00.
*o++ = Z2 | offset; // Z2, -- --.
return o - out;
}
if( b_1 == 0xFF && b == 0xFF ){ // , FF FF.
*o++ = F2 | offset; // F2, -- --.
return o - out;
}
if( b_1 == 0 ){ // , 00 xx.
zeroCount = 1; // , z1 -- xx.
OUT_zeroSigil // Z1, -- xx.
goto lastByte;
}
// , aa xx.
ASSERT( b_1 != 0 )
OUTB( b_1 ) // aa, -- xx.
goto lastByte;
}
// At this point exactly one count was incremented.
// If a count is >0 it is necessarily related to b_1.
if( zeroCount == 1 ) { // , z1 00 yy
ASSERT( fullCount == 0 )
ASSERT( reptCount == 0 )
ASSERT( b_1 == 0 )
if( b != 0 ){ // , z1 00 !00.
zeroCount++; // , z2 -- !00.
OUT_zeroSigil // Z2, -- !00.
goto lastByte;
}
if( b == 0 ){ // , z1 00 00.
*o++ = Z3 | offset; // Z3, -- --.
return o - out;
}
ASSERT( 0 )
}
if( zeroCount == 2 ) { // , z2 00 !00.
ASSERT( fullCount == 0 )
ASSERT( reptCount == 0 )
ASSERT( b_1 == 0 )
ASSERT( b != 0 )
zeroCount = 3; // , z3 -- aa.
OUT_zeroSigil // Z3, -- aa.
goto lastByte;
}
if( fullCount == 1 ) { // , f1 FF yy.
ASSERT( zeroCount == 0 )
ASSERT( reptCount == 0 )
ASSERT( b_1 == 0xFF )
if( b == 0xFF ){ // , f1 FF FF.
ASSERT( offset <= 31 );
*o++ = F3 | offset; // F3, -- --.
return o - out;
}
fullCount = 2; // , f2 -- yy.
OUT_fullSigil // F2, -- yy.
goto lastByte;
}
if( fullCount == 2 ) { // , f2 FF yy.
ASSERT( zeroCount == 0 )
ASSERT( reptCount == 0 )
ASSERT( b_1 == 0xFF )
if( b == 0xFF ){ // , f2 FF FF.
ASSERT( offset <= 31 );
*o++ = F4 | offset; // F4, -- --.
return o - out;
}
// , f2 FF !FF
fullCount++; // , f3 -- !FF.
OUT_fullSigil // F3, -- !FF.
goto lastByte;
}
if( fullCount == 3 ) { // , f3 FF yy.
ASSERT( zeroCount == 0 )
ASSERT( reptCount == 0 )
ASSERT( b_1 == 0xFF )
if( b == 0xFF ){ // , f3 FF FF.
OUT_fullSigil // F3, FF FF.
ASSERT( offset <= 31 );
*o++ = F2 | offset; // F3 F2, -- --.
return o - out; // option: F4 FF, -- --. is also right
}
// , f3 FF !FF.
fullCount = 4; // , f4 -- xx.
OUT_fullSigil // F4, -- xx.
goto lastByte;
}
if( reptCount == 1 ) { // , r1 aa yy.
ASSERT( zeroCount == 0 )
ASSERT( fullCount == 0 )
ASSERT( b_1 != 0 )
ASSERT( b_1 != 0xFF )
if( b_1 == b ){ // , r1 aa aa.
OUTB( b_1 ) // aa, r1 -- aa.
ASSERT( offset <= 31 );
if( offset > 7 ){
*o++ = N | offset;
offset = 0;
}
*o++ = R2 | offset; // aa R2, -- --.
return o - out;
}
OUTB( b_1 ) // aa, r0 aa -- yy.
OUTB( b_1 ) // aa aa, -- yy.
goto lastByte;
}
if( reptCount == 2 ) { // , r2 aa yy.
ASSERT( zeroCount == 0 )
ASSERT( fullCount == 0 )
ASSERT( b_1 != 0 )
ASSERT( b_1 != 0xFF )
if( b_1 == b ){ // , r2 aa aa.
OUTB( b_1 ) // aa, r2 -- aa.
ASSERT( offset <= 31 );
if( offset > 7 ){
*o++ = N | offset;
offset = 0;
}
*o++ = R3 | offset; // aa R3, -- --.
return o - out;
}
OUTB( b_1 ) // aa, r2 -- yy.
OUT_reptSigil // aa R2, -- xx.
goto lastByte;
}
if( reptCount == 3 ) { // , r3 aa yy.
ASSERT( zeroCount == 0 )
ASSERT( fullCount == 0 )
ASSERT( b_1 != 0 )
ASSERT( b_1 != 0xFF )
OUTB( b_1 ) // aa, r3 -- yy.
if( b_1 == b ){ // , r3 aa aa.
ASSERT( offset <= 31 );
if( offset > 7 ){
*o++ = N | offset;
offset = 0;
}
*o++ = R4 | offset; // aa R4, -- --.
return o - out;
}
// aa, r3 -- yy.
OUT_reptSigil // aa R3, -- xx.
goto lastByte;
}
ASSERT( 0 ) // will not be reached
}
}
}
if( length == 0 ){ // , -- --.
return 0;
}
if( length == 1 ){ // , . xx
b = *i; // , -- xx.
goto lastByte;
}
lastByte: // , -- xx.
if( b == 0 ){ // , -- 00.
ASSERT( offset <= 31 );
*o++ = Z1 | offset; // Z1, -- --.
return o - out;
}else{ // , -- aa.
*o++ = b; // aa|ff, -- --.
offset++;
ASSERT( offset <= 31 );
*o++ = N | offset; // aa Nn, -- --.
return o - out;
}
ASSERT( 0 )
//return 0; // will not be reached
}