blob: 8e3e97f437b95a5003671af206e56386aff2fdb1 [file] [log] [blame]
Harald Welte92c45f32010-06-12 18:59:38 +02001/*-
2 * Copyright (c) 2003, 2004, 2005 Lev Walkin <vlm@lionet.info>.
3 * All rights reserved.
4 * Redistribution and modifications are permitted subject to BSD license.
5 */
6#include <asn_internal.h>
7#include <RELATIVE-OID.h>
Harald Welteec0e2172010-07-20 00:03:44 +02008#include <OCTET_STRING.h>
Harald Welte92c45f32010-06-12 18:59:38 +02009#include <asn_codecs_prim.h> /* Encoder and decoder of a primitive type */
10#include <limits.h> /* for CHAR_BIT */
11#include <errno.h>
12
13/*
14 * RELATIVE-OID basic type description.
15 */
Harald Welte41b85d52015-08-31 08:56:53 +020016static const ber_tlv_tag_t asn_DEF_RELATIVE_OID_tags[] = {
Harald Welte92c45f32010-06-12 18:59:38 +020017 (ASN_TAG_CLASS_UNIVERSAL | (13 << 2))
18};
19asn_TYPE_descriptor_t asn_DEF_RELATIVE_OID = {
20 "RELATIVE-OID",
21 "RELATIVE_OID",
22 ASN__PRIMITIVE_TYPE_free,
23 RELATIVE_OID_print,
24 asn_generic_no_constraint,
25 ber_decode_primitive,
26 der_encode_primitive,
27 RELATIVE_OID_decode_xer,
28 RELATIVE_OID_encode_xer,
Harald Welteec0e2172010-07-20 00:03:44 +020029 OCTET_STRING_decode_uper,
30 OCTET_STRING_encode_uper,
Harald Welte41b85d52015-08-31 08:56:53 +020031 OCTET_STRING_decode_aper,
32 OCTET_STRING_encode_aper,
Harald Welte92c45f32010-06-12 18:59:38 +020033 0, /* Use generic outmost tag fetcher */
34 asn_DEF_RELATIVE_OID_tags,
35 sizeof(asn_DEF_RELATIVE_OID_tags)
36 / sizeof(asn_DEF_RELATIVE_OID_tags[0]),
37 asn_DEF_RELATIVE_OID_tags, /* Same as above */
38 sizeof(asn_DEF_RELATIVE_OID_tags)
39 / sizeof(asn_DEF_RELATIVE_OID_tags[0]),
40 0, /* No PER visible constraints */
41 0, 0, /* No members */
42 0 /* No specifics */
43};
44
45static ssize_t
46RELATIVE_OID__dump_body(const RELATIVE_OID_t *st, asn_app_consume_bytes_f *cb, void *app_key) {
47 ssize_t wrote = 0;
48 ssize_t ret;
49 int startn;
50 int i;
51
52 for(i = 0, startn = 0; i < st->size; i++) {
53 uint8_t b = st->buf[i];
54 if((b & 0x80)) /* Continuation expected */
55 continue;
56 if(startn) {
57 /* Separate arcs */
58 if(cb(".", 1, app_key) < 0)
59 return -1;
60 wrote++;
61 }
62
63 ret = OBJECT_IDENTIFIER__dump_arc(&st->buf[startn],
64 i - startn + 1, 0, cb, app_key);
65 if(ret < 0) return -1;
66 wrote += ret;
67
68 startn = i + 1;
69 }
70
71 return wrote;
72}
73
74int
75RELATIVE_OID_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
76 asn_app_consume_bytes_f *cb, void *app_key) {
77 const RELATIVE_OID_t *st = (const RELATIVE_OID_t *)sptr;
78
79 (void)td; /* Unused argument */
80 (void)ilevel; /* Unused argument */
81
82 if(!st || !st->buf)
83 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
84
85 /* Dump preamble */
86 if(cb("{ ", 2, app_key) < 0)
87 return -1;
88
89 if(RELATIVE_OID__dump_body(st, cb, app_key) < 0)
90 return -1;
91
92 return (cb(" }", 2, app_key) < 0) ? -1 : 0;
93}
94
95static enum xer_pbd_rval
96RELATIVE_OID__xer_body_decode(asn_TYPE_descriptor_t *td, void *sptr, const void *chunk_buf, size_t chunk_size) {
97 RELATIVE_OID_t *st = (RELATIVE_OID_t *)sptr;
98 const char *chunk_end = (const char *)chunk_buf + chunk_size;
99 const char *endptr;
100 long s_arcs[6];
101 long *arcs = s_arcs;
102 int arcs_count;
103 int ret;
104
105 (void)td;
106
107 arcs_count = OBJECT_IDENTIFIER_parse_arcs(
108 (const char *)chunk_buf, chunk_size,
109 arcs, sizeof(s_arcs)/sizeof(s_arcs[0]), &endptr);
110 if(arcs_count < 0) {
Harald Welte41b85d52015-08-31 08:56:53 +0200111 /* Expecting at least one arc arcs */
Harald Welte92c45f32010-06-12 18:59:38 +0200112 return XPBD_BROKEN_ENCODING;
Harald Welte41b85d52015-08-31 08:56:53 +0200113 } else if(arcs_count == 0) {
114 return XPBD_NOT_BODY_IGNORE;
Harald Welte92c45f32010-06-12 18:59:38 +0200115 }
Harald Welte41b85d52015-08-31 08:56:53 +0200116 assert(endptr == chunk_end);
Harald Welte92c45f32010-06-12 18:59:38 +0200117
118 if((size_t)arcs_count > sizeof(s_arcs)/sizeof(s_arcs[0])) {
119 arcs = (long *)MALLOC(arcs_count * sizeof(long));
120 if(!arcs) return XPBD_SYSTEM_FAILURE;
121 ret = OBJECT_IDENTIFIER_parse_arcs(
122 (const char *)chunk_buf, chunk_size,
123 arcs, arcs_count, &endptr);
124 if(ret != arcs_count)
125 return XPBD_SYSTEM_FAILURE; /* assert?.. */
126 }
127
128 /*
129 * Convert arcs into BER representation.
130 */
131 ret = RELATIVE_OID_set_arcs(st, arcs, sizeof(*arcs), arcs_count);
132 if(arcs != s_arcs) FREEMEM(arcs);
133
134 return ret ? XPBD_SYSTEM_FAILURE : XPBD_BODY_CONSUMED;
135}
136
137asn_dec_rval_t
138RELATIVE_OID_decode_xer(asn_codec_ctx_t *opt_codec_ctx,
139 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
140 const void *buf_ptr, size_t size) {
141
142 return xer_decode_primitive(opt_codec_ctx, td,
143 sptr, sizeof(RELATIVE_OID_t), opt_mname,
144 buf_ptr, size, RELATIVE_OID__xer_body_decode);
145}
146
147asn_enc_rval_t
148RELATIVE_OID_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
149 int ilevel, enum xer_encoder_flags_e flags,
150 asn_app_consume_bytes_f *cb, void *app_key) {
151 RELATIVE_OID_t *st = (RELATIVE_OID_t *)sptr;
152 asn_enc_rval_t er;
153
154 (void)ilevel; /* Unused argument */
155 (void)flags; /* Unused argument */
156
157 if(!st || !st->buf)
158 _ASN_ENCODE_FAILED;
159
160 er.encoded = RELATIVE_OID__dump_body(st, cb, app_key);
161 if(er.encoded < 0) _ASN_ENCODE_FAILED;
162
163 _ASN_ENCODED_OK(er);
164}
165
166int
Harald Welte41b85d52015-08-31 08:56:53 +0200167RELATIVE_OID_get_arcs(const RELATIVE_OID_t *roid,
Harald Welte92c45f32010-06-12 18:59:38 +0200168 void *arcs, unsigned int arc_type_size, unsigned int arc_slots) {
169 void *arcs_end = (char *)arcs + (arc_slots * arc_type_size);
170 int num_arcs = 0;
171 int startn = 0;
172 int i;
173
174 if(!roid || !roid->buf) {
175 errno = EINVAL;
176 return -1;
177 }
178
179 for(i = 0; i < roid->size; i++) {
180 uint8_t b = roid->buf[i];
181 if((b & 0x80)) /* Continuation expected */
182 continue;
183
184 if(arcs < arcs_end) {
185 if(OBJECT_IDENTIFIER_get_single_arc(
186 &roid->buf[startn],
187 i - startn + 1, 0,
188 arcs, arc_type_size))
189 return -1;
190 arcs = ((char *)arcs) + arc_type_size;
191 num_arcs++;
192 }
193
194 startn = i + 1;
195 }
196
197 return num_arcs;
198}
199
200int
201RELATIVE_OID_set_arcs(RELATIVE_OID_t *roid, void *arcs, unsigned int arc_type_size, unsigned int arcs_slots) {
202 uint8_t *buf;
203 uint8_t *bp;
204 unsigned int size;
205 unsigned int i;
206
207 if(roid == NULL || arcs == NULL || arc_type_size < 1) {
208 errno = EINVAL;
209 return -1;
210 }
211
212 /*
213 * Roughly estimate the maximum size necessary to encode these arcs.
214 */
215 size = ((arc_type_size * CHAR_BIT + 6) / 7) * arcs_slots;
216 bp = buf = (uint8_t *)MALLOC(size + 1);
217 if(!buf) {
218 /* ENOMEM */
219 return -1;
220 }
221
222 /*
223 * Encode the arcs.
224 */
225 for(i = 0; i < arcs_slots; i++, arcs = ((char *)arcs) + arc_type_size) {
226 bp += OBJECT_IDENTIFIER_set_single_arc(bp,
227 arcs, arc_type_size, 0);
228 }
229
230 assert((unsigned)(bp - buf) <= size);
231
232 /*
233 * Replace buffer.
234 */
235 roid->size = (int)(bp - buf);
236 bp = roid->buf;
237 roid->buf = buf;
238 if(bp) FREEMEM(bp);
239
240 return 0;
241}
242