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