blob: 30f1e29ff920ee13f33e912fe09fe764904f2e99 [file] [log] [blame]
Lev Walkina737f3b2004-09-02 12:11:47 +00001/*-
2 * Copyright (c) 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
3 * Redistribution and modifications are permitted subject to BSD license.
4 */
Lev Walkina9cc46e2004-09-22 16:06:28 +00005#include <asn_internal.h>
Lev Walkina737f3b2004-09-02 12:11:47 +00006#include <ANY.h>
Lev Walkina737f3b2004-09-02 12:11:47 +00007#include <errno.h>
8
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +08009static asn_OCTET_STRING_specifics_t asn_SPC_ANY_specs = {
Lev Walkinbbd93252004-10-12 05:57:23 +000010 sizeof(ANY_t),
11 offsetof(ANY_t, _asn_ctx),
Lev Walkin3a4689a2006-11-24 11:20:27 +000012 ASN_OSUBV_ANY
Lev Walkinbbd93252004-10-12 05:57:23 +000013};
Lev Walkin5e033762004-09-29 13:26:15 +000014asn_TYPE_descriptor_t asn_DEF_ANY = {
Lev Walkina737f3b2004-09-02 12:11:47 +000015 "ANY",
Lev Walkindc06f6b2004-10-20 15:50:55 +000016 "ANY",
Lev Walkina9cc46e2004-09-22 16:06:28 +000017 OCTET_STRING_free,
18 OCTET_STRING_print,
Lev Walkina737f3b2004-09-02 12:11:47 +000019 asn_generic_no_constraint,
20 OCTET_STRING_decode_ber,
21 OCTET_STRING_encode_der,
Lev Walkindc06f6b2004-10-20 15:50:55 +000022 OCTET_STRING_decode_xer_hex,
Lev Walkina9cc46e2004-09-22 16:06:28 +000023 ANY_encode_xer,
Lev Walkincc159472017-07-06 08:26:36 -070024#ifdef ASN_DISABLE_OER_SUPPORT
25 0,
26 0,
27#else
28 0,
29 0,
30#endif /* ASN_DISABLE_OER_SUPPORT */
Lev Walkinb33425f2017-07-14 14:59:52 +040031#ifdef ASN_DISABLE_PER_SUPPORT
32 0, 0,
33#else
34 0, 0,
35#endif /* ASN_DISABLE_PER_SUPPORT */
Lev Walkina737f3b2004-09-02 12:11:47 +000036 0, /* Use generic outmost tag fetcher */
Lev Walkind563d392004-09-13 08:26:57 +000037 0, 0, 0, 0,
Lev Walkin76780762017-07-07 10:07:30 -070038 0, /* No OER visible constraints */
Lev Walkin59b176e2005-11-26 11:25:14 +000039 0, /* No PER visible constraints */
Lev Walkina737f3b2004-09-02 12:11:47 +000040 0, 0, /* No members */
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +080041 &asn_SPC_ANY_specs,
Lev Walkina737f3b2004-09-02 12:11:47 +000042};
Lev Walkind563d392004-09-13 08:26:57 +000043
44
Lev Walkina9cc46e2004-09-22 16:06:28 +000045asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +000046ANY_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +000047 int ilevel, enum xer_encoder_flags_e flags,
48 asn_app_consume_bytes_f *cb, void *app_key) {
49
Lev Walkincc6a9102004-09-23 22:06:26 +000050 if(flags & XER_F_CANONICAL) {
51 /*
52 * Canonical XER-encoding of ANY type is not supported.
53 */
Lev Walkin7c1dc052016-03-14 03:08:15 -070054 ASN__ENCODE_FAILED;
Lev Walkincc6a9102004-09-23 22:06:26 +000055 }
Lev Walkina9cc46e2004-09-22 16:06:28 +000056
Lev Walkincc6a9102004-09-23 22:06:26 +000057 /* Dump as binary */
58 return OCTET_STRING_encode_xer(td, sptr, ilevel, flags, cb, app_key);
Lev Walkina9cc46e2004-09-22 16:06:28 +000059}
60
Lev Walkind563d392004-09-13 08:26:57 +000061struct _callback_arg {
62 uint8_t *buffer;
63 size_t offset;
64 size_t size;
65};
66
67static int ANY__consume_bytes(const void *buffer, size_t size, void *key);
68
69int
Lev Walkin5e033762004-09-29 13:26:15 +000070ANY_fromType(ANY_t *st, asn_TYPE_descriptor_t *td, void *sptr) {
Lev Walkind563d392004-09-13 08:26:57 +000071 struct _callback_arg arg;
Lev Walkina9cc46e2004-09-22 16:06:28 +000072 asn_enc_rval_t erval;
Lev Walkind563d392004-09-13 08:26:57 +000073
74 if(!st || !td) {
75 errno = EINVAL;
76 return -1;
77 }
78
79 if(!sptr) {
80 if(st->buf) FREEMEM(st->buf);
81 st->size = 0;
82 return 0;
83 }
84
85 arg.offset = arg.size = 0;
86 arg.buffer = 0;
87
88 erval = der_encode(td, sptr, ANY__consume_bytes, &arg);
89 if(erval.encoded == -1) {
90 if(arg.buffer) FREEMEM(arg.buffer);
91 return -1;
92 }
Lev Walkina9cc46e2004-09-22 16:06:28 +000093 assert((size_t)erval.encoded == arg.offset);
Lev Walkind563d392004-09-13 08:26:57 +000094
95 if(st->buf) FREEMEM(st->buf);
96 st->buf = arg.buffer;
97 st->size = arg.offset;
98
99 return 0;
100}
101
102ANY_t *
Lev Walkin5e033762004-09-29 13:26:15 +0000103ANY_new_fromType(asn_TYPE_descriptor_t *td, void *sptr) {
Lev Walkind563d392004-09-13 08:26:57 +0000104 ANY_t tmp;
105 ANY_t *st;
106
107 if(!td || !sptr) {
108 errno = EINVAL;
109 return 0;
110 }
111
112 memset(&tmp, 0, sizeof(tmp));
113
114 if(ANY_fromType(&tmp, td, sptr)) return 0;
115
Lev Walkin40fecb02005-01-22 00:11:28 +0000116 st = (ANY_t *)CALLOC(1, sizeof(ANY_t));
Lev Walkind563d392004-09-13 08:26:57 +0000117 if(st) {
118 *st = tmp;
119 return st;
120 } else {
121 FREEMEM(tmp.buf);
122 return 0;
123 }
124}
125
126int
Lev Walkin5e033762004-09-29 13:26:15 +0000127ANY_to_type(ANY_t *st, asn_TYPE_descriptor_t *td, void **struct_ptr) {
Lev Walkindc06f6b2004-10-20 15:50:55 +0000128 asn_dec_rval_t rval;
Lev Walkind563d392004-09-13 08:26:57 +0000129 void *newst = 0;
130
131 if(!st || !td || !struct_ptr) {
132 errno = EINVAL;
133 return -1;
134 }
135
136 if(st->buf == 0) {
137 /* Nothing to convert, make it empty. */
138 *struct_ptr = (void *)0;
139 return 0;
140 }
141
Lev Walkin5e033762004-09-29 13:26:15 +0000142 rval = ber_decode(0, td, (void **)&newst, st->buf, st->size);
Lev Walkind563d392004-09-13 08:26:57 +0000143 if(rval.code == RC_OK) {
144 *struct_ptr = newst;
145 return 0;
146 } else {
147 /* Remove possibly partially decoded data. */
Lev Walkinadcb5862006-03-17 02:11:12 +0000148 ASN_STRUCT_FREE(*td, newst);
Lev Walkind563d392004-09-13 08:26:57 +0000149 return -1;
150 }
151}
152
153static int ANY__consume_bytes(const void *buffer, size_t size, void *key) {
154 struct _callback_arg *arg = (struct _callback_arg *)key;
155
156 if((arg->offset + size) >= arg->size) {
157 size_t nsize = (arg->size ? arg->size << 2 : 16) + size;
158 void *p = REALLOC(arg->buffer, nsize);
159 if(!p) return -1;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000160 arg->buffer = (uint8_t *)p;
Lev Walkind563d392004-09-13 08:26:57 +0000161 arg->size = nsize;
162 }
163
164 memcpy(arg->buffer + arg->offset, buffer, size);
165 arg->offset += size;
166 assert(arg->offset < arg->size);
167
168 return 0;
169}
170