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