1 /*****************************************************************************
3 # Copyright 2019 AT&T Intellectual Property *
5 # Licensed under the Apache License, Version 2.0 (the "License"); *
6 # you may not use this file except in compliance with the License. *
7 # You may obtain a copy of the License at *
9 # http://www.apache.org/licenses/LICENSE-2.0 *
11 # Unless required by applicable law or agreed to in writing, software *
12 # distributed under the License is distributed on an "AS IS" BASIS, *
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
14 # See the License for the specific language governing permissions and *
15 # limitations under the License. *
17 ******************************************************************************/
20 * Copyright (c) 2004-2017 Lev Walkin <vlm@lionet.info>. All rights reserved.
21 * Redistribution and modifications are permitted subject to BSD license.
23 #include <asn_internal.h>
27 asn_OCTET_STRING_specifics_t asn_SPC_ANY_specs = {
29 offsetof(ANY_t, _asn_ctx),
32 asn_TYPE_operation_t asn_OP_ANY = {
36 OCTET_STRING_decode_ber,
37 OCTET_STRING_encode_der,
38 OCTET_STRING_decode_xer_hex,
40 #ifdef ASN_DISABLE_OER_SUPPORT
46 #endif /* ASN_DISABLE_OER_SUPPORT */
47 #ifdef ASN_DISABLE_PER_SUPPORT
54 #endif /* ASN_DISABLE_PER_SUPPORT */
55 0, /* Random fill is not defined for ANY type */
56 0 /* Use generic outmost tag fetcher */
58 asn_TYPE_descriptor_t asn_DEF_ANY = {
63 { 0, 0, asn_generic_no_constraint }, /* No constraints */
64 0, 0, /* No members */
69 #define RETURN(_code) \
71 asn_dec_rval_t tmprval; \
72 tmprval.code = _code; \
73 tmprval.consumed = consumed_myself; \
78 ANY_encode_xer(const asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
79 enum xer_encoder_flags_e flags, asn_app_consume_bytes_f *cb,
81 if(flags & XER_F_CANONICAL) {
83 * Canonical XER-encoding of ANY type is not supported.
89 return OCTET_STRING_encode_xer(td, sptr, ilevel, flags, cb, app_key);
92 struct _callback_arg {
98 static int ANY__consume_bytes(const void *buffer, size_t size, void *key);
101 ANY_fromType(ANY_t *st, asn_TYPE_descriptor_t *td, void *sptr) {
102 struct _callback_arg arg;
103 asn_enc_rval_t erval = {0,0,0};
111 if(st->buf) FREEMEM(st->buf);
116 arg.offset = arg.size = 0;
119 erval = der_encode(td, sptr, ANY__consume_bytes, &arg);
120 if(erval.encoded == -1) {
121 if(arg.buffer) FREEMEM(arg.buffer);
124 assert((size_t)erval.encoded == arg.offset);
126 if(st->buf) FREEMEM(st->buf);
127 st->buf = arg.buffer;
128 st->size = arg.offset;
134 ANY_fromType_aper(ANY_t *st, asn_TYPE_descriptor_t *td, void *sptr) {
135 uint8_t *buffer = NULL;
144 if(st->buf) FREEMEM(st->buf);
149 erval = aper_encode_to_new_buffer(td, td->encoding_constraints.per_constraints, sptr, (void**)&buffer);
152 if(buffer) FREEMEM(buffer);
155 assert((size_t)erval > 0);
157 if(st->buf) FREEMEM(st->buf);
165 ANY_new_fromType(asn_TYPE_descriptor_t *td, void *sptr) {
174 memset(&tmp, 0, sizeof(tmp));
176 if(ANY_fromType(&tmp, td, sptr)) return 0;
178 st = (ANY_t *)CALLOC(1, sizeof(ANY_t));
189 ANY_new_fromType_aper(asn_TYPE_descriptor_t *td, void *sptr) {
198 memset(&tmp, 0, sizeof(tmp));
200 if(ANY_fromType_aper(&tmp, td, sptr)) return 0;
202 st = (ANY_t *)CALLOC(1, sizeof(ANY_t));
213 ANY_to_type(ANY_t *st, asn_TYPE_descriptor_t *td, void **struct_ptr) {
217 if(!st || !td || !struct_ptr) {
223 /* Nothing to convert, make it empty. */
224 *struct_ptr = (void *)0;
228 rval = ber_decode(0, td, (void **)&newst, st->buf, st->size);
229 if(rval.code == RC_OK) {
233 /* Remove possibly partially decoded data. */
234 ASN_STRUCT_FREE(*td, newst);
240 ANY_to_type_aper(ANY_t *st, asn_TYPE_descriptor_t *td, void **struct_ptr) {
244 if(!st || !td || !struct_ptr) {
250 /* Nothing to convert, make it empty. */
251 *struct_ptr = (void *)0;
255 rval = aper_decode(0, td, (void **)&newst, st->buf, st->size, 0, 0);
256 if(rval.code == RC_OK) {
260 /* Remove possibly partially decoded data. */
261 ASN_STRUCT_FREE(*td, newst);
266 static int ANY__consume_bytes(const void *buffer, size_t size, void *key) {
267 struct _callback_arg *arg = (struct _callback_arg *)key;
269 if((arg->offset + size) >= arg->size) {
270 size_t nsize = (arg->size ? arg->size << 2 : 16) + size;
271 void *p = REALLOC(arg->buffer, nsize);
273 arg->buffer = (uint8_t *)p;
277 memcpy(arg->buffer + arg->offset, buffer, size);
279 assert(arg->offset < arg->size);
284 #ifndef ASN_DISABLE_PER_SUPPORT
287 ANY_decode_uper(const asn_codec_ctx_t *opt_codec_ctx,
288 const asn_TYPE_descriptor_t *td,
289 const asn_per_constraints_t *constraints, void **sptr,
290 asn_per_data_t *pd) {
291 const asn_OCTET_STRING_specifics_t *specs =
292 td->specifics ? (const asn_OCTET_STRING_specifics_t *)td->specifics
293 : &asn_SPC_ANY_specs;
294 size_t consumed_myself = 0;
296 ANY_t *st = (ANY_t *)*sptr;
302 * Allocate the structure.
305 st = (ANY_t *)(*sptr = CALLOC(1, specs->struct_size));
306 if(!st) RETURN(RC_FAIL);
309 ASN_DEBUG("UPER Decoding ANY type");
319 /* Get the PER length */
320 raw_len = uper_get_length(pd, -1, 0, &repeat);
321 if(raw_len < 0) RETURN(RC_WMORE);
322 if(raw_len == 0 && st->buf) break;
324 ASN_DEBUG("Got PER length len %" ASN_PRI_SIZE ", %s (%s)", raw_len,
325 repeat ? "repeat" : "once", td->name);
327 len_bits = len_bytes * 8;
329 p = REALLOC(st->buf, st->size + len_bytes + 1);
330 if(!p) RETURN(RC_FAIL);
331 st->buf = (uint8_t *)p;
333 ret = per_get_many_bits(pd, &st->buf[st->size], 0, len_bits);
334 if(ret < 0) RETURN(RC_WMORE);
335 consumed_myself += len_bits;
336 st->size += len_bytes;
338 st->buf[st->size] = 0; /* nul-terminate */
344 ANY_encode_uper(const asn_TYPE_descriptor_t *td,
345 const asn_per_constraints_t *constraints, const void *sptr,
346 asn_per_outp_t *po) {
347 const ANY_t *st = (const ANY_t *)sptr;
348 asn_enc_rval_t er = {0, 0, 0};
355 if(!st || (!st->buf && st->size)) ASN__ENCODE_FAILED;
361 ssize_t may_save = uper_put_length(po, size, &need_eom);
362 if(may_save < 0) ASN__ENCODE_FAILED;
364 ret = per_put_many_bits(po, buf, may_save * 8);
365 if(ret) ASN__ENCODE_FAILED;
369 assert(!(may_save & 0x07) || !size);
370 if(need_eom && uper_put_length(po, 0, 0))
371 ASN__ENCODE_FAILED; /* End of Message length */
378 ANY_decode_aper(const asn_codec_ctx_t *opt_codec_ctx,
379 const asn_TYPE_descriptor_t *td,
380 const asn_per_constraints_t *constraints, void **sptr,
381 asn_per_data_t *pd) {
382 const asn_OCTET_STRING_specifics_t *specs =
383 td->specifics ? (const asn_OCTET_STRING_specifics_t *)td->specifics
384 : &asn_SPC_ANY_specs;
385 size_t consumed_myself = 0;
387 ANY_t *st = (ANY_t *)*sptr;
393 * Allocate the structure.
396 st = (ANY_t *)(*sptr = CALLOC(1, specs->struct_size));
397 if(!st) RETURN(RC_FAIL);
400 ASN_DEBUG("APER Decoding ANY type");
410 /* Get the PER length */
411 raw_len = aper_get_length(pd, -1, 0, &repeat);
412 if(raw_len < 0) RETURN(RC_WMORE);
413 if(raw_len == 0 && st->buf) break;
415 ASN_DEBUG("Got PER length len %" ASN_PRI_SIZE ", %s (%s)", raw_len,
416 repeat ? "repeat" : "once", td->name);
418 len_bits = len_bytes * 8;
420 p = REALLOC(st->buf, st->size + len_bytes + 1);
421 if(!p) RETURN(RC_FAIL);
422 st->buf = (uint8_t *)p;
424 ret = per_get_many_bits(pd, &st->buf[st->size], 0, len_bits);
425 if(ret < 0) RETURN(RC_WMORE);
426 consumed_myself += len_bits;
427 st->size += len_bytes;
429 st->buf[st->size] = 0; /* nul-terminate */
435 ANY_encode_aper(const asn_TYPE_descriptor_t *td,
436 const asn_per_constraints_t *constraints, const void *sptr,
437 asn_per_outp_t *po) {
438 const ANY_t *st = (const ANY_t *)sptr;
439 asn_enc_rval_t er = {0, 0, 0};
446 if(!st || (!st->buf && st->size)) ASN__ENCODE_FAILED;
452 ssize_t may_save = uper_put_length(po, size, &need_eom);
453 if(may_save < 0) ASN__ENCODE_FAILED;
455 ret = per_put_many_bits(po, buf, may_save * 8);
456 if(ret) ASN__ENCODE_FAILED;
460 assert(!(may_save & 0x07) || !size);
461 if(need_eom && uper_put_length(po, 0, 0))
462 ASN__ENCODE_FAILED; /* End of Message length */
467 #endif /* ASN_DISABLE_PER_SUPPORT */