blob: 447b2425bebdacf920ea51458661d7a051a11b1d [file] [log] [blame]
vlmfa67ddc2004-06-03 03:38:44 +00001/*-
vlm82abba82005-04-25 19:38:21 +00002 * Copyright (c) 2003, 2004, 2005 Lev Walkin <vlm@lionet.info>.
3 * All rights reserved.
vlmfa67ddc2004-06-03 03:38:44 +00004 * Redistribution and modifications are permitted subject to BSD license.
5 */
vlm39ba4c42004-09-22 16:06:28 +00006#include <asn_internal.h>
vlmfa67ddc2004-06-03 03:38:44 +00007#include <OCTET_STRING.h>
vlm0f1ab762004-10-12 05:57:23 +00008#include <BIT_STRING.h> /* for .bits_unused member */
vlmfa67ddc2004-06-03 03:38:44 +00009#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
vlm82abba82005-04-25 19:38:21 +000046#define _CH_PHASE(ctx, inc) do { \
47 if(ctx->phase == 0) \
48 ctx->context = 0; \
49 ctx->phase += inc; \
vlmfa67ddc2004-06-03 03:38:44 +000050 } 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
vlm82abba82005-04-25 19:38:21 +000055#define ADVANCE(num_bytes) do { \
56 size_t num = (num_bytes); \
57 buf_ptr = ((const 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
vlm82abba82005-04-25 19:38:21 +000063#define RETURN(_code) do { \
64 rval.code = _code; \
65 rval.consumed = consumed_myself; \
66 return rval; \
vlmfa67ddc2004-06-03 03:38:44 +000067 } while(0)
68
vlmef1b4c02004-09-23 22:06:26 +000069#undef APPEND
vlmb42843a2004-06-05 08:17:50 +000070#define APPEND(bufptr, bufsize) do { \
vlm82abba82005-04-25 19:38:21 +000071 size_t _bs = (bufsize); /* Append size */ \
72 size_t _ns = ctx->context; /* Allocated now */ \
73 size_t _es = st->size + _bs; /* Expected size */ \
74 /* int is really a typeof(st->size): */ \
75 if((int)_es < 0) RETURN(RC_FAIL); \
76 if(_ns <= _es) { \
vlmb42843a2004-06-05 08:17:50 +000077 void *ptr; \
vlm72425de2004-09-13 08:31:01 +000078 /* Be nice and round to the memory allocator */ \
vlm82abba82005-04-25 19:38:21 +000079 do { _ns = _ns ? _ns << 1 : 16; } \
80 while(_ns <= _es); \
81 /* int is really a typeof(st->size): */ \
82 if((int)_ns < 0) RETURN(RC_FAIL); \
vlmb42843a2004-06-05 08:17:50 +000083 ptr = REALLOC(st->buf, _ns); \
84 if(ptr) { \
vlmda674682004-08-11 09:07:36 +000085 st->buf = (uint8_t *)ptr; \
vlm82abba82005-04-25 19:38:21 +000086 ctx->context = _ns; \
vlmb42843a2004-06-05 08:17:50 +000087 } else { \
88 RETURN(RC_FAIL); \
89 } \
vlm9309d572005-07-02 20:24:27 +000090 ASN_DEBUG("Reallocating into %ld", (long)_ns); \
vlmb42843a2004-06-05 08:17:50 +000091 } \
vlmcdfdb722004-09-04 04:44:50 +000092 memcpy(st->buf + st->size, bufptr, _bs); \
vlmb42843a2004-06-05 08:17:50 +000093 /* Convenient nul-termination */ \
vlm82abba82005-04-25 19:38:21 +000094 st->buf[_es] = '\0'; \
95 st->size = _es; \
vlmfa67ddc2004-06-03 03:38:44 +000096 } while(0)
97
98/*
vlm267b7fd2004-10-11 11:43:08 +000099 * Internal variant of the OCTET STRING.
100 */
101typedef enum OS_type {
102 _TT_GENERIC = 0, /* Just a random OCTET STRING */
103 _TT_BIT_STRING = 1, /* BIT STRING type, a special case */
vlme66fd8a2005-07-21 09:32:49 +0000104 _TT_ANY = 2 /* ANY type, a special case too */
vlm267b7fd2004-10-11 11:43:08 +0000105} OS_type_e;
106
107/*
vlmfa67ddc2004-06-03 03:38:44 +0000108 * The main reason why ASN.1 is still alive is that too much time and effort
109 * is necessary for learning it more or less adequately, thus creating a gut
110 * necessity to demonstrate that aquired skill everywhere afterwards.
111 * No, I am not going to explain what the following stuff is.
112 */
113struct _stack_el {
vlmb33ed682004-09-24 20:57:41 +0000114 ber_tlv_len_t left; /* What's left to read (or -1) */
vlmdb6b2902004-09-27 20:54:06 +0000115 ber_tlv_len_t got; /* What was actually processed */
vlm72425de2004-09-13 08:31:01 +0000116 int cont_level; /* Depth of subcontainment */
vlmfa67ddc2004-06-03 03:38:44 +0000117 int want_nulls; /* Want null "end of content" octets? */
118 int bits_chopped; /* Flag in BIT STRING mode */
vlmdb6b2902004-09-27 20:54:06 +0000119 ber_tlv_tag_t tag; /* For debugging purposes */
vlmfa67ddc2004-06-03 03:38:44 +0000120 struct _stack_el *prev;
121 struct _stack_el *next;
122};
123struct _stack {
124 struct _stack_el *tail;
125 struct _stack_el *cur_ptr;
126};
127
128static struct _stack_el *
vlmb33ed682004-09-24 20:57:41 +0000129OS__add_stack_el(struct _stack *st) {
vlmfa67ddc2004-06-03 03:38:44 +0000130 struct _stack_el *nel;
131
vlm72425de2004-09-13 08:31:01 +0000132 /*
133 * Reuse the old stack frame or allocate a new one.
134 */
vlmfa67ddc2004-06-03 03:38:44 +0000135 if(st->cur_ptr && st->cur_ptr->next) {
136 nel = st->cur_ptr->next;
vlmfa67ddc2004-06-03 03:38:44 +0000137 nel->bits_chopped = 0;
vlmdb6b2902004-09-27 20:54:06 +0000138 nel->got = 0;
139 /* Retain the nel->cont_level, it's correct. */
vlmfa67ddc2004-06-03 03:38:44 +0000140 } else {
vlmd3c80792004-12-15 23:23:53 +0000141 nel = (struct _stack_el *)CALLOC(1, sizeof(struct _stack_el));
vlmfa67ddc2004-06-03 03:38:44 +0000142 if(nel == NULL)
143 return NULL;
144
145 if(st->tail) {
vlm72425de2004-09-13 08:31:01 +0000146 /* Increase a subcontainment depth */
147 nel->cont_level = st->tail->cont_level + 1;
vlmfa67ddc2004-06-03 03:38:44 +0000148 st->tail->next = nel;
149 }
150 nel->prev = st->tail;
151 st->tail = nel;
152 }
153
154 st->cur_ptr = nel;
155
156 return nel;
157}
158
159static struct _stack *
160_new_stack() {
vlm0f1ab762004-10-12 05:57:23 +0000161 return (struct _stack *)CALLOC(1, sizeof(struct _stack));
vlmfa67ddc2004-06-03 03:38:44 +0000162}
163
164/*
165 * Decode OCTET STRING type.
166 */
vlm9de248e2004-10-20 15:50:55 +0000167asn_dec_rval_t
vlm7b75d982004-09-29 13:20:14 +0000168OCTET_STRING_decode_ber(asn_codec_ctx_t *opt_codec_ctx,
169 asn_TYPE_descriptor_t *td,
vlmb02dcc62005-03-10 18:52:02 +0000170 void **os_structure, const void *buf_ptr, size_t size, int tag_mode) {
vlm0f1ab762004-10-12 05:57:23 +0000171 asn_OCTET_STRING_specifics_t *specs = td->specifics
vlm9de248e2004-10-20 15:50:55 +0000172 ? (asn_OCTET_STRING_specifics_t *)td->specifics
173 : &asn_DEF_OCTET_STRING_specs;
vlm0f1ab762004-10-12 05:57:23 +0000174 BIT_STRING_t *st = (BIT_STRING_t *)*os_structure;
vlm9de248e2004-10-20 15:50:55 +0000175 asn_dec_rval_t rval;
vlm7b75d982004-09-29 13:20:14 +0000176 asn_struct_ctx_t *ctx;
vlmfa67ddc2004-06-03 03:38:44 +0000177 ssize_t consumed_myself = 0;
vlm0f1ab762004-10-12 05:57:23 +0000178 struct _stack *stck; /* Expectations stack structure */
vlmdb6b2902004-09-27 20:54:06 +0000179 struct _stack_el *sel = 0; /* Stack element */
vlmfa67ddc2004-06-03 03:38:44 +0000180 int tlv_constr;
vlm0f1ab762004-10-12 05:57:23 +0000181 OS_type_e type_variant = (OS_type_e)specs->subvariant;
vlmfa67ddc2004-06-03 03:38:44 +0000182
vlmef1b4c02004-09-23 22:06:26 +0000183 ASN_DEBUG("Decoding %s as %s (frame %ld)",
184 td->name,
vlm0f1ab762004-10-12 05:57:23 +0000185 (type_variant == _TT_GENERIC) ?
186 "OCTET STRING" : "OS-SpecialCase",
vlmef1b4c02004-09-23 22:06:26 +0000187 (long)size);
vlmfa67ddc2004-06-03 03:38:44 +0000188
189 /*
190 * Create the string if does not exist.
191 */
192 if(st == NULL) {
vlmd3c80792004-12-15 23:23:53 +0000193 *os_structure = CALLOC(1, specs->struct_size);
194 st = (BIT_STRING_t *)*os_structure;
vlmfa67ddc2004-06-03 03:38:44 +0000195 if(st == NULL)
196 RETURN(RC_FAIL);
197 }
198
199 /* Restore parsing context */
vlm0f1ab762004-10-12 05:57:23 +0000200 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
vlmfa67ddc2004-06-03 03:38:44 +0000201
202 switch(ctx->phase) {
203 case 0:
204 /*
205 * Check tags.
206 */
vlm7b75d982004-09-29 13:20:14 +0000207 rval = ber_check_tags(opt_codec_ctx, td, ctx,
vlm6678cb12004-09-26 13:10:40 +0000208 buf_ptr, size, tag_mode, -1,
vlmfa67ddc2004-06-03 03:38:44 +0000209 &ctx->left, &tlv_constr);
vlmd7c2ef32004-10-05 06:35:31 +0000210 if(rval.code != RC_OK)
211 return rval;
vlmfa67ddc2004-06-03 03:38:44 +0000212
vlmfa67ddc2004-06-03 03:38:44 +0000213 if(tlv_constr) {
214 /*
215 * Complex operation, requires stack of expectations.
216 */
217 ctx->ptr = _new_stack();
218 if(ctx->ptr) {
vlmd3c80792004-12-15 23:23:53 +0000219 stck = (struct _stack *)ctx->ptr;
vlmfa67ddc2004-06-03 03:38:44 +0000220 } else {
221 RETURN(RC_FAIL);
222 }
223 } else {
224 /*
225 * Jump into stackless primitive decoding.
226 */
227 _CH_PHASE(ctx, 3);
vlm0f1ab762004-10-12 05:57:23 +0000228 if(type_variant == _TT_ANY && tag_mode != 1)
vlm72425de2004-09-13 08:31:01 +0000229 APPEND(buf_ptr, rval.consumed);
vlmfa67ddc2004-06-03 03:38:44 +0000230 ADVANCE(rval.consumed);
231 goto phase3;
232 }
233
vlmfa67ddc2004-06-03 03:38:44 +0000234 NEXT_PHASE(ctx);
235 /* Fall through */
236 case 1:
237 phase1:
238 /*
239 * Fill the stack with expectations.
240 */
vlmd3c80792004-12-15 23:23:53 +0000241 stck = (struct _stack *)ctx->ptr;
vlmfa67ddc2004-06-03 03:38:44 +0000242 sel = stck->cur_ptr;
243 do {
244 ber_tlv_tag_t tlv_tag;
245 ber_tlv_len_t tlv_len;
vlm72425de2004-09-13 08:31:01 +0000246 ber_tlv_tag_t expected_tag;
vlmdb6b2902004-09-27 20:54:06 +0000247 ssize_t tl, ll, tlvl;
vlm6678cb12004-09-26 13:10:40 +0000248 /* This one works even if (sel->left == -1) */
249 ssize_t Left = ((!sel||(size_t)sel->left >= size)
vlmdb6b2902004-09-27 20:54:06 +0000250 ?(ssize_t)size:sel->left);
vlmfa67ddc2004-06-03 03:38:44 +0000251
vlmb33ed682004-09-24 20:57:41 +0000252
vlma351c2b2004-10-26 10:12:14 +0000253 ASN_DEBUG("%p, s->l=%ld, s->wn=%ld, s->g=%ld\n", sel,
vlm13318c12004-10-26 10:56:10 +0000254 (long)(sel?sel->left:0),
255 (long)(sel?sel->want_nulls:0),
256 (long)(sel?sel->got:0)
vlmdb6b2902004-09-27 20:54:06 +0000257 );
258 if(sel && sel->left <= 0 && sel->want_nulls == 0) {
259 if(sel->prev) {
260 struct _stack_el *prev = sel->prev;
261 if(prev->left != -1) {
262 if(prev->left < sel->got)
263 RETURN(RC_FAIL);
264 prev->left -= sel->got;
265 }
266 prev->got += sel->got;
267 sel = stck->cur_ptr = prev;
268 if(!sel) break;
269 tlv_constr = 1;
270 continue;
271 } else {
272 sel = stck->cur_ptr = 0;
273 break; /* Nothing to wait */
274 }
275 }
276
vlmb33ed682004-09-24 20:57:41 +0000277 tl = ber_fetch_tag(buf_ptr, Left, &tlv_tag);
vlma351c2b2004-10-26 10:12:14 +0000278 ASN_DEBUG("fetch tag(size=%ld,L=%ld), %sstack, left=%ld, wn=%ld, tl=%ld",
279 (long)size, (long)Left, sel?"":"!",
vlm13318c12004-10-26 10:56:10 +0000280 (long)(sel?sel->left:0),
281 (long)(sel?sel->want_nulls:0),
vlma351c2b2004-10-26 10:12:14 +0000282 (long)tl);
vlmfa67ddc2004-06-03 03:38:44 +0000283 switch(tl) {
284 case -1: RETURN(RC_FAIL);
285 case 0: RETURN(RC_WMORE);
286 }
287
288 tlv_constr = BER_TLV_CONSTRUCTED(buf_ptr);
289
290 ll = ber_fetch_length(tlv_constr,
vlmb02dcc62005-03-10 18:52:02 +0000291 (const char *)buf_ptr + tl,Left - tl,&tlv_len);
vlmdc20efc2005-03-02 01:54:28 +0000292 ASN_DEBUG("Got tag=%s, tc=%d, left=%ld, tl=%ld, len=%ld, ll=%ld",
vlmcdfdb722004-09-04 04:44:50 +0000293 ber_tlv_tag_string(tlv_tag), tlv_constr,
vlmdc20efc2005-03-02 01:54:28 +0000294 (long)Left, (long)tl, (long)tlv_len, (long)ll);
vlmfa67ddc2004-06-03 03:38:44 +0000295 switch(ll) {
296 case -1: RETURN(RC_FAIL);
297 case 0: RETURN(RC_WMORE);
298 }
299
vlmcdfdb722004-09-04 04:44:50 +0000300 if(sel && sel->want_nulls
vlmb02dcc62005-03-10 18:52:02 +0000301 && ((const uint8_t *)buf_ptr)[0] == 0
302 && ((const uint8_t *)buf_ptr)[1] == 0)
vlmfa67ddc2004-06-03 03:38:44 +0000303 {
vlmcdfdb722004-09-04 04:44:50 +0000304
305 ASN_DEBUG("Eat EOC; wn=%d--", sel->want_nulls);
306
vlm0f1ab762004-10-12 05:57:23 +0000307 if(type_variant == _TT_ANY
vlm267b7fd2004-10-11 11:43:08 +0000308 && (tag_mode != 1 || sel->cont_level))
309 APPEND("\0\0", 2);
vlmdb6b2902004-09-27 20:54:06 +0000310
311 ADVANCE(2);
312 sel->got += 2;
313 if(sel->left != -1) {
314 sel->left -= 2; /* assert(sel->left >= 2) */
315 }
316
vlmfa67ddc2004-06-03 03:38:44 +0000317 sel->want_nulls--;
318 if(sel->want_nulls == 0) {
319 /* Move to the next expectation */
vlmdb6b2902004-09-27 20:54:06 +0000320 sel->left = 0;
vlmfa67ddc2004-06-03 03:38:44 +0000321 tlv_constr = 1;
vlmfa67ddc2004-06-03 03:38:44 +0000322 }
vlmcdfdb722004-09-04 04:44:50 +0000323
324 continue;
vlm72425de2004-09-13 08:31:01 +0000325 }
326
327 /*
328 * Set up expected tags,
329 * depending on ASN.1 type being decoded.
330 */
vlm0f1ab762004-10-12 05:57:23 +0000331 switch(type_variant) {
vlm72425de2004-09-13 08:31:01 +0000332 case _TT_BIT_STRING:
333 /* X.690: 8.6.4.1, NOTE 2 */
334 /* Fall through */
335 case _TT_GENERIC:
336 default:
337 if(sel) {
338 int level = sel->cont_level;
339 if(level < td->all_tags_count) {
340 expected_tag = td->all_tags[level];
341 break;
342 } else if(td->all_tags_count) {
343 expected_tag = td->all_tags
344 [td->all_tags_count - 1];
345 break;
346 }
347 /* else, Fall through */
348 }
349 /* Fall through */
350 case _TT_ANY:
351 expected_tag = tlv_tag;
352 break;
353 }
354
355
356 if(tlv_tag != expected_tag) {
vlmfa67ddc2004-06-03 03:38:44 +0000357 char buf[2][32];
358 ber_tlv_tag_snprint(tlv_tag,
359 buf[0], sizeof(buf[0]));
360 ber_tlv_tag_snprint(td->tags[td->tags_count-1],
361 buf[1], sizeof(buf[1]));
362 ASN_DEBUG("Tag does not match expectation: %s != %s",
363 buf[0], buf[1]);
364 RETURN(RC_FAIL);
365 }
366
vlm7b75d982004-09-29 13:20:14 +0000367 tlvl = tl + ll; /* Combined length of T and L encoding */
368 if((tlv_len + tlvl) < 0) {
369 /* tlv_len value is too big */
370 ASN_DEBUG("TLV encoding + length (%ld) is too big",
371 (long)tlv_len);
372 RETURN(RC_FAIL);
373 }
374
vlmfa67ddc2004-06-03 03:38:44 +0000375 /*
376 * Append a new expectation.
377 */
vlmb33ed682004-09-24 20:57:41 +0000378 sel = OS__add_stack_el(stck);
vlmdb6b2902004-09-27 20:54:06 +0000379 if(!sel) RETURN(RC_FAIL);
vlmfa67ddc2004-06-03 03:38:44 +0000380
vlmdb6b2902004-09-27 20:54:06 +0000381 sel->tag = tlv_tag;
382
383 sel->want_nulls = (tlv_len==-1);
384 if(sel->prev && sel->prev->left != -1) {
385 /* Check that the parent frame is big enough */
386 if(sel->prev->left < tlvl + (tlv_len==-1?0:tlv_len))
387 RETURN(RC_FAIL);
388 if(tlv_len == -1)
389 sel->left = sel->prev->left - tlvl;
390 else
391 sel->left = tlv_len;
392 } else {
393 sel->left = tlv_len;
394 }
vlm0f1ab762004-10-12 05:57:23 +0000395 if(type_variant == _TT_ANY
vlm267b7fd2004-10-11 11:43:08 +0000396 && (tag_mode != 1 || sel->cont_level))
397 APPEND(buf_ptr, tlvl);
vlmdb6b2902004-09-27 20:54:06 +0000398 sel->got += tlvl;
399 ADVANCE(tlvl);
400
vlma351c2b2004-10-26 10:12:14 +0000401 ASN_DEBUG("+EXPECT2 got=%ld left=%ld, wn=%d, clvl=%d",
402 (long)sel->got, (long)sel->left,
403 sel->want_nulls, sel->cont_level);
vlmdb6b2902004-09-27 20:54:06 +0000404
vlmfa67ddc2004-06-03 03:38:44 +0000405 } while(tlv_constr);
406 if(sel == NULL) {
407 /* Finished operation, "phase out" */
vlm72425de2004-09-13 08:31:01 +0000408 ASN_DEBUG("Phase out");
vlmfa67ddc2004-06-03 03:38:44 +0000409 _CH_PHASE(ctx, +3);
410 break;
411 }
412
413 NEXT_PHASE(ctx);
414 /* Fall through */
415 case 2:
vlmd3c80792004-12-15 23:23:53 +0000416 stck = (struct _stack *)ctx->ptr;
vlmfa67ddc2004-06-03 03:38:44 +0000417 sel = stck->cur_ptr;
vlmdb6b2902004-09-27 20:54:06 +0000418 ASN_DEBUG("Phase 2: Need %ld bytes, size=%ld, alrg=%ld, wn=%d",
419 (long)sel->left, (long)size, (long)sel->got,
420 sel->want_nulls);
vlmfa67ddc2004-06-03 03:38:44 +0000421 {
422 ber_tlv_len_t len;
423
424 assert(sel->left >= 0);
425
vlmc5190612004-08-18 04:53:32 +0000426 len = ((ber_tlv_len_t)size < sel->left)
427 ? (ber_tlv_len_t)size : sel->left;
vlmfa67ddc2004-06-03 03:38:44 +0000428 if(len > 0) {
vlm0f1ab762004-10-12 05:57:23 +0000429 if(type_variant == _TT_BIT_STRING
vlm72425de2004-09-13 08:31:01 +0000430 && sel->bits_chopped == 0) {
vlm0f1ab762004-10-12 05:57:23 +0000431 /* Put the unused-bits-octet away */
vlmb02dcc62005-03-10 18:52:02 +0000432 st->bits_unused = *(const uint8_t *)buf_ptr;
433 APPEND(((const char *)buf_ptr+1), (len - 1));
vlmfa67ddc2004-06-03 03:38:44 +0000434 sel->bits_chopped = 1;
435 } else {
436 APPEND(buf_ptr, len);
437 }
438 ADVANCE(len);
439 sel->left -= len;
vlmdb6b2902004-09-27 20:54:06 +0000440 sel->got += len;
vlmfa67ddc2004-06-03 03:38:44 +0000441 }
442
vlmdb6b2902004-09-27 20:54:06 +0000443 if(sel->left) {
444 ASN_DEBUG("OS left %ld, size = %ld, wn=%d\n",
445 (long)sel->left, (long)size, sel->want_nulls);
vlmfa67ddc2004-06-03 03:38:44 +0000446 RETURN(RC_WMORE);
vlmfa67ddc2004-06-03 03:38:44 +0000447 }
vlmdb6b2902004-09-27 20:54:06 +0000448
449 PREV_PHASE(ctx);
450 goto phase1;
vlmfa67ddc2004-06-03 03:38:44 +0000451 }
452 break;
453 case 3:
454 phase3:
455 /*
456 * Primitive form, no stack required.
457 */
vlm0f1ab762004-10-12 05:57:23 +0000458 assert(ctx->left >= 0);
459
vlmb42843a2004-06-05 08:17:50 +0000460 if(size < (size_t)ctx->left) {
vlm0f1ab762004-10-12 05:57:23 +0000461 if(!size) RETURN(RC_WMORE);
vlm82abba82005-04-25 19:38:21 +0000462 if(type_variant == _TT_BIT_STRING && !ctx->context) {
vlmb02dcc62005-03-10 18:52:02 +0000463 st->bits_unused = *(const uint8_t *)buf_ptr;
vlm0f1ab762004-10-12 05:57:23 +0000464 ctx->left--;
465 ADVANCE(1);
466 }
vlmfa67ddc2004-06-03 03:38:44 +0000467 APPEND(buf_ptr, size);
vlm82abba82005-04-25 19:38:21 +0000468 assert(ctx->context > 0);
vlmfa67ddc2004-06-03 03:38:44 +0000469 ctx->left -= size;
470 ADVANCE(size);
471 RETURN(RC_WMORE);
472 } else {
vlm0f1ab762004-10-12 05:57:23 +0000473 if(type_variant == _TT_BIT_STRING
vlm82abba82005-04-25 19:38:21 +0000474 && !ctx->context && ctx->left) {
vlmb02dcc62005-03-10 18:52:02 +0000475 st->bits_unused = *(const uint8_t *)buf_ptr;
vlm0f1ab762004-10-12 05:57:23 +0000476 ctx->left--;
477 ADVANCE(1);
478 }
vlmfa67ddc2004-06-03 03:38:44 +0000479 APPEND(buf_ptr, ctx->left);
480 ADVANCE(ctx->left);
481 ctx->left = 0;
482
483 NEXT_PHASE(ctx);
484 }
485 break;
486 }
487
vlmdb6b2902004-09-27 20:54:06 +0000488 if(sel) {
489 ASN_DEBUG("3sel p=%p, wn=%d, l=%ld, g=%ld, size=%ld",
490 sel->prev, sel->want_nulls,
491 (long)sel->left, (long)sel->got, (long)size);
492 if(sel->prev || sel->want_nulls > 1 || sel->left > 0) {
493 RETURN(RC_WMORE);
494 }
495 }
496
vlmfa67ddc2004-06-03 03:38:44 +0000497 /*
498 * BIT STRING-specific processing.
499 */
vlm0f1ab762004-10-12 05:57:23 +0000500 if(type_variant == _TT_BIT_STRING && st->size) {
vlmfa67ddc2004-06-03 03:38:44 +0000501 /* Finalize BIT STRING: zero out unused bits. */
vlm0f1ab762004-10-12 05:57:23 +0000502 st->buf[st->size-1] &= 0xff << st->bits_unused;
vlmfa67ddc2004-06-03 03:38:44 +0000503 }
504
vlma351c2b2004-10-26 10:12:14 +0000505 ASN_DEBUG("Took %ld bytes to encode %s: [%s]:%ld",
506 (long)consumed_myself, td->name,
vlm0f1ab762004-10-12 05:57:23 +0000507 (type_variant == _TT_GENERIC) ? (char *)st->buf : "<data>",
vlma351c2b2004-10-26 10:12:14 +0000508 (long)st->size);
vlmfa67ddc2004-06-03 03:38:44 +0000509
vlmfa67ddc2004-06-03 03:38:44 +0000510
vlmdb6b2902004-09-27 20:54:06 +0000511 RETURN(RC_OK);
vlmfa67ddc2004-06-03 03:38:44 +0000512}
513
514/*
515 * Encode OCTET STRING type using DER.
516 */
vlm39ba4c42004-09-22 16:06:28 +0000517asn_enc_rval_t
vlm0f1ab762004-10-12 05:57:23 +0000518OCTET_STRING_encode_der(asn_TYPE_descriptor_t *td, void *sptr,
vlmfa67ddc2004-06-03 03:38:44 +0000519 int tag_mode, ber_tlv_tag_t tag,
520 asn_app_consume_bytes_f *cb, void *app_key) {
vlm0f1ab762004-10-12 05:57:23 +0000521 asn_enc_rval_t er;
522 asn_OCTET_STRING_specifics_t *specs = td->specifics
vlm9de248e2004-10-20 15:50:55 +0000523 ? (asn_OCTET_STRING_specifics_t *)td->specifics
524 : &asn_DEF_OCTET_STRING_specs;
vlm0f1ab762004-10-12 05:57:23 +0000525 BIT_STRING_t *st = (BIT_STRING_t *)sptr;
526 OS_type_e type_variant = (OS_type_e)specs->subvariant;
527 int fix_last_byte = 0;
vlmfa67ddc2004-06-03 03:38:44 +0000528
529 ASN_DEBUG("%s %s as OCTET STRING",
vlm3e693022004-09-02 12:57:25 +0000530 cb?"Estimating":"Encoding", td->name);
vlmfa67ddc2004-06-03 03:38:44 +0000531
532 /*
vlm0f1ab762004-10-12 05:57:23 +0000533 * Write tags.
vlmfa67ddc2004-06-03 03:38:44 +0000534 */
vlm0f1ab762004-10-12 05:57:23 +0000535 if(type_variant != _TT_ANY || tag_mode == 1) {
536 er.encoded = der_write_tags(td,
537 (type_variant == _TT_BIT_STRING) + st->size,
538 tag_mode, type_variant == _TT_ANY, tag, cb, app_key);
539 if(er.encoded == -1) {
540 er.failed_type = td;
541 er.structure_ptr = sptr;
542 return er;
vlmfa67ddc2004-06-03 03:38:44 +0000543 }
vlm0f1ab762004-10-12 05:57:23 +0000544 } else {
545 /* Disallow: [<tag>] IMPLICIT ANY */
546 assert(type_variant != _TT_ANY || tag_mode != -1);
547 er.encoded = 0;
548 }
549
550 if(!cb) {
551 er.encoded += (type_variant == _TT_BIT_STRING) + st->size;
vlm6d44a542005-11-08 03:06:16 +0000552 er.structure_ptr = 0;
553 er.failed_type = 0;
vlm0f1ab762004-10-12 05:57:23 +0000554 return er;
vlmfa67ddc2004-06-03 03:38:44 +0000555 }
556
vlm267b7fd2004-10-11 11:43:08 +0000557 /*
vlm0f1ab762004-10-12 05:57:23 +0000558 * Prepare to deal with the last octet of BIT STRING.
vlm267b7fd2004-10-11 11:43:08 +0000559 */
vlm0f1ab762004-10-12 05:57:23 +0000560 if(type_variant == _TT_BIT_STRING) {
561 uint8_t b = st->bits_unused & 0x07;
562 if(b && st->size) fix_last_byte = 1;
563 _ASN_CALLBACK(&b, 1);
564 er.encoded++;
vlmfa67ddc2004-06-03 03:38:44 +0000565 }
566
vlm0f1ab762004-10-12 05:57:23 +0000567 /* Invoke callback for the main part of the buffer */
568 _ASN_CALLBACK(st->buf, st->size - fix_last_byte);
vlmfa67ddc2004-06-03 03:38:44 +0000569
vlm0f1ab762004-10-12 05:57:23 +0000570 /* The last octet should be stripped off the unused bits */
571 if(fix_last_byte) {
572 uint8_t b = st->buf[st->size-1] & (0xff << st->bits_unused);
573 _ASN_CALLBACK(&b, 1);
vlmfa67ddc2004-06-03 03:38:44 +0000574 }
575
vlm0f1ab762004-10-12 05:57:23 +0000576 er.encoded += st->size;
vlm6d44a542005-11-08 03:06:16 +0000577 er.structure_ptr = 0;
578 er.failed_type = 0;
vlm0f1ab762004-10-12 05:57:23 +0000579 return er;
580cb_failed:
581 _ASN_ENCODE_FAILED;
vlmfa67ddc2004-06-03 03:38:44 +0000582}
583
vlm39ba4c42004-09-22 16:06:28 +0000584asn_enc_rval_t
vlm7b75d982004-09-29 13:20:14 +0000585OCTET_STRING_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
vlm39ba4c42004-09-22 16:06:28 +0000586 int ilevel, enum xer_encoder_flags_e flags,
587 asn_app_consume_bytes_f *cb, void *app_key) {
588 static const char *h2c = "0123456789ABCDEF";
589 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
590 asn_enc_rval_t er;
591 char scratch[16 * 3 + 4];
592 char *p = scratch;
593 uint8_t *buf;
594 uint8_t *end;
595 size_t i;
596
vlm84d551b2004-10-03 09:13:02 +0000597 if(!st || !st->buf)
598 _ASN_ENCODE_FAILED;
vlm39ba4c42004-09-22 16:06:28 +0000599
600 er.encoded = 0;
601
602 /*
603 * Dump the contents of the buffer in hexadecimal.
604 */
605 buf = st->buf;
606 end = buf + st->size;
607 if(flags & XER_F_CANONICAL) {
608 char *scend = scratch + (sizeof(scratch) - 2);
609 for(; buf < end; buf++) {
610 if(p >= scend) {
611 _ASN_CALLBACK(scratch, p - scratch);
612 er.encoded += p - scratch;
613 p = scratch;
614 }
615 *p++ = h2c[(*buf >> 4) & 0x0F];
616 *p++ = h2c[*buf & 0x0F];
617 }
vlmef1b4c02004-09-23 22:06:26 +0000618
619 _ASN_CALLBACK(scratch, p-scratch); /* Dump the rest */
620 er.encoded += p - scratch;
vlm39ba4c42004-09-22 16:06:28 +0000621 } else {
622 for(i = 0; buf < end; buf++, i++) {
623 if(!(i % 16) && (i || st->size > 16)) {
624 _ASN_CALLBACK(scratch, p-scratch);
625 er.encoded += (p-scratch);
626 p = scratch;
627 _i_ASN_TEXT_INDENT(1, ilevel);
628 }
629 *p++ = h2c[(*buf >> 4) & 0x0F];
630 *p++ = h2c[*buf & 0x0F];
631 *p++ = 0x20;
632 }
vlmef1b4c02004-09-23 22:06:26 +0000633 if(p - scratch) {
634 p--; /* Remove the tail space */
635 _ASN_CALLBACK(scratch, p-scratch); /* Dump the rest */
636 er.encoded += p - scratch;
637 if(st->size > 16)
638 _i_ASN_TEXT_INDENT(1, ilevel-1);
639 }
vlm39ba4c42004-09-22 16:06:28 +0000640 }
641
vlmdcb9abb2005-11-13 09:49:05 +0000642 er.structure_ptr = 0;
643 er.failed_type = 0;
vlm39ba4c42004-09-22 16:06:28 +0000644 return er;
vlm84d551b2004-10-03 09:13:02 +0000645cb_failed:
646 _ASN_ENCODE_FAILED;
vlm39ba4c42004-09-22 16:06:28 +0000647}
648
vlm393a57a2004-10-03 10:54:25 +0000649static struct OCTET_STRING__xer_escape_table_s {
650 char *string;
651 int size;
652} OCTET_STRING__xer_escape_table[] = {
653#define OSXET(s) { s, sizeof(s) - 1 }
654 OSXET("\074\156\165\154\057\076"), /* <nul/> */
655 OSXET("\074\163\157\150\057\076"), /* <soh/> */
656 OSXET("\074\163\164\170\057\076"), /* <stx/> */
657 OSXET("\074\145\164\170\057\076"), /* <etx/> */
658 OSXET("\074\145\157\164\057\076"), /* <eot/> */
659 OSXET("\074\145\156\161\057\076"), /* <enq/> */
660 OSXET("\074\141\143\153\057\076"), /* <ack/> */
661 OSXET("\074\142\145\154\057\076"), /* <bel/> */
662 OSXET("\074\142\163\057\076"), /* <bs/> */
663 OSXET("\011"), /* \t */
664 OSXET("\012"), /* \n */
665 OSXET("\074\166\164\057\076"), /* <vt/> */
666 OSXET("\074\146\146\057\076"), /* <ff/> */
667 OSXET("\015"), /* \r */
668 OSXET("\074\163\157\057\076"), /* <so/> */
669 OSXET("\074\163\151\057\076"), /* <si/> */
670 OSXET("\074\144\154\145\057\076"), /* <dle/> */
671 OSXET("\074\144\143\061\057\076"), /* <de1/> */
672 OSXET("\074\144\143\062\057\076"), /* <de2/> */
673 OSXET("\074\144\143\063\057\076"), /* <de3/> */
674 OSXET("\074\144\143\064\057\076"), /* <de4/> */
675 OSXET("\074\156\141\153\057\076"), /* <nak/> */
676 OSXET("\074\163\171\156\057\076"), /* <syn/> */
677 OSXET("\074\145\164\142\057\076"), /* <etb/> */
678 OSXET("\074\143\141\156\057\076"), /* <can/> */
679 OSXET("\074\145\155\057\076"), /* <em/> */
680 OSXET("\074\163\165\142\057\076"), /* <sub/> */
681 OSXET("\074\145\163\143\057\076"), /* <esc/> */
682 OSXET("\074\151\163\064\057\076"), /* <is4/> */
683 OSXET("\074\151\163\063\057\076"), /* <is3/> */
684 OSXET("\074\151\163\062\057\076"), /* <is2/> */
685 OSXET("\074\151\163\061\057\076"), /* <is1/> */
686 { 0, 0 }, /* " " */
687 { 0, 0 }, /* ! */
688 { 0, 0 }, /* \" */
689 { 0, 0 }, /* # */
690 { 0, 0 }, /* $ */
691 { 0, 0 }, /* % */
692 OSXET("\046\141\155\160\073"), /* &amp; */
693 { 0, 0 }, /* ' */
694 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, /* ()*+,-./ */
695 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, /* 01234567 */
696 {0,0},{0,0},{0,0},{0,0}, /* 89:; */
697 OSXET("\046\154\164\073"), /* &lt; */
698 { 0, 0 }, /* = */
699 OSXET("\046\147\164\073"), /* &gt; */
700};
701
vlm9de248e2004-10-20 15:50:55 +0000702static int
vlm4df9cc12005-03-09 22:19:25 +0000703OS__check_escaped_control_char(const void *buf, int size) {
vlm9de248e2004-10-20 15:50:55 +0000704 size_t i;
705 /*
706 * Inefficient algorithm which translates the escape sequences
707 * defined above into characters. Returns -1 if not found.
708 * TODO: replace by a faster algorithm (bsearch(), hash or
709 * nested table lookups).
710 */
711 for(i = 0; i < 32 /* Don't spend time on the bottom half */; i++) {
712 struct OCTET_STRING__xer_escape_table_s *el;
713 el = &OCTET_STRING__xer_escape_table[i];
714 if(el->size == size && memcmp(buf, el->string, size) == 0)
715 return i;
716 }
717 return -1;
718}
719
720static int
vlm4df9cc12005-03-09 22:19:25 +0000721OCTET_STRING__handle_control_chars(void *struct_ptr, const void *chunk_buf, size_t chunk_size) {
vlm9de248e2004-10-20 15:50:55 +0000722 /*
723 * This might be one of the escape sequences
724 * for control characters. Check it out.
725 * #11.15.5
726 */
727 int control_char = OS__check_escaped_control_char(chunk_buf,chunk_size);
728 if(control_char >= 0) {
729 OCTET_STRING_t *st = (OCTET_STRING_t *)struct_ptr;
730 void *p = REALLOC(st->buf, st->size + 2);
731 if(p) {
732 st->buf = (uint8_t *)p;
733 st->buf[st->size++] = control_char;
734 st->buf[st->size] = '\0'; /* nul-termination */
735 return 0;
736 }
737 }
738
739 return -1; /* No, it's not */
740}
741
vlm39ba4c42004-09-22 16:06:28 +0000742asn_enc_rval_t
vlm9de248e2004-10-20 15:50:55 +0000743OCTET_STRING_encode_xer_utf8(asn_TYPE_descriptor_t *td, void *sptr,
vlm39ba4c42004-09-22 16:06:28 +0000744 int ilevel, enum xer_encoder_flags_e flags,
745 asn_app_consume_bytes_f *cb, void *app_key) {
746 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
747 asn_enc_rval_t er;
vlm393a57a2004-10-03 10:54:25 +0000748 uint8_t *buf, *end;
749 uint8_t *ss; /* Sequence start */
750 ssize_t encoded_len = 0;
vlm39ba4c42004-09-22 16:06:28 +0000751
752 (void)ilevel; /* Unused argument */
753 (void)flags; /* Unused argument */
754
755 if(!st || !st->buf)
756 _ASN_ENCODE_FAILED;
757
vlm393a57a2004-10-03 10:54:25 +0000758 buf = st->buf;
759 end = buf + st->size;
760 for(ss = buf; buf < end; buf++) {
vlmd7c2ef32004-10-05 06:35:31 +0000761 unsigned int ch = *buf;
vlm393a57a2004-10-03 10:54:25 +0000762 int s_len; /* Special encoding sequence length */
vlm39ba4c42004-09-22 16:06:28 +0000763
vlm393a57a2004-10-03 10:54:25 +0000764 /*
765 * Escape certain characters: X.680/11.15
766 */
767 if(ch < sizeof(OCTET_STRING__xer_escape_table)
768 /sizeof(OCTET_STRING__xer_escape_table[0])
769 && (s_len = OCTET_STRING__xer_escape_table[ch].size)) {
770 if(((buf - ss) && cb(ss, buf - ss, app_key) < 0)
771 || cb(OCTET_STRING__xer_escape_table[ch].string, s_len,
772 app_key) < 0)
773 _ASN_ENCODE_FAILED;
774 encoded_len += (buf - ss) + s_len;
775 ss = buf + 1;
776 }
777 }
778
779 encoded_len += (buf - ss);
780 if((buf - ss) && cb(ss, buf - ss, app_key) < 0)
781 _ASN_ENCODE_FAILED;
782
783 er.encoded = encoded_len;
vlmdcb9abb2005-11-13 09:49:05 +0000784 er.structure_ptr = 0;
785 er.failed_type = 0;
vlm39ba4c42004-09-22 16:06:28 +0000786 return er;
787}
788
vlm9de248e2004-10-20 15:50:55 +0000789/*
790 * Convert from hexadecimal format (cstring): "AB CD EF"
791 */
vlm4df9cc12005-03-09 22:19:25 +0000792static ssize_t OCTET_STRING__convert_hexadecimal(void *sptr, const void *chunk_buf, size_t chunk_size, int have_more) {
vlm9de248e2004-10-20 15:50:55 +0000793 OCTET_STRING_t *st = (OCTET_STRING_t *)sptr;
vlm4df9cc12005-03-09 22:19:25 +0000794 const char *chunk_stop = (const char *)chunk_buf;
795 const char *p = chunk_stop;
796 const char *pend = p + chunk_size;
vlm9de248e2004-10-20 15:50:55 +0000797 unsigned int clv = 0;
798 int half = 0; /* Half bit */
799 uint8_t *buf;
800
801 /* Reallocate buffer according to high cap estimation */
802 ssize_t _ns = st->size + (chunk_size + 1) / 2;
803 void *nptr = REALLOC(st->buf, _ns + 1);
804 if(!nptr) return -1;
805 st->buf = (uint8_t *)nptr;
806 buf = st->buf + st->size;
807
808 /*
809 * If something like " a b c " appears here, the " a b":3 will be
810 * converted, and the rest skipped. That is, unless buf_size is greater
811 * than chunk_size, then it'll be equivalent to "ABC0".
812 */
813 for(; p < pend; p++) {
vlm4df9cc12005-03-09 22:19:25 +0000814 int ch = *(const unsigned char *)p;
vlm9de248e2004-10-20 15:50:55 +0000815 switch(ch) {
816 case 0x09: case 0x0a: case 0x0c: case 0x0d:
817 case 0x20:
818 /* Ignore whitespace */
819 continue;
820 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: /*01234*/
821 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: /*56789*/
822 clv = (clv << 4) + (ch - 0x30);
823 break;
824 case 0x41: case 0x42: case 0x43: /* ABC */
825 case 0x44: case 0x45: case 0x46: /* DEF */
vlm2ada0992005-02-21 14:43:48 +0000826 clv = (clv << 4) + (ch - 0x41 + 10);
vlm9de248e2004-10-20 15:50:55 +0000827 break;
828 case 0x61: case 0x62: case 0x63: /* abc */
829 case 0x64: case 0x65: case 0x66: /* def */
vlm2ada0992005-02-21 14:43:48 +0000830 clv = (clv << 4) + (ch - 0x61 + 10);
vlm9de248e2004-10-20 15:50:55 +0000831 break;
832 default:
833 *buf = 0; /* JIC */
834 return -1;
835 }
836 if(half++) {
837 half = 0;
838 *buf++ = clv;
839 chunk_stop = p + 1;
840 }
841 }
842
843 /*
844 * Check partial decoding.
845 */
846 if(half) {
847 if(have_more) {
848 /*
849 * Partial specification is fine,
850 * because no more more PXER_TEXT data is available.
851 */
852 *buf++ = clv << 4;
853 chunk_stop = p;
854 }
855 } else {
856 chunk_stop = p;
857 }
858
859 st->size = buf - st->buf; /* Adjust the buffer size */
860 assert(st->size <= _ns);
861 st->buf[st->size] = 0; /* Courtesy termination */
862
vlm4df9cc12005-03-09 22:19:25 +0000863 return (chunk_stop - (const char *)chunk_buf); /* Converted size */
vlm9de248e2004-10-20 15:50:55 +0000864}
865
866/*
867 * Convert from binary format: "00101011101"
868 */
vlm4df9cc12005-03-09 22:19:25 +0000869static ssize_t OCTET_STRING__convert_binary(void *sptr, const void *chunk_buf, size_t chunk_size, int have_more) {
vlm9de248e2004-10-20 15:50:55 +0000870 BIT_STRING_t *st = (BIT_STRING_t *)sptr;
vlm4df9cc12005-03-09 22:19:25 +0000871 const char *p = (const char *)chunk_buf;
872 const char *pend = p + chunk_size;
vlm9de248e2004-10-20 15:50:55 +0000873 int bits_unused = st->bits_unused & 0x7;
874 uint8_t *buf;
875
876 /* Reallocate buffer according to high cap estimation */
877 ssize_t _ns = st->size + (chunk_size + 7) / 8;
878 void *nptr = REALLOC(st->buf, _ns + 1);
879 if(!nptr) return -1;
880 st->buf = (uint8_t *)nptr;
881 buf = st->buf + st->size;
882
883 (void)have_more;
884
885 if(bits_unused == 0)
886 bits_unused = 8;
887 else if(st->size)
888 buf--;
889
890 /*
891 * Convert series of 0 and 1 into the octet string.
892 */
893 for(; p < pend; p++) {
vlm4df9cc12005-03-09 22:19:25 +0000894 int ch = *(const unsigned char *)p;
vlm9de248e2004-10-20 15:50:55 +0000895 switch(ch) {
896 case 0x09: case 0x0a: case 0x0c: case 0x0d:
897 case 0x20:
898 /* Ignore whitespace */
899 break;
900 case 0x30:
901 case 0x31:
902 if(bits_unused-- <= 0) {
903 *++buf = 0; /* Clean the cell */
904 bits_unused = 7;
905 }
906 *buf |= (ch&1) << bits_unused;
907 break;
908 default:
909 st->bits_unused = bits_unused;
910 return -1;
911 }
912 }
913
914 if(bits_unused == 8) {
915 st->size = buf - st->buf;
916 st->bits_unused = 0;
917 } else {
918 st->size = buf - st->buf + 1;
919 st->bits_unused = bits_unused;
920 }
921
922 assert(st->size <= _ns);
923 st->buf[st->size] = 0; /* Courtesy termination */
924
925 return chunk_size; /* Converted in full */
926}
927
928/*
929 * Something like strtod(), but with stricter rules.
930 */
931static int
vlm4df9cc12005-03-09 22:19:25 +0000932OS__strtoent(int base, const char *buf, const char *end, int32_t *ret_value) {
vlm6c593842004-10-26 09:03:31 +0000933 int32_t val = 0;
vlm4df9cc12005-03-09 22:19:25 +0000934 const char *p;
vlm9de248e2004-10-20 15:50:55 +0000935
936 for(p = buf; p < end; p++) {
937 int ch = *p;
vlm6b11c602005-07-03 05:30:15 +0000938
939 /* Strange huge value */
940 if((val * base + base) < 0)
941 return -1;
942
vlm9de248e2004-10-20 15:50:55 +0000943 switch(ch) {
944 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: /*01234*/
945 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: /*56789*/
946 val = val * base + (ch - 0x30);
947 break;
948 case 0x41: case 0x42: case 0x43: /* ABC */
949 case 0x44: case 0x45: case 0x46: /* DEF */
vlm2ada0992005-02-21 14:43:48 +0000950 val = val * base + (ch - 0x41 + 10);
vlm9de248e2004-10-20 15:50:55 +0000951 break;
952 case 0x61: case 0x62: case 0x63: /* abc */
953 case 0x64: case 0x65: case 0x66: /* def */
vlm2ada0992005-02-21 14:43:48 +0000954 val = val * base + (ch - 0x61 + 10);
vlm9de248e2004-10-20 15:50:55 +0000955 break;
956 case 0x3b: /* ';' */
vlm4df9cc12005-03-09 22:19:25 +0000957 *ret_value = val;
vlm9de248e2004-10-20 15:50:55 +0000958 return (p - buf) + 1;
959 default:
960 return -1; /* Character set error */
961 }
962 }
963
vlm6b11c602005-07-03 05:30:15 +0000964 *ret_value = -1;
vlm9de248e2004-10-20 15:50:55 +0000965 return (p - buf);
966}
967
968/*
969 * Convert from the plain UTF-8 format, expanding entity references: "2 &lt; 3"
970 */
vlm4df9cc12005-03-09 22:19:25 +0000971static ssize_t OCTET_STRING__convert_entrefs(void *sptr, const void *chunk_buf, size_t chunk_size, int have_more) {
vlm9de248e2004-10-20 15:50:55 +0000972 OCTET_STRING_t *st = (OCTET_STRING_t *)sptr;
vlm4df9cc12005-03-09 22:19:25 +0000973 const char *p = (const char *)chunk_buf;
974 const char *pend = p + chunk_size;
vlm9de248e2004-10-20 15:50:55 +0000975 uint8_t *buf;
976
977 /* Reallocate buffer */
978 ssize_t _ns = st->size + chunk_size;
979 void *nptr = REALLOC(st->buf, _ns + 1);
980 if(!nptr) return -1;
981 st->buf = (uint8_t *)nptr;
982 buf = st->buf + st->size;
983
984 /*
985 * Convert series of 0 and 1 into the octet string.
986 */
987 for(; p < pend; p++) {
vlm4df9cc12005-03-09 22:19:25 +0000988 int ch = *(const unsigned char *)p;
vlm9de248e2004-10-20 15:50:55 +0000989 int len; /* Length of the rest of the chunk */
990
991 if(ch != 0x26 /* '&' */) {
992 *buf++ = ch;
993 continue; /* That was easy... */
994 }
995
996 /*
997 * Process entity reference.
998 */
vlm4df9cc12005-03-09 22:19:25 +0000999 len = chunk_size - (p - (const char *)chunk_buf);
vlm9de248e2004-10-20 15:50:55 +00001000 if(len == 1 /* "&" */) goto want_more;
1001 if(p[1] == 0x23 /* '#' */) {
vlm4df9cc12005-03-09 22:19:25 +00001002 const char *pval; /* Pointer to start of digits */
1003 int32_t val; /* Entity reference value */
vlm9de248e2004-10-20 15:50:55 +00001004 int base;
1005
1006 if(len == 2 /* "&#" */) goto want_more;
1007 if(p[2] == 0x78 /* 'x' */)
1008 pval = p + 3, base = 16;
1009 else
1010 pval = p + 2, base = 10;
1011 len = OS__strtoent(base, pval, p + len, &val);
1012 if(len == -1) {
1013 /* Invalid charset. Just copy verbatim. */
1014 *buf++ = ch;
1015 continue;
1016 }
1017 if(!len || pval[len-1] != 0x3b) goto want_more;
1018 assert(val > 0);
1019 p += (pval - p) + len - 1; /* Advance past entref */
1020
1021 if(val < 0x80) {
1022 *buf++ = (char)val;
1023 } else if(val < 0x800) {
1024 *buf++ = 0xc0 | ((val >> 6));
1025 *buf++ = 0x80 | ((val & 0x3f));
1026 } else if(val < 0x10000) {
1027 *buf++ = 0xe0 | ((val >> 12));
1028 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1029 *buf++ = 0x80 | ((val & 0x3f));
1030 } else if(val < 0x200000) {
1031 *buf++ = 0xf0 | ((val >> 18));
1032 *buf++ = 0x80 | ((val >> 12) & 0x3f);
1033 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1034 *buf++ = 0x80 | ((val & 0x3f));
1035 } else if(val < 0x4000000) {
1036 *buf++ = 0xf8 | ((val >> 24));
1037 *buf++ = 0x80 | ((val >> 18) & 0x3f);
1038 *buf++ = 0x80 | ((val >> 12) & 0x3f);
1039 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1040 *buf++ = 0x80 | ((val & 0x3f));
1041 } else {
1042 *buf++ = 0xfc | ((val >> 30) & 0x1);
1043 *buf++ = 0x80 | ((val >> 24) & 0x3f);
1044 *buf++ = 0x80 | ((val >> 18) & 0x3f);
1045 *buf++ = 0x80 | ((val >> 12) & 0x3f);
1046 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1047 *buf++ = 0x80 | ((val & 0x3f));
1048 }
1049 } else {
1050 /*
1051 * Ugly, limited parsing of &amp; &gt; &lt;
1052 */
1053 char *sc = (char *)memchr(p, 0x3b, len > 5 ? 5 : len);
1054 if(!sc) goto want_more;
1055 if((sc - p) == 4
1056 && p[1] == 0x61 /* 'a' */
1057 && p[2] == 0x6d /* 'm' */
1058 && p[3] == 0x70 /* 'p' */) {
1059 *buf++ = 0x26;
1060 p = sc;
1061 continue;
1062 }
1063 if((sc - p) == 3) {
1064 if(p[1] == 0x6c) {
1065 *buf = 0x3c; /* '<' */
1066 } else if(p[1] == 0x67) {
1067 *buf = 0x3e; /* '>' */
1068 } else {
1069 /* Unsupported entity reference */
1070 *buf++ = ch;
1071 continue;
1072 }
1073 if(p[2] != 0x74) {
1074 /* Unsupported entity reference */
1075 *buf++ = ch;
1076 continue;
1077 }
1078 buf++;
1079 p = sc;
1080 continue;
1081 }
1082 /* Unsupported entity reference */
1083 *buf++ = ch;
1084 }
1085
1086 continue;
1087 want_more:
1088 if(have_more) {
1089 /*
1090 * We know that no more data (of the same type)
1091 * is coming. Copy the rest verbatim.
1092 */
1093 *buf++ = ch;
1094 continue;
1095 }
vlm4df9cc12005-03-09 22:19:25 +00001096 chunk_size = (p - (const char *)chunk_buf);
vlm9de248e2004-10-20 15:50:55 +00001097 /* Processing stalled: need more data */
vlm97f4e0d2005-02-18 12:25:47 +00001098 break;
vlm9de248e2004-10-20 15:50:55 +00001099 }
1100
1101 st->size = buf - st->buf;
1102 assert(st->size <= _ns);
1103 st->buf[st->size] = 0; /* Courtesy termination */
1104
1105 return chunk_size; /* Converted in full */
1106}
1107
1108/*
1109 * Decode OCTET STRING from the XML element's body.
1110 */
1111static asn_dec_rval_t
1112OCTET_STRING__decode_xer(asn_codec_ctx_t *opt_codec_ctx,
1113 asn_TYPE_descriptor_t *td, void **sptr,
vlmb02dcc62005-03-10 18:52:02 +00001114 const char *opt_mname, const void *buf_ptr, size_t size,
vlm9de248e2004-10-20 15:50:55 +00001115 int (*opt_unexpected_tag_decoder)
vlm4df9cc12005-03-09 22:19:25 +00001116 (void *struct_ptr, const void *chunk_buf, size_t chunk_size),
vlm9de248e2004-10-20 15:50:55 +00001117 ssize_t (*body_receiver)
vlm4df9cc12005-03-09 22:19:25 +00001118 (void *struct_ptr, const void *chunk_buf, size_t chunk_size,
vlm9de248e2004-10-20 15:50:55 +00001119 int have_more)
1120) {
vlmedf21972005-02-14 20:08:00 +00001121 OCTET_STRING_t *st = (OCTET_STRING_t *)*sptr;
vlm9de248e2004-10-20 15:50:55 +00001122 asn_OCTET_STRING_specifics_t *specs = td->specifics
1123 ? (asn_OCTET_STRING_specifics_t *)td->specifics
1124 : &asn_DEF_OCTET_STRING_specs;
1125 const char *xml_tag = opt_mname ? opt_mname : td->xml_tag;
1126 asn_struct_ctx_t *ctx; /* Per-structure parser context */
vlm390a9022005-02-14 17:21:22 +00001127 asn_dec_rval_t rval; /* Return value from the decoder */
1128 int st_allocated;
vlm9de248e2004-10-20 15:50:55 +00001129
1130 /*
1131 * Create the string if does not exist.
1132 */
vlm390a9022005-02-14 17:21:22 +00001133 if(!st) {
vlm82e317b2004-12-14 13:31:01 +00001134 st = (OCTET_STRING_t *)CALLOC(1, specs->struct_size);
vlm82e317b2004-12-14 13:31:01 +00001135 *sptr = (void *)st;
vlm390a9022005-02-14 17:21:22 +00001136 if(!st) goto sta_failed;
1137 st_allocated = 1;
vlm97f4e0d2005-02-18 12:25:47 +00001138 } else {
1139 st_allocated = 0;
1140 }
vlm390a9022005-02-14 17:21:22 +00001141 if(!st->buf) {
1142 /* This is separate from above section */
1143 st->buf = (uint8_t *)CALLOC(1, 1);
1144 if(!st->buf) {
1145 if(st_allocated) {
1146 *sptr = 0;
1147 goto stb_failed;
1148 } else {
1149 goto sta_failed;
1150 }
vlm9de248e2004-10-20 15:50:55 +00001151 }
1152 }
1153
1154 /* Restore parsing context */
1155 ctx = (asn_struct_ctx_t *)(((char *)*sptr) + specs->ctx_offset);
1156
1157 return xer_decode_general(opt_codec_ctx, ctx, *sptr, xml_tag,
1158 buf_ptr, size, opt_unexpected_tag_decoder, body_receiver);
vlm390a9022005-02-14 17:21:22 +00001159
1160stb_failed:
1161 FREEMEM(st);
1162sta_failed:
1163 rval.code = RC_FAIL;
1164 rval.consumed = 0;
1165 return rval;
vlm9de248e2004-10-20 15:50:55 +00001166}
1167
1168/*
1169 * Decode OCTET STRING from the hexadecimal data.
1170 */
1171asn_dec_rval_t
1172OCTET_STRING_decode_xer_hex(asn_codec_ctx_t *opt_codec_ctx,
1173 asn_TYPE_descriptor_t *td, void **sptr,
vlmb02dcc62005-03-10 18:52:02 +00001174 const char *opt_mname, const void *buf_ptr, size_t size) {
vlm9de248e2004-10-20 15:50:55 +00001175 return OCTET_STRING__decode_xer(opt_codec_ctx, td, sptr, opt_mname,
1176 buf_ptr, size, 0, OCTET_STRING__convert_hexadecimal);
1177}
1178
1179/*
1180 * Decode OCTET STRING from the binary (0/1) data.
1181 */
1182asn_dec_rval_t
1183OCTET_STRING_decode_xer_binary(asn_codec_ctx_t *opt_codec_ctx,
1184 asn_TYPE_descriptor_t *td, void **sptr,
vlmb02dcc62005-03-10 18:52:02 +00001185 const char *opt_mname, const void *buf_ptr, size_t size) {
vlm9de248e2004-10-20 15:50:55 +00001186 return OCTET_STRING__decode_xer(opt_codec_ctx, td, sptr, opt_mname,
1187 buf_ptr, size, 0, OCTET_STRING__convert_binary);
1188}
1189
1190/*
1191 * Decode OCTET STRING from the string (ASCII/UTF-8) data.
1192 */
1193asn_dec_rval_t
1194OCTET_STRING_decode_xer_utf8(asn_codec_ctx_t *opt_codec_ctx,
1195 asn_TYPE_descriptor_t *td, void **sptr,
vlmb02dcc62005-03-10 18:52:02 +00001196 const char *opt_mname, const void *buf_ptr, size_t size) {
vlm9de248e2004-10-20 15:50:55 +00001197 return OCTET_STRING__decode_xer(opt_codec_ctx, td, sptr, opt_mname,
1198 buf_ptr, size,
1199 OCTET_STRING__handle_control_chars,
1200 OCTET_STRING__convert_entrefs);
1201}
1202
1203
vlmfa67ddc2004-06-03 03:38:44 +00001204int
vlm7b75d982004-09-29 13:20:14 +00001205OCTET_STRING_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
vlmfa67ddc2004-06-03 03:38:44 +00001206 asn_app_consume_bytes_f *cb, void *app_key) {
vlm1ff928d2004-08-11 08:10:13 +00001207 static const char *h2c = "0123456789ABCDEF";
vlmda674682004-08-11 09:07:36 +00001208 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
vlmfa67ddc2004-06-03 03:38:44 +00001209 char scratch[16 * 3 + 4];
1210 char *p = scratch;
1211 uint8_t *buf;
1212 uint8_t *end;
1213 size_t i;
vlmfa67ddc2004-06-03 03:38:44 +00001214
vlmb42843a2004-06-05 08:17:50 +00001215 (void)td; /* Unused argument */
1216
vlm6678cb12004-09-26 13:10:40 +00001217 if(!st || !st->buf) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
vlmfa67ddc2004-06-03 03:38:44 +00001218
1219 /*
1220 * Dump the contents of the buffer in hexadecimal.
1221 */
1222 buf = st->buf;
1223 end = buf + st->size;
1224 for(i = 0; buf < end; buf++, i++) {
1225 if(!(i % 16) && (i || st->size > 16)) {
vlm6678cb12004-09-26 13:10:40 +00001226 if(cb(scratch, p - scratch, app_key) < 0)
vlmfa67ddc2004-06-03 03:38:44 +00001227 return -1;
vlm6678cb12004-09-26 13:10:40 +00001228 _i_INDENT(1);
vlmfa67ddc2004-06-03 03:38:44 +00001229 p = scratch;
1230 }
1231 *p++ = h2c[(*buf >> 4) & 0x0F];
1232 *p++ = h2c[*buf & 0x0F];
vlm39ba4c42004-09-22 16:06:28 +00001233 *p++ = 0x20;
vlmfa67ddc2004-06-03 03:38:44 +00001234 }
1235
vlmef1b4c02004-09-23 22:06:26 +00001236 if(p > scratch) {
1237 p--; /* Remove the tail space */
vlm6678cb12004-09-26 13:10:40 +00001238 if(cb(scratch, p - scratch, app_key) < 0)
vlmef1b4c02004-09-23 22:06:26 +00001239 return -1;
1240 }
1241
1242 return 0;
vlmfa67ddc2004-06-03 03:38:44 +00001243}
1244
1245int
vlm9de248e2004-10-20 15:50:55 +00001246OCTET_STRING_print_utf8(asn_TYPE_descriptor_t *td, const void *sptr,
vlmfa67ddc2004-06-03 03:38:44 +00001247 int ilevel, asn_app_consume_bytes_f *cb, void *app_key) {
vlmda674682004-08-11 09:07:36 +00001248 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
vlmfa67ddc2004-06-03 03:38:44 +00001249
vlmb42843a2004-06-05 08:17:50 +00001250 (void)td; /* Unused argument */
1251 (void)ilevel; /* Unused argument */
1252
vlmfa67ddc2004-06-03 03:38:44 +00001253 if(st && st->buf) {
vlm6678cb12004-09-26 13:10:40 +00001254 return (cb(st->buf, st->size, app_key) < 0) ? -1 : 0;
vlmfa67ddc2004-06-03 03:38:44 +00001255 } else {
vlm6678cb12004-09-26 13:10:40 +00001256 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
vlmfa67ddc2004-06-03 03:38:44 +00001257 }
1258}
1259
1260void
vlm7b75d982004-09-29 13:20:14 +00001261OCTET_STRING_free(asn_TYPE_descriptor_t *td, void *sptr, int contents_only) {
vlmda674682004-08-11 09:07:36 +00001262 OCTET_STRING_t *st = (OCTET_STRING_t *)sptr;
vlm0f1ab762004-10-12 05:57:23 +00001263 asn_OCTET_STRING_specifics_t *specs = td->specifics
vlm9de248e2004-10-20 15:50:55 +00001264 ? (asn_OCTET_STRING_specifics_t *)td->specifics
1265 : &asn_DEF_OCTET_STRING_specs;
vlm0f1ab762004-10-12 05:57:23 +00001266 asn_struct_ctx_t *ctx = (asn_struct_ctx_t *)
1267 ((char *)st + specs->ctx_offset);
vlme2375902004-11-08 10:47:12 +00001268 struct _stack *stck;
vlmfa67ddc2004-06-03 03:38:44 +00001269
1270 if(!td || !st)
1271 return;
1272
1273 ASN_DEBUG("Freeing %s as OCTET STRING", td->name);
1274
1275 if(st->buf) {
1276 FREEMEM(st->buf);
1277 }
1278
1279 /*
1280 * Remove decode-time stack.
1281 */
vlme2375902004-11-08 10:47:12 +00001282 stck = (struct _stack *)ctx->ptr;
vlmfa67ddc2004-06-03 03:38:44 +00001283 if(stck) {
1284 while(stck->tail) {
1285 struct _stack_el *sel = stck->tail;
1286 stck->tail = sel->prev;
1287 FREEMEM(sel);
1288 }
1289 FREEMEM(stck);
1290 }
1291
1292 if(!contents_only) {
1293 FREEMEM(st);
1294 }
1295}
1296
1297/*
1298 * Conversion routines.
1299 */
1300int
1301OCTET_STRING_fromBuf(OCTET_STRING_t *st, const char *str, int len) {
1302 void *buf;
1303
1304 if(st == 0 || (str == 0 && len)) {
1305 errno = EINVAL;
1306 return -1;
1307 }
1308
1309 /*
1310 * Clear the OCTET STRING.
1311 */
1312 if(str == NULL) {
1313 if(st->buf)
1314 FREEMEM(st->buf);
1315 st->size = 0;
1316 return 0;
1317 }
1318
1319 /* Determine the original string size, if not explicitly given */
1320 if(len < 0)
1321 len = strlen(str);
1322
1323 /* Allocate and fill the memory */
1324 buf = MALLOC(len + 1);
1325 if(buf == NULL) {
1326 return -1;
1327 } else {
vlmda674682004-08-11 09:07:36 +00001328 st->buf = (uint8_t *)buf;
vlmfa67ddc2004-06-03 03:38:44 +00001329 st->size = len;
1330 }
1331
1332 memcpy(buf, str, len);
1333 st->buf[st->size] = '\0'; /* Couldn't use memcpy(len+1)! */
1334
1335 return 0;
1336}
1337
1338OCTET_STRING_t *
vlm0f1ab762004-10-12 05:57:23 +00001339OCTET_STRING_new_fromBuf(asn_TYPE_descriptor_t *td, const char *str, int len) {
1340 asn_OCTET_STRING_specifics_t *specs = td->specifics
vlm9de248e2004-10-20 15:50:55 +00001341 ? (asn_OCTET_STRING_specifics_t *)td->specifics
1342 : &asn_DEF_OCTET_STRING_specs;
vlmfa67ddc2004-06-03 03:38:44 +00001343 OCTET_STRING_t *st;
1344
vlm0f1ab762004-10-12 05:57:23 +00001345 st = (OCTET_STRING_t *)CALLOC(1, specs->struct_size);
vlmfa67ddc2004-06-03 03:38:44 +00001346 if(st && str && OCTET_STRING_fromBuf(st, str, len)) {
1347 free(st);
1348 st = NULL;
1349 }
1350
1351 return st;
1352}
1353