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