blob: 352ce79fcb9ed04ce963412d4601b9602d9fc474 [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001/*-
Lev Walkin2c34aad2005-03-10 11:50:24 +00002 * Copyright (c) 2003, 2004, 2005 Lev Walkin <vlm@lionet.info>.
3 * All rights reserved.
Lev Walkinf15320b2004-06-03 03:38:44 +00004 * Redistribution and modifications are permitted subject to BSD license.
5 */
Lev Walkina9cc46e2004-09-22 16:06:28 +00006#include <asn_internal.h>
Lev Walkinf15320b2004-06-03 03:38:44 +00007#include <RELATIVE-OID.h>
Lev Walkin725883b2006-10-09 12:07:58 +00008#include <OCTET_STRING.h>
Lev Walkin3256d6f2004-10-21 11:22:12 +00009#include <asn_codecs_prim.h> /* Encoder and decoder of a primitive type */
Lev Walkin0787ff02004-06-17 23:43:39 +000010#include <limits.h> /* for CHAR_BIT */
Lev Walkinf15320b2004-06-03 03:38:44 +000011#include <errno.h>
12
13/*
14 * RELATIVE-OID basic type description.
15 */
Wim Lewis18c2ec92014-07-29 11:30:10 -070016static const ber_tlv_tag_t asn_DEF_RELATIVE_OID_tags[] = {
Lev Walkinf15320b2004-06-03 03:38:44 +000017 (ASN_TAG_CLASS_UNIVERSAL | (13 << 2))
18};
Lev Walkin5e033762004-09-29 13:26:15 +000019asn_TYPE_descriptor_t asn_DEF_RELATIVE_OID = {
Lev Walkinf15320b2004-06-03 03:38:44 +000020 "RELATIVE-OID",
Lev Walkindc06f6b2004-10-20 15:50:55 +000021 "RELATIVE_OID",
Lev Walkin8e8078a2004-09-26 13:10:40 +000022 ASN__PRIMITIVE_TYPE_free,
Lev Walkina9cc46e2004-09-22 16:06:28 +000023 RELATIVE_OID_print,
Lev Walkinf15320b2004-06-03 03:38:44 +000024 asn_generic_no_constraint,
Lev Walkin8e8078a2004-09-26 13:10:40 +000025 ber_decode_primitive,
26 der_encode_primitive,
Lev Walkin92302252004-10-23 10:16:51 +000027 RELATIVE_OID_decode_xer,
Lev Walkina9cc46e2004-09-22 16:06:28 +000028 RELATIVE_OID_encode_xer,
Lev Walkin725883b2006-10-09 12:07:58 +000029 OCTET_STRING_decode_uper,
30 OCTET_STRING_encode_uper,
Lev Walkinf15320b2004-06-03 03:38:44 +000031 0, /* Use generic outmost tag fetcher */
Lev Walkin5e033762004-09-29 13:26:15 +000032 asn_DEF_RELATIVE_OID_tags,
33 sizeof(asn_DEF_RELATIVE_OID_tags)
34 / sizeof(asn_DEF_RELATIVE_OID_tags[0]),
35 asn_DEF_RELATIVE_OID_tags, /* Same as above */
36 sizeof(asn_DEF_RELATIVE_OID_tags)
37 / sizeof(asn_DEF_RELATIVE_OID_tags[0]),
Lev Walkin59b176e2005-11-26 11:25:14 +000038 0, /* No PER visible constraints */
Lev Walkin449f8322004-08-20 13:23:42 +000039 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +000040 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +000041};
42
Lev Walkina9cc46e2004-09-22 16:06:28 +000043static ssize_t
44RELATIVE_OID__dump_body(const RELATIVE_OID_t *st, asn_app_consume_bytes_f *cb, void *app_key) {
45 ssize_t wrote = 0;
46 ssize_t ret;
47 int startn;
48 int i;
49
50 for(i = 0, startn = 0; i < st->size; i++) {
51 uint8_t b = st->buf[i];
52 if((b & 0x80)) /* Continuation expected */
53 continue;
54 if(startn) {
55 /* Separate arcs */
56 if(cb(".", 1, app_key) < 0)
57 return -1;
58 wrote++;
59 }
60
61 ret = OBJECT_IDENTIFIER__dump_arc(&st->buf[startn],
62 i - startn + 1, 0, cb, app_key);
63 if(ret < 0) return -1;
64 wrote += ret;
65
66 startn = i + 1;
67 }
68
69 return wrote;
70}
71
Lev Walkinf15320b2004-06-03 03:38:44 +000072int
Lev Walkin5e033762004-09-29 13:26:15 +000073RELATIVE_OID_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +000074 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +000075 const RELATIVE_OID_t *st = (const RELATIVE_OID_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +000076
Lev Walkind9bd7752004-06-05 08:17:50 +000077 (void)td; /* Unused argument */
78 (void)ilevel; /* Unused argument */
79
Lev Walkinf15320b2004-06-03 03:38:44 +000080 if(!st || !st->buf)
Lev Walkin8e8078a2004-09-26 13:10:40 +000081 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +000082
83 /* Dump preamble */
Lev Walkin8e8078a2004-09-26 13:10:40 +000084 if(cb("{ ", 2, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +000085 return -1;
86
Lev Walkina9cc46e2004-09-22 16:06:28 +000087 if(RELATIVE_OID__dump_body(st, cb, app_key) < 0)
88 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +000089
Lev Walkin8e8078a2004-09-26 13:10:40 +000090 return (cb(" }", 2, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +000091}
92
Lev Walkin0fab1a62005-03-09 22:19:25 +000093static enum xer_pbd_rval
94RELATIVE_OID__xer_body_decode(asn_TYPE_descriptor_t *td, void *sptr, const void *chunk_buf, size_t chunk_size) {
Lev Walkin92302252004-10-23 10:16:51 +000095 RELATIVE_OID_t *st = (RELATIVE_OID_t *)sptr;
Lev Walkin2c34aad2005-03-10 11:50:24 +000096 const char *chunk_end = (const char *)chunk_buf + chunk_size;
97 const char *endptr;
Lev Walkin92302252004-10-23 10:16:51 +000098 long s_arcs[6];
99 long *arcs = s_arcs;
100 int arcs_count;
101 int ret;
102
Lev Walkine0b56e02005-02-25 12:10:27 +0000103 (void)td;
104
Lev Walkin92302252004-10-23 10:16:51 +0000105 arcs_count = OBJECT_IDENTIFIER_parse_arcs(
106 (const char *)chunk_buf, chunk_size,
Lev Walkin0fab1a62005-03-09 22:19:25 +0000107 arcs, sizeof(s_arcs)/sizeof(s_arcs[0]), &endptr);
108 if(arcs_count < 0) {
Lev Walkinaed43c82012-09-04 14:56:27 -0700109 /* Expecting at least one arc arcs */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000110 return XPBD_BROKEN_ENCODING;
Lev Walkinaed43c82012-09-04 14:56:27 -0700111 } else if(arcs_count == 0) {
112 return XPBD_NOT_BODY_IGNORE;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000113 }
Lev Walkinaed43c82012-09-04 14:56:27 -0700114 assert(endptr == chunk_end);
Lev Walkin0fab1a62005-03-09 22:19:25 +0000115
116 if((size_t)arcs_count > sizeof(s_arcs)/sizeof(s_arcs[0])) {
Lev Walkin92302252004-10-23 10:16:51 +0000117 arcs = (long *)MALLOC(arcs_count * sizeof(long));
Lev Walkin0fab1a62005-03-09 22:19:25 +0000118 if(!arcs) return XPBD_SYSTEM_FAILURE;
Lev Walkin92302252004-10-23 10:16:51 +0000119 ret = OBJECT_IDENTIFIER_parse_arcs(
120 (const char *)chunk_buf, chunk_size,
121 arcs, arcs_count, &endptr);
122 if(ret != arcs_count)
Lev Walkin0fab1a62005-03-09 22:19:25 +0000123 return XPBD_SYSTEM_FAILURE; /* assert?.. */
Lev Walkin92302252004-10-23 10:16:51 +0000124 }
125
126 /*
127 * Convert arcs into BER representation.
128 */
129 ret = RELATIVE_OID_set_arcs(st, arcs, sizeof(*arcs), arcs_count);
Lev Walkin92302252004-10-23 10:16:51 +0000130 if(arcs != s_arcs) FREEMEM(arcs);
131
Lev Walkin2c34aad2005-03-10 11:50:24 +0000132 return ret ? XPBD_SYSTEM_FAILURE : XPBD_BODY_CONSUMED;
Lev Walkin92302252004-10-23 10:16:51 +0000133}
134
135asn_dec_rval_t
136RELATIVE_OID_decode_xer(asn_codec_ctx_t *opt_codec_ctx,
137 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000138 const void *buf_ptr, size_t size) {
Lev Walkin92302252004-10-23 10:16:51 +0000139
140 return xer_decode_primitive(opt_codec_ctx, td,
141 sptr, sizeof(RELATIVE_OID_t), opt_mname,
142 buf_ptr, size, RELATIVE_OID__xer_body_decode);
143}
144
Lev Walkina9cc46e2004-09-22 16:06:28 +0000145asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000146RELATIVE_OID_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000147 int ilevel, enum xer_encoder_flags_e flags,
148 asn_app_consume_bytes_f *cb, void *app_key) {
149 RELATIVE_OID_t *st = (RELATIVE_OID_t *)sptr;
150 asn_enc_rval_t er;
151
152 (void)ilevel; /* Unused argument */
153 (void)flags; /* Unused argument */
154
155 if(!st || !st->buf)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700156 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000157
158 er.encoded = RELATIVE_OID__dump_body(st, cb, app_key);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700159 if(er.encoded < 0) ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000160
Lev Walkin7c1dc052016-03-14 03:08:15 -0700161 ASN__ENCODED_OK(er);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000162}
Lev Walkinf15320b2004-06-03 03:38:44 +0000163
164int
Wim Lewis14e6b162014-07-23 16:06:01 -0700165RELATIVE_OID_get_arcs(const RELATIVE_OID_t *roid,
Lev Walkin29a044b2004-06-14 07:24:36 +0000166 void *arcs, unsigned int arc_type_size, unsigned int arc_slots) {
Lev Walkin4d9528c2004-08-11 08:10:13 +0000167 void *arcs_end = (char *)arcs + (arc_slots * arc_type_size);
Lev Walkin29a044b2004-06-14 07:24:36 +0000168 int num_arcs = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000169 int startn = 0;
170 int i;
171
172 if(!roid || !roid->buf) {
173 errno = EINVAL;
174 return -1;
175 }
176
177 for(i = 0; i < roid->size; i++) {
178 uint8_t b = roid->buf[i];
179 if((b & 0x80)) /* Continuation expected */
180 continue;
181
Lev Walkin29a044b2004-06-14 07:24:36 +0000182 if(arcs < arcs_end) {
183 if(OBJECT_IDENTIFIER_get_single_arc(
184 &roid->buf[startn],
185 i - startn + 1, 0,
186 arcs, arc_type_size))
Lev Walkinf15320b2004-06-03 03:38:44 +0000187 return -1;
Lev Walkin4ce78ca2004-08-25 01:34:11 +0000188 arcs = ((char *)arcs) + arc_type_size;
Lev Walkin29a044b2004-06-14 07:24:36 +0000189 num_arcs++;
Lev Walkinf15320b2004-06-03 03:38:44 +0000190 }
191
192 startn = i + 1;
193 }
194
Lev Walkin29a044b2004-06-14 07:24:36 +0000195 return num_arcs;
Lev Walkinf15320b2004-06-03 03:38:44 +0000196}
197
198int
Lev Walkin0787ff02004-06-17 23:43:39 +0000199RELATIVE_OID_set_arcs(RELATIVE_OID_t *roid, void *arcs, unsigned int arc_type_size, unsigned int arcs_slots) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000200 uint8_t *buf;
201 uint8_t *bp;
Lev Walkin34b2a932004-06-17 23:46:45 +0000202 unsigned int size;
203 unsigned int i;
Lev Walkinf15320b2004-06-03 03:38:44 +0000204
Lev Walkin0787ff02004-06-17 23:43:39 +0000205 if(roid == NULL || arcs == NULL || arc_type_size < 1) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000206 errno = EINVAL;
207 return -1;
208 }
209
210 /*
211 * Roughly estimate the maximum size necessary to encode these arcs.
212 */
Lev Walkin0787ff02004-06-17 23:43:39 +0000213 size = ((arc_type_size * CHAR_BIT + 6) / 7) * arcs_slots;
Lev Walkinc2346572004-08-11 09:07:36 +0000214 bp = buf = (uint8_t *)MALLOC(size + 1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000215 if(!buf) {
216 /* ENOMEM */
217 return -1;
218 }
219
220 /*
Lev Walkin0787ff02004-06-17 23:43:39 +0000221 * Encode the arcs.
Lev Walkinf15320b2004-06-03 03:38:44 +0000222 */
Lev Walkin4ce78ca2004-08-25 01:34:11 +0000223 for(i = 0; i < arcs_slots; i++, arcs = ((char *)arcs) + arc_type_size) {
Lev Walkin0787ff02004-06-17 23:43:39 +0000224 bp += OBJECT_IDENTIFIER_set_single_arc(bp,
225 arcs, arc_type_size, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000226 }
227
Lev Walkin34b2a932004-06-17 23:46:45 +0000228 assert((unsigned)(bp - buf) <= size);
Lev Walkin0787ff02004-06-17 23:43:39 +0000229
Lev Walkinf15320b2004-06-03 03:38:44 +0000230 /*
231 * Replace buffer.
232 */
Lev Walkin4efbfb72005-02-25 14:20:30 +0000233 roid->size = (int)(bp - buf);
Lev Walkinf15320b2004-06-03 03:38:44 +0000234 bp = roid->buf;
235 roid->buf = buf;
236 if(bp) FREEMEM(bp);
237
238 return 0;
239}
Lev Walkin0787ff02004-06-17 23:43:39 +0000240