blob: df25df941a74f3345feebe5ea1dc2a2c18a95437 [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 Walkin59b176e2005-11-26 11:25:14 +000022static asn_per_constraint_t asn_DEF_OCTET_STRING_constraint = {
Lev Walkince676dd2005-12-20 22:34:55 +000023 APC_SEMI_CONSTRAINED, -1, -1, 0, 0
Lev Walkin59b176e2005-11-26 11:25:14 +000024};
Lev Walkinde4825d2004-09-29 13:20:14 +000025asn_TYPE_descriptor_t asn_DEF_OCTET_STRING = {
Lev Walkindc06f6b2004-10-20 15:50:55 +000026 "OCTET STRING", /* Canonical name */
27 "OCTET_STRING", /* XML tag name */
Lev Walkina9cc46e2004-09-22 16:06:28 +000028 OCTET_STRING_free,
29 OCTET_STRING_print, /* non-ascii stuff, generally */
Lev Walkinf15320b2004-06-03 03:38:44 +000030 asn_generic_no_constraint,
31 OCTET_STRING_decode_ber,
32 OCTET_STRING_encode_der,
Lev Walkindc06f6b2004-10-20 15:50:55 +000033 OCTET_STRING_decode_xer_hex,
Lev Walkina9cc46e2004-09-22 16:06:28 +000034 OCTET_STRING_encode_xer,
Lev Walkin59b176e2005-11-26 11:25:14 +000035 OCTET_STRING_decode_uper, /* Unaligned PER decoder */
Lev Walkinf15320b2004-06-03 03:38:44 +000036 0, /* Use generic outmost tag fetcher */
Lev Walkinde4825d2004-09-29 13:20:14 +000037 asn_DEF_OCTET_STRING_tags,
38 sizeof(asn_DEF_OCTET_STRING_tags)
39 / sizeof(asn_DEF_OCTET_STRING_tags[0]),
40 asn_DEF_OCTET_STRING_tags, /* Same as above */
41 sizeof(asn_DEF_OCTET_STRING_tags)
42 / sizeof(asn_DEF_OCTET_STRING_tags[0]),
Lev Walkin59b176e2005-11-26 11:25:14 +000043 0, /* No PER visible constraints */
Lev Walkin449f8322004-08-20 13:23:42 +000044 0, 0, /* No members */
Lev Walkinbbd93252004-10-12 05:57:23 +000045 &asn_DEF_OCTET_STRING_specs
Lev Walkinf15320b2004-06-03 03:38:44 +000046};
47
Lev Walkincc6a9102004-09-23 22:06:26 +000048#undef _CH_PHASE
49#undef NEXT_PHASE
50#undef PREV_PHASE
Lev Walkin75b1bef2005-04-25 19:38:21 +000051#define _CH_PHASE(ctx, inc) do { \
52 if(ctx->phase == 0) \
53 ctx->context = 0; \
54 ctx->phase += inc; \
Lev Walkinf15320b2004-06-03 03:38:44 +000055 } while(0)
56#define NEXT_PHASE(ctx) _CH_PHASE(ctx, +1)
57#define PREV_PHASE(ctx) _CH_PHASE(ctx, -1)
58
Lev Walkincc6a9102004-09-23 22:06:26 +000059#undef ADVANCE
Lev Walkin75b1bef2005-04-25 19:38:21 +000060#define ADVANCE(num_bytes) do { \
61 size_t num = (num_bytes); \
62 buf_ptr = ((const char *)buf_ptr) + num; \
63 size -= num; \
64 consumed_myself += num; \
Lev Walkinf15320b2004-06-03 03:38:44 +000065 } while(0)
66
Lev Walkincc6a9102004-09-23 22:06:26 +000067#undef RETURN
Lev Walkin75b1bef2005-04-25 19:38:21 +000068#define RETURN(_code) do { \
Lev Walkin59b176e2005-11-26 11:25:14 +000069 asn_dec_rval_t tmprval; \
70 tmprval.code = _code; \
71 tmprval.consumed = consumed_myself; \
72 return tmprval; \
Lev Walkinf15320b2004-06-03 03:38:44 +000073 } while(0)
74
Lev Walkincc6a9102004-09-23 22:06:26 +000075#undef APPEND
Lev Walkind9bd7752004-06-05 08:17:50 +000076#define APPEND(bufptr, bufsize) do { \
Lev Walkin75b1bef2005-04-25 19:38:21 +000077 size_t _bs = (bufsize); /* Append size */ \
78 size_t _ns = ctx->context; /* Allocated now */ \
79 size_t _es = st->size + _bs; /* Expected size */ \
80 /* int is really a typeof(st->size): */ \
81 if((int)_es < 0) RETURN(RC_FAIL); \
82 if(_ns <= _es) { \
Lev Walkind9bd7752004-06-05 08:17:50 +000083 void *ptr; \
Lev Walkin188ed2c2004-09-13 08:31:01 +000084 /* Be nice and round to the memory allocator */ \
Lev Walkin75b1bef2005-04-25 19:38:21 +000085 do { _ns = _ns ? _ns << 1 : 16; } \
86 while(_ns <= _es); \
87 /* int is really a typeof(st->size): */ \
88 if((int)_ns < 0) RETURN(RC_FAIL); \
Lev Walkind9bd7752004-06-05 08:17:50 +000089 ptr = REALLOC(st->buf, _ns); \
90 if(ptr) { \
Lev Walkinc2346572004-08-11 09:07:36 +000091 st->buf = (uint8_t *)ptr; \
Lev Walkin75b1bef2005-04-25 19:38:21 +000092 ctx->context = _ns; \
Lev Walkind9bd7752004-06-05 08:17:50 +000093 } else { \
94 RETURN(RC_FAIL); \
95 } \
Lev Walkin7f85ef42005-07-02 20:24:27 +000096 ASN_DEBUG("Reallocating into %ld", (long)_ns); \
Lev Walkind9bd7752004-06-05 08:17:50 +000097 } \
Lev Walkin8d127872004-09-04 04:44:50 +000098 memcpy(st->buf + st->size, bufptr, _bs); \
Lev Walkind9bd7752004-06-05 08:17:50 +000099 /* Convenient nul-termination */ \
Lev Walkin75b1bef2005-04-25 19:38:21 +0000100 st->buf[_es] = '\0'; \
101 st->size = _es; \
Lev Walkinf15320b2004-06-03 03:38:44 +0000102 } while(0)
103
104/*
Lev Walkin07f388c2004-10-11 11:43:08 +0000105 * Internal variant of the OCTET STRING.
106 */
107typedef enum OS_type {
108 _TT_GENERIC = 0, /* Just a random OCTET STRING */
109 _TT_BIT_STRING = 1, /* BIT STRING type, a special case */
Lev Walkinfdb25922005-07-21 09:32:49 +0000110 _TT_ANY = 2 /* ANY type, a special case too */
Lev Walkin07f388c2004-10-11 11:43:08 +0000111} OS_type_e;
112
113/*
Lev Walkinf15320b2004-06-03 03:38:44 +0000114 * The main reason why ASN.1 is still alive is that too much time and effort
115 * is necessary for learning it more or less adequately, thus creating a gut
116 * necessity to demonstrate that aquired skill everywhere afterwards.
117 * No, I am not going to explain what the following stuff is.
118 */
119struct _stack_el {
Lev Walkin3990ba62004-09-24 20:57:41 +0000120 ber_tlv_len_t left; /* What's left to read (or -1) */
Lev Walkin5c915992004-09-27 20:54:06 +0000121 ber_tlv_len_t got; /* What was actually processed */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000122 int cont_level; /* Depth of subcontainment */
Lev Walkinf15320b2004-06-03 03:38:44 +0000123 int want_nulls; /* Want null "end of content" octets? */
124 int bits_chopped; /* Flag in BIT STRING mode */
Lev Walkin5c915992004-09-27 20:54:06 +0000125 ber_tlv_tag_t tag; /* For debugging purposes */
Lev Walkinf15320b2004-06-03 03:38:44 +0000126 struct _stack_el *prev;
127 struct _stack_el *next;
128};
129struct _stack {
130 struct _stack_el *tail;
131 struct _stack_el *cur_ptr;
132};
133
134static struct _stack_el *
Lev Walkin3990ba62004-09-24 20:57:41 +0000135OS__add_stack_el(struct _stack *st) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000136 struct _stack_el *nel;
137
Lev Walkin188ed2c2004-09-13 08:31:01 +0000138 /*
139 * Reuse the old stack frame or allocate a new one.
140 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000141 if(st->cur_ptr && st->cur_ptr->next) {
142 nel = st->cur_ptr->next;
Lev Walkinf15320b2004-06-03 03:38:44 +0000143 nel->bits_chopped = 0;
Lev Walkin5c915992004-09-27 20:54:06 +0000144 nel->got = 0;
145 /* Retain the nel->cont_level, it's correct. */
Lev Walkinf15320b2004-06-03 03:38:44 +0000146 } else {
Lev Walkin814cca72004-12-15 23:23:53 +0000147 nel = (struct _stack_el *)CALLOC(1, sizeof(struct _stack_el));
Lev Walkinf15320b2004-06-03 03:38:44 +0000148 if(nel == NULL)
149 return NULL;
150
151 if(st->tail) {
Lev Walkin188ed2c2004-09-13 08:31:01 +0000152 /* Increase a subcontainment depth */
153 nel->cont_level = st->tail->cont_level + 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000154 st->tail->next = nel;
155 }
156 nel->prev = st->tail;
157 st->tail = nel;
158 }
159
160 st->cur_ptr = nel;
161
162 return nel;
163}
164
165static struct _stack *
166_new_stack() {
Lev Walkinbbd93252004-10-12 05:57:23 +0000167 return (struct _stack *)CALLOC(1, sizeof(struct _stack));
Lev Walkinf15320b2004-06-03 03:38:44 +0000168}
169
170/*
171 * Decode OCTET STRING type.
172 */
Lev Walkindc06f6b2004-10-20 15:50:55 +0000173asn_dec_rval_t
Lev Walkinde4825d2004-09-29 13:20:14 +0000174OCTET_STRING_decode_ber(asn_codec_ctx_t *opt_codec_ctx,
175 asn_TYPE_descriptor_t *td,
Lev Walkin59b176e2005-11-26 11:25:14 +0000176 void **sptr, const void *buf_ptr, size_t size, int tag_mode) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000177 asn_OCTET_STRING_specifics_t *specs = td->specifics
Lev Walkindc06f6b2004-10-20 15:50:55 +0000178 ? (asn_OCTET_STRING_specifics_t *)td->specifics
179 : &asn_DEF_OCTET_STRING_specs;
Lev Walkin59b176e2005-11-26 11:25:14 +0000180 BIT_STRING_t *st = (BIT_STRING_t *)*sptr;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000181 asn_dec_rval_t rval;
Lev Walkinde4825d2004-09-29 13:20:14 +0000182 asn_struct_ctx_t *ctx;
Lev Walkinf15320b2004-06-03 03:38:44 +0000183 ssize_t consumed_myself = 0;
Lev Walkinbbd93252004-10-12 05:57:23 +0000184 struct _stack *stck; /* Expectations stack structure */
Lev Walkin5c915992004-09-27 20:54:06 +0000185 struct _stack_el *sel = 0; /* Stack element */
Lev Walkinf15320b2004-06-03 03:38:44 +0000186 int tlv_constr;
Lev Walkinbbd93252004-10-12 05:57:23 +0000187 OS_type_e type_variant = (OS_type_e)specs->subvariant;
Lev Walkinf15320b2004-06-03 03:38:44 +0000188
Lev Walkincc6a9102004-09-23 22:06:26 +0000189 ASN_DEBUG("Decoding %s as %s (frame %ld)",
190 td->name,
Lev Walkinbbd93252004-10-12 05:57:23 +0000191 (type_variant == _TT_GENERIC) ?
192 "OCTET STRING" : "OS-SpecialCase",
Lev Walkincc6a9102004-09-23 22:06:26 +0000193 (long)size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000194
195 /*
196 * Create the string if does not exist.
197 */
198 if(st == NULL) {
Lev Walkin59b176e2005-11-26 11:25:14 +0000199 st = (BIT_STRING_t *)(*sptr = CALLOC(1, specs->struct_size));
200 if(st == NULL) RETURN(RC_FAIL);
Lev Walkinf15320b2004-06-03 03:38:44 +0000201 }
202
203 /* Restore parsing context */
Lev Walkinbbd93252004-10-12 05:57:23 +0000204 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000205
206 switch(ctx->phase) {
207 case 0:
208 /*
209 * Check tags.
210 */
Lev Walkinde4825d2004-09-29 13:20:14 +0000211 rval = ber_check_tags(opt_codec_ctx, td, ctx,
Lev Walkin8e8078a2004-09-26 13:10:40 +0000212 buf_ptr, size, tag_mode, -1,
Lev Walkinf15320b2004-06-03 03:38:44 +0000213 &ctx->left, &tlv_constr);
Lev Walkin443d2512004-10-05 06:35:31 +0000214 if(rval.code != RC_OK)
215 return rval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000216
Lev Walkinf15320b2004-06-03 03:38:44 +0000217 if(tlv_constr) {
218 /*
219 * Complex operation, requires stack of expectations.
220 */
221 ctx->ptr = _new_stack();
222 if(ctx->ptr) {
Lev Walkin814cca72004-12-15 23:23:53 +0000223 stck = (struct _stack *)ctx->ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000224 } else {
225 RETURN(RC_FAIL);
226 }
227 } else {
228 /*
229 * Jump into stackless primitive decoding.
230 */
231 _CH_PHASE(ctx, 3);
Lev Walkinbbd93252004-10-12 05:57:23 +0000232 if(type_variant == _TT_ANY && tag_mode != 1)
Lev Walkin188ed2c2004-09-13 08:31:01 +0000233 APPEND(buf_ptr, rval.consumed);
Lev Walkinf15320b2004-06-03 03:38:44 +0000234 ADVANCE(rval.consumed);
235 goto phase3;
236 }
237
Lev Walkinf15320b2004-06-03 03:38:44 +0000238 NEXT_PHASE(ctx);
239 /* Fall through */
240 case 1:
241 phase1:
242 /*
243 * Fill the stack with expectations.
244 */
Lev Walkin814cca72004-12-15 23:23:53 +0000245 stck = (struct _stack *)ctx->ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000246 sel = stck->cur_ptr;
247 do {
248 ber_tlv_tag_t tlv_tag;
249 ber_tlv_len_t tlv_len;
Lev Walkin188ed2c2004-09-13 08:31:01 +0000250 ber_tlv_tag_t expected_tag;
Lev Walkin5c915992004-09-27 20:54:06 +0000251 ssize_t tl, ll, tlvl;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000252 /* This one works even if (sel->left == -1) */
253 ssize_t Left = ((!sel||(size_t)sel->left >= size)
Lev Walkin5c915992004-09-27 20:54:06 +0000254 ?(ssize_t)size:sel->left);
Lev Walkinf15320b2004-06-03 03:38:44 +0000255
Lev Walkin3990ba62004-09-24 20:57:41 +0000256
Lev Walkinabf68892004-10-26 10:12:14 +0000257 ASN_DEBUG("%p, s->l=%ld, s->wn=%ld, s->g=%ld\n", sel,
Lev Walkin1e3ccbb2004-10-26 10:56:10 +0000258 (long)(sel?sel->left:0),
259 (long)(sel?sel->want_nulls:0),
260 (long)(sel?sel->got:0)
Lev Walkin5c915992004-09-27 20:54:06 +0000261 );
262 if(sel && sel->left <= 0 && sel->want_nulls == 0) {
263 if(sel->prev) {
264 struct _stack_el *prev = sel->prev;
265 if(prev->left != -1) {
266 if(prev->left < sel->got)
267 RETURN(RC_FAIL);
268 prev->left -= sel->got;
269 }
270 prev->got += sel->got;
271 sel = stck->cur_ptr = prev;
272 if(!sel) break;
273 tlv_constr = 1;
274 continue;
275 } else {
276 sel = stck->cur_ptr = 0;
277 break; /* Nothing to wait */
278 }
279 }
280
Lev Walkin3990ba62004-09-24 20:57:41 +0000281 tl = ber_fetch_tag(buf_ptr, Left, &tlv_tag);
Lev Walkinabf68892004-10-26 10:12:14 +0000282 ASN_DEBUG("fetch tag(size=%ld,L=%ld), %sstack, left=%ld, wn=%ld, tl=%ld",
283 (long)size, (long)Left, sel?"":"!",
Lev Walkin1e3ccbb2004-10-26 10:56:10 +0000284 (long)(sel?sel->left:0),
285 (long)(sel?sel->want_nulls:0),
Lev Walkinabf68892004-10-26 10:12:14 +0000286 (long)tl);
Lev Walkinf15320b2004-06-03 03:38:44 +0000287 switch(tl) {
288 case -1: RETURN(RC_FAIL);
289 case 0: RETURN(RC_WMORE);
290 }
291
292 tlv_constr = BER_TLV_CONSTRUCTED(buf_ptr);
293
294 ll = ber_fetch_length(tlv_constr,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000295 (const char *)buf_ptr + tl,Left - tl,&tlv_len);
Lev Walkina6a926a2005-03-02 01:54:28 +0000296 ASN_DEBUG("Got tag=%s, tc=%d, left=%ld, tl=%ld, len=%ld, ll=%ld",
Lev Walkin8d127872004-09-04 04:44:50 +0000297 ber_tlv_tag_string(tlv_tag), tlv_constr,
Lev Walkina6a926a2005-03-02 01:54:28 +0000298 (long)Left, (long)tl, (long)tlv_len, (long)ll);
Lev Walkinf15320b2004-06-03 03:38:44 +0000299 switch(ll) {
300 case -1: RETURN(RC_FAIL);
301 case 0: RETURN(RC_WMORE);
302 }
303
Lev Walkin8d127872004-09-04 04:44:50 +0000304 if(sel && sel->want_nulls
Lev Walkin8c3b8542005-03-10 18:52:02 +0000305 && ((const uint8_t *)buf_ptr)[0] == 0
306 && ((const uint8_t *)buf_ptr)[1] == 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000307 {
Lev Walkin8d127872004-09-04 04:44:50 +0000308
309 ASN_DEBUG("Eat EOC; wn=%d--", sel->want_nulls);
310
Lev Walkinbbd93252004-10-12 05:57:23 +0000311 if(type_variant == _TT_ANY
Lev Walkin07f388c2004-10-11 11:43:08 +0000312 && (tag_mode != 1 || sel->cont_level))
313 APPEND("\0\0", 2);
Lev Walkin5c915992004-09-27 20:54:06 +0000314
315 ADVANCE(2);
316 sel->got += 2;
317 if(sel->left != -1) {
318 sel->left -= 2; /* assert(sel->left >= 2) */
319 }
320
Lev Walkinf15320b2004-06-03 03:38:44 +0000321 sel->want_nulls--;
322 if(sel->want_nulls == 0) {
323 /* Move to the next expectation */
Lev Walkin5c915992004-09-27 20:54:06 +0000324 sel->left = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000325 tlv_constr = 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000326 }
Lev Walkin8d127872004-09-04 04:44:50 +0000327
328 continue;
Lev Walkin188ed2c2004-09-13 08:31:01 +0000329 }
330
331 /*
332 * Set up expected tags,
333 * depending on ASN.1 type being decoded.
334 */
Lev Walkinbbd93252004-10-12 05:57:23 +0000335 switch(type_variant) {
Lev Walkin188ed2c2004-09-13 08:31:01 +0000336 case _TT_BIT_STRING:
337 /* X.690: 8.6.4.1, NOTE 2 */
338 /* Fall through */
339 case _TT_GENERIC:
340 default:
341 if(sel) {
342 int level = sel->cont_level;
343 if(level < td->all_tags_count) {
344 expected_tag = td->all_tags[level];
345 break;
346 } else if(td->all_tags_count) {
347 expected_tag = td->all_tags
348 [td->all_tags_count - 1];
349 break;
350 }
351 /* else, Fall through */
352 }
353 /* Fall through */
354 case _TT_ANY:
355 expected_tag = tlv_tag;
356 break;
357 }
358
359
360 if(tlv_tag != expected_tag) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000361 char buf[2][32];
362 ber_tlv_tag_snprint(tlv_tag,
363 buf[0], sizeof(buf[0]));
364 ber_tlv_tag_snprint(td->tags[td->tags_count-1],
365 buf[1], sizeof(buf[1]));
366 ASN_DEBUG("Tag does not match expectation: %s != %s",
367 buf[0], buf[1]);
368 RETURN(RC_FAIL);
369 }
370
Lev Walkinde4825d2004-09-29 13:20:14 +0000371 tlvl = tl + ll; /* Combined length of T and L encoding */
372 if((tlv_len + tlvl) < 0) {
373 /* tlv_len value is too big */
374 ASN_DEBUG("TLV encoding + length (%ld) is too big",
375 (long)tlv_len);
376 RETURN(RC_FAIL);
377 }
378
Lev Walkinf15320b2004-06-03 03:38:44 +0000379 /*
380 * Append a new expectation.
381 */
Lev Walkin3990ba62004-09-24 20:57:41 +0000382 sel = OS__add_stack_el(stck);
Lev Walkin5c915992004-09-27 20:54:06 +0000383 if(!sel) RETURN(RC_FAIL);
Lev Walkinf15320b2004-06-03 03:38:44 +0000384
Lev Walkin5c915992004-09-27 20:54:06 +0000385 sel->tag = tlv_tag;
386
387 sel->want_nulls = (tlv_len==-1);
388 if(sel->prev && sel->prev->left != -1) {
389 /* Check that the parent frame is big enough */
390 if(sel->prev->left < tlvl + (tlv_len==-1?0:tlv_len))
391 RETURN(RC_FAIL);
392 if(tlv_len == -1)
393 sel->left = sel->prev->left - tlvl;
394 else
395 sel->left = tlv_len;
396 } else {
397 sel->left = tlv_len;
398 }
Lev Walkinbbd93252004-10-12 05:57:23 +0000399 if(type_variant == _TT_ANY
Lev Walkin07f388c2004-10-11 11:43:08 +0000400 && (tag_mode != 1 || sel->cont_level))
401 APPEND(buf_ptr, tlvl);
Lev Walkin5c915992004-09-27 20:54:06 +0000402 sel->got += tlvl;
403 ADVANCE(tlvl);
404
Lev Walkinabf68892004-10-26 10:12:14 +0000405 ASN_DEBUG("+EXPECT2 got=%ld left=%ld, wn=%d, clvl=%d",
406 (long)sel->got, (long)sel->left,
407 sel->want_nulls, sel->cont_level);
Lev Walkin5c915992004-09-27 20:54:06 +0000408
Lev Walkinf15320b2004-06-03 03:38:44 +0000409 } while(tlv_constr);
410 if(sel == NULL) {
411 /* Finished operation, "phase out" */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000412 ASN_DEBUG("Phase out");
Lev Walkinf15320b2004-06-03 03:38:44 +0000413 _CH_PHASE(ctx, +3);
414 break;
415 }
416
417 NEXT_PHASE(ctx);
418 /* Fall through */
419 case 2:
Lev Walkin814cca72004-12-15 23:23:53 +0000420 stck = (struct _stack *)ctx->ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000421 sel = stck->cur_ptr;
Lev Walkin5c915992004-09-27 20:54:06 +0000422 ASN_DEBUG("Phase 2: Need %ld bytes, size=%ld, alrg=%ld, wn=%d",
423 (long)sel->left, (long)size, (long)sel->got,
424 sel->want_nulls);
Lev Walkinf15320b2004-06-03 03:38:44 +0000425 {
426 ber_tlv_len_t len;
427
428 assert(sel->left >= 0);
429
Lev Walkinec1ffd42004-08-18 04:53:32 +0000430 len = ((ber_tlv_len_t)size < sel->left)
431 ? (ber_tlv_len_t)size : sel->left;
Lev Walkinf15320b2004-06-03 03:38:44 +0000432 if(len > 0) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000433 if(type_variant == _TT_BIT_STRING
Lev Walkin188ed2c2004-09-13 08:31:01 +0000434 && sel->bits_chopped == 0) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000435 /* Put the unused-bits-octet away */
Lev Walkin8c3b8542005-03-10 18:52:02 +0000436 st->bits_unused = *(const uint8_t *)buf_ptr;
437 APPEND(((const char *)buf_ptr+1), (len - 1));
Lev Walkinf15320b2004-06-03 03:38:44 +0000438 sel->bits_chopped = 1;
439 } else {
440 APPEND(buf_ptr, len);
441 }
442 ADVANCE(len);
443 sel->left -= len;
Lev Walkin5c915992004-09-27 20:54:06 +0000444 sel->got += len;
Lev Walkinf15320b2004-06-03 03:38:44 +0000445 }
446
Lev Walkin5c915992004-09-27 20:54:06 +0000447 if(sel->left) {
448 ASN_DEBUG("OS left %ld, size = %ld, wn=%d\n",
449 (long)sel->left, (long)size, sel->want_nulls);
Lev Walkinf15320b2004-06-03 03:38:44 +0000450 RETURN(RC_WMORE);
Lev Walkinf15320b2004-06-03 03:38:44 +0000451 }
Lev Walkin5c915992004-09-27 20:54:06 +0000452
453 PREV_PHASE(ctx);
454 goto phase1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000455 }
456 break;
457 case 3:
458 phase3:
459 /*
460 * Primitive form, no stack required.
461 */
Lev Walkinbbd93252004-10-12 05:57:23 +0000462 assert(ctx->left >= 0);
463
Lev Walkind9bd7752004-06-05 08:17:50 +0000464 if(size < (size_t)ctx->left) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000465 if(!size) RETURN(RC_WMORE);
Lev Walkin75b1bef2005-04-25 19:38:21 +0000466 if(type_variant == _TT_BIT_STRING && !ctx->context) {
Lev Walkin8c3b8542005-03-10 18:52:02 +0000467 st->bits_unused = *(const uint8_t *)buf_ptr;
Lev Walkinbbd93252004-10-12 05:57:23 +0000468 ctx->left--;
469 ADVANCE(1);
470 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000471 APPEND(buf_ptr, size);
Lev Walkin75b1bef2005-04-25 19:38:21 +0000472 assert(ctx->context > 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000473 ctx->left -= size;
474 ADVANCE(size);
475 RETURN(RC_WMORE);
476 } else {
Lev Walkinbbd93252004-10-12 05:57:23 +0000477 if(type_variant == _TT_BIT_STRING
Lev Walkin75b1bef2005-04-25 19:38:21 +0000478 && !ctx->context && ctx->left) {
Lev Walkin8c3b8542005-03-10 18:52:02 +0000479 st->bits_unused = *(const uint8_t *)buf_ptr;
Lev Walkinbbd93252004-10-12 05:57:23 +0000480 ctx->left--;
481 ADVANCE(1);
482 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000483 APPEND(buf_ptr, ctx->left);
484 ADVANCE(ctx->left);
485 ctx->left = 0;
486
487 NEXT_PHASE(ctx);
488 }
489 break;
490 }
491
Lev Walkin5c915992004-09-27 20:54:06 +0000492 if(sel) {
493 ASN_DEBUG("3sel p=%p, wn=%d, l=%ld, g=%ld, size=%ld",
494 sel->prev, sel->want_nulls,
495 (long)sel->left, (long)sel->got, (long)size);
496 if(sel->prev || sel->want_nulls > 1 || sel->left > 0) {
497 RETURN(RC_WMORE);
498 }
499 }
500
Lev Walkinf15320b2004-06-03 03:38:44 +0000501 /*
502 * BIT STRING-specific processing.
503 */
Lev Walkinbbd93252004-10-12 05:57:23 +0000504 if(type_variant == _TT_BIT_STRING && st->size) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000505 /* Finalize BIT STRING: zero out unused bits. */
Lev Walkinbbd93252004-10-12 05:57:23 +0000506 st->buf[st->size-1] &= 0xff << st->bits_unused;
Lev Walkinf15320b2004-06-03 03:38:44 +0000507 }
508
Lev Walkinabf68892004-10-26 10:12:14 +0000509 ASN_DEBUG("Took %ld bytes to encode %s: [%s]:%ld",
510 (long)consumed_myself, td->name,
Lev Walkinbbd93252004-10-12 05:57:23 +0000511 (type_variant == _TT_GENERIC) ? (char *)st->buf : "<data>",
Lev Walkinabf68892004-10-26 10:12:14 +0000512 (long)st->size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000513
Lev Walkinf15320b2004-06-03 03:38:44 +0000514
Lev Walkin5c915992004-09-27 20:54:06 +0000515 RETURN(RC_OK);
Lev Walkinf15320b2004-06-03 03:38:44 +0000516}
517
518/*
519 * Encode OCTET STRING type using DER.
520 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000521asn_enc_rval_t
Lev Walkinbbd93252004-10-12 05:57:23 +0000522OCTET_STRING_encode_der(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000523 int tag_mode, ber_tlv_tag_t tag,
524 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000525 asn_enc_rval_t er;
526 asn_OCTET_STRING_specifics_t *specs = td->specifics
Lev Walkindc06f6b2004-10-20 15:50:55 +0000527 ? (asn_OCTET_STRING_specifics_t *)td->specifics
528 : &asn_DEF_OCTET_STRING_specs;
Lev Walkinbbd93252004-10-12 05:57:23 +0000529 BIT_STRING_t *st = (BIT_STRING_t *)sptr;
530 OS_type_e type_variant = (OS_type_e)specs->subvariant;
531 int fix_last_byte = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000532
533 ASN_DEBUG("%s %s as OCTET STRING",
Lev Walkin1f670c12004-09-02 12:57:25 +0000534 cb?"Estimating":"Encoding", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000535
536 /*
Lev Walkinbbd93252004-10-12 05:57:23 +0000537 * Write tags.
Lev Walkinf15320b2004-06-03 03:38:44 +0000538 */
Lev Walkinbbd93252004-10-12 05:57:23 +0000539 if(type_variant != _TT_ANY || tag_mode == 1) {
540 er.encoded = der_write_tags(td,
541 (type_variant == _TT_BIT_STRING) + st->size,
542 tag_mode, type_variant == _TT_ANY, tag, cb, app_key);
543 if(er.encoded == -1) {
544 er.failed_type = td;
545 er.structure_ptr = sptr;
546 return er;
Lev Walkinf15320b2004-06-03 03:38:44 +0000547 }
Lev Walkinbbd93252004-10-12 05:57:23 +0000548 } else {
549 /* Disallow: [<tag>] IMPLICIT ANY */
550 assert(type_variant != _TT_ANY || tag_mode != -1);
551 er.encoded = 0;
552 }
553
554 if(!cb) {
555 er.encoded += (type_variant == _TT_BIT_STRING) + st->size;
Lev Walkin59b176e2005-11-26 11:25:14 +0000556 _ASN_ENCODED_OK(er);
Lev Walkinf15320b2004-06-03 03:38:44 +0000557 }
558
Lev Walkin07f388c2004-10-11 11:43:08 +0000559 /*
Lev Walkinbbd93252004-10-12 05:57:23 +0000560 * Prepare to deal with the last octet of BIT STRING.
Lev Walkin07f388c2004-10-11 11:43:08 +0000561 */
Lev Walkinbbd93252004-10-12 05:57:23 +0000562 if(type_variant == _TT_BIT_STRING) {
563 uint8_t b = st->bits_unused & 0x07;
564 if(b && st->size) fix_last_byte = 1;
565 _ASN_CALLBACK(&b, 1);
566 er.encoded++;
Lev Walkinf15320b2004-06-03 03:38:44 +0000567 }
568
Lev Walkinbbd93252004-10-12 05:57:23 +0000569 /* Invoke callback for the main part of the buffer */
570 _ASN_CALLBACK(st->buf, st->size - fix_last_byte);
Lev Walkinf15320b2004-06-03 03:38:44 +0000571
Lev Walkinbbd93252004-10-12 05:57:23 +0000572 /* The last octet should be stripped off the unused bits */
573 if(fix_last_byte) {
574 uint8_t b = st->buf[st->size-1] & (0xff << st->bits_unused);
575 _ASN_CALLBACK(&b, 1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000576 }
577
Lev Walkinbbd93252004-10-12 05:57:23 +0000578 er.encoded += st->size;
Lev Walkin59b176e2005-11-26 11:25:14 +0000579 _ASN_ENCODED_OK(er);
Lev Walkinbbd93252004-10-12 05:57:23 +0000580cb_failed:
581 _ASN_ENCODE_FAILED;
Lev Walkinf15320b2004-06-03 03:38:44 +0000582}
583
Lev Walkina9cc46e2004-09-22 16:06:28 +0000584asn_enc_rval_t
Lev Walkinde4825d2004-09-29 13:20:14 +0000585OCTET_STRING_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-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
Lev Walkin942fd082004-10-03 09:13:02 +0000597 if(!st || !st->buf)
598 _ASN_ENCODE_FAILED;
Lev Walkina9cc46e2004-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 }
Lev Walkincc6a9102004-09-23 22:06:26 +0000618
619 _ASN_CALLBACK(scratch, p-scratch); /* Dump the rest */
620 er.encoded += p - scratch;
Lev Walkina9cc46e2004-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 }
Lev Walkincc6a9102004-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 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000640 }
641
Lev Walkin59b176e2005-11-26 11:25:14 +0000642 _ASN_ENCODED_OK(er);
Lev Walkin942fd082004-10-03 09:13:02 +0000643cb_failed:
644 _ASN_ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000645}
646
Lev Walkin9ca81892004-10-03 10:54:25 +0000647static struct OCTET_STRING__xer_escape_table_s {
648 char *string;
649 int size;
650} OCTET_STRING__xer_escape_table[] = {
651#define OSXET(s) { s, sizeof(s) - 1 }
652 OSXET("\074\156\165\154\057\076"), /* <nul/> */
653 OSXET("\074\163\157\150\057\076"), /* <soh/> */
654 OSXET("\074\163\164\170\057\076"), /* <stx/> */
655 OSXET("\074\145\164\170\057\076"), /* <etx/> */
656 OSXET("\074\145\157\164\057\076"), /* <eot/> */
657 OSXET("\074\145\156\161\057\076"), /* <enq/> */
658 OSXET("\074\141\143\153\057\076"), /* <ack/> */
659 OSXET("\074\142\145\154\057\076"), /* <bel/> */
660 OSXET("\074\142\163\057\076"), /* <bs/> */
661 OSXET("\011"), /* \t */
662 OSXET("\012"), /* \n */
663 OSXET("\074\166\164\057\076"), /* <vt/> */
664 OSXET("\074\146\146\057\076"), /* <ff/> */
665 OSXET("\015"), /* \r */
666 OSXET("\074\163\157\057\076"), /* <so/> */
667 OSXET("\074\163\151\057\076"), /* <si/> */
668 OSXET("\074\144\154\145\057\076"), /* <dle/> */
669 OSXET("\074\144\143\061\057\076"), /* <de1/> */
670 OSXET("\074\144\143\062\057\076"), /* <de2/> */
671 OSXET("\074\144\143\063\057\076"), /* <de3/> */
672 OSXET("\074\144\143\064\057\076"), /* <de4/> */
673 OSXET("\074\156\141\153\057\076"), /* <nak/> */
674 OSXET("\074\163\171\156\057\076"), /* <syn/> */
675 OSXET("\074\145\164\142\057\076"), /* <etb/> */
676 OSXET("\074\143\141\156\057\076"), /* <can/> */
677 OSXET("\074\145\155\057\076"), /* <em/> */
678 OSXET("\074\163\165\142\057\076"), /* <sub/> */
679 OSXET("\074\145\163\143\057\076"), /* <esc/> */
680 OSXET("\074\151\163\064\057\076"), /* <is4/> */
681 OSXET("\074\151\163\063\057\076"), /* <is3/> */
682 OSXET("\074\151\163\062\057\076"), /* <is2/> */
683 OSXET("\074\151\163\061\057\076"), /* <is1/> */
684 { 0, 0 }, /* " " */
685 { 0, 0 }, /* ! */
686 { 0, 0 }, /* \" */
687 { 0, 0 }, /* # */
688 { 0, 0 }, /* $ */
689 { 0, 0 }, /* % */
690 OSXET("\046\141\155\160\073"), /* &amp; */
691 { 0, 0 }, /* ' */
692 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, /* ()*+,-./ */
693 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, /* 01234567 */
694 {0,0},{0,0},{0,0},{0,0}, /* 89:; */
695 OSXET("\046\154\164\073"), /* &lt; */
696 { 0, 0 }, /* = */
697 OSXET("\046\147\164\073"), /* &gt; */
698};
699
Lev Walkindc06f6b2004-10-20 15:50:55 +0000700static int
Lev Walkin0fab1a62005-03-09 22:19:25 +0000701OS__check_escaped_control_char(const void *buf, int size) {
Lev Walkindc06f6b2004-10-20 15:50:55 +0000702 size_t i;
703 /*
704 * Inefficient algorithm which translates the escape sequences
705 * defined above into characters. Returns -1 if not found.
706 * TODO: replace by a faster algorithm (bsearch(), hash or
707 * nested table lookups).
708 */
709 for(i = 0; i < 32 /* Don't spend time on the bottom half */; i++) {
710 struct OCTET_STRING__xer_escape_table_s *el;
711 el = &OCTET_STRING__xer_escape_table[i];
712 if(el->size == size && memcmp(buf, el->string, size) == 0)
713 return i;
714 }
715 return -1;
716}
717
718static int
Lev Walkin0fab1a62005-03-09 22:19:25 +0000719OCTET_STRING__handle_control_chars(void *struct_ptr, const void *chunk_buf, size_t chunk_size) {
Lev Walkindc06f6b2004-10-20 15:50:55 +0000720 /*
721 * This might be one of the escape sequences
722 * for control characters. Check it out.
723 * #11.15.5
724 */
725 int control_char = OS__check_escaped_control_char(chunk_buf,chunk_size);
726 if(control_char >= 0) {
727 OCTET_STRING_t *st = (OCTET_STRING_t *)struct_ptr;
728 void *p = REALLOC(st->buf, st->size + 2);
729 if(p) {
730 st->buf = (uint8_t *)p;
731 st->buf[st->size++] = control_char;
732 st->buf[st->size] = '\0'; /* nul-termination */
733 return 0;
734 }
735 }
736
737 return -1; /* No, it's not */
738}
739
Lev Walkina9cc46e2004-09-22 16:06:28 +0000740asn_enc_rval_t
Lev Walkindc06f6b2004-10-20 15:50:55 +0000741OCTET_STRING_encode_xer_utf8(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000742 int ilevel, enum xer_encoder_flags_e flags,
743 asn_app_consume_bytes_f *cb, void *app_key) {
744 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
745 asn_enc_rval_t er;
Lev Walkin9ca81892004-10-03 10:54:25 +0000746 uint8_t *buf, *end;
747 uint8_t *ss; /* Sequence start */
748 ssize_t encoded_len = 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000749
750 (void)ilevel; /* Unused argument */
751 (void)flags; /* Unused argument */
752
753 if(!st || !st->buf)
754 _ASN_ENCODE_FAILED;
755
Lev Walkin9ca81892004-10-03 10:54:25 +0000756 buf = st->buf;
757 end = buf + st->size;
758 for(ss = buf; buf < end; buf++) {
Lev Walkin443d2512004-10-05 06:35:31 +0000759 unsigned int ch = *buf;
Lev Walkin9ca81892004-10-03 10:54:25 +0000760 int s_len; /* Special encoding sequence length */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000761
Lev Walkin9ca81892004-10-03 10:54:25 +0000762 /*
763 * Escape certain characters: X.680/11.15
764 */
765 if(ch < sizeof(OCTET_STRING__xer_escape_table)
766 /sizeof(OCTET_STRING__xer_escape_table[0])
767 && (s_len = OCTET_STRING__xer_escape_table[ch].size)) {
768 if(((buf - ss) && cb(ss, buf - ss, app_key) < 0)
769 || cb(OCTET_STRING__xer_escape_table[ch].string, s_len,
770 app_key) < 0)
771 _ASN_ENCODE_FAILED;
772 encoded_len += (buf - ss) + s_len;
773 ss = buf + 1;
774 }
775 }
776
777 encoded_len += (buf - ss);
778 if((buf - ss) && cb(ss, buf - ss, app_key) < 0)
779 _ASN_ENCODE_FAILED;
780
781 er.encoded = encoded_len;
Lev Walkin59b176e2005-11-26 11:25:14 +0000782 _ASN_ENCODED_OK(er);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000783}
784
Lev Walkindc06f6b2004-10-20 15:50:55 +0000785/*
786 * Convert from hexadecimal format (cstring): "AB CD EF"
787 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000788static 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 +0000789 OCTET_STRING_t *st = (OCTET_STRING_t *)sptr;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000790 const char *chunk_stop = (const char *)chunk_buf;
791 const char *p = chunk_stop;
792 const char *pend = p + chunk_size;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000793 unsigned int clv = 0;
794 int half = 0; /* Half bit */
795 uint8_t *buf;
796
797 /* Reallocate buffer according to high cap estimation */
798 ssize_t _ns = st->size + (chunk_size + 1) / 2;
799 void *nptr = REALLOC(st->buf, _ns + 1);
800 if(!nptr) return -1;
801 st->buf = (uint8_t *)nptr;
802 buf = st->buf + st->size;
803
804 /*
805 * If something like " a b c " appears here, the " a b":3 will be
806 * converted, and the rest skipped. That is, unless buf_size is greater
807 * than chunk_size, then it'll be equivalent to "ABC0".
808 */
809 for(; p < pend; p++) {
Lev Walkin0fab1a62005-03-09 22:19:25 +0000810 int ch = *(const unsigned char *)p;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000811 switch(ch) {
812 case 0x09: case 0x0a: case 0x0c: case 0x0d:
813 case 0x20:
814 /* Ignore whitespace */
815 continue;
816 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: /*01234*/
817 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: /*56789*/
818 clv = (clv << 4) + (ch - 0x30);
819 break;
820 case 0x41: case 0x42: case 0x43: /* ABC */
821 case 0x44: case 0x45: case 0x46: /* DEF */
Lev Walkin3ae21bd2005-02-21 14:43:48 +0000822 clv = (clv << 4) + (ch - 0x41 + 10);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000823 break;
824 case 0x61: case 0x62: case 0x63: /* abc */
825 case 0x64: case 0x65: case 0x66: /* def */
Lev Walkin3ae21bd2005-02-21 14:43:48 +0000826 clv = (clv << 4) + (ch - 0x61 + 10);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000827 break;
828 default:
829 *buf = 0; /* JIC */
830 return -1;
831 }
832 if(half++) {
833 half = 0;
834 *buf++ = clv;
835 chunk_stop = p + 1;
836 }
837 }
838
839 /*
840 * Check partial decoding.
841 */
842 if(half) {
843 if(have_more) {
844 /*
845 * Partial specification is fine,
846 * because no more more PXER_TEXT data is available.
847 */
848 *buf++ = clv << 4;
849 chunk_stop = p;
850 }
851 } else {
852 chunk_stop = p;
853 }
854
855 st->size = buf - st->buf; /* Adjust the buffer size */
856 assert(st->size <= _ns);
857 st->buf[st->size] = 0; /* Courtesy termination */
858
Lev Walkin0fab1a62005-03-09 22:19:25 +0000859 return (chunk_stop - (const char *)chunk_buf); /* Converted size */
Lev Walkindc06f6b2004-10-20 15:50:55 +0000860}
861
862/*
863 * Convert from binary format: "00101011101"
864 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000865static 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 +0000866 BIT_STRING_t *st = (BIT_STRING_t *)sptr;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000867 const char *p = (const char *)chunk_buf;
868 const char *pend = p + chunk_size;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000869 int bits_unused = st->bits_unused & 0x7;
870 uint8_t *buf;
871
872 /* Reallocate buffer according to high cap estimation */
873 ssize_t _ns = st->size + (chunk_size + 7) / 8;
874 void *nptr = REALLOC(st->buf, _ns + 1);
875 if(!nptr) return -1;
876 st->buf = (uint8_t *)nptr;
877 buf = st->buf + st->size;
878
879 (void)have_more;
880
881 if(bits_unused == 0)
882 bits_unused = 8;
883 else if(st->size)
884 buf--;
885
886 /*
887 * Convert series of 0 and 1 into the octet string.
888 */
889 for(; p < pend; p++) {
Lev Walkin0fab1a62005-03-09 22:19:25 +0000890 int ch = *(const unsigned char *)p;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000891 switch(ch) {
892 case 0x09: case 0x0a: case 0x0c: case 0x0d:
893 case 0x20:
894 /* Ignore whitespace */
895 break;
896 case 0x30:
897 case 0x31:
898 if(bits_unused-- <= 0) {
899 *++buf = 0; /* Clean the cell */
900 bits_unused = 7;
901 }
902 *buf |= (ch&1) << bits_unused;
903 break;
904 default:
905 st->bits_unused = bits_unused;
906 return -1;
907 }
908 }
909
910 if(bits_unused == 8) {
911 st->size = buf - st->buf;
912 st->bits_unused = 0;
913 } else {
914 st->size = buf - st->buf + 1;
915 st->bits_unused = bits_unused;
916 }
917
918 assert(st->size <= _ns);
919 st->buf[st->size] = 0; /* Courtesy termination */
920
921 return chunk_size; /* Converted in full */
922}
923
924/*
925 * Something like strtod(), but with stricter rules.
926 */
927static int
Lev Walkin0fab1a62005-03-09 22:19:25 +0000928OS__strtoent(int base, const char *buf, const char *end, int32_t *ret_value) {
Lev Walkin33700162004-10-26 09:03:31 +0000929 int32_t val = 0;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000930 const char *p;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000931
932 for(p = buf; p < end; p++) {
933 int ch = *p;
Lev Walkinb0f3db62005-07-03 05:30:15 +0000934
935 /* Strange huge value */
936 if((val * base + base) < 0)
937 return -1;
938
Lev Walkindc06f6b2004-10-20 15:50:55 +0000939 switch(ch) {
940 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: /*01234*/
941 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: /*56789*/
942 val = val * base + (ch - 0x30);
943 break;
944 case 0x41: case 0x42: case 0x43: /* ABC */
945 case 0x44: case 0x45: case 0x46: /* DEF */
Lev Walkin3ae21bd2005-02-21 14:43:48 +0000946 val = val * base + (ch - 0x41 + 10);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000947 break;
948 case 0x61: case 0x62: case 0x63: /* abc */
949 case 0x64: case 0x65: case 0x66: /* def */
Lev Walkin3ae21bd2005-02-21 14:43:48 +0000950 val = val * base + (ch - 0x61 + 10);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000951 break;
952 case 0x3b: /* ';' */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000953 *ret_value = val;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000954 return (p - buf) + 1;
955 default:
956 return -1; /* Character set error */
957 }
958 }
959
Lev Walkinb0f3db62005-07-03 05:30:15 +0000960 *ret_value = -1;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000961 return (p - buf);
962}
963
964/*
965 * Convert from the plain UTF-8 format, expanding entity references: "2 &lt; 3"
966 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000967static 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 +0000968 OCTET_STRING_t *st = (OCTET_STRING_t *)sptr;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000969 const char *p = (const char *)chunk_buf;
970 const char *pend = p + chunk_size;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000971 uint8_t *buf;
972
973 /* Reallocate buffer */
974 ssize_t _ns = st->size + chunk_size;
975 void *nptr = REALLOC(st->buf, _ns + 1);
976 if(!nptr) return -1;
977 st->buf = (uint8_t *)nptr;
978 buf = st->buf + st->size;
979
980 /*
981 * Convert series of 0 and 1 into the octet string.
982 */
983 for(; p < pend; p++) {
Lev Walkin0fab1a62005-03-09 22:19:25 +0000984 int ch = *(const unsigned char *)p;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000985 int len; /* Length of the rest of the chunk */
986
987 if(ch != 0x26 /* '&' */) {
988 *buf++ = ch;
989 continue; /* That was easy... */
990 }
991
992 /*
993 * Process entity reference.
994 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000995 len = chunk_size - (p - (const char *)chunk_buf);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000996 if(len == 1 /* "&" */) goto want_more;
997 if(p[1] == 0x23 /* '#' */) {
Lev Walkin0fab1a62005-03-09 22:19:25 +0000998 const char *pval; /* Pointer to start of digits */
999 int32_t val; /* Entity reference value */
Lev Walkindc06f6b2004-10-20 15:50:55 +00001000 int base;
1001
1002 if(len == 2 /* "&#" */) goto want_more;
1003 if(p[2] == 0x78 /* 'x' */)
1004 pval = p + 3, base = 16;
1005 else
1006 pval = p + 2, base = 10;
1007 len = OS__strtoent(base, pval, p + len, &val);
1008 if(len == -1) {
1009 /* Invalid charset. Just copy verbatim. */
1010 *buf++ = ch;
1011 continue;
1012 }
1013 if(!len || pval[len-1] != 0x3b) goto want_more;
1014 assert(val > 0);
1015 p += (pval - p) + len - 1; /* Advance past entref */
1016
1017 if(val < 0x80) {
1018 *buf++ = (char)val;
1019 } else if(val < 0x800) {
1020 *buf++ = 0xc0 | ((val >> 6));
1021 *buf++ = 0x80 | ((val & 0x3f));
1022 } else if(val < 0x10000) {
1023 *buf++ = 0xe0 | ((val >> 12));
1024 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1025 *buf++ = 0x80 | ((val & 0x3f));
1026 } else if(val < 0x200000) {
1027 *buf++ = 0xf0 | ((val >> 18));
1028 *buf++ = 0x80 | ((val >> 12) & 0x3f);
1029 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1030 *buf++ = 0x80 | ((val & 0x3f));
1031 } else if(val < 0x4000000) {
1032 *buf++ = 0xf8 | ((val >> 24));
1033 *buf++ = 0x80 | ((val >> 18) & 0x3f);
1034 *buf++ = 0x80 | ((val >> 12) & 0x3f);
1035 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1036 *buf++ = 0x80 | ((val & 0x3f));
1037 } else {
1038 *buf++ = 0xfc | ((val >> 30) & 0x1);
1039 *buf++ = 0x80 | ((val >> 24) & 0x3f);
1040 *buf++ = 0x80 | ((val >> 18) & 0x3f);
1041 *buf++ = 0x80 | ((val >> 12) & 0x3f);
1042 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1043 *buf++ = 0x80 | ((val & 0x3f));
1044 }
1045 } else {
1046 /*
1047 * Ugly, limited parsing of &amp; &gt; &lt;
1048 */
1049 char *sc = (char *)memchr(p, 0x3b, len > 5 ? 5 : len);
1050 if(!sc) goto want_more;
1051 if((sc - p) == 4
1052 && p[1] == 0x61 /* 'a' */
1053 && p[2] == 0x6d /* 'm' */
1054 && p[3] == 0x70 /* 'p' */) {
1055 *buf++ = 0x26;
1056 p = sc;
1057 continue;
1058 }
1059 if((sc - p) == 3) {
1060 if(p[1] == 0x6c) {
1061 *buf = 0x3c; /* '<' */
1062 } else if(p[1] == 0x67) {
1063 *buf = 0x3e; /* '>' */
1064 } else {
1065 /* Unsupported entity reference */
1066 *buf++ = ch;
1067 continue;
1068 }
1069 if(p[2] != 0x74) {
1070 /* Unsupported entity reference */
1071 *buf++ = ch;
1072 continue;
1073 }
1074 buf++;
1075 p = sc;
1076 continue;
1077 }
1078 /* Unsupported entity reference */
1079 *buf++ = ch;
1080 }
1081
1082 continue;
1083 want_more:
1084 if(have_more) {
1085 /*
1086 * We know that no more data (of the same type)
1087 * is coming. Copy the rest verbatim.
1088 */
1089 *buf++ = ch;
1090 continue;
1091 }
Lev Walkin0fab1a62005-03-09 22:19:25 +00001092 chunk_size = (p - (const char *)chunk_buf);
Lev Walkindc06f6b2004-10-20 15:50:55 +00001093 /* Processing stalled: need more data */
Lev Walkinbdaae772005-02-18 12:25:47 +00001094 break;
Lev Walkindc06f6b2004-10-20 15:50:55 +00001095 }
1096
1097 st->size = buf - st->buf;
1098 assert(st->size <= _ns);
1099 st->buf[st->size] = 0; /* Courtesy termination */
1100
1101 return chunk_size; /* Converted in full */
1102}
1103
1104/*
1105 * Decode OCTET STRING from the XML element's body.
1106 */
1107static asn_dec_rval_t
1108OCTET_STRING__decode_xer(asn_codec_ctx_t *opt_codec_ctx,
1109 asn_TYPE_descriptor_t *td, void **sptr,
Lev Walkin8c3b8542005-03-10 18:52:02 +00001110 const char *opt_mname, const void *buf_ptr, size_t size,
Lev Walkindc06f6b2004-10-20 15:50:55 +00001111 int (*opt_unexpected_tag_decoder)
Lev Walkin0fab1a62005-03-09 22:19:25 +00001112 (void *struct_ptr, const void *chunk_buf, size_t chunk_size),
Lev Walkindc06f6b2004-10-20 15:50:55 +00001113 ssize_t (*body_receiver)
Lev Walkin0fab1a62005-03-09 22:19:25 +00001114 (void *struct_ptr, const void *chunk_buf, size_t chunk_size,
Lev Walkindc06f6b2004-10-20 15:50:55 +00001115 int have_more)
1116) {
Lev Walkind5125642005-02-14 20:08:00 +00001117 OCTET_STRING_t *st = (OCTET_STRING_t *)*sptr;
Lev Walkindc06f6b2004-10-20 15:50:55 +00001118 asn_OCTET_STRING_specifics_t *specs = td->specifics
1119 ? (asn_OCTET_STRING_specifics_t *)td->specifics
1120 : &asn_DEF_OCTET_STRING_specs;
1121 const char *xml_tag = opt_mname ? opt_mname : td->xml_tag;
1122 asn_struct_ctx_t *ctx; /* Per-structure parser context */
Lev Walkinc61f3862005-02-14 17:21:22 +00001123 asn_dec_rval_t rval; /* Return value from the decoder */
1124 int st_allocated;
Lev Walkindc06f6b2004-10-20 15:50:55 +00001125
1126 /*
1127 * Create the string if does not exist.
1128 */
Lev Walkinc61f3862005-02-14 17:21:22 +00001129 if(!st) {
Lev Walkin8484ed82004-12-14 13:31:01 +00001130 st = (OCTET_STRING_t *)CALLOC(1, specs->struct_size);
Lev Walkin8484ed82004-12-14 13:31:01 +00001131 *sptr = (void *)st;
Lev Walkinc61f3862005-02-14 17:21:22 +00001132 if(!st) goto sta_failed;
1133 st_allocated = 1;
Lev Walkinbdaae772005-02-18 12:25:47 +00001134 } else {
1135 st_allocated = 0;
1136 }
Lev Walkinc61f3862005-02-14 17:21:22 +00001137 if(!st->buf) {
1138 /* This is separate from above section */
1139 st->buf = (uint8_t *)CALLOC(1, 1);
1140 if(!st->buf) {
1141 if(st_allocated) {
1142 *sptr = 0;
1143 goto stb_failed;
1144 } else {
1145 goto sta_failed;
1146 }
Lev Walkindc06f6b2004-10-20 15:50:55 +00001147 }
1148 }
1149
1150 /* Restore parsing context */
1151 ctx = (asn_struct_ctx_t *)(((char *)*sptr) + specs->ctx_offset);
1152
1153 return xer_decode_general(opt_codec_ctx, ctx, *sptr, xml_tag,
1154 buf_ptr, size, opt_unexpected_tag_decoder, body_receiver);
Lev Walkinc61f3862005-02-14 17:21:22 +00001155
1156stb_failed:
1157 FREEMEM(st);
1158sta_failed:
1159 rval.code = RC_FAIL;
1160 rval.consumed = 0;
1161 return rval;
Lev Walkindc06f6b2004-10-20 15:50:55 +00001162}
1163
1164/*
1165 * Decode OCTET STRING from the hexadecimal data.
1166 */
1167asn_dec_rval_t
1168OCTET_STRING_decode_xer_hex(asn_codec_ctx_t *opt_codec_ctx,
1169 asn_TYPE_descriptor_t *td, void **sptr,
Lev Walkin8c3b8542005-03-10 18:52:02 +00001170 const char *opt_mname, const void *buf_ptr, size_t size) {
Lev Walkindc06f6b2004-10-20 15:50:55 +00001171 return OCTET_STRING__decode_xer(opt_codec_ctx, td, sptr, opt_mname,
1172 buf_ptr, size, 0, OCTET_STRING__convert_hexadecimal);
1173}
1174
1175/*
1176 * Decode OCTET STRING from the binary (0/1) data.
1177 */
1178asn_dec_rval_t
1179OCTET_STRING_decode_xer_binary(asn_codec_ctx_t *opt_codec_ctx,
1180 asn_TYPE_descriptor_t *td, void **sptr,
Lev Walkin8c3b8542005-03-10 18:52:02 +00001181 const char *opt_mname, const void *buf_ptr, size_t size) {
Lev Walkindc06f6b2004-10-20 15:50:55 +00001182 return OCTET_STRING__decode_xer(opt_codec_ctx, td, sptr, opt_mname,
1183 buf_ptr, size, 0, OCTET_STRING__convert_binary);
1184}
1185
1186/*
1187 * Decode OCTET STRING from the string (ASCII/UTF-8) data.
1188 */
1189asn_dec_rval_t
1190OCTET_STRING_decode_xer_utf8(asn_codec_ctx_t *opt_codec_ctx,
1191 asn_TYPE_descriptor_t *td, void **sptr,
Lev Walkin8c3b8542005-03-10 18:52:02 +00001192 const char *opt_mname, const void *buf_ptr, size_t size) {
Lev Walkindc06f6b2004-10-20 15:50:55 +00001193 return OCTET_STRING__decode_xer(opt_codec_ctx, td, sptr, opt_mname,
1194 buf_ptr, size,
1195 OCTET_STRING__handle_control_chars,
1196 OCTET_STRING__convert_entrefs);
1197}
1198
Lev Walkin59b176e2005-11-26 11:25:14 +00001199asn_dec_rval_t
1200OCTET_STRING_decode_uper(asn_codec_ctx_t *opt_codec_ctx,
1201 asn_TYPE_descriptor_t *td, asn_per_constraints_t *constraints,
1202 void **sptr, asn_per_data_t *pd) {
1203
1204 asn_OCTET_STRING_specifics_t *specs = td->specifics
1205 ? (asn_OCTET_STRING_specifics_t *)td->specifics
1206 : &asn_DEF_OCTET_STRING_specs;
1207 asn_per_constraint_t *ct = constraints ? &constraints->size
1208 : (td->per_constraints
1209 ? &td->per_constraints->size
1210 : &asn_DEF_OCTET_STRING_constraint);
1211 asn_dec_rval_t rval = { RC_OK, 0 };
1212 BIT_STRING_t *st = (BIT_STRING_t *)*sptr;
1213 ssize_t consumed_myself = 0;
1214 int repeat;
1215 int unit_bits = (specs->subvariant != 1) * 7 + 1;
1216
1217 (void)opt_codec_ctx;
1218
1219 /*
1220 * Allocate the string.
1221 */
1222 if(!st) {
1223 st = (BIT_STRING_t *)(*sptr = CALLOC(1, specs->struct_size));
1224 if(!st) RETURN(RC_FAIL);
1225 }
1226
Lev Walkince676dd2005-12-20 22:34:55 +00001227 ASN_DEBUG("PER Decoding %s %ld .. %ld bits %d",
1228 ct->flags & APC_EXTENSIBLE ? "extensible" : "fixed",
1229 ct->lower_bound, ct->upper_bound, ct->effective_bits);
1230
Lev Walkin59b176e2005-11-26 11:25:14 +00001231 if(ct->flags & APC_EXTENSIBLE) {
1232 int inext = per_get_few_bits(pd, 1);
1233 if(inext < 0) RETURN(RC_FAIL);
1234 if(inext) ct = &asn_DEF_OCTET_STRING_constraint;
1235 consumed_myself = 0;
1236 }
1237
1238 if(ct->effective_bits >= 0
1239 && (!st->buf || st->size < ct->upper_bound)) {
1240 FREEMEM(st->buf);
1241 if(unit_bits == 1) {
1242 st->size = (ct->upper_bound + 7) >> 3;
1243 } else {
1244 st->size = ct->upper_bound;
1245 }
1246 st->buf = (uint8_t *)MALLOC(st->size + 1);
1247 if(!st->buf) { st->size = 0; RETURN(RC_FAIL); }
1248 }
1249
1250 /* X.691, #16.5: zero-length encoding */
1251 /* X.691, #16.6: short fixed length encoding (up to 2 octets) */
1252 /* X.691, #16.7: long fixed length encoding (up to 64K octets) */
1253 if(ct->effective_bits == 0) {
1254 int ret = per_get_many_bits(pd, st->buf, 0,
1255 unit_bits * ct->upper_bound);
1256 if(ret < 0) RETURN(RC_FAIL);
1257 consumed_myself += unit_bits * ct->upper_bound;
1258 st->buf[st->size] = 0;
1259 if(unit_bits == 1 && (ct->upper_bound & 0x7))
1260 st->bits_unused = 8 - (ct->upper_bound & 0x7);
1261 RETURN(RC_OK);
1262 }
1263
1264 st->size = 0;
1265 do {
1266 ssize_t len_bytes;
1267 ssize_t len_bits;
1268 void *p;
1269 int ret;
1270
1271 /* Get the PER length */
1272 len_bits = uper_get_length(pd, ct->effective_bits, &repeat);
1273 if(len_bits < 0) RETURN(RC_FAIL);
Lev Walkin1d5f0f22005-12-17 12:02:35 +00001274 len_bits += ct->lower_bound;
Lev Walkin59b176e2005-11-26 11:25:14 +00001275
Lev Walkin1d5f0f22005-12-17 12:02:35 +00001276 ASN_DEBUG("Got per length eb %ld, len %ld",
Lev Walkin1dc5b6f2005-12-21 10:08:27 +00001277 (long)ct->effective_bits, (long)len_bits);
Lev Walkin59b176e2005-11-26 11:25:14 +00001278 if(unit_bits == 1) {
1279 len_bytes = (len_bits + 7) >> 3;
1280 if(len_bits & 0x7)
1281 st->bits_unused = 8 - (len_bits & 0x7);
1282 /* len_bits be multiple of 16K if repeat is set */
1283 } else {
1284 len_bytes = len_bits;
1285 len_bits = len_bytes << 3;
1286 }
1287 p = REALLOC(st->buf, st->size + len_bytes + 1);
1288 if(!p) RETURN(RC_FAIL);
1289 st->buf = (uint8_t *)p;
1290
1291 ret = per_get_many_bits(pd, &st->buf[st->size], 0, len_bits);
1292 if(ret < 0) RETURN(RC_FAIL);
1293 st->size += len_bytes;
1294 } while(repeat);
1295 st->buf[st->size] = 0; /* nul-terminate */
1296
1297 return rval;
1298}
Lev Walkindc06f6b2004-10-20 15:50:55 +00001299
Lev Walkinf15320b2004-06-03 03:38:44 +00001300int
Lev Walkinde4825d2004-09-29 13:20:14 +00001301OCTET_STRING_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +00001302 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin4d9528c2004-08-11 08:10:13 +00001303 static const char *h2c = "0123456789ABCDEF";
Lev Walkinc2346572004-08-11 09:07:36 +00001304 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +00001305 char scratch[16 * 3 + 4];
1306 char *p = scratch;
1307 uint8_t *buf;
1308 uint8_t *end;
1309 size_t i;
Lev Walkinf15320b2004-06-03 03:38:44 +00001310
Lev Walkind9bd7752004-06-05 08:17:50 +00001311 (void)td; /* Unused argument */
1312
Lev Walkin8e8078a2004-09-26 13:10:40 +00001313 if(!st || !st->buf) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001314
1315 /*
1316 * Dump the contents of the buffer in hexadecimal.
1317 */
1318 buf = st->buf;
1319 end = buf + st->size;
1320 for(i = 0; buf < end; buf++, i++) {
1321 if(!(i % 16) && (i || st->size > 16)) {
Lev Walkin8e8078a2004-09-26 13:10:40 +00001322 if(cb(scratch, p - scratch, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +00001323 return -1;
Lev Walkin8e8078a2004-09-26 13:10:40 +00001324 _i_INDENT(1);
Lev Walkinf15320b2004-06-03 03:38:44 +00001325 p = scratch;
1326 }
1327 *p++ = h2c[(*buf >> 4) & 0x0F];
1328 *p++ = h2c[*buf & 0x0F];
Lev Walkina9cc46e2004-09-22 16:06:28 +00001329 *p++ = 0x20;
Lev Walkinf15320b2004-06-03 03:38:44 +00001330 }
1331
Lev Walkincc6a9102004-09-23 22:06:26 +00001332 if(p > scratch) {
1333 p--; /* Remove the tail space */
Lev Walkin8e8078a2004-09-26 13:10:40 +00001334 if(cb(scratch, p - scratch, app_key) < 0)
Lev Walkincc6a9102004-09-23 22:06:26 +00001335 return -1;
1336 }
1337
1338 return 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001339}
1340
1341int
Lev Walkindc06f6b2004-10-20 15:50:55 +00001342OCTET_STRING_print_utf8(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +00001343 int ilevel, asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +00001344 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +00001345
Lev Walkind9bd7752004-06-05 08:17:50 +00001346 (void)td; /* Unused argument */
1347 (void)ilevel; /* Unused argument */
1348
Lev Walkinf15320b2004-06-03 03:38:44 +00001349 if(st && st->buf) {
Lev Walkin8e8078a2004-09-26 13:10:40 +00001350 return (cb(st->buf, st->size, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001351 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +00001352 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001353 }
1354}
1355
1356void
Lev Walkinde4825d2004-09-29 13:20:14 +00001357OCTET_STRING_free(asn_TYPE_descriptor_t *td, void *sptr, int contents_only) {
Lev Walkinc2346572004-08-11 09:07:36 +00001358 OCTET_STRING_t *st = (OCTET_STRING_t *)sptr;
Lev Walkinbbd93252004-10-12 05:57:23 +00001359 asn_OCTET_STRING_specifics_t *specs = td->specifics
Lev Walkindc06f6b2004-10-20 15:50:55 +00001360 ? (asn_OCTET_STRING_specifics_t *)td->specifics
1361 : &asn_DEF_OCTET_STRING_specs;
Lev Walkinbbd93252004-10-12 05:57:23 +00001362 asn_struct_ctx_t *ctx = (asn_struct_ctx_t *)
1363 ((char *)st + specs->ctx_offset);
Lev Walkinb54577a2004-11-08 10:47:12 +00001364 struct _stack *stck;
Lev Walkinf15320b2004-06-03 03:38:44 +00001365
1366 if(!td || !st)
1367 return;
1368
1369 ASN_DEBUG("Freeing %s as OCTET STRING", td->name);
1370
1371 if(st->buf) {
1372 FREEMEM(st->buf);
1373 }
1374
1375 /*
1376 * Remove decode-time stack.
1377 */
Lev Walkinb54577a2004-11-08 10:47:12 +00001378 stck = (struct _stack *)ctx->ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +00001379 if(stck) {
1380 while(stck->tail) {
1381 struct _stack_el *sel = stck->tail;
1382 stck->tail = sel->prev;
1383 FREEMEM(sel);
1384 }
1385 FREEMEM(stck);
1386 }
1387
1388 if(!contents_only) {
1389 FREEMEM(st);
1390 }
1391}
1392
1393/*
1394 * Conversion routines.
1395 */
1396int
1397OCTET_STRING_fromBuf(OCTET_STRING_t *st, const char *str, int len) {
1398 void *buf;
1399
1400 if(st == 0 || (str == 0 && len)) {
1401 errno = EINVAL;
1402 return -1;
1403 }
1404
1405 /*
1406 * Clear the OCTET STRING.
1407 */
1408 if(str == NULL) {
Lev Walkin7b284812005-12-17 11:43:25 +00001409 FREEMEM(st->buf);
1410 st->buf = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001411 st->size = 0;
1412 return 0;
1413 }
1414
1415 /* Determine the original string size, if not explicitly given */
1416 if(len < 0)
1417 len = strlen(str);
1418
1419 /* Allocate and fill the memory */
1420 buf = MALLOC(len + 1);
Lev Walkin7b284812005-12-17 11:43:25 +00001421 if(buf == NULL)
Lev Walkinf15320b2004-06-03 03:38:44 +00001422 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +00001423
1424 memcpy(buf, str, len);
Lev Walkin7b284812005-12-17 11:43:25 +00001425 ((uint8_t *)buf)[len] = '\0'; /* Couldn't use memcpy(len+1)! */
1426 FREEMEM(st->buf);
1427 st->buf = (uint8_t *)buf;
1428 st->size = len;
Lev Walkinf15320b2004-06-03 03:38:44 +00001429
1430 return 0;
1431}
1432
1433OCTET_STRING_t *
Lev Walkinbbd93252004-10-12 05:57:23 +00001434OCTET_STRING_new_fromBuf(asn_TYPE_descriptor_t *td, const char *str, int len) {
1435 asn_OCTET_STRING_specifics_t *specs = td->specifics
Lev Walkindc06f6b2004-10-20 15:50:55 +00001436 ? (asn_OCTET_STRING_specifics_t *)td->specifics
1437 : &asn_DEF_OCTET_STRING_specs;
Lev Walkinf15320b2004-06-03 03:38:44 +00001438 OCTET_STRING_t *st;
1439
Lev Walkinbbd93252004-10-12 05:57:23 +00001440 st = (OCTET_STRING_t *)CALLOC(1, specs->struct_size);
Lev Walkinf15320b2004-06-03 03:38:44 +00001441 if(st && str && OCTET_STRING_fromBuf(st, str, len)) {
1442 free(st);
1443 st = NULL;
1444 }
1445
1446 return st;
1447}
1448