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