NCEPLIBS-g2c 2.0.0
Loading...
Searching...
No Matches
gbits.c
Go to the documentation of this file.
1
6#include "grib2_int.h"
7
19void
20gbit(unsigned char *in, g2int *iout, g2int iskip, g2int nbits)
21{
22 gbits(in, iout, iskip, nbits, (g2int)0, (g2int)1);
23}
24
37void
38sbit(unsigned char *out, g2int *in, g2int iskip, g2int nbits)
39{
40 sbits(out, in, iskip, nbits, (g2int)0, (g2int)1);
41}
42
56void
57gbits(unsigned char *in, g2int *iout, g2int iskip, g2int nbits,
58 g2int nskip, g2int n)
59{
60 g2int i, tbit, bitcnt, ibit, itmp;
61 g2int nbit, index;
62 static g2int ones[] = {1, 3, 7, 15, 31, 63, 127, 255};
63
64 /* nbit is the start position of the field in bits */
65 nbit = iskip;
66 for (i = 0; i < n; i++)
67 {
68 bitcnt = nbits;
69 index = nbit / 8;
70 ibit = nbit % 8;
71 nbit = nbit + nbits + nskip;
72
73 /* first byte */
74 tbit = (bitcnt < (8 - ibit)) ? bitcnt : 8 - ibit; // find min
75 itmp = (int)*(in + index) & ones[7 - ibit];
76 if (tbit != 8 - ibit)
77 itmp >>= (8 - ibit - tbit);
78 index++;
79 bitcnt = bitcnt - tbit;
80
81 /* now transfer whole bytes */
82 while (bitcnt >= 8)
83 {
84 itmp = itmp << 8 | (int)*(in + index);
85 bitcnt = bitcnt - 8;
86 index++;
87 }
88
89 /* get data from last byte */
90 if (bitcnt > 0)
91 {
92 itmp = (itmp << bitcnt) |
93 (((int)*(in + index) >> (8 - bitcnt)) & ones[bitcnt - 1]);
94 }
95
96 iout[i] = itmp;
97 }
98}
99
112void
113g2c_gbit_int(unsigned char *in, int *iout, int iskip, int nbits)
114{
115 g2c_gbits_int(in, iout, iskip, nbits, 0, 1);
116}
117
136int
137g2c_gbits_int(unsigned char *in, int *iout, int iskip, int nbits,
138 int nskip, int n)
139{
140 g2int *g2iout;
141 int i;
142
143 /* The in parameter is required. */
144 if (!in)
145 return G2C_EINVAL;
146
147 /* Get some memory for results. */
148 if (!(g2iout = malloc(n * sizeof(g2int))))
149 return G2C_ENOMEM;
150
151 gbits(in, g2iout, iskip, nbits, nskip, n);
152
153 /* Copy from g2int to int. */
154 for (i = 0; i < n; i++)
155 iout[i] = (int)g2iout[i];
156
157 /* Free memory. */
158 free(g2iout);
159
160 return G2C_NOERROR;
161}
162
177void
178sbits(unsigned char *out, g2int *in, g2int iskip, g2int nbits,
179 g2int nskip, g2int n)
180{
181 g2int i, bitcnt, tbit, ibit, itmp, imask, itmp2, itmp3;
182 g2int nbit, index;
183 static g2int ones[] = {1, 3, 7, 15, 31, 63, 127, 255};
184
185 /* number bits from zero to ... nbit is the last bit of the field
186 * to be filled. */
187 nbit = iskip + nbits - 1;
188 for (i = 0; i < n; i++)
189 {
190 itmp = *(in + i);
191 bitcnt = nbits;
192 index = nbit / 8;
193 ibit = nbit % 8;
194 nbit = nbit + nbits + nskip;
195
196 /* make byte aligned */
197 if (ibit != 7)
198 {
199 tbit = (bitcnt < (ibit + 1)) ? bitcnt : ibit + 1; /* find min */
200 imask = ones[tbit - 1] << (7 - ibit);
201 itmp2 = (itmp << (7 - ibit)) & imask;
202 itmp3 = (int)*(out + index) & (255 - imask);
203 out[index] = (unsigned char)(itmp2 | itmp3);
204 bitcnt = bitcnt - tbit;
205 itmp = itmp >> tbit;
206 index--;
207 }
208 /* now byte aligned */
209
210 /* do by bytes */
211 while (bitcnt >= 8)
212 {
213 out[index] = (unsigned char)(itmp & 255);
214 itmp = itmp >> 8;
215 bitcnt = bitcnt - 8;
216 index--;
217 }
218
219 /* do last byte */
220 if (bitcnt > 0)
221 {
222 itmp2 = itmp & ones[bitcnt - 1];
223 itmp3 = (int)*(out + index) & (255 - ones[bitcnt - 1]);
224 out[index] = (unsigned char)(itmp2 | itmp3);
225 }
226 }
227}
void gbit(unsigned char *in, g2int *iout, g2int iskip, g2int nbits)
Get arbitrary size values from a packed bit string, right justifying each value in the unpacked iout ...
Definition gbits.c:20
void gbits(unsigned char *in, g2int *iout, g2int iskip, g2int nbits, g2int nskip, g2int n)
Unpack arbitrary size values from a packed bit string, right justifying each value in the unpacked io...
Definition gbits.c:57
void sbits(unsigned char *out, g2int *in, g2int iskip, g2int nbits, g2int nskip, g2int n)
Store arbitrary size values into a packed bit string, taking the low order bits from each value in th...
Definition gbits.c:178
int g2c_gbits_int(unsigned char *in, int *iout, int iskip, int nbits, int nskip, int n)
Extract arbitrary size values from a packed bit string, right justifying each value in the unpacked i...
Definition gbits.c:137
void g2c_gbit_int(unsigned char *in, int *iout, int iskip, int nbits)
Extract arbitrary size values from a packed bit string, right justifying each value in the unpacked i...
Definition gbits.c:113
void sbit(unsigned char *out, g2int *in, g2int iskip, g2int nbits)
Store arbitrary size values into a packed bit string, taking the low order bits from each value in th...
Definition gbits.c:38
#define G2C_ENOMEM
Out of memory.
Definition grib2.h:485
#define G2C_EINVAL
Invalid input.
Definition grib2.h:481
int64_t g2int
Long integer type.
Definition grib2.h:32
#define G2C_NOERROR
No error.
Definition grib2.h:476
Header file with internal function prototypes NCEPLIBS-g2c library.