Debian Codegraph @

biosig4c++/t240/per_support.c

raw symbols SCIP
1 /*
2 * Copyright (c) 2005, 2006, 2007 Lev Walkin <[email protected]>.
3 * All rights reserved.
4 * Redistribution and modifications are permitted subject to BSD license.
5 */
6 #include <asn_system.h>
7 #include <asn_internal.h>
8 #include <per_support.h>
9
10 char *
11 per_data_string(asn_per_data_t *pd) {
12 static char buf[2][32];
13 static int n;
14 n = (n+1) % 2;
15 snprintf(buf[n], sizeof(buf),
16 "{m=%d span %+d[%d..%d] (%d)}",
17 pd->moved,
18 (((int)pd->buffer) & 0xf),
19 pd->nboff, pd->nbits,
20 pd->nbits - pd->nboff);
21 return buf[n];
22 }
23
24 void
25 per_get_undo(asn_per_data_t *pd, int nbits) {
26 if((ssize_t)pd->nboff < nbits) {
27 assert((ssize_t)pd->nboff < nbits);
28 } else {
29 pd->nboff -= nbits;
30 pd->moved -= nbits;
31 }
32 }
33
34 /*
35 * Extract a small number of bits (<= 31) from the specified PER data pointer.
36 */
37 int32_t
38 per_get_few_bits(asn_per_data_t *pd, int nbits) {
39 size_t off; /* Next after last bit offset */
40 ssize_t nleft; /* Number of bits left in this stream */
41 uint32_t accum;
42 const uint8_t *buf;
43
44 if(nbits < 0)
45 return -1;
46
47 nleft = pd->nbits - pd->nboff;
48 if(nbits > nleft) {
49 int32_t tailv, vhead;
50 if(!pd->refill || nbits > 31) return -1;
51 /* Accumulate unused bytes before refill */
52 ASN_DEBUG("Obtain the rest %d bits (want %d)", nleft, nbits);
53 tailv = per_get_few_bits(pd, nleft);
54 if(tailv < 0) return -1;
55 /* Refill (replace pd contents with new data) */
56 if(pd->refill(pd))
57 return -1;
58 nbits -= nleft;
59 vhead = per_get_few_bits(pd, nbits);
60 /* Combine the rest of previous pd with the head of new one */
61 tailv = (tailv << nbits) | vhead; /* Could == -1 */
62 return tailv;
63 }
64
65 /*
66 * Normalize position indicator.
67 */
68 if(pd->nboff >= 8) {
69 pd->buffer += (pd->nboff >> 3);
70 pd->nbits -= (pd->nboff & ~0x07);
71 pd->nboff &= 0x07;
72 }
73 pd->moved += nbits;
74 pd->nboff += nbits;
75 off = pd->nboff;
76 buf = pd->buffer;
77
78 /*
79 * Extract specified number of bits.
80 */
81 if(off <= 8)
82 accum = nbits ? (buf[0]) >> (8 - off) : 0;
83 else if(off <= 16)
84 accum = ((buf[0] << 8) + buf[1]) >> (16 - off);
85 else if(off <= 24)
86 accum = ((buf[0] << 16) + (buf[1] << 8) + buf[2]) >> (24 - off);
87 else if(off <= 31)
88 accum = ((buf[0] << 24) + (buf[1] << 16)
89 + (buf[2] << 8) + (buf[3])) >> (32 - off);
90 else if(nbits <= 31) {
91 asn_per_data_t tpd = *pd;
92 /* Here are we with our 31-bits limit plus 1..7 bits offset. */
93 per_get_undo(&tpd, nbits);
94 /* The number of available bits in the stream allow
95 * for the following operations to take place without
96 * invoking the ->refill() function */
97 accum = per_get_few_bits(&tpd, nbits - 24) << 24;
98 accum |= per_get_few_bits(&tpd, 24);
99 } else {
100 per_get_undo(pd, nbits);
101 return -1;
102 }
103
104 accum &= (((uint32_t)1 << nbits) - 1);
105
106 ASN_DEBUG(" [PER got %2d<=%2d bits => span %d %+d[%d..%d]:%02x (%d) => 0x%x]",
107 nbits, nleft,
108 pd->moved,
109 (((int)pd->buffer) & 0xf),
110 pd->nboff, pd->nbits,
111 pd->buffer[0],
112 pd->nbits - pd->nboff,
113 (int)accum);
114
115 return accum;
116 }
117
118 /*
119 * Extract a large number of bits from the specified PER data pointer.
120 */
121 int
122 per_get_many_bits(asn_per_data_t *pd, uint8_t *dst, int alright, int nbits) {
123 int32_t value;
124
125 if(alright && (nbits & 7)) {
126 /* Perform right alignment of a first few bits */
127 value = per_get_few_bits(pd, nbits & 0x07);
128 if(value < 0) return -1;
129 *dst++ = value; /* value is already right-aligned */
130 nbits &= ~7;
131 }
132
133 while(nbits) {
134 if(nbits >= 24) {
135 value = per_get_few_bits(pd, 24);
136 if(value < 0) return -1;
137 *(dst++) = value >> 16;
138 *(dst++) = value >> 8;
139 *(dst++) = value;
140 nbits -= 24;
141 } else {
142 value = per_get_few_bits(pd, nbits);
143 if(value < 0) return -1;
144 if(nbits & 7) { /* implies left alignment */
145 value <<= 8 - (nbits & 7),
146 nbits += 8 - (nbits & 7);
147 if(nbits > 24)
148 *dst++ = value >> 24;
149 }
150 if(nbits > 16)
151 *dst++ = value >> 16;
152 if(nbits > 8)
153 *dst++ = value >> 8;
154 *dst++ = value;
155 break;
156 }
157 }
158
159 return 0;
160 }
161
162 /*
163 * Get the length "n" from the stream.
164 */
165 ssize_t
166 uper_get_length(asn_per_data_t *pd, int ebits, int *repeat) {
167 ssize_t value;
168
169 *repeat = 0;
170
171 if(ebits >= 0) return per_get_few_bits(pd, ebits);
172
173 value = per_get_few_bits(pd, 8);
174 if(value < 0) return -1;
175 if((value & 128) == 0) /* #10.9.3.6 */
176 return (value & 0x7F);
177 if((value & 64) == 0) { /* #10.9.3.7 */
178 value = ((value & 63) << 8) | per_get_few_bits(pd, 8);
179 if(value < 0) return -1;
180 return value;
181 }
182 value &= 63; /* this is "m" from X.691, #10.9.3.8 */
183 if(value < 1 || value > 4)
184 return -1;
185 *repeat = 1;
186 return (16384 * value);
187 }
188
189 /*
190 * Get the normally small length "n".
191 * This procedure used to decode length of extensions bit-maps
192 * for SET and SEQUENCE types.
193 */
194 ssize_t
195 uper_get_nslength(asn_per_data_t *pd) {
196 ssize_t length;
197
198 ASN_DEBUG("Getting normally small length");
199
200 if(per_get_few_bits(pd, 1) == 0) {
201 length = per_get_few_bits(pd, 6) + 1;
202 if(length <= 0) return -1;
203 ASN_DEBUG("l=%d", length);
204 return length;
205 } else {
206 int repeat;
207 length = uper_get_length(pd, -1, &repeat);
208 if(length >= 0 && !repeat) return length;
209 return -1; /* Error, or do not support >16K extensions */
210 }
211 }
212
213 /*
214 * Get the normally small non-negative whole number.
215 * X.691, #10.6
216 */
217 ssize_t
218 uper_get_nsnnwn(asn_per_data_t *pd) {
219 ssize_t value;
220
221 value = per_get_few_bits(pd, 7);
222 if(value & 64) { /* implicit (value < 0) */
223 value &= 63;
224 value <<= 2;
225 value |= per_get_few_bits(pd, 2);
226 if(value & 128) /* implicit (value < 0) */
227 return -1;
228 if(value == 0)
229 return 0;
230 if(value >= 3)
231 return -1;
232 value = per_get_few_bits(pd, 8 * value);
233 return value;
234 }
235
236 return value;
237 }
238
239 /*
240 * Put the normally small non-negative whole number.
241 * X.691, #10.6
242 */
243 int
244 uper_put_nsnnwn(asn_per_outp_t *po, int n) {
245 int bytes;
246
247 if(n <= 63) {
248 if(n < 0) return -1;
249 return per_put_few_bits(po, n, 7);
250 }
251 if(n < 256)
252 bytes = 1;
253 else if(n < 65536)
254 bytes = 2;
255 else if(n < 256 * 65536)
256 bytes = 3;
257 else
258 return -1; /* This is not a "normally small" value */
259 if(per_put_few_bits(po, bytes, 8))
260 return -1;
261
262 return per_put_few_bits(po, n, 8 * bytes);
263 }
264
265
266 /*
267 * Put a small number of bits (<= 31).
268 */
269 int
270 per_put_few_bits(asn_per_outp_t *po, uint32_t bits, int obits) {
271 size_t off; /* Next after last bit offset */
272 size_t omsk; /* Existing last byte meaningful bits mask */
273 uint8_t *buf;
274
275 if(obits <= 0 || obits >= 32) return obits ? -1 : 0;
276
277 ASN_DEBUG("[PER put %d bits %x to %p+%d bits]",
278 obits, (int)bits, po->buffer, po->nboff);
279
280 /*
281 * Normalize position indicator.
282 */
283 if(po->nboff >= 8) {
284 po->buffer += (po->nboff >> 3);
285 po->nbits -= (po->nboff & ~0x07);
286 po->nboff &= 0x07;
287 }
288
289 /*
290 * Flush whole-bytes output, if necessary.
291 */
292 if(po->nboff + obits > po->nbits) {
293 int complete_bytes = (po->buffer - po->tmpspace);
294 ASN_DEBUG("[PER output %d complete + %d]",
295 complete_bytes, po->flushed_bytes);
296 if(po->outper(po->tmpspace, complete_bytes, po->op_key) < 0)
297 return -1;
298 if(po->nboff)
299 po->tmpspace[0] = po->buffer[0];
300 po->buffer = po->tmpspace;
301 po->nbits = 8 * sizeof(po->tmpspace);
302 po->flushed_bytes += complete_bytes;
303 }
304
305 /*
306 * Now, due to sizeof(tmpspace), we are guaranteed large enough space.
307 */
308 buf = po->buffer;
309 omsk = ~((1 << (8 - po->nboff)) - 1);
310 off = (po->nboff += obits);
311
312 /* Clear data of debris before meaningful bits */
313 bits &= (((uint32_t)1 << obits) - 1);
314
315 ASN_DEBUG("[PER out %d %u/%x (t=%d,o=%d) %x&%x=%x]", obits,
316 (int)bits, (int)bits,
317 po->nboff - obits, off, buf[0], omsk&0xff, buf[0] & omsk);
318
319 if(off <= 8) /* Completely within 1 byte */
320 bits <<= (8 - off),
321 buf[0] = (buf[0] & omsk) | bits;
322 else if(off <= 16)
323 bits <<= (16 - off),
324 buf[0] = (buf[0] & omsk) | (bits >> 8),
325 buf[1] = bits;
326 else if(off <= 24)
327 bits <<= (24 - off),
328 buf[0] = (buf[0] & omsk) | (bits >> 16),
329 buf[1] = bits >> 8,
330 buf[2] = bits;
331 else if(off <= 31)
332 bits <<= (32 - off),
333 buf[0] = (buf[0] & omsk) | (bits >> 24),
334 buf[1] = bits >> 16,
335 buf[2] = bits >> 8,
336 buf[3] = bits;
337 else {
338 ASN_DEBUG("->[PER out split %d]", obits);
339 per_put_few_bits(po, bits >> 8, 24);
340 per_put_few_bits(po, bits, obits - 24);
341 ASN_DEBUG("<-[PER out split %d]", obits);
342 }
343
344 ASN_DEBUG("[PER out %u/%x => %02x buf+%d]",
345 (int)bits, (int)bits, buf[0], po->buffer - po->tmpspace);
346
347 return 0;
348 }
349
350
351 /*
352 * Output a large number of bits.
353 */
354 int
355 per_put_many_bits(asn_per_outp_t *po, const uint8_t *src, int nbits) {
356
357 while(nbits) {
358 uint32_t value;
359
360 if(nbits >= 24) {
361 value = (src[0] << 16) | (src[1] << 8) | src[2];
362 src += 3;
363 nbits -= 24;
364 if(per_put_few_bits(po, value, 24))
365 return -1;
366 } else {
367 value = src[0];
368 if(nbits > 8)
369 value = (value << 8) | src[1];
370 if(nbits > 16)
371 value = (value << 8) | src[2];
372 if(nbits & 0x07)
373 value >>= (8 - (nbits & 0x07));
374 if(per_put_few_bits(po, value, nbits))
375 return -1;
376 break;
377 }
378 }
379
380 return 0;
381 }
382
383 /*
384 * Put the length "n" (or part of it) into the stream.
385 */
386 ssize_t
387 uper_put_length(asn_per_outp_t *po, size_t length) {
388
389 if(length <= 127) /* #10.9.3.6 */
390 return per_put_few_bits(po, length, 8)
391 ? -1 : (ssize_t)length;
392 else if(length < 16384) /* #10.9.3.7 */
393 return per_put_few_bits(po, length|0x8000, 16)
394 ? -1 : (ssize_t)length;
395
396 length >>= 14;
397 if(length > 4) length = 4;
398
399 return per_put_few_bits(po, 0xC0 | length, 8)
400 ? -1 : (ssize_t)(length << 14);
401 }
402
403
404 /*
405 * Put the normally small length "n" into the stream.
406 * This procedure used to encode length of extensions bit-maps
407 * for SET and SEQUENCE types.
408 */
409 int
410 uper_put_nslength(asn_per_outp_t *po, size_t length) {
411
412 if(length <= 64) {
413 /* #10.9.3.4 */
414 if(length == 0) return -1;
415 return per_put_few_bits(po, length-1, 7) ? -1 : 0;
416 } else {
417 if(uper_put_length(po, length) != (ssize_t)length) {
418 /* This might happen in case of >16K extensions */
419 return -1;
420 }
421 }
422
423 return 0;
424 }
425