forked from pd0mz/go-dmr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bits.go
309 lines (304 loc) · 4.19 KB
/
bits.go
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
package dmr
// Various sizes of information chunks.
const (
PayloadBits = 98 + 10 + 48 + 10 + 98
InfoHalfBits = 98
InfoBits = 2 * InfoHalfBits
InfoSize = 12 // After BPTC(196, 96) decoding
SlotTypeHalfBits = 10
SlotTypeBits = 2 * SlotTypeHalfBits
SignalBits = 48
SyncOffsetBits = InfoHalfBits + SlotTypeHalfBits
SyncBits = SignalBits
VoiceHalfBits = 108
VoiceBits = 2 * VoiceHalfBits
EMBHalfBits = 8
EMBBits = 2 * EMBHalfBits
EMBSignallingLCFragmentBits = 32
)
// Because Go doesn't have binary literals ("We've found hex and octal to be sufficient")
const (
B00000000 = iota
B00000001
B00000010
B00000011
B00000100
B00000101
B00000110
B00000111
B00001000
B00001001
B00001010
B00001011
B00001100
B00001101
B00001110
B00001111
B00010000
B00010001
B00010010
B00010011
B00010100
B00010101
B00010110
B00010111
B00011000
B00011001
B00011010
B00011011
B00011100
B00011101
B00011110
B00011111
B00100000
B00100001
B00100010
B00100011
B00100100
B00100101
B00100110
B00100111
B00101000
B00101001
B00101010
B00101011
B00101100
B00101101
B00101110
B00101111
B00110000
B00110001
B00110010
B00110011
B00110100
B00110101
B00110110
B00110111
B00111000
B00111001
B00111010
B00111011
B00111100
B00111101
B00111110
B00111111
B01000000
B01000001
B01000010
B01000011
B01000100
B01000101
B01000110
B01000111
B01001000
B01001001
B01001010
B01001011
B01001100
B01001101
B01001110
B01001111
B01010000
B01010001
B01010010
B01010011
B01010100
B01010101
B01010110
B01010111
B01011000
B01011001
B01011010
B01011011
B01011100
B01011101
B01011110
B01011111
B01100000
B01100001
B01100010
B01100011
B01100100
B01100101
B01100110
B01100111
B01101000
B01101001
B01101010
B01101011
B01101100
B01101101
B01101110
B01101111
B01110000
B01110001
B01110010
B01110011
B01110100
B01110101
B01110110
B01110111
B01111000
B01111001
B01111010
B01111011
B01111100
B01111101
B01111110
B01111111
B10000000
B10000001
B10000010
B10000011
B10000100
B10000101
B10000110
B10000111
B10001000
B10001001
B10001010
B10001011
B10001100
B10001101
B10001110
B10001111
B10010000
B10010001
B10010010
B10010011
B10010100
B10010101
B10010110
B10010111
B10011000
B10011001
B10011010
B10011011
B10011100
B10011101
B10011110
B10011111
B10100000
B10100001
B10100010
B10100011
B10100100
B10100101
B10100110
B10100111
B10101000
B10101001
B10101010
B10101011
B10101100
B10101101
B10101110
B10101111
B10110000
B10110001
B10110010
B10110011
B10110100
B10110101
B10110110
B10110111
B10111000
B10111001
B10111010
B10111011
B10111100
B10111101
B10111110
B10111111
B11000000
B11000001
B11000010
B11000011
B11000100
B11000101
B11000110
B11000111
B11001000
B11001001
B11001010
B11001011
B11001100
B11001101
B11001110
B11001111
B11010000
B11010001
B11010010
B11010011
B11010100
B11010101
B11010110
B11010111
B11011000
B11011001
B11011010
B11011011
B11011100
B11011101
B11011110
B11011111
B11100000
B11100001
B11100010
B11100011
B11100100
B11100101
B11100110
B11100111
B11101000
B11101001
B11101010
B11101011
B11101100
B11101101
B11101110
B11101111
B11110000
B11110001
B11110010
B11110011
B11110100
B11110101
B11110110
B11110111
B11111000
B11111001
B11111010
B11111011
B11111100
B11111101
B11111110
B11111111
)
// BytesToBits converts a byte slice to a byte slice representing the individual data bits.
func BytesToBits(data []byte) []byte {
var bits = make([]byte, len(data)*8)
for i := 0; i < len(data); i++ {
copy(bits[i*8:], toBits(data[i]))
}
return bits
}
func toBits(b byte) []byte {
var o = make([]byte, 8)
for bit, mask := 0, byte(128); bit < 8; bit, mask = bit+1, mask>>1 {
if b&mask != 0 {
o[bit] = 1
}
}
return o
}
// BitsToBytes converts a byte slice of bits to a byte slice.
func BitsToBytes(bits []byte) []byte {
var data = make([]byte, (len(bits)+7)/8)
for i, b := range bits {
if b == 0x01 {
data[i/8] |= (1 << byte(7-(i%8)))
}
}
return data
}