-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstuff64.c
More file actions
207 lines (196 loc) · 3.37 KB
/
stuff64.c
File metadata and controls
207 lines (196 loc) · 3.37 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
206
207
#include <stdint.h>
void cobs_encode(const uint8_t* input, int len, uint8_t* output)
{
uint8_t* stuff = output++;
uint8_t s = 1;
while (len--)
{
if (*input)
{
s++;
*output++ = *input;
}
if (!*input || s == 255)
{
*stuff = s;
stuff = output++;
s = 1;
}
input++;
}
*stuff = s;
*output = 0;
}
void cobs_decode(const uint8_t* input, uint8_t* output)
{
uint8_t stuff = *input++;
while (stuff)
{
for (int i = 1; i < stuff; i++)
{
*output++ = *input++;
if (!output[-1])
{
return;
}
}
if (stuff < 255)
{
*output++ = 0;
}
stuff = *input++;
}
}
void cobs64_encode(const uint64_t* input, int len, uint64_t* output)
{
uint8_t* stuff = (uint8_t*)output++;
uint8_t s = 1;
while (len--)
{
uint8_t ones = (uint8_t)*input != 0;
if (ones)
{
s++;
}
*output = *input;
output += ones;
if (!ones || s == 255)
{
*stuff = s;
stuff = (uint8_t*)output++;
s = 1;
}
input++;
}
*stuff = s;
*output = 0;
}
void cobs64_decode(const uint64_t* input, uint64_t* output)
{
uint8_t stuff = (uint8_t)*input++;
while (stuff)
{
for (int i = 1; i < stuff; i++)
{
*output++ = *input++;
if (!output[-1])
{
return;
}
}
if (stuff < 255)
{
*output++ = *input & ~0xffULL;
}
stuff = (uint8_t)*input++;
}
}
void z2_encode(const uint64_t* input, int len, uint64_t* output)
{
while (len)
{
if ((char)*input == 0 || (char)*input == 2)
{
uint64_t* stuff = output++;
uint64_t s = 0;
int l = (len > 56) ? 56 : len;
output += l;
input += l;
len -= l++;
for (int i = 1; i < l; i++)
{
s = (s << 1) | !(input[-i] & 1);
output[-i] = input[-i] ^ s;
}
*stuff = (s << 8) | 2;
}
else
{
*output++ = *input++;
len--;
}
}
*output = 0;
}
void z2_decode(const uint64_t* input, uint64_t* output)
{
while (*input)
{
if ((char)*input == 2)
{
uint64_t stuff = *input++ >> 8;
while (stuff && (*input))
{
*output++ = *input++ ^ stuff;
stuff >>= 1;
}
}
else
{
*output++ = *input++;
}
}
}
void evenify_encode(const uint64_t* input, int len, uint64_t* output)
{
while (len)
{
uint64_t* stuff = output++;
uint64_t s = 0;
int l = (len > 63) ? 63 : len;
output += l;
input += l;
len -= l++;
for (int i = 1; i < l; i++)
{
s = (s << 1) | (input[-i] & 1);
output[-i] = input[-i] & ~1;
}
s <<= 1;
*stuff = s;
}
*output = ~0ULL;
}
void evenify_decode(const uint64_t* input, uint64_t* output)
{
uint64_t stuff = 0;
while (!(*input & 1))
{
stuff = *input++;
stuff = (1ULL << 63) | (stuff >> 1);
while (stuff > 1 && !(*input & 1))
{
*output++ = *input++ | (stuff & 1);
stuff >>= 1;
}
}
}
void oddify_encode(const uint64_t* input, int len, uint64_t* output)
{
while (len)
{
uint64_t* stuff = output++;
uint64_t s = 0;
int i = 63;
for (; i && len; i--, len--)
{
s = (s << 1) | !(*input & 1);
*output++ = *input++ ^ s;
}
s <<= i;
*stuff = (s << 1) | 1;
}
*output = 0;
}
void oddify_decode(const uint64_t* input, uint64_t* output)
{
uint64_t stuff = 0;
while (*input & 1)
{
stuff = *input++;
for (int i = 63; i && (*input & 1); i--)
{
*output++ = *input++ ^ (stuff >> i);
}
}
}