blob: e3b7a0aba97f7a97bafb016d23bf0b181b642861 [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001/*-
Lev Walkin188ed2c2004-09-13 08:31:01 +00002 * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
Lev Walkinf15320b2004-06-03 03:38:44 +00003 * Redistribution and modifications are permitted subject to BSD license.
4 */
Lev Walkina9cc46e2004-09-22 16:06:28 +00005#include <asn_internal.h>
Lev Walkinf15320b2004-06-03 03:38:44 +00006#include <OCTET_STRING.h>
7#include <assert.h>
8#include <errno.h>
9
10/*
11 * OCTET STRING basic type description.
12 */
Lev Walkinde4825d2004-09-29 13:20:14 +000013static ber_tlv_tag_t asn_DEF_OCTET_STRING_tags[] = {
Lev Walkinf15320b2004-06-03 03:38:44 +000014 (ASN_TAG_CLASS_UNIVERSAL | (4 << 2))
15};
Lev Walkinde4825d2004-09-29 13:20:14 +000016asn_TYPE_descriptor_t asn_DEF_OCTET_STRING = {
Lev Walkinf15320b2004-06-03 03:38:44 +000017 "OCTET STRING",
Lev Walkina9cc46e2004-09-22 16:06:28 +000018 OCTET_STRING_free,
19 OCTET_STRING_print, /* non-ascii stuff, generally */
Lev Walkinf15320b2004-06-03 03:38:44 +000020 asn_generic_no_constraint,
21 OCTET_STRING_decode_ber,
22 OCTET_STRING_encode_der,
Lev Walkina9cc46e2004-09-22 16:06:28 +000023 0, /* Not implemented yet */
24 OCTET_STRING_encode_xer,
Lev Walkinf15320b2004-06-03 03:38:44 +000025 0, /* Use generic outmost tag fetcher */
Lev Walkinde4825d2004-09-29 13:20:14 +000026 asn_DEF_OCTET_STRING_tags,
27 sizeof(asn_DEF_OCTET_STRING_tags)
28 / sizeof(asn_DEF_OCTET_STRING_tags[0]),
29 asn_DEF_OCTET_STRING_tags, /* Same as above */
30 sizeof(asn_DEF_OCTET_STRING_tags)
31 / sizeof(asn_DEF_OCTET_STRING_tags[0]),
Lev Walkin449f8322004-08-20 13:23:42 +000032 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +000033 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +000034};
35
Lev Walkincc6a9102004-09-23 22:06:26 +000036#undef _CH_PHASE
37#undef NEXT_PHASE
38#undef PREV_PHASE
Lev Walkinf15320b2004-06-03 03:38:44 +000039#define _CH_PHASE(ctx, inc) do { \
40 if(ctx->phase == 0) \
41 ctx->step = 0; \
42 ctx->phase += inc; \
43 } while(0)
44#define NEXT_PHASE(ctx) _CH_PHASE(ctx, +1)
45#define PREV_PHASE(ctx) _CH_PHASE(ctx, -1)
46
Lev Walkincc6a9102004-09-23 22:06:26 +000047#undef ADVANCE
Lev Walkin4ce78ca2004-08-25 01:34:11 +000048#define ADVANCE(num_bytes) do { \
Lev Walkin3990ba62004-09-24 20:57:41 +000049 size_t num = (num_bytes); \
Lev Walkin4ce78ca2004-08-25 01:34:11 +000050 buf_ptr = ((char *)buf_ptr) + num; \
51 size -= num; \
52 consumed_myself += num; \
Lev Walkinf15320b2004-06-03 03:38:44 +000053 } while(0)
54
Lev Walkincc6a9102004-09-23 22:06:26 +000055#undef RETURN
Lev Walkinf15320b2004-06-03 03:38:44 +000056#define RETURN(_code) do { \
57 rval.code = _code; \
58 rval.consumed = consumed_myself;\
59 return rval; \
60 } while(0)
61
Lev Walkincc6a9102004-09-23 22:06:26 +000062#undef APPEND
Lev Walkind9bd7752004-06-05 08:17:50 +000063#define APPEND(bufptr, bufsize) do { \
Lev Walkin8d127872004-09-04 04:44:50 +000064 size_t _bs = (bufsize); \
Lev Walkind9bd7752004-06-05 08:17:50 +000065 size_t _ns = ctx->step; /* Allocated */ \
Lev Walkin8d127872004-09-04 04:44:50 +000066 if(_ns <= (size_t)(st->size + _bs)) { \
Lev Walkind9bd7752004-06-05 08:17:50 +000067 void *ptr; \
Lev Walkin188ed2c2004-09-13 08:31:01 +000068 /* Be nice and round to the memory allocator */ \
Lev Walkind9bd7752004-06-05 08:17:50 +000069 do { _ns = _ns ? _ns<<2 : 16; } \
Lev Walkin8d127872004-09-04 04:44:50 +000070 while(_ns <= (size_t)(st->size + _bs)); \
Lev Walkind9bd7752004-06-05 08:17:50 +000071 ptr = REALLOC(st->buf, _ns); \
72 if(ptr) { \
Lev Walkinc2346572004-08-11 09:07:36 +000073 st->buf = (uint8_t *)ptr; \
Lev Walkind9bd7752004-06-05 08:17:50 +000074 ctx->step = _ns; \
75 } else { \
76 RETURN(RC_FAIL); \
77 } \
78 } \
Lev Walkin8d127872004-09-04 04:44:50 +000079 memcpy(st->buf + st->size, bufptr, _bs); \
80 st->size += _bs; \
Lev Walkind9bd7752004-06-05 08:17:50 +000081 if(st->size < 0) \
82 /* Why even care?.. JIC */ \
83 RETURN(RC_FAIL); \
84 /* Convenient nul-termination */ \
85 st->buf[st->size] = '\0'; \
Lev Walkinf15320b2004-06-03 03:38:44 +000086 } while(0)
87
88/*
89 * The main reason why ASN.1 is still alive is that too much time and effort
90 * is necessary for learning it more or less adequately, thus creating a gut
91 * necessity to demonstrate that aquired skill everywhere afterwards.
92 * No, I am not going to explain what the following stuff is.
93 */
94struct _stack_el {
Lev Walkin3990ba62004-09-24 20:57:41 +000095 ber_tlv_len_t left; /* What's left to read (or -1) */
Lev Walkin5c915992004-09-27 20:54:06 +000096 ber_tlv_len_t got; /* What was actually processed */
Lev Walkin188ed2c2004-09-13 08:31:01 +000097 int cont_level; /* Depth of subcontainment */
Lev Walkinf15320b2004-06-03 03:38:44 +000098 int want_nulls; /* Want null "end of content" octets? */
99 int bits_chopped; /* Flag in BIT STRING mode */
Lev Walkin5c915992004-09-27 20:54:06 +0000100 ber_tlv_tag_t tag; /* For debugging purposes */
Lev Walkinf15320b2004-06-03 03:38:44 +0000101 struct _stack_el *prev;
102 struct _stack_el *next;
103};
104struct _stack {
105 struct _stack_el *tail;
106 struct _stack_el *cur_ptr;
107};
108
109static struct _stack_el *
Lev Walkin3990ba62004-09-24 20:57:41 +0000110OS__add_stack_el(struct _stack *st) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000111 struct _stack_el *nel;
112
Lev Walkin188ed2c2004-09-13 08:31:01 +0000113 /*
114 * Reuse the old stack frame or allocate a new one.
115 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000116 if(st->cur_ptr && st->cur_ptr->next) {
117 nel = st->cur_ptr->next;
Lev Walkinf15320b2004-06-03 03:38:44 +0000118 nel->bits_chopped = 0;
Lev Walkin5c915992004-09-27 20:54:06 +0000119 nel->got = 0;
120 /* Retain the nel->cont_level, it's correct. */
Lev Walkinf15320b2004-06-03 03:38:44 +0000121 } else {
Lev Walkinc2346572004-08-11 09:07:36 +0000122 (void *)nel = CALLOC(1, sizeof(struct _stack_el));
Lev Walkinf15320b2004-06-03 03:38:44 +0000123 if(nel == NULL)
124 return NULL;
125
126 if(st->tail) {
Lev Walkin188ed2c2004-09-13 08:31:01 +0000127 /* Increase a subcontainment depth */
128 nel->cont_level = st->tail->cont_level + 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000129 st->tail->next = nel;
130 }
131 nel->prev = st->tail;
132 st->tail = nel;
133 }
134
135 st->cur_ptr = nel;
136
137 return nel;
138}
139
140static struct _stack *
141_new_stack() {
142 struct _stack *st;
Lev Walkinc2346572004-08-11 09:07:36 +0000143 (void *)st = CALLOC(1, sizeof(struct _stack));
Lev Walkinf15320b2004-06-03 03:38:44 +0000144 if(st == NULL)
145 return NULL;
146
Lev Walkinf15320b2004-06-03 03:38:44 +0000147 return st;
148}
149
150/*
151 * Decode OCTET STRING type.
152 */
153ber_dec_rval_t
Lev Walkinde4825d2004-09-29 13:20:14 +0000154OCTET_STRING_decode_ber(asn_codec_ctx_t *opt_codec_ctx,
155 asn_TYPE_descriptor_t *td,
Lev Walkinf15320b2004-06-03 03:38:44 +0000156 void **os_structure, void *buf_ptr, size_t size, int tag_mode) {
Lev Walkinc2346572004-08-11 09:07:36 +0000157 OCTET_STRING_t *st = (OCTET_STRING_t *)*os_structure;
Lev Walkinf15320b2004-06-03 03:38:44 +0000158 ber_dec_rval_t rval;
Lev Walkinde4825d2004-09-29 13:20:14 +0000159 asn_struct_ctx_t *ctx;
Lev Walkinf15320b2004-06-03 03:38:44 +0000160 ssize_t consumed_myself = 0;
161 struct _stack *stck; /* A stack structure */
Lev Walkin5c915992004-09-27 20:54:06 +0000162 struct _stack_el *sel = 0; /* Stack element */
Lev Walkinf15320b2004-06-03 03:38:44 +0000163 int tlv_constr;
Lev Walkin188ed2c2004-09-13 08:31:01 +0000164 enum type_type_e {
165 _TT_GENERIC = 0, /* Just a random OCTET STRING */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000166 _TT_BIT_STRING = 1, /* BIT STRING type, a special case */
167 _TT_ANY = 2, /* ANY type, a special case too */
168 } type_type = (enum type_type_e)(int)td->specifics;
Lev Walkinf15320b2004-06-03 03:38:44 +0000169
Lev Walkincc6a9102004-09-23 22:06:26 +0000170 ASN_DEBUG("Decoding %s as %s (frame %ld)",
171 td->name,
172 (type_type == _TT_GENERIC) ? "OCTET STRING" : "OS-SpecialCase",
173 (long)size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000174
175 /*
176 * Create the string if does not exist.
177 */
178 if(st == NULL) {
Lev Walkinc2346572004-08-11 09:07:36 +0000179 (void *)st = *os_structure = CALLOC(1, sizeof(*st));
Lev Walkinf15320b2004-06-03 03:38:44 +0000180 if(st == NULL)
181 RETURN(RC_FAIL);
182 }
183
184 /* Restore parsing context */
Lev Walkinde4825d2004-09-29 13:20:14 +0000185 ctx = &st->_asn_ctx;
Lev Walkinf15320b2004-06-03 03:38:44 +0000186
187 switch(ctx->phase) {
188 case 0:
189 /*
190 * Check tags.
191 */
Lev Walkinde4825d2004-09-29 13:20:14 +0000192 rval = ber_check_tags(opt_codec_ctx, td, ctx,
Lev Walkin8e8078a2004-09-26 13:10:40 +0000193 buf_ptr, size, tag_mode, -1,
Lev Walkinf15320b2004-06-03 03:38:44 +0000194 &ctx->left, &tlv_constr);
195 if(rval.code != RC_OK) {
196 RETURN(rval.code);
197 }
198
Lev Walkinf15320b2004-06-03 03:38:44 +0000199 if(tlv_constr) {
200 /*
201 * Complex operation, requires stack of expectations.
202 */
203 ctx->ptr = _new_stack();
204 if(ctx->ptr) {
Lev Walkinc2346572004-08-11 09:07:36 +0000205 (void *)stck = ctx->ptr;
Lev Walkin188ed2c2004-09-13 08:31:01 +0000206 if(type_type == _TT_BIT_STRING) {
Lev Walkin8d127872004-09-04 04:44:50 +0000207 /* Number of meaningless tail bits */
Lev Walkinf15320b2004-06-03 03:38:44 +0000208 APPEND("\0", 1);
209 }
210 } else {
211 RETURN(RC_FAIL);
212 }
213 } else {
214 /*
215 * Jump into stackless primitive decoding.
216 */
217 _CH_PHASE(ctx, 3);
Lev Walkin188ed2c2004-09-13 08:31:01 +0000218 if(type_type == _TT_ANY)
219 APPEND(buf_ptr, rval.consumed);
Lev Walkinf15320b2004-06-03 03:38:44 +0000220 ADVANCE(rval.consumed);
221 goto phase3;
222 }
223
Lev Walkinf15320b2004-06-03 03:38:44 +0000224 NEXT_PHASE(ctx);
225 /* Fall through */
226 case 1:
227 phase1:
228 /*
229 * Fill the stack with expectations.
230 */
Lev Walkinc2346572004-08-11 09:07:36 +0000231 (void *)stck = ctx->ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000232 sel = stck->cur_ptr;
233 do {
234 ber_tlv_tag_t tlv_tag;
235 ber_tlv_len_t tlv_len;
Lev Walkin188ed2c2004-09-13 08:31:01 +0000236 ber_tlv_tag_t expected_tag;
Lev Walkin5c915992004-09-27 20:54:06 +0000237 ssize_t tl, ll, tlvl;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000238 /* This one works even if (sel->left == -1) */
239 ssize_t Left = ((!sel||(size_t)sel->left >= size)
Lev Walkin5c915992004-09-27 20:54:06 +0000240 ?(ssize_t)size:sel->left);
Lev Walkinf15320b2004-06-03 03:38:44 +0000241
Lev Walkin3990ba62004-09-24 20:57:41 +0000242
Lev Walkin5c915992004-09-27 20:54:06 +0000243 ASN_DEBUG("%p, s->l=%d, s->wn=%d, s->g=%d\n", sel,
244 sel?sel->left:0,
245 sel?sel->want_nulls:0,
246 sel?sel->got:0
247 );
248 if(sel && sel->left <= 0 && sel->want_nulls == 0) {
249 if(sel->prev) {
250 struct _stack_el *prev = sel->prev;
251 if(prev->left != -1) {
252 if(prev->left < sel->got)
253 RETURN(RC_FAIL);
254 prev->left -= sel->got;
255 }
256 prev->got += sel->got;
257 sel = stck->cur_ptr = prev;
258 if(!sel) break;
259 tlv_constr = 1;
260 continue;
261 } else {
262 sel = stck->cur_ptr = 0;
263 break; /* Nothing to wait */
264 }
265 }
266
Lev Walkin3990ba62004-09-24 20:57:41 +0000267 tl = ber_fetch_tag(buf_ptr, Left, &tlv_tag);
Lev Walkin5c915992004-09-27 20:54:06 +0000268 ASN_DEBUG("fetch tag(size=%d,L=%d), %sstack, left=%d, wn=%d, tl=%d",
269 (int)size, Left, sel?"":"!",
270 sel?sel->left:0, sel?sel->want_nulls:0, tl);
Lev Walkinf15320b2004-06-03 03:38:44 +0000271 switch(tl) {
272 case -1: RETURN(RC_FAIL);
273 case 0: RETURN(RC_WMORE);
274 }
275
276 tlv_constr = BER_TLV_CONSTRUCTED(buf_ptr);
277
278 ll = ber_fetch_length(tlv_constr,
Lev Walkin3990ba62004-09-24 20:57:41 +0000279 (char *)buf_ptr + tl, Left - tl, &tlv_len);
Lev Walkin5c915992004-09-27 20:54:06 +0000280 ASN_DEBUG("Got tag=%s, tc=%d, left=%ld, tl=%d, len=%d, ll=%d",
Lev Walkin8d127872004-09-04 04:44:50 +0000281 ber_tlv_tag_string(tlv_tag), tlv_constr,
Lev Walkin5c915992004-09-27 20:54:06 +0000282 (long)Left, tl, tlv_len, ll);
Lev Walkinf15320b2004-06-03 03:38:44 +0000283 switch(ll) {
284 case -1: RETURN(RC_FAIL);
285 case 0: RETURN(RC_WMORE);
286 }
287
Lev Walkin8d127872004-09-04 04:44:50 +0000288 if(sel && sel->want_nulls
Lev Walkinf15320b2004-06-03 03:38:44 +0000289 && ((uint8_t *)buf_ptr)[0] == 0
290 && ((uint8_t *)buf_ptr)[1] == 0)
291 {
Lev Walkin8d127872004-09-04 04:44:50 +0000292
293 ASN_DEBUG("Eat EOC; wn=%d--", sel->want_nulls);
294
Lev Walkin5c915992004-09-27 20:54:06 +0000295 if(type_type == _TT_ANY) APPEND("\0\0", 2);
296
297 ADVANCE(2);
298 sel->got += 2;
299 if(sel->left != -1) {
300 sel->left -= 2; /* assert(sel->left >= 2) */
301 }
302
Lev Walkinf15320b2004-06-03 03:38:44 +0000303 sel->want_nulls--;
304 if(sel->want_nulls == 0) {
305 /* Move to the next expectation */
Lev Walkin5c915992004-09-27 20:54:06 +0000306 sel->left = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000307 tlv_constr = 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000308 }
Lev Walkin8d127872004-09-04 04:44:50 +0000309
310 continue;
Lev Walkin188ed2c2004-09-13 08:31:01 +0000311 }
312
313 /*
314 * Set up expected tags,
315 * depending on ASN.1 type being decoded.
316 */
317 switch(type_type) {
318 case _TT_BIT_STRING:
319 /* X.690: 8.6.4.1, NOTE 2 */
320 /* Fall through */
321 case _TT_GENERIC:
322 default:
323 if(sel) {
324 int level = sel->cont_level;
325 if(level < td->all_tags_count) {
326 expected_tag = td->all_tags[level];
327 break;
328 } else if(td->all_tags_count) {
329 expected_tag = td->all_tags
330 [td->all_tags_count - 1];
331 break;
332 }
333 /* else, Fall through */
334 }
335 /* Fall through */
336 case _TT_ANY:
337 expected_tag = tlv_tag;
338 break;
339 }
340
341
342 if(tlv_tag != expected_tag) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000343 char buf[2][32];
344 ber_tlv_tag_snprint(tlv_tag,
345 buf[0], sizeof(buf[0]));
346 ber_tlv_tag_snprint(td->tags[td->tags_count-1],
347 buf[1], sizeof(buf[1]));
348 ASN_DEBUG("Tag does not match expectation: %s != %s",
349 buf[0], buf[1]);
350 RETURN(RC_FAIL);
351 }
352
Lev Walkinde4825d2004-09-29 13:20:14 +0000353 tlvl = tl + ll; /* Combined length of T and L encoding */
354 if((tlv_len + tlvl) < 0) {
355 /* tlv_len value is too big */
356 ASN_DEBUG("TLV encoding + length (%ld) is too big",
357 (long)tlv_len);
358 RETURN(RC_FAIL);
359 }
360
Lev Walkinf15320b2004-06-03 03:38:44 +0000361 /*
362 * Append a new expectation.
363 */
Lev Walkin3990ba62004-09-24 20:57:41 +0000364 sel = OS__add_stack_el(stck);
Lev Walkin5c915992004-09-27 20:54:06 +0000365 if(!sel) RETURN(RC_FAIL);
Lev Walkinf15320b2004-06-03 03:38:44 +0000366
Lev Walkin5c915992004-09-27 20:54:06 +0000367 sel->tag = tlv_tag;
368
369 sel->want_nulls = (tlv_len==-1);
370 if(sel->prev && sel->prev->left != -1) {
371 /* Check that the parent frame is big enough */
372 if(sel->prev->left < tlvl + (tlv_len==-1?0:tlv_len))
373 RETURN(RC_FAIL);
374 if(tlv_len == -1)
375 sel->left = sel->prev->left - tlvl;
376 else
377 sel->left = tlv_len;
378 } else {
379 sel->left = tlv_len;
380 }
381 if(type_type == _TT_ANY) APPEND(buf_ptr, tlvl);
382 sel->got += tlvl;
383 ADVANCE(tlvl);
384
385 ASN_DEBUG("+EXPECT2 got=%d left=%d, wn=%d, clvl=%d",
386 sel->got, sel->left, sel->want_nulls, sel->cont_level);
387
Lev Walkinf15320b2004-06-03 03:38:44 +0000388 } while(tlv_constr);
389 if(sel == NULL) {
390 /* Finished operation, "phase out" */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000391 ASN_DEBUG("Phase out");
Lev Walkinf15320b2004-06-03 03:38:44 +0000392 _CH_PHASE(ctx, +3);
393 break;
394 }
395
396 NEXT_PHASE(ctx);
397 /* Fall through */
398 case 2:
Lev Walkinc2346572004-08-11 09:07:36 +0000399 (void *)stck = ctx->ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000400 sel = stck->cur_ptr;
Lev Walkin5c915992004-09-27 20:54:06 +0000401 ASN_DEBUG("Phase 2: Need %ld bytes, size=%ld, alrg=%ld, wn=%d",
402 (long)sel->left, (long)size, (long)sel->got,
403 sel->want_nulls);
Lev Walkinf15320b2004-06-03 03:38:44 +0000404 {
405 ber_tlv_len_t len;
406
407 assert(sel->left >= 0);
408
Lev Walkinec1ffd42004-08-18 04:53:32 +0000409 len = ((ber_tlv_len_t)size < sel->left)
410 ? (ber_tlv_len_t)size : sel->left;
Lev Walkinf15320b2004-06-03 03:38:44 +0000411 if(len > 0) {
Lev Walkin188ed2c2004-09-13 08:31:01 +0000412 if(type_type == _TT_BIT_STRING
413 && sel->bits_chopped == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000414 /*
415 * Finalize the previous chunk:
416 * strip down unused bits.
417 */
418 st->buf[st->size-1] &= 0xff << st->buf[0];
419
Lev Walkin4d9528c2004-08-11 08:10:13 +0000420 APPEND(((char *)buf_ptr+1), (len - 1));
Lev Walkinf15320b2004-06-03 03:38:44 +0000421 st->buf[0] = *(uint8_t *)buf_ptr;
422 sel->bits_chopped = 1;
423 } else {
424 APPEND(buf_ptr, len);
425 }
426 ADVANCE(len);
427 sel->left -= len;
Lev Walkin5c915992004-09-27 20:54:06 +0000428 sel->got += len;
Lev Walkinf15320b2004-06-03 03:38:44 +0000429 }
430
Lev Walkin5c915992004-09-27 20:54:06 +0000431 if(sel->left) {
432 ASN_DEBUG("OS left %ld, size = %ld, wn=%d\n",
433 (long)sel->left, (long)size, sel->want_nulls);
Lev Walkinf15320b2004-06-03 03:38:44 +0000434 RETURN(RC_WMORE);
Lev Walkinf15320b2004-06-03 03:38:44 +0000435 }
Lev Walkin5c915992004-09-27 20:54:06 +0000436
437 PREV_PHASE(ctx);
438 goto phase1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000439 }
440 break;
441 case 3:
442 phase3:
443 /*
444 * Primitive form, no stack required.
445 */
Lev Walkind9bd7752004-06-05 08:17:50 +0000446 if(size < (size_t)ctx->left) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000447 APPEND(buf_ptr, size);
448 ctx->left -= size;
449 ADVANCE(size);
450 RETURN(RC_WMORE);
451 } else {
452 APPEND(buf_ptr, ctx->left);
453 ADVANCE(ctx->left);
454 ctx->left = 0;
455
456 NEXT_PHASE(ctx);
457 }
458 break;
459 }
460
Lev Walkin5c915992004-09-27 20:54:06 +0000461 if(sel) {
462 ASN_DEBUG("3sel p=%p, wn=%d, l=%ld, g=%ld, size=%ld",
463 sel->prev, sel->want_nulls,
464 (long)sel->left, (long)sel->got, (long)size);
465 if(sel->prev || sel->want_nulls > 1 || sel->left > 0) {
466 RETURN(RC_WMORE);
467 }
468 }
469
Lev Walkinf15320b2004-06-03 03:38:44 +0000470 /*
471 * BIT STRING-specific processing.
472 */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000473 if(type_type == _TT_BIT_STRING && st->size >= 2) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000474 /* Finalize BIT STRING: zero out unused bits. */
475 st->buf[st->size-1] &= 0xff << st->buf[0];
476 }
477
478 ASN_DEBUG("Took %d bytes to encode %s: [%s]:%d",
Lev Walkin3990ba62004-09-24 20:57:41 +0000479 consumed_myself, td->name,
480 (type_type == _TT_GENERIC) ? (char *)st->buf : "", st->size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000481
Lev Walkinf15320b2004-06-03 03:38:44 +0000482
Lev Walkin5c915992004-09-27 20:54:06 +0000483 RETURN(RC_OK);
Lev Walkinf15320b2004-06-03 03:38:44 +0000484}
485
486/*
487 * Encode OCTET STRING type using DER.
488 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000489asn_enc_rval_t
Lev Walkinde4825d2004-09-29 13:20:14 +0000490OCTET_STRING_encode_der(asn_TYPE_descriptor_t *td, void *ptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000491 int tag_mode, ber_tlv_tag_t tag,
492 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkina9cc46e2004-09-22 16:06:28 +0000493 asn_enc_rval_t erval;
Lev Walkinc2346572004-08-11 09:07:36 +0000494 OCTET_STRING_t *st = (OCTET_STRING_t *)ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000495 int add_byte = 0;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000496 int is_bit_str = (td->specifics == (void *)1);
497 int is_ANY_type = (td->specifics == (void *)2);
Lev Walkinf15320b2004-06-03 03:38:44 +0000498
499 ASN_DEBUG("%s %s as OCTET STRING",
Lev Walkin1f670c12004-09-02 12:57:25 +0000500 cb?"Estimating":"Encoding", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000501
502 /*
503 * Canonicalize BIT STRING.
504 */
Lev Walkina737f3b2004-09-02 12:11:47 +0000505 if(is_bit_str && st->buf) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000506 switch(st->size) {
507 case 0: add_byte = 1; break;
508 case 1: st->buf[0] = 0; break;
509 default:
510 /* Finalize BIT STRING: zero out unused bits. */
511 st->buf[st->size-1] &= 0xff << st->buf[0];
512 }
513 }
514
Lev Walkina737f3b2004-09-02 12:11:47 +0000515 if(is_ANY_type) {
516 erval.encoded = 0;
517 } else {
Lev Walkin1f670c12004-09-02 12:57:25 +0000518 erval.encoded = der_write_tags(td, st->size + add_byte,
Lev Walkin8e8078a2004-09-26 13:10:40 +0000519 tag_mode, 0, tag, cb, app_key);
Lev Walkina737f3b2004-09-02 12:11:47 +0000520 if(erval.encoded == -1) {
Lev Walkin1f670c12004-09-02 12:57:25 +0000521 erval.failed_type = td;
Lev Walkina737f3b2004-09-02 12:11:47 +0000522 erval.structure_ptr = ptr;
523 return erval;
524 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000525 }
526
527 if(cb) {
528 uint8_t zero;
529 uint8_t *buf;
530 int size;
Lev Walkinf15320b2004-06-03 03:38:44 +0000531
532 /* BIT STRING-aware handling */
533 if(add_byte) {
534 zero = 0;
535 buf = &zero;
536 size = 1;
537 } else if(st->buf) {
538 buf = st->buf;
539 size = st->size;
540 } else {
541 assert(st->size == 0);
542 buf = 0; /* Not used */
543 size = 0;
544 }
545
546 if(size) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000547 if(cb(buf, size, app_key) < 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000548 erval.encoded = -1;
Lev Walkin1f670c12004-09-02 12:57:25 +0000549 erval.failed_type = td;
Lev Walkinf15320b2004-06-03 03:38:44 +0000550 erval.structure_ptr = ptr;
551 return erval;
552 }
553 }
554 }
555
556 erval.encoded += st->size + add_byte;
557
558 return erval;
559}
560
Lev Walkina9cc46e2004-09-22 16:06:28 +0000561asn_enc_rval_t
Lev Walkinde4825d2004-09-29 13:20:14 +0000562OCTET_STRING_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000563 int ilevel, enum xer_encoder_flags_e flags,
564 asn_app_consume_bytes_f *cb, void *app_key) {
565 static const char *h2c = "0123456789ABCDEF";
566 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
567 asn_enc_rval_t er;
568 char scratch[16 * 3 + 4];
569 char *p = scratch;
570 uint8_t *buf;
571 uint8_t *end;
572 size_t i;
573
Lev Walkin942fd082004-10-03 09:13:02 +0000574 if(!st || !st->buf)
575 _ASN_ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000576
577 er.encoded = 0;
578
579 /*
580 * Dump the contents of the buffer in hexadecimal.
581 */
582 buf = st->buf;
583 end = buf + st->size;
584 if(flags & XER_F_CANONICAL) {
585 char *scend = scratch + (sizeof(scratch) - 2);
586 for(; buf < end; buf++) {
587 if(p >= scend) {
588 _ASN_CALLBACK(scratch, p - scratch);
589 er.encoded += p - scratch;
590 p = scratch;
591 }
592 *p++ = h2c[(*buf >> 4) & 0x0F];
593 *p++ = h2c[*buf & 0x0F];
594 }
Lev Walkincc6a9102004-09-23 22:06:26 +0000595
596 _ASN_CALLBACK(scratch, p-scratch); /* Dump the rest */
597 er.encoded += p - scratch;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000598 } else {
599 for(i = 0; buf < end; buf++, i++) {
600 if(!(i % 16) && (i || st->size > 16)) {
601 _ASN_CALLBACK(scratch, p-scratch);
602 er.encoded += (p-scratch);
603 p = scratch;
604 _i_ASN_TEXT_INDENT(1, ilevel);
605 }
606 *p++ = h2c[(*buf >> 4) & 0x0F];
607 *p++ = h2c[*buf & 0x0F];
608 *p++ = 0x20;
609 }
Lev Walkincc6a9102004-09-23 22:06:26 +0000610 if(p - scratch) {
611 p--; /* Remove the tail space */
612 _ASN_CALLBACK(scratch, p-scratch); /* Dump the rest */
613 er.encoded += p - scratch;
614 if(st->size > 16)
615 _i_ASN_TEXT_INDENT(1, ilevel-1);
616 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000617 }
618
Lev Walkina9cc46e2004-09-22 16:06:28 +0000619 return er;
Lev Walkin942fd082004-10-03 09:13:02 +0000620cb_failed:
621 _ASN_ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000622}
623
624asn_enc_rval_t
Lev Walkinde4825d2004-09-29 13:20:14 +0000625OCTET_STRING_encode_xer_ascii(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000626 int ilevel, enum xer_encoder_flags_e flags,
627 asn_app_consume_bytes_f *cb, void *app_key) {
628 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
629 asn_enc_rval_t er;
630
631 (void)ilevel; /* Unused argument */
632 (void)flags; /* Unused argument */
633
634 if(!st || !st->buf)
635 _ASN_ENCODE_FAILED;
636
Lev Walkin942fd082004-10-03 09:13:02 +0000637 if(cb(st->buf, st->size, app_key) < 0)
638 _ASN_ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000639 er.encoded = st->size;
640
641 return er;
642}
643
Lev Walkinf15320b2004-06-03 03:38:44 +0000644int
Lev Walkinde4825d2004-09-29 13:20:14 +0000645OCTET_STRING_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +0000646 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin4d9528c2004-08-11 08:10:13 +0000647 static const char *h2c = "0123456789ABCDEF";
Lev Walkinc2346572004-08-11 09:07:36 +0000648 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000649 char scratch[16 * 3 + 4];
650 char *p = scratch;
651 uint8_t *buf;
652 uint8_t *end;
653 size_t i;
Lev Walkinf15320b2004-06-03 03:38:44 +0000654
Lev Walkind9bd7752004-06-05 08:17:50 +0000655 (void)td; /* Unused argument */
656
Lev Walkin8e8078a2004-09-26 13:10:40 +0000657 if(!st || !st->buf) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000658
659 /*
660 * Dump the contents of the buffer in hexadecimal.
661 */
662 buf = st->buf;
663 end = buf + st->size;
664 for(i = 0; buf < end; buf++, i++) {
665 if(!(i % 16) && (i || st->size > 16)) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000666 if(cb(scratch, p - scratch, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000667 return -1;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000668 _i_INDENT(1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000669 p = scratch;
670 }
671 *p++ = h2c[(*buf >> 4) & 0x0F];
672 *p++ = h2c[*buf & 0x0F];
Lev Walkina9cc46e2004-09-22 16:06:28 +0000673 *p++ = 0x20;
Lev Walkinf15320b2004-06-03 03:38:44 +0000674 }
675
Lev Walkincc6a9102004-09-23 22:06:26 +0000676 if(p > scratch) {
677 p--; /* Remove the tail space */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000678 if(cb(scratch, p - scratch, app_key) < 0)
Lev Walkincc6a9102004-09-23 22:06:26 +0000679 return -1;
680 }
681
682 return 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000683}
684
685int
Lev Walkinde4825d2004-09-29 13:20:14 +0000686OCTET_STRING_print_ascii(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000687 int ilevel, asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +0000688 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000689
Lev Walkind9bd7752004-06-05 08:17:50 +0000690 (void)td; /* Unused argument */
691 (void)ilevel; /* Unused argument */
692
Lev Walkinf15320b2004-06-03 03:38:44 +0000693 if(st && st->buf) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000694 return (cb(st->buf, st->size, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000695 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000696 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000697 }
698}
699
700void
Lev Walkinde4825d2004-09-29 13:20:14 +0000701OCTET_STRING_free(asn_TYPE_descriptor_t *td, void *sptr, int contents_only) {
Lev Walkinc2346572004-08-11 09:07:36 +0000702 OCTET_STRING_t *st = (OCTET_STRING_t *)sptr;
Lev Walkinde4825d2004-09-29 13:20:14 +0000703 struct _stack *stck = (struct _stack *)st->_asn_ctx.ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000704
705 if(!td || !st)
706 return;
707
708 ASN_DEBUG("Freeing %s as OCTET STRING", td->name);
709
710 if(st->buf) {
711 FREEMEM(st->buf);
712 }
713
714 /*
715 * Remove decode-time stack.
716 */
717 if(stck) {
718 while(stck->tail) {
719 struct _stack_el *sel = stck->tail;
720 stck->tail = sel->prev;
721 FREEMEM(sel);
722 }
723 FREEMEM(stck);
724 }
725
726 if(!contents_only) {
727 FREEMEM(st);
728 }
729}
730
731/*
732 * Conversion routines.
733 */
734int
735OCTET_STRING_fromBuf(OCTET_STRING_t *st, const char *str, int len) {
736 void *buf;
737
738 if(st == 0 || (str == 0 && len)) {
739 errno = EINVAL;
740 return -1;
741 }
742
743 /*
744 * Clear the OCTET STRING.
745 */
746 if(str == NULL) {
747 if(st->buf)
748 FREEMEM(st->buf);
749 st->size = 0;
750 return 0;
751 }
752
753 /* Determine the original string size, if not explicitly given */
754 if(len < 0)
755 len = strlen(str);
756
757 /* Allocate and fill the memory */
758 buf = MALLOC(len + 1);
759 if(buf == NULL) {
760 return -1;
761 } else {
Lev Walkinc2346572004-08-11 09:07:36 +0000762 st->buf = (uint8_t *)buf;
Lev Walkinf15320b2004-06-03 03:38:44 +0000763 st->size = len;
764 }
765
766 memcpy(buf, str, len);
767 st->buf[st->size] = '\0'; /* Couldn't use memcpy(len+1)! */
768
769 return 0;
770}
771
772OCTET_STRING_t *
773OCTET_STRING_new_fromBuf(const char *str, int len) {
774 OCTET_STRING_t *st;
775
Lev Walkinc2346572004-08-11 09:07:36 +0000776 (void *)st = CALLOC(1, sizeof(*st));
Lev Walkinf15320b2004-06-03 03:38:44 +0000777 if(st && str && OCTET_STRING_fromBuf(st, str, len)) {
778 free(st);
779 st = NULL;
780 }
781
782 return st;
783}
784