blob: 6a064d036de43967b5bc708e96bc9167e1c0dbd0 [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001/*-
2 * Copyright (c) 2003, 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 Walkinf15320b2004-06-03 03:38:44 +00006#include <RELATIVE-OID.h>
Lev Walkin3256d6f2004-10-21 11:22:12 +00007#include <asn_codecs_prim.h> /* Encoder and decoder of a primitive type */
Lev Walkin0787ff02004-06-17 23:43:39 +00008#include <limits.h> /* for CHAR_BIT */
Lev Walkinf15320b2004-06-03 03:38:44 +00009#include <assert.h>
10#include <errno.h>
11
12/*
13 * RELATIVE-OID basic type description.
14 */
Lev Walkin5e033762004-09-29 13:26:15 +000015static ber_tlv_tag_t asn_DEF_RELATIVE_OID_tags[] = {
Lev Walkinf15320b2004-06-03 03:38:44 +000016 (ASN_TAG_CLASS_UNIVERSAL | (13 << 2))
17};
Lev Walkin5e033762004-09-29 13:26:15 +000018asn_TYPE_descriptor_t asn_DEF_RELATIVE_OID = {
Lev Walkinf15320b2004-06-03 03:38:44 +000019 "RELATIVE-OID",
Lev Walkindc06f6b2004-10-20 15:50:55 +000020 "RELATIVE_OID",
Lev Walkin8e8078a2004-09-26 13:10:40 +000021 ASN__PRIMITIVE_TYPE_free,
Lev Walkina9cc46e2004-09-22 16:06:28 +000022 RELATIVE_OID_print,
Lev Walkinf15320b2004-06-03 03:38:44 +000023 asn_generic_no_constraint,
Lev Walkin8e8078a2004-09-26 13:10:40 +000024 ber_decode_primitive,
25 der_encode_primitive,
Lev Walkin92302252004-10-23 10:16:51 +000026 RELATIVE_OID_decode_xer,
Lev Walkina9cc46e2004-09-22 16:06:28 +000027 RELATIVE_OID_encode_xer,
Lev Walkinf15320b2004-06-03 03:38:44 +000028 0, /* Use generic outmost tag fetcher */
Lev Walkin5e033762004-09-29 13:26:15 +000029 asn_DEF_RELATIVE_OID_tags,
30 sizeof(asn_DEF_RELATIVE_OID_tags)
31 / sizeof(asn_DEF_RELATIVE_OID_tags[0]),
32 asn_DEF_RELATIVE_OID_tags, /* Same as above */
33 sizeof(asn_DEF_RELATIVE_OID_tags)
34 / sizeof(asn_DEF_RELATIVE_OID_tags[0]),
Lev Walkin449f8322004-08-20 13:23:42 +000035 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +000036 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +000037};
38
Lev Walkina9cc46e2004-09-22 16:06:28 +000039static ssize_t
40RELATIVE_OID__dump_body(const RELATIVE_OID_t *st, asn_app_consume_bytes_f *cb, void *app_key) {
41 ssize_t wrote = 0;
42 ssize_t ret;
43 int startn;
44 int i;
45
46 for(i = 0, startn = 0; i < st->size; i++) {
47 uint8_t b = st->buf[i];
48 if((b & 0x80)) /* Continuation expected */
49 continue;
50 if(startn) {
51 /* Separate arcs */
52 if(cb(".", 1, app_key) < 0)
53 return -1;
54 wrote++;
55 }
56
57 ret = OBJECT_IDENTIFIER__dump_arc(&st->buf[startn],
58 i - startn + 1, 0, cb, app_key);
59 if(ret < 0) return -1;
60 wrote += ret;
61
62 startn = i + 1;
63 }
64
65 return wrote;
66}
67
Lev Walkinf15320b2004-06-03 03:38:44 +000068int
Lev Walkin5e033762004-09-29 13:26:15 +000069RELATIVE_OID_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +000070 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +000071 const RELATIVE_OID_t *st = (const RELATIVE_OID_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +000072
Lev Walkind9bd7752004-06-05 08:17:50 +000073 (void)td; /* Unused argument */
74 (void)ilevel; /* Unused argument */
75
Lev Walkinf15320b2004-06-03 03:38:44 +000076 if(!st || !st->buf)
Lev Walkin8e8078a2004-09-26 13:10:40 +000077 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +000078
79 /* Dump preamble */
Lev Walkin8e8078a2004-09-26 13:10:40 +000080 if(cb("{ ", 2, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +000081 return -1;
82
Lev Walkina9cc46e2004-09-22 16:06:28 +000083 if(RELATIVE_OID__dump_body(st, cb, app_key) < 0)
84 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +000085
Lev Walkin8e8078a2004-09-26 13:10:40 +000086 return (cb(" }", 2, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +000087}
88
Lev Walkin0fab1a62005-03-09 22:19:25 +000089static enum xer_pbd_rval
90RELATIVE_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 +000091 RELATIVE_OID_t *st = (RELATIVE_OID_t *)sptr;
92 char *endptr;
93 long s_arcs[6];
94 long *arcs = s_arcs;
95 int arcs_count;
96 int ret;
97
Lev Walkine0b56e02005-02-25 12:10:27 +000098 (void)td;
99
Lev Walkin92302252004-10-23 10:16:51 +0000100 arcs_count = OBJECT_IDENTIFIER_parse_arcs(
101 (const char *)chunk_buf, chunk_size,
Lev Walkin0fab1a62005-03-09 22:19:25 +0000102 arcs, sizeof(s_arcs)/sizeof(s_arcs[0]), &endptr);
103 if(arcs_count < 0) {
104 /* Expecting at least zero arcs */
105 return XPBD_BROKEN_ENCODING;
106 }
107
108 if((size_t)arcs_count > sizeof(s_arcs)/sizeof(s_arcs[0])) {
Lev Walkin92302252004-10-23 10:16:51 +0000109 arcs = (long *)MALLOC(arcs_count * sizeof(long));
Lev Walkin0fab1a62005-03-09 22:19:25 +0000110 if(!arcs) return XPBD_SYSTEM_FAILURE;
Lev Walkin92302252004-10-23 10:16:51 +0000111 ret = OBJECT_IDENTIFIER_parse_arcs(
112 (const char *)chunk_buf, chunk_size,
113 arcs, arcs_count, &endptr);
114 if(ret != arcs_count)
Lev Walkin0fab1a62005-03-09 22:19:25 +0000115 return XPBD_SYSTEM_FAILURE; /* assert?.. */
Lev Walkin92302252004-10-23 10:16:51 +0000116 }
117
118 /*
119 * Convert arcs into BER representation.
120 */
121 ret = RELATIVE_OID_set_arcs(st, arcs, sizeof(*arcs), arcs_count);
Lev Walkin0fab1a62005-03-09 22:19:25 +0000122 if(ret) return XPBD_BROKEN_ENCODING;
Lev Walkin92302252004-10-23 10:16:51 +0000123 if(arcs != s_arcs) FREEMEM(arcs);
124
Lev Walkin0fab1a62005-03-09 22:19:25 +0000125 return XPBD_BODY_CONSUMED;
Lev Walkin92302252004-10-23 10:16:51 +0000126}
127
128asn_dec_rval_t
129RELATIVE_OID_decode_xer(asn_codec_ctx_t *opt_codec_ctx,
130 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
131 void *buf_ptr, size_t size) {
132
133 return xer_decode_primitive(opt_codec_ctx, td,
134 sptr, sizeof(RELATIVE_OID_t), opt_mname,
135 buf_ptr, size, RELATIVE_OID__xer_body_decode);
136}
137
Lev Walkina9cc46e2004-09-22 16:06:28 +0000138asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000139RELATIVE_OID_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000140 int ilevel, enum xer_encoder_flags_e flags,
141 asn_app_consume_bytes_f *cb, void *app_key) {
142 RELATIVE_OID_t *st = (RELATIVE_OID_t *)sptr;
143 asn_enc_rval_t er;
144
145 (void)ilevel; /* Unused argument */
146 (void)flags; /* Unused argument */
147
148 if(!st || !st->buf)
149 _ASN_ENCODE_FAILED;
150
151 er.encoded = RELATIVE_OID__dump_body(st, cb, app_key);
152 if(er.encoded < 0) _ASN_ENCODE_FAILED;
153
154 return er;
155}
Lev Walkinf15320b2004-06-03 03:38:44 +0000156
157int
Lev Walkin29a044b2004-06-14 07:24:36 +0000158RELATIVE_OID_get_arcs(RELATIVE_OID_t *roid,
159 void *arcs, unsigned int arc_type_size, unsigned int arc_slots) {
Lev Walkin4d9528c2004-08-11 08:10:13 +0000160 void *arcs_end = (char *)arcs + (arc_slots * arc_type_size);
Lev Walkin29a044b2004-06-14 07:24:36 +0000161 int num_arcs = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000162 int startn = 0;
163 int i;
164
165 if(!roid || !roid->buf) {
166 errno = EINVAL;
167 return -1;
168 }
169
170 for(i = 0; i < roid->size; i++) {
171 uint8_t b = roid->buf[i];
172 if((b & 0x80)) /* Continuation expected */
173 continue;
174
Lev Walkin29a044b2004-06-14 07:24:36 +0000175 if(arcs < arcs_end) {
176 if(OBJECT_IDENTIFIER_get_single_arc(
177 &roid->buf[startn],
178 i - startn + 1, 0,
179 arcs, arc_type_size))
Lev Walkinf15320b2004-06-03 03:38:44 +0000180 return -1;
Lev Walkin4ce78ca2004-08-25 01:34:11 +0000181 arcs = ((char *)arcs) + arc_type_size;
Lev Walkin29a044b2004-06-14 07:24:36 +0000182 num_arcs++;
Lev Walkinf15320b2004-06-03 03:38:44 +0000183 }
184
185 startn = i + 1;
186 }
187
Lev Walkin29a044b2004-06-14 07:24:36 +0000188 return num_arcs;
Lev Walkinf15320b2004-06-03 03:38:44 +0000189}
190
191int
Lev Walkin0787ff02004-06-17 23:43:39 +0000192RELATIVE_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 +0000193 uint8_t *buf;
194 uint8_t *bp;
Lev Walkin34b2a932004-06-17 23:46:45 +0000195 unsigned int size;
196 unsigned int i;
Lev Walkinf15320b2004-06-03 03:38:44 +0000197
Lev Walkin0787ff02004-06-17 23:43:39 +0000198 if(roid == NULL || arcs == NULL || arc_type_size < 1) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000199 errno = EINVAL;
200 return -1;
201 }
202
203 /*
204 * Roughly estimate the maximum size necessary to encode these arcs.
205 */
Lev Walkin0787ff02004-06-17 23:43:39 +0000206 size = ((arc_type_size * CHAR_BIT + 6) / 7) * arcs_slots;
Lev Walkinc2346572004-08-11 09:07:36 +0000207 bp = buf = (uint8_t *)MALLOC(size + 1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000208 if(!buf) {
209 /* ENOMEM */
210 return -1;
211 }
212
213 /*
Lev Walkin0787ff02004-06-17 23:43:39 +0000214 * Encode the arcs.
Lev Walkinf15320b2004-06-03 03:38:44 +0000215 */
Lev Walkin4ce78ca2004-08-25 01:34:11 +0000216 for(i = 0; i < arcs_slots; i++, arcs = ((char *)arcs) + arc_type_size) {
Lev Walkin0787ff02004-06-17 23:43:39 +0000217 bp += OBJECT_IDENTIFIER_set_single_arc(bp,
218 arcs, arc_type_size, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000219 }
220
Lev Walkin34b2a932004-06-17 23:46:45 +0000221 assert((unsigned)(bp - buf) <= size);
Lev Walkin0787ff02004-06-17 23:43:39 +0000222
Lev Walkinf15320b2004-06-03 03:38:44 +0000223 /*
224 * Replace buffer.
225 */
Lev Walkin4efbfb72005-02-25 14:20:30 +0000226 roid->size = (int)(bp - buf);
Lev Walkinf15320b2004-06-03 03:38:44 +0000227 bp = roid->buf;
228 roid->buf = buf;
229 if(bp) FREEMEM(bp);
230
231 return 0;
232}
Lev Walkin0787ff02004-06-17 23:43:39 +0000233