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