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