2 * Copyright (c) 2003-2017 Lev Walkin <vlm@lionet.info>.
4 * Redistribution and modifications are permitted subject to BSD license.
6 #include <asn_internal.h>
7 #include <OCTET_STRING.h>
8 #include <BIT_STRING.h> /* for .bits_unused member */
12 * OCTET STRING basic type description.
14 static const ber_tlv_tag_t asn_DEF_OCTET_STRING_tags[] = {
15 (ASN_TAG_CLASS_UNIVERSAL | (4 << 2))
17 asn_OCTET_STRING_specifics_t asn_SPC_OCTET_STRING_specs = {
18 sizeof(OCTET_STRING_t),
19 offsetof(OCTET_STRING_t, _asn_ctx),
23 asn_TYPE_operation_t asn_OP_OCTET_STRING = {
25 OCTET_STRING_print, /* OCTET STRING generally means a non-ascii sequence */
27 OCTET_STRING_decode_ber,
28 OCTET_STRING_encode_der,
29 OCTET_STRING_decode_xer_hex,
30 OCTET_STRING_encode_xer,
31 #ifdef ASN_DISABLE_OER_SUPPORT
35 OCTET_STRING_decode_oer,
36 OCTET_STRING_encode_oer,
37 #endif /* ASN_DISABLE_OER_SUPPORT */
38 #ifdef ASN_DISABLE_PER_SUPPORT
44 OCTET_STRING_decode_uper, /* Unaligned PER decoder */
45 OCTET_STRING_encode_uper, /* Unaligned PER encoder */
46 OCTET_STRING_decode_aper, /* Aligned PER decoder */
47 OCTET_STRING_encode_aper, /* Aligned PER encoder */
48 #endif /* ASN_DISABLE_PER_SUPPORT */
49 OCTET_STRING_random_fill,
50 0 /* Use generic outmost tag fetcher */
52 asn_TYPE_descriptor_t asn_DEF_OCTET_STRING = {
53 "OCTET STRING", /* Canonical name */
54 "OCTET_STRING", /* XML tag name */
56 asn_DEF_OCTET_STRING_tags,
57 sizeof(asn_DEF_OCTET_STRING_tags)
58 / sizeof(asn_DEF_OCTET_STRING_tags[0]),
59 asn_DEF_OCTET_STRING_tags, /* Same as above */
60 sizeof(asn_DEF_OCTET_STRING_tags)
61 / sizeof(asn_DEF_OCTET_STRING_tags[0]),
62 { 0, 0, asn_generic_no_constraint },
63 0, 0, /* No members */
64 &asn_SPC_OCTET_STRING_specs
70 #define _CH_PHASE(ctx, inc) do { \
75 #define NEXT_PHASE(ctx) _CH_PHASE(ctx, +1)
76 #define PREV_PHASE(ctx) _CH_PHASE(ctx, -1)
79 #define ADVANCE(num_bytes) do { \
80 size_t num = (num_bytes); \
81 buf_ptr = ((const char *)buf_ptr) + num; \
83 consumed_myself += num; \
87 #define RETURN(_code) do { \
88 asn_dec_rval_t tmprval; \
89 tmprval.code = _code; \
90 tmprval.consumed = consumed_myself; \
95 #define APPEND(bufptr, bufsize) do { \
96 size_t _bs = (bufsize); /* Append size */ \
97 size_t _ns = ctx->context; /* Allocated now */ \
98 size_t _es = st->size + _bs; /* Expected size */ \
99 /* int is really a typeof(st->size): */ \
100 if((int)_es < 0) RETURN(RC_FAIL); \
103 /* Be nice and round to the memory allocator */ \
104 do { _ns = _ns ? _ns << 1 : 16; } \
106 /* int is really a typeof(st->size): */ \
107 if((int)_ns < 0) RETURN(RC_FAIL); \
108 ptr = REALLOC(st->buf, _ns); \
110 st->buf = (uint8_t *)ptr; \
111 ctx->context = _ns; \
115 ASN_DEBUG("Reallocating into %ld", (long)_ns); \
117 memcpy(st->buf + st->size, bufptr, _bs); \
118 /* Convenient nul-termination */ \
119 st->buf[_es] = '\0'; \
124 * The main reason why ASN.1 is still alive is that too much time and effort
125 * is necessary for learning it more or less adequately, thus creating a gut
126 * necessity to demonstrate that aquired skill everywhere afterwards.
127 * No, I am not going to explain what the following stuff is.
130 ber_tlv_len_t left; /* What's left to read (or -1) */
131 ber_tlv_len_t got; /* What was actually processed */
132 unsigned cont_level; /* Depth of subcontainment */
133 int want_nulls; /* Want null "end of content" octets? */
134 int bits_chopped; /* Flag in BIT STRING mode */
135 ber_tlv_tag_t tag; /* For debugging purposes */
136 struct _stack_el *prev;
137 struct _stack_el *next;
140 struct _stack_el *tail;
141 struct _stack_el *cur_ptr;
144 static struct _stack_el *
145 OS__add_stack_el(struct _stack *st) {
146 struct _stack_el *nel;
149 * Reuse the old stack frame or allocate a new one.
151 if(st->cur_ptr && st->cur_ptr->next) {
152 nel = st->cur_ptr->next;
153 nel->bits_chopped = 0;
155 /* Retain the nel->cont_level, it's correct. */
157 nel = (struct _stack_el *)CALLOC(1, sizeof(struct _stack_el));
162 /* Increase a subcontainment depth */
163 nel->cont_level = st->tail->cont_level + 1;
164 st->tail->next = nel;
166 nel->prev = st->tail;
175 static struct _stack *
177 return (struct _stack *)CALLOC(1, sizeof(struct _stack));
181 * Decode OCTET STRING type.
184 OCTET_STRING_decode_ber(const asn_codec_ctx_t *opt_codec_ctx,
185 const asn_TYPE_descriptor_t *td, void **sptr,
186 const void *buf_ptr, size_t size, int tag_mode) {
187 const asn_OCTET_STRING_specifics_t *specs = td->specifics
188 ? (const asn_OCTET_STRING_specifics_t *)td->specifics
189 : &asn_SPC_OCTET_STRING_specs;
190 BIT_STRING_t *st = (BIT_STRING_t *)*sptr;
192 asn_struct_ctx_t *ctx;
193 ssize_t consumed_myself = 0;
194 struct _stack *stck; /* Expectations stack structure */
195 struct _stack_el *sel = 0; /* Stack element */
197 enum asn_OS_Subvariant type_variant = specs->subvariant;
199 ASN_DEBUG("Decoding %s as %s (frame %ld)",
201 (type_variant == ASN_OSUBV_STR) ?
202 "OCTET STRING" : "OS-SpecialCase",
206 * Create the string if does not exist.
209 st = (BIT_STRING_t *)(*sptr = CALLOC(1, specs->struct_size));
210 if(st == NULL) RETURN(RC_FAIL);
213 /* Restore parsing context */
214 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
221 rval = ber_check_tags(opt_codec_ctx, td, ctx,
222 buf_ptr, size, tag_mode, -1,
223 &ctx->left, &tlv_constr);
224 if(rval.code != RC_OK)
229 * Complex operation, requires stack of expectations.
231 ctx->ptr = _new_stack();
237 * Jump into stackless primitive decoding.
240 if(type_variant == ASN_OSUBV_ANY && tag_mode != 1)
241 APPEND(buf_ptr, rval.consumed);
242 ADVANCE(rval.consumed);
251 * Fill the stack with expectations.
253 stck = (struct _stack *)ctx->ptr;
256 ber_tlv_tag_t tlv_tag;
257 ber_tlv_len_t tlv_len;
258 ber_tlv_tag_t expected_tag;
259 ssize_t tl, ll, tlvl;
260 /* This one works even if (sel->left == -1) */
261 size_t Left = ((!sel||(size_t)sel->left >= size)
262 ?size:(size_t)sel->left);
265 ASN_DEBUG("%p, s->l=%ld, s->wn=%ld, s->g=%ld\n", (void *)sel,
266 (long)(sel?sel->left:0),
267 (long)(sel?sel->want_nulls:0),
268 (long)(sel?sel->got:0)
270 if(sel && sel->left <= 0 && sel->want_nulls == 0) {
272 struct _stack_el *prev = sel->prev;
273 if(prev->left != -1) {
274 if(prev->left < sel->got)
276 prev->left -= sel->got;
278 prev->got += sel->got;
279 sel = stck->cur_ptr = prev;
284 sel = stck->cur_ptr = 0;
285 break; /* Nothing to wait */
289 tl = ber_fetch_tag(buf_ptr, Left, &tlv_tag);
290 ASN_DEBUG("fetch tag(size=%ld,L=%ld), %sstack, left=%ld, wn=%ld, tl=%ld",
291 (long)size, (long)Left, sel?"":"!",
292 (long)(sel?sel->left:0),
293 (long)(sel?sel->want_nulls:0),
296 case -1: RETURN(RC_FAIL);
297 case 0: RETURN(RC_WMORE);
300 tlv_constr = BER_TLV_CONSTRUCTED(buf_ptr);
302 ll = ber_fetch_length(tlv_constr,
303 (const char *)buf_ptr + tl,Left - tl,&tlv_len);
304 ASN_DEBUG("Got tag=%s, tc=%d, left=%ld, tl=%ld, len=%ld, ll=%ld",
305 ber_tlv_tag_string(tlv_tag), tlv_constr,
306 (long)Left, (long)tl, (long)tlv_len, (long)ll);
308 case -1: RETURN(RC_FAIL);
309 case 0: RETURN(RC_WMORE);
312 if(sel && sel->want_nulls
313 && ((const uint8_t *)buf_ptr)[0] == 0
314 && ((const uint8_t *)buf_ptr)[1] == 0)
317 ASN_DEBUG("Eat EOC; wn=%d--", sel->want_nulls);
319 if(type_variant == ASN_OSUBV_ANY
320 && (tag_mode != 1 || sel->cont_level))
325 if(sel->left != -1) {
326 sel->left -= 2; /* assert(sel->left >= 2) */
330 if(sel->want_nulls == 0) {
331 /* Move to the next expectation */
340 * Set up expected tags,
341 * depending on ASN.1 type being decoded.
343 switch(type_variant) {
345 /* X.690: 8.6.4.1, NOTE 2 */
350 unsigned level = sel->cont_level;
351 if(level < td->all_tags_count) {
352 expected_tag = td->all_tags[level];
354 } else if(td->all_tags_count) {
355 expected_tag = td->all_tags
356 [td->all_tags_count - 1];
359 /* else, Fall through */
363 expected_tag = tlv_tag;
368 if(tlv_tag != expected_tag) {
370 ber_tlv_tag_snprint(tlv_tag,
371 buf[0], sizeof(buf[0]));
372 ber_tlv_tag_snprint(td->tags[td->tags_count-1],
373 buf[1], sizeof(buf[1]));
374 ASN_DEBUG("Tag does not match expectation: %s != %s",
379 tlvl = tl + ll; /* Combined length of T and L encoding */
380 if((tlv_len + tlvl) < 0) {
381 /* tlv_len value is too big */
382 ASN_DEBUG("TLV encoding + length (%ld) is too big",
388 * Append a new expectation.
390 sel = OS__add_stack_el(stck);
391 if(!sel) RETURN(RC_FAIL);
395 sel->want_nulls = (tlv_len==-1);
396 if(sel->prev && sel->prev->left != -1) {
397 /* Check that the parent frame is big enough */
398 if(sel->prev->left < tlvl + (tlv_len==-1?0:tlv_len))
401 sel->left = sel->prev->left - tlvl;
407 if(type_variant == ASN_OSUBV_ANY
408 && (tag_mode != 1 || sel->cont_level))
409 APPEND(buf_ptr, tlvl);
413 ASN_DEBUG("+EXPECT2 got=%ld left=%ld, wn=%d, clvl=%u",
414 (long)sel->got, (long)sel->left,
415 sel->want_nulls, sel->cont_level);
419 /* Finished operation, "phase out" */
420 ASN_DEBUG("Phase out");
428 stck = (struct _stack *)ctx->ptr;
430 ASN_DEBUG("Phase 2: Need %ld bytes, size=%ld, alrg=%ld, wn=%d",
431 (long)sel->left, (long)size, (long)sel->got,
436 assert(sel->left >= 0);
438 len = ((ber_tlv_len_t)size < sel->left)
439 ? (ber_tlv_len_t)size : sel->left;
441 if(type_variant == ASN_OSUBV_BIT
442 && sel->bits_chopped == 0) {
443 /* Put the unused-bits-octet away */
444 st->bits_unused = *(const uint8_t *)buf_ptr;
445 APPEND(((const char *)buf_ptr+1), (len - 1));
446 sel->bits_chopped = 1;
448 APPEND(buf_ptr, len);
456 ASN_DEBUG("OS left %ld, size = %ld, wn=%d\n",
457 (long)sel->left, (long)size, sel->want_nulls);
468 * Primitive form, no stack required.
470 assert(ctx->left >= 0);
472 if(size < (size_t)ctx->left) {
473 if(!size) RETURN(RC_WMORE);
474 if(type_variant == ASN_OSUBV_BIT && !ctx->context) {
475 st->bits_unused = *(const uint8_t *)buf_ptr;
479 APPEND(buf_ptr, size);
480 assert(ctx->context > 0);
485 if(type_variant == ASN_OSUBV_BIT
486 && !ctx->context && ctx->left) {
487 st->bits_unused = *(const uint8_t *)buf_ptr;
491 APPEND(buf_ptr, ctx->left);
501 ASN_DEBUG("3sel p=%p, wn=%d, l=%ld, g=%ld, size=%ld",
502 (void *)sel->prev, sel->want_nulls,
503 (long)sel->left, (long)sel->got, (long)size);
504 if(sel->prev || sel->want_nulls > 1 || sel->left > 0) {
510 * BIT STRING-specific processing.
512 if(type_variant == ASN_OSUBV_BIT) {
514 if(st->bits_unused < 0 || st->bits_unused > 7) {
517 /* Finalize BIT STRING: zero out unused bits. */
518 st->buf[st->size-1] &= 0xff << st->bits_unused;
520 if(st->bits_unused) {
526 ASN_DEBUG("Took %ld bytes to encode %s: [%s]:%ld",
527 (long)consumed_myself, td->name,
528 (type_variant == ASN_OSUBV_STR) ? (char *)st->buf : "<data>",
536 * Encode OCTET STRING type using DER.
539 OCTET_STRING_encode_der(const asn_TYPE_descriptor_t *td, const void *sptr,
540 int tag_mode, ber_tlv_tag_t tag,
541 asn_app_consume_bytes_f *cb, void *app_key) {
542 asn_enc_rval_t er = { 0, 0, 0 };
543 const asn_OCTET_STRING_specifics_t *specs = td->specifics
544 ? (const asn_OCTET_STRING_specifics_t *)td->specifics
545 : &asn_SPC_OCTET_STRING_specs;
546 const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
547 enum asn_OS_Subvariant type_variant = specs->subvariant;
548 int fix_last_byte = 0;
550 ASN_DEBUG("%s %s as OCTET STRING",
551 cb?"Estimating":"Encoding", td->name);
556 if(type_variant != ASN_OSUBV_ANY || tag_mode == 1) {
557 er.encoded = der_write_tags(td,
558 (type_variant == ASN_OSUBV_BIT) + st->size,
559 tag_mode, type_variant == ASN_OSUBV_ANY, tag,
561 if(er.encoded == -1) {
563 er.structure_ptr = sptr;
567 /* Disallow: [<tag>] IMPLICIT ANY */
568 assert(type_variant != ASN_OSUBV_ANY || tag_mode != -1);
573 er.encoded += (type_variant == ASN_OSUBV_BIT) + st->size;
578 * Prepare to deal with the last octet of BIT STRING.
580 if(type_variant == ASN_OSUBV_BIT) {
581 uint8_t b = st->bits_unused & 0x07;
582 if(b && st->size) fix_last_byte = 1;
583 ASN__CALLBACK(&b, 1);
586 /* Invoke callback for the main part of the buffer */
587 ASN__CALLBACK(st->buf, st->size - fix_last_byte);
589 /* The last octet should be stripped off the unused bits */
591 uint8_t b = st->buf[st->size-1] & (0xff << st->bits_unused);
592 ASN__CALLBACK(&b, 1);
601 OCTET_STRING_encode_xer(const asn_TYPE_descriptor_t *td, const void *sptr,
602 int ilevel, enum xer_encoder_flags_e flags,
603 asn_app_consume_bytes_f *cb, void *app_key) {
604 const char * const h2c = "0123456789ABCDEF";
605 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
606 asn_enc_rval_t er = { 0, 0, 0 };
607 char scratch[16 * 3 + 4];
613 if(!st || (!st->buf && st->size))
619 * Dump the contents of the buffer in hexadecimal.
622 end = buf + st->size;
623 if(flags & XER_F_CANONICAL) {
624 char *scend = scratch + (sizeof(scratch) - 2);
625 for(; buf < end; buf++) {
627 ASN__CALLBACK(scratch, p - scratch);
630 *p++ = h2c[(*buf >> 4) & 0x0F];
631 *p++ = h2c[*buf & 0x0F];
634 ASN__CALLBACK(scratch, p-scratch); /* Dump the rest */
636 for(i = 0; buf < end; buf++, i++) {
637 if(!(i % 16) && (i || st->size > 16)) {
638 ASN__CALLBACK(scratch, p-scratch);
640 ASN__TEXT_INDENT(1, ilevel);
642 *p++ = h2c[(*buf >> 4) & 0x0F];
643 *p++ = h2c[*buf & 0x0F];
647 p--; /* Remove the tail space */
648 ASN__CALLBACK(scratch, p-scratch); /* Dump the rest */
650 ASN__TEXT_INDENT(1, ilevel-1);
659 static const struct OCTET_STRING__xer_escape_table_s {
662 } OCTET_STRING__xer_escape_table[] = {
663 #define OSXET(s) { s, sizeof(s) - 1 }
664 OSXET("\074\156\165\154\057\076"), /* <nul/> */
665 OSXET("\074\163\157\150\057\076"), /* <soh/> */
666 OSXET("\074\163\164\170\057\076"), /* <stx/> */
667 OSXET("\074\145\164\170\057\076"), /* <etx/> */
668 OSXET("\074\145\157\164\057\076"), /* <eot/> */
669 OSXET("\074\145\156\161\057\076"), /* <enq/> */
670 OSXET("\074\141\143\153\057\076"), /* <ack/> */
671 OSXET("\074\142\145\154\057\076"), /* <bel/> */
672 OSXET("\074\142\163\057\076"), /* <bs/> */
673 OSXET("\011"), /* \t */
674 OSXET("\012"), /* \n */
675 OSXET("\074\166\164\057\076"), /* <vt/> */
676 OSXET("\074\146\146\057\076"), /* <ff/> */
677 OSXET("\015"), /* \r */
678 OSXET("\074\163\157\057\076"), /* <so/> */
679 OSXET("\074\163\151\057\076"), /* <si/> */
680 OSXET("\074\144\154\145\057\076"), /* <dle/> */
681 OSXET("\074\144\143\061\057\076"), /* <de1/> */
682 OSXET("\074\144\143\062\057\076"), /* <de2/> */
683 OSXET("\074\144\143\063\057\076"), /* <de3/> */
684 OSXET("\074\144\143\064\057\076"), /* <de4/> */
685 OSXET("\074\156\141\153\057\076"), /* <nak/> */
686 OSXET("\074\163\171\156\057\076"), /* <syn/> */
687 OSXET("\074\145\164\142\057\076"), /* <etb/> */
688 OSXET("\074\143\141\156\057\076"), /* <can/> */
689 OSXET("\074\145\155\057\076"), /* <em/> */
690 OSXET("\074\163\165\142\057\076"), /* <sub/> */
691 OSXET("\074\145\163\143\057\076"), /* <esc/> */
692 OSXET("\074\151\163\064\057\076"), /* <is4/> */
693 OSXET("\074\151\163\063\057\076"), /* <is3/> */
694 OSXET("\074\151\163\062\057\076"), /* <is2/> */
695 OSXET("\074\151\163\061\057\076"), /* <is1/> */
702 OSXET("\046\141\155\160\073"), /* & */
704 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, /* ()*+,-./ */
705 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, /* 01234567 */
706 {0,0},{0,0},{0,0},{0,0}, /* 89:; */
707 OSXET("\046\154\164\073"), /* < */
709 OSXET("\046\147\164\073"), /* > */
713 OS__check_escaped_control_char(const void *buf, int size) {
716 * Inefficient algorithm which translates the escape sequences
717 * defined above into characters. Returns -1 if not found.
718 * TODO: replace by a faster algorithm (bsearch(), hash or
719 * nested table lookups).
721 for(i = 0; i < 32 /* Don't spend time on the bottom half */; i++) {
722 const struct OCTET_STRING__xer_escape_table_s *el;
723 el = &OCTET_STRING__xer_escape_table[i];
724 if(el->size == size && memcmp(buf, el->string, size) == 0)
731 OCTET_STRING__handle_control_chars(void *struct_ptr, const void *chunk_buf, size_t chunk_size) {
733 * This might be one of the escape sequences
734 * for control characters. Check it out.
737 int control_char = OS__check_escaped_control_char(chunk_buf,chunk_size);
738 if(control_char >= 0) {
739 OCTET_STRING_t *st = (OCTET_STRING_t *)struct_ptr;
740 void *p = REALLOC(st->buf, st->size + 2);
742 st->buf = (uint8_t *)p;
743 st->buf[st->size++] = control_char;
744 st->buf[st->size] = '\0'; /* nul-termination */
749 return -1; /* No, it's not */
753 OCTET_STRING_encode_xer_utf8(const asn_TYPE_descriptor_t *td, const void *sptr,
754 int ilevel, enum xer_encoder_flags_e flags,
755 asn_app_consume_bytes_f *cb, void *app_key) {
756 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
757 asn_enc_rval_t er = { 0, 0, 0 };
759 uint8_t *ss; /* Sequence start */
760 ssize_t encoded_len = 0;
762 (void)ilevel; /* Unused argument */
763 (void)flags; /* Unused argument */
765 if(!st || (!st->buf && st->size))
769 end = buf + st->size;
770 for(ss = buf; buf < end; buf++) {
771 unsigned int ch = *buf;
772 int s_len; /* Special encoding sequence length */
775 * Escape certain characters: X.680/11.15
777 if(ch < sizeof(OCTET_STRING__xer_escape_table)
778 /sizeof(OCTET_STRING__xer_escape_table[0])
779 && (s_len = OCTET_STRING__xer_escape_table[ch].size)) {
780 if(((buf - ss) && cb(ss, buf - ss, app_key) < 0)
781 || cb(OCTET_STRING__xer_escape_table[ch].string, s_len,
784 encoded_len += (buf - ss) + s_len;
789 encoded_len += (buf - ss);
790 if((buf - ss) && cb(ss, buf - ss, app_key) < 0)
793 er.encoded = encoded_len;
798 * Convert from hexadecimal format (cstring): "AB CD EF"
800 static ssize_t OCTET_STRING__convert_hexadecimal(void *sptr, const void *chunk_buf, size_t chunk_size, int have_more) {
801 OCTET_STRING_t *st = (OCTET_STRING_t *)sptr;
802 const char *chunk_stop = (const char *)chunk_buf;
803 const char *p = chunk_stop;
804 const char *pend = p + chunk_size;
805 unsigned int clv = 0;
806 int half = 0; /* Half bit */
809 /* Reallocate buffer according to high cap estimation */
810 size_t new_size = st->size + (chunk_size + 1) / 2;
811 void *nptr = REALLOC(st->buf, new_size + 1);
813 st->buf = (uint8_t *)nptr;
814 buf = st->buf + st->size;
817 * If something like " a b c " appears here, the " a b":3 will be
818 * converted, and the rest skipped. That is, unless buf_size is greater
819 * than chunk_size, then it'll be equivalent to "ABC0".
821 for(; p < pend; p++) {
822 int ch = *(const unsigned char *)p;
824 case 0x09: case 0x0a: case 0x0c: case 0x0d:
826 /* Ignore whitespace */
828 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: /*01234*/
829 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: /*56789*/
830 clv = (clv << 4) + (ch - 0x30);
832 case 0x41: case 0x42: case 0x43: /* ABC */
833 case 0x44: case 0x45: case 0x46: /* DEF */
834 clv = (clv << 4) + (ch - 0x41 + 10);
836 case 0x61: case 0x62: case 0x63: /* abc */
837 case 0x64: case 0x65: case 0x66: /* def */
838 clv = (clv << 4) + (ch - 0x61 + 10);
852 * Check partial decoding.
857 * Partial specification is fine,
858 * because no more more PXER_TEXT data is available.
867 st->size = buf - st->buf; /* Adjust the buffer size */
868 assert(st->size <= new_size);
869 st->buf[st->size] = 0; /* Courtesy termination */
871 return (chunk_stop - (const char *)chunk_buf); /* Converted size */
875 * Convert from binary format: "00101011101"
877 static ssize_t OCTET_STRING__convert_binary(void *sptr, const void *chunk_buf, size_t chunk_size, int have_more) {
878 BIT_STRING_t *st = (BIT_STRING_t *)sptr;
879 const char *p = (const char *)chunk_buf;
880 const char *pend = p + chunk_size;
881 int bits_unused = st->bits_unused & 0x7;
884 /* Reallocate buffer according to high cap estimation */
885 size_t new_size = st->size + (chunk_size + 7) / 8;
886 void *nptr = REALLOC(st->buf, new_size + 1);
888 st->buf = (uint8_t *)nptr;
889 buf = st->buf + st->size;
899 * Convert series of 0 and 1 into the octet string.
901 for(; p < pend; p++) {
902 int ch = *(const unsigned char *)p;
904 case 0x09: case 0x0a: case 0x0c: case 0x0d:
906 /* Ignore whitespace */
910 if(bits_unused-- <= 0) {
911 *++buf = 0; /* Clean the cell */
914 *buf |= (ch&1) << bits_unused;
917 st->bits_unused = bits_unused;
922 if(bits_unused == 8) {
923 st->size = buf - st->buf;
926 st->size = buf - st->buf + 1;
927 st->bits_unused = bits_unused;
930 assert(st->size <= new_size);
931 st->buf[st->size] = 0; /* Courtesy termination */
933 return chunk_size; /* Converted in full */
937 * Something like strtod(), but with stricter rules.
940 OS__strtoent(int base, const char *buf, const char *end, int32_t *ret_value) {
941 const int32_t last_unicode_codepoint = 0x10ffff;
945 for(p = buf; p < end; p++) {
949 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: /*01234*/
950 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: /*56789*/
951 val = val * base + (ch - 0x30);
953 case 0x41: case 0x42: case 0x43: /* ABC */
954 case 0x44: case 0x45: case 0x46: /* DEF */
955 val = val * base + (ch - 0x41 + 10);
957 case 0x61: case 0x62: case 0x63: /* abc */
958 case 0x64: case 0x65: case 0x66: /* def */
959 val = val * base + (ch - 0x61 + 10);
963 return (p - buf) + 1;
965 return -1; /* Character set error */
968 /* Value exceeds the Unicode range. */
969 if(val > last_unicode_codepoint) {
979 * Convert from the plain UTF-8 format, expanding entity references: "2 < 3"
982 OCTET_STRING__convert_entrefs(void *sptr, const void *chunk_buf,
983 size_t chunk_size, int have_more) {
984 OCTET_STRING_t *st = (OCTET_STRING_t *)sptr;
985 const char *p = (const char *)chunk_buf;
986 const char *pend = p + chunk_size;
989 /* Reallocate buffer */
990 size_t new_size = st->size + chunk_size;
991 void *nptr = REALLOC(st->buf, new_size + 1);
993 st->buf = (uint8_t *)nptr;
994 buf = st->buf + st->size;
997 * Convert series of 0 and 1 into the octet string.
999 for(; p < pend; p++) {
1000 int ch = *(const unsigned char *)p;
1001 int len; /* Length of the rest of the chunk */
1003 if(ch != 0x26 /* '&' */) {
1005 continue; /* That was easy... */
1009 * Process entity reference.
1011 len = chunk_size - (p - (const char *)chunk_buf);
1012 if(len == 1 /* "&" */) goto want_more;
1013 if(p[1] == 0x23 /* '#' */) {
1014 const char *pval; /* Pointer to start of digits */
1015 int32_t val = 0; /* Entity reference value */
1018 if(len == 2 /* "&#" */) goto want_more;
1019 if(p[2] == 0x78 /* 'x' */)
1020 pval = p + 3, base = 16;
1022 pval = p + 2, base = 10;
1023 len = OS__strtoent(base, pval, p + len, &val);
1025 /* Invalid charset. Just copy verbatim. */
1029 if(!len || pval[len-1] != 0x3b) goto want_more;
1031 p += (pval - p) + len - 1; /* Advance past entref */
1035 } else if(val < 0x800) {
1036 *buf++ = 0xc0 | ((val >> 6));
1037 *buf++ = 0x80 | ((val & 0x3f));
1038 } else if(val < 0x10000) {
1039 *buf++ = 0xe0 | ((val >> 12));
1040 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1041 *buf++ = 0x80 | ((val & 0x3f));
1042 } else if(val < 0x200000) {
1043 *buf++ = 0xf0 | ((val >> 18));
1044 *buf++ = 0x80 | ((val >> 12) & 0x3f);
1045 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1046 *buf++ = 0x80 | ((val & 0x3f));
1047 } else if(val < 0x4000000) {
1048 *buf++ = 0xf8 | ((val >> 24));
1049 *buf++ = 0x80 | ((val >> 18) & 0x3f);
1050 *buf++ = 0x80 | ((val >> 12) & 0x3f);
1051 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1052 *buf++ = 0x80 | ((val & 0x3f));
1054 *buf++ = 0xfc | ((val >> 30) & 0x1);
1055 *buf++ = 0x80 | ((val >> 24) & 0x3f);
1056 *buf++ = 0x80 | ((val >> 18) & 0x3f);
1057 *buf++ = 0x80 | ((val >> 12) & 0x3f);
1058 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1059 *buf++ = 0x80 | ((val & 0x3f));
1063 * Ugly, limited parsing of & > <
1065 char *sc = (char *)memchr(p, 0x3b, len > 5 ? 5 : len);
1066 if(!sc) goto want_more;
1068 && p[1] == 0x61 /* 'a' */
1069 && p[2] == 0x6d /* 'm' */
1070 && p[3] == 0x70 /* 'p' */) {
1077 *buf = 0x3c; /* '<' */
1078 } else if(p[1] == 0x67) {
1079 *buf = 0x3e; /* '>' */
1081 /* Unsupported entity reference */
1086 /* Unsupported entity reference */
1094 /* Unsupported entity reference */
1102 * We know that no more data (of the same type)
1103 * is coming. Copy the rest verbatim.
1108 chunk_size = (p - (const char *)chunk_buf);
1109 /* Processing stalled: need more data */
1113 st->size = buf - st->buf;
1114 assert(st->size <= new_size);
1115 st->buf[st->size] = 0; /* Courtesy termination */
1117 return chunk_size; /* Converted in full */
1121 * Decode OCTET STRING from the XML element's body.
1123 static asn_dec_rval_t
1124 OCTET_STRING__decode_xer(
1125 const asn_codec_ctx_t *opt_codec_ctx, const asn_TYPE_descriptor_t *td,
1126 void **sptr, const char *opt_mname, const void *buf_ptr, size_t size,
1127 int (*opt_unexpected_tag_decoder)(void *struct_ptr, const void *chunk_buf,
1129 ssize_t (*body_receiver)(void *struct_ptr, const void *chunk_buf,
1130 size_t chunk_size, int have_more)) {
1131 OCTET_STRING_t *st = (OCTET_STRING_t *)*sptr;
1132 const asn_OCTET_STRING_specifics_t *specs = td->specifics
1133 ? (const asn_OCTET_STRING_specifics_t *)td->specifics
1134 : &asn_SPC_OCTET_STRING_specs;
1135 const char *xml_tag = opt_mname ? opt_mname : td->xml_tag;
1136 asn_struct_ctx_t *ctx; /* Per-structure parser context */
1137 asn_dec_rval_t rval; /* Return value from the decoder */
1141 * Create the string if does not exist.
1144 st = (OCTET_STRING_t *)CALLOC(1, specs->struct_size);
1146 if(!st) goto sta_failed;
1152 /* This is separate from above section */
1153 st->buf = (uint8_t *)CALLOC(1, 1);
1164 /* Restore parsing context */
1165 ctx = (asn_struct_ctx_t *)(((char *)*sptr) + specs->ctx_offset);
1167 return xer_decode_general(opt_codec_ctx, ctx, *sptr, xml_tag,
1168 buf_ptr, size, opt_unexpected_tag_decoder, body_receiver);
1173 rval.code = RC_FAIL;
1179 * Decode OCTET STRING from the hexadecimal data.
1182 OCTET_STRING_decode_xer_hex(const asn_codec_ctx_t *opt_codec_ctx,
1183 const asn_TYPE_descriptor_t *td, void **sptr,
1184 const char *opt_mname, const void *buf_ptr,
1186 return OCTET_STRING__decode_xer(opt_codec_ctx, td, sptr, opt_mname,
1187 buf_ptr, size, 0, OCTET_STRING__convert_hexadecimal);
1191 * Decode OCTET STRING from the binary (0/1) data.
1194 OCTET_STRING_decode_xer_binary(const asn_codec_ctx_t *opt_codec_ctx,
1195 const asn_TYPE_descriptor_t *td, void **sptr,
1196 const char *opt_mname, const void *buf_ptr,
1198 return OCTET_STRING__decode_xer(opt_codec_ctx, td, sptr, opt_mname,
1199 buf_ptr, size, 0, OCTET_STRING__convert_binary);
1203 * Decode OCTET STRING from the string (ASCII/UTF-8) data.
1206 OCTET_STRING_decode_xer_utf8(const asn_codec_ctx_t *opt_codec_ctx,
1207 const asn_TYPE_descriptor_t *td, void **sptr,
1208 const char *opt_mname, const void *buf_ptr,
1210 return OCTET_STRING__decode_xer(opt_codec_ctx, td, sptr, opt_mname,
1212 OCTET_STRING__handle_control_chars,
1213 OCTET_STRING__convert_entrefs);
1216 #ifndef ASN_DISABLE_PER_SUPPORT
1219 OCTET_STRING_per_get_characters(asn_per_data_t *po, uint8_t *buf,
1220 size_t units, unsigned int bpc, unsigned int unit_bits,
1221 long lb, long ub, const asn_per_constraints_t *pc) {
1222 uint8_t *end = buf + units * bpc;
1224 ASN_DEBUG("Expanding %d characters into (%ld..%ld):%d",
1225 (int)units, lb, ub, unit_bits);
1228 if((unsigned long)ub <= ((unsigned long)2 << (unit_bits - 1))) {
1229 /* Decode without translation */
1231 } else if(pc && pc->code2value) {
1233 return 1; /* FATAL: can't have constrained
1234 * UniversalString with more than
1235 * 16 million code points */
1236 for(; buf < end; buf += bpc) {
1238 int code = per_get_few_bits(po, unit_bits);
1239 if(code < 0) return -1; /* WMORE */
1240 value = pc->code2value(code);
1242 ASN_DEBUG("Code %d (0x%02x) is"
1243 " not in map (%ld..%ld)",
1244 code, code, lb, ub);
1245 return 1; /* FATAL */
1248 case 1: *buf = value; break;
1249 case 2: buf[0] = value >> 8; buf[1] = value; break;
1250 case 4: buf[0] = value >> 24; buf[1] = value >> 16;
1251 buf[2] = value >> 8; buf[3] = value; break;
1257 /* Shortcut the no-op copying to the aligned structure */
1258 if(lb == 0 && (unit_bits == 8 * bpc)) {
1259 return per_get_many_bits(po, buf, 0, unit_bits * units);
1262 for(; buf < end; buf += bpc) {
1263 int32_t code = per_get_few_bits(po, unit_bits);
1264 int32_t ch = code + lb;
1265 if(code < 0) return -1; /* WMORE */
1267 ASN_DEBUG("Code %d is out of range (%ld..%ld)",
1269 return 1; /* FATAL */
1272 case 1: *buf = ch; break;
1273 case 2: buf[0] = ch >> 8; buf[1] = ch; break;
1274 case 4: buf[0] = ch >> 24; buf[1] = ch >> 16;
1275 buf[2] = ch >> 8; buf[3] = ch; break;
1283 OCTET_STRING_per_put_characters(asn_per_outp_t *po, const uint8_t *buf,
1284 size_t units, unsigned int bpc, unsigned int unit_bits,
1285 long lb, long ub, const asn_per_constraints_t *pc) {
1286 const uint8_t *end = buf + units * bpc;
1288 ASN_DEBUG("Squeezing %d characters into (%ld..%ld):%d (%d bpc)",
1289 (int)units, lb, ub, unit_bits, bpc);
1292 if((unsigned long)ub <= ((unsigned long)2 << (unit_bits - 1))) {
1295 } else if(pc && pc->value2code) {
1296 for(; buf < end; buf += bpc) {
1300 case 1: value = *(const uint8_t *)buf; break;
1301 case 2: value = (buf[0] << 8) | buf[1]; break;
1302 case 4: value = (buf[0] << 24) | (buf[1] << 16)
1303 | (buf[2] << 8) | buf[3]; break;
1306 code = pc->value2code(value);
1308 ASN_DEBUG("Character %d (0x%02x) is"
1309 " not in map (%ld..%ld)",
1310 *buf, *buf, lb, ub);
1313 if(per_put_few_bits(po, code, unit_bits))
1318 /* Shortcut the no-op copying to the aligned structure */
1319 if(lb == 0 && (unit_bits == 8 * bpc)) {
1320 return per_put_many_bits(po, buf, unit_bits * units);
1323 for(ub -= lb; buf < end; buf += bpc) {
1328 value = *(const uint8_t *)buf;
1331 value = (buf[0] << 8) | buf[1];
1334 value = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
1340 if(ch < 0 || ch > ub) {
1341 ASN_DEBUG("Character %d (0x%02x) is out of range (%ld..%ld)", *buf,
1342 value, lb, ub + lb);
1345 if(per_put_few_bits(po, ch, unit_bits)) return -1;
1351 static asn_per_constraints_t asn_DEF_OCTET_STRING_constraints = {
1352 { APC_CONSTRAINED, 8, 8, 0, 255 },
1353 { APC_SEMI_CONSTRAINED, -1, -1, 0, 0 },
1358 OCTET_STRING_decode_uper(const asn_codec_ctx_t *opt_codec_ctx,
1359 const asn_TYPE_descriptor_t *td,
1360 const asn_per_constraints_t *constraints, void **sptr,
1361 asn_per_data_t *pd) {
1362 const asn_OCTET_STRING_specifics_t *specs = td->specifics
1363 ? (const asn_OCTET_STRING_specifics_t *)td->specifics
1364 : &asn_SPC_OCTET_STRING_specs;
1365 const asn_per_constraints_t *pc =
1366 constraints ? constraints : td->encoding_constraints.per_constraints;
1367 const asn_per_constraint_t *cval;
1368 const asn_per_constraint_t *csiz;
1369 asn_dec_rval_t rval = { RC_OK, 0 };
1370 OCTET_STRING_t *st = (OCTET_STRING_t *)*sptr;
1371 ssize_t consumed_myself = 0;
1377 } bpc; /* Bytes per character */
1378 unsigned int unit_bits;
1379 unsigned int canonical_unit_bits;
1381 (void)opt_codec_ctx;
1387 cval = &asn_DEF_OCTET_STRING_constraints.value;
1388 csiz = &asn_DEF_OCTET_STRING_constraints.size;
1391 switch(specs->subvariant) {
1395 ASN_DEBUG("Unrecognized subvariant %d", specs->subvariant);
1399 canonical_unit_bits = unit_bits = 8;
1400 if(cval->flags & APC_CONSTRAINED)
1401 unit_bits = cval->range_bits;
1405 canonical_unit_bits = unit_bits = 16;
1406 if(cval->flags & APC_CONSTRAINED)
1407 unit_bits = cval->range_bits;
1411 canonical_unit_bits = unit_bits = 32;
1412 if(cval->flags & APC_CONSTRAINED)
1413 unit_bits = cval->range_bits;
1419 * Allocate the string.
1422 st = (OCTET_STRING_t *)(*sptr = CALLOC(1, specs->struct_size));
1423 if(!st) RETURN(RC_FAIL);
1426 ASN_DEBUG("PER Decoding %s size %ld .. %ld bits %d",
1427 csiz->flags & APC_EXTENSIBLE ? "extensible" : "non-extensible",
1428 csiz->lower_bound, csiz->upper_bound, csiz->effective_bits);
1430 if(csiz->flags & APC_EXTENSIBLE) {
1431 int inext = per_get_few_bits(pd, 1);
1432 if(inext < 0) RETURN(RC_WMORE);
1434 csiz = &asn_DEF_OCTET_STRING_constraints.size;
1435 unit_bits = canonical_unit_bits;
1439 if(csiz->effective_bits >= 0) {
1442 st->size = csiz->upper_bound * bpc;
1444 st->size = (csiz->upper_bound + 7) >> 3;
1446 st->buf = (uint8_t *)MALLOC(st->size + 1);
1447 if(!st->buf) { st->size = 0; RETURN(RC_FAIL); }
1450 /* X.691, #16.5: zero-length encoding */
1451 /* X.691, #16.6: short fixed length encoding (up to 2 octets) */
1452 /* X.691, #16.7: long fixed length encoding (up to 64K octets) */
1453 if(csiz->effective_bits == 0) {
1456 ASN_DEBUG("Encoding OCTET STRING size %ld",
1458 ret = OCTET_STRING_per_get_characters(pd, st->buf,
1459 csiz->upper_bound, bpc, unit_bits,
1460 cval->lower_bound, cval->upper_bound, pc);
1461 if(ret > 0) RETURN(RC_FAIL);
1463 ASN_DEBUG("Encoding BIT STRING size %ld",
1465 ret = per_get_many_bits(pd, st->buf, 0,
1466 unit_bits * csiz->upper_bound);
1468 if(ret < 0) RETURN(RC_WMORE);
1469 consumed_myself += unit_bits * csiz->upper_bound;
1470 st->buf[st->size] = 0;
1481 /* Get the PER length */
1482 raw_len = uper_get_length(pd, csiz->effective_bits, csiz->lower_bound,
1484 if(raw_len < 0) RETURN(RC_WMORE);
1485 if(raw_len == 0 && st->buf) break;
1487 ASN_DEBUG("Got PER length eb %ld, len %ld, %s (%s)",
1488 (long)csiz->effective_bits, (long)raw_len,
1489 repeat ? "repeat" : "once", td->name);
1490 len_bytes = raw_len * bpc;
1491 p = REALLOC(st->buf, st->size + len_bytes + 1);
1492 if(!p) RETURN(RC_FAIL);
1493 st->buf = (uint8_t *)p;
1495 ret = OCTET_STRING_per_get_characters(pd, &st->buf[st->size], raw_len,
1496 bpc, unit_bits, cval->lower_bound,
1497 cval->upper_bound, pc);
1498 if(ret > 0) RETURN(RC_FAIL);
1499 if(ret < 0) RETURN(RC_WMORE);
1500 st->size += len_bytes;
1502 st->buf[st->size] = 0; /* nul-terminate */
1508 OCTET_STRING_encode_uper(const asn_TYPE_descriptor_t *td,
1509 const asn_per_constraints_t *constraints,
1510 const void *sptr, asn_per_outp_t *po) {
1511 const asn_OCTET_STRING_specifics_t *specs = td->specifics
1512 ? (const asn_OCTET_STRING_specifics_t *)td->specifics
1513 : &asn_SPC_OCTET_STRING_specs;
1514 const asn_per_constraints_t *pc = constraints ? constraints
1515 : td->encoding_constraints.per_constraints;
1516 const asn_per_constraint_t *cval;
1517 const asn_per_constraint_t *csiz;
1518 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
1519 asn_enc_rval_t er = { 0, 0, 0 };
1520 int inext = 0; /* Lies not within extension root */
1521 unsigned int unit_bits;
1522 unsigned int canonical_unit_bits;
1523 size_t size_in_units;
1530 } bpc; /* Bytes per character */
1533 if(!st || (!st->buf && st->size))
1540 cval = &asn_DEF_OCTET_STRING_constraints.value;
1541 csiz = &asn_DEF_OCTET_STRING_constraints.size;
1543 ct_extensible = csiz->flags & APC_EXTENSIBLE;
1545 switch(specs->subvariant) {
1551 canonical_unit_bits = unit_bits = 8;
1552 if(cval->flags & APC_CONSTRAINED)
1553 unit_bits = cval->range_bits;
1555 size_in_units = st->size;
1558 canonical_unit_bits = unit_bits = 16;
1559 if(cval->flags & APC_CONSTRAINED)
1560 unit_bits = cval->range_bits;
1562 size_in_units = st->size >> 1;
1564 ASN_DEBUG("%s string size is not modulo 2", td->name);
1569 canonical_unit_bits = unit_bits = 32;
1570 if(cval->flags & APC_CONSTRAINED)
1571 unit_bits = cval->range_bits;
1573 size_in_units = st->size >> 2;
1575 ASN_DEBUG("%s string size is not modulo 4", td->name);
1581 ASN_DEBUG("Encoding %s into %" ASN_PRI_SIZE " units of %d bits"
1582 " (%ld..%ld, effective %d)%s",
1583 td->name, size_in_units, unit_bits,
1584 csiz->lower_bound, csiz->upper_bound,
1585 csiz->effective_bits, ct_extensible ? " EXT" : "");
1587 /* Figure out whether size lies within PER visible constraint */
1589 if(csiz->effective_bits >= 0) {
1590 if((ssize_t)size_in_units < csiz->lower_bound
1591 || (ssize_t)size_in_units > csiz->upper_bound) {
1593 csiz = &asn_DEF_OCTET_STRING_constraints.size;
1594 unit_bits = canonical_unit_bits;
1605 /* Declare whether length is [not] within extension root */
1606 if(per_put_few_bits(po, inext, 1))
1610 if(csiz->effective_bits >= 0 && !inext) {
1611 ASN_DEBUG("Encoding %" ASN_PRI_SIZE " bytes (%ld), length in %d bits", st->size,
1612 size_in_units - csiz->lower_bound, csiz->effective_bits);
1613 ret = per_put_few_bits(po, size_in_units - csiz->lower_bound,
1614 csiz->effective_bits);
1615 if(ret) ASN__ENCODE_FAILED;
1616 ret = OCTET_STRING_per_put_characters(po, st->buf, size_in_units, bpc,
1617 unit_bits, cval->lower_bound,
1618 cval->upper_bound, pc);
1619 if(ret) ASN__ENCODE_FAILED;
1620 ASN__ENCODED_OK(er);
1623 ASN_DEBUG("Encoding %" ASN_PRI_SIZE " bytes", st->size);
1626 ASN_DEBUG("Encoding %" ASN_PRI_SIZE " in units", size_in_units);
1629 ssize_t may_save = uper_put_length(po, size_in_units, &need_eom);
1630 if(may_save < 0) ASN__ENCODE_FAILED;
1632 ASN_DEBUG("Encoding %" ASN_PRI_SSIZE " of %" ASN_PRI_SIZE "%s", may_save, size_in_units,
1633 need_eom ? ",+EOM" : "");
1635 ret = OCTET_STRING_per_put_characters(po, buf, may_save, bpc, unit_bits,
1637 cval->upper_bound, pc);
1638 if(ret) ASN__ENCODE_FAILED;
1640 buf += may_save * bpc;
1641 size_in_units -= may_save;
1642 assert(!(may_save & 0x07) || !size_in_units);
1643 if(need_eom && uper_put_length(po, 0, 0))
1644 ASN__ENCODE_FAILED; /* End of Message length */
1645 } while(size_in_units);
1647 ASN__ENCODED_OK(er);
1651 OCTET_STRING_decode_aper(const asn_codec_ctx_t *opt_codec_ctx,
1652 const asn_TYPE_descriptor_t *td,
1653 const asn_per_constraints_t *constraints,
1654 void **sptr, asn_per_data_t *pd) {
1656 const asn_OCTET_STRING_specifics_t *specs = td->specifics
1657 ? (const asn_OCTET_STRING_specifics_t *)td->specifics
1658 : &asn_SPC_OCTET_STRING_specs;
1659 const asn_per_constraints_t *pc = constraints ? constraints
1660 : td->encoding_constraints.per_constraints;
1661 const asn_per_constraint_t *cval;
1662 const asn_per_constraint_t *csiz;
1663 asn_dec_rval_t rval = { RC_OK, 0 };
1664 BIT_STRING_t *st = (BIT_STRING_t *)*sptr;
1665 ssize_t consumed_myself = 0;
1672 } bpc; /* Bytes per character */
1673 unsigned int unit_bits;
1674 unsigned int canonical_unit_bits;
1676 (void)opt_codec_ctx;
1682 cval = &asn_DEF_OCTET_STRING_constraints.value;
1683 csiz = &asn_DEF_OCTET_STRING_constraints.size;
1686 switch(specs->subvariant) {
1688 /* case ASN_OSUBV_ANY:
1689 ASN_DEBUG("Unrecognized subvariant %d", specs->subvariant);
1693 canonical_unit_bits = unit_bits = 1;
1698 canonical_unit_bits = unit_bits = 8;
1699 /* if(cval->flags & APC_CONSTRAINED)
1700 unit_bits = cval->range_bits;
1705 canonical_unit_bits = unit_bits = 16;
1706 if(cval->flags & APC_CONSTRAINED)
1707 unit_bits = cval->range_bits;
1711 canonical_unit_bits = unit_bits = 32;
1712 if(cval->flags & APC_CONSTRAINED)
1713 unit_bits = cval->range_bits;
1719 * Allocate the string.
1722 st = (BIT_STRING_t *)(*sptr = CALLOC(1, specs->struct_size));
1723 if(!st) RETURN(RC_FAIL);
1726 ASN_DEBUG("PER Decoding %s size %ld .. %ld bits %d",
1727 csiz->flags & APC_EXTENSIBLE ? "extensible" : "non-extensible",
1728 csiz->lower_bound, csiz->upper_bound, csiz->effective_bits);
1730 if(csiz->flags & APC_EXTENSIBLE) {
1731 int inext = per_get_few_bits(pd, 1);
1732 if(inext < 0) RETURN(RC_WMORE);
1734 csiz = &asn_DEF_OCTET_STRING_constraints.size;
1735 cval = &asn_DEF_OCTET_STRING_constraints.value;
1736 unit_bits = canonical_unit_bits;
1740 if(csiz->effective_bits >= 0) {
1743 st->size = csiz->upper_bound * bpc;
1745 st->size = (csiz->upper_bound + 7) >> 3;
1747 st->buf = (uint8_t *)MALLOC(st->size + 1);
1748 if(!st->buf) { st->size = 0; RETURN(RC_FAIL); }
1751 /* X.691, #16.5: zero-length encoding */
1752 /* X.691, #16.6: short fixed length encoding (up to 2 octets) */
1753 /* X.691, #16.7: long fixed length encoding (up to 64K octets) */
1754 if(csiz->effective_bits == 0) {
1756 if (st->size > 2) { /* X.691 #16 NOTE 1 */
1757 if (aper_get_align(pd) < 0)
1761 ASN_DEBUG("Decoding OCTET STRING size %ld",
1763 ret = OCTET_STRING_per_get_characters(pd, st->buf,
1764 csiz->upper_bound, bpc, unit_bits,
1765 cval->lower_bound, cval->upper_bound, pc);
1766 if(ret > 0) RETURN(RC_FAIL);
1768 ASN_DEBUG("Decoding BIT STRING size %ld",
1770 ret = per_get_many_bits(pd, st->buf, 0,
1771 unit_bits * csiz->upper_bound);
1773 if(ret < 0) RETURN(RC_WMORE);
1774 consumed_myself += unit_bits * csiz->upper_bound;
1775 st->buf[st->size] = 0;
1777 int ubs = (csiz->upper_bound & 0x7);
1778 st->bits_unused = ubs ? 8 - ubs : 0;
1791 /* Get the PER length */
1792 if (csiz->upper_bound - csiz->lower_bound == 0)
1793 /* Indefinite length case */
1794 raw_len = aper_get_length(pd, -1, csiz->effective_bits, &repeat);
1796 raw_len = aper_get_length(pd, csiz->upper_bound - csiz->lower_bound + 1, csiz->effective_bits, &repeat);
1798 if(raw_len < 0) RETURN(RC_WMORE);
1799 raw_len += csiz->lower_bound;
1801 ASN_DEBUG("Got PER length eb %ld, len %ld, %s (%s)",
1802 (long)csiz->effective_bits, (long)raw_len,
1803 repeat ? "repeat" : "once", td->name);
1805 if (raw_len > 2) { /* X.691 #16 NOTE 1 */
1806 if (aper_get_align(pd) < 0)
1811 len_bytes = raw_len * bpc;
1812 len_bits = len_bytes * unit_bits;
1815 len_bytes = (len_bits + 7) >> 3;
1817 st->bits_unused = 8 - (len_bits & 0x7);
1818 /* len_bits be multiple of 16K if repeat is set */
1820 p = REALLOC(st->buf, st->size + len_bytes + 1);
1821 if(!p) RETURN(RC_FAIL);
1822 st->buf = (uint8_t *)p;
1825 ret = OCTET_STRING_per_get_characters(pd,
1826 &st->buf[st->size], raw_len, bpc, unit_bits,
1827 cval->lower_bound, cval->upper_bound, pc);
1828 if(ret > 0) RETURN(RC_FAIL);
1830 ret = per_get_many_bits(pd, &st->buf[st->size],
1833 if(ret < 0) RETURN(RC_WMORE);
1834 st->size += len_bytes;
1836 st->buf[st->size] = 0; /* nul-terminate */
1842 OCTET_STRING_encode_aper(const asn_TYPE_descriptor_t *td,
1843 const asn_per_constraints_t *constraints,
1844 const void *sptr, asn_per_outp_t *po) {
1846 const asn_OCTET_STRING_specifics_t *specs = td->specifics
1847 ? (const asn_OCTET_STRING_specifics_t *)td->specifics
1848 : &asn_SPC_OCTET_STRING_specs;
1849 const asn_per_constraints_t *pc = constraints ? constraints
1850 : td->encoding_constraints.per_constraints;
1851 const asn_per_constraint_t *cval;
1852 const asn_per_constraint_t *csiz;
1853 const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
1854 asn_enc_rval_t er = { 0, 0, 0 };
1855 int inext = 0; /* Lies not within extension root */
1856 unsigned int unit_bits;
1857 unsigned int canonical_unit_bits;
1858 unsigned int sizeinunits;
1866 } bpc; /* Bytes per character */
1869 if(!st || (!st->buf && st->size))
1876 cval = &asn_DEF_OCTET_STRING_constraints.value;
1877 csiz = &asn_DEF_OCTET_STRING_constraints.size;
1879 ct_extensible = csiz->flags & APC_EXTENSIBLE;
1881 switch(specs->subvariant) {
1883 /* case ASN_OSUBV_ANY:
1887 canonical_unit_bits = unit_bits = 1;
1889 sizeinunits = st->size * 8 - (st->bits_unused & 0x07);
1890 ASN_DEBUG("BIT STRING of %d bytes",
1895 canonical_unit_bits = unit_bits = 8;
1896 /* if(cval->flags & APC_CONSTRAINED)
1900 sizeinunits = st->size;
1903 canonical_unit_bits = unit_bits = 16;
1904 if(cval->flags & APC_CONSTRAINED)
1905 unit_bits = cval->range_bits;
1907 sizeinunits = st->size / 2;
1910 canonical_unit_bits = unit_bits = 32;
1911 if(cval->flags & APC_CONSTRAINED)
1912 unit_bits = cval->range_bits;
1914 sizeinunits = st->size / 4;
1918 ASN_DEBUG("Encoding %s into %d units of %d bits"
1919 " (%ld..%ld, effective %d)%s",
1920 td->name, sizeinunits, unit_bits,
1921 csiz->lower_bound, csiz->upper_bound,
1922 csiz->effective_bits, ct_extensible ? " EXT" : "");
1924 /* Figure out wheter size lies within PER visible constraint */
1926 if(csiz->effective_bits >= 0) {
1927 if((int)sizeinunits < csiz->lower_bound
1928 || (int)sizeinunits > csiz->upper_bound) {
1930 cval = &asn_DEF_OCTET_STRING_constraints.value;
1931 csiz = &asn_DEF_OCTET_STRING_constraints.size;
1932 unit_bits = canonical_unit_bits;
1943 /* Declare whether length is [not] within extension root */
1944 if(per_put_few_bits(po, inext, 1))
1948 /* X.691, #16.5: zero-length encoding */
1949 /* X.691, #16.6: short fixed length encoding (up to 2 octets) */
1950 /* X.691, #16.7: long fixed length encoding (up to 64K octets) */
1951 if(csiz->effective_bits >= 0) {
1952 ASN_DEBUG("Encoding %lu bytes (%ld), length in %d bits",
1953 st->size, sizeinunits - csiz->lower_bound,
1954 csiz->effective_bits);
1955 if (csiz->effective_bits > 0) {
1956 ret = aper_put_length(po, csiz->upper_bound - csiz->lower_bound + 1, sizeinunits - csiz->lower_bound);
1957 if(ret) ASN__ENCODE_FAILED;
1960 AFAIU if lb != ub it is aligned whatever the number of bits */
1961 if ((st->size > 2) || (csiz->lower_bound != csiz->upper_bound)) { /* X.691 #16.11 */
1962 if (aper_put_align(po) < 0)
1966 ret = OCTET_STRING_per_put_characters(po, st->buf,
1967 sizeinunits, bpc, unit_bits,
1968 cval->lower_bound, cval->upper_bound, pc);
1970 ret = per_put_many_bits(po, st->buf,
1971 sizeinunits * unit_bits);
1973 if(ret) ASN__ENCODE_FAILED;
1974 ASN__ENCODED_OK(er);
1977 ASN_DEBUG("Encoding %lu bytes", st->size);
1979 if(sizeinunits == 0) {
1980 if(aper_put_length(po, -1, 0))
1982 ASN__ENCODED_OK(er);
1986 while(sizeinunits) {
1987 ssize_t maySave = aper_put_length(po, -1, sizeinunits);
1989 if(maySave < 0) ASN__ENCODE_FAILED;
1991 ASN_DEBUG("Encoding %ld of %ld",
1992 (long)maySave, (long)sizeinunits);
1995 ret = OCTET_STRING_per_put_characters(po, buf,
1996 maySave, bpc, unit_bits,
1997 cval->lower_bound, cval->upper_bound, pc);
1999 ret = per_put_many_bits(po, buf, maySave * unit_bits);
2001 if(ret) ASN__ENCODE_FAILED;
2004 buf += maySave * bpc;
2006 buf += maySave >> 3;
2007 sizeinunits -= maySave;
2008 assert(!(maySave & 0x07) || !sizeinunits);
2011 ASN__ENCODED_OK(er);
2014 #endif /* ASN_DISABLE_PER_SUPPORT */
2017 OCTET_STRING_print(const asn_TYPE_descriptor_t *td, const void *sptr,
2018 int ilevel, asn_app_consume_bytes_f *cb, void *app_key) {
2019 const char * const h2c = "0123456789ABCDEF";
2020 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
2021 char scratch[16 * 3 + 4];
2027 (void)td; /* Unused argument */
2029 if(!st || (!st->buf && st->size))
2030 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
2033 * Dump the contents of the buffer in hexadecimal.
2036 end = buf + st->size;
2037 for(i = 0; buf < end; buf++, i++) {
2038 if(!(i % 16) && (i || st->size > 16)) {
2039 if(cb(scratch, p - scratch, app_key) < 0)
2044 *p++ = h2c[(*buf >> 4) & 0x0F];
2045 *p++ = h2c[*buf & 0x0F];
2050 p--; /* Remove the tail space */
2051 if(cb(scratch, p - scratch, app_key) < 0)
2059 OCTET_STRING_print_utf8(const asn_TYPE_descriptor_t *td, const void *sptr,
2060 int ilevel, asn_app_consume_bytes_f *cb,
2062 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
2064 (void)td; /* Unused argument */
2065 (void)ilevel; /* Unused argument */
2067 if(st && (st->buf || !st->size)) {
2068 return (cb(st->buf, st->size, app_key) < 0) ? -1 : 0;
2070 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
2075 OCTET_STRING_free(const asn_TYPE_descriptor_t *td, void *sptr,
2076 enum asn_struct_free_method method) {
2077 OCTET_STRING_t *st = (OCTET_STRING_t *)sptr;
2078 const asn_OCTET_STRING_specifics_t *specs;
2079 asn_struct_ctx_t *ctx;
2080 struct _stack *stck;
2085 specs = td->specifics
2086 ? (const asn_OCTET_STRING_specifics_t *)td->specifics
2087 : &asn_SPC_OCTET_STRING_specs;
2088 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
2090 ASN_DEBUG("Freeing %s as OCTET STRING", td->name);
2098 * Remove decode-time stack.
2100 stck = (struct _stack *)ctx->ptr;
2103 struct _stack_el *sel = stck->tail;
2104 stck->tail = sel->prev;
2111 case ASFM_FREE_EVERYTHING:
2114 case ASFM_FREE_UNDERLYING:
2116 case ASFM_FREE_UNDERLYING_AND_RESET:
2119 ? ((const asn_OCTET_STRING_specifics_t *)(td->specifics))
2121 : sizeof(OCTET_STRING_t));
2127 * Conversion routines.
2130 OCTET_STRING_fromBuf(OCTET_STRING_t *st, const char *str, int len) {
2133 if(st == 0 || (str == 0 && len)) {
2139 * Clear the OCTET STRING.
2148 /* Determine the original string size, if not explicitly given */
2152 /* Allocate and fill the memory */
2153 buf = MALLOC(len + 1);
2157 memcpy(buf, str, len);
2158 ((uint8_t *)buf)[len] = '\0'; /* Couldn't use memcpy(len+1)! */
2160 st->buf = (uint8_t *)buf;
2167 OCTET_STRING_new_fromBuf(const asn_TYPE_descriptor_t *td, const char *str,
2169 const asn_OCTET_STRING_specifics_t *specs =
2170 td->specifics ? (const asn_OCTET_STRING_specifics_t *)td->specifics
2171 : &asn_SPC_OCTET_STRING_specs;
2174 st = (OCTET_STRING_t *)CALLOC(1, specs->struct_size);
2175 if(st && str && OCTET_STRING_fromBuf(st, str, len)) {
2184 * Lexicographically compare the common prefix of both strings,
2185 * and if it is the same return -1 for the smallest string.
2188 OCTET_STRING_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
2190 const asn_OCTET_STRING_specifics_t *specs = td->specifics;
2191 const OCTET_STRING_t *a = aptr;
2192 const OCTET_STRING_t *b = bptr;
2194 assert(!specs || specs->subvariant != ASN_OSUBV_BIT);
2197 size_t common_prefix_size = a->size <= b->size ? a->size : b->size;
2198 int ret = memcmp(a->buf, b->buf, common_prefix_size);
2200 /* Figure out which string with equal prefixes is longer. */
2201 if(a->size < b->size) {
2203 } else if(a->size > b->size) {
2209 return ret < 0 ? -1 : 1;
2211 } else if(!a && !b) {
2222 * Biased function for randomizing character values around their limits.
2225 OCTET_STRING__random_char(unsigned long lb, unsigned long ub) {
2227 switch(asn_random_between(0, 16)) {
2229 if(lb < ub) return lb + 1;
2234 if(lb < ub) return ub - 1;
2239 return asn_random_between(lb, ub);
2245 OCTET_STRING_random_length_constrained(
2246 const asn_TYPE_descriptor_t *td,
2247 const asn_encoding_constraints_t *constraints, size_t max_length) {
2248 const unsigned lengths[] = {0, 1, 2, 3, 4, 8,
2249 126, 127, 128, 16383, 16384, 16385,
2250 65534, 65535, 65536, 65537};
2253 /* Figure out how far we should go */
2254 rnd_len = lengths[asn_random_between(
2255 0, sizeof(lengths) / sizeof(lengths[0]) - 1)];
2257 if(!constraints || !constraints->per_constraints)
2258 constraints = &td->encoding_constraints;
2259 if(constraints->per_constraints) {
2260 const asn_per_constraint_t *pc = &constraints->per_constraints->size;
2261 if(pc->flags & APC_CONSTRAINED) {
2262 long suggested_upper_bound = pc->upper_bound < (ssize_t)max_length
2264 : (ssize_t)max_length;
2265 if(max_length <= (size_t)pc->lower_bound) {
2266 return pc->lower_bound;
2268 if(pc->flags & APC_EXTENSIBLE) {
2269 switch(asn_random_between(0, 5)) {
2271 if(pc->lower_bound > 0) {
2272 rnd_len = pc->lower_bound - 1;
2277 rnd_len = pc->upper_bound + 1;
2280 /* Keep rnd_len from the table */
2281 if(rnd_len <= max_length) {
2286 rnd_len = asn_random_between(pc->lower_bound,
2287 suggested_upper_bound);
2291 asn_random_between(pc->lower_bound, suggested_upper_bound);
2294 rnd_len = asn_random_between(0, max_length);
2296 } else if(rnd_len > max_length) {
2297 rnd_len = asn_random_between(0, max_length);
2303 asn_random_fill_result_t
2304 OCTET_STRING_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
2305 const asn_encoding_constraints_t *constraints,
2306 size_t max_length) {
2307 const asn_OCTET_STRING_specifics_t *specs = td->specifics
2308 ? (const asn_OCTET_STRING_specifics_t *)td->specifics
2309 : &asn_SPC_OCTET_STRING_specs;
2310 asn_random_fill_result_t result_ok = {ARFILL_OK, 1};
2311 asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0};
2312 asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0};
2313 unsigned int unit_bytes = 1;
2314 unsigned long clb = 0; /* Lower bound on char */
2315 unsigned long cub = 255; /* Higher bound on char value */
2322 if(max_length == 0 && !*sptr) return result_skipped;
2324 switch(specs->subvariant) {
2327 return result_failed;
2329 /* Handled by BIT_STRING itself. */
2330 return result_failed;
2348 if(!constraints || !constraints->per_constraints)
2349 constraints = &td->encoding_constraints;
2350 if(constraints->per_constraints) {
2351 const asn_per_constraint_t *pc = &constraints->per_constraints->value;
2352 if(pc->flags & APC_SEMI_CONSTRAINED) {
2353 clb = pc->lower_bound;
2354 } else if(pc->flags & APC_CONSTRAINED) {
2355 clb = pc->lower_bound;
2356 cub = pc->upper_bound;
2361 OCTET_STRING_random_length_constrained(td, constraints, max_length);
2363 buf = CALLOC(unit_bytes, rnd_len + 1);
2364 if(!buf) return result_failed;
2366 bend = &buf[unit_bytes * rnd_len];
2368 switch(unit_bytes) {
2370 for(b = buf; b < bend; b += unit_bytes) {
2371 *(uint8_t *)b = OCTET_STRING__random_char(clb, cub);
2376 for(b = buf; b < bend; b += unit_bytes) {
2377 uint32_t code = OCTET_STRING__random_char(clb, cub);
2384 for(b = buf; b < bend; b += unit_bytes) {
2385 uint32_t code = OCTET_STRING__random_char(clb, cub);
2399 st = (OCTET_STRING_t *)(*sptr = CALLOC(1, specs->struct_size));
2402 return result_failed;
2407 st->size = unit_bytes * rnd_len;
2409 result_ok.length = st->size;