-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathgbits.c
227 lines (206 loc) · 6.29 KB
/
gbits.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
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
/** @file
* @brief Functions to pack and unpack bits to/from a packed bit
* string.
* @author NOAA Programmer
*/
#include "grib2_int.h"
/**
* Get arbitrary size values from a packed bit string, right
* justifying each value in the unpacked iout array.
*
* @param in pointer to character array input.
* @param iout pointer that gets the unpacked array output.
* @param iskip initial number of bits to skip.
* @param nbits number of bits to take.
*
* @author NOAA Programmer
*/
void
gbit(unsigned char *in, g2int *iout, g2int iskip, g2int nbits)
{
gbits(in, iout, iskip, nbits, (g2int)0, (g2int)1);
}
/**
* Store arbitrary size values into a packed bit string,
* taking the low order bits from each value in the unpacked array.
*
* @param out Pointer to packed array output. Must be allocated large
* enough to hold output.
* @param in Pointer that gets the unpacked array input.
* @param iskip Initial number of bits to skip.
* @param nbits Number of bits to pack.
*
* @author NOAA Programmer
*/
void
sbit(unsigned char *out, g2int *in, g2int iskip, g2int nbits)
{
sbits(out, in, iskip, nbits, (g2int)0, (g2int)1);
}
/**
* Unpack arbitrary size values from a packed bit string, right
* justifying each value in the unpacked iout array.
*
* @param in Pointer to character array input.
* @param iout Pointer that gets the unpacked array output.
* @param iskip Initial number of bits to skip.
* @param nbits Number of bits to take.
* @param nskip Additional number of bits to skip on each iteration.
* @param n Number of iterations.
*
* @author NOAA Programmer
*/
void
gbits(unsigned char *in, g2int *iout, g2int iskip, g2int nbits,
g2int nskip, g2int n)
{
g2int i, tbit, bitcnt, ibit, itmp;
g2int nbit, index;
static g2int ones[] = {1, 3, 7, 15, 31, 63, 127, 255};
/* nbit is the start position of the field in bits */
nbit = iskip;
for (i = 0; i < n; i++)
{
bitcnt = nbits;
index = nbit / 8;
ibit = nbit % 8;
nbit = nbit + nbits + nskip;
/* first byte */
tbit = (bitcnt < (8 - ibit)) ? bitcnt : 8 - ibit; // find min
itmp = (int)*(in + index) & ones[7 - ibit];
if (tbit != 8 - ibit)
itmp >>= (8 - ibit - tbit);
index++;
bitcnt = bitcnt - tbit;
/* now transfer whole bytes */
while (bitcnt >= 8)
{
itmp = itmp << 8 | (int)*(in + index);
bitcnt = bitcnt - 8;
index++;
}
/* get data from last byte */
if (bitcnt > 0)
{
itmp = (itmp << bitcnt) |
(((int)*(in + index) >> (8 - bitcnt)) & ones[bitcnt - 1]);
}
iout[i] = itmp;
}
}
/**
* Extract arbitrary size values from a packed bit string, right
* justifying each value in the unpacked iout array. This is similar
* to gbit(), but with int types instead of g2int.
*
* @param in pointer to character array input.
* @param iout pointer that gets the unpacked array output.
* @param iskip initial number of bits to skip.
* @param nbits number of bits to take.
*
* @author NOAA Programmer
*/
void
g2c_gbit_int(unsigned char *in, int *iout, int iskip, int nbits)
{
g2c_gbits_int(in, iout, iskip, nbits, 0, 1);
}
/**
* Extract arbitrary size values from a packed bit string, right
* justifying each value in the unpacked iout array. This is similar
* to gbits(), but with int types instead of g2int.
*
* @param in Pointer to character array input.
* @param iout Pointer that gets the unpacked array output.
* @param iskip Initial number of bits to skip.
* @param nbits Number of bits to take.
* @param nskip Additional number of bits to skip on each iteration.
* @param n Number of iterations.
*
* @return
* - ::G2C_NOERROR No error.
* - ::G2C_ENOMEM Out of memory.
*
* @author Ed Hartnett @date 8/31/22
*/
int
g2c_gbits_int(unsigned char *in, int *iout, int iskip, int nbits,
int nskip, int n)
{
g2int *g2iout;
int i;
/* The in parameter is required. */
if (!in)
return G2C_EINVAL;
/* Get some memory for results. */
if (!(g2iout = malloc(n * sizeof(g2int))))
return G2C_ENOMEM;
gbits(in, g2iout, iskip, nbits, nskip, n);
/* Copy from g2int to int. */
for (i = 0; i < n; i++)
iout[i] = (int)g2iout[i];
/* Free memory. */
free(g2iout);
return G2C_NOERROR;
}
/**
* Store arbitrary size values into a packed bit string, taking the
* low order bits from each value in the unpacked array.
*
* @param out Pointer to packed array output. Must be allocated large
* enough to hold output.
* @param in Pointer that gets the unpacked array input.
* @param iskip Initial number of bits to skip.
* @param nbits Number of bits to pack.
* @param nskip Additional number of bits to skip on each iteration.
* @param n Number of iterations.
*
* @author NOAA Programmer
*/
void
sbits(unsigned char *out, g2int *in, g2int iskip, g2int nbits,
g2int nskip, g2int n)
{
g2int i, bitcnt, tbit, ibit, itmp, imask, itmp2, itmp3;
g2int nbit, index;
static g2int ones[] = {1, 3, 7, 15, 31, 63, 127, 255};
/* number bits from zero to ... nbit is the last bit of the field
* to be filled. */
nbit = iskip + nbits - 1;
for (i = 0; i < n; i++)
{
itmp = *(in + i);
bitcnt = nbits;
index = nbit / 8;
ibit = nbit % 8;
nbit = nbit + nbits + nskip;
/* make byte aligned */
if (ibit != 7)
{
tbit = (bitcnt < (ibit + 1)) ? bitcnt : ibit + 1; /* find min */
imask = ones[tbit - 1] << (7 - ibit);
itmp2 = (itmp << (7 - ibit)) & imask;
itmp3 = (int)*(out + index) & (255 - imask);
out[index] = (unsigned char)(itmp2 | itmp3);
bitcnt = bitcnt - tbit;
itmp = itmp >> tbit;
index--;
}
/* now byte aligned */
/* do by bytes */
while (bitcnt >= 8)
{
out[index] = (unsigned char)(itmp & 255);
itmp = itmp >> 8;
bitcnt = bitcnt - 8;
index--;
}
/* do last byte */
if (bitcnt > 0)
{
itmp2 = itmp & ones[bitcnt - 1];
itmp3 = (int)*(out + index) & (255 - ones[bitcnt - 1]);
out[index] = (unsigned char)(itmp2 | itmp3);
}
}
}