-
Notifications
You must be signed in to change notification settings - Fork 1
/
rain_6_bit.c
235 lines (221 loc) · 4.71 KB
/
rain_6_bit.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
// This file provides functions for droplet 6-bit format
#include "rain.h"
#include <stdint.h>
// lookup_table_to_6_bit[six_bit_value]
// contains the 8 bit value (byte) corresponding to six_bit_value
static const uint8_t lookup_table_to_6_bit[256] = {
[0x00] = 0x00,
[0x09] = 0x01,
[0x0a] = 0x02,
[0x0d] = 0x03,
[0x20] = 0x04,
[0x21] = 0x05,
[0x22] = 0x06,
[0x23] = 0x07,
[0x24] = 0x08,
[0x25] = 0x09,
[0x26] = 0x0a,
[0x27] = 0x0b,
[0x28] = 0x0c,
[0x29] = 0x0d,
[0x2a] = 0x0e,
[0x2b] = 0x0f,
[0x2c] = 0x10,
[0x2d] = 0x11,
[0x2e] = 0x12,
[0x2f] = 0x13,
[0x30] = 0x14,
[0x31] = 0x15,
[0x32] = 0x16,
[0x33] = 0x17,
[0x34] = 0x18,
[0x35] = 0x19,
[0x36] = 0x1a,
[0x37] = 0x1b,
[0x38] = 0x1c,
[0x39] = 0x1d,
[0x3a] = 0x1e,
[0x3b] = 0x1f,
[0x3c] = 0x20,
[0x3d] = 0x21,
[0x3e] = 0x22,
[0x3f] = 0x23,
[0x40] = 0x24,
[0x60] = 0x25,
[0x61] = 0x26,
[0x62] = 0x27,
[0x63] = 0x28,
[0x64] = 0x29,
[0x65] = 0x2a,
[0x66] = 0x2b,
[0x67] = 0x2c,
[0x68] = 0x2d,
[0x69] = 0x2e,
[0x6a] = 0x2f,
[0x6b] = 0x30,
[0x6c] = 0x31,
[0x6d] = 0x32,
[0x6e] = 0x33,
[0x6f] = 0x34,
[0x70] = 0x35,
[0x71] = 0x36,
[0x72] = 0x37,
[0x73] = 0x38,
[0x74] = 0x39,
[0x75] = 0x3a,
[0x76] = 0x3b,
[0x77] = 0x3c,
[0x78] = 0x3d,
[0x79] = 0x3e,
[0x7a] = 0x3f,
};
// lookup_table_from_6_bit[six_bit_value]
// contains the 8 bit value (byte) corresponding to six_bit_value
static const uint8_t lookup_table_from_6_bit[64] = {
[0x00] = 0x00,
[0x01] = 0x09,
[0x02] = 0x0a,
[0x03] = 0x0d,
[0x04] = 0x20,
[0x05] = 0x21,
[0x06] = 0x22,
[0x07] = 0x23,
[0x08] = 0x24,
[0x09] = 0x25,
[0x0a] = 0x26,
[0x0b] = 0x27,
[0x0c] = 0x28,
[0x0d] = 0x29,
[0x0e] = 0x2a,
[0x0f] = 0x2b,
[0x10] = 0x2c,
[0x11] = 0x2d,
[0x12] = 0x2e,
[0x13] = 0x2f,
[0x14] = 0x30,
[0x15] = 0x31,
[0x16] = 0x32,
[0x17] = 0x33,
[0x18] = 0x34,
[0x19] = 0x35,
[0x1a] = 0x36,
[0x1b] = 0x37,
[0x1c] = 0x38,
[0x1d] = 0x39,
[0x1e] = 0x3a,
[0x1f] = 0x3b,
[0x20] = 0x3c,
[0x21] = 0x3d,
[0x22] = 0x3e,
[0x23] = 0x3f,
[0x24] = 0x40,
[0x25] = 0x60,
[0x26] = 0x61,
[0x27] = 0x62,
[0x28] = 0x63,
[0x29] = 0x64,
[0x2a] = 0x65,
[0x2b] = 0x66,
[0x2c] = 0x67,
[0x2d] = 0x68,
[0x2e] = 0x69,
[0x2f] = 0x6a,
[0x30] = 0x6b,
[0x31] = 0x6c,
[0x32] = 0x6d,
[0x33] = 0x6e,
[0x34] = 0x6f,
[0x35] = 0x70,
[0x36] = 0x71,
[0x37] = 0x72,
[0x38] = 0x73,
[0x39] = 0x74,
[0x3a] = 0x75,
[0x3b] = 0x76,
[0x3c] = 0x77,
[0x3d] = 0x78,
[0x3e] = 0x79,
[0x3f] = 0x7a,
};
// lookup_table_valid_6_bit[byte]
// contains 1 iff byte can be converted to droplet 6 bit format
// it contains 0 otherwise.
static const uint8_t lookup_table_valid_6_bit[256] = {
[0x00] = 1,
[0x09] = 1,
[0x0a] = 1,
[0x0d] = 1,
[0x20] = 1,
[0x21] = 1,
[0x22] = 1,
[0x23] = 1,
[0x24] = 1,
[0x25] = 1,
[0x26] = 1,
[0x27] = 1,
[0x28] = 1,
[0x29] = 1,
[0x2a] = 1,
[0x2b] = 1,
[0x2c] = 1,
[0x2d] = 1,
[0x2e] = 1,
[0x2f] = 1,
[0x30] = 1,
[0x31] = 1,
[0x32] = 1,
[0x33] = 1,
[0x34] = 1,
[0x35] = 1,
[0x36] = 1,
[0x37] = 1,
[0x38] = 1,
[0x39] = 1,
[0x3a] = 1,
[0x3b] = 1,
[0x3c] = 1,
[0x3d] = 1,
[0x3e] = 1,
[0x3f] = 1,
[0x40] = 1,
[0x60] = 1,
[0x61] = 1,
[0x62] = 1,
[0x63] = 1,
[0x64] = 1,
[0x65] = 1,
[0x66] = 1,
[0x67] = 1,
[0x68] = 1,
[0x69] = 1,
[0x6a] = 1,
[0x6b] = 1,
[0x6c] = 1,
[0x6d] = 1,
[0x6e] = 1,
[0x6f] = 1,
[0x70] = 1,
[0x71] = 1,
[0x72] = 1,
[0x73] = 1,
[0x74] = 1,
[0x75] = 1,
[0x76] = 1,
[0x77] = 1,
[0x78] = 1,
[0x79] = 1,
[0x7a] = 1,
};
// use these functions to convert from 8-bit encoding to 6-bit encoding and vice versa
// given an 8 bit value (byte), return the corresponding value in droplet 6-bit format
// return -1 if it is not possible to translate the 8 bit value to droplet 6-bit format
int droplet_to_6_bit(uint8_t eight_bit_value) {
return lookup_table_valid_6_bit[eight_bit_value] ?
lookup_table_to_6_bit[eight_bit_value] : -1;
}
// given a 6-bit droplet format value, return the corresponding 8-bit value (byte)
// return -1 if not given a valid 6-bit value
int droplet_from_6_bit(uint8_t six_bit_value) {
return (six_bit_value < sizeof lookup_table_from_6_bit / sizeof lookup_table_from_6_bit[0]) ?
lookup_table_from_6_bit[six_bit_value] : -1;
}