29b7b10f5e04fb99aeda8836565472c99a8977e5
[ric-plt/resource-status-manager.git] / RSM / asn1codec / e2ap_engine / asn_bit_data.c
1
2 /*
3  * Copyright (c) 2005-2017 Lev Walkin <vlm@lionet.info>.
4  * All rights reserved.
5  * Redistribution and modifications are permitted subject to BSD license.
6  */
7 #include <asn_system.h>
8 #include <asn_internal.h>
9 #include <asn_bit_data.h>
10
11 /*
12  * Create a contiguous non-refillable bit data structure.
13  * Can be freed by FREEMEM().
14  */
15 asn_bit_data_t *
16 asn_bit_data_new_contiguous(const void *data, size_t size_bits) {
17     size_t size_bytes = (size_bits + 7) / 8;
18     asn_bit_data_t *pd;
19     uint8_t *bytes;
20
21     /* Get the extensions map */
22     pd = CALLOC(1, sizeof(*pd) + size_bytes + 1);
23     if(!pd) {
24         return NULL;
25     }
26     bytes = (void *)(((char *)pd) + sizeof(*pd));
27     memcpy(bytes, data, size_bytes);
28     bytes[size_bytes] = 0;
29     pd->buffer = bytes;
30     pd->nboff = 0;
31     pd->nbits = size_bits;
32
33     return pd;
34 }
35
36
37 char *
38 asn_bit_data_string(asn_bit_data_t *pd) {
39         static char buf[2][32];
40         static int n;
41         n = (n+1) % 2;
42     snprintf(buf[n], sizeof(buf[n]),
43              "{m=%" ASN_PRI_SIZE " span %" ASN_PRI_SIZE "[%" ASN_PRI_SIZE
44              "..%" ASN_PRI_SIZE "] (%" ASN_PRI_SIZE ")}",
45              pd->moved, ((uintptr_t)(pd->buffer) & 0xf), pd->nboff, pd->nbits,
46              pd->nbits - pd->nboff);
47     return buf[n];
48 }
49
50 void
51 asn_get_undo(asn_bit_data_t *pd, int nbits) {
52         if((ssize_t)pd->nboff < nbits) {
53                 assert((ssize_t)pd->nboff < nbits);
54         } else {
55                 pd->nboff -= nbits;
56                 pd->moved -= nbits;
57         }
58 }
59
60 /*
61  * Extract a small number of bits (<= 31) from the specified PER data pointer.
62  */
63 int32_t
64 asn_get_few_bits(asn_bit_data_t *pd, int nbits) {
65         size_t off;     /* Next after last bit offset */
66         ssize_t nleft;  /* Number of bits left in this stream */
67         uint32_t accum;
68         const uint8_t *buf;
69
70         if(nbits < 0)
71                 return -1;
72
73         nleft = pd->nbits - pd->nboff;
74         if(nbits > nleft) {
75                 int32_t tailv, vhead;
76                 if(!pd->refill || nbits > 31) return -1;
77                 /* Accumulate unused bytes before refill */
78                 ASN_DEBUG("Obtain the rest %d bits (want %d)",
79                         (int)nleft, (int)nbits);
80                 tailv = asn_get_few_bits(pd, nleft);
81                 if(tailv < 0) return -1;
82                 /* Refill (replace pd contents with new data) */
83                 if(pd->refill(pd))
84                         return -1;
85                 nbits -= nleft;
86                 vhead = asn_get_few_bits(pd, nbits);
87                 /* Combine the rest of previous pd with the head of new one */
88                 tailv = (tailv << nbits) | vhead;  /* Could == -1 */
89                 return tailv;
90         }
91
92         /*
93          * Normalize position indicator.
94          */
95         if(pd->nboff >= 8) {
96                 pd->buffer += (pd->nboff >> 3);
97                 pd->nbits  -= (pd->nboff & ~0x07);
98                 pd->nboff  &= 0x07;
99         }
100         pd->moved += nbits;
101         pd->nboff += nbits;
102         off = pd->nboff;
103         buf = pd->buffer;
104
105         /*
106          * Extract specified number of bits.
107          */
108         if(off <= 8)
109                 accum = nbits ? (buf[0]) >> (8 - off) : 0;
110         else if(off <= 16)
111                 accum = ((buf[0] << 8) + buf[1]) >> (16 - off);
112         else if(off <= 24)
113                 accum = ((buf[0] << 16) + (buf[1] << 8) + buf[2]) >> (24 - off);
114         else if(off <= 31)
115                 accum = (((uint32_t)buf[0] << 24) + (buf[1] << 16)
116                         + (buf[2] << 8) + (buf[3])) >> (32 - off);
117         else if(nbits <= 31) {
118                 asn_bit_data_t tpd = *pd;
119                 /* Here are we with our 31-bits limit plus 1..7 bits offset. */
120                 asn_get_undo(&tpd, nbits);
121                 /* The number of available bits in the stream allow
122                  * for the following operations to take place without
123                  * invoking the ->refill() function */
124                 accum  = asn_get_few_bits(&tpd, nbits - 24) << 24;
125                 accum |= asn_get_few_bits(&tpd, 24);
126         } else {
127                 asn_get_undo(pd, nbits);
128                 return -1;
129         }
130
131         accum &= (((uint32_t)1 << nbits) - 1);
132
133         ASN_DEBUG("  [PER got %2d<=%2d bits => span %d %+ld[%d..%d]:%02x (%d) => 0x%x]",
134                 (int)nbits, (int)nleft,
135                 (int)pd->moved,
136                 (((long)pd->buffer) & 0xf),
137                 (int)pd->nboff, (int)pd->nbits,
138                 ((pd->buffer != NULL)?pd->buffer[0]:0),
139                 (int)(pd->nbits - pd->nboff),
140                 (int)accum);
141
142         return accum;
143 }
144
145 /*
146  * Extract a large number of bits from the specified PER data pointer.
147  */
148 int
149 asn_get_many_bits(asn_bit_data_t *pd, uint8_t *dst, int alright, int nbits) {
150         int32_t value;
151
152         if(alright && (nbits & 7)) {
153                 /* Perform right alignment of a first few bits */
154                 value = asn_get_few_bits(pd, nbits & 0x07);
155                 if(value < 0) return -1;
156                 *dst++ = value; /* value is already right-aligned */
157                 nbits &= ~7;
158         }
159
160         while(nbits) {
161                 if(nbits >= 24) {
162                         value = asn_get_few_bits(pd, 24);
163                         if(value < 0) return -1;
164                         *(dst++) = value >> 16;
165                         *(dst++) = value >> 8;
166                         *(dst++) = value;
167                         nbits -= 24;
168                 } else {
169                         value = asn_get_few_bits(pd, nbits);
170                         if(value < 0) return -1;
171                         if(nbits & 7) { /* implies left alignment */
172                                 value <<= 8 - (nbits & 7),
173                                 nbits += 8 - (nbits & 7);
174                                 if(nbits > 24)
175                                         *dst++ = value >> 24;
176                         }
177                         if(nbits > 16)
178                                 *dst++ = value >> 16;
179                         if(nbits > 8)
180                                 *dst++ = value >> 8;
181                         *dst++ = value;
182                         break;
183                 }
184         }
185
186         return 0;
187 }
188
189 /*
190  * Put a small number of bits (<= 31).
191  */
192 int
193 asn_put_few_bits(asn_bit_outp_t *po, uint32_t bits, int obits) {
194         size_t off;     /* Next after last bit offset */
195         size_t omsk;    /* Existing last byte meaningful bits mask */
196         uint8_t *buf;
197
198         if(obits <= 0 || obits >= 32) return obits ? -1 : 0;
199
200         ASN_DEBUG("[PER put %d bits %x to %p+%d bits]",
201                         obits, (int)bits, (void *)po->buffer, (int)po->nboff);
202
203         /*
204          * Normalize position indicator.
205          */
206         if(po->nboff >= 8) {
207                 po->buffer += (po->nboff >> 3);
208                 po->nbits  -= (po->nboff & ~0x07);
209                 po->nboff  &= 0x07;
210         }
211
212         /*
213          * Flush whole-bytes output, if necessary.
214          */
215         if(po->nboff + obits > po->nbits) {
216                 size_t complete_bytes;
217                 if(!po->buffer) po->buffer = po->tmpspace;
218                 complete_bytes = (po->buffer - po->tmpspace);
219                 ASN_DEBUG("[PER output %ld complete + %ld]",
220                         (long)complete_bytes, (long)po->flushed_bytes);
221                 if(po->output(po->tmpspace, complete_bytes, po->op_key) < 0)
222                         return -1;
223                 if(po->nboff)
224                         po->tmpspace[0] = po->buffer[0];
225                 po->buffer = po->tmpspace;
226                 po->nbits = 8 * sizeof(po->tmpspace);
227                 po->flushed_bytes += complete_bytes;
228         }
229
230         /*
231          * Now, due to sizeof(tmpspace), we are guaranteed large enough space.
232          */
233         buf = po->buffer;
234         omsk = ~((1 << (8 - po->nboff)) - 1);
235         off = (po->nboff + obits);
236
237         /* Clear data of debris before meaningful bits */
238         bits &= (((uint32_t)1 << obits) - 1);
239
240         ASN_DEBUG("[PER out %d %u/%x (t=%d,o=%d) %x&%x=%x]", obits,
241                 (int)bits, (int)bits,
242                 (int)po->nboff, (int)off,
243                 buf[0], (int)(omsk&0xff),
244                 (int)(buf[0] & omsk));
245
246         if(off <= 8)    /* Completely within 1 byte */
247                 po->nboff = off,
248                 bits <<= (8 - off),
249                 buf[0] = (buf[0] & omsk) | bits;
250         else if(off <= 16)
251                 po->nboff = off,
252                 bits <<= (16 - off),
253                 buf[0] = (buf[0] & omsk) | (bits >> 8),
254                 buf[1] = bits;
255         else if(off <= 24)
256                 po->nboff = off,
257                 bits <<= (24 - off),
258                 buf[0] = (buf[0] & omsk) | (bits >> 16),
259                 buf[1] = bits >> 8,
260                 buf[2] = bits;
261         else if(off <= 31)
262                 po->nboff = off,
263                 bits <<= (32 - off),
264                 buf[0] = (buf[0] & omsk) | (bits >> 24),
265                 buf[1] = bits >> 16,
266                 buf[2] = bits >> 8,
267                 buf[3] = bits;
268         else {
269                 if(asn_put_few_bits(po, bits >> (obits - 24), 24)) return -1;
270                 if(asn_put_few_bits(po, bits, obits - 24)) return -1;
271         }
272
273         ASN_DEBUG("[PER out %u/%x => %02x buf+%ld]",
274                 (int)bits, (int)bits, buf[0],
275                 (long)(po->buffer - po->tmpspace));
276
277         return 0;
278 }
279
280
281 /*
282  * Output a large number of bits.
283  */
284 int
285 asn_put_many_bits(asn_bit_outp_t *po, const uint8_t *src, int nbits) {
286
287         while(nbits) {
288                 uint32_t value;
289
290                 if(nbits >= 24) {
291                         value = (src[0] << 16) | (src[1] << 8) | src[2];
292                         src += 3;
293                         nbits -= 24;
294                         if(asn_put_few_bits(po, value, 24))
295                                 return -1;
296                 } else {
297                         value = src[0];
298                         if(nbits > 8)
299                                 value = (value << 8) | src[1];
300                         if(nbits > 16)
301                                 value = (value << 8) | src[2];
302                         if(nbits & 0x07)
303                                 value >>= (8 - (nbits & 0x07));
304                         if(asn_put_few_bits(po, value, nbits))
305                                 return -1;
306                         break;
307                 }
308         }
309
310         return 0;
311 }
312
313
314 int
315 asn_put_aligned_flush(asn_bit_outp_t *po) {
316     uint32_t unused_bits = (0x7 & (8 - (po->nboff & 0x07)));
317     size_t complete_bytes =
318         (po->buffer ? po->buffer - po->tmpspace : 0) + ((po->nboff + 7) >> 3);
319
320     if(unused_bits) {
321         po->buffer[po->nboff >> 3] &= ~0u << unused_bits;
322     }
323
324     if(po->output(po->tmpspace, complete_bytes, po->op_key) < 0) {
325         return -1;
326     } else {
327         po->buffer = po->tmpspace;
328         po->nboff = 0;
329         po->nbits = 8 * sizeof(po->tmpspace);
330         po->flushed_bytes += complete_bytes;
331         return 0;
332     }
333 }
334