forked from szpajder/dumpvdl2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitstream.c
141 lines (129 loc) · 3.84 KB
/
bitstream.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
/*
* dumpvdl2 - a VDL Mode 2 message decoder and protocol analyzer
*
* Copyright (c) 2017 Tomasz Lemiech <szpajder@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include <errno.h>
#include "dumpvdl2.h"
bitstream_t *bitstream_init(uint32_t len) {
bitstream_t *ret;
if(len == 0) return NULL;
ret = XCALLOC(1, sizeof(bitstream_t));
ret->buf = XCALLOC(len, sizeof(uint8_t));
ret->start = ret->end = ret->descrambler_pos = 0;
ret->len = len;
return ret;
}
void bitstream_reset(bitstream_t *bs) {
bs->start = bs->end = bs->descrambler_pos = 0;
}
void bitstream_destroy(bitstream_t *bs) {
if(bs != NULL) XFREE(bs->buf);
XFREE(bs);
}
int bitstream_append_msbfirst(bitstream_t *bs, const uint8_t *v, const uint32_t numbytes, const uint32_t numbits) {
if(bs->end + numbits * numbytes > bs->len)
return -1;
for(int i = 0; i < numbytes; i++) {
uint8_t t = v[i];
for(int j = numbits - 1; j >= 0; j--)
bs->buf[bs->end++] = (t >> j) & 0x01;
}
return 0;
}
int bitstream_append_lsbfirst(bitstream_t *bs, const uint8_t *bytes, const uint32_t numbytes, const uint32_t numbits) {
if(bs->end + numbits * numbytes > bs->len)
return -1;
for(int i = 0; i < numbytes; i++) {
uint8_t t = bytes[i];
for(int j = 0; j < numbits; j++)
bs->buf[bs->end++] = (t >> j) & 0x01;
}
return 0;
}
int bitstream_read_lsbfirst(bitstream_t *bs, uint8_t *bytes, const uint32_t numbytes, const uint32_t numbits) {
if(bs->start + numbits * numbytes > bs->end)
return -1;
for(uint32_t i = 0; i < numbytes; i++) {
bytes[i] = 0x00;
for(uint32_t j = 0; j < numbits; j++) {
bytes[i] |= (0x01 & bs->buf[bs->start++]) << j;
}
}
return 0;
}
int bitstream_read_word_msbfirst(bitstream_t *bs, uint32_t *ret, const uint32_t numbits) {
if(bs->start + numbits > bs->end)
return -1;
*ret = 0;
for(uint32_t i = 0; i < numbits; i++) {
*ret |= (0x01 & bs->buf[bs->start++]) << (numbits-i-1);
}
return 0;
}
void bitstream_descramble(bitstream_t *bs, uint16_t *lfsr) {
uint8_t bit;
int i;
if(bs->descrambler_pos < bs->start)
bs->descrambler_pos = bs->start;
for(i = bs->descrambler_pos; i < bs->end; i++) {
/* LFSR length: 15; feedback polynomial: x^15 + x + 1 */
bit = ((*lfsr >> 0) ^ (*lfsr >> 14)) & 1;
*lfsr = (*lfsr >> 1) | (bit << 14);
bs->buf[i] ^= bit;
}
debug_print("descrambled from %u to %u\n", bs->descrambler_pos, bs->end-1);
bs->descrambler_pos = bs->end;
}
int bitstream_hdlc_unstuff(bitstream_t *bs) {
int ones = 0;
int i, j;
for(i = j = bs->start; i < bs->end; i++) {
if(bs->buf[i] == 0x1) {
ones++;
if(ones > 6) // 7 ones - invalid bit sequence
return -1;
} else { // bs->buf[i] == 0
if(ones == 5) { // stuffed 0 bit - skip it
ones = 0;
continue;
}
ones = 0;
}
bs->buf[j++] = bs->buf[i];
}
debug_print("Unstuffed %u bits\n", bs->end - j);
bs->end = j;
return 0;
}
uint32_t reverse(uint32_t v, int numbits) {
uint32_t r = v; // r will be reversed bits of v; first get LSB of v
int s = sizeof(v) * CHAR_BIT - 1; // extra shift needed at end
for (v >>= 1; v; v >>= 1) {
r <<= 1;
r |= v & 1;
s--;
}
r <<= s; // shift when v's highest bits are zero
r >>= 32 - numbits;
return r;
}