blob: 85f93f4e4c8258aa1b7b3156cb933a93b6f0f913 [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001/*-
Lev Walkin0a8aa602006-09-18 20:05:55 +00002 * Copyright (c) 2003, 2004, 2005, 2006 Lev Walkin <vlm@lionet.info>.
Lev Walkin75b1bef2005-04-25 19:38:21 +00003 * 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 */
Wim Lewis18c2ec92014-07-29 11:30:10 -070014static const ber_tlv_tag_t asn_DEF_OCTET_STRING_tags[] = {
Lev Walkinf15320b2004-06-03 03:38:44 +000015 (ASN_TAG_CLASS_UNIVERSAL | (4 << 2))
16};
Vasil Velichkovd82f77b2016-07-19 19:34:09 +030017static asn_OCTET_STRING_specifics_t asn_SPC_OCTET_STRING_specs = {
Lev Walkinbbd93252004-10-12 05:57:23 +000018 sizeof(OCTET_STRING_t),
19 offsetof(OCTET_STRING_t, _asn_ctx),
Lev Walkin3a4689a2006-11-24 11:20:27 +000020 ASN_OSUBV_STR
Lev Walkinbbd93252004-10-12 05:57:23 +000021};
Vasil Velichkovd82f77b2016-07-19 19:34:09 +030022static asn_per_constraints_t asn_DEF_OCTET_STRING_constraints = {
Lev Walkin3a4689a2006-11-24 11:20:27 +000023 { APC_CONSTRAINED, 8, 8, 0, 255 },
24 { APC_SEMI_CONSTRAINED, -1, -1, 0, 0 },
25 0, 0
Lev Walkin59b176e2005-11-26 11:25:14 +000026};
Lev Walkinde4825d2004-09-29 13:20:14 +000027asn_TYPE_descriptor_t asn_DEF_OCTET_STRING = {
Lev Walkindc06f6b2004-10-20 15:50:55 +000028 "OCTET STRING", /* Canonical name */
29 "OCTET_STRING", /* XML tag name */
Lev Walkina9cc46e2004-09-22 16:06:28 +000030 OCTET_STRING_free,
31 OCTET_STRING_print, /* non-ascii stuff, generally */
Lev Walkinf15320b2004-06-03 03:38:44 +000032 asn_generic_no_constraint,
33 OCTET_STRING_decode_ber,
34 OCTET_STRING_encode_der,
Lev Walkindc06f6b2004-10-20 15:50:55 +000035 OCTET_STRING_decode_xer_hex,
Lev Walkina9cc46e2004-09-22 16:06:28 +000036 OCTET_STRING_encode_xer,
Lev Walkin80334ed2017-07-06 07:28:21 -070037#ifdef ASN_DISABLE_PER_SUPPORT
38 0,
39 0,
40#else
Lev Walkin59b176e2005-11-26 11:25:14 +000041 OCTET_STRING_decode_uper, /* Unaligned PER decoder */
Lev Walkin523de9e2006-08-18 01:34:18 +000042 OCTET_STRING_encode_uper, /* Unaligned PER encoder */
Lev Walkin80334ed2017-07-06 07:28:21 -070043#endif /* ASN_DISABLE_PER_SUPPORT */
Lev Walkinf15320b2004-06-03 03:38:44 +000044 0, /* Use generic outmost tag fetcher */
Lev Walkinde4825d2004-09-29 13:20:14 +000045 asn_DEF_OCTET_STRING_tags,
46 sizeof(asn_DEF_OCTET_STRING_tags)
47 / sizeof(asn_DEF_OCTET_STRING_tags[0]),
48 asn_DEF_OCTET_STRING_tags, /* Same as above */
49 sizeof(asn_DEF_OCTET_STRING_tags)
50 / sizeof(asn_DEF_OCTET_STRING_tags[0]),
Lev Walkin59b176e2005-11-26 11:25:14 +000051 0, /* No PER visible constraints */
Lev Walkin449f8322004-08-20 13:23:42 +000052 0, 0, /* No members */
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +080053 &asn_SPC_OCTET_STRING_specs
Lev Walkinf15320b2004-06-03 03:38:44 +000054};
55
Lev Walkincc6a9102004-09-23 22:06:26 +000056#undef _CH_PHASE
57#undef NEXT_PHASE
58#undef PREV_PHASE
Lev Walkin75b1bef2005-04-25 19:38:21 +000059#define _CH_PHASE(ctx, inc) do { \
60 if(ctx->phase == 0) \
61 ctx->context = 0; \
62 ctx->phase += inc; \
Lev Walkinf15320b2004-06-03 03:38:44 +000063 } while(0)
64#define NEXT_PHASE(ctx) _CH_PHASE(ctx, +1)
65#define PREV_PHASE(ctx) _CH_PHASE(ctx, -1)
66
Lev Walkincc6a9102004-09-23 22:06:26 +000067#undef ADVANCE
Lev Walkin75b1bef2005-04-25 19:38:21 +000068#define ADVANCE(num_bytes) do { \
69 size_t num = (num_bytes); \
70 buf_ptr = ((const char *)buf_ptr) + num; \
71 size -= num; \
72 consumed_myself += num; \
Lev Walkinf15320b2004-06-03 03:38:44 +000073 } while(0)
74
Lev Walkincc6a9102004-09-23 22:06:26 +000075#undef RETURN
Lev Walkin75b1bef2005-04-25 19:38:21 +000076#define RETURN(_code) do { \
Lev Walkin59b176e2005-11-26 11:25:14 +000077 asn_dec_rval_t tmprval; \
78 tmprval.code = _code; \
79 tmprval.consumed = consumed_myself; \
80 return tmprval; \
Lev Walkinf15320b2004-06-03 03:38:44 +000081 } while(0)
82
Lev Walkincc6a9102004-09-23 22:06:26 +000083#undef APPEND
Lev Walkind9bd7752004-06-05 08:17:50 +000084#define APPEND(bufptr, bufsize) do { \
Lev Walkin75b1bef2005-04-25 19:38:21 +000085 size_t _bs = (bufsize); /* Append size */ \
86 size_t _ns = ctx->context; /* Allocated now */ \
87 size_t _es = st->size + _bs; /* Expected size */ \
88 /* int is really a typeof(st->size): */ \
89 if((int)_es < 0) RETURN(RC_FAIL); \
90 if(_ns <= _es) { \
Lev Walkind9bd7752004-06-05 08:17:50 +000091 void *ptr; \
Lev Walkin188ed2c2004-09-13 08:31:01 +000092 /* Be nice and round to the memory allocator */ \
Lev Walkin75b1bef2005-04-25 19:38:21 +000093 do { _ns = _ns ? _ns << 1 : 16; } \
94 while(_ns <= _es); \
95 /* int is really a typeof(st->size): */ \
96 if((int)_ns < 0) RETURN(RC_FAIL); \
Lev Walkind9bd7752004-06-05 08:17:50 +000097 ptr = REALLOC(st->buf, _ns); \
98 if(ptr) { \
Lev Walkinc2346572004-08-11 09:07:36 +000099 st->buf = (uint8_t *)ptr; \
Lev Walkin75b1bef2005-04-25 19:38:21 +0000100 ctx->context = _ns; \
Lev Walkind9bd7752004-06-05 08:17:50 +0000101 } else { \
102 RETURN(RC_FAIL); \
103 } \
Lev Walkin7f85ef42005-07-02 20:24:27 +0000104 ASN_DEBUG("Reallocating into %ld", (long)_ns); \
Lev Walkind9bd7752004-06-05 08:17:50 +0000105 } \
Lev Walkin8d127872004-09-04 04:44:50 +0000106 memcpy(st->buf + st->size, bufptr, _bs); \
Lev Walkind9bd7752004-06-05 08:17:50 +0000107 /* Convenient nul-termination */ \
Lev Walkin75b1bef2005-04-25 19:38:21 +0000108 st->buf[_es] = '\0'; \
109 st->size = _es; \
Lev Walkinf15320b2004-06-03 03:38:44 +0000110 } while(0)
111
112/*
113 * The main reason why ASN.1 is still alive is that too much time and effort
114 * is necessary for learning it more or less adequately, thus creating a gut
115 * necessity to demonstrate that aquired skill everywhere afterwards.
116 * No, I am not going to explain what the following stuff is.
117 */
118struct _stack_el {
Lev Walkin3990ba62004-09-24 20:57:41 +0000119 ber_tlv_len_t left; /* What's left to read (or -1) */
Lev Walkin5c915992004-09-27 20:54:06 +0000120 ber_tlv_len_t got; /* What was actually processed */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000121 int cont_level; /* Depth of subcontainment */
Lev Walkinf15320b2004-06-03 03:38:44 +0000122 int want_nulls; /* Want null "end of content" octets? */
123 int bits_chopped; /* Flag in BIT STRING mode */
Lev Walkin5c915992004-09-27 20:54:06 +0000124 ber_tlv_tag_t tag; /* For debugging purposes */
Lev Walkinf15320b2004-06-03 03:38:44 +0000125 struct _stack_el *prev;
126 struct _stack_el *next;
127};
128struct _stack {
129 struct _stack_el *tail;
130 struct _stack_el *cur_ptr;
131};
132
133static struct _stack_el *
Lev Walkin3990ba62004-09-24 20:57:41 +0000134OS__add_stack_el(struct _stack *st) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000135 struct _stack_el *nel;
136
Lev Walkin188ed2c2004-09-13 08:31:01 +0000137 /*
138 * Reuse the old stack frame or allocate a new one.
139 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000140 if(st->cur_ptr && st->cur_ptr->next) {
141 nel = st->cur_ptr->next;
Lev Walkinf15320b2004-06-03 03:38:44 +0000142 nel->bits_chopped = 0;
Lev Walkin5c915992004-09-27 20:54:06 +0000143 nel->got = 0;
144 /* Retain the nel->cont_level, it's correct. */
Lev Walkinf15320b2004-06-03 03:38:44 +0000145 } else {
Lev Walkin814cca72004-12-15 23:23:53 +0000146 nel = (struct _stack_el *)CALLOC(1, sizeof(struct _stack_el));
Lev Walkinf15320b2004-06-03 03:38:44 +0000147 if(nel == NULL)
148 return NULL;
149
150 if(st->tail) {
Lev Walkin188ed2c2004-09-13 08:31:01 +0000151 /* Increase a subcontainment depth */
152 nel->cont_level = st->tail->cont_level + 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000153 st->tail->next = nel;
154 }
155 nel->prev = st->tail;
156 st->tail = nel;
157 }
158
159 st->cur_ptr = nel;
160
161 return nel;
162}
163
164static struct _stack *
johvik28268d92017-05-09 10:27:52 +0200165_new_stack(void) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000166 return (struct _stack *)CALLOC(1, sizeof(struct _stack));
Lev Walkinf15320b2004-06-03 03:38:44 +0000167}
168
169/*
170 * Decode OCTET STRING type.
171 */
Lev Walkindc06f6b2004-10-20 15:50:55 +0000172asn_dec_rval_t
Lev Walkinde4825d2004-09-29 13:20:14 +0000173OCTET_STRING_decode_ber(asn_codec_ctx_t *opt_codec_ctx,
174 asn_TYPE_descriptor_t *td,
Lev Walkin59b176e2005-11-26 11:25:14 +0000175 void **sptr, const void *buf_ptr, size_t size, int tag_mode) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000176 asn_OCTET_STRING_specifics_t *specs = td->specifics
Lev Walkindc06f6b2004-10-20 15:50:55 +0000177 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +0800178 : &asn_SPC_OCTET_STRING_specs;
Lev Walkin59b176e2005-11-26 11:25:14 +0000179 BIT_STRING_t *st = (BIT_STRING_t *)*sptr;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000180 asn_dec_rval_t rval;
Lev Walkinde4825d2004-09-29 13:20:14 +0000181 asn_struct_ctx_t *ctx;
Lev Walkinf15320b2004-06-03 03:38:44 +0000182 ssize_t consumed_myself = 0;
Lev Walkinbbd93252004-10-12 05:57:23 +0000183 struct _stack *stck; /* Expectations stack structure */
Lev Walkin5c915992004-09-27 20:54:06 +0000184 struct _stack_el *sel = 0; /* Stack element */
Lev Walkinf15320b2004-06-03 03:38:44 +0000185 int tlv_constr;
Lev Walkin3a4689a2006-11-24 11:20:27 +0000186 enum asn_OS_Subvariant type_variant = specs->subvariant;
Lev Walkinf15320b2004-06-03 03:38:44 +0000187
Lev Walkincc6a9102004-09-23 22:06:26 +0000188 ASN_DEBUG("Decoding %s as %s (frame %ld)",
189 td->name,
Lev Walkin3a4689a2006-11-24 11:20:27 +0000190 (type_variant == ASN_OSUBV_STR) ?
Lev Walkinbbd93252004-10-12 05:57:23 +0000191 "OCTET STRING" : "OS-SpecialCase",
Lev Walkincc6a9102004-09-23 22:06:26 +0000192 (long)size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000193
194 /*
195 * Create the string if does not exist.
196 */
197 if(st == NULL) {
Lev Walkin59b176e2005-11-26 11:25:14 +0000198 st = (BIT_STRING_t *)(*sptr = CALLOC(1, specs->struct_size));
199 if(st == NULL) RETURN(RC_FAIL);
Lev Walkinf15320b2004-06-03 03:38:44 +0000200 }
201
202 /* Restore parsing context */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800203 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000204
205 switch(ctx->phase) {
206 case 0:
207 /*
208 * Check tags.
209 */
Lev Walkinde4825d2004-09-29 13:20:14 +0000210 rval = ber_check_tags(opt_codec_ctx, td, ctx,
Lev Walkin8e8078a2004-09-26 13:10:40 +0000211 buf_ptr, size, tag_mode, -1,
Lev Walkinf15320b2004-06-03 03:38:44 +0000212 &ctx->left, &tlv_constr);
Lev Walkin443d2512004-10-05 06:35:31 +0000213 if(rval.code != RC_OK)
214 return rval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000215
Lev Walkinf15320b2004-06-03 03:38:44 +0000216 if(tlv_constr) {
217 /*
218 * Complex operation, requires stack of expectations.
219 */
220 ctx->ptr = _new_stack();
Lev Walkinb02145b2017-06-28 08:52:07 -0700221 if(!ctx->ptr) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000222 RETURN(RC_FAIL);
223 }
224 } else {
225 /*
226 * Jump into stackless primitive decoding.
227 */
228 _CH_PHASE(ctx, 3);
Lev Walkin3a4689a2006-11-24 11:20:27 +0000229 if(type_variant == ASN_OSUBV_ANY && tag_mode != 1)
Lev Walkin188ed2c2004-09-13 08:31:01 +0000230 APPEND(buf_ptr, rval.consumed);
Lev Walkinf15320b2004-06-03 03:38:44 +0000231 ADVANCE(rval.consumed);
232 goto phase3;
233 }
234
Lev Walkinf15320b2004-06-03 03:38:44 +0000235 NEXT_PHASE(ctx);
236 /* Fall through */
237 case 1:
238 phase1:
239 /*
240 * Fill the stack with expectations.
241 */
Lev Walkin814cca72004-12-15 23:23:53 +0000242 stck = (struct _stack *)ctx->ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000243 sel = stck->cur_ptr;
244 do {
245 ber_tlv_tag_t tlv_tag;
246 ber_tlv_len_t tlv_len;
Lev Walkin188ed2c2004-09-13 08:31:01 +0000247 ber_tlv_tag_t expected_tag;
Lev Walkin5c915992004-09-27 20:54:06 +0000248 ssize_t tl, ll, tlvl;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000249 /* This one works even if (sel->left == -1) */
Simo Sorcecd943c22016-07-25 04:39:40 -0400250 size_t Left = ((!sel||(size_t)sel->left >= size)
251 ?size:(size_t)sel->left);
Lev Walkinf15320b2004-06-03 03:38:44 +0000252
Lev Walkin3990ba62004-09-24 20:57:41 +0000253
Lev Walkinabf68892004-10-26 10:12:14 +0000254 ASN_DEBUG("%p, s->l=%ld, s->wn=%ld, s->g=%ld\n", sel,
Lev Walkin1e3ccbb2004-10-26 10:56:10 +0000255 (long)(sel?sel->left:0),
256 (long)(sel?sel->want_nulls:0),
257 (long)(sel?sel->got:0)
Lev Walkin5c915992004-09-27 20:54:06 +0000258 );
259 if(sel && sel->left <= 0 && sel->want_nulls == 0) {
260 if(sel->prev) {
261 struct _stack_el *prev = sel->prev;
262 if(prev->left != -1) {
263 if(prev->left < sel->got)
264 RETURN(RC_FAIL);
265 prev->left -= sel->got;
266 }
267 prev->got += sel->got;
268 sel = stck->cur_ptr = prev;
269 if(!sel) break;
270 tlv_constr = 1;
271 continue;
272 } else {
273 sel = stck->cur_ptr = 0;
274 break; /* Nothing to wait */
275 }
276 }
277
Lev Walkin3990ba62004-09-24 20:57:41 +0000278 tl = ber_fetch_tag(buf_ptr, Left, &tlv_tag);
Lev Walkinabf68892004-10-26 10:12:14 +0000279 ASN_DEBUG("fetch tag(size=%ld,L=%ld), %sstack, left=%ld, wn=%ld, tl=%ld",
280 (long)size, (long)Left, sel?"":"!",
Lev Walkin1e3ccbb2004-10-26 10:56:10 +0000281 (long)(sel?sel->left:0),
282 (long)(sel?sel->want_nulls:0),
Lev Walkinabf68892004-10-26 10:12:14 +0000283 (long)tl);
Lev Walkinf15320b2004-06-03 03:38:44 +0000284 switch(tl) {
285 case -1: RETURN(RC_FAIL);
286 case 0: RETURN(RC_WMORE);
287 }
288
289 tlv_constr = BER_TLV_CONSTRUCTED(buf_ptr);
290
291 ll = ber_fetch_length(tlv_constr,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000292 (const char *)buf_ptr + tl,Left - tl,&tlv_len);
Lev Walkina6a926a2005-03-02 01:54:28 +0000293 ASN_DEBUG("Got tag=%s, tc=%d, left=%ld, tl=%ld, len=%ld, ll=%ld",
Lev Walkin8d127872004-09-04 04:44:50 +0000294 ber_tlv_tag_string(tlv_tag), tlv_constr,
Lev Walkina6a926a2005-03-02 01:54:28 +0000295 (long)Left, (long)tl, (long)tlv_len, (long)ll);
Lev Walkinf15320b2004-06-03 03:38:44 +0000296 switch(ll) {
297 case -1: RETURN(RC_FAIL);
298 case 0: RETURN(RC_WMORE);
299 }
300
Lev Walkin8d127872004-09-04 04:44:50 +0000301 if(sel && sel->want_nulls
Lev Walkin8c3b8542005-03-10 18:52:02 +0000302 && ((const uint8_t *)buf_ptr)[0] == 0
303 && ((const uint8_t *)buf_ptr)[1] == 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000304 {
Lev Walkin8d127872004-09-04 04:44:50 +0000305
306 ASN_DEBUG("Eat EOC; wn=%d--", sel->want_nulls);
307
Lev Walkin3a4689a2006-11-24 11:20:27 +0000308 if(type_variant == ASN_OSUBV_ANY
Lev Walkin07f388c2004-10-11 11:43:08 +0000309 && (tag_mode != 1 || sel->cont_level))
310 APPEND("\0\0", 2);
Lev Walkin5c915992004-09-27 20:54:06 +0000311
312 ADVANCE(2);
313 sel->got += 2;
314 if(sel->left != -1) {
315 sel->left -= 2; /* assert(sel->left >= 2) */
316 }
317
Lev Walkinf15320b2004-06-03 03:38:44 +0000318 sel->want_nulls--;
319 if(sel->want_nulls == 0) {
320 /* Move to the next expectation */
Lev Walkin5c915992004-09-27 20:54:06 +0000321 sel->left = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000322 tlv_constr = 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000323 }
Lev Walkin8d127872004-09-04 04:44:50 +0000324
325 continue;
Lev Walkin188ed2c2004-09-13 08:31:01 +0000326 }
327
328 /*
329 * Set up expected tags,
330 * depending on ASN.1 type being decoded.
331 */
Lev Walkinbbd93252004-10-12 05:57:23 +0000332 switch(type_variant) {
Lev Walkin3a4689a2006-11-24 11:20:27 +0000333 case ASN_OSUBV_BIT:
Lev Walkin188ed2c2004-09-13 08:31:01 +0000334 /* X.690: 8.6.4.1, NOTE 2 */
335 /* Fall through */
Lev Walkin3a4689a2006-11-24 11:20:27 +0000336 case ASN_OSUBV_STR:
Lev Walkin188ed2c2004-09-13 08:31:01 +0000337 default:
338 if(sel) {
339 int level = sel->cont_level;
340 if(level < td->all_tags_count) {
341 expected_tag = td->all_tags[level];
342 break;
343 } else if(td->all_tags_count) {
344 expected_tag = td->all_tags
345 [td->all_tags_count - 1];
346 break;
347 }
348 /* else, Fall through */
349 }
350 /* Fall through */
Lev Walkin3a4689a2006-11-24 11:20:27 +0000351 case ASN_OSUBV_ANY:
Lev Walkin188ed2c2004-09-13 08:31:01 +0000352 expected_tag = tlv_tag;
353 break;
354 }
355
356
357 if(tlv_tag != expected_tag) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000358 char buf[2][32];
359 ber_tlv_tag_snprint(tlv_tag,
360 buf[0], sizeof(buf[0]));
361 ber_tlv_tag_snprint(td->tags[td->tags_count-1],
362 buf[1], sizeof(buf[1]));
363 ASN_DEBUG("Tag does not match expectation: %s != %s",
364 buf[0], buf[1]);
365 RETURN(RC_FAIL);
366 }
367
Lev Walkinde4825d2004-09-29 13:20:14 +0000368 tlvl = tl + ll; /* Combined length of T and L encoding */
369 if((tlv_len + tlvl) < 0) {
370 /* tlv_len value is too big */
371 ASN_DEBUG("TLV encoding + length (%ld) is too big",
372 (long)tlv_len);
373 RETURN(RC_FAIL);
374 }
375
Lev Walkinf15320b2004-06-03 03:38:44 +0000376 /*
377 * Append a new expectation.
378 */
Lev Walkin3990ba62004-09-24 20:57:41 +0000379 sel = OS__add_stack_el(stck);
Lev Walkin5c915992004-09-27 20:54:06 +0000380 if(!sel) RETURN(RC_FAIL);
Lev Walkinf15320b2004-06-03 03:38:44 +0000381
Lev Walkin5c915992004-09-27 20:54:06 +0000382 sel->tag = tlv_tag;
383
384 sel->want_nulls = (tlv_len==-1);
385 if(sel->prev && sel->prev->left != -1) {
386 /* Check that the parent frame is big enough */
387 if(sel->prev->left < tlvl + (tlv_len==-1?0:tlv_len))
388 RETURN(RC_FAIL);
389 if(tlv_len == -1)
390 sel->left = sel->prev->left - tlvl;
391 else
392 sel->left = tlv_len;
393 } else {
394 sel->left = tlv_len;
395 }
Lev Walkin3a4689a2006-11-24 11:20:27 +0000396 if(type_variant == ASN_OSUBV_ANY
Lev Walkin07f388c2004-10-11 11:43:08 +0000397 && (tag_mode != 1 || sel->cont_level))
398 APPEND(buf_ptr, tlvl);
Lev Walkin5c915992004-09-27 20:54:06 +0000399 sel->got += tlvl;
400 ADVANCE(tlvl);
401
Lev Walkinabf68892004-10-26 10:12:14 +0000402 ASN_DEBUG("+EXPECT2 got=%ld left=%ld, wn=%d, clvl=%d",
403 (long)sel->got, (long)sel->left,
404 sel->want_nulls, sel->cont_level);
Lev Walkin5c915992004-09-27 20:54:06 +0000405
Lev Walkinf15320b2004-06-03 03:38:44 +0000406 } while(tlv_constr);
407 if(sel == NULL) {
408 /* Finished operation, "phase out" */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000409 ASN_DEBUG("Phase out");
Lev Walkinf15320b2004-06-03 03:38:44 +0000410 _CH_PHASE(ctx, +3);
411 break;
412 }
413
414 NEXT_PHASE(ctx);
415 /* Fall through */
416 case 2:
Lev Walkin814cca72004-12-15 23:23:53 +0000417 stck = (struct _stack *)ctx->ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000418 sel = stck->cur_ptr;
Lev Walkin5c915992004-09-27 20:54:06 +0000419 ASN_DEBUG("Phase 2: Need %ld bytes, size=%ld, alrg=%ld, wn=%d",
420 (long)sel->left, (long)size, (long)sel->got,
421 sel->want_nulls);
Lev Walkinf15320b2004-06-03 03:38:44 +0000422 {
423 ber_tlv_len_t len;
424
425 assert(sel->left >= 0);
426
Lev Walkinec1ffd42004-08-18 04:53:32 +0000427 len = ((ber_tlv_len_t)size < sel->left)
428 ? (ber_tlv_len_t)size : sel->left;
Lev Walkinf15320b2004-06-03 03:38:44 +0000429 if(len > 0) {
Lev Walkin3a4689a2006-11-24 11:20:27 +0000430 if(type_variant == ASN_OSUBV_BIT
Lev Walkin188ed2c2004-09-13 08:31:01 +0000431 && sel->bits_chopped == 0) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000432 /* Put the unused-bits-octet away */
Lev Walkin8c3b8542005-03-10 18:52:02 +0000433 st->bits_unused = *(const uint8_t *)buf_ptr;
434 APPEND(((const char *)buf_ptr+1), (len - 1));
Lev Walkinf15320b2004-06-03 03:38:44 +0000435 sel->bits_chopped = 1;
436 } else {
437 APPEND(buf_ptr, len);
438 }
439 ADVANCE(len);
440 sel->left -= len;
Lev Walkin5c915992004-09-27 20:54:06 +0000441 sel->got += len;
Lev Walkinf15320b2004-06-03 03:38:44 +0000442 }
443
Lev Walkin5c915992004-09-27 20:54:06 +0000444 if(sel->left) {
445 ASN_DEBUG("OS left %ld, size = %ld, wn=%d\n",
446 (long)sel->left, (long)size, sel->want_nulls);
Lev Walkinf15320b2004-06-03 03:38:44 +0000447 RETURN(RC_WMORE);
Lev Walkinf15320b2004-06-03 03:38:44 +0000448 }
Lev Walkin5c915992004-09-27 20:54:06 +0000449
450 PREV_PHASE(ctx);
451 goto phase1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000452 }
453 break;
454 case 3:
455 phase3:
456 /*
457 * Primitive form, no stack required.
458 */
Lev Walkinbbd93252004-10-12 05:57:23 +0000459 assert(ctx->left >= 0);
460
Lev Walkind9bd7752004-06-05 08:17:50 +0000461 if(size < (size_t)ctx->left) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000462 if(!size) RETURN(RC_WMORE);
Lev Walkin3a4689a2006-11-24 11:20:27 +0000463 if(type_variant == ASN_OSUBV_BIT && !ctx->context) {
Lev Walkin8c3b8542005-03-10 18:52:02 +0000464 st->bits_unused = *(const uint8_t *)buf_ptr;
Lev Walkinbbd93252004-10-12 05:57:23 +0000465 ctx->left--;
466 ADVANCE(1);
467 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000468 APPEND(buf_ptr, size);
Lev Walkin75b1bef2005-04-25 19:38:21 +0000469 assert(ctx->context > 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000470 ctx->left -= size;
471 ADVANCE(size);
472 RETURN(RC_WMORE);
473 } else {
Lev Walkin3a4689a2006-11-24 11:20:27 +0000474 if(type_variant == ASN_OSUBV_BIT
Lev Walkin75b1bef2005-04-25 19:38:21 +0000475 && !ctx->context && ctx->left) {
Lev Walkin8c3b8542005-03-10 18:52:02 +0000476 st->bits_unused = *(const uint8_t *)buf_ptr;
Lev Walkinbbd93252004-10-12 05:57:23 +0000477 ctx->left--;
478 ADVANCE(1);
479 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000480 APPEND(buf_ptr, ctx->left);
481 ADVANCE(ctx->left);
482 ctx->left = 0;
483
484 NEXT_PHASE(ctx);
485 }
486 break;
487 }
488
Lev Walkin5c915992004-09-27 20:54:06 +0000489 if(sel) {
490 ASN_DEBUG("3sel p=%p, wn=%d, l=%ld, g=%ld, size=%ld",
491 sel->prev, sel->want_nulls,
492 (long)sel->left, (long)sel->got, (long)size);
493 if(sel->prev || sel->want_nulls > 1 || sel->left > 0) {
494 RETURN(RC_WMORE);
495 }
496 }
497
Lev Walkinf15320b2004-06-03 03:38:44 +0000498 /*
499 * BIT STRING-specific processing.
500 */
Lev Walkin3a4689a2006-11-24 11:20:27 +0000501 if(type_variant == ASN_OSUBV_BIT && st->size) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000502 /* Finalize BIT STRING: zero out unused bits. */
Lev Walkinbbd93252004-10-12 05:57:23 +0000503 st->buf[st->size-1] &= 0xff << st->bits_unused;
Lev Walkinf15320b2004-06-03 03:38:44 +0000504 }
505
Lev Walkinabf68892004-10-26 10:12:14 +0000506 ASN_DEBUG("Took %ld bytes to encode %s: [%s]:%ld",
507 (long)consumed_myself, td->name,
Lev Walkin3a4689a2006-11-24 11:20:27 +0000508 (type_variant == ASN_OSUBV_STR) ? (char *)st->buf : "<data>",
Lev Walkinabf68892004-10-26 10:12:14 +0000509 (long)st->size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000510
Lev Walkinf15320b2004-06-03 03:38:44 +0000511
Lev Walkin5c915992004-09-27 20:54:06 +0000512 RETURN(RC_OK);
Lev Walkinf15320b2004-06-03 03:38:44 +0000513}
514
515/*
516 * Encode OCTET STRING type using DER.
517 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000518asn_enc_rval_t
Lev Walkinbbd93252004-10-12 05:57:23 +0000519OCTET_STRING_encode_der(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000520 int tag_mode, ber_tlv_tag_t tag,
521 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000522 asn_enc_rval_t er;
523 asn_OCTET_STRING_specifics_t *specs = td->specifics
Lev Walkindc06f6b2004-10-20 15:50:55 +0000524 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +0800525 : &asn_SPC_OCTET_STRING_specs;
Lev Walkinbbd93252004-10-12 05:57:23 +0000526 BIT_STRING_t *st = (BIT_STRING_t *)sptr;
Lev Walkin3a4689a2006-11-24 11:20:27 +0000527 enum asn_OS_Subvariant type_variant = specs->subvariant;
Lev Walkinbbd93252004-10-12 05:57:23 +0000528 int fix_last_byte = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000529
530 ASN_DEBUG("%s %s as OCTET STRING",
Lev Walkin1f670c12004-09-02 12:57:25 +0000531 cb?"Estimating":"Encoding", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000532
533 /*
Lev Walkinbbd93252004-10-12 05:57:23 +0000534 * Write tags.
Lev Walkinf15320b2004-06-03 03:38:44 +0000535 */
Lev Walkin3a4689a2006-11-24 11:20:27 +0000536 if(type_variant != ASN_OSUBV_ANY || tag_mode == 1) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000537 er.encoded = der_write_tags(td,
Lev Walkin3a4689a2006-11-24 11:20:27 +0000538 (type_variant == ASN_OSUBV_BIT) + st->size,
539 tag_mode, type_variant == ASN_OSUBV_ANY, tag,
540 cb, app_key);
Lev Walkinbbd93252004-10-12 05:57:23 +0000541 if(er.encoded == -1) {
542 er.failed_type = td;
543 er.structure_ptr = sptr;
544 return er;
Lev Walkinf15320b2004-06-03 03:38:44 +0000545 }
Lev Walkinbbd93252004-10-12 05:57:23 +0000546 } else {
547 /* Disallow: [<tag>] IMPLICIT ANY */
Lev Walkin3a4689a2006-11-24 11:20:27 +0000548 assert(type_variant != ASN_OSUBV_ANY || tag_mode != -1);
Lev Walkinbbd93252004-10-12 05:57:23 +0000549 er.encoded = 0;
550 }
551
552 if(!cb) {
Lev Walkin3a4689a2006-11-24 11:20:27 +0000553 er.encoded += (type_variant == ASN_OSUBV_BIT) + st->size;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700554 ASN__ENCODED_OK(er);
Lev Walkinf15320b2004-06-03 03:38:44 +0000555 }
556
Lev Walkin07f388c2004-10-11 11:43:08 +0000557 /*
Lev Walkinbbd93252004-10-12 05:57:23 +0000558 * Prepare to deal with the last octet of BIT STRING.
Lev Walkin07f388c2004-10-11 11:43:08 +0000559 */
Lev Walkin3a4689a2006-11-24 11:20:27 +0000560 if(type_variant == ASN_OSUBV_BIT) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000561 uint8_t b = st->bits_unused & 0x07;
562 if(b && st->size) fix_last_byte = 1;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700563 ASN__CALLBACK(&b, 1);
Lev Walkinbbd93252004-10-12 05:57:23 +0000564 er.encoded++;
Lev Walkinf15320b2004-06-03 03:38:44 +0000565 }
566
Lev Walkinbbd93252004-10-12 05:57:23 +0000567 /* Invoke callback for the main part of the buffer */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700568 ASN__CALLBACK(st->buf, st->size - fix_last_byte);
Lev Walkinf15320b2004-06-03 03:38:44 +0000569
Lev Walkinbbd93252004-10-12 05:57:23 +0000570 /* The last octet should be stripped off the unused bits */
571 if(fix_last_byte) {
572 uint8_t b = st->buf[st->size-1] & (0xff << st->bits_unused);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700573 ASN__CALLBACK(&b, 1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000574 }
575
Lev Walkinbbd93252004-10-12 05:57:23 +0000576 er.encoded += st->size;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700577 ASN__ENCODED_OK(er);
Lev Walkinbbd93252004-10-12 05:57:23 +0000578cb_failed:
Lev Walkin7c1dc052016-03-14 03:08:15 -0700579 ASN__ENCODE_FAILED;
Lev Walkinf15320b2004-06-03 03:38:44 +0000580}
581
Lev Walkina9cc46e2004-09-22 16:06:28 +0000582asn_enc_rval_t
Lev Walkinde4825d2004-09-29 13:20:14 +0000583OCTET_STRING_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000584 int ilevel, enum xer_encoder_flags_e flags,
585 asn_app_consume_bytes_f *cb, void *app_key) {
Wim Lewis59e8d282014-08-04 12:39:35 -0700586 const char * const h2c = "0123456789ABCDEF";
Lev Walkina9cc46e2004-09-22 16:06:28 +0000587 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
588 asn_enc_rval_t er;
589 char scratch[16 * 3 + 4];
590 char *p = scratch;
591 uint8_t *buf;
592 uint8_t *end;
593 size_t i;
594
Lev Walkin2ddd3082006-10-16 12:32:14 +0000595 if(!st || (!st->buf && st->size))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700596 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000597
598 er.encoded = 0;
599
600 /*
601 * Dump the contents of the buffer in hexadecimal.
602 */
603 buf = st->buf;
604 end = buf + st->size;
605 if(flags & XER_F_CANONICAL) {
606 char *scend = scratch + (sizeof(scratch) - 2);
607 for(; buf < end; buf++) {
608 if(p >= scend) {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700609 ASN__CALLBACK(scratch, p - scratch);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000610 er.encoded += p - scratch;
611 p = scratch;
612 }
613 *p++ = h2c[(*buf >> 4) & 0x0F];
614 *p++ = h2c[*buf & 0x0F];
615 }
Lev Walkincc6a9102004-09-23 22:06:26 +0000616
Lev Walkin7c1dc052016-03-14 03:08:15 -0700617 ASN__CALLBACK(scratch, p-scratch); /* Dump the rest */
Lev Walkincc6a9102004-09-23 22:06:26 +0000618 er.encoded += p - scratch;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000619 } else {
620 for(i = 0; buf < end; buf++, i++) {
621 if(!(i % 16) && (i || st->size > 16)) {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700622 ASN__CALLBACK(scratch, p-scratch);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000623 er.encoded += (p-scratch);
624 p = scratch;
Lev Walkinc6cac8e2016-03-14 02:57:07 -0700625 ASN__TEXT_INDENT(1, ilevel);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000626 }
627 *p++ = h2c[(*buf >> 4) & 0x0F];
628 *p++ = h2c[*buf & 0x0F];
629 *p++ = 0x20;
630 }
Lev Walkincc6a9102004-09-23 22:06:26 +0000631 if(p - scratch) {
632 p--; /* Remove the tail space */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700633 ASN__CALLBACK(scratch, p-scratch); /* Dump the rest */
Lev Walkincc6a9102004-09-23 22:06:26 +0000634 er.encoded += p - scratch;
635 if(st->size > 16)
Lev Walkinc6cac8e2016-03-14 02:57:07 -0700636 ASN__TEXT_INDENT(1, ilevel-1);
Lev Walkincc6a9102004-09-23 22:06:26 +0000637 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000638 }
639
Lev Walkin7c1dc052016-03-14 03:08:15 -0700640 ASN__ENCODED_OK(er);
Lev Walkin942fd082004-10-03 09:13:02 +0000641cb_failed:
Lev Walkin7c1dc052016-03-14 03:08:15 -0700642 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000643}
644
Wim Lewis18c2ec92014-07-29 11:30:10 -0700645static const struct OCTET_STRING__xer_escape_table_s {
646 const char *string;
Lev Walkin9ca81892004-10-03 10:54:25 +0000647 int size;
648} OCTET_STRING__xer_escape_table[] = {
649#define OSXET(s) { s, sizeof(s) - 1 }
650 OSXET("\074\156\165\154\057\076"), /* <nul/> */
651 OSXET("\074\163\157\150\057\076"), /* <soh/> */
652 OSXET("\074\163\164\170\057\076"), /* <stx/> */
653 OSXET("\074\145\164\170\057\076"), /* <etx/> */
654 OSXET("\074\145\157\164\057\076"), /* <eot/> */
655 OSXET("\074\145\156\161\057\076"), /* <enq/> */
656 OSXET("\074\141\143\153\057\076"), /* <ack/> */
657 OSXET("\074\142\145\154\057\076"), /* <bel/> */
658 OSXET("\074\142\163\057\076"), /* <bs/> */
659 OSXET("\011"), /* \t */
660 OSXET("\012"), /* \n */
661 OSXET("\074\166\164\057\076"), /* <vt/> */
662 OSXET("\074\146\146\057\076"), /* <ff/> */
663 OSXET("\015"), /* \r */
664 OSXET("\074\163\157\057\076"), /* <so/> */
665 OSXET("\074\163\151\057\076"), /* <si/> */
666 OSXET("\074\144\154\145\057\076"), /* <dle/> */
667 OSXET("\074\144\143\061\057\076"), /* <de1/> */
668 OSXET("\074\144\143\062\057\076"), /* <de2/> */
669 OSXET("\074\144\143\063\057\076"), /* <de3/> */
670 OSXET("\074\144\143\064\057\076"), /* <de4/> */
671 OSXET("\074\156\141\153\057\076"), /* <nak/> */
672 OSXET("\074\163\171\156\057\076"), /* <syn/> */
673 OSXET("\074\145\164\142\057\076"), /* <etb/> */
674 OSXET("\074\143\141\156\057\076"), /* <can/> */
675 OSXET("\074\145\155\057\076"), /* <em/> */
676 OSXET("\074\163\165\142\057\076"), /* <sub/> */
677 OSXET("\074\145\163\143\057\076"), /* <esc/> */
678 OSXET("\074\151\163\064\057\076"), /* <is4/> */
679 OSXET("\074\151\163\063\057\076"), /* <is3/> */
680 OSXET("\074\151\163\062\057\076"), /* <is2/> */
681 OSXET("\074\151\163\061\057\076"), /* <is1/> */
682 { 0, 0 }, /* " " */
683 { 0, 0 }, /* ! */
684 { 0, 0 }, /* \" */
685 { 0, 0 }, /* # */
686 { 0, 0 }, /* $ */
687 { 0, 0 }, /* % */
688 OSXET("\046\141\155\160\073"), /* &amp; */
689 { 0, 0 }, /* ' */
690 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, /* ()*+,-./ */
691 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, /* 01234567 */
692 {0,0},{0,0},{0,0},{0,0}, /* 89:; */
693 OSXET("\046\154\164\073"), /* &lt; */
694 { 0, 0 }, /* = */
695 OSXET("\046\147\164\073"), /* &gt; */
696};
697
Lev Walkindc06f6b2004-10-20 15:50:55 +0000698static int
Lev Walkin0fab1a62005-03-09 22:19:25 +0000699OS__check_escaped_control_char(const void *buf, int size) {
Lev Walkindc06f6b2004-10-20 15:50:55 +0000700 size_t i;
701 /*
702 * Inefficient algorithm which translates the escape sequences
703 * defined above into characters. Returns -1 if not found.
704 * TODO: replace by a faster algorithm (bsearch(), hash or
705 * nested table lookups).
706 */
707 for(i = 0; i < 32 /* Don't spend time on the bottom half */; i++) {
Wim Lewis18c2ec92014-07-29 11:30:10 -0700708 const struct OCTET_STRING__xer_escape_table_s *el;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000709 el = &OCTET_STRING__xer_escape_table[i];
710 if(el->size == size && memcmp(buf, el->string, size) == 0)
711 return i;
712 }
713 return -1;
714}
715
716static int
Lev Walkin0fab1a62005-03-09 22:19:25 +0000717OCTET_STRING__handle_control_chars(void *struct_ptr, const void *chunk_buf, size_t chunk_size) {
Lev Walkindc06f6b2004-10-20 15:50:55 +0000718 /*
719 * This might be one of the escape sequences
720 * for control characters. Check it out.
721 * #11.15.5
722 */
723 int control_char = OS__check_escaped_control_char(chunk_buf,chunk_size);
724 if(control_char >= 0) {
725 OCTET_STRING_t *st = (OCTET_STRING_t *)struct_ptr;
726 void *p = REALLOC(st->buf, st->size + 2);
727 if(p) {
728 st->buf = (uint8_t *)p;
729 st->buf[st->size++] = control_char;
730 st->buf[st->size] = '\0'; /* nul-termination */
731 return 0;
732 }
733 }
734
735 return -1; /* No, it's not */
736}
737
Lev Walkina9cc46e2004-09-22 16:06:28 +0000738asn_enc_rval_t
Lev Walkindc06f6b2004-10-20 15:50:55 +0000739OCTET_STRING_encode_xer_utf8(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000740 int ilevel, enum xer_encoder_flags_e flags,
741 asn_app_consume_bytes_f *cb, void *app_key) {
742 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
743 asn_enc_rval_t er;
Lev Walkin9ca81892004-10-03 10:54:25 +0000744 uint8_t *buf, *end;
745 uint8_t *ss; /* Sequence start */
746 ssize_t encoded_len = 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000747
748 (void)ilevel; /* Unused argument */
749 (void)flags; /* Unused argument */
750
Lev Walkin2ddd3082006-10-16 12:32:14 +0000751 if(!st || (!st->buf && st->size))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700752 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000753
Lev Walkin9ca81892004-10-03 10:54:25 +0000754 buf = st->buf;
755 end = buf + st->size;
756 for(ss = buf; buf < end; buf++) {
Lev Walkin443d2512004-10-05 06:35:31 +0000757 unsigned int ch = *buf;
Lev Walkin9ca81892004-10-03 10:54:25 +0000758 int s_len; /* Special encoding sequence length */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000759
Lev Walkin9ca81892004-10-03 10:54:25 +0000760 /*
761 * Escape certain characters: X.680/11.15
762 */
763 if(ch < sizeof(OCTET_STRING__xer_escape_table)
764 /sizeof(OCTET_STRING__xer_escape_table[0])
765 && (s_len = OCTET_STRING__xer_escape_table[ch].size)) {
766 if(((buf - ss) && cb(ss, buf - ss, app_key) < 0)
767 || cb(OCTET_STRING__xer_escape_table[ch].string, s_len,
768 app_key) < 0)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700769 ASN__ENCODE_FAILED;
Lev Walkin9ca81892004-10-03 10:54:25 +0000770 encoded_len += (buf - ss) + s_len;
771 ss = buf + 1;
772 }
773 }
774
775 encoded_len += (buf - ss);
776 if((buf - ss) && cb(ss, buf - ss, app_key) < 0)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700777 ASN__ENCODE_FAILED;
Lev Walkin9ca81892004-10-03 10:54:25 +0000778
779 er.encoded = encoded_len;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700780 ASN__ENCODED_OK(er);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000781}
782
Lev Walkindc06f6b2004-10-20 15:50:55 +0000783/*
784 * Convert from hexadecimal format (cstring): "AB CD EF"
785 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000786static 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 +0000787 OCTET_STRING_t *st = (OCTET_STRING_t *)sptr;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000788 const char *chunk_stop = (const char *)chunk_buf;
789 const char *p = chunk_stop;
790 const char *pend = p + chunk_size;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000791 unsigned int clv = 0;
792 int half = 0; /* Half bit */
793 uint8_t *buf;
794
795 /* Reallocate buffer according to high cap estimation */
796 ssize_t _ns = st->size + (chunk_size + 1) / 2;
797 void *nptr = REALLOC(st->buf, _ns + 1);
798 if(!nptr) return -1;
799 st->buf = (uint8_t *)nptr;
800 buf = st->buf + st->size;
801
802 /*
803 * If something like " a b c " appears here, the " a b":3 will be
804 * converted, and the rest skipped. That is, unless buf_size is greater
805 * than chunk_size, then it'll be equivalent to "ABC0".
806 */
807 for(; p < pend; p++) {
Lev Walkin0fab1a62005-03-09 22:19:25 +0000808 int ch = *(const unsigned char *)p;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000809 switch(ch) {
810 case 0x09: case 0x0a: case 0x0c: case 0x0d:
811 case 0x20:
812 /* Ignore whitespace */
813 continue;
814 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: /*01234*/
815 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: /*56789*/
816 clv = (clv << 4) + (ch - 0x30);
817 break;
818 case 0x41: case 0x42: case 0x43: /* ABC */
819 case 0x44: case 0x45: case 0x46: /* DEF */
Lev Walkin3ae21bd2005-02-21 14:43:48 +0000820 clv = (clv << 4) + (ch - 0x41 + 10);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000821 break;
822 case 0x61: case 0x62: case 0x63: /* abc */
823 case 0x64: case 0x65: case 0x66: /* def */
Lev Walkin3ae21bd2005-02-21 14:43:48 +0000824 clv = (clv << 4) + (ch - 0x61 + 10);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000825 break;
826 default:
827 *buf = 0; /* JIC */
828 return -1;
829 }
830 if(half++) {
831 half = 0;
832 *buf++ = clv;
833 chunk_stop = p + 1;
834 }
835 }
836
837 /*
838 * Check partial decoding.
839 */
840 if(half) {
841 if(have_more) {
842 /*
843 * Partial specification is fine,
844 * because no more more PXER_TEXT data is available.
845 */
846 *buf++ = clv << 4;
847 chunk_stop = p;
848 }
849 } else {
850 chunk_stop = p;
851 }
852
853 st->size = buf - st->buf; /* Adjust the buffer size */
854 assert(st->size <= _ns);
855 st->buf[st->size] = 0; /* Courtesy termination */
856
Lev Walkin0fab1a62005-03-09 22:19:25 +0000857 return (chunk_stop - (const char *)chunk_buf); /* Converted size */
Lev Walkindc06f6b2004-10-20 15:50:55 +0000858}
859
860/*
861 * Convert from binary format: "00101011101"
862 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000863static 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 +0000864 BIT_STRING_t *st = (BIT_STRING_t *)sptr;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000865 const char *p = (const char *)chunk_buf;
866 const char *pend = p + chunk_size;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000867 int bits_unused = st->bits_unused & 0x7;
868 uint8_t *buf;
869
870 /* Reallocate buffer according to high cap estimation */
871 ssize_t _ns = st->size + (chunk_size + 7) / 8;
872 void *nptr = REALLOC(st->buf, _ns + 1);
873 if(!nptr) return -1;
874 st->buf = (uint8_t *)nptr;
875 buf = st->buf + st->size;
876
877 (void)have_more;
878
879 if(bits_unused == 0)
880 bits_unused = 8;
881 else if(st->size)
882 buf--;
883
884 /*
885 * Convert series of 0 and 1 into the octet string.
886 */
887 for(; p < pend; p++) {
Lev Walkin0fab1a62005-03-09 22:19:25 +0000888 int ch = *(const unsigned char *)p;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000889 switch(ch) {
890 case 0x09: case 0x0a: case 0x0c: case 0x0d:
891 case 0x20:
892 /* Ignore whitespace */
893 break;
894 case 0x30:
895 case 0x31:
896 if(bits_unused-- <= 0) {
897 *++buf = 0; /* Clean the cell */
898 bits_unused = 7;
899 }
900 *buf |= (ch&1) << bits_unused;
901 break;
902 default:
903 st->bits_unused = bits_unused;
904 return -1;
905 }
906 }
907
908 if(bits_unused == 8) {
909 st->size = buf - st->buf;
910 st->bits_unused = 0;
911 } else {
912 st->size = buf - st->buf + 1;
913 st->bits_unused = bits_unused;
914 }
915
916 assert(st->size <= _ns);
917 st->buf[st->size] = 0; /* Courtesy termination */
918
919 return chunk_size; /* Converted in full */
920}
921
922/*
923 * Something like strtod(), but with stricter rules.
924 */
925static int
Lev Walkin0fab1a62005-03-09 22:19:25 +0000926OS__strtoent(int base, const char *buf, const char *end, int32_t *ret_value) {
Lev Walkin33700162004-10-26 09:03:31 +0000927 int32_t val = 0;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000928 const char *p;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000929
930 for(p = buf; p < end; p++) {
931 int ch = *p;
Lev Walkinb0f3db62005-07-03 05:30:15 +0000932
933 /* Strange huge value */
934 if((val * base + base) < 0)
935 return -1;
936
Lev Walkindc06f6b2004-10-20 15:50:55 +0000937 switch(ch) {
938 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: /*01234*/
939 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: /*56789*/
940 val = val * base + (ch - 0x30);
941 break;
942 case 0x41: case 0x42: case 0x43: /* ABC */
943 case 0x44: case 0x45: case 0x46: /* DEF */
Lev Walkin3ae21bd2005-02-21 14:43:48 +0000944 val = val * base + (ch - 0x41 + 10);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000945 break;
946 case 0x61: case 0x62: case 0x63: /* abc */
947 case 0x64: case 0x65: case 0x66: /* def */
Lev Walkin3ae21bd2005-02-21 14:43:48 +0000948 val = val * base + (ch - 0x61 + 10);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000949 break;
950 case 0x3b: /* ';' */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000951 *ret_value = val;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000952 return (p - buf) + 1;
953 default:
954 return -1; /* Character set error */
955 }
956 }
957
Lev Walkinb0f3db62005-07-03 05:30:15 +0000958 *ret_value = -1;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000959 return (p - buf);
960}
961
962/*
963 * Convert from the plain UTF-8 format, expanding entity references: "2 &lt; 3"
964 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000965static 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 +0000966 OCTET_STRING_t *st = (OCTET_STRING_t *)sptr;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000967 const char *p = (const char *)chunk_buf;
968 const char *pend = p + chunk_size;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000969 uint8_t *buf;
970
971 /* Reallocate buffer */
972 ssize_t _ns = st->size + chunk_size;
973 void *nptr = REALLOC(st->buf, _ns + 1);
974 if(!nptr) return -1;
975 st->buf = (uint8_t *)nptr;
976 buf = st->buf + st->size;
977
978 /*
979 * Convert series of 0 and 1 into the octet string.
980 */
981 for(; p < pend; p++) {
Lev Walkin0fab1a62005-03-09 22:19:25 +0000982 int ch = *(const unsigned char *)p;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000983 int len; /* Length of the rest of the chunk */
984
985 if(ch != 0x26 /* '&' */) {
986 *buf++ = ch;
987 continue; /* That was easy... */
988 }
989
990 /*
991 * Process entity reference.
992 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000993 len = chunk_size - (p - (const char *)chunk_buf);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000994 if(len == 1 /* "&" */) goto want_more;
995 if(p[1] == 0x23 /* '#' */) {
Lev Walkin0fab1a62005-03-09 22:19:25 +0000996 const char *pval; /* Pointer to start of digits */
Lev Walkinbc0dce32006-07-27 09:56:13 +0000997 int32_t val = 0; /* Entity reference value */
Lev Walkindc06f6b2004-10-20 15:50:55 +0000998 int base;
999
1000 if(len == 2 /* "&#" */) goto want_more;
1001 if(p[2] == 0x78 /* 'x' */)
1002 pval = p + 3, base = 16;
1003 else
1004 pval = p + 2, base = 10;
1005 len = OS__strtoent(base, pval, p + len, &val);
1006 if(len == -1) {
1007 /* Invalid charset. Just copy verbatim. */
1008 *buf++ = ch;
1009 continue;
1010 }
1011 if(!len || pval[len-1] != 0x3b) goto want_more;
1012 assert(val > 0);
1013 p += (pval - p) + len - 1; /* Advance past entref */
1014
1015 if(val < 0x80) {
1016 *buf++ = (char)val;
1017 } else if(val < 0x800) {
1018 *buf++ = 0xc0 | ((val >> 6));
1019 *buf++ = 0x80 | ((val & 0x3f));
1020 } else if(val < 0x10000) {
1021 *buf++ = 0xe0 | ((val >> 12));
1022 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1023 *buf++ = 0x80 | ((val & 0x3f));
1024 } else if(val < 0x200000) {
1025 *buf++ = 0xf0 | ((val >> 18));
1026 *buf++ = 0x80 | ((val >> 12) & 0x3f);
1027 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1028 *buf++ = 0x80 | ((val & 0x3f));
1029 } else if(val < 0x4000000) {
1030 *buf++ = 0xf8 | ((val >> 24));
1031 *buf++ = 0x80 | ((val >> 18) & 0x3f);
1032 *buf++ = 0x80 | ((val >> 12) & 0x3f);
1033 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1034 *buf++ = 0x80 | ((val & 0x3f));
1035 } else {
1036 *buf++ = 0xfc | ((val >> 30) & 0x1);
1037 *buf++ = 0x80 | ((val >> 24) & 0x3f);
1038 *buf++ = 0x80 | ((val >> 18) & 0x3f);
1039 *buf++ = 0x80 | ((val >> 12) & 0x3f);
1040 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1041 *buf++ = 0x80 | ((val & 0x3f));
1042 }
1043 } else {
1044 /*
1045 * Ugly, limited parsing of &amp; &gt; &lt;
1046 */
1047 char *sc = (char *)memchr(p, 0x3b, len > 5 ? 5 : len);
1048 if(!sc) goto want_more;
1049 if((sc - p) == 4
1050 && p[1] == 0x61 /* 'a' */
1051 && p[2] == 0x6d /* 'm' */
1052 && p[3] == 0x70 /* 'p' */) {
1053 *buf++ = 0x26;
1054 p = sc;
1055 continue;
1056 }
1057 if((sc - p) == 3) {
1058 if(p[1] == 0x6c) {
1059 *buf = 0x3c; /* '<' */
1060 } else if(p[1] == 0x67) {
1061 *buf = 0x3e; /* '>' */
1062 } else {
1063 /* Unsupported entity reference */
1064 *buf++ = ch;
1065 continue;
1066 }
1067 if(p[2] != 0x74) {
1068 /* Unsupported entity reference */
1069 *buf++ = ch;
1070 continue;
1071 }
1072 buf++;
1073 p = sc;
1074 continue;
1075 }
1076 /* Unsupported entity reference */
1077 *buf++ = ch;
1078 }
1079
1080 continue;
1081 want_more:
1082 if(have_more) {
1083 /*
1084 * We know that no more data (of the same type)
1085 * is coming. Copy the rest verbatim.
1086 */
1087 *buf++ = ch;
1088 continue;
1089 }
Lev Walkin0fab1a62005-03-09 22:19:25 +00001090 chunk_size = (p - (const char *)chunk_buf);
Lev Walkindc06f6b2004-10-20 15:50:55 +00001091 /* Processing stalled: need more data */
Lev Walkinbdaae772005-02-18 12:25:47 +00001092 break;
Lev Walkindc06f6b2004-10-20 15:50:55 +00001093 }
1094
1095 st->size = buf - st->buf;
1096 assert(st->size <= _ns);
1097 st->buf[st->size] = 0; /* Courtesy termination */
1098
1099 return chunk_size; /* Converted in full */
1100}
1101
1102/*
1103 * Decode OCTET STRING from the XML element's body.
1104 */
1105static asn_dec_rval_t
1106OCTET_STRING__decode_xer(asn_codec_ctx_t *opt_codec_ctx,
1107 asn_TYPE_descriptor_t *td, void **sptr,
Lev Walkin8c3b8542005-03-10 18:52:02 +00001108 const char *opt_mname, const void *buf_ptr, size_t size,
Lev Walkindc06f6b2004-10-20 15:50:55 +00001109 int (*opt_unexpected_tag_decoder)
Lev Walkin0fab1a62005-03-09 22:19:25 +00001110 (void *struct_ptr, const void *chunk_buf, size_t chunk_size),
Lev Walkindc06f6b2004-10-20 15:50:55 +00001111 ssize_t (*body_receiver)
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 int have_more)
1114) {
Lev Walkind5125642005-02-14 20:08:00 +00001115 OCTET_STRING_t *st = (OCTET_STRING_t *)*sptr;
Lev Walkindc06f6b2004-10-20 15:50:55 +00001116 asn_OCTET_STRING_specifics_t *specs = td->specifics
1117 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +08001118 : &asn_SPC_OCTET_STRING_specs;
Lev Walkindc06f6b2004-10-20 15:50:55 +00001119 const char *xml_tag = opt_mname ? opt_mname : td->xml_tag;
1120 asn_struct_ctx_t *ctx; /* Per-structure parser context */
Lev Walkinc61f3862005-02-14 17:21:22 +00001121 asn_dec_rval_t rval; /* Return value from the decoder */
1122 int st_allocated;
Lev Walkindc06f6b2004-10-20 15:50:55 +00001123
1124 /*
1125 * Create the string if does not exist.
1126 */
Lev Walkinc61f3862005-02-14 17:21:22 +00001127 if(!st) {
Lev Walkin8484ed82004-12-14 13:31:01 +00001128 st = (OCTET_STRING_t *)CALLOC(1, specs->struct_size);
Lev Walkin8484ed82004-12-14 13:31:01 +00001129 *sptr = (void *)st;
Lev Walkinc61f3862005-02-14 17:21:22 +00001130 if(!st) goto sta_failed;
1131 st_allocated = 1;
Lev Walkinbdaae772005-02-18 12:25:47 +00001132 } else {
1133 st_allocated = 0;
1134 }
Lev Walkinc61f3862005-02-14 17:21:22 +00001135 if(!st->buf) {
1136 /* This is separate from above section */
1137 st->buf = (uint8_t *)CALLOC(1, 1);
1138 if(!st->buf) {
1139 if(st_allocated) {
1140 *sptr = 0;
1141 goto stb_failed;
1142 } else {
1143 goto sta_failed;
1144 }
Lev Walkindc06f6b2004-10-20 15:50:55 +00001145 }
1146 }
1147
1148 /* Restore parsing context */
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001149 ctx = (asn_struct_ctx_t *)(((char *)*sptr) + specs->ctx_offset);
Lev Walkindc06f6b2004-10-20 15:50:55 +00001150
1151 return xer_decode_general(opt_codec_ctx, ctx, *sptr, xml_tag,
1152 buf_ptr, size, opt_unexpected_tag_decoder, body_receiver);
Lev Walkinc61f3862005-02-14 17:21:22 +00001153
1154stb_failed:
1155 FREEMEM(st);
1156sta_failed:
1157 rval.code = RC_FAIL;
1158 rval.consumed = 0;
1159 return rval;
Lev Walkindc06f6b2004-10-20 15:50:55 +00001160}
1161
1162/*
1163 * Decode OCTET STRING from the hexadecimal data.
1164 */
1165asn_dec_rval_t
1166OCTET_STRING_decode_xer_hex(asn_codec_ctx_t *opt_codec_ctx,
1167 asn_TYPE_descriptor_t *td, void **sptr,
Lev Walkin8c3b8542005-03-10 18:52:02 +00001168 const char *opt_mname, const void *buf_ptr, size_t size) {
Lev Walkindc06f6b2004-10-20 15:50:55 +00001169 return OCTET_STRING__decode_xer(opt_codec_ctx, td, sptr, opt_mname,
1170 buf_ptr, size, 0, OCTET_STRING__convert_hexadecimal);
1171}
1172
1173/*
1174 * Decode OCTET STRING from the binary (0/1) data.
1175 */
1176asn_dec_rval_t
1177OCTET_STRING_decode_xer_binary(asn_codec_ctx_t *opt_codec_ctx,
1178 asn_TYPE_descriptor_t *td, void **sptr,
Lev Walkin8c3b8542005-03-10 18:52:02 +00001179 const char *opt_mname, const void *buf_ptr, size_t size) {
Lev Walkindc06f6b2004-10-20 15:50:55 +00001180 return OCTET_STRING__decode_xer(opt_codec_ctx, td, sptr, opt_mname,
1181 buf_ptr, size, 0, OCTET_STRING__convert_binary);
1182}
1183
1184/*
1185 * Decode OCTET STRING from the string (ASCII/UTF-8) data.
1186 */
1187asn_dec_rval_t
1188OCTET_STRING_decode_xer_utf8(asn_codec_ctx_t *opt_codec_ctx,
1189 asn_TYPE_descriptor_t *td, void **sptr,
Lev Walkin8c3b8542005-03-10 18:52:02 +00001190 const char *opt_mname, const void *buf_ptr, size_t size) {
Lev Walkindc06f6b2004-10-20 15:50:55 +00001191 return OCTET_STRING__decode_xer(opt_codec_ctx, td, sptr, opt_mname,
1192 buf_ptr, size,
1193 OCTET_STRING__handle_control_chars,
1194 OCTET_STRING__convert_entrefs);
1195}
1196
Lev Walkin725883b2006-10-09 12:07:58 +00001197static int
Lev Walkin3a4689a2006-11-24 11:20:27 +00001198OCTET_STRING_per_get_characters(asn_per_data_t *po, uint8_t *buf,
1199 size_t units, unsigned int bpc, unsigned int unit_bits,
1200 long lb, long ub, asn_per_constraints_t *pc) {
1201 uint8_t *end = buf + units * bpc;
Lev Walkin725883b2006-10-09 12:07:58 +00001202
1203 ASN_DEBUG("Expanding %d characters into (%ld..%ld):%d",
Lev Walkin3a4689a2006-11-24 11:20:27 +00001204 (int)units, lb, ub, unit_bits);
Lev Walkin725883b2006-10-09 12:07:58 +00001205
1206 /* X.691: 27.5.4 */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001207 if((unsigned long)ub <= ((unsigned long)2 << (unit_bits - 1))) {
Lev Walkin725883b2006-10-09 12:07:58 +00001208 /* Decode without translation */
1209 lb = 0;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001210 } else if(pc && pc->code2value) {
1211 if(unit_bits > 16)
1212 return 1; /* FATAL: can't have constrained
1213 * UniversalString with more than
1214 * 16 million code points */
1215 for(; buf < end; buf += bpc) {
Lev Walkin725883b2006-10-09 12:07:58 +00001216 int value;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001217 int code = per_get_few_bits(po, unit_bits);
Lev Walkin725883b2006-10-09 12:07:58 +00001218 if(code < 0) return -1; /* WMORE */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001219 value = pc->code2value(code);
Lev Walkin725883b2006-10-09 12:07:58 +00001220 if(value < 0) {
1221 ASN_DEBUG("Code %d (0x%02x) is"
1222 " not in map (%ld..%ld)",
1223 code, code, lb, ub);
1224 return 1; /* FATAL */
1225 }
Lev Walkin3a4689a2006-11-24 11:20:27 +00001226 switch(bpc) {
1227 case 1: *buf = value; break;
1228 case 2: buf[0] = value >> 8; buf[1] = value; break;
1229 case 4: buf[0] = value >> 24; buf[1] = value >> 16;
1230 buf[2] = value >> 8; buf[3] = value; break;
1231 }
Lev Walkin725883b2006-10-09 12:07:58 +00001232 }
1233 return 0;
1234 }
1235
Lev Walkin3a4689a2006-11-24 11:20:27 +00001236 /* Shortcut the no-op copying to the aligned structure */
1237 if(lb == 0 && (unit_bits == 8 * bpc)) {
1238 return per_get_many_bits(po, buf, 0, unit_bits * units);
1239 }
1240
1241 for(; buf < end; buf += bpc) {
Lev Walkin725883b2006-10-09 12:07:58 +00001242 int code = per_get_few_bits(po, unit_bits);
1243 int ch = code + lb;
1244 if(code < 0) return -1; /* WMORE */
1245 if(ch > ub) {
1246 ASN_DEBUG("Code %d is out of range (%ld..%ld)",
1247 ch, lb, ub);
1248 return 1; /* FATAL */
1249 }
Lev Walkin3a4689a2006-11-24 11:20:27 +00001250 switch(bpc) {
1251 case 1: *buf = ch; break;
1252 case 2: buf[0] = ch >> 8; buf[1] = ch; break;
1253 case 4: buf[0] = ch >> 24; buf[1] = ch >> 16;
1254 buf[2] = ch >> 8; buf[3] = ch; break;
1255 }
Lev Walkin725883b2006-10-09 12:07:58 +00001256 }
1257
1258 return 0;
1259}
1260
1261static int
Lev Walkin3a4689a2006-11-24 11:20:27 +00001262OCTET_STRING_per_put_characters(asn_per_outp_t *po, const uint8_t *buf,
1263 size_t units, unsigned int bpc, unsigned int unit_bits,
1264 long lb, long ub, asn_per_constraints_t *pc) {
1265 const uint8_t *end = buf + units * bpc;
Lev Walkin725883b2006-10-09 12:07:58 +00001266
Lev Walkin3a4689a2006-11-24 11:20:27 +00001267 ASN_DEBUG("Squeezing %d characters into (%ld..%ld):%d (%d bpc)",
1268 (int)units, lb, ub, unit_bits, bpc);
Lev Walkin725883b2006-10-09 12:07:58 +00001269
1270 /* X.691: 27.5.4 */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001271 if((unsigned long)ub <= ((unsigned long)2 << (unit_bits - 1))) {
Lev Walkin725883b2006-10-09 12:07:58 +00001272 /* Encode as is */
1273 lb = 0;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001274 } else if(pc && pc->value2code) {
1275 for(; buf < end; buf += bpc) {
1276 int code;
1277 uint32_t value;
1278 switch(bpc) {
1279 case 1: value = *(const uint8_t *)buf; break;
1280 case 2: value = (buf[0] << 8) | buf[1]; break;
1281 case 4: value = (buf[0] << 24) | (buf[1] << 16)
1282 | (buf[2] << 8) | buf[3]; break;
1283 default: return -1;
1284 }
1285 code = pc->value2code(value);
Lev Walkin725883b2006-10-09 12:07:58 +00001286 if(code < 0) {
1287 ASN_DEBUG("Character %d (0x%02x) is"
1288 " not in map (%ld..%ld)",
1289 *buf, *buf, lb, ub);
1290 return -1;
1291 }
1292 if(per_put_few_bits(po, code, unit_bits))
1293 return -1;
1294 }
1295 }
1296
Lev Walkin3a4689a2006-11-24 11:20:27 +00001297 /* Shortcut the no-op copying to the aligned structure */
1298 if(lb == 0 && (unit_bits == 8 * bpc)) {
1299 return per_put_many_bits(po, buf, unit_bits * units);
1300 }
1301
1302 for(ub -= lb; buf < end; buf += bpc) {
1303 int ch;
1304 uint32_t value;
1305 switch(bpc) {
1306 case 1: value = *(const uint8_t *)buf; break;
1307 case 2: value = (buf[0] << 8) | buf[1]; break;
1308 case 4: value = (buf[0] << 24) | (buf[1] << 16)
1309 | (buf[2] << 8) | buf[3]; break;
1310 default: return -1;
1311 }
1312 ch = value - lb;
Lev Walkin725883b2006-10-09 12:07:58 +00001313 if(ch < 0 || ch > ub) {
1314 ASN_DEBUG("Character %d (0x%02x)"
1315 " is out of range (%ld..%ld)",
1316 *buf, *buf, lb, ub + lb);
1317 return -1;
1318 }
1319 if(per_put_few_bits(po, ch, unit_bits))
1320 return -1;
1321 }
1322
1323 return 0;
1324}
1325
Lev Walkin59b176e2005-11-26 11:25:14 +00001326asn_dec_rval_t
1327OCTET_STRING_decode_uper(asn_codec_ctx_t *opt_codec_ctx,
1328 asn_TYPE_descriptor_t *td, asn_per_constraints_t *constraints,
1329 void **sptr, asn_per_data_t *pd) {
1330
1331 asn_OCTET_STRING_specifics_t *specs = td->specifics
1332 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +08001333 : &asn_SPC_OCTET_STRING_specs;
Lev Walkin725883b2006-10-09 12:07:58 +00001334 asn_per_constraints_t *pc = constraints ? constraints
1335 : td->per_constraints;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001336 asn_per_constraint_t *cval;
1337 asn_per_constraint_t *csiz;
Lev Walkin59b176e2005-11-26 11:25:14 +00001338 asn_dec_rval_t rval = { RC_OK, 0 };
1339 BIT_STRING_t *st = (BIT_STRING_t *)*sptr;
1340 ssize_t consumed_myself = 0;
1341 int repeat;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001342 enum {
1343 OS__BPC_BIT = 0,
1344 OS__BPC_CHAR = 1,
1345 OS__BPC_U16 = 2,
1346 OS__BPC_U32 = 4
1347 } bpc; /* Bytes per character */
1348 unsigned int unit_bits;
1349 unsigned int canonical_unit_bits;
Lev Walkin59b176e2005-11-26 11:25:14 +00001350
1351 (void)opt_codec_ctx;
1352
Lev Walkin3a4689a2006-11-24 11:20:27 +00001353 if(pc) {
1354 cval = &pc->value;
1355 csiz = &pc->size;
1356 } else {
1357 cval = &asn_DEF_OCTET_STRING_constraints.value;
1358 csiz = &asn_DEF_OCTET_STRING_constraints.size;
1359 }
1360
1361 switch(specs->subvariant) {
1362 default:
1363 case ASN_OSUBV_ANY:
1364 ASN_DEBUG("Unrecognized subvariant %d", specs->subvariant);
1365 RETURN(RC_FAIL);
1366 case ASN_OSUBV_BIT:
1367 canonical_unit_bits = unit_bits = 1;
1368 bpc = OS__BPC_BIT;
1369 break;
1370 case ASN_OSUBV_STR:
1371 canonical_unit_bits = unit_bits = 8;
1372 if(cval->flags & APC_CONSTRAINED)
1373 unit_bits = cval->range_bits;
1374 bpc = OS__BPC_CHAR;
1375 break;
1376 case ASN_OSUBV_U16:
1377 canonical_unit_bits = unit_bits = 16;
1378 if(cval->flags & APC_CONSTRAINED)
1379 unit_bits = cval->range_bits;
1380 bpc = OS__BPC_U16;
1381 break;
1382 case ASN_OSUBV_U32:
1383 canonical_unit_bits = unit_bits = 32;
1384 if(cval->flags & APC_CONSTRAINED)
1385 unit_bits = cval->range_bits;
1386 bpc = OS__BPC_U32;
1387 break;
1388 }
1389
Lev Walkin59b176e2005-11-26 11:25:14 +00001390 /*
1391 * Allocate the string.
1392 */
1393 if(!st) {
1394 st = (BIT_STRING_t *)(*sptr = CALLOC(1, specs->struct_size));
1395 if(!st) RETURN(RC_FAIL);
1396 }
1397
Lev Walkin725883b2006-10-09 12:07:58 +00001398 ASN_DEBUG("PER Decoding %s size %ld .. %ld bits %d",
Lev Walkin3a4689a2006-11-24 11:20:27 +00001399 csiz->flags & APC_EXTENSIBLE ? "extensible" : "non-extensible",
1400 csiz->lower_bound, csiz->upper_bound, csiz->effective_bits);
Lev Walkince676dd2005-12-20 22:34:55 +00001401
Lev Walkin3a4689a2006-11-24 11:20:27 +00001402 if(csiz->flags & APC_EXTENSIBLE) {
Lev Walkin59b176e2005-11-26 11:25:14 +00001403 int inext = per_get_few_bits(pd, 1);
Lev Walkin0a8aa602006-09-18 20:05:55 +00001404 if(inext < 0) RETURN(RC_WMORE);
Lev Walkin3a4689a2006-11-24 11:20:27 +00001405 if(inext) {
1406 csiz = &asn_DEF_OCTET_STRING_constraints.size;
1407 cval = &asn_DEF_OCTET_STRING_constraints.value;
1408 unit_bits = canonical_unit_bits;
1409 }
Lev Walkin59b176e2005-11-26 11:25:14 +00001410 }
1411
Lev Walkin3a4689a2006-11-24 11:20:27 +00001412 if(csiz->effective_bits >= 0) {
Lev Walkin59b176e2005-11-26 11:25:14 +00001413 FREEMEM(st->buf);
Lev Walkin3a4689a2006-11-24 11:20:27 +00001414 if(bpc) {
1415 st->size = csiz->upper_bound * bpc;
Lev Walkin59b176e2005-11-26 11:25:14 +00001416 } else {
Lev Walkin3a4689a2006-11-24 11:20:27 +00001417 st->size = (csiz->upper_bound + 7) >> 3;
Lev Walkin59b176e2005-11-26 11:25:14 +00001418 }
1419 st->buf = (uint8_t *)MALLOC(st->size + 1);
1420 if(!st->buf) { st->size = 0; RETURN(RC_FAIL); }
1421 }
1422
1423 /* X.691, #16.5: zero-length encoding */
1424 /* X.691, #16.6: short fixed length encoding (up to 2 octets) */
1425 /* X.691, #16.7: long fixed length encoding (up to 64K octets) */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001426 if(csiz->effective_bits == 0) {
Lev Walkin725883b2006-10-09 12:07:58 +00001427 int ret;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001428 if(bpc) {
1429 ASN_DEBUG("Encoding OCTET STRING size %ld",
1430 csiz->upper_bound);
1431 ret = OCTET_STRING_per_get_characters(pd, st->buf,
1432 csiz->upper_bound, bpc, unit_bits,
1433 cval->lower_bound, cval->upper_bound, pc);
Lev Walkin725883b2006-10-09 12:07:58 +00001434 if(ret > 0) RETURN(RC_FAIL);
1435 } else {
Lev Walkin3a4689a2006-11-24 11:20:27 +00001436 ASN_DEBUG("Encoding BIT STRING size %ld",
1437 csiz->upper_bound);
Lev Walkin725883b2006-10-09 12:07:58 +00001438 ret = per_get_many_bits(pd, st->buf, 0,
Lev Walkin3a4689a2006-11-24 11:20:27 +00001439 unit_bits * csiz->upper_bound);
Lev Walkin725883b2006-10-09 12:07:58 +00001440 }
Lev Walkin0a8aa602006-09-18 20:05:55 +00001441 if(ret < 0) RETURN(RC_WMORE);
Lev Walkin3a4689a2006-11-24 11:20:27 +00001442 consumed_myself += unit_bits * csiz->upper_bound;
Lev Walkin59b176e2005-11-26 11:25:14 +00001443 st->buf[st->size] = 0;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001444 if(bpc == 0) {
1445 int ubs = (csiz->upper_bound & 0x7);
1446 st->bits_unused = ubs ? 8 - ubs : 0;
1447 }
Lev Walkin59b176e2005-11-26 11:25:14 +00001448 RETURN(RC_OK);
1449 }
1450
1451 st->size = 0;
1452 do {
Lev Walkin3a4689a2006-11-24 11:20:27 +00001453 ssize_t raw_len;
Lev Walkin59b176e2005-11-26 11:25:14 +00001454 ssize_t len_bytes;
1455 ssize_t len_bits;
1456 void *p;
1457 int ret;
1458
1459 /* Get the PER length */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001460 raw_len = uper_get_length(pd, csiz->effective_bits, &repeat);
1461 if(raw_len < 0) RETURN(RC_WMORE);
1462 raw_len += csiz->lower_bound;
Lev Walkin59b176e2005-11-26 11:25:14 +00001463
Lev Walkin0a8aa602006-09-18 20:05:55 +00001464 ASN_DEBUG("Got PER length eb %ld, len %ld, %s (%s)",
Lev Walkin3a4689a2006-11-24 11:20:27 +00001465 (long)csiz->effective_bits, (long)raw_len,
Lev Walkin0a8aa602006-09-18 20:05:55 +00001466 repeat ? "repeat" : "once", td->name);
Lev Walkin3a4689a2006-11-24 11:20:27 +00001467 if(bpc) {
1468 len_bytes = raw_len * bpc;
1469 len_bits = len_bytes * unit_bits;
1470 } else {
1471 len_bits = raw_len;
Lev Walkin59b176e2005-11-26 11:25:14 +00001472 len_bytes = (len_bits + 7) >> 3;
1473 if(len_bits & 0x7)
1474 st->bits_unused = 8 - (len_bits & 0x7);
1475 /* len_bits be multiple of 16K if repeat is set */
Lev Walkin59b176e2005-11-26 11:25:14 +00001476 }
1477 p = REALLOC(st->buf, st->size + len_bytes + 1);
1478 if(!p) RETURN(RC_FAIL);
1479 st->buf = (uint8_t *)p;
1480
Lev Walkin3a4689a2006-11-24 11:20:27 +00001481 if(bpc) {
1482 ret = OCTET_STRING_per_get_characters(pd,
1483 &st->buf[st->size], raw_len, bpc, unit_bits,
1484 cval->lower_bound, cval->upper_bound, pc);
Lev Walkin725883b2006-10-09 12:07:58 +00001485 if(ret > 0) RETURN(RC_FAIL);
1486 } else {
1487 ret = per_get_many_bits(pd, &st->buf[st->size],
1488 0, len_bits);
1489 }
Lev Walkin0a8aa602006-09-18 20:05:55 +00001490 if(ret < 0) RETURN(RC_WMORE);
Lev Walkin59b176e2005-11-26 11:25:14 +00001491 st->size += len_bytes;
1492 } while(repeat);
1493 st->buf[st->size] = 0; /* nul-terminate */
1494
1495 return rval;
1496}
Lev Walkindc06f6b2004-10-20 15:50:55 +00001497
Lev Walkin523de9e2006-08-18 01:34:18 +00001498asn_enc_rval_t
1499OCTET_STRING_encode_uper(asn_TYPE_descriptor_t *td,
1500 asn_per_constraints_t *constraints, void *sptr, asn_per_outp_t *po) {
1501
1502 asn_OCTET_STRING_specifics_t *specs = td->specifics
1503 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +08001504 : &asn_SPC_OCTET_STRING_specs;
Lev Walkin725883b2006-10-09 12:07:58 +00001505 asn_per_constraints_t *pc = constraints ? constraints
1506 : td->per_constraints;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001507 asn_per_constraint_t *cval;
1508 asn_per_constraint_t *csiz;
Lev Walkin523de9e2006-08-18 01:34:18 +00001509 const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001510 asn_enc_rval_t er = { 0, 0, 0 };
Lev Walkin523de9e2006-08-18 01:34:18 +00001511 int inext = 0; /* Lies not within extension root */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001512 unsigned int unit_bits;
1513 unsigned int canonical_unit_bits;
1514 unsigned int sizeinunits;
Lev Walkin523de9e2006-08-18 01:34:18 +00001515 const uint8_t *buf;
1516 int ret;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001517 enum {
1518 OS__BPC_BIT = 0,
1519 OS__BPC_CHAR = 1,
1520 OS__BPC_U16 = 2,
1521 OS__BPC_U32 = 4
1522 } bpc; /* Bytes per character */
1523 int ct_extensible;
Lev Walkin523de9e2006-08-18 01:34:18 +00001524
Lev Walkin2ddd3082006-10-16 12:32:14 +00001525 if(!st || (!st->buf && st->size))
Lev Walkin7c1dc052016-03-14 03:08:15 -07001526 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001527
Lev Walkin3a4689a2006-11-24 11:20:27 +00001528 if(pc) {
1529 cval = &pc->value;
1530 csiz = &pc->size;
1531 } else {
1532 cval = &asn_DEF_OCTET_STRING_constraints.value;
1533 csiz = &asn_DEF_OCTET_STRING_constraints.size;
1534 }
1535 ct_extensible = csiz->flags & APC_EXTENSIBLE;
1536
1537 switch(specs->subvariant) {
1538 default:
1539 case ASN_OSUBV_ANY:
Lev Walkin7c1dc052016-03-14 03:08:15 -07001540 ASN__ENCODE_FAILED;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001541 case ASN_OSUBV_BIT:
1542 canonical_unit_bits = unit_bits = 1;
1543 bpc = OS__BPC_BIT;
1544 sizeinunits = st->size * 8 - (st->bits_unused & 0x07);
Lev Walkin523de9e2006-08-18 01:34:18 +00001545 ASN_DEBUG("BIT STRING of %d bytes, %d bits unused",
1546 sizeinunits, st->bits_unused);
Lev Walkin3a4689a2006-11-24 11:20:27 +00001547 break;
1548 case ASN_OSUBV_STR:
1549 canonical_unit_bits = unit_bits = 8;
1550 if(cval->flags & APC_CONSTRAINED)
1551 unit_bits = cval->range_bits;
1552 bpc = OS__BPC_CHAR;
1553 sizeinunits = st->size;
1554 break;
1555 case ASN_OSUBV_U16:
1556 canonical_unit_bits = unit_bits = 16;
1557 if(cval->flags & APC_CONSTRAINED)
1558 unit_bits = cval->range_bits;
1559 bpc = OS__BPC_U16;
1560 sizeinunits = st->size / 2;
1561 break;
1562 case ASN_OSUBV_U32:
1563 canonical_unit_bits = unit_bits = 32;
1564 if(cval->flags & APC_CONSTRAINED)
1565 unit_bits = cval->range_bits;
1566 bpc = OS__BPC_U32;
1567 sizeinunits = st->size / 4;
1568 break;
Lev Walkin725883b2006-10-09 12:07:58 +00001569 }
1570
Lev Walkin00918812006-09-18 21:19:32 +00001571 ASN_DEBUG("Encoding %s into %d units of %d bits"
Lev Walkin725883b2006-10-09 12:07:58 +00001572 " (%ld..%ld, effective %d)%s",
Lev Walkin00918812006-09-18 21:19:32 +00001573 td->name, sizeinunits, unit_bits,
Lev Walkin3a4689a2006-11-24 11:20:27 +00001574 csiz->lower_bound, csiz->upper_bound,
1575 csiz->effective_bits, ct_extensible ? " EXT" : "");
Lev Walkin523de9e2006-08-18 01:34:18 +00001576
Lev Walkind4c16732013-03-28 05:00:47 -07001577 /* Figure out whether size lies within PER visible constraint */
Lev Walkin523de9e2006-08-18 01:34:18 +00001578
Lev Walkin3a4689a2006-11-24 11:20:27 +00001579 if(csiz->effective_bits >= 0) {
1580 if((int)sizeinunits < csiz->lower_bound
1581 || (int)sizeinunits > csiz->upper_bound) {
Lev Walkin523de9e2006-08-18 01:34:18 +00001582 if(ct_extensible) {
Lev Walkin3a4689a2006-11-24 11:20:27 +00001583 cval = &asn_DEF_OCTET_STRING_constraints.value;
1584 csiz = &asn_DEF_OCTET_STRING_constraints.size;
1585 unit_bits = canonical_unit_bits;
Lev Walkin523de9e2006-08-18 01:34:18 +00001586 inext = 1;
1587 } else
Lev Walkin7c1dc052016-03-14 03:08:15 -07001588 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001589 }
1590 } else {
1591 inext = 0;
1592 }
1593
1594 if(ct_extensible) {
1595 /* Declare whether length is [not] within extension root */
1596 if(per_put_few_bits(po, inext, 1))
Lev Walkin7c1dc052016-03-14 03:08:15 -07001597 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001598 }
1599
1600 /* X.691, #16.5: zero-length encoding */
1601 /* X.691, #16.6: short fixed length encoding (up to 2 octets) */
1602 /* X.691, #16.7: long fixed length encoding (up to 64K octets) */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001603 if(csiz->effective_bits >= 0) {
Lev Walkin523de9e2006-08-18 01:34:18 +00001604 ASN_DEBUG("Encoding %d bytes (%ld), length in %d bits",
Lev Walkin3a4689a2006-11-24 11:20:27 +00001605 st->size, sizeinunits - csiz->lower_bound,
1606 csiz->effective_bits);
1607 ret = per_put_few_bits(po, sizeinunits - csiz->lower_bound,
1608 csiz->effective_bits);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001609 if(ret) ASN__ENCODE_FAILED;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001610 if(bpc) {
1611 ret = OCTET_STRING_per_put_characters(po, st->buf,
1612 sizeinunits, bpc, unit_bits,
1613 cval->lower_bound, cval->upper_bound, pc);
Lev Walkin725883b2006-10-09 12:07:58 +00001614 } else {
1615 ret = per_put_many_bits(po, st->buf,
1616 sizeinunits * unit_bits);
1617 }
Lev Walkin7c1dc052016-03-14 03:08:15 -07001618 if(ret) ASN__ENCODE_FAILED;
1619 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +00001620 }
1621
1622 ASN_DEBUG("Encoding %d bytes", st->size);
1623
1624 if(sizeinunits == 0) {
1625 if(uper_put_length(po, 0))
Lev Walkin7c1dc052016-03-14 03:08:15 -07001626 ASN__ENCODE_FAILED;
1627 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +00001628 }
1629
1630 buf = st->buf;
1631 while(sizeinunits) {
1632 ssize_t maySave = uper_put_length(po, sizeinunits);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001633 if(maySave < 0) ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001634
Lev Walkin345e4c62006-10-19 02:44:08 +00001635 ASN_DEBUG("Encoding %ld of %ld",
1636 (long)maySave, (long)sizeinunits);
Lev Walkin523de9e2006-08-18 01:34:18 +00001637
Lev Walkin3a4689a2006-11-24 11:20:27 +00001638 if(bpc) {
1639 ret = OCTET_STRING_per_put_characters(po, buf,
1640 maySave, bpc, unit_bits,
1641 cval->lower_bound, cval->upper_bound, pc);
Lev Walkin725883b2006-10-09 12:07:58 +00001642 } else {
1643 ret = per_put_many_bits(po, buf, maySave * unit_bits);
1644 }
Lev Walkin7c1dc052016-03-14 03:08:15 -07001645 if(ret) ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001646
Lev Walkin3a4689a2006-11-24 11:20:27 +00001647 if(bpc)
1648 buf += maySave * bpc;
Lev Walkin523de9e2006-08-18 01:34:18 +00001649 else
Lev Walkin3a4689a2006-11-24 11:20:27 +00001650 buf += maySave >> 3;
Lev Walkin523de9e2006-08-18 01:34:18 +00001651 sizeinunits -= maySave;
1652 assert(!(maySave & 0x07) || !sizeinunits);
1653 }
1654
Lev Walkin7c1dc052016-03-14 03:08:15 -07001655 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +00001656}
1657
Lev Walkinf15320b2004-06-03 03:38:44 +00001658int
Lev Walkinde4825d2004-09-29 13:20:14 +00001659OCTET_STRING_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +00001660 asn_app_consume_bytes_f *cb, void *app_key) {
Wim Lewis59e8d282014-08-04 12:39:35 -07001661 const char * const h2c = "0123456789ABCDEF";
Lev Walkinc2346572004-08-11 09:07:36 +00001662 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +00001663 char scratch[16 * 3 + 4];
1664 char *p = scratch;
1665 uint8_t *buf;
1666 uint8_t *end;
1667 size_t i;
Lev Walkinf15320b2004-06-03 03:38:44 +00001668
Lev Walkind9bd7752004-06-05 08:17:50 +00001669 (void)td; /* Unused argument */
1670
Lev Walkin2ddd3082006-10-16 12:32:14 +00001671 if(!st || (!st->buf && st->size))
1672 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001673
1674 /*
1675 * Dump the contents of the buffer in hexadecimal.
1676 */
1677 buf = st->buf;
1678 end = buf + st->size;
1679 for(i = 0; buf < end; buf++, i++) {
1680 if(!(i % 16) && (i || st->size > 16)) {
Lev Walkin8e8078a2004-09-26 13:10:40 +00001681 if(cb(scratch, p - scratch, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +00001682 return -1;
Lev Walkin8e8078a2004-09-26 13:10:40 +00001683 _i_INDENT(1);
Lev Walkinf15320b2004-06-03 03:38:44 +00001684 p = scratch;
1685 }
1686 *p++ = h2c[(*buf >> 4) & 0x0F];
1687 *p++ = h2c[*buf & 0x0F];
Lev Walkina9cc46e2004-09-22 16:06:28 +00001688 *p++ = 0x20;
Lev Walkinf15320b2004-06-03 03:38:44 +00001689 }
1690
Lev Walkincc6a9102004-09-23 22:06:26 +00001691 if(p > scratch) {
1692 p--; /* Remove the tail space */
Lev Walkin8e8078a2004-09-26 13:10:40 +00001693 if(cb(scratch, p - scratch, app_key) < 0)
Lev Walkincc6a9102004-09-23 22:06:26 +00001694 return -1;
1695 }
1696
1697 return 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001698}
1699
1700int
Lev Walkindc06f6b2004-10-20 15:50:55 +00001701OCTET_STRING_print_utf8(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +00001702 int ilevel, asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +00001703 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +00001704
Lev Walkind9bd7752004-06-05 08:17:50 +00001705 (void)td; /* Unused argument */
1706 (void)ilevel; /* Unused argument */
1707
Lev Walkin2ddd3082006-10-16 12:32:14 +00001708 if(st && (st->buf || !st->size)) {
Lev Walkin8e8078a2004-09-26 13:10:40 +00001709 return (cb(st->buf, st->size, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001710 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +00001711 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001712 }
1713}
1714
1715void
Lev Walkinde4825d2004-09-29 13:20:14 +00001716OCTET_STRING_free(asn_TYPE_descriptor_t *td, void *sptr, int contents_only) {
Lev Walkinc2346572004-08-11 09:07:36 +00001717 OCTET_STRING_t *st = (OCTET_STRING_t *)sptr;
Lev Walkin034ee192016-03-14 02:28:33 -07001718 asn_OCTET_STRING_specifics_t *specs;
1719 asn_struct_ctx_t *ctx;
Lev Walkinb54577a2004-11-08 10:47:12 +00001720 struct _stack *stck;
Lev Walkinf15320b2004-06-03 03:38:44 +00001721
1722 if(!td || !st)
1723 return;
1724
Lev Walkin034ee192016-03-14 02:28:33 -07001725 specs = td->specifics
1726 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +08001727 : &asn_SPC_OCTET_STRING_specs;
Lev Walkin034ee192016-03-14 02:28:33 -07001728 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
1729
Lev Walkinf15320b2004-06-03 03:38:44 +00001730 ASN_DEBUG("Freeing %s as OCTET STRING", td->name);
1731
1732 if(st->buf) {
1733 FREEMEM(st->buf);
Lev Walkin2ddd3082006-10-16 12:32:14 +00001734 st->buf = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001735 }
1736
1737 /*
1738 * Remove decode-time stack.
1739 */
Lev Walkinb54577a2004-11-08 10:47:12 +00001740 stck = (struct _stack *)ctx->ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +00001741 if(stck) {
1742 while(stck->tail) {
1743 struct _stack_el *sel = stck->tail;
1744 stck->tail = sel->prev;
1745 FREEMEM(sel);
1746 }
1747 FREEMEM(stck);
1748 }
1749
1750 if(!contents_only) {
1751 FREEMEM(st);
1752 }
1753}
1754
1755/*
1756 * Conversion routines.
1757 */
1758int
1759OCTET_STRING_fromBuf(OCTET_STRING_t *st, const char *str, int len) {
1760 void *buf;
1761
1762 if(st == 0 || (str == 0 && len)) {
1763 errno = EINVAL;
1764 return -1;
1765 }
1766
1767 /*
1768 * Clear the OCTET STRING.
1769 */
1770 if(str == NULL) {
Lev Walkin7b284812005-12-17 11:43:25 +00001771 FREEMEM(st->buf);
1772 st->buf = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001773 st->size = 0;
1774 return 0;
1775 }
1776
1777 /* Determine the original string size, if not explicitly given */
1778 if(len < 0)
1779 len = strlen(str);
1780
1781 /* Allocate and fill the memory */
1782 buf = MALLOC(len + 1);
Lev Walkin7b284812005-12-17 11:43:25 +00001783 if(buf == NULL)
Lev Walkinf15320b2004-06-03 03:38:44 +00001784 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +00001785
1786 memcpy(buf, str, len);
Lev Walkin7b284812005-12-17 11:43:25 +00001787 ((uint8_t *)buf)[len] = '\0'; /* Couldn't use memcpy(len+1)! */
1788 FREEMEM(st->buf);
1789 st->buf = (uint8_t *)buf;
1790 st->size = len;
Lev Walkinf15320b2004-06-03 03:38:44 +00001791
1792 return 0;
1793}
1794
1795OCTET_STRING_t *
Lev Walkinbbd93252004-10-12 05:57:23 +00001796OCTET_STRING_new_fromBuf(asn_TYPE_descriptor_t *td, const char *str, int len) {
1797 asn_OCTET_STRING_specifics_t *specs = td->specifics
Lev Walkindc06f6b2004-10-20 15:50:55 +00001798 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +08001799 : &asn_SPC_OCTET_STRING_specs;
Lev Walkinf15320b2004-06-03 03:38:44 +00001800 OCTET_STRING_t *st;
1801
Lev Walkinbbd93252004-10-12 05:57:23 +00001802 st = (OCTET_STRING_t *)CALLOC(1, specs->struct_size);
Lev Walkinf15320b2004-06-03 03:38:44 +00001803 if(st && str && OCTET_STRING_fromBuf(st, str, len)) {
Lev Walkin419f6752006-09-13 04:02:00 +00001804 FREEMEM(st);
Lev Walkinf15320b2004-06-03 03:38:44 +00001805 st = NULL;
1806 }
1807
1808 return st;
1809}
1810