blob: 6403107863101d581608f01afbfae7bfd1dab786 [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 Walkin59b176e2005-11-26 11:25:14 +000037 OCTET_STRING_decode_uper, /* Unaligned PER decoder */
Lev Walkin523de9e2006-08-18 01:34:18 +000038 OCTET_STRING_encode_uper, /* Unaligned PER encoder */
Lev Walkinf15320b2004-06-03 03:38:44 +000039 0, /* Use generic outmost tag fetcher */
Lev Walkinde4825d2004-09-29 13:20:14 +000040 asn_DEF_OCTET_STRING_tags,
41 sizeof(asn_DEF_OCTET_STRING_tags)
42 / sizeof(asn_DEF_OCTET_STRING_tags[0]),
43 asn_DEF_OCTET_STRING_tags, /* Same as above */
44 sizeof(asn_DEF_OCTET_STRING_tags)
45 / sizeof(asn_DEF_OCTET_STRING_tags[0]),
Lev Walkin59b176e2005-11-26 11:25:14 +000046 0, /* No PER visible constraints */
Lev Walkin449f8322004-08-20 13:23:42 +000047 0, 0, /* No members */
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +080048 &asn_SPC_OCTET_STRING_specs
Lev Walkinf15320b2004-06-03 03:38:44 +000049};
50
Lev Walkincc6a9102004-09-23 22:06:26 +000051#undef _CH_PHASE
52#undef NEXT_PHASE
53#undef PREV_PHASE
Lev Walkin75b1bef2005-04-25 19:38:21 +000054#define _CH_PHASE(ctx, inc) do { \
55 if(ctx->phase == 0) \
56 ctx->context = 0; \
57 ctx->phase += inc; \
Lev Walkinf15320b2004-06-03 03:38:44 +000058 } while(0)
59#define NEXT_PHASE(ctx) _CH_PHASE(ctx, +1)
60#define PREV_PHASE(ctx) _CH_PHASE(ctx, -1)
61
Lev Walkincc6a9102004-09-23 22:06:26 +000062#undef ADVANCE
Lev Walkin75b1bef2005-04-25 19:38:21 +000063#define ADVANCE(num_bytes) do { \
64 size_t num = (num_bytes); \
65 buf_ptr = ((const char *)buf_ptr) + num; \
66 size -= num; \
67 consumed_myself += num; \
Lev Walkinf15320b2004-06-03 03:38:44 +000068 } while(0)
69
Lev Walkincc6a9102004-09-23 22:06:26 +000070#undef RETURN
Lev Walkin75b1bef2005-04-25 19:38:21 +000071#define RETURN(_code) do { \
Lev Walkin59b176e2005-11-26 11:25:14 +000072 asn_dec_rval_t tmprval; \
73 tmprval.code = _code; \
74 tmprval.consumed = consumed_myself; \
75 return tmprval; \
Lev Walkinf15320b2004-06-03 03:38:44 +000076 } while(0)
77
Lev Walkincc6a9102004-09-23 22:06:26 +000078#undef APPEND
Lev Walkind9bd7752004-06-05 08:17:50 +000079#define APPEND(bufptr, bufsize) do { \
Lev Walkin75b1bef2005-04-25 19:38:21 +000080 size_t _bs = (bufsize); /* Append size */ \
81 size_t _ns = ctx->context; /* Allocated now */ \
82 size_t _es = st->size + _bs; /* Expected size */ \
83 /* int is really a typeof(st->size): */ \
84 if((int)_es < 0) RETURN(RC_FAIL); \
85 if(_ns <= _es) { \
Lev Walkind9bd7752004-06-05 08:17:50 +000086 void *ptr; \
Lev Walkin188ed2c2004-09-13 08:31:01 +000087 /* Be nice and round to the memory allocator */ \
Lev Walkin75b1bef2005-04-25 19:38:21 +000088 do { _ns = _ns ? _ns << 1 : 16; } \
89 while(_ns <= _es); \
90 /* int is really a typeof(st->size): */ \
91 if((int)_ns < 0) RETURN(RC_FAIL); \
Lev Walkind9bd7752004-06-05 08:17:50 +000092 ptr = REALLOC(st->buf, _ns); \
93 if(ptr) { \
Lev Walkinc2346572004-08-11 09:07:36 +000094 st->buf = (uint8_t *)ptr; \
Lev Walkin75b1bef2005-04-25 19:38:21 +000095 ctx->context = _ns; \
Lev Walkind9bd7752004-06-05 08:17:50 +000096 } else { \
97 RETURN(RC_FAIL); \
98 } \
Lev Walkin7f85ef42005-07-02 20:24:27 +000099 ASN_DEBUG("Reallocating into %ld", (long)_ns); \
Lev Walkind9bd7752004-06-05 08:17:50 +0000100 } \
Lev Walkin8d127872004-09-04 04:44:50 +0000101 memcpy(st->buf + st->size, bufptr, _bs); \
Lev Walkind9bd7752004-06-05 08:17:50 +0000102 /* Convenient nul-termination */ \
Lev Walkin75b1bef2005-04-25 19:38:21 +0000103 st->buf[_es] = '\0'; \
104 st->size = _es; \
Lev Walkinf15320b2004-06-03 03:38:44 +0000105 } while(0)
106
107/*
108 * The main reason why ASN.1 is still alive is that too much time and effort
109 * is necessary for learning it more or less adequately, thus creating a gut
110 * necessity to demonstrate that aquired skill everywhere afterwards.
111 * No, I am not going to explain what the following stuff is.
112 */
113struct _stack_el {
Lev Walkin3990ba62004-09-24 20:57:41 +0000114 ber_tlv_len_t left; /* What's left to read (or -1) */
Lev Walkin5c915992004-09-27 20:54:06 +0000115 ber_tlv_len_t got; /* What was actually processed */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000116 int cont_level; /* Depth of subcontainment */
Lev Walkinf15320b2004-06-03 03:38:44 +0000117 int want_nulls; /* Want null "end of content" octets? */
118 int bits_chopped; /* Flag in BIT STRING mode */
Lev Walkin5c915992004-09-27 20:54:06 +0000119 ber_tlv_tag_t tag; /* For debugging purposes */
Lev Walkinf15320b2004-06-03 03:38:44 +0000120 struct _stack_el *prev;
121 struct _stack_el *next;
122};
123struct _stack {
124 struct _stack_el *tail;
125 struct _stack_el *cur_ptr;
126};
127
128static struct _stack_el *
Lev Walkin3990ba62004-09-24 20:57:41 +0000129OS__add_stack_el(struct _stack *st) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000130 struct _stack_el *nel;
131
Lev Walkin188ed2c2004-09-13 08:31:01 +0000132 /*
133 * Reuse the old stack frame or allocate a new one.
134 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000135 if(st->cur_ptr && st->cur_ptr->next) {
136 nel = st->cur_ptr->next;
Lev Walkinf15320b2004-06-03 03:38:44 +0000137 nel->bits_chopped = 0;
Lev Walkin5c915992004-09-27 20:54:06 +0000138 nel->got = 0;
139 /* Retain the nel->cont_level, it's correct. */
Lev Walkinf15320b2004-06-03 03:38:44 +0000140 } else {
Lev Walkin814cca72004-12-15 23:23:53 +0000141 nel = (struct _stack_el *)CALLOC(1, sizeof(struct _stack_el));
Lev Walkinf15320b2004-06-03 03:38:44 +0000142 if(nel == NULL)
143 return NULL;
144
145 if(st->tail) {
Lev Walkin188ed2c2004-09-13 08:31:01 +0000146 /* Increase a subcontainment depth */
147 nel->cont_level = st->tail->cont_level + 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000148 st->tail->next = nel;
149 }
150 nel->prev = st->tail;
151 st->tail = nel;
152 }
153
154 st->cur_ptr = nel;
155
156 return nel;
157}
158
159static struct _stack *
johvik28268d92017-05-09 10:27:52 +0200160_new_stack(void) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000161 return (struct _stack *)CALLOC(1, sizeof(struct _stack));
Lev Walkinf15320b2004-06-03 03:38:44 +0000162}
163
164/*
165 * Decode OCTET STRING type.
166 */
Lev Walkindc06f6b2004-10-20 15:50:55 +0000167asn_dec_rval_t
Lev Walkinde4825d2004-09-29 13:20:14 +0000168OCTET_STRING_decode_ber(asn_codec_ctx_t *opt_codec_ctx,
169 asn_TYPE_descriptor_t *td,
Lev Walkin59b176e2005-11-26 11:25:14 +0000170 void **sptr, const void *buf_ptr, size_t size, int tag_mode) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000171 asn_OCTET_STRING_specifics_t *specs = td->specifics
Lev Walkindc06f6b2004-10-20 15:50:55 +0000172 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +0800173 : &asn_SPC_OCTET_STRING_specs;
Lev Walkin59b176e2005-11-26 11:25:14 +0000174 BIT_STRING_t *st = (BIT_STRING_t *)*sptr;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000175 asn_dec_rval_t rval;
Lev Walkinde4825d2004-09-29 13:20:14 +0000176 asn_struct_ctx_t *ctx;
Lev Walkinf15320b2004-06-03 03:38:44 +0000177 ssize_t consumed_myself = 0;
Lev Walkinbbd93252004-10-12 05:57:23 +0000178 struct _stack *stck; /* Expectations stack structure */
Lev Walkin5c915992004-09-27 20:54:06 +0000179 struct _stack_el *sel = 0; /* Stack element */
Lev Walkinf15320b2004-06-03 03:38:44 +0000180 int tlv_constr;
Lev Walkin3a4689a2006-11-24 11:20:27 +0000181 enum asn_OS_Subvariant type_variant = specs->subvariant;
Lev Walkinf15320b2004-06-03 03:38:44 +0000182
Lev Walkincc6a9102004-09-23 22:06:26 +0000183 ASN_DEBUG("Decoding %s as %s (frame %ld)",
184 td->name,
Lev Walkin3a4689a2006-11-24 11:20:27 +0000185 (type_variant == ASN_OSUBV_STR) ?
Lev Walkinbbd93252004-10-12 05:57:23 +0000186 "OCTET STRING" : "OS-SpecialCase",
Lev Walkincc6a9102004-09-23 22:06:26 +0000187 (long)size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000188
189 /*
190 * Create the string if does not exist.
191 */
192 if(st == NULL) {
Lev Walkin59b176e2005-11-26 11:25:14 +0000193 st = (BIT_STRING_t *)(*sptr = CALLOC(1, specs->struct_size));
194 if(st == NULL) RETURN(RC_FAIL);
Lev Walkinf15320b2004-06-03 03:38:44 +0000195 }
196
197 /* Restore parsing context */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800198 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000199
200 switch(ctx->phase) {
201 case 0:
202 /*
203 * Check tags.
204 */
Lev Walkinde4825d2004-09-29 13:20:14 +0000205 rval = ber_check_tags(opt_codec_ctx, td, ctx,
Lev Walkin8e8078a2004-09-26 13:10:40 +0000206 buf_ptr, size, tag_mode, -1,
Lev Walkinf15320b2004-06-03 03:38:44 +0000207 &ctx->left, &tlv_constr);
Lev Walkin443d2512004-10-05 06:35:31 +0000208 if(rval.code != RC_OK)
209 return rval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000210
Lev Walkinf15320b2004-06-03 03:38:44 +0000211 if(tlv_constr) {
212 /*
213 * Complex operation, requires stack of expectations.
214 */
215 ctx->ptr = _new_stack();
Lev Walkinb02145b2017-06-28 08:52:07 -0700216 if(!ctx->ptr) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000217 RETURN(RC_FAIL);
218 }
219 } else {
220 /*
221 * Jump into stackless primitive decoding.
222 */
223 _CH_PHASE(ctx, 3);
Lev Walkin3a4689a2006-11-24 11:20:27 +0000224 if(type_variant == ASN_OSUBV_ANY && tag_mode != 1)
Lev Walkin188ed2c2004-09-13 08:31:01 +0000225 APPEND(buf_ptr, rval.consumed);
Lev Walkinf15320b2004-06-03 03:38:44 +0000226 ADVANCE(rval.consumed);
227 goto phase3;
228 }
229
Lev Walkinf15320b2004-06-03 03:38:44 +0000230 NEXT_PHASE(ctx);
231 /* Fall through */
232 case 1:
233 phase1:
234 /*
235 * Fill the stack with expectations.
236 */
Lev Walkin814cca72004-12-15 23:23:53 +0000237 stck = (struct _stack *)ctx->ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000238 sel = stck->cur_ptr;
239 do {
240 ber_tlv_tag_t tlv_tag;
241 ber_tlv_len_t tlv_len;
Lev Walkin188ed2c2004-09-13 08:31:01 +0000242 ber_tlv_tag_t expected_tag;
Lev Walkin5c915992004-09-27 20:54:06 +0000243 ssize_t tl, ll, tlvl;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000244 /* This one works even if (sel->left == -1) */
Simo Sorcecd943c22016-07-25 04:39:40 -0400245 size_t Left = ((!sel||(size_t)sel->left >= size)
246 ?size:(size_t)sel->left);
Lev Walkinf15320b2004-06-03 03:38:44 +0000247
Lev Walkin3990ba62004-09-24 20:57:41 +0000248
Lev Walkinabf68892004-10-26 10:12:14 +0000249 ASN_DEBUG("%p, s->l=%ld, s->wn=%ld, s->g=%ld\n", sel,
Lev Walkin1e3ccbb2004-10-26 10:56:10 +0000250 (long)(sel?sel->left:0),
251 (long)(sel?sel->want_nulls:0),
252 (long)(sel?sel->got:0)
Lev Walkin5c915992004-09-27 20:54:06 +0000253 );
254 if(sel && sel->left <= 0 && sel->want_nulls == 0) {
255 if(sel->prev) {
256 struct _stack_el *prev = sel->prev;
257 if(prev->left != -1) {
258 if(prev->left < sel->got)
259 RETURN(RC_FAIL);
260 prev->left -= sel->got;
261 }
262 prev->got += sel->got;
263 sel = stck->cur_ptr = prev;
264 if(!sel) break;
265 tlv_constr = 1;
266 continue;
267 } else {
268 sel = stck->cur_ptr = 0;
269 break; /* Nothing to wait */
270 }
271 }
272
Lev Walkin3990ba62004-09-24 20:57:41 +0000273 tl = ber_fetch_tag(buf_ptr, Left, &tlv_tag);
Lev Walkinabf68892004-10-26 10:12:14 +0000274 ASN_DEBUG("fetch tag(size=%ld,L=%ld), %sstack, left=%ld, wn=%ld, tl=%ld",
275 (long)size, (long)Left, sel?"":"!",
Lev Walkin1e3ccbb2004-10-26 10:56:10 +0000276 (long)(sel?sel->left:0),
277 (long)(sel?sel->want_nulls:0),
Lev Walkinabf68892004-10-26 10:12:14 +0000278 (long)tl);
Lev Walkinf15320b2004-06-03 03:38:44 +0000279 switch(tl) {
280 case -1: RETURN(RC_FAIL);
281 case 0: RETURN(RC_WMORE);
282 }
283
284 tlv_constr = BER_TLV_CONSTRUCTED(buf_ptr);
285
286 ll = ber_fetch_length(tlv_constr,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000287 (const char *)buf_ptr + tl,Left - tl,&tlv_len);
Lev Walkina6a926a2005-03-02 01:54:28 +0000288 ASN_DEBUG("Got tag=%s, tc=%d, left=%ld, tl=%ld, len=%ld, ll=%ld",
Lev Walkin8d127872004-09-04 04:44:50 +0000289 ber_tlv_tag_string(tlv_tag), tlv_constr,
Lev Walkina6a926a2005-03-02 01:54:28 +0000290 (long)Left, (long)tl, (long)tlv_len, (long)ll);
Lev Walkinf15320b2004-06-03 03:38:44 +0000291 switch(ll) {
292 case -1: RETURN(RC_FAIL);
293 case 0: RETURN(RC_WMORE);
294 }
295
Lev Walkin8d127872004-09-04 04:44:50 +0000296 if(sel && sel->want_nulls
Lev Walkin8c3b8542005-03-10 18:52:02 +0000297 && ((const uint8_t *)buf_ptr)[0] == 0
298 && ((const uint8_t *)buf_ptr)[1] == 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000299 {
Lev Walkin8d127872004-09-04 04:44:50 +0000300
301 ASN_DEBUG("Eat EOC; wn=%d--", sel->want_nulls);
302
Lev Walkin3a4689a2006-11-24 11:20:27 +0000303 if(type_variant == ASN_OSUBV_ANY
Lev Walkin07f388c2004-10-11 11:43:08 +0000304 && (tag_mode != 1 || sel->cont_level))
305 APPEND("\0\0", 2);
Lev Walkin5c915992004-09-27 20:54:06 +0000306
307 ADVANCE(2);
308 sel->got += 2;
309 if(sel->left != -1) {
310 sel->left -= 2; /* assert(sel->left >= 2) */
311 }
312
Lev Walkinf15320b2004-06-03 03:38:44 +0000313 sel->want_nulls--;
314 if(sel->want_nulls == 0) {
315 /* Move to the next expectation */
Lev Walkin5c915992004-09-27 20:54:06 +0000316 sel->left = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000317 tlv_constr = 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000318 }
Lev Walkin8d127872004-09-04 04:44:50 +0000319
320 continue;
Lev Walkin188ed2c2004-09-13 08:31:01 +0000321 }
322
323 /*
324 * Set up expected tags,
325 * depending on ASN.1 type being decoded.
326 */
Lev Walkinbbd93252004-10-12 05:57:23 +0000327 switch(type_variant) {
Lev Walkin3a4689a2006-11-24 11:20:27 +0000328 case ASN_OSUBV_BIT:
Lev Walkin188ed2c2004-09-13 08:31:01 +0000329 /* X.690: 8.6.4.1, NOTE 2 */
330 /* Fall through */
Lev Walkin3a4689a2006-11-24 11:20:27 +0000331 case ASN_OSUBV_STR:
Lev Walkin188ed2c2004-09-13 08:31:01 +0000332 default:
333 if(sel) {
334 int level = sel->cont_level;
335 if(level < td->all_tags_count) {
336 expected_tag = td->all_tags[level];
337 break;
338 } else if(td->all_tags_count) {
339 expected_tag = td->all_tags
340 [td->all_tags_count - 1];
341 break;
342 }
343 /* else, Fall through */
344 }
345 /* Fall through */
Lev Walkin3a4689a2006-11-24 11:20:27 +0000346 case ASN_OSUBV_ANY:
Lev Walkin188ed2c2004-09-13 08:31:01 +0000347 expected_tag = tlv_tag;
348 break;
349 }
350
351
352 if(tlv_tag != expected_tag) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000353 char buf[2][32];
354 ber_tlv_tag_snprint(tlv_tag,
355 buf[0], sizeof(buf[0]));
356 ber_tlv_tag_snprint(td->tags[td->tags_count-1],
357 buf[1], sizeof(buf[1]));
358 ASN_DEBUG("Tag does not match expectation: %s != %s",
359 buf[0], buf[1]);
360 RETURN(RC_FAIL);
361 }
362
Lev Walkinde4825d2004-09-29 13:20:14 +0000363 tlvl = tl + ll; /* Combined length of T and L encoding */
364 if((tlv_len + tlvl) < 0) {
365 /* tlv_len value is too big */
366 ASN_DEBUG("TLV encoding + length (%ld) is too big",
367 (long)tlv_len);
368 RETURN(RC_FAIL);
369 }
370
Lev Walkinf15320b2004-06-03 03:38:44 +0000371 /*
372 * Append a new expectation.
373 */
Lev Walkin3990ba62004-09-24 20:57:41 +0000374 sel = OS__add_stack_el(stck);
Lev Walkin5c915992004-09-27 20:54:06 +0000375 if(!sel) RETURN(RC_FAIL);
Lev Walkinf15320b2004-06-03 03:38:44 +0000376
Lev Walkin5c915992004-09-27 20:54:06 +0000377 sel->tag = tlv_tag;
378
379 sel->want_nulls = (tlv_len==-1);
380 if(sel->prev && sel->prev->left != -1) {
381 /* Check that the parent frame is big enough */
382 if(sel->prev->left < tlvl + (tlv_len==-1?0:tlv_len))
383 RETURN(RC_FAIL);
384 if(tlv_len == -1)
385 sel->left = sel->prev->left - tlvl;
386 else
387 sel->left = tlv_len;
388 } else {
389 sel->left = tlv_len;
390 }
Lev Walkin3a4689a2006-11-24 11:20:27 +0000391 if(type_variant == ASN_OSUBV_ANY
Lev Walkin07f388c2004-10-11 11:43:08 +0000392 && (tag_mode != 1 || sel->cont_level))
393 APPEND(buf_ptr, tlvl);
Lev Walkin5c915992004-09-27 20:54:06 +0000394 sel->got += tlvl;
395 ADVANCE(tlvl);
396
Lev Walkinabf68892004-10-26 10:12:14 +0000397 ASN_DEBUG("+EXPECT2 got=%ld left=%ld, wn=%d, clvl=%d",
398 (long)sel->got, (long)sel->left,
399 sel->want_nulls, sel->cont_level);
Lev Walkin5c915992004-09-27 20:54:06 +0000400
Lev Walkinf15320b2004-06-03 03:38:44 +0000401 } while(tlv_constr);
402 if(sel == NULL) {
403 /* Finished operation, "phase out" */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000404 ASN_DEBUG("Phase out");
Lev Walkinf15320b2004-06-03 03:38:44 +0000405 _CH_PHASE(ctx, +3);
406 break;
407 }
408
409 NEXT_PHASE(ctx);
410 /* Fall through */
411 case 2:
Lev Walkin814cca72004-12-15 23:23:53 +0000412 stck = (struct _stack *)ctx->ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000413 sel = stck->cur_ptr;
Lev Walkin5c915992004-09-27 20:54:06 +0000414 ASN_DEBUG("Phase 2: Need %ld bytes, size=%ld, alrg=%ld, wn=%d",
415 (long)sel->left, (long)size, (long)sel->got,
416 sel->want_nulls);
Lev Walkinf15320b2004-06-03 03:38:44 +0000417 {
418 ber_tlv_len_t len;
419
420 assert(sel->left >= 0);
421
Lev Walkinec1ffd42004-08-18 04:53:32 +0000422 len = ((ber_tlv_len_t)size < sel->left)
423 ? (ber_tlv_len_t)size : sel->left;
Lev Walkinf15320b2004-06-03 03:38:44 +0000424 if(len > 0) {
Lev Walkin3a4689a2006-11-24 11:20:27 +0000425 if(type_variant == ASN_OSUBV_BIT
Lev Walkin188ed2c2004-09-13 08:31:01 +0000426 && sel->bits_chopped == 0) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000427 /* Put the unused-bits-octet away */
Lev Walkin8c3b8542005-03-10 18:52:02 +0000428 st->bits_unused = *(const uint8_t *)buf_ptr;
429 APPEND(((const char *)buf_ptr+1), (len - 1));
Lev Walkinf15320b2004-06-03 03:38:44 +0000430 sel->bits_chopped = 1;
431 } else {
432 APPEND(buf_ptr, len);
433 }
434 ADVANCE(len);
435 sel->left -= len;
Lev Walkin5c915992004-09-27 20:54:06 +0000436 sel->got += len;
Lev Walkinf15320b2004-06-03 03:38:44 +0000437 }
438
Lev Walkin5c915992004-09-27 20:54:06 +0000439 if(sel->left) {
440 ASN_DEBUG("OS left %ld, size = %ld, wn=%d\n",
441 (long)sel->left, (long)size, sel->want_nulls);
Lev Walkinf15320b2004-06-03 03:38:44 +0000442 RETURN(RC_WMORE);
Lev Walkinf15320b2004-06-03 03:38:44 +0000443 }
Lev Walkin5c915992004-09-27 20:54:06 +0000444
445 PREV_PHASE(ctx);
446 goto phase1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000447 }
448 break;
449 case 3:
450 phase3:
451 /*
452 * Primitive form, no stack required.
453 */
Lev Walkinbbd93252004-10-12 05:57:23 +0000454 assert(ctx->left >= 0);
455
Lev Walkind9bd7752004-06-05 08:17:50 +0000456 if(size < (size_t)ctx->left) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000457 if(!size) RETURN(RC_WMORE);
Lev Walkin3a4689a2006-11-24 11:20:27 +0000458 if(type_variant == ASN_OSUBV_BIT && !ctx->context) {
Lev Walkin8c3b8542005-03-10 18:52:02 +0000459 st->bits_unused = *(const uint8_t *)buf_ptr;
Lev Walkinbbd93252004-10-12 05:57:23 +0000460 ctx->left--;
461 ADVANCE(1);
462 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000463 APPEND(buf_ptr, size);
Lev Walkin75b1bef2005-04-25 19:38:21 +0000464 assert(ctx->context > 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000465 ctx->left -= size;
466 ADVANCE(size);
467 RETURN(RC_WMORE);
468 } else {
Lev Walkin3a4689a2006-11-24 11:20:27 +0000469 if(type_variant == ASN_OSUBV_BIT
Lev Walkin75b1bef2005-04-25 19:38:21 +0000470 && !ctx->context && ctx->left) {
Lev Walkin8c3b8542005-03-10 18:52:02 +0000471 st->bits_unused = *(const uint8_t *)buf_ptr;
Lev Walkinbbd93252004-10-12 05:57:23 +0000472 ctx->left--;
473 ADVANCE(1);
474 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000475 APPEND(buf_ptr, ctx->left);
476 ADVANCE(ctx->left);
477 ctx->left = 0;
478
479 NEXT_PHASE(ctx);
480 }
481 break;
482 }
483
Lev Walkin5c915992004-09-27 20:54:06 +0000484 if(sel) {
485 ASN_DEBUG("3sel p=%p, wn=%d, l=%ld, g=%ld, size=%ld",
486 sel->prev, sel->want_nulls,
487 (long)sel->left, (long)sel->got, (long)size);
488 if(sel->prev || sel->want_nulls > 1 || sel->left > 0) {
489 RETURN(RC_WMORE);
490 }
491 }
492
Lev Walkinf15320b2004-06-03 03:38:44 +0000493 /*
494 * BIT STRING-specific processing.
495 */
Lev Walkin3a4689a2006-11-24 11:20:27 +0000496 if(type_variant == ASN_OSUBV_BIT && st->size) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000497 /* Finalize BIT STRING: zero out unused bits. */
Lev Walkinbbd93252004-10-12 05:57:23 +0000498 st->buf[st->size-1] &= 0xff << st->bits_unused;
Lev Walkinf15320b2004-06-03 03:38:44 +0000499 }
500
Lev Walkinabf68892004-10-26 10:12:14 +0000501 ASN_DEBUG("Took %ld bytes to encode %s: [%s]:%ld",
502 (long)consumed_myself, td->name,
Lev Walkin3a4689a2006-11-24 11:20:27 +0000503 (type_variant == ASN_OSUBV_STR) ? (char *)st->buf : "<data>",
Lev Walkinabf68892004-10-26 10:12:14 +0000504 (long)st->size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000505
Lev Walkinf15320b2004-06-03 03:38:44 +0000506
Lev Walkin5c915992004-09-27 20:54:06 +0000507 RETURN(RC_OK);
Lev Walkinf15320b2004-06-03 03:38:44 +0000508}
509
510/*
511 * Encode OCTET STRING type using DER.
512 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000513asn_enc_rval_t
Lev Walkinbbd93252004-10-12 05:57:23 +0000514OCTET_STRING_encode_der(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000515 int tag_mode, ber_tlv_tag_t tag,
516 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000517 asn_enc_rval_t er;
518 asn_OCTET_STRING_specifics_t *specs = td->specifics
Lev Walkindc06f6b2004-10-20 15:50:55 +0000519 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +0800520 : &asn_SPC_OCTET_STRING_specs;
Lev Walkinbbd93252004-10-12 05:57:23 +0000521 BIT_STRING_t *st = (BIT_STRING_t *)sptr;
Lev Walkin3a4689a2006-11-24 11:20:27 +0000522 enum asn_OS_Subvariant type_variant = specs->subvariant;
Lev Walkinbbd93252004-10-12 05:57:23 +0000523 int fix_last_byte = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000524
525 ASN_DEBUG("%s %s as OCTET STRING",
Lev Walkin1f670c12004-09-02 12:57:25 +0000526 cb?"Estimating":"Encoding", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000527
528 /*
Lev Walkinbbd93252004-10-12 05:57:23 +0000529 * Write tags.
Lev Walkinf15320b2004-06-03 03:38:44 +0000530 */
Lev Walkin3a4689a2006-11-24 11:20:27 +0000531 if(type_variant != ASN_OSUBV_ANY || tag_mode == 1) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000532 er.encoded = der_write_tags(td,
Lev Walkin3a4689a2006-11-24 11:20:27 +0000533 (type_variant == ASN_OSUBV_BIT) + st->size,
534 tag_mode, type_variant == ASN_OSUBV_ANY, tag,
535 cb, app_key);
Lev Walkinbbd93252004-10-12 05:57:23 +0000536 if(er.encoded == -1) {
537 er.failed_type = td;
538 er.structure_ptr = sptr;
539 return er;
Lev Walkinf15320b2004-06-03 03:38:44 +0000540 }
Lev Walkinbbd93252004-10-12 05:57:23 +0000541 } else {
542 /* Disallow: [<tag>] IMPLICIT ANY */
Lev Walkin3a4689a2006-11-24 11:20:27 +0000543 assert(type_variant != ASN_OSUBV_ANY || tag_mode != -1);
Lev Walkinbbd93252004-10-12 05:57:23 +0000544 er.encoded = 0;
545 }
546
547 if(!cb) {
Lev Walkin3a4689a2006-11-24 11:20:27 +0000548 er.encoded += (type_variant == ASN_OSUBV_BIT) + st->size;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700549 ASN__ENCODED_OK(er);
Lev Walkinf15320b2004-06-03 03:38:44 +0000550 }
551
Lev Walkin07f388c2004-10-11 11:43:08 +0000552 /*
Lev Walkinbbd93252004-10-12 05:57:23 +0000553 * Prepare to deal with the last octet of BIT STRING.
Lev Walkin07f388c2004-10-11 11:43:08 +0000554 */
Lev Walkin3a4689a2006-11-24 11:20:27 +0000555 if(type_variant == ASN_OSUBV_BIT) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000556 uint8_t b = st->bits_unused & 0x07;
557 if(b && st->size) fix_last_byte = 1;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700558 ASN__CALLBACK(&b, 1);
Lev Walkinbbd93252004-10-12 05:57:23 +0000559 er.encoded++;
Lev Walkinf15320b2004-06-03 03:38:44 +0000560 }
561
Lev Walkinbbd93252004-10-12 05:57:23 +0000562 /* Invoke callback for the main part of the buffer */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700563 ASN__CALLBACK(st->buf, st->size - fix_last_byte);
Lev Walkinf15320b2004-06-03 03:38:44 +0000564
Lev Walkinbbd93252004-10-12 05:57:23 +0000565 /* The last octet should be stripped off the unused bits */
566 if(fix_last_byte) {
567 uint8_t b = st->buf[st->size-1] & (0xff << st->bits_unused);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700568 ASN__CALLBACK(&b, 1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000569 }
570
Lev Walkinbbd93252004-10-12 05:57:23 +0000571 er.encoded += st->size;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700572 ASN__ENCODED_OK(er);
Lev Walkinbbd93252004-10-12 05:57:23 +0000573cb_failed:
Lev Walkin7c1dc052016-03-14 03:08:15 -0700574 ASN__ENCODE_FAILED;
Lev Walkinf15320b2004-06-03 03:38:44 +0000575}
576
Lev Walkina9cc46e2004-09-22 16:06:28 +0000577asn_enc_rval_t
Lev Walkinde4825d2004-09-29 13:20:14 +0000578OCTET_STRING_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000579 int ilevel, enum xer_encoder_flags_e flags,
580 asn_app_consume_bytes_f *cb, void *app_key) {
Wim Lewis59e8d282014-08-04 12:39:35 -0700581 const char * const h2c = "0123456789ABCDEF";
Lev Walkina9cc46e2004-09-22 16:06:28 +0000582 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
583 asn_enc_rval_t er;
584 char scratch[16 * 3 + 4];
585 char *p = scratch;
586 uint8_t *buf;
587 uint8_t *end;
588 size_t i;
589
Lev Walkin2ddd3082006-10-16 12:32:14 +0000590 if(!st || (!st->buf && st->size))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700591 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000592
593 er.encoded = 0;
594
595 /*
596 * Dump the contents of the buffer in hexadecimal.
597 */
598 buf = st->buf;
599 end = buf + st->size;
600 if(flags & XER_F_CANONICAL) {
601 char *scend = scratch + (sizeof(scratch) - 2);
602 for(; buf < end; buf++) {
603 if(p >= scend) {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700604 ASN__CALLBACK(scratch, p - scratch);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000605 er.encoded += p - scratch;
606 p = scratch;
607 }
608 *p++ = h2c[(*buf >> 4) & 0x0F];
609 *p++ = h2c[*buf & 0x0F];
610 }
Lev Walkincc6a9102004-09-23 22:06:26 +0000611
Lev Walkin7c1dc052016-03-14 03:08:15 -0700612 ASN__CALLBACK(scratch, p-scratch); /* Dump the rest */
Lev Walkincc6a9102004-09-23 22:06:26 +0000613 er.encoded += p - scratch;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000614 } else {
615 for(i = 0; buf < end; buf++, i++) {
616 if(!(i % 16) && (i || st->size > 16)) {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700617 ASN__CALLBACK(scratch, p-scratch);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000618 er.encoded += (p-scratch);
619 p = scratch;
Lev Walkinc6cac8e2016-03-14 02:57:07 -0700620 ASN__TEXT_INDENT(1, ilevel);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000621 }
622 *p++ = h2c[(*buf >> 4) & 0x0F];
623 *p++ = h2c[*buf & 0x0F];
624 *p++ = 0x20;
625 }
Lev Walkincc6a9102004-09-23 22:06:26 +0000626 if(p - scratch) {
627 p--; /* Remove the tail space */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700628 ASN__CALLBACK(scratch, p-scratch); /* Dump the rest */
Lev Walkincc6a9102004-09-23 22:06:26 +0000629 er.encoded += p - scratch;
630 if(st->size > 16)
Lev Walkinc6cac8e2016-03-14 02:57:07 -0700631 ASN__TEXT_INDENT(1, ilevel-1);
Lev Walkincc6a9102004-09-23 22:06:26 +0000632 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000633 }
634
Lev Walkin7c1dc052016-03-14 03:08:15 -0700635 ASN__ENCODED_OK(er);
Lev Walkin942fd082004-10-03 09:13:02 +0000636cb_failed:
Lev Walkin7c1dc052016-03-14 03:08:15 -0700637 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000638}
639
Wim Lewis18c2ec92014-07-29 11:30:10 -0700640static const struct OCTET_STRING__xer_escape_table_s {
641 const char *string;
Lev Walkin9ca81892004-10-03 10:54:25 +0000642 int size;
643} OCTET_STRING__xer_escape_table[] = {
644#define OSXET(s) { s, sizeof(s) - 1 }
645 OSXET("\074\156\165\154\057\076"), /* <nul/> */
646 OSXET("\074\163\157\150\057\076"), /* <soh/> */
647 OSXET("\074\163\164\170\057\076"), /* <stx/> */
648 OSXET("\074\145\164\170\057\076"), /* <etx/> */
649 OSXET("\074\145\157\164\057\076"), /* <eot/> */
650 OSXET("\074\145\156\161\057\076"), /* <enq/> */
651 OSXET("\074\141\143\153\057\076"), /* <ack/> */
652 OSXET("\074\142\145\154\057\076"), /* <bel/> */
653 OSXET("\074\142\163\057\076"), /* <bs/> */
654 OSXET("\011"), /* \t */
655 OSXET("\012"), /* \n */
656 OSXET("\074\166\164\057\076"), /* <vt/> */
657 OSXET("\074\146\146\057\076"), /* <ff/> */
658 OSXET("\015"), /* \r */
659 OSXET("\074\163\157\057\076"), /* <so/> */
660 OSXET("\074\163\151\057\076"), /* <si/> */
661 OSXET("\074\144\154\145\057\076"), /* <dle/> */
662 OSXET("\074\144\143\061\057\076"), /* <de1/> */
663 OSXET("\074\144\143\062\057\076"), /* <de2/> */
664 OSXET("\074\144\143\063\057\076"), /* <de3/> */
665 OSXET("\074\144\143\064\057\076"), /* <de4/> */
666 OSXET("\074\156\141\153\057\076"), /* <nak/> */
667 OSXET("\074\163\171\156\057\076"), /* <syn/> */
668 OSXET("\074\145\164\142\057\076"), /* <etb/> */
669 OSXET("\074\143\141\156\057\076"), /* <can/> */
670 OSXET("\074\145\155\057\076"), /* <em/> */
671 OSXET("\074\163\165\142\057\076"), /* <sub/> */
672 OSXET("\074\145\163\143\057\076"), /* <esc/> */
673 OSXET("\074\151\163\064\057\076"), /* <is4/> */
674 OSXET("\074\151\163\063\057\076"), /* <is3/> */
675 OSXET("\074\151\163\062\057\076"), /* <is2/> */
676 OSXET("\074\151\163\061\057\076"), /* <is1/> */
677 { 0, 0 }, /* " " */
678 { 0, 0 }, /* ! */
679 { 0, 0 }, /* \" */
680 { 0, 0 }, /* # */
681 { 0, 0 }, /* $ */
682 { 0, 0 }, /* % */
683 OSXET("\046\141\155\160\073"), /* &amp; */
684 { 0, 0 }, /* ' */
685 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, /* ()*+,-./ */
686 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, /* 01234567 */
687 {0,0},{0,0},{0,0},{0,0}, /* 89:; */
688 OSXET("\046\154\164\073"), /* &lt; */
689 { 0, 0 }, /* = */
690 OSXET("\046\147\164\073"), /* &gt; */
691};
692
Lev Walkindc06f6b2004-10-20 15:50:55 +0000693static int
Lev Walkin0fab1a62005-03-09 22:19:25 +0000694OS__check_escaped_control_char(const void *buf, int size) {
Lev Walkindc06f6b2004-10-20 15:50:55 +0000695 size_t i;
696 /*
697 * Inefficient algorithm which translates the escape sequences
698 * defined above into characters. Returns -1 if not found.
699 * TODO: replace by a faster algorithm (bsearch(), hash or
700 * nested table lookups).
701 */
702 for(i = 0; i < 32 /* Don't spend time on the bottom half */; i++) {
Wim Lewis18c2ec92014-07-29 11:30:10 -0700703 const struct OCTET_STRING__xer_escape_table_s *el;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000704 el = &OCTET_STRING__xer_escape_table[i];
705 if(el->size == size && memcmp(buf, el->string, size) == 0)
706 return i;
707 }
708 return -1;
709}
710
711static int
Lev Walkin0fab1a62005-03-09 22:19:25 +0000712OCTET_STRING__handle_control_chars(void *struct_ptr, const void *chunk_buf, size_t chunk_size) {
Lev Walkindc06f6b2004-10-20 15:50:55 +0000713 /*
714 * This might be one of the escape sequences
715 * for control characters. Check it out.
716 * #11.15.5
717 */
718 int control_char = OS__check_escaped_control_char(chunk_buf,chunk_size);
719 if(control_char >= 0) {
720 OCTET_STRING_t *st = (OCTET_STRING_t *)struct_ptr;
721 void *p = REALLOC(st->buf, st->size + 2);
722 if(p) {
723 st->buf = (uint8_t *)p;
724 st->buf[st->size++] = control_char;
725 st->buf[st->size] = '\0'; /* nul-termination */
726 return 0;
727 }
728 }
729
730 return -1; /* No, it's not */
731}
732
Lev Walkina9cc46e2004-09-22 16:06:28 +0000733asn_enc_rval_t
Lev Walkindc06f6b2004-10-20 15:50:55 +0000734OCTET_STRING_encode_xer_utf8(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000735 int ilevel, enum xer_encoder_flags_e flags,
736 asn_app_consume_bytes_f *cb, void *app_key) {
737 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
738 asn_enc_rval_t er;
Lev Walkin9ca81892004-10-03 10:54:25 +0000739 uint8_t *buf, *end;
740 uint8_t *ss; /* Sequence start */
741 ssize_t encoded_len = 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000742
743 (void)ilevel; /* Unused argument */
744 (void)flags; /* Unused argument */
745
Lev Walkin2ddd3082006-10-16 12:32:14 +0000746 if(!st || (!st->buf && st->size))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700747 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000748
Lev Walkin9ca81892004-10-03 10:54:25 +0000749 buf = st->buf;
750 end = buf + st->size;
751 for(ss = buf; buf < end; buf++) {
Lev Walkin443d2512004-10-05 06:35:31 +0000752 unsigned int ch = *buf;
Lev Walkin9ca81892004-10-03 10:54:25 +0000753 int s_len; /* Special encoding sequence length */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000754
Lev Walkin9ca81892004-10-03 10:54:25 +0000755 /*
756 * Escape certain characters: X.680/11.15
757 */
758 if(ch < sizeof(OCTET_STRING__xer_escape_table)
759 /sizeof(OCTET_STRING__xer_escape_table[0])
760 && (s_len = OCTET_STRING__xer_escape_table[ch].size)) {
761 if(((buf - ss) && cb(ss, buf - ss, app_key) < 0)
762 || cb(OCTET_STRING__xer_escape_table[ch].string, s_len,
763 app_key) < 0)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700764 ASN__ENCODE_FAILED;
Lev Walkin9ca81892004-10-03 10:54:25 +0000765 encoded_len += (buf - ss) + s_len;
766 ss = buf + 1;
767 }
768 }
769
770 encoded_len += (buf - ss);
771 if((buf - ss) && cb(ss, buf - ss, app_key) < 0)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700772 ASN__ENCODE_FAILED;
Lev Walkin9ca81892004-10-03 10:54:25 +0000773
774 er.encoded = encoded_len;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700775 ASN__ENCODED_OK(er);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000776}
777
Lev Walkindc06f6b2004-10-20 15:50:55 +0000778/*
779 * Convert from hexadecimal format (cstring): "AB CD EF"
780 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000781static 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 +0000782 OCTET_STRING_t *st = (OCTET_STRING_t *)sptr;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000783 const char *chunk_stop = (const char *)chunk_buf;
784 const char *p = chunk_stop;
785 const char *pend = p + chunk_size;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000786 unsigned int clv = 0;
787 int half = 0; /* Half bit */
788 uint8_t *buf;
789
790 /* Reallocate buffer according to high cap estimation */
791 ssize_t _ns = st->size + (chunk_size + 1) / 2;
792 void *nptr = REALLOC(st->buf, _ns + 1);
793 if(!nptr) return -1;
794 st->buf = (uint8_t *)nptr;
795 buf = st->buf + st->size;
796
797 /*
798 * If something like " a b c " appears here, the " a b":3 will be
799 * converted, and the rest skipped. That is, unless buf_size is greater
800 * than chunk_size, then it'll be equivalent to "ABC0".
801 */
802 for(; p < pend; p++) {
Lev Walkin0fab1a62005-03-09 22:19:25 +0000803 int ch = *(const unsigned char *)p;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000804 switch(ch) {
805 case 0x09: case 0x0a: case 0x0c: case 0x0d:
806 case 0x20:
807 /* Ignore whitespace */
808 continue;
809 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: /*01234*/
810 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: /*56789*/
811 clv = (clv << 4) + (ch - 0x30);
812 break;
813 case 0x41: case 0x42: case 0x43: /* ABC */
814 case 0x44: case 0x45: case 0x46: /* DEF */
Lev Walkin3ae21bd2005-02-21 14:43:48 +0000815 clv = (clv << 4) + (ch - 0x41 + 10);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000816 break;
817 case 0x61: case 0x62: case 0x63: /* abc */
818 case 0x64: case 0x65: case 0x66: /* def */
Lev Walkin3ae21bd2005-02-21 14:43:48 +0000819 clv = (clv << 4) + (ch - 0x61 + 10);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000820 break;
821 default:
822 *buf = 0; /* JIC */
823 return -1;
824 }
825 if(half++) {
826 half = 0;
827 *buf++ = clv;
828 chunk_stop = p + 1;
829 }
830 }
831
832 /*
833 * Check partial decoding.
834 */
835 if(half) {
836 if(have_more) {
837 /*
838 * Partial specification is fine,
839 * because no more more PXER_TEXT data is available.
840 */
841 *buf++ = clv << 4;
842 chunk_stop = p;
843 }
844 } else {
845 chunk_stop = p;
846 }
847
848 st->size = buf - st->buf; /* Adjust the buffer size */
849 assert(st->size <= _ns);
850 st->buf[st->size] = 0; /* Courtesy termination */
851
Lev Walkin0fab1a62005-03-09 22:19:25 +0000852 return (chunk_stop - (const char *)chunk_buf); /* Converted size */
Lev Walkindc06f6b2004-10-20 15:50:55 +0000853}
854
855/*
856 * Convert from binary format: "00101011101"
857 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000858static 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 +0000859 BIT_STRING_t *st = (BIT_STRING_t *)sptr;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000860 const char *p = (const char *)chunk_buf;
861 const char *pend = p + chunk_size;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000862 int bits_unused = st->bits_unused & 0x7;
863 uint8_t *buf;
864
865 /* Reallocate buffer according to high cap estimation */
866 ssize_t _ns = st->size + (chunk_size + 7) / 8;
867 void *nptr = REALLOC(st->buf, _ns + 1);
868 if(!nptr) return -1;
869 st->buf = (uint8_t *)nptr;
870 buf = st->buf + st->size;
871
872 (void)have_more;
873
874 if(bits_unused == 0)
875 bits_unused = 8;
876 else if(st->size)
877 buf--;
878
879 /*
880 * Convert series of 0 and 1 into the octet string.
881 */
882 for(; p < pend; p++) {
Lev Walkin0fab1a62005-03-09 22:19:25 +0000883 int ch = *(const unsigned char *)p;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000884 switch(ch) {
885 case 0x09: case 0x0a: case 0x0c: case 0x0d:
886 case 0x20:
887 /* Ignore whitespace */
888 break;
889 case 0x30:
890 case 0x31:
891 if(bits_unused-- <= 0) {
892 *++buf = 0; /* Clean the cell */
893 bits_unused = 7;
894 }
895 *buf |= (ch&1) << bits_unused;
896 break;
897 default:
898 st->bits_unused = bits_unused;
899 return -1;
900 }
901 }
902
903 if(bits_unused == 8) {
904 st->size = buf - st->buf;
905 st->bits_unused = 0;
906 } else {
907 st->size = buf - st->buf + 1;
908 st->bits_unused = bits_unused;
909 }
910
911 assert(st->size <= _ns);
912 st->buf[st->size] = 0; /* Courtesy termination */
913
914 return chunk_size; /* Converted in full */
915}
916
917/*
918 * Something like strtod(), but with stricter rules.
919 */
920static int
Lev Walkin0fab1a62005-03-09 22:19:25 +0000921OS__strtoent(int base, const char *buf, const char *end, int32_t *ret_value) {
Lev Walkin33700162004-10-26 09:03:31 +0000922 int32_t val = 0;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000923 const char *p;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000924
925 for(p = buf; p < end; p++) {
926 int ch = *p;
Lev Walkinb0f3db62005-07-03 05:30:15 +0000927
928 /* Strange huge value */
929 if((val * base + base) < 0)
930 return -1;
931
Lev Walkindc06f6b2004-10-20 15:50:55 +0000932 switch(ch) {
933 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: /*01234*/
934 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: /*56789*/
935 val = val * base + (ch - 0x30);
936 break;
937 case 0x41: case 0x42: case 0x43: /* ABC */
938 case 0x44: case 0x45: case 0x46: /* DEF */
Lev Walkin3ae21bd2005-02-21 14:43:48 +0000939 val = val * base + (ch - 0x41 + 10);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000940 break;
941 case 0x61: case 0x62: case 0x63: /* abc */
942 case 0x64: case 0x65: case 0x66: /* def */
Lev Walkin3ae21bd2005-02-21 14:43:48 +0000943 val = val * base + (ch - 0x61 + 10);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000944 break;
945 case 0x3b: /* ';' */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000946 *ret_value = val;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000947 return (p - buf) + 1;
948 default:
949 return -1; /* Character set error */
950 }
951 }
952
Lev Walkinb0f3db62005-07-03 05:30:15 +0000953 *ret_value = -1;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000954 return (p - buf);
955}
956
957/*
958 * Convert from the plain UTF-8 format, expanding entity references: "2 &lt; 3"
959 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000960static 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 +0000961 OCTET_STRING_t *st = (OCTET_STRING_t *)sptr;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000962 const char *p = (const char *)chunk_buf;
963 const char *pend = p + chunk_size;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000964 uint8_t *buf;
965
966 /* Reallocate buffer */
967 ssize_t _ns = st->size + chunk_size;
968 void *nptr = REALLOC(st->buf, _ns + 1);
969 if(!nptr) return -1;
970 st->buf = (uint8_t *)nptr;
971 buf = st->buf + st->size;
972
973 /*
974 * Convert series of 0 and 1 into the octet string.
975 */
976 for(; p < pend; p++) {
Lev Walkin0fab1a62005-03-09 22:19:25 +0000977 int ch = *(const unsigned char *)p;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000978 int len; /* Length of the rest of the chunk */
979
980 if(ch != 0x26 /* '&' */) {
981 *buf++ = ch;
982 continue; /* That was easy... */
983 }
984
985 /*
986 * Process entity reference.
987 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000988 len = chunk_size - (p - (const char *)chunk_buf);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000989 if(len == 1 /* "&" */) goto want_more;
990 if(p[1] == 0x23 /* '#' */) {
Lev Walkin0fab1a62005-03-09 22:19:25 +0000991 const char *pval; /* Pointer to start of digits */
Lev Walkinbc0dce32006-07-27 09:56:13 +0000992 int32_t val = 0; /* Entity reference value */
Lev Walkindc06f6b2004-10-20 15:50:55 +0000993 int base;
994
995 if(len == 2 /* "&#" */) goto want_more;
996 if(p[2] == 0x78 /* 'x' */)
997 pval = p + 3, base = 16;
998 else
999 pval = p + 2, base = 10;
1000 len = OS__strtoent(base, pval, p + len, &val);
1001 if(len == -1) {
1002 /* Invalid charset. Just copy verbatim. */
1003 *buf++ = ch;
1004 continue;
1005 }
1006 if(!len || pval[len-1] != 0x3b) goto want_more;
1007 assert(val > 0);
1008 p += (pval - p) + len - 1; /* Advance past entref */
1009
1010 if(val < 0x80) {
1011 *buf++ = (char)val;
1012 } else if(val < 0x800) {
1013 *buf++ = 0xc0 | ((val >> 6));
1014 *buf++ = 0x80 | ((val & 0x3f));
1015 } else if(val < 0x10000) {
1016 *buf++ = 0xe0 | ((val >> 12));
1017 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1018 *buf++ = 0x80 | ((val & 0x3f));
1019 } else if(val < 0x200000) {
1020 *buf++ = 0xf0 | ((val >> 18));
1021 *buf++ = 0x80 | ((val >> 12) & 0x3f);
1022 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1023 *buf++ = 0x80 | ((val & 0x3f));
1024 } else if(val < 0x4000000) {
1025 *buf++ = 0xf8 | ((val >> 24));
1026 *buf++ = 0x80 | ((val >> 18) & 0x3f);
1027 *buf++ = 0x80 | ((val >> 12) & 0x3f);
1028 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1029 *buf++ = 0x80 | ((val & 0x3f));
1030 } else {
1031 *buf++ = 0xfc | ((val >> 30) & 0x1);
1032 *buf++ = 0x80 | ((val >> 24) & 0x3f);
1033 *buf++ = 0x80 | ((val >> 18) & 0x3f);
1034 *buf++ = 0x80 | ((val >> 12) & 0x3f);
1035 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1036 *buf++ = 0x80 | ((val & 0x3f));
1037 }
1038 } else {
1039 /*
1040 * Ugly, limited parsing of &amp; &gt; &lt;
1041 */
1042 char *sc = (char *)memchr(p, 0x3b, len > 5 ? 5 : len);
1043 if(!sc) goto want_more;
1044 if((sc - p) == 4
1045 && p[1] == 0x61 /* 'a' */
1046 && p[2] == 0x6d /* 'm' */
1047 && p[3] == 0x70 /* 'p' */) {
1048 *buf++ = 0x26;
1049 p = sc;
1050 continue;
1051 }
1052 if((sc - p) == 3) {
1053 if(p[1] == 0x6c) {
1054 *buf = 0x3c; /* '<' */
1055 } else if(p[1] == 0x67) {
1056 *buf = 0x3e; /* '>' */
1057 } else {
1058 /* Unsupported entity reference */
1059 *buf++ = ch;
1060 continue;
1061 }
1062 if(p[2] != 0x74) {
1063 /* Unsupported entity reference */
1064 *buf++ = ch;
1065 continue;
1066 }
1067 buf++;
1068 p = sc;
1069 continue;
1070 }
1071 /* Unsupported entity reference */
1072 *buf++ = ch;
1073 }
1074
1075 continue;
1076 want_more:
1077 if(have_more) {
1078 /*
1079 * We know that no more data (of the same type)
1080 * is coming. Copy the rest verbatim.
1081 */
1082 *buf++ = ch;
1083 continue;
1084 }
Lev Walkin0fab1a62005-03-09 22:19:25 +00001085 chunk_size = (p - (const char *)chunk_buf);
Lev Walkindc06f6b2004-10-20 15:50:55 +00001086 /* Processing stalled: need more data */
Lev Walkinbdaae772005-02-18 12:25:47 +00001087 break;
Lev Walkindc06f6b2004-10-20 15:50:55 +00001088 }
1089
1090 st->size = buf - st->buf;
1091 assert(st->size <= _ns);
1092 st->buf[st->size] = 0; /* Courtesy termination */
1093
1094 return chunk_size; /* Converted in full */
1095}
1096
1097/*
1098 * Decode OCTET STRING from the XML element's body.
1099 */
1100static asn_dec_rval_t
1101OCTET_STRING__decode_xer(asn_codec_ctx_t *opt_codec_ctx,
1102 asn_TYPE_descriptor_t *td, void **sptr,
Lev Walkin8c3b8542005-03-10 18:52:02 +00001103 const char *opt_mname, const void *buf_ptr, size_t size,
Lev Walkindc06f6b2004-10-20 15:50:55 +00001104 int (*opt_unexpected_tag_decoder)
Lev Walkin0fab1a62005-03-09 22:19:25 +00001105 (void *struct_ptr, const void *chunk_buf, size_t chunk_size),
Lev Walkindc06f6b2004-10-20 15:50:55 +00001106 ssize_t (*body_receiver)
Lev Walkin0fab1a62005-03-09 22:19:25 +00001107 (void *struct_ptr, const void *chunk_buf, size_t chunk_size,
Lev Walkindc06f6b2004-10-20 15:50:55 +00001108 int have_more)
1109) {
Lev Walkind5125642005-02-14 20:08:00 +00001110 OCTET_STRING_t *st = (OCTET_STRING_t *)*sptr;
Lev Walkindc06f6b2004-10-20 15:50:55 +00001111 asn_OCTET_STRING_specifics_t *specs = td->specifics
1112 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +08001113 : &asn_SPC_OCTET_STRING_specs;
Lev Walkindc06f6b2004-10-20 15:50:55 +00001114 const char *xml_tag = opt_mname ? opt_mname : td->xml_tag;
1115 asn_struct_ctx_t *ctx; /* Per-structure parser context */
Lev Walkinc61f3862005-02-14 17:21:22 +00001116 asn_dec_rval_t rval; /* Return value from the decoder */
1117 int st_allocated;
Lev Walkindc06f6b2004-10-20 15:50:55 +00001118
1119 /*
1120 * Create the string if does not exist.
1121 */
Lev Walkinc61f3862005-02-14 17:21:22 +00001122 if(!st) {
Lev Walkin8484ed82004-12-14 13:31:01 +00001123 st = (OCTET_STRING_t *)CALLOC(1, specs->struct_size);
Lev Walkin8484ed82004-12-14 13:31:01 +00001124 *sptr = (void *)st;
Lev Walkinc61f3862005-02-14 17:21:22 +00001125 if(!st) goto sta_failed;
1126 st_allocated = 1;
Lev Walkinbdaae772005-02-18 12:25:47 +00001127 } else {
1128 st_allocated = 0;
1129 }
Lev Walkinc61f3862005-02-14 17:21:22 +00001130 if(!st->buf) {
1131 /* This is separate from above section */
1132 st->buf = (uint8_t *)CALLOC(1, 1);
1133 if(!st->buf) {
1134 if(st_allocated) {
1135 *sptr = 0;
1136 goto stb_failed;
1137 } else {
1138 goto sta_failed;
1139 }
Lev Walkindc06f6b2004-10-20 15:50:55 +00001140 }
1141 }
1142
1143 /* Restore parsing context */
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001144 ctx = (asn_struct_ctx_t *)(((char *)*sptr) + specs->ctx_offset);
Lev Walkindc06f6b2004-10-20 15:50:55 +00001145
1146 return xer_decode_general(opt_codec_ctx, ctx, *sptr, xml_tag,
1147 buf_ptr, size, opt_unexpected_tag_decoder, body_receiver);
Lev Walkinc61f3862005-02-14 17:21:22 +00001148
1149stb_failed:
1150 FREEMEM(st);
1151sta_failed:
1152 rval.code = RC_FAIL;
1153 rval.consumed = 0;
1154 return rval;
Lev Walkindc06f6b2004-10-20 15:50:55 +00001155}
1156
1157/*
1158 * Decode OCTET STRING from the hexadecimal data.
1159 */
1160asn_dec_rval_t
1161OCTET_STRING_decode_xer_hex(asn_codec_ctx_t *opt_codec_ctx,
1162 asn_TYPE_descriptor_t *td, void **sptr,
Lev Walkin8c3b8542005-03-10 18:52:02 +00001163 const char *opt_mname, const void *buf_ptr, size_t size) {
Lev Walkindc06f6b2004-10-20 15:50:55 +00001164 return OCTET_STRING__decode_xer(opt_codec_ctx, td, sptr, opt_mname,
1165 buf_ptr, size, 0, OCTET_STRING__convert_hexadecimal);
1166}
1167
1168/*
1169 * Decode OCTET STRING from the binary (0/1) data.
1170 */
1171asn_dec_rval_t
1172OCTET_STRING_decode_xer_binary(asn_codec_ctx_t *opt_codec_ctx,
1173 asn_TYPE_descriptor_t *td, void **sptr,
Lev Walkin8c3b8542005-03-10 18:52:02 +00001174 const char *opt_mname, const void *buf_ptr, size_t size) {
Lev Walkindc06f6b2004-10-20 15:50:55 +00001175 return OCTET_STRING__decode_xer(opt_codec_ctx, td, sptr, opt_mname,
1176 buf_ptr, size, 0, OCTET_STRING__convert_binary);
1177}
1178
1179/*
1180 * Decode OCTET STRING from the string (ASCII/UTF-8) data.
1181 */
1182asn_dec_rval_t
1183OCTET_STRING_decode_xer_utf8(asn_codec_ctx_t *opt_codec_ctx,
1184 asn_TYPE_descriptor_t *td, void **sptr,
Lev Walkin8c3b8542005-03-10 18:52:02 +00001185 const char *opt_mname, const void *buf_ptr, size_t size) {
Lev Walkindc06f6b2004-10-20 15:50:55 +00001186 return OCTET_STRING__decode_xer(opt_codec_ctx, td, sptr, opt_mname,
1187 buf_ptr, size,
1188 OCTET_STRING__handle_control_chars,
1189 OCTET_STRING__convert_entrefs);
1190}
1191
Lev Walkin725883b2006-10-09 12:07:58 +00001192static int
Lev Walkin3a4689a2006-11-24 11:20:27 +00001193OCTET_STRING_per_get_characters(asn_per_data_t *po, uint8_t *buf,
1194 size_t units, unsigned int bpc, unsigned int unit_bits,
1195 long lb, long ub, asn_per_constraints_t *pc) {
1196 uint8_t *end = buf + units * bpc;
Lev Walkin725883b2006-10-09 12:07:58 +00001197
1198 ASN_DEBUG("Expanding %d characters into (%ld..%ld):%d",
Lev Walkin3a4689a2006-11-24 11:20:27 +00001199 (int)units, lb, ub, unit_bits);
Lev Walkin725883b2006-10-09 12:07:58 +00001200
1201 /* X.691: 27.5.4 */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001202 if((unsigned long)ub <= ((unsigned long)2 << (unit_bits - 1))) {
Lev Walkin725883b2006-10-09 12:07:58 +00001203 /* Decode without translation */
1204 lb = 0;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001205 } else if(pc && pc->code2value) {
1206 if(unit_bits > 16)
1207 return 1; /* FATAL: can't have constrained
1208 * UniversalString with more than
1209 * 16 million code points */
1210 for(; buf < end; buf += bpc) {
Lev Walkin725883b2006-10-09 12:07:58 +00001211 int value;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001212 int code = per_get_few_bits(po, unit_bits);
Lev Walkin725883b2006-10-09 12:07:58 +00001213 if(code < 0) return -1; /* WMORE */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001214 value = pc->code2value(code);
Lev Walkin725883b2006-10-09 12:07:58 +00001215 if(value < 0) {
1216 ASN_DEBUG("Code %d (0x%02x) is"
1217 " not in map (%ld..%ld)",
1218 code, code, lb, ub);
1219 return 1; /* FATAL */
1220 }
Lev Walkin3a4689a2006-11-24 11:20:27 +00001221 switch(bpc) {
1222 case 1: *buf = value; break;
1223 case 2: buf[0] = value >> 8; buf[1] = value; break;
1224 case 4: buf[0] = value >> 24; buf[1] = value >> 16;
1225 buf[2] = value >> 8; buf[3] = value; break;
1226 }
Lev Walkin725883b2006-10-09 12:07:58 +00001227 }
1228 return 0;
1229 }
1230
Lev Walkin3a4689a2006-11-24 11:20:27 +00001231 /* Shortcut the no-op copying to the aligned structure */
1232 if(lb == 0 && (unit_bits == 8 * bpc)) {
1233 return per_get_many_bits(po, buf, 0, unit_bits * units);
1234 }
1235
1236 for(; buf < end; buf += bpc) {
Lev Walkin725883b2006-10-09 12:07:58 +00001237 int code = per_get_few_bits(po, unit_bits);
1238 int ch = code + lb;
1239 if(code < 0) return -1; /* WMORE */
1240 if(ch > ub) {
1241 ASN_DEBUG("Code %d is out of range (%ld..%ld)",
1242 ch, lb, ub);
1243 return 1; /* FATAL */
1244 }
Lev Walkin3a4689a2006-11-24 11:20:27 +00001245 switch(bpc) {
1246 case 1: *buf = ch; break;
1247 case 2: buf[0] = ch >> 8; buf[1] = ch; break;
1248 case 4: buf[0] = ch >> 24; buf[1] = ch >> 16;
1249 buf[2] = ch >> 8; buf[3] = ch; break;
1250 }
Lev Walkin725883b2006-10-09 12:07:58 +00001251 }
1252
1253 return 0;
1254}
1255
1256static int
Lev Walkin3a4689a2006-11-24 11:20:27 +00001257OCTET_STRING_per_put_characters(asn_per_outp_t *po, const uint8_t *buf,
1258 size_t units, unsigned int bpc, unsigned int unit_bits,
1259 long lb, long ub, asn_per_constraints_t *pc) {
1260 const uint8_t *end = buf + units * bpc;
Lev Walkin725883b2006-10-09 12:07:58 +00001261
Lev Walkin3a4689a2006-11-24 11:20:27 +00001262 ASN_DEBUG("Squeezing %d characters into (%ld..%ld):%d (%d bpc)",
1263 (int)units, lb, ub, unit_bits, bpc);
Lev Walkin725883b2006-10-09 12:07:58 +00001264
1265 /* X.691: 27.5.4 */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001266 if((unsigned long)ub <= ((unsigned long)2 << (unit_bits - 1))) {
Lev Walkin725883b2006-10-09 12:07:58 +00001267 /* Encode as is */
1268 lb = 0;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001269 } else if(pc && pc->value2code) {
1270 for(; buf < end; buf += bpc) {
1271 int code;
1272 uint32_t value;
1273 switch(bpc) {
1274 case 1: value = *(const uint8_t *)buf; break;
1275 case 2: value = (buf[0] << 8) | buf[1]; break;
1276 case 4: value = (buf[0] << 24) | (buf[1] << 16)
1277 | (buf[2] << 8) | buf[3]; break;
1278 default: return -1;
1279 }
1280 code = pc->value2code(value);
Lev Walkin725883b2006-10-09 12:07:58 +00001281 if(code < 0) {
1282 ASN_DEBUG("Character %d (0x%02x) is"
1283 " not in map (%ld..%ld)",
1284 *buf, *buf, lb, ub);
1285 return -1;
1286 }
1287 if(per_put_few_bits(po, code, unit_bits))
1288 return -1;
1289 }
1290 }
1291
Lev Walkin3a4689a2006-11-24 11:20:27 +00001292 /* Shortcut the no-op copying to the aligned structure */
1293 if(lb == 0 && (unit_bits == 8 * bpc)) {
1294 return per_put_many_bits(po, buf, unit_bits * units);
1295 }
1296
1297 for(ub -= lb; buf < end; buf += bpc) {
1298 int ch;
1299 uint32_t value;
1300 switch(bpc) {
1301 case 1: value = *(const uint8_t *)buf; break;
1302 case 2: value = (buf[0] << 8) | buf[1]; break;
1303 case 4: value = (buf[0] << 24) | (buf[1] << 16)
1304 | (buf[2] << 8) | buf[3]; break;
1305 default: return -1;
1306 }
1307 ch = value - lb;
Lev Walkin725883b2006-10-09 12:07:58 +00001308 if(ch < 0 || ch > ub) {
1309 ASN_DEBUG("Character %d (0x%02x)"
1310 " is out of range (%ld..%ld)",
1311 *buf, *buf, lb, ub + lb);
1312 return -1;
1313 }
1314 if(per_put_few_bits(po, ch, unit_bits))
1315 return -1;
1316 }
1317
1318 return 0;
1319}
1320
Lev Walkin59b176e2005-11-26 11:25:14 +00001321asn_dec_rval_t
1322OCTET_STRING_decode_uper(asn_codec_ctx_t *opt_codec_ctx,
1323 asn_TYPE_descriptor_t *td, asn_per_constraints_t *constraints,
1324 void **sptr, asn_per_data_t *pd) {
1325
1326 asn_OCTET_STRING_specifics_t *specs = td->specifics
1327 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +08001328 : &asn_SPC_OCTET_STRING_specs;
Lev Walkin725883b2006-10-09 12:07:58 +00001329 asn_per_constraints_t *pc = constraints ? constraints
1330 : td->per_constraints;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001331 asn_per_constraint_t *cval;
1332 asn_per_constraint_t *csiz;
Lev Walkin59b176e2005-11-26 11:25:14 +00001333 asn_dec_rval_t rval = { RC_OK, 0 };
1334 BIT_STRING_t *st = (BIT_STRING_t *)*sptr;
1335 ssize_t consumed_myself = 0;
1336 int repeat;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001337 enum {
1338 OS__BPC_BIT = 0,
1339 OS__BPC_CHAR = 1,
1340 OS__BPC_U16 = 2,
1341 OS__BPC_U32 = 4
1342 } bpc; /* Bytes per character */
1343 unsigned int unit_bits;
1344 unsigned int canonical_unit_bits;
Lev Walkin59b176e2005-11-26 11:25:14 +00001345
1346 (void)opt_codec_ctx;
1347
Lev Walkin3a4689a2006-11-24 11:20:27 +00001348 if(pc) {
1349 cval = &pc->value;
1350 csiz = &pc->size;
1351 } else {
1352 cval = &asn_DEF_OCTET_STRING_constraints.value;
1353 csiz = &asn_DEF_OCTET_STRING_constraints.size;
1354 }
1355
1356 switch(specs->subvariant) {
1357 default:
1358 case ASN_OSUBV_ANY:
1359 ASN_DEBUG("Unrecognized subvariant %d", specs->subvariant);
1360 RETURN(RC_FAIL);
1361 case ASN_OSUBV_BIT:
1362 canonical_unit_bits = unit_bits = 1;
1363 bpc = OS__BPC_BIT;
1364 break;
1365 case ASN_OSUBV_STR:
1366 canonical_unit_bits = unit_bits = 8;
1367 if(cval->flags & APC_CONSTRAINED)
1368 unit_bits = cval->range_bits;
1369 bpc = OS__BPC_CHAR;
1370 break;
1371 case ASN_OSUBV_U16:
1372 canonical_unit_bits = unit_bits = 16;
1373 if(cval->flags & APC_CONSTRAINED)
1374 unit_bits = cval->range_bits;
1375 bpc = OS__BPC_U16;
1376 break;
1377 case ASN_OSUBV_U32:
1378 canonical_unit_bits = unit_bits = 32;
1379 if(cval->flags & APC_CONSTRAINED)
1380 unit_bits = cval->range_bits;
1381 bpc = OS__BPC_U32;
1382 break;
1383 }
1384
Lev Walkin59b176e2005-11-26 11:25:14 +00001385 /*
1386 * Allocate the string.
1387 */
1388 if(!st) {
1389 st = (BIT_STRING_t *)(*sptr = CALLOC(1, specs->struct_size));
1390 if(!st) RETURN(RC_FAIL);
1391 }
1392
Lev Walkin725883b2006-10-09 12:07:58 +00001393 ASN_DEBUG("PER Decoding %s size %ld .. %ld bits %d",
Lev Walkin3a4689a2006-11-24 11:20:27 +00001394 csiz->flags & APC_EXTENSIBLE ? "extensible" : "non-extensible",
1395 csiz->lower_bound, csiz->upper_bound, csiz->effective_bits);
Lev Walkince676dd2005-12-20 22:34:55 +00001396
Lev Walkin3a4689a2006-11-24 11:20:27 +00001397 if(csiz->flags & APC_EXTENSIBLE) {
Lev Walkin59b176e2005-11-26 11:25:14 +00001398 int inext = per_get_few_bits(pd, 1);
Lev Walkin0a8aa602006-09-18 20:05:55 +00001399 if(inext < 0) RETURN(RC_WMORE);
Lev Walkin3a4689a2006-11-24 11:20:27 +00001400 if(inext) {
1401 csiz = &asn_DEF_OCTET_STRING_constraints.size;
1402 cval = &asn_DEF_OCTET_STRING_constraints.value;
1403 unit_bits = canonical_unit_bits;
1404 }
Lev Walkin59b176e2005-11-26 11:25:14 +00001405 }
1406
Lev Walkin3a4689a2006-11-24 11:20:27 +00001407 if(csiz->effective_bits >= 0) {
Lev Walkin59b176e2005-11-26 11:25:14 +00001408 FREEMEM(st->buf);
Lev Walkin3a4689a2006-11-24 11:20:27 +00001409 if(bpc) {
1410 st->size = csiz->upper_bound * bpc;
Lev Walkin59b176e2005-11-26 11:25:14 +00001411 } else {
Lev Walkin3a4689a2006-11-24 11:20:27 +00001412 st->size = (csiz->upper_bound + 7) >> 3;
Lev Walkin59b176e2005-11-26 11:25:14 +00001413 }
1414 st->buf = (uint8_t *)MALLOC(st->size + 1);
1415 if(!st->buf) { st->size = 0; RETURN(RC_FAIL); }
1416 }
1417
1418 /* X.691, #16.5: zero-length encoding */
1419 /* X.691, #16.6: short fixed length encoding (up to 2 octets) */
1420 /* X.691, #16.7: long fixed length encoding (up to 64K octets) */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001421 if(csiz->effective_bits == 0) {
Lev Walkin725883b2006-10-09 12:07:58 +00001422 int ret;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001423 if(bpc) {
1424 ASN_DEBUG("Encoding OCTET STRING size %ld",
1425 csiz->upper_bound);
1426 ret = OCTET_STRING_per_get_characters(pd, st->buf,
1427 csiz->upper_bound, bpc, unit_bits,
1428 cval->lower_bound, cval->upper_bound, pc);
Lev Walkin725883b2006-10-09 12:07:58 +00001429 if(ret > 0) RETURN(RC_FAIL);
1430 } else {
Lev Walkin3a4689a2006-11-24 11:20:27 +00001431 ASN_DEBUG("Encoding BIT STRING size %ld",
1432 csiz->upper_bound);
Lev Walkin725883b2006-10-09 12:07:58 +00001433 ret = per_get_many_bits(pd, st->buf, 0,
Lev Walkin3a4689a2006-11-24 11:20:27 +00001434 unit_bits * csiz->upper_bound);
Lev Walkin725883b2006-10-09 12:07:58 +00001435 }
Lev Walkin0a8aa602006-09-18 20:05:55 +00001436 if(ret < 0) RETURN(RC_WMORE);
Lev Walkin3a4689a2006-11-24 11:20:27 +00001437 consumed_myself += unit_bits * csiz->upper_bound;
Lev Walkin59b176e2005-11-26 11:25:14 +00001438 st->buf[st->size] = 0;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001439 if(bpc == 0) {
1440 int ubs = (csiz->upper_bound & 0x7);
1441 st->bits_unused = ubs ? 8 - ubs : 0;
1442 }
Lev Walkin59b176e2005-11-26 11:25:14 +00001443 RETURN(RC_OK);
1444 }
1445
1446 st->size = 0;
1447 do {
Lev Walkin3a4689a2006-11-24 11:20:27 +00001448 ssize_t raw_len;
Lev Walkin59b176e2005-11-26 11:25:14 +00001449 ssize_t len_bytes;
1450 ssize_t len_bits;
1451 void *p;
1452 int ret;
1453
1454 /* Get the PER length */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001455 raw_len = uper_get_length(pd, csiz->effective_bits, &repeat);
1456 if(raw_len < 0) RETURN(RC_WMORE);
1457 raw_len += csiz->lower_bound;
Lev Walkin59b176e2005-11-26 11:25:14 +00001458
Lev Walkin0a8aa602006-09-18 20:05:55 +00001459 ASN_DEBUG("Got PER length eb %ld, len %ld, %s (%s)",
Lev Walkin3a4689a2006-11-24 11:20:27 +00001460 (long)csiz->effective_bits, (long)raw_len,
Lev Walkin0a8aa602006-09-18 20:05:55 +00001461 repeat ? "repeat" : "once", td->name);
Lev Walkin3a4689a2006-11-24 11:20:27 +00001462 if(bpc) {
1463 len_bytes = raw_len * bpc;
1464 len_bits = len_bytes * unit_bits;
1465 } else {
1466 len_bits = raw_len;
Lev Walkin59b176e2005-11-26 11:25:14 +00001467 len_bytes = (len_bits + 7) >> 3;
1468 if(len_bits & 0x7)
1469 st->bits_unused = 8 - (len_bits & 0x7);
1470 /* len_bits be multiple of 16K if repeat is set */
Lev Walkin59b176e2005-11-26 11:25:14 +00001471 }
1472 p = REALLOC(st->buf, st->size + len_bytes + 1);
1473 if(!p) RETURN(RC_FAIL);
1474 st->buf = (uint8_t *)p;
1475
Lev Walkin3a4689a2006-11-24 11:20:27 +00001476 if(bpc) {
1477 ret = OCTET_STRING_per_get_characters(pd,
1478 &st->buf[st->size], raw_len, bpc, unit_bits,
1479 cval->lower_bound, cval->upper_bound, pc);
Lev Walkin725883b2006-10-09 12:07:58 +00001480 if(ret > 0) RETURN(RC_FAIL);
1481 } else {
1482 ret = per_get_many_bits(pd, &st->buf[st->size],
1483 0, len_bits);
1484 }
Lev Walkin0a8aa602006-09-18 20:05:55 +00001485 if(ret < 0) RETURN(RC_WMORE);
Lev Walkin59b176e2005-11-26 11:25:14 +00001486 st->size += len_bytes;
1487 } while(repeat);
1488 st->buf[st->size] = 0; /* nul-terminate */
1489
1490 return rval;
1491}
Lev Walkindc06f6b2004-10-20 15:50:55 +00001492
Lev Walkin523de9e2006-08-18 01:34:18 +00001493asn_enc_rval_t
1494OCTET_STRING_encode_uper(asn_TYPE_descriptor_t *td,
1495 asn_per_constraints_t *constraints, void *sptr, asn_per_outp_t *po) {
1496
1497 asn_OCTET_STRING_specifics_t *specs = td->specifics
1498 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +08001499 : &asn_SPC_OCTET_STRING_specs;
Lev Walkin725883b2006-10-09 12:07:58 +00001500 asn_per_constraints_t *pc = constraints ? constraints
1501 : td->per_constraints;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001502 asn_per_constraint_t *cval;
1503 asn_per_constraint_t *csiz;
Lev Walkin523de9e2006-08-18 01:34:18 +00001504 const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001505 asn_enc_rval_t er = { 0, 0, 0 };
Lev Walkin523de9e2006-08-18 01:34:18 +00001506 int inext = 0; /* Lies not within extension root */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001507 unsigned int unit_bits;
1508 unsigned int canonical_unit_bits;
1509 unsigned int sizeinunits;
Lev Walkin523de9e2006-08-18 01:34:18 +00001510 const uint8_t *buf;
1511 int ret;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001512 enum {
1513 OS__BPC_BIT = 0,
1514 OS__BPC_CHAR = 1,
1515 OS__BPC_U16 = 2,
1516 OS__BPC_U32 = 4
1517 } bpc; /* Bytes per character */
1518 int ct_extensible;
Lev Walkin523de9e2006-08-18 01:34:18 +00001519
Lev Walkin2ddd3082006-10-16 12:32:14 +00001520 if(!st || (!st->buf && st->size))
Lev Walkin7c1dc052016-03-14 03:08:15 -07001521 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001522
Lev Walkin3a4689a2006-11-24 11:20:27 +00001523 if(pc) {
1524 cval = &pc->value;
1525 csiz = &pc->size;
1526 } else {
1527 cval = &asn_DEF_OCTET_STRING_constraints.value;
1528 csiz = &asn_DEF_OCTET_STRING_constraints.size;
1529 }
1530 ct_extensible = csiz->flags & APC_EXTENSIBLE;
1531
1532 switch(specs->subvariant) {
1533 default:
1534 case ASN_OSUBV_ANY:
Lev Walkin7c1dc052016-03-14 03:08:15 -07001535 ASN__ENCODE_FAILED;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001536 case ASN_OSUBV_BIT:
1537 canonical_unit_bits = unit_bits = 1;
1538 bpc = OS__BPC_BIT;
1539 sizeinunits = st->size * 8 - (st->bits_unused & 0x07);
Lev Walkin523de9e2006-08-18 01:34:18 +00001540 ASN_DEBUG("BIT STRING of %d bytes, %d bits unused",
1541 sizeinunits, st->bits_unused);
Lev Walkin3a4689a2006-11-24 11:20:27 +00001542 break;
1543 case ASN_OSUBV_STR:
1544 canonical_unit_bits = unit_bits = 8;
1545 if(cval->flags & APC_CONSTRAINED)
1546 unit_bits = cval->range_bits;
1547 bpc = OS__BPC_CHAR;
1548 sizeinunits = st->size;
1549 break;
1550 case ASN_OSUBV_U16:
1551 canonical_unit_bits = unit_bits = 16;
1552 if(cval->flags & APC_CONSTRAINED)
1553 unit_bits = cval->range_bits;
1554 bpc = OS__BPC_U16;
1555 sizeinunits = st->size / 2;
1556 break;
1557 case ASN_OSUBV_U32:
1558 canonical_unit_bits = unit_bits = 32;
1559 if(cval->flags & APC_CONSTRAINED)
1560 unit_bits = cval->range_bits;
1561 bpc = OS__BPC_U32;
1562 sizeinunits = st->size / 4;
1563 break;
Lev Walkin725883b2006-10-09 12:07:58 +00001564 }
1565
Lev Walkin00918812006-09-18 21:19:32 +00001566 ASN_DEBUG("Encoding %s into %d units of %d bits"
Lev Walkin725883b2006-10-09 12:07:58 +00001567 " (%ld..%ld, effective %d)%s",
Lev Walkin00918812006-09-18 21:19:32 +00001568 td->name, sizeinunits, unit_bits,
Lev Walkin3a4689a2006-11-24 11:20:27 +00001569 csiz->lower_bound, csiz->upper_bound,
1570 csiz->effective_bits, ct_extensible ? " EXT" : "");
Lev Walkin523de9e2006-08-18 01:34:18 +00001571
Lev Walkind4c16732013-03-28 05:00:47 -07001572 /* Figure out whether size lies within PER visible constraint */
Lev Walkin523de9e2006-08-18 01:34:18 +00001573
Lev Walkin3a4689a2006-11-24 11:20:27 +00001574 if(csiz->effective_bits >= 0) {
1575 if((int)sizeinunits < csiz->lower_bound
1576 || (int)sizeinunits > csiz->upper_bound) {
Lev Walkin523de9e2006-08-18 01:34:18 +00001577 if(ct_extensible) {
Lev Walkin3a4689a2006-11-24 11:20:27 +00001578 cval = &asn_DEF_OCTET_STRING_constraints.value;
1579 csiz = &asn_DEF_OCTET_STRING_constraints.size;
1580 unit_bits = canonical_unit_bits;
Lev Walkin523de9e2006-08-18 01:34:18 +00001581 inext = 1;
1582 } else
Lev Walkin7c1dc052016-03-14 03:08:15 -07001583 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001584 }
1585 } else {
1586 inext = 0;
1587 }
1588
1589 if(ct_extensible) {
1590 /* Declare whether length is [not] within extension root */
1591 if(per_put_few_bits(po, inext, 1))
Lev Walkin7c1dc052016-03-14 03:08:15 -07001592 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001593 }
1594
1595 /* X.691, #16.5: zero-length encoding */
1596 /* X.691, #16.6: short fixed length encoding (up to 2 octets) */
1597 /* X.691, #16.7: long fixed length encoding (up to 64K octets) */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001598 if(csiz->effective_bits >= 0) {
Lev Walkin523de9e2006-08-18 01:34:18 +00001599 ASN_DEBUG("Encoding %d bytes (%ld), length in %d bits",
Lev Walkin3a4689a2006-11-24 11:20:27 +00001600 st->size, sizeinunits - csiz->lower_bound,
1601 csiz->effective_bits);
1602 ret = per_put_few_bits(po, sizeinunits - csiz->lower_bound,
1603 csiz->effective_bits);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001604 if(ret) ASN__ENCODE_FAILED;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001605 if(bpc) {
1606 ret = OCTET_STRING_per_put_characters(po, st->buf,
1607 sizeinunits, bpc, unit_bits,
1608 cval->lower_bound, cval->upper_bound, pc);
Lev Walkin725883b2006-10-09 12:07:58 +00001609 } else {
1610 ret = per_put_many_bits(po, st->buf,
1611 sizeinunits * unit_bits);
1612 }
Lev Walkin7c1dc052016-03-14 03:08:15 -07001613 if(ret) ASN__ENCODE_FAILED;
1614 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +00001615 }
1616
1617 ASN_DEBUG("Encoding %d bytes", st->size);
1618
1619 if(sizeinunits == 0) {
1620 if(uper_put_length(po, 0))
Lev Walkin7c1dc052016-03-14 03:08:15 -07001621 ASN__ENCODE_FAILED;
1622 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +00001623 }
1624
1625 buf = st->buf;
1626 while(sizeinunits) {
1627 ssize_t maySave = uper_put_length(po, sizeinunits);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001628 if(maySave < 0) ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001629
Lev Walkin345e4c62006-10-19 02:44:08 +00001630 ASN_DEBUG("Encoding %ld of %ld",
1631 (long)maySave, (long)sizeinunits);
Lev Walkin523de9e2006-08-18 01:34:18 +00001632
Lev Walkin3a4689a2006-11-24 11:20:27 +00001633 if(bpc) {
1634 ret = OCTET_STRING_per_put_characters(po, buf,
1635 maySave, bpc, unit_bits,
1636 cval->lower_bound, cval->upper_bound, pc);
Lev Walkin725883b2006-10-09 12:07:58 +00001637 } else {
1638 ret = per_put_many_bits(po, buf, maySave * unit_bits);
1639 }
Lev Walkin7c1dc052016-03-14 03:08:15 -07001640 if(ret) ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001641
Lev Walkin3a4689a2006-11-24 11:20:27 +00001642 if(bpc)
1643 buf += maySave * bpc;
Lev Walkin523de9e2006-08-18 01:34:18 +00001644 else
Lev Walkin3a4689a2006-11-24 11:20:27 +00001645 buf += maySave >> 3;
Lev Walkin523de9e2006-08-18 01:34:18 +00001646 sizeinunits -= maySave;
1647 assert(!(maySave & 0x07) || !sizeinunits);
1648 }
1649
Lev Walkin7c1dc052016-03-14 03:08:15 -07001650 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +00001651}
1652
Lev Walkinf15320b2004-06-03 03:38:44 +00001653int
Lev Walkinde4825d2004-09-29 13:20:14 +00001654OCTET_STRING_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +00001655 asn_app_consume_bytes_f *cb, void *app_key) {
Wim Lewis59e8d282014-08-04 12:39:35 -07001656 const char * const h2c = "0123456789ABCDEF";
Lev Walkinc2346572004-08-11 09:07:36 +00001657 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +00001658 char scratch[16 * 3 + 4];
1659 char *p = scratch;
1660 uint8_t *buf;
1661 uint8_t *end;
1662 size_t i;
Lev Walkinf15320b2004-06-03 03:38:44 +00001663
Lev Walkind9bd7752004-06-05 08:17:50 +00001664 (void)td; /* Unused argument */
1665
Lev Walkin2ddd3082006-10-16 12:32:14 +00001666 if(!st || (!st->buf && st->size))
1667 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001668
1669 /*
1670 * Dump the contents of the buffer in hexadecimal.
1671 */
1672 buf = st->buf;
1673 end = buf + st->size;
1674 for(i = 0; buf < end; buf++, i++) {
1675 if(!(i % 16) && (i || st->size > 16)) {
Lev Walkin8e8078a2004-09-26 13:10:40 +00001676 if(cb(scratch, p - scratch, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +00001677 return -1;
Lev Walkin8e8078a2004-09-26 13:10:40 +00001678 _i_INDENT(1);
Lev Walkinf15320b2004-06-03 03:38:44 +00001679 p = scratch;
1680 }
1681 *p++ = h2c[(*buf >> 4) & 0x0F];
1682 *p++ = h2c[*buf & 0x0F];
Lev Walkina9cc46e2004-09-22 16:06:28 +00001683 *p++ = 0x20;
Lev Walkinf15320b2004-06-03 03:38:44 +00001684 }
1685
Lev Walkincc6a9102004-09-23 22:06:26 +00001686 if(p > scratch) {
1687 p--; /* Remove the tail space */
Lev Walkin8e8078a2004-09-26 13:10:40 +00001688 if(cb(scratch, p - scratch, app_key) < 0)
Lev Walkincc6a9102004-09-23 22:06:26 +00001689 return -1;
1690 }
1691
1692 return 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001693}
1694
1695int
Lev Walkindc06f6b2004-10-20 15:50:55 +00001696OCTET_STRING_print_utf8(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +00001697 int ilevel, asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +00001698 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +00001699
Lev Walkind9bd7752004-06-05 08:17:50 +00001700 (void)td; /* Unused argument */
1701 (void)ilevel; /* Unused argument */
1702
Lev Walkin2ddd3082006-10-16 12:32:14 +00001703 if(st && (st->buf || !st->size)) {
Lev Walkin8e8078a2004-09-26 13:10:40 +00001704 return (cb(st->buf, st->size, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001705 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +00001706 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001707 }
1708}
1709
1710void
Lev Walkinde4825d2004-09-29 13:20:14 +00001711OCTET_STRING_free(asn_TYPE_descriptor_t *td, void *sptr, int contents_only) {
Lev Walkinc2346572004-08-11 09:07:36 +00001712 OCTET_STRING_t *st = (OCTET_STRING_t *)sptr;
Lev Walkin034ee192016-03-14 02:28:33 -07001713 asn_OCTET_STRING_specifics_t *specs;
1714 asn_struct_ctx_t *ctx;
Lev Walkinb54577a2004-11-08 10:47:12 +00001715 struct _stack *stck;
Lev Walkinf15320b2004-06-03 03:38:44 +00001716
1717 if(!td || !st)
1718 return;
1719
Lev Walkin034ee192016-03-14 02:28:33 -07001720 specs = td->specifics
1721 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +08001722 : &asn_SPC_OCTET_STRING_specs;
Lev Walkin034ee192016-03-14 02:28:33 -07001723 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
1724
Lev Walkinf15320b2004-06-03 03:38:44 +00001725 ASN_DEBUG("Freeing %s as OCTET STRING", td->name);
1726
1727 if(st->buf) {
1728 FREEMEM(st->buf);
Lev Walkin2ddd3082006-10-16 12:32:14 +00001729 st->buf = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001730 }
1731
1732 /*
1733 * Remove decode-time stack.
1734 */
Lev Walkinb54577a2004-11-08 10:47:12 +00001735 stck = (struct _stack *)ctx->ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +00001736 if(stck) {
1737 while(stck->tail) {
1738 struct _stack_el *sel = stck->tail;
1739 stck->tail = sel->prev;
1740 FREEMEM(sel);
1741 }
1742 FREEMEM(stck);
1743 }
1744
1745 if(!contents_only) {
1746 FREEMEM(st);
1747 }
1748}
1749
1750/*
1751 * Conversion routines.
1752 */
1753int
1754OCTET_STRING_fromBuf(OCTET_STRING_t *st, const char *str, int len) {
1755 void *buf;
1756
1757 if(st == 0 || (str == 0 && len)) {
1758 errno = EINVAL;
1759 return -1;
1760 }
1761
1762 /*
1763 * Clear the OCTET STRING.
1764 */
1765 if(str == NULL) {
Lev Walkin7b284812005-12-17 11:43:25 +00001766 FREEMEM(st->buf);
1767 st->buf = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001768 st->size = 0;
1769 return 0;
1770 }
1771
1772 /* Determine the original string size, if not explicitly given */
1773 if(len < 0)
1774 len = strlen(str);
1775
1776 /* Allocate and fill the memory */
1777 buf = MALLOC(len + 1);
Lev Walkin7b284812005-12-17 11:43:25 +00001778 if(buf == NULL)
Lev Walkinf15320b2004-06-03 03:38:44 +00001779 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +00001780
1781 memcpy(buf, str, len);
Lev Walkin7b284812005-12-17 11:43:25 +00001782 ((uint8_t *)buf)[len] = '\0'; /* Couldn't use memcpy(len+1)! */
1783 FREEMEM(st->buf);
1784 st->buf = (uint8_t *)buf;
1785 st->size = len;
Lev Walkinf15320b2004-06-03 03:38:44 +00001786
1787 return 0;
1788}
1789
1790OCTET_STRING_t *
Lev Walkinbbd93252004-10-12 05:57:23 +00001791OCTET_STRING_new_fromBuf(asn_TYPE_descriptor_t *td, const char *str, int len) {
1792 asn_OCTET_STRING_specifics_t *specs = td->specifics
Lev Walkindc06f6b2004-10-20 15:50:55 +00001793 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +08001794 : &asn_SPC_OCTET_STRING_specs;
Lev Walkinf15320b2004-06-03 03:38:44 +00001795 OCTET_STRING_t *st;
1796
Lev Walkinbbd93252004-10-12 05:57:23 +00001797 st = (OCTET_STRING_t *)CALLOC(1, specs->struct_size);
Lev Walkinf15320b2004-06-03 03:38:44 +00001798 if(st && str && OCTET_STRING_fromBuf(st, str, len)) {
Lev Walkin419f6752006-09-13 04:02:00 +00001799 FREEMEM(st);
Lev Walkinf15320b2004-06-03 03:38:44 +00001800 st = NULL;
1801 }
1802
1803 return st;
1804}
1805