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 | { \
|
---|
30 | while((a) > numbits) \
|
---|
31 | { \
|
---|
32 | if(!size--) return false; \
|
---|
33 | bitfield = (bitfield<<8)|*(data++); \
|
---|
34 | numbits += 8; \
|
---|
35 | } \
|
---|
36 | }
|
---|
37 |
|
---|
38 | /* extract bits from data stream
|
---|
39 | b = variable to store result, a = number of bits */
|
---|
40 | #define GETBITS64(b, a) \
|
---|
41 | { \
|
---|
42 | if(((a) > 56) && ((a)-56) > numbits) \
|
---|
43 | { \
|
---|
44 | uint64_t x; \
|
---|
45 | GETBITS(x, 56) \
|
---|
46 | LOADBITS((a)-56) \
|
---|
47 | b = ((x<<((a)-56)) | (bitfield<<(sizeof(bitfield)*8-numbits)) \
|
---|
48 | >>(sizeof(bitfield)*8-((a)-56))); \
|
---|
49 | numbits -= ((a)-56); \
|
---|
50 | } \
|
---|
51 | else \
|
---|
52 | { \
|
---|
53 | GETBITS(b, a) \
|
---|
54 | } \
|
---|
55 | }
|
---|
56 |
|
---|
57 | /* extract bits from data stream
|
---|
58 | b = variable to store result, a = number of bits */
|
---|
59 | #define GETBITS(b, a) \
|
---|
60 | { \
|
---|
61 | LOADBITS(a) \
|
---|
62 | b = (bitfield<<(64-numbits))>>(64-(a)); \
|
---|
63 | numbits -= (a); \
|
---|
64 | }
|
---|
65 |
|
---|
66 | /* extract bits from data stream
|
---|
67 | b = variable to store result, a = number of bits */
|
---|
68 | #define GETBITSFACTOR(b, a, c) \
|
---|
69 | { \
|
---|
70 | LOADBITS(a) \
|
---|
71 | b = ((bitfield<<(sizeof(bitfield)*8-numbits))>>(sizeof(bitfield)*8-(a)))*(c); \
|
---|
72 | numbits -= (a); \
|
---|
73 | }
|
---|
74 |
|
---|
75 | /* extract floating value from data stream
|
---|
76 | b = variable to store result, a = number of bits */
|
---|
77 | #define GETFLOAT(b, a, c) \
|
---|
78 | { \
|
---|
79 | LOADBITS(a) \
|
---|
80 | b = ((double)((bitfield<<(64-numbits))>>(64-(a))))*(c); \
|
---|
81 | numbits -= (a); \
|
---|
82 | }
|
---|
83 |
|
---|
84 | /* extract signed floating value from data stream
|
---|
85 | b = variable to store result, a = number of bits */
|
---|
86 | #define GETFLOATSIGN(b, a, c) \
|
---|
87 | { \
|
---|
88 | LOADBITS(a) \
|
---|
89 | b = ((double)(((int64_t)(bitfield<<(64-numbits)))>>(64-(a))))*(c); \
|
---|
90 | numbits -= (a); \
|
---|
91 | }
|
---|
92 |
|
---|
93 | /* extract bits from data stream
|
---|
94 | b = variable to store result, a = number of bits */
|
---|
95 | #define GETBITSSIGN(b, a) \
|
---|
96 | { \
|
---|
97 | LOADBITS(a) \
|
---|
98 | b = ((int64_t)(bitfield<<(64-numbits)))>>(64-(a)); \
|
---|
99 | numbits -= (a); \
|
---|
100 | }
|
---|
101 |
|
---|
102 | #define GETFLOATSIGNM(b, a, c) \
|
---|
103 | { int l; \
|
---|
104 | LOADBITS(a) \
|
---|
105 | l = (bitfield<<(64-numbits))>>(64-1); \
|
---|
106 | b = ((double)(((bitfield<<(64-(numbits-1))))>>(64-(a-1))))*(c); \
|
---|
107 | numbits -= (a); \
|
---|
108 | if(l) b *= -1.0; \
|
---|
109 | }
|
---|
110 |
|
---|
111 | #define SKIPBITS(b) { LOADBITS(b) numbits -= (b); }
|
---|
112 |
|
---|
113 | /* extract byte-aligned byte from data stream,
|
---|
114 | b = variable to store size, s = variable to store string pointer */
|
---|
115 | #define GETSTRING(b, s) \
|
---|
116 | { \
|
---|
117 | b = *(data++); \
|
---|
118 | s = (char *) data; \
|
---|
119 | data += b; \
|
---|
120 | size -= b+1; \
|
---|
121 | }
|
---|
122 |
|
---|
123 | #endif /* BITS_H */
|
---|