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