blob: 82850f12cbc400c838b05e6bbd67287ea20a016b [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};
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080019asn_TYPE_operation_t asn_OP_RELATIVE_OID = {
Lev Walkin8e8078a2004-09-26 13:10:40 +000020 ASN__PRIMITIVE_TYPE_free,
Lev Walkina9cc46e2004-09-22 16:06:28 +000021 RELATIVE_OID_print,
Lev Walkincd2f48e2017-08-10 02:14:59 -070022 OCTET_STRING_compare, /* Implemented in terms of opaque comparison */
Lev Walkin8e8078a2004-09-26 13:10:40 +000023 ber_decode_primitive,
24 der_encode_primitive,
Lev Walkin92302252004-10-23 10:16:51 +000025 RELATIVE_OID_decode_xer,
Lev Walkina9cc46e2004-09-22 16:06:28 +000026 RELATIVE_OID_encode_xer,
Lev Walkincc159472017-07-06 08:26:36 -070027#ifdef ASN_DISABLE_OER_SUPPORT
28 0,
29 0,
30#else
31 0,
32 0,
33#endif /* ASN_DISABLE_OER_SUPPORT */
Lev Walkinb33425f2017-07-14 14:59:52 +040034#ifdef ASN_DISABLE_PER_SUPPORT
35 0,
36 0,
37#else
38 OCTET_STRING_decode_uper,
39 OCTET_STRING_encode_uper,
40#endif /* ASN_DISABLE_PER_SUPPORT */
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080041 0 /* Use generic outmost tag fetcher */
42};
43asn_TYPE_descriptor_t asn_DEF_RELATIVE_OID = {
44 "RELATIVE-OID",
45 "RELATIVE_OID",
46 &asn_OP_RELATIVE_OID,
47 asn_generic_no_constraint,
Lev Walkin5e033762004-09-29 13:26:15 +000048 asn_DEF_RELATIVE_OID_tags,
49 sizeof(asn_DEF_RELATIVE_OID_tags)
50 / sizeof(asn_DEF_RELATIVE_OID_tags[0]),
51 asn_DEF_RELATIVE_OID_tags, /* Same as above */
52 sizeof(asn_DEF_RELATIVE_OID_tags)
53 / sizeof(asn_DEF_RELATIVE_OID_tags[0]),
Lev Walkin76780762017-07-07 10:07:30 -070054 0, /* No OER visible constraints */
Lev Walkin59b176e2005-11-26 11:25:14 +000055 0, /* No PER visible constraints */
Lev Walkin449f8322004-08-20 13:23:42 +000056 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +000057 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +000058};
59
Lev Walkina9cc46e2004-09-22 16:06:28 +000060static ssize_t
61RELATIVE_OID__dump_body(const RELATIVE_OID_t *st, asn_app_consume_bytes_f *cb, void *app_key) {
62 ssize_t wrote = 0;
63 ssize_t ret;
Lev Walkin494fb702017-08-07 20:07:00 -070064 size_t startn;
65 size_t i;
Lev Walkina9cc46e2004-09-22 16:06:28 +000066
67 for(i = 0, startn = 0; i < st->size; i++) {
68 uint8_t b = st->buf[i];
69 if((b & 0x80)) /* Continuation expected */
70 continue;
71 if(startn) {
72 /* Separate arcs */
73 if(cb(".", 1, app_key) < 0)
74 return -1;
75 wrote++;
76 }
77
78 ret = OBJECT_IDENTIFIER__dump_arc(&st->buf[startn],
79 i - startn + 1, 0, cb, app_key);
80 if(ret < 0) return -1;
81 wrote += ret;
82
83 startn = i + 1;
84 }
85
86 return wrote;
87}
88
Lev Walkinf15320b2004-06-03 03:38:44 +000089int
Lev Walkin5e033762004-09-29 13:26:15 +000090RELATIVE_OID_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +000091 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +000092 const RELATIVE_OID_t *st = (const RELATIVE_OID_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +000093
Lev Walkind9bd7752004-06-05 08:17:50 +000094 (void)td; /* Unused argument */
95 (void)ilevel; /* Unused argument */
96
Lev Walkinf15320b2004-06-03 03:38:44 +000097 if(!st || !st->buf)
Lev Walkin8e8078a2004-09-26 13:10:40 +000098 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +000099
100 /* Dump preamble */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000101 if(cb("{ ", 2, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000102 return -1;
103
Lev Walkina9cc46e2004-09-22 16:06:28 +0000104 if(RELATIVE_OID__dump_body(st, cb, app_key) < 0)
105 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000106
Lev Walkin8e8078a2004-09-26 13:10:40 +0000107 return (cb(" }", 2, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000108}
109
Lev Walkin0fab1a62005-03-09 22:19:25 +0000110static enum xer_pbd_rval
111RELATIVE_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 +0000112 RELATIVE_OID_t *st = (RELATIVE_OID_t *)sptr;
Lev Walkin2c34aad2005-03-10 11:50:24 +0000113 const char *chunk_end = (const char *)chunk_buf + chunk_size;
114 const char *endptr;
Lev Walkin92302252004-10-23 10:16:51 +0000115 long s_arcs[6];
116 long *arcs = s_arcs;
117 int arcs_count;
118 int ret;
119
Lev Walkine0b56e02005-02-25 12:10:27 +0000120 (void)td;
121
Lev Walkin92302252004-10-23 10:16:51 +0000122 arcs_count = OBJECT_IDENTIFIER_parse_arcs(
123 (const char *)chunk_buf, chunk_size,
Lev Walkin0fab1a62005-03-09 22:19:25 +0000124 arcs, sizeof(s_arcs)/sizeof(s_arcs[0]), &endptr);
125 if(arcs_count < 0) {
Lev Walkinaed43c82012-09-04 14:56:27 -0700126 /* Expecting at least one arc arcs */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000127 return XPBD_BROKEN_ENCODING;
Lev Walkinaed43c82012-09-04 14:56:27 -0700128 } else if(arcs_count == 0) {
129 return XPBD_NOT_BODY_IGNORE;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000130 }
Lev Walkinaed43c82012-09-04 14:56:27 -0700131 assert(endptr == chunk_end);
Lev Walkin0fab1a62005-03-09 22:19:25 +0000132
133 if((size_t)arcs_count > sizeof(s_arcs)/sizeof(s_arcs[0])) {
Lev Walkin92302252004-10-23 10:16:51 +0000134 arcs = (long *)MALLOC(arcs_count * sizeof(long));
Lev Walkin0fab1a62005-03-09 22:19:25 +0000135 if(!arcs) return XPBD_SYSTEM_FAILURE;
Lev Walkin92302252004-10-23 10:16:51 +0000136 ret = OBJECT_IDENTIFIER_parse_arcs(
137 (const char *)chunk_buf, chunk_size,
138 arcs, arcs_count, &endptr);
139 if(ret != arcs_count)
Lev Walkin0fab1a62005-03-09 22:19:25 +0000140 return XPBD_SYSTEM_FAILURE; /* assert?.. */
Lev Walkin92302252004-10-23 10:16:51 +0000141 }
142
143 /*
144 * Convert arcs into BER representation.
145 */
146 ret = RELATIVE_OID_set_arcs(st, arcs, sizeof(*arcs), arcs_count);
Lev Walkin92302252004-10-23 10:16:51 +0000147 if(arcs != s_arcs) FREEMEM(arcs);
148
Lev Walkin2c34aad2005-03-10 11:50:24 +0000149 return ret ? XPBD_SYSTEM_FAILURE : XPBD_BODY_CONSUMED;
Lev Walkin92302252004-10-23 10:16:51 +0000150}
151
152asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -0700153RELATIVE_OID_decode_xer(const asn_codec_ctx_t *opt_codec_ctx,
Lev Walkin92302252004-10-23 10:16:51 +0000154 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000155 const void *buf_ptr, size_t size) {
Lev Walkin92302252004-10-23 10:16:51 +0000156
157 return xer_decode_primitive(opt_codec_ctx, td,
158 sptr, sizeof(RELATIVE_OID_t), opt_mname,
159 buf_ptr, size, RELATIVE_OID__xer_body_decode);
160}
161
Lev Walkina9cc46e2004-09-22 16:06:28 +0000162asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000163RELATIVE_OID_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000164 int ilevel, enum xer_encoder_flags_e flags,
165 asn_app_consume_bytes_f *cb, void *app_key) {
166 RELATIVE_OID_t *st = (RELATIVE_OID_t *)sptr;
167 asn_enc_rval_t er;
168
169 (void)ilevel; /* Unused argument */
170 (void)flags; /* Unused argument */
171
172 if(!st || !st->buf)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700173 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000174
175 er.encoded = RELATIVE_OID__dump_body(st, cb, app_key);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700176 if(er.encoded < 0) ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000177
Lev Walkin7c1dc052016-03-14 03:08:15 -0700178 ASN__ENCODED_OK(er);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000179}
Lev Walkinf15320b2004-06-03 03:38:44 +0000180
181int
Wim Lewis14e6b162014-07-23 16:06:01 -0700182RELATIVE_OID_get_arcs(const RELATIVE_OID_t *roid,
Lev Walkin29a044b2004-06-14 07:24:36 +0000183 void *arcs, unsigned int arc_type_size, unsigned int arc_slots) {
Lev Walkin4d9528c2004-08-11 08:10:13 +0000184 void *arcs_end = (char *)arcs + (arc_slots * arc_type_size);
Lev Walkin29a044b2004-06-14 07:24:36 +0000185 int num_arcs = 0;
Lev Walkin494fb702017-08-07 20:07:00 -0700186 size_t startn = 0;
187 size_t i;
Lev Walkinf15320b2004-06-03 03:38:44 +0000188
189 if(!roid || !roid->buf) {
190 errno = EINVAL;
191 return -1;
192 }
193
194 for(i = 0; i < roid->size; i++) {
195 uint8_t b = roid->buf[i];
196 if((b & 0x80)) /* Continuation expected */
197 continue;
198
Lev Walkin29a044b2004-06-14 07:24:36 +0000199 if(arcs < arcs_end) {
200 if(OBJECT_IDENTIFIER_get_single_arc(
201 &roid->buf[startn],
202 i - startn + 1, 0,
203 arcs, arc_type_size))
Lev Walkinf15320b2004-06-03 03:38:44 +0000204 return -1;
Lev Walkin4ce78ca2004-08-25 01:34:11 +0000205 arcs = ((char *)arcs) + arc_type_size;
Lev Walkin29a044b2004-06-14 07:24:36 +0000206 num_arcs++;
Lev Walkinf15320b2004-06-03 03:38:44 +0000207 }
208
209 startn = i + 1;
210 }
211
Lev Walkin29a044b2004-06-14 07:24:36 +0000212 return num_arcs;
Lev Walkinf15320b2004-06-03 03:38:44 +0000213}
214
215int
Lev Walkin0787ff02004-06-17 23:43:39 +0000216RELATIVE_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 +0000217 uint8_t *buf;
218 uint8_t *bp;
Lev Walkin34b2a932004-06-17 23:46:45 +0000219 unsigned int size;
220 unsigned int i;
Lev Walkinf15320b2004-06-03 03:38:44 +0000221
Lev Walkin0787ff02004-06-17 23:43:39 +0000222 if(roid == NULL || arcs == NULL || arc_type_size < 1) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000223 errno = EINVAL;
224 return -1;
225 }
226
227 /*
228 * Roughly estimate the maximum size necessary to encode these arcs.
229 */
Lev Walkin0787ff02004-06-17 23:43:39 +0000230 size = ((arc_type_size * CHAR_BIT + 6) / 7) * arcs_slots;
Lev Walkinc2346572004-08-11 09:07:36 +0000231 bp = buf = (uint8_t *)MALLOC(size + 1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000232 if(!buf) {
233 /* ENOMEM */
234 return -1;
235 }
236
237 /*
Lev Walkin0787ff02004-06-17 23:43:39 +0000238 * Encode the arcs.
Lev Walkinf15320b2004-06-03 03:38:44 +0000239 */
Lev Walkin4ce78ca2004-08-25 01:34:11 +0000240 for(i = 0; i < arcs_slots; i++, arcs = ((char *)arcs) + arc_type_size) {
Lev Walkin0787ff02004-06-17 23:43:39 +0000241 bp += OBJECT_IDENTIFIER_set_single_arc(bp,
242 arcs, arc_type_size, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000243 }
244
Lev Walkin34b2a932004-06-17 23:46:45 +0000245 assert((unsigned)(bp - buf) <= size);
Lev Walkin0787ff02004-06-17 23:43:39 +0000246
Lev Walkinf15320b2004-06-03 03:38:44 +0000247 /*
248 * Replace buffer.
249 */
Lev Walkin4efbfb72005-02-25 14:20:30 +0000250 roid->size = (int)(bp - buf);
Lev Walkinf15320b2004-06-03 03:38:44 +0000251 bp = roid->buf;
252 roid->buf = buf;
253 if(bp) FREEMEM(bp);
254
255 return 0;
256}
Lev Walkin0787ff02004-06-17 23:43:39 +0000257