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> |
| 7 | #include <assert.h> |
| 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 | de4825d | 2004-09-29 13:20:14 +0000 | [diff] [blame] | 16 | asn_TYPE_descriptor_t asn_DEF_OCTET_STRING = { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 17 | "OCTET STRING", |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 18 | OCTET_STRING_free, |
| 19 | OCTET_STRING_print, /* non-ascii stuff, generally */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 20 | asn_generic_no_constraint, |
| 21 | OCTET_STRING_decode_ber, |
| 22 | OCTET_STRING_encode_der, |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 23 | 0, /* Not implemented yet */ |
| 24 | OCTET_STRING_encode_xer, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 25 | 0, /* Use generic outmost tag fetcher */ |
Lev Walkin | de4825d | 2004-09-29 13:20:14 +0000 | [diff] [blame] | 26 | asn_DEF_OCTET_STRING_tags, |
| 27 | sizeof(asn_DEF_OCTET_STRING_tags) |
| 28 | / sizeof(asn_DEF_OCTET_STRING_tags[0]), |
| 29 | asn_DEF_OCTET_STRING_tags, /* Same as above */ |
| 30 | sizeof(asn_DEF_OCTET_STRING_tags) |
| 31 | / sizeof(asn_DEF_OCTET_STRING_tags[0]), |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 32 | 0, 0, /* No members */ |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 33 | 0 /* No specifics */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 34 | }; |
| 35 | |
Lev Walkin | cc6a910 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 36 | #undef _CH_PHASE |
| 37 | #undef NEXT_PHASE |
| 38 | #undef PREV_PHASE |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 39 | #define _CH_PHASE(ctx, inc) do { \ |
| 40 | if(ctx->phase == 0) \ |
| 41 | ctx->step = 0; \ |
| 42 | ctx->phase += inc; \ |
| 43 | } while(0) |
| 44 | #define NEXT_PHASE(ctx) _CH_PHASE(ctx, +1) |
| 45 | #define PREV_PHASE(ctx) _CH_PHASE(ctx, -1) |
| 46 | |
Lev Walkin | cc6a910 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 47 | #undef ADVANCE |
Lev Walkin | 4ce78ca | 2004-08-25 01:34:11 +0000 | [diff] [blame] | 48 | #define ADVANCE(num_bytes) do { \ |
Lev Walkin | 3990ba6 | 2004-09-24 20:57:41 +0000 | [diff] [blame] | 49 | size_t num = (num_bytes); \ |
Lev Walkin | 4ce78ca | 2004-08-25 01:34:11 +0000 | [diff] [blame] | 50 | buf_ptr = ((char *)buf_ptr) + num; \ |
| 51 | size -= num; \ |
| 52 | consumed_myself += num; \ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 53 | } while(0) |
| 54 | |
Lev Walkin | cc6a910 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 55 | #undef RETURN |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 56 | #define RETURN(_code) do { \ |
| 57 | rval.code = _code; \ |
| 58 | rval.consumed = consumed_myself;\ |
| 59 | return rval; \ |
| 60 | } while(0) |
| 61 | |
Lev Walkin | cc6a910 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 62 | #undef APPEND |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 63 | #define APPEND(bufptr, bufsize) do { \ |
Lev Walkin | 8d12787 | 2004-09-04 04:44:50 +0000 | [diff] [blame] | 64 | size_t _bs = (bufsize); \ |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 65 | size_t _ns = ctx->step; /* Allocated */ \ |
Lev Walkin | 8d12787 | 2004-09-04 04:44:50 +0000 | [diff] [blame] | 66 | if(_ns <= (size_t)(st->size + _bs)) { \ |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 67 | void *ptr; \ |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 68 | /* Be nice and round to the memory allocator */ \ |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 69 | do { _ns = _ns ? _ns<<2 : 16; } \ |
Lev Walkin | 8d12787 | 2004-09-04 04:44:50 +0000 | [diff] [blame] | 70 | while(_ns <= (size_t)(st->size + _bs)); \ |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 71 | ptr = REALLOC(st->buf, _ns); \ |
| 72 | if(ptr) { \ |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 73 | st->buf = (uint8_t *)ptr; \ |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 74 | ctx->step = _ns; \ |
| 75 | } else { \ |
| 76 | RETURN(RC_FAIL); \ |
| 77 | } \ |
| 78 | } \ |
Lev Walkin | 8d12787 | 2004-09-04 04:44:50 +0000 | [diff] [blame] | 79 | memcpy(st->buf + st->size, bufptr, _bs); \ |
| 80 | st->size += _bs; \ |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 81 | if(st->size < 0) \ |
| 82 | /* Why even care?.. JIC */ \ |
| 83 | RETURN(RC_FAIL); \ |
| 84 | /* Convenient nul-termination */ \ |
| 85 | st->buf[st->size] = '\0'; \ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 86 | } while(0) |
| 87 | |
| 88 | /* |
| 89 | * The main reason why ASN.1 is still alive is that too much time and effort |
| 90 | * is necessary for learning it more or less adequately, thus creating a gut |
| 91 | * necessity to demonstrate that aquired skill everywhere afterwards. |
| 92 | * No, I am not going to explain what the following stuff is. |
| 93 | */ |
| 94 | struct _stack_el { |
Lev Walkin | 3990ba6 | 2004-09-24 20:57:41 +0000 | [diff] [blame] | 95 | ber_tlv_len_t left; /* What's left to read (or -1) */ |
Lev Walkin | 5c91599 | 2004-09-27 20:54:06 +0000 | [diff] [blame] | 96 | ber_tlv_len_t got; /* What was actually processed */ |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 97 | int cont_level; /* Depth of subcontainment */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 98 | int want_nulls; /* Want null "end of content" octets? */ |
| 99 | int bits_chopped; /* Flag in BIT STRING mode */ |
Lev Walkin | 5c91599 | 2004-09-27 20:54:06 +0000 | [diff] [blame] | 100 | ber_tlv_tag_t tag; /* For debugging purposes */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 101 | struct _stack_el *prev; |
| 102 | struct _stack_el *next; |
| 103 | }; |
| 104 | struct _stack { |
| 105 | struct _stack_el *tail; |
| 106 | struct _stack_el *cur_ptr; |
| 107 | }; |
| 108 | |
| 109 | static struct _stack_el * |
Lev Walkin | 3990ba6 | 2004-09-24 20:57:41 +0000 | [diff] [blame] | 110 | OS__add_stack_el(struct _stack *st) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 111 | struct _stack_el *nel; |
| 112 | |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 113 | /* |
| 114 | * Reuse the old stack frame or allocate a new one. |
| 115 | */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 116 | if(st->cur_ptr && st->cur_ptr->next) { |
| 117 | nel = st->cur_ptr->next; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 118 | nel->bits_chopped = 0; |
Lev Walkin | 5c91599 | 2004-09-27 20:54:06 +0000 | [diff] [blame] | 119 | nel->got = 0; |
| 120 | /* Retain the nel->cont_level, it's correct. */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 121 | } else { |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 122 | (void *)nel = CALLOC(1, sizeof(struct _stack_el)); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 123 | if(nel == NULL) |
| 124 | return NULL; |
| 125 | |
| 126 | if(st->tail) { |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 127 | /* Increase a subcontainment depth */ |
| 128 | nel->cont_level = st->tail->cont_level + 1; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 129 | st->tail->next = nel; |
| 130 | } |
| 131 | nel->prev = st->tail; |
| 132 | st->tail = nel; |
| 133 | } |
| 134 | |
| 135 | st->cur_ptr = nel; |
| 136 | |
| 137 | return nel; |
| 138 | } |
| 139 | |
| 140 | static struct _stack * |
| 141 | _new_stack() { |
| 142 | struct _stack *st; |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 143 | (void *)st = CALLOC(1, sizeof(struct _stack)); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 144 | if(st == NULL) |
| 145 | return NULL; |
| 146 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 147 | return st; |
| 148 | } |
| 149 | |
| 150 | /* |
| 151 | * Decode OCTET STRING type. |
| 152 | */ |
| 153 | ber_dec_rval_t |
Lev Walkin | de4825d | 2004-09-29 13:20:14 +0000 | [diff] [blame] | 154 | OCTET_STRING_decode_ber(asn_codec_ctx_t *opt_codec_ctx, |
| 155 | asn_TYPE_descriptor_t *td, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 156 | void **os_structure, void *buf_ptr, size_t size, int tag_mode) { |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 157 | OCTET_STRING_t *st = (OCTET_STRING_t *)*os_structure; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 158 | ber_dec_rval_t rval; |
Lev Walkin | de4825d | 2004-09-29 13:20:14 +0000 | [diff] [blame] | 159 | asn_struct_ctx_t *ctx; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 160 | ssize_t consumed_myself = 0; |
| 161 | struct _stack *stck; /* A stack structure */ |
Lev Walkin | 5c91599 | 2004-09-27 20:54:06 +0000 | [diff] [blame] | 162 | struct _stack_el *sel = 0; /* Stack element */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 163 | int tlv_constr; |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 164 | enum type_type_e { |
| 165 | _TT_GENERIC = 0, /* Just a random OCTET STRING */ |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 166 | _TT_BIT_STRING = 1, /* BIT STRING type, a special case */ |
| 167 | _TT_ANY = 2, /* ANY type, a special case too */ |
| 168 | } type_type = (enum type_type_e)(int)td->specifics; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 169 | |
Lev Walkin | cc6a910 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 170 | ASN_DEBUG("Decoding %s as %s (frame %ld)", |
| 171 | td->name, |
| 172 | (type_type == _TT_GENERIC) ? "OCTET STRING" : "OS-SpecialCase", |
| 173 | (long)size); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 174 | |
| 175 | /* |
| 176 | * Create the string if does not exist. |
| 177 | */ |
| 178 | if(st == NULL) { |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 179 | (void *)st = *os_structure = CALLOC(1, sizeof(*st)); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 180 | if(st == NULL) |
| 181 | RETURN(RC_FAIL); |
| 182 | } |
| 183 | |
| 184 | /* Restore parsing context */ |
Lev Walkin | de4825d | 2004-09-29 13:20:14 +0000 | [diff] [blame] | 185 | ctx = &st->_asn_ctx; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 186 | |
| 187 | switch(ctx->phase) { |
| 188 | case 0: |
| 189 | /* |
| 190 | * Check tags. |
| 191 | */ |
Lev Walkin | de4825d | 2004-09-29 13:20:14 +0000 | [diff] [blame] | 192 | rval = ber_check_tags(opt_codec_ctx, td, ctx, |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 193 | buf_ptr, size, tag_mode, -1, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 194 | &ctx->left, &tlv_constr); |
| 195 | if(rval.code != RC_OK) { |
| 196 | RETURN(rval.code); |
| 197 | } |
| 198 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 199 | if(tlv_constr) { |
| 200 | /* |
| 201 | * Complex operation, requires stack of expectations. |
| 202 | */ |
| 203 | ctx->ptr = _new_stack(); |
| 204 | if(ctx->ptr) { |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 205 | (void *)stck = ctx->ptr; |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 206 | if(type_type == _TT_BIT_STRING) { |
Lev Walkin | 8d12787 | 2004-09-04 04:44:50 +0000 | [diff] [blame] | 207 | /* Number of meaningless tail bits */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 208 | APPEND("\0", 1); |
| 209 | } |
| 210 | } else { |
| 211 | RETURN(RC_FAIL); |
| 212 | } |
| 213 | } else { |
| 214 | /* |
| 215 | * Jump into stackless primitive decoding. |
| 216 | */ |
| 217 | _CH_PHASE(ctx, 3); |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 218 | if(type_type == _TT_ANY) |
| 219 | APPEND(buf_ptr, rval.consumed); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 220 | ADVANCE(rval.consumed); |
| 221 | goto phase3; |
| 222 | } |
| 223 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 224 | NEXT_PHASE(ctx); |
| 225 | /* Fall through */ |
| 226 | case 1: |
| 227 | phase1: |
| 228 | /* |
| 229 | * Fill the stack with expectations. |
| 230 | */ |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 231 | (void *)stck = ctx->ptr; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 232 | sel = stck->cur_ptr; |
| 233 | do { |
| 234 | ber_tlv_tag_t tlv_tag; |
| 235 | ber_tlv_len_t tlv_len; |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 236 | ber_tlv_tag_t expected_tag; |
Lev Walkin | 5c91599 | 2004-09-27 20:54:06 +0000 | [diff] [blame] | 237 | ssize_t tl, ll, tlvl; |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 238 | /* This one works even if (sel->left == -1) */ |
| 239 | ssize_t Left = ((!sel||(size_t)sel->left >= size) |
Lev Walkin | 5c91599 | 2004-09-27 20:54:06 +0000 | [diff] [blame] | 240 | ?(ssize_t)size:sel->left); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 241 | |
Lev Walkin | 3990ba6 | 2004-09-24 20:57:41 +0000 | [diff] [blame] | 242 | |
Lev Walkin | 5c91599 | 2004-09-27 20:54:06 +0000 | [diff] [blame] | 243 | ASN_DEBUG("%p, s->l=%d, s->wn=%d, s->g=%d\n", sel, |
| 244 | sel?sel->left:0, |
| 245 | sel?sel->want_nulls:0, |
| 246 | sel?sel->got:0 |
| 247 | ); |
| 248 | if(sel && sel->left <= 0 && sel->want_nulls == 0) { |
| 249 | if(sel->prev) { |
| 250 | struct _stack_el *prev = sel->prev; |
| 251 | if(prev->left != -1) { |
| 252 | if(prev->left < sel->got) |
| 253 | RETURN(RC_FAIL); |
| 254 | prev->left -= sel->got; |
| 255 | } |
| 256 | prev->got += sel->got; |
| 257 | sel = stck->cur_ptr = prev; |
| 258 | if(!sel) break; |
| 259 | tlv_constr = 1; |
| 260 | continue; |
| 261 | } else { |
| 262 | sel = stck->cur_ptr = 0; |
| 263 | break; /* Nothing to wait */ |
| 264 | } |
| 265 | } |
| 266 | |
Lev Walkin | 3990ba6 | 2004-09-24 20:57:41 +0000 | [diff] [blame] | 267 | tl = ber_fetch_tag(buf_ptr, Left, &tlv_tag); |
Lev Walkin | 5c91599 | 2004-09-27 20:54:06 +0000 | [diff] [blame] | 268 | ASN_DEBUG("fetch tag(size=%d,L=%d), %sstack, left=%d, wn=%d, tl=%d", |
| 269 | (int)size, Left, sel?"":"!", |
| 270 | sel?sel->left:0, sel?sel->want_nulls:0, tl); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 271 | switch(tl) { |
| 272 | case -1: RETURN(RC_FAIL); |
| 273 | case 0: RETURN(RC_WMORE); |
| 274 | } |
| 275 | |
| 276 | tlv_constr = BER_TLV_CONSTRUCTED(buf_ptr); |
| 277 | |
| 278 | ll = ber_fetch_length(tlv_constr, |
Lev Walkin | 3990ba6 | 2004-09-24 20:57:41 +0000 | [diff] [blame] | 279 | (char *)buf_ptr + tl, Left - tl, &tlv_len); |
Lev Walkin | 5c91599 | 2004-09-27 20:54:06 +0000 | [diff] [blame] | 280 | ASN_DEBUG("Got tag=%s, tc=%d, left=%ld, tl=%d, len=%d, ll=%d", |
Lev Walkin | 8d12787 | 2004-09-04 04:44:50 +0000 | [diff] [blame] | 281 | ber_tlv_tag_string(tlv_tag), tlv_constr, |
Lev Walkin | 5c91599 | 2004-09-27 20:54:06 +0000 | [diff] [blame] | 282 | (long)Left, tl, tlv_len, ll); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 283 | switch(ll) { |
| 284 | case -1: RETURN(RC_FAIL); |
| 285 | case 0: RETURN(RC_WMORE); |
| 286 | } |
| 287 | |
Lev Walkin | 8d12787 | 2004-09-04 04:44:50 +0000 | [diff] [blame] | 288 | if(sel && sel->want_nulls |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 289 | && ((uint8_t *)buf_ptr)[0] == 0 |
| 290 | && ((uint8_t *)buf_ptr)[1] == 0) |
| 291 | { |
Lev Walkin | 8d12787 | 2004-09-04 04:44:50 +0000 | [diff] [blame] | 292 | |
| 293 | ASN_DEBUG("Eat EOC; wn=%d--", sel->want_nulls); |
| 294 | |
Lev Walkin | 5c91599 | 2004-09-27 20:54:06 +0000 | [diff] [blame] | 295 | if(type_type == _TT_ANY) APPEND("\0\0", 2); |
| 296 | |
| 297 | ADVANCE(2); |
| 298 | sel->got += 2; |
| 299 | if(sel->left != -1) { |
| 300 | sel->left -= 2; /* assert(sel->left >= 2) */ |
| 301 | } |
| 302 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 303 | sel->want_nulls--; |
| 304 | if(sel->want_nulls == 0) { |
| 305 | /* Move to the next expectation */ |
Lev Walkin | 5c91599 | 2004-09-27 20:54:06 +0000 | [diff] [blame] | 306 | sel->left = 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 307 | tlv_constr = 1; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 308 | } |
Lev Walkin | 8d12787 | 2004-09-04 04:44:50 +0000 | [diff] [blame] | 309 | |
| 310 | continue; |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | /* |
| 314 | * Set up expected tags, |
| 315 | * depending on ASN.1 type being decoded. |
| 316 | */ |
| 317 | switch(type_type) { |
| 318 | case _TT_BIT_STRING: |
| 319 | /* X.690: 8.6.4.1, NOTE 2 */ |
| 320 | /* Fall through */ |
| 321 | case _TT_GENERIC: |
| 322 | default: |
| 323 | if(sel) { |
| 324 | int level = sel->cont_level; |
| 325 | if(level < td->all_tags_count) { |
| 326 | expected_tag = td->all_tags[level]; |
| 327 | break; |
| 328 | } else if(td->all_tags_count) { |
| 329 | expected_tag = td->all_tags |
| 330 | [td->all_tags_count - 1]; |
| 331 | break; |
| 332 | } |
| 333 | /* else, Fall through */ |
| 334 | } |
| 335 | /* Fall through */ |
| 336 | case _TT_ANY: |
| 337 | expected_tag = tlv_tag; |
| 338 | break; |
| 339 | } |
| 340 | |
| 341 | |
| 342 | if(tlv_tag != expected_tag) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 343 | char buf[2][32]; |
| 344 | ber_tlv_tag_snprint(tlv_tag, |
| 345 | buf[0], sizeof(buf[0])); |
| 346 | ber_tlv_tag_snprint(td->tags[td->tags_count-1], |
| 347 | buf[1], sizeof(buf[1])); |
| 348 | ASN_DEBUG("Tag does not match expectation: %s != %s", |
| 349 | buf[0], buf[1]); |
| 350 | RETURN(RC_FAIL); |
| 351 | } |
| 352 | |
Lev Walkin | de4825d | 2004-09-29 13:20:14 +0000 | [diff] [blame] | 353 | tlvl = tl + ll; /* Combined length of T and L encoding */ |
| 354 | if((tlv_len + tlvl) < 0) { |
| 355 | /* tlv_len value is too big */ |
| 356 | ASN_DEBUG("TLV encoding + length (%ld) is too big", |
| 357 | (long)tlv_len); |
| 358 | RETURN(RC_FAIL); |
| 359 | } |
| 360 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 361 | /* |
| 362 | * Append a new expectation. |
| 363 | */ |
Lev Walkin | 3990ba6 | 2004-09-24 20:57:41 +0000 | [diff] [blame] | 364 | sel = OS__add_stack_el(stck); |
Lev Walkin | 5c91599 | 2004-09-27 20:54:06 +0000 | [diff] [blame] | 365 | if(!sel) RETURN(RC_FAIL); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 366 | |
Lev Walkin | 5c91599 | 2004-09-27 20:54:06 +0000 | [diff] [blame] | 367 | sel->tag = tlv_tag; |
| 368 | |
| 369 | sel->want_nulls = (tlv_len==-1); |
| 370 | if(sel->prev && sel->prev->left != -1) { |
| 371 | /* Check that the parent frame is big enough */ |
| 372 | if(sel->prev->left < tlvl + (tlv_len==-1?0:tlv_len)) |
| 373 | RETURN(RC_FAIL); |
| 374 | if(tlv_len == -1) |
| 375 | sel->left = sel->prev->left - tlvl; |
| 376 | else |
| 377 | sel->left = tlv_len; |
| 378 | } else { |
| 379 | sel->left = tlv_len; |
| 380 | } |
| 381 | if(type_type == _TT_ANY) APPEND(buf_ptr, tlvl); |
| 382 | sel->got += tlvl; |
| 383 | ADVANCE(tlvl); |
| 384 | |
| 385 | ASN_DEBUG("+EXPECT2 got=%d left=%d, wn=%d, clvl=%d", |
| 386 | sel->got, sel->left, sel->want_nulls, sel->cont_level); |
| 387 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 388 | } while(tlv_constr); |
| 389 | if(sel == NULL) { |
| 390 | /* Finished operation, "phase out" */ |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 391 | ASN_DEBUG("Phase out"); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 392 | _CH_PHASE(ctx, +3); |
| 393 | break; |
| 394 | } |
| 395 | |
| 396 | NEXT_PHASE(ctx); |
| 397 | /* Fall through */ |
| 398 | case 2: |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 399 | (void *)stck = ctx->ptr; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 400 | sel = stck->cur_ptr; |
Lev Walkin | 5c91599 | 2004-09-27 20:54:06 +0000 | [diff] [blame] | 401 | ASN_DEBUG("Phase 2: Need %ld bytes, size=%ld, alrg=%ld, wn=%d", |
| 402 | (long)sel->left, (long)size, (long)sel->got, |
| 403 | sel->want_nulls); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 404 | { |
| 405 | ber_tlv_len_t len; |
| 406 | |
| 407 | assert(sel->left >= 0); |
| 408 | |
Lev Walkin | ec1ffd4 | 2004-08-18 04:53:32 +0000 | [diff] [blame] | 409 | len = ((ber_tlv_len_t)size < sel->left) |
| 410 | ? (ber_tlv_len_t)size : sel->left; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 411 | if(len > 0) { |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 412 | if(type_type == _TT_BIT_STRING |
| 413 | && sel->bits_chopped == 0) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 414 | /* |
| 415 | * Finalize the previous chunk: |
| 416 | * strip down unused bits. |
| 417 | */ |
| 418 | st->buf[st->size-1] &= 0xff << st->buf[0]; |
| 419 | |
Lev Walkin | 4d9528c | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 420 | APPEND(((char *)buf_ptr+1), (len - 1)); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 421 | st->buf[0] = *(uint8_t *)buf_ptr; |
| 422 | sel->bits_chopped = 1; |
| 423 | } else { |
| 424 | APPEND(buf_ptr, len); |
| 425 | } |
| 426 | ADVANCE(len); |
| 427 | sel->left -= len; |
Lev Walkin | 5c91599 | 2004-09-27 20:54:06 +0000 | [diff] [blame] | 428 | sel->got += len; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 429 | } |
| 430 | |
Lev Walkin | 5c91599 | 2004-09-27 20:54:06 +0000 | [diff] [blame] | 431 | if(sel->left) { |
| 432 | ASN_DEBUG("OS left %ld, size = %ld, wn=%d\n", |
| 433 | (long)sel->left, (long)size, sel->want_nulls); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 434 | RETURN(RC_WMORE); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 435 | } |
Lev Walkin | 5c91599 | 2004-09-27 20:54:06 +0000 | [diff] [blame] | 436 | |
| 437 | PREV_PHASE(ctx); |
| 438 | goto phase1; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 439 | } |
| 440 | break; |
| 441 | case 3: |
| 442 | phase3: |
| 443 | /* |
| 444 | * Primitive form, no stack required. |
| 445 | */ |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 446 | if(size < (size_t)ctx->left) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 447 | APPEND(buf_ptr, size); |
| 448 | ctx->left -= size; |
| 449 | ADVANCE(size); |
| 450 | RETURN(RC_WMORE); |
| 451 | } else { |
| 452 | APPEND(buf_ptr, ctx->left); |
| 453 | ADVANCE(ctx->left); |
| 454 | ctx->left = 0; |
| 455 | |
| 456 | NEXT_PHASE(ctx); |
| 457 | } |
| 458 | break; |
| 459 | } |
| 460 | |
Lev Walkin | 5c91599 | 2004-09-27 20:54:06 +0000 | [diff] [blame] | 461 | if(sel) { |
| 462 | ASN_DEBUG("3sel p=%p, wn=%d, l=%ld, g=%ld, size=%ld", |
| 463 | sel->prev, sel->want_nulls, |
| 464 | (long)sel->left, (long)sel->got, (long)size); |
| 465 | if(sel->prev || sel->want_nulls > 1 || sel->left > 0) { |
| 466 | RETURN(RC_WMORE); |
| 467 | } |
| 468 | } |
| 469 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 470 | /* |
| 471 | * BIT STRING-specific processing. |
| 472 | */ |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 473 | if(type_type == _TT_BIT_STRING && st->size >= 2) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 474 | /* Finalize BIT STRING: zero out unused bits. */ |
| 475 | st->buf[st->size-1] &= 0xff << st->buf[0]; |
| 476 | } |
| 477 | |
| 478 | ASN_DEBUG("Took %d bytes to encode %s: [%s]:%d", |
Lev Walkin | 3990ba6 | 2004-09-24 20:57:41 +0000 | [diff] [blame] | 479 | consumed_myself, td->name, |
| 480 | (type_type == _TT_GENERIC) ? (char *)st->buf : "", st->size); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 481 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 482 | |
Lev Walkin | 5c91599 | 2004-09-27 20:54:06 +0000 | [diff] [blame] | 483 | RETURN(RC_OK); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | /* |
| 487 | * Encode OCTET STRING type using DER. |
| 488 | */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 489 | asn_enc_rval_t |
Lev Walkin | de4825d | 2004-09-29 13:20:14 +0000 | [diff] [blame] | 490 | OCTET_STRING_encode_der(asn_TYPE_descriptor_t *td, void *ptr, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 491 | int tag_mode, ber_tlv_tag_t tag, |
| 492 | asn_app_consume_bytes_f *cb, void *app_key) { |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 493 | asn_enc_rval_t erval; |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 494 | OCTET_STRING_t *st = (OCTET_STRING_t *)ptr; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 495 | int add_byte = 0; |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 496 | int is_bit_str = (td->specifics == (void *)1); |
| 497 | int is_ANY_type = (td->specifics == (void *)2); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 498 | |
| 499 | ASN_DEBUG("%s %s as OCTET STRING", |
Lev Walkin | 1f670c1 | 2004-09-02 12:57:25 +0000 | [diff] [blame] | 500 | cb?"Estimating":"Encoding", td->name); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 501 | |
| 502 | /* |
| 503 | * Canonicalize BIT STRING. |
| 504 | */ |
Lev Walkin | a737f3b | 2004-09-02 12:11:47 +0000 | [diff] [blame] | 505 | if(is_bit_str && st->buf) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 506 | switch(st->size) { |
| 507 | case 0: add_byte = 1; break; |
| 508 | case 1: st->buf[0] = 0; break; |
| 509 | default: |
| 510 | /* Finalize BIT STRING: zero out unused bits. */ |
| 511 | st->buf[st->size-1] &= 0xff << st->buf[0]; |
| 512 | } |
| 513 | } |
| 514 | |
Lev Walkin | a737f3b | 2004-09-02 12:11:47 +0000 | [diff] [blame] | 515 | if(is_ANY_type) { |
| 516 | erval.encoded = 0; |
| 517 | } else { |
Lev Walkin | 1f670c1 | 2004-09-02 12:57:25 +0000 | [diff] [blame] | 518 | erval.encoded = der_write_tags(td, st->size + add_byte, |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 519 | tag_mode, 0, tag, cb, app_key); |
Lev Walkin | a737f3b | 2004-09-02 12:11:47 +0000 | [diff] [blame] | 520 | if(erval.encoded == -1) { |
Lev Walkin | 1f670c1 | 2004-09-02 12:57:25 +0000 | [diff] [blame] | 521 | erval.failed_type = td; |
Lev Walkin | a737f3b | 2004-09-02 12:11:47 +0000 | [diff] [blame] | 522 | erval.structure_ptr = ptr; |
| 523 | return erval; |
| 524 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | if(cb) { |
| 528 | uint8_t zero; |
| 529 | uint8_t *buf; |
| 530 | int size; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 531 | |
| 532 | /* BIT STRING-aware handling */ |
| 533 | if(add_byte) { |
| 534 | zero = 0; |
| 535 | buf = &zero; |
| 536 | size = 1; |
| 537 | } else if(st->buf) { |
| 538 | buf = st->buf; |
| 539 | size = st->size; |
| 540 | } else { |
| 541 | assert(st->size == 0); |
| 542 | buf = 0; /* Not used */ |
| 543 | size = 0; |
| 544 | } |
| 545 | |
| 546 | if(size) { |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 547 | if(cb(buf, size, app_key) < 0) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 548 | erval.encoded = -1; |
Lev Walkin | 1f670c1 | 2004-09-02 12:57:25 +0000 | [diff] [blame] | 549 | erval.failed_type = td; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 550 | erval.structure_ptr = ptr; |
| 551 | return erval; |
| 552 | } |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | erval.encoded += st->size + add_byte; |
| 557 | |
| 558 | return erval; |
| 559 | } |
| 560 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 561 | asn_enc_rval_t |
Lev Walkin | de4825d | 2004-09-29 13:20:14 +0000 | [diff] [blame] | 562 | OCTET_STRING_encode_xer(asn_TYPE_descriptor_t *td, void *sptr, |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 563 | int ilevel, enum xer_encoder_flags_e flags, |
| 564 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 565 | static const char *h2c = "0123456789ABCDEF"; |
| 566 | const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr; |
| 567 | asn_enc_rval_t er; |
| 568 | char scratch[16 * 3 + 4]; |
| 569 | char *p = scratch; |
| 570 | uint8_t *buf; |
| 571 | uint8_t *end; |
| 572 | size_t i; |
| 573 | |
| 574 | if(!st || !st->buf) { |
| 575 | er.encoded = -1; |
| 576 | er.failed_type = td; |
| 577 | er.structure_ptr = sptr; |
| 578 | return er; |
| 579 | } |
| 580 | |
| 581 | er.encoded = 0; |
| 582 | |
| 583 | /* |
| 584 | * Dump the contents of the buffer in hexadecimal. |
| 585 | */ |
| 586 | buf = st->buf; |
| 587 | end = buf + st->size; |
| 588 | if(flags & XER_F_CANONICAL) { |
| 589 | char *scend = scratch + (sizeof(scratch) - 2); |
| 590 | for(; buf < end; buf++) { |
| 591 | if(p >= scend) { |
| 592 | _ASN_CALLBACK(scratch, p - scratch); |
| 593 | er.encoded += p - scratch; |
| 594 | p = scratch; |
| 595 | } |
| 596 | *p++ = h2c[(*buf >> 4) & 0x0F]; |
| 597 | *p++ = h2c[*buf & 0x0F]; |
| 598 | } |
Lev Walkin | cc6a910 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 599 | |
| 600 | _ASN_CALLBACK(scratch, p-scratch); /* Dump the rest */ |
| 601 | er.encoded += p - scratch; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 602 | } else { |
| 603 | for(i = 0; buf < end; buf++, i++) { |
| 604 | if(!(i % 16) && (i || st->size > 16)) { |
| 605 | _ASN_CALLBACK(scratch, p-scratch); |
| 606 | er.encoded += (p-scratch); |
| 607 | p = scratch; |
| 608 | _i_ASN_TEXT_INDENT(1, ilevel); |
| 609 | } |
| 610 | *p++ = h2c[(*buf >> 4) & 0x0F]; |
| 611 | *p++ = h2c[*buf & 0x0F]; |
| 612 | *p++ = 0x20; |
| 613 | } |
Lev Walkin | cc6a910 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 614 | if(p - scratch) { |
| 615 | p--; /* Remove the tail space */ |
| 616 | _ASN_CALLBACK(scratch, p-scratch); /* Dump the rest */ |
| 617 | er.encoded += p - scratch; |
| 618 | if(st->size > 16) |
| 619 | _i_ASN_TEXT_INDENT(1, ilevel-1); |
| 620 | } |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 621 | } |
| 622 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 623 | return er; |
| 624 | } |
| 625 | |
| 626 | asn_enc_rval_t |
Lev Walkin | de4825d | 2004-09-29 13:20:14 +0000 | [diff] [blame] | 627 | OCTET_STRING_encode_xer_ascii(asn_TYPE_descriptor_t *td, void *sptr, |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 628 | int ilevel, enum xer_encoder_flags_e flags, |
| 629 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 630 | const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr; |
| 631 | asn_enc_rval_t er; |
| 632 | |
| 633 | (void)ilevel; /* Unused argument */ |
| 634 | (void)flags; /* Unused argument */ |
| 635 | |
| 636 | if(!st || !st->buf) |
| 637 | _ASN_ENCODE_FAILED; |
| 638 | |
| 639 | _ASN_CALLBACK(st->buf, st->size); |
| 640 | er.encoded = st->size; |
| 641 | |
| 642 | return er; |
| 643 | } |
| 644 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 645 | int |
Lev Walkin | de4825d | 2004-09-29 13:20:14 +0000 | [diff] [blame] | 646 | 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] | 647 | asn_app_consume_bytes_f *cb, void *app_key) { |
Lev Walkin | 4d9528c | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 648 | static const char *h2c = "0123456789ABCDEF"; |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 649 | const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 650 | char scratch[16 * 3 + 4]; |
| 651 | char *p = scratch; |
| 652 | uint8_t *buf; |
| 653 | uint8_t *end; |
| 654 | size_t i; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 655 | |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 656 | (void)td; /* Unused argument */ |
| 657 | |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 658 | 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] | 659 | |
| 660 | /* |
| 661 | * Dump the contents of the buffer in hexadecimal. |
| 662 | */ |
| 663 | buf = st->buf; |
| 664 | end = buf + st->size; |
| 665 | for(i = 0; buf < end; buf++, i++) { |
| 666 | if(!(i % 16) && (i || st->size > 16)) { |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 667 | if(cb(scratch, p - scratch, app_key) < 0) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 668 | return -1; |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 669 | _i_INDENT(1); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 670 | p = scratch; |
| 671 | } |
| 672 | *p++ = h2c[(*buf >> 4) & 0x0F]; |
| 673 | *p++ = h2c[*buf & 0x0F]; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 674 | *p++ = 0x20; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 675 | } |
| 676 | |
Lev Walkin | cc6a910 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 677 | if(p > scratch) { |
| 678 | p--; /* Remove the tail space */ |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 679 | if(cb(scratch, p - scratch, app_key) < 0) |
Lev Walkin | cc6a910 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 680 | return -1; |
| 681 | } |
| 682 | |
| 683 | return 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | int |
Lev Walkin | de4825d | 2004-09-29 13:20:14 +0000 | [diff] [blame] | 687 | OCTET_STRING_print_ascii(asn_TYPE_descriptor_t *td, const void *sptr, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 688 | int ilevel, asn_app_consume_bytes_f *cb, void *app_key) { |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 689 | const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 690 | |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 691 | (void)td; /* Unused argument */ |
| 692 | (void)ilevel; /* Unused argument */ |
| 693 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 694 | if(st && st->buf) { |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 695 | return (cb(st->buf, st->size, app_key) < 0) ? -1 : 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 696 | } else { |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 697 | return (cb("<absent>", 8, app_key) < 0) ? -1 : 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 698 | } |
| 699 | } |
| 700 | |
| 701 | void |
Lev Walkin | de4825d | 2004-09-29 13:20:14 +0000 | [diff] [blame] | 702 | 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] | 703 | OCTET_STRING_t *st = (OCTET_STRING_t *)sptr; |
Lev Walkin | de4825d | 2004-09-29 13:20:14 +0000 | [diff] [blame] | 704 | struct _stack *stck = (struct _stack *)st->_asn_ctx.ptr; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 705 | |
| 706 | if(!td || !st) |
| 707 | return; |
| 708 | |
| 709 | ASN_DEBUG("Freeing %s as OCTET STRING", td->name); |
| 710 | |
| 711 | if(st->buf) { |
| 712 | FREEMEM(st->buf); |
| 713 | } |
| 714 | |
| 715 | /* |
| 716 | * Remove decode-time stack. |
| 717 | */ |
| 718 | if(stck) { |
| 719 | while(stck->tail) { |
| 720 | struct _stack_el *sel = stck->tail; |
| 721 | stck->tail = sel->prev; |
| 722 | FREEMEM(sel); |
| 723 | } |
| 724 | FREEMEM(stck); |
| 725 | } |
| 726 | |
| 727 | if(!contents_only) { |
| 728 | FREEMEM(st); |
| 729 | } |
| 730 | } |
| 731 | |
| 732 | /* |
| 733 | * Conversion routines. |
| 734 | */ |
| 735 | int |
| 736 | OCTET_STRING_fromBuf(OCTET_STRING_t *st, const char *str, int len) { |
| 737 | void *buf; |
| 738 | |
| 739 | if(st == 0 || (str == 0 && len)) { |
| 740 | errno = EINVAL; |
| 741 | return -1; |
| 742 | } |
| 743 | |
| 744 | /* |
| 745 | * Clear the OCTET STRING. |
| 746 | */ |
| 747 | if(str == NULL) { |
| 748 | if(st->buf) |
| 749 | FREEMEM(st->buf); |
| 750 | st->size = 0; |
| 751 | return 0; |
| 752 | } |
| 753 | |
| 754 | /* Determine the original string size, if not explicitly given */ |
| 755 | if(len < 0) |
| 756 | len = strlen(str); |
| 757 | |
| 758 | /* Allocate and fill the memory */ |
| 759 | buf = MALLOC(len + 1); |
| 760 | if(buf == NULL) { |
| 761 | return -1; |
| 762 | } else { |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 763 | st->buf = (uint8_t *)buf; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 764 | st->size = len; |
| 765 | } |
| 766 | |
| 767 | memcpy(buf, str, len); |
| 768 | st->buf[st->size] = '\0'; /* Couldn't use memcpy(len+1)! */ |
| 769 | |
| 770 | return 0; |
| 771 | } |
| 772 | |
| 773 | OCTET_STRING_t * |
| 774 | OCTET_STRING_new_fromBuf(const char *str, int len) { |
| 775 | OCTET_STRING_t *st; |
| 776 | |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 777 | (void *)st = CALLOC(1, sizeof(*st)); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 778 | if(st && str && OCTET_STRING_fromBuf(st, str, len)) { |
| 779 | free(st); |
| 780 | st = NULL; |
| 781 | } |
| 782 | |
| 783 | return st; |
| 784 | } |
| 785 | |