blob: fa3d65e29c4e135bc1b2b3f98f07f53dea6dfc90 [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 Walkincc159472017-07-06 08:26:36 -070044#ifdef ASN_DISABLE_OER_SUPPORT
45 0,
46 0,
47#else
48 0,
49 0,
50#endif /* ASN_DISABLE_OER_SUPPORT */
Lev Walkinf15320b2004-06-03 03:38:44 +000051 0, /* Use generic outmost tag fetcher */
Lev Walkinde4825d2004-09-29 13:20:14 +000052 asn_DEF_OCTET_STRING_tags,
53 sizeof(asn_DEF_OCTET_STRING_tags)
54 / sizeof(asn_DEF_OCTET_STRING_tags[0]),
55 asn_DEF_OCTET_STRING_tags, /* Same as above */
56 sizeof(asn_DEF_OCTET_STRING_tags)
57 / sizeof(asn_DEF_OCTET_STRING_tags[0]),
Lev Walkin59b176e2005-11-26 11:25:14 +000058 0, /* No PER visible constraints */
Lev Walkin449f8322004-08-20 13:23:42 +000059 0, 0, /* No members */
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +080060 &asn_SPC_OCTET_STRING_specs
Lev Walkinf15320b2004-06-03 03:38:44 +000061};
62
Lev Walkincc6a9102004-09-23 22:06:26 +000063#undef _CH_PHASE
64#undef NEXT_PHASE
65#undef PREV_PHASE
Lev Walkin75b1bef2005-04-25 19:38:21 +000066#define _CH_PHASE(ctx, inc) do { \
67 if(ctx->phase == 0) \
68 ctx->context = 0; \
69 ctx->phase += inc; \
Lev Walkinf15320b2004-06-03 03:38:44 +000070 } while(0)
71#define NEXT_PHASE(ctx) _CH_PHASE(ctx, +1)
72#define PREV_PHASE(ctx) _CH_PHASE(ctx, -1)
73
Lev Walkincc6a9102004-09-23 22:06:26 +000074#undef ADVANCE
Lev Walkin75b1bef2005-04-25 19:38:21 +000075#define ADVANCE(num_bytes) do { \
76 size_t num = (num_bytes); \
77 buf_ptr = ((const char *)buf_ptr) + num; \
78 size -= num; \
79 consumed_myself += num; \
Lev Walkinf15320b2004-06-03 03:38:44 +000080 } while(0)
81
Lev Walkincc6a9102004-09-23 22:06:26 +000082#undef RETURN
Lev Walkin75b1bef2005-04-25 19:38:21 +000083#define RETURN(_code) do { \
Lev Walkin59b176e2005-11-26 11:25:14 +000084 asn_dec_rval_t tmprval; \
85 tmprval.code = _code; \
86 tmprval.consumed = consumed_myself; \
87 return tmprval; \
Lev Walkinf15320b2004-06-03 03:38:44 +000088 } while(0)
89
Lev Walkincc6a9102004-09-23 22:06:26 +000090#undef APPEND
Lev Walkind9bd7752004-06-05 08:17:50 +000091#define APPEND(bufptr, bufsize) do { \
Lev Walkin75b1bef2005-04-25 19:38:21 +000092 size_t _bs = (bufsize); /* Append size */ \
93 size_t _ns = ctx->context; /* Allocated now */ \
94 size_t _es = st->size + _bs; /* Expected size */ \
95 /* int is really a typeof(st->size): */ \
96 if((int)_es < 0) RETURN(RC_FAIL); \
97 if(_ns <= _es) { \
Lev Walkind9bd7752004-06-05 08:17:50 +000098 void *ptr; \
Lev Walkin188ed2c2004-09-13 08:31:01 +000099 /* Be nice and round to the memory allocator */ \
Lev Walkin75b1bef2005-04-25 19:38:21 +0000100 do { _ns = _ns ? _ns << 1 : 16; } \
101 while(_ns <= _es); \
102 /* int is really a typeof(st->size): */ \
103 if((int)_ns < 0) RETURN(RC_FAIL); \
Lev Walkind9bd7752004-06-05 08:17:50 +0000104 ptr = REALLOC(st->buf, _ns); \
105 if(ptr) { \
Lev Walkinc2346572004-08-11 09:07:36 +0000106 st->buf = (uint8_t *)ptr; \
Lev Walkin75b1bef2005-04-25 19:38:21 +0000107 ctx->context = _ns; \
Lev Walkind9bd7752004-06-05 08:17:50 +0000108 } else { \
109 RETURN(RC_FAIL); \
110 } \
Lev Walkin7f85ef42005-07-02 20:24:27 +0000111 ASN_DEBUG("Reallocating into %ld", (long)_ns); \
Lev Walkind9bd7752004-06-05 08:17:50 +0000112 } \
Lev Walkin8d127872004-09-04 04:44:50 +0000113 memcpy(st->buf + st->size, bufptr, _bs); \
Lev Walkind9bd7752004-06-05 08:17:50 +0000114 /* Convenient nul-termination */ \
Lev Walkin75b1bef2005-04-25 19:38:21 +0000115 st->buf[_es] = '\0'; \
116 st->size = _es; \
Lev Walkinf15320b2004-06-03 03:38:44 +0000117 } while(0)
118
119/*
120 * The main reason why ASN.1 is still alive is that too much time and effort
121 * is necessary for learning it more or less adequately, thus creating a gut
122 * necessity to demonstrate that aquired skill everywhere afterwards.
123 * No, I am not going to explain what the following stuff is.
124 */
125struct _stack_el {
Lev Walkin3990ba62004-09-24 20:57:41 +0000126 ber_tlv_len_t left; /* What's left to read (or -1) */
Lev Walkin5c915992004-09-27 20:54:06 +0000127 ber_tlv_len_t got; /* What was actually processed */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000128 int cont_level; /* Depth of subcontainment */
Lev Walkinf15320b2004-06-03 03:38:44 +0000129 int want_nulls; /* Want null "end of content" octets? */
130 int bits_chopped; /* Flag in BIT STRING mode */
Lev Walkin5c915992004-09-27 20:54:06 +0000131 ber_tlv_tag_t tag; /* For debugging purposes */
Lev Walkinf15320b2004-06-03 03:38:44 +0000132 struct _stack_el *prev;
133 struct _stack_el *next;
134};
135struct _stack {
136 struct _stack_el *tail;
137 struct _stack_el *cur_ptr;
138};
139
140static struct _stack_el *
Lev Walkin3990ba62004-09-24 20:57:41 +0000141OS__add_stack_el(struct _stack *st) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000142 struct _stack_el *nel;
143
Lev Walkin188ed2c2004-09-13 08:31:01 +0000144 /*
145 * Reuse the old stack frame or allocate a new one.
146 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000147 if(st->cur_ptr && st->cur_ptr->next) {
148 nel = st->cur_ptr->next;
Lev Walkinf15320b2004-06-03 03:38:44 +0000149 nel->bits_chopped = 0;
Lev Walkin5c915992004-09-27 20:54:06 +0000150 nel->got = 0;
151 /* Retain the nel->cont_level, it's correct. */
Lev Walkinf15320b2004-06-03 03:38:44 +0000152 } else {
Lev Walkin814cca72004-12-15 23:23:53 +0000153 nel = (struct _stack_el *)CALLOC(1, sizeof(struct _stack_el));
Lev Walkinf15320b2004-06-03 03:38:44 +0000154 if(nel == NULL)
155 return NULL;
156
157 if(st->tail) {
Lev Walkin188ed2c2004-09-13 08:31:01 +0000158 /* Increase a subcontainment depth */
159 nel->cont_level = st->tail->cont_level + 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000160 st->tail->next = nel;
161 }
162 nel->prev = st->tail;
163 st->tail = nel;
164 }
165
166 st->cur_ptr = nel;
167
168 return nel;
169}
170
171static struct _stack *
johvik28268d92017-05-09 10:27:52 +0200172_new_stack(void) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000173 return (struct _stack *)CALLOC(1, sizeof(struct _stack));
Lev Walkinf15320b2004-06-03 03:38:44 +0000174}
175
176/*
177 * Decode OCTET STRING type.
178 */
Lev Walkindc06f6b2004-10-20 15:50:55 +0000179asn_dec_rval_t
Lev Walkinde4825d2004-09-29 13:20:14 +0000180OCTET_STRING_decode_ber(asn_codec_ctx_t *opt_codec_ctx,
181 asn_TYPE_descriptor_t *td,
Lev Walkin59b176e2005-11-26 11:25:14 +0000182 void **sptr, const void *buf_ptr, size_t size, int tag_mode) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000183 asn_OCTET_STRING_specifics_t *specs = td->specifics
Lev Walkindc06f6b2004-10-20 15:50:55 +0000184 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +0800185 : &asn_SPC_OCTET_STRING_specs;
Lev Walkin59b176e2005-11-26 11:25:14 +0000186 BIT_STRING_t *st = (BIT_STRING_t *)*sptr;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000187 asn_dec_rval_t rval;
Lev Walkinde4825d2004-09-29 13:20:14 +0000188 asn_struct_ctx_t *ctx;
Lev Walkinf15320b2004-06-03 03:38:44 +0000189 ssize_t consumed_myself = 0;
Lev Walkinbbd93252004-10-12 05:57:23 +0000190 struct _stack *stck; /* Expectations stack structure */
Lev Walkin5c915992004-09-27 20:54:06 +0000191 struct _stack_el *sel = 0; /* Stack element */
Lev Walkinf15320b2004-06-03 03:38:44 +0000192 int tlv_constr;
Lev Walkin3a4689a2006-11-24 11:20:27 +0000193 enum asn_OS_Subvariant type_variant = specs->subvariant;
Lev Walkinf15320b2004-06-03 03:38:44 +0000194
Lev Walkincc6a9102004-09-23 22:06:26 +0000195 ASN_DEBUG("Decoding %s as %s (frame %ld)",
196 td->name,
Lev Walkin3a4689a2006-11-24 11:20:27 +0000197 (type_variant == ASN_OSUBV_STR) ?
Lev Walkinbbd93252004-10-12 05:57:23 +0000198 "OCTET STRING" : "OS-SpecialCase",
Lev Walkincc6a9102004-09-23 22:06:26 +0000199 (long)size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000200
201 /*
202 * Create the string if does not exist.
203 */
204 if(st == NULL) {
Lev Walkin59b176e2005-11-26 11:25:14 +0000205 st = (BIT_STRING_t *)(*sptr = CALLOC(1, specs->struct_size));
206 if(st == NULL) RETURN(RC_FAIL);
Lev Walkinf15320b2004-06-03 03:38:44 +0000207 }
208
209 /* Restore parsing context */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800210 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000211
212 switch(ctx->phase) {
213 case 0:
214 /*
215 * Check tags.
216 */
Lev Walkinde4825d2004-09-29 13:20:14 +0000217 rval = ber_check_tags(opt_codec_ctx, td, ctx,
Lev Walkin8e8078a2004-09-26 13:10:40 +0000218 buf_ptr, size, tag_mode, -1,
Lev Walkinf15320b2004-06-03 03:38:44 +0000219 &ctx->left, &tlv_constr);
Lev Walkin443d2512004-10-05 06:35:31 +0000220 if(rval.code != RC_OK)
221 return rval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000222
Lev Walkinf15320b2004-06-03 03:38:44 +0000223 if(tlv_constr) {
224 /*
225 * Complex operation, requires stack of expectations.
226 */
227 ctx->ptr = _new_stack();
Lev Walkinb02145b2017-06-28 08:52:07 -0700228 if(!ctx->ptr) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000229 RETURN(RC_FAIL);
230 }
231 } else {
232 /*
233 * Jump into stackless primitive decoding.
234 */
235 _CH_PHASE(ctx, 3);
Lev Walkin3a4689a2006-11-24 11:20:27 +0000236 if(type_variant == ASN_OSUBV_ANY && tag_mode != 1)
Lev Walkin188ed2c2004-09-13 08:31:01 +0000237 APPEND(buf_ptr, rval.consumed);
Lev Walkinf15320b2004-06-03 03:38:44 +0000238 ADVANCE(rval.consumed);
239 goto phase3;
240 }
241
Lev Walkinf15320b2004-06-03 03:38:44 +0000242 NEXT_PHASE(ctx);
243 /* Fall through */
244 case 1:
245 phase1:
246 /*
247 * Fill the stack with expectations.
248 */
Lev Walkin814cca72004-12-15 23:23:53 +0000249 stck = (struct _stack *)ctx->ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000250 sel = stck->cur_ptr;
251 do {
252 ber_tlv_tag_t tlv_tag;
253 ber_tlv_len_t tlv_len;
Lev Walkin188ed2c2004-09-13 08:31:01 +0000254 ber_tlv_tag_t expected_tag;
Lev Walkin5c915992004-09-27 20:54:06 +0000255 ssize_t tl, ll, tlvl;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000256 /* This one works even if (sel->left == -1) */
Simo Sorcecd943c22016-07-25 04:39:40 -0400257 size_t Left = ((!sel||(size_t)sel->left >= size)
258 ?size:(size_t)sel->left);
Lev Walkinf15320b2004-06-03 03:38:44 +0000259
Lev Walkin3990ba62004-09-24 20:57:41 +0000260
Lev Walkinabf68892004-10-26 10:12:14 +0000261 ASN_DEBUG("%p, s->l=%ld, s->wn=%ld, s->g=%ld\n", sel,
Lev Walkin1e3ccbb2004-10-26 10:56:10 +0000262 (long)(sel?sel->left:0),
263 (long)(sel?sel->want_nulls:0),
264 (long)(sel?sel->got:0)
Lev Walkin5c915992004-09-27 20:54:06 +0000265 );
266 if(sel && sel->left <= 0 && sel->want_nulls == 0) {
267 if(sel->prev) {
268 struct _stack_el *prev = sel->prev;
269 if(prev->left != -1) {
270 if(prev->left < sel->got)
271 RETURN(RC_FAIL);
272 prev->left -= sel->got;
273 }
274 prev->got += sel->got;
275 sel = stck->cur_ptr = prev;
276 if(!sel) break;
277 tlv_constr = 1;
278 continue;
279 } else {
280 sel = stck->cur_ptr = 0;
281 break; /* Nothing to wait */
282 }
283 }
284
Lev Walkin3990ba62004-09-24 20:57:41 +0000285 tl = ber_fetch_tag(buf_ptr, Left, &tlv_tag);
Lev Walkinabf68892004-10-26 10:12:14 +0000286 ASN_DEBUG("fetch tag(size=%ld,L=%ld), %sstack, left=%ld, wn=%ld, tl=%ld",
287 (long)size, (long)Left, sel?"":"!",
Lev Walkin1e3ccbb2004-10-26 10:56:10 +0000288 (long)(sel?sel->left:0),
289 (long)(sel?sel->want_nulls:0),
Lev Walkinabf68892004-10-26 10:12:14 +0000290 (long)tl);
Lev Walkinf15320b2004-06-03 03:38:44 +0000291 switch(tl) {
292 case -1: RETURN(RC_FAIL);
293 case 0: RETURN(RC_WMORE);
294 }
295
296 tlv_constr = BER_TLV_CONSTRUCTED(buf_ptr);
297
298 ll = ber_fetch_length(tlv_constr,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000299 (const char *)buf_ptr + tl,Left - tl,&tlv_len);
Lev Walkina6a926a2005-03-02 01:54:28 +0000300 ASN_DEBUG("Got tag=%s, tc=%d, left=%ld, tl=%ld, len=%ld, ll=%ld",
Lev Walkin8d127872004-09-04 04:44:50 +0000301 ber_tlv_tag_string(tlv_tag), tlv_constr,
Lev Walkina6a926a2005-03-02 01:54:28 +0000302 (long)Left, (long)tl, (long)tlv_len, (long)ll);
Lev Walkinf15320b2004-06-03 03:38:44 +0000303 switch(ll) {
304 case -1: RETURN(RC_FAIL);
305 case 0: RETURN(RC_WMORE);
306 }
307
Lev Walkin8d127872004-09-04 04:44:50 +0000308 if(sel && sel->want_nulls
Lev Walkin8c3b8542005-03-10 18:52:02 +0000309 && ((const uint8_t *)buf_ptr)[0] == 0
310 && ((const uint8_t *)buf_ptr)[1] == 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000311 {
Lev Walkin8d127872004-09-04 04:44:50 +0000312
313 ASN_DEBUG("Eat EOC; wn=%d--", sel->want_nulls);
314
Lev Walkin3a4689a2006-11-24 11:20:27 +0000315 if(type_variant == ASN_OSUBV_ANY
Lev Walkin07f388c2004-10-11 11:43:08 +0000316 && (tag_mode != 1 || sel->cont_level))
317 APPEND("\0\0", 2);
Lev Walkin5c915992004-09-27 20:54:06 +0000318
319 ADVANCE(2);
320 sel->got += 2;
321 if(sel->left != -1) {
322 sel->left -= 2; /* assert(sel->left >= 2) */
323 }
324
Lev Walkinf15320b2004-06-03 03:38:44 +0000325 sel->want_nulls--;
326 if(sel->want_nulls == 0) {
327 /* Move to the next expectation */
Lev Walkin5c915992004-09-27 20:54:06 +0000328 sel->left = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000329 tlv_constr = 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000330 }
Lev Walkin8d127872004-09-04 04:44:50 +0000331
332 continue;
Lev Walkin188ed2c2004-09-13 08:31:01 +0000333 }
334
335 /*
336 * Set up expected tags,
337 * depending on ASN.1 type being decoded.
338 */
Lev Walkinbbd93252004-10-12 05:57:23 +0000339 switch(type_variant) {
Lev Walkin3a4689a2006-11-24 11:20:27 +0000340 case ASN_OSUBV_BIT:
Lev Walkin188ed2c2004-09-13 08:31:01 +0000341 /* X.690: 8.6.4.1, NOTE 2 */
342 /* Fall through */
Lev Walkin3a4689a2006-11-24 11:20:27 +0000343 case ASN_OSUBV_STR:
Lev Walkin188ed2c2004-09-13 08:31:01 +0000344 default:
345 if(sel) {
346 int level = sel->cont_level;
347 if(level < td->all_tags_count) {
348 expected_tag = td->all_tags[level];
349 break;
350 } else if(td->all_tags_count) {
351 expected_tag = td->all_tags
352 [td->all_tags_count - 1];
353 break;
354 }
355 /* else, Fall through */
356 }
357 /* Fall through */
Lev Walkin3a4689a2006-11-24 11:20:27 +0000358 case ASN_OSUBV_ANY:
Lev Walkin188ed2c2004-09-13 08:31:01 +0000359 expected_tag = tlv_tag;
360 break;
361 }
362
363
364 if(tlv_tag != expected_tag) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000365 char buf[2][32];
366 ber_tlv_tag_snprint(tlv_tag,
367 buf[0], sizeof(buf[0]));
368 ber_tlv_tag_snprint(td->tags[td->tags_count-1],
369 buf[1], sizeof(buf[1]));
370 ASN_DEBUG("Tag does not match expectation: %s != %s",
371 buf[0], buf[1]);
372 RETURN(RC_FAIL);
373 }
374
Lev Walkinde4825d2004-09-29 13:20:14 +0000375 tlvl = tl + ll; /* Combined length of T and L encoding */
376 if((tlv_len + tlvl) < 0) {
377 /* tlv_len value is too big */
378 ASN_DEBUG("TLV encoding + length (%ld) is too big",
379 (long)tlv_len);
380 RETURN(RC_FAIL);
381 }
382
Lev Walkinf15320b2004-06-03 03:38:44 +0000383 /*
384 * Append a new expectation.
385 */
Lev Walkin3990ba62004-09-24 20:57:41 +0000386 sel = OS__add_stack_el(stck);
Lev Walkin5c915992004-09-27 20:54:06 +0000387 if(!sel) RETURN(RC_FAIL);
Lev Walkinf15320b2004-06-03 03:38:44 +0000388
Lev Walkin5c915992004-09-27 20:54:06 +0000389 sel->tag = tlv_tag;
390
391 sel->want_nulls = (tlv_len==-1);
392 if(sel->prev && sel->prev->left != -1) {
393 /* Check that the parent frame is big enough */
394 if(sel->prev->left < tlvl + (tlv_len==-1?0:tlv_len))
395 RETURN(RC_FAIL);
396 if(tlv_len == -1)
397 sel->left = sel->prev->left - tlvl;
398 else
399 sel->left = tlv_len;
400 } else {
401 sel->left = tlv_len;
402 }
Lev Walkin3a4689a2006-11-24 11:20:27 +0000403 if(type_variant == ASN_OSUBV_ANY
Lev Walkin07f388c2004-10-11 11:43:08 +0000404 && (tag_mode != 1 || sel->cont_level))
405 APPEND(buf_ptr, tlvl);
Lev Walkin5c915992004-09-27 20:54:06 +0000406 sel->got += tlvl;
407 ADVANCE(tlvl);
408
Lev Walkinabf68892004-10-26 10:12:14 +0000409 ASN_DEBUG("+EXPECT2 got=%ld left=%ld, wn=%d, clvl=%d",
410 (long)sel->got, (long)sel->left,
411 sel->want_nulls, sel->cont_level);
Lev Walkin5c915992004-09-27 20:54:06 +0000412
Lev Walkinf15320b2004-06-03 03:38:44 +0000413 } while(tlv_constr);
414 if(sel == NULL) {
415 /* Finished operation, "phase out" */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000416 ASN_DEBUG("Phase out");
Lev Walkinf15320b2004-06-03 03:38:44 +0000417 _CH_PHASE(ctx, +3);
418 break;
419 }
420
421 NEXT_PHASE(ctx);
422 /* Fall through */
423 case 2:
Lev Walkin814cca72004-12-15 23:23:53 +0000424 stck = (struct _stack *)ctx->ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000425 sel = stck->cur_ptr;
Lev Walkin5c915992004-09-27 20:54:06 +0000426 ASN_DEBUG("Phase 2: Need %ld bytes, size=%ld, alrg=%ld, wn=%d",
427 (long)sel->left, (long)size, (long)sel->got,
428 sel->want_nulls);
Lev Walkinf15320b2004-06-03 03:38:44 +0000429 {
430 ber_tlv_len_t len;
431
432 assert(sel->left >= 0);
433
Lev Walkinec1ffd42004-08-18 04:53:32 +0000434 len = ((ber_tlv_len_t)size < sel->left)
435 ? (ber_tlv_len_t)size : sel->left;
Lev Walkinf15320b2004-06-03 03:38:44 +0000436 if(len > 0) {
Lev Walkin3a4689a2006-11-24 11:20:27 +0000437 if(type_variant == ASN_OSUBV_BIT
Lev Walkin188ed2c2004-09-13 08:31:01 +0000438 && sel->bits_chopped == 0) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000439 /* Put the unused-bits-octet away */
Lev Walkin8c3b8542005-03-10 18:52:02 +0000440 st->bits_unused = *(const uint8_t *)buf_ptr;
441 APPEND(((const char *)buf_ptr+1), (len - 1));
Lev Walkinf15320b2004-06-03 03:38:44 +0000442 sel->bits_chopped = 1;
443 } else {
444 APPEND(buf_ptr, len);
445 }
446 ADVANCE(len);
447 sel->left -= len;
Lev Walkin5c915992004-09-27 20:54:06 +0000448 sel->got += len;
Lev Walkinf15320b2004-06-03 03:38:44 +0000449 }
450
Lev Walkin5c915992004-09-27 20:54:06 +0000451 if(sel->left) {
452 ASN_DEBUG("OS left %ld, size = %ld, wn=%d\n",
453 (long)sel->left, (long)size, sel->want_nulls);
Lev Walkinf15320b2004-06-03 03:38:44 +0000454 RETURN(RC_WMORE);
Lev Walkinf15320b2004-06-03 03:38:44 +0000455 }
Lev Walkin5c915992004-09-27 20:54:06 +0000456
457 PREV_PHASE(ctx);
458 goto phase1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000459 }
460 break;
461 case 3:
462 phase3:
463 /*
464 * Primitive form, no stack required.
465 */
Lev Walkinbbd93252004-10-12 05:57:23 +0000466 assert(ctx->left >= 0);
467
Lev Walkind9bd7752004-06-05 08:17:50 +0000468 if(size < (size_t)ctx->left) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000469 if(!size) RETURN(RC_WMORE);
Lev Walkin3a4689a2006-11-24 11:20:27 +0000470 if(type_variant == ASN_OSUBV_BIT && !ctx->context) {
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, size);
Lev Walkin75b1bef2005-04-25 19:38:21 +0000476 assert(ctx->context > 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000477 ctx->left -= size;
478 ADVANCE(size);
479 RETURN(RC_WMORE);
480 } else {
Lev Walkin3a4689a2006-11-24 11:20:27 +0000481 if(type_variant == ASN_OSUBV_BIT
Lev Walkin75b1bef2005-04-25 19:38:21 +0000482 && !ctx->context && ctx->left) {
Lev Walkin8c3b8542005-03-10 18:52:02 +0000483 st->bits_unused = *(const uint8_t *)buf_ptr;
Lev Walkinbbd93252004-10-12 05:57:23 +0000484 ctx->left--;
485 ADVANCE(1);
486 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000487 APPEND(buf_ptr, ctx->left);
488 ADVANCE(ctx->left);
489 ctx->left = 0;
490
491 NEXT_PHASE(ctx);
492 }
493 break;
494 }
495
Lev Walkin5c915992004-09-27 20:54:06 +0000496 if(sel) {
497 ASN_DEBUG("3sel p=%p, wn=%d, l=%ld, g=%ld, size=%ld",
498 sel->prev, sel->want_nulls,
499 (long)sel->left, (long)sel->got, (long)size);
500 if(sel->prev || sel->want_nulls > 1 || sel->left > 0) {
501 RETURN(RC_WMORE);
502 }
503 }
504
Lev Walkinf15320b2004-06-03 03:38:44 +0000505 /*
506 * BIT STRING-specific processing.
507 */
Lev Walkin3a4689a2006-11-24 11:20:27 +0000508 if(type_variant == ASN_OSUBV_BIT && st->size) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000509 /* Finalize BIT STRING: zero out unused bits. */
Lev Walkinbbd93252004-10-12 05:57:23 +0000510 st->buf[st->size-1] &= 0xff << st->bits_unused;
Lev Walkinf15320b2004-06-03 03:38:44 +0000511 }
512
Lev Walkinabf68892004-10-26 10:12:14 +0000513 ASN_DEBUG("Took %ld bytes to encode %s: [%s]:%ld",
514 (long)consumed_myself, td->name,
Lev Walkin3a4689a2006-11-24 11:20:27 +0000515 (type_variant == ASN_OSUBV_STR) ? (char *)st->buf : "<data>",
Lev Walkinabf68892004-10-26 10:12:14 +0000516 (long)st->size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000517
Lev Walkinf15320b2004-06-03 03:38:44 +0000518
Lev Walkin5c915992004-09-27 20:54:06 +0000519 RETURN(RC_OK);
Lev Walkinf15320b2004-06-03 03:38:44 +0000520}
521
522/*
523 * Encode OCTET STRING type using DER.
524 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000525asn_enc_rval_t
Lev Walkinbbd93252004-10-12 05:57:23 +0000526OCTET_STRING_encode_der(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000527 int tag_mode, ber_tlv_tag_t tag,
528 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000529 asn_enc_rval_t er;
530 asn_OCTET_STRING_specifics_t *specs = td->specifics
Lev Walkindc06f6b2004-10-20 15:50:55 +0000531 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +0800532 : &asn_SPC_OCTET_STRING_specs;
Lev Walkinbbd93252004-10-12 05:57:23 +0000533 BIT_STRING_t *st = (BIT_STRING_t *)sptr;
Lev Walkin3a4689a2006-11-24 11:20:27 +0000534 enum asn_OS_Subvariant type_variant = specs->subvariant;
Lev Walkinbbd93252004-10-12 05:57:23 +0000535 int fix_last_byte = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000536
537 ASN_DEBUG("%s %s as OCTET STRING",
Lev Walkin1f670c12004-09-02 12:57:25 +0000538 cb?"Estimating":"Encoding", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000539
540 /*
Lev Walkinbbd93252004-10-12 05:57:23 +0000541 * Write tags.
Lev Walkinf15320b2004-06-03 03:38:44 +0000542 */
Lev Walkin3a4689a2006-11-24 11:20:27 +0000543 if(type_variant != ASN_OSUBV_ANY || tag_mode == 1) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000544 er.encoded = der_write_tags(td,
Lev Walkin3a4689a2006-11-24 11:20:27 +0000545 (type_variant == ASN_OSUBV_BIT) + st->size,
546 tag_mode, type_variant == ASN_OSUBV_ANY, tag,
547 cb, app_key);
Lev Walkinbbd93252004-10-12 05:57:23 +0000548 if(er.encoded == -1) {
549 er.failed_type = td;
550 er.structure_ptr = sptr;
551 return er;
Lev Walkinf15320b2004-06-03 03:38:44 +0000552 }
Lev Walkinbbd93252004-10-12 05:57:23 +0000553 } else {
554 /* Disallow: [<tag>] IMPLICIT ANY */
Lev Walkin3a4689a2006-11-24 11:20:27 +0000555 assert(type_variant != ASN_OSUBV_ANY || tag_mode != -1);
Lev Walkinbbd93252004-10-12 05:57:23 +0000556 er.encoded = 0;
557 }
558
559 if(!cb) {
Lev Walkin3a4689a2006-11-24 11:20:27 +0000560 er.encoded += (type_variant == ASN_OSUBV_BIT) + st->size;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700561 ASN__ENCODED_OK(er);
Lev Walkinf15320b2004-06-03 03:38:44 +0000562 }
563
Lev Walkin07f388c2004-10-11 11:43:08 +0000564 /*
Lev Walkinbbd93252004-10-12 05:57:23 +0000565 * Prepare to deal with the last octet of BIT STRING.
Lev Walkin07f388c2004-10-11 11:43:08 +0000566 */
Lev Walkin3a4689a2006-11-24 11:20:27 +0000567 if(type_variant == ASN_OSUBV_BIT) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000568 uint8_t b = st->bits_unused & 0x07;
569 if(b && st->size) fix_last_byte = 1;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700570 ASN__CALLBACK(&b, 1);
Lev Walkinbbd93252004-10-12 05:57:23 +0000571 er.encoded++;
Lev Walkinf15320b2004-06-03 03:38:44 +0000572 }
573
Lev Walkinbbd93252004-10-12 05:57:23 +0000574 /* Invoke callback for the main part of the buffer */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700575 ASN__CALLBACK(st->buf, st->size - fix_last_byte);
Lev Walkinf15320b2004-06-03 03:38:44 +0000576
Lev Walkinbbd93252004-10-12 05:57:23 +0000577 /* The last octet should be stripped off the unused bits */
578 if(fix_last_byte) {
579 uint8_t b = st->buf[st->size-1] & (0xff << st->bits_unused);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700580 ASN__CALLBACK(&b, 1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000581 }
582
Lev Walkinbbd93252004-10-12 05:57:23 +0000583 er.encoded += st->size;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700584 ASN__ENCODED_OK(er);
Lev Walkinbbd93252004-10-12 05:57:23 +0000585cb_failed:
Lev Walkin7c1dc052016-03-14 03:08:15 -0700586 ASN__ENCODE_FAILED;
Lev Walkinf15320b2004-06-03 03:38:44 +0000587}
588
Lev Walkina9cc46e2004-09-22 16:06:28 +0000589asn_enc_rval_t
Lev Walkinde4825d2004-09-29 13:20:14 +0000590OCTET_STRING_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000591 int ilevel, enum xer_encoder_flags_e flags,
592 asn_app_consume_bytes_f *cb, void *app_key) {
Wim Lewis59e8d282014-08-04 12:39:35 -0700593 const char * const h2c = "0123456789ABCDEF";
Lev Walkina9cc46e2004-09-22 16:06:28 +0000594 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
595 asn_enc_rval_t er;
596 char scratch[16 * 3 + 4];
597 char *p = scratch;
598 uint8_t *buf;
599 uint8_t *end;
600 size_t i;
601
Lev Walkin2ddd3082006-10-16 12:32:14 +0000602 if(!st || (!st->buf && st->size))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700603 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000604
605 er.encoded = 0;
606
607 /*
608 * Dump the contents of the buffer in hexadecimal.
609 */
610 buf = st->buf;
611 end = buf + st->size;
612 if(flags & XER_F_CANONICAL) {
613 char *scend = scratch + (sizeof(scratch) - 2);
614 for(; buf < end; buf++) {
615 if(p >= scend) {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700616 ASN__CALLBACK(scratch, p - scratch);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000617 er.encoded += p - scratch;
618 p = scratch;
619 }
620 *p++ = h2c[(*buf >> 4) & 0x0F];
621 *p++ = h2c[*buf & 0x0F];
622 }
Lev Walkincc6a9102004-09-23 22:06:26 +0000623
Lev Walkin7c1dc052016-03-14 03:08:15 -0700624 ASN__CALLBACK(scratch, p-scratch); /* Dump the rest */
Lev Walkincc6a9102004-09-23 22:06:26 +0000625 er.encoded += p - scratch;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000626 } else {
627 for(i = 0; buf < end; buf++, i++) {
628 if(!(i % 16) && (i || st->size > 16)) {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700629 ASN__CALLBACK(scratch, p-scratch);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000630 er.encoded += (p-scratch);
631 p = scratch;
Lev Walkinc6cac8e2016-03-14 02:57:07 -0700632 ASN__TEXT_INDENT(1, ilevel);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000633 }
634 *p++ = h2c[(*buf >> 4) & 0x0F];
635 *p++ = h2c[*buf & 0x0F];
636 *p++ = 0x20;
637 }
Lev Walkincc6a9102004-09-23 22:06:26 +0000638 if(p - scratch) {
639 p--; /* Remove the tail space */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700640 ASN__CALLBACK(scratch, p-scratch); /* Dump the rest */
Lev Walkincc6a9102004-09-23 22:06:26 +0000641 er.encoded += p - scratch;
642 if(st->size > 16)
Lev Walkinc6cac8e2016-03-14 02:57:07 -0700643 ASN__TEXT_INDENT(1, ilevel-1);
Lev Walkincc6a9102004-09-23 22:06:26 +0000644 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000645 }
646
Lev Walkin7c1dc052016-03-14 03:08:15 -0700647 ASN__ENCODED_OK(er);
Lev Walkin942fd082004-10-03 09:13:02 +0000648cb_failed:
Lev Walkin7c1dc052016-03-14 03:08:15 -0700649 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000650}
651
Wim Lewis18c2ec92014-07-29 11:30:10 -0700652static const struct OCTET_STRING__xer_escape_table_s {
653 const char *string;
Lev Walkin9ca81892004-10-03 10:54:25 +0000654 int size;
655} OCTET_STRING__xer_escape_table[] = {
656#define OSXET(s) { s, sizeof(s) - 1 }
657 OSXET("\074\156\165\154\057\076"), /* <nul/> */
658 OSXET("\074\163\157\150\057\076"), /* <soh/> */
659 OSXET("\074\163\164\170\057\076"), /* <stx/> */
660 OSXET("\074\145\164\170\057\076"), /* <etx/> */
661 OSXET("\074\145\157\164\057\076"), /* <eot/> */
662 OSXET("\074\145\156\161\057\076"), /* <enq/> */
663 OSXET("\074\141\143\153\057\076"), /* <ack/> */
664 OSXET("\074\142\145\154\057\076"), /* <bel/> */
665 OSXET("\074\142\163\057\076"), /* <bs/> */
666 OSXET("\011"), /* \t */
667 OSXET("\012"), /* \n */
668 OSXET("\074\166\164\057\076"), /* <vt/> */
669 OSXET("\074\146\146\057\076"), /* <ff/> */
670 OSXET("\015"), /* \r */
671 OSXET("\074\163\157\057\076"), /* <so/> */
672 OSXET("\074\163\151\057\076"), /* <si/> */
673 OSXET("\074\144\154\145\057\076"), /* <dle/> */
674 OSXET("\074\144\143\061\057\076"), /* <de1/> */
675 OSXET("\074\144\143\062\057\076"), /* <de2/> */
676 OSXET("\074\144\143\063\057\076"), /* <de3/> */
677 OSXET("\074\144\143\064\057\076"), /* <de4/> */
678 OSXET("\074\156\141\153\057\076"), /* <nak/> */
679 OSXET("\074\163\171\156\057\076"), /* <syn/> */
680 OSXET("\074\145\164\142\057\076"), /* <etb/> */
681 OSXET("\074\143\141\156\057\076"), /* <can/> */
682 OSXET("\074\145\155\057\076"), /* <em/> */
683 OSXET("\074\163\165\142\057\076"), /* <sub/> */
684 OSXET("\074\145\163\143\057\076"), /* <esc/> */
685 OSXET("\074\151\163\064\057\076"), /* <is4/> */
686 OSXET("\074\151\163\063\057\076"), /* <is3/> */
687 OSXET("\074\151\163\062\057\076"), /* <is2/> */
688 OSXET("\074\151\163\061\057\076"), /* <is1/> */
689 { 0, 0 }, /* " " */
690 { 0, 0 }, /* ! */
691 { 0, 0 }, /* \" */
692 { 0, 0 }, /* # */
693 { 0, 0 }, /* $ */
694 { 0, 0 }, /* % */
695 OSXET("\046\141\155\160\073"), /* &amp; */
696 { 0, 0 }, /* ' */
697 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, /* ()*+,-./ */
698 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, /* 01234567 */
699 {0,0},{0,0},{0,0},{0,0}, /* 89:; */
700 OSXET("\046\154\164\073"), /* &lt; */
701 { 0, 0 }, /* = */
702 OSXET("\046\147\164\073"), /* &gt; */
703};
704
Lev Walkindc06f6b2004-10-20 15:50:55 +0000705static int
Lev Walkin0fab1a62005-03-09 22:19:25 +0000706OS__check_escaped_control_char(const void *buf, int size) {
Lev Walkindc06f6b2004-10-20 15:50:55 +0000707 size_t i;
708 /*
709 * Inefficient algorithm which translates the escape sequences
710 * defined above into characters. Returns -1 if not found.
711 * TODO: replace by a faster algorithm (bsearch(), hash or
712 * nested table lookups).
713 */
714 for(i = 0; i < 32 /* Don't spend time on the bottom half */; i++) {
Wim Lewis18c2ec92014-07-29 11:30:10 -0700715 const struct OCTET_STRING__xer_escape_table_s *el;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000716 el = &OCTET_STRING__xer_escape_table[i];
717 if(el->size == size && memcmp(buf, el->string, size) == 0)
718 return i;
719 }
720 return -1;
721}
722
723static int
Lev Walkin0fab1a62005-03-09 22:19:25 +0000724OCTET_STRING__handle_control_chars(void *struct_ptr, const void *chunk_buf, size_t chunk_size) {
Lev Walkindc06f6b2004-10-20 15:50:55 +0000725 /*
726 * This might be one of the escape sequences
727 * for control characters. Check it out.
728 * #11.15.5
729 */
730 int control_char = OS__check_escaped_control_char(chunk_buf,chunk_size);
731 if(control_char >= 0) {
732 OCTET_STRING_t *st = (OCTET_STRING_t *)struct_ptr;
733 void *p = REALLOC(st->buf, st->size + 2);
734 if(p) {
735 st->buf = (uint8_t *)p;
736 st->buf[st->size++] = control_char;
737 st->buf[st->size] = '\0'; /* nul-termination */
738 return 0;
739 }
740 }
741
742 return -1; /* No, it's not */
743}
744
Lev Walkina9cc46e2004-09-22 16:06:28 +0000745asn_enc_rval_t
Lev Walkindc06f6b2004-10-20 15:50:55 +0000746OCTET_STRING_encode_xer_utf8(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000747 int ilevel, enum xer_encoder_flags_e flags,
748 asn_app_consume_bytes_f *cb, void *app_key) {
749 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
750 asn_enc_rval_t er;
Lev Walkin9ca81892004-10-03 10:54:25 +0000751 uint8_t *buf, *end;
752 uint8_t *ss; /* Sequence start */
753 ssize_t encoded_len = 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000754
755 (void)ilevel; /* Unused argument */
756 (void)flags; /* Unused argument */
757
Lev Walkin2ddd3082006-10-16 12:32:14 +0000758 if(!st || (!st->buf && st->size))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700759 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000760
Lev Walkin9ca81892004-10-03 10:54:25 +0000761 buf = st->buf;
762 end = buf + st->size;
763 for(ss = buf; buf < end; buf++) {
Lev Walkin443d2512004-10-05 06:35:31 +0000764 unsigned int ch = *buf;
Lev Walkin9ca81892004-10-03 10:54:25 +0000765 int s_len; /* Special encoding sequence length */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000766
Lev Walkin9ca81892004-10-03 10:54:25 +0000767 /*
768 * Escape certain characters: X.680/11.15
769 */
770 if(ch < sizeof(OCTET_STRING__xer_escape_table)
771 /sizeof(OCTET_STRING__xer_escape_table[0])
772 && (s_len = OCTET_STRING__xer_escape_table[ch].size)) {
773 if(((buf - ss) && cb(ss, buf - ss, app_key) < 0)
774 || cb(OCTET_STRING__xer_escape_table[ch].string, s_len,
775 app_key) < 0)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700776 ASN__ENCODE_FAILED;
Lev Walkin9ca81892004-10-03 10:54:25 +0000777 encoded_len += (buf - ss) + s_len;
778 ss = buf + 1;
779 }
780 }
781
782 encoded_len += (buf - ss);
783 if((buf - ss) && cb(ss, buf - ss, app_key) < 0)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700784 ASN__ENCODE_FAILED;
Lev Walkin9ca81892004-10-03 10:54:25 +0000785
786 er.encoded = encoded_len;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700787 ASN__ENCODED_OK(er);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000788}
789
Lev Walkindc06f6b2004-10-20 15:50:55 +0000790/*
791 * Convert from hexadecimal format (cstring): "AB CD EF"
792 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000793static 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 +0000794 OCTET_STRING_t *st = (OCTET_STRING_t *)sptr;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000795 const char *chunk_stop = (const char *)chunk_buf;
796 const char *p = chunk_stop;
797 const char *pend = p + chunk_size;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000798 unsigned int clv = 0;
799 int half = 0; /* Half bit */
800 uint8_t *buf;
801
802 /* Reallocate buffer according to high cap estimation */
803 ssize_t _ns = st->size + (chunk_size + 1) / 2;
804 void *nptr = REALLOC(st->buf, _ns + 1);
805 if(!nptr) return -1;
806 st->buf = (uint8_t *)nptr;
807 buf = st->buf + st->size;
808
809 /*
810 * If something like " a b c " appears here, the " a b":3 will be
811 * converted, and the rest skipped. That is, unless buf_size is greater
812 * than chunk_size, then it'll be equivalent to "ABC0".
813 */
814 for(; p < pend; p++) {
Lev Walkin0fab1a62005-03-09 22:19:25 +0000815 int ch = *(const unsigned char *)p;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000816 switch(ch) {
817 case 0x09: case 0x0a: case 0x0c: case 0x0d:
818 case 0x20:
819 /* Ignore whitespace */
820 continue;
821 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: /*01234*/
822 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: /*56789*/
823 clv = (clv << 4) + (ch - 0x30);
824 break;
825 case 0x41: case 0x42: case 0x43: /* ABC */
826 case 0x44: case 0x45: case 0x46: /* DEF */
Lev Walkin3ae21bd2005-02-21 14:43:48 +0000827 clv = (clv << 4) + (ch - 0x41 + 10);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000828 break;
829 case 0x61: case 0x62: case 0x63: /* abc */
830 case 0x64: case 0x65: case 0x66: /* def */
Lev Walkin3ae21bd2005-02-21 14:43:48 +0000831 clv = (clv << 4) + (ch - 0x61 + 10);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000832 break;
833 default:
834 *buf = 0; /* JIC */
835 return -1;
836 }
837 if(half++) {
838 half = 0;
839 *buf++ = clv;
840 chunk_stop = p + 1;
841 }
842 }
843
844 /*
845 * Check partial decoding.
846 */
847 if(half) {
848 if(have_more) {
849 /*
850 * Partial specification is fine,
851 * because no more more PXER_TEXT data is available.
852 */
853 *buf++ = clv << 4;
854 chunk_stop = p;
855 }
856 } else {
857 chunk_stop = p;
858 }
859
860 st->size = buf - st->buf; /* Adjust the buffer size */
861 assert(st->size <= _ns);
862 st->buf[st->size] = 0; /* Courtesy termination */
863
Lev Walkin0fab1a62005-03-09 22:19:25 +0000864 return (chunk_stop - (const char *)chunk_buf); /* Converted size */
Lev Walkindc06f6b2004-10-20 15:50:55 +0000865}
866
867/*
868 * Convert from binary format: "00101011101"
869 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000870static 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 +0000871 BIT_STRING_t *st = (BIT_STRING_t *)sptr;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000872 const char *p = (const char *)chunk_buf;
873 const char *pend = p + chunk_size;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000874 int bits_unused = st->bits_unused & 0x7;
875 uint8_t *buf;
876
877 /* Reallocate buffer according to high cap estimation */
878 ssize_t _ns = st->size + (chunk_size + 7) / 8;
879 void *nptr = REALLOC(st->buf, _ns + 1);
880 if(!nptr) return -1;
881 st->buf = (uint8_t *)nptr;
882 buf = st->buf + st->size;
883
884 (void)have_more;
885
886 if(bits_unused == 0)
887 bits_unused = 8;
888 else if(st->size)
889 buf--;
890
891 /*
892 * Convert series of 0 and 1 into the octet string.
893 */
894 for(; p < pend; p++) {
Lev Walkin0fab1a62005-03-09 22:19:25 +0000895 int ch = *(const unsigned char *)p;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000896 switch(ch) {
897 case 0x09: case 0x0a: case 0x0c: case 0x0d:
898 case 0x20:
899 /* Ignore whitespace */
900 break;
901 case 0x30:
902 case 0x31:
903 if(bits_unused-- <= 0) {
904 *++buf = 0; /* Clean the cell */
905 bits_unused = 7;
906 }
907 *buf |= (ch&1) << bits_unused;
908 break;
909 default:
910 st->bits_unused = bits_unused;
911 return -1;
912 }
913 }
914
915 if(bits_unused == 8) {
916 st->size = buf - st->buf;
917 st->bits_unused = 0;
918 } else {
919 st->size = buf - st->buf + 1;
920 st->bits_unused = bits_unused;
921 }
922
923 assert(st->size <= _ns);
924 st->buf[st->size] = 0; /* Courtesy termination */
925
926 return chunk_size; /* Converted in full */
927}
928
929/*
930 * Something like strtod(), but with stricter rules.
931 */
932static int
Lev Walkin0fab1a62005-03-09 22:19:25 +0000933OS__strtoent(int base, const char *buf, const char *end, int32_t *ret_value) {
Lev Walkin33700162004-10-26 09:03:31 +0000934 int32_t val = 0;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000935 const char *p;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000936
937 for(p = buf; p < end; p++) {
938 int ch = *p;
Lev Walkinb0f3db62005-07-03 05:30:15 +0000939
940 /* Strange huge value */
941 if((val * base + base) < 0)
942 return -1;
943
Lev Walkindc06f6b2004-10-20 15:50:55 +0000944 switch(ch) {
945 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: /*01234*/
946 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: /*56789*/
947 val = val * base + (ch - 0x30);
948 break;
949 case 0x41: case 0x42: case 0x43: /* ABC */
950 case 0x44: case 0x45: case 0x46: /* DEF */
Lev Walkin3ae21bd2005-02-21 14:43:48 +0000951 val = val * base + (ch - 0x41 + 10);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000952 break;
953 case 0x61: case 0x62: case 0x63: /* abc */
954 case 0x64: case 0x65: case 0x66: /* def */
Lev Walkin3ae21bd2005-02-21 14:43:48 +0000955 val = val * base + (ch - 0x61 + 10);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000956 break;
957 case 0x3b: /* ';' */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000958 *ret_value = val;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000959 return (p - buf) + 1;
960 default:
961 return -1; /* Character set error */
962 }
963 }
964
Lev Walkinb0f3db62005-07-03 05:30:15 +0000965 *ret_value = -1;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000966 return (p - buf);
967}
968
969/*
970 * Convert from the plain UTF-8 format, expanding entity references: "2 &lt; 3"
971 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000972static 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 +0000973 OCTET_STRING_t *st = (OCTET_STRING_t *)sptr;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000974 const char *p = (const char *)chunk_buf;
975 const char *pend = p + chunk_size;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000976 uint8_t *buf;
977
978 /* Reallocate buffer */
979 ssize_t _ns = st->size + chunk_size;
980 void *nptr = REALLOC(st->buf, _ns + 1);
981 if(!nptr) return -1;
982 st->buf = (uint8_t *)nptr;
983 buf = st->buf + st->size;
984
985 /*
986 * Convert series of 0 and 1 into the octet string.
987 */
988 for(; p < pend; p++) {
Lev Walkin0fab1a62005-03-09 22:19:25 +0000989 int ch = *(const unsigned char *)p;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000990 int len; /* Length of the rest of the chunk */
991
992 if(ch != 0x26 /* '&' */) {
993 *buf++ = ch;
994 continue; /* That was easy... */
995 }
996
997 /*
998 * Process entity reference.
999 */
Lev Walkin0fab1a62005-03-09 22:19:25 +00001000 len = chunk_size - (p - (const char *)chunk_buf);
Lev Walkindc06f6b2004-10-20 15:50:55 +00001001 if(len == 1 /* "&" */) goto want_more;
1002 if(p[1] == 0x23 /* '#' */) {
Lev Walkin0fab1a62005-03-09 22:19:25 +00001003 const char *pval; /* Pointer to start of digits */
Lev Walkinbc0dce32006-07-27 09:56:13 +00001004 int32_t val = 0; /* Entity reference value */
Lev Walkindc06f6b2004-10-20 15:50:55 +00001005 int base;
1006
1007 if(len == 2 /* "&#" */) goto want_more;
1008 if(p[2] == 0x78 /* 'x' */)
1009 pval = p + 3, base = 16;
1010 else
1011 pval = p + 2, base = 10;
1012 len = OS__strtoent(base, pval, p + len, &val);
1013 if(len == -1) {
1014 /* Invalid charset. Just copy verbatim. */
1015 *buf++ = ch;
1016 continue;
1017 }
1018 if(!len || pval[len-1] != 0x3b) goto want_more;
1019 assert(val > 0);
1020 p += (pval - p) + len - 1; /* Advance past entref */
1021
1022 if(val < 0x80) {
1023 *buf++ = (char)val;
1024 } else if(val < 0x800) {
1025 *buf++ = 0xc0 | ((val >> 6));
1026 *buf++ = 0x80 | ((val & 0x3f));
1027 } else if(val < 0x10000) {
1028 *buf++ = 0xe0 | ((val >> 12));
1029 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1030 *buf++ = 0x80 | ((val & 0x3f));
1031 } else if(val < 0x200000) {
1032 *buf++ = 0xf0 | ((val >> 18));
1033 *buf++ = 0x80 | ((val >> 12) & 0x3f);
1034 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1035 *buf++ = 0x80 | ((val & 0x3f));
1036 } else if(val < 0x4000000) {
1037 *buf++ = 0xf8 | ((val >> 24));
1038 *buf++ = 0x80 | ((val >> 18) & 0x3f);
1039 *buf++ = 0x80 | ((val >> 12) & 0x3f);
1040 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1041 *buf++ = 0x80 | ((val & 0x3f));
1042 } else {
1043 *buf++ = 0xfc | ((val >> 30) & 0x1);
1044 *buf++ = 0x80 | ((val >> 24) & 0x3f);
1045 *buf++ = 0x80 | ((val >> 18) & 0x3f);
1046 *buf++ = 0x80 | ((val >> 12) & 0x3f);
1047 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1048 *buf++ = 0x80 | ((val & 0x3f));
1049 }
1050 } else {
1051 /*
1052 * Ugly, limited parsing of &amp; &gt; &lt;
1053 */
1054 char *sc = (char *)memchr(p, 0x3b, len > 5 ? 5 : len);
1055 if(!sc) goto want_more;
1056 if((sc - p) == 4
1057 && p[1] == 0x61 /* 'a' */
1058 && p[2] == 0x6d /* 'm' */
1059 && p[3] == 0x70 /* 'p' */) {
1060 *buf++ = 0x26;
1061 p = sc;
1062 continue;
1063 }
1064 if((sc - p) == 3) {
1065 if(p[1] == 0x6c) {
1066 *buf = 0x3c; /* '<' */
1067 } else if(p[1] == 0x67) {
1068 *buf = 0x3e; /* '>' */
1069 } else {
1070 /* Unsupported entity reference */
1071 *buf++ = ch;
1072 continue;
1073 }
1074 if(p[2] != 0x74) {
1075 /* Unsupported entity reference */
1076 *buf++ = ch;
1077 continue;
1078 }
1079 buf++;
1080 p = sc;
1081 continue;
1082 }
1083 /* Unsupported entity reference */
1084 *buf++ = ch;
1085 }
1086
1087 continue;
1088 want_more:
1089 if(have_more) {
1090 /*
1091 * We know that no more data (of the same type)
1092 * is coming. Copy the rest verbatim.
1093 */
1094 *buf++ = ch;
1095 continue;
1096 }
Lev Walkin0fab1a62005-03-09 22:19:25 +00001097 chunk_size = (p - (const char *)chunk_buf);
Lev Walkindc06f6b2004-10-20 15:50:55 +00001098 /* Processing stalled: need more data */
Lev Walkinbdaae772005-02-18 12:25:47 +00001099 break;
Lev Walkindc06f6b2004-10-20 15:50:55 +00001100 }
1101
1102 st->size = buf - st->buf;
1103 assert(st->size <= _ns);
1104 st->buf[st->size] = 0; /* Courtesy termination */
1105
1106 return chunk_size; /* Converted in full */
1107}
1108
1109/*
1110 * Decode OCTET STRING from the XML element's body.
1111 */
1112static asn_dec_rval_t
1113OCTET_STRING__decode_xer(asn_codec_ctx_t *opt_codec_ctx,
1114 asn_TYPE_descriptor_t *td, void **sptr,
Lev Walkin8c3b8542005-03-10 18:52:02 +00001115 const char *opt_mname, const void *buf_ptr, size_t size,
Lev Walkindc06f6b2004-10-20 15:50:55 +00001116 int (*opt_unexpected_tag_decoder)
Lev Walkin0fab1a62005-03-09 22:19:25 +00001117 (void *struct_ptr, const void *chunk_buf, size_t chunk_size),
Lev Walkindc06f6b2004-10-20 15:50:55 +00001118 ssize_t (*body_receiver)
Lev Walkin0fab1a62005-03-09 22:19:25 +00001119 (void *struct_ptr, const void *chunk_buf, size_t chunk_size,
Lev Walkindc06f6b2004-10-20 15:50:55 +00001120 int have_more)
1121) {
Lev Walkind5125642005-02-14 20:08:00 +00001122 OCTET_STRING_t *st = (OCTET_STRING_t *)*sptr;
Lev Walkindc06f6b2004-10-20 15:50:55 +00001123 asn_OCTET_STRING_specifics_t *specs = td->specifics
1124 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +08001125 : &asn_SPC_OCTET_STRING_specs;
Lev Walkindc06f6b2004-10-20 15:50:55 +00001126 const char *xml_tag = opt_mname ? opt_mname : td->xml_tag;
1127 asn_struct_ctx_t *ctx; /* Per-structure parser context */
Lev Walkinc61f3862005-02-14 17:21:22 +00001128 asn_dec_rval_t rval; /* Return value from the decoder */
1129 int st_allocated;
Lev Walkindc06f6b2004-10-20 15:50:55 +00001130
1131 /*
1132 * Create the string if does not exist.
1133 */
Lev Walkinc61f3862005-02-14 17:21:22 +00001134 if(!st) {
Lev Walkin8484ed82004-12-14 13:31:01 +00001135 st = (OCTET_STRING_t *)CALLOC(1, specs->struct_size);
Lev Walkin8484ed82004-12-14 13:31:01 +00001136 *sptr = (void *)st;
Lev Walkinc61f3862005-02-14 17:21:22 +00001137 if(!st) goto sta_failed;
1138 st_allocated = 1;
Lev Walkinbdaae772005-02-18 12:25:47 +00001139 } else {
1140 st_allocated = 0;
1141 }
Lev Walkinc61f3862005-02-14 17:21:22 +00001142 if(!st->buf) {
1143 /* This is separate from above section */
1144 st->buf = (uint8_t *)CALLOC(1, 1);
1145 if(!st->buf) {
1146 if(st_allocated) {
1147 *sptr = 0;
1148 goto stb_failed;
1149 } else {
1150 goto sta_failed;
1151 }
Lev Walkindc06f6b2004-10-20 15:50:55 +00001152 }
1153 }
1154
1155 /* Restore parsing context */
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001156 ctx = (asn_struct_ctx_t *)(((char *)*sptr) + specs->ctx_offset);
Lev Walkindc06f6b2004-10-20 15:50:55 +00001157
1158 return xer_decode_general(opt_codec_ctx, ctx, *sptr, xml_tag,
1159 buf_ptr, size, opt_unexpected_tag_decoder, body_receiver);
Lev Walkinc61f3862005-02-14 17:21:22 +00001160
1161stb_failed:
1162 FREEMEM(st);
1163sta_failed:
1164 rval.code = RC_FAIL;
1165 rval.consumed = 0;
1166 return rval;
Lev Walkindc06f6b2004-10-20 15:50:55 +00001167}
1168
1169/*
1170 * Decode OCTET STRING from the hexadecimal data.
1171 */
1172asn_dec_rval_t
1173OCTET_STRING_decode_xer_hex(asn_codec_ctx_t *opt_codec_ctx,
1174 asn_TYPE_descriptor_t *td, void **sptr,
Lev Walkin8c3b8542005-03-10 18:52:02 +00001175 const char *opt_mname, const void *buf_ptr, size_t size) {
Lev Walkindc06f6b2004-10-20 15:50:55 +00001176 return OCTET_STRING__decode_xer(opt_codec_ctx, td, sptr, opt_mname,
1177 buf_ptr, size, 0, OCTET_STRING__convert_hexadecimal);
1178}
1179
1180/*
1181 * Decode OCTET STRING from the binary (0/1) data.
1182 */
1183asn_dec_rval_t
1184OCTET_STRING_decode_xer_binary(asn_codec_ctx_t *opt_codec_ctx,
1185 asn_TYPE_descriptor_t *td, void **sptr,
Lev Walkin8c3b8542005-03-10 18:52:02 +00001186 const char *opt_mname, const void *buf_ptr, size_t size) {
Lev Walkindc06f6b2004-10-20 15:50:55 +00001187 return OCTET_STRING__decode_xer(opt_codec_ctx, td, sptr, opt_mname,
1188 buf_ptr, size, 0, OCTET_STRING__convert_binary);
1189}
1190
1191/*
1192 * Decode OCTET STRING from the string (ASCII/UTF-8) data.
1193 */
1194asn_dec_rval_t
1195OCTET_STRING_decode_xer_utf8(asn_codec_ctx_t *opt_codec_ctx,
1196 asn_TYPE_descriptor_t *td, void **sptr,
Lev Walkin8c3b8542005-03-10 18:52:02 +00001197 const char *opt_mname, const void *buf_ptr, size_t size) {
Lev Walkindc06f6b2004-10-20 15:50:55 +00001198 return OCTET_STRING__decode_xer(opt_codec_ctx, td, sptr, opt_mname,
1199 buf_ptr, size,
1200 OCTET_STRING__handle_control_chars,
1201 OCTET_STRING__convert_entrefs);
1202}
1203
Lev Walkin725883b2006-10-09 12:07:58 +00001204static int
Lev Walkin3a4689a2006-11-24 11:20:27 +00001205OCTET_STRING_per_get_characters(asn_per_data_t *po, uint8_t *buf,
1206 size_t units, unsigned int bpc, unsigned int unit_bits,
1207 long lb, long ub, asn_per_constraints_t *pc) {
1208 uint8_t *end = buf + units * bpc;
Lev Walkin725883b2006-10-09 12:07:58 +00001209
1210 ASN_DEBUG("Expanding %d characters into (%ld..%ld):%d",
Lev Walkin3a4689a2006-11-24 11:20:27 +00001211 (int)units, lb, ub, unit_bits);
Lev Walkin725883b2006-10-09 12:07:58 +00001212
1213 /* X.691: 27.5.4 */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001214 if((unsigned long)ub <= ((unsigned long)2 << (unit_bits - 1))) {
Lev Walkin725883b2006-10-09 12:07:58 +00001215 /* Decode without translation */
1216 lb = 0;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001217 } else if(pc && pc->code2value) {
1218 if(unit_bits > 16)
1219 return 1; /* FATAL: can't have constrained
1220 * UniversalString with more than
1221 * 16 million code points */
1222 for(; buf < end; buf += bpc) {
Lev Walkin725883b2006-10-09 12:07:58 +00001223 int value;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001224 int code = per_get_few_bits(po, unit_bits);
Lev Walkin725883b2006-10-09 12:07:58 +00001225 if(code < 0) return -1; /* WMORE */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001226 value = pc->code2value(code);
Lev Walkin725883b2006-10-09 12:07:58 +00001227 if(value < 0) {
1228 ASN_DEBUG("Code %d (0x%02x) is"
1229 " not in map (%ld..%ld)",
1230 code, code, lb, ub);
1231 return 1; /* FATAL */
1232 }
Lev Walkin3a4689a2006-11-24 11:20:27 +00001233 switch(bpc) {
1234 case 1: *buf = value; break;
1235 case 2: buf[0] = value >> 8; buf[1] = value; break;
1236 case 4: buf[0] = value >> 24; buf[1] = value >> 16;
1237 buf[2] = value >> 8; buf[3] = value; break;
1238 }
Lev Walkin725883b2006-10-09 12:07:58 +00001239 }
1240 return 0;
1241 }
1242
Lev Walkin3a4689a2006-11-24 11:20:27 +00001243 /* Shortcut the no-op copying to the aligned structure */
1244 if(lb == 0 && (unit_bits == 8 * bpc)) {
1245 return per_get_many_bits(po, buf, 0, unit_bits * units);
1246 }
1247
1248 for(; buf < end; buf += bpc) {
Lev Walkin725883b2006-10-09 12:07:58 +00001249 int code = per_get_few_bits(po, unit_bits);
1250 int ch = code + lb;
1251 if(code < 0) return -1; /* WMORE */
1252 if(ch > ub) {
1253 ASN_DEBUG("Code %d is out of range (%ld..%ld)",
1254 ch, lb, ub);
1255 return 1; /* FATAL */
1256 }
Lev Walkin3a4689a2006-11-24 11:20:27 +00001257 switch(bpc) {
1258 case 1: *buf = ch; break;
1259 case 2: buf[0] = ch >> 8; buf[1] = ch; break;
1260 case 4: buf[0] = ch >> 24; buf[1] = ch >> 16;
1261 buf[2] = ch >> 8; buf[3] = ch; break;
1262 }
Lev Walkin725883b2006-10-09 12:07:58 +00001263 }
1264
1265 return 0;
1266}
1267
1268static int
Lev Walkin3a4689a2006-11-24 11:20:27 +00001269OCTET_STRING_per_put_characters(asn_per_outp_t *po, const uint8_t *buf,
1270 size_t units, unsigned int bpc, unsigned int unit_bits,
1271 long lb, long ub, asn_per_constraints_t *pc) {
1272 const uint8_t *end = buf + units * bpc;
Lev Walkin725883b2006-10-09 12:07:58 +00001273
Lev Walkin3a4689a2006-11-24 11:20:27 +00001274 ASN_DEBUG("Squeezing %d characters into (%ld..%ld):%d (%d bpc)",
1275 (int)units, lb, ub, unit_bits, bpc);
Lev Walkin725883b2006-10-09 12:07:58 +00001276
1277 /* X.691: 27.5.4 */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001278 if((unsigned long)ub <= ((unsigned long)2 << (unit_bits - 1))) {
Lev Walkin725883b2006-10-09 12:07:58 +00001279 /* Encode as is */
1280 lb = 0;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001281 } else if(pc && pc->value2code) {
1282 for(; buf < end; buf += bpc) {
1283 int code;
1284 uint32_t value;
1285 switch(bpc) {
1286 case 1: value = *(const uint8_t *)buf; break;
1287 case 2: value = (buf[0] << 8) | buf[1]; break;
1288 case 4: value = (buf[0] << 24) | (buf[1] << 16)
1289 | (buf[2] << 8) | buf[3]; break;
1290 default: return -1;
1291 }
1292 code = pc->value2code(value);
Lev Walkin725883b2006-10-09 12:07:58 +00001293 if(code < 0) {
1294 ASN_DEBUG("Character %d (0x%02x) is"
1295 " not in map (%ld..%ld)",
1296 *buf, *buf, lb, ub);
1297 return -1;
1298 }
1299 if(per_put_few_bits(po, code, unit_bits))
1300 return -1;
1301 }
1302 }
1303
Lev Walkin3a4689a2006-11-24 11:20:27 +00001304 /* Shortcut the no-op copying to the aligned structure */
1305 if(lb == 0 && (unit_bits == 8 * bpc)) {
1306 return per_put_many_bits(po, buf, unit_bits * units);
1307 }
1308
1309 for(ub -= lb; buf < end; buf += bpc) {
1310 int ch;
1311 uint32_t value;
1312 switch(bpc) {
1313 case 1: value = *(const uint8_t *)buf; break;
1314 case 2: value = (buf[0] << 8) | buf[1]; break;
1315 case 4: value = (buf[0] << 24) | (buf[1] << 16)
1316 | (buf[2] << 8) | buf[3]; break;
1317 default: return -1;
1318 }
1319 ch = value - lb;
Lev Walkin725883b2006-10-09 12:07:58 +00001320 if(ch < 0 || ch > ub) {
1321 ASN_DEBUG("Character %d (0x%02x)"
1322 " is out of range (%ld..%ld)",
1323 *buf, *buf, lb, ub + lb);
1324 return -1;
1325 }
1326 if(per_put_few_bits(po, ch, unit_bits))
1327 return -1;
1328 }
1329
1330 return 0;
1331}
1332
Lev Walkin59b176e2005-11-26 11:25:14 +00001333asn_dec_rval_t
1334OCTET_STRING_decode_uper(asn_codec_ctx_t *opt_codec_ctx,
1335 asn_TYPE_descriptor_t *td, asn_per_constraints_t *constraints,
1336 void **sptr, asn_per_data_t *pd) {
1337
1338 asn_OCTET_STRING_specifics_t *specs = td->specifics
1339 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +08001340 : &asn_SPC_OCTET_STRING_specs;
Lev Walkin725883b2006-10-09 12:07:58 +00001341 asn_per_constraints_t *pc = constraints ? constraints
1342 : td->per_constraints;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001343 asn_per_constraint_t *cval;
1344 asn_per_constraint_t *csiz;
Lev Walkin59b176e2005-11-26 11:25:14 +00001345 asn_dec_rval_t rval = { RC_OK, 0 };
1346 BIT_STRING_t *st = (BIT_STRING_t *)*sptr;
1347 ssize_t consumed_myself = 0;
1348 int repeat;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001349 enum {
1350 OS__BPC_BIT = 0,
1351 OS__BPC_CHAR = 1,
1352 OS__BPC_U16 = 2,
1353 OS__BPC_U32 = 4
1354 } bpc; /* Bytes per character */
1355 unsigned int unit_bits;
1356 unsigned int canonical_unit_bits;
Lev Walkin59b176e2005-11-26 11:25:14 +00001357
1358 (void)opt_codec_ctx;
1359
Lev Walkin3a4689a2006-11-24 11:20:27 +00001360 if(pc) {
1361 cval = &pc->value;
1362 csiz = &pc->size;
1363 } else {
1364 cval = &asn_DEF_OCTET_STRING_constraints.value;
1365 csiz = &asn_DEF_OCTET_STRING_constraints.size;
1366 }
1367
1368 switch(specs->subvariant) {
1369 default:
1370 case ASN_OSUBV_ANY:
1371 ASN_DEBUG("Unrecognized subvariant %d", specs->subvariant);
1372 RETURN(RC_FAIL);
1373 case ASN_OSUBV_BIT:
1374 canonical_unit_bits = unit_bits = 1;
1375 bpc = OS__BPC_BIT;
1376 break;
1377 case ASN_OSUBV_STR:
1378 canonical_unit_bits = unit_bits = 8;
1379 if(cval->flags & APC_CONSTRAINED)
1380 unit_bits = cval->range_bits;
1381 bpc = OS__BPC_CHAR;
1382 break;
1383 case ASN_OSUBV_U16:
1384 canonical_unit_bits = unit_bits = 16;
1385 if(cval->flags & APC_CONSTRAINED)
1386 unit_bits = cval->range_bits;
1387 bpc = OS__BPC_U16;
1388 break;
1389 case ASN_OSUBV_U32:
1390 canonical_unit_bits = unit_bits = 32;
1391 if(cval->flags & APC_CONSTRAINED)
1392 unit_bits = cval->range_bits;
1393 bpc = OS__BPC_U32;
1394 break;
1395 }
1396
Lev Walkin59b176e2005-11-26 11:25:14 +00001397 /*
1398 * Allocate the string.
1399 */
1400 if(!st) {
1401 st = (BIT_STRING_t *)(*sptr = CALLOC(1, specs->struct_size));
1402 if(!st) RETURN(RC_FAIL);
1403 }
1404
Lev Walkin725883b2006-10-09 12:07:58 +00001405 ASN_DEBUG("PER Decoding %s size %ld .. %ld bits %d",
Lev Walkin3a4689a2006-11-24 11:20:27 +00001406 csiz->flags & APC_EXTENSIBLE ? "extensible" : "non-extensible",
1407 csiz->lower_bound, csiz->upper_bound, csiz->effective_bits);
Lev Walkince676dd2005-12-20 22:34:55 +00001408
Lev Walkin3a4689a2006-11-24 11:20:27 +00001409 if(csiz->flags & APC_EXTENSIBLE) {
Lev Walkin59b176e2005-11-26 11:25:14 +00001410 int inext = per_get_few_bits(pd, 1);
Lev Walkin0a8aa602006-09-18 20:05:55 +00001411 if(inext < 0) RETURN(RC_WMORE);
Lev Walkin3a4689a2006-11-24 11:20:27 +00001412 if(inext) {
1413 csiz = &asn_DEF_OCTET_STRING_constraints.size;
1414 cval = &asn_DEF_OCTET_STRING_constraints.value;
1415 unit_bits = canonical_unit_bits;
1416 }
Lev Walkin59b176e2005-11-26 11:25:14 +00001417 }
1418
Lev Walkin3a4689a2006-11-24 11:20:27 +00001419 if(csiz->effective_bits >= 0) {
Lev Walkin59b176e2005-11-26 11:25:14 +00001420 FREEMEM(st->buf);
Lev Walkin3a4689a2006-11-24 11:20:27 +00001421 if(bpc) {
1422 st->size = csiz->upper_bound * bpc;
Lev Walkin59b176e2005-11-26 11:25:14 +00001423 } else {
Lev Walkin3a4689a2006-11-24 11:20:27 +00001424 st->size = (csiz->upper_bound + 7) >> 3;
Lev Walkin59b176e2005-11-26 11:25:14 +00001425 }
1426 st->buf = (uint8_t *)MALLOC(st->size + 1);
1427 if(!st->buf) { st->size = 0; RETURN(RC_FAIL); }
1428 }
1429
1430 /* X.691, #16.5: zero-length encoding */
1431 /* X.691, #16.6: short fixed length encoding (up to 2 octets) */
1432 /* X.691, #16.7: long fixed length encoding (up to 64K octets) */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001433 if(csiz->effective_bits == 0) {
Lev Walkin725883b2006-10-09 12:07:58 +00001434 int ret;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001435 if(bpc) {
1436 ASN_DEBUG("Encoding OCTET STRING size %ld",
1437 csiz->upper_bound);
1438 ret = OCTET_STRING_per_get_characters(pd, st->buf,
1439 csiz->upper_bound, bpc, unit_bits,
1440 cval->lower_bound, cval->upper_bound, pc);
Lev Walkin725883b2006-10-09 12:07:58 +00001441 if(ret > 0) RETURN(RC_FAIL);
1442 } else {
Lev Walkin3a4689a2006-11-24 11:20:27 +00001443 ASN_DEBUG("Encoding BIT STRING size %ld",
1444 csiz->upper_bound);
Lev Walkin725883b2006-10-09 12:07:58 +00001445 ret = per_get_many_bits(pd, st->buf, 0,
Lev Walkin3a4689a2006-11-24 11:20:27 +00001446 unit_bits * csiz->upper_bound);
Lev Walkin725883b2006-10-09 12:07:58 +00001447 }
Lev Walkin0a8aa602006-09-18 20:05:55 +00001448 if(ret < 0) RETURN(RC_WMORE);
Lev Walkin3a4689a2006-11-24 11:20:27 +00001449 consumed_myself += unit_bits * csiz->upper_bound;
Lev Walkin59b176e2005-11-26 11:25:14 +00001450 st->buf[st->size] = 0;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001451 if(bpc == 0) {
1452 int ubs = (csiz->upper_bound & 0x7);
1453 st->bits_unused = ubs ? 8 - ubs : 0;
1454 }
Lev Walkin59b176e2005-11-26 11:25:14 +00001455 RETURN(RC_OK);
1456 }
1457
1458 st->size = 0;
1459 do {
Lev Walkin3a4689a2006-11-24 11:20:27 +00001460 ssize_t raw_len;
Lev Walkin59b176e2005-11-26 11:25:14 +00001461 ssize_t len_bytes;
1462 ssize_t len_bits;
1463 void *p;
1464 int ret;
1465
1466 /* Get the PER length */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001467 raw_len = uper_get_length(pd, csiz->effective_bits, &repeat);
1468 if(raw_len < 0) RETURN(RC_WMORE);
1469 raw_len += csiz->lower_bound;
Lev Walkin59b176e2005-11-26 11:25:14 +00001470
Lev Walkin0a8aa602006-09-18 20:05:55 +00001471 ASN_DEBUG("Got PER length eb %ld, len %ld, %s (%s)",
Lev Walkin3a4689a2006-11-24 11:20:27 +00001472 (long)csiz->effective_bits, (long)raw_len,
Lev Walkin0a8aa602006-09-18 20:05:55 +00001473 repeat ? "repeat" : "once", td->name);
Lev Walkin3a4689a2006-11-24 11:20:27 +00001474 if(bpc) {
1475 len_bytes = raw_len * bpc;
1476 len_bits = len_bytes * unit_bits;
1477 } else {
1478 len_bits = raw_len;
Lev Walkin59b176e2005-11-26 11:25:14 +00001479 len_bytes = (len_bits + 7) >> 3;
1480 if(len_bits & 0x7)
1481 st->bits_unused = 8 - (len_bits & 0x7);
1482 /* len_bits be multiple of 16K if repeat is set */
Lev Walkin59b176e2005-11-26 11:25:14 +00001483 }
1484 p = REALLOC(st->buf, st->size + len_bytes + 1);
1485 if(!p) RETURN(RC_FAIL);
1486 st->buf = (uint8_t *)p;
1487
Lev Walkin3a4689a2006-11-24 11:20:27 +00001488 if(bpc) {
1489 ret = OCTET_STRING_per_get_characters(pd,
1490 &st->buf[st->size], raw_len, bpc, unit_bits,
1491 cval->lower_bound, cval->upper_bound, pc);
Lev Walkin725883b2006-10-09 12:07:58 +00001492 if(ret > 0) RETURN(RC_FAIL);
1493 } else {
1494 ret = per_get_many_bits(pd, &st->buf[st->size],
1495 0, len_bits);
1496 }
Lev Walkin0a8aa602006-09-18 20:05:55 +00001497 if(ret < 0) RETURN(RC_WMORE);
Lev Walkin59b176e2005-11-26 11:25:14 +00001498 st->size += len_bytes;
1499 } while(repeat);
1500 st->buf[st->size] = 0; /* nul-terminate */
1501
1502 return rval;
1503}
Lev Walkindc06f6b2004-10-20 15:50:55 +00001504
Lev Walkin523de9e2006-08-18 01:34:18 +00001505asn_enc_rval_t
1506OCTET_STRING_encode_uper(asn_TYPE_descriptor_t *td,
1507 asn_per_constraints_t *constraints, void *sptr, asn_per_outp_t *po) {
1508
1509 asn_OCTET_STRING_specifics_t *specs = td->specifics
1510 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +08001511 : &asn_SPC_OCTET_STRING_specs;
Lev Walkin725883b2006-10-09 12:07:58 +00001512 asn_per_constraints_t *pc = constraints ? constraints
1513 : td->per_constraints;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001514 asn_per_constraint_t *cval;
1515 asn_per_constraint_t *csiz;
Lev Walkin523de9e2006-08-18 01:34:18 +00001516 const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001517 asn_enc_rval_t er = { 0, 0, 0 };
Lev Walkin523de9e2006-08-18 01:34:18 +00001518 int inext = 0; /* Lies not within extension root */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001519 unsigned int unit_bits;
1520 unsigned int canonical_unit_bits;
1521 unsigned int sizeinunits;
Lev Walkin523de9e2006-08-18 01:34:18 +00001522 const uint8_t *buf;
1523 int ret;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001524 enum {
1525 OS__BPC_BIT = 0,
1526 OS__BPC_CHAR = 1,
1527 OS__BPC_U16 = 2,
1528 OS__BPC_U32 = 4
1529 } bpc; /* Bytes per character */
1530 int ct_extensible;
Lev Walkin523de9e2006-08-18 01:34:18 +00001531
Lev Walkin2ddd3082006-10-16 12:32:14 +00001532 if(!st || (!st->buf && st->size))
Lev Walkin7c1dc052016-03-14 03:08:15 -07001533 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001534
Lev Walkin3a4689a2006-11-24 11:20:27 +00001535 if(pc) {
1536 cval = &pc->value;
1537 csiz = &pc->size;
1538 } else {
1539 cval = &asn_DEF_OCTET_STRING_constraints.value;
1540 csiz = &asn_DEF_OCTET_STRING_constraints.size;
1541 }
1542 ct_extensible = csiz->flags & APC_EXTENSIBLE;
1543
1544 switch(specs->subvariant) {
1545 default:
1546 case ASN_OSUBV_ANY:
Lev Walkin7c1dc052016-03-14 03:08:15 -07001547 ASN__ENCODE_FAILED;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001548 case ASN_OSUBV_BIT:
1549 canonical_unit_bits = unit_bits = 1;
1550 bpc = OS__BPC_BIT;
1551 sizeinunits = st->size * 8 - (st->bits_unused & 0x07);
Lev Walkin523de9e2006-08-18 01:34:18 +00001552 ASN_DEBUG("BIT STRING of %d bytes, %d bits unused",
1553 sizeinunits, st->bits_unused);
Lev Walkin3a4689a2006-11-24 11:20:27 +00001554 break;
1555 case ASN_OSUBV_STR:
1556 canonical_unit_bits = unit_bits = 8;
1557 if(cval->flags & APC_CONSTRAINED)
1558 unit_bits = cval->range_bits;
1559 bpc = OS__BPC_CHAR;
1560 sizeinunits = st->size;
1561 break;
1562 case ASN_OSUBV_U16:
1563 canonical_unit_bits = unit_bits = 16;
1564 if(cval->flags & APC_CONSTRAINED)
1565 unit_bits = cval->range_bits;
1566 bpc = OS__BPC_U16;
1567 sizeinunits = st->size / 2;
1568 break;
1569 case ASN_OSUBV_U32:
1570 canonical_unit_bits = unit_bits = 32;
1571 if(cval->flags & APC_CONSTRAINED)
1572 unit_bits = cval->range_bits;
1573 bpc = OS__BPC_U32;
1574 sizeinunits = st->size / 4;
1575 break;
Lev Walkin725883b2006-10-09 12:07:58 +00001576 }
1577
Lev Walkin00918812006-09-18 21:19:32 +00001578 ASN_DEBUG("Encoding %s into %d units of %d bits"
Lev Walkin725883b2006-10-09 12:07:58 +00001579 " (%ld..%ld, effective %d)%s",
Lev Walkin00918812006-09-18 21:19:32 +00001580 td->name, sizeinunits, unit_bits,
Lev Walkin3a4689a2006-11-24 11:20:27 +00001581 csiz->lower_bound, csiz->upper_bound,
1582 csiz->effective_bits, ct_extensible ? " EXT" : "");
Lev Walkin523de9e2006-08-18 01:34:18 +00001583
Lev Walkind4c16732013-03-28 05:00:47 -07001584 /* Figure out whether size lies within PER visible constraint */
Lev Walkin523de9e2006-08-18 01:34:18 +00001585
Lev Walkin3a4689a2006-11-24 11:20:27 +00001586 if(csiz->effective_bits >= 0) {
1587 if((int)sizeinunits < csiz->lower_bound
1588 || (int)sizeinunits > csiz->upper_bound) {
Lev Walkin523de9e2006-08-18 01:34:18 +00001589 if(ct_extensible) {
Lev Walkin3a4689a2006-11-24 11:20:27 +00001590 cval = &asn_DEF_OCTET_STRING_constraints.value;
1591 csiz = &asn_DEF_OCTET_STRING_constraints.size;
1592 unit_bits = canonical_unit_bits;
Lev Walkin523de9e2006-08-18 01:34:18 +00001593 inext = 1;
1594 } else
Lev Walkin7c1dc052016-03-14 03:08:15 -07001595 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001596 }
1597 } else {
1598 inext = 0;
1599 }
1600
1601 if(ct_extensible) {
1602 /* Declare whether length is [not] within extension root */
1603 if(per_put_few_bits(po, inext, 1))
Lev Walkin7c1dc052016-03-14 03:08:15 -07001604 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001605 }
1606
1607 /* X.691, #16.5: zero-length encoding */
1608 /* X.691, #16.6: short fixed length encoding (up to 2 octets) */
1609 /* X.691, #16.7: long fixed length encoding (up to 64K octets) */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001610 if(csiz->effective_bits >= 0) {
Lev Walkin523de9e2006-08-18 01:34:18 +00001611 ASN_DEBUG("Encoding %d bytes (%ld), length in %d bits",
Lev Walkin3a4689a2006-11-24 11:20:27 +00001612 st->size, sizeinunits - csiz->lower_bound,
1613 csiz->effective_bits);
1614 ret = per_put_few_bits(po, sizeinunits - csiz->lower_bound,
1615 csiz->effective_bits);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001616 if(ret) ASN__ENCODE_FAILED;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001617 if(bpc) {
1618 ret = OCTET_STRING_per_put_characters(po, st->buf,
1619 sizeinunits, bpc, unit_bits,
1620 cval->lower_bound, cval->upper_bound, pc);
Lev Walkin725883b2006-10-09 12:07:58 +00001621 } else {
1622 ret = per_put_many_bits(po, st->buf,
1623 sizeinunits * unit_bits);
1624 }
Lev Walkin7c1dc052016-03-14 03:08:15 -07001625 if(ret) ASN__ENCODE_FAILED;
1626 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +00001627 }
1628
1629 ASN_DEBUG("Encoding %d bytes", st->size);
1630
1631 if(sizeinunits == 0) {
1632 if(uper_put_length(po, 0))
Lev Walkin7c1dc052016-03-14 03:08:15 -07001633 ASN__ENCODE_FAILED;
1634 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +00001635 }
1636
1637 buf = st->buf;
1638 while(sizeinunits) {
1639 ssize_t maySave = uper_put_length(po, sizeinunits);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001640 if(maySave < 0) ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001641
Lev Walkin345e4c62006-10-19 02:44:08 +00001642 ASN_DEBUG("Encoding %ld of %ld",
1643 (long)maySave, (long)sizeinunits);
Lev Walkin523de9e2006-08-18 01:34:18 +00001644
Lev Walkin3a4689a2006-11-24 11:20:27 +00001645 if(bpc) {
1646 ret = OCTET_STRING_per_put_characters(po, buf,
1647 maySave, bpc, unit_bits,
1648 cval->lower_bound, cval->upper_bound, pc);
Lev Walkin725883b2006-10-09 12:07:58 +00001649 } else {
1650 ret = per_put_many_bits(po, buf, maySave * unit_bits);
1651 }
Lev Walkin7c1dc052016-03-14 03:08:15 -07001652 if(ret) ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001653
Lev Walkin3a4689a2006-11-24 11:20:27 +00001654 if(bpc)
1655 buf += maySave * bpc;
Lev Walkin523de9e2006-08-18 01:34:18 +00001656 else
Lev Walkin3a4689a2006-11-24 11:20:27 +00001657 buf += maySave >> 3;
Lev Walkin523de9e2006-08-18 01:34:18 +00001658 sizeinunits -= maySave;
1659 assert(!(maySave & 0x07) || !sizeinunits);
1660 }
1661
Lev Walkin7c1dc052016-03-14 03:08:15 -07001662 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +00001663}
1664
Lev Walkinf15320b2004-06-03 03:38:44 +00001665int
Lev Walkinde4825d2004-09-29 13:20:14 +00001666OCTET_STRING_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +00001667 asn_app_consume_bytes_f *cb, void *app_key) {
Wim Lewis59e8d282014-08-04 12:39:35 -07001668 const char * const h2c = "0123456789ABCDEF";
Lev Walkinc2346572004-08-11 09:07:36 +00001669 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +00001670 char scratch[16 * 3 + 4];
1671 char *p = scratch;
1672 uint8_t *buf;
1673 uint8_t *end;
1674 size_t i;
Lev Walkinf15320b2004-06-03 03:38:44 +00001675
Lev Walkind9bd7752004-06-05 08:17:50 +00001676 (void)td; /* Unused argument */
1677
Lev Walkin2ddd3082006-10-16 12:32:14 +00001678 if(!st || (!st->buf && st->size))
1679 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001680
1681 /*
1682 * Dump the contents of the buffer in hexadecimal.
1683 */
1684 buf = st->buf;
1685 end = buf + st->size;
1686 for(i = 0; buf < end; buf++, i++) {
1687 if(!(i % 16) && (i || st->size > 16)) {
Lev Walkin8e8078a2004-09-26 13:10:40 +00001688 if(cb(scratch, p - scratch, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +00001689 return -1;
Lev Walkin8e8078a2004-09-26 13:10:40 +00001690 _i_INDENT(1);
Lev Walkinf15320b2004-06-03 03:38:44 +00001691 p = scratch;
1692 }
1693 *p++ = h2c[(*buf >> 4) & 0x0F];
1694 *p++ = h2c[*buf & 0x0F];
Lev Walkina9cc46e2004-09-22 16:06:28 +00001695 *p++ = 0x20;
Lev Walkinf15320b2004-06-03 03:38:44 +00001696 }
1697
Lev Walkincc6a9102004-09-23 22:06:26 +00001698 if(p > scratch) {
1699 p--; /* Remove the tail space */
Lev Walkin8e8078a2004-09-26 13:10:40 +00001700 if(cb(scratch, p - scratch, app_key) < 0)
Lev Walkincc6a9102004-09-23 22:06:26 +00001701 return -1;
1702 }
1703
1704 return 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001705}
1706
1707int
Lev Walkindc06f6b2004-10-20 15:50:55 +00001708OCTET_STRING_print_utf8(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +00001709 int ilevel, asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +00001710 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +00001711
Lev Walkind9bd7752004-06-05 08:17:50 +00001712 (void)td; /* Unused argument */
1713 (void)ilevel; /* Unused argument */
1714
Lev Walkin2ddd3082006-10-16 12:32:14 +00001715 if(st && (st->buf || !st->size)) {
Lev Walkin8e8078a2004-09-26 13:10:40 +00001716 return (cb(st->buf, st->size, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001717 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +00001718 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001719 }
1720}
1721
1722void
Lev Walkinde4825d2004-09-29 13:20:14 +00001723OCTET_STRING_free(asn_TYPE_descriptor_t *td, void *sptr, int contents_only) {
Lev Walkinc2346572004-08-11 09:07:36 +00001724 OCTET_STRING_t *st = (OCTET_STRING_t *)sptr;
Lev Walkin034ee192016-03-14 02:28:33 -07001725 asn_OCTET_STRING_specifics_t *specs;
1726 asn_struct_ctx_t *ctx;
Lev Walkinb54577a2004-11-08 10:47:12 +00001727 struct _stack *stck;
Lev Walkinf15320b2004-06-03 03:38:44 +00001728
1729 if(!td || !st)
1730 return;
1731
Lev Walkin034ee192016-03-14 02:28:33 -07001732 specs = td->specifics
1733 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +08001734 : &asn_SPC_OCTET_STRING_specs;
Lev Walkin034ee192016-03-14 02:28:33 -07001735 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
1736
Lev Walkinf15320b2004-06-03 03:38:44 +00001737 ASN_DEBUG("Freeing %s as OCTET STRING", td->name);
1738
1739 if(st->buf) {
1740 FREEMEM(st->buf);
Lev Walkin2ddd3082006-10-16 12:32:14 +00001741 st->buf = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001742 }
1743
1744 /*
1745 * Remove decode-time stack.
1746 */
Lev Walkinb54577a2004-11-08 10:47:12 +00001747 stck = (struct _stack *)ctx->ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +00001748 if(stck) {
1749 while(stck->tail) {
1750 struct _stack_el *sel = stck->tail;
1751 stck->tail = sel->prev;
1752 FREEMEM(sel);
1753 }
1754 FREEMEM(stck);
1755 }
1756
1757 if(!contents_only) {
1758 FREEMEM(st);
1759 }
1760}
1761
1762/*
1763 * Conversion routines.
1764 */
1765int
1766OCTET_STRING_fromBuf(OCTET_STRING_t *st, const char *str, int len) {
1767 void *buf;
1768
1769 if(st == 0 || (str == 0 && len)) {
1770 errno = EINVAL;
1771 return -1;
1772 }
1773
1774 /*
1775 * Clear the OCTET STRING.
1776 */
1777 if(str == NULL) {
Lev Walkin7b284812005-12-17 11:43:25 +00001778 FREEMEM(st->buf);
1779 st->buf = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001780 st->size = 0;
1781 return 0;
1782 }
1783
1784 /* Determine the original string size, if not explicitly given */
1785 if(len < 0)
1786 len = strlen(str);
1787
1788 /* Allocate and fill the memory */
1789 buf = MALLOC(len + 1);
Lev Walkin7b284812005-12-17 11:43:25 +00001790 if(buf == NULL)
Lev Walkinf15320b2004-06-03 03:38:44 +00001791 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +00001792
1793 memcpy(buf, str, len);
Lev Walkin7b284812005-12-17 11:43:25 +00001794 ((uint8_t *)buf)[len] = '\0'; /* Couldn't use memcpy(len+1)! */
1795 FREEMEM(st->buf);
1796 st->buf = (uint8_t *)buf;
1797 st->size = len;
Lev Walkinf15320b2004-06-03 03:38:44 +00001798
1799 return 0;
1800}
1801
1802OCTET_STRING_t *
Lev Walkinbbd93252004-10-12 05:57:23 +00001803OCTET_STRING_new_fromBuf(asn_TYPE_descriptor_t *td, const char *str, int len) {
1804 asn_OCTET_STRING_specifics_t *specs = td->specifics
Lev Walkindc06f6b2004-10-20 15:50:55 +00001805 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +08001806 : &asn_SPC_OCTET_STRING_specs;
Lev Walkinf15320b2004-06-03 03:38:44 +00001807 OCTET_STRING_t *st;
1808
Lev Walkinbbd93252004-10-12 05:57:23 +00001809 st = (OCTET_STRING_t *)CALLOC(1, specs->struct_size);
Lev Walkinf15320b2004-06-03 03:38:44 +00001810 if(st && str && OCTET_STRING_fromBuf(st, str, len)) {
Lev Walkin419f6752006-09-13 04:02:00 +00001811 FREEMEM(st);
Lev Walkinf15320b2004-06-03 03:38:44 +00001812 st = NULL;
1813 }
1814
1815 return st;
1816}
1817