blob: b99bcbdeae293e39c86a6fdf865f55032f3f98dc [file] [log] [blame]
vlmfa67ddc2004-06-03 03:38:44 +00001/*-
vlm72425de2004-09-13 08:31:01 +00002 * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
vlmfa67ddc2004-06-03 03:38:44 +00003 * Redistribution and modifications are permitted subject to BSD license.
4 */
vlm39ba4c42004-09-22 16:06:28 +00005#include <asn_internal.h>
vlmfa67ddc2004-06-03 03:38:44 +00006#include <OCTET_STRING.h>
vlm0f1ab762004-10-12 05:57:23 +00007#include <BIT_STRING.h> /* for .bits_unused member */
vlmfa67ddc2004-06-03 03:38:44 +00008#include <assert.h>
9#include <errno.h>
10
11/*
12 * OCTET STRING basic type description.
13 */
vlm7b75d982004-09-29 13:20:14 +000014static ber_tlv_tag_t asn_DEF_OCTET_STRING_tags[] = {
vlmfa67ddc2004-06-03 03:38:44 +000015 (ASN_TAG_CLASS_UNIVERSAL | (4 << 2))
16};
vlm0f1ab762004-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};
vlm7b75d982004-09-29 13:20:14 +000022asn_TYPE_descriptor_t asn_DEF_OCTET_STRING = {
vlm9de248e2004-10-20 15:50:55 +000023 "OCTET STRING", /* Canonical name */
24 "OCTET_STRING", /* XML tag name */
vlm39ba4c42004-09-22 16:06:28 +000025 OCTET_STRING_free,
26 OCTET_STRING_print, /* non-ascii stuff, generally */
vlmfa67ddc2004-06-03 03:38:44 +000027 asn_generic_no_constraint,
28 OCTET_STRING_decode_ber,
29 OCTET_STRING_encode_der,
vlm9de248e2004-10-20 15:50:55 +000030 OCTET_STRING_decode_xer_hex,
vlm39ba4c42004-09-22 16:06:28 +000031 OCTET_STRING_encode_xer,
vlmfa67ddc2004-06-03 03:38:44 +000032 0, /* Use generic outmost tag fetcher */
vlm7b75d982004-09-29 13:20:14 +000033 asn_DEF_OCTET_STRING_tags,
34 sizeof(asn_DEF_OCTET_STRING_tags)
35 / sizeof(asn_DEF_OCTET_STRING_tags[0]),
36 asn_DEF_OCTET_STRING_tags, /* Same as above */
37 sizeof(asn_DEF_OCTET_STRING_tags)
38 / sizeof(asn_DEF_OCTET_STRING_tags[0]),
vlme413c122004-08-20 13:23:42 +000039 0, 0, /* No members */
vlm0f1ab762004-10-12 05:57:23 +000040 &asn_DEF_OCTET_STRING_specs
vlmfa67ddc2004-06-03 03:38:44 +000041};
42
vlmef1b4c02004-09-23 22:06:26 +000043#undef _CH_PHASE
44#undef NEXT_PHASE
45#undef PREV_PHASE
vlmfa67ddc2004-06-03 03:38:44 +000046#define _CH_PHASE(ctx, inc) do { \
47 if(ctx->phase == 0) \
48 ctx->step = 0; \
49 ctx->phase += inc; \
50 } while(0)
51#define NEXT_PHASE(ctx) _CH_PHASE(ctx, +1)
52#define PREV_PHASE(ctx) _CH_PHASE(ctx, -1)
53
vlmef1b4c02004-09-23 22:06:26 +000054#undef ADVANCE
vlmd86c9252004-08-25 01:34:11 +000055#define ADVANCE(num_bytes) do { \
vlmb33ed682004-09-24 20:57:41 +000056 size_t num = (num_bytes); \
vlmd86c9252004-08-25 01:34:11 +000057 buf_ptr = ((char *)buf_ptr) + num; \
58 size -= num; \
59 consumed_myself += num; \
vlmfa67ddc2004-06-03 03:38:44 +000060 } while(0)
61
vlmef1b4c02004-09-23 22:06:26 +000062#undef RETURN
vlmfa67ddc2004-06-03 03:38:44 +000063#define RETURN(_code) do { \
64 rval.code = _code; \
65 rval.consumed = consumed_myself;\
66 return rval; \
67 } while(0)
68
vlmef1b4c02004-09-23 22:06:26 +000069#undef APPEND
vlmb42843a2004-06-05 08:17:50 +000070#define APPEND(bufptr, bufsize) do { \
vlmcdfdb722004-09-04 04:44:50 +000071 size_t _bs = (bufsize); \
vlmb42843a2004-06-05 08:17:50 +000072 size_t _ns = ctx->step; /* Allocated */ \
vlmcdfdb722004-09-04 04:44:50 +000073 if(_ns <= (size_t)(st->size + _bs)) { \
vlmb42843a2004-06-05 08:17:50 +000074 void *ptr; \
vlm72425de2004-09-13 08:31:01 +000075 /* Be nice and round to the memory allocator */ \
vlmb42843a2004-06-05 08:17:50 +000076 do { _ns = _ns ? _ns<<2 : 16; } \
vlmcdfdb722004-09-04 04:44:50 +000077 while(_ns <= (size_t)(st->size + _bs)); \
vlmb42843a2004-06-05 08:17:50 +000078 ptr = REALLOC(st->buf, _ns); \
79 if(ptr) { \
vlmda674682004-08-11 09:07:36 +000080 st->buf = (uint8_t *)ptr; \
vlmb42843a2004-06-05 08:17:50 +000081 ctx->step = _ns; \
82 } else { \
83 RETURN(RC_FAIL); \
84 } \
85 } \
vlmcdfdb722004-09-04 04:44:50 +000086 memcpy(st->buf + st->size, bufptr, _bs); \
87 st->size += _bs; \
vlmb42843a2004-06-05 08:17:50 +000088 if(st->size < 0) \
89 /* Why even care?.. JIC */ \
90 RETURN(RC_FAIL); \
91 /* Convenient nul-termination */ \
92 st->buf[st->size] = '\0'; \
vlmfa67ddc2004-06-03 03:38:44 +000093 } while(0)
94
95/*
vlm267b7fd2004-10-11 11:43:08 +000096 * Internal variant of the OCTET STRING.
97 */
98typedef enum OS_type {
99 _TT_GENERIC = 0, /* Just a random OCTET STRING */
100 _TT_BIT_STRING = 1, /* BIT STRING type, a special case */
101 _TT_ANY = 2, /* ANY type, a special case too */
102} OS_type_e;
103
104/*
vlmfa67ddc2004-06-03 03:38:44 +0000105 * The main reason why ASN.1 is still alive is that too much time and effort
106 * is necessary for learning it more or less adequately, thus creating a gut
107 * necessity to demonstrate that aquired skill everywhere afterwards.
108 * No, I am not going to explain what the following stuff is.
109 */
110struct _stack_el {
vlmb33ed682004-09-24 20:57:41 +0000111 ber_tlv_len_t left; /* What's left to read (or -1) */
vlmdb6b2902004-09-27 20:54:06 +0000112 ber_tlv_len_t got; /* What was actually processed */
vlm72425de2004-09-13 08:31:01 +0000113 int cont_level; /* Depth of subcontainment */
vlmfa67ddc2004-06-03 03:38:44 +0000114 int want_nulls; /* Want null "end of content" octets? */
115 int bits_chopped; /* Flag in BIT STRING mode */
vlmdb6b2902004-09-27 20:54:06 +0000116 ber_tlv_tag_t tag; /* For debugging purposes */
vlmfa67ddc2004-06-03 03:38:44 +0000117 struct _stack_el *prev;
118 struct _stack_el *next;
119};
120struct _stack {
121 struct _stack_el *tail;
122 struct _stack_el *cur_ptr;
123};
124
125static struct _stack_el *
vlmb33ed682004-09-24 20:57:41 +0000126OS__add_stack_el(struct _stack *st) {
vlmfa67ddc2004-06-03 03:38:44 +0000127 struct _stack_el *nel;
128
vlm72425de2004-09-13 08:31:01 +0000129 /*
130 * Reuse the old stack frame or allocate a new one.
131 */
vlmfa67ddc2004-06-03 03:38:44 +0000132 if(st->cur_ptr && st->cur_ptr->next) {
133 nel = st->cur_ptr->next;
vlmfa67ddc2004-06-03 03:38:44 +0000134 nel->bits_chopped = 0;
vlmdb6b2902004-09-27 20:54:06 +0000135 nel->got = 0;
136 /* Retain the nel->cont_level, it's correct. */
vlmfa67ddc2004-06-03 03:38:44 +0000137 } else {
vlmda674682004-08-11 09:07:36 +0000138 (void *)nel = CALLOC(1, sizeof(struct _stack_el));
vlmfa67ddc2004-06-03 03:38:44 +0000139 if(nel == NULL)
140 return NULL;
141
142 if(st->tail) {
vlm72425de2004-09-13 08:31:01 +0000143 /* Increase a subcontainment depth */
144 nel->cont_level = st->tail->cont_level + 1;
vlmfa67ddc2004-06-03 03:38:44 +0000145 st->tail->next = nel;
146 }
147 nel->prev = st->tail;
148 st->tail = nel;
149 }
150
151 st->cur_ptr = nel;
152
153 return nel;
154}
155
156static struct _stack *
157_new_stack() {
vlm0f1ab762004-10-12 05:57:23 +0000158 return (struct _stack *)CALLOC(1, sizeof(struct _stack));
vlmfa67ddc2004-06-03 03:38:44 +0000159}
160
161/*
162 * Decode OCTET STRING type.
163 */
vlm9de248e2004-10-20 15:50:55 +0000164asn_dec_rval_t
vlm7b75d982004-09-29 13:20:14 +0000165OCTET_STRING_decode_ber(asn_codec_ctx_t *opt_codec_ctx,
166 asn_TYPE_descriptor_t *td,
vlmfa67ddc2004-06-03 03:38:44 +0000167 void **os_structure, void *buf_ptr, size_t size, int tag_mode) {
vlm0f1ab762004-10-12 05:57:23 +0000168 asn_OCTET_STRING_specifics_t *specs = td->specifics
vlm9de248e2004-10-20 15:50:55 +0000169 ? (asn_OCTET_STRING_specifics_t *)td->specifics
170 : &asn_DEF_OCTET_STRING_specs;
vlm0f1ab762004-10-12 05:57:23 +0000171 BIT_STRING_t *st = (BIT_STRING_t *)*os_structure;
vlm9de248e2004-10-20 15:50:55 +0000172 asn_dec_rval_t rval;
vlm7b75d982004-09-29 13:20:14 +0000173 asn_struct_ctx_t *ctx;
vlmfa67ddc2004-06-03 03:38:44 +0000174 ssize_t consumed_myself = 0;
vlm0f1ab762004-10-12 05:57:23 +0000175 struct _stack *stck; /* Expectations stack structure */
vlmdb6b2902004-09-27 20:54:06 +0000176 struct _stack_el *sel = 0; /* Stack element */
vlmfa67ddc2004-06-03 03:38:44 +0000177 int tlv_constr;
vlm0f1ab762004-10-12 05:57:23 +0000178 OS_type_e type_variant = (OS_type_e)specs->subvariant;
vlmfa67ddc2004-06-03 03:38:44 +0000179
vlmef1b4c02004-09-23 22:06:26 +0000180 ASN_DEBUG("Decoding %s as %s (frame %ld)",
181 td->name,
vlm0f1ab762004-10-12 05:57:23 +0000182 (type_variant == _TT_GENERIC) ?
183 "OCTET STRING" : "OS-SpecialCase",
vlmef1b4c02004-09-23 22:06:26 +0000184 (long)size);
vlmfa67ddc2004-06-03 03:38:44 +0000185
186 /*
187 * Create the string if does not exist.
188 */
189 if(st == NULL) {
vlm0f1ab762004-10-12 05:57:23 +0000190 (void *)st = *os_structure = CALLOC(1, specs->struct_size);
vlmfa67ddc2004-06-03 03:38:44 +0000191 if(st == NULL)
192 RETURN(RC_FAIL);
193 }
194
195 /* Restore parsing context */
vlm0f1ab762004-10-12 05:57:23 +0000196 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
vlmfa67ddc2004-06-03 03:38:44 +0000197
198 switch(ctx->phase) {
199 case 0:
200 /*
201 * Check tags.
202 */
vlm7b75d982004-09-29 13:20:14 +0000203 rval = ber_check_tags(opt_codec_ctx, td, ctx,
vlm6678cb12004-09-26 13:10:40 +0000204 buf_ptr, size, tag_mode, -1,
vlmfa67ddc2004-06-03 03:38:44 +0000205 &ctx->left, &tlv_constr);
vlmd7c2ef32004-10-05 06:35:31 +0000206 if(rval.code != RC_OK)
207 return rval;
vlmfa67ddc2004-06-03 03:38:44 +0000208
vlmfa67ddc2004-06-03 03:38:44 +0000209 if(tlv_constr) {
210 /*
211 * Complex operation, requires stack of expectations.
212 */
213 ctx->ptr = _new_stack();
214 if(ctx->ptr) {
vlmda674682004-08-11 09:07:36 +0000215 (void *)stck = ctx->ptr;
vlmfa67ddc2004-06-03 03:38:44 +0000216 } else {
217 RETURN(RC_FAIL);
218 }
219 } else {
220 /*
221 * Jump into stackless primitive decoding.
222 */
223 _CH_PHASE(ctx, 3);
vlm0f1ab762004-10-12 05:57:23 +0000224 if(type_variant == _TT_ANY && tag_mode != 1)
vlm72425de2004-09-13 08:31:01 +0000225 APPEND(buf_ptr, rval.consumed);
vlmfa67ddc2004-06-03 03:38:44 +0000226 ADVANCE(rval.consumed);
227 goto phase3;
228 }
229
vlmfa67ddc2004-06-03 03:38:44 +0000230 NEXT_PHASE(ctx);
231 /* Fall through */
232 case 1:
233 phase1:
234 /*
235 * Fill the stack with expectations.
236 */
vlmda674682004-08-11 09:07:36 +0000237 (void *)stck = ctx->ptr;
vlmfa67ddc2004-06-03 03:38:44 +0000238 sel = stck->cur_ptr;
239 do {
240 ber_tlv_tag_t tlv_tag;
241 ber_tlv_len_t tlv_len;
vlm72425de2004-09-13 08:31:01 +0000242 ber_tlv_tag_t expected_tag;
vlmdb6b2902004-09-27 20:54:06 +0000243 ssize_t tl, ll, tlvl;
vlm6678cb12004-09-26 13:10:40 +0000244 /* This one works even if (sel->left == -1) */
245 ssize_t Left = ((!sel||(size_t)sel->left >= size)
vlmdb6b2902004-09-27 20:54:06 +0000246 ?(ssize_t)size:sel->left);
vlmfa67ddc2004-06-03 03:38:44 +0000247
vlmb33ed682004-09-24 20:57:41 +0000248
vlma351c2b2004-10-26 10:12:14 +0000249 ASN_DEBUG("%p, s->l=%ld, s->wn=%ld, s->g=%ld\n", sel,
250 (long)sel?sel->left:0,
251 (long)sel?sel->want_nulls:0,
252 (long)sel?sel->got:0
vlmdb6b2902004-09-27 20:54:06 +0000253 );
254 if(sel && sel->left <= 0 && sel->want_nulls == 0) {
255 if(sel->prev) {
256 struct _stack_el *prev = sel->prev;
257 if(prev->left != -1) {
258 if(prev->left < sel->got)
259 RETURN(RC_FAIL);
260 prev->left -= sel->got;
261 }
262 prev->got += sel->got;
263 sel = stck->cur_ptr = prev;
264 if(!sel) break;
265 tlv_constr = 1;
266 continue;
267 } else {
268 sel = stck->cur_ptr = 0;
269 break; /* Nothing to wait */
270 }
271 }
272
vlmb33ed682004-09-24 20:57:41 +0000273 tl = ber_fetch_tag(buf_ptr, Left, &tlv_tag);
vlma351c2b2004-10-26 10:12:14 +0000274 ASN_DEBUG("fetch tag(size=%ld,L=%ld), %sstack, left=%ld, wn=%ld, tl=%ld",
275 (long)size, (long)Left, sel?"":"!",
276 (long)sel?sel->left:0, (long)sel?sel->want_nulls:0,
277 (long)tl);
vlmfa67ddc2004-06-03 03:38:44 +0000278 switch(tl) {
279 case -1: RETURN(RC_FAIL);
280 case 0: RETURN(RC_WMORE);
281 }
282
283 tlv_constr = BER_TLV_CONSTRUCTED(buf_ptr);
284
285 ll = ber_fetch_length(tlv_constr,
vlmb33ed682004-09-24 20:57:41 +0000286 (char *)buf_ptr + tl, Left - tl, &tlv_len);
vlmdb6b2902004-09-27 20:54:06 +0000287 ASN_DEBUG("Got tag=%s, tc=%d, left=%ld, tl=%d, len=%d, ll=%d",
vlmcdfdb722004-09-04 04:44:50 +0000288 ber_tlv_tag_string(tlv_tag), tlv_constr,
vlmdb6b2902004-09-27 20:54:06 +0000289 (long)Left, tl, tlv_len, ll);
vlmfa67ddc2004-06-03 03:38:44 +0000290 switch(ll) {
291 case -1: RETURN(RC_FAIL);
292 case 0: RETURN(RC_WMORE);
293 }
294
vlmcdfdb722004-09-04 04:44:50 +0000295 if(sel && sel->want_nulls
vlmfa67ddc2004-06-03 03:38:44 +0000296 && ((uint8_t *)buf_ptr)[0] == 0
297 && ((uint8_t *)buf_ptr)[1] == 0)
298 {
vlmcdfdb722004-09-04 04:44:50 +0000299
300 ASN_DEBUG("Eat EOC; wn=%d--", sel->want_nulls);
301
vlm0f1ab762004-10-12 05:57:23 +0000302 if(type_variant == _TT_ANY
vlm267b7fd2004-10-11 11:43:08 +0000303 && (tag_mode != 1 || sel->cont_level))
304 APPEND("\0\0", 2);
vlmdb6b2902004-09-27 20:54:06 +0000305
306 ADVANCE(2);
307 sel->got += 2;
308 if(sel->left != -1) {
309 sel->left -= 2; /* assert(sel->left >= 2) */
310 }
311
vlmfa67ddc2004-06-03 03:38:44 +0000312 sel->want_nulls--;
313 if(sel->want_nulls == 0) {
314 /* Move to the next expectation */
vlmdb6b2902004-09-27 20:54:06 +0000315 sel->left = 0;
vlmfa67ddc2004-06-03 03:38:44 +0000316 tlv_constr = 1;
vlmfa67ddc2004-06-03 03:38:44 +0000317 }
vlmcdfdb722004-09-04 04:44:50 +0000318
319 continue;
vlm72425de2004-09-13 08:31:01 +0000320 }
321
322 /*
323 * Set up expected tags,
324 * depending on ASN.1 type being decoded.
325 */
vlm0f1ab762004-10-12 05:57:23 +0000326 switch(type_variant) {
vlm72425de2004-09-13 08:31:01 +0000327 case _TT_BIT_STRING:
328 /* X.690: 8.6.4.1, NOTE 2 */
329 /* Fall through */
330 case _TT_GENERIC:
331 default:
332 if(sel) {
333 int level = sel->cont_level;
334 if(level < td->all_tags_count) {
335 expected_tag = td->all_tags[level];
336 break;
337 } else if(td->all_tags_count) {
338 expected_tag = td->all_tags
339 [td->all_tags_count - 1];
340 break;
341 }
342 /* else, Fall through */
343 }
344 /* Fall through */
345 case _TT_ANY:
346 expected_tag = tlv_tag;
347 break;
348 }
349
350
351 if(tlv_tag != expected_tag) {
vlmfa67ddc2004-06-03 03:38:44 +0000352 char buf[2][32];
353 ber_tlv_tag_snprint(tlv_tag,
354 buf[0], sizeof(buf[0]));
355 ber_tlv_tag_snprint(td->tags[td->tags_count-1],
356 buf[1], sizeof(buf[1]));
357 ASN_DEBUG("Tag does not match expectation: %s != %s",
358 buf[0], buf[1]);
359 RETURN(RC_FAIL);
360 }
361
vlm7b75d982004-09-29 13:20:14 +0000362 tlvl = tl + ll; /* Combined length of T and L encoding */
363 if((tlv_len + tlvl) < 0) {
364 /* tlv_len value is too big */
365 ASN_DEBUG("TLV encoding + length (%ld) is too big",
366 (long)tlv_len);
367 RETURN(RC_FAIL);
368 }
369
vlmfa67ddc2004-06-03 03:38:44 +0000370 /*
371 * Append a new expectation.
372 */
vlmb33ed682004-09-24 20:57:41 +0000373 sel = OS__add_stack_el(stck);
vlmdb6b2902004-09-27 20:54:06 +0000374 if(!sel) RETURN(RC_FAIL);
vlmfa67ddc2004-06-03 03:38:44 +0000375
vlmdb6b2902004-09-27 20:54:06 +0000376 sel->tag = tlv_tag;
377
378 sel->want_nulls = (tlv_len==-1);
379 if(sel->prev && sel->prev->left != -1) {
380 /* Check that the parent frame is big enough */
381 if(sel->prev->left < tlvl + (tlv_len==-1?0:tlv_len))
382 RETURN(RC_FAIL);
383 if(tlv_len == -1)
384 sel->left = sel->prev->left - tlvl;
385 else
386 sel->left = tlv_len;
387 } else {
388 sel->left = tlv_len;
389 }
vlm0f1ab762004-10-12 05:57:23 +0000390 if(type_variant == _TT_ANY
vlm267b7fd2004-10-11 11:43:08 +0000391 && (tag_mode != 1 || sel->cont_level))
392 APPEND(buf_ptr, tlvl);
vlmdb6b2902004-09-27 20:54:06 +0000393 sel->got += tlvl;
394 ADVANCE(tlvl);
395
vlma351c2b2004-10-26 10:12:14 +0000396 ASN_DEBUG("+EXPECT2 got=%ld left=%ld, wn=%d, clvl=%d",
397 (long)sel->got, (long)sel->left,
398 sel->want_nulls, sel->cont_level);
vlmdb6b2902004-09-27 20:54:06 +0000399
vlmfa67ddc2004-06-03 03:38:44 +0000400 } while(tlv_constr);
401 if(sel == NULL) {
402 /* Finished operation, "phase out" */
vlm72425de2004-09-13 08:31:01 +0000403 ASN_DEBUG("Phase out");
vlmfa67ddc2004-06-03 03:38:44 +0000404 _CH_PHASE(ctx, +3);
405 break;
406 }
407
408 NEXT_PHASE(ctx);
409 /* Fall through */
410 case 2:
vlmda674682004-08-11 09:07:36 +0000411 (void *)stck = ctx->ptr;
vlmfa67ddc2004-06-03 03:38:44 +0000412 sel = stck->cur_ptr;
vlmdb6b2902004-09-27 20:54:06 +0000413 ASN_DEBUG("Phase 2: Need %ld bytes, size=%ld, alrg=%ld, wn=%d",
414 (long)sel->left, (long)size, (long)sel->got,
415 sel->want_nulls);
vlmfa67ddc2004-06-03 03:38:44 +0000416 {
417 ber_tlv_len_t len;
418
419 assert(sel->left >= 0);
420
vlmc5190612004-08-18 04:53:32 +0000421 len = ((ber_tlv_len_t)size < sel->left)
422 ? (ber_tlv_len_t)size : sel->left;
vlmfa67ddc2004-06-03 03:38:44 +0000423 if(len > 0) {
vlm0f1ab762004-10-12 05:57:23 +0000424 if(type_variant == _TT_BIT_STRING
vlm72425de2004-09-13 08:31:01 +0000425 && sel->bits_chopped == 0) {
vlm0f1ab762004-10-12 05:57:23 +0000426 /* Put the unused-bits-octet away */
427 st->bits_unused = *(uint8_t *)buf_ptr;
vlm1ff928d2004-08-11 08:10:13 +0000428 APPEND(((char *)buf_ptr+1), (len - 1));
vlmfa67ddc2004-06-03 03:38:44 +0000429 sel->bits_chopped = 1;
430 } else {
431 APPEND(buf_ptr, len);
432 }
433 ADVANCE(len);
434 sel->left -= len;
vlmdb6b2902004-09-27 20:54:06 +0000435 sel->got += len;
vlmfa67ddc2004-06-03 03:38:44 +0000436 }
437
vlmdb6b2902004-09-27 20:54:06 +0000438 if(sel->left) {
439 ASN_DEBUG("OS left %ld, size = %ld, wn=%d\n",
440 (long)sel->left, (long)size, sel->want_nulls);
vlmfa67ddc2004-06-03 03:38:44 +0000441 RETURN(RC_WMORE);
vlmfa67ddc2004-06-03 03:38:44 +0000442 }
vlmdb6b2902004-09-27 20:54:06 +0000443
444 PREV_PHASE(ctx);
445 goto phase1;
vlmfa67ddc2004-06-03 03:38:44 +0000446 }
447 break;
448 case 3:
449 phase3:
450 /*
451 * Primitive form, no stack required.
452 */
vlm0f1ab762004-10-12 05:57:23 +0000453 assert(ctx->left >= 0);
454
vlmb42843a2004-06-05 08:17:50 +0000455 if(size < (size_t)ctx->left) {
vlm0f1ab762004-10-12 05:57:23 +0000456 if(!size) RETURN(RC_WMORE);
457 if(type_variant == _TT_BIT_STRING && ctx->step == 0) {
458 st->bits_unused = *(uint8_t *)buf_ptr;
459 ctx->left--;
460 ADVANCE(1);
461 }
vlmfa67ddc2004-06-03 03:38:44 +0000462 APPEND(buf_ptr, size);
vlm0f1ab762004-10-12 05:57:23 +0000463 assert(ctx->step);
vlmfa67ddc2004-06-03 03:38:44 +0000464 ctx->left -= size;
465 ADVANCE(size);
466 RETURN(RC_WMORE);
467 } else {
vlm0f1ab762004-10-12 05:57:23 +0000468 if(type_variant == _TT_BIT_STRING
469 && ctx->step == 0 && ctx->left) {
470 st->bits_unused = *(uint8_t *)buf_ptr;
471 ctx->left--;
472 ADVANCE(1);
473 }
vlmfa67ddc2004-06-03 03:38:44 +0000474 APPEND(buf_ptr, ctx->left);
475 ADVANCE(ctx->left);
476 ctx->left = 0;
477
478 NEXT_PHASE(ctx);
479 }
480 break;
481 }
482
vlmdb6b2902004-09-27 20:54:06 +0000483 if(sel) {
484 ASN_DEBUG("3sel p=%p, wn=%d, l=%ld, g=%ld, size=%ld",
485 sel->prev, sel->want_nulls,
486 (long)sel->left, (long)sel->got, (long)size);
487 if(sel->prev || sel->want_nulls > 1 || sel->left > 0) {
488 RETURN(RC_WMORE);
489 }
490 }
491
vlmfa67ddc2004-06-03 03:38:44 +0000492 /*
493 * BIT STRING-specific processing.
494 */
vlm0f1ab762004-10-12 05:57:23 +0000495 if(type_variant == _TT_BIT_STRING && st->size) {
vlmfa67ddc2004-06-03 03:38:44 +0000496 /* Finalize BIT STRING: zero out unused bits. */
vlm0f1ab762004-10-12 05:57:23 +0000497 st->buf[st->size-1] &= 0xff << st->bits_unused;
vlmfa67ddc2004-06-03 03:38:44 +0000498 }
499
vlma351c2b2004-10-26 10:12:14 +0000500 ASN_DEBUG("Took %ld bytes to encode %s: [%s]:%ld",
501 (long)consumed_myself, td->name,
vlm0f1ab762004-10-12 05:57:23 +0000502 (type_variant == _TT_GENERIC) ? (char *)st->buf : "<data>",
vlma351c2b2004-10-26 10:12:14 +0000503 (long)st->size);
vlmfa67ddc2004-06-03 03:38:44 +0000504
vlmfa67ddc2004-06-03 03:38:44 +0000505
vlmdb6b2902004-09-27 20:54:06 +0000506 RETURN(RC_OK);
vlmfa67ddc2004-06-03 03:38:44 +0000507}
508
509/*
510 * Encode OCTET STRING type using DER.
511 */
vlm39ba4c42004-09-22 16:06:28 +0000512asn_enc_rval_t
vlm0f1ab762004-10-12 05:57:23 +0000513OCTET_STRING_encode_der(asn_TYPE_descriptor_t *td, void *sptr,
vlmfa67ddc2004-06-03 03:38:44 +0000514 int tag_mode, ber_tlv_tag_t tag,
515 asn_app_consume_bytes_f *cb, void *app_key) {
vlm0f1ab762004-10-12 05:57:23 +0000516 asn_enc_rval_t er;
517 asn_OCTET_STRING_specifics_t *specs = td->specifics
vlm9de248e2004-10-20 15:50:55 +0000518 ? (asn_OCTET_STRING_specifics_t *)td->specifics
519 : &asn_DEF_OCTET_STRING_specs;
vlm0f1ab762004-10-12 05:57:23 +0000520 BIT_STRING_t *st = (BIT_STRING_t *)sptr;
521 OS_type_e type_variant = (OS_type_e)specs->subvariant;
522 int fix_last_byte = 0;
vlmfa67ddc2004-06-03 03:38:44 +0000523
524 ASN_DEBUG("%s %s as OCTET STRING",
vlm3e693022004-09-02 12:57:25 +0000525 cb?"Estimating":"Encoding", td->name);
vlmfa67ddc2004-06-03 03:38:44 +0000526
527 /*
vlm0f1ab762004-10-12 05:57:23 +0000528 * Write tags.
vlmfa67ddc2004-06-03 03:38:44 +0000529 */
vlm0f1ab762004-10-12 05:57:23 +0000530 if(type_variant != _TT_ANY || tag_mode == 1) {
531 er.encoded = der_write_tags(td,
532 (type_variant == _TT_BIT_STRING) + st->size,
533 tag_mode, type_variant == _TT_ANY, tag, cb, app_key);
534 if(er.encoded == -1) {
535 er.failed_type = td;
536 er.structure_ptr = sptr;
537 return er;
vlmfa67ddc2004-06-03 03:38:44 +0000538 }
vlm0f1ab762004-10-12 05:57:23 +0000539 } else {
540 /* Disallow: [<tag>] IMPLICIT ANY */
541 assert(type_variant != _TT_ANY || tag_mode != -1);
542 er.encoded = 0;
543 }
544
545 if(!cb) {
546 er.encoded += (type_variant == _TT_BIT_STRING) + st->size;
547 return er;
vlmfa67ddc2004-06-03 03:38:44 +0000548 }
549
vlm267b7fd2004-10-11 11:43:08 +0000550 /*
vlm0f1ab762004-10-12 05:57:23 +0000551 * Prepare to deal with the last octet of BIT STRING.
vlm267b7fd2004-10-11 11:43:08 +0000552 */
vlm0f1ab762004-10-12 05:57:23 +0000553 if(type_variant == _TT_BIT_STRING) {
554 uint8_t b = st->bits_unused & 0x07;
555 if(b && st->size) fix_last_byte = 1;
556 _ASN_CALLBACK(&b, 1);
557 er.encoded++;
vlmfa67ddc2004-06-03 03:38:44 +0000558 }
559
vlm0f1ab762004-10-12 05:57:23 +0000560 /* Invoke callback for the main part of the buffer */
561 _ASN_CALLBACK(st->buf, st->size - fix_last_byte);
vlmfa67ddc2004-06-03 03:38:44 +0000562
vlm0f1ab762004-10-12 05:57:23 +0000563 /* The last octet should be stripped off the unused bits */
564 if(fix_last_byte) {
565 uint8_t b = st->buf[st->size-1] & (0xff << st->bits_unused);
566 _ASN_CALLBACK(&b, 1);
vlmfa67ddc2004-06-03 03:38:44 +0000567 }
568
vlm0f1ab762004-10-12 05:57:23 +0000569 er.encoded += st->size;
570 return er;
571cb_failed:
572 _ASN_ENCODE_FAILED;
vlmfa67ddc2004-06-03 03:38:44 +0000573}
574
vlm39ba4c42004-09-22 16:06:28 +0000575asn_enc_rval_t
vlm7b75d982004-09-29 13:20:14 +0000576OCTET_STRING_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
vlm39ba4c42004-09-22 16:06:28 +0000577 int ilevel, enum xer_encoder_flags_e flags,
578 asn_app_consume_bytes_f *cb, void *app_key) {
579 static const char *h2c = "0123456789ABCDEF";
580 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
581 asn_enc_rval_t er;
582 char scratch[16 * 3 + 4];
583 char *p = scratch;
584 uint8_t *buf;
585 uint8_t *end;
586 size_t i;
587
vlm84d551b2004-10-03 09:13:02 +0000588 if(!st || !st->buf)
589 _ASN_ENCODE_FAILED;
vlm39ba4c42004-09-22 16:06:28 +0000590
591 er.encoded = 0;
592
593 /*
594 * Dump the contents of the buffer in hexadecimal.
595 */
596 buf = st->buf;
597 end = buf + st->size;
598 if(flags & XER_F_CANONICAL) {
599 char *scend = scratch + (sizeof(scratch) - 2);
600 for(; buf < end; buf++) {
601 if(p >= scend) {
602 _ASN_CALLBACK(scratch, p - scratch);
603 er.encoded += p - scratch;
604 p = scratch;
605 }
606 *p++ = h2c[(*buf >> 4) & 0x0F];
607 *p++ = h2c[*buf & 0x0F];
608 }
vlmef1b4c02004-09-23 22:06:26 +0000609
610 _ASN_CALLBACK(scratch, p-scratch); /* Dump the rest */
611 er.encoded += p - scratch;
vlm39ba4c42004-09-22 16:06:28 +0000612 } else {
613 for(i = 0; buf < end; buf++, i++) {
614 if(!(i % 16) && (i || st->size > 16)) {
615 _ASN_CALLBACK(scratch, p-scratch);
616 er.encoded += (p-scratch);
617 p = scratch;
618 _i_ASN_TEXT_INDENT(1, ilevel);
619 }
620 *p++ = h2c[(*buf >> 4) & 0x0F];
621 *p++ = h2c[*buf & 0x0F];
622 *p++ = 0x20;
623 }
vlmef1b4c02004-09-23 22:06:26 +0000624 if(p - scratch) {
625 p--; /* Remove the tail space */
626 _ASN_CALLBACK(scratch, p-scratch); /* Dump the rest */
627 er.encoded += p - scratch;
628 if(st->size > 16)
629 _i_ASN_TEXT_INDENT(1, ilevel-1);
630 }
vlm39ba4c42004-09-22 16:06:28 +0000631 }
632
vlm39ba4c42004-09-22 16:06:28 +0000633 return er;
vlm84d551b2004-10-03 09:13:02 +0000634cb_failed:
635 _ASN_ENCODE_FAILED;
vlm39ba4c42004-09-22 16:06:28 +0000636}
637
vlm393a57a2004-10-03 10:54:25 +0000638static struct OCTET_STRING__xer_escape_table_s {
639 char *string;
640 int size;
641} OCTET_STRING__xer_escape_table[] = {
642#define OSXET(s) { s, sizeof(s) - 1 }
643 OSXET("\074\156\165\154\057\076"), /* <nul/> */
644 OSXET("\074\163\157\150\057\076"), /* <soh/> */
645 OSXET("\074\163\164\170\057\076"), /* <stx/> */
646 OSXET("\074\145\164\170\057\076"), /* <etx/> */
647 OSXET("\074\145\157\164\057\076"), /* <eot/> */
648 OSXET("\074\145\156\161\057\076"), /* <enq/> */
649 OSXET("\074\141\143\153\057\076"), /* <ack/> */
650 OSXET("\074\142\145\154\057\076"), /* <bel/> */
651 OSXET("\074\142\163\057\076"), /* <bs/> */
652 OSXET("\011"), /* \t */
653 OSXET("\012"), /* \n */
654 OSXET("\074\166\164\057\076"), /* <vt/> */
655 OSXET("\074\146\146\057\076"), /* <ff/> */
656 OSXET("\015"), /* \r */
657 OSXET("\074\163\157\057\076"), /* <so/> */
658 OSXET("\074\163\151\057\076"), /* <si/> */
659 OSXET("\074\144\154\145\057\076"), /* <dle/> */
660 OSXET("\074\144\143\061\057\076"), /* <de1/> */
661 OSXET("\074\144\143\062\057\076"), /* <de2/> */
662 OSXET("\074\144\143\063\057\076"), /* <de3/> */
663 OSXET("\074\144\143\064\057\076"), /* <de4/> */
664 OSXET("\074\156\141\153\057\076"), /* <nak/> */
665 OSXET("\074\163\171\156\057\076"), /* <syn/> */
666 OSXET("\074\145\164\142\057\076"), /* <etb/> */
667 OSXET("\074\143\141\156\057\076"), /* <can/> */
668 OSXET("\074\145\155\057\076"), /* <em/> */
669 OSXET("\074\163\165\142\057\076"), /* <sub/> */
670 OSXET("\074\145\163\143\057\076"), /* <esc/> */
671 OSXET("\074\151\163\064\057\076"), /* <is4/> */
672 OSXET("\074\151\163\063\057\076"), /* <is3/> */
673 OSXET("\074\151\163\062\057\076"), /* <is2/> */
674 OSXET("\074\151\163\061\057\076"), /* <is1/> */
675 { 0, 0 }, /* " " */
676 { 0, 0 }, /* ! */
677 { 0, 0 }, /* \" */
678 { 0, 0 }, /* # */
679 { 0, 0 }, /* $ */
680 { 0, 0 }, /* % */
681 OSXET("\046\141\155\160\073"), /* &amp; */
682 { 0, 0 }, /* ' */
683 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, /* ()*+,-./ */
684 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, /* 01234567 */
685 {0,0},{0,0},{0,0},{0,0}, /* 89:; */
686 OSXET("\046\154\164\073"), /* &lt; */
687 { 0, 0 }, /* = */
688 OSXET("\046\147\164\073"), /* &gt; */
689};
690
vlm9de248e2004-10-20 15:50:55 +0000691static int
692OS__check_escaped_control_char(void *buf, int size) {
693 size_t i;
694 /*
695 * Inefficient algorithm which translates the escape sequences
696 * defined above into characters. Returns -1 if not found.
697 * TODO: replace by a faster algorithm (bsearch(), hash or
698 * nested table lookups).
699 */
700 for(i = 0; i < 32 /* Don't spend time on the bottom half */; i++) {
701 struct OCTET_STRING__xer_escape_table_s *el;
702 el = &OCTET_STRING__xer_escape_table[i];
703 if(el->size == size && memcmp(buf, el->string, size) == 0)
704 return i;
705 }
706 return -1;
707}
708
709static int
710OCTET_STRING__handle_control_chars(void *struct_ptr, void *chunk_buf, size_t chunk_size) {
711 /*
712 * This might be one of the escape sequences
713 * for control characters. Check it out.
714 * #11.15.5
715 */
716 int control_char = OS__check_escaped_control_char(chunk_buf,chunk_size);
717 if(control_char >= 0) {
718 OCTET_STRING_t *st = (OCTET_STRING_t *)struct_ptr;
719 void *p = REALLOC(st->buf, st->size + 2);
720 if(p) {
721 st->buf = (uint8_t *)p;
722 st->buf[st->size++] = control_char;
723 st->buf[st->size] = '\0'; /* nul-termination */
724 return 0;
725 }
726 }
727
728 return -1; /* No, it's not */
729}
730
vlm39ba4c42004-09-22 16:06:28 +0000731asn_enc_rval_t
vlm9de248e2004-10-20 15:50:55 +0000732OCTET_STRING_encode_xer_utf8(asn_TYPE_descriptor_t *td, void *sptr,
vlm39ba4c42004-09-22 16:06:28 +0000733 int ilevel, enum xer_encoder_flags_e flags,
734 asn_app_consume_bytes_f *cb, void *app_key) {
735 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
736 asn_enc_rval_t er;
vlm393a57a2004-10-03 10:54:25 +0000737 uint8_t *buf, *end;
738 uint8_t *ss; /* Sequence start */
739 ssize_t encoded_len = 0;
vlm39ba4c42004-09-22 16:06:28 +0000740
741 (void)ilevel; /* Unused argument */
742 (void)flags; /* Unused argument */
743
744 if(!st || !st->buf)
745 _ASN_ENCODE_FAILED;
746
vlm393a57a2004-10-03 10:54:25 +0000747 buf = st->buf;
748 end = buf + st->size;
749 for(ss = buf; buf < end; buf++) {
vlmd7c2ef32004-10-05 06:35:31 +0000750 unsigned int ch = *buf;
vlm393a57a2004-10-03 10:54:25 +0000751 int s_len; /* Special encoding sequence length */
vlm39ba4c42004-09-22 16:06:28 +0000752
vlm393a57a2004-10-03 10:54:25 +0000753 /*
754 * Escape certain characters: X.680/11.15
755 */
756 if(ch < sizeof(OCTET_STRING__xer_escape_table)
757 /sizeof(OCTET_STRING__xer_escape_table[0])
758 && (s_len = OCTET_STRING__xer_escape_table[ch].size)) {
759 if(((buf - ss) && cb(ss, buf - ss, app_key) < 0)
760 || cb(OCTET_STRING__xer_escape_table[ch].string, s_len,
761 app_key) < 0)
762 _ASN_ENCODE_FAILED;
763 encoded_len += (buf - ss) + s_len;
764 ss = buf + 1;
765 }
766 }
767
768 encoded_len += (buf - ss);
769 if((buf - ss) && cb(ss, buf - ss, app_key) < 0)
770 _ASN_ENCODE_FAILED;
771
772 er.encoded = encoded_len;
vlm39ba4c42004-09-22 16:06:28 +0000773 return er;
774}
775
vlm9de248e2004-10-20 15:50:55 +0000776/*
777 * Convert from hexadecimal format (cstring): "AB CD EF"
778 */
779static ssize_t OCTET_STRING__convert_hexadecimal(void *sptr, void *chunk_buf, size_t chunk_size, int have_more) {
780 OCTET_STRING_t *st = (OCTET_STRING_t *)sptr;
781 char *chunk_stop = (char *)chunk_buf;
782 char *p = chunk_stop;
783 char *pend = p + chunk_size;
784 unsigned int clv = 0;
785 int half = 0; /* Half bit */
786 uint8_t *buf;
787
788 /* Reallocate buffer according to high cap estimation */
789 ssize_t _ns = st->size + (chunk_size + 1) / 2;
790 void *nptr = REALLOC(st->buf, _ns + 1);
791 if(!nptr) return -1;
792 st->buf = (uint8_t *)nptr;
793 buf = st->buf + st->size;
794
795 /*
796 * If something like " a b c " appears here, the " a b":3 will be
797 * converted, and the rest skipped. That is, unless buf_size is greater
798 * than chunk_size, then it'll be equivalent to "ABC0".
799 */
800 for(; p < pend; p++) {
801 int ch = *(unsigned char *)p;
802 switch(ch) {
803 case 0x09: case 0x0a: case 0x0c: case 0x0d:
804 case 0x20:
805 /* Ignore whitespace */
806 continue;
807 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: /*01234*/
808 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: /*56789*/
809 clv = (clv << 4) + (ch - 0x30);
810 break;
811 case 0x41: case 0x42: case 0x43: /* ABC */
812 case 0x44: case 0x45: case 0x46: /* DEF */
813 clv = (clv << 4) + (ch - (0x41 + 10));
814 break;
815 case 0x61: case 0x62: case 0x63: /* abc */
816 case 0x64: case 0x65: case 0x66: /* def */
817 clv = (clv << 4) + (ch - (0x61 + 10));
818 break;
819 default:
820 *buf = 0; /* JIC */
821 return -1;
822 }
823 if(half++) {
824 half = 0;
825 *buf++ = clv;
826 chunk_stop = p + 1;
827 }
828 }
829
830 /*
831 * Check partial decoding.
832 */
833 if(half) {
834 if(have_more) {
835 /*
836 * Partial specification is fine,
837 * because no more more PXER_TEXT data is available.
838 */
839 *buf++ = clv << 4;
840 chunk_stop = p;
841 }
842 } else {
843 chunk_stop = p;
844 }
845
846 st->size = buf - st->buf; /* Adjust the buffer size */
847 assert(st->size <= _ns);
848 st->buf[st->size] = 0; /* Courtesy termination */
849
850 return (chunk_stop - (char *)chunk_buf); /* Converted size */
851}
852
853/*
854 * Convert from binary format: "00101011101"
855 */
856static ssize_t OCTET_STRING__convert_binary(void *sptr, void *chunk_buf, size_t chunk_size, int have_more) {
857 BIT_STRING_t *st = (BIT_STRING_t *)sptr;
858 char *p = (char *)chunk_buf;
859 char *pend = p + chunk_size;
860 int bits_unused = st->bits_unused & 0x7;
861 uint8_t *buf;
862
863 /* Reallocate buffer according to high cap estimation */
864 ssize_t _ns = st->size + (chunk_size + 7) / 8;
865 void *nptr = REALLOC(st->buf, _ns + 1);
866 if(!nptr) return -1;
867 st->buf = (uint8_t *)nptr;
868 buf = st->buf + st->size;
869
870 (void)have_more;
871
872 if(bits_unused == 0)
873 bits_unused = 8;
874 else if(st->size)
875 buf--;
876
877 /*
878 * Convert series of 0 and 1 into the octet string.
879 */
880 for(; p < pend; p++) {
881 int ch = *(unsigned char *)p;
882 switch(ch) {
883 case 0x09: case 0x0a: case 0x0c: case 0x0d:
884 case 0x20:
885 /* Ignore whitespace */
886 break;
887 case 0x30:
888 case 0x31:
889 if(bits_unused-- <= 0) {
890 *++buf = 0; /* Clean the cell */
891 bits_unused = 7;
892 }
893 *buf |= (ch&1) << bits_unused;
894 break;
895 default:
896 st->bits_unused = bits_unused;
897 return -1;
898 }
899 }
900
901 if(bits_unused == 8) {
902 st->size = buf - st->buf;
903 st->bits_unused = 0;
904 } else {
905 st->size = buf - st->buf + 1;
906 st->bits_unused = bits_unused;
907 }
908
909 assert(st->size <= _ns);
910 st->buf[st->size] = 0; /* Courtesy termination */
911
912 return chunk_size; /* Converted in full */
913}
914
915/*
916 * Something like strtod(), but with stricter rules.
917 */
918static int
vlm6c593842004-10-26 09:03:31 +0000919OS__strtoent(int base, char *buf, char *end, int32_t *return_value) {
920 int32_t val = 0;
vlm9de248e2004-10-20 15:50:55 +0000921 char *p;
922
923 for(p = buf; p < end; p++) {
924 int ch = *p;
925 if((val * base + base) < 0) return -1; /* Strange huge value */
926 switch(ch) {
927 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: /*01234*/
928 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: /*56789*/
929 val = val * base + (ch - 0x30);
930 break;
931 case 0x41: case 0x42: case 0x43: /* ABC */
932 case 0x44: case 0x45: case 0x46: /* DEF */
933 val = val * base + (ch - (0x41 + 10));
934 break;
935 case 0x61: case 0x62: case 0x63: /* abc */
936 case 0x64: case 0x65: case 0x66: /* def */
937 val = val * base + (ch - (0x61 + 10));
938 break;
939 case 0x3b: /* ';' */
940 *return_value = val;
941 return (p - buf) + 1;
942 default:
943 return -1; /* Character set error */
944 }
945 }
946
947 /* Do not return value. It's an error we're talking about here. */
948 return (p - buf);
949}
950
951/*
952 * Convert from the plain UTF-8 format, expanding entity references: "2 &lt; 3"
953 */
954static ssize_t OCTET_STRING__convert_entrefs(void *sptr, void *chunk_buf, size_t chunk_size, int have_more) {
955 OCTET_STRING_t *st = (OCTET_STRING_t *)sptr;
956 char *p = (char *)chunk_buf;
957 char *pend = p + chunk_size;
958 uint8_t *buf;
959
960 /* Reallocate buffer */
961 ssize_t _ns = st->size + chunk_size;
962 void *nptr = REALLOC(st->buf, _ns + 1);
963 if(!nptr) return -1;
964 st->buf = (uint8_t *)nptr;
965 buf = st->buf + st->size;
966
967 /*
968 * Convert series of 0 and 1 into the octet string.
969 */
970 for(; p < pend; p++) {
971 int ch = *(unsigned char *)p;
972 int len; /* Length of the rest of the chunk */
973
974 if(ch != 0x26 /* '&' */) {
975 *buf++ = ch;
976 continue; /* That was easy... */
977 }
978
979 /*
980 * Process entity reference.
981 */
982 len = chunk_size - (p - (char *)chunk_buf);
983 if(len == 1 /* "&" */) goto want_more;
984 if(p[1] == 0x23 /* '#' */) {
985 char *pval; /* Pointer to start of digits */
vlm6c593842004-10-26 09:03:31 +0000986 int32_t val; /* Entity reference value */
vlm9de248e2004-10-20 15:50:55 +0000987 int base;
988
989 if(len == 2 /* "&#" */) goto want_more;
990 if(p[2] == 0x78 /* 'x' */)
991 pval = p + 3, base = 16;
992 else
993 pval = p + 2, base = 10;
994 len = OS__strtoent(base, pval, p + len, &val);
995 if(len == -1) {
996 /* Invalid charset. Just copy verbatim. */
997 *buf++ = ch;
998 continue;
999 }
1000 if(!len || pval[len-1] != 0x3b) goto want_more;
1001 assert(val > 0);
1002 p += (pval - p) + len - 1; /* Advance past entref */
1003
1004 if(val < 0x80) {
1005 *buf++ = (char)val;
1006 } else if(val < 0x800) {
1007 *buf++ = 0xc0 | ((val >> 6));
1008 *buf++ = 0x80 | ((val & 0x3f));
1009 } else if(val < 0x10000) {
1010 *buf++ = 0xe0 | ((val >> 12));
1011 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1012 *buf++ = 0x80 | ((val & 0x3f));
1013 } else if(val < 0x200000) {
1014 *buf++ = 0xf0 | ((val >> 18));
1015 *buf++ = 0x80 | ((val >> 12) & 0x3f);
1016 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1017 *buf++ = 0x80 | ((val & 0x3f));
1018 } else if(val < 0x4000000) {
1019 *buf++ = 0xf8 | ((val >> 24));
1020 *buf++ = 0x80 | ((val >> 18) & 0x3f);
1021 *buf++ = 0x80 | ((val >> 12) & 0x3f);
1022 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1023 *buf++ = 0x80 | ((val & 0x3f));
1024 } else {
1025 *buf++ = 0xfc | ((val >> 30) & 0x1);
1026 *buf++ = 0x80 | ((val >> 24) & 0x3f);
1027 *buf++ = 0x80 | ((val >> 18) & 0x3f);
1028 *buf++ = 0x80 | ((val >> 12) & 0x3f);
1029 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1030 *buf++ = 0x80 | ((val & 0x3f));
1031 }
1032 } else {
1033 /*
1034 * Ugly, limited parsing of &amp; &gt; &lt;
1035 */
1036 char *sc = (char *)memchr(p, 0x3b, len > 5 ? 5 : len);
1037 if(!sc) goto want_more;
1038 if((sc - p) == 4
1039 && p[1] == 0x61 /* 'a' */
1040 && p[2] == 0x6d /* 'm' */
1041 && p[3] == 0x70 /* 'p' */) {
1042 *buf++ = 0x26;
1043 p = sc;
1044 continue;
1045 }
1046 if((sc - p) == 3) {
1047 if(p[1] == 0x6c) {
1048 *buf = 0x3c; /* '<' */
1049 } else if(p[1] == 0x67) {
1050 *buf = 0x3e; /* '>' */
1051 } else {
1052 /* Unsupported entity reference */
1053 *buf++ = ch;
1054 continue;
1055 }
1056 if(p[2] != 0x74) {
1057 /* Unsupported entity reference */
1058 *buf++ = ch;
1059 continue;
1060 }
1061 buf++;
1062 p = sc;
1063 continue;
1064 }
1065 /* Unsupported entity reference */
1066 *buf++ = ch;
1067 }
1068
1069 continue;
1070 want_more:
1071 if(have_more) {
1072 /*
1073 * We know that no more data (of the same type)
1074 * is coming. Copy the rest verbatim.
1075 */
1076 *buf++ = ch;
1077 continue;
1078 }
1079 *buf = 0; /* JIC */
1080 /* Processing stalled: need more data */
1081 return (p - (char *)chunk_buf);
1082 }
1083
1084 st->size = buf - st->buf;
1085 assert(st->size <= _ns);
1086 st->buf[st->size] = 0; /* Courtesy termination */
1087
1088 return chunk_size; /* Converted in full */
1089}
1090
1091/*
1092 * Decode OCTET STRING from the XML element's body.
1093 */
1094static asn_dec_rval_t
1095OCTET_STRING__decode_xer(asn_codec_ctx_t *opt_codec_ctx,
1096 asn_TYPE_descriptor_t *td, void **sptr,
1097 const char *opt_mname, void *buf_ptr, size_t size,
1098 int (*opt_unexpected_tag_decoder)
1099 (void *struct_ptr, void *chunk_buf, size_t chunk_size),
1100 ssize_t (*body_receiver)
1101 (void *struct_ptr, void *chunk_buf, size_t chunk_size,
1102 int have_more)
1103) {
1104 asn_OCTET_STRING_specifics_t *specs = td->specifics
1105 ? (asn_OCTET_STRING_specifics_t *)td->specifics
1106 : &asn_DEF_OCTET_STRING_specs;
1107 const char *xml_tag = opt_mname ? opt_mname : td->xml_tag;
1108 asn_struct_ctx_t *ctx; /* Per-structure parser context */
1109
1110 /*
1111 * Create the string if does not exist.
1112 */
1113 if(!*sptr) {
vlma56a9e82004-10-23 10:20:27 +00001114 OCTET_STRING_t *st;
1115 (void *)st = *sptr = CALLOC(1, specs->struct_size);
1116 if(st) st->buf = (uint8_t *)CALLOC(1, 1);
1117 if(!*sptr || !st->buf) {
vlm9de248e2004-10-20 15:50:55 +00001118 asn_dec_rval_t rval;
vlma56a9e82004-10-23 10:20:27 +00001119 if(*sptr) FREEMEM(*sptr);
vlm9de248e2004-10-20 15:50:55 +00001120 rval.code = RC_FAIL;
1121 rval.consumed = 0;
1122 return rval;
1123 }
1124 }
1125
1126 /* Restore parsing context */
1127 ctx = (asn_struct_ctx_t *)(((char *)*sptr) + specs->ctx_offset);
1128
1129 return xer_decode_general(opt_codec_ctx, ctx, *sptr, xml_tag,
1130 buf_ptr, size, opt_unexpected_tag_decoder, body_receiver);
1131}
1132
1133/*
1134 * Decode OCTET STRING from the hexadecimal data.
1135 */
1136asn_dec_rval_t
1137OCTET_STRING_decode_xer_hex(asn_codec_ctx_t *opt_codec_ctx,
1138 asn_TYPE_descriptor_t *td, void **sptr,
1139 const char *opt_mname, void *buf_ptr, size_t size) {
1140 return OCTET_STRING__decode_xer(opt_codec_ctx, td, sptr, opt_mname,
1141 buf_ptr, size, 0, OCTET_STRING__convert_hexadecimal);
1142}
1143
1144/*
1145 * Decode OCTET STRING from the binary (0/1) data.
1146 */
1147asn_dec_rval_t
1148OCTET_STRING_decode_xer_binary(asn_codec_ctx_t *opt_codec_ctx,
1149 asn_TYPE_descriptor_t *td, void **sptr,
1150 const char *opt_mname, void *buf_ptr, size_t size) {
1151 return OCTET_STRING__decode_xer(opt_codec_ctx, td, sptr, opt_mname,
1152 buf_ptr, size, 0, OCTET_STRING__convert_binary);
1153}
1154
1155/*
1156 * Decode OCTET STRING from the string (ASCII/UTF-8) data.
1157 */
1158asn_dec_rval_t
1159OCTET_STRING_decode_xer_utf8(asn_codec_ctx_t *opt_codec_ctx,
1160 asn_TYPE_descriptor_t *td, void **sptr,
1161 const char *opt_mname, void *buf_ptr, size_t size) {
1162 return OCTET_STRING__decode_xer(opt_codec_ctx, td, sptr, opt_mname,
1163 buf_ptr, size,
1164 OCTET_STRING__handle_control_chars,
1165 OCTET_STRING__convert_entrefs);
1166}
1167
1168
vlmfa67ddc2004-06-03 03:38:44 +00001169int
vlm7b75d982004-09-29 13:20:14 +00001170OCTET_STRING_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
vlmfa67ddc2004-06-03 03:38:44 +00001171 asn_app_consume_bytes_f *cb, void *app_key) {
vlm1ff928d2004-08-11 08:10:13 +00001172 static const char *h2c = "0123456789ABCDEF";
vlmda674682004-08-11 09:07:36 +00001173 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
vlmfa67ddc2004-06-03 03:38:44 +00001174 char scratch[16 * 3 + 4];
1175 char *p = scratch;
1176 uint8_t *buf;
1177 uint8_t *end;
1178 size_t i;
vlmfa67ddc2004-06-03 03:38:44 +00001179
vlmb42843a2004-06-05 08:17:50 +00001180 (void)td; /* Unused argument */
1181
vlm6678cb12004-09-26 13:10:40 +00001182 if(!st || !st->buf) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
vlmfa67ddc2004-06-03 03:38:44 +00001183
1184 /*
1185 * Dump the contents of the buffer in hexadecimal.
1186 */
1187 buf = st->buf;
1188 end = buf + st->size;
1189 for(i = 0; buf < end; buf++, i++) {
1190 if(!(i % 16) && (i || st->size > 16)) {
vlm6678cb12004-09-26 13:10:40 +00001191 if(cb(scratch, p - scratch, app_key) < 0)
vlmfa67ddc2004-06-03 03:38:44 +00001192 return -1;
vlm6678cb12004-09-26 13:10:40 +00001193 _i_INDENT(1);
vlmfa67ddc2004-06-03 03:38:44 +00001194 p = scratch;
1195 }
1196 *p++ = h2c[(*buf >> 4) & 0x0F];
1197 *p++ = h2c[*buf & 0x0F];
vlm39ba4c42004-09-22 16:06:28 +00001198 *p++ = 0x20;
vlmfa67ddc2004-06-03 03:38:44 +00001199 }
1200
vlmef1b4c02004-09-23 22:06:26 +00001201 if(p > scratch) {
1202 p--; /* Remove the tail space */
vlm6678cb12004-09-26 13:10:40 +00001203 if(cb(scratch, p - scratch, app_key) < 0)
vlmef1b4c02004-09-23 22:06:26 +00001204 return -1;
1205 }
1206
1207 return 0;
vlmfa67ddc2004-06-03 03:38:44 +00001208}
1209
1210int
vlm9de248e2004-10-20 15:50:55 +00001211OCTET_STRING_print_utf8(asn_TYPE_descriptor_t *td, const void *sptr,
vlmfa67ddc2004-06-03 03:38:44 +00001212 int ilevel, asn_app_consume_bytes_f *cb, void *app_key) {
vlmda674682004-08-11 09:07:36 +00001213 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
vlmfa67ddc2004-06-03 03:38:44 +00001214
vlmb42843a2004-06-05 08:17:50 +00001215 (void)td; /* Unused argument */
1216 (void)ilevel; /* Unused argument */
1217
vlmfa67ddc2004-06-03 03:38:44 +00001218 if(st && st->buf) {
vlm6678cb12004-09-26 13:10:40 +00001219 return (cb(st->buf, st->size, app_key) < 0) ? -1 : 0;
vlmfa67ddc2004-06-03 03:38:44 +00001220 } else {
vlm6678cb12004-09-26 13:10:40 +00001221 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
vlmfa67ddc2004-06-03 03:38:44 +00001222 }
1223}
1224
1225void
vlm7b75d982004-09-29 13:20:14 +00001226OCTET_STRING_free(asn_TYPE_descriptor_t *td, void *sptr, int contents_only) {
vlmda674682004-08-11 09:07:36 +00001227 OCTET_STRING_t *st = (OCTET_STRING_t *)sptr;
vlm0f1ab762004-10-12 05:57:23 +00001228 asn_OCTET_STRING_specifics_t *specs = td->specifics
vlm9de248e2004-10-20 15:50:55 +00001229 ? (asn_OCTET_STRING_specifics_t *)td->specifics
1230 : &asn_DEF_OCTET_STRING_specs;
vlm0f1ab762004-10-12 05:57:23 +00001231 asn_struct_ctx_t *ctx = (asn_struct_ctx_t *)
1232 ((char *)st + specs->ctx_offset);
vlm9de248e2004-10-20 15:50:55 +00001233 struct _stack *stck = (struct _stack *)ctx->ptr;
vlmfa67ddc2004-06-03 03:38:44 +00001234
1235 if(!td || !st)
1236 return;
1237
1238 ASN_DEBUG("Freeing %s as OCTET STRING", td->name);
1239
1240 if(st->buf) {
1241 FREEMEM(st->buf);
1242 }
1243
1244 /*
1245 * Remove decode-time stack.
1246 */
1247 if(stck) {
1248 while(stck->tail) {
1249 struct _stack_el *sel = stck->tail;
1250 stck->tail = sel->prev;
1251 FREEMEM(sel);
1252 }
1253 FREEMEM(stck);
1254 }
1255
1256 if(!contents_only) {
1257 FREEMEM(st);
1258 }
1259}
1260
1261/*
1262 * Conversion routines.
1263 */
1264int
1265OCTET_STRING_fromBuf(OCTET_STRING_t *st, const char *str, int len) {
1266 void *buf;
1267
1268 if(st == 0 || (str == 0 && len)) {
1269 errno = EINVAL;
1270 return -1;
1271 }
1272
1273 /*
1274 * Clear the OCTET STRING.
1275 */
1276 if(str == NULL) {
1277 if(st->buf)
1278 FREEMEM(st->buf);
1279 st->size = 0;
1280 return 0;
1281 }
1282
1283 /* Determine the original string size, if not explicitly given */
1284 if(len < 0)
1285 len = strlen(str);
1286
1287 /* Allocate and fill the memory */
1288 buf = MALLOC(len + 1);
1289 if(buf == NULL) {
1290 return -1;
1291 } else {
vlmda674682004-08-11 09:07:36 +00001292 st->buf = (uint8_t *)buf;
vlmfa67ddc2004-06-03 03:38:44 +00001293 st->size = len;
1294 }
1295
1296 memcpy(buf, str, len);
1297 st->buf[st->size] = '\0'; /* Couldn't use memcpy(len+1)! */
1298
1299 return 0;
1300}
1301
1302OCTET_STRING_t *
vlm0f1ab762004-10-12 05:57:23 +00001303OCTET_STRING_new_fromBuf(asn_TYPE_descriptor_t *td, const char *str, int len) {
1304 asn_OCTET_STRING_specifics_t *specs = td->specifics
vlm9de248e2004-10-20 15:50:55 +00001305 ? (asn_OCTET_STRING_specifics_t *)td->specifics
1306 : &asn_DEF_OCTET_STRING_specs;
vlmfa67ddc2004-06-03 03:38:44 +00001307 OCTET_STRING_t *st;
1308
vlm0f1ab762004-10-12 05:57:23 +00001309 st = (OCTET_STRING_t *)CALLOC(1, specs->struct_size);
vlmfa67ddc2004-06-03 03:38:44 +00001310 if(st && str && OCTET_STRING_fromBuf(st, str, len)) {
1311 free(st);
1312 st = NULL;
1313 }
1314
1315 return st;
1316}
1317