NCEPLIBS-g2c  1.8.0
gbits.c
Go to the documentation of this file.
1 
6 #include "grib2_int.h"
7 
19 void
20 gbit(unsigned char *in, g2int *iout, g2int iskip, g2int nbits)
21 {
22  gbits(in, iout, iskip, nbits, (g2int)0, (g2int)1);
23 }
24 
37 void
38 sbit(unsigned char *out, g2int *in, g2int iskip, g2int nbits)
39 {
40  sbits(out, in, iskip, nbits, (g2int)0, (g2int)1);
41 }
42 
56 void
57 gbits(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 
113 void
114 g2c_gbit_int(unsigned char *in, int *iout, int iskip, int nbits)
115 {
116  g2c_gbits_int(in, iout, iskip, nbits, 0, 1);
117 }
118 
138 int
139 g2c_gbits_int(unsigned char *in, int *iout, int iskip, int nbits,
140  int nskip, int n)
141 {
142  g2int *g2iout;
143  int i;
144 
145  /* The in parameter is required. */
146  if (!in)
147  return G2C_EINVAL;
148 
149  /* Get some memory for results. */
150  if (!(g2iout = malloc(n * sizeof(g2int))))
151  return G2C_ENOMEM;
152 
153  gbits(in, g2iout, iskip, nbits, nskip, n);
154 
155  /* Copy from g2int to int. */
156  for (i = 0; i < n; i++)
157  iout[i] = (int)g2iout[i];
158 
159  /* Free memory. */
160  free(g2iout);
161 
162  return G2C_NOERROR;
163 }
164 
179 void
180 sbits(unsigned char *out, g2int *in, g2int iskip, g2int nbits,
181  g2int nskip, g2int n)
182 {
183  g2int i, bitcnt, tbit, ibit, itmp, imask, itmp2, itmp3;
184  g2int nbit, index;
185  static g2int ones[]={1, 3, 7, 15, 31, 63, 127, 255};
186 
187  /* number bits from zero to ... nbit is the last bit of the field
188  * to be filled. */
189  nbit = iskip + nbits - 1;
190  for (i = 0; i < n; i++)
191  {
192  itmp = *(in + i);
193  bitcnt = nbits;
194  index = nbit / 8;
195  ibit = nbit % 8;
196  nbit = nbit + nbits + nskip;
197 
198  /* make byte aligned */
199  if (ibit != 7)
200  {
201  tbit = (bitcnt < (ibit+1)) ? bitcnt : ibit + 1; /* find min */
202  imask = ones[tbit - 1] << (7 - ibit);
203  itmp2 = (itmp << (7 - ibit)) & imask;
204  itmp3 = (int)*(out + index) & (255 - imask);
205  out[index] = (unsigned char)(itmp2 | itmp3);
206  bitcnt = bitcnt - tbit;
207  itmp = itmp >> tbit;
208  index--;
209  }
210  /* now byte aligned */
211 
212  /* do by bytes */
213  while (bitcnt >= 8)
214  {
215  out[index] = (unsigned char)(itmp & 255);
216  itmp = itmp >> 8;
217  bitcnt = bitcnt - 8;
218  index--;
219  }
220 
221  /* do last byte */
222  if (bitcnt > 0)
223  {
224  itmp2 = itmp & ones[bitcnt - 1];
225  itmp3 = (int)*(out + index) & (255 - ones[bitcnt - 1]);
226  out[index] = (unsigned char)(itmp2 | itmp3);
227  }
228  }
229 
230 }
void gbit(unsigned char *in, g2int *iout, g2int iskip, g2int nbits)
Get bits - unpack bits: Extract arbitrary size values from a packed bit string, right justifying each...
Definition: gbits.c:20
void gbits(unsigned char *in, g2int *iout, g2int iskip, g2int nbits, g2int nskip, g2int n)
Get bits - unpack bits: Extract arbitrary size values from a packed bit string, right justifying each...
Definition: gbits.c:57
void sbits(unsigned char *out, g2int *in, g2int iskip, g2int nbits, g2int nskip, g2int n)
Store bits - put arbitrary size values into a packed bit string, taking the low order bits from each ...
Definition: gbits.c:180
int g2c_gbits_int(unsigned char *in, int *iout, int iskip, int nbits, int nskip, int n)
Get bits - unpack bits: Extract arbitrary size values from a packed bit string, right justifying each...
Definition: gbits.c:139
void g2c_gbit_int(unsigned char *in, int *iout, int iskip, int nbits)
Get bits - unpack bits: Extract arbitrary size values from a packed bit string, right justifying each...
Definition: gbits.c:114
void sbit(unsigned char *out, g2int *in, g2int iskip, g2int nbits)
Store bits - put arbitrary size values into a packed bit string, taking the low order bits from each ...
Definition: gbits.c:38
#define G2C_ENOMEM
Out of memory.
Definition: grib2.h:500
#define G2C_EINVAL
Invalid input.
Definition: grib2.h:496
int64_t g2int
Long integer type.
Definition: grib2.h:33
#define G2C_NOERROR
No error.
Definition: grib2.h:491
Header file with internal function prototypes NCEPLIBS-g2c library.