Debian Codegraph @

skeletons/OCTET_STRING.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /*-
2 * Copyright (c) 2003, 2004, 2005, 2006 Lev Walkin <[email protected]>.
3 * All rights reserved.
4 * Redistribution and modifications are permitted subject to BSD license.
5 */
6 #include <asn_internal.h>
7 #include <OCTET_STRING.h>
8 #include <BIT_STRING.h> /* for .bits_unused member */
9 #include <errno.h>
10
11 /*
12 * OCTET STRING basic type description.
13 */
14 static const ber_tlv_tag_t asn_DEF_OCTET_STRING_tags[] = {
15 (ASN_TAG_CLASS_UNIVERSAL | (4 << 2))
16 };
17 static const asn_OCTET_STRING_specifics_t asn_DEF_OCTET_STRING_specs = {
18 sizeof(OCTET_STRING_t),
19 offsetof(OCTET_STRING_t, _asn_ctx),
20 ASN_OSUBV_STR
21 };
22 static const asn_per_constraints_t asn_DEF_OCTET_STRING_constraints = {
23 { APC_CONSTRAINED, 8, 8, 0, 255 },
24 { APC_SEMI_CONSTRAINED, -1, -1, 0, 0 },
25 0, 0
26 };
27 asn_TYPE_descriptor_t asn_DEF_OCTET_STRING = {
28 "OCTET STRING", /* Canonical name */
29 "OCTET_STRING", /* XML tag name */
30 OCTET_STRING_free,
31 OCTET_STRING_print, /* non-ascii stuff, generally */
32 asn_generic_no_constraint,
33 OCTET_STRING_decode_ber,
34 OCTET_STRING_encode_der,
35 OCTET_STRING_decode_xer_hex,
36 OCTET_STRING_encode_xer,
37 OCTET_STRING_decode_uper, /* Unaligned PER decoder */
38 OCTET_STRING_encode_uper, /* Unaligned PER encoder */
39 0, /* Use generic outmost tag fetcher */
40 asn_DEF_OCTET_STRING_tags,
41 sizeof(asn_DEF_OCTET_STRING_tags)
42 / sizeof(asn_DEF_OCTET_STRING_tags[0]),
43 asn_DEF_OCTET_STRING_tags, /* Same as above */
44 sizeof(asn_DEF_OCTET_STRING_tags)
45 / sizeof(asn_DEF_OCTET_STRING_tags[0]),
46 0, /* No PER visible constraints */
47 0, 0, /* No members */
48 &asn_DEF_OCTET_STRING_specs
49 };
50
51 #undef _CH_PHASE
52 #undef NEXT_PHASE
53 #undef PREV_PHASE
54 #define _CH_PHASE(ctx, inc) do { \
55 if(ctx->phase == 0) \
56 ctx->context = 0; \
57 ctx->phase += inc; \
58 } while(0)
59 #define NEXT_PHASE(ctx) _CH_PHASE(ctx, +1)
60 #define PREV_PHASE(ctx) _CH_PHASE(ctx, -1)
61
62 #undef ADVANCE
63 #define ADVANCE(num_bytes) do { \
64 size_t num = (num_bytes); \
65 buf_ptr = ((const char *)buf_ptr) + num; \
66 size -= num; \
67 consumed_myself += num; \
68 } while(0)
69
70 #undef RETURN
71 #define RETURN(_code) do { \
72 asn_dec_rval_t tmprval; \
73 tmprval.code = _code; \
74 tmprval.consumed = consumed_myself; \
75 return tmprval; \
76 } while(0)
77
78 #undef APPEND
79 #define APPEND(bufptr, bufsize) do { \
80 size_t _bs = (bufsize); /* Append size */ \
81 size_t _ns = ctx->context; /* Allocated now */ \
82 size_t _es = st->size + _bs; /* Expected size */ \
83 /* int is really a typeof(st->size): */ \
84 if((int)_es < 0) RETURN(RC_FAIL); \
85 if(_ns <= _es) { \
86 void *ptr; \
87 /* Be nice and round to the memory allocator */ \
88 do { _ns = _ns ? _ns << 1 : 16; } \
89 while(_ns <= _es); \
90 /* int is really a typeof(st->size): */ \
91 if((int)_ns < 0) RETURN(RC_FAIL); \
92 ptr = REALLOC(st->buf, _ns); \
93 if(ptr) { \
94 st->buf = (uint8_t *)ptr; \
95 ctx->context = _ns; \
96 } else { \
97 RETURN(RC_FAIL); \
98 } \
99 ASN_DEBUG("Reallocating into %ld", (long)_ns); \
100 } \
101 memcpy(st->buf + st->size, bufptr, _bs); \
102 /* Convenient nul-termination */ \
103 st->buf[_es] = '\0'; \
104 st->size = _es; \
105 } while(0)
106
107 /*
108 * The main reason why ASN.1 is still alive is that too much time and effort
109 * is necessary for learning it more or less adequately, thus creating a gut
110 * necessity to demonstrate that aquired skill everywhere afterwards.
111 * No, I am not going to explain what the following stuff is.
112 */
113 struct _stack_el {
114 ber_tlv_len_t left; /* What's left to read (or -1) */
115 ber_tlv_len_t got; /* What was actually processed */
116 int cont_level; /* Depth of subcontainment */
117 int want_nulls; /* Want null "end of content" octets? */
118 int bits_chopped; /* Flag in BIT STRING mode */
119 ber_tlv_tag_t tag; /* For debugging purposes */
120 struct _stack_el *prev;
121 struct _stack_el *next;
122 };
123 struct _stack {
124 struct _stack_el *tail;
125 struct _stack_el *cur_ptr;
126 };
127
128 static struct _stack_el *
129 OS__add_stack_el(struct _stack *st) {
130 struct _stack_el *nel;
131
132 /*
133 * Reuse the old stack frame or allocate a new one.
134 */
135 if(st->cur_ptr && st->cur_ptr->next) {
136 nel = st->cur_ptr->next;
137 nel->bits_chopped = 0;
138 nel->got = 0;
139 /* Retain the nel->cont_level, it's correct. */
140 } else {
141 nel = (struct _stack_el *)CALLOC(1, sizeof(struct _stack_el));
142 if(nel == NULL)
143 return NULL;
144
145 if(st->tail) {
146 /* Increase a subcontainment depth */
147 nel->cont_level = st->tail->cont_level + 1;
148 st->tail->next = nel;
149 }
150 nel->prev = st->tail;
151 st->tail = nel;
152 }
153
154 st->cur_ptr = nel;
155
156 return nel;
157 }
158
159 static struct _stack *
160 _new_stack() {
161 return (struct _stack *)CALLOC(1, sizeof(struct _stack));
162 }
163
164 /*
165 * Decode OCTET STRING type.
166 */
167 asn_dec_rval_t
168 OCTET_STRING_decode_ber(asn_codec_ctx_t *opt_codec_ctx,
169 asn_TYPE_descriptor_t *td,
170 void **sptr, const void *buf_ptr, size_t size, int tag_mode) {
171 asn_OCTET_STRING_specifics_t *specs = td->specifics
172 ? (asn_OCTET_STRING_specifics_t *)td->specifics
173 : &asn_DEF_OCTET_STRING_specs;
174 BIT_STRING_t *st = (BIT_STRING_t *)*sptr;
175 asn_dec_rval_t rval;
176 asn_struct_ctx_t *ctx;
177 ssize_t consumed_myself = 0;
178 struct _stack *stck; /* Expectations stack structure */
179 struct _stack_el *sel = 0; /* Stack element */
180 int tlv_constr;
181 enum asn_OS_Subvariant type_variant = specs->subvariant;
182
183 ASN_DEBUG("Decoding %s as %s (frame %ld)",
184 td->name,
185 (type_variant == ASN_OSUBV_STR) ?
186 "OCTET STRING" : "OS-SpecialCase",
187 (long)size);
188
189 /*
190 * Create the string if does not exist.
191 */
192 if(st == NULL) {
193 st = (BIT_STRING_t *)(*sptr = CALLOC(1, specs->struct_size));
194 if(st == NULL) RETURN(RC_FAIL);
195 }
196
197 /* Restore parsing context */
198 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
199
200 switch(ctx->phase) {
201 case 0:
202 /*
203 * Check tags.
204 */
205 rval = ber_check_tags(opt_codec_ctx, td, ctx,
206 buf_ptr, size, tag_mode, -1,
207 &ctx->left, &tlv_constr);
208 if(rval.code != RC_OK)
209 return rval;
210
211 if(tlv_constr) {
212 /*
213 * Complex operation, requires stack of expectations.
214 */
215 ctx->ptr = _new_stack();
216 if(ctx->ptr) {
217 stck = (struct _stack *)ctx->ptr;
218 } else {
219 RETURN(RC_FAIL);
220 }
221 } else {
222 /*
223 * Jump into stackless primitive decoding.
224 */
225 _CH_PHASE(ctx, 3);
226 if(type_variant == ASN_OSUBV_ANY && tag_mode != 1)
227 APPEND(buf_ptr, rval.consumed);
228 ADVANCE(rval.consumed);
229 goto phase3;
230 }
231
232 NEXT_PHASE(ctx);
233 /* Fall through */
234 case 1:
235 phase1:
236 /*
237 * Fill the stack with expectations.
238 */
239 stck = (struct _stack *)ctx->ptr;
240 sel = stck->cur_ptr;
241 do {
242 ber_tlv_tag_t tlv_tag;
243 ber_tlv_len_t tlv_len;
244 ber_tlv_tag_t expected_tag;
245 ssize_t tl, ll, tlvl;
246 /* This one works even if (sel->left == -1) */
247 ssize_t Left = ((!sel||(size_t)sel->left >= size)
248 ?(ssize_t)size:sel->left);
249
250
251 ASN_DEBUG("%p, s->l=%ld, s->wn=%ld, s->g=%ld\n", sel,
252 (long)(sel?sel->left:0),
253 (long)(sel?sel->want_nulls:0),
254 (long)(sel?sel->got:0)
255 );
256 if(sel && sel->left <= 0 && sel->want_nulls == 0) {
257 if(sel->prev) {
258 struct _stack_el *prev = sel->prev;
259 if(prev->left != -1) {
260 if(prev->left < sel->got)
261 RETURN(RC_FAIL);
262 prev->left -= sel->got;
263 }
264 prev->got += sel->got;
265 sel = stck->cur_ptr = prev;
266 if(!sel) break;
267 tlv_constr = 1;
268 continue;
269 } else {
270 sel = stck->cur_ptr = 0;
271 break; /* Nothing to wait */
272 }
273 }
274
275 tl = ber_fetch_tag(buf_ptr, Left, &tlv_tag);
276 ASN_DEBUG("fetch tag(size=%ld,L=%ld), %sstack, left=%ld, wn=%ld, tl=%ld",
277 (long)size, (long)Left, sel?"":"!",
278 (long)(sel?sel->left:0),
279 (long)(sel?sel->want_nulls:0),
280 (long)tl);
281 switch(tl) {
282 case -1: RETURN(RC_FAIL);
283 case 0: RETURN(RC_WMORE);
284 }
285
286 tlv_constr = BER_TLV_CONSTRUCTED(buf_ptr);
287
288 ll = ber_fetch_length(tlv_constr,
289 (const char *)buf_ptr + tl,Left - tl,&tlv_len);
290 ASN_DEBUG("Got tag=%s, tc=%d, left=%ld, tl=%ld, len=%ld, ll=%ld",
291 ber_tlv_tag_string(tlv_tag), tlv_constr,
292 (long)Left, (long)tl, (long)tlv_len, (long)ll);
293 switch(ll) {
294 case -1: RETURN(RC_FAIL);
295 case 0: RETURN(RC_WMORE);
296 }
297
298 if(sel && sel->want_nulls
299 && ((const uint8_t *)buf_ptr)[0] == 0
300 && ((const uint8_t *)buf_ptr)[1] == 0)
301 {
302
303 ASN_DEBUG("Eat EOC; wn=%d--", sel->want_nulls);
304
305 if(type_variant == ASN_OSUBV_ANY
306 && (tag_mode != 1 || sel->cont_level))
307 APPEND("\0\0", 2);
308
309 ADVANCE(2);
310 sel->got += 2;
311 if(sel->left != -1) {
312 sel->left -= 2; /* assert(sel->left >= 2) */
313 }
314
315 sel->want_nulls--;
316 if(sel->want_nulls == 0) {
317 /* Move to the next expectation */
318 sel->left = 0;
319 tlv_constr = 1;
320 }
321
322 continue;
323 }
324
325 /*
326 * Set up expected tags,
327 * depending on ASN.1 type being decoded.
328 */
329 switch(type_variant) {
330 case ASN_OSUBV_BIT:
331 /* X.690: 8.6.4.1, NOTE 2 */
332 /* Fall through */
333 case ASN_OSUBV_STR:
334 default:
335 if(sel) {
336 int level = sel->cont_level;
337 if(level < td->all_tags_count) {
338 expected_tag = td->all_tags[level];
339 break;
340 } else if(td->all_tags_count) {
341 expected_tag = td->all_tags
342 [td->all_tags_count - 1];
343 break;
344 }
345 /* else, Fall through */
346 }
347 /* Fall through */
348 case ASN_OSUBV_ANY:
349 expected_tag = tlv_tag;
350 break;
351 }
352
353
354 if(tlv_tag != expected_tag) {
355 char buf[2][32];
356 ber_tlv_tag_snprint(tlv_tag,
357 buf[0], sizeof(buf[0]));
358 ber_tlv_tag_snprint(td->tags[td->tags_count-1],
359 buf[1], sizeof(buf[1]));
360 ASN_DEBUG("Tag does not match expectation: %s != %s",
361 buf[0], buf[1]);
362 RETURN(RC_FAIL);
363 }
364
365 tlvl = tl + ll; /* Combined length of T and L encoding */
366 if((tlv_len + tlvl) < 0) {
367 /* tlv_len value is too big */
368 ASN_DEBUG("TLV encoding + length (%ld) is too big",
369 (long)tlv_len);
370 RETURN(RC_FAIL);
371 }
372
373 /*
374 * Append a new expectation.
375 */
376 sel = OS__add_stack_el(stck);
377 if(!sel) RETURN(RC_FAIL);
378
379 sel->tag = tlv_tag;
380
381 sel->want_nulls = (tlv_len==-1);
382 if(sel->prev && sel->prev->left != -1) {
383 /* Check that the parent frame is big enough */
384 if(sel->prev->left < tlvl + (tlv_len==-1?0:tlv_len))
385 RETURN(RC_FAIL);
386 if(tlv_len == -1)
387 sel->left = sel->prev->left - tlvl;
388 else
389 sel->left = tlv_len;
390 } else {
391 sel->left = tlv_len;
392 }
393 if(type_variant == ASN_OSUBV_ANY
394 && (tag_mode != 1 || sel->cont_level))
395 APPEND(buf_ptr, tlvl);
396 sel->got += tlvl;
397 ADVANCE(tlvl);
398
399 ASN_DEBUG("+EXPECT2 got=%ld left=%ld, wn=%d, clvl=%d",
400 (long)sel->got, (long)sel->left,
401 sel->want_nulls, sel->cont_level);
402
403 } while(tlv_constr);
404 if(sel == NULL) {
405 /* Finished operation, "phase out" */
406 ASN_DEBUG("Phase out");
407 _CH_PHASE(ctx, +3);
408 break;
409 }
410
411 NEXT_PHASE(ctx);
412 /* Fall through */
413 case 2:
414 stck = (struct _stack *)ctx->ptr;
415 sel = stck->cur_ptr;
416 ASN_DEBUG("Phase 2: Need %ld bytes, size=%ld, alrg=%ld, wn=%d",
417 (long)sel->left, (long)size, (long)sel->got,
418 sel->want_nulls);
419 {
420 ber_tlv_len_t len;
421
422 assert(sel->left >= 0);
423
424 len = ((ber_tlv_len_t)size < sel->left)
425 ? (ber_tlv_len_t)size : sel->left;
426 if(len > 0) {
427 if(type_variant == ASN_OSUBV_BIT
428 && sel->bits_chopped == 0) {
429 /* Put the unused-bits-octet away */
430 st->bits_unused = *(const uint8_t *)buf_ptr;
431 APPEND(((const char *)buf_ptr+1), (len - 1));
432 sel->bits_chopped = 1;
433 } else {
434 APPEND(buf_ptr, len);
435 }
436 ADVANCE(len);
437 sel->left -= len;
438 sel->got += len;
439 }
440
441 if(sel->left) {
442 ASN_DEBUG("OS left %ld, size = %ld, wn=%d\n",
443 (long)sel->left, (long)size, sel->want_nulls);
444 RETURN(RC_WMORE);
445 }
446
447 PREV_PHASE(ctx);
448 goto phase1;
449 }
450 break;
451 case 3:
452 phase3:
453 /*
454 * Primitive form, no stack required.
455 */
456 assert(ctx->left >= 0);
457
458 if(size < (size_t)ctx->left) {
459 if(!size) RETURN(RC_WMORE);
460 if(type_variant == ASN_OSUBV_BIT && !ctx->context) {
461 st->bits_unused = *(const uint8_t *)buf_ptr;
462 ctx->left--;
463 ADVANCE(1);
464 }
465 APPEND(buf_ptr, size);
466 assert(ctx->context > 0);
467 ctx->left -= size;
468 ADVANCE(size);
469 RETURN(RC_WMORE);
470 } else {
471 if(type_variant == ASN_OSUBV_BIT
472 && !ctx->context && ctx->left) {
473 st->bits_unused = *(const uint8_t *)buf_ptr;
474 ctx->left--;
475 ADVANCE(1);
476 }
477 APPEND(buf_ptr, ctx->left);
478 ADVANCE(ctx->left);
479 ctx->left = 0;
480
481 NEXT_PHASE(ctx);
482 }
483 break;
484 }
485
486 if(sel) {
487 ASN_DEBUG("3sel p=%p, wn=%d, l=%ld, g=%ld, size=%ld",
488 sel->prev, sel->want_nulls,
489 (long)sel->left, (long)sel->got, (long)size);
490 if(sel->prev || sel->want_nulls > 1 || sel->left > 0) {
491 RETURN(RC_WMORE);
492 }
493 }
494
495 /*
496 * BIT STRING-specific processing.
497 */
498 if(type_variant == ASN_OSUBV_BIT && st->size) {
499 /* Finalize BIT STRING: zero out unused bits. */
500 st->buf[st->size-1] &= 0xff << st->bits_unused;
501 }
502
503 ASN_DEBUG("Took %ld bytes to encode %s: [%s]:%ld",
504 (long)consumed_myself, td->name,
505 (type_variant == ASN_OSUBV_STR) ? (char *)st->buf : "<data>",
506 (long)st->size);
507
508
509 RETURN(RC_OK);
510 }
511
512 /*
513 * Encode OCTET STRING type using DER.
514 */
515 asn_enc_rval_t
516 OCTET_STRING_encode_der(asn_TYPE_descriptor_t *td, void *sptr,
517 int tag_mode, ber_tlv_tag_t tag,
518 asn_app_consume_bytes_f *cb, void *app_key) {
519 asn_enc_rval_t er;
520 asn_OCTET_STRING_specifics_t *specs = td->specifics
521 ? (asn_OCTET_STRING_specifics_t *)td->specifics
522 : &asn_DEF_OCTET_STRING_specs;
523 BIT_STRING_t *st = (BIT_STRING_t *)sptr;
524 enum asn_OS_Subvariant type_variant = specs->subvariant;
525 int fix_last_byte = 0;
526
527 ASN_DEBUG("%s %s as OCTET STRING",
528 cb?"Estimating":"Encoding", td->name);
529
530 /*
531 * Write tags.
532 */
533 if(type_variant != ASN_OSUBV_ANY || tag_mode == 1) {
534 er.encoded = der_write_tags(td,
535 (type_variant == ASN_OSUBV_BIT) + st->size,
536 tag_mode, type_variant == ASN_OSUBV_ANY, tag,
537 cb, app_key);
538 if(er.encoded == -1) {
539 er.failed_type = td;
540 er.structure_ptr = sptr;
541 return er;
542 }
543 } else {
544 /* Disallow: [<tag>] IMPLICIT ANY */
545 assert(type_variant != ASN_OSUBV_ANY || tag_mode != -1);
546 er.encoded = 0;
547 }
548
549 if(!cb) {
550 er.encoded += (type_variant == ASN_OSUBV_BIT) + st->size;
551 ASN__ENCODED_OK(er);
552 }
553
554 /*
555 * Prepare to deal with the last octet of BIT STRING.
556 */
557 if(type_variant == ASN_OSUBV_BIT) {
558 uint8_t b = st->bits_unused & 0x07;
559 if(b && st->size) fix_last_byte = 1;
560 ASN__CALLBACK(&b, 1);
561 er.encoded++;
562 }
563
564 /* Invoke callback for the main part of the buffer */
565 ASN__CALLBACK(st->buf, st->size - fix_last_byte);
566
567 /* The last octet should be stripped off the unused bits */
568 if(fix_last_byte) {
569 uint8_t b = st->buf[st->size-1] & (0xff << st->bits_unused);
570 ASN__CALLBACK(&b, 1);
571 }
572
573 er.encoded += st->size;
574 ASN__ENCODED_OK(er);
575 cb_failed:
576 ASN__ENCODE_FAILED;
577 }
578
579 asn_enc_rval_t
580 OCTET_STRING_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
581 int ilevel, enum xer_encoder_flags_e flags,
582 asn_app_consume_bytes_f *cb, void *app_key) {
583 const char * const h2c = "0123456789ABCDEF";
584 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
585 asn_enc_rval_t er;
586 char scratch[16 * 3 + 4];
587 char *p = scratch;
588 uint8_t *buf;
589 uint8_t *end;
590 size_t i;
591
592 if(!st || (!st->buf && st->size))
593 ASN__ENCODE_FAILED;
594
595 er.encoded = 0;
596
597 /*
598 * Dump the contents of the buffer in hexadecimal.
599 */
600 buf = st->buf;
601 end = buf + st->size;
602 if(flags & XER_F_CANONICAL) {
603 char *scend = scratch + (sizeof(scratch) - 2);
604 for(; buf < end; buf++) {
605 if(p >= scend) {
606 ASN__CALLBACK(scratch, p - scratch);
607 er.encoded += p - scratch;
608 p = scratch;
609 }
610 *p++ = h2c[(*buf >> 4) & 0x0F];
611 *p++ = h2c[*buf & 0x0F];
612 }
613
614 ASN__CALLBACK(scratch, p-scratch); /* Dump the rest */
615 er.encoded += p - scratch;
616 } else {
617 for(i = 0; buf < end; buf++, i++) {
618 if(!(i % 16) && (i || st->size > 16)) {
619 ASN__CALLBACK(scratch, p-scratch);
620 er.encoded += (p-scratch);
621 p = scratch;
622 ASN__TEXT_INDENT(1, ilevel);
623 }
624 *p++ = h2c[(*buf >> 4) & 0x0F];
625 *p++ = h2c[*buf & 0x0F];
626 *p++ = 0x20;
627 }
628 if(p - scratch) {
629 p--; /* Remove the tail space */
630 ASN__CALLBACK(scratch, p-scratch); /* Dump the rest */
631 er.encoded += p - scratch;
632 if(st->size > 16)
633 ASN__TEXT_INDENT(1, ilevel-1);
634 }
635 }
636
637 ASN__ENCODED_OK(er);
638 cb_failed:
639 ASN__ENCODE_FAILED;
640 }
641
642 static const struct OCTET_STRING__xer_escape_table_s {
643 const char *string;
644 int size;
645 } OCTET_STRING__xer_escape_table[] = {
646 #define OSXET(s) { s, sizeof(s) - 1 }
647 OSXET("\074\156\165\154\057\076"), /* <nul/> */
648 OSXET("\074\163\157\150\057\076"), /* <soh/> */
649 OSXET("\074\163\164\170\057\076"), /* <stx/> */
650 OSXET("\074\145\164\170\057\076"), /* <etx/> */
651 OSXET("\074\145\157\164\057\076"), /* <eot/> */
652 OSXET("\074\145\156\161\057\076"), /* <enq/> */
653 OSXET("\074\141\143\153\057\076"), /* <ack/> */
654 OSXET("\074\142\145\154\057\076"), /* <bel/> */
655 OSXET("\074\142\163\057\076"), /* <bs/> */
656 OSXET("\011"), /* \t */
657 OSXET("\012"), /* \n */
658 OSXET("\074\166\164\057\076"), /* <vt/> */
659 OSXET("\074\146\146\057\076"), /* <ff/> */
660 OSXET("\015"), /* \r */
661 OSXET("\074\163\157\057\076"), /* <so/> */
662 OSXET("\074\163\151\057\076"), /* <si/> */
663 OSXET("\074\144\154\145\057\076"), /* <dle/> */
664 OSXET("\074\144\143\061\057\076"), /* <de1/> */
665 OSXET("\074\144\143\062\057\076"), /* <de2/> */
666 OSXET("\074\144\143\063\057\076"), /* <de3/> */
667 OSXET("\074\144\143\064\057\076"), /* <de4/> */
668 OSXET("\074\156\141\153\057\076"), /* <nak/> */
669 OSXET("\074\163\171\156\057\076"), /* <syn/> */
670 OSXET("\074\145\164\142\057\076"), /* <etb/> */
671 OSXET("\074\143\141\156\057\076"), /* <can/> */
672 OSXET("\074\145\155\057\076"), /* <em/> */
673 OSXET("\074\163\165\142\057\076"), /* <sub/> */
674 OSXET("\074\145\163\143\057\076"), /* <esc/> */
675 OSXET("\074\151\163\064\057\076"), /* <is4/> */
676 OSXET("\074\151\163\063\057\076"), /* <is3/> */
677 OSXET("\074\151\163\062\057\076"), /* <is2/> */
678 OSXET("\074\151\163\061\057\076"), /* <is1/> */
679 { 0, 0 }, /* " " */
680 { 0, 0 }, /* ! */
681 { 0, 0 }, /* \" */
682 { 0, 0 }, /* # */
683 { 0, 0 }, /* $ */
684 { 0, 0 }, /* % */
685 OSXET("\046\141\155\160\073"), /* &amp; */
686 { 0, 0 }, /* ' */
687 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, /* ()*+,-./ */
688 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, /* 01234567 */
689 {0,0},{0,0},{0,0},{0,0}, /* 89:; */
690 OSXET("\046\154\164\073"), /* &lt; */
691 { 0, 0 }, /* = */
692 OSXET("\046\147\164\073"), /* &gt; */
693 };
694
695 static int
696 OS__check_escaped_control_char(const void *buf, int size) {
697 size_t i;
698 /*
699 * Inefficient algorithm which translates the escape sequences
700 * defined above into characters. Returns -1 if not found.
701 * TODO: replace by a faster algorithm (bsearch(), hash or
702 * nested table lookups).
703 */
704 for(i = 0; i < 32 /* Don't spend time on the bottom half */; i++) {
705 const struct OCTET_STRING__xer_escape_table_s *el;
706 el = &OCTET_STRING__xer_escape_table[i];
707 if(el->size == size && memcmp(buf, el->string, size) == 0)
708 return i;
709 }
710 return -1;
711 }
712
713 static int
714 OCTET_STRING__handle_control_chars(void *struct_ptr, const void *chunk_buf, size_t chunk_size) {
715 /*
716 * This might be one of the escape sequences
717 * for control characters. Check it out.
718 * #11.15.5
719 */
720 int control_char = OS__check_escaped_control_char(chunk_buf,chunk_size);
721 if(control_char >= 0) {
722 OCTET_STRING_t *st = (OCTET_STRING_t *)struct_ptr;
723 void *p = REALLOC(st->buf, st->size + 2);
724 if(p) {
725 st->buf = (uint8_t *)p;
726 st->buf[st->size++] = control_char;
727 st->buf[st->size] = '\0'; /* nul-termination */
728 return 0;
729 }
730 }
731
732 return -1; /* No, it's not */
733 }
734
735 asn_enc_rval_t
736 OCTET_STRING_encode_xer_utf8(asn_TYPE_descriptor_t *td, void *sptr,
737 int ilevel, enum xer_encoder_flags_e flags,
738 asn_app_consume_bytes_f *cb, void *app_key) {
739 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
740 asn_enc_rval_t er;
741 uint8_t *buf, *end;
742 uint8_t *ss; /* Sequence start */
743 ssize_t encoded_len = 0;
744
745 (void)ilevel; /* Unused argument */
746 (void)flags; /* Unused argument */
747
748 if(!st || (!st->buf && st->size))
749 ASN__ENCODE_FAILED;
750
751 buf = st->buf;
752 end = buf + st->size;
753 for(ss = buf; buf < end; buf++) {
754 unsigned int ch = *buf;
755 int s_len; /* Special encoding sequence length */
756
757 /*
758 * Escape certain characters: X.680/11.15
759 */
760 if(ch < sizeof(OCTET_STRING__xer_escape_table)
761 /sizeof(OCTET_STRING__xer_escape_table[0])
762 && (s_len = OCTET_STRING__xer_escape_table[ch].size)) {
763 if(((buf - ss) && cb(ss, buf - ss, app_key) < 0)
764 || cb(OCTET_STRING__xer_escape_table[ch].string, s_len,
765 app_key) < 0)
766 ASN__ENCODE_FAILED;
767 encoded_len += (buf - ss) + s_len;
768 ss = buf + 1;
769 }
770 }
771
772 encoded_len += (buf - ss);
773 if((buf - ss) && cb(ss, buf - ss, app_key) < 0)
774 ASN__ENCODE_FAILED;
775
776 er.encoded = encoded_len;
777 ASN__ENCODED_OK(er);
778 }
779
780 /*
781 * Convert from hexadecimal format (cstring): "AB CD EF"
782 */
783 static ssize_t OCTET_STRING__convert_hexadecimal(void *sptr, const void *chunk_buf, size_t chunk_size, int have_more) {
784 OCTET_STRING_t *st = (OCTET_STRING_t *)sptr;
785 const char *chunk_stop = (const char *)chunk_buf;
786 const char *p = chunk_stop;
787 const char *pend = p + chunk_size;
788 unsigned int clv = 0;
789 int half = 0; /* Half bit */
790 uint8_t *buf;
791
792 /* Reallocate buffer according to high cap estimation */
793 ssize_t _ns = st->size + (chunk_size + 1) / 2;
794 void *nptr = REALLOC(st->buf, _ns + 1);
795 if(!nptr) return -1;
796 st->buf = (uint8_t *)nptr;
797 buf = st->buf + st->size;
798
799 /*
800 * If something like " a b c " appears here, the " a b":3 will be
801 * converted, and the rest skipped. That is, unless buf_size is greater
802 * than chunk_size, then it'll be equivalent to "ABC0".
803 */
804 for(; p < pend; p++) {
805 int ch = *(const unsigned char *)p;
806 switch(ch) {
807 case 0x09: case 0x0a: case 0x0c: case 0x0d:
808 case 0x20:
809 /* Ignore whitespace */
810 continue;
811 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: /*01234*/
812 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: /*56789*/
813 clv = (clv << 4) + (ch - 0x30);
814 break;
815 case 0x41: case 0x42: case 0x43: /* ABC */
816 case 0x44: case 0x45: case 0x46: /* DEF */
817 clv = (clv << 4) + (ch - 0x41 + 10);
818 break;
819 case 0x61: case 0x62: case 0x63: /* abc */
820 case 0x64: case 0x65: case 0x66: /* def */
821 clv = (clv << 4) + (ch - 0x61 + 10);
822 break;
823 default:
824 *buf = 0; /* JIC */
825 return -1;
826 }
827 if(half++) {
828 half = 0;
829 *buf++ = clv;
830 chunk_stop = p + 1;
831 }
832 }
833
834 /*
835 * Check partial decoding.
836 */
837 if(half) {
838 if(have_more) {
839 /*
840 * Partial specification is fine,
841 * because no more more PXER_TEXT data is available.
842 */
843 *buf++ = clv << 4;
844 chunk_stop = p;
845 }
846 } else {
847 chunk_stop = p;
848 }
849
850 st->size = buf - st->buf; /* Adjust the buffer size */
851 assert(st->size <= _ns);
852 st->buf[st->size] = 0; /* Courtesy termination */
853
854 return (chunk_stop - (const char *)chunk_buf); /* Converted size */
855 }
856
857 /*
858 * Convert from binary format: "00101011101"
859 */
860 static ssize_t OCTET_STRING__convert_binary(void *sptr, const void *chunk_buf, size_t chunk_size, int have_more) {
861 BIT_STRING_t *st = (BIT_STRING_t *)sptr;
862 const char *p = (const char *)chunk_buf;
863 const char *pend = p + chunk_size;
864 int bits_unused = st->bits_unused & 0x7;
865 uint8_t *buf;
866
867 /* Reallocate buffer according to high cap estimation */
868 ssize_t _ns = st->size + (chunk_size + 7) / 8;
869 void *nptr = REALLOC(st->buf, _ns + 1);
870 if(!nptr) return -1;
871 st->buf = (uint8_t *)nptr;
872 buf = st->buf + st->size;
873
874 (void)have_more;
875
876 if(bits_unused == 0)
877 bits_unused = 8;
878 else if(st->size)
879 buf--;
880
881 /*
882 * Convert series of 0 and 1 into the octet string.
883 */
884 for(; p < pend; p++) {
885 int ch = *(const unsigned char *)p;
886 switch(ch) {
887 case 0x09: case 0x0a: case 0x0c: case 0x0d:
888 case 0x20:
889 /* Ignore whitespace */
890 break;
891 case 0x30:
892 case 0x31:
893 if(bits_unused-- <= 0) {
894 *++buf = 0; /* Clean the cell */
895 bits_unused = 7;
896 }
897 *buf |= (ch&1) << bits_unused;
898 break;
899 default:
900 st->bits_unused = bits_unused;
901 return -1;
902 }
903 }
904
905 if(bits_unused == 8) {
906 st->size = buf - st->buf;
907 st->bits_unused = 0;
908 } else {
909 st->size = buf - st->buf + 1;
910 st->bits_unused = bits_unused;
911 }
912
913 assert(st->size <= _ns);
914 st->buf[st->size] = 0; /* Courtesy termination */
915
916 return chunk_size; /* Converted in full */
917 }
918
919 /*
920 * Something like strtod(), but with stricter rules.
921 */
922 static int
923 OS__strtoent(int base, const char *buf, const char *end, int32_t *ret_value) {
•924 const int32_t last_unicode_codepoint = 0x10ffff;
925 int32_t val = 0;
926 const char *p;
927
928 for(p = buf; p < end; p++) {
929 int ch = *p;
930
931 switch(ch) {
932 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: /*01234*/
933 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: /*56789*/
934 val = val * base + (ch - 0x30);
935 break;
936 case 0x41: case 0x42: case 0x43: /* ABC */
937 case 0x44: case 0x45: case 0x46: /* DEF */
938 val = val * base + (ch - 0x41 + 10);
939 break;
940 case 0x61: case 0x62: case 0x63: /* abc */
941 case 0x64: case 0x65: case 0x66: /* def */
942 val = val * base + (ch - 0x61 + 10);
943 break;
944 case 0x3b: /* ';' */
945 *ret_value = val;
946 return (p - buf) + 1;
947 default:
948 return -1; /* Character set error */
949 }
•950
951 /* Value exceeds the Unicode range. */
952 if(val > last_unicode_codepoint) {
953 return -1;
954 }
955 }
956
957 *ret_value = -1;
958 return (p - buf);
959 }
960
961 /*
962 * Convert from the plain UTF-8 format, expanding entity references: "2 &lt; 3"
963 */
964 static ssize_t OCTET_STRING__convert_entrefs(void *sptr, const void *chunk_buf, size_t chunk_size, int have_more) {
965 OCTET_STRING_t *st = (OCTET_STRING_t *)sptr;
966 const char *p = (const char *)chunk_buf;
967 const char *pend = p + chunk_size;
968 uint8_t *buf;
969
970 /* Reallocate buffer */
971 ssize_t _ns = st->size + chunk_size;
972 void *nptr = REALLOC(st->buf, _ns + 1);
973 if(!nptr) return -1;
974 st->buf = (uint8_t *)nptr;
975 buf = st->buf + st->size;
976
977 /*
978 * Convert series of 0 and 1 into the octet string.
979 */
980 for(; p < pend; p++) {
981 int ch = *(const unsigned char *)p;
982 int len; /* Length of the rest of the chunk */
983
984 if(ch != 0x26 /* '&' */) {
985 *buf++ = ch;
986 continue; /* That was easy... */
987 }
988
989 /*
990 * Process entity reference.
991 */
992 len = chunk_size - (p - (const char *)chunk_buf);
993 if(len == 1 /* "&" */) goto want_more;
994 if(p[1] == 0x23 /* '#' */) {
995 const char *pval; /* Pointer to start of digits */
996 int32_t val = 0; /* Entity reference value */
997 int base;
998
999 if(len == 2 /* "&#" */) goto want_more;
1000 if(p[2] == 0x78 /* 'x' */)
1001 pval = p + 3, base = 16;
1002 else
1003 pval = p + 2, base = 10;
1004 len = OS__strtoent(base, pval, p + len, &val);
1005 if(len == -1) {
1006 /* Invalid charset. Just copy verbatim. */
1007 *buf++ = ch;
1008 continue;
1009 }
1010 if(!len || pval[len-1] != 0x3b) goto want_more;
1011 assert(val > 0);
1012 p += (pval - p) + len - 1; /* Advance past entref */
1013
1014 if(val < 0x80) {
1015 *buf++ = (char)val;
1016 } else if(val < 0x800) {
1017 *buf++ = 0xc0 | ((val >> 6));
1018 *buf++ = 0x80 | ((val & 0x3f));
1019 } else if(val < 0x10000) {
1020 *buf++ = 0xe0 | ((val >> 12));
1021 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1022 *buf++ = 0x80 | ((val & 0x3f));
1023 } else if(val < 0x200000) {
1024 *buf++ = 0xf0 | ((val >> 18));
1025 *buf++ = 0x80 | ((val >> 12) & 0x3f);
1026 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1027 *buf++ = 0x80 | ((val & 0x3f));
1028 } else if(val < 0x4000000) {
1029 *buf++ = 0xf8 | ((val >> 24));
1030 *buf++ = 0x80 | ((val >> 18) & 0x3f);
1031 *buf++ = 0x80 | ((val >> 12) & 0x3f);
1032 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1033 *buf++ = 0x80 | ((val & 0x3f));
1034 } else {
1035 *buf++ = 0xfc | ((val >> 30) & 0x1);
1036 *buf++ = 0x80 | ((val >> 24) & 0x3f);
1037 *buf++ = 0x80 | ((val >> 18) & 0x3f);
1038 *buf++ = 0x80 | ((val >> 12) & 0x3f);
1039 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1040 *buf++ = 0x80 | ((val & 0x3f));
1041 }
1042 } else {
1043 /*
1044 * Ugly, limited parsing of &amp; &gt; &lt;
1045 */
1046 char *sc = (char *)memchr(p, 0x3b, len > 5 ? 5 : len);
1047 if(!sc) goto want_more;
1048 if((sc - p) == 4
1049 && p[1] == 0x61 /* 'a' */
1050 && p[2] == 0x6d /* 'm' */
1051 && p[3] == 0x70 /* 'p' */) {
1052 *buf++ = 0x26;
1053 p = sc;
1054 continue;
1055 }
1056 if((sc - p) == 3) {
1057 if(p[1] == 0x6c) {
1058 *buf = 0x3c; /* '<' */
1059 } else if(p[1] == 0x67) {
1060 *buf = 0x3e; /* '>' */
1061 } else {
1062 /* Unsupported entity reference */
1063 *buf++ = ch;
1064 continue;
1065 }
1066 if(p[2] != 0x74) {
1067 /* Unsupported entity reference */
1068 *buf++ = ch;
1069 continue;
1070 }
1071 buf++;
1072 p = sc;
1073 continue;
1074 }
1075 /* Unsupported entity reference */
1076 *buf++ = ch;
1077 }
1078
1079 continue;
1080 want_more:
1081 if(have_more) {
1082 /*
1083 * We know that no more data (of the same type)
1084 * is coming. Copy the rest verbatim.
1085 */
1086 *buf++ = ch;
1087 continue;
1088 }
1089 chunk_size = (p - (const char *)chunk_buf);
1090 /* Processing stalled: need more data */
1091 break;
1092 }
1093
1094 st->size = buf - st->buf;
1095 assert(st->size <= _ns);
1096 st->buf[st->size] = 0; /* Courtesy termination */
1097
1098 return chunk_size; /* Converted in full */
1099 }
1100
1101 /*
1102 * Decode OCTET STRING from the XML element's body.
1103 */
1104 static asn_dec_rval_t
1105 OCTET_STRING__decode_xer(asn_codec_ctx_t *opt_codec_ctx,
1106 asn_TYPE_descriptor_t *td, void **sptr,
1107 const char *opt_mname, const void *buf_ptr, size_t size,
1108 int (*opt_unexpected_tag_decoder)
1109 (void *struct_ptr, const void *chunk_buf, size_t chunk_size),
1110 ssize_t (*body_receiver)
1111 (void *struct_ptr, const void *chunk_buf, size_t chunk_size,
1112 int have_more)
1113 ) {
1114 OCTET_STRING_t *st = (OCTET_STRING_t *)*sptr;
1115 asn_OCTET_STRING_specifics_t *specs = td->specifics
1116 ? (asn_OCTET_STRING_specifics_t *)td->specifics
1117 : &asn_DEF_OCTET_STRING_specs;
1118 const char *xml_tag = opt_mname ? opt_mname : td->xml_tag;
1119 asn_struct_ctx_t *ctx; /* Per-structure parser context */
1120 asn_dec_rval_t rval; /* Return value from the decoder */
1121 int st_allocated;
1122
1123 /*
1124 * Create the string if does not exist.
1125 */
1126 if(!st) {
1127 st = (OCTET_STRING_t *)CALLOC(1, specs->struct_size);
1128 *sptr = (void *)st;
1129 if(!st) goto sta_failed;
1130 st_allocated = 1;
1131 } else {
1132 st_allocated = 0;
1133 }
1134 if(!st->buf) {
1135 /* This is separate from above section */
1136 st->buf = (uint8_t *)CALLOC(1, 1);
1137 if(!st->buf) {
1138 if(st_allocated) {
1139 *sptr = 0;
1140 goto stb_failed;
1141 } else {
1142 goto sta_failed;
1143 }
1144 }
1145 }
1146
1147 /* Restore parsing context */
1148 ctx = (asn_struct_ctx_t *)(((char *)*sptr) + specs->ctx_offset);
1149
1150 return xer_decode_general(opt_codec_ctx, ctx, *sptr, xml_tag,
1151 buf_ptr, size, opt_unexpected_tag_decoder, body_receiver);
1152
1153 stb_failed:
1154 FREEMEM(st);
1155 sta_failed:
1156 rval.code = RC_FAIL;
1157 rval.consumed = 0;
1158 return rval;
1159 }
1160
1161 /*
1162 * Decode OCTET STRING from the hexadecimal data.
1163 */
1164 asn_dec_rval_t
1165 OCTET_STRING_decode_xer_hex(asn_codec_ctx_t *opt_codec_ctx,
1166 asn_TYPE_descriptor_t *td, void **sptr,
1167 const char *opt_mname, const void *buf_ptr, size_t size) {
1168 return OCTET_STRING__decode_xer(opt_codec_ctx, td, sptr, opt_mname,
1169 buf_ptr, size, 0, OCTET_STRING__convert_hexadecimal);
1170 }
1171
1172 /*
1173 * Decode OCTET STRING from the binary (0/1) data.
1174 */
1175 asn_dec_rval_t
1176 OCTET_STRING_decode_xer_binary(asn_codec_ctx_t *opt_codec_ctx,
1177 asn_TYPE_descriptor_t *td, void **sptr,
1178 const char *opt_mname, const void *buf_ptr, size_t size) {
1179 return OCTET_STRING__decode_xer(opt_codec_ctx, td, sptr, opt_mname,
1180 buf_ptr, size, 0, OCTET_STRING__convert_binary);
1181 }
1182
1183 /*
1184 * Decode OCTET STRING from the string (ASCII/UTF-8) data.
1185 */
1186 asn_dec_rval_t
1187 OCTET_STRING_decode_xer_utf8(asn_codec_ctx_t *opt_codec_ctx,
1188 asn_TYPE_descriptor_t *td, void **sptr,
1189 const char *opt_mname, const void *buf_ptr, size_t size) {
1190 return OCTET_STRING__decode_xer(opt_codec_ctx, td, sptr, opt_mname,
1191 buf_ptr, size,
1192 OCTET_STRING__handle_control_chars,
1193 OCTET_STRING__convert_entrefs);
1194 }
1195
1196 static int
1197 OCTET_STRING_per_get_characters(asn_per_data_t *po, uint8_t *buf,
1198 size_t units, unsigned int bpc, unsigned int unit_bits,
1199 long lb, long ub, asn_per_constraints_t *pc) {
1200 uint8_t *end = buf + units * bpc;
1201
1202 ASN_DEBUG("Expanding %d characters into (%ld..%ld):%d",
1203 (int)units, lb, ub, unit_bits);
1204
1205 /* X.691: 27.5.4 */
1206 if((unsigned long)ub <= ((unsigned long)2 << (unit_bits - 1))) {
1207 /* Decode without translation */
1208 lb = 0;
1209 } else if(pc && pc->code2value) {
1210 if(unit_bits > 16)
1211 return 1; /* FATAL: can't have constrained
1212 * UniversalString with more than
1213 * 16 million code points */
1214 for(; buf < end; buf += bpc) {
1215 int value;
1216 int code = per_get_few_bits(po, unit_bits);
1217 if(code < 0) return -1; /* WMORE */
1218 value = pc->code2value(code);
1219 if(value < 0) {
1220 ASN_DEBUG("Code %d (0x%02x) is"
1221 " not in map (%ld..%ld)",
1222 code, code, lb, ub);
1223 return 1; /* FATAL */
1224 }
1225 switch(bpc) {
1226 case 1: *buf = value; break;
1227 case 2: buf[0] = value >> 8; buf[1] = value; break;
1228 case 4: buf[0] = value >> 24; buf[1] = value >> 16;
1229 buf[2] = value >> 8; buf[3] = value; break;
1230 }
1231 }
1232 return 0;
1233 }
1234
1235 /* Shortcut the no-op copying to the aligned structure */
1236 if(lb == 0 && (unit_bits == 8 * bpc)) {
1237 return per_get_many_bits(po, buf, 0, unit_bits * units);
1238 }
1239
1240 for(; buf < end; buf += bpc) {
1241 int code = per_get_few_bits(po, unit_bits);
1242 int ch = code + lb;
1243 if(code < 0) return -1; /* WMORE */
1244 if(ch > ub) {
1245 ASN_DEBUG("Code %d is out of range (%ld..%ld)",
1246 ch, lb, ub);
1247 return 1; /* FATAL */
1248 }
1249 switch(bpc) {
1250 case 1: *buf = ch; break;
1251 case 2: buf[0] = ch >> 8; buf[1] = ch; break;
1252 case 4: buf[0] = ch >> 24; buf[1] = ch >> 16;
1253 buf[2] = ch >> 8; buf[3] = ch; break;
1254 }
1255 }
1256
1257 return 0;
1258 }
1259
1260 static int
1261 OCTET_STRING_per_put_characters(asn_per_outp_t *po, const uint8_t *buf,
1262 size_t units, unsigned int bpc, unsigned int unit_bits,
1263 long lb, long ub, asn_per_constraints_t *pc) {
1264 const uint8_t *end = buf + units * bpc;
1265
1266 ASN_DEBUG("Squeezing %d characters into (%ld..%ld):%d (%d bpc)",
1267 (int)units, lb, ub, unit_bits, bpc);
1268
1269 /* X.691: 27.5.4 */
1270 if((unsigned long)ub <= ((unsigned long)2 << (unit_bits - 1))) {
1271 /* Encode as is */
1272 lb = 0;
1273 } else if(pc && pc->value2code) {
1274 for(; buf < end; buf += bpc) {
1275 int code;
1276 uint32_t value;
1277 switch(bpc) {
1278 case 1: value = *(const uint8_t *)buf; break;
1279 case 2: value = (buf[0] << 8) | buf[1]; break;
1280 case 4: value = (buf[0] << 24) | (buf[1] << 16)
1281 | (buf[2] << 8) | buf[3]; break;
1282 default: return -1;
1283 }
1284 code = pc->value2code(value);
1285 if(code < 0) {
1286 ASN_DEBUG("Character %d (0x%02x) is"
1287 " not in map (%ld..%ld)",
1288 *buf, *buf, lb, ub);
1289 return -1;
1290 }
1291 if(per_put_few_bits(po, code, unit_bits))
1292 return -1;
1293 }
1294 }
1295
1296 /* Shortcut the no-op copying to the aligned structure */
1297 if(lb == 0 && (unit_bits == 8 * bpc)) {
1298 return per_put_many_bits(po, buf, unit_bits * units);
1299 }
1300
1301 for(ub -= lb; buf < end; buf += bpc) {
1302 int ch;
1303 uint32_t value;
1304 switch(bpc) {
1305 case 1: value = *(const uint8_t *)buf; break;
1306 case 2: value = (buf[0] << 8) | buf[1]; break;
1307 case 4: value = (buf[0] << 24) | (buf[1] << 16)
1308 | (buf[2] << 8) | buf[3]; break;
1309 default: return -1;
1310 }
1311 ch = value - lb;
1312 if(ch < 0 || ch > ub) {
1313 ASN_DEBUG("Character %d (0x%02x)"
1314 " is out of range (%ld..%ld)",
1315 *buf, *buf, lb, ub + lb);
1316 return -1;
1317 }
1318 if(per_put_few_bits(po, ch, unit_bits))
1319 return -1;
1320 }
1321
1322 return 0;
1323 }
1324
1325 asn_dec_rval_t
1326 OCTET_STRING_decode_uper(asn_codec_ctx_t *opt_codec_ctx,
1327 asn_TYPE_descriptor_t *td, asn_per_constraints_t *constraints,
1328 void **sptr, asn_per_data_t *pd) {
1329
1330 asn_OCTET_STRING_specifics_t *specs = td->specifics
1331 ? (asn_OCTET_STRING_specifics_t *)td->specifics
1332 : &asn_DEF_OCTET_STRING_specs;
1333 asn_per_constraints_t *pc = constraints ? constraints
1334 : td->per_constraints;
1335 asn_per_constraint_t *cval;
1336 asn_per_constraint_t *csiz;
1337 asn_dec_rval_t rval = { RC_OK, 0 };
1338 BIT_STRING_t *st = (BIT_STRING_t *)*sptr;
1339 ssize_t consumed_myself = 0;
1340 int repeat;
1341 enum {
1342 OS__BPC_BIT = 0,
1343 OS__BPC_CHAR = 1,
1344 OS__BPC_U16 = 2,
1345 OS__BPC_U32 = 4
1346 } bpc; /* Bytes per character */
1347 unsigned int unit_bits;
1348 unsigned int canonical_unit_bits;
1349
1350 (void)opt_codec_ctx;
1351
1352 if(pc) {
1353 cval = &pc->value;
1354 csiz = &pc->size;
1355 } else {
1356 cval = &asn_DEF_OCTET_STRING_constraints.value;
1357 csiz = &asn_DEF_OCTET_STRING_constraints.size;
1358 }
1359
1360 switch(specs->subvariant) {
1361 default:
1362 case ASN_OSUBV_ANY:
1363 ASN_DEBUG("Unrecognized subvariant %d", specs->subvariant);
1364 RETURN(RC_FAIL);
1365 case ASN_OSUBV_BIT:
1366 canonical_unit_bits = unit_bits = 1;
1367 bpc = OS__BPC_BIT;
1368 break;
1369 case ASN_OSUBV_STR:
1370 canonical_unit_bits = unit_bits = 8;
1371 if(cval->flags & APC_CONSTRAINED)
1372 unit_bits = cval->range_bits;
1373 bpc = OS__BPC_CHAR;
1374 break;
1375 case ASN_OSUBV_U16:
1376 canonical_unit_bits = unit_bits = 16;
1377 if(cval->flags & APC_CONSTRAINED)
1378 unit_bits = cval->range_bits;
1379 bpc = OS__BPC_U16;
1380 break;
1381 case ASN_OSUBV_U32:
1382 canonical_unit_bits = unit_bits = 32;
1383 if(cval->flags & APC_CONSTRAINED)
1384 unit_bits = cval->range_bits;
1385 bpc = OS__BPC_U32;
1386 break;
1387 }
1388
1389 /*
1390 * Allocate the string.
1391 */
1392 if(!st) {
1393 st = (BIT_STRING_t *)(*sptr = CALLOC(1, specs->struct_size));
1394 if(!st) RETURN(RC_FAIL);
1395 }
1396
1397 ASN_DEBUG("PER Decoding %s size %ld .. %ld bits %d",
1398 csiz->flags & APC_EXTENSIBLE ? "extensible" : "non-extensible",
1399 csiz->lower_bound, csiz->upper_bound, csiz->effective_bits);
1400
1401 if(csiz->flags & APC_EXTENSIBLE) {
1402 int inext = per_get_few_bits(pd, 1);
1403 if(inext < 0) RETURN(RC_WMORE);
1404 if(inext) {
1405 csiz = &asn_DEF_OCTET_STRING_constraints.size;
1406 cval = &asn_DEF_OCTET_STRING_constraints.value;
1407 unit_bits = canonical_unit_bits;
1408 }
1409 }
1410
1411 if(csiz->effective_bits >= 0) {
1412 FREEMEM(st->buf);
1413 if(bpc) {
1414 st->size = csiz->upper_bound * bpc;
1415 } else {
1416 st->size = (csiz->upper_bound + 7) >> 3;
1417 }
1418 st->buf = (uint8_t *)MALLOC(st->size + 1);
1419 if(!st->buf) { st->size = 0; RETURN(RC_FAIL); }
1420 }
1421
1422 /* X.691, #16.5: zero-length encoding */
1423 /* X.691, #16.6: short fixed length encoding (up to 2 octets) */
1424 /* X.691, #16.7: long fixed length encoding (up to 64K octets) */
1425 if(csiz->effective_bits == 0) {
1426 int ret;
1427 if(bpc) {
1428 ASN_DEBUG("Encoding OCTET STRING size %ld",
1429 csiz->upper_bound);
1430 ret = OCTET_STRING_per_get_characters(pd, st->buf,
1431 csiz->upper_bound, bpc, unit_bits,
1432 cval->lower_bound, cval->upper_bound, pc);
1433 if(ret > 0) RETURN(RC_FAIL);
1434 } else {
1435 ASN_DEBUG("Encoding BIT STRING size %ld",
1436 csiz->upper_bound);
1437 ret = per_get_many_bits(pd, st->buf, 0,
1438 unit_bits * csiz->upper_bound);
1439 }
1440 if(ret < 0) RETURN(RC_WMORE);
1441 consumed_myself += unit_bits * csiz->upper_bound;
1442 st->buf[st->size] = 0;
1443 if(bpc == 0) {
1444 int ubs = (csiz->upper_bound & 0x7);
1445 st->bits_unused = ubs ? 8 - ubs : 0;
1446 }
1447 RETURN(RC_OK);
1448 }
1449
1450 st->size = 0;
1451 do {
1452 ssize_t raw_len;
1453 ssize_t len_bytes;
1454 ssize_t len_bits;
1455 void *p;
1456 int ret;
1457
1458 /* Get the PER length */
1459 raw_len = uper_get_length(pd, csiz->effective_bits, &repeat);
1460 if(raw_len < 0) RETURN(RC_WMORE);
1461 raw_len += csiz->lower_bound;
1462
1463 ASN_DEBUG("Got PER length eb %ld, len %ld, %s (%s)",
1464 (long)csiz->effective_bits, (long)raw_len,
1465 repeat ? "repeat" : "once", td->name);
1466 if(bpc) {
1467 len_bytes = raw_len * bpc;
1468 len_bits = len_bytes * unit_bits;
1469 } else {
1470 len_bits = raw_len;
1471 len_bytes = (len_bits + 7) >> 3;
1472 if(len_bits & 0x7)
1473 st->bits_unused = 8 - (len_bits & 0x7);
1474 /* len_bits be multiple of 16K if repeat is set */
1475 }
1476 p = REALLOC(st->buf, st->size + len_bytes + 1);
1477 if(!p) RETURN(RC_FAIL);
1478 st->buf = (uint8_t *)p;
1479
1480 if(bpc) {
1481 ret = OCTET_STRING_per_get_characters(pd,
1482 &st->buf[st->size], raw_len, bpc, unit_bits,
1483 cval->lower_bound, cval->upper_bound, pc);
1484 if(ret > 0) RETURN(RC_FAIL);
1485 } else {
1486 ret = per_get_many_bits(pd, &st->buf[st->size],
1487 0, len_bits);
1488 }
1489 if(ret < 0) RETURN(RC_WMORE);
1490 st->size += len_bytes;
1491 } while(repeat);
1492 st->buf[st->size] = 0; /* nul-terminate */
1493
1494 return rval;
1495 }
1496
1497 asn_enc_rval_t
1498 OCTET_STRING_encode_uper(asn_TYPE_descriptor_t *td,
1499 asn_per_constraints_t *constraints, void *sptr, asn_per_outp_t *po) {
1500
1501 asn_OCTET_STRING_specifics_t *specs = td->specifics
1502 ? (asn_OCTET_STRING_specifics_t *)td->specifics
1503 : &asn_DEF_OCTET_STRING_specs;
1504 asn_per_constraints_t *pc = constraints ? constraints
1505 : td->per_constraints;
1506 asn_per_constraint_t *cval;
1507 asn_per_constraint_t *csiz;
1508 const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
1509 asn_enc_rval_t er = { 0, 0, 0 };
1510 int inext = 0; /* Lies not within extension root */
1511 unsigned int unit_bits;
1512 unsigned int canonical_unit_bits;
1513 unsigned int sizeinunits;
1514 const uint8_t *buf;
1515 int ret;
1516 enum {
1517 OS__BPC_BIT = 0,
1518 OS__BPC_CHAR = 1,
1519 OS__BPC_U16 = 2,
1520 OS__BPC_U32 = 4
1521 } bpc; /* Bytes per character */
1522 int ct_extensible;
1523
1524 if(!st || (!st->buf && st->size))
1525 ASN__ENCODE_FAILED;
1526
1527 if(pc) {
1528 cval = &pc->value;
1529 csiz = &pc->size;
1530 } else {
1531 cval = &asn_DEF_OCTET_STRING_constraints.value;
1532 csiz = &asn_DEF_OCTET_STRING_constraints.size;
1533 }
1534 ct_extensible = csiz->flags & APC_EXTENSIBLE;
1535
1536 switch(specs->subvariant) {
1537 default:
1538 case ASN_OSUBV_ANY:
1539 ASN__ENCODE_FAILED;
1540 case ASN_OSUBV_BIT:
1541 canonical_unit_bits = unit_bits = 1;
1542 bpc = OS__BPC_BIT;
1543 sizeinunits = st->size * 8 - (st->bits_unused & 0x07);
1544 ASN_DEBUG("BIT STRING of %d bytes, %d bits unused",
1545 sizeinunits, st->bits_unused);
1546 break;
1547 case ASN_OSUBV_STR:
1548 canonical_unit_bits = unit_bits = 8;
1549 if(cval->flags & APC_CONSTRAINED)
1550 unit_bits = cval->range_bits;
1551 bpc = OS__BPC_CHAR;
1552 sizeinunits = st->size;
1553 break;
1554 case ASN_OSUBV_U16:
1555 canonical_unit_bits = unit_bits = 16;
1556 if(cval->flags & APC_CONSTRAINED)
1557 unit_bits = cval->range_bits;
1558 bpc = OS__BPC_U16;
1559 sizeinunits = st->size / 2;
1560 break;
1561 case ASN_OSUBV_U32:
1562 canonical_unit_bits = unit_bits = 32;
1563 if(cval->flags & APC_CONSTRAINED)
1564 unit_bits = cval->range_bits;
1565 bpc = OS__BPC_U32;
1566 sizeinunits = st->size / 4;
1567 break;
1568 }
1569
1570 ASN_DEBUG("Encoding %s into %d units of %d bits"
1571 " (%ld..%ld, effective %d)%s",
1572 td->name, sizeinunits, unit_bits,
1573 csiz->lower_bound, csiz->upper_bound,
1574 csiz->effective_bits, ct_extensible ? " EXT" : "");
1575
1576 /* Figure out whether size lies within PER visible constraint */
1577
1578 if(csiz->effective_bits >= 0) {
1579 if((int)sizeinunits < csiz->lower_bound
1580 || (int)sizeinunits > csiz->upper_bound) {
1581 if(ct_extensible) {
1582 cval = &asn_DEF_OCTET_STRING_constraints.value;
1583 csiz = &asn_DEF_OCTET_STRING_constraints.size;
1584 unit_bits = canonical_unit_bits;
1585 inext = 1;
1586 } else
1587 ASN__ENCODE_FAILED;
1588 }
1589 } else {
1590 inext = 0;
1591 }
1592
1593 if(ct_extensible) {
1594 /* Declare whether length is [not] within extension root */
1595 if(per_put_few_bits(po, inext, 1))
1596 ASN__ENCODE_FAILED;
1597 }
1598
1599 /* X.691, #16.5: zero-length encoding */
1600 /* X.691, #16.6: short fixed length encoding (up to 2 octets) */
1601 /* X.691, #16.7: long fixed length encoding (up to 64K octets) */
1602 if(csiz->effective_bits >= 0) {
1603 ASN_DEBUG("Encoding %d bytes (%ld), length in %d bits",
1604 st->size, sizeinunits - csiz->lower_bound,
1605 csiz->effective_bits);
1606 ret = per_put_few_bits(po, sizeinunits - csiz->lower_bound,
1607 csiz->effective_bits);
1608 if(ret) ASN__ENCODE_FAILED;
1609 if(bpc) {
1610 ret = OCTET_STRING_per_put_characters(po, st->buf,
1611 sizeinunits, bpc, unit_bits,
1612 cval->lower_bound, cval->upper_bound, pc);
1613 } else {
1614 ret = per_put_many_bits(po, st->buf,
1615 sizeinunits * unit_bits);
1616 }
1617 if(ret) ASN__ENCODE_FAILED;
1618 ASN__ENCODED_OK(er);
1619 }
1620
1621 ASN_DEBUG("Encoding %d bytes", st->size);
1622
1623 if(sizeinunits == 0) {
1624 if(uper_put_length(po, 0))
1625 ASN__ENCODE_FAILED;
1626 ASN__ENCODED_OK(er);
1627 }
1628
1629 buf = st->buf;
1630 while(sizeinunits) {
1631 ssize_t maySave = uper_put_length(po, sizeinunits);
1632 if(maySave < 0) ASN__ENCODE_FAILED;
1633
1634 ASN_DEBUG("Encoding %ld of %ld",
1635 (long)maySave, (long)sizeinunits);
1636
1637 if(bpc) {
1638 ret = OCTET_STRING_per_put_characters(po, buf,
1639 maySave, bpc, unit_bits,
1640 cval->lower_bound, cval->upper_bound, pc);
1641 } else {
1642 ret = per_put_many_bits(po, buf, maySave * unit_bits);
1643 }
1644 if(ret) ASN__ENCODE_FAILED;
1645
1646 if(bpc)
1647 buf += maySave * bpc;
1648 else
1649 buf += maySave >> 3;
1650 sizeinunits -= maySave;
1651 assert(!(maySave & 0x07) || !sizeinunits);
1652 }
1653
1654 ASN__ENCODED_OK(er);
1655 }
1656
1657 int
1658 OCTET_STRING_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
1659 asn_app_consume_bytes_f *cb, void *app_key) {
1660 const char * const h2c = "0123456789ABCDEF";
1661 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
1662 char scratch[16 * 3 + 4];
1663 char *p = scratch;
1664 uint8_t *buf;
1665 uint8_t *end;
1666 size_t i;
1667
1668 (void)td; /* Unused argument */
1669
1670 if(!st || (!st->buf && st->size))
1671 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
1672
1673 /*
1674 * Dump the contents of the buffer in hexadecimal.
1675 */
1676 buf = st->buf;
1677 end = buf + st->size;
1678 for(i = 0; buf < end; buf++, i++) {
1679 if(!(i % 16) && (i || st->size > 16)) {
1680 if(cb(scratch, p - scratch, app_key) < 0)
1681 return -1;
1682 _i_INDENT(1);
1683 p = scratch;
1684 }
1685 *p++ = h2c[(*buf >> 4) & 0x0F];
1686 *p++ = h2c[*buf & 0x0F];
1687 *p++ = 0x20;
1688 }
1689
1690 if(p > scratch) {
1691 p--; /* Remove the tail space */
1692 if(cb(scratch, p - scratch, app_key) < 0)
1693 return -1;
1694 }
1695
1696 return 0;
1697 }
1698
1699 int
1700 OCTET_STRING_print_utf8(asn_TYPE_descriptor_t *td, const void *sptr,
1701 int ilevel, asn_app_consume_bytes_f *cb, void *app_key) {
1702 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
1703
1704 (void)td; /* Unused argument */
1705 (void)ilevel; /* Unused argument */
1706
1707 if(st && (st->buf || !st->size)) {
1708 return (cb(st->buf, st->size, app_key) < 0) ? -1 : 0;
1709 } else {
1710 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
1711 }
1712 }
1713
1714 void
1715 OCTET_STRING_free(asn_TYPE_descriptor_t *td, void *sptr, int contents_only) {
1716 OCTET_STRING_t *st = (OCTET_STRING_t *)sptr;
1717 asn_OCTET_STRING_specifics_t *specs;
1718 asn_struct_ctx_t *ctx;
1719 struct _stack *stck;
1720
1721 if(!td || !st)
1722 return;
1723
1724 specs = td->specifics
1725 ? (asn_OCTET_STRING_specifics_t *)td->specifics
1726 : &asn_DEF_OCTET_STRING_specs;
1727 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
1728
1729 ASN_DEBUG("Freeing %s as OCTET STRING", td->name);
1730
1731 if(st->buf) {
1732 FREEMEM(st->buf);
1733 st->buf = 0;
1734 }
1735
1736 /*
1737 * Remove decode-time stack.
1738 */
1739 stck = (struct _stack *)ctx->ptr;
1740 if(stck) {
1741 while(stck->tail) {
1742 struct _stack_el *sel = stck->tail;
1743 stck->tail = sel->prev;
1744 FREEMEM(sel);
1745 }
1746 FREEMEM(stck);
1747 }
1748
1749 if(!contents_only) {
1750 FREEMEM(st);
1751 }
1752 }
1753
1754 /*
1755 * Conversion routines.
1756 */
1757 int
1758 OCTET_STRING_fromBuf(OCTET_STRING_t *st, const char *str, int len) {
1759 void *buf;
1760
1761 if(st == 0 || (str == 0 && len)) {
1762 errno = EINVAL;
1763 return -1;
1764 }
1765
1766 /*
1767 * Clear the OCTET STRING.
1768 */
1769 if(str == NULL) {
1770 FREEMEM(st->buf);
1771 st->buf = 0;
1772 st->size = 0;
1773 return 0;
1774 }
1775
1776 /* Determine the original string size, if not explicitly given */
1777 if(len < 0)
1778 len = strlen(str);
1779
1780 /* Allocate and fill the memory */
1781 buf = MALLOC(len + 1);
1782 if(buf == NULL)
1783 return -1;
1784
1785 memcpy(buf, str, len);
1786 ((uint8_t *)buf)[len] = '\0'; /* Couldn't use memcpy(len+1)! */
1787 FREEMEM(st->buf);
1788 st->buf = (uint8_t *)buf;
1789 st->size = len;
1790
1791 return 0;
1792 }
1793
1794 OCTET_STRING_t *
1795 OCTET_STRING_new_fromBuf(asn_TYPE_descriptor_t *td, const char *str, int len) {
1796 asn_OCTET_STRING_specifics_t *specs = td->specifics
1797 ? (asn_OCTET_STRING_specifics_t *)td->specifics
1798 : &asn_DEF_OCTET_STRING_specs;
1799 OCTET_STRING_t *st;
1800
1801 st = (OCTET_STRING_t *)CALLOC(1, specs->struct_size);
1802 if(st && str && OCTET_STRING_fromBuf(st, str, len)) {
1803 FREEMEM(st);
1804 st = NULL;
1805 }
1806
1807 return st;
1808 }
1809