Harald Welte | d523a69 | 2015-08-30 23:11:19 +0200 | [diff] [blame^] | 1 | /*- |
| 2 | * Copyright (c) 2004 Lev Walkin <vlm@lionet.info>. All rights reserved. |
| 3 | * Redistribution and modifications are permitted subject to BSD license. |
| 4 | */ |
| 5 | #include <asn_internal.h> |
| 6 | #include <ANY.h> |
| 7 | #include <errno.h> |
| 8 | |
| 9 | static asn_OCTET_STRING_specifics_t asn_DEF_ANY_specs = { |
| 10 | sizeof(ANY_t), |
| 11 | offsetof(ANY_t, _asn_ctx), |
| 12 | ASN_OSUBV_ANY |
| 13 | }; |
| 14 | asn_TYPE_descriptor_t asn_DEF_ANY = { |
| 15 | "ANY", |
| 16 | "ANY", |
| 17 | OCTET_STRING_free, |
| 18 | OCTET_STRING_print, |
| 19 | asn_generic_no_constraint, |
| 20 | OCTET_STRING_decode_ber, |
| 21 | OCTET_STRING_encode_der, |
| 22 | OCTET_STRING_decode_xer_hex, |
| 23 | ANY_encode_xer, |
| 24 | OCTET_STRING_decode_uper, |
| 25 | OCTET_STRING_encode_uper, |
| 26 | OCTET_STRING_decode_aper, |
| 27 | OCTET_STRING_encode_aper, |
| 28 | 0, /* Use generic outmost tag fetcher */ |
| 29 | 0, 0, 0, 0, |
| 30 | 0, /* No PER visible constraints */ |
| 31 | 0, 0, /* No members */ |
| 32 | &asn_DEF_ANY_specs, |
| 33 | }; |
| 34 | |
| 35 | |
| 36 | asn_enc_rval_t |
| 37 | ANY_encode_xer(asn_TYPE_descriptor_t *td, void *sptr, |
| 38 | int ilevel, enum xer_encoder_flags_e flags, |
| 39 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 40 | |
| 41 | if(flags & XER_F_CANONICAL) { |
| 42 | /* |
| 43 | * Canonical XER-encoding of ANY type is not supported. |
| 44 | */ |
| 45 | _ASN_ENCODE_FAILED; |
| 46 | } |
| 47 | |
| 48 | /* Dump as binary */ |
| 49 | return OCTET_STRING_encode_xer(td, sptr, ilevel, flags, cb, app_key); |
| 50 | } |
| 51 | |
| 52 | struct _callback_arg { |
| 53 | uint8_t *buffer; |
| 54 | size_t offset; |
| 55 | size_t size; |
| 56 | }; |
| 57 | |
| 58 | static int ANY__consume_bytes(const void *buffer, size_t size, void *key); |
| 59 | |
| 60 | int |
| 61 | ANY_fromType(ANY_t *st, asn_TYPE_descriptor_t *td, void *sptr) { |
| 62 | struct _callback_arg arg; |
| 63 | asn_enc_rval_t erval; |
| 64 | |
| 65 | if(!st || !td) { |
| 66 | errno = EINVAL; |
| 67 | return -1; |
| 68 | } |
| 69 | |
| 70 | if(!sptr) { |
| 71 | if(st->buf) FREEMEM(st->buf); |
| 72 | st->size = 0; |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | arg.offset = arg.size = 0; |
| 77 | arg.buffer = 0; |
| 78 | |
| 79 | erval = der_encode(td, sptr, ANY__consume_bytes, &arg); |
| 80 | if(erval.encoded == -1) { |
| 81 | if(arg.buffer) FREEMEM(arg.buffer); |
| 82 | return -1; |
| 83 | } |
| 84 | assert((size_t)erval.encoded == arg.offset); |
| 85 | |
| 86 | if(st->buf) FREEMEM(st->buf); |
| 87 | st->buf = arg.buffer; |
| 88 | st->size = arg.offset; |
| 89 | |
| 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | int |
| 94 | ANY_fromType_aper(ANY_t *st, asn_TYPE_descriptor_t *td, void *sptr) { |
| 95 | uint8_t *buffer = NULL; |
| 96 | ssize_t erval; |
| 97 | |
| 98 | if(!st || !td) { |
| 99 | errno = EINVAL; |
| 100 | return -1; |
| 101 | } |
| 102 | |
| 103 | if(!sptr) { |
| 104 | if(st->buf) FREEMEM(st->buf); |
| 105 | st->size = 0; |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | erval = aper_encode_to_new_buffer(td, td->per_constraints, sptr, (void**)&buffer); |
| 110 | |
| 111 | if(erval == -1) { |
| 112 | if(buffer) FREEMEM(buffer); |
| 113 | return -1; |
| 114 | } |
| 115 | assert((size_t)erval > 0); |
| 116 | |
| 117 | if(st->buf) FREEMEM(st->buf); |
| 118 | st->buf = buffer; |
| 119 | st->size = erval; |
| 120 | |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | ANY_t * |
| 125 | ANY_new_fromType(asn_TYPE_descriptor_t *td, void *sptr) { |
| 126 | ANY_t tmp; |
| 127 | ANY_t *st; |
| 128 | |
| 129 | if(!td || !sptr) { |
| 130 | errno = EINVAL; |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | memset(&tmp, 0, sizeof(tmp)); |
| 135 | |
| 136 | if(ANY_fromType(&tmp, td, sptr)) return 0; |
| 137 | |
| 138 | st = (ANY_t *)CALLOC(1, sizeof(ANY_t)); |
| 139 | if(st) { |
| 140 | *st = tmp; |
| 141 | return st; |
| 142 | } else { |
| 143 | FREEMEM(tmp.buf); |
| 144 | return 0; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | ANY_t * |
| 149 | ANY_new_fromType_aper(asn_TYPE_descriptor_t *td, void *sptr) { |
| 150 | ANY_t tmp; |
| 151 | ANY_t *st; |
| 152 | |
| 153 | if(!td || !sptr) { |
| 154 | errno = EINVAL; |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | memset(&tmp, 0, sizeof(tmp)); |
| 159 | |
| 160 | if(ANY_fromType_aper(&tmp, td, sptr)) return 0; |
| 161 | |
| 162 | st = (ANY_t *)CALLOC(1, sizeof(ANY_t)); |
| 163 | if(st) { |
| 164 | *st = tmp; |
| 165 | return st; |
| 166 | } else { |
| 167 | FREEMEM(tmp.buf); |
| 168 | return 0; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | int |
| 173 | ANY_to_type(ANY_t *st, asn_TYPE_descriptor_t *td, void **struct_ptr) { |
| 174 | asn_dec_rval_t rval; |
| 175 | void *newst = 0; |
| 176 | |
| 177 | if(!st || !td || !struct_ptr) { |
| 178 | errno = EINVAL; |
| 179 | return -1; |
| 180 | } |
| 181 | |
| 182 | if(st->buf == 0) { |
| 183 | /* Nothing to convert, make it empty. */ |
| 184 | *struct_ptr = (void *)0; |
| 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | rval = ber_decode(0, td, (void **)&newst, st->buf, st->size); |
| 189 | if(rval.code == RC_OK) { |
| 190 | *struct_ptr = newst; |
| 191 | return 0; |
| 192 | } else { |
| 193 | /* Remove possibly partially decoded data. */ |
| 194 | ASN_STRUCT_FREE(*td, newst); |
| 195 | return -1; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | int |
| 200 | ANY_to_type_aper(ANY_t *st, asn_TYPE_descriptor_t *td, void **struct_ptr) { |
| 201 | asn_dec_rval_t rval; |
| 202 | void *newst = 0; |
| 203 | |
| 204 | if(!st || !td || !struct_ptr) { |
| 205 | errno = EINVAL; |
| 206 | return -1; |
| 207 | } |
| 208 | |
| 209 | if(st->buf == 0) { |
| 210 | /* Nothing to convert, make it empty. */ |
| 211 | *struct_ptr = (void *)0; |
| 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | rval = aper_decode(0, td, (void **)&newst, st->buf, st->size, 0, 0); |
| 216 | if(rval.code == RC_OK) { |
| 217 | *struct_ptr = newst; |
| 218 | return 0; |
| 219 | } else { |
| 220 | /* Remove possibly partially decoded data. */ |
| 221 | ASN_STRUCT_FREE(*td, newst); |
| 222 | return -1; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | static int ANY__consume_bytes(const void *buffer, size_t size, void *key) { |
| 227 | struct _callback_arg *arg = (struct _callback_arg *)key; |
| 228 | |
| 229 | if((arg->offset + size) >= arg->size) { |
| 230 | size_t nsize = (arg->size ? arg->size << 2 : 16) + size; |
| 231 | void *p = REALLOC(arg->buffer, nsize); |
| 232 | if(!p) return -1; |
| 233 | arg->buffer = (uint8_t *)p; |
| 234 | arg->size = nsize; |
| 235 | } |
| 236 | |
| 237 | memcpy(arg->buffer + arg->offset, buffer, size); |
| 238 | arg->offset += size; |
| 239 | assert(arg->offset < arg->size); |
| 240 | |
| 241 | return 0; |
| 242 | } |
| 243 | |