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