1 | // Part of BNC, a utility for retrieving decoding and
|
---|
2 | // converting GNSS data streams from NTRIP broadcasters.
|
---|
3 | //
|
---|
4 | // Copyright (C) 2015
|
---|
5 | // German Federal Agency for Cartography and Geodesy (BKG)
|
---|
6 | // http://www.bkg.bund.de
|
---|
7 | // Alberding GmbH
|
---|
8 | // http://www.alberding.eu
|
---|
9 | //
|
---|
10 | // Email: euref-ip@bkg.bund.de
|
---|
11 | //
|
---|
12 | // This program is free software; you can redistribute it and/or
|
---|
13 | // modify it under the terms of the GNU General Public License
|
---|
14 | // as published by the Free Software Foundation, version 2.
|
---|
15 | //
|
---|
16 | // This program is distributed in the hope that it will be useful,
|
---|
17 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
18 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
19 | // GNU General Public License for more details.
|
---|
20 | //
|
---|
21 | // You should have received a copy of the GNU General Public License
|
---|
22 | // along with this program; if not, write to the Free Software
|
---|
23 | // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
---|
24 |
|
---|
25 | #ifndef BITS_H
|
---|
26 | #define BITS_H
|
---|
27 |
|
---|
28 | #define LOADBITS(a) { \
|
---|
29 | while((a) > numbits) { \
|
---|
30 | if(!size--) return false; \
|
---|
31 | bitfield = (bitfield<<8)|*(data++); \
|
---|
32 | numbits += 8; \
|
---|
33 | } \
|
---|
34 | }
|
---|
35 |
|
---|
36 | /* extract bits from data stream
|
---|
37 | b = variable to store result, a = number of bits */
|
---|
38 | #define GETBITS64(b, a) { \
|
---|
39 | if(((a) > 56) && ((a)-56) > numbits) { \
|
---|
40 | uint64_t x; \
|
---|
41 | GETBITS(x, 56) \
|
---|
42 | LOADBITS((a)-56) \
|
---|
43 | b = ((x<<((a)-56)) | (bitfield<<(sizeof(bitfield)*8-numbits)) \
|
---|
44 | >>(sizeof(bitfield)*8-((a)-56))); \
|
---|
45 | numbits -= ((a)-56); \
|
---|
46 | } \
|
---|
47 | else { \
|
---|
48 | GETBITS(b, a) \
|
---|
49 | } \
|
---|
50 | }
|
---|
51 |
|
---|
52 | /* extract bits from data stream
|
---|
53 | b = variable to store result, a = number of bits */
|
---|
54 | #define GETBITS(b, a) { \
|
---|
55 | LOADBITS(a) \
|
---|
56 | b = (bitfield<<(64-numbits))>>(64-(a)); \
|
---|
57 | numbits -= (a); \
|
---|
58 | }
|
---|
59 |
|
---|
60 | /* extract bits from data stream
|
---|
61 | b = variable to store result, a = number of bits */
|
---|
62 | #define GETBITSFACTOR(b, a, c) { \
|
---|
63 | LOADBITS(a) \
|
---|
64 | b = ((bitfield<<(sizeof(bitfield)*8-numbits))>>(sizeof(bitfield)*8-(a)))*(c); \
|
---|
65 | numbits -= (a); \
|
---|
66 | }
|
---|
67 |
|
---|
68 | /* extract floating value from data stream
|
---|
69 | b = variable to store result, a = number of bits */
|
---|
70 | #define GETFLOAT(b, a, c) { \
|
---|
71 | LOADBITS(a) \
|
---|
72 | b = ((double)((bitfield<<(64-numbits))>>(64-(a))))*(c); \
|
---|
73 | numbits -= (a); \
|
---|
74 | }
|
---|
75 |
|
---|
76 | /* extract signed floating value from data stream
|
---|
77 | b = variable to store result, a = number of bits */
|
---|
78 | #define GETFLOATSIGN(b, a, c) { \
|
---|
79 | LOADBITS(a) \
|
---|
80 | b = ((double)(((int64_t)(bitfield<<(64-numbits)))>>(64-(a))))*(c); \
|
---|
81 | numbits -= (a); \
|
---|
82 | }
|
---|
83 |
|
---|
84 | /* extract bits from data stream
|
---|
85 | b = variable to store result, a = number of bits */
|
---|
86 | #define GETBITSSIGN(b, a) { \
|
---|
87 | LOADBITS(a) \
|
---|
88 | b = ((int64_t)(bitfield<<(64-numbits)))>>(64-(a)); \
|
---|
89 | numbits -= (a); \
|
---|
90 | }
|
---|
91 |
|
---|
92 | #define GETFLOATSIGNM(b, a, c) { \
|
---|
93 | int l; \
|
---|
94 | LOADBITS(a) \
|
---|
95 | l = (bitfield<<(64-numbits))>>(64-1); \
|
---|
96 | b = ((double)(((bitfield<<(64-(numbits-1))))>>(64-(a-1))))*(c); \
|
---|
97 | numbits -= (a); \
|
---|
98 | if(l) b *= -1.0; \
|
---|
99 | }
|
---|
100 |
|
---|
101 | #define SKIPBITS(b) { LOADBITS(b) numbits -= (b); }
|
---|
102 |
|
---|
103 | /* extract byte-aligned byte from data stream,
|
---|
104 | b = variable to store size, s = variable to store string pointer */
|
---|
105 | #define GETSTRING(b, s) { \
|
---|
106 | b = *(data++); \
|
---|
107 | s = (char *) data; \
|
---|
108 | data += b; \
|
---|
109 | size -= b+1; \
|
---|
110 | }
|
---|
111 |
|
---|
112 | #endif /* BITS_H */
|
---|