1 : #include "grib2.h"
2 :
3 320 : void gbit(unsigned char *in,g2int *iout,g2int iskip,g2int nbyte)
4 : {
5 320 : gbits(in,iout,iskip,nbyte,(g2int)0,(g2int)1);
6 320 : }
7 :
8 0 : void sbit(unsigned char *out,g2int *in,g2int iskip,g2int nbyte)
9 : {
10 0 : sbits(out,in,iskip,nbyte,(g2int)0,(g2int)1);
11 0 : }
12 :
13 :
14 1225 : void gbits(unsigned char *in,g2int *iout,g2int iskip,g2int nbyte,g2int nskip,
15 : g2int n)
16 : /* Get bits - unpack bits: Extract arbitrary size values from a
17 : / packed bit string, right justifying each value in the unpacked
18 : / iout array.
19 : / *in = pointer to character array input
20 : / *iout = pointer to unpacked array output
21 : / iskip = initial number of bits to skip
22 : / nbyte = number of bits to take
23 : / nskip = additional number of bits to skip on each iteration
24 : / n = number of iterations
25 : / v1.1
26 : */
27 : {
28 : g2int i,tbit,bitcnt,ibit,itmp;
29 : g2int nbit,index;
30 : static g2int ones[]={1,3,7,15,31,63,127,255};
31 :
32 : // nbit is the start position of the field in bits
33 1225 : nbit = iskip;
34 43202 : for (i=0;i<n;i++) {
35 41977 : bitcnt = nbyte;
36 41977 : index=nbit/8;
37 41977 : ibit=nbit%8;
38 41977 : nbit = nbit + nbyte + nskip;
39 :
40 : // first byte
41 41977 : tbit= ( bitcnt < (8-ibit) ) ? bitcnt : 8-ibit; // find min
42 41977 : itmp = (int)*(in+index) & ones[7-ibit];
43 41977 : if (tbit != 8-ibit) itmp >>= (8-ibit-tbit);
44 41977 : index++;
45 41977 : bitcnt = bitcnt - tbit;
46 :
47 : // now transfer whole bytes
48 84258 : while (bitcnt >= 8) {
49 304 : itmp = itmp<<8 | (int)*(in+index);
50 304 : bitcnt = bitcnt - 8;
51 304 : index++;
52 : }
53 :
54 : // get data from last byte
55 41977 : if (bitcnt > 0) {
56 4131 : itmp = ( itmp << bitcnt ) | ( ((int)*(in+index) >> (8-bitcnt)) & ones[bitcnt-1] );
57 : }
58 :
59 41977 : *(iout+i) = itmp;
60 : }
61 1225 : }
62 :
63 :
64 0 : void sbits(unsigned char *out,g2int *in,g2int iskip,g2int nbyte,g2int nskip,
65 : g2int n)
66 : /*C Store bits - pack bits: Put arbitrary size values into a
67 : / packed bit string, taking the low order bits from each value
68 : / in the unpacked array.
69 : / *iout = pointer to packed array output
70 : / *in = pointer to unpacked array input
71 : / iskip = initial number of bits to skip
72 : / nbyte = number of bits to pack
73 : / nskip = additional number of bits to skip on each iteration
74 : / n = number of iterations
75 : / v1.1
76 : */
77 : {
78 : g2int i,bitcnt,tbit,ibit,itmp,imask,itmp2,itmp3;
79 : g2int nbit,index;
80 : static g2int ones[]={1,3,7,15,31,63,127,255};
81 :
82 : // number bits from zero to ...
83 : // nbit is the last bit of the field to be filled
84 :
85 0 : nbit = iskip + nbyte - 1;
86 0 : for (i=0;i<n;i++) {
87 0 : itmp = *(in+i);
88 0 : bitcnt = nbyte;
89 0 : index=nbit/8;
90 0 : ibit=nbit%8;
91 0 : nbit = nbit + nbyte + nskip;
92 :
93 : // make byte aligned
94 0 : if (ibit != 7) {
95 0 : tbit= ( bitcnt < (ibit+1) ) ? bitcnt : ibit+1; // find min
96 0 : imask = ones[tbit-1] << (7-ibit);
97 0 : itmp2 = (itmp << (7-ibit)) & imask;
98 0 : itmp3 = (int)*(out+index) & (255-imask);
99 0 : out[index] = (unsigned char)(itmp2 | itmp3);
100 0 : bitcnt = bitcnt - tbit;
101 0 : itmp = itmp >> tbit;
102 0 : index--;
103 : }
104 :
105 : // now byte aligned
106 :
107 : // do by bytes
108 0 : while (bitcnt >= 8) {
109 0 : out[index] = (unsigned char)(itmp & 255);
110 0 : itmp = itmp >> 8;
111 0 : bitcnt = bitcnt - 8;
112 0 : index--;
113 : }
114 :
115 : // do last byte
116 :
117 0 : if (bitcnt > 0) {
118 0 : itmp2 = itmp & ones[bitcnt-1];
119 0 : itmp3 = (int)*(out+index) & (255-ones[bitcnt-1]);
120 0 : out[index] = (unsigned char)(itmp2 | itmp3);
121 : }
122 : }
123 :
124 0 : }
|