blob: 97b69f60f14db21882c3870ccfd2f6c8b49b0b68 [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 Walkin3256d6f2004-10-21 11:22:12 +00008#include <asn_codecs_prim.h> /* Encoder and decoder of a primitive type */
Lev Walkin0787ff02004-06-17 23:43:39 +00009#include <limits.h> /* for CHAR_BIT */
Lev Walkinf15320b2004-06-03 03:38:44 +000010#include <assert.h>
11#include <errno.h>
12
13/*
14 * RELATIVE-OID basic type description.
15 */
Lev Walkin5e033762004-09-29 13:26:15 +000016static 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 Walkinf15320b2004-06-03 03:38:44 +000029 0, /* Use generic outmost tag fetcher */
Lev Walkin5e033762004-09-29 13:26:15 +000030 asn_DEF_RELATIVE_OID_tags,
31 sizeof(asn_DEF_RELATIVE_OID_tags)
32 / sizeof(asn_DEF_RELATIVE_OID_tags[0]),
33 asn_DEF_RELATIVE_OID_tags, /* Same as above */
34 sizeof(asn_DEF_RELATIVE_OID_tags)
35 / sizeof(asn_DEF_RELATIVE_OID_tags[0]),
Lev Walkin449f8322004-08-20 13:23:42 +000036 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +000037 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +000038};
39
Lev Walkina9cc46e2004-09-22 16:06:28 +000040static ssize_t
41RELATIVE_OID__dump_body(const RELATIVE_OID_t *st, asn_app_consume_bytes_f *cb, void *app_key) {
42 ssize_t wrote = 0;
43 ssize_t ret;
44 int startn;
45 int i;
46
47 for(i = 0, startn = 0; i < st->size; i++) {
48 uint8_t b = st->buf[i];
49 if((b & 0x80)) /* Continuation expected */
50 continue;
51 if(startn) {
52 /* Separate arcs */
53 if(cb(".", 1, app_key) < 0)
54 return -1;
55 wrote++;
56 }
57
58 ret = OBJECT_IDENTIFIER__dump_arc(&st->buf[startn],
59 i - startn + 1, 0, cb, app_key);
60 if(ret < 0) return -1;
61 wrote += ret;
62
63 startn = i + 1;
64 }
65
66 return wrote;
67}
68
Lev Walkinf15320b2004-06-03 03:38:44 +000069int
Lev Walkin5e033762004-09-29 13:26:15 +000070RELATIVE_OID_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +000071 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +000072 const RELATIVE_OID_t *st = (const RELATIVE_OID_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +000073
Lev Walkind9bd7752004-06-05 08:17:50 +000074 (void)td; /* Unused argument */
75 (void)ilevel; /* Unused argument */
76
Lev Walkinf15320b2004-06-03 03:38:44 +000077 if(!st || !st->buf)
Lev Walkin8e8078a2004-09-26 13:10:40 +000078 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +000079
80 /* Dump preamble */
Lev Walkin8e8078a2004-09-26 13:10:40 +000081 if(cb("{ ", 2, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +000082 return -1;
83
Lev Walkina9cc46e2004-09-22 16:06:28 +000084 if(RELATIVE_OID__dump_body(st, cb, app_key) < 0)
85 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +000086
Lev Walkin8e8078a2004-09-26 13:10:40 +000087 return (cb(" }", 2, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +000088}
89
Lev Walkin0fab1a62005-03-09 22:19:25 +000090static enum xer_pbd_rval
91RELATIVE_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 +000092 RELATIVE_OID_t *st = (RELATIVE_OID_t *)sptr;
Lev Walkin2c34aad2005-03-10 11:50:24 +000093 const char *chunk_end = (const char *)chunk_buf + chunk_size;
94 const char *endptr;
Lev Walkin92302252004-10-23 10:16:51 +000095 long s_arcs[6];
96 long *arcs = s_arcs;
97 int arcs_count;
98 int ret;
99
Lev Walkine0b56e02005-02-25 12:10:27 +0000100 (void)td;
101
Lev Walkin92302252004-10-23 10:16:51 +0000102 arcs_count = OBJECT_IDENTIFIER_parse_arcs(
103 (const char *)chunk_buf, chunk_size,
Lev Walkin0fab1a62005-03-09 22:19:25 +0000104 arcs, sizeof(s_arcs)/sizeof(s_arcs[0]), &endptr);
105 if(arcs_count < 0) {
106 /* Expecting at least zero arcs */
107 return XPBD_BROKEN_ENCODING;
108 }
Lev Walkin2c34aad2005-03-10 11:50:24 +0000109 if(endptr < chunk_end) {
110 /* We have a tail of unrecognized data. Check its safety. */
111 if(!xer_is_whitespace(endptr, chunk_end - endptr))
112 return XPBD_BROKEN_ENCODING;
113 }
Lev Walkin0fab1a62005-03-09 22:19:25 +0000114
115 if((size_t)arcs_count > sizeof(s_arcs)/sizeof(s_arcs[0])) {
Lev Walkin92302252004-10-23 10:16:51 +0000116 arcs = (long *)MALLOC(arcs_count * sizeof(long));
Lev Walkin0fab1a62005-03-09 22:19:25 +0000117 if(!arcs) return XPBD_SYSTEM_FAILURE;
Lev Walkin92302252004-10-23 10:16:51 +0000118 ret = OBJECT_IDENTIFIER_parse_arcs(
119 (const char *)chunk_buf, chunk_size,
120 arcs, arcs_count, &endptr);
121 if(ret != arcs_count)
Lev Walkin0fab1a62005-03-09 22:19:25 +0000122 return XPBD_SYSTEM_FAILURE; /* assert?.. */
Lev Walkin92302252004-10-23 10:16:51 +0000123 }
124
125 /*
126 * Convert arcs into BER representation.
127 */
128 ret = RELATIVE_OID_set_arcs(st, arcs, sizeof(*arcs), arcs_count);
Lev Walkin92302252004-10-23 10:16:51 +0000129 if(arcs != s_arcs) FREEMEM(arcs);
130
Lev Walkin2c34aad2005-03-10 11:50:24 +0000131 return ret ? XPBD_SYSTEM_FAILURE : XPBD_BODY_CONSUMED;
Lev Walkin92302252004-10-23 10:16:51 +0000132}
133
134asn_dec_rval_t
135RELATIVE_OID_decode_xer(asn_codec_ctx_t *opt_codec_ctx,
136 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
137 void *buf_ptr, size_t size) {
138
139 return xer_decode_primitive(opt_codec_ctx, td,
140 sptr, sizeof(RELATIVE_OID_t), opt_mname,
141 buf_ptr, size, RELATIVE_OID__xer_body_decode);
142}
143
Lev Walkina9cc46e2004-09-22 16:06:28 +0000144asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000145RELATIVE_OID_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000146 int ilevel, enum xer_encoder_flags_e flags,
147 asn_app_consume_bytes_f *cb, void *app_key) {
148 RELATIVE_OID_t *st = (RELATIVE_OID_t *)sptr;
149 asn_enc_rval_t er;
150
151 (void)ilevel; /* Unused argument */
152 (void)flags; /* Unused argument */
153
154 if(!st || !st->buf)
155 _ASN_ENCODE_FAILED;
156
157 er.encoded = RELATIVE_OID__dump_body(st, cb, app_key);
158 if(er.encoded < 0) _ASN_ENCODE_FAILED;
159
160 return er;
161}
Lev Walkinf15320b2004-06-03 03:38:44 +0000162
163int
Lev Walkin29a044b2004-06-14 07:24:36 +0000164RELATIVE_OID_get_arcs(RELATIVE_OID_t *roid,
165 void *arcs, unsigned int arc_type_size, unsigned int arc_slots) {
Lev Walkin4d9528c2004-08-11 08:10:13 +0000166 void *arcs_end = (char *)arcs + (arc_slots * arc_type_size);
Lev Walkin29a044b2004-06-14 07:24:36 +0000167 int num_arcs = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000168 int startn = 0;
169 int i;
170
171 if(!roid || !roid->buf) {
172 errno = EINVAL;
173 return -1;
174 }
175
176 for(i = 0; i < roid->size; i++) {
177 uint8_t b = roid->buf[i];
178 if((b & 0x80)) /* Continuation expected */
179 continue;
180
Lev Walkin29a044b2004-06-14 07:24:36 +0000181 if(arcs < arcs_end) {
182 if(OBJECT_IDENTIFIER_get_single_arc(
183 &roid->buf[startn],
184 i - startn + 1, 0,
185 arcs, arc_type_size))
Lev Walkinf15320b2004-06-03 03:38:44 +0000186 return -1;
Lev Walkin4ce78ca2004-08-25 01:34:11 +0000187 arcs = ((char *)arcs) + arc_type_size;
Lev Walkin29a044b2004-06-14 07:24:36 +0000188 num_arcs++;
Lev Walkinf15320b2004-06-03 03:38:44 +0000189 }
190
191 startn = i + 1;
192 }
193
Lev Walkin29a044b2004-06-14 07:24:36 +0000194 return num_arcs;
Lev Walkinf15320b2004-06-03 03:38:44 +0000195}
196
197int
Lev Walkin0787ff02004-06-17 23:43:39 +0000198RELATIVE_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 +0000199 uint8_t *buf;
200 uint8_t *bp;
Lev Walkin34b2a932004-06-17 23:46:45 +0000201 unsigned int size;
202 unsigned int i;
Lev Walkinf15320b2004-06-03 03:38:44 +0000203
Lev Walkin0787ff02004-06-17 23:43:39 +0000204 if(roid == NULL || arcs == NULL || arc_type_size < 1) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000205 errno = EINVAL;
206 return -1;
207 }
208
209 /*
210 * Roughly estimate the maximum size necessary to encode these arcs.
211 */
Lev Walkin0787ff02004-06-17 23:43:39 +0000212 size = ((arc_type_size * CHAR_BIT + 6) / 7) * arcs_slots;
Lev Walkinc2346572004-08-11 09:07:36 +0000213 bp = buf = (uint8_t *)MALLOC(size + 1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000214 if(!buf) {
215 /* ENOMEM */
216 return -1;
217 }
218
219 /*
Lev Walkin0787ff02004-06-17 23:43:39 +0000220 * Encode the arcs.
Lev Walkinf15320b2004-06-03 03:38:44 +0000221 */
Lev Walkin4ce78ca2004-08-25 01:34:11 +0000222 for(i = 0; i < arcs_slots; i++, arcs = ((char *)arcs) + arc_type_size) {
Lev Walkin0787ff02004-06-17 23:43:39 +0000223 bp += OBJECT_IDENTIFIER_set_single_arc(bp,
224 arcs, arc_type_size, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000225 }
226
Lev Walkin34b2a932004-06-17 23:46:45 +0000227 assert((unsigned)(bp - buf) <= size);
Lev Walkin0787ff02004-06-17 23:43:39 +0000228
Lev Walkinf15320b2004-06-03 03:38:44 +0000229 /*
230 * Replace buffer.
231 */
Lev Walkin4efbfb72005-02-25 14:20:30 +0000232 roid->size = (int)(bp - buf);
Lev Walkinf15320b2004-06-03 03:38:44 +0000233 bp = roid->buf;
234 roid->buf = buf;
235 if(bp) FREEMEM(bp);
236
237 return 0;
238}
Lev Walkin0787ff02004-06-17 23:43:39 +0000239