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