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