blob: 2f2efc4e213df8dbb02af64c7561f57e28a3276f [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};
Bi-Ruei, Chiu1fa31c92016-05-16 13:50:09 +080017asn_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};
Lev Walkin0368c622017-09-18 00:33:16 -070022
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080023asn_TYPE_operation_t asn_OP_OCTET_STRING = {
Lev Walkina9cc46e2004-09-22 16:06:28 +000024 OCTET_STRING_free,
Lev Walkincd2f48e2017-08-10 02:14:59 -070025 OCTET_STRING_print, /* OCTET STRING generally means a non-ascii sequence */
26 OCTET_STRING_compare,
Lev Walkinf15320b2004-06-03 03:38:44 +000027 OCTET_STRING_decode_ber,
28 OCTET_STRING_encode_der,
Lev Walkindc06f6b2004-10-20 15:50:55 +000029 OCTET_STRING_decode_xer_hex,
Lev Walkina9cc46e2004-09-22 16:06:28 +000030 OCTET_STRING_encode_xer,
Lev Walkincc159472017-07-06 08:26:36 -070031#ifdef ASN_DISABLE_OER_SUPPORT
32 0,
33 0,
34#else
Lev Walkin2535bd52017-08-27 23:45:56 -070035 OCTET_STRING_decode_oer,
36 OCTET_STRING_encode_oer,
Lev Walkincc159472017-07-06 08:26:36 -070037#endif /* ASN_DISABLE_OER_SUPPORT */
Lev Walkinb33425f2017-07-14 14:59:52 +040038#ifdef ASN_DISABLE_PER_SUPPORT
39 0,
40 0,
41#else
42 OCTET_STRING_decode_uper, /* Unaligned PER decoder */
43 OCTET_STRING_encode_uper, /* Unaligned PER encoder */
44#endif /* ASN_DISABLE_PER_SUPPORT */
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080045 0 /* Use generic outmost tag fetcher */
46};
47asn_TYPE_descriptor_t asn_DEF_OCTET_STRING = {
48 "OCTET STRING", /* Canonical name */
49 "OCTET_STRING", /* XML tag name */
50 &asn_OP_OCTET_STRING,
51 asn_generic_no_constraint,
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 Walkin76780762017-07-07 10:07:30 -070058 0, /* No OER visible constraints */
Lev Walkin59b176e2005-11-26 11:25:14 +000059 0, /* No PER visible constraints */
Lev Walkin449f8322004-08-20 13:23:42 +000060 0, 0, /* No members */
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +080061 &asn_SPC_OCTET_STRING_specs
Lev Walkinf15320b2004-06-03 03:38:44 +000062};
63
Lev Walkincc6a9102004-09-23 22:06:26 +000064#undef _CH_PHASE
65#undef NEXT_PHASE
66#undef PREV_PHASE
Lev Walkin75b1bef2005-04-25 19:38:21 +000067#define _CH_PHASE(ctx, inc) do { \
68 if(ctx->phase == 0) \
69 ctx->context = 0; \
70 ctx->phase += inc; \
Lev Walkinf15320b2004-06-03 03:38:44 +000071 } while(0)
72#define NEXT_PHASE(ctx) _CH_PHASE(ctx, +1)
73#define PREV_PHASE(ctx) _CH_PHASE(ctx, -1)
74
Lev Walkincc6a9102004-09-23 22:06:26 +000075#undef ADVANCE
Lev Walkin75b1bef2005-04-25 19:38:21 +000076#define ADVANCE(num_bytes) do { \
77 size_t num = (num_bytes); \
78 buf_ptr = ((const char *)buf_ptr) + num; \
79 size -= num; \
80 consumed_myself += num; \
Lev Walkinf15320b2004-06-03 03:38:44 +000081 } while(0)
82
Lev Walkincc6a9102004-09-23 22:06:26 +000083#undef RETURN
Lev Walkin75b1bef2005-04-25 19:38:21 +000084#define RETURN(_code) do { \
Lev Walkin59b176e2005-11-26 11:25:14 +000085 asn_dec_rval_t tmprval; \
86 tmprval.code = _code; \
87 tmprval.consumed = consumed_myself; \
88 return tmprval; \
Lev Walkinf15320b2004-06-03 03:38:44 +000089 } while(0)
90
Lev Walkincc6a9102004-09-23 22:06:26 +000091#undef APPEND
Lev Walkind9bd7752004-06-05 08:17:50 +000092#define APPEND(bufptr, bufsize) do { \
Lev Walkin75b1bef2005-04-25 19:38:21 +000093 size_t _bs = (bufsize); /* Append size */ \
94 size_t _ns = ctx->context; /* Allocated now */ \
95 size_t _es = st->size + _bs; /* Expected size */ \
96 /* int is really a typeof(st->size): */ \
97 if((int)_es < 0) RETURN(RC_FAIL); \
98 if(_ns <= _es) { \
Lev Walkind9bd7752004-06-05 08:17:50 +000099 void *ptr; \
Lev Walkin188ed2c2004-09-13 08:31:01 +0000100 /* Be nice and round to the memory allocator */ \
Lev Walkin75b1bef2005-04-25 19:38:21 +0000101 do { _ns = _ns ? _ns << 1 : 16; } \
102 while(_ns <= _es); \
103 /* int is really a typeof(st->size): */ \
104 if((int)_ns < 0) RETURN(RC_FAIL); \
Lev Walkind9bd7752004-06-05 08:17:50 +0000105 ptr = REALLOC(st->buf, _ns); \
106 if(ptr) { \
Lev Walkinc2346572004-08-11 09:07:36 +0000107 st->buf = (uint8_t *)ptr; \
Lev Walkin75b1bef2005-04-25 19:38:21 +0000108 ctx->context = _ns; \
Lev Walkind9bd7752004-06-05 08:17:50 +0000109 } else { \
110 RETURN(RC_FAIL); \
111 } \
Lev Walkin7f85ef42005-07-02 20:24:27 +0000112 ASN_DEBUG("Reallocating into %ld", (long)_ns); \
Lev Walkind9bd7752004-06-05 08:17:50 +0000113 } \
Lev Walkin8d127872004-09-04 04:44:50 +0000114 memcpy(st->buf + st->size, bufptr, _bs); \
Lev Walkind9bd7752004-06-05 08:17:50 +0000115 /* Convenient nul-termination */ \
Lev Walkin75b1bef2005-04-25 19:38:21 +0000116 st->buf[_es] = '\0'; \
117 st->size = _es; \
Lev Walkinf15320b2004-06-03 03:38:44 +0000118 } while(0)
119
120/*
121 * The main reason why ASN.1 is still alive is that too much time and effort
122 * is necessary for learning it more or less adequately, thus creating a gut
123 * necessity to demonstrate that aquired skill everywhere afterwards.
124 * No, I am not going to explain what the following stuff is.
125 */
126struct _stack_el {
Lev Walkin494fb702017-08-07 20:07:00 -0700127 ber_tlv_len_t left; /* What's left to read (or -1) */
128 ber_tlv_len_t got; /* What was actually processed */
Lev Walkin24bf3f62017-08-10 17:58:43 -0700129 unsigned cont_level; /* Depth of subcontainment */
Lev Walkin494fb702017-08-07 20:07:00 -0700130 int want_nulls; /* Want null "end of content" octets? */
131 int bits_chopped; /* Flag in BIT STRING mode */
132 ber_tlv_tag_t tag; /* For debugging purposes */
133 struct _stack_el *prev;
134 struct _stack_el *next;
Lev Walkinf15320b2004-06-03 03:38:44 +0000135};
136struct _stack {
137 struct _stack_el *tail;
138 struct _stack_el *cur_ptr;
139};
140
141static struct _stack_el *
Lev Walkin3990ba62004-09-24 20:57:41 +0000142OS__add_stack_el(struct _stack *st) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000143 struct _stack_el *nel;
144
Lev Walkin188ed2c2004-09-13 08:31:01 +0000145 /*
146 * Reuse the old stack frame or allocate a new one.
147 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000148 if(st->cur_ptr && st->cur_ptr->next) {
149 nel = st->cur_ptr->next;
Lev Walkinf15320b2004-06-03 03:38:44 +0000150 nel->bits_chopped = 0;
Lev Walkin5c915992004-09-27 20:54:06 +0000151 nel->got = 0;
152 /* Retain the nel->cont_level, it's correct. */
Lev Walkinf15320b2004-06-03 03:38:44 +0000153 } else {
Lev Walkin814cca72004-12-15 23:23:53 +0000154 nel = (struct _stack_el *)CALLOC(1, sizeof(struct _stack_el));
Lev Walkinf15320b2004-06-03 03:38:44 +0000155 if(nel == NULL)
156 return NULL;
157
158 if(st->tail) {
Lev Walkin188ed2c2004-09-13 08:31:01 +0000159 /* Increase a subcontainment depth */
160 nel->cont_level = st->tail->cont_level + 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000161 st->tail->next = nel;
162 }
163 nel->prev = st->tail;
164 st->tail = nel;
165 }
166
167 st->cur_ptr = nel;
168
169 return nel;
170}
171
172static struct _stack *
johvik28268d92017-05-09 10:27:52 +0200173_new_stack(void) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000174 return (struct _stack *)CALLOC(1, sizeof(struct _stack));
Lev Walkinf15320b2004-06-03 03:38:44 +0000175}
176
177/*
178 * Decode OCTET STRING type.
179 */
Lev Walkindc06f6b2004-10-20 15:50:55 +0000180asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -0700181OCTET_STRING_decode_ber(const asn_codec_ctx_t *opt_codec_ctx,
Lev Walkinde4825d2004-09-29 13:20:14 +0000182 asn_TYPE_descriptor_t *td,
Lev Walkin59b176e2005-11-26 11:25:14 +0000183 void **sptr, const void *buf_ptr, size_t size, int tag_mode) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000184 asn_OCTET_STRING_specifics_t *specs = td->specifics
Lev Walkindc06f6b2004-10-20 15:50:55 +0000185 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +0800186 : &asn_SPC_OCTET_STRING_specs;
Lev Walkin59b176e2005-11-26 11:25:14 +0000187 BIT_STRING_t *st = (BIT_STRING_t *)*sptr;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000188 asn_dec_rval_t rval;
Lev Walkinde4825d2004-09-29 13:20:14 +0000189 asn_struct_ctx_t *ctx;
Lev Walkinf15320b2004-06-03 03:38:44 +0000190 ssize_t consumed_myself = 0;
Lev Walkinbbd93252004-10-12 05:57:23 +0000191 struct _stack *stck; /* Expectations stack structure */
Lev Walkin5c915992004-09-27 20:54:06 +0000192 struct _stack_el *sel = 0; /* Stack element */
Lev Walkinf15320b2004-06-03 03:38:44 +0000193 int tlv_constr;
Lev Walkin3a4689a2006-11-24 11:20:27 +0000194 enum asn_OS_Subvariant type_variant = specs->subvariant;
Lev Walkinf15320b2004-06-03 03:38:44 +0000195
Lev Walkincc6a9102004-09-23 22:06:26 +0000196 ASN_DEBUG("Decoding %s as %s (frame %ld)",
197 td->name,
Lev Walkin3a4689a2006-11-24 11:20:27 +0000198 (type_variant == ASN_OSUBV_STR) ?
Lev Walkinbbd93252004-10-12 05:57:23 +0000199 "OCTET STRING" : "OS-SpecialCase",
Lev Walkincc6a9102004-09-23 22:06:26 +0000200 (long)size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000201
202 /*
203 * Create the string if does not exist.
204 */
205 if(st == NULL) {
Lev Walkin59b176e2005-11-26 11:25:14 +0000206 st = (BIT_STRING_t *)(*sptr = CALLOC(1, specs->struct_size));
207 if(st == NULL) RETURN(RC_FAIL);
Lev Walkinf15320b2004-06-03 03:38:44 +0000208 }
209
210 /* Restore parsing context */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800211 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000212
213 switch(ctx->phase) {
214 case 0:
215 /*
216 * Check tags.
217 */
Lev Walkinde4825d2004-09-29 13:20:14 +0000218 rval = ber_check_tags(opt_codec_ctx, td, ctx,
Lev Walkin8e8078a2004-09-26 13:10:40 +0000219 buf_ptr, size, tag_mode, -1,
Lev Walkinf15320b2004-06-03 03:38:44 +0000220 &ctx->left, &tlv_constr);
Lev Walkin443d2512004-10-05 06:35:31 +0000221 if(rval.code != RC_OK)
222 return rval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000223
Lev Walkinf15320b2004-06-03 03:38:44 +0000224 if(tlv_constr) {
225 /*
226 * Complex operation, requires stack of expectations.
227 */
228 ctx->ptr = _new_stack();
Lev Walkinb02145b2017-06-28 08:52:07 -0700229 if(!ctx->ptr) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000230 RETURN(RC_FAIL);
231 }
232 } else {
233 /*
234 * Jump into stackless primitive decoding.
235 */
236 _CH_PHASE(ctx, 3);
Lev Walkin3a4689a2006-11-24 11:20:27 +0000237 if(type_variant == ASN_OSUBV_ANY && tag_mode != 1)
Lev Walkin188ed2c2004-09-13 08:31:01 +0000238 APPEND(buf_ptr, rval.consumed);
Lev Walkinf15320b2004-06-03 03:38:44 +0000239 ADVANCE(rval.consumed);
240 goto phase3;
241 }
242
Lev Walkinf15320b2004-06-03 03:38:44 +0000243 NEXT_PHASE(ctx);
244 /* Fall through */
245 case 1:
246 phase1:
247 /*
248 * Fill the stack with expectations.
249 */
Lev Walkin814cca72004-12-15 23:23:53 +0000250 stck = (struct _stack *)ctx->ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000251 sel = stck->cur_ptr;
252 do {
253 ber_tlv_tag_t tlv_tag;
254 ber_tlv_len_t tlv_len;
Lev Walkin188ed2c2004-09-13 08:31:01 +0000255 ber_tlv_tag_t expected_tag;
Lev Walkin5c915992004-09-27 20:54:06 +0000256 ssize_t tl, ll, tlvl;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000257 /* This one works even if (sel->left == -1) */
Simo Sorcecd943c22016-07-25 04:39:40 -0400258 size_t Left = ((!sel||(size_t)sel->left >= size)
259 ?size:(size_t)sel->left);
Lev Walkinf15320b2004-06-03 03:38:44 +0000260
Lev Walkin3990ba62004-09-24 20:57:41 +0000261
Lev Walkinabf68892004-10-26 10:12:14 +0000262 ASN_DEBUG("%p, s->l=%ld, s->wn=%ld, s->g=%ld\n", sel,
Lev Walkin1e3ccbb2004-10-26 10:56:10 +0000263 (long)(sel?sel->left:0),
264 (long)(sel?sel->want_nulls:0),
265 (long)(sel?sel->got:0)
Lev Walkin5c915992004-09-27 20:54:06 +0000266 );
267 if(sel && sel->left <= 0 && sel->want_nulls == 0) {
268 if(sel->prev) {
269 struct _stack_el *prev = sel->prev;
270 if(prev->left != -1) {
271 if(prev->left < sel->got)
272 RETURN(RC_FAIL);
273 prev->left -= sel->got;
274 }
275 prev->got += sel->got;
276 sel = stck->cur_ptr = prev;
277 if(!sel) break;
278 tlv_constr = 1;
279 continue;
280 } else {
281 sel = stck->cur_ptr = 0;
282 break; /* Nothing to wait */
283 }
284 }
285
Lev Walkin3990ba62004-09-24 20:57:41 +0000286 tl = ber_fetch_tag(buf_ptr, Left, &tlv_tag);
Lev Walkinabf68892004-10-26 10:12:14 +0000287 ASN_DEBUG("fetch tag(size=%ld,L=%ld), %sstack, left=%ld, wn=%ld, tl=%ld",
288 (long)size, (long)Left, sel?"":"!",
Lev Walkin1e3ccbb2004-10-26 10:56:10 +0000289 (long)(sel?sel->left:0),
290 (long)(sel?sel->want_nulls:0),
Lev Walkinabf68892004-10-26 10:12:14 +0000291 (long)tl);
Lev Walkinf15320b2004-06-03 03:38:44 +0000292 switch(tl) {
293 case -1: RETURN(RC_FAIL);
294 case 0: RETURN(RC_WMORE);
295 }
296
297 tlv_constr = BER_TLV_CONSTRUCTED(buf_ptr);
298
299 ll = ber_fetch_length(tlv_constr,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000300 (const char *)buf_ptr + tl,Left - tl,&tlv_len);
Lev Walkina6a926a2005-03-02 01:54:28 +0000301 ASN_DEBUG("Got tag=%s, tc=%d, left=%ld, tl=%ld, len=%ld, ll=%ld",
Lev Walkin8d127872004-09-04 04:44:50 +0000302 ber_tlv_tag_string(tlv_tag), tlv_constr,
Lev Walkina6a926a2005-03-02 01:54:28 +0000303 (long)Left, (long)tl, (long)tlv_len, (long)ll);
Lev Walkinf15320b2004-06-03 03:38:44 +0000304 switch(ll) {
305 case -1: RETURN(RC_FAIL);
306 case 0: RETURN(RC_WMORE);
307 }
308
Lev Walkin8d127872004-09-04 04:44:50 +0000309 if(sel && sel->want_nulls
Lev Walkin8c3b8542005-03-10 18:52:02 +0000310 && ((const uint8_t *)buf_ptr)[0] == 0
311 && ((const uint8_t *)buf_ptr)[1] == 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000312 {
Lev Walkin8d127872004-09-04 04:44:50 +0000313
314 ASN_DEBUG("Eat EOC; wn=%d--", sel->want_nulls);
315
Lev Walkin3a4689a2006-11-24 11:20:27 +0000316 if(type_variant == ASN_OSUBV_ANY
Lev Walkin07f388c2004-10-11 11:43:08 +0000317 && (tag_mode != 1 || sel->cont_level))
318 APPEND("\0\0", 2);
Lev Walkin5c915992004-09-27 20:54:06 +0000319
320 ADVANCE(2);
321 sel->got += 2;
322 if(sel->left != -1) {
323 sel->left -= 2; /* assert(sel->left >= 2) */
324 }
325
Lev Walkinf15320b2004-06-03 03:38:44 +0000326 sel->want_nulls--;
327 if(sel->want_nulls == 0) {
328 /* Move to the next expectation */
Lev Walkin5c915992004-09-27 20:54:06 +0000329 sel->left = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000330 tlv_constr = 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000331 }
Lev Walkin8d127872004-09-04 04:44:50 +0000332
333 continue;
Lev Walkin188ed2c2004-09-13 08:31:01 +0000334 }
335
336 /*
337 * Set up expected tags,
338 * depending on ASN.1 type being decoded.
339 */
Lev Walkinbbd93252004-10-12 05:57:23 +0000340 switch(type_variant) {
Lev Walkin3a4689a2006-11-24 11:20:27 +0000341 case ASN_OSUBV_BIT:
Lev Walkin188ed2c2004-09-13 08:31:01 +0000342 /* X.690: 8.6.4.1, NOTE 2 */
343 /* Fall through */
Lev Walkin3a4689a2006-11-24 11:20:27 +0000344 case ASN_OSUBV_STR:
Lev Walkin188ed2c2004-09-13 08:31:01 +0000345 default:
346 if(sel) {
Lev Walkin24bf3f62017-08-10 17:58:43 -0700347 unsigned level = sel->cont_level;
Lev Walkin188ed2c2004-09-13 08:31:01 +0000348 if(level < td->all_tags_count) {
349 expected_tag = td->all_tags[level];
350 break;
351 } else if(td->all_tags_count) {
352 expected_tag = td->all_tags
353 [td->all_tags_count - 1];
354 break;
355 }
356 /* else, Fall through */
357 }
358 /* Fall through */
Lev Walkin3a4689a2006-11-24 11:20:27 +0000359 case ASN_OSUBV_ANY:
Lev Walkin188ed2c2004-09-13 08:31:01 +0000360 expected_tag = tlv_tag;
361 break;
362 }
363
364
365 if(tlv_tag != expected_tag) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000366 char buf[2][32];
367 ber_tlv_tag_snprint(tlv_tag,
368 buf[0], sizeof(buf[0]));
369 ber_tlv_tag_snprint(td->tags[td->tags_count-1],
370 buf[1], sizeof(buf[1]));
371 ASN_DEBUG("Tag does not match expectation: %s != %s",
372 buf[0], buf[1]);
373 RETURN(RC_FAIL);
374 }
375
Lev Walkinde4825d2004-09-29 13:20:14 +0000376 tlvl = tl + ll; /* Combined length of T and L encoding */
377 if((tlv_len + tlvl) < 0) {
378 /* tlv_len value is too big */
379 ASN_DEBUG("TLV encoding + length (%ld) is too big",
380 (long)tlv_len);
381 RETURN(RC_FAIL);
382 }
383
Lev Walkinf15320b2004-06-03 03:38:44 +0000384 /*
385 * Append a new expectation.
386 */
Lev Walkin3990ba62004-09-24 20:57:41 +0000387 sel = OS__add_stack_el(stck);
Lev Walkin5c915992004-09-27 20:54:06 +0000388 if(!sel) RETURN(RC_FAIL);
Lev Walkinf15320b2004-06-03 03:38:44 +0000389
Lev Walkin5c915992004-09-27 20:54:06 +0000390 sel->tag = tlv_tag;
391
392 sel->want_nulls = (tlv_len==-1);
393 if(sel->prev && sel->prev->left != -1) {
394 /* Check that the parent frame is big enough */
395 if(sel->prev->left < tlvl + (tlv_len==-1?0:tlv_len))
396 RETURN(RC_FAIL);
397 if(tlv_len == -1)
398 sel->left = sel->prev->left - tlvl;
399 else
400 sel->left = tlv_len;
401 } else {
402 sel->left = tlv_len;
403 }
Lev Walkin3a4689a2006-11-24 11:20:27 +0000404 if(type_variant == ASN_OSUBV_ANY
Lev Walkin07f388c2004-10-11 11:43:08 +0000405 && (tag_mode != 1 || sel->cont_level))
406 APPEND(buf_ptr, tlvl);
Lev Walkin5c915992004-09-27 20:54:06 +0000407 sel->got += tlvl;
408 ADVANCE(tlvl);
409
Lev Walkin24bf3f62017-08-10 17:58:43 -0700410 ASN_DEBUG("+EXPECT2 got=%ld left=%ld, wn=%d, clvl=%u",
Lev Walkinabf68892004-10-26 10:12:14 +0000411 (long)sel->got, (long)sel->left,
412 sel->want_nulls, sel->cont_level);
Lev Walkin5c915992004-09-27 20:54:06 +0000413
Lev Walkinf15320b2004-06-03 03:38:44 +0000414 } while(tlv_constr);
415 if(sel == NULL) {
416 /* Finished operation, "phase out" */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000417 ASN_DEBUG("Phase out");
Lev Walkinf15320b2004-06-03 03:38:44 +0000418 _CH_PHASE(ctx, +3);
419 break;
420 }
421
422 NEXT_PHASE(ctx);
423 /* Fall through */
424 case 2:
Lev Walkin814cca72004-12-15 23:23:53 +0000425 stck = (struct _stack *)ctx->ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000426 sel = stck->cur_ptr;
Lev Walkin5c915992004-09-27 20:54:06 +0000427 ASN_DEBUG("Phase 2: Need %ld bytes, size=%ld, alrg=%ld, wn=%d",
428 (long)sel->left, (long)size, (long)sel->got,
429 sel->want_nulls);
Lev Walkinf15320b2004-06-03 03:38:44 +0000430 {
431 ber_tlv_len_t len;
432
433 assert(sel->left >= 0);
434
Lev Walkinec1ffd42004-08-18 04:53:32 +0000435 len = ((ber_tlv_len_t)size < sel->left)
436 ? (ber_tlv_len_t)size : sel->left;
Lev Walkinf15320b2004-06-03 03:38:44 +0000437 if(len > 0) {
Lev Walkin3a4689a2006-11-24 11:20:27 +0000438 if(type_variant == ASN_OSUBV_BIT
Lev Walkin188ed2c2004-09-13 08:31:01 +0000439 && sel->bits_chopped == 0) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000440 /* Put the unused-bits-octet away */
Lev Walkin8c3b8542005-03-10 18:52:02 +0000441 st->bits_unused = *(const uint8_t *)buf_ptr;
442 APPEND(((const char *)buf_ptr+1), (len - 1));
Lev Walkinf15320b2004-06-03 03:38:44 +0000443 sel->bits_chopped = 1;
444 } else {
445 APPEND(buf_ptr, len);
446 }
447 ADVANCE(len);
448 sel->left -= len;
Lev Walkin5c915992004-09-27 20:54:06 +0000449 sel->got += len;
Lev Walkinf15320b2004-06-03 03:38:44 +0000450 }
451
Lev Walkin5c915992004-09-27 20:54:06 +0000452 if(sel->left) {
453 ASN_DEBUG("OS left %ld, size = %ld, wn=%d\n",
454 (long)sel->left, (long)size, sel->want_nulls);
Lev Walkinf15320b2004-06-03 03:38:44 +0000455 RETURN(RC_WMORE);
Lev Walkinf15320b2004-06-03 03:38:44 +0000456 }
Lev Walkin5c915992004-09-27 20:54:06 +0000457
458 PREV_PHASE(ctx);
459 goto phase1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000460 }
461 break;
462 case 3:
463 phase3:
464 /*
465 * Primitive form, no stack required.
466 */
Lev Walkinbbd93252004-10-12 05:57:23 +0000467 assert(ctx->left >= 0);
468
Lev Walkind9bd7752004-06-05 08:17:50 +0000469 if(size < (size_t)ctx->left) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000470 if(!size) RETURN(RC_WMORE);
Lev Walkin3a4689a2006-11-24 11:20:27 +0000471 if(type_variant == ASN_OSUBV_BIT && !ctx->context) {
Lev Walkin8c3b8542005-03-10 18:52:02 +0000472 st->bits_unused = *(const uint8_t *)buf_ptr;
Lev Walkinbbd93252004-10-12 05:57:23 +0000473 ctx->left--;
474 ADVANCE(1);
475 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000476 APPEND(buf_ptr, size);
Lev Walkin75b1bef2005-04-25 19:38:21 +0000477 assert(ctx->context > 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000478 ctx->left -= size;
479 ADVANCE(size);
480 RETURN(RC_WMORE);
481 } else {
Lev Walkin3a4689a2006-11-24 11:20:27 +0000482 if(type_variant == ASN_OSUBV_BIT
Lev Walkin75b1bef2005-04-25 19:38:21 +0000483 && !ctx->context && ctx->left) {
Lev Walkin8c3b8542005-03-10 18:52:02 +0000484 st->bits_unused = *(const uint8_t *)buf_ptr;
Lev Walkinbbd93252004-10-12 05:57:23 +0000485 ctx->left--;
486 ADVANCE(1);
487 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000488 APPEND(buf_ptr, ctx->left);
489 ADVANCE(ctx->left);
490 ctx->left = 0;
491
492 NEXT_PHASE(ctx);
493 }
494 break;
495 }
496
Lev Walkin5c915992004-09-27 20:54:06 +0000497 if(sel) {
498 ASN_DEBUG("3sel p=%p, wn=%d, l=%ld, g=%ld, size=%ld",
499 sel->prev, sel->want_nulls,
500 (long)sel->left, (long)sel->got, (long)size);
501 if(sel->prev || sel->want_nulls > 1 || sel->left > 0) {
502 RETURN(RC_WMORE);
503 }
504 }
505
Lev Walkinf15320b2004-06-03 03:38:44 +0000506 /*
507 * BIT STRING-specific processing.
508 */
Lev Walkin0368c622017-09-18 00:33:16 -0700509 if(type_variant == ASN_OSUBV_BIT) {
510 if(st->size) {
511 if(st->bits_unused < 0 || st->bits_unused > 7) {
512 RETURN(RC_FAIL);
513 }
514 /* Finalize BIT STRING: zero out unused bits. */
515 st->buf[st->size-1] &= 0xff << st->bits_unused;
516 } else {
517 if(st->bits_unused) {
518 RETURN(RC_FAIL);
519 }
520 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000521 }
522
Lev Walkinabf68892004-10-26 10:12:14 +0000523 ASN_DEBUG("Took %ld bytes to encode %s: [%s]:%ld",
524 (long)consumed_myself, td->name,
Lev Walkin3a4689a2006-11-24 11:20:27 +0000525 (type_variant == ASN_OSUBV_STR) ? (char *)st->buf : "<data>",
Lev Walkinabf68892004-10-26 10:12:14 +0000526 (long)st->size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000527
Lev Walkinf15320b2004-06-03 03:38:44 +0000528
Lev Walkin5c915992004-09-27 20:54:06 +0000529 RETURN(RC_OK);
Lev Walkinf15320b2004-06-03 03:38:44 +0000530}
531
532/*
533 * Encode OCTET STRING type using DER.
534 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000535asn_enc_rval_t
Lev Walkinbbd93252004-10-12 05:57:23 +0000536OCTET_STRING_encode_der(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000537 int tag_mode, ber_tlv_tag_t tag,
538 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000539 asn_enc_rval_t er;
540 asn_OCTET_STRING_specifics_t *specs = td->specifics
Lev Walkindc06f6b2004-10-20 15:50:55 +0000541 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +0800542 : &asn_SPC_OCTET_STRING_specs;
Lev Walkinbbd93252004-10-12 05:57:23 +0000543 BIT_STRING_t *st = (BIT_STRING_t *)sptr;
Lev Walkin3a4689a2006-11-24 11:20:27 +0000544 enum asn_OS_Subvariant type_variant = specs->subvariant;
Lev Walkinbbd93252004-10-12 05:57:23 +0000545 int fix_last_byte = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000546
547 ASN_DEBUG("%s %s as OCTET STRING",
Lev Walkin1f670c12004-09-02 12:57:25 +0000548 cb?"Estimating":"Encoding", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000549
550 /*
Lev Walkinbbd93252004-10-12 05:57:23 +0000551 * Write tags.
Lev Walkinf15320b2004-06-03 03:38:44 +0000552 */
Lev Walkin3a4689a2006-11-24 11:20:27 +0000553 if(type_variant != ASN_OSUBV_ANY || tag_mode == 1) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000554 er.encoded = der_write_tags(td,
Lev Walkin3a4689a2006-11-24 11:20:27 +0000555 (type_variant == ASN_OSUBV_BIT) + st->size,
556 tag_mode, type_variant == ASN_OSUBV_ANY, tag,
557 cb, app_key);
Lev Walkinbbd93252004-10-12 05:57:23 +0000558 if(er.encoded == -1) {
559 er.failed_type = td;
560 er.structure_ptr = sptr;
561 return er;
Lev Walkinf15320b2004-06-03 03:38:44 +0000562 }
Lev Walkinbbd93252004-10-12 05:57:23 +0000563 } else {
564 /* Disallow: [<tag>] IMPLICIT ANY */
Lev Walkin3a4689a2006-11-24 11:20:27 +0000565 assert(type_variant != ASN_OSUBV_ANY || tag_mode != -1);
Lev Walkinbbd93252004-10-12 05:57:23 +0000566 er.encoded = 0;
567 }
568
569 if(!cb) {
Lev Walkin3a4689a2006-11-24 11:20:27 +0000570 er.encoded += (type_variant == ASN_OSUBV_BIT) + st->size;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700571 ASN__ENCODED_OK(er);
Lev Walkinf15320b2004-06-03 03:38:44 +0000572 }
573
Lev Walkin07f388c2004-10-11 11:43:08 +0000574 /*
Lev Walkinbbd93252004-10-12 05:57:23 +0000575 * Prepare to deal with the last octet of BIT STRING.
Lev Walkin07f388c2004-10-11 11:43:08 +0000576 */
Lev Walkin3a4689a2006-11-24 11:20:27 +0000577 if(type_variant == ASN_OSUBV_BIT) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000578 uint8_t b = st->bits_unused & 0x07;
579 if(b && st->size) fix_last_byte = 1;
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 /* Invoke callback for the main part of the buffer */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700584 ASN__CALLBACK(st->buf, st->size - fix_last_byte);
Lev Walkinf15320b2004-06-03 03:38:44 +0000585
Lev Walkinbbd93252004-10-12 05:57:23 +0000586 /* The last octet should be stripped off the unused bits */
587 if(fix_last_byte) {
588 uint8_t b = st->buf[st->size-1] & (0xff << st->bits_unused);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700589 ASN__CALLBACK(&b, 1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000590 }
591
Lev Walkin7c1dc052016-03-14 03:08:15 -0700592 ASN__ENCODED_OK(er);
Lev Walkinbbd93252004-10-12 05:57:23 +0000593cb_failed:
Lev Walkin7c1dc052016-03-14 03:08:15 -0700594 ASN__ENCODE_FAILED;
Lev Walkinf15320b2004-06-03 03:38:44 +0000595}
596
Lev Walkina9cc46e2004-09-22 16:06:28 +0000597asn_enc_rval_t
Lev Walkinde4825d2004-09-29 13:20:14 +0000598OCTET_STRING_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000599 int ilevel, enum xer_encoder_flags_e flags,
600 asn_app_consume_bytes_f *cb, void *app_key) {
Wim Lewis59e8d282014-08-04 12:39:35 -0700601 const char * const h2c = "0123456789ABCDEF";
Lev Walkina9cc46e2004-09-22 16:06:28 +0000602 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
603 asn_enc_rval_t er;
604 char scratch[16 * 3 + 4];
605 char *p = scratch;
606 uint8_t *buf;
607 uint8_t *end;
608 size_t i;
609
Lev Walkin2ddd3082006-10-16 12:32:14 +0000610 if(!st || (!st->buf && st->size))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700611 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000612
613 er.encoded = 0;
614
615 /*
616 * Dump the contents of the buffer in hexadecimal.
617 */
618 buf = st->buf;
619 end = buf + st->size;
620 if(flags & XER_F_CANONICAL) {
621 char *scend = scratch + (sizeof(scratch) - 2);
622 for(; buf < end; buf++) {
623 if(p >= scend) {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700624 ASN__CALLBACK(scratch, p - scratch);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000625 p = scratch;
626 }
627 *p++ = h2c[(*buf >> 4) & 0x0F];
628 *p++ = h2c[*buf & 0x0F];
629 }
Lev Walkincc6a9102004-09-23 22:06:26 +0000630
Lev Walkin7c1dc052016-03-14 03:08:15 -0700631 ASN__CALLBACK(scratch, p-scratch); /* Dump the rest */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000632 } else {
633 for(i = 0; buf < end; buf++, i++) {
634 if(!(i % 16) && (i || st->size > 16)) {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700635 ASN__CALLBACK(scratch, p-scratch);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000636 p = scratch;
Lev Walkinc6cac8e2016-03-14 02:57:07 -0700637 ASN__TEXT_INDENT(1, ilevel);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000638 }
639 *p++ = h2c[(*buf >> 4) & 0x0F];
640 *p++ = h2c[*buf & 0x0F];
641 *p++ = 0x20;
642 }
Lev Walkincc6a9102004-09-23 22:06:26 +0000643 if(p - scratch) {
644 p--; /* Remove the tail space */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700645 ASN__CALLBACK(scratch, p-scratch); /* Dump the rest */
Lev Walkincc6a9102004-09-23 22:06:26 +0000646 if(st->size > 16)
Lev Walkinc6cac8e2016-03-14 02:57:07 -0700647 ASN__TEXT_INDENT(1, ilevel-1);
Lev Walkincc6a9102004-09-23 22:06:26 +0000648 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000649 }
650
Lev Walkin7c1dc052016-03-14 03:08:15 -0700651 ASN__ENCODED_OK(er);
Lev Walkin942fd082004-10-03 09:13:02 +0000652cb_failed:
Lev Walkin7c1dc052016-03-14 03:08:15 -0700653 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000654}
655
Wim Lewis18c2ec92014-07-29 11:30:10 -0700656static const struct OCTET_STRING__xer_escape_table_s {
657 const char *string;
Lev Walkin9ca81892004-10-03 10:54:25 +0000658 int size;
659} OCTET_STRING__xer_escape_table[] = {
660#define OSXET(s) { s, sizeof(s) - 1 }
661 OSXET("\074\156\165\154\057\076"), /* <nul/> */
662 OSXET("\074\163\157\150\057\076"), /* <soh/> */
663 OSXET("\074\163\164\170\057\076"), /* <stx/> */
664 OSXET("\074\145\164\170\057\076"), /* <etx/> */
665 OSXET("\074\145\157\164\057\076"), /* <eot/> */
666 OSXET("\074\145\156\161\057\076"), /* <enq/> */
667 OSXET("\074\141\143\153\057\076"), /* <ack/> */
668 OSXET("\074\142\145\154\057\076"), /* <bel/> */
669 OSXET("\074\142\163\057\076"), /* <bs/> */
670 OSXET("\011"), /* \t */
671 OSXET("\012"), /* \n */
672 OSXET("\074\166\164\057\076"), /* <vt/> */
673 OSXET("\074\146\146\057\076"), /* <ff/> */
674 OSXET("\015"), /* \r */
675 OSXET("\074\163\157\057\076"), /* <so/> */
676 OSXET("\074\163\151\057\076"), /* <si/> */
677 OSXET("\074\144\154\145\057\076"), /* <dle/> */
678 OSXET("\074\144\143\061\057\076"), /* <de1/> */
679 OSXET("\074\144\143\062\057\076"), /* <de2/> */
680 OSXET("\074\144\143\063\057\076"), /* <de3/> */
681 OSXET("\074\144\143\064\057\076"), /* <de4/> */
682 OSXET("\074\156\141\153\057\076"), /* <nak/> */
683 OSXET("\074\163\171\156\057\076"), /* <syn/> */
684 OSXET("\074\145\164\142\057\076"), /* <etb/> */
685 OSXET("\074\143\141\156\057\076"), /* <can/> */
686 OSXET("\074\145\155\057\076"), /* <em/> */
687 OSXET("\074\163\165\142\057\076"), /* <sub/> */
688 OSXET("\074\145\163\143\057\076"), /* <esc/> */
689 OSXET("\074\151\163\064\057\076"), /* <is4/> */
690 OSXET("\074\151\163\063\057\076"), /* <is3/> */
691 OSXET("\074\151\163\062\057\076"), /* <is2/> */
692 OSXET("\074\151\163\061\057\076"), /* <is1/> */
693 { 0, 0 }, /* " " */
694 { 0, 0 }, /* ! */
695 { 0, 0 }, /* \" */
696 { 0, 0 }, /* # */
697 { 0, 0 }, /* $ */
698 { 0, 0 }, /* % */
699 OSXET("\046\141\155\160\073"), /* &amp; */
700 { 0, 0 }, /* ' */
701 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, /* ()*+,-./ */
702 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, /* 01234567 */
703 {0,0},{0,0},{0,0},{0,0}, /* 89:; */
704 OSXET("\046\154\164\073"), /* &lt; */
705 { 0, 0 }, /* = */
706 OSXET("\046\147\164\073"), /* &gt; */
707};
708
Lev Walkindc06f6b2004-10-20 15:50:55 +0000709static int
Lev Walkin0fab1a62005-03-09 22:19:25 +0000710OS__check_escaped_control_char(const void *buf, int size) {
Lev Walkindc06f6b2004-10-20 15:50:55 +0000711 size_t i;
712 /*
713 * Inefficient algorithm which translates the escape sequences
714 * defined above into characters. Returns -1 if not found.
715 * TODO: replace by a faster algorithm (bsearch(), hash or
716 * nested table lookups).
717 */
718 for(i = 0; i < 32 /* Don't spend time on the bottom half */; i++) {
Wim Lewis18c2ec92014-07-29 11:30:10 -0700719 const struct OCTET_STRING__xer_escape_table_s *el;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000720 el = &OCTET_STRING__xer_escape_table[i];
721 if(el->size == size && memcmp(buf, el->string, size) == 0)
722 return i;
723 }
724 return -1;
725}
726
727static int
Lev Walkin0fab1a62005-03-09 22:19:25 +0000728OCTET_STRING__handle_control_chars(void *struct_ptr, const void *chunk_buf, size_t chunk_size) {
Lev Walkindc06f6b2004-10-20 15:50:55 +0000729 /*
730 * This might be one of the escape sequences
731 * for control characters. Check it out.
732 * #11.15.5
733 */
734 int control_char = OS__check_escaped_control_char(chunk_buf,chunk_size);
735 if(control_char >= 0) {
736 OCTET_STRING_t *st = (OCTET_STRING_t *)struct_ptr;
737 void *p = REALLOC(st->buf, st->size + 2);
738 if(p) {
739 st->buf = (uint8_t *)p;
740 st->buf[st->size++] = control_char;
741 st->buf[st->size] = '\0'; /* nul-termination */
742 return 0;
743 }
744 }
745
746 return -1; /* No, it's not */
747}
748
Lev Walkina9cc46e2004-09-22 16:06:28 +0000749asn_enc_rval_t
Lev Walkindc06f6b2004-10-20 15:50:55 +0000750OCTET_STRING_encode_xer_utf8(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000751 int ilevel, enum xer_encoder_flags_e flags,
752 asn_app_consume_bytes_f *cb, void *app_key) {
753 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
754 asn_enc_rval_t er;
Lev Walkin9ca81892004-10-03 10:54:25 +0000755 uint8_t *buf, *end;
756 uint8_t *ss; /* Sequence start */
757 ssize_t encoded_len = 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000758
759 (void)ilevel; /* Unused argument */
760 (void)flags; /* Unused argument */
761
Lev Walkin2ddd3082006-10-16 12:32:14 +0000762 if(!st || (!st->buf && st->size))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700763 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000764
Lev Walkin9ca81892004-10-03 10:54:25 +0000765 buf = st->buf;
766 end = buf + st->size;
767 for(ss = buf; buf < end; buf++) {
Lev Walkin443d2512004-10-05 06:35:31 +0000768 unsigned int ch = *buf;
Lev Walkin9ca81892004-10-03 10:54:25 +0000769 int s_len; /* Special encoding sequence length */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000770
Lev Walkin9ca81892004-10-03 10:54:25 +0000771 /*
772 * Escape certain characters: X.680/11.15
773 */
774 if(ch < sizeof(OCTET_STRING__xer_escape_table)
775 /sizeof(OCTET_STRING__xer_escape_table[0])
776 && (s_len = OCTET_STRING__xer_escape_table[ch].size)) {
777 if(((buf - ss) && cb(ss, buf - ss, app_key) < 0)
778 || cb(OCTET_STRING__xer_escape_table[ch].string, s_len,
779 app_key) < 0)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700780 ASN__ENCODE_FAILED;
Lev Walkin9ca81892004-10-03 10:54:25 +0000781 encoded_len += (buf - ss) + s_len;
782 ss = buf + 1;
783 }
784 }
785
786 encoded_len += (buf - ss);
787 if((buf - ss) && cb(ss, buf - ss, app_key) < 0)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700788 ASN__ENCODE_FAILED;
Lev Walkin9ca81892004-10-03 10:54:25 +0000789
790 er.encoded = encoded_len;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700791 ASN__ENCODED_OK(er);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000792}
793
Lev Walkindc06f6b2004-10-20 15:50:55 +0000794/*
795 * Convert from hexadecimal format (cstring): "AB CD EF"
796 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000797static 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 +0000798 OCTET_STRING_t *st = (OCTET_STRING_t *)sptr;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000799 const char *chunk_stop = (const char *)chunk_buf;
800 const char *p = chunk_stop;
801 const char *pend = p + chunk_size;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000802 unsigned int clv = 0;
803 int half = 0; /* Half bit */
804 uint8_t *buf;
805
806 /* Reallocate buffer according to high cap estimation */
Lev Walkin494fb702017-08-07 20:07:00 -0700807 size_t new_size = st->size + (chunk_size + 1) / 2;
808 void *nptr = REALLOC(st->buf, new_size + 1);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000809 if(!nptr) return -1;
810 st->buf = (uint8_t *)nptr;
811 buf = st->buf + st->size;
812
813 /*
814 * If something like " a b c " appears here, the " a b":3 will be
815 * converted, and the rest skipped. That is, unless buf_size is greater
816 * than chunk_size, then it'll be equivalent to "ABC0".
817 */
818 for(; p < pend; p++) {
Lev Walkin0fab1a62005-03-09 22:19:25 +0000819 int ch = *(const unsigned char *)p;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000820 switch(ch) {
821 case 0x09: case 0x0a: case 0x0c: case 0x0d:
822 case 0x20:
823 /* Ignore whitespace */
824 continue;
825 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: /*01234*/
826 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: /*56789*/
827 clv = (clv << 4) + (ch - 0x30);
828 break;
829 case 0x41: case 0x42: case 0x43: /* ABC */
830 case 0x44: case 0x45: case 0x46: /* DEF */
Lev Walkin3ae21bd2005-02-21 14:43:48 +0000831 clv = (clv << 4) + (ch - 0x41 + 10);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000832 break;
833 case 0x61: case 0x62: case 0x63: /* abc */
834 case 0x64: case 0x65: case 0x66: /* def */
Lev Walkin3ae21bd2005-02-21 14:43:48 +0000835 clv = (clv << 4) + (ch - 0x61 + 10);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000836 break;
837 default:
838 *buf = 0; /* JIC */
839 return -1;
840 }
841 if(half++) {
842 half = 0;
843 *buf++ = clv;
844 chunk_stop = p + 1;
845 }
846 }
847
848 /*
849 * Check partial decoding.
850 */
851 if(half) {
852 if(have_more) {
853 /*
854 * Partial specification is fine,
855 * because no more more PXER_TEXT data is available.
856 */
857 *buf++ = clv << 4;
858 chunk_stop = p;
859 }
860 } else {
861 chunk_stop = p;
862 }
863
864 st->size = buf - st->buf; /* Adjust the buffer size */
Lev Walkin494fb702017-08-07 20:07:00 -0700865 assert(st->size <= new_size);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000866 st->buf[st->size] = 0; /* Courtesy termination */
867
Lev Walkin0fab1a62005-03-09 22:19:25 +0000868 return (chunk_stop - (const char *)chunk_buf); /* Converted size */
Lev Walkindc06f6b2004-10-20 15:50:55 +0000869}
870
871/*
872 * Convert from binary format: "00101011101"
873 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000874static 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 +0000875 BIT_STRING_t *st = (BIT_STRING_t *)sptr;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000876 const char *p = (const char *)chunk_buf;
877 const char *pend = p + chunk_size;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000878 int bits_unused = st->bits_unused & 0x7;
879 uint8_t *buf;
880
881 /* Reallocate buffer according to high cap estimation */
Lev Walkin494fb702017-08-07 20:07:00 -0700882 size_t new_size = st->size + (chunk_size + 7) / 8;
883 void *nptr = REALLOC(st->buf, new_size + 1);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000884 if(!nptr) return -1;
885 st->buf = (uint8_t *)nptr;
886 buf = st->buf + st->size;
887
888 (void)have_more;
889
890 if(bits_unused == 0)
891 bits_unused = 8;
892 else if(st->size)
893 buf--;
894
895 /*
896 * Convert series of 0 and 1 into the octet string.
897 */
898 for(; p < pend; p++) {
Lev Walkin0fab1a62005-03-09 22:19:25 +0000899 int ch = *(const unsigned char *)p;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000900 switch(ch) {
901 case 0x09: case 0x0a: case 0x0c: case 0x0d:
902 case 0x20:
903 /* Ignore whitespace */
904 break;
905 case 0x30:
906 case 0x31:
907 if(bits_unused-- <= 0) {
908 *++buf = 0; /* Clean the cell */
909 bits_unused = 7;
910 }
911 *buf |= (ch&1) << bits_unused;
912 break;
913 default:
914 st->bits_unused = bits_unused;
915 return -1;
916 }
917 }
918
919 if(bits_unused == 8) {
920 st->size = buf - st->buf;
921 st->bits_unused = 0;
922 } else {
923 st->size = buf - st->buf + 1;
924 st->bits_unused = bits_unused;
925 }
926
Lev Walkin494fb702017-08-07 20:07:00 -0700927 assert(st->size <= new_size);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000928 st->buf[st->size] = 0; /* Courtesy termination */
929
930 return chunk_size; /* Converted in full */
931}
932
933/*
934 * Something like strtod(), but with stricter rules.
935 */
936static int
Lev Walkin0fab1a62005-03-09 22:19:25 +0000937OS__strtoent(int base, const char *buf, const char *end, int32_t *ret_value) {
Lev Walkinbeedbde2017-09-17 22:54:51 -0700938 const int32_t last_unicode_codepoint = 0x10ffff;
Lev Walkin33700162004-10-26 09:03:31 +0000939 int32_t val = 0;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000940 const char *p;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000941
942 for(p = buf; p < end; p++) {
943 int ch = *p;
Lev Walkinb0f3db62005-07-03 05:30:15 +0000944
Lev Walkindc06f6b2004-10-20 15:50:55 +0000945 switch(ch) {
946 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: /*01234*/
947 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: /*56789*/
948 val = val * base + (ch - 0x30);
949 break;
950 case 0x41: case 0x42: case 0x43: /* ABC */
951 case 0x44: case 0x45: case 0x46: /* DEF */
Lev Walkin3ae21bd2005-02-21 14:43:48 +0000952 val = val * base + (ch - 0x41 + 10);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000953 break;
954 case 0x61: case 0x62: case 0x63: /* abc */
955 case 0x64: case 0x65: case 0x66: /* def */
Lev Walkin3ae21bd2005-02-21 14:43:48 +0000956 val = val * base + (ch - 0x61 + 10);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000957 break;
958 case 0x3b: /* ';' */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000959 *ret_value = val;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000960 return (p - buf) + 1;
961 default:
962 return -1; /* Character set error */
963 }
Lev Walkinbeedbde2017-09-17 22:54:51 -0700964
965 /* Value exceeds the Unicode range. */
966 if(val > last_unicode_codepoint) {
967 return -1;
968 }
Lev Walkindc06f6b2004-10-20 15:50:55 +0000969 }
970
Lev Walkinb0f3db62005-07-03 05:30:15 +0000971 *ret_value = -1;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000972 return (p - buf);
973}
974
975/*
976 * Convert from the plain UTF-8 format, expanding entity references: "2 &lt; 3"
977 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000978static 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 +0000979 OCTET_STRING_t *st = (OCTET_STRING_t *)sptr;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000980 const char *p = (const char *)chunk_buf;
981 const char *pend = p + chunk_size;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000982 uint8_t *buf;
983
984 /* Reallocate buffer */
Lev Walkin494fb702017-08-07 20:07:00 -0700985 size_t new_size = st->size + chunk_size;
986 void *nptr = REALLOC(st->buf, new_size + 1);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000987 if(!nptr) return -1;
988 st->buf = (uint8_t *)nptr;
989 buf = st->buf + st->size;
990
991 /*
992 * Convert series of 0 and 1 into the octet string.
993 */
994 for(; p < pend; p++) {
Lev Walkin0fab1a62005-03-09 22:19:25 +0000995 int ch = *(const unsigned char *)p;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000996 int len; /* Length of the rest of the chunk */
997
998 if(ch != 0x26 /* '&' */) {
999 *buf++ = ch;
1000 continue; /* That was easy... */
1001 }
1002
1003 /*
1004 * Process entity reference.
1005 */
Lev Walkin0fab1a62005-03-09 22:19:25 +00001006 len = chunk_size - (p - (const char *)chunk_buf);
Lev Walkindc06f6b2004-10-20 15:50:55 +00001007 if(len == 1 /* "&" */) goto want_more;
1008 if(p[1] == 0x23 /* '#' */) {
Lev Walkin0fab1a62005-03-09 22:19:25 +00001009 const char *pval; /* Pointer to start of digits */
Lev Walkinbc0dce32006-07-27 09:56:13 +00001010 int32_t val = 0; /* Entity reference value */
Lev Walkindc06f6b2004-10-20 15:50:55 +00001011 int base;
1012
1013 if(len == 2 /* "&#" */) goto want_more;
1014 if(p[2] == 0x78 /* 'x' */)
1015 pval = p + 3, base = 16;
1016 else
1017 pval = p + 2, base = 10;
1018 len = OS__strtoent(base, pval, p + len, &val);
1019 if(len == -1) {
1020 /* Invalid charset. Just copy verbatim. */
1021 *buf++ = ch;
1022 continue;
1023 }
1024 if(!len || pval[len-1] != 0x3b) goto want_more;
1025 assert(val > 0);
1026 p += (pval - p) + len - 1; /* Advance past entref */
1027
1028 if(val < 0x80) {
1029 *buf++ = (char)val;
1030 } else if(val < 0x800) {
1031 *buf++ = 0xc0 | ((val >> 6));
1032 *buf++ = 0x80 | ((val & 0x3f));
1033 } else if(val < 0x10000) {
1034 *buf++ = 0xe0 | ((val >> 12));
1035 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1036 *buf++ = 0x80 | ((val & 0x3f));
1037 } else if(val < 0x200000) {
1038 *buf++ = 0xf0 | ((val >> 18));
1039 *buf++ = 0x80 | ((val >> 12) & 0x3f);
1040 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1041 *buf++ = 0x80 | ((val & 0x3f));
1042 } else if(val < 0x4000000) {
1043 *buf++ = 0xf8 | ((val >> 24));
1044 *buf++ = 0x80 | ((val >> 18) & 0x3f);
1045 *buf++ = 0x80 | ((val >> 12) & 0x3f);
1046 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1047 *buf++ = 0x80 | ((val & 0x3f));
1048 } else {
1049 *buf++ = 0xfc | ((val >> 30) & 0x1);
1050 *buf++ = 0x80 | ((val >> 24) & 0x3f);
1051 *buf++ = 0x80 | ((val >> 18) & 0x3f);
1052 *buf++ = 0x80 | ((val >> 12) & 0x3f);
1053 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1054 *buf++ = 0x80 | ((val & 0x3f));
1055 }
1056 } else {
1057 /*
1058 * Ugly, limited parsing of &amp; &gt; &lt;
1059 */
1060 char *sc = (char *)memchr(p, 0x3b, len > 5 ? 5 : len);
1061 if(!sc) goto want_more;
1062 if((sc - p) == 4
1063 && p[1] == 0x61 /* 'a' */
1064 && p[2] == 0x6d /* 'm' */
1065 && p[3] == 0x70 /* 'p' */) {
1066 *buf++ = 0x26;
1067 p = sc;
1068 continue;
1069 }
1070 if((sc - p) == 3) {
1071 if(p[1] == 0x6c) {
1072 *buf = 0x3c; /* '<' */
1073 } else if(p[1] == 0x67) {
1074 *buf = 0x3e; /* '>' */
1075 } else {
1076 /* Unsupported entity reference */
1077 *buf++ = ch;
1078 continue;
1079 }
1080 if(p[2] != 0x74) {
1081 /* Unsupported entity reference */
1082 *buf++ = ch;
1083 continue;
1084 }
1085 buf++;
1086 p = sc;
1087 continue;
1088 }
1089 /* Unsupported entity reference */
1090 *buf++ = ch;
1091 }
1092
1093 continue;
1094 want_more:
1095 if(have_more) {
1096 /*
1097 * We know that no more data (of the same type)
1098 * is coming. Copy the rest verbatim.
1099 */
1100 *buf++ = ch;
1101 continue;
1102 }
Lev Walkin0fab1a62005-03-09 22:19:25 +00001103 chunk_size = (p - (const char *)chunk_buf);
Lev Walkindc06f6b2004-10-20 15:50:55 +00001104 /* Processing stalled: need more data */
Lev Walkinbdaae772005-02-18 12:25:47 +00001105 break;
Lev Walkindc06f6b2004-10-20 15:50:55 +00001106 }
1107
1108 st->size = buf - st->buf;
Lev Walkin494fb702017-08-07 20:07:00 -07001109 assert(st->size <= new_size);
Lev Walkindc06f6b2004-10-20 15:50:55 +00001110 st->buf[st->size] = 0; /* Courtesy termination */
1111
1112 return chunk_size; /* Converted in full */
1113}
1114
1115/*
1116 * Decode OCTET STRING from the XML element's body.
1117 */
1118static asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -07001119OCTET_STRING__decode_xer(const asn_codec_ctx_t *opt_codec_ctx,
Lev Walkindc06f6b2004-10-20 15:50:55 +00001120 asn_TYPE_descriptor_t *td, void **sptr,
Lev Walkin8c3b8542005-03-10 18:52:02 +00001121 const char *opt_mname, const void *buf_ptr, size_t size,
Lev Walkindc06f6b2004-10-20 15:50:55 +00001122 int (*opt_unexpected_tag_decoder)
Lev Walkin0fab1a62005-03-09 22:19:25 +00001123 (void *struct_ptr, const void *chunk_buf, size_t chunk_size),
Lev Walkindc06f6b2004-10-20 15:50:55 +00001124 ssize_t (*body_receiver)
Lev Walkin0fab1a62005-03-09 22:19:25 +00001125 (void *struct_ptr, const void *chunk_buf, size_t chunk_size,
Lev Walkindc06f6b2004-10-20 15:50:55 +00001126 int have_more)
1127) {
Lev Walkind5125642005-02-14 20:08:00 +00001128 OCTET_STRING_t *st = (OCTET_STRING_t *)*sptr;
Lev Walkindc06f6b2004-10-20 15:50:55 +00001129 asn_OCTET_STRING_specifics_t *specs = td->specifics
1130 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +08001131 : &asn_SPC_OCTET_STRING_specs;
Lev Walkindc06f6b2004-10-20 15:50:55 +00001132 const char *xml_tag = opt_mname ? opt_mname : td->xml_tag;
1133 asn_struct_ctx_t *ctx; /* Per-structure parser context */
Lev Walkinc61f3862005-02-14 17:21:22 +00001134 asn_dec_rval_t rval; /* Return value from the decoder */
1135 int st_allocated;
Lev Walkindc06f6b2004-10-20 15:50:55 +00001136
1137 /*
1138 * Create the string if does not exist.
1139 */
Lev Walkinc61f3862005-02-14 17:21:22 +00001140 if(!st) {
Lev Walkin8484ed82004-12-14 13:31:01 +00001141 st = (OCTET_STRING_t *)CALLOC(1, specs->struct_size);
Lev Walkin8484ed82004-12-14 13:31:01 +00001142 *sptr = (void *)st;
Lev Walkinc61f3862005-02-14 17:21:22 +00001143 if(!st) goto sta_failed;
1144 st_allocated = 1;
Lev Walkinbdaae772005-02-18 12:25:47 +00001145 } else {
1146 st_allocated = 0;
1147 }
Lev Walkinc61f3862005-02-14 17:21:22 +00001148 if(!st->buf) {
1149 /* This is separate from above section */
1150 st->buf = (uint8_t *)CALLOC(1, 1);
1151 if(!st->buf) {
1152 if(st_allocated) {
1153 *sptr = 0;
1154 goto stb_failed;
1155 } else {
1156 goto sta_failed;
1157 }
Lev Walkindc06f6b2004-10-20 15:50:55 +00001158 }
1159 }
1160
1161 /* Restore parsing context */
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001162 ctx = (asn_struct_ctx_t *)(((char *)*sptr) + specs->ctx_offset);
Lev Walkindc06f6b2004-10-20 15:50:55 +00001163
1164 return xer_decode_general(opt_codec_ctx, ctx, *sptr, xml_tag,
1165 buf_ptr, size, opt_unexpected_tag_decoder, body_receiver);
Lev Walkinc61f3862005-02-14 17:21:22 +00001166
1167stb_failed:
1168 FREEMEM(st);
1169sta_failed:
1170 rval.code = RC_FAIL;
1171 rval.consumed = 0;
1172 return rval;
Lev Walkindc06f6b2004-10-20 15:50:55 +00001173}
1174
1175/*
1176 * Decode OCTET STRING from the hexadecimal data.
1177 */
1178asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -07001179OCTET_STRING_decode_xer_hex(const asn_codec_ctx_t *opt_codec_ctx,
Lev Walkindc06f6b2004-10-20 15:50:55 +00001180 asn_TYPE_descriptor_t *td, void **sptr,
Lev Walkin8c3b8542005-03-10 18:52:02 +00001181 const char *opt_mname, const void *buf_ptr, size_t size) {
Lev Walkindc06f6b2004-10-20 15:50:55 +00001182 return OCTET_STRING__decode_xer(opt_codec_ctx, td, sptr, opt_mname,
1183 buf_ptr, size, 0, OCTET_STRING__convert_hexadecimal);
1184}
1185
1186/*
1187 * Decode OCTET STRING from the binary (0/1) data.
1188 */
1189asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -07001190OCTET_STRING_decode_xer_binary(const asn_codec_ctx_t *opt_codec_ctx,
Lev Walkindc06f6b2004-10-20 15:50:55 +00001191 asn_TYPE_descriptor_t *td, void **sptr,
Lev Walkin8c3b8542005-03-10 18:52:02 +00001192 const char *opt_mname, const void *buf_ptr, size_t size) {
Lev Walkindc06f6b2004-10-20 15:50:55 +00001193 return OCTET_STRING__decode_xer(opt_codec_ctx, td, sptr, opt_mname,
1194 buf_ptr, size, 0, OCTET_STRING__convert_binary);
1195}
1196
1197/*
1198 * Decode OCTET STRING from the string (ASCII/UTF-8) data.
1199 */
1200asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -07001201OCTET_STRING_decode_xer_utf8(const asn_codec_ctx_t *opt_codec_ctx,
Lev Walkindc06f6b2004-10-20 15:50:55 +00001202 asn_TYPE_descriptor_t *td, void **sptr,
Lev Walkin8c3b8542005-03-10 18:52:02 +00001203 const char *opt_mname, const void *buf_ptr, size_t size) {
Lev Walkindc06f6b2004-10-20 15:50:55 +00001204 return OCTET_STRING__decode_xer(opt_codec_ctx, td, sptr, opt_mname,
1205 buf_ptr, size,
1206 OCTET_STRING__handle_control_chars,
1207 OCTET_STRING__convert_entrefs);
1208}
1209
Lev Walkin0368c622017-09-18 00:33:16 -07001210#ifndef ASN_DISABLE_PER_SUPPORT
1211
Lev Walkin725883b2006-10-09 12:07:58 +00001212static int
Lev Walkin3a4689a2006-11-24 11:20:27 +00001213OCTET_STRING_per_get_characters(asn_per_data_t *po, uint8_t *buf,
1214 size_t units, unsigned int bpc, unsigned int unit_bits,
Lev Walkin494fb702017-08-07 20:07:00 -07001215 long lb, long ub, const asn_per_constraints_t *pc) {
Lev Walkin3a4689a2006-11-24 11:20:27 +00001216 uint8_t *end = buf + units * bpc;
Lev Walkin725883b2006-10-09 12:07:58 +00001217
1218 ASN_DEBUG("Expanding %d characters into (%ld..%ld):%d",
Lev Walkin3a4689a2006-11-24 11:20:27 +00001219 (int)units, lb, ub, unit_bits);
Lev Walkin725883b2006-10-09 12:07:58 +00001220
1221 /* X.691: 27.5.4 */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001222 if((unsigned long)ub <= ((unsigned long)2 << (unit_bits - 1))) {
Lev Walkin725883b2006-10-09 12:07:58 +00001223 /* Decode without translation */
1224 lb = 0;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001225 } else if(pc && pc->code2value) {
1226 if(unit_bits > 16)
1227 return 1; /* FATAL: can't have constrained
1228 * UniversalString with more than
1229 * 16 million code points */
1230 for(; buf < end; buf += bpc) {
Lev Walkin725883b2006-10-09 12:07:58 +00001231 int value;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001232 int code = per_get_few_bits(po, unit_bits);
Lev Walkin725883b2006-10-09 12:07:58 +00001233 if(code < 0) return -1; /* WMORE */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001234 value = pc->code2value(code);
Lev Walkin725883b2006-10-09 12:07:58 +00001235 if(value < 0) {
1236 ASN_DEBUG("Code %d (0x%02x) is"
1237 " not in map (%ld..%ld)",
1238 code, code, lb, ub);
1239 return 1; /* FATAL */
1240 }
Lev Walkin3a4689a2006-11-24 11:20:27 +00001241 switch(bpc) {
1242 case 1: *buf = value; break;
1243 case 2: buf[0] = value >> 8; buf[1] = value; break;
1244 case 4: buf[0] = value >> 24; buf[1] = value >> 16;
1245 buf[2] = value >> 8; buf[3] = value; break;
1246 }
Lev Walkin725883b2006-10-09 12:07:58 +00001247 }
1248 return 0;
1249 }
1250
Lev Walkin3a4689a2006-11-24 11:20:27 +00001251 /* Shortcut the no-op copying to the aligned structure */
1252 if(lb == 0 && (unit_bits == 8 * bpc)) {
1253 return per_get_many_bits(po, buf, 0, unit_bits * units);
1254 }
1255
1256 for(; buf < end; buf += bpc) {
Lev Walkin725883b2006-10-09 12:07:58 +00001257 int code = per_get_few_bits(po, unit_bits);
1258 int ch = code + lb;
1259 if(code < 0) return -1; /* WMORE */
1260 if(ch > ub) {
1261 ASN_DEBUG("Code %d is out of range (%ld..%ld)",
1262 ch, lb, ub);
1263 return 1; /* FATAL */
1264 }
Lev Walkin3a4689a2006-11-24 11:20:27 +00001265 switch(bpc) {
1266 case 1: *buf = ch; break;
1267 case 2: buf[0] = ch >> 8; buf[1] = ch; break;
1268 case 4: buf[0] = ch >> 24; buf[1] = ch >> 16;
1269 buf[2] = ch >> 8; buf[3] = ch; break;
1270 }
Lev Walkin725883b2006-10-09 12:07:58 +00001271 }
1272
1273 return 0;
1274}
1275
1276static int
Lev Walkin3a4689a2006-11-24 11:20:27 +00001277OCTET_STRING_per_put_characters(asn_per_outp_t *po, const uint8_t *buf,
1278 size_t units, unsigned int bpc, unsigned int unit_bits,
Lev Walkin494fb702017-08-07 20:07:00 -07001279 long lb, long ub, const asn_per_constraints_t *pc) {
Lev Walkin3a4689a2006-11-24 11:20:27 +00001280 const uint8_t *end = buf + units * bpc;
Lev Walkin725883b2006-10-09 12:07:58 +00001281
Lev Walkin3a4689a2006-11-24 11:20:27 +00001282 ASN_DEBUG("Squeezing %d characters into (%ld..%ld):%d (%d bpc)",
1283 (int)units, lb, ub, unit_bits, bpc);
Lev Walkin725883b2006-10-09 12:07:58 +00001284
1285 /* X.691: 27.5.4 */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001286 if((unsigned long)ub <= ((unsigned long)2 << (unit_bits - 1))) {
Lev Walkin725883b2006-10-09 12:07:58 +00001287 /* Encode as is */
1288 lb = 0;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001289 } else if(pc && pc->value2code) {
1290 for(; buf < end; buf += bpc) {
1291 int code;
1292 uint32_t value;
1293 switch(bpc) {
1294 case 1: value = *(const uint8_t *)buf; break;
1295 case 2: value = (buf[0] << 8) | buf[1]; break;
1296 case 4: value = (buf[0] << 24) | (buf[1] << 16)
1297 | (buf[2] << 8) | buf[3]; break;
1298 default: return -1;
1299 }
1300 code = pc->value2code(value);
Lev Walkin725883b2006-10-09 12:07:58 +00001301 if(code < 0) {
1302 ASN_DEBUG("Character %d (0x%02x) is"
1303 " not in map (%ld..%ld)",
1304 *buf, *buf, lb, ub);
1305 return -1;
1306 }
1307 if(per_put_few_bits(po, code, unit_bits))
1308 return -1;
1309 }
1310 }
1311
Lev Walkin3a4689a2006-11-24 11:20:27 +00001312 /* Shortcut the no-op copying to the aligned structure */
1313 if(lb == 0 && (unit_bits == 8 * bpc)) {
1314 return per_put_many_bits(po, buf, unit_bits * units);
1315 }
1316
1317 for(ub -= lb; buf < end; buf += bpc) {
1318 int ch;
1319 uint32_t value;
1320 switch(bpc) {
1321 case 1: value = *(const uint8_t *)buf; break;
1322 case 2: value = (buf[0] << 8) | buf[1]; break;
1323 case 4: value = (buf[0] << 24) | (buf[1] << 16)
1324 | (buf[2] << 8) | buf[3]; break;
1325 default: return -1;
1326 }
1327 ch = value - lb;
Lev Walkin725883b2006-10-09 12:07:58 +00001328 if(ch < 0 || ch > ub) {
1329 ASN_DEBUG("Character %d (0x%02x)"
1330 " is out of range (%ld..%ld)",
1331 *buf, *buf, lb, ub + lb);
1332 return -1;
1333 }
1334 if(per_put_few_bits(po, ch, unit_bits))
1335 return -1;
1336 }
1337
1338 return 0;
1339}
1340
Lev Walkin0368c622017-09-18 00:33:16 -07001341static asn_per_constraints_t asn_DEF_OCTET_STRING_constraints = {
1342 { APC_CONSTRAINED, 8, 8, 0, 255 },
1343 { APC_SEMI_CONSTRAINED, -1, -1, 0, 0 },
1344 0, 0
1345};
Lev Walkin62d76872017-08-05 22:49:42 -07001346
Lev Walkin59b176e2005-11-26 11:25:14 +00001347asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -07001348OCTET_STRING_decode_uper(const asn_codec_ctx_t *opt_codec_ctx,
Lev Walkin494fb702017-08-07 20:07:00 -07001349 asn_TYPE_descriptor_t *td,
1350 const asn_per_constraints_t *constraints, void **sptr,
1351 asn_per_data_t *pd) {
1352 asn_OCTET_STRING_specifics_t *specs = td->specifics
Lev Walkin59b176e2005-11-26 11:25:14 +00001353 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +08001354 : &asn_SPC_OCTET_STRING_specs;
Lev Walkin494fb702017-08-07 20:07:00 -07001355 const asn_per_constraints_t *pc =
1356 constraints ? constraints : td->per_constraints;
1357 const asn_per_constraint_t *cval;
1358 const asn_per_constraint_t *csiz;
Lev Walkin59b176e2005-11-26 11:25:14 +00001359 asn_dec_rval_t rval = { RC_OK, 0 };
1360 BIT_STRING_t *st = (BIT_STRING_t *)*sptr;
1361 ssize_t consumed_myself = 0;
1362 int repeat;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001363 enum {
1364 OS__BPC_BIT = 0,
1365 OS__BPC_CHAR = 1,
1366 OS__BPC_U16 = 2,
1367 OS__BPC_U32 = 4
1368 } bpc; /* Bytes per character */
1369 unsigned int unit_bits;
1370 unsigned int canonical_unit_bits;
Lev Walkin59b176e2005-11-26 11:25:14 +00001371
1372 (void)opt_codec_ctx;
1373
Lev Walkin3a4689a2006-11-24 11:20:27 +00001374 if(pc) {
1375 cval = &pc->value;
1376 csiz = &pc->size;
1377 } else {
1378 cval = &asn_DEF_OCTET_STRING_constraints.value;
1379 csiz = &asn_DEF_OCTET_STRING_constraints.size;
1380 }
1381
1382 switch(specs->subvariant) {
1383 default:
1384 case ASN_OSUBV_ANY:
1385 ASN_DEBUG("Unrecognized subvariant %d", specs->subvariant);
1386 RETURN(RC_FAIL);
1387 case ASN_OSUBV_BIT:
1388 canonical_unit_bits = unit_bits = 1;
1389 bpc = OS__BPC_BIT;
1390 break;
1391 case ASN_OSUBV_STR:
1392 canonical_unit_bits = unit_bits = 8;
1393 if(cval->flags & APC_CONSTRAINED)
1394 unit_bits = cval->range_bits;
1395 bpc = OS__BPC_CHAR;
1396 break;
1397 case ASN_OSUBV_U16:
1398 canonical_unit_bits = unit_bits = 16;
1399 if(cval->flags & APC_CONSTRAINED)
1400 unit_bits = cval->range_bits;
1401 bpc = OS__BPC_U16;
1402 break;
1403 case ASN_OSUBV_U32:
1404 canonical_unit_bits = unit_bits = 32;
1405 if(cval->flags & APC_CONSTRAINED)
1406 unit_bits = cval->range_bits;
1407 bpc = OS__BPC_U32;
1408 break;
1409 }
1410
Lev Walkin59b176e2005-11-26 11:25:14 +00001411 /*
1412 * Allocate the string.
1413 */
1414 if(!st) {
1415 st = (BIT_STRING_t *)(*sptr = CALLOC(1, specs->struct_size));
1416 if(!st) RETURN(RC_FAIL);
1417 }
1418
Lev Walkin725883b2006-10-09 12:07:58 +00001419 ASN_DEBUG("PER Decoding %s size %ld .. %ld bits %d",
Lev Walkin3a4689a2006-11-24 11:20:27 +00001420 csiz->flags & APC_EXTENSIBLE ? "extensible" : "non-extensible",
1421 csiz->lower_bound, csiz->upper_bound, csiz->effective_bits);
Lev Walkince676dd2005-12-20 22:34:55 +00001422
Lev Walkin3a4689a2006-11-24 11:20:27 +00001423 if(csiz->flags & APC_EXTENSIBLE) {
Lev Walkin59b176e2005-11-26 11:25:14 +00001424 int inext = per_get_few_bits(pd, 1);
Lev Walkin0a8aa602006-09-18 20:05:55 +00001425 if(inext < 0) RETURN(RC_WMORE);
Lev Walkin3a4689a2006-11-24 11:20:27 +00001426 if(inext) {
1427 csiz = &asn_DEF_OCTET_STRING_constraints.size;
1428 cval = &asn_DEF_OCTET_STRING_constraints.value;
1429 unit_bits = canonical_unit_bits;
1430 }
Lev Walkin59b176e2005-11-26 11:25:14 +00001431 }
1432
Lev Walkin3a4689a2006-11-24 11:20:27 +00001433 if(csiz->effective_bits >= 0) {
Lev Walkin59b176e2005-11-26 11:25:14 +00001434 FREEMEM(st->buf);
Lev Walkin3a4689a2006-11-24 11:20:27 +00001435 if(bpc) {
1436 st->size = csiz->upper_bound * bpc;
Lev Walkin59b176e2005-11-26 11:25:14 +00001437 } else {
Lev Walkin3a4689a2006-11-24 11:20:27 +00001438 st->size = (csiz->upper_bound + 7) >> 3;
Lev Walkin59b176e2005-11-26 11:25:14 +00001439 }
1440 st->buf = (uint8_t *)MALLOC(st->size + 1);
1441 if(!st->buf) { st->size = 0; RETURN(RC_FAIL); }
1442 }
1443
1444 /* X.691, #16.5: zero-length encoding */
1445 /* X.691, #16.6: short fixed length encoding (up to 2 octets) */
1446 /* X.691, #16.7: long fixed length encoding (up to 64K octets) */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001447 if(csiz->effective_bits == 0) {
Lev Walkin725883b2006-10-09 12:07:58 +00001448 int ret;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001449 if(bpc) {
1450 ASN_DEBUG("Encoding OCTET STRING size %ld",
1451 csiz->upper_bound);
1452 ret = OCTET_STRING_per_get_characters(pd, st->buf,
1453 csiz->upper_bound, bpc, unit_bits,
1454 cval->lower_bound, cval->upper_bound, pc);
Lev Walkin725883b2006-10-09 12:07:58 +00001455 if(ret > 0) RETURN(RC_FAIL);
1456 } else {
Lev Walkin3a4689a2006-11-24 11:20:27 +00001457 ASN_DEBUG("Encoding BIT STRING size %ld",
1458 csiz->upper_bound);
Lev Walkin725883b2006-10-09 12:07:58 +00001459 ret = per_get_many_bits(pd, st->buf, 0,
Lev Walkin3a4689a2006-11-24 11:20:27 +00001460 unit_bits * csiz->upper_bound);
Lev Walkin725883b2006-10-09 12:07:58 +00001461 }
Lev Walkin0a8aa602006-09-18 20:05:55 +00001462 if(ret < 0) RETURN(RC_WMORE);
Lev Walkin3a4689a2006-11-24 11:20:27 +00001463 consumed_myself += unit_bits * csiz->upper_bound;
Lev Walkin59b176e2005-11-26 11:25:14 +00001464 st->buf[st->size] = 0;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001465 if(bpc == 0) {
1466 int ubs = (csiz->upper_bound & 0x7);
1467 st->bits_unused = ubs ? 8 - ubs : 0;
1468 }
Lev Walkin59b176e2005-11-26 11:25:14 +00001469 RETURN(RC_OK);
1470 }
1471
1472 st->size = 0;
1473 do {
Lev Walkin3a4689a2006-11-24 11:20:27 +00001474 ssize_t raw_len;
Lev Walkin59b176e2005-11-26 11:25:14 +00001475 ssize_t len_bytes;
1476 ssize_t len_bits;
1477 void *p;
1478 int ret;
1479
1480 /* Get the PER length */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001481 raw_len = uper_get_length(pd, csiz->effective_bits, &repeat);
1482 if(raw_len < 0) RETURN(RC_WMORE);
1483 raw_len += csiz->lower_bound;
Lev Walkin59b176e2005-11-26 11:25:14 +00001484
Lev Walkin0a8aa602006-09-18 20:05:55 +00001485 ASN_DEBUG("Got PER length eb %ld, len %ld, %s (%s)",
Lev Walkin3a4689a2006-11-24 11:20:27 +00001486 (long)csiz->effective_bits, (long)raw_len,
Lev Walkin0a8aa602006-09-18 20:05:55 +00001487 repeat ? "repeat" : "once", td->name);
Lev Walkin3a4689a2006-11-24 11:20:27 +00001488 if(bpc) {
1489 len_bytes = raw_len * bpc;
1490 len_bits = len_bytes * unit_bits;
1491 } else {
1492 len_bits = raw_len;
Lev Walkin59b176e2005-11-26 11:25:14 +00001493 len_bytes = (len_bits + 7) >> 3;
1494 if(len_bits & 0x7)
1495 st->bits_unused = 8 - (len_bits & 0x7);
1496 /* len_bits be multiple of 16K if repeat is set */
Lev Walkin59b176e2005-11-26 11:25:14 +00001497 }
1498 p = REALLOC(st->buf, st->size + len_bytes + 1);
1499 if(!p) RETURN(RC_FAIL);
1500 st->buf = (uint8_t *)p;
1501
Lev Walkin3a4689a2006-11-24 11:20:27 +00001502 if(bpc) {
1503 ret = OCTET_STRING_per_get_characters(pd,
1504 &st->buf[st->size], raw_len, bpc, unit_bits,
1505 cval->lower_bound, cval->upper_bound, pc);
Lev Walkin725883b2006-10-09 12:07:58 +00001506 if(ret > 0) RETURN(RC_FAIL);
1507 } else {
1508 ret = per_get_many_bits(pd, &st->buf[st->size],
1509 0, len_bits);
1510 }
Lev Walkin0a8aa602006-09-18 20:05:55 +00001511 if(ret < 0) RETURN(RC_WMORE);
Lev Walkin59b176e2005-11-26 11:25:14 +00001512 st->size += len_bytes;
1513 } while(repeat);
1514 st->buf[st->size] = 0; /* nul-terminate */
1515
1516 return rval;
1517}
Lev Walkindc06f6b2004-10-20 15:50:55 +00001518
Lev Walkin523de9e2006-08-18 01:34:18 +00001519asn_enc_rval_t
1520OCTET_STRING_encode_uper(asn_TYPE_descriptor_t *td,
Lev Walkin494fb702017-08-07 20:07:00 -07001521 const asn_per_constraints_t *constraints, void *sptr,
1522 asn_per_outp_t *po) {
1523 asn_OCTET_STRING_specifics_t *specs = td->specifics
Lev Walkin523de9e2006-08-18 01:34:18 +00001524 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +08001525 : &asn_SPC_OCTET_STRING_specs;
Lev Walkin494fb702017-08-07 20:07:00 -07001526 const asn_per_constraints_t *pc = constraints ? constraints
Lev Walkin725883b2006-10-09 12:07:58 +00001527 : td->per_constraints;
Lev Walkin494fb702017-08-07 20:07:00 -07001528 const asn_per_constraint_t *cval;
1529 const asn_per_constraint_t *csiz;
Lev Walkin523de9e2006-08-18 01:34:18 +00001530 const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001531 asn_enc_rval_t er = { 0, 0, 0 };
Lev Walkin523de9e2006-08-18 01:34:18 +00001532 int inext = 0; /* Lies not within extension root */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001533 unsigned int unit_bits;
1534 unsigned int canonical_unit_bits;
1535 unsigned int sizeinunits;
Lev Walkin523de9e2006-08-18 01:34:18 +00001536 const uint8_t *buf;
1537 int ret;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001538 enum {
1539 OS__BPC_BIT = 0,
1540 OS__BPC_CHAR = 1,
1541 OS__BPC_U16 = 2,
1542 OS__BPC_U32 = 4
1543 } bpc; /* Bytes per character */
1544 int ct_extensible;
Lev Walkin523de9e2006-08-18 01:34:18 +00001545
Lev Walkin2ddd3082006-10-16 12:32:14 +00001546 if(!st || (!st->buf && st->size))
Lev Walkin7c1dc052016-03-14 03:08:15 -07001547 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001548
Lev Walkin3a4689a2006-11-24 11:20:27 +00001549 if(pc) {
1550 cval = &pc->value;
1551 csiz = &pc->size;
1552 } else {
1553 cval = &asn_DEF_OCTET_STRING_constraints.value;
1554 csiz = &asn_DEF_OCTET_STRING_constraints.size;
1555 }
1556 ct_extensible = csiz->flags & APC_EXTENSIBLE;
1557
1558 switch(specs->subvariant) {
1559 default:
1560 case ASN_OSUBV_ANY:
Lev Walkin7c1dc052016-03-14 03:08:15 -07001561 ASN__ENCODE_FAILED;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001562 case ASN_OSUBV_BIT:
1563 canonical_unit_bits = unit_bits = 1;
1564 bpc = OS__BPC_BIT;
1565 sizeinunits = st->size * 8 - (st->bits_unused & 0x07);
Lev Walkin523de9e2006-08-18 01:34:18 +00001566 ASN_DEBUG("BIT STRING of %d bytes, %d bits unused",
1567 sizeinunits, st->bits_unused);
Lev Walkin3a4689a2006-11-24 11:20:27 +00001568 break;
1569 case ASN_OSUBV_STR:
1570 canonical_unit_bits = unit_bits = 8;
1571 if(cval->flags & APC_CONSTRAINED)
1572 unit_bits = cval->range_bits;
1573 bpc = OS__BPC_CHAR;
1574 sizeinunits = st->size;
1575 break;
1576 case ASN_OSUBV_U16:
1577 canonical_unit_bits = unit_bits = 16;
1578 if(cval->flags & APC_CONSTRAINED)
1579 unit_bits = cval->range_bits;
1580 bpc = OS__BPC_U16;
Lev Walkin4ef01a22017-09-06 22:54:39 -07001581 sizeinunits = st->size >> 1;
1582 if(st->size & 1) {
1583 ASN_DEBUG("%s string size is not modulo 2", td->name);
1584 ASN__ENCODE_FAILED;
1585 }
Lev Walkin3a4689a2006-11-24 11:20:27 +00001586 break;
1587 case ASN_OSUBV_U32:
1588 canonical_unit_bits = unit_bits = 32;
1589 if(cval->flags & APC_CONSTRAINED)
1590 unit_bits = cval->range_bits;
1591 bpc = OS__BPC_U32;
Lev Walkin4ef01a22017-09-06 22:54:39 -07001592 sizeinunits = st->size >> 2;
1593 if(st->size & 3) {
1594 ASN_DEBUG("%s string size is not modulo 4", td->name);
1595 ASN__ENCODE_FAILED;
1596 }
Lev Walkin3a4689a2006-11-24 11:20:27 +00001597 break;
Lev Walkin725883b2006-10-09 12:07:58 +00001598 }
1599
Lev Walkin00918812006-09-18 21:19:32 +00001600 ASN_DEBUG("Encoding %s into %d units of %d bits"
Lev Walkin725883b2006-10-09 12:07:58 +00001601 " (%ld..%ld, effective %d)%s",
Lev Walkin00918812006-09-18 21:19:32 +00001602 td->name, sizeinunits, unit_bits,
Lev Walkin3a4689a2006-11-24 11:20:27 +00001603 csiz->lower_bound, csiz->upper_bound,
1604 csiz->effective_bits, ct_extensible ? " EXT" : "");
Lev Walkin523de9e2006-08-18 01:34:18 +00001605
Lev Walkind4c16732013-03-28 05:00:47 -07001606 /* Figure out whether size lies within PER visible constraint */
Lev Walkin523de9e2006-08-18 01:34:18 +00001607
Lev Walkin3a4689a2006-11-24 11:20:27 +00001608 if(csiz->effective_bits >= 0) {
1609 if((int)sizeinunits < csiz->lower_bound
1610 || (int)sizeinunits > csiz->upper_bound) {
Lev Walkin523de9e2006-08-18 01:34:18 +00001611 if(ct_extensible) {
Lev Walkin3a4689a2006-11-24 11:20:27 +00001612 cval = &asn_DEF_OCTET_STRING_constraints.value;
1613 csiz = &asn_DEF_OCTET_STRING_constraints.size;
1614 unit_bits = canonical_unit_bits;
Lev Walkin523de9e2006-08-18 01:34:18 +00001615 inext = 1;
Lev Walkin4ef01a22017-09-06 22:54:39 -07001616 } else {
Lev Walkin7c1dc052016-03-14 03:08:15 -07001617 ASN__ENCODE_FAILED;
Lev Walkin4ef01a22017-09-06 22:54:39 -07001618 }
Lev Walkin523de9e2006-08-18 01:34:18 +00001619 }
1620 } else {
1621 inext = 0;
1622 }
1623
1624 if(ct_extensible) {
1625 /* Declare whether length is [not] within extension root */
1626 if(per_put_few_bits(po, inext, 1))
Lev Walkin7c1dc052016-03-14 03:08:15 -07001627 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001628 }
1629
1630 /* X.691, #16.5: zero-length encoding */
1631 /* X.691, #16.6: short fixed length encoding (up to 2 octets) */
1632 /* X.691, #16.7: long fixed length encoding (up to 64K octets) */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001633 if(csiz->effective_bits >= 0) {
Lev Walkin24810022017-08-25 12:16:11 -07001634 ASN_DEBUG("Encoding %zu bytes (%ld), length in %d bits",
Lev Walkin3a4689a2006-11-24 11:20:27 +00001635 st->size, sizeinunits - csiz->lower_bound,
1636 csiz->effective_bits);
1637 ret = per_put_few_bits(po, sizeinunits - csiz->lower_bound,
1638 csiz->effective_bits);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001639 if(ret) ASN__ENCODE_FAILED;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001640 if(bpc) {
1641 ret = OCTET_STRING_per_put_characters(po, st->buf,
1642 sizeinunits, bpc, unit_bits,
1643 cval->lower_bound, cval->upper_bound, pc);
Lev Walkin725883b2006-10-09 12:07:58 +00001644 } else {
1645 ret = per_put_many_bits(po, st->buf,
1646 sizeinunits * unit_bits);
1647 }
Lev Walkin7c1dc052016-03-14 03:08:15 -07001648 if(ret) ASN__ENCODE_FAILED;
1649 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +00001650 }
1651
Lev Walkin24810022017-08-25 12:16:11 -07001652 ASN_DEBUG("Encoding %zu bytes", st->size);
Lev Walkin523de9e2006-08-18 01:34:18 +00001653
1654 if(sizeinunits == 0) {
1655 if(uper_put_length(po, 0))
Lev Walkin7c1dc052016-03-14 03:08:15 -07001656 ASN__ENCODE_FAILED;
1657 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +00001658 }
1659
1660 buf = st->buf;
1661 while(sizeinunits) {
1662 ssize_t maySave = uper_put_length(po, sizeinunits);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001663 if(maySave < 0) ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001664
Lev Walkin345e4c62006-10-19 02:44:08 +00001665 ASN_DEBUG("Encoding %ld of %ld",
1666 (long)maySave, (long)sizeinunits);
Lev Walkin523de9e2006-08-18 01:34:18 +00001667
Lev Walkin3a4689a2006-11-24 11:20:27 +00001668 if(bpc) {
1669 ret = OCTET_STRING_per_put_characters(po, buf,
1670 maySave, bpc, unit_bits,
1671 cval->lower_bound, cval->upper_bound, pc);
Lev Walkin725883b2006-10-09 12:07:58 +00001672 } else {
1673 ret = per_put_many_bits(po, buf, maySave * unit_bits);
1674 }
Lev Walkin7c1dc052016-03-14 03:08:15 -07001675 if(ret) ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001676
Lev Walkin3a4689a2006-11-24 11:20:27 +00001677 if(bpc)
1678 buf += maySave * bpc;
Lev Walkin523de9e2006-08-18 01:34:18 +00001679 else
Lev Walkin3a4689a2006-11-24 11:20:27 +00001680 buf += maySave >> 3;
Lev Walkin523de9e2006-08-18 01:34:18 +00001681 sizeinunits -= maySave;
1682 assert(!(maySave & 0x07) || !sizeinunits);
1683 }
1684
Lev Walkin7c1dc052016-03-14 03:08:15 -07001685 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +00001686}
1687
Lev Walkin62d76872017-08-05 22:49:42 -07001688#endif /* ASN_DISABLE_PER_SUPPORT */
1689
Lev Walkinf15320b2004-06-03 03:38:44 +00001690int
Lev Walkinde4825d2004-09-29 13:20:14 +00001691OCTET_STRING_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +00001692 asn_app_consume_bytes_f *cb, void *app_key) {
Wim Lewis59e8d282014-08-04 12:39:35 -07001693 const char * const h2c = "0123456789ABCDEF";
Lev Walkinc2346572004-08-11 09:07:36 +00001694 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +00001695 char scratch[16 * 3 + 4];
1696 char *p = scratch;
1697 uint8_t *buf;
1698 uint8_t *end;
1699 size_t i;
Lev Walkinf15320b2004-06-03 03:38:44 +00001700
Lev Walkind9bd7752004-06-05 08:17:50 +00001701 (void)td; /* Unused argument */
1702
Lev Walkin2ddd3082006-10-16 12:32:14 +00001703 if(!st || (!st->buf && st->size))
1704 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001705
1706 /*
1707 * Dump the contents of the buffer in hexadecimal.
1708 */
1709 buf = st->buf;
1710 end = buf + st->size;
1711 for(i = 0; buf < end; buf++, i++) {
1712 if(!(i % 16) && (i || st->size > 16)) {
Lev Walkin8e8078a2004-09-26 13:10:40 +00001713 if(cb(scratch, p - scratch, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +00001714 return -1;
Lev Walkin8e8078a2004-09-26 13:10:40 +00001715 _i_INDENT(1);
Lev Walkinf15320b2004-06-03 03:38:44 +00001716 p = scratch;
1717 }
1718 *p++ = h2c[(*buf >> 4) & 0x0F];
1719 *p++ = h2c[*buf & 0x0F];
Lev Walkina9cc46e2004-09-22 16:06:28 +00001720 *p++ = 0x20;
Lev Walkinf15320b2004-06-03 03:38:44 +00001721 }
1722
Lev Walkincc6a9102004-09-23 22:06:26 +00001723 if(p > scratch) {
1724 p--; /* Remove the tail space */
Lev Walkin8e8078a2004-09-26 13:10:40 +00001725 if(cb(scratch, p - scratch, app_key) < 0)
Lev Walkincc6a9102004-09-23 22:06:26 +00001726 return -1;
1727 }
1728
1729 return 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001730}
1731
1732int
Lev Walkindc06f6b2004-10-20 15:50:55 +00001733OCTET_STRING_print_utf8(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +00001734 int ilevel, asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +00001735 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +00001736
Lev Walkind9bd7752004-06-05 08:17:50 +00001737 (void)td; /* Unused argument */
1738 (void)ilevel; /* Unused argument */
1739
Lev Walkin2ddd3082006-10-16 12:32:14 +00001740 if(st && (st->buf || !st->size)) {
Lev Walkin8e8078a2004-09-26 13:10:40 +00001741 return (cb(st->buf, st->size, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001742 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +00001743 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001744 }
1745}
1746
1747void
Lev Walkinf6853ce2017-08-11 00:50:27 -07001748OCTET_STRING_free(const asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkin8d99d7b2017-08-25 01:06:00 -07001749 enum asn_struct_free_method method) {
1750 OCTET_STRING_t *st = (OCTET_STRING_t *)sptr;
Lev Walkin034ee192016-03-14 02:28:33 -07001751 asn_OCTET_STRING_specifics_t *specs;
1752 asn_struct_ctx_t *ctx;
Lev Walkinb54577a2004-11-08 10:47:12 +00001753 struct _stack *stck;
Lev Walkinf15320b2004-06-03 03:38:44 +00001754
1755 if(!td || !st)
1756 return;
1757
Lev Walkin034ee192016-03-14 02:28:33 -07001758 specs = td->specifics
1759 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +08001760 : &asn_SPC_OCTET_STRING_specs;
Lev Walkin034ee192016-03-14 02:28:33 -07001761 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
1762
Lev Walkinf15320b2004-06-03 03:38:44 +00001763 ASN_DEBUG("Freeing %s as OCTET STRING", td->name);
1764
1765 if(st->buf) {
1766 FREEMEM(st->buf);
Lev Walkin2ddd3082006-10-16 12:32:14 +00001767 st->buf = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001768 }
1769
1770 /*
1771 * Remove decode-time stack.
1772 */
Lev Walkinb54577a2004-11-08 10:47:12 +00001773 stck = (struct _stack *)ctx->ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +00001774 if(stck) {
1775 while(stck->tail) {
1776 struct _stack_el *sel = stck->tail;
1777 stck->tail = sel->prev;
1778 FREEMEM(sel);
1779 }
1780 FREEMEM(stck);
1781 }
1782
Lev Walkin8d99d7b2017-08-25 01:06:00 -07001783 switch(method) {
1784 case ASFM_FREE_EVERYTHING:
1785 FREEMEM(sptr);
1786 break;
1787 case ASFM_FREE_UNDERLYING:
1788 break;
1789 case ASFM_FREE_UNDERLYING_AND_RESET:
1790 memset(sptr, 0,
1791 ((asn_OCTET_STRING_specifics_t *)(td->specifics))->struct_size);
1792 break;
1793 }
Lev Walkinf15320b2004-06-03 03:38:44 +00001794}
1795
1796/*
1797 * Conversion routines.
1798 */
1799int
1800OCTET_STRING_fromBuf(OCTET_STRING_t *st, const char *str, int len) {
1801 void *buf;
1802
1803 if(st == 0 || (str == 0 && len)) {
1804 errno = EINVAL;
1805 return -1;
1806 }
1807
1808 /*
1809 * Clear the OCTET STRING.
1810 */
1811 if(str == NULL) {
Lev Walkin7b284812005-12-17 11:43:25 +00001812 FREEMEM(st->buf);
1813 st->buf = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001814 st->size = 0;
1815 return 0;
1816 }
1817
1818 /* Determine the original string size, if not explicitly given */
1819 if(len < 0)
1820 len = strlen(str);
1821
1822 /* Allocate and fill the memory */
1823 buf = MALLOC(len + 1);
Lev Walkin7b284812005-12-17 11:43:25 +00001824 if(buf == NULL)
Lev Walkinf15320b2004-06-03 03:38:44 +00001825 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +00001826
1827 memcpy(buf, str, len);
Lev Walkin7b284812005-12-17 11:43:25 +00001828 ((uint8_t *)buf)[len] = '\0'; /* Couldn't use memcpy(len+1)! */
1829 FREEMEM(st->buf);
1830 st->buf = (uint8_t *)buf;
1831 st->size = len;
Lev Walkinf15320b2004-06-03 03:38:44 +00001832
1833 return 0;
1834}
1835
1836OCTET_STRING_t *
Lev Walkinbbd93252004-10-12 05:57:23 +00001837OCTET_STRING_new_fromBuf(asn_TYPE_descriptor_t *td, const char *str, int len) {
1838 asn_OCTET_STRING_specifics_t *specs = td->specifics
Lev Walkindc06f6b2004-10-20 15:50:55 +00001839 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +08001840 : &asn_SPC_OCTET_STRING_specs;
Lev Walkinf15320b2004-06-03 03:38:44 +00001841 OCTET_STRING_t *st;
1842
Lev Walkinbbd93252004-10-12 05:57:23 +00001843 st = (OCTET_STRING_t *)CALLOC(1, specs->struct_size);
Lev Walkinf15320b2004-06-03 03:38:44 +00001844 if(st && str && OCTET_STRING_fromBuf(st, str, len)) {
Lev Walkin419f6752006-09-13 04:02:00 +00001845 FREEMEM(st);
Lev Walkinf15320b2004-06-03 03:38:44 +00001846 st = NULL;
1847 }
1848
1849 return st;
1850}
1851
Lev Walkincd2f48e2017-08-10 02:14:59 -07001852/*
1853 * Lexicographically compare the common prefix of both strings,
1854 * and if it is the same return -1 for the smallest string.
1855 */
1856int
1857OCTET_STRING_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
1858 const void *bptr) {
1859 const OCTET_STRING_t *a = aptr;
1860 const OCTET_STRING_t *b = bptr;
1861
1862 (void)td;
1863
1864 if(a && b) {
1865 size_t common_prefix_size = a->size <= b->size ? a->size : b->size;
1866 int ret = memcmp(a->buf, b->buf, common_prefix_size);
1867 if(ret == 0) {
1868 /* Figure out which string with equal prefixes is longer. */
1869 if(a->size < b->size) {
1870 return -1;
1871 } else if(a->size > b->size) {
1872 return 1;
1873 } else {
Lev Walkin86526e32017-09-06 23:16:13 -07001874 asn_OCTET_STRING_specifics_t *specs = td->specifics;
1875 if(specs && specs->subvariant == ASN_OSUBV_BIT) {
1876 const BIT_STRING_t *ba = aptr;
1877 const BIT_STRING_t *bb = bptr;
1878 if(ba->bits_unused > bb->bits_unused) {
1879 return -1;
1880 } else if(ba->bits_unused < bb->bits_unused) {
1881 return 1;
1882 } else {
1883 return 0;
1884 }
1885 }
Lev Walkincd2f48e2017-08-10 02:14:59 -07001886 return 0;
1887 }
1888 } else {
1889 return ret;
1890 }
1891 } else if(!a && !b) {
1892 return 0;
1893 } else if(!a) {
1894 return -1;
1895 } else {
1896 return 1;
1897 }
1898
1899}
1900