blob: b44228c44653561bcaefe148f0843223e541e2fb [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
vlmdb6b2902004-09-27 20:54:06 +0000249 ASN_DEBUG("%p, s->l=%d, s->wn=%d, s->g=%d\n", sel,
250 sel?sel->left:0,
251 sel?sel->want_nulls:0,
252 sel?sel->got:0
253 );
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);
vlmdb6b2902004-09-27 20:54:06 +0000274 ASN_DEBUG("fetch tag(size=%d,L=%d), %sstack, left=%d, wn=%d, tl=%d",
275 (int)size, Left, sel?"":"!",
276 sel?sel->left:0, sel?sel->want_nulls:0, tl);
vlmfa67ddc2004-06-03 03:38:44 +0000277 switch(tl) {
278 case -1: RETURN(RC_FAIL);
279 case 0: RETURN(RC_WMORE);
280 }
281
282 tlv_constr = BER_TLV_CONSTRUCTED(buf_ptr);
283
284 ll = ber_fetch_length(tlv_constr,
vlmb33ed682004-09-24 20:57:41 +0000285 (char *)buf_ptr + tl, Left - tl, &tlv_len);
vlmdb6b2902004-09-27 20:54:06 +0000286 ASN_DEBUG("Got tag=%s, tc=%d, left=%ld, tl=%d, len=%d, ll=%d",
vlmcdfdb722004-09-04 04:44:50 +0000287 ber_tlv_tag_string(tlv_tag), tlv_constr,
vlmdb6b2902004-09-27 20:54:06 +0000288 (long)Left, tl, tlv_len, ll);
vlmfa67ddc2004-06-03 03:38:44 +0000289 switch(ll) {
290 case -1: RETURN(RC_FAIL);
291 case 0: RETURN(RC_WMORE);
292 }
293
vlmcdfdb722004-09-04 04:44:50 +0000294 if(sel && sel->want_nulls
vlmfa67ddc2004-06-03 03:38:44 +0000295 && ((uint8_t *)buf_ptr)[0] == 0
296 && ((uint8_t *)buf_ptr)[1] == 0)
297 {
vlmcdfdb722004-09-04 04:44:50 +0000298
299 ASN_DEBUG("Eat EOC; wn=%d--", sel->want_nulls);
300
vlm0f1ab762004-10-12 05:57:23 +0000301 if(type_variant == _TT_ANY
vlm267b7fd2004-10-11 11:43:08 +0000302 && (tag_mode != 1 || sel->cont_level))
303 APPEND("\0\0", 2);
vlmdb6b2902004-09-27 20:54:06 +0000304
305 ADVANCE(2);
306 sel->got += 2;
307 if(sel->left != -1) {
308 sel->left -= 2; /* assert(sel->left >= 2) */
309 }
310
vlmfa67ddc2004-06-03 03:38:44 +0000311 sel->want_nulls--;
312 if(sel->want_nulls == 0) {
313 /* Move to the next expectation */
vlmdb6b2902004-09-27 20:54:06 +0000314 sel->left = 0;
vlmfa67ddc2004-06-03 03:38:44 +0000315 tlv_constr = 1;
vlmfa67ddc2004-06-03 03:38:44 +0000316 }
vlmcdfdb722004-09-04 04:44:50 +0000317
318 continue;
vlm72425de2004-09-13 08:31:01 +0000319 }
320
321 /*
322 * Set up expected tags,
323 * depending on ASN.1 type being decoded.
324 */
vlm0f1ab762004-10-12 05:57:23 +0000325 switch(type_variant) {
vlm72425de2004-09-13 08:31:01 +0000326 case _TT_BIT_STRING:
327 /* X.690: 8.6.4.1, NOTE 2 */
328 /* Fall through */
329 case _TT_GENERIC:
330 default:
331 if(sel) {
332 int level = sel->cont_level;
333 if(level < td->all_tags_count) {
334 expected_tag = td->all_tags[level];
335 break;
336 } else if(td->all_tags_count) {
337 expected_tag = td->all_tags
338 [td->all_tags_count - 1];
339 break;
340 }
341 /* else, Fall through */
342 }
343 /* Fall through */
344 case _TT_ANY:
345 expected_tag = tlv_tag;
346 break;
347 }
348
349
350 if(tlv_tag != expected_tag) {
vlmfa67ddc2004-06-03 03:38:44 +0000351 char buf[2][32];
352 ber_tlv_tag_snprint(tlv_tag,
353 buf[0], sizeof(buf[0]));
354 ber_tlv_tag_snprint(td->tags[td->tags_count-1],
355 buf[1], sizeof(buf[1]));
356 ASN_DEBUG("Tag does not match expectation: %s != %s",
357 buf[0], buf[1]);
358 RETURN(RC_FAIL);
359 }
360
vlm7b75d982004-09-29 13:20:14 +0000361 tlvl = tl + ll; /* Combined length of T and L encoding */
362 if((tlv_len + tlvl) < 0) {
363 /* tlv_len value is too big */
364 ASN_DEBUG("TLV encoding + length (%ld) is too big",
365 (long)tlv_len);
366 RETURN(RC_FAIL);
367 }
368
vlmfa67ddc2004-06-03 03:38:44 +0000369 /*
370 * Append a new expectation.
371 */
vlmb33ed682004-09-24 20:57:41 +0000372 sel = OS__add_stack_el(stck);
vlmdb6b2902004-09-27 20:54:06 +0000373 if(!sel) RETURN(RC_FAIL);
vlmfa67ddc2004-06-03 03:38:44 +0000374
vlmdb6b2902004-09-27 20:54:06 +0000375 sel->tag = tlv_tag;
376
377 sel->want_nulls = (tlv_len==-1);
378 if(sel->prev && sel->prev->left != -1) {
379 /* Check that the parent frame is big enough */
380 if(sel->prev->left < tlvl + (tlv_len==-1?0:tlv_len))
381 RETURN(RC_FAIL);
382 if(tlv_len == -1)
383 sel->left = sel->prev->left - tlvl;
384 else
385 sel->left = tlv_len;
386 } else {
387 sel->left = tlv_len;
388 }
vlm0f1ab762004-10-12 05:57:23 +0000389 if(type_variant == _TT_ANY
vlm267b7fd2004-10-11 11:43:08 +0000390 && (tag_mode != 1 || sel->cont_level))
391 APPEND(buf_ptr, tlvl);
vlmdb6b2902004-09-27 20:54:06 +0000392 sel->got += tlvl;
393 ADVANCE(tlvl);
394
395 ASN_DEBUG("+EXPECT2 got=%d left=%d, wn=%d, clvl=%d",
396 sel->got, sel->left, sel->want_nulls, sel->cont_level);
397
vlmfa67ddc2004-06-03 03:38:44 +0000398 } while(tlv_constr);
399 if(sel == NULL) {
400 /* Finished operation, "phase out" */
vlm72425de2004-09-13 08:31:01 +0000401 ASN_DEBUG("Phase out");
vlmfa67ddc2004-06-03 03:38:44 +0000402 _CH_PHASE(ctx, +3);
403 break;
404 }
405
406 NEXT_PHASE(ctx);
407 /* Fall through */
408 case 2:
vlmda674682004-08-11 09:07:36 +0000409 (void *)stck = ctx->ptr;
vlmfa67ddc2004-06-03 03:38:44 +0000410 sel = stck->cur_ptr;
vlmdb6b2902004-09-27 20:54:06 +0000411 ASN_DEBUG("Phase 2: Need %ld bytes, size=%ld, alrg=%ld, wn=%d",
412 (long)sel->left, (long)size, (long)sel->got,
413 sel->want_nulls);
vlmfa67ddc2004-06-03 03:38:44 +0000414 {
415 ber_tlv_len_t len;
416
417 assert(sel->left >= 0);
418
vlmc5190612004-08-18 04:53:32 +0000419 len = ((ber_tlv_len_t)size < sel->left)
420 ? (ber_tlv_len_t)size : sel->left;
vlmfa67ddc2004-06-03 03:38:44 +0000421 if(len > 0) {
vlm0f1ab762004-10-12 05:57:23 +0000422 if(type_variant == _TT_BIT_STRING
vlm72425de2004-09-13 08:31:01 +0000423 && sel->bits_chopped == 0) {
vlm0f1ab762004-10-12 05:57:23 +0000424 /* Put the unused-bits-octet away */
425 st->bits_unused = *(uint8_t *)buf_ptr;
vlm1ff928d2004-08-11 08:10:13 +0000426 APPEND(((char *)buf_ptr+1), (len - 1));
vlmfa67ddc2004-06-03 03:38:44 +0000427 sel->bits_chopped = 1;
428 } else {
429 APPEND(buf_ptr, len);
430 }
431 ADVANCE(len);
432 sel->left -= len;
vlmdb6b2902004-09-27 20:54:06 +0000433 sel->got += len;
vlmfa67ddc2004-06-03 03:38:44 +0000434 }
435
vlmdb6b2902004-09-27 20:54:06 +0000436 if(sel->left) {
437 ASN_DEBUG("OS left %ld, size = %ld, wn=%d\n",
438 (long)sel->left, (long)size, sel->want_nulls);
vlmfa67ddc2004-06-03 03:38:44 +0000439 RETURN(RC_WMORE);
vlmfa67ddc2004-06-03 03:38:44 +0000440 }
vlmdb6b2902004-09-27 20:54:06 +0000441
442 PREV_PHASE(ctx);
443 goto phase1;
vlmfa67ddc2004-06-03 03:38:44 +0000444 }
445 break;
446 case 3:
447 phase3:
448 /*
449 * Primitive form, no stack required.
450 */
vlm0f1ab762004-10-12 05:57:23 +0000451 assert(ctx->left >= 0);
452
vlmb42843a2004-06-05 08:17:50 +0000453 if(size < (size_t)ctx->left) {
vlm0f1ab762004-10-12 05:57:23 +0000454 if(!size) RETURN(RC_WMORE);
455 if(type_variant == _TT_BIT_STRING && ctx->step == 0) {
456 st->bits_unused = *(uint8_t *)buf_ptr;
457 ctx->left--;
458 ADVANCE(1);
459 }
vlmfa67ddc2004-06-03 03:38:44 +0000460 APPEND(buf_ptr, size);
vlm0f1ab762004-10-12 05:57:23 +0000461 assert(ctx->step);
vlmfa67ddc2004-06-03 03:38:44 +0000462 ctx->left -= size;
463 ADVANCE(size);
464 RETURN(RC_WMORE);
465 } else {
vlm0f1ab762004-10-12 05:57:23 +0000466 if(type_variant == _TT_BIT_STRING
467 && ctx->step == 0 && ctx->left) {
468 st->bits_unused = *(uint8_t *)buf_ptr;
469 ctx->left--;
470 ADVANCE(1);
471 }
vlmfa67ddc2004-06-03 03:38:44 +0000472 APPEND(buf_ptr, ctx->left);
473 ADVANCE(ctx->left);
474 ctx->left = 0;
475
476 NEXT_PHASE(ctx);
477 }
478 break;
479 }
480
vlmdb6b2902004-09-27 20:54:06 +0000481 if(sel) {
482 ASN_DEBUG("3sel p=%p, wn=%d, l=%ld, g=%ld, size=%ld",
483 sel->prev, sel->want_nulls,
484 (long)sel->left, (long)sel->got, (long)size);
485 if(sel->prev || sel->want_nulls > 1 || sel->left > 0) {
486 RETURN(RC_WMORE);
487 }
488 }
489
vlmfa67ddc2004-06-03 03:38:44 +0000490 /*
491 * BIT STRING-specific processing.
492 */
vlm0f1ab762004-10-12 05:57:23 +0000493 if(type_variant == _TT_BIT_STRING && st->size) {
vlmfa67ddc2004-06-03 03:38:44 +0000494 /* Finalize BIT STRING: zero out unused bits. */
vlm0f1ab762004-10-12 05:57:23 +0000495 st->buf[st->size-1] &= 0xff << st->bits_unused;
vlmfa67ddc2004-06-03 03:38:44 +0000496 }
497
498 ASN_DEBUG("Took %d bytes to encode %s: [%s]:%d",
vlmb33ed682004-09-24 20:57:41 +0000499 consumed_myself, td->name,
vlm0f1ab762004-10-12 05:57:23 +0000500 (type_variant == _TT_GENERIC) ? (char *)st->buf : "<data>",
501 st->size);
vlmfa67ddc2004-06-03 03:38:44 +0000502
vlmfa67ddc2004-06-03 03:38:44 +0000503
vlmdb6b2902004-09-27 20:54:06 +0000504 RETURN(RC_OK);
vlmfa67ddc2004-06-03 03:38:44 +0000505}
506
507/*
508 * Encode OCTET STRING type using DER.
509 */
vlm39ba4c42004-09-22 16:06:28 +0000510asn_enc_rval_t
vlm0f1ab762004-10-12 05:57:23 +0000511OCTET_STRING_encode_der(asn_TYPE_descriptor_t *td, void *sptr,
vlmfa67ddc2004-06-03 03:38:44 +0000512 int tag_mode, ber_tlv_tag_t tag,
513 asn_app_consume_bytes_f *cb, void *app_key) {
vlm0f1ab762004-10-12 05:57:23 +0000514 asn_enc_rval_t er;
515 asn_OCTET_STRING_specifics_t *specs = td->specifics
vlm9de248e2004-10-20 15:50:55 +0000516 ? (asn_OCTET_STRING_specifics_t *)td->specifics
517 : &asn_DEF_OCTET_STRING_specs;
vlm0f1ab762004-10-12 05:57:23 +0000518 BIT_STRING_t *st = (BIT_STRING_t *)sptr;
519 OS_type_e type_variant = (OS_type_e)specs->subvariant;
520 int fix_last_byte = 0;
vlmfa67ddc2004-06-03 03:38:44 +0000521
522 ASN_DEBUG("%s %s as OCTET STRING",
vlm3e693022004-09-02 12:57:25 +0000523 cb?"Estimating":"Encoding", td->name);
vlmfa67ddc2004-06-03 03:38:44 +0000524
525 /*
vlm0f1ab762004-10-12 05:57:23 +0000526 * Write tags.
vlmfa67ddc2004-06-03 03:38:44 +0000527 */
vlm0f1ab762004-10-12 05:57:23 +0000528 if(type_variant != _TT_ANY || tag_mode == 1) {
529 er.encoded = der_write_tags(td,
530 (type_variant == _TT_BIT_STRING) + st->size,
531 tag_mode, type_variant == _TT_ANY, tag, cb, app_key);
532 if(er.encoded == -1) {
533 er.failed_type = td;
534 er.structure_ptr = sptr;
535 return er;
vlmfa67ddc2004-06-03 03:38:44 +0000536 }
vlm0f1ab762004-10-12 05:57:23 +0000537 } else {
538 /* Disallow: [<tag>] IMPLICIT ANY */
539 assert(type_variant != _TT_ANY || tag_mode != -1);
540 er.encoded = 0;
541 }
542
543 if(!cb) {
544 er.encoded += (type_variant == _TT_BIT_STRING) + st->size;
545 return er;
vlmfa67ddc2004-06-03 03:38:44 +0000546 }
547
vlm267b7fd2004-10-11 11:43:08 +0000548 /*
vlm0f1ab762004-10-12 05:57:23 +0000549 * Prepare to deal with the last octet of BIT STRING.
vlm267b7fd2004-10-11 11:43:08 +0000550 */
vlm0f1ab762004-10-12 05:57:23 +0000551 if(type_variant == _TT_BIT_STRING) {
552 uint8_t b = st->bits_unused & 0x07;
553 if(b && st->size) fix_last_byte = 1;
554 _ASN_CALLBACK(&b, 1);
555 er.encoded++;
vlmfa67ddc2004-06-03 03:38:44 +0000556 }
557
vlm0f1ab762004-10-12 05:57:23 +0000558 /* Invoke callback for the main part of the buffer */
559 _ASN_CALLBACK(st->buf, st->size - fix_last_byte);
vlmfa67ddc2004-06-03 03:38:44 +0000560
vlm0f1ab762004-10-12 05:57:23 +0000561 /* The last octet should be stripped off the unused bits */
562 if(fix_last_byte) {
563 uint8_t b = st->buf[st->size-1] & (0xff << st->bits_unused);
564 _ASN_CALLBACK(&b, 1);
vlmfa67ddc2004-06-03 03:38:44 +0000565 }
566
vlm0f1ab762004-10-12 05:57:23 +0000567 er.encoded += st->size;
568 return er;
569cb_failed:
570 _ASN_ENCODE_FAILED;
vlmfa67ddc2004-06-03 03:38:44 +0000571}
572
vlm39ba4c42004-09-22 16:06:28 +0000573asn_enc_rval_t
vlm7b75d982004-09-29 13:20:14 +0000574OCTET_STRING_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
vlm39ba4c42004-09-22 16:06:28 +0000575 int ilevel, enum xer_encoder_flags_e flags,
576 asn_app_consume_bytes_f *cb, void *app_key) {
577 static const char *h2c = "0123456789ABCDEF";
578 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
579 asn_enc_rval_t er;
580 char scratch[16 * 3 + 4];
581 char *p = scratch;
582 uint8_t *buf;
583 uint8_t *end;
584 size_t i;
585
vlm84d551b2004-10-03 09:13:02 +0000586 if(!st || !st->buf)
587 _ASN_ENCODE_FAILED;
vlm39ba4c42004-09-22 16:06:28 +0000588
589 er.encoded = 0;
590
591 /*
592 * Dump the contents of the buffer in hexadecimal.
593 */
594 buf = st->buf;
595 end = buf + st->size;
596 if(flags & XER_F_CANONICAL) {
597 char *scend = scratch + (sizeof(scratch) - 2);
598 for(; buf < end; buf++) {
599 if(p >= scend) {
600 _ASN_CALLBACK(scratch, p - scratch);
601 er.encoded += p - scratch;
602 p = scratch;
603 }
604 *p++ = h2c[(*buf >> 4) & 0x0F];
605 *p++ = h2c[*buf & 0x0F];
606 }
vlmef1b4c02004-09-23 22:06:26 +0000607
608 _ASN_CALLBACK(scratch, p-scratch); /* Dump the rest */
609 er.encoded += p - scratch;
vlm39ba4c42004-09-22 16:06:28 +0000610 } else {
611 for(i = 0; buf < end; buf++, i++) {
612 if(!(i % 16) && (i || st->size > 16)) {
613 _ASN_CALLBACK(scratch, p-scratch);
614 er.encoded += (p-scratch);
615 p = scratch;
616 _i_ASN_TEXT_INDENT(1, ilevel);
617 }
618 *p++ = h2c[(*buf >> 4) & 0x0F];
619 *p++ = h2c[*buf & 0x0F];
620 *p++ = 0x20;
621 }
vlmef1b4c02004-09-23 22:06:26 +0000622 if(p - scratch) {
623 p--; /* Remove the tail space */
624 _ASN_CALLBACK(scratch, p-scratch); /* Dump the rest */
625 er.encoded += p - scratch;
626 if(st->size > 16)
627 _i_ASN_TEXT_INDENT(1, ilevel-1);
628 }
vlm39ba4c42004-09-22 16:06:28 +0000629 }
630
vlm39ba4c42004-09-22 16:06:28 +0000631 return er;
vlm84d551b2004-10-03 09:13:02 +0000632cb_failed:
633 _ASN_ENCODE_FAILED;
vlm39ba4c42004-09-22 16:06:28 +0000634}
635
vlm393a57a2004-10-03 10:54:25 +0000636static struct OCTET_STRING__xer_escape_table_s {
637 char *string;
638 int size;
639} OCTET_STRING__xer_escape_table[] = {
640#define OSXET(s) { s, sizeof(s) - 1 }
641 OSXET("\074\156\165\154\057\076"), /* <nul/> */
642 OSXET("\074\163\157\150\057\076"), /* <soh/> */
643 OSXET("\074\163\164\170\057\076"), /* <stx/> */
644 OSXET("\074\145\164\170\057\076"), /* <etx/> */
645 OSXET("\074\145\157\164\057\076"), /* <eot/> */
646 OSXET("\074\145\156\161\057\076"), /* <enq/> */
647 OSXET("\074\141\143\153\057\076"), /* <ack/> */
648 OSXET("\074\142\145\154\057\076"), /* <bel/> */
649 OSXET("\074\142\163\057\076"), /* <bs/> */
650 OSXET("\011"), /* \t */
651 OSXET("\012"), /* \n */
652 OSXET("\074\166\164\057\076"), /* <vt/> */
653 OSXET("\074\146\146\057\076"), /* <ff/> */
654 OSXET("\015"), /* \r */
655 OSXET("\074\163\157\057\076"), /* <so/> */
656 OSXET("\074\163\151\057\076"), /* <si/> */
657 OSXET("\074\144\154\145\057\076"), /* <dle/> */
658 OSXET("\074\144\143\061\057\076"), /* <de1/> */
659 OSXET("\074\144\143\062\057\076"), /* <de2/> */
660 OSXET("\074\144\143\063\057\076"), /* <de3/> */
661 OSXET("\074\144\143\064\057\076"), /* <de4/> */
662 OSXET("\074\156\141\153\057\076"), /* <nak/> */
663 OSXET("\074\163\171\156\057\076"), /* <syn/> */
664 OSXET("\074\145\164\142\057\076"), /* <etb/> */
665 OSXET("\074\143\141\156\057\076"), /* <can/> */
666 OSXET("\074\145\155\057\076"), /* <em/> */
667 OSXET("\074\163\165\142\057\076"), /* <sub/> */
668 OSXET("\074\145\163\143\057\076"), /* <esc/> */
669 OSXET("\074\151\163\064\057\076"), /* <is4/> */
670 OSXET("\074\151\163\063\057\076"), /* <is3/> */
671 OSXET("\074\151\163\062\057\076"), /* <is2/> */
672 OSXET("\074\151\163\061\057\076"), /* <is1/> */
673 { 0, 0 }, /* " " */
674 { 0, 0 }, /* ! */
675 { 0, 0 }, /* \" */
676 { 0, 0 }, /* # */
677 { 0, 0 }, /* $ */
678 { 0, 0 }, /* % */
679 OSXET("\046\141\155\160\073"), /* &amp; */
680 { 0, 0 }, /* ' */
681 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, /* ()*+,-./ */
682 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, /* 01234567 */
683 {0,0},{0,0},{0,0},{0,0}, /* 89:; */
684 OSXET("\046\154\164\073"), /* &lt; */
685 { 0, 0 }, /* = */
686 OSXET("\046\147\164\073"), /* &gt; */
687};
688
vlm9de248e2004-10-20 15:50:55 +0000689static int
690OS__check_escaped_control_char(void *buf, int size) {
691 size_t i;
692 /*
693 * Inefficient algorithm which translates the escape sequences
694 * defined above into characters. Returns -1 if not found.
695 * TODO: replace by a faster algorithm (bsearch(), hash or
696 * nested table lookups).
697 */
698 for(i = 0; i < 32 /* Don't spend time on the bottom half */; i++) {
699 struct OCTET_STRING__xer_escape_table_s *el;
700 el = &OCTET_STRING__xer_escape_table[i];
701 if(el->size == size && memcmp(buf, el->string, size) == 0)
702 return i;
703 }
704 return -1;
705}
706
707static int
708OCTET_STRING__handle_control_chars(void *struct_ptr, void *chunk_buf, size_t chunk_size) {
709 /*
710 * This might be one of the escape sequences
711 * for control characters. Check it out.
712 * #11.15.5
713 */
714 int control_char = OS__check_escaped_control_char(chunk_buf,chunk_size);
715 if(control_char >= 0) {
716 OCTET_STRING_t *st = (OCTET_STRING_t *)struct_ptr;
717 void *p = REALLOC(st->buf, st->size + 2);
718 if(p) {
719 st->buf = (uint8_t *)p;
720 st->buf[st->size++] = control_char;
721 st->buf[st->size] = '\0'; /* nul-termination */
722 return 0;
723 }
724 }
725
726 return -1; /* No, it's not */
727}
728
vlm39ba4c42004-09-22 16:06:28 +0000729asn_enc_rval_t
vlm9de248e2004-10-20 15:50:55 +0000730OCTET_STRING_encode_xer_utf8(asn_TYPE_descriptor_t *td, void *sptr,
vlm39ba4c42004-09-22 16:06:28 +0000731 int ilevel, enum xer_encoder_flags_e flags,
732 asn_app_consume_bytes_f *cb, void *app_key) {
733 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
734 asn_enc_rval_t er;
vlm393a57a2004-10-03 10:54:25 +0000735 uint8_t *buf, *end;
736 uint8_t *ss; /* Sequence start */
737 ssize_t encoded_len = 0;
vlm39ba4c42004-09-22 16:06:28 +0000738
739 (void)ilevel; /* Unused argument */
740 (void)flags; /* Unused argument */
741
742 if(!st || !st->buf)
743 _ASN_ENCODE_FAILED;
744
vlm393a57a2004-10-03 10:54:25 +0000745 buf = st->buf;
746 end = buf + st->size;
747 for(ss = buf; buf < end; buf++) {
vlmd7c2ef32004-10-05 06:35:31 +0000748 unsigned int ch = *buf;
vlm393a57a2004-10-03 10:54:25 +0000749 int s_len; /* Special encoding sequence length */
vlm39ba4c42004-09-22 16:06:28 +0000750
vlm393a57a2004-10-03 10:54:25 +0000751 /*
752 * Escape certain characters: X.680/11.15
753 */
754 if(ch < sizeof(OCTET_STRING__xer_escape_table)
755 /sizeof(OCTET_STRING__xer_escape_table[0])
756 && (s_len = OCTET_STRING__xer_escape_table[ch].size)) {
757 if(((buf - ss) && cb(ss, buf - ss, app_key) < 0)
758 || cb(OCTET_STRING__xer_escape_table[ch].string, s_len,
759 app_key) < 0)
760 _ASN_ENCODE_FAILED;
761 encoded_len += (buf - ss) + s_len;
762 ss = buf + 1;
763 }
764 }
765
766 encoded_len += (buf - ss);
767 if((buf - ss) && cb(ss, buf - ss, app_key) < 0)
768 _ASN_ENCODE_FAILED;
769
770 er.encoded = encoded_len;
vlm39ba4c42004-09-22 16:06:28 +0000771 return er;
772}
773
vlm9de248e2004-10-20 15:50:55 +0000774/*
775 * Convert from hexadecimal format (cstring): "AB CD EF"
776 */
777static ssize_t OCTET_STRING__convert_hexadecimal(void *sptr, void *chunk_buf, size_t chunk_size, int have_more) {
778 OCTET_STRING_t *st = (OCTET_STRING_t *)sptr;
779 char *chunk_stop = (char *)chunk_buf;
780 char *p = chunk_stop;
781 char *pend = p + chunk_size;
782 unsigned int clv = 0;
783 int half = 0; /* Half bit */
784 uint8_t *buf;
785
786 /* Reallocate buffer according to high cap estimation */
787 ssize_t _ns = st->size + (chunk_size + 1) / 2;
788 void *nptr = REALLOC(st->buf, _ns + 1);
789 if(!nptr) return -1;
790 st->buf = (uint8_t *)nptr;
791 buf = st->buf + st->size;
792
793 /*
794 * If something like " a b c " appears here, the " a b":3 will be
795 * converted, and the rest skipped. That is, unless buf_size is greater
796 * than chunk_size, then it'll be equivalent to "ABC0".
797 */
798 for(; p < pend; p++) {
799 int ch = *(unsigned char *)p;
800 switch(ch) {
801 case 0x09: case 0x0a: case 0x0c: case 0x0d:
802 case 0x20:
803 /* Ignore whitespace */
804 continue;
805 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: /*01234*/
806 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: /*56789*/
807 clv = (clv << 4) + (ch - 0x30);
808 break;
809 case 0x41: case 0x42: case 0x43: /* ABC */
810 case 0x44: case 0x45: case 0x46: /* DEF */
811 clv = (clv << 4) + (ch - (0x41 + 10));
812 break;
813 case 0x61: case 0x62: case 0x63: /* abc */
814 case 0x64: case 0x65: case 0x66: /* def */
815 clv = (clv << 4) + (ch - (0x61 + 10));
816 break;
817 default:
818 *buf = 0; /* JIC */
819 return -1;
820 }
821 if(half++) {
822 half = 0;
823 *buf++ = clv;
824 chunk_stop = p + 1;
825 }
826 }
827
828 /*
829 * Check partial decoding.
830 */
831 if(half) {
832 if(have_more) {
833 /*
834 * Partial specification is fine,
835 * because no more more PXER_TEXT data is available.
836 */
837 *buf++ = clv << 4;
838 chunk_stop = p;
839 }
840 } else {
841 chunk_stop = p;
842 }
843
844 st->size = buf - st->buf; /* Adjust the buffer size */
845 assert(st->size <= _ns);
846 st->buf[st->size] = 0; /* Courtesy termination */
847
848 return (chunk_stop - (char *)chunk_buf); /* Converted size */
849}
850
851/*
852 * Convert from binary format: "00101011101"
853 */
854static ssize_t OCTET_STRING__convert_binary(void *sptr, void *chunk_buf, size_t chunk_size, int have_more) {
855 BIT_STRING_t *st = (BIT_STRING_t *)sptr;
856 char *p = (char *)chunk_buf;
857 char *pend = p + chunk_size;
858 int bits_unused = st->bits_unused & 0x7;
859 uint8_t *buf;
860
861 /* Reallocate buffer according to high cap estimation */
862 ssize_t _ns = st->size + (chunk_size + 7) / 8;
863 void *nptr = REALLOC(st->buf, _ns + 1);
864 if(!nptr) return -1;
865 st->buf = (uint8_t *)nptr;
866 buf = st->buf + st->size;
867
868 (void)have_more;
869
870 if(bits_unused == 0)
871 bits_unused = 8;
872 else if(st->size)
873 buf--;
874
875 /*
876 * Convert series of 0 and 1 into the octet string.
877 */
878 for(; p < pend; p++) {
879 int ch = *(unsigned char *)p;
880 switch(ch) {
881 case 0x09: case 0x0a: case 0x0c: case 0x0d:
882 case 0x20:
883 /* Ignore whitespace */
884 break;
885 case 0x30:
886 case 0x31:
887 if(bits_unused-- <= 0) {
888 *++buf = 0; /* Clean the cell */
889 bits_unused = 7;
890 }
891 *buf |= (ch&1) << bits_unused;
892 break;
893 default:
894 st->bits_unused = bits_unused;
895 return -1;
896 }
897 }
898
899 if(bits_unused == 8) {
900 st->size = buf - st->buf;
901 st->bits_unused = 0;
902 } else {
903 st->size = buf - st->buf + 1;
904 st->bits_unused = bits_unused;
905 }
906
907 assert(st->size <= _ns);
908 st->buf[st->size] = 0; /* Courtesy termination */
909
910 return chunk_size; /* Converted in full */
911}
912
913/*
914 * Something like strtod(), but with stricter rules.
915 */
916static int
vlm6c593842004-10-26 09:03:31 +0000917OS__strtoent(int base, char *buf, char *end, int32_t *return_value) {
918 int32_t val = 0;
vlm9de248e2004-10-20 15:50:55 +0000919 char *p;
920
921 for(p = buf; p < end; p++) {
922 int ch = *p;
923 if((val * base + base) < 0) return -1; /* Strange huge value */
924 switch(ch) {
925 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: /*01234*/
926 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: /*56789*/
927 val = val * base + (ch - 0x30);
928 break;
929 case 0x41: case 0x42: case 0x43: /* ABC */
930 case 0x44: case 0x45: case 0x46: /* DEF */
931 val = val * base + (ch - (0x41 + 10));
932 break;
933 case 0x61: case 0x62: case 0x63: /* abc */
934 case 0x64: case 0x65: case 0x66: /* def */
935 val = val * base + (ch - (0x61 + 10));
936 break;
937 case 0x3b: /* ';' */
938 *return_value = val;
939 return (p - buf) + 1;
940 default:
941 return -1; /* Character set error */
942 }
943 }
944
945 /* Do not return value. It's an error we're talking about here. */
946 return (p - buf);
947}
948
949/*
950 * Convert from the plain UTF-8 format, expanding entity references: "2 &lt; 3"
951 */
952static ssize_t OCTET_STRING__convert_entrefs(void *sptr, void *chunk_buf, size_t chunk_size, int have_more) {
953 OCTET_STRING_t *st = (OCTET_STRING_t *)sptr;
954 char *p = (char *)chunk_buf;
955 char *pend = p + chunk_size;
956 uint8_t *buf;
957
958 /* Reallocate buffer */
959 ssize_t _ns = st->size + chunk_size;
960 void *nptr = REALLOC(st->buf, _ns + 1);
961 if(!nptr) return -1;
962 st->buf = (uint8_t *)nptr;
963 buf = st->buf + st->size;
964
965 /*
966 * Convert series of 0 and 1 into the octet string.
967 */
968 for(; p < pend; p++) {
969 int ch = *(unsigned char *)p;
970 int len; /* Length of the rest of the chunk */
971
972 if(ch != 0x26 /* '&' */) {
973 *buf++ = ch;
974 continue; /* That was easy... */
975 }
976
977 /*
978 * Process entity reference.
979 */
980 len = chunk_size - (p - (char *)chunk_buf);
981 if(len == 1 /* "&" */) goto want_more;
982 if(p[1] == 0x23 /* '#' */) {
983 char *pval; /* Pointer to start of digits */
vlm6c593842004-10-26 09:03:31 +0000984 int32_t val; /* Entity reference value */
vlm9de248e2004-10-20 15:50:55 +0000985 int base;
986
987 if(len == 2 /* "&#" */) goto want_more;
988 if(p[2] == 0x78 /* 'x' */)
989 pval = p + 3, base = 16;
990 else
991 pval = p + 2, base = 10;
992 len = OS__strtoent(base, pval, p + len, &val);
993 if(len == -1) {
994 /* Invalid charset. Just copy verbatim. */
995 *buf++ = ch;
996 continue;
997 }
998 if(!len || pval[len-1] != 0x3b) goto want_more;
999 assert(val > 0);
1000 p += (pval - p) + len - 1; /* Advance past entref */
1001
1002 if(val < 0x80) {
1003 *buf++ = (char)val;
1004 } else if(val < 0x800) {
1005 *buf++ = 0xc0 | ((val >> 6));
1006 *buf++ = 0x80 | ((val & 0x3f));
1007 } else if(val < 0x10000) {
1008 *buf++ = 0xe0 | ((val >> 12));
1009 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1010 *buf++ = 0x80 | ((val & 0x3f));
1011 } else if(val < 0x200000) {
1012 *buf++ = 0xf0 | ((val >> 18));
1013 *buf++ = 0x80 | ((val >> 12) & 0x3f);
1014 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1015 *buf++ = 0x80 | ((val & 0x3f));
1016 } else if(val < 0x4000000) {
1017 *buf++ = 0xf8 | ((val >> 24));
1018 *buf++ = 0x80 | ((val >> 18) & 0x3f);
1019 *buf++ = 0x80 | ((val >> 12) & 0x3f);
1020 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1021 *buf++ = 0x80 | ((val & 0x3f));
1022 } else {
1023 *buf++ = 0xfc | ((val >> 30) & 0x1);
1024 *buf++ = 0x80 | ((val >> 24) & 0x3f);
1025 *buf++ = 0x80 | ((val >> 18) & 0x3f);
1026 *buf++ = 0x80 | ((val >> 12) & 0x3f);
1027 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1028 *buf++ = 0x80 | ((val & 0x3f));
1029 }
1030 } else {
1031 /*
1032 * Ugly, limited parsing of &amp; &gt; &lt;
1033 */
1034 char *sc = (char *)memchr(p, 0x3b, len > 5 ? 5 : len);
1035 if(!sc) goto want_more;
1036 if((sc - p) == 4
1037 && p[1] == 0x61 /* 'a' */
1038 && p[2] == 0x6d /* 'm' */
1039 && p[3] == 0x70 /* 'p' */) {
1040 *buf++ = 0x26;
1041 p = sc;
1042 continue;
1043 }
1044 if((sc - p) == 3) {
1045 if(p[1] == 0x6c) {
1046 *buf = 0x3c; /* '<' */
1047 } else if(p[1] == 0x67) {
1048 *buf = 0x3e; /* '>' */
1049 } else {
1050 /* Unsupported entity reference */
1051 *buf++ = ch;
1052 continue;
1053 }
1054 if(p[2] != 0x74) {
1055 /* Unsupported entity reference */
1056 *buf++ = ch;
1057 continue;
1058 }
1059 buf++;
1060 p = sc;
1061 continue;
1062 }
1063 /* Unsupported entity reference */
1064 *buf++ = ch;
1065 }
1066
1067 continue;
1068 want_more:
1069 if(have_more) {
1070 /*
1071 * We know that no more data (of the same type)
1072 * is coming. Copy the rest verbatim.
1073 */
1074 *buf++ = ch;
1075 continue;
1076 }
1077 *buf = 0; /* JIC */
1078 /* Processing stalled: need more data */
1079 return (p - (char *)chunk_buf);
1080 }
1081
1082 st->size = buf - st->buf;
1083 assert(st->size <= _ns);
1084 st->buf[st->size] = 0; /* Courtesy termination */
1085
1086 return chunk_size; /* Converted in full */
1087}
1088
1089/*
1090 * Decode OCTET STRING from the XML element's body.
1091 */
1092static asn_dec_rval_t
1093OCTET_STRING__decode_xer(asn_codec_ctx_t *opt_codec_ctx,
1094 asn_TYPE_descriptor_t *td, void **sptr,
1095 const char *opt_mname, void *buf_ptr, size_t size,
1096 int (*opt_unexpected_tag_decoder)
1097 (void *struct_ptr, void *chunk_buf, size_t chunk_size),
1098 ssize_t (*body_receiver)
1099 (void *struct_ptr, void *chunk_buf, size_t chunk_size,
1100 int have_more)
1101) {
1102 asn_OCTET_STRING_specifics_t *specs = td->specifics
1103 ? (asn_OCTET_STRING_specifics_t *)td->specifics
1104 : &asn_DEF_OCTET_STRING_specs;
1105 const char *xml_tag = opt_mname ? opt_mname : td->xml_tag;
1106 asn_struct_ctx_t *ctx; /* Per-structure parser context */
1107
1108 /*
1109 * Create the string if does not exist.
1110 */
1111 if(!*sptr) {
vlma56a9e82004-10-23 10:20:27 +00001112 OCTET_STRING_t *st;
1113 (void *)st = *sptr = CALLOC(1, specs->struct_size);
1114 if(st) st->buf = (uint8_t *)CALLOC(1, 1);
1115 if(!*sptr || !st->buf) {
vlm9de248e2004-10-20 15:50:55 +00001116 asn_dec_rval_t rval;
vlma56a9e82004-10-23 10:20:27 +00001117 if(*sptr) FREEMEM(*sptr);
vlm9de248e2004-10-20 15:50:55 +00001118 rval.code = RC_FAIL;
1119 rval.consumed = 0;
1120 return rval;
1121 }
1122 }
1123
1124 /* Restore parsing context */
1125 ctx = (asn_struct_ctx_t *)(((char *)*sptr) + specs->ctx_offset);
1126
1127 return xer_decode_general(opt_codec_ctx, ctx, *sptr, xml_tag,
1128 buf_ptr, size, opt_unexpected_tag_decoder, body_receiver);
1129}
1130
1131/*
1132 * Decode OCTET STRING from the hexadecimal data.
1133 */
1134asn_dec_rval_t
1135OCTET_STRING_decode_xer_hex(asn_codec_ctx_t *opt_codec_ctx,
1136 asn_TYPE_descriptor_t *td, void **sptr,
1137 const char *opt_mname, void *buf_ptr, size_t size) {
1138 return OCTET_STRING__decode_xer(opt_codec_ctx, td, sptr, opt_mname,
1139 buf_ptr, size, 0, OCTET_STRING__convert_hexadecimal);
1140}
1141
1142/*
1143 * Decode OCTET STRING from the binary (0/1) data.
1144 */
1145asn_dec_rval_t
1146OCTET_STRING_decode_xer_binary(asn_codec_ctx_t *opt_codec_ctx,
1147 asn_TYPE_descriptor_t *td, void **sptr,
1148 const char *opt_mname, void *buf_ptr, size_t size) {
1149 return OCTET_STRING__decode_xer(opt_codec_ctx, td, sptr, opt_mname,
1150 buf_ptr, size, 0, OCTET_STRING__convert_binary);
1151}
1152
1153/*
1154 * Decode OCTET STRING from the string (ASCII/UTF-8) data.
1155 */
1156asn_dec_rval_t
1157OCTET_STRING_decode_xer_utf8(asn_codec_ctx_t *opt_codec_ctx,
1158 asn_TYPE_descriptor_t *td, void **sptr,
1159 const char *opt_mname, void *buf_ptr, size_t size) {
1160 return OCTET_STRING__decode_xer(opt_codec_ctx, td, sptr, opt_mname,
1161 buf_ptr, size,
1162 OCTET_STRING__handle_control_chars,
1163 OCTET_STRING__convert_entrefs);
1164}
1165
1166
vlmfa67ddc2004-06-03 03:38:44 +00001167int
vlm7b75d982004-09-29 13:20:14 +00001168OCTET_STRING_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
vlmfa67ddc2004-06-03 03:38:44 +00001169 asn_app_consume_bytes_f *cb, void *app_key) {
vlm1ff928d2004-08-11 08:10:13 +00001170 static const char *h2c = "0123456789ABCDEF";
vlmda674682004-08-11 09:07:36 +00001171 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
vlmfa67ddc2004-06-03 03:38:44 +00001172 char scratch[16 * 3 + 4];
1173 char *p = scratch;
1174 uint8_t *buf;
1175 uint8_t *end;
1176 size_t i;
vlmfa67ddc2004-06-03 03:38:44 +00001177
vlmb42843a2004-06-05 08:17:50 +00001178 (void)td; /* Unused argument */
1179
vlm6678cb12004-09-26 13:10:40 +00001180 if(!st || !st->buf) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
vlmfa67ddc2004-06-03 03:38:44 +00001181
1182 /*
1183 * Dump the contents of the buffer in hexadecimal.
1184 */
1185 buf = st->buf;
1186 end = buf + st->size;
1187 for(i = 0; buf < end; buf++, i++) {
1188 if(!(i % 16) && (i || st->size > 16)) {
vlm6678cb12004-09-26 13:10:40 +00001189 if(cb(scratch, p - scratch, app_key) < 0)
vlmfa67ddc2004-06-03 03:38:44 +00001190 return -1;
vlm6678cb12004-09-26 13:10:40 +00001191 _i_INDENT(1);
vlmfa67ddc2004-06-03 03:38:44 +00001192 p = scratch;
1193 }
1194 *p++ = h2c[(*buf >> 4) & 0x0F];
1195 *p++ = h2c[*buf & 0x0F];
vlm39ba4c42004-09-22 16:06:28 +00001196 *p++ = 0x20;
vlmfa67ddc2004-06-03 03:38:44 +00001197 }
1198
vlmef1b4c02004-09-23 22:06:26 +00001199 if(p > scratch) {
1200 p--; /* Remove the tail space */
vlm6678cb12004-09-26 13:10:40 +00001201 if(cb(scratch, p - scratch, app_key) < 0)
vlmef1b4c02004-09-23 22:06:26 +00001202 return -1;
1203 }
1204
1205 return 0;
vlmfa67ddc2004-06-03 03:38:44 +00001206}
1207
1208int
vlm9de248e2004-10-20 15:50:55 +00001209OCTET_STRING_print_utf8(asn_TYPE_descriptor_t *td, const void *sptr,
vlmfa67ddc2004-06-03 03:38:44 +00001210 int ilevel, asn_app_consume_bytes_f *cb, void *app_key) {
vlmda674682004-08-11 09:07:36 +00001211 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
vlmfa67ddc2004-06-03 03:38:44 +00001212
vlmb42843a2004-06-05 08:17:50 +00001213 (void)td; /* Unused argument */
1214 (void)ilevel; /* Unused argument */
1215
vlmfa67ddc2004-06-03 03:38:44 +00001216 if(st && st->buf) {
vlm6678cb12004-09-26 13:10:40 +00001217 return (cb(st->buf, st->size, app_key) < 0) ? -1 : 0;
vlmfa67ddc2004-06-03 03:38:44 +00001218 } else {
vlm6678cb12004-09-26 13:10:40 +00001219 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
vlmfa67ddc2004-06-03 03:38:44 +00001220 }
1221}
1222
1223void
vlm7b75d982004-09-29 13:20:14 +00001224OCTET_STRING_free(asn_TYPE_descriptor_t *td, void *sptr, int contents_only) {
vlmda674682004-08-11 09:07:36 +00001225 OCTET_STRING_t *st = (OCTET_STRING_t *)sptr;
vlm0f1ab762004-10-12 05:57:23 +00001226 asn_OCTET_STRING_specifics_t *specs = td->specifics
vlm9de248e2004-10-20 15:50:55 +00001227 ? (asn_OCTET_STRING_specifics_t *)td->specifics
1228 : &asn_DEF_OCTET_STRING_specs;
vlm0f1ab762004-10-12 05:57:23 +00001229 asn_struct_ctx_t *ctx = (asn_struct_ctx_t *)
1230 ((char *)st + specs->ctx_offset);
vlm9de248e2004-10-20 15:50:55 +00001231 struct _stack *stck = (struct _stack *)ctx->ptr;
vlmfa67ddc2004-06-03 03:38:44 +00001232
1233 if(!td || !st)
1234 return;
1235
1236 ASN_DEBUG("Freeing %s as OCTET STRING", td->name);
1237
1238 if(st->buf) {
1239 FREEMEM(st->buf);
1240 }
1241
1242 /*
1243 * Remove decode-time stack.
1244 */
1245 if(stck) {
1246 while(stck->tail) {
1247 struct _stack_el *sel = stck->tail;
1248 stck->tail = sel->prev;
1249 FREEMEM(sel);
1250 }
1251 FREEMEM(stck);
1252 }
1253
1254 if(!contents_only) {
1255 FREEMEM(st);
1256 }
1257}
1258
1259/*
1260 * Conversion routines.
1261 */
1262int
1263OCTET_STRING_fromBuf(OCTET_STRING_t *st, const char *str, int len) {
1264 void *buf;
1265
1266 if(st == 0 || (str == 0 && len)) {
1267 errno = EINVAL;
1268 return -1;
1269 }
1270
1271 /*
1272 * Clear the OCTET STRING.
1273 */
1274 if(str == NULL) {
1275 if(st->buf)
1276 FREEMEM(st->buf);
1277 st->size = 0;
1278 return 0;
1279 }
1280
1281 /* Determine the original string size, if not explicitly given */
1282 if(len < 0)
1283 len = strlen(str);
1284
1285 /* Allocate and fill the memory */
1286 buf = MALLOC(len + 1);
1287 if(buf == NULL) {
1288 return -1;
1289 } else {
vlmda674682004-08-11 09:07:36 +00001290 st->buf = (uint8_t *)buf;
vlmfa67ddc2004-06-03 03:38:44 +00001291 st->size = len;
1292 }
1293
1294 memcpy(buf, str, len);
1295 st->buf[st->size] = '\0'; /* Couldn't use memcpy(len+1)! */
1296
1297 return 0;
1298}
1299
1300OCTET_STRING_t *
vlm0f1ab762004-10-12 05:57:23 +00001301OCTET_STRING_new_fromBuf(asn_TYPE_descriptor_t *td, const char *str, int len) {
1302 asn_OCTET_STRING_specifics_t *specs = td->specifics
vlm9de248e2004-10-20 15:50:55 +00001303 ? (asn_OCTET_STRING_specifics_t *)td->specifics
1304 : &asn_DEF_OCTET_STRING_specs;
vlmfa67ddc2004-06-03 03:38:44 +00001305 OCTET_STRING_t *st;
1306
vlm0f1ab762004-10-12 05:57:23 +00001307 st = (OCTET_STRING_t *)CALLOC(1, specs->struct_size);
vlmfa67ddc2004-06-03 03:38:44 +00001308 if(st && str && OCTET_STRING_fromBuf(st, str, len)) {
1309 free(st);
1310 st = NULL;
1311 }
1312
1313 return st;
1314}
1315