blob: 66f3a4ba9d0c8c2cf93bdc47075488524ea14458 [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001/*-
Lev Walkin188ed2c2004-09-13 08:31:01 +00002 * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
Lev Walkinf15320b2004-06-03 03:38:44 +00003 * 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 <OCTET_STRING.h>
Lev Walkinbbd93252004-10-12 05:57:23 +00007#include <BIT_STRING.h> /* for .bits_unused member */
Lev Walkinf15320b2004-06-03 03:38:44 +00008#include <assert.h>
9#include <errno.h>
10
11/*
12 * OCTET STRING basic type description.
13 */
Lev Walkinde4825d2004-09-29 13:20:14 +000014static ber_tlv_tag_t asn_DEF_OCTET_STRING_tags[] = {
Lev Walkinf15320b2004-06-03 03:38:44 +000015 (ASN_TAG_CLASS_UNIVERSAL | (4 << 2))
16};
Lev Walkinbbd93252004-10-12 05:57:23 +000017static asn_OCTET_STRING_specifics_t asn_DEF_OCTET_STRING_specs = {
18 sizeof(OCTET_STRING_t),
19 offsetof(OCTET_STRING_t, _asn_ctx),
20 0
21};
Lev Walkinde4825d2004-09-29 13:20:14 +000022asn_TYPE_descriptor_t asn_DEF_OCTET_STRING = {
Lev Walkinf15320b2004-06-03 03:38:44 +000023 "OCTET STRING",
Lev Walkina9cc46e2004-09-22 16:06:28 +000024 OCTET_STRING_free,
25 OCTET_STRING_print, /* non-ascii stuff, generally */
Lev Walkinf15320b2004-06-03 03:38:44 +000026 asn_generic_no_constraint,
27 OCTET_STRING_decode_ber,
28 OCTET_STRING_encode_der,
Lev Walkina9cc46e2004-09-22 16:06:28 +000029 0, /* Not implemented yet */
30 OCTET_STRING_encode_xer,
Lev Walkinf15320b2004-06-03 03:38:44 +000031 0, /* Use generic outmost tag fetcher */
Lev Walkinde4825d2004-09-29 13:20:14 +000032 asn_DEF_OCTET_STRING_tags,
33 sizeof(asn_DEF_OCTET_STRING_tags)
34 / sizeof(asn_DEF_OCTET_STRING_tags[0]),
35 asn_DEF_OCTET_STRING_tags, /* Same as above */
36 sizeof(asn_DEF_OCTET_STRING_tags)
37 / sizeof(asn_DEF_OCTET_STRING_tags[0]),
Lev Walkin449f8322004-08-20 13:23:42 +000038 0, 0, /* No members */
Lev Walkinbbd93252004-10-12 05:57:23 +000039 &asn_DEF_OCTET_STRING_specs
Lev Walkinf15320b2004-06-03 03:38:44 +000040};
41
Lev Walkincc6a9102004-09-23 22:06:26 +000042#undef _CH_PHASE
43#undef NEXT_PHASE
44#undef PREV_PHASE
Lev Walkinf15320b2004-06-03 03:38:44 +000045#define _CH_PHASE(ctx, inc) do { \
46 if(ctx->phase == 0) \
47 ctx->step = 0; \
48 ctx->phase += inc; \
49 } while(0)
50#define NEXT_PHASE(ctx) _CH_PHASE(ctx, +1)
51#define PREV_PHASE(ctx) _CH_PHASE(ctx, -1)
52
Lev Walkincc6a9102004-09-23 22:06:26 +000053#undef ADVANCE
Lev Walkin4ce78ca2004-08-25 01:34:11 +000054#define ADVANCE(num_bytes) do { \
Lev Walkin3990ba62004-09-24 20:57:41 +000055 size_t num = (num_bytes); \
Lev Walkin4ce78ca2004-08-25 01:34:11 +000056 buf_ptr = ((char *)buf_ptr) + num; \
57 size -= num; \
58 consumed_myself += num; \
Lev Walkinf15320b2004-06-03 03:38:44 +000059 } while(0)
60
Lev Walkincc6a9102004-09-23 22:06:26 +000061#undef RETURN
Lev Walkinf15320b2004-06-03 03:38:44 +000062#define RETURN(_code) do { \
63 rval.code = _code; \
64 rval.consumed = consumed_myself;\
65 return rval; \
66 } while(0)
67
Lev Walkincc6a9102004-09-23 22:06:26 +000068#undef APPEND
Lev Walkind9bd7752004-06-05 08:17:50 +000069#define APPEND(bufptr, bufsize) do { \
Lev Walkin8d127872004-09-04 04:44:50 +000070 size_t _bs = (bufsize); \
Lev Walkind9bd7752004-06-05 08:17:50 +000071 size_t _ns = ctx->step; /* Allocated */ \
Lev Walkin8d127872004-09-04 04:44:50 +000072 if(_ns <= (size_t)(st->size + _bs)) { \
Lev Walkind9bd7752004-06-05 08:17:50 +000073 void *ptr; \
Lev Walkin188ed2c2004-09-13 08:31:01 +000074 /* Be nice and round to the memory allocator */ \
Lev Walkind9bd7752004-06-05 08:17:50 +000075 do { _ns = _ns ? _ns<<2 : 16; } \
Lev Walkin8d127872004-09-04 04:44:50 +000076 while(_ns <= (size_t)(st->size + _bs)); \
Lev Walkind9bd7752004-06-05 08:17:50 +000077 ptr = REALLOC(st->buf, _ns); \
78 if(ptr) { \
Lev Walkinc2346572004-08-11 09:07:36 +000079 st->buf = (uint8_t *)ptr; \
Lev Walkind9bd7752004-06-05 08:17:50 +000080 ctx->step = _ns; \
81 } else { \
82 RETURN(RC_FAIL); \
83 } \
84 } \
Lev Walkin8d127872004-09-04 04:44:50 +000085 memcpy(st->buf + st->size, bufptr, _bs); \
86 st->size += _bs; \
Lev Walkind9bd7752004-06-05 08:17:50 +000087 if(st->size < 0) \
88 /* Why even care?.. JIC */ \
89 RETURN(RC_FAIL); \
90 /* Convenient nul-termination */ \
91 st->buf[st->size] = '\0'; \
Lev Walkinf15320b2004-06-03 03:38:44 +000092 } while(0)
93
94/*
Lev Walkin07f388c2004-10-11 11:43:08 +000095 * Internal variant of the OCTET STRING.
96 */
97typedef enum OS_type {
98 _TT_GENERIC = 0, /* Just a random OCTET STRING */
99 _TT_BIT_STRING = 1, /* BIT STRING type, a special case */
100 _TT_ANY = 2, /* ANY type, a special case too */
101} OS_type_e;
102
103/*
Lev Walkinf15320b2004-06-03 03:38:44 +0000104 * The main reason why ASN.1 is still alive is that too much time and effort
105 * is necessary for learning it more or less adequately, thus creating a gut
106 * necessity to demonstrate that aquired skill everywhere afterwards.
107 * No, I am not going to explain what the following stuff is.
108 */
109struct _stack_el {
Lev Walkin3990ba62004-09-24 20:57:41 +0000110 ber_tlv_len_t left; /* What's left to read (or -1) */
Lev Walkin5c915992004-09-27 20:54:06 +0000111 ber_tlv_len_t got; /* What was actually processed */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000112 int cont_level; /* Depth of subcontainment */
Lev Walkinf15320b2004-06-03 03:38:44 +0000113 int want_nulls; /* Want null "end of content" octets? */
114 int bits_chopped; /* Flag in BIT STRING mode */
Lev Walkin5c915992004-09-27 20:54:06 +0000115 ber_tlv_tag_t tag; /* For debugging purposes */
Lev Walkinf15320b2004-06-03 03:38:44 +0000116 struct _stack_el *prev;
117 struct _stack_el *next;
118};
119struct _stack {
120 struct _stack_el *tail;
121 struct _stack_el *cur_ptr;
122};
123
124static struct _stack_el *
Lev Walkin3990ba62004-09-24 20:57:41 +0000125OS__add_stack_el(struct _stack *st) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000126 struct _stack_el *nel;
127
Lev Walkin188ed2c2004-09-13 08:31:01 +0000128 /*
129 * Reuse the old stack frame or allocate a new one.
130 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000131 if(st->cur_ptr && st->cur_ptr->next) {
132 nel = st->cur_ptr->next;
Lev Walkinf15320b2004-06-03 03:38:44 +0000133 nel->bits_chopped = 0;
Lev Walkin5c915992004-09-27 20:54:06 +0000134 nel->got = 0;
135 /* Retain the nel->cont_level, it's correct. */
Lev Walkinf15320b2004-06-03 03:38:44 +0000136 } else {
Lev Walkinc2346572004-08-11 09:07:36 +0000137 (void *)nel = CALLOC(1, sizeof(struct _stack_el));
Lev Walkinf15320b2004-06-03 03:38:44 +0000138 if(nel == NULL)
139 return NULL;
140
141 if(st->tail) {
Lev Walkin188ed2c2004-09-13 08:31:01 +0000142 /* Increase a subcontainment depth */
143 nel->cont_level = st->tail->cont_level + 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000144 st->tail->next = nel;
145 }
146 nel->prev = st->tail;
147 st->tail = nel;
148 }
149
150 st->cur_ptr = nel;
151
152 return nel;
153}
154
155static struct _stack *
156_new_stack() {
Lev Walkinbbd93252004-10-12 05:57:23 +0000157 return (struct _stack *)CALLOC(1, sizeof(struct _stack));
Lev Walkinf15320b2004-06-03 03:38:44 +0000158}
159
160/*
161 * Decode OCTET STRING type.
162 */
163ber_dec_rval_t
Lev Walkinde4825d2004-09-29 13:20:14 +0000164OCTET_STRING_decode_ber(asn_codec_ctx_t *opt_codec_ctx,
165 asn_TYPE_descriptor_t *td,
Lev Walkinf15320b2004-06-03 03:38:44 +0000166 void **os_structure, void *buf_ptr, size_t size, int tag_mode) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000167 asn_OCTET_STRING_specifics_t *specs = td->specifics
168 ? td->specifics : &asn_DEF_OCTET_STRING_specs;
169 BIT_STRING_t *st = (BIT_STRING_t *)*os_structure;
Lev Walkinf15320b2004-06-03 03:38:44 +0000170 ber_dec_rval_t rval;
Lev Walkinde4825d2004-09-29 13:20:14 +0000171 asn_struct_ctx_t *ctx;
Lev Walkinf15320b2004-06-03 03:38:44 +0000172 ssize_t consumed_myself = 0;
Lev Walkinbbd93252004-10-12 05:57:23 +0000173 struct _stack *stck; /* Expectations stack structure */
Lev Walkin5c915992004-09-27 20:54:06 +0000174 struct _stack_el *sel = 0; /* Stack element */
Lev Walkinf15320b2004-06-03 03:38:44 +0000175 int tlv_constr;
Lev Walkinbbd93252004-10-12 05:57:23 +0000176 OS_type_e type_variant = (OS_type_e)specs->subvariant;
Lev Walkinf15320b2004-06-03 03:38:44 +0000177
Lev Walkincc6a9102004-09-23 22:06:26 +0000178 ASN_DEBUG("Decoding %s as %s (frame %ld)",
179 td->name,
Lev Walkinbbd93252004-10-12 05:57:23 +0000180 (type_variant == _TT_GENERIC) ?
181 "OCTET STRING" : "OS-SpecialCase",
Lev Walkincc6a9102004-09-23 22:06:26 +0000182 (long)size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000183
184 /*
185 * Create the string if does not exist.
186 */
187 if(st == NULL) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000188 (void *)st = *os_structure = CALLOC(1, specs->struct_size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000189 if(st == NULL)
190 RETURN(RC_FAIL);
191 }
192
193 /* Restore parsing context */
Lev Walkinbbd93252004-10-12 05:57:23 +0000194 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000195
196 switch(ctx->phase) {
197 case 0:
198 /*
199 * Check tags.
200 */
Lev Walkinde4825d2004-09-29 13:20:14 +0000201 rval = ber_check_tags(opt_codec_ctx, td, ctx,
Lev Walkin8e8078a2004-09-26 13:10:40 +0000202 buf_ptr, size, tag_mode, -1,
Lev Walkinf15320b2004-06-03 03:38:44 +0000203 &ctx->left, &tlv_constr);
Lev Walkin443d2512004-10-05 06:35:31 +0000204 if(rval.code != RC_OK)
205 return rval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000206
Lev Walkinf15320b2004-06-03 03:38:44 +0000207 if(tlv_constr) {
208 /*
209 * Complex operation, requires stack of expectations.
210 */
211 ctx->ptr = _new_stack();
212 if(ctx->ptr) {
Lev Walkinc2346572004-08-11 09:07:36 +0000213 (void *)stck = ctx->ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000214 } else {
215 RETURN(RC_FAIL);
216 }
217 } else {
218 /*
219 * Jump into stackless primitive decoding.
220 */
221 _CH_PHASE(ctx, 3);
Lev Walkinbbd93252004-10-12 05:57:23 +0000222 if(type_variant == _TT_ANY && tag_mode != 1)
Lev Walkin188ed2c2004-09-13 08:31:01 +0000223 APPEND(buf_ptr, rval.consumed);
Lev Walkinf15320b2004-06-03 03:38:44 +0000224 ADVANCE(rval.consumed);
225 goto phase3;
226 }
227
Lev Walkinf15320b2004-06-03 03:38:44 +0000228 NEXT_PHASE(ctx);
229 /* Fall through */
230 case 1:
231 phase1:
232 /*
233 * Fill the stack with expectations.
234 */
Lev Walkinc2346572004-08-11 09:07:36 +0000235 (void *)stck = ctx->ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000236 sel = stck->cur_ptr;
237 do {
238 ber_tlv_tag_t tlv_tag;
239 ber_tlv_len_t tlv_len;
Lev Walkin188ed2c2004-09-13 08:31:01 +0000240 ber_tlv_tag_t expected_tag;
Lev Walkin5c915992004-09-27 20:54:06 +0000241 ssize_t tl, ll, tlvl;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000242 /* This one works even if (sel->left == -1) */
243 ssize_t Left = ((!sel||(size_t)sel->left >= size)
Lev Walkin5c915992004-09-27 20:54:06 +0000244 ?(ssize_t)size:sel->left);
Lev Walkinf15320b2004-06-03 03:38:44 +0000245
Lev Walkin3990ba62004-09-24 20:57:41 +0000246
Lev Walkin5c915992004-09-27 20:54:06 +0000247 ASN_DEBUG("%p, s->l=%d, s->wn=%d, s->g=%d\n", sel,
248 sel?sel->left:0,
249 sel?sel->want_nulls:0,
250 sel?sel->got:0
251 );
252 if(sel && sel->left <= 0 && sel->want_nulls == 0) {
253 if(sel->prev) {
254 struct _stack_el *prev = sel->prev;
255 if(prev->left != -1) {
256 if(prev->left < sel->got)
257 RETURN(RC_FAIL);
258 prev->left -= sel->got;
259 }
260 prev->got += sel->got;
261 sel = stck->cur_ptr = prev;
262 if(!sel) break;
263 tlv_constr = 1;
264 continue;
265 } else {
266 sel = stck->cur_ptr = 0;
267 break; /* Nothing to wait */
268 }
269 }
270
Lev Walkin3990ba62004-09-24 20:57:41 +0000271 tl = ber_fetch_tag(buf_ptr, Left, &tlv_tag);
Lev Walkin5c915992004-09-27 20:54:06 +0000272 ASN_DEBUG("fetch tag(size=%d,L=%d), %sstack, left=%d, wn=%d, tl=%d",
273 (int)size, Left, sel?"":"!",
274 sel?sel->left:0, sel?sel->want_nulls:0, tl);
Lev Walkinf15320b2004-06-03 03:38:44 +0000275 switch(tl) {
276 case -1: RETURN(RC_FAIL);
277 case 0: RETURN(RC_WMORE);
278 }
279
280 tlv_constr = BER_TLV_CONSTRUCTED(buf_ptr);
281
282 ll = ber_fetch_length(tlv_constr,
Lev Walkin3990ba62004-09-24 20:57:41 +0000283 (char *)buf_ptr + tl, Left - tl, &tlv_len);
Lev Walkin5c915992004-09-27 20:54:06 +0000284 ASN_DEBUG("Got tag=%s, tc=%d, left=%ld, tl=%d, len=%d, ll=%d",
Lev Walkin8d127872004-09-04 04:44:50 +0000285 ber_tlv_tag_string(tlv_tag), tlv_constr,
Lev Walkin5c915992004-09-27 20:54:06 +0000286 (long)Left, tl, tlv_len, ll);
Lev Walkinf15320b2004-06-03 03:38:44 +0000287 switch(ll) {
288 case -1: RETURN(RC_FAIL);
289 case 0: RETURN(RC_WMORE);
290 }
291
Lev Walkin8d127872004-09-04 04:44:50 +0000292 if(sel && sel->want_nulls
Lev Walkinf15320b2004-06-03 03:38:44 +0000293 && ((uint8_t *)buf_ptr)[0] == 0
294 && ((uint8_t *)buf_ptr)[1] == 0)
295 {
Lev Walkin8d127872004-09-04 04:44:50 +0000296
297 ASN_DEBUG("Eat EOC; wn=%d--", sel->want_nulls);
298
Lev Walkinbbd93252004-10-12 05:57:23 +0000299 if(type_variant == _TT_ANY
Lev Walkin07f388c2004-10-11 11:43:08 +0000300 && (tag_mode != 1 || sel->cont_level))
301 APPEND("\0\0", 2);
Lev Walkin5c915992004-09-27 20:54:06 +0000302
303 ADVANCE(2);
304 sel->got += 2;
305 if(sel->left != -1) {
306 sel->left -= 2; /* assert(sel->left >= 2) */
307 }
308
Lev Walkinf15320b2004-06-03 03:38:44 +0000309 sel->want_nulls--;
310 if(sel->want_nulls == 0) {
311 /* Move to the next expectation */
Lev Walkin5c915992004-09-27 20:54:06 +0000312 sel->left = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000313 tlv_constr = 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000314 }
Lev Walkin8d127872004-09-04 04:44:50 +0000315
316 continue;
Lev Walkin188ed2c2004-09-13 08:31:01 +0000317 }
318
319 /*
320 * Set up expected tags,
321 * depending on ASN.1 type being decoded.
322 */
Lev Walkinbbd93252004-10-12 05:57:23 +0000323 switch(type_variant) {
Lev Walkin188ed2c2004-09-13 08:31:01 +0000324 case _TT_BIT_STRING:
325 /* X.690: 8.6.4.1, NOTE 2 */
326 /* Fall through */
327 case _TT_GENERIC:
328 default:
329 if(sel) {
330 int level = sel->cont_level;
331 if(level < td->all_tags_count) {
332 expected_tag = td->all_tags[level];
333 break;
334 } else if(td->all_tags_count) {
335 expected_tag = td->all_tags
336 [td->all_tags_count - 1];
337 break;
338 }
339 /* else, Fall through */
340 }
341 /* Fall through */
342 case _TT_ANY:
343 expected_tag = tlv_tag;
344 break;
345 }
346
347
348 if(tlv_tag != expected_tag) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000349 char buf[2][32];
350 ber_tlv_tag_snprint(tlv_tag,
351 buf[0], sizeof(buf[0]));
352 ber_tlv_tag_snprint(td->tags[td->tags_count-1],
353 buf[1], sizeof(buf[1]));
354 ASN_DEBUG("Tag does not match expectation: %s != %s",
355 buf[0], buf[1]);
356 RETURN(RC_FAIL);
357 }
358
Lev Walkinde4825d2004-09-29 13:20:14 +0000359 tlvl = tl + ll; /* Combined length of T and L encoding */
360 if((tlv_len + tlvl) < 0) {
361 /* tlv_len value is too big */
362 ASN_DEBUG("TLV encoding + length (%ld) is too big",
363 (long)tlv_len);
364 RETURN(RC_FAIL);
365 }
366
Lev Walkinf15320b2004-06-03 03:38:44 +0000367 /*
368 * Append a new expectation.
369 */
Lev Walkin3990ba62004-09-24 20:57:41 +0000370 sel = OS__add_stack_el(stck);
Lev Walkin5c915992004-09-27 20:54:06 +0000371 if(!sel) RETURN(RC_FAIL);
Lev Walkinf15320b2004-06-03 03:38:44 +0000372
Lev Walkin5c915992004-09-27 20:54:06 +0000373 sel->tag = tlv_tag;
374
375 sel->want_nulls = (tlv_len==-1);
376 if(sel->prev && sel->prev->left != -1) {
377 /* Check that the parent frame is big enough */
378 if(sel->prev->left < tlvl + (tlv_len==-1?0:tlv_len))
379 RETURN(RC_FAIL);
380 if(tlv_len == -1)
381 sel->left = sel->prev->left - tlvl;
382 else
383 sel->left = tlv_len;
384 } else {
385 sel->left = tlv_len;
386 }
Lev Walkinbbd93252004-10-12 05:57:23 +0000387 if(type_variant == _TT_ANY
Lev Walkin07f388c2004-10-11 11:43:08 +0000388 && (tag_mode != 1 || sel->cont_level))
389 APPEND(buf_ptr, tlvl);
Lev Walkin5c915992004-09-27 20:54:06 +0000390 sel->got += tlvl;
391 ADVANCE(tlvl);
392
393 ASN_DEBUG("+EXPECT2 got=%d left=%d, wn=%d, clvl=%d",
394 sel->got, sel->left, sel->want_nulls, sel->cont_level);
395
Lev Walkinf15320b2004-06-03 03:38:44 +0000396 } while(tlv_constr);
397 if(sel == NULL) {
398 /* Finished operation, "phase out" */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000399 ASN_DEBUG("Phase out");
Lev Walkinf15320b2004-06-03 03:38:44 +0000400 _CH_PHASE(ctx, +3);
401 break;
402 }
403
404 NEXT_PHASE(ctx);
405 /* Fall through */
406 case 2:
Lev Walkinc2346572004-08-11 09:07:36 +0000407 (void *)stck = ctx->ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000408 sel = stck->cur_ptr;
Lev Walkin5c915992004-09-27 20:54:06 +0000409 ASN_DEBUG("Phase 2: Need %ld bytes, size=%ld, alrg=%ld, wn=%d",
410 (long)sel->left, (long)size, (long)sel->got,
411 sel->want_nulls);
Lev Walkinf15320b2004-06-03 03:38:44 +0000412 {
413 ber_tlv_len_t len;
414
415 assert(sel->left >= 0);
416
Lev Walkinec1ffd42004-08-18 04:53:32 +0000417 len = ((ber_tlv_len_t)size < sel->left)
418 ? (ber_tlv_len_t)size : sel->left;
Lev Walkinf15320b2004-06-03 03:38:44 +0000419 if(len > 0) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000420 if(type_variant == _TT_BIT_STRING
Lev Walkin188ed2c2004-09-13 08:31:01 +0000421 && sel->bits_chopped == 0) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000422 /* Put the unused-bits-octet away */
423 st->bits_unused = *(uint8_t *)buf_ptr;
Lev Walkin4d9528c2004-08-11 08:10:13 +0000424 APPEND(((char *)buf_ptr+1), (len - 1));
Lev Walkinf15320b2004-06-03 03:38:44 +0000425 sel->bits_chopped = 1;
426 } else {
427 APPEND(buf_ptr, len);
428 }
429 ADVANCE(len);
430 sel->left -= len;
Lev Walkin5c915992004-09-27 20:54:06 +0000431 sel->got += len;
Lev Walkinf15320b2004-06-03 03:38:44 +0000432 }
433
Lev Walkin5c915992004-09-27 20:54:06 +0000434 if(sel->left) {
435 ASN_DEBUG("OS left %ld, size = %ld, wn=%d\n",
436 (long)sel->left, (long)size, sel->want_nulls);
Lev Walkinf15320b2004-06-03 03:38:44 +0000437 RETURN(RC_WMORE);
Lev Walkinf15320b2004-06-03 03:38:44 +0000438 }
Lev Walkin5c915992004-09-27 20:54:06 +0000439
440 PREV_PHASE(ctx);
441 goto phase1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000442 }
443 break;
444 case 3:
445 phase3:
446 /*
447 * Primitive form, no stack required.
448 */
Lev Walkinbbd93252004-10-12 05:57:23 +0000449 assert(ctx->left >= 0);
450
Lev Walkind9bd7752004-06-05 08:17:50 +0000451 if(size < (size_t)ctx->left) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000452 if(!size) RETURN(RC_WMORE);
453 if(type_variant == _TT_BIT_STRING && ctx->step == 0) {
454 st->bits_unused = *(uint8_t *)buf_ptr;
455 ctx->left--;
456 ADVANCE(1);
457 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000458 APPEND(buf_ptr, size);
Lev Walkinbbd93252004-10-12 05:57:23 +0000459 assert(ctx->step);
Lev Walkinf15320b2004-06-03 03:38:44 +0000460 ctx->left -= size;
461 ADVANCE(size);
462 RETURN(RC_WMORE);
463 } else {
Lev Walkinbbd93252004-10-12 05:57:23 +0000464 if(type_variant == _TT_BIT_STRING
465 && ctx->step == 0 && ctx->left) {
466 st->bits_unused = *(uint8_t *)buf_ptr;
467 ctx->left--;
468 ADVANCE(1);
469 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000470 APPEND(buf_ptr, ctx->left);
471 ADVANCE(ctx->left);
472 ctx->left = 0;
473
474 NEXT_PHASE(ctx);
475 }
476 break;
477 }
478
Lev Walkin5c915992004-09-27 20:54:06 +0000479 if(sel) {
480 ASN_DEBUG("3sel p=%p, wn=%d, l=%ld, g=%ld, size=%ld",
481 sel->prev, sel->want_nulls,
482 (long)sel->left, (long)sel->got, (long)size);
483 if(sel->prev || sel->want_nulls > 1 || sel->left > 0) {
484 RETURN(RC_WMORE);
485 }
486 }
487
Lev Walkinf15320b2004-06-03 03:38:44 +0000488 /*
489 * BIT STRING-specific processing.
490 */
Lev Walkinbbd93252004-10-12 05:57:23 +0000491 if(type_variant == _TT_BIT_STRING && st->size) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000492 /* Finalize BIT STRING: zero out unused bits. */
Lev Walkinbbd93252004-10-12 05:57:23 +0000493 st->buf[st->size-1] &= 0xff << st->bits_unused;
Lev Walkinf15320b2004-06-03 03:38:44 +0000494 }
495
496 ASN_DEBUG("Took %d bytes to encode %s: [%s]:%d",
Lev Walkin3990ba62004-09-24 20:57:41 +0000497 consumed_myself, td->name,
Lev Walkinbbd93252004-10-12 05:57:23 +0000498 (type_variant == _TT_GENERIC) ? (char *)st->buf : "<data>",
499 st->size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000500
Lev Walkinf15320b2004-06-03 03:38:44 +0000501
Lev Walkin5c915992004-09-27 20:54:06 +0000502 RETURN(RC_OK);
Lev Walkinf15320b2004-06-03 03:38:44 +0000503}
504
505/*
506 * Encode OCTET STRING type using DER.
507 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000508asn_enc_rval_t
Lev Walkinbbd93252004-10-12 05:57:23 +0000509OCTET_STRING_encode_der(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000510 int tag_mode, ber_tlv_tag_t tag,
511 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000512 asn_enc_rval_t er;
513 asn_OCTET_STRING_specifics_t *specs = td->specifics
514 ? td->specifics : &asn_DEF_OCTET_STRING_specs;
515 BIT_STRING_t *st = (BIT_STRING_t *)sptr;
516 OS_type_e type_variant = (OS_type_e)specs->subvariant;
517 int fix_last_byte = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000518
519 ASN_DEBUG("%s %s as OCTET STRING",
Lev Walkin1f670c12004-09-02 12:57:25 +0000520 cb?"Estimating":"Encoding", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000521
522 /*
Lev Walkinbbd93252004-10-12 05:57:23 +0000523 * Write tags.
Lev Walkinf15320b2004-06-03 03:38:44 +0000524 */
Lev Walkinbbd93252004-10-12 05:57:23 +0000525 if(type_variant != _TT_ANY || tag_mode == 1) {
526 er.encoded = der_write_tags(td,
527 (type_variant == _TT_BIT_STRING) + st->size,
528 tag_mode, type_variant == _TT_ANY, tag, cb, app_key);
529 if(er.encoded == -1) {
530 er.failed_type = td;
531 er.structure_ptr = sptr;
532 return er;
Lev Walkinf15320b2004-06-03 03:38:44 +0000533 }
Lev Walkinbbd93252004-10-12 05:57:23 +0000534 } else {
535 /* Disallow: [<tag>] IMPLICIT ANY */
536 assert(type_variant != _TT_ANY || tag_mode != -1);
537 er.encoded = 0;
538 }
539
540 if(!cb) {
541 er.encoded += (type_variant == _TT_BIT_STRING) + st->size;
542 return er;
Lev Walkinf15320b2004-06-03 03:38:44 +0000543 }
544
Lev Walkin07f388c2004-10-11 11:43:08 +0000545 /*
Lev Walkinbbd93252004-10-12 05:57:23 +0000546 * Prepare to deal with the last octet of BIT STRING.
Lev Walkin07f388c2004-10-11 11:43:08 +0000547 */
Lev Walkinbbd93252004-10-12 05:57:23 +0000548 if(type_variant == _TT_BIT_STRING) {
549 uint8_t b = st->bits_unused & 0x07;
550 if(b && st->size) fix_last_byte = 1;
551 _ASN_CALLBACK(&b, 1);
552 er.encoded++;
Lev Walkinf15320b2004-06-03 03:38:44 +0000553 }
554
Lev Walkinbbd93252004-10-12 05:57:23 +0000555 /* Invoke callback for the main part of the buffer */
556 _ASN_CALLBACK(st->buf, st->size - fix_last_byte);
Lev Walkinf15320b2004-06-03 03:38:44 +0000557
Lev Walkinbbd93252004-10-12 05:57:23 +0000558 /* The last octet should be stripped off the unused bits */
559 if(fix_last_byte) {
560 uint8_t b = st->buf[st->size-1] & (0xff << st->bits_unused);
561 _ASN_CALLBACK(&b, 1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000562 }
563
Lev Walkinbbd93252004-10-12 05:57:23 +0000564 er.encoded += st->size;
565 return er;
566cb_failed:
567 _ASN_ENCODE_FAILED;
Lev Walkinf15320b2004-06-03 03:38:44 +0000568}
569
Lev Walkina9cc46e2004-09-22 16:06:28 +0000570asn_enc_rval_t
Lev Walkinde4825d2004-09-29 13:20:14 +0000571OCTET_STRING_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000572 int ilevel, enum xer_encoder_flags_e flags,
573 asn_app_consume_bytes_f *cb, void *app_key) {
574 static const char *h2c = "0123456789ABCDEF";
575 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
576 asn_enc_rval_t er;
577 char scratch[16 * 3 + 4];
578 char *p = scratch;
579 uint8_t *buf;
580 uint8_t *end;
581 size_t i;
582
Lev Walkin942fd082004-10-03 09:13:02 +0000583 if(!st || !st->buf)
584 _ASN_ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000585
586 er.encoded = 0;
587
588 /*
589 * Dump the contents of the buffer in hexadecimal.
590 */
591 buf = st->buf;
592 end = buf + st->size;
593 if(flags & XER_F_CANONICAL) {
594 char *scend = scratch + (sizeof(scratch) - 2);
595 for(; buf < end; buf++) {
596 if(p >= scend) {
597 _ASN_CALLBACK(scratch, p - scratch);
598 er.encoded += p - scratch;
599 p = scratch;
600 }
601 *p++ = h2c[(*buf >> 4) & 0x0F];
602 *p++ = h2c[*buf & 0x0F];
603 }
Lev Walkincc6a9102004-09-23 22:06:26 +0000604
605 _ASN_CALLBACK(scratch, p-scratch); /* Dump the rest */
606 er.encoded += p - scratch;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000607 } else {
608 for(i = 0; buf < end; buf++, i++) {
609 if(!(i % 16) && (i || st->size > 16)) {
610 _ASN_CALLBACK(scratch, p-scratch);
611 er.encoded += (p-scratch);
612 p = scratch;
613 _i_ASN_TEXT_INDENT(1, ilevel);
614 }
615 *p++ = h2c[(*buf >> 4) & 0x0F];
616 *p++ = h2c[*buf & 0x0F];
617 *p++ = 0x20;
618 }
Lev Walkincc6a9102004-09-23 22:06:26 +0000619 if(p - scratch) {
620 p--; /* Remove the tail space */
621 _ASN_CALLBACK(scratch, p-scratch); /* Dump the rest */
622 er.encoded += p - scratch;
623 if(st->size > 16)
624 _i_ASN_TEXT_INDENT(1, ilevel-1);
625 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000626 }
627
Lev Walkina9cc46e2004-09-22 16:06:28 +0000628 return er;
Lev Walkin942fd082004-10-03 09:13:02 +0000629cb_failed:
630 _ASN_ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000631}
632
Lev Walkin9ca81892004-10-03 10:54:25 +0000633static struct OCTET_STRING__xer_escape_table_s {
634 char *string;
635 int size;
636} OCTET_STRING__xer_escape_table[] = {
637#define OSXET(s) { s, sizeof(s) - 1 }
638 OSXET("\074\156\165\154\057\076"), /* <nul/> */
639 OSXET("\074\163\157\150\057\076"), /* <soh/> */
640 OSXET("\074\163\164\170\057\076"), /* <stx/> */
641 OSXET("\074\145\164\170\057\076"), /* <etx/> */
642 OSXET("\074\145\157\164\057\076"), /* <eot/> */
643 OSXET("\074\145\156\161\057\076"), /* <enq/> */
644 OSXET("\074\141\143\153\057\076"), /* <ack/> */
645 OSXET("\074\142\145\154\057\076"), /* <bel/> */
646 OSXET("\074\142\163\057\076"), /* <bs/> */
647 OSXET("\011"), /* \t */
648 OSXET("\012"), /* \n */
649 OSXET("\074\166\164\057\076"), /* <vt/> */
650 OSXET("\074\146\146\057\076"), /* <ff/> */
651 OSXET("\015"), /* \r */
652 OSXET("\074\163\157\057\076"), /* <so/> */
653 OSXET("\074\163\151\057\076"), /* <si/> */
654 OSXET("\074\144\154\145\057\076"), /* <dle/> */
655 OSXET("\074\144\143\061\057\076"), /* <de1/> */
656 OSXET("\074\144\143\062\057\076"), /* <de2/> */
657 OSXET("\074\144\143\063\057\076"), /* <de3/> */
658 OSXET("\074\144\143\064\057\076"), /* <de4/> */
659 OSXET("\074\156\141\153\057\076"), /* <nak/> */
660 OSXET("\074\163\171\156\057\076"), /* <syn/> */
661 OSXET("\074\145\164\142\057\076"), /* <etb/> */
662 OSXET("\074\143\141\156\057\076"), /* <can/> */
663 OSXET("\074\145\155\057\076"), /* <em/> */
664 OSXET("\074\163\165\142\057\076"), /* <sub/> */
665 OSXET("\074\145\163\143\057\076"), /* <esc/> */
666 OSXET("\074\151\163\064\057\076"), /* <is4/> */
667 OSXET("\074\151\163\063\057\076"), /* <is3/> */
668 OSXET("\074\151\163\062\057\076"), /* <is2/> */
669 OSXET("\074\151\163\061\057\076"), /* <is1/> */
670 { 0, 0 }, /* " " */
671 { 0, 0 }, /* ! */
672 { 0, 0 }, /* \" */
673 { 0, 0 }, /* # */
674 { 0, 0 }, /* $ */
675 { 0, 0 }, /* % */
676 OSXET("\046\141\155\160\073"), /* &amp; */
677 { 0, 0 }, /* ' */
678 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, /* ()*+,-./ */
679 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, /* 01234567 */
680 {0,0},{0,0},{0,0},{0,0}, /* 89:; */
681 OSXET("\046\154\164\073"), /* &lt; */
682 { 0, 0 }, /* = */
683 OSXET("\046\147\164\073"), /* &gt; */
684};
685
Lev Walkina9cc46e2004-09-22 16:06:28 +0000686asn_enc_rval_t
Lev Walkinde4825d2004-09-29 13:20:14 +0000687OCTET_STRING_encode_xer_ascii(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000688 int ilevel, enum xer_encoder_flags_e flags,
689 asn_app_consume_bytes_f *cb, void *app_key) {
690 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
691 asn_enc_rval_t er;
Lev Walkin9ca81892004-10-03 10:54:25 +0000692 uint8_t *buf, *end;
693 uint8_t *ss; /* Sequence start */
694 ssize_t encoded_len = 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000695
696 (void)ilevel; /* Unused argument */
697 (void)flags; /* Unused argument */
698
699 if(!st || !st->buf)
700 _ASN_ENCODE_FAILED;
701
Lev Walkin9ca81892004-10-03 10:54:25 +0000702 buf = st->buf;
703 end = buf + st->size;
704 for(ss = buf; buf < end; buf++) {
Lev Walkin443d2512004-10-05 06:35:31 +0000705 unsigned int ch = *buf;
Lev Walkin9ca81892004-10-03 10:54:25 +0000706 int s_len; /* Special encoding sequence length */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000707
Lev Walkin9ca81892004-10-03 10:54:25 +0000708 /*
709 * Escape certain characters: X.680/11.15
710 */
711 if(ch < sizeof(OCTET_STRING__xer_escape_table)
712 /sizeof(OCTET_STRING__xer_escape_table[0])
713 && (s_len = OCTET_STRING__xer_escape_table[ch].size)) {
714 if(((buf - ss) && cb(ss, buf - ss, app_key) < 0)
715 || cb(OCTET_STRING__xer_escape_table[ch].string, s_len,
716 app_key) < 0)
717 _ASN_ENCODE_FAILED;
718 encoded_len += (buf - ss) + s_len;
719 ss = buf + 1;
720 }
721 }
722
723 encoded_len += (buf - ss);
724 if((buf - ss) && cb(ss, buf - ss, app_key) < 0)
725 _ASN_ENCODE_FAILED;
726
727 er.encoded = encoded_len;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000728 return er;
729}
730
Lev Walkinf15320b2004-06-03 03:38:44 +0000731int
Lev Walkinde4825d2004-09-29 13:20:14 +0000732OCTET_STRING_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +0000733 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin4d9528c2004-08-11 08:10:13 +0000734 static const char *h2c = "0123456789ABCDEF";
Lev Walkinc2346572004-08-11 09:07:36 +0000735 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000736 char scratch[16 * 3 + 4];
737 char *p = scratch;
738 uint8_t *buf;
739 uint8_t *end;
740 size_t i;
Lev Walkinf15320b2004-06-03 03:38:44 +0000741
Lev Walkind9bd7752004-06-05 08:17:50 +0000742 (void)td; /* Unused argument */
743
Lev Walkin8e8078a2004-09-26 13:10:40 +0000744 if(!st || !st->buf) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000745
746 /*
747 * Dump the contents of the buffer in hexadecimal.
748 */
749 buf = st->buf;
750 end = buf + st->size;
751 for(i = 0; buf < end; buf++, i++) {
752 if(!(i % 16) && (i || st->size > 16)) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000753 if(cb(scratch, p - scratch, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000754 return -1;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000755 _i_INDENT(1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000756 p = scratch;
757 }
758 *p++ = h2c[(*buf >> 4) & 0x0F];
759 *p++ = h2c[*buf & 0x0F];
Lev Walkina9cc46e2004-09-22 16:06:28 +0000760 *p++ = 0x20;
Lev Walkinf15320b2004-06-03 03:38:44 +0000761 }
762
Lev Walkincc6a9102004-09-23 22:06:26 +0000763 if(p > scratch) {
764 p--; /* Remove the tail space */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000765 if(cb(scratch, p - scratch, app_key) < 0)
Lev Walkincc6a9102004-09-23 22:06:26 +0000766 return -1;
767 }
768
769 return 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000770}
771
772int
Lev Walkinde4825d2004-09-29 13:20:14 +0000773OCTET_STRING_print_ascii(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000774 int ilevel, asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +0000775 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000776
Lev Walkind9bd7752004-06-05 08:17:50 +0000777 (void)td; /* Unused argument */
778 (void)ilevel; /* Unused argument */
779
Lev Walkinf15320b2004-06-03 03:38:44 +0000780 if(st && st->buf) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000781 return (cb(st->buf, st->size, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000782 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000783 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000784 }
785}
786
787void
Lev Walkinde4825d2004-09-29 13:20:14 +0000788OCTET_STRING_free(asn_TYPE_descriptor_t *td, void *sptr, int contents_only) {
Lev Walkinc2346572004-08-11 09:07:36 +0000789 OCTET_STRING_t *st = (OCTET_STRING_t *)sptr;
Lev Walkinbbd93252004-10-12 05:57:23 +0000790 asn_OCTET_STRING_specifics_t *specs = td->specifics
791 ? td->specifics : &asn_DEF_OCTET_STRING_specs;
792 asn_struct_ctx_t *ctx = (asn_struct_ctx_t *)
793 ((char *)st + specs->ctx_offset);
794 struct _stack *stck = ctx->ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000795
796 if(!td || !st)
797 return;
798
799 ASN_DEBUG("Freeing %s as OCTET STRING", td->name);
800
801 if(st->buf) {
802 FREEMEM(st->buf);
803 }
804
805 /*
806 * Remove decode-time stack.
807 */
808 if(stck) {
809 while(stck->tail) {
810 struct _stack_el *sel = stck->tail;
811 stck->tail = sel->prev;
812 FREEMEM(sel);
813 }
814 FREEMEM(stck);
815 }
816
817 if(!contents_only) {
818 FREEMEM(st);
819 }
820}
821
822/*
823 * Conversion routines.
824 */
825int
826OCTET_STRING_fromBuf(OCTET_STRING_t *st, const char *str, int len) {
827 void *buf;
828
829 if(st == 0 || (str == 0 && len)) {
830 errno = EINVAL;
831 return -1;
832 }
833
834 /*
835 * Clear the OCTET STRING.
836 */
837 if(str == NULL) {
838 if(st->buf)
839 FREEMEM(st->buf);
840 st->size = 0;
841 return 0;
842 }
843
844 /* Determine the original string size, if not explicitly given */
845 if(len < 0)
846 len = strlen(str);
847
848 /* Allocate and fill the memory */
849 buf = MALLOC(len + 1);
850 if(buf == NULL) {
851 return -1;
852 } else {
Lev Walkinc2346572004-08-11 09:07:36 +0000853 st->buf = (uint8_t *)buf;
Lev Walkinf15320b2004-06-03 03:38:44 +0000854 st->size = len;
855 }
856
857 memcpy(buf, str, len);
858 st->buf[st->size] = '\0'; /* Couldn't use memcpy(len+1)! */
859
860 return 0;
861}
862
863OCTET_STRING_t *
Lev Walkinbbd93252004-10-12 05:57:23 +0000864OCTET_STRING_new_fromBuf(asn_TYPE_descriptor_t *td, const char *str, int len) {
865 asn_OCTET_STRING_specifics_t *specs = td->specifics
866 ? td->specifics : &asn_DEF_OCTET_STRING_specs;
Lev Walkinf15320b2004-06-03 03:38:44 +0000867 OCTET_STRING_t *st;
868
Lev Walkinbbd93252004-10-12 05:57:23 +0000869 st = (OCTET_STRING_t *)CALLOC(1, specs->struct_size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000870 if(st && str && OCTET_STRING_fromBuf(st, str, len)) {
871 free(st);
872 st = NULL;
873 }
874
875 return st;
876}
877