blob: 2b32315eba6b8db9c2a32aff8e04543043436832 [file] [log] [blame]
vlmfa67ddc2004-06-03 03:38:44 +00001/*-
vlm31feaf32005-03-10 11:50:24 +00002 * Copyright (c) 2003, 2004, 2005 Lev Walkin <vlm@lionet.info>.
3 * All rights reserved.
vlmfa67ddc2004-06-03 03:38:44 +00004 * Redistribution and modifications are permitted subject to BSD license.
5 */
vlm39ba4c42004-09-22 16:06:28 +00006#include <asn_internal.h>
vlmfa67ddc2004-06-03 03:38:44 +00007#include <RELATIVE-OID.h>
vlm3b6e1122004-10-21 11:22:12 +00008#include <asn_codecs_prim.h> /* Encoder and decoder of a primitive type */
vlm12557712004-06-17 23:43:39 +00009#include <limits.h> /* for CHAR_BIT */
vlmfa67ddc2004-06-03 03:38:44 +000010#include <errno.h>
11
12/*
13 * RELATIVE-OID basic type description.
14 */
vlmef6355b2004-09-29 13:26:15 +000015static ber_tlv_tag_t asn_DEF_RELATIVE_OID_tags[] = {
vlmfa67ddc2004-06-03 03:38:44 +000016 (ASN_TAG_CLASS_UNIVERSAL | (13 << 2))
17};
vlmef6355b2004-09-29 13:26:15 +000018asn_TYPE_descriptor_t asn_DEF_RELATIVE_OID = {
vlmfa67ddc2004-06-03 03:38:44 +000019 "RELATIVE-OID",
vlm9de248e2004-10-20 15:50:55 +000020 "RELATIVE_OID",
vlm6678cb12004-09-26 13:10:40 +000021 ASN__PRIMITIVE_TYPE_free,
vlm39ba4c42004-09-22 16:06:28 +000022 RELATIVE_OID_print,
vlmfa67ddc2004-06-03 03:38:44 +000023 asn_generic_no_constraint,
vlm6678cb12004-09-26 13:10:40 +000024 ber_decode_primitive,
25 der_encode_primitive,
vlm1f73df22004-10-23 10:16:51 +000026 RELATIVE_OID_decode_xer,
vlm39ba4c42004-09-22 16:06:28 +000027 RELATIVE_OID_encode_xer,
vlmfa67ddc2004-06-03 03:38:44 +000028 0, /* Use generic outmost tag fetcher */
vlmef6355b2004-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]),
vlme413c122004-08-20 13:23:42 +000035 0, 0, /* No members */
vlmb42843a2004-06-05 08:17:50 +000036 0 /* No specifics */
vlmfa67ddc2004-06-03 03:38:44 +000037};
38
vlm39ba4c42004-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
vlmfa67ddc2004-06-03 03:38:44 +000068int
vlmef6355b2004-09-29 13:26:15 +000069RELATIVE_OID_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
vlmfa67ddc2004-06-03 03:38:44 +000070 asn_app_consume_bytes_f *cb, void *app_key) {
vlmda674682004-08-11 09:07:36 +000071 const RELATIVE_OID_t *st = (const RELATIVE_OID_t *)sptr;
vlmfa67ddc2004-06-03 03:38:44 +000072
vlmb42843a2004-06-05 08:17:50 +000073 (void)td; /* Unused argument */
74 (void)ilevel; /* Unused argument */
75
vlmfa67ddc2004-06-03 03:38:44 +000076 if(!st || !st->buf)
vlm6678cb12004-09-26 13:10:40 +000077 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
vlmfa67ddc2004-06-03 03:38:44 +000078
79 /* Dump preamble */
vlm6678cb12004-09-26 13:10:40 +000080 if(cb("{ ", 2, app_key) < 0)
vlmfa67ddc2004-06-03 03:38:44 +000081 return -1;
82
vlm39ba4c42004-09-22 16:06:28 +000083 if(RELATIVE_OID__dump_body(st, cb, app_key) < 0)
84 return -1;
vlmfa67ddc2004-06-03 03:38:44 +000085
vlm6678cb12004-09-26 13:10:40 +000086 return (cb(" }", 2, app_key) < 0) ? -1 : 0;
vlmfa67ddc2004-06-03 03:38:44 +000087}
88
vlm4df9cc12005-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) {
vlm1f73df22004-10-23 10:16:51 +000091 RELATIVE_OID_t *st = (RELATIVE_OID_t *)sptr;
vlm31feaf32005-03-10 11:50:24 +000092 const char *chunk_end = (const char *)chunk_buf + chunk_size;
93 const char *endptr;
vlm1f73df22004-10-23 10:16:51 +000094 long s_arcs[6];
95 long *arcs = s_arcs;
96 int arcs_count;
97 int ret;
98
vlm80a48592005-02-25 12:10:27 +000099 (void)td;
100
vlm1f73df22004-10-23 10:16:51 +0000101 arcs_count = OBJECT_IDENTIFIER_parse_arcs(
102 (const char *)chunk_buf, chunk_size,
vlm4df9cc12005-03-09 22:19:25 +0000103 arcs, sizeof(s_arcs)/sizeof(s_arcs[0]), &endptr);
104 if(arcs_count < 0) {
105 /* Expecting at least zero arcs */
106 return XPBD_BROKEN_ENCODING;
107 }
vlm31feaf32005-03-10 11:50:24 +0000108 if(endptr < chunk_end) {
109 /* We have a tail of unrecognized data. Check its safety. */
110 if(!xer_is_whitespace(endptr, chunk_end - endptr))
111 return XPBD_BROKEN_ENCODING;
112 }
vlm4df9cc12005-03-09 22:19:25 +0000113
114 if((size_t)arcs_count > sizeof(s_arcs)/sizeof(s_arcs[0])) {
vlm1f73df22004-10-23 10:16:51 +0000115 arcs = (long *)MALLOC(arcs_count * sizeof(long));
vlm4df9cc12005-03-09 22:19:25 +0000116 if(!arcs) return XPBD_SYSTEM_FAILURE;
vlm1f73df22004-10-23 10:16:51 +0000117 ret = OBJECT_IDENTIFIER_parse_arcs(
118 (const char *)chunk_buf, chunk_size,
119 arcs, arcs_count, &endptr);
120 if(ret != arcs_count)
vlm4df9cc12005-03-09 22:19:25 +0000121 return XPBD_SYSTEM_FAILURE; /* assert?.. */
vlm1f73df22004-10-23 10:16:51 +0000122 }
123
124 /*
125 * Convert arcs into BER representation.
126 */
127 ret = RELATIVE_OID_set_arcs(st, arcs, sizeof(*arcs), arcs_count);
vlm1f73df22004-10-23 10:16:51 +0000128 if(arcs != s_arcs) FREEMEM(arcs);
129
vlm31feaf32005-03-10 11:50:24 +0000130 return ret ? XPBD_SYSTEM_FAILURE : XPBD_BODY_CONSUMED;
vlm1f73df22004-10-23 10:16:51 +0000131}
132
133asn_dec_rval_t
134RELATIVE_OID_decode_xer(asn_codec_ctx_t *opt_codec_ctx,
135 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
vlmb02dcc62005-03-10 18:52:02 +0000136 const void *buf_ptr, size_t size) {
vlm1f73df22004-10-23 10:16:51 +0000137
138 return xer_decode_primitive(opt_codec_ctx, td,
139 sptr, sizeof(RELATIVE_OID_t), opt_mname,
140 buf_ptr, size, RELATIVE_OID__xer_body_decode);
141}
142
vlm39ba4c42004-09-22 16:06:28 +0000143asn_enc_rval_t
vlmef6355b2004-09-29 13:26:15 +0000144RELATIVE_OID_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
vlm39ba4c42004-09-22 16:06:28 +0000145 int ilevel, enum xer_encoder_flags_e flags,
146 asn_app_consume_bytes_f *cb, void *app_key) {
147 RELATIVE_OID_t *st = (RELATIVE_OID_t *)sptr;
148 asn_enc_rval_t er;
149
150 (void)ilevel; /* Unused argument */
151 (void)flags; /* Unused argument */
152
153 if(!st || !st->buf)
154 _ASN_ENCODE_FAILED;
155
156 er.encoded = RELATIVE_OID__dump_body(st, cb, app_key);
157 if(er.encoded < 0) _ASN_ENCODE_FAILED;
158
vlmc4707442005-11-13 09:19:11 +0000159 er.structure_ptr = 0;
160 er.failed_type = 0;
vlm39ba4c42004-09-22 16:06:28 +0000161 return er;
162}
vlmfa67ddc2004-06-03 03:38:44 +0000163
164int
vlm2e3dd3b2004-06-14 07:24:36 +0000165RELATIVE_OID_get_arcs(RELATIVE_OID_t *roid,
166 void *arcs, unsigned int arc_type_size, unsigned int arc_slots) {
vlm1ff928d2004-08-11 08:10:13 +0000167 void *arcs_end = (char *)arcs + (arc_slots * arc_type_size);
vlm2e3dd3b2004-06-14 07:24:36 +0000168 int num_arcs = 0;
vlmfa67ddc2004-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
vlm2e3dd3b2004-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))
vlmfa67ddc2004-06-03 03:38:44 +0000187 return -1;
vlmd86c9252004-08-25 01:34:11 +0000188 arcs = ((char *)arcs) + arc_type_size;
vlm2e3dd3b2004-06-14 07:24:36 +0000189 num_arcs++;
vlmfa67ddc2004-06-03 03:38:44 +0000190 }
191
192 startn = i + 1;
193 }
194
vlm2e3dd3b2004-06-14 07:24:36 +0000195 return num_arcs;
vlmfa67ddc2004-06-03 03:38:44 +0000196}
197
198int
vlm12557712004-06-17 23:43:39 +0000199RELATIVE_OID_set_arcs(RELATIVE_OID_t *roid, void *arcs, unsigned int arc_type_size, unsigned int arcs_slots) {
vlmfa67ddc2004-06-03 03:38:44 +0000200 uint8_t *buf;
201 uint8_t *bp;
vlma63e0292004-06-17 23:46:45 +0000202 unsigned int size;
203 unsigned int i;
vlmfa67ddc2004-06-03 03:38:44 +0000204
vlm12557712004-06-17 23:43:39 +0000205 if(roid == NULL || arcs == NULL || arc_type_size < 1) {
vlmfa67ddc2004-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 */
vlm12557712004-06-17 23:43:39 +0000213 size = ((arc_type_size * CHAR_BIT + 6) / 7) * arcs_slots;
vlmda674682004-08-11 09:07:36 +0000214 bp = buf = (uint8_t *)MALLOC(size + 1);
vlmfa67ddc2004-06-03 03:38:44 +0000215 if(!buf) {
216 /* ENOMEM */
217 return -1;
218 }
219
220 /*
vlm12557712004-06-17 23:43:39 +0000221 * Encode the arcs.
vlmfa67ddc2004-06-03 03:38:44 +0000222 */
vlmd86c9252004-08-25 01:34:11 +0000223 for(i = 0; i < arcs_slots; i++, arcs = ((char *)arcs) + arc_type_size) {
vlm12557712004-06-17 23:43:39 +0000224 bp += OBJECT_IDENTIFIER_set_single_arc(bp,
225 arcs, arc_type_size, 0);
vlmfa67ddc2004-06-03 03:38:44 +0000226 }
227
vlma63e0292004-06-17 23:46:45 +0000228 assert((unsigned)(bp - buf) <= size);
vlm12557712004-06-17 23:43:39 +0000229
vlmfa67ddc2004-06-03 03:38:44 +0000230 /*
231 * Replace buffer.
232 */
vlm8a09e0f2005-02-25 14:20:30 +0000233 roid->size = (int)(bp - buf);
vlmfa67ddc2004-06-03 03:38:44 +0000234 bp = roid->buf;
235 roid->buf = buf;
236 if(bp) FREEMEM(bp);
237
238 return 0;
239}
vlm12557712004-06-17 23:43:39 +0000240