blob: 19ae95dbfd9956d453f9527962542e85fee677e3 [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001/*-
Lev Walkin75b1bef2005-04-25 19:38:21 +00002 * Copyright (c) 2003, 2004, 2005 Lev Walkin <vlm@lionet.info>.
3 * All rights reserved.
Lev Walkinf15320b2004-06-03 03:38:44 +00004 * Redistribution and modifications are permitted subject to BSD license.
5 */
Lev Walkina9cc46e2004-09-22 16:06:28 +00006#include <asn_internal.h>
Lev Walkinf15320b2004-06-03 03:38:44 +00007#include <OCTET_STRING.h>
Lev Walkinbbd93252004-10-12 05:57:23 +00008#include <BIT_STRING.h> /* for .bits_unused member */
Lev Walkinf15320b2004-06-03 03:38:44 +00009#include <errno.h>
10
11/*
12 * OCTET STRING basic type description.
13 */
Lev Walkinde4825d2004-09-29 13:20:14 +000014static ber_tlv_tag_t asn_DEF_OCTET_STRING_tags[] = {
Lev Walkinf15320b2004-06-03 03:38:44 +000015 (ASN_TAG_CLASS_UNIVERSAL | (4 << 2))
16};
Lev Walkinbbd93252004-10-12 05:57:23 +000017static asn_OCTET_STRING_specifics_t asn_DEF_OCTET_STRING_specs = {
18 sizeof(OCTET_STRING_t),
19 offsetof(OCTET_STRING_t, _asn_ctx),
20 0
21};
Lev Walkinde4825d2004-09-29 13:20:14 +000022asn_TYPE_descriptor_t asn_DEF_OCTET_STRING = {
Lev Walkindc06f6b2004-10-20 15:50:55 +000023 "OCTET STRING", /* Canonical name */
24 "OCTET_STRING", /* XML tag name */
Lev Walkina9cc46e2004-09-22 16:06:28 +000025 OCTET_STRING_free,
26 OCTET_STRING_print, /* non-ascii stuff, generally */
Lev Walkinf15320b2004-06-03 03:38:44 +000027 asn_generic_no_constraint,
28 OCTET_STRING_decode_ber,
29 OCTET_STRING_encode_der,
Lev Walkindc06f6b2004-10-20 15:50:55 +000030 OCTET_STRING_decode_xer_hex,
Lev Walkina9cc46e2004-09-22 16:06:28 +000031 OCTET_STRING_encode_xer,
Lev Walkinf15320b2004-06-03 03:38:44 +000032 0, /* Use generic outmost tag fetcher */
Lev Walkinde4825d2004-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]),
Lev Walkin449f8322004-08-20 13:23:42 +000039 0, 0, /* No members */
Lev Walkinbbd93252004-10-12 05:57:23 +000040 &asn_DEF_OCTET_STRING_specs
Lev Walkinf15320b2004-06-03 03:38:44 +000041};
42
Lev Walkincc6a9102004-09-23 22:06:26 +000043#undef _CH_PHASE
44#undef NEXT_PHASE
45#undef PREV_PHASE
Lev Walkin75b1bef2005-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; \
Lev Walkinf15320b2004-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
Lev Walkincc6a9102004-09-23 22:06:26 +000054#undef ADVANCE
Lev Walkin75b1bef2005-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; \
Lev Walkinf15320b2004-06-03 03:38:44 +000060 } while(0)
61
Lev Walkincc6a9102004-09-23 22:06:26 +000062#undef RETURN
Lev Walkin75b1bef2005-04-25 19:38:21 +000063#define RETURN(_code) do { \
64 rval.code = _code; \
65 rval.consumed = consumed_myself; \
66 return rval; \
Lev Walkinf15320b2004-06-03 03:38:44 +000067 } while(0)
68
Lev Walkincc6a9102004-09-23 22:06:26 +000069#undef APPEND
Lev Walkind9bd7752004-06-05 08:17:50 +000070#define APPEND(bufptr, bufsize) do { \
Lev Walkin75b1bef2005-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) { \
Lev Walkind9bd7752004-06-05 08:17:50 +000077 void *ptr; \
Lev Walkin188ed2c2004-09-13 08:31:01 +000078 /* Be nice and round to the memory allocator */ \
Lev Walkin75b1bef2005-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); \
Lev Walkind9bd7752004-06-05 08:17:50 +000083 ptr = REALLOC(st->buf, _ns); \
84 if(ptr) { \
Lev Walkinc2346572004-08-11 09:07:36 +000085 st->buf = (uint8_t *)ptr; \
Lev Walkin75b1bef2005-04-25 19:38:21 +000086 ctx->context = _ns; \
Lev Walkind9bd7752004-06-05 08:17:50 +000087 } else { \
88 RETURN(RC_FAIL); \
89 } \
Lev Walkin7f85ef42005-07-02 20:24:27 +000090 ASN_DEBUG("Reallocating into %ld", (long)_ns); \
Lev Walkind9bd7752004-06-05 08:17:50 +000091 } \
Lev Walkin8d127872004-09-04 04:44:50 +000092 memcpy(st->buf + st->size, bufptr, _bs); \
Lev Walkind9bd7752004-06-05 08:17:50 +000093 /* Convenient nul-termination */ \
Lev Walkin75b1bef2005-04-25 19:38:21 +000094 st->buf[_es] = '\0'; \
95 st->size = _es; \
Lev Walkinf15320b2004-06-03 03:38:44 +000096 } while(0)
97
98/*
Lev Walkin07f388c2004-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 */
104 _TT_ANY = 2, /* ANY type, a special case too */
105} OS_type_e;
106
107/*
Lev Walkinf15320b2004-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 {
Lev Walkin3990ba62004-09-24 20:57:41 +0000114 ber_tlv_len_t left; /* What's left to read (or -1) */
Lev Walkin5c915992004-09-27 20:54:06 +0000115 ber_tlv_len_t got; /* What was actually processed */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000116 int cont_level; /* Depth of subcontainment */
Lev Walkinf15320b2004-06-03 03:38:44 +0000117 int want_nulls; /* Want null "end of content" octets? */
118 int bits_chopped; /* Flag in BIT STRING mode */
Lev Walkin5c915992004-09-27 20:54:06 +0000119 ber_tlv_tag_t tag; /* For debugging purposes */
Lev Walkinf15320b2004-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 *
Lev Walkin3990ba62004-09-24 20:57:41 +0000129OS__add_stack_el(struct _stack *st) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000130 struct _stack_el *nel;
131
Lev Walkin188ed2c2004-09-13 08:31:01 +0000132 /*
133 * Reuse the old stack frame or allocate a new one.
134 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000135 if(st->cur_ptr && st->cur_ptr->next) {
136 nel = st->cur_ptr->next;
Lev Walkinf15320b2004-06-03 03:38:44 +0000137 nel->bits_chopped = 0;
Lev Walkin5c915992004-09-27 20:54:06 +0000138 nel->got = 0;
139 /* Retain the nel->cont_level, it's correct. */
Lev Walkinf15320b2004-06-03 03:38:44 +0000140 } else {
Lev Walkin814cca72004-12-15 23:23:53 +0000141 nel = (struct _stack_el *)CALLOC(1, sizeof(struct _stack_el));
Lev Walkinf15320b2004-06-03 03:38:44 +0000142 if(nel == NULL)
143 return NULL;
144
145 if(st->tail) {
Lev Walkin188ed2c2004-09-13 08:31:01 +0000146 /* Increase a subcontainment depth */
147 nel->cont_level = st->tail->cont_level + 1;
Lev Walkinf15320b2004-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() {
Lev Walkinbbd93252004-10-12 05:57:23 +0000161 return (struct _stack *)CALLOC(1, sizeof(struct _stack));
Lev Walkinf15320b2004-06-03 03:38:44 +0000162}
163
164/*
165 * Decode OCTET STRING type.
166 */
Lev Walkindc06f6b2004-10-20 15:50:55 +0000167asn_dec_rval_t
Lev Walkinde4825d2004-09-29 13:20:14 +0000168OCTET_STRING_decode_ber(asn_codec_ctx_t *opt_codec_ctx,
169 asn_TYPE_descriptor_t *td,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000170 void **os_structure, const void *buf_ptr, size_t size, int tag_mode) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000171 asn_OCTET_STRING_specifics_t *specs = td->specifics
Lev Walkindc06f6b2004-10-20 15:50:55 +0000172 ? (asn_OCTET_STRING_specifics_t *)td->specifics
173 : &asn_DEF_OCTET_STRING_specs;
Lev Walkinbbd93252004-10-12 05:57:23 +0000174 BIT_STRING_t *st = (BIT_STRING_t *)*os_structure;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000175 asn_dec_rval_t rval;
Lev Walkinde4825d2004-09-29 13:20:14 +0000176 asn_struct_ctx_t *ctx;
Lev Walkinf15320b2004-06-03 03:38:44 +0000177 ssize_t consumed_myself = 0;
Lev Walkinbbd93252004-10-12 05:57:23 +0000178 struct _stack *stck; /* Expectations stack structure */
Lev Walkin5c915992004-09-27 20:54:06 +0000179 struct _stack_el *sel = 0; /* Stack element */
Lev Walkinf15320b2004-06-03 03:38:44 +0000180 int tlv_constr;
Lev Walkinbbd93252004-10-12 05:57:23 +0000181 OS_type_e type_variant = (OS_type_e)specs->subvariant;
Lev Walkinf15320b2004-06-03 03:38:44 +0000182
Lev Walkincc6a9102004-09-23 22:06:26 +0000183 ASN_DEBUG("Decoding %s as %s (frame %ld)",
184 td->name,
Lev Walkinbbd93252004-10-12 05:57:23 +0000185 (type_variant == _TT_GENERIC) ?
186 "OCTET STRING" : "OS-SpecialCase",
Lev Walkincc6a9102004-09-23 22:06:26 +0000187 (long)size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000188
189 /*
190 * Create the string if does not exist.
191 */
192 if(st == NULL) {
Lev Walkin814cca72004-12-15 23:23:53 +0000193 *os_structure = CALLOC(1, specs->struct_size);
194 st = (BIT_STRING_t *)*os_structure;
Lev Walkinf15320b2004-06-03 03:38:44 +0000195 if(st == NULL)
196 RETURN(RC_FAIL);
197 }
198
199 /* Restore parsing context */
Lev Walkinbbd93252004-10-12 05:57:23 +0000200 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000201
202 switch(ctx->phase) {
203 case 0:
204 /*
205 * Check tags.
206 */
Lev Walkinde4825d2004-09-29 13:20:14 +0000207 rval = ber_check_tags(opt_codec_ctx, td, ctx,
Lev Walkin8e8078a2004-09-26 13:10:40 +0000208 buf_ptr, size, tag_mode, -1,
Lev Walkinf15320b2004-06-03 03:38:44 +0000209 &ctx->left, &tlv_constr);
Lev Walkin443d2512004-10-05 06:35:31 +0000210 if(rval.code != RC_OK)
211 return rval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000212
Lev Walkinf15320b2004-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) {
Lev Walkin814cca72004-12-15 23:23:53 +0000219 stck = (struct _stack *)ctx->ptr;
Lev Walkinf15320b2004-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);
Lev Walkinbbd93252004-10-12 05:57:23 +0000228 if(type_variant == _TT_ANY && tag_mode != 1)
Lev Walkin188ed2c2004-09-13 08:31:01 +0000229 APPEND(buf_ptr, rval.consumed);
Lev Walkinf15320b2004-06-03 03:38:44 +0000230 ADVANCE(rval.consumed);
231 goto phase3;
232 }
233
Lev Walkinf15320b2004-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 */
Lev Walkin814cca72004-12-15 23:23:53 +0000241 stck = (struct _stack *)ctx->ptr;
Lev Walkinf15320b2004-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;
Lev Walkin188ed2c2004-09-13 08:31:01 +0000246 ber_tlv_tag_t expected_tag;
Lev Walkin5c915992004-09-27 20:54:06 +0000247 ssize_t tl, ll, tlvl;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000248 /* This one works even if (sel->left == -1) */
249 ssize_t Left = ((!sel||(size_t)sel->left >= size)
Lev Walkin5c915992004-09-27 20:54:06 +0000250 ?(ssize_t)size:sel->left);
Lev Walkinf15320b2004-06-03 03:38:44 +0000251
Lev Walkin3990ba62004-09-24 20:57:41 +0000252
Lev Walkinabf68892004-10-26 10:12:14 +0000253 ASN_DEBUG("%p, s->l=%ld, s->wn=%ld, s->g=%ld\n", sel,
Lev Walkin1e3ccbb2004-10-26 10:56:10 +0000254 (long)(sel?sel->left:0),
255 (long)(sel?sel->want_nulls:0),
256 (long)(sel?sel->got:0)
Lev Walkin5c915992004-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
Lev Walkin3990ba62004-09-24 20:57:41 +0000277 tl = ber_fetch_tag(buf_ptr, Left, &tlv_tag);
Lev Walkinabf68892004-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?"":"!",
Lev Walkin1e3ccbb2004-10-26 10:56:10 +0000280 (long)(sel?sel->left:0),
281 (long)(sel?sel->want_nulls:0),
Lev Walkinabf68892004-10-26 10:12:14 +0000282 (long)tl);
Lev Walkinf15320b2004-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,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000291 (const char *)buf_ptr + tl,Left - tl,&tlv_len);
Lev Walkina6a926a2005-03-02 01:54:28 +0000292 ASN_DEBUG("Got tag=%s, tc=%d, left=%ld, tl=%ld, len=%ld, ll=%ld",
Lev Walkin8d127872004-09-04 04:44:50 +0000293 ber_tlv_tag_string(tlv_tag), tlv_constr,
Lev Walkina6a926a2005-03-02 01:54:28 +0000294 (long)Left, (long)tl, (long)tlv_len, (long)ll);
Lev Walkinf15320b2004-06-03 03:38:44 +0000295 switch(ll) {
296 case -1: RETURN(RC_FAIL);
297 case 0: RETURN(RC_WMORE);
298 }
299
Lev Walkin8d127872004-09-04 04:44:50 +0000300 if(sel && sel->want_nulls
Lev Walkin8c3b8542005-03-10 18:52:02 +0000301 && ((const uint8_t *)buf_ptr)[0] == 0
302 && ((const uint8_t *)buf_ptr)[1] == 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000303 {
Lev Walkin8d127872004-09-04 04:44:50 +0000304
305 ASN_DEBUG("Eat EOC; wn=%d--", sel->want_nulls);
306
Lev Walkinbbd93252004-10-12 05:57:23 +0000307 if(type_variant == _TT_ANY
Lev Walkin07f388c2004-10-11 11:43:08 +0000308 && (tag_mode != 1 || sel->cont_level))
309 APPEND("\0\0", 2);
Lev Walkin5c915992004-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
Lev Walkinf15320b2004-06-03 03:38:44 +0000317 sel->want_nulls--;
318 if(sel->want_nulls == 0) {
319 /* Move to the next expectation */
Lev Walkin5c915992004-09-27 20:54:06 +0000320 sel->left = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000321 tlv_constr = 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000322 }
Lev Walkin8d127872004-09-04 04:44:50 +0000323
324 continue;
Lev Walkin188ed2c2004-09-13 08:31:01 +0000325 }
326
327 /*
328 * Set up expected tags,
329 * depending on ASN.1 type being decoded.
330 */
Lev Walkinbbd93252004-10-12 05:57:23 +0000331 switch(type_variant) {
Lev Walkin188ed2c2004-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) {
Lev Walkinf15320b2004-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
Lev Walkinde4825d2004-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
Lev Walkinf15320b2004-06-03 03:38:44 +0000375 /*
376 * Append a new expectation.
377 */
Lev Walkin3990ba62004-09-24 20:57:41 +0000378 sel = OS__add_stack_el(stck);
Lev Walkin5c915992004-09-27 20:54:06 +0000379 if(!sel) RETURN(RC_FAIL);
Lev Walkinf15320b2004-06-03 03:38:44 +0000380
Lev Walkin5c915992004-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 }
Lev Walkinbbd93252004-10-12 05:57:23 +0000395 if(type_variant == _TT_ANY
Lev Walkin07f388c2004-10-11 11:43:08 +0000396 && (tag_mode != 1 || sel->cont_level))
397 APPEND(buf_ptr, tlvl);
Lev Walkin5c915992004-09-27 20:54:06 +0000398 sel->got += tlvl;
399 ADVANCE(tlvl);
400
Lev Walkinabf68892004-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);
Lev Walkin5c915992004-09-27 20:54:06 +0000404
Lev Walkinf15320b2004-06-03 03:38:44 +0000405 } while(tlv_constr);
406 if(sel == NULL) {
407 /* Finished operation, "phase out" */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000408 ASN_DEBUG("Phase out");
Lev Walkinf15320b2004-06-03 03:38:44 +0000409 _CH_PHASE(ctx, +3);
410 break;
411 }
412
413 NEXT_PHASE(ctx);
414 /* Fall through */
415 case 2:
Lev Walkin814cca72004-12-15 23:23:53 +0000416 stck = (struct _stack *)ctx->ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000417 sel = stck->cur_ptr;
Lev Walkin5c915992004-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);
Lev Walkinf15320b2004-06-03 03:38:44 +0000421 {
422 ber_tlv_len_t len;
423
424 assert(sel->left >= 0);
425
Lev Walkinec1ffd42004-08-18 04:53:32 +0000426 len = ((ber_tlv_len_t)size < sel->left)
427 ? (ber_tlv_len_t)size : sel->left;
Lev Walkinf15320b2004-06-03 03:38:44 +0000428 if(len > 0) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000429 if(type_variant == _TT_BIT_STRING
Lev Walkin188ed2c2004-09-13 08:31:01 +0000430 && sel->bits_chopped == 0) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000431 /* Put the unused-bits-octet away */
Lev Walkin8c3b8542005-03-10 18:52:02 +0000432 st->bits_unused = *(const uint8_t *)buf_ptr;
433 APPEND(((const char *)buf_ptr+1), (len - 1));
Lev Walkinf15320b2004-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;
Lev Walkin5c915992004-09-27 20:54:06 +0000440 sel->got += len;
Lev Walkinf15320b2004-06-03 03:38:44 +0000441 }
442
Lev Walkin5c915992004-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);
Lev Walkinf15320b2004-06-03 03:38:44 +0000446 RETURN(RC_WMORE);
Lev Walkinf15320b2004-06-03 03:38:44 +0000447 }
Lev Walkin5c915992004-09-27 20:54:06 +0000448
449 PREV_PHASE(ctx);
450 goto phase1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000451 }
452 break;
453 case 3:
454 phase3:
455 /*
456 * Primitive form, no stack required.
457 */
Lev Walkinbbd93252004-10-12 05:57:23 +0000458 assert(ctx->left >= 0);
459
Lev Walkind9bd7752004-06-05 08:17:50 +0000460 if(size < (size_t)ctx->left) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000461 if(!size) RETURN(RC_WMORE);
Lev Walkin75b1bef2005-04-25 19:38:21 +0000462 if(type_variant == _TT_BIT_STRING && !ctx->context) {
Lev Walkin8c3b8542005-03-10 18:52:02 +0000463 st->bits_unused = *(const uint8_t *)buf_ptr;
Lev Walkinbbd93252004-10-12 05:57:23 +0000464 ctx->left--;
465 ADVANCE(1);
466 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000467 APPEND(buf_ptr, size);
Lev Walkin75b1bef2005-04-25 19:38:21 +0000468 assert(ctx->context > 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000469 ctx->left -= size;
470 ADVANCE(size);
471 RETURN(RC_WMORE);
472 } else {
Lev Walkinbbd93252004-10-12 05:57:23 +0000473 if(type_variant == _TT_BIT_STRING
Lev Walkin75b1bef2005-04-25 19:38:21 +0000474 && !ctx->context && ctx->left) {
Lev Walkin8c3b8542005-03-10 18:52:02 +0000475 st->bits_unused = *(const uint8_t *)buf_ptr;
Lev Walkinbbd93252004-10-12 05:57:23 +0000476 ctx->left--;
477 ADVANCE(1);
478 }
Lev Walkinf15320b2004-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
Lev Walkin5c915992004-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
Lev Walkinf15320b2004-06-03 03:38:44 +0000497 /*
498 * BIT STRING-specific processing.
499 */
Lev Walkinbbd93252004-10-12 05:57:23 +0000500 if(type_variant == _TT_BIT_STRING && st->size) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000501 /* Finalize BIT STRING: zero out unused bits. */
Lev Walkinbbd93252004-10-12 05:57:23 +0000502 st->buf[st->size-1] &= 0xff << st->bits_unused;
Lev Walkinf15320b2004-06-03 03:38:44 +0000503 }
504
Lev Walkinabf68892004-10-26 10:12:14 +0000505 ASN_DEBUG("Took %ld bytes to encode %s: [%s]:%ld",
506 (long)consumed_myself, td->name,
Lev Walkinbbd93252004-10-12 05:57:23 +0000507 (type_variant == _TT_GENERIC) ? (char *)st->buf : "<data>",
Lev Walkinabf68892004-10-26 10:12:14 +0000508 (long)st->size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000509
Lev Walkinf15320b2004-06-03 03:38:44 +0000510
Lev Walkin5c915992004-09-27 20:54:06 +0000511 RETURN(RC_OK);
Lev Walkinf15320b2004-06-03 03:38:44 +0000512}
513
514/*
515 * Encode OCTET STRING type using DER.
516 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000517asn_enc_rval_t
Lev Walkinbbd93252004-10-12 05:57:23 +0000518OCTET_STRING_encode_der(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000519 int tag_mode, ber_tlv_tag_t tag,
520 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000521 asn_enc_rval_t er;
522 asn_OCTET_STRING_specifics_t *specs = td->specifics
Lev Walkindc06f6b2004-10-20 15:50:55 +0000523 ? (asn_OCTET_STRING_specifics_t *)td->specifics
524 : &asn_DEF_OCTET_STRING_specs;
Lev Walkinbbd93252004-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;
Lev Walkinf15320b2004-06-03 03:38:44 +0000528
529 ASN_DEBUG("%s %s as OCTET STRING",
Lev Walkin1f670c12004-09-02 12:57:25 +0000530 cb?"Estimating":"Encoding", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000531
532 /*
Lev Walkinbbd93252004-10-12 05:57:23 +0000533 * Write tags.
Lev Walkinf15320b2004-06-03 03:38:44 +0000534 */
Lev Walkinbbd93252004-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;
Lev Walkinf15320b2004-06-03 03:38:44 +0000543 }
Lev Walkinbbd93252004-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;
552 return er;
Lev Walkinf15320b2004-06-03 03:38:44 +0000553 }
554
Lev Walkin07f388c2004-10-11 11:43:08 +0000555 /*
Lev Walkinbbd93252004-10-12 05:57:23 +0000556 * Prepare to deal with the last octet of BIT STRING.
Lev Walkin07f388c2004-10-11 11:43:08 +0000557 */
Lev Walkinbbd93252004-10-12 05:57:23 +0000558 if(type_variant == _TT_BIT_STRING) {
559 uint8_t b = st->bits_unused & 0x07;
560 if(b && st->size) fix_last_byte = 1;
561 _ASN_CALLBACK(&b, 1);
562 er.encoded++;
Lev Walkinf15320b2004-06-03 03:38:44 +0000563 }
564
Lev Walkinbbd93252004-10-12 05:57:23 +0000565 /* Invoke callback for the main part of the buffer */
566 _ASN_CALLBACK(st->buf, st->size - fix_last_byte);
Lev Walkinf15320b2004-06-03 03:38:44 +0000567
Lev Walkinbbd93252004-10-12 05:57:23 +0000568 /* The last octet should be stripped off the unused bits */
569 if(fix_last_byte) {
570 uint8_t b = st->buf[st->size-1] & (0xff << st->bits_unused);
571 _ASN_CALLBACK(&b, 1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000572 }
573
Lev Walkinbbd93252004-10-12 05:57:23 +0000574 er.encoded += st->size;
575 return er;
576cb_failed:
577 _ASN_ENCODE_FAILED;
Lev Walkinf15320b2004-06-03 03:38:44 +0000578}
579
Lev Walkina9cc46e2004-09-22 16:06:28 +0000580asn_enc_rval_t
Lev Walkinde4825d2004-09-29 13:20:14 +0000581OCTET_STRING_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000582 int ilevel, enum xer_encoder_flags_e flags,
583 asn_app_consume_bytes_f *cb, void *app_key) {
584 static const char *h2c = "0123456789ABCDEF";
585 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
586 asn_enc_rval_t er;
587 char scratch[16 * 3 + 4];
588 char *p = scratch;
589 uint8_t *buf;
590 uint8_t *end;
591 size_t i;
592
Lev Walkin942fd082004-10-03 09:13:02 +0000593 if(!st || !st->buf)
594 _ASN_ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000595
596 er.encoded = 0;
597
598 /*
599 * Dump the contents of the buffer in hexadecimal.
600 */
601 buf = st->buf;
602 end = buf + st->size;
603 if(flags & XER_F_CANONICAL) {
604 char *scend = scratch + (sizeof(scratch) - 2);
605 for(; buf < end; buf++) {
606 if(p >= scend) {
607 _ASN_CALLBACK(scratch, p - scratch);
608 er.encoded += p - scratch;
609 p = scratch;
610 }
611 *p++ = h2c[(*buf >> 4) & 0x0F];
612 *p++ = h2c[*buf & 0x0F];
613 }
Lev Walkincc6a9102004-09-23 22:06:26 +0000614
615 _ASN_CALLBACK(scratch, p-scratch); /* Dump the rest */
616 er.encoded += p - scratch;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000617 } else {
618 for(i = 0; buf < end; buf++, i++) {
619 if(!(i % 16) && (i || st->size > 16)) {
620 _ASN_CALLBACK(scratch, p-scratch);
621 er.encoded += (p-scratch);
622 p = scratch;
623 _i_ASN_TEXT_INDENT(1, ilevel);
624 }
625 *p++ = h2c[(*buf >> 4) & 0x0F];
626 *p++ = h2c[*buf & 0x0F];
627 *p++ = 0x20;
628 }
Lev Walkincc6a9102004-09-23 22:06:26 +0000629 if(p - scratch) {
630 p--; /* Remove the tail space */
631 _ASN_CALLBACK(scratch, p-scratch); /* Dump the rest */
632 er.encoded += p - scratch;
633 if(st->size > 16)
634 _i_ASN_TEXT_INDENT(1, ilevel-1);
635 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000636 }
637
Lev Walkina9cc46e2004-09-22 16:06:28 +0000638 return er;
Lev Walkin942fd082004-10-03 09:13:02 +0000639cb_failed:
640 _ASN_ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000641}
642
Lev Walkin9ca81892004-10-03 10:54:25 +0000643static struct OCTET_STRING__xer_escape_table_s {
644 char *string;
645 int size;
646} OCTET_STRING__xer_escape_table[] = {
647#define OSXET(s) { s, sizeof(s) - 1 }
648 OSXET("\074\156\165\154\057\076"), /* <nul/> */
649 OSXET("\074\163\157\150\057\076"), /* <soh/> */
650 OSXET("\074\163\164\170\057\076"), /* <stx/> */
651 OSXET("\074\145\164\170\057\076"), /* <etx/> */
652 OSXET("\074\145\157\164\057\076"), /* <eot/> */
653 OSXET("\074\145\156\161\057\076"), /* <enq/> */
654 OSXET("\074\141\143\153\057\076"), /* <ack/> */
655 OSXET("\074\142\145\154\057\076"), /* <bel/> */
656 OSXET("\074\142\163\057\076"), /* <bs/> */
657 OSXET("\011"), /* \t */
658 OSXET("\012"), /* \n */
659 OSXET("\074\166\164\057\076"), /* <vt/> */
660 OSXET("\074\146\146\057\076"), /* <ff/> */
661 OSXET("\015"), /* \r */
662 OSXET("\074\163\157\057\076"), /* <so/> */
663 OSXET("\074\163\151\057\076"), /* <si/> */
664 OSXET("\074\144\154\145\057\076"), /* <dle/> */
665 OSXET("\074\144\143\061\057\076"), /* <de1/> */
666 OSXET("\074\144\143\062\057\076"), /* <de2/> */
667 OSXET("\074\144\143\063\057\076"), /* <de3/> */
668 OSXET("\074\144\143\064\057\076"), /* <de4/> */
669 OSXET("\074\156\141\153\057\076"), /* <nak/> */
670 OSXET("\074\163\171\156\057\076"), /* <syn/> */
671 OSXET("\074\145\164\142\057\076"), /* <etb/> */
672 OSXET("\074\143\141\156\057\076"), /* <can/> */
673 OSXET("\074\145\155\057\076"), /* <em/> */
674 OSXET("\074\163\165\142\057\076"), /* <sub/> */
675 OSXET("\074\145\163\143\057\076"), /* <esc/> */
676 OSXET("\074\151\163\064\057\076"), /* <is4/> */
677 OSXET("\074\151\163\063\057\076"), /* <is3/> */
678 OSXET("\074\151\163\062\057\076"), /* <is2/> */
679 OSXET("\074\151\163\061\057\076"), /* <is1/> */
680 { 0, 0 }, /* " " */
681 { 0, 0 }, /* ! */
682 { 0, 0 }, /* \" */
683 { 0, 0 }, /* # */
684 { 0, 0 }, /* $ */
685 { 0, 0 }, /* % */
686 OSXET("\046\141\155\160\073"), /* &amp; */
687 { 0, 0 }, /* ' */
688 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, /* ()*+,-./ */
689 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, /* 01234567 */
690 {0,0},{0,0},{0,0},{0,0}, /* 89:; */
691 OSXET("\046\154\164\073"), /* &lt; */
692 { 0, 0 }, /* = */
693 OSXET("\046\147\164\073"), /* &gt; */
694};
695
Lev Walkindc06f6b2004-10-20 15:50:55 +0000696static int
Lev Walkin0fab1a62005-03-09 22:19:25 +0000697OS__check_escaped_control_char(const void *buf, int size) {
Lev Walkindc06f6b2004-10-20 15:50:55 +0000698 size_t i;
699 /*
700 * Inefficient algorithm which translates the escape sequences
701 * defined above into characters. Returns -1 if not found.
702 * TODO: replace by a faster algorithm (bsearch(), hash or
703 * nested table lookups).
704 */
705 for(i = 0; i < 32 /* Don't spend time on the bottom half */; i++) {
706 struct OCTET_STRING__xer_escape_table_s *el;
707 el = &OCTET_STRING__xer_escape_table[i];
708 if(el->size == size && memcmp(buf, el->string, size) == 0)
709 return i;
710 }
711 return -1;
712}
713
714static int
Lev Walkin0fab1a62005-03-09 22:19:25 +0000715OCTET_STRING__handle_control_chars(void *struct_ptr, const void *chunk_buf, size_t chunk_size) {
Lev Walkindc06f6b2004-10-20 15:50:55 +0000716 /*
717 * This might be one of the escape sequences
718 * for control characters. Check it out.
719 * #11.15.5
720 */
721 int control_char = OS__check_escaped_control_char(chunk_buf,chunk_size);
722 if(control_char >= 0) {
723 OCTET_STRING_t *st = (OCTET_STRING_t *)struct_ptr;
724 void *p = REALLOC(st->buf, st->size + 2);
725 if(p) {
726 st->buf = (uint8_t *)p;
727 st->buf[st->size++] = control_char;
728 st->buf[st->size] = '\0'; /* nul-termination */
729 return 0;
730 }
731 }
732
733 return -1; /* No, it's not */
734}
735
Lev Walkina9cc46e2004-09-22 16:06:28 +0000736asn_enc_rval_t
Lev Walkindc06f6b2004-10-20 15:50:55 +0000737OCTET_STRING_encode_xer_utf8(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000738 int ilevel, enum xer_encoder_flags_e flags,
739 asn_app_consume_bytes_f *cb, void *app_key) {
740 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
741 asn_enc_rval_t er;
Lev Walkin9ca81892004-10-03 10:54:25 +0000742 uint8_t *buf, *end;
743 uint8_t *ss; /* Sequence start */
744 ssize_t encoded_len = 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000745
746 (void)ilevel; /* Unused argument */
747 (void)flags; /* Unused argument */
748
749 if(!st || !st->buf)
750 _ASN_ENCODE_FAILED;
751
Lev Walkin9ca81892004-10-03 10:54:25 +0000752 buf = st->buf;
753 end = buf + st->size;
754 for(ss = buf; buf < end; buf++) {
Lev Walkin443d2512004-10-05 06:35:31 +0000755 unsigned int ch = *buf;
Lev Walkin9ca81892004-10-03 10:54:25 +0000756 int s_len; /* Special encoding sequence length */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000757
Lev Walkin9ca81892004-10-03 10:54:25 +0000758 /*
759 * Escape certain characters: X.680/11.15
760 */
761 if(ch < sizeof(OCTET_STRING__xer_escape_table)
762 /sizeof(OCTET_STRING__xer_escape_table[0])
763 && (s_len = OCTET_STRING__xer_escape_table[ch].size)) {
764 if(((buf - ss) && cb(ss, buf - ss, app_key) < 0)
765 || cb(OCTET_STRING__xer_escape_table[ch].string, s_len,
766 app_key) < 0)
767 _ASN_ENCODE_FAILED;
768 encoded_len += (buf - ss) + s_len;
769 ss = buf + 1;
770 }
771 }
772
773 encoded_len += (buf - ss);
774 if((buf - ss) && cb(ss, buf - ss, app_key) < 0)
775 _ASN_ENCODE_FAILED;
776
777 er.encoded = encoded_len;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000778 return er;
779}
780
Lev Walkindc06f6b2004-10-20 15:50:55 +0000781/*
782 * Convert from hexadecimal format (cstring): "AB CD EF"
783 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000784static ssize_t OCTET_STRING__convert_hexadecimal(void *sptr, const void *chunk_buf, size_t chunk_size, int have_more) {
Lev Walkindc06f6b2004-10-20 15:50:55 +0000785 OCTET_STRING_t *st = (OCTET_STRING_t *)sptr;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000786 const char *chunk_stop = (const char *)chunk_buf;
787 const char *p = chunk_stop;
788 const char *pend = p + chunk_size;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000789 unsigned int clv = 0;
790 int half = 0; /* Half bit */
791 uint8_t *buf;
792
793 /* Reallocate buffer according to high cap estimation */
794 ssize_t _ns = st->size + (chunk_size + 1) / 2;
795 void *nptr = REALLOC(st->buf, _ns + 1);
796 if(!nptr) return -1;
797 st->buf = (uint8_t *)nptr;
798 buf = st->buf + st->size;
799
800 /*
801 * If something like " a b c " appears here, the " a b":3 will be
802 * converted, and the rest skipped. That is, unless buf_size is greater
803 * than chunk_size, then it'll be equivalent to "ABC0".
804 */
805 for(; p < pend; p++) {
Lev Walkin0fab1a62005-03-09 22:19:25 +0000806 int ch = *(const unsigned char *)p;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000807 switch(ch) {
808 case 0x09: case 0x0a: case 0x0c: case 0x0d:
809 case 0x20:
810 /* Ignore whitespace */
811 continue;
812 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: /*01234*/
813 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: /*56789*/
814 clv = (clv << 4) + (ch - 0x30);
815 break;
816 case 0x41: case 0x42: case 0x43: /* ABC */
817 case 0x44: case 0x45: case 0x46: /* DEF */
Lev Walkin3ae21bd2005-02-21 14:43:48 +0000818 clv = (clv << 4) + (ch - 0x41 + 10);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000819 break;
820 case 0x61: case 0x62: case 0x63: /* abc */
821 case 0x64: case 0x65: case 0x66: /* def */
Lev Walkin3ae21bd2005-02-21 14:43:48 +0000822 clv = (clv << 4) + (ch - 0x61 + 10);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000823 break;
824 default:
825 *buf = 0; /* JIC */
826 return -1;
827 }
828 if(half++) {
829 half = 0;
830 *buf++ = clv;
831 chunk_stop = p + 1;
832 }
833 }
834
835 /*
836 * Check partial decoding.
837 */
838 if(half) {
839 if(have_more) {
840 /*
841 * Partial specification is fine,
842 * because no more more PXER_TEXT data is available.
843 */
844 *buf++ = clv << 4;
845 chunk_stop = p;
846 }
847 } else {
848 chunk_stop = p;
849 }
850
851 st->size = buf - st->buf; /* Adjust the buffer size */
852 assert(st->size <= _ns);
853 st->buf[st->size] = 0; /* Courtesy termination */
854
Lev Walkin0fab1a62005-03-09 22:19:25 +0000855 return (chunk_stop - (const char *)chunk_buf); /* Converted size */
Lev Walkindc06f6b2004-10-20 15:50:55 +0000856}
857
858/*
859 * Convert from binary format: "00101011101"
860 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000861static ssize_t OCTET_STRING__convert_binary(void *sptr, const void *chunk_buf, size_t chunk_size, int have_more) {
Lev Walkindc06f6b2004-10-20 15:50:55 +0000862 BIT_STRING_t *st = (BIT_STRING_t *)sptr;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000863 const char *p = (const char *)chunk_buf;
864 const char *pend = p + chunk_size;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000865 int bits_unused = st->bits_unused & 0x7;
866 uint8_t *buf;
867
868 /* Reallocate buffer according to high cap estimation */
869 ssize_t _ns = st->size + (chunk_size + 7) / 8;
870 void *nptr = REALLOC(st->buf, _ns + 1);
871 if(!nptr) return -1;
872 st->buf = (uint8_t *)nptr;
873 buf = st->buf + st->size;
874
875 (void)have_more;
876
877 if(bits_unused == 0)
878 bits_unused = 8;
879 else if(st->size)
880 buf--;
881
882 /*
883 * Convert series of 0 and 1 into the octet string.
884 */
885 for(; p < pend; p++) {
Lev Walkin0fab1a62005-03-09 22:19:25 +0000886 int ch = *(const unsigned char *)p;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000887 switch(ch) {
888 case 0x09: case 0x0a: case 0x0c: case 0x0d:
889 case 0x20:
890 /* Ignore whitespace */
891 break;
892 case 0x30:
893 case 0x31:
894 if(bits_unused-- <= 0) {
895 *++buf = 0; /* Clean the cell */
896 bits_unused = 7;
897 }
898 *buf |= (ch&1) << bits_unused;
899 break;
900 default:
901 st->bits_unused = bits_unused;
902 return -1;
903 }
904 }
905
906 if(bits_unused == 8) {
907 st->size = buf - st->buf;
908 st->bits_unused = 0;
909 } else {
910 st->size = buf - st->buf + 1;
911 st->bits_unused = bits_unused;
912 }
913
914 assert(st->size <= _ns);
915 st->buf[st->size] = 0; /* Courtesy termination */
916
917 return chunk_size; /* Converted in full */
918}
919
920/*
921 * Something like strtod(), but with stricter rules.
922 */
923static int
Lev Walkin0fab1a62005-03-09 22:19:25 +0000924OS__strtoent(int base, const char *buf, const char *end, int32_t *ret_value) {
Lev Walkin33700162004-10-26 09:03:31 +0000925 int32_t val = 0;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000926 const char *p;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000927
928 for(p = buf; p < end; p++) {
929 int ch = *p;
930 if((val * base + base) < 0) return -1; /* Strange huge value */
931 switch(ch) {
932 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: /*01234*/
933 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: /*56789*/
934 val = val * base + (ch - 0x30);
935 break;
936 case 0x41: case 0x42: case 0x43: /* ABC */
937 case 0x44: case 0x45: case 0x46: /* DEF */
Lev Walkin3ae21bd2005-02-21 14:43:48 +0000938 val = val * base + (ch - 0x41 + 10);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000939 break;
940 case 0x61: case 0x62: case 0x63: /* abc */
941 case 0x64: case 0x65: case 0x66: /* def */
Lev Walkin3ae21bd2005-02-21 14:43:48 +0000942 val = val * base + (ch - 0x61 + 10);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000943 break;
944 case 0x3b: /* ';' */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000945 *ret_value = val;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000946 return (p - buf) + 1;
947 default:
948 return -1; /* Character set error */
949 }
950 }
951
952 /* Do not return value. It's an error we're talking about here. */
953 return (p - buf);
954}
955
956/*
957 * Convert from the plain UTF-8 format, expanding entity references: "2 &lt; 3"
958 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000959static ssize_t OCTET_STRING__convert_entrefs(void *sptr, const void *chunk_buf, size_t chunk_size, int have_more) {
Lev Walkindc06f6b2004-10-20 15:50:55 +0000960 OCTET_STRING_t *st = (OCTET_STRING_t *)sptr;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000961 const char *p = (const char *)chunk_buf;
962 const char *pend = p + chunk_size;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000963 uint8_t *buf;
964
965 /* Reallocate buffer */
966 ssize_t _ns = st->size + chunk_size;
967 void *nptr = REALLOC(st->buf, _ns + 1);
968 if(!nptr) return -1;
969 st->buf = (uint8_t *)nptr;
970 buf = st->buf + st->size;
971
972 /*
973 * Convert series of 0 and 1 into the octet string.
974 */
975 for(; p < pend; p++) {
Lev Walkin0fab1a62005-03-09 22:19:25 +0000976 int ch = *(const unsigned char *)p;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000977 int len; /* Length of the rest of the chunk */
978
979 if(ch != 0x26 /* '&' */) {
980 *buf++ = ch;
981 continue; /* That was easy... */
982 }
983
984 /*
985 * Process entity reference.
986 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000987 len = chunk_size - (p - (const char *)chunk_buf);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000988 if(len == 1 /* "&" */) goto want_more;
989 if(p[1] == 0x23 /* '#' */) {
Lev Walkin0fab1a62005-03-09 22:19:25 +0000990 const char *pval; /* Pointer to start of digits */
991 int32_t val; /* Entity reference value */
Lev Walkindc06f6b2004-10-20 15:50:55 +0000992 int base;
993
994 if(len == 2 /* "&#" */) goto want_more;
995 if(p[2] == 0x78 /* 'x' */)
996 pval = p + 3, base = 16;
997 else
998 pval = p + 2, base = 10;
999 len = OS__strtoent(base, pval, p + len, &val);
1000 if(len == -1) {
1001 /* Invalid charset. Just copy verbatim. */
1002 *buf++ = ch;
1003 continue;
1004 }
1005 if(!len || pval[len-1] != 0x3b) goto want_more;
1006 assert(val > 0);
1007 p += (pval - p) + len - 1; /* Advance past entref */
1008
1009 if(val < 0x80) {
1010 *buf++ = (char)val;
1011 } else if(val < 0x800) {
1012 *buf++ = 0xc0 | ((val >> 6));
1013 *buf++ = 0x80 | ((val & 0x3f));
1014 } else if(val < 0x10000) {
1015 *buf++ = 0xe0 | ((val >> 12));
1016 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1017 *buf++ = 0x80 | ((val & 0x3f));
1018 } else if(val < 0x200000) {
1019 *buf++ = 0xf0 | ((val >> 18));
1020 *buf++ = 0x80 | ((val >> 12) & 0x3f);
1021 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1022 *buf++ = 0x80 | ((val & 0x3f));
1023 } else if(val < 0x4000000) {
1024 *buf++ = 0xf8 | ((val >> 24));
1025 *buf++ = 0x80 | ((val >> 18) & 0x3f);
1026 *buf++ = 0x80 | ((val >> 12) & 0x3f);
1027 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1028 *buf++ = 0x80 | ((val & 0x3f));
1029 } else {
1030 *buf++ = 0xfc | ((val >> 30) & 0x1);
1031 *buf++ = 0x80 | ((val >> 24) & 0x3f);
1032 *buf++ = 0x80 | ((val >> 18) & 0x3f);
1033 *buf++ = 0x80 | ((val >> 12) & 0x3f);
1034 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1035 *buf++ = 0x80 | ((val & 0x3f));
1036 }
1037 } else {
1038 /*
1039 * Ugly, limited parsing of &amp; &gt; &lt;
1040 */
1041 char *sc = (char *)memchr(p, 0x3b, len > 5 ? 5 : len);
1042 if(!sc) goto want_more;
1043 if((sc - p) == 4
1044 && p[1] == 0x61 /* 'a' */
1045 && p[2] == 0x6d /* 'm' */
1046 && p[3] == 0x70 /* 'p' */) {
1047 *buf++ = 0x26;
1048 p = sc;
1049 continue;
1050 }
1051 if((sc - p) == 3) {
1052 if(p[1] == 0x6c) {
1053 *buf = 0x3c; /* '<' */
1054 } else if(p[1] == 0x67) {
1055 *buf = 0x3e; /* '>' */
1056 } else {
1057 /* Unsupported entity reference */
1058 *buf++ = ch;
1059 continue;
1060 }
1061 if(p[2] != 0x74) {
1062 /* Unsupported entity reference */
1063 *buf++ = ch;
1064 continue;
1065 }
1066 buf++;
1067 p = sc;
1068 continue;
1069 }
1070 /* Unsupported entity reference */
1071 *buf++ = ch;
1072 }
1073
1074 continue;
1075 want_more:
1076 if(have_more) {
1077 /*
1078 * We know that no more data (of the same type)
1079 * is coming. Copy the rest verbatim.
1080 */
1081 *buf++ = ch;
1082 continue;
1083 }
Lev Walkin0fab1a62005-03-09 22:19:25 +00001084 chunk_size = (p - (const char *)chunk_buf);
Lev Walkindc06f6b2004-10-20 15:50:55 +00001085 /* Processing stalled: need more data */
Lev Walkinbdaae772005-02-18 12:25:47 +00001086 break;
Lev Walkindc06f6b2004-10-20 15:50:55 +00001087 }
1088
1089 st->size = buf - st->buf;
1090 assert(st->size <= _ns);
1091 st->buf[st->size] = 0; /* Courtesy termination */
1092
1093 return chunk_size; /* Converted in full */
1094}
1095
1096/*
1097 * Decode OCTET STRING from the XML element's body.
1098 */
1099static asn_dec_rval_t
1100OCTET_STRING__decode_xer(asn_codec_ctx_t *opt_codec_ctx,
1101 asn_TYPE_descriptor_t *td, void **sptr,
Lev Walkin8c3b8542005-03-10 18:52:02 +00001102 const char *opt_mname, const void *buf_ptr, size_t size,
Lev Walkindc06f6b2004-10-20 15:50:55 +00001103 int (*opt_unexpected_tag_decoder)
Lev Walkin0fab1a62005-03-09 22:19:25 +00001104 (void *struct_ptr, const void *chunk_buf, size_t chunk_size),
Lev Walkindc06f6b2004-10-20 15:50:55 +00001105 ssize_t (*body_receiver)
Lev Walkin0fab1a62005-03-09 22:19:25 +00001106 (void *struct_ptr, const void *chunk_buf, size_t chunk_size,
Lev Walkindc06f6b2004-10-20 15:50:55 +00001107 int have_more)
1108) {
Lev Walkind5125642005-02-14 20:08:00 +00001109 OCTET_STRING_t *st = (OCTET_STRING_t *)*sptr;
Lev Walkindc06f6b2004-10-20 15:50:55 +00001110 asn_OCTET_STRING_specifics_t *specs = td->specifics
1111 ? (asn_OCTET_STRING_specifics_t *)td->specifics
1112 : &asn_DEF_OCTET_STRING_specs;
1113 const char *xml_tag = opt_mname ? opt_mname : td->xml_tag;
1114 asn_struct_ctx_t *ctx; /* Per-structure parser context */
Lev Walkinc61f3862005-02-14 17:21:22 +00001115 asn_dec_rval_t rval; /* Return value from the decoder */
1116 int st_allocated;
Lev Walkindc06f6b2004-10-20 15:50:55 +00001117
1118 /*
1119 * Create the string if does not exist.
1120 */
Lev Walkinc61f3862005-02-14 17:21:22 +00001121 if(!st) {
Lev Walkin8484ed82004-12-14 13:31:01 +00001122 st = (OCTET_STRING_t *)CALLOC(1, specs->struct_size);
Lev Walkin8484ed82004-12-14 13:31:01 +00001123 *sptr = (void *)st;
Lev Walkinc61f3862005-02-14 17:21:22 +00001124 if(!st) goto sta_failed;
1125 st_allocated = 1;
Lev Walkinbdaae772005-02-18 12:25:47 +00001126 } else {
1127 st_allocated = 0;
1128 }
Lev Walkinc61f3862005-02-14 17:21:22 +00001129 if(!st->buf) {
1130 /* This is separate from above section */
1131 st->buf = (uint8_t *)CALLOC(1, 1);
1132 if(!st->buf) {
1133 if(st_allocated) {
1134 *sptr = 0;
1135 goto stb_failed;
1136 } else {
1137 goto sta_failed;
1138 }
Lev Walkindc06f6b2004-10-20 15:50:55 +00001139 }
1140 }
1141
1142 /* Restore parsing context */
1143 ctx = (asn_struct_ctx_t *)(((char *)*sptr) + specs->ctx_offset);
1144
1145 return xer_decode_general(opt_codec_ctx, ctx, *sptr, xml_tag,
1146 buf_ptr, size, opt_unexpected_tag_decoder, body_receiver);
Lev Walkinc61f3862005-02-14 17:21:22 +00001147
1148stb_failed:
1149 FREEMEM(st);
1150sta_failed:
1151 rval.code = RC_FAIL;
1152 rval.consumed = 0;
1153 return rval;
Lev Walkindc06f6b2004-10-20 15:50:55 +00001154}
1155
1156/*
1157 * Decode OCTET STRING from the hexadecimal data.
1158 */
1159asn_dec_rval_t
1160OCTET_STRING_decode_xer_hex(asn_codec_ctx_t *opt_codec_ctx,
1161 asn_TYPE_descriptor_t *td, void **sptr,
Lev Walkin8c3b8542005-03-10 18:52:02 +00001162 const char *opt_mname, const void *buf_ptr, size_t size) {
Lev Walkindc06f6b2004-10-20 15:50:55 +00001163 return OCTET_STRING__decode_xer(opt_codec_ctx, td, sptr, opt_mname,
1164 buf_ptr, size, 0, OCTET_STRING__convert_hexadecimal);
1165}
1166
1167/*
1168 * Decode OCTET STRING from the binary (0/1) data.
1169 */
1170asn_dec_rval_t
1171OCTET_STRING_decode_xer_binary(asn_codec_ctx_t *opt_codec_ctx,
1172 asn_TYPE_descriptor_t *td, void **sptr,
Lev Walkin8c3b8542005-03-10 18:52:02 +00001173 const char *opt_mname, const void *buf_ptr, size_t size) {
Lev Walkindc06f6b2004-10-20 15:50:55 +00001174 return OCTET_STRING__decode_xer(opt_codec_ctx, td, sptr, opt_mname,
1175 buf_ptr, size, 0, OCTET_STRING__convert_binary);
1176}
1177
1178/*
1179 * Decode OCTET STRING from the string (ASCII/UTF-8) data.
1180 */
1181asn_dec_rval_t
1182OCTET_STRING_decode_xer_utf8(asn_codec_ctx_t *opt_codec_ctx,
1183 asn_TYPE_descriptor_t *td, void **sptr,
Lev Walkin8c3b8542005-03-10 18:52:02 +00001184 const char *opt_mname, const void *buf_ptr, size_t size) {
Lev Walkindc06f6b2004-10-20 15:50:55 +00001185 return OCTET_STRING__decode_xer(opt_codec_ctx, td, sptr, opt_mname,
1186 buf_ptr, size,
1187 OCTET_STRING__handle_control_chars,
1188 OCTET_STRING__convert_entrefs);
1189}
1190
1191
Lev Walkinf15320b2004-06-03 03:38:44 +00001192int
Lev Walkinde4825d2004-09-29 13:20:14 +00001193OCTET_STRING_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +00001194 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin4d9528c2004-08-11 08:10:13 +00001195 static const char *h2c = "0123456789ABCDEF";
Lev Walkinc2346572004-08-11 09:07:36 +00001196 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +00001197 char scratch[16 * 3 + 4];
1198 char *p = scratch;
1199 uint8_t *buf;
1200 uint8_t *end;
1201 size_t i;
Lev Walkinf15320b2004-06-03 03:38:44 +00001202
Lev Walkind9bd7752004-06-05 08:17:50 +00001203 (void)td; /* Unused argument */
1204
Lev Walkin8e8078a2004-09-26 13:10:40 +00001205 if(!st || !st->buf) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001206
1207 /*
1208 * Dump the contents of the buffer in hexadecimal.
1209 */
1210 buf = st->buf;
1211 end = buf + st->size;
1212 for(i = 0; buf < end; buf++, i++) {
1213 if(!(i % 16) && (i || st->size > 16)) {
Lev Walkin8e8078a2004-09-26 13:10:40 +00001214 if(cb(scratch, p - scratch, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +00001215 return -1;
Lev Walkin8e8078a2004-09-26 13:10:40 +00001216 _i_INDENT(1);
Lev Walkinf15320b2004-06-03 03:38:44 +00001217 p = scratch;
1218 }
1219 *p++ = h2c[(*buf >> 4) & 0x0F];
1220 *p++ = h2c[*buf & 0x0F];
Lev Walkina9cc46e2004-09-22 16:06:28 +00001221 *p++ = 0x20;
Lev Walkinf15320b2004-06-03 03:38:44 +00001222 }
1223
Lev Walkincc6a9102004-09-23 22:06:26 +00001224 if(p > scratch) {
1225 p--; /* Remove the tail space */
Lev Walkin8e8078a2004-09-26 13:10:40 +00001226 if(cb(scratch, p - scratch, app_key) < 0)
Lev Walkincc6a9102004-09-23 22:06:26 +00001227 return -1;
1228 }
1229
1230 return 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001231}
1232
1233int
Lev Walkindc06f6b2004-10-20 15:50:55 +00001234OCTET_STRING_print_utf8(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +00001235 int ilevel, asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +00001236 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +00001237
Lev Walkind9bd7752004-06-05 08:17:50 +00001238 (void)td; /* Unused argument */
1239 (void)ilevel; /* Unused argument */
1240
Lev Walkinf15320b2004-06-03 03:38:44 +00001241 if(st && st->buf) {
Lev Walkin8e8078a2004-09-26 13:10:40 +00001242 return (cb(st->buf, st->size, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001243 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +00001244 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001245 }
1246}
1247
1248void
Lev Walkinde4825d2004-09-29 13:20:14 +00001249OCTET_STRING_free(asn_TYPE_descriptor_t *td, void *sptr, int contents_only) {
Lev Walkinc2346572004-08-11 09:07:36 +00001250 OCTET_STRING_t *st = (OCTET_STRING_t *)sptr;
Lev Walkinbbd93252004-10-12 05:57:23 +00001251 asn_OCTET_STRING_specifics_t *specs = td->specifics
Lev Walkindc06f6b2004-10-20 15:50:55 +00001252 ? (asn_OCTET_STRING_specifics_t *)td->specifics
1253 : &asn_DEF_OCTET_STRING_specs;
Lev Walkinbbd93252004-10-12 05:57:23 +00001254 asn_struct_ctx_t *ctx = (asn_struct_ctx_t *)
1255 ((char *)st + specs->ctx_offset);
Lev Walkinb54577a2004-11-08 10:47:12 +00001256 struct _stack *stck;
Lev Walkinf15320b2004-06-03 03:38:44 +00001257
1258 if(!td || !st)
1259 return;
1260
1261 ASN_DEBUG("Freeing %s as OCTET STRING", td->name);
1262
1263 if(st->buf) {
1264 FREEMEM(st->buf);
1265 }
1266
1267 /*
1268 * Remove decode-time stack.
1269 */
Lev Walkinb54577a2004-11-08 10:47:12 +00001270 stck = (struct _stack *)ctx->ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +00001271 if(stck) {
1272 while(stck->tail) {
1273 struct _stack_el *sel = stck->tail;
1274 stck->tail = sel->prev;
1275 FREEMEM(sel);
1276 }
1277 FREEMEM(stck);
1278 }
1279
1280 if(!contents_only) {
1281 FREEMEM(st);
1282 }
1283}
1284
1285/*
1286 * Conversion routines.
1287 */
1288int
1289OCTET_STRING_fromBuf(OCTET_STRING_t *st, const char *str, int len) {
1290 void *buf;
1291
1292 if(st == 0 || (str == 0 && len)) {
1293 errno = EINVAL;
1294 return -1;
1295 }
1296
1297 /*
1298 * Clear the OCTET STRING.
1299 */
1300 if(str == NULL) {
1301 if(st->buf)
1302 FREEMEM(st->buf);
1303 st->size = 0;
1304 return 0;
1305 }
1306
1307 /* Determine the original string size, if not explicitly given */
1308 if(len < 0)
1309 len = strlen(str);
1310
1311 /* Allocate and fill the memory */
1312 buf = MALLOC(len + 1);
1313 if(buf == NULL) {
1314 return -1;
1315 } else {
Lev Walkinc2346572004-08-11 09:07:36 +00001316 st->buf = (uint8_t *)buf;
Lev Walkinf15320b2004-06-03 03:38:44 +00001317 st->size = len;
1318 }
1319
1320 memcpy(buf, str, len);
1321 st->buf[st->size] = '\0'; /* Couldn't use memcpy(len+1)! */
1322
1323 return 0;
1324}
1325
1326OCTET_STRING_t *
Lev Walkinbbd93252004-10-12 05:57:23 +00001327OCTET_STRING_new_fromBuf(asn_TYPE_descriptor_t *td, const char *str, int len) {
1328 asn_OCTET_STRING_specifics_t *specs = td->specifics
Lev Walkindc06f6b2004-10-20 15:50:55 +00001329 ? (asn_OCTET_STRING_specifics_t *)td->specifics
1330 : &asn_DEF_OCTET_STRING_specs;
Lev Walkinf15320b2004-06-03 03:38:44 +00001331 OCTET_STRING_t *st;
1332
Lev Walkinbbd93252004-10-12 05:57:23 +00001333 st = (OCTET_STRING_t *)CALLOC(1, specs->struct_size);
Lev Walkinf15320b2004-06-03 03:38:44 +00001334 if(st && str && OCTET_STRING_fromBuf(st, str, len)) {
1335 free(st);
1336 st = NULL;
1337 }
1338
1339 return st;
1340}
1341