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