remark release date
[Net-Radio-Location-SUPL-Test.git] / asn1 / per_support.c
blob2481fffb1f97b26b4b9bb0cad8afae8bcf9090b6
1 /*
2 * Copyright (c) 2005, 2006, 2007 Lev Walkin <vlm@lionet.info>.
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>
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=%ld span %+ld[%d..%d] (%d)}",
17 (long)pd->moved,
18 (((long)pd->buffer) & 0xf),
19 (int)pd->nboff, (int)pd->nbits,
20 (int)(pd->nbits - pd->nboff));
21 return buf[n];
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;
35 * Extract a small number of bits (<= 31) from the specified PER data pointer.
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;
44 if(nbits < 0)
45 return -1;
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)",
53 (int)nleft, (int)nbits);
54 tailv = per_get_few_bits(pd, nleft);
55 if(tailv < 0) return -1;
56 /* Refill (replace pd contents with new data) */
57 if(pd->refill(pd))
58 return -1;
59 nbits -= nleft;
60 vhead = per_get_few_bits(pd, nbits);
61 /* Combine the rest of previous pd with the head of new one */
62 tailv = (tailv << nbits) | vhead; /* Could == -1 */
63 return tailv;
67 * Normalize position indicator.
69 if(pd->nboff >= 8) {
70 pd->buffer += (pd->nboff >> 3);
71 pd->nbits -= (pd->nboff & ~0x07);
72 pd->nboff &= 0x07;
74 pd->moved += nbits;
75 pd->nboff += nbits;
76 off = pd->nboff;
77 buf = pd->buffer;
80 * Extract specified number of bits.
82 if(off <= 8)
83 accum = nbits ? (buf[0]) >> (8 - off) : 0;
84 else if(off <= 16)
85 accum = ((buf[0] << 8) + buf[1]) >> (16 - off);
86 else if(off <= 24)
87 accum = ((buf[0] << 16) + (buf[1] << 8) + buf[2]) >> (24 - off);
88 else if(off <= 31)
89 accum = ((buf[0] << 24) + (buf[1] << 16)
90 + (buf[2] << 8) + (buf[3])) >> (32 - off);
91 else if(nbits <= 31) {
92 asn_per_data_t tpd = *pd;
93 /* Here are we with our 31-bits limit plus 1..7 bits offset. */
94 per_get_undo(&tpd, nbits);
95 /* The number of available bits in the stream allow
96 * for the following operations to take place without
97 * invoking the ->refill() function */
98 accum = per_get_few_bits(&tpd, nbits - 24) << 24;
99 accum |= per_get_few_bits(&tpd, 24);
100 } else {
101 per_get_undo(pd, nbits);
102 return -1;
105 accum &= (((uint32_t)1 << nbits) - 1);
107 ASN_DEBUG(" [PER got %2d<=%2d bits => span %d %+ld[%d..%d]:%02x (%d) => 0x%x]",
108 (int)nbits, (int)nleft,
109 (int)pd->moved,
110 (((long)pd->buffer) & 0xf),
111 (int)pd->nboff, (int)pd->nbits,
112 pd->buffer[0],
113 (int)(pd->nbits - pd->nboff),
114 (int)accum);
116 return accum;
120 * Extract a large number of bits from the specified PER data pointer.
123 per_get_many_bits(asn_per_data_t *pd, uint8_t *dst, int alright, int nbits) {
124 int32_t value;
126 if(alright && (nbits & 7)) {
127 /* Perform right alignment of a first few bits */
128 value = per_get_few_bits(pd, nbits & 0x07);
129 if(value < 0) return -1;
130 *dst++ = value; /* value is already right-aligned */
131 nbits &= ~7;
134 while(nbits) {
135 if(nbits >= 24) {
136 value = per_get_few_bits(pd, 24);
137 if(value < 0) return -1;
138 *(dst++) = value >> 16;
139 *(dst++) = value >> 8;
140 *(dst++) = value;
141 nbits -= 24;
142 } else {
143 value = per_get_few_bits(pd, nbits);
144 if(value < 0) return -1;
145 if(nbits & 7) { /* implies left alignment */
146 value <<= 8 - (nbits & 7),
147 nbits += 8 - (nbits & 7);
148 if(nbits > 24)
149 *dst++ = value >> 24;
151 if(nbits > 16)
152 *dst++ = value >> 16;
153 if(nbits > 8)
154 *dst++ = value >> 8;
155 *dst++ = value;
156 break;
160 return 0;
164 * Get the length "n" from the stream.
166 ssize_t
167 uper_get_length(asn_per_data_t *pd, int ebits, int *repeat) {
168 ssize_t value;
170 *repeat = 0;
172 if(ebits >= 0) return per_get_few_bits(pd, ebits);
174 value = per_get_few_bits(pd, 8);
175 if(value < 0) return -1;
176 if((value & 128) == 0) /* #10.9.3.6 */
177 return (value & 0x7F);
178 if((value & 64) == 0) { /* #10.9.3.7 */
179 value = ((value & 63) << 8) | per_get_few_bits(pd, 8);
180 if(value < 0) return -1;
181 return value;
183 value &= 63; /* this is "m" from X.691, #10.9.3.8 */
184 if(value < 1 || value > 4)
185 return -1;
186 *repeat = 1;
187 return (16384 * value);
191 * Get the normally small length "n".
192 * This procedure used to decode length of extensions bit-maps
193 * for SET and SEQUENCE types.
195 ssize_t
196 uper_get_nslength(asn_per_data_t *pd) {
197 ssize_t length;
199 ASN_DEBUG("Getting normally small length");
201 if(per_get_few_bits(pd, 1) == 0) {
202 length = per_get_few_bits(pd, 6) + 1;
203 if(length <= 0) return -1;
204 ASN_DEBUG("l=%d", (int)length);
205 return length;
206 } else {
207 int repeat;
208 length = uper_get_length(pd, -1, &repeat);
209 if(length >= 0 && !repeat) return length;
210 return -1; /* Error, or do not support >16K extensions */
215 * Get the normally small non-negative whole number.
216 * X.691, #10.6
218 ssize_t
219 uper_get_nsnnwn(asn_per_data_t *pd) {
220 ssize_t value;
222 value = per_get_few_bits(pd, 7);
223 if(value & 64) { /* implicit (value < 0) */
224 value &= 63;
225 value <<= 2;
226 value |= per_get_few_bits(pd, 2);
227 if(value & 128) /* implicit (value < 0) */
228 return -1;
229 if(value == 0)
230 return 0;
231 if(value >= 3)
232 return -1;
233 value = per_get_few_bits(pd, 8 * value);
234 return value;
237 return value;
241 * Put the normally small non-negative whole number.
242 * X.691, #10.6
245 uper_put_nsnnwn(asn_per_outp_t *po, int n) {
246 int bytes;
248 if(n <= 63) {
249 if(n < 0) return -1;
250 return per_put_few_bits(po, n, 7);
252 if(n < 256)
253 bytes = 1;
254 else if(n < 65536)
255 bytes = 2;
256 else if(n < 256 * 65536)
257 bytes = 3;
258 else
259 return -1; /* This is not a "normally small" value */
260 if(per_put_few_bits(po, bytes, 8))
261 return -1;
263 return per_put_few_bits(po, n, 8 * bytes);
268 * Put a small number of bits (<= 31).
271 per_put_few_bits(asn_per_outp_t *po, uint32_t bits, int obits) {
272 size_t off; /* Next after last bit offset */
273 size_t omsk; /* Existing last byte meaningful bits mask */
274 uint8_t *buf;
276 if(obits <= 0 || obits >= 32) return obits ? -1 : 0;
278 ASN_DEBUG("[PER put %d bits %x to %p+%d bits]",
279 obits, (int)bits, po->buffer, (int)po->nboff);
282 * Normalize position indicator.
284 if(po->nboff >= 8) {
285 po->buffer += (po->nboff >> 3);
286 po->nbits -= (po->nboff & ~0x07);
287 po->nboff &= 0x07;
291 * Flush whole-bytes output, if necessary.
293 if(po->nboff + obits > po->nbits) {
294 int complete_bytes = (po->buffer - po->tmpspace);
295 ASN_DEBUG("[PER output %ld complete + %ld]",
296 (long)complete_bytes, (long)po->flushed_bytes);
297 if(po->outper(po->tmpspace, complete_bytes, po->op_key) < 0)
298 return -1;
299 if(po->nboff)
300 po->tmpspace[0] = po->buffer[0];
301 po->buffer = po->tmpspace;
302 po->nbits = 8 * sizeof(po->tmpspace);
303 po->flushed_bytes += complete_bytes;
307 * Now, due to sizeof(tmpspace), we are guaranteed large enough space.
309 buf = po->buffer;
310 omsk = ~((1 << (8 - po->nboff)) - 1);
311 off = (po->nboff + obits);
313 /* Clear data of debris before meaningful bits */
314 bits &= (((uint32_t)1 << obits) - 1);
316 ASN_DEBUG("[PER out %d %u/%x (t=%d,o=%d) %x&%x=%x]", obits,
317 (int)bits, (int)bits,
318 (int)po->nboff, (int)off,
319 buf[0], (int)(omsk&0xff),
320 (int)(buf[0] & omsk));
322 if(off <= 8) /* Completely within 1 byte */
323 po->nboff = off,
324 bits <<= (8 - off),
325 buf[0] = (buf[0] & omsk) | bits;
326 else if(off <= 16)
327 po->nboff = off,
328 bits <<= (16 - off),
329 buf[0] = (buf[0] & omsk) | (bits >> 8),
330 buf[1] = bits;
331 else if(off <= 24)
332 po->nboff = off,
333 bits <<= (24 - off),
334 buf[0] = (buf[0] & omsk) | (bits >> 16),
335 buf[1] = bits >> 8,
336 buf[2] = bits;
337 else if(off <= 31)
338 po->nboff = off,
339 bits <<= (32 - off),
340 buf[0] = (buf[0] & omsk) | (bits >> 24),
341 buf[1] = bits >> 16,
342 buf[2] = bits >> 8,
343 buf[3] = bits;
344 else {
345 per_put_few_bits(po, bits >> (obits - 24), 24);
346 per_put_few_bits(po, bits, obits - 24);
349 ASN_DEBUG("[PER out %u/%x => %02x buf+%ld]",
350 (int)bits, (int)bits, buf[0],
351 (long)(po->buffer - po->tmpspace));
353 return 0;
358 * Output a large number of bits.
361 per_put_many_bits(asn_per_outp_t *po, const uint8_t *src, int nbits) {
363 while(nbits) {
364 uint32_t value;
366 if(nbits >= 24) {
367 value = (src[0] << 16) | (src[1] << 8) | src[2];
368 src += 3;
369 nbits -= 24;
370 if(per_put_few_bits(po, value, 24))
371 return -1;
372 } else {
373 value = src[0];
374 if(nbits > 8)
375 value = (value << 8) | src[1];
376 if(nbits > 16)
377 value = (value << 8) | src[2];
378 if(nbits & 0x07)
379 value >>= (8 - (nbits & 0x07));
380 if(per_put_few_bits(po, value, nbits))
381 return -1;
382 break;
386 return 0;
390 * Put the length "n" (or part of it) into the stream.
392 ssize_t
393 uper_put_length(asn_per_outp_t *po, size_t length) {
395 if(length <= 127) /* #10.9.3.6 */
396 return per_put_few_bits(po, length, 8)
397 ? -1 : (ssize_t)length;
398 else if(length < 16384) /* #10.9.3.7 */
399 return per_put_few_bits(po, length|0x8000, 16)
400 ? -1 : (ssize_t)length;
402 length >>= 14;
403 if(length > 4) length = 4;
405 return per_put_few_bits(po, 0xC0 | length, 8)
406 ? -1 : (ssize_t)(length << 14);
411 * Put the normally small length "n" into the stream.
412 * This procedure used to encode length of extensions bit-maps
413 * for SET and SEQUENCE types.
416 uper_put_nslength(asn_per_outp_t *po, size_t length) {
418 if(length <= 64) {
419 /* #10.9.3.4 */
420 if(length == 0) return -1;
421 return per_put_few_bits(po, length-1, 7) ? -1 : 0;
422 } else {
423 if(uper_put_length(po, length) != (ssize_t)length) {
424 /* This might happen in case of >16K extensions */
425 return -1;
429 return 0;