NCEPLIBS-g2c  1.6.4
gbits.c
Go to the documentation of this file.
1 
6 #include "grib2.h"
7 
19 void
20 gbit(unsigned char *in, g2int *iout, g2int iskip, g2int nbyte)
21 {
22  gbits(in, iout, iskip, nbyte, (g2int)0, (g2int)1);
23 }
24 
37 void
38 sbit(unsigned char *out, g2int *in, g2int iskip, g2int nbyte)
39 {
40  sbits(out, in, iskip, nbyte, (g2int)0, (g2int)1);
41 }
42 
56 void
57 gbits(unsigned char *in, g2int *iout, g2int iskip, g2int nbyte,
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 = nbyte;
69  index = nbit / 8;
70  ibit = nbit % 8;
71  nbit = nbit + nbyte + 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) itmp >>= (8 - ibit - tbit);
77  index++;
78  bitcnt = bitcnt - tbit;
79 
80  /* now transfer whole bytes */
81  while (bitcnt >= 8)
82  {
83  itmp = itmp << 8 | (int)*(in + index);
84  bitcnt = bitcnt - 8;
85  index++;
86  }
87 
88  /* get data from last byte */
89  if (bitcnt > 0)
90  {
91  itmp = ( itmp << bitcnt ) |
92  (((int)*(in + index) >> (8 - bitcnt)) & ones[bitcnt - 1]);
93  }
94 
95  *(iout + i) = itmp;
96  }
97 }
98 
113 void
114 sbits(unsigned char *out, g2int *in, g2int iskip, g2int nbyte,
115  g2int nskip, g2int n)
116 {
117  g2int i, bitcnt, tbit, ibit, itmp, imask, itmp2, itmp3;
118  g2int nbit, index;
119  static g2int ones[]={1, 3, 7, 15, 31, 63, 127, 255};
120 
121  /* number bits from zero to ... nbit is the last bit of the field
122  * to be filled. */
123  nbit = iskip + nbyte - 1;
124  for (i = 0; i < n; i++)
125  {
126  itmp = *(in + i);
127  bitcnt = nbyte;
128  index = nbit / 8;
129  ibit = nbit % 8;
130  nbit = nbit + nbyte + nskip;
131 
132  /* make byte aligned */
133  if (ibit != 7)
134  {
135  tbit = (bitcnt < (ibit+1)) ? bitcnt : ibit + 1; /* find min */
136  imask = ones[tbit - 1] << (7 - ibit);
137  itmp2 = (itmp << (7 - ibit)) & imask;
138  itmp3 = (int)*(out + index) & (255 - imask);
139  out[index] = (unsigned char)(itmp2 | itmp3);
140  bitcnt = bitcnt - tbit;
141  itmp = itmp >> tbit;
142  index--;
143  }
144  /* now byte aligned */
145 
146  /* do by bytes */
147  while (bitcnt >= 8)
148  {
149  out[index] = (unsigned char)(itmp & 255);
150  itmp = itmp >> 8;
151  bitcnt = bitcnt - 8;
152  index--;
153  }
154 
155  /* do last byte */
156  if (bitcnt > 0)
157  {
158  itmp2 = itmp & ones[bitcnt - 1];
159  itmp3 = (int)*(out + index) & (255 - ones[bitcnt - 1]);
160  out[index] = (unsigned char)(itmp2 | itmp3);
161  }
162  }
163 
164 }
void gbit(unsigned char *in, g2int *iout, g2int iskip, g2int nbyte)
Get bits - unpack bits: Extract arbitrary size values from a packed bit string, right justifying each...
Definition: gbits.c:20
void sbit(unsigned char *out, g2int *in, g2int iskip, g2int nbyte)
Store bits - put arbitrary size values into a packed bit string, taking the low order bits from each ...
Definition: gbits.c:38
void gbits(unsigned char *in, g2int *iout, g2int iskip, g2int nbyte, 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 nbyte, 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:114
Header file for NCEPLIBS-g2c library.
int64_t g2int
Long integer type.
Definition: grib2.h:20