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