Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | /*- |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 2 | * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved. |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 3 | * Redistribution and modifications are permitted subject to BSD license. |
| 4 | */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 5 | #include <asn_internal.h> |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 6 | #include <OCTET_STRING.h> |
Lev Walkin | bbd9325 | 2004-10-12 05:57:23 +0000 | [diff] [blame] | 7 | #include <BIT_STRING.h> /* for .bits_unused member */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 8 | #include <assert.h> |
| 9 | #include <errno.h> |
| 10 | |
| 11 | /* |
| 12 | * OCTET STRING basic type description. |
| 13 | */ |
Lev Walkin | de4825d | 2004-09-29 13:20:14 +0000 | [diff] [blame] | 14 | static 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 | }; |
Lev Walkin | bbd9325 | 2004-10-12 05:57:23 +0000 | [diff] [blame] | 17 | static asn_OCTET_STRING_specifics_t asn_DEF_OCTET_STRING_specs = { |
| 18 | sizeof(OCTET_STRING_t), |
| 19 | offsetof(OCTET_STRING_t, _asn_ctx), |
| 20 | 0 |
| 21 | }; |
Lev Walkin | de4825d | 2004-09-29 13:20:14 +0000 | [diff] [blame] | 22 | asn_TYPE_descriptor_t asn_DEF_OCTET_STRING = { |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 23 | "OCTET STRING", /* Canonical name */ |
| 24 | "OCTET_STRING", /* XML tag name */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 25 | OCTET_STRING_free, |
| 26 | OCTET_STRING_print, /* non-ascii stuff, generally */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 27 | asn_generic_no_constraint, |
| 28 | OCTET_STRING_decode_ber, |
| 29 | OCTET_STRING_encode_der, |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 30 | OCTET_STRING_decode_xer_hex, |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 31 | OCTET_STRING_encode_xer, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 32 | 0, /* Use generic outmost tag fetcher */ |
Lev Walkin | de4825d | 2004-09-29 13:20:14 +0000 | [diff] [blame] | 33 | asn_DEF_OCTET_STRING_tags, |
| 34 | sizeof(asn_DEF_OCTET_STRING_tags) |
| 35 | / sizeof(asn_DEF_OCTET_STRING_tags[0]), |
| 36 | asn_DEF_OCTET_STRING_tags, /* Same as above */ |
| 37 | sizeof(asn_DEF_OCTET_STRING_tags) |
| 38 | / sizeof(asn_DEF_OCTET_STRING_tags[0]), |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 39 | 0, 0, /* No members */ |
Lev Walkin | bbd9325 | 2004-10-12 05:57:23 +0000 | [diff] [blame] | 40 | &asn_DEF_OCTET_STRING_specs |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 41 | }; |
| 42 | |
Lev Walkin | cc6a910 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 43 | #undef _CH_PHASE |
| 44 | #undef NEXT_PHASE |
| 45 | #undef PREV_PHASE |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 46 | #define _CH_PHASE(ctx, inc) do { \ |
| 47 | if(ctx->phase == 0) \ |
| 48 | ctx->step = 0; \ |
| 49 | ctx->phase += inc; \ |
| 50 | } while(0) |
| 51 | #define NEXT_PHASE(ctx) _CH_PHASE(ctx, +1) |
| 52 | #define PREV_PHASE(ctx) _CH_PHASE(ctx, -1) |
| 53 | |
Lev Walkin | cc6a910 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 54 | #undef ADVANCE |
Lev Walkin | 4ce78ca | 2004-08-25 01:34:11 +0000 | [diff] [blame] | 55 | #define ADVANCE(num_bytes) do { \ |
Lev Walkin | 3990ba6 | 2004-09-24 20:57:41 +0000 | [diff] [blame] | 56 | size_t num = (num_bytes); \ |
Lev Walkin | 4ce78ca | 2004-08-25 01:34:11 +0000 | [diff] [blame] | 57 | buf_ptr = ((char *)buf_ptr) + num; \ |
| 58 | size -= num; \ |
| 59 | consumed_myself += num; \ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 60 | } while(0) |
| 61 | |
Lev Walkin | cc6a910 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 62 | #undef RETURN |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 63 | #define RETURN(_code) do { \ |
| 64 | rval.code = _code; \ |
| 65 | rval.consumed = consumed_myself;\ |
| 66 | return rval; \ |
| 67 | } while(0) |
| 68 | |
Lev Walkin | cc6a910 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 69 | #undef APPEND |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 70 | #define APPEND(bufptr, bufsize) do { \ |
Lev Walkin | 8d12787 | 2004-09-04 04:44:50 +0000 | [diff] [blame] | 71 | size_t _bs = (bufsize); \ |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 72 | size_t _ns = ctx->step; /* Allocated */ \ |
Lev Walkin | 8d12787 | 2004-09-04 04:44:50 +0000 | [diff] [blame] | 73 | if(_ns <= (size_t)(st->size + _bs)) { \ |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 74 | void *ptr; \ |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 75 | /* Be nice and round to the memory allocator */ \ |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 76 | do { _ns = _ns ? _ns<<2 : 16; } \ |
Lev Walkin | 8d12787 | 2004-09-04 04:44:50 +0000 | [diff] [blame] | 77 | while(_ns <= (size_t)(st->size + _bs)); \ |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 78 | ptr = REALLOC(st->buf, _ns); \ |
| 79 | if(ptr) { \ |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 80 | st->buf = (uint8_t *)ptr; \ |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 81 | ctx->step = _ns; \ |
| 82 | } else { \ |
| 83 | RETURN(RC_FAIL); \ |
| 84 | } \ |
| 85 | } \ |
Lev Walkin | 8d12787 | 2004-09-04 04:44:50 +0000 | [diff] [blame] | 86 | memcpy(st->buf + st->size, bufptr, _bs); \ |
| 87 | st->size += _bs; \ |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 88 | if(st->size < 0) \ |
| 89 | /* Why even care?.. JIC */ \ |
| 90 | RETURN(RC_FAIL); \ |
| 91 | /* Convenient nul-termination */ \ |
| 92 | st->buf[st->size] = '\0'; \ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 93 | } while(0) |
| 94 | |
| 95 | /* |
Lev Walkin | 07f388c | 2004-10-11 11:43:08 +0000 | [diff] [blame] | 96 | * Internal variant of the OCTET STRING. |
| 97 | */ |
| 98 | typedef enum OS_type { |
| 99 | _TT_GENERIC = 0, /* Just a random OCTET STRING */ |
| 100 | _TT_BIT_STRING = 1, /* BIT STRING type, a special case */ |
| 101 | _TT_ANY = 2, /* ANY type, a special case too */ |
| 102 | } OS_type_e; |
| 103 | |
| 104 | /* |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 105 | * The main reason why ASN.1 is still alive is that too much time and effort |
| 106 | * is necessary for learning it more or less adequately, thus creating a gut |
| 107 | * necessity to demonstrate that aquired skill everywhere afterwards. |
| 108 | * No, I am not going to explain what the following stuff is. |
| 109 | */ |
| 110 | struct _stack_el { |
Lev Walkin | 3990ba6 | 2004-09-24 20:57:41 +0000 | [diff] [blame] | 111 | ber_tlv_len_t left; /* What's left to read (or -1) */ |
Lev Walkin | 5c91599 | 2004-09-27 20:54:06 +0000 | [diff] [blame] | 112 | ber_tlv_len_t got; /* What was actually processed */ |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 113 | int cont_level; /* Depth of subcontainment */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 114 | int want_nulls; /* Want null "end of content" octets? */ |
| 115 | int bits_chopped; /* Flag in BIT STRING mode */ |
Lev Walkin | 5c91599 | 2004-09-27 20:54:06 +0000 | [diff] [blame] | 116 | ber_tlv_tag_t tag; /* For debugging purposes */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 117 | struct _stack_el *prev; |
| 118 | struct _stack_el *next; |
| 119 | }; |
| 120 | struct _stack { |
| 121 | struct _stack_el *tail; |
| 122 | struct _stack_el *cur_ptr; |
| 123 | }; |
| 124 | |
| 125 | static struct _stack_el * |
Lev Walkin | 3990ba6 | 2004-09-24 20:57:41 +0000 | [diff] [blame] | 126 | OS__add_stack_el(struct _stack *st) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 127 | struct _stack_el *nel; |
| 128 | |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 129 | /* |
| 130 | * Reuse the old stack frame or allocate a new one. |
| 131 | */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 132 | if(st->cur_ptr && st->cur_ptr->next) { |
| 133 | nel = st->cur_ptr->next; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 134 | nel->bits_chopped = 0; |
Lev Walkin | 5c91599 | 2004-09-27 20:54:06 +0000 | [diff] [blame] | 135 | nel->got = 0; |
| 136 | /* Retain the nel->cont_level, it's correct. */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 137 | } else { |
Lev Walkin | 814cca7 | 2004-12-15 23:23:53 +0000 | [diff] [blame] | 138 | nel = (struct _stack_el *)CALLOC(1, sizeof(struct _stack_el)); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 139 | if(nel == NULL) |
| 140 | return NULL; |
| 141 | |
| 142 | if(st->tail) { |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 143 | /* Increase a subcontainment depth */ |
| 144 | nel->cont_level = st->tail->cont_level + 1; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 145 | st->tail->next = nel; |
| 146 | } |
| 147 | nel->prev = st->tail; |
| 148 | st->tail = nel; |
| 149 | } |
| 150 | |
| 151 | st->cur_ptr = nel; |
| 152 | |
| 153 | return nel; |
| 154 | } |
| 155 | |
| 156 | static struct _stack * |
| 157 | _new_stack() { |
Lev Walkin | bbd9325 | 2004-10-12 05:57:23 +0000 | [diff] [blame] | 158 | return (struct _stack *)CALLOC(1, sizeof(struct _stack)); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | /* |
| 162 | * Decode OCTET STRING type. |
| 163 | */ |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 164 | asn_dec_rval_t |
Lev Walkin | de4825d | 2004-09-29 13:20:14 +0000 | [diff] [blame] | 165 | OCTET_STRING_decode_ber(asn_codec_ctx_t *opt_codec_ctx, |
| 166 | asn_TYPE_descriptor_t *td, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 167 | void **os_structure, void *buf_ptr, size_t size, int tag_mode) { |
Lev Walkin | bbd9325 | 2004-10-12 05:57:23 +0000 | [diff] [blame] | 168 | asn_OCTET_STRING_specifics_t *specs = td->specifics |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 169 | ? (asn_OCTET_STRING_specifics_t *)td->specifics |
| 170 | : &asn_DEF_OCTET_STRING_specs; |
Lev Walkin | bbd9325 | 2004-10-12 05:57:23 +0000 | [diff] [blame] | 171 | BIT_STRING_t *st = (BIT_STRING_t *)*os_structure; |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 172 | asn_dec_rval_t rval; |
Lev Walkin | de4825d | 2004-09-29 13:20:14 +0000 | [diff] [blame] | 173 | asn_struct_ctx_t *ctx; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 174 | ssize_t consumed_myself = 0; |
Lev Walkin | bbd9325 | 2004-10-12 05:57:23 +0000 | [diff] [blame] | 175 | struct _stack *stck; /* Expectations stack structure */ |
Lev Walkin | 5c91599 | 2004-09-27 20:54:06 +0000 | [diff] [blame] | 176 | struct _stack_el *sel = 0; /* Stack element */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 177 | int tlv_constr; |
Lev Walkin | bbd9325 | 2004-10-12 05:57:23 +0000 | [diff] [blame] | 178 | OS_type_e type_variant = (OS_type_e)specs->subvariant; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 179 | |
Lev Walkin | cc6a910 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 180 | ASN_DEBUG("Decoding %s as %s (frame %ld)", |
| 181 | td->name, |
Lev Walkin | bbd9325 | 2004-10-12 05:57:23 +0000 | [diff] [blame] | 182 | (type_variant == _TT_GENERIC) ? |
| 183 | "OCTET STRING" : "OS-SpecialCase", |
Lev Walkin | cc6a910 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 184 | (long)size); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 185 | |
| 186 | /* |
| 187 | * Create the string if does not exist. |
| 188 | */ |
| 189 | if(st == NULL) { |
Lev Walkin | 814cca7 | 2004-12-15 23:23:53 +0000 | [diff] [blame] | 190 | *os_structure = CALLOC(1, specs->struct_size); |
| 191 | st = (BIT_STRING_t *)*os_structure; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 192 | if(st == NULL) |
| 193 | RETURN(RC_FAIL); |
| 194 | } |
| 195 | |
| 196 | /* Restore parsing context */ |
Lev Walkin | bbd9325 | 2004-10-12 05:57:23 +0000 | [diff] [blame] | 197 | ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 198 | |
| 199 | switch(ctx->phase) { |
| 200 | case 0: |
| 201 | /* |
| 202 | * Check tags. |
| 203 | */ |
Lev Walkin | de4825d | 2004-09-29 13:20:14 +0000 | [diff] [blame] | 204 | rval = ber_check_tags(opt_codec_ctx, td, ctx, |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 205 | buf_ptr, size, tag_mode, -1, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 206 | &ctx->left, &tlv_constr); |
Lev Walkin | 443d251 | 2004-10-05 06:35:31 +0000 | [diff] [blame] | 207 | if(rval.code != RC_OK) |
| 208 | return rval; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 209 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 210 | if(tlv_constr) { |
| 211 | /* |
| 212 | * Complex operation, requires stack of expectations. |
| 213 | */ |
| 214 | ctx->ptr = _new_stack(); |
| 215 | if(ctx->ptr) { |
Lev Walkin | 814cca7 | 2004-12-15 23:23:53 +0000 | [diff] [blame] | 216 | stck = (struct _stack *)ctx->ptr; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 217 | } else { |
| 218 | RETURN(RC_FAIL); |
| 219 | } |
| 220 | } else { |
| 221 | /* |
| 222 | * Jump into stackless primitive decoding. |
| 223 | */ |
| 224 | _CH_PHASE(ctx, 3); |
Lev Walkin | bbd9325 | 2004-10-12 05:57:23 +0000 | [diff] [blame] | 225 | if(type_variant == _TT_ANY && tag_mode != 1) |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 226 | APPEND(buf_ptr, rval.consumed); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 227 | ADVANCE(rval.consumed); |
| 228 | goto phase3; |
| 229 | } |
| 230 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 231 | NEXT_PHASE(ctx); |
| 232 | /* Fall through */ |
| 233 | case 1: |
| 234 | phase1: |
| 235 | /* |
| 236 | * Fill the stack with expectations. |
| 237 | */ |
Lev Walkin | 814cca7 | 2004-12-15 23:23:53 +0000 | [diff] [blame] | 238 | stck = (struct _stack *)ctx->ptr; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 239 | sel = stck->cur_ptr; |
| 240 | do { |
| 241 | ber_tlv_tag_t tlv_tag; |
| 242 | ber_tlv_len_t tlv_len; |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 243 | ber_tlv_tag_t expected_tag; |
Lev Walkin | 5c91599 | 2004-09-27 20:54:06 +0000 | [diff] [blame] | 244 | ssize_t tl, ll, tlvl; |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 245 | /* This one works even if (sel->left == -1) */ |
| 246 | ssize_t Left = ((!sel||(size_t)sel->left >= size) |
Lev Walkin | 5c91599 | 2004-09-27 20:54:06 +0000 | [diff] [blame] | 247 | ?(ssize_t)size:sel->left); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 248 | |
Lev Walkin | 3990ba6 | 2004-09-24 20:57:41 +0000 | [diff] [blame] | 249 | |
Lev Walkin | abf6889 | 2004-10-26 10:12:14 +0000 | [diff] [blame] | 250 | 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] | 251 | (long)(sel?sel->left:0), |
| 252 | (long)(sel?sel->want_nulls:0), |
| 253 | (long)(sel?sel->got:0) |
Lev Walkin | 5c91599 | 2004-09-27 20:54:06 +0000 | [diff] [blame] | 254 | ); |
| 255 | if(sel && sel->left <= 0 && sel->want_nulls == 0) { |
| 256 | if(sel->prev) { |
| 257 | struct _stack_el *prev = sel->prev; |
| 258 | if(prev->left != -1) { |
| 259 | if(prev->left < sel->got) |
| 260 | RETURN(RC_FAIL); |
| 261 | prev->left -= sel->got; |
| 262 | } |
| 263 | prev->got += sel->got; |
| 264 | sel = stck->cur_ptr = prev; |
| 265 | if(!sel) break; |
| 266 | tlv_constr = 1; |
| 267 | continue; |
| 268 | } else { |
| 269 | sel = stck->cur_ptr = 0; |
| 270 | break; /* Nothing to wait */ |
| 271 | } |
| 272 | } |
| 273 | |
Lev Walkin | 3990ba6 | 2004-09-24 20:57:41 +0000 | [diff] [blame] | 274 | tl = ber_fetch_tag(buf_ptr, Left, &tlv_tag); |
Lev Walkin | abf6889 | 2004-10-26 10:12:14 +0000 | [diff] [blame] | 275 | ASN_DEBUG("fetch tag(size=%ld,L=%ld), %sstack, left=%ld, wn=%ld, tl=%ld", |
| 276 | (long)size, (long)Left, sel?"":"!", |
Lev Walkin | 1e3ccbb | 2004-10-26 10:56:10 +0000 | [diff] [blame] | 277 | (long)(sel?sel->left:0), |
| 278 | (long)(sel?sel->want_nulls:0), |
Lev Walkin | abf6889 | 2004-10-26 10:12:14 +0000 | [diff] [blame] | 279 | (long)tl); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 280 | switch(tl) { |
| 281 | case -1: RETURN(RC_FAIL); |
| 282 | case 0: RETURN(RC_WMORE); |
| 283 | } |
| 284 | |
| 285 | tlv_constr = BER_TLV_CONSTRUCTED(buf_ptr); |
| 286 | |
| 287 | ll = ber_fetch_length(tlv_constr, |
Lev Walkin | 3990ba6 | 2004-09-24 20:57:41 +0000 | [diff] [blame] | 288 | (char *)buf_ptr + tl, Left - tl, &tlv_len); |
Lev Walkin | 5c91599 | 2004-09-27 20:54:06 +0000 | [diff] [blame] | 289 | ASN_DEBUG("Got tag=%s, tc=%d, left=%ld, tl=%d, len=%d, ll=%d", |
Lev Walkin | 8d12787 | 2004-09-04 04:44:50 +0000 | [diff] [blame] | 290 | ber_tlv_tag_string(tlv_tag), tlv_constr, |
Lev Walkin | 5c91599 | 2004-09-27 20:54:06 +0000 | [diff] [blame] | 291 | (long)Left, tl, tlv_len, ll); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 292 | switch(ll) { |
| 293 | case -1: RETURN(RC_FAIL); |
| 294 | case 0: RETURN(RC_WMORE); |
| 295 | } |
| 296 | |
Lev Walkin | 8d12787 | 2004-09-04 04:44:50 +0000 | [diff] [blame] | 297 | if(sel && sel->want_nulls |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 298 | && ((uint8_t *)buf_ptr)[0] == 0 |
| 299 | && ((uint8_t *)buf_ptr)[1] == 0) |
| 300 | { |
Lev Walkin | 8d12787 | 2004-09-04 04:44:50 +0000 | [diff] [blame] | 301 | |
| 302 | ASN_DEBUG("Eat EOC; wn=%d--", sel->want_nulls); |
| 303 | |
Lev Walkin | bbd9325 | 2004-10-12 05:57:23 +0000 | [diff] [blame] | 304 | if(type_variant == _TT_ANY |
Lev Walkin | 07f388c | 2004-10-11 11:43:08 +0000 | [diff] [blame] | 305 | && (tag_mode != 1 || sel->cont_level)) |
| 306 | APPEND("\0\0", 2); |
Lev Walkin | 5c91599 | 2004-09-27 20:54:06 +0000 | [diff] [blame] | 307 | |
| 308 | ADVANCE(2); |
| 309 | sel->got += 2; |
| 310 | if(sel->left != -1) { |
| 311 | sel->left -= 2; /* assert(sel->left >= 2) */ |
| 312 | } |
| 313 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 314 | sel->want_nulls--; |
| 315 | if(sel->want_nulls == 0) { |
| 316 | /* Move to the next expectation */ |
Lev Walkin | 5c91599 | 2004-09-27 20:54:06 +0000 | [diff] [blame] | 317 | sel->left = 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 318 | tlv_constr = 1; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 319 | } |
Lev Walkin | 8d12787 | 2004-09-04 04:44:50 +0000 | [diff] [blame] | 320 | |
| 321 | continue; |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | /* |
| 325 | * Set up expected tags, |
| 326 | * depending on ASN.1 type being decoded. |
| 327 | */ |
Lev Walkin | bbd9325 | 2004-10-12 05:57:23 +0000 | [diff] [blame] | 328 | switch(type_variant) { |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 329 | case _TT_BIT_STRING: |
| 330 | /* X.690: 8.6.4.1, NOTE 2 */ |
| 331 | /* Fall through */ |
| 332 | case _TT_GENERIC: |
| 333 | default: |
| 334 | if(sel) { |
| 335 | int level = sel->cont_level; |
| 336 | if(level < td->all_tags_count) { |
| 337 | expected_tag = td->all_tags[level]; |
| 338 | break; |
| 339 | } else if(td->all_tags_count) { |
| 340 | expected_tag = td->all_tags |
| 341 | [td->all_tags_count - 1]; |
| 342 | break; |
| 343 | } |
| 344 | /* else, Fall through */ |
| 345 | } |
| 346 | /* Fall through */ |
| 347 | case _TT_ANY: |
| 348 | expected_tag = tlv_tag; |
| 349 | break; |
| 350 | } |
| 351 | |
| 352 | |
| 353 | if(tlv_tag != expected_tag) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 354 | char buf[2][32]; |
| 355 | ber_tlv_tag_snprint(tlv_tag, |
| 356 | buf[0], sizeof(buf[0])); |
| 357 | ber_tlv_tag_snprint(td->tags[td->tags_count-1], |
| 358 | buf[1], sizeof(buf[1])); |
| 359 | ASN_DEBUG("Tag does not match expectation: %s != %s", |
| 360 | buf[0], buf[1]); |
| 361 | RETURN(RC_FAIL); |
| 362 | } |
| 363 | |
Lev Walkin | de4825d | 2004-09-29 13:20:14 +0000 | [diff] [blame] | 364 | tlvl = tl + ll; /* Combined length of T and L encoding */ |
| 365 | if((tlv_len + tlvl) < 0) { |
| 366 | /* tlv_len value is too big */ |
| 367 | ASN_DEBUG("TLV encoding + length (%ld) is too big", |
| 368 | (long)tlv_len); |
| 369 | RETURN(RC_FAIL); |
| 370 | } |
| 371 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 372 | /* |
| 373 | * Append a new expectation. |
| 374 | */ |
Lev Walkin | 3990ba6 | 2004-09-24 20:57:41 +0000 | [diff] [blame] | 375 | sel = OS__add_stack_el(stck); |
Lev Walkin | 5c91599 | 2004-09-27 20:54:06 +0000 | [diff] [blame] | 376 | if(!sel) RETURN(RC_FAIL); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 377 | |
Lev Walkin | 5c91599 | 2004-09-27 20:54:06 +0000 | [diff] [blame] | 378 | sel->tag = tlv_tag; |
| 379 | |
| 380 | sel->want_nulls = (tlv_len==-1); |
| 381 | if(sel->prev && sel->prev->left != -1) { |
| 382 | /* Check that the parent frame is big enough */ |
| 383 | if(sel->prev->left < tlvl + (tlv_len==-1?0:tlv_len)) |
| 384 | RETURN(RC_FAIL); |
| 385 | if(tlv_len == -1) |
| 386 | sel->left = sel->prev->left - tlvl; |
| 387 | else |
| 388 | sel->left = tlv_len; |
| 389 | } else { |
| 390 | sel->left = tlv_len; |
| 391 | } |
Lev Walkin | bbd9325 | 2004-10-12 05:57:23 +0000 | [diff] [blame] | 392 | if(type_variant == _TT_ANY |
Lev Walkin | 07f388c | 2004-10-11 11:43:08 +0000 | [diff] [blame] | 393 | && (tag_mode != 1 || sel->cont_level)) |
| 394 | APPEND(buf_ptr, tlvl); |
Lev Walkin | 5c91599 | 2004-09-27 20:54:06 +0000 | [diff] [blame] | 395 | sel->got += tlvl; |
| 396 | ADVANCE(tlvl); |
| 397 | |
Lev Walkin | abf6889 | 2004-10-26 10:12:14 +0000 | [diff] [blame] | 398 | ASN_DEBUG("+EXPECT2 got=%ld left=%ld, wn=%d, clvl=%d", |
| 399 | (long)sel->got, (long)sel->left, |
| 400 | sel->want_nulls, sel->cont_level); |
Lev Walkin | 5c91599 | 2004-09-27 20:54:06 +0000 | [diff] [blame] | 401 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 402 | } while(tlv_constr); |
| 403 | if(sel == NULL) { |
| 404 | /* Finished operation, "phase out" */ |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 405 | ASN_DEBUG("Phase out"); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 406 | _CH_PHASE(ctx, +3); |
| 407 | break; |
| 408 | } |
| 409 | |
| 410 | NEXT_PHASE(ctx); |
| 411 | /* Fall through */ |
| 412 | case 2: |
Lev Walkin | 814cca7 | 2004-12-15 23:23:53 +0000 | [diff] [blame] | 413 | stck = (struct _stack *)ctx->ptr; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 414 | sel = stck->cur_ptr; |
Lev Walkin | 5c91599 | 2004-09-27 20:54:06 +0000 | [diff] [blame] | 415 | ASN_DEBUG("Phase 2: Need %ld bytes, size=%ld, alrg=%ld, wn=%d", |
| 416 | (long)sel->left, (long)size, (long)sel->got, |
| 417 | sel->want_nulls); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 418 | { |
| 419 | ber_tlv_len_t len; |
| 420 | |
| 421 | assert(sel->left >= 0); |
| 422 | |
Lev Walkin | ec1ffd4 | 2004-08-18 04:53:32 +0000 | [diff] [blame] | 423 | len = ((ber_tlv_len_t)size < sel->left) |
| 424 | ? (ber_tlv_len_t)size : sel->left; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 425 | if(len > 0) { |
Lev Walkin | bbd9325 | 2004-10-12 05:57:23 +0000 | [diff] [blame] | 426 | if(type_variant == _TT_BIT_STRING |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 427 | && sel->bits_chopped == 0) { |
Lev Walkin | bbd9325 | 2004-10-12 05:57:23 +0000 | [diff] [blame] | 428 | /* Put the unused-bits-octet away */ |
| 429 | st->bits_unused = *(uint8_t *)buf_ptr; |
Lev Walkin | 4d9528c | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 430 | APPEND(((char *)buf_ptr+1), (len - 1)); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 431 | sel->bits_chopped = 1; |
| 432 | } else { |
| 433 | APPEND(buf_ptr, len); |
| 434 | } |
| 435 | ADVANCE(len); |
| 436 | sel->left -= len; |
Lev Walkin | 5c91599 | 2004-09-27 20:54:06 +0000 | [diff] [blame] | 437 | sel->got += len; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 438 | } |
| 439 | |
Lev Walkin | 5c91599 | 2004-09-27 20:54:06 +0000 | [diff] [blame] | 440 | if(sel->left) { |
| 441 | ASN_DEBUG("OS left %ld, size = %ld, wn=%d\n", |
| 442 | (long)sel->left, (long)size, sel->want_nulls); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 443 | RETURN(RC_WMORE); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 444 | } |
Lev Walkin | 5c91599 | 2004-09-27 20:54:06 +0000 | [diff] [blame] | 445 | |
| 446 | PREV_PHASE(ctx); |
| 447 | goto phase1; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 448 | } |
| 449 | break; |
| 450 | case 3: |
| 451 | phase3: |
| 452 | /* |
| 453 | * Primitive form, no stack required. |
| 454 | */ |
Lev Walkin | bbd9325 | 2004-10-12 05:57:23 +0000 | [diff] [blame] | 455 | assert(ctx->left >= 0); |
| 456 | |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 457 | if(size < (size_t)ctx->left) { |
Lev Walkin | bbd9325 | 2004-10-12 05:57:23 +0000 | [diff] [blame] | 458 | if(!size) RETURN(RC_WMORE); |
| 459 | if(type_variant == _TT_BIT_STRING && ctx->step == 0) { |
| 460 | st->bits_unused = *(uint8_t *)buf_ptr; |
| 461 | ctx->left--; |
| 462 | ADVANCE(1); |
| 463 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 464 | APPEND(buf_ptr, size); |
Lev Walkin | bbd9325 | 2004-10-12 05:57:23 +0000 | [diff] [blame] | 465 | assert(ctx->step); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 466 | ctx->left -= size; |
| 467 | ADVANCE(size); |
| 468 | RETURN(RC_WMORE); |
| 469 | } else { |
Lev Walkin | bbd9325 | 2004-10-12 05:57:23 +0000 | [diff] [blame] | 470 | if(type_variant == _TT_BIT_STRING |
| 471 | && ctx->step == 0 && ctx->left) { |
| 472 | st->bits_unused = *(uint8_t *)buf_ptr; |
| 473 | ctx->left--; |
| 474 | ADVANCE(1); |
| 475 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 476 | APPEND(buf_ptr, ctx->left); |
| 477 | ADVANCE(ctx->left); |
| 478 | ctx->left = 0; |
| 479 | |
| 480 | NEXT_PHASE(ctx); |
| 481 | } |
| 482 | break; |
| 483 | } |
| 484 | |
Lev Walkin | 5c91599 | 2004-09-27 20:54:06 +0000 | [diff] [blame] | 485 | if(sel) { |
| 486 | ASN_DEBUG("3sel p=%p, wn=%d, l=%ld, g=%ld, size=%ld", |
| 487 | sel->prev, sel->want_nulls, |
| 488 | (long)sel->left, (long)sel->got, (long)size); |
| 489 | if(sel->prev || sel->want_nulls > 1 || sel->left > 0) { |
| 490 | RETURN(RC_WMORE); |
| 491 | } |
| 492 | } |
| 493 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 494 | /* |
| 495 | * BIT STRING-specific processing. |
| 496 | */ |
Lev Walkin | bbd9325 | 2004-10-12 05:57:23 +0000 | [diff] [blame] | 497 | if(type_variant == _TT_BIT_STRING && st->size) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 498 | /* Finalize BIT STRING: zero out unused bits. */ |
Lev Walkin | bbd9325 | 2004-10-12 05:57:23 +0000 | [diff] [blame] | 499 | st->buf[st->size-1] &= 0xff << st->bits_unused; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 500 | } |
| 501 | |
Lev Walkin | abf6889 | 2004-10-26 10:12:14 +0000 | [diff] [blame] | 502 | ASN_DEBUG("Took %ld bytes to encode %s: [%s]:%ld", |
| 503 | (long)consumed_myself, td->name, |
Lev Walkin | bbd9325 | 2004-10-12 05:57:23 +0000 | [diff] [blame] | 504 | (type_variant == _TT_GENERIC) ? (char *)st->buf : "<data>", |
Lev Walkin | abf6889 | 2004-10-26 10:12:14 +0000 | [diff] [blame] | 505 | (long)st->size); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 506 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 507 | |
Lev Walkin | 5c91599 | 2004-09-27 20:54:06 +0000 | [diff] [blame] | 508 | RETURN(RC_OK); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | /* |
| 512 | * Encode OCTET STRING type using DER. |
| 513 | */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 514 | asn_enc_rval_t |
Lev Walkin | bbd9325 | 2004-10-12 05:57:23 +0000 | [diff] [blame] | 515 | OCTET_STRING_encode_der(asn_TYPE_descriptor_t *td, void *sptr, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 516 | int tag_mode, ber_tlv_tag_t tag, |
| 517 | asn_app_consume_bytes_f *cb, void *app_key) { |
Lev Walkin | bbd9325 | 2004-10-12 05:57:23 +0000 | [diff] [blame] | 518 | asn_enc_rval_t er; |
| 519 | asn_OCTET_STRING_specifics_t *specs = td->specifics |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 520 | ? (asn_OCTET_STRING_specifics_t *)td->specifics |
| 521 | : &asn_DEF_OCTET_STRING_specs; |
Lev Walkin | bbd9325 | 2004-10-12 05:57:23 +0000 | [diff] [blame] | 522 | BIT_STRING_t *st = (BIT_STRING_t *)sptr; |
| 523 | OS_type_e type_variant = (OS_type_e)specs->subvariant; |
| 524 | int fix_last_byte = 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 525 | |
| 526 | ASN_DEBUG("%s %s as OCTET STRING", |
Lev Walkin | 1f670c1 | 2004-09-02 12:57:25 +0000 | [diff] [blame] | 527 | cb?"Estimating":"Encoding", td->name); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 528 | |
| 529 | /* |
Lev Walkin | bbd9325 | 2004-10-12 05:57:23 +0000 | [diff] [blame] | 530 | * Write tags. |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 531 | */ |
Lev Walkin | bbd9325 | 2004-10-12 05:57:23 +0000 | [diff] [blame] | 532 | if(type_variant != _TT_ANY || tag_mode == 1) { |
| 533 | er.encoded = der_write_tags(td, |
| 534 | (type_variant == _TT_BIT_STRING) + st->size, |
| 535 | tag_mode, type_variant == _TT_ANY, tag, cb, app_key); |
| 536 | if(er.encoded == -1) { |
| 537 | er.failed_type = td; |
| 538 | er.structure_ptr = sptr; |
| 539 | return er; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 540 | } |
Lev Walkin | bbd9325 | 2004-10-12 05:57:23 +0000 | [diff] [blame] | 541 | } else { |
| 542 | /* Disallow: [<tag>] IMPLICIT ANY */ |
| 543 | assert(type_variant != _TT_ANY || tag_mode != -1); |
| 544 | er.encoded = 0; |
| 545 | } |
| 546 | |
| 547 | if(!cb) { |
| 548 | er.encoded += (type_variant == _TT_BIT_STRING) + st->size; |
| 549 | return er; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 550 | } |
| 551 | |
Lev Walkin | 07f388c | 2004-10-11 11:43:08 +0000 | [diff] [blame] | 552 | /* |
Lev Walkin | bbd9325 | 2004-10-12 05:57:23 +0000 | [diff] [blame] | 553 | * Prepare to deal with the last octet of BIT STRING. |
Lev Walkin | 07f388c | 2004-10-11 11:43:08 +0000 | [diff] [blame] | 554 | */ |
Lev Walkin | bbd9325 | 2004-10-12 05:57:23 +0000 | [diff] [blame] | 555 | if(type_variant == _TT_BIT_STRING) { |
| 556 | uint8_t b = st->bits_unused & 0x07; |
| 557 | if(b && st->size) fix_last_byte = 1; |
| 558 | _ASN_CALLBACK(&b, 1); |
| 559 | er.encoded++; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 560 | } |
| 561 | |
Lev Walkin | bbd9325 | 2004-10-12 05:57:23 +0000 | [diff] [blame] | 562 | /* Invoke callback for the main part of the buffer */ |
| 563 | _ASN_CALLBACK(st->buf, st->size - fix_last_byte); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 564 | |
Lev Walkin | bbd9325 | 2004-10-12 05:57:23 +0000 | [diff] [blame] | 565 | /* The last octet should be stripped off the unused bits */ |
| 566 | if(fix_last_byte) { |
| 567 | uint8_t b = st->buf[st->size-1] & (0xff << st->bits_unused); |
| 568 | _ASN_CALLBACK(&b, 1); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 569 | } |
| 570 | |
Lev Walkin | bbd9325 | 2004-10-12 05:57:23 +0000 | [diff] [blame] | 571 | er.encoded += st->size; |
| 572 | return er; |
| 573 | cb_failed: |
| 574 | _ASN_ENCODE_FAILED; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 575 | } |
| 576 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 577 | asn_enc_rval_t |
Lev Walkin | de4825d | 2004-09-29 13:20:14 +0000 | [diff] [blame] | 578 | OCTET_STRING_encode_xer(asn_TYPE_descriptor_t *td, void *sptr, |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 579 | int ilevel, enum xer_encoder_flags_e flags, |
| 580 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 581 | static const char *h2c = "0123456789ABCDEF"; |
| 582 | const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr; |
| 583 | asn_enc_rval_t er; |
| 584 | char scratch[16 * 3 + 4]; |
| 585 | char *p = scratch; |
| 586 | uint8_t *buf; |
| 587 | uint8_t *end; |
| 588 | size_t i; |
| 589 | |
Lev Walkin | 942fd08 | 2004-10-03 09:13:02 +0000 | [diff] [blame] | 590 | if(!st || !st->buf) |
| 591 | _ASN_ENCODE_FAILED; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 592 | |
| 593 | er.encoded = 0; |
| 594 | |
| 595 | /* |
| 596 | * Dump the contents of the buffer in hexadecimal. |
| 597 | */ |
| 598 | buf = st->buf; |
| 599 | end = buf + st->size; |
| 600 | if(flags & XER_F_CANONICAL) { |
| 601 | char *scend = scratch + (sizeof(scratch) - 2); |
| 602 | for(; buf < end; buf++) { |
| 603 | if(p >= scend) { |
| 604 | _ASN_CALLBACK(scratch, p - scratch); |
| 605 | er.encoded += p - scratch; |
| 606 | p = scratch; |
| 607 | } |
| 608 | *p++ = h2c[(*buf >> 4) & 0x0F]; |
| 609 | *p++ = h2c[*buf & 0x0F]; |
| 610 | } |
Lev Walkin | cc6a910 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 611 | |
| 612 | _ASN_CALLBACK(scratch, p-scratch); /* Dump the rest */ |
| 613 | er.encoded += p - scratch; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 614 | } else { |
| 615 | for(i = 0; buf < end; buf++, i++) { |
| 616 | if(!(i % 16) && (i || st->size > 16)) { |
| 617 | _ASN_CALLBACK(scratch, p-scratch); |
| 618 | er.encoded += (p-scratch); |
| 619 | p = scratch; |
| 620 | _i_ASN_TEXT_INDENT(1, ilevel); |
| 621 | } |
| 622 | *p++ = h2c[(*buf >> 4) & 0x0F]; |
| 623 | *p++ = h2c[*buf & 0x0F]; |
| 624 | *p++ = 0x20; |
| 625 | } |
Lev Walkin | cc6a910 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 626 | if(p - scratch) { |
| 627 | p--; /* Remove the tail space */ |
| 628 | _ASN_CALLBACK(scratch, p-scratch); /* Dump the rest */ |
| 629 | er.encoded += p - scratch; |
| 630 | if(st->size > 16) |
| 631 | _i_ASN_TEXT_INDENT(1, ilevel-1); |
| 632 | } |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 633 | } |
| 634 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 635 | return er; |
Lev Walkin | 942fd08 | 2004-10-03 09:13:02 +0000 | [diff] [blame] | 636 | cb_failed: |
| 637 | _ASN_ENCODE_FAILED; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 638 | } |
| 639 | |
Lev Walkin | 9ca8189 | 2004-10-03 10:54:25 +0000 | [diff] [blame] | 640 | static struct OCTET_STRING__xer_escape_table_s { |
| 641 | char *string; |
| 642 | int size; |
| 643 | } OCTET_STRING__xer_escape_table[] = { |
| 644 | #define OSXET(s) { s, sizeof(s) - 1 } |
| 645 | OSXET("\074\156\165\154\057\076"), /* <nul/> */ |
| 646 | OSXET("\074\163\157\150\057\076"), /* <soh/> */ |
| 647 | OSXET("\074\163\164\170\057\076"), /* <stx/> */ |
| 648 | OSXET("\074\145\164\170\057\076"), /* <etx/> */ |
| 649 | OSXET("\074\145\157\164\057\076"), /* <eot/> */ |
| 650 | OSXET("\074\145\156\161\057\076"), /* <enq/> */ |
| 651 | OSXET("\074\141\143\153\057\076"), /* <ack/> */ |
| 652 | OSXET("\074\142\145\154\057\076"), /* <bel/> */ |
| 653 | OSXET("\074\142\163\057\076"), /* <bs/> */ |
| 654 | OSXET("\011"), /* \t */ |
| 655 | OSXET("\012"), /* \n */ |
| 656 | OSXET("\074\166\164\057\076"), /* <vt/> */ |
| 657 | OSXET("\074\146\146\057\076"), /* <ff/> */ |
| 658 | OSXET("\015"), /* \r */ |
| 659 | OSXET("\074\163\157\057\076"), /* <so/> */ |
| 660 | OSXET("\074\163\151\057\076"), /* <si/> */ |
| 661 | OSXET("\074\144\154\145\057\076"), /* <dle/> */ |
| 662 | OSXET("\074\144\143\061\057\076"), /* <de1/> */ |
| 663 | OSXET("\074\144\143\062\057\076"), /* <de2/> */ |
| 664 | OSXET("\074\144\143\063\057\076"), /* <de3/> */ |
| 665 | OSXET("\074\144\143\064\057\076"), /* <de4/> */ |
| 666 | OSXET("\074\156\141\153\057\076"), /* <nak/> */ |
| 667 | OSXET("\074\163\171\156\057\076"), /* <syn/> */ |
| 668 | OSXET("\074\145\164\142\057\076"), /* <etb/> */ |
| 669 | OSXET("\074\143\141\156\057\076"), /* <can/> */ |
| 670 | OSXET("\074\145\155\057\076"), /* <em/> */ |
| 671 | OSXET("\074\163\165\142\057\076"), /* <sub/> */ |
| 672 | OSXET("\074\145\163\143\057\076"), /* <esc/> */ |
| 673 | OSXET("\074\151\163\064\057\076"), /* <is4/> */ |
| 674 | OSXET("\074\151\163\063\057\076"), /* <is3/> */ |
| 675 | OSXET("\074\151\163\062\057\076"), /* <is2/> */ |
| 676 | OSXET("\074\151\163\061\057\076"), /* <is1/> */ |
| 677 | { 0, 0 }, /* " " */ |
| 678 | { 0, 0 }, /* ! */ |
| 679 | { 0, 0 }, /* \" */ |
| 680 | { 0, 0 }, /* # */ |
| 681 | { 0, 0 }, /* $ */ |
| 682 | { 0, 0 }, /* % */ |
| 683 | OSXET("\046\141\155\160\073"), /* & */ |
| 684 | { 0, 0 }, /* ' */ |
| 685 | {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, /* ()*+,-./ */ |
| 686 | {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, /* 01234567 */ |
| 687 | {0,0},{0,0},{0,0},{0,0}, /* 89:; */ |
| 688 | OSXET("\046\154\164\073"), /* < */ |
| 689 | { 0, 0 }, /* = */ |
| 690 | OSXET("\046\147\164\073"), /* > */ |
| 691 | }; |
| 692 | |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 693 | static int |
| 694 | OS__check_escaped_control_char(void *buf, int size) { |
| 695 | size_t i; |
| 696 | /* |
| 697 | * Inefficient algorithm which translates the escape sequences |
| 698 | * defined above into characters. Returns -1 if not found. |
| 699 | * TODO: replace by a faster algorithm (bsearch(), hash or |
| 700 | * nested table lookups). |
| 701 | */ |
| 702 | for(i = 0; i < 32 /* Don't spend time on the bottom half */; i++) { |
| 703 | struct OCTET_STRING__xer_escape_table_s *el; |
| 704 | el = &OCTET_STRING__xer_escape_table[i]; |
| 705 | if(el->size == size && memcmp(buf, el->string, size) == 0) |
| 706 | return i; |
| 707 | } |
| 708 | return -1; |
| 709 | } |
| 710 | |
| 711 | static int |
| 712 | OCTET_STRING__handle_control_chars(void *struct_ptr, void *chunk_buf, size_t chunk_size) { |
| 713 | /* |
| 714 | * This might be one of the escape sequences |
| 715 | * for control characters. Check it out. |
| 716 | * #11.15.5 |
| 717 | */ |
| 718 | int control_char = OS__check_escaped_control_char(chunk_buf,chunk_size); |
| 719 | if(control_char >= 0) { |
| 720 | OCTET_STRING_t *st = (OCTET_STRING_t *)struct_ptr; |
| 721 | void *p = REALLOC(st->buf, st->size + 2); |
| 722 | if(p) { |
| 723 | st->buf = (uint8_t *)p; |
| 724 | st->buf[st->size++] = control_char; |
| 725 | st->buf[st->size] = '\0'; /* nul-termination */ |
| 726 | return 0; |
| 727 | } |
| 728 | } |
| 729 | |
| 730 | return -1; /* No, it's not */ |
| 731 | } |
| 732 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 733 | asn_enc_rval_t |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 734 | OCTET_STRING_encode_xer_utf8(asn_TYPE_descriptor_t *td, void *sptr, |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 735 | int ilevel, enum xer_encoder_flags_e flags, |
| 736 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 737 | const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr; |
| 738 | asn_enc_rval_t er; |
Lev Walkin | 9ca8189 | 2004-10-03 10:54:25 +0000 | [diff] [blame] | 739 | uint8_t *buf, *end; |
| 740 | uint8_t *ss; /* Sequence start */ |
| 741 | ssize_t encoded_len = 0; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 742 | |
| 743 | (void)ilevel; /* Unused argument */ |
| 744 | (void)flags; /* Unused argument */ |
| 745 | |
| 746 | if(!st || !st->buf) |
| 747 | _ASN_ENCODE_FAILED; |
| 748 | |
Lev Walkin | 9ca8189 | 2004-10-03 10:54:25 +0000 | [diff] [blame] | 749 | buf = st->buf; |
| 750 | end = buf + st->size; |
| 751 | for(ss = buf; buf < end; buf++) { |
Lev Walkin | 443d251 | 2004-10-05 06:35:31 +0000 | [diff] [blame] | 752 | unsigned int ch = *buf; |
Lev Walkin | 9ca8189 | 2004-10-03 10:54:25 +0000 | [diff] [blame] | 753 | int s_len; /* Special encoding sequence length */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 754 | |
Lev Walkin | 9ca8189 | 2004-10-03 10:54:25 +0000 | [diff] [blame] | 755 | /* |
| 756 | * Escape certain characters: X.680/11.15 |
| 757 | */ |
| 758 | if(ch < sizeof(OCTET_STRING__xer_escape_table) |
| 759 | /sizeof(OCTET_STRING__xer_escape_table[0]) |
| 760 | && (s_len = OCTET_STRING__xer_escape_table[ch].size)) { |
| 761 | if(((buf - ss) && cb(ss, buf - ss, app_key) < 0) |
| 762 | || cb(OCTET_STRING__xer_escape_table[ch].string, s_len, |
| 763 | app_key) < 0) |
| 764 | _ASN_ENCODE_FAILED; |
| 765 | encoded_len += (buf - ss) + s_len; |
| 766 | ss = buf + 1; |
| 767 | } |
| 768 | } |
| 769 | |
| 770 | encoded_len += (buf - ss); |
| 771 | if((buf - ss) && cb(ss, buf - ss, app_key) < 0) |
| 772 | _ASN_ENCODE_FAILED; |
| 773 | |
| 774 | er.encoded = encoded_len; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 775 | return er; |
| 776 | } |
| 777 | |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 778 | /* |
| 779 | * Convert from hexadecimal format (cstring): "AB CD EF" |
| 780 | */ |
| 781 | static ssize_t OCTET_STRING__convert_hexadecimal(void *sptr, void *chunk_buf, size_t chunk_size, int have_more) { |
| 782 | OCTET_STRING_t *st = (OCTET_STRING_t *)sptr; |
| 783 | char *chunk_stop = (char *)chunk_buf; |
| 784 | char *p = chunk_stop; |
| 785 | char *pend = p + chunk_size; |
| 786 | unsigned int clv = 0; |
| 787 | int half = 0; /* Half bit */ |
| 788 | uint8_t *buf; |
| 789 | |
| 790 | /* Reallocate buffer according to high cap estimation */ |
| 791 | ssize_t _ns = st->size + (chunk_size + 1) / 2; |
| 792 | void *nptr = REALLOC(st->buf, _ns + 1); |
| 793 | if(!nptr) return -1; |
| 794 | st->buf = (uint8_t *)nptr; |
| 795 | buf = st->buf + st->size; |
| 796 | |
| 797 | /* |
| 798 | * If something like " a b c " appears here, the " a b":3 will be |
| 799 | * converted, and the rest skipped. That is, unless buf_size is greater |
| 800 | * than chunk_size, then it'll be equivalent to "ABC0". |
| 801 | */ |
| 802 | for(; p < pend; p++) { |
| 803 | int ch = *(unsigned char *)p; |
| 804 | switch(ch) { |
| 805 | case 0x09: case 0x0a: case 0x0c: case 0x0d: |
| 806 | case 0x20: |
| 807 | /* Ignore whitespace */ |
| 808 | continue; |
| 809 | case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: /*01234*/ |
| 810 | case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: /*56789*/ |
| 811 | clv = (clv << 4) + (ch - 0x30); |
| 812 | break; |
| 813 | case 0x41: case 0x42: case 0x43: /* ABC */ |
| 814 | case 0x44: case 0x45: case 0x46: /* DEF */ |
Lev Walkin | 3ae21bd | 2005-02-21 14:43:48 +0000 | [diff] [blame] | 815 | clv = (clv << 4) + (ch - 0x41 + 10); |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 816 | break; |
| 817 | case 0x61: case 0x62: case 0x63: /* abc */ |
| 818 | case 0x64: case 0x65: case 0x66: /* def */ |
Lev Walkin | 3ae21bd | 2005-02-21 14:43:48 +0000 | [diff] [blame] | 819 | clv = (clv << 4) + (ch - 0x61 + 10); |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 820 | break; |
| 821 | default: |
| 822 | *buf = 0; /* JIC */ |
| 823 | return -1; |
| 824 | } |
| 825 | if(half++) { |
| 826 | half = 0; |
| 827 | *buf++ = clv; |
| 828 | chunk_stop = p + 1; |
| 829 | } |
| 830 | } |
| 831 | |
| 832 | /* |
| 833 | * Check partial decoding. |
| 834 | */ |
| 835 | if(half) { |
| 836 | if(have_more) { |
| 837 | /* |
| 838 | * Partial specification is fine, |
| 839 | * because no more more PXER_TEXT data is available. |
| 840 | */ |
| 841 | *buf++ = clv << 4; |
| 842 | chunk_stop = p; |
| 843 | } |
| 844 | } else { |
| 845 | chunk_stop = p; |
| 846 | } |
| 847 | |
| 848 | st->size = buf - st->buf; /* Adjust the buffer size */ |
| 849 | assert(st->size <= _ns); |
| 850 | st->buf[st->size] = 0; /* Courtesy termination */ |
| 851 | |
| 852 | return (chunk_stop - (char *)chunk_buf); /* Converted size */ |
| 853 | } |
| 854 | |
| 855 | /* |
| 856 | * Convert from binary format: "00101011101" |
| 857 | */ |
| 858 | static ssize_t OCTET_STRING__convert_binary(void *sptr, void *chunk_buf, size_t chunk_size, int have_more) { |
| 859 | BIT_STRING_t *st = (BIT_STRING_t *)sptr; |
| 860 | char *p = (char *)chunk_buf; |
| 861 | char *pend = p + chunk_size; |
| 862 | int bits_unused = st->bits_unused & 0x7; |
| 863 | uint8_t *buf; |
| 864 | |
| 865 | /* Reallocate buffer according to high cap estimation */ |
| 866 | ssize_t _ns = st->size + (chunk_size + 7) / 8; |
| 867 | void *nptr = REALLOC(st->buf, _ns + 1); |
| 868 | if(!nptr) return -1; |
| 869 | st->buf = (uint8_t *)nptr; |
| 870 | buf = st->buf + st->size; |
| 871 | |
| 872 | (void)have_more; |
| 873 | |
| 874 | if(bits_unused == 0) |
| 875 | bits_unused = 8; |
| 876 | else if(st->size) |
| 877 | buf--; |
| 878 | |
| 879 | /* |
| 880 | * Convert series of 0 and 1 into the octet string. |
| 881 | */ |
| 882 | for(; p < pend; p++) { |
| 883 | int ch = *(unsigned char *)p; |
| 884 | switch(ch) { |
| 885 | case 0x09: case 0x0a: case 0x0c: case 0x0d: |
| 886 | case 0x20: |
| 887 | /* Ignore whitespace */ |
| 888 | break; |
| 889 | case 0x30: |
| 890 | case 0x31: |
| 891 | if(bits_unused-- <= 0) { |
| 892 | *++buf = 0; /* Clean the cell */ |
| 893 | bits_unused = 7; |
| 894 | } |
| 895 | *buf |= (ch&1) << bits_unused; |
| 896 | break; |
| 897 | default: |
| 898 | st->bits_unused = bits_unused; |
| 899 | return -1; |
| 900 | } |
| 901 | } |
| 902 | |
| 903 | if(bits_unused == 8) { |
| 904 | st->size = buf - st->buf; |
| 905 | st->bits_unused = 0; |
| 906 | } else { |
| 907 | st->size = buf - st->buf + 1; |
| 908 | st->bits_unused = bits_unused; |
| 909 | } |
| 910 | |
| 911 | assert(st->size <= _ns); |
| 912 | st->buf[st->size] = 0; /* Courtesy termination */ |
| 913 | |
| 914 | return chunk_size; /* Converted in full */ |
| 915 | } |
| 916 | |
| 917 | /* |
| 918 | * Something like strtod(), but with stricter rules. |
| 919 | */ |
| 920 | static int |
Lev Walkin | 3370016 | 2004-10-26 09:03:31 +0000 | [diff] [blame] | 921 | OS__strtoent(int base, char *buf, char *end, int32_t *return_value) { |
| 922 | int32_t val = 0; |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 923 | char *p; |
| 924 | |
| 925 | for(p = buf; p < end; p++) { |
| 926 | int ch = *p; |
| 927 | if((val * base + base) < 0) return -1; /* Strange huge value */ |
| 928 | switch(ch) { |
| 929 | case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: /*01234*/ |
| 930 | case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: /*56789*/ |
| 931 | val = val * base + (ch - 0x30); |
| 932 | break; |
| 933 | case 0x41: case 0x42: case 0x43: /* ABC */ |
| 934 | case 0x44: case 0x45: case 0x46: /* DEF */ |
Lev Walkin | 3ae21bd | 2005-02-21 14:43:48 +0000 | [diff] [blame] | 935 | val = val * base + (ch - 0x41 + 10); |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 936 | break; |
| 937 | case 0x61: case 0x62: case 0x63: /* abc */ |
| 938 | case 0x64: case 0x65: case 0x66: /* def */ |
Lev Walkin | 3ae21bd | 2005-02-21 14:43:48 +0000 | [diff] [blame] | 939 | val = val * base + (ch - 0x61 + 10); |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 940 | break; |
| 941 | case 0x3b: /* ';' */ |
| 942 | *return_value = val; |
| 943 | return (p - buf) + 1; |
| 944 | default: |
| 945 | return -1; /* Character set error */ |
| 946 | } |
| 947 | } |
| 948 | |
| 949 | /* Do not return value. It's an error we're talking about here. */ |
| 950 | return (p - buf); |
| 951 | } |
| 952 | |
| 953 | /* |
| 954 | * Convert from the plain UTF-8 format, expanding entity references: "2 < 3" |
| 955 | */ |
| 956 | static ssize_t OCTET_STRING__convert_entrefs(void *sptr, void *chunk_buf, size_t chunk_size, int have_more) { |
| 957 | OCTET_STRING_t *st = (OCTET_STRING_t *)sptr; |
| 958 | char *p = (char *)chunk_buf; |
| 959 | char *pend = p + chunk_size; |
| 960 | uint8_t *buf; |
| 961 | |
| 962 | /* Reallocate buffer */ |
| 963 | ssize_t _ns = st->size + chunk_size; |
| 964 | void *nptr = REALLOC(st->buf, _ns + 1); |
| 965 | if(!nptr) return -1; |
| 966 | st->buf = (uint8_t *)nptr; |
| 967 | buf = st->buf + st->size; |
| 968 | |
| 969 | /* |
| 970 | * Convert series of 0 and 1 into the octet string. |
| 971 | */ |
| 972 | for(; p < pend; p++) { |
| 973 | int ch = *(unsigned char *)p; |
| 974 | int len; /* Length of the rest of the chunk */ |
| 975 | |
| 976 | if(ch != 0x26 /* '&' */) { |
| 977 | *buf++ = ch; |
| 978 | continue; /* That was easy... */ |
| 979 | } |
| 980 | |
| 981 | /* |
| 982 | * Process entity reference. |
| 983 | */ |
| 984 | len = chunk_size - (p - (char *)chunk_buf); |
| 985 | if(len == 1 /* "&" */) goto want_more; |
| 986 | if(p[1] == 0x23 /* '#' */) { |
| 987 | char *pval; /* Pointer to start of digits */ |
Lev Walkin | 3370016 | 2004-10-26 09:03:31 +0000 | [diff] [blame] | 988 | int32_t val; /* Entity reference value */ |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 989 | int base; |
| 990 | |
| 991 | if(len == 2 /* "&#" */) goto want_more; |
| 992 | if(p[2] == 0x78 /* 'x' */) |
| 993 | pval = p + 3, base = 16; |
| 994 | else |
| 995 | pval = p + 2, base = 10; |
| 996 | len = OS__strtoent(base, pval, p + len, &val); |
| 997 | if(len == -1) { |
| 998 | /* Invalid charset. Just copy verbatim. */ |
| 999 | *buf++ = ch; |
| 1000 | continue; |
| 1001 | } |
| 1002 | if(!len || pval[len-1] != 0x3b) goto want_more; |
| 1003 | assert(val > 0); |
| 1004 | p += (pval - p) + len - 1; /* Advance past entref */ |
| 1005 | |
| 1006 | if(val < 0x80) { |
| 1007 | *buf++ = (char)val; |
| 1008 | } else if(val < 0x800) { |
| 1009 | *buf++ = 0xc0 | ((val >> 6)); |
| 1010 | *buf++ = 0x80 | ((val & 0x3f)); |
| 1011 | } else if(val < 0x10000) { |
| 1012 | *buf++ = 0xe0 | ((val >> 12)); |
| 1013 | *buf++ = 0x80 | ((val >> 6) & 0x3f); |
| 1014 | *buf++ = 0x80 | ((val & 0x3f)); |
| 1015 | } else if(val < 0x200000) { |
| 1016 | *buf++ = 0xf0 | ((val >> 18)); |
| 1017 | *buf++ = 0x80 | ((val >> 12) & 0x3f); |
| 1018 | *buf++ = 0x80 | ((val >> 6) & 0x3f); |
| 1019 | *buf++ = 0x80 | ((val & 0x3f)); |
| 1020 | } else if(val < 0x4000000) { |
| 1021 | *buf++ = 0xf8 | ((val >> 24)); |
| 1022 | *buf++ = 0x80 | ((val >> 18) & 0x3f); |
| 1023 | *buf++ = 0x80 | ((val >> 12) & 0x3f); |
| 1024 | *buf++ = 0x80 | ((val >> 6) & 0x3f); |
| 1025 | *buf++ = 0x80 | ((val & 0x3f)); |
| 1026 | } else { |
| 1027 | *buf++ = 0xfc | ((val >> 30) & 0x1); |
| 1028 | *buf++ = 0x80 | ((val >> 24) & 0x3f); |
| 1029 | *buf++ = 0x80 | ((val >> 18) & 0x3f); |
| 1030 | *buf++ = 0x80 | ((val >> 12) & 0x3f); |
| 1031 | *buf++ = 0x80 | ((val >> 6) & 0x3f); |
| 1032 | *buf++ = 0x80 | ((val & 0x3f)); |
| 1033 | } |
| 1034 | } else { |
| 1035 | /* |
| 1036 | * Ugly, limited parsing of & > < |
| 1037 | */ |
| 1038 | char *sc = (char *)memchr(p, 0x3b, len > 5 ? 5 : len); |
| 1039 | if(!sc) goto want_more; |
| 1040 | if((sc - p) == 4 |
| 1041 | && p[1] == 0x61 /* 'a' */ |
| 1042 | && p[2] == 0x6d /* 'm' */ |
| 1043 | && p[3] == 0x70 /* 'p' */) { |
| 1044 | *buf++ = 0x26; |
| 1045 | p = sc; |
| 1046 | continue; |
| 1047 | } |
| 1048 | if((sc - p) == 3) { |
| 1049 | if(p[1] == 0x6c) { |
| 1050 | *buf = 0x3c; /* '<' */ |
| 1051 | } else if(p[1] == 0x67) { |
| 1052 | *buf = 0x3e; /* '>' */ |
| 1053 | } else { |
| 1054 | /* Unsupported entity reference */ |
| 1055 | *buf++ = ch; |
| 1056 | continue; |
| 1057 | } |
| 1058 | if(p[2] != 0x74) { |
| 1059 | /* Unsupported entity reference */ |
| 1060 | *buf++ = ch; |
| 1061 | continue; |
| 1062 | } |
| 1063 | buf++; |
| 1064 | p = sc; |
| 1065 | continue; |
| 1066 | } |
| 1067 | /* Unsupported entity reference */ |
| 1068 | *buf++ = ch; |
| 1069 | } |
| 1070 | |
| 1071 | continue; |
| 1072 | want_more: |
| 1073 | if(have_more) { |
| 1074 | /* |
| 1075 | * We know that no more data (of the same type) |
| 1076 | * is coming. Copy the rest verbatim. |
| 1077 | */ |
| 1078 | *buf++ = ch; |
| 1079 | continue; |
| 1080 | } |
Lev Walkin | bdaae77 | 2005-02-18 12:25:47 +0000 | [diff] [blame] | 1081 | chunk_size = (p - (char *)chunk_buf); |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 1082 | /* Processing stalled: need more data */ |
Lev Walkin | bdaae77 | 2005-02-18 12:25:47 +0000 | [diff] [blame] | 1083 | break; |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 1084 | } |
| 1085 | |
| 1086 | st->size = buf - st->buf; |
| 1087 | assert(st->size <= _ns); |
| 1088 | st->buf[st->size] = 0; /* Courtesy termination */ |
| 1089 | |
| 1090 | return chunk_size; /* Converted in full */ |
| 1091 | } |
| 1092 | |
| 1093 | /* |
| 1094 | * Decode OCTET STRING from the XML element's body. |
| 1095 | */ |
| 1096 | static asn_dec_rval_t |
| 1097 | OCTET_STRING__decode_xer(asn_codec_ctx_t *opt_codec_ctx, |
| 1098 | asn_TYPE_descriptor_t *td, void **sptr, |
| 1099 | const char *opt_mname, void *buf_ptr, size_t size, |
| 1100 | int (*opt_unexpected_tag_decoder) |
| 1101 | (void *struct_ptr, void *chunk_buf, size_t chunk_size), |
| 1102 | ssize_t (*body_receiver) |
| 1103 | (void *struct_ptr, void *chunk_buf, size_t chunk_size, |
| 1104 | int have_more) |
| 1105 | ) { |
Lev Walkin | d512564 | 2005-02-14 20:08:00 +0000 | [diff] [blame] | 1106 | OCTET_STRING_t *st = (OCTET_STRING_t *)*sptr; |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 1107 | asn_OCTET_STRING_specifics_t *specs = td->specifics |
| 1108 | ? (asn_OCTET_STRING_specifics_t *)td->specifics |
| 1109 | : &asn_DEF_OCTET_STRING_specs; |
| 1110 | const char *xml_tag = opt_mname ? opt_mname : td->xml_tag; |
| 1111 | asn_struct_ctx_t *ctx; /* Per-structure parser context */ |
Lev Walkin | c61f386 | 2005-02-14 17:21:22 +0000 | [diff] [blame] | 1112 | asn_dec_rval_t rval; /* Return value from the decoder */ |
| 1113 | int st_allocated; |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 1114 | |
| 1115 | /* |
| 1116 | * Create the string if does not exist. |
| 1117 | */ |
Lev Walkin | c61f386 | 2005-02-14 17:21:22 +0000 | [diff] [blame] | 1118 | if(!st) { |
Lev Walkin | 8484ed8 | 2004-12-14 13:31:01 +0000 | [diff] [blame] | 1119 | st = (OCTET_STRING_t *)CALLOC(1, specs->struct_size); |
Lev Walkin | 8484ed8 | 2004-12-14 13:31:01 +0000 | [diff] [blame] | 1120 | *sptr = (void *)st; |
Lev Walkin | c61f386 | 2005-02-14 17:21:22 +0000 | [diff] [blame] | 1121 | if(!st) goto sta_failed; |
| 1122 | st_allocated = 1; |
Lev Walkin | bdaae77 | 2005-02-18 12:25:47 +0000 | [diff] [blame] | 1123 | } else { |
| 1124 | st_allocated = 0; |
| 1125 | } |
Lev Walkin | c61f386 | 2005-02-14 17:21:22 +0000 | [diff] [blame] | 1126 | if(!st->buf) { |
| 1127 | /* This is separate from above section */ |
| 1128 | st->buf = (uint8_t *)CALLOC(1, 1); |
| 1129 | if(!st->buf) { |
| 1130 | if(st_allocated) { |
| 1131 | *sptr = 0; |
| 1132 | goto stb_failed; |
| 1133 | } else { |
| 1134 | goto sta_failed; |
| 1135 | } |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 1136 | } |
| 1137 | } |
| 1138 | |
| 1139 | /* Restore parsing context */ |
| 1140 | ctx = (asn_struct_ctx_t *)(((char *)*sptr) + specs->ctx_offset); |
| 1141 | |
| 1142 | return xer_decode_general(opt_codec_ctx, ctx, *sptr, xml_tag, |
| 1143 | buf_ptr, size, opt_unexpected_tag_decoder, body_receiver); |
Lev Walkin | c61f386 | 2005-02-14 17:21:22 +0000 | [diff] [blame] | 1144 | |
| 1145 | stb_failed: |
| 1146 | FREEMEM(st); |
| 1147 | sta_failed: |
| 1148 | rval.code = RC_FAIL; |
| 1149 | rval.consumed = 0; |
| 1150 | return rval; |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 1151 | } |
| 1152 | |
| 1153 | /* |
| 1154 | * Decode OCTET STRING from the hexadecimal data. |
| 1155 | */ |
| 1156 | asn_dec_rval_t |
| 1157 | OCTET_STRING_decode_xer_hex(asn_codec_ctx_t *opt_codec_ctx, |
| 1158 | asn_TYPE_descriptor_t *td, void **sptr, |
| 1159 | const char *opt_mname, void *buf_ptr, size_t size) { |
| 1160 | return OCTET_STRING__decode_xer(opt_codec_ctx, td, sptr, opt_mname, |
| 1161 | buf_ptr, size, 0, OCTET_STRING__convert_hexadecimal); |
| 1162 | } |
| 1163 | |
| 1164 | /* |
| 1165 | * Decode OCTET STRING from the binary (0/1) data. |
| 1166 | */ |
| 1167 | asn_dec_rval_t |
| 1168 | OCTET_STRING_decode_xer_binary(asn_codec_ctx_t *opt_codec_ctx, |
| 1169 | asn_TYPE_descriptor_t *td, void **sptr, |
| 1170 | const char *opt_mname, void *buf_ptr, size_t size) { |
| 1171 | return OCTET_STRING__decode_xer(opt_codec_ctx, td, sptr, opt_mname, |
| 1172 | buf_ptr, size, 0, OCTET_STRING__convert_binary); |
| 1173 | } |
| 1174 | |
| 1175 | /* |
| 1176 | * Decode OCTET STRING from the string (ASCII/UTF-8) data. |
| 1177 | */ |
| 1178 | asn_dec_rval_t |
| 1179 | OCTET_STRING_decode_xer_utf8(asn_codec_ctx_t *opt_codec_ctx, |
| 1180 | asn_TYPE_descriptor_t *td, void **sptr, |
| 1181 | const char *opt_mname, void *buf_ptr, size_t size) { |
| 1182 | return OCTET_STRING__decode_xer(opt_codec_ctx, td, sptr, opt_mname, |
| 1183 | buf_ptr, size, |
| 1184 | OCTET_STRING__handle_control_chars, |
| 1185 | OCTET_STRING__convert_entrefs); |
| 1186 | } |
| 1187 | |
| 1188 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1189 | int |
Lev Walkin | de4825d | 2004-09-29 13:20:14 +0000 | [diff] [blame] | 1190 | 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] | 1191 | asn_app_consume_bytes_f *cb, void *app_key) { |
Lev Walkin | 4d9528c | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 1192 | static const char *h2c = "0123456789ABCDEF"; |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 1193 | const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1194 | char scratch[16 * 3 + 4]; |
| 1195 | char *p = scratch; |
| 1196 | uint8_t *buf; |
| 1197 | uint8_t *end; |
| 1198 | size_t i; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1199 | |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 1200 | (void)td; /* Unused argument */ |
| 1201 | |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 1202 | if(!st || !st->buf) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1203 | |
| 1204 | /* |
| 1205 | * Dump the contents of the buffer in hexadecimal. |
| 1206 | */ |
| 1207 | buf = st->buf; |
| 1208 | end = buf + st->size; |
| 1209 | for(i = 0; buf < end; buf++, i++) { |
| 1210 | if(!(i % 16) && (i || st->size > 16)) { |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 1211 | if(cb(scratch, p - scratch, app_key) < 0) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1212 | return -1; |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 1213 | _i_INDENT(1); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1214 | p = scratch; |
| 1215 | } |
| 1216 | *p++ = h2c[(*buf >> 4) & 0x0F]; |
| 1217 | *p++ = h2c[*buf & 0x0F]; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 1218 | *p++ = 0x20; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1219 | } |
| 1220 | |
Lev Walkin | cc6a910 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 1221 | if(p > scratch) { |
| 1222 | p--; /* Remove the tail space */ |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 1223 | if(cb(scratch, p - scratch, app_key) < 0) |
Lev Walkin | cc6a910 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 1224 | return -1; |
| 1225 | } |
| 1226 | |
| 1227 | return 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1228 | } |
| 1229 | |
| 1230 | int |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 1231 | OCTET_STRING_print_utf8(asn_TYPE_descriptor_t *td, const void *sptr, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1232 | int ilevel, asn_app_consume_bytes_f *cb, void *app_key) { |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 1233 | const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1234 | |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 1235 | (void)td; /* Unused argument */ |
| 1236 | (void)ilevel; /* Unused argument */ |
| 1237 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1238 | if(st && st->buf) { |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 1239 | return (cb(st->buf, st->size, app_key) < 0) ? -1 : 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1240 | } else { |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 1241 | return (cb("<absent>", 8, app_key) < 0) ? -1 : 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1242 | } |
| 1243 | } |
| 1244 | |
| 1245 | void |
Lev Walkin | de4825d | 2004-09-29 13:20:14 +0000 | [diff] [blame] | 1246 | 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] | 1247 | OCTET_STRING_t *st = (OCTET_STRING_t *)sptr; |
Lev Walkin | bbd9325 | 2004-10-12 05:57:23 +0000 | [diff] [blame] | 1248 | asn_OCTET_STRING_specifics_t *specs = td->specifics |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 1249 | ? (asn_OCTET_STRING_specifics_t *)td->specifics |
| 1250 | : &asn_DEF_OCTET_STRING_specs; |
Lev Walkin | bbd9325 | 2004-10-12 05:57:23 +0000 | [diff] [blame] | 1251 | asn_struct_ctx_t *ctx = (asn_struct_ctx_t *) |
| 1252 | ((char *)st + specs->ctx_offset); |
Lev Walkin | b54577a | 2004-11-08 10:47:12 +0000 | [diff] [blame] | 1253 | struct _stack *stck; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1254 | |
| 1255 | if(!td || !st) |
| 1256 | return; |
| 1257 | |
| 1258 | ASN_DEBUG("Freeing %s as OCTET STRING", td->name); |
| 1259 | |
| 1260 | if(st->buf) { |
| 1261 | FREEMEM(st->buf); |
| 1262 | } |
| 1263 | |
| 1264 | /* |
| 1265 | * Remove decode-time stack. |
| 1266 | */ |
Lev Walkin | b54577a | 2004-11-08 10:47:12 +0000 | [diff] [blame] | 1267 | stck = (struct _stack *)ctx->ptr; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1268 | if(stck) { |
| 1269 | while(stck->tail) { |
| 1270 | struct _stack_el *sel = stck->tail; |
| 1271 | stck->tail = sel->prev; |
| 1272 | FREEMEM(sel); |
| 1273 | } |
| 1274 | FREEMEM(stck); |
| 1275 | } |
| 1276 | |
| 1277 | if(!contents_only) { |
| 1278 | FREEMEM(st); |
| 1279 | } |
| 1280 | } |
| 1281 | |
| 1282 | /* |
| 1283 | * Conversion routines. |
| 1284 | */ |
| 1285 | int |
| 1286 | OCTET_STRING_fromBuf(OCTET_STRING_t *st, const char *str, int len) { |
| 1287 | void *buf; |
| 1288 | |
| 1289 | if(st == 0 || (str == 0 && len)) { |
| 1290 | errno = EINVAL; |
| 1291 | return -1; |
| 1292 | } |
| 1293 | |
| 1294 | /* |
| 1295 | * Clear the OCTET STRING. |
| 1296 | */ |
| 1297 | if(str == NULL) { |
| 1298 | if(st->buf) |
| 1299 | FREEMEM(st->buf); |
| 1300 | st->size = 0; |
| 1301 | return 0; |
| 1302 | } |
| 1303 | |
| 1304 | /* Determine the original string size, if not explicitly given */ |
| 1305 | if(len < 0) |
| 1306 | len = strlen(str); |
| 1307 | |
| 1308 | /* Allocate and fill the memory */ |
| 1309 | buf = MALLOC(len + 1); |
| 1310 | if(buf == NULL) { |
| 1311 | return -1; |
| 1312 | } else { |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 1313 | st->buf = (uint8_t *)buf; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1314 | st->size = len; |
| 1315 | } |
| 1316 | |
| 1317 | memcpy(buf, str, len); |
| 1318 | st->buf[st->size] = '\0'; /* Couldn't use memcpy(len+1)! */ |
| 1319 | |
| 1320 | return 0; |
| 1321 | } |
| 1322 | |
| 1323 | OCTET_STRING_t * |
Lev Walkin | bbd9325 | 2004-10-12 05:57:23 +0000 | [diff] [blame] | 1324 | OCTET_STRING_new_fromBuf(asn_TYPE_descriptor_t *td, const char *str, int len) { |
| 1325 | asn_OCTET_STRING_specifics_t *specs = td->specifics |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 1326 | ? (asn_OCTET_STRING_specifics_t *)td->specifics |
| 1327 | : &asn_DEF_OCTET_STRING_specs; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1328 | OCTET_STRING_t *st; |
| 1329 | |
Lev Walkin | bbd9325 | 2004-10-12 05:57:23 +0000 | [diff] [blame] | 1330 | st = (OCTET_STRING_t *)CALLOC(1, specs->struct_size); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1331 | if(st && str && OCTET_STRING_fromBuf(st, str, len)) { |
| 1332 | free(st); |
| 1333 | st = NULL; |
| 1334 | } |
| 1335 | |
| 1336 | return st; |
| 1337 | } |
| 1338 | |