blob: 39462062630c31b4ec05082da09163f637196bcd [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001/*-
Lev Walkina5b02882017-10-01 22:48:44 -07002 * Copyright (c) 2003-2017 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 */
Lev Walkina5972be2017-09-29 23:15:58 -070045 OCTET_STRING_random_fill,
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080046 0 /* Use generic outmost tag fetcher */
47};
48asn_TYPE_descriptor_t asn_DEF_OCTET_STRING = {
49 "OCTET STRING", /* Canonical name */
50 "OCTET_STRING", /* XML tag name */
51 &asn_OP_OCTET_STRING,
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 Walkina5972be2017-09-29 23:15:58 -070058 { 0, 0, asn_generic_no_constraint },
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 Walkin494fb702017-08-07 20:07:00 -0700126 ber_tlv_len_t left; /* What's left to read (or -1) */
127 ber_tlv_len_t got; /* What was actually processed */
Lev Walkin24bf3f62017-08-10 17:58:43 -0700128 unsigned cont_level; /* Depth of subcontainment */
Lev Walkin494fb702017-08-07 20:07:00 -0700129 int want_nulls; /* Want null "end of content" octets? */
130 int bits_chopped; /* Flag in BIT STRING mode */
131 ber_tlv_tag_t tag; /* For debugging purposes */
132 struct _stack_el *prev;
133 struct _stack_el *next;
Lev Walkinf15320b2004-06-03 03:38:44 +0000134};
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 Walkinafbf2a92017-09-12 23:30:27 -0700180OCTET_STRING_decode_ber(const asn_codec_ctx_t *opt_codec_ctx,
Lev Walkin20696a42017-10-17 21:27:33 -0700181 const asn_TYPE_descriptor_t *td, void **sptr,
182 const void *buf_ptr, size_t size, int tag_mode) {
183 const asn_OCTET_STRING_specifics_t *specs = td->specifics
Lev Walkind84f6032017-10-03 16:33:59 -0700184 ? (const 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 Walkind84f6032017-10-03 16:33:59 -0700261 ASN_DEBUG("%p, s->l=%ld, s->wn=%ld, s->g=%ld\n", (void *)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) {
Lev Walkin24bf3f62017-08-10 17:58:43 -0700346 unsigned level = sel->cont_level;
Lev Walkin188ed2c2004-09-13 08:31:01 +0000347 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 Walkin24bf3f62017-08-10 17:58:43 -0700409 ASN_DEBUG("+EXPECT2 got=%ld left=%ld, wn=%d, clvl=%u",
Lev Walkinabf68892004-10-26 10:12:14 +0000410 (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",
Lev Walkind84f6032017-10-03 16:33:59 -0700498 (void *)sel->prev, sel->want_nulls,
Lev Walkin5c915992004-09-27 20:54:06 +0000499 (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 Walkin0368c622017-09-18 00:33:16 -0700508 if(type_variant == ASN_OSUBV_BIT) {
509 if(st->size) {
510 if(st->bits_unused < 0 || st->bits_unused > 7) {
511 RETURN(RC_FAIL);
512 }
513 /* Finalize BIT STRING: zero out unused bits. */
514 st->buf[st->size-1] &= 0xff << st->bits_unused;
515 } else {
516 if(st->bits_unused) {
517 RETURN(RC_FAIL);
518 }
519 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000520 }
521
Lev Walkinabf68892004-10-26 10:12:14 +0000522 ASN_DEBUG("Took %ld bytes to encode %s: [%s]:%ld",
523 (long)consumed_myself, td->name,
Lev Walkin3a4689a2006-11-24 11:20:27 +0000524 (type_variant == ASN_OSUBV_STR) ? (char *)st->buf : "<data>",
Lev Walkinabf68892004-10-26 10:12:14 +0000525 (long)st->size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000526
Lev Walkinf15320b2004-06-03 03:38:44 +0000527
Lev Walkin5c915992004-09-27 20:54:06 +0000528 RETURN(RC_OK);
Lev Walkinf15320b2004-06-03 03:38:44 +0000529}
530
531/*
532 * Encode OCTET STRING type using DER.
533 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000534asn_enc_rval_t
Lev Walkin20696a42017-10-17 21:27:33 -0700535OCTET_STRING_encode_der(const asn_TYPE_descriptor_t *td, const void *sptr,
536 int tag_mode, ber_tlv_tag_t tag,
537 asn_app_consume_bytes_f *cb, void *app_key) {
538 asn_enc_rval_t er;
Lev Walkind84f6032017-10-03 16:33:59 -0700539 const asn_OCTET_STRING_specifics_t *specs = td->specifics
540 ? (const asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +0800541 : &asn_SPC_OCTET_STRING_specs;
Lev Walkin20696a42017-10-17 21:27:33 -0700542 const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
Lev Walkin3a4689a2006-11-24 11:20:27 +0000543 enum asn_OS_Subvariant type_variant = specs->subvariant;
Lev Walkinbbd93252004-10-12 05:57:23 +0000544 int fix_last_byte = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000545
546 ASN_DEBUG("%s %s as OCTET STRING",
Lev Walkin1f670c12004-09-02 12:57:25 +0000547 cb?"Estimating":"Encoding", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000548
549 /*
Lev Walkinbbd93252004-10-12 05:57:23 +0000550 * Write tags.
Lev Walkinf15320b2004-06-03 03:38:44 +0000551 */
Lev Walkin3a4689a2006-11-24 11:20:27 +0000552 if(type_variant != ASN_OSUBV_ANY || tag_mode == 1) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000553 er.encoded = der_write_tags(td,
Lev Walkin3a4689a2006-11-24 11:20:27 +0000554 (type_variant == ASN_OSUBV_BIT) + st->size,
555 tag_mode, type_variant == ASN_OSUBV_ANY, tag,
556 cb, app_key);
Lev Walkinbbd93252004-10-12 05:57:23 +0000557 if(er.encoded == -1) {
558 er.failed_type = td;
559 er.structure_ptr = sptr;
560 return er;
Lev Walkinf15320b2004-06-03 03:38:44 +0000561 }
Lev Walkinbbd93252004-10-12 05:57:23 +0000562 } else {
563 /* Disallow: [<tag>] IMPLICIT ANY */
Lev Walkin3a4689a2006-11-24 11:20:27 +0000564 assert(type_variant != ASN_OSUBV_ANY || tag_mode != -1);
Lev Walkinbbd93252004-10-12 05:57:23 +0000565 er.encoded = 0;
566 }
567
568 if(!cb) {
Lev Walkin3a4689a2006-11-24 11:20:27 +0000569 er.encoded += (type_variant == ASN_OSUBV_BIT) + st->size;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700570 ASN__ENCODED_OK(er);
Lev Walkinf15320b2004-06-03 03:38:44 +0000571 }
572
Lev Walkin07f388c2004-10-11 11:43:08 +0000573 /*
Lev Walkinbbd93252004-10-12 05:57:23 +0000574 * Prepare to deal with the last octet of BIT STRING.
Lev Walkin07f388c2004-10-11 11:43:08 +0000575 */
Lev Walkin3a4689a2006-11-24 11:20:27 +0000576 if(type_variant == ASN_OSUBV_BIT) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000577 uint8_t b = st->bits_unused & 0x07;
578 if(b && st->size) fix_last_byte = 1;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700579 ASN__CALLBACK(&b, 1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000580 }
581
Lev Walkinbbd93252004-10-12 05:57:23 +0000582 /* Invoke callback for the main part of the buffer */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700583 ASN__CALLBACK(st->buf, st->size - fix_last_byte);
Lev Walkinf15320b2004-06-03 03:38:44 +0000584
Lev Walkinbbd93252004-10-12 05:57:23 +0000585 /* The last octet should be stripped off the unused bits */
586 if(fix_last_byte) {
587 uint8_t b = st->buf[st->size-1] & (0xff << st->bits_unused);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700588 ASN__CALLBACK(&b, 1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000589 }
590
Lev Walkin7c1dc052016-03-14 03:08:15 -0700591 ASN__ENCODED_OK(er);
Lev Walkinbbd93252004-10-12 05:57:23 +0000592cb_failed:
Lev Walkin7c1dc052016-03-14 03:08:15 -0700593 ASN__ENCODE_FAILED;
Lev Walkinf15320b2004-06-03 03:38:44 +0000594}
595
Lev Walkina9cc46e2004-09-22 16:06:28 +0000596asn_enc_rval_t
Lev Walkin20696a42017-10-17 21:27:33 -0700597OCTET_STRING_encode_xer(const asn_TYPE_descriptor_t *td, const void *sptr,
598 int ilevel, enum xer_encoder_flags_e flags,
599 asn_app_consume_bytes_f *cb, void *app_key) {
600 const char * const h2c = "0123456789ABCDEF";
Lev Walkina9cc46e2004-09-22 16:06:28 +0000601 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
602 asn_enc_rval_t er;
603 char scratch[16 * 3 + 4];
604 char *p = scratch;
605 uint8_t *buf;
606 uint8_t *end;
607 size_t i;
608
Lev Walkin2ddd3082006-10-16 12:32:14 +0000609 if(!st || (!st->buf && st->size))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700610 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000611
612 er.encoded = 0;
613
614 /*
615 * Dump the contents of the buffer in hexadecimal.
616 */
617 buf = st->buf;
618 end = buf + st->size;
619 if(flags & XER_F_CANONICAL) {
620 char *scend = scratch + (sizeof(scratch) - 2);
621 for(; buf < end; buf++) {
622 if(p >= scend) {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700623 ASN__CALLBACK(scratch, p - scratch);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000624 p = scratch;
625 }
626 *p++ = h2c[(*buf >> 4) & 0x0F];
627 *p++ = h2c[*buf & 0x0F];
628 }
Lev Walkincc6a9102004-09-23 22:06:26 +0000629
Lev Walkin7c1dc052016-03-14 03:08:15 -0700630 ASN__CALLBACK(scratch, p-scratch); /* Dump the rest */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000631 } else {
632 for(i = 0; buf < end; buf++, i++) {
633 if(!(i % 16) && (i || st->size > 16)) {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700634 ASN__CALLBACK(scratch, p-scratch);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000635 p = scratch;
Lev Walkinc6cac8e2016-03-14 02:57:07 -0700636 ASN__TEXT_INDENT(1, ilevel);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000637 }
638 *p++ = h2c[(*buf >> 4) & 0x0F];
639 *p++ = h2c[*buf & 0x0F];
640 *p++ = 0x20;
641 }
Lev Walkincc6a9102004-09-23 22:06:26 +0000642 if(p - scratch) {
643 p--; /* Remove the tail space */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700644 ASN__CALLBACK(scratch, p-scratch); /* Dump the rest */
Lev Walkincc6a9102004-09-23 22:06:26 +0000645 if(st->size > 16)
Lev Walkinc6cac8e2016-03-14 02:57:07 -0700646 ASN__TEXT_INDENT(1, ilevel-1);
Lev Walkincc6a9102004-09-23 22:06:26 +0000647 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000648 }
649
Lev Walkin7c1dc052016-03-14 03:08:15 -0700650 ASN__ENCODED_OK(er);
Lev Walkin942fd082004-10-03 09:13:02 +0000651cb_failed:
Lev Walkin7c1dc052016-03-14 03:08:15 -0700652 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000653}
654
Wim Lewis18c2ec92014-07-29 11:30:10 -0700655static const struct OCTET_STRING__xer_escape_table_s {
656 const char *string;
Lev Walkin9ca81892004-10-03 10:54:25 +0000657 int size;
658} OCTET_STRING__xer_escape_table[] = {
659#define OSXET(s) { s, sizeof(s) - 1 }
660 OSXET("\074\156\165\154\057\076"), /* <nul/> */
661 OSXET("\074\163\157\150\057\076"), /* <soh/> */
662 OSXET("\074\163\164\170\057\076"), /* <stx/> */
663 OSXET("\074\145\164\170\057\076"), /* <etx/> */
664 OSXET("\074\145\157\164\057\076"), /* <eot/> */
665 OSXET("\074\145\156\161\057\076"), /* <enq/> */
666 OSXET("\074\141\143\153\057\076"), /* <ack/> */
667 OSXET("\074\142\145\154\057\076"), /* <bel/> */
668 OSXET("\074\142\163\057\076"), /* <bs/> */
669 OSXET("\011"), /* \t */
670 OSXET("\012"), /* \n */
671 OSXET("\074\166\164\057\076"), /* <vt/> */
672 OSXET("\074\146\146\057\076"), /* <ff/> */
673 OSXET("\015"), /* \r */
674 OSXET("\074\163\157\057\076"), /* <so/> */
675 OSXET("\074\163\151\057\076"), /* <si/> */
676 OSXET("\074\144\154\145\057\076"), /* <dle/> */
677 OSXET("\074\144\143\061\057\076"), /* <de1/> */
678 OSXET("\074\144\143\062\057\076"), /* <de2/> */
679 OSXET("\074\144\143\063\057\076"), /* <de3/> */
680 OSXET("\074\144\143\064\057\076"), /* <de4/> */
681 OSXET("\074\156\141\153\057\076"), /* <nak/> */
682 OSXET("\074\163\171\156\057\076"), /* <syn/> */
683 OSXET("\074\145\164\142\057\076"), /* <etb/> */
684 OSXET("\074\143\141\156\057\076"), /* <can/> */
685 OSXET("\074\145\155\057\076"), /* <em/> */
686 OSXET("\074\163\165\142\057\076"), /* <sub/> */
687 OSXET("\074\145\163\143\057\076"), /* <esc/> */
688 OSXET("\074\151\163\064\057\076"), /* <is4/> */
689 OSXET("\074\151\163\063\057\076"), /* <is3/> */
690 OSXET("\074\151\163\062\057\076"), /* <is2/> */
691 OSXET("\074\151\163\061\057\076"), /* <is1/> */
692 { 0, 0 }, /* " " */
693 { 0, 0 }, /* ! */
694 { 0, 0 }, /* \" */
695 { 0, 0 }, /* # */
696 { 0, 0 }, /* $ */
697 { 0, 0 }, /* % */
698 OSXET("\046\141\155\160\073"), /* &amp; */
699 { 0, 0 }, /* ' */
700 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, /* ()*+,-./ */
701 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, /* 01234567 */
702 {0,0},{0,0},{0,0},{0,0}, /* 89:; */
703 OSXET("\046\154\164\073"), /* &lt; */
704 { 0, 0 }, /* = */
705 OSXET("\046\147\164\073"), /* &gt; */
706};
707
Lev Walkindc06f6b2004-10-20 15:50:55 +0000708static int
Lev Walkin0fab1a62005-03-09 22:19:25 +0000709OS__check_escaped_control_char(const void *buf, int size) {
Lev Walkindc06f6b2004-10-20 15:50:55 +0000710 size_t i;
711 /*
712 * Inefficient algorithm which translates the escape sequences
713 * defined above into characters. Returns -1 if not found.
714 * TODO: replace by a faster algorithm (bsearch(), hash or
715 * nested table lookups).
716 */
717 for(i = 0; i < 32 /* Don't spend time on the bottom half */; i++) {
Wim Lewis18c2ec92014-07-29 11:30:10 -0700718 const struct OCTET_STRING__xer_escape_table_s *el;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000719 el = &OCTET_STRING__xer_escape_table[i];
720 if(el->size == size && memcmp(buf, el->string, size) == 0)
721 return i;
722 }
723 return -1;
724}
725
726static int
Lev Walkin0fab1a62005-03-09 22:19:25 +0000727OCTET_STRING__handle_control_chars(void *struct_ptr, const void *chunk_buf, size_t chunk_size) {
Lev Walkindc06f6b2004-10-20 15:50:55 +0000728 /*
729 * This might be one of the escape sequences
730 * for control characters. Check it out.
731 * #11.15.5
732 */
733 int control_char = OS__check_escaped_control_char(chunk_buf,chunk_size);
734 if(control_char >= 0) {
735 OCTET_STRING_t *st = (OCTET_STRING_t *)struct_ptr;
736 void *p = REALLOC(st->buf, st->size + 2);
737 if(p) {
738 st->buf = (uint8_t *)p;
739 st->buf[st->size++] = control_char;
740 st->buf[st->size] = '\0'; /* nul-termination */
741 return 0;
742 }
743 }
744
745 return -1; /* No, it's not */
746}
747
Lev Walkina9cc46e2004-09-22 16:06:28 +0000748asn_enc_rval_t
Lev Walkin20696a42017-10-17 21:27:33 -0700749OCTET_STRING_encode_xer_utf8(const asn_TYPE_descriptor_t *td, const void *sptr,
750 int ilevel, enum xer_encoder_flags_e flags,
751 asn_app_consume_bytes_f *cb, void *app_key) {
752 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000753 asn_enc_rval_t er;
Lev Walkin9ca81892004-10-03 10:54:25 +0000754 uint8_t *buf, *end;
755 uint8_t *ss; /* Sequence start */
756 ssize_t encoded_len = 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000757
758 (void)ilevel; /* Unused argument */
759 (void)flags; /* Unused argument */
760
Lev Walkin2ddd3082006-10-16 12:32:14 +0000761 if(!st || (!st->buf && st->size))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700762 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000763
Lev Walkin9ca81892004-10-03 10:54:25 +0000764 buf = st->buf;
765 end = buf + st->size;
766 for(ss = buf; buf < end; buf++) {
Lev Walkin443d2512004-10-05 06:35:31 +0000767 unsigned int ch = *buf;
Lev Walkin9ca81892004-10-03 10:54:25 +0000768 int s_len; /* Special encoding sequence length */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000769
Lev Walkin9ca81892004-10-03 10:54:25 +0000770 /*
771 * Escape certain characters: X.680/11.15
772 */
773 if(ch < sizeof(OCTET_STRING__xer_escape_table)
774 /sizeof(OCTET_STRING__xer_escape_table[0])
775 && (s_len = OCTET_STRING__xer_escape_table[ch].size)) {
776 if(((buf - ss) && cb(ss, buf - ss, app_key) < 0)
777 || cb(OCTET_STRING__xer_escape_table[ch].string, s_len,
778 app_key) < 0)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700779 ASN__ENCODE_FAILED;
Lev Walkin9ca81892004-10-03 10:54:25 +0000780 encoded_len += (buf - ss) + s_len;
781 ss = buf + 1;
782 }
783 }
784
785 encoded_len += (buf - ss);
786 if((buf - ss) && cb(ss, buf - ss, app_key) < 0)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700787 ASN__ENCODE_FAILED;
Lev Walkin9ca81892004-10-03 10:54:25 +0000788
789 er.encoded = encoded_len;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700790 ASN__ENCODED_OK(er);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000791}
792
Lev Walkindc06f6b2004-10-20 15:50:55 +0000793/*
794 * Convert from hexadecimal format (cstring): "AB CD EF"
795 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000796static 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 +0000797 OCTET_STRING_t *st = (OCTET_STRING_t *)sptr;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000798 const char *chunk_stop = (const char *)chunk_buf;
799 const char *p = chunk_stop;
800 const char *pend = p + chunk_size;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000801 unsigned int clv = 0;
802 int half = 0; /* Half bit */
803 uint8_t *buf;
804
805 /* Reallocate buffer according to high cap estimation */
Lev Walkin494fb702017-08-07 20:07:00 -0700806 size_t new_size = st->size + (chunk_size + 1) / 2;
807 void *nptr = REALLOC(st->buf, new_size + 1);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000808 if(!nptr) return -1;
809 st->buf = (uint8_t *)nptr;
810 buf = st->buf + st->size;
811
812 /*
813 * If something like " a b c " appears here, the " a b":3 will be
814 * converted, and the rest skipped. That is, unless buf_size is greater
815 * than chunk_size, then it'll be equivalent to "ABC0".
816 */
817 for(; p < pend; p++) {
Lev Walkin0fab1a62005-03-09 22:19:25 +0000818 int ch = *(const unsigned char *)p;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000819 switch(ch) {
820 case 0x09: case 0x0a: case 0x0c: case 0x0d:
821 case 0x20:
822 /* Ignore whitespace */
823 continue;
824 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: /*01234*/
825 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: /*56789*/
826 clv = (clv << 4) + (ch - 0x30);
827 break;
828 case 0x41: case 0x42: case 0x43: /* ABC */
829 case 0x44: case 0x45: case 0x46: /* DEF */
Lev Walkin3ae21bd2005-02-21 14:43:48 +0000830 clv = (clv << 4) + (ch - 0x41 + 10);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000831 break;
832 case 0x61: case 0x62: case 0x63: /* abc */
833 case 0x64: case 0x65: case 0x66: /* def */
Lev Walkin3ae21bd2005-02-21 14:43:48 +0000834 clv = (clv << 4) + (ch - 0x61 + 10);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000835 break;
836 default:
837 *buf = 0; /* JIC */
838 return -1;
839 }
840 if(half++) {
841 half = 0;
842 *buf++ = clv;
843 chunk_stop = p + 1;
844 }
845 }
846
847 /*
848 * Check partial decoding.
849 */
850 if(half) {
851 if(have_more) {
852 /*
853 * Partial specification is fine,
854 * because no more more PXER_TEXT data is available.
855 */
856 *buf++ = clv << 4;
857 chunk_stop = p;
858 }
859 } else {
860 chunk_stop = p;
861 }
862
863 st->size = buf - st->buf; /* Adjust the buffer size */
Lev Walkin494fb702017-08-07 20:07:00 -0700864 assert(st->size <= new_size);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000865 st->buf[st->size] = 0; /* Courtesy termination */
866
Lev Walkin0fab1a62005-03-09 22:19:25 +0000867 return (chunk_stop - (const char *)chunk_buf); /* Converted size */
Lev Walkindc06f6b2004-10-20 15:50:55 +0000868}
869
870/*
871 * Convert from binary format: "00101011101"
872 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000873static 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 +0000874 BIT_STRING_t *st = (BIT_STRING_t *)sptr;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000875 const char *p = (const char *)chunk_buf;
876 const char *pend = p + chunk_size;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000877 int bits_unused = st->bits_unused & 0x7;
878 uint8_t *buf;
879
880 /* Reallocate buffer according to high cap estimation */
Lev Walkin494fb702017-08-07 20:07:00 -0700881 size_t new_size = st->size + (chunk_size + 7) / 8;
882 void *nptr = REALLOC(st->buf, new_size + 1);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000883 if(!nptr) return -1;
884 st->buf = (uint8_t *)nptr;
885 buf = st->buf + st->size;
886
887 (void)have_more;
888
889 if(bits_unused == 0)
890 bits_unused = 8;
891 else if(st->size)
892 buf--;
893
894 /*
895 * Convert series of 0 and 1 into the octet string.
896 */
897 for(; p < pend; p++) {
Lev Walkin0fab1a62005-03-09 22:19:25 +0000898 int ch = *(const unsigned char *)p;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000899 switch(ch) {
900 case 0x09: case 0x0a: case 0x0c: case 0x0d:
901 case 0x20:
902 /* Ignore whitespace */
903 break;
904 case 0x30:
905 case 0x31:
906 if(bits_unused-- <= 0) {
907 *++buf = 0; /* Clean the cell */
908 bits_unused = 7;
909 }
910 *buf |= (ch&1) << bits_unused;
911 break;
912 default:
913 st->bits_unused = bits_unused;
914 return -1;
915 }
916 }
917
918 if(bits_unused == 8) {
919 st->size = buf - st->buf;
920 st->bits_unused = 0;
921 } else {
922 st->size = buf - st->buf + 1;
923 st->bits_unused = bits_unused;
924 }
925
Lev Walkin494fb702017-08-07 20:07:00 -0700926 assert(st->size <= new_size);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000927 st->buf[st->size] = 0; /* Courtesy termination */
928
929 return chunk_size; /* Converted in full */
930}
931
932/*
933 * Something like strtod(), but with stricter rules.
934 */
935static int
Lev Walkin0fab1a62005-03-09 22:19:25 +0000936OS__strtoent(int base, const char *buf, const char *end, int32_t *ret_value) {
Lev Walkinbeedbde2017-09-17 22:54:51 -0700937 const int32_t last_unicode_codepoint = 0x10ffff;
Lev Walkin33700162004-10-26 09:03:31 +0000938 int32_t val = 0;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000939 const char *p;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000940
941 for(p = buf; p < end; p++) {
942 int ch = *p;
Lev Walkinb0f3db62005-07-03 05:30:15 +0000943
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 }
Lev Walkinbeedbde2017-09-17 22:54:51 -0700963
964 /* Value exceeds the Unicode range. */
965 if(val > last_unicode_codepoint) {
966 return -1;
967 }
Lev Walkindc06f6b2004-10-20 15:50:55 +0000968 }
969
Lev Walkinb0f3db62005-07-03 05:30:15 +0000970 *ret_value = -1;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000971 return (p - buf);
972}
973
974/*
975 * Convert from the plain UTF-8 format, expanding entity references: "2 &lt; 3"
976 */
Lev Walkin20696a42017-10-17 21:27:33 -0700977static ssize_t
978OCTET_STRING__convert_entrefs(void *sptr, const void *chunk_buf,
979 size_t chunk_size, int have_more) {
980 OCTET_STRING_t *st = (OCTET_STRING_t *)sptr;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000981 const char *p = (const char *)chunk_buf;
982 const char *pend = p + chunk_size;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000983 uint8_t *buf;
984
985 /* Reallocate buffer */
Lev Walkin494fb702017-08-07 20:07:00 -0700986 size_t new_size = st->size + chunk_size;
987 void *nptr = REALLOC(st->buf, new_size + 1);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000988 if(!nptr) return -1;
989 st->buf = (uint8_t *)nptr;
990 buf = st->buf + st->size;
991
992 /*
993 * Convert series of 0 and 1 into the octet string.
994 */
995 for(; p < pend; p++) {
Lev Walkin0fab1a62005-03-09 22:19:25 +0000996 int ch = *(const unsigned char *)p;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000997 int len; /* Length of the rest of the chunk */
998
999 if(ch != 0x26 /* '&' */) {
1000 *buf++ = ch;
1001 continue; /* That was easy... */
1002 }
1003
1004 /*
1005 * Process entity reference.
1006 */
Lev Walkin0fab1a62005-03-09 22:19:25 +00001007 len = chunk_size - (p - (const char *)chunk_buf);
Lev Walkindc06f6b2004-10-20 15:50:55 +00001008 if(len == 1 /* "&" */) goto want_more;
1009 if(p[1] == 0x23 /* '#' */) {
Lev Walkin0fab1a62005-03-09 22:19:25 +00001010 const char *pval; /* Pointer to start of digits */
Lev Walkinbc0dce32006-07-27 09:56:13 +00001011 int32_t val = 0; /* Entity reference value */
Lev Walkindc06f6b2004-10-20 15:50:55 +00001012 int base;
1013
1014 if(len == 2 /* "&#" */) goto want_more;
1015 if(p[2] == 0x78 /* 'x' */)
1016 pval = p + 3, base = 16;
1017 else
1018 pval = p + 2, base = 10;
1019 len = OS__strtoent(base, pval, p + len, &val);
1020 if(len == -1) {
1021 /* Invalid charset. Just copy verbatim. */
1022 *buf++ = ch;
1023 continue;
1024 }
1025 if(!len || pval[len-1] != 0x3b) goto want_more;
1026 assert(val > 0);
1027 p += (pval - p) + len - 1; /* Advance past entref */
1028
1029 if(val < 0x80) {
1030 *buf++ = (char)val;
1031 } else if(val < 0x800) {
1032 *buf++ = 0xc0 | ((val >> 6));
1033 *buf++ = 0x80 | ((val & 0x3f));
1034 } else if(val < 0x10000) {
1035 *buf++ = 0xe0 | ((val >> 12));
1036 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1037 *buf++ = 0x80 | ((val & 0x3f));
1038 } else if(val < 0x200000) {
1039 *buf++ = 0xf0 | ((val >> 18));
1040 *buf++ = 0x80 | ((val >> 12) & 0x3f);
1041 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1042 *buf++ = 0x80 | ((val & 0x3f));
1043 } else if(val < 0x4000000) {
1044 *buf++ = 0xf8 | ((val >> 24));
1045 *buf++ = 0x80 | ((val >> 18) & 0x3f);
1046 *buf++ = 0x80 | ((val >> 12) & 0x3f);
1047 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1048 *buf++ = 0x80 | ((val & 0x3f));
1049 } else {
1050 *buf++ = 0xfc | ((val >> 30) & 0x1);
1051 *buf++ = 0x80 | ((val >> 24) & 0x3f);
1052 *buf++ = 0x80 | ((val >> 18) & 0x3f);
1053 *buf++ = 0x80 | ((val >> 12) & 0x3f);
1054 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1055 *buf++ = 0x80 | ((val & 0x3f));
1056 }
1057 } else {
1058 /*
1059 * Ugly, limited parsing of &amp; &gt; &lt;
1060 */
1061 char *sc = (char *)memchr(p, 0x3b, len > 5 ? 5 : len);
1062 if(!sc) goto want_more;
1063 if((sc - p) == 4
1064 && p[1] == 0x61 /* 'a' */
1065 && p[2] == 0x6d /* 'm' */
1066 && p[3] == 0x70 /* 'p' */) {
1067 *buf++ = 0x26;
1068 p = sc;
1069 continue;
1070 }
1071 if((sc - p) == 3) {
1072 if(p[1] == 0x6c) {
1073 *buf = 0x3c; /* '<' */
1074 } else if(p[1] == 0x67) {
1075 *buf = 0x3e; /* '>' */
1076 } else {
1077 /* Unsupported entity reference */
1078 *buf++ = ch;
1079 continue;
1080 }
1081 if(p[2] != 0x74) {
1082 /* Unsupported entity reference */
1083 *buf++ = ch;
1084 continue;
1085 }
1086 buf++;
1087 p = sc;
1088 continue;
1089 }
1090 /* Unsupported entity reference */
1091 *buf++ = ch;
1092 }
1093
1094 continue;
1095 want_more:
1096 if(have_more) {
1097 /*
1098 * We know that no more data (of the same type)
1099 * is coming. Copy the rest verbatim.
1100 */
1101 *buf++ = ch;
1102 continue;
1103 }
Lev Walkin0fab1a62005-03-09 22:19:25 +00001104 chunk_size = (p - (const char *)chunk_buf);
Lev Walkindc06f6b2004-10-20 15:50:55 +00001105 /* Processing stalled: need more data */
Lev Walkinbdaae772005-02-18 12:25:47 +00001106 break;
Lev Walkindc06f6b2004-10-20 15:50:55 +00001107 }
1108
1109 st->size = buf - st->buf;
Lev Walkin494fb702017-08-07 20:07:00 -07001110 assert(st->size <= new_size);
Lev Walkindc06f6b2004-10-20 15:50:55 +00001111 st->buf[st->size] = 0; /* Courtesy termination */
1112
1113 return chunk_size; /* Converted in full */
1114}
1115
1116/*
1117 * Decode OCTET STRING from the XML element's body.
1118 */
1119static asn_dec_rval_t
Lev Walkin20696a42017-10-17 21:27:33 -07001120OCTET_STRING__decode_xer(
1121 const asn_codec_ctx_t *opt_codec_ctx, const asn_TYPE_descriptor_t *td,
1122 void **sptr, const char *opt_mname, const void *buf_ptr, size_t size,
1123 int (*opt_unexpected_tag_decoder)(void *struct_ptr, const void *chunk_buf,
1124 size_t chunk_size),
1125 ssize_t (*body_receiver)(void *struct_ptr, const void *chunk_buf,
1126 size_t chunk_size, int have_more)) {
1127 OCTET_STRING_t *st = (OCTET_STRING_t *)*sptr;
Lev Walkind84f6032017-10-03 16:33:59 -07001128 const asn_OCTET_STRING_specifics_t *specs = td->specifics
1129 ? (const asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +08001130 : &asn_SPC_OCTET_STRING_specs;
Lev Walkindc06f6b2004-10-20 15:50:55 +00001131 const char *xml_tag = opt_mname ? opt_mname : td->xml_tag;
1132 asn_struct_ctx_t *ctx; /* Per-structure parser context */
Lev Walkinc61f3862005-02-14 17:21:22 +00001133 asn_dec_rval_t rval; /* Return value from the decoder */
1134 int st_allocated;
Lev Walkindc06f6b2004-10-20 15:50:55 +00001135
1136 /*
1137 * Create the string if does not exist.
1138 */
Lev Walkinc61f3862005-02-14 17:21:22 +00001139 if(!st) {
Lev Walkin8484ed82004-12-14 13:31:01 +00001140 st = (OCTET_STRING_t *)CALLOC(1, specs->struct_size);
Lev Walkin8484ed82004-12-14 13:31:01 +00001141 *sptr = (void *)st;
Lev Walkinc61f3862005-02-14 17:21:22 +00001142 if(!st) goto sta_failed;
1143 st_allocated = 1;
Lev Walkinbdaae772005-02-18 12:25:47 +00001144 } else {
1145 st_allocated = 0;
1146 }
Lev Walkinc61f3862005-02-14 17:21:22 +00001147 if(!st->buf) {
1148 /* This is separate from above section */
1149 st->buf = (uint8_t *)CALLOC(1, 1);
1150 if(!st->buf) {
1151 if(st_allocated) {
1152 *sptr = 0;
1153 goto stb_failed;
1154 } else {
1155 goto sta_failed;
1156 }
Lev Walkindc06f6b2004-10-20 15:50:55 +00001157 }
1158 }
1159
1160 /* Restore parsing context */
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001161 ctx = (asn_struct_ctx_t *)(((char *)*sptr) + specs->ctx_offset);
Lev Walkindc06f6b2004-10-20 15:50:55 +00001162
1163 return xer_decode_general(opt_codec_ctx, ctx, *sptr, xml_tag,
1164 buf_ptr, size, opt_unexpected_tag_decoder, body_receiver);
Lev Walkinc61f3862005-02-14 17:21:22 +00001165
1166stb_failed:
1167 FREEMEM(st);
1168sta_failed:
1169 rval.code = RC_FAIL;
1170 rval.consumed = 0;
1171 return rval;
Lev Walkindc06f6b2004-10-20 15:50:55 +00001172}
1173
1174/*
1175 * Decode OCTET STRING from the hexadecimal data.
1176 */
1177asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -07001178OCTET_STRING_decode_xer_hex(const asn_codec_ctx_t *opt_codec_ctx,
Lev Walkin20696a42017-10-17 21:27:33 -07001179 const asn_TYPE_descriptor_t *td, void **sptr,
1180 const char *opt_mname, const void *buf_ptr,
1181 size_t size) {
1182 return OCTET_STRING__decode_xer(opt_codec_ctx, td, sptr, opt_mname,
Lev Walkindc06f6b2004-10-20 15:50:55 +00001183 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 Walkin20696a42017-10-17 21:27:33 -07001191 const asn_TYPE_descriptor_t *td, void **sptr,
1192 const char *opt_mname, const void *buf_ptr,
1193 size_t size) {
1194 return OCTET_STRING__decode_xer(opt_codec_ctx, td, sptr, opt_mname,
Lev Walkindc06f6b2004-10-20 15:50:55 +00001195 buf_ptr, size, 0, OCTET_STRING__convert_binary);
1196}
1197
1198/*
1199 * Decode OCTET STRING from the string (ASCII/UTF-8) data.
1200 */
1201asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -07001202OCTET_STRING_decode_xer_utf8(const asn_codec_ctx_t *opt_codec_ctx,
Lev Walkin20696a42017-10-17 21:27:33 -07001203 const asn_TYPE_descriptor_t *td, void **sptr,
1204 const char *opt_mname, const void *buf_ptr,
1205 size_t size) {
1206 return OCTET_STRING__decode_xer(opt_codec_ctx, td, sptr, opt_mname,
Lev Walkindc06f6b2004-10-20 15:50:55 +00001207 buf_ptr, size,
1208 OCTET_STRING__handle_control_chars,
1209 OCTET_STRING__convert_entrefs);
1210}
1211
Lev Walkin0368c622017-09-18 00:33:16 -07001212#ifndef ASN_DISABLE_PER_SUPPORT
1213
Lev Walkin725883b2006-10-09 12:07:58 +00001214static int
Lev Walkin3a4689a2006-11-24 11:20:27 +00001215OCTET_STRING_per_get_characters(asn_per_data_t *po, uint8_t *buf,
1216 size_t units, unsigned int bpc, unsigned int unit_bits,
Lev Walkin494fb702017-08-07 20:07:00 -07001217 long lb, long ub, const asn_per_constraints_t *pc) {
Lev Walkin3a4689a2006-11-24 11:20:27 +00001218 uint8_t *end = buf + units * bpc;
Lev Walkin725883b2006-10-09 12:07:58 +00001219
1220 ASN_DEBUG("Expanding %d characters into (%ld..%ld):%d",
Lev Walkin3a4689a2006-11-24 11:20:27 +00001221 (int)units, lb, ub, unit_bits);
Lev Walkin725883b2006-10-09 12:07:58 +00001222
1223 /* X.691: 27.5.4 */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001224 if((unsigned long)ub <= ((unsigned long)2 << (unit_bits - 1))) {
Lev Walkin725883b2006-10-09 12:07:58 +00001225 /* Decode without translation */
1226 lb = 0;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001227 } else if(pc && pc->code2value) {
1228 if(unit_bits > 16)
1229 return 1; /* FATAL: can't have constrained
1230 * UniversalString with more than
1231 * 16 million code points */
1232 for(; buf < end; buf += bpc) {
Lev Walkin725883b2006-10-09 12:07:58 +00001233 int value;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001234 int code = per_get_few_bits(po, unit_bits);
Lev Walkin725883b2006-10-09 12:07:58 +00001235 if(code < 0) return -1; /* WMORE */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001236 value = pc->code2value(code);
Lev Walkin725883b2006-10-09 12:07:58 +00001237 if(value < 0) {
1238 ASN_DEBUG("Code %d (0x%02x) is"
1239 " not in map (%ld..%ld)",
1240 code, code, lb, ub);
1241 return 1; /* FATAL */
1242 }
Lev Walkin3a4689a2006-11-24 11:20:27 +00001243 switch(bpc) {
1244 case 1: *buf = value; break;
1245 case 2: buf[0] = value >> 8; buf[1] = value; break;
1246 case 4: buf[0] = value >> 24; buf[1] = value >> 16;
1247 buf[2] = value >> 8; buf[3] = value; break;
1248 }
Lev Walkin725883b2006-10-09 12:07:58 +00001249 }
1250 return 0;
1251 }
1252
Lev Walkin3a4689a2006-11-24 11:20:27 +00001253 /* Shortcut the no-op copying to the aligned structure */
1254 if(lb == 0 && (unit_bits == 8 * bpc)) {
1255 return per_get_many_bits(po, buf, 0, unit_bits * units);
1256 }
1257
1258 for(; buf < end; buf += bpc) {
Lev Walkin5d947a82017-10-03 01:04:03 -07001259 int32_t code = per_get_few_bits(po, unit_bits);
1260 int32_t ch = code + lb;
Lev Walkin725883b2006-10-09 12:07:58 +00001261 if(code < 0) return -1; /* WMORE */
1262 if(ch > ub) {
1263 ASN_DEBUG("Code %d is out of range (%ld..%ld)",
1264 ch, lb, ub);
1265 return 1; /* FATAL */
1266 }
Lev Walkin3a4689a2006-11-24 11:20:27 +00001267 switch(bpc) {
1268 case 1: *buf = ch; break;
1269 case 2: buf[0] = ch >> 8; buf[1] = ch; break;
1270 case 4: buf[0] = ch >> 24; buf[1] = ch >> 16;
1271 buf[2] = ch >> 8; buf[3] = ch; break;
1272 }
Lev Walkin725883b2006-10-09 12:07:58 +00001273 }
1274
1275 return 0;
1276}
1277
1278static int
Lev Walkin3a4689a2006-11-24 11:20:27 +00001279OCTET_STRING_per_put_characters(asn_per_outp_t *po, const uint8_t *buf,
1280 size_t units, unsigned int bpc, unsigned int unit_bits,
Lev Walkin494fb702017-08-07 20:07:00 -07001281 long lb, long ub, const asn_per_constraints_t *pc) {
Lev Walkin3a4689a2006-11-24 11:20:27 +00001282 const uint8_t *end = buf + units * bpc;
Lev Walkin725883b2006-10-09 12:07:58 +00001283
Lev Walkin3a4689a2006-11-24 11:20:27 +00001284 ASN_DEBUG("Squeezing %d characters into (%ld..%ld):%d (%d bpc)",
1285 (int)units, lb, ub, unit_bits, bpc);
Lev Walkin725883b2006-10-09 12:07:58 +00001286
1287 /* X.691: 27.5.4 */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001288 if((unsigned long)ub <= ((unsigned long)2 << (unit_bits - 1))) {
Lev Walkin725883b2006-10-09 12:07:58 +00001289 /* Encode as is */
1290 lb = 0;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001291 } else if(pc && pc->value2code) {
1292 for(; buf < end; buf += bpc) {
1293 int code;
1294 uint32_t value;
1295 switch(bpc) {
1296 case 1: value = *(const uint8_t *)buf; break;
1297 case 2: value = (buf[0] << 8) | buf[1]; break;
1298 case 4: value = (buf[0] << 24) | (buf[1] << 16)
1299 | (buf[2] << 8) | buf[3]; break;
1300 default: return -1;
1301 }
1302 code = pc->value2code(value);
Lev Walkin725883b2006-10-09 12:07:58 +00001303 if(code < 0) {
1304 ASN_DEBUG("Character %d (0x%02x) is"
1305 " not in map (%ld..%ld)",
1306 *buf, *buf, lb, ub);
1307 return -1;
1308 }
1309 if(per_put_few_bits(po, code, unit_bits))
1310 return -1;
1311 }
1312 }
1313
Lev Walkin3a4689a2006-11-24 11:20:27 +00001314 /* Shortcut the no-op copying to the aligned structure */
1315 if(lb == 0 && (unit_bits == 8 * bpc)) {
1316 return per_put_many_bits(po, buf, unit_bits * units);
1317 }
1318
Lev Walkin22cef6c2017-10-16 20:33:07 -07001319 for(ub -= lb; buf < end; buf += bpc) {
1320 int ch;
1321 uint32_t value;
1322 switch(bpc) {
1323 case 1:
1324 value = *(const uint8_t *)buf;
1325 break;
1326 case 2:
1327 value = (buf[0] << 8) | buf[1];
1328 break;
1329 case 4:
1330 value = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
1331 break;
1332 default:
1333 return -1;
1334 }
1335 ch = value - lb;
1336 if(ch < 0 || ch > ub) {
1337 ASN_DEBUG("Character %d (0x%02x) is out of range (%ld..%ld)", *buf,
1338 value, lb, ub + lb);
1339 return -1;
1340 }
1341 if(per_put_few_bits(po, ch, unit_bits)) return -1;
1342 }
Lev Walkin725883b2006-10-09 12:07:58 +00001343
Lev Walkin22cef6c2017-10-16 20:33:07 -07001344 return 0;
Lev Walkin725883b2006-10-09 12:07:58 +00001345}
1346
Lev Walkin0368c622017-09-18 00:33:16 -07001347static asn_per_constraints_t asn_DEF_OCTET_STRING_constraints = {
1348 { APC_CONSTRAINED, 8, 8, 0, 255 },
1349 { APC_SEMI_CONSTRAINED, -1, -1, 0, 0 },
1350 0, 0
1351};
Lev Walkin62d76872017-08-05 22:49:42 -07001352
Lev Walkin59b176e2005-11-26 11:25:14 +00001353asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -07001354OCTET_STRING_decode_uper(const asn_codec_ctx_t *opt_codec_ctx,
Lev Walkin20696a42017-10-17 21:27:33 -07001355 const asn_TYPE_descriptor_t *td,
Lev Walkin494fb702017-08-07 20:07:00 -07001356 const asn_per_constraints_t *constraints, void **sptr,
1357 asn_per_data_t *pd) {
Lev Walkind84f6032017-10-03 16:33:59 -07001358 const asn_OCTET_STRING_specifics_t *specs = td->specifics
1359 ? (const asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +08001360 : &asn_SPC_OCTET_STRING_specs;
Lev Walkin494fb702017-08-07 20:07:00 -07001361 const asn_per_constraints_t *pc =
Lev Walkina5972be2017-09-29 23:15:58 -07001362 constraints ? constraints : td->encoding_constraints.per_constraints;
Lev Walkin494fb702017-08-07 20:07:00 -07001363 const asn_per_constraint_t *cval;
1364 const asn_per_constraint_t *csiz;
Lev Walkin59b176e2005-11-26 11:25:14 +00001365 asn_dec_rval_t rval = { RC_OK, 0 };
Lev Walkina5b02882017-10-01 22:48:44 -07001366 OCTET_STRING_t *st = (OCTET_STRING_t *)*sptr;
Lev Walkin59b176e2005-11-26 11:25:14 +00001367 ssize_t consumed_myself = 0;
1368 int repeat;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001369 enum {
Lev Walkin3a4689a2006-11-24 11:20:27 +00001370 OS__BPC_CHAR = 1,
1371 OS__BPC_U16 = 2,
1372 OS__BPC_U32 = 4
1373 } bpc; /* Bytes per character */
1374 unsigned int unit_bits;
1375 unsigned int canonical_unit_bits;
Lev Walkin59b176e2005-11-26 11:25:14 +00001376
1377 (void)opt_codec_ctx;
1378
Lev Walkin3a4689a2006-11-24 11:20:27 +00001379 if(pc) {
1380 cval = &pc->value;
1381 csiz = &pc->size;
1382 } else {
1383 cval = &asn_DEF_OCTET_STRING_constraints.value;
1384 csiz = &asn_DEF_OCTET_STRING_constraints.size;
1385 }
1386
1387 switch(specs->subvariant) {
1388 default:
1389 case ASN_OSUBV_ANY:
Lev Walkina5b02882017-10-01 22:48:44 -07001390 case ASN_OSUBV_BIT:
Lev Walkin3a4689a2006-11-24 11:20:27 +00001391 ASN_DEBUG("Unrecognized subvariant %d", specs->subvariant);
1392 RETURN(RC_FAIL);
Lev Walkin3a4689a2006-11-24 11:20:27 +00001393 break;
1394 case ASN_OSUBV_STR:
1395 canonical_unit_bits = unit_bits = 8;
1396 if(cval->flags & APC_CONSTRAINED)
1397 unit_bits = cval->range_bits;
1398 bpc = OS__BPC_CHAR;
1399 break;
1400 case ASN_OSUBV_U16:
1401 canonical_unit_bits = unit_bits = 16;
1402 if(cval->flags & APC_CONSTRAINED)
1403 unit_bits = cval->range_bits;
1404 bpc = OS__BPC_U16;
1405 break;
1406 case ASN_OSUBV_U32:
1407 canonical_unit_bits = unit_bits = 32;
1408 if(cval->flags & APC_CONSTRAINED)
1409 unit_bits = cval->range_bits;
1410 bpc = OS__BPC_U32;
1411 break;
1412 }
1413
Lev Walkin59b176e2005-11-26 11:25:14 +00001414 /*
1415 * Allocate the string.
1416 */
1417 if(!st) {
Lev Walkina5b02882017-10-01 22:48:44 -07001418 st = (OCTET_STRING_t *)(*sptr = CALLOC(1, specs->struct_size));
Lev Walkin59b176e2005-11-26 11:25:14 +00001419 if(!st) RETURN(RC_FAIL);
1420 }
1421
Lev Walkin725883b2006-10-09 12:07:58 +00001422 ASN_DEBUG("PER Decoding %s size %ld .. %ld bits %d",
Lev Walkin3a4689a2006-11-24 11:20:27 +00001423 csiz->flags & APC_EXTENSIBLE ? "extensible" : "non-extensible",
1424 csiz->lower_bound, csiz->upper_bound, csiz->effective_bits);
Lev Walkince676dd2005-12-20 22:34:55 +00001425
Lev Walkin3a4689a2006-11-24 11:20:27 +00001426 if(csiz->flags & APC_EXTENSIBLE) {
Lev Walkin59b176e2005-11-26 11:25:14 +00001427 int inext = per_get_few_bits(pd, 1);
Lev Walkin0a8aa602006-09-18 20:05:55 +00001428 if(inext < 0) RETURN(RC_WMORE);
Lev Walkin3a4689a2006-11-24 11:20:27 +00001429 if(inext) {
1430 csiz = &asn_DEF_OCTET_STRING_constraints.size;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001431 unit_bits = canonical_unit_bits;
1432 }
Lev Walkin59b176e2005-11-26 11:25:14 +00001433 }
1434
Lev Walkin3a4689a2006-11-24 11:20:27 +00001435 if(csiz->effective_bits >= 0) {
Lev Walkin59b176e2005-11-26 11:25:14 +00001436 FREEMEM(st->buf);
Lev Walkin3a4689a2006-11-24 11:20:27 +00001437 if(bpc) {
1438 st->size = csiz->upper_bound * bpc;
Lev Walkin59b176e2005-11-26 11:25:14 +00001439 } else {
Lev Walkin3a4689a2006-11-24 11:20:27 +00001440 st->size = (csiz->upper_bound + 7) >> 3;
Lev Walkin59b176e2005-11-26 11:25:14 +00001441 }
1442 st->buf = (uint8_t *)MALLOC(st->size + 1);
1443 if(!st->buf) { st->size = 0; RETURN(RC_FAIL); }
1444 }
1445
1446 /* X.691, #16.5: zero-length encoding */
1447 /* X.691, #16.6: short fixed length encoding (up to 2 octets) */
1448 /* X.691, #16.7: long fixed length encoding (up to 64K octets) */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001449 if(csiz->effective_bits == 0) {
Lev Walkin725883b2006-10-09 12:07:58 +00001450 int ret;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001451 if(bpc) {
1452 ASN_DEBUG("Encoding OCTET STRING size %ld",
1453 csiz->upper_bound);
1454 ret = OCTET_STRING_per_get_characters(pd, st->buf,
1455 csiz->upper_bound, bpc, unit_bits,
1456 cval->lower_bound, cval->upper_bound, pc);
Lev Walkin725883b2006-10-09 12:07:58 +00001457 if(ret > 0) RETURN(RC_FAIL);
1458 } else {
Lev Walkin3a4689a2006-11-24 11:20:27 +00001459 ASN_DEBUG("Encoding BIT STRING size %ld",
1460 csiz->upper_bound);
Lev Walkin725883b2006-10-09 12:07:58 +00001461 ret = per_get_many_bits(pd, st->buf, 0,
Lev Walkin3a4689a2006-11-24 11:20:27 +00001462 unit_bits * csiz->upper_bound);
Lev Walkin725883b2006-10-09 12:07:58 +00001463 }
Lev Walkin0a8aa602006-09-18 20:05:55 +00001464 if(ret < 0) RETURN(RC_WMORE);
Lev Walkin3a4689a2006-11-24 11:20:27 +00001465 consumed_myself += unit_bits * csiz->upper_bound;
Lev Walkin59b176e2005-11-26 11:25:14 +00001466 st->buf[st->size] = 0;
Lev Walkin59b176e2005-11-26 11:25:14 +00001467 RETURN(RC_OK);
1468 }
1469
1470 st->size = 0;
1471 do {
Lev Walkin3a4689a2006-11-24 11:20:27 +00001472 ssize_t raw_len;
Lev Walkin59b176e2005-11-26 11:25:14 +00001473 ssize_t len_bytes;
Lev Walkin59b176e2005-11-26 11:25:14 +00001474 void *p;
1475 int ret;
1476
1477 /* Get the PER length */
Lev Walkin9d1b45f2017-10-01 17:04:48 -07001478 raw_len = uper_get_length(pd, csiz->effective_bits, csiz->lower_bound,
1479 &repeat);
Lev Walkin3a4689a2006-11-24 11:20:27 +00001480 if(raw_len < 0) RETURN(RC_WMORE);
Lev Walkin5d947a82017-10-03 01:04:03 -07001481 if(raw_len == 0 && st->buf) break;
Lev Walkin59b176e2005-11-26 11:25:14 +00001482
Lev Walkin0a8aa602006-09-18 20:05:55 +00001483 ASN_DEBUG("Got PER length eb %ld, len %ld, %s (%s)",
Lev Walkin3a4689a2006-11-24 11:20:27 +00001484 (long)csiz->effective_bits, (long)raw_len,
Lev Walkin0a8aa602006-09-18 20:05:55 +00001485 repeat ? "repeat" : "once", td->name);
Lev Walkina5b02882017-10-01 22:48:44 -07001486 len_bytes = raw_len * bpc;
Lev Walkin59b176e2005-11-26 11:25:14 +00001487 p = REALLOC(st->buf, st->size + len_bytes + 1);
1488 if(!p) RETURN(RC_FAIL);
1489 st->buf = (uint8_t *)p;
1490
Lev Walkina5b02882017-10-01 22:48:44 -07001491 ret = OCTET_STRING_per_get_characters(pd, &st->buf[st->size], raw_len,
1492 bpc, unit_bits, cval->lower_bound,
1493 cval->upper_bound, pc);
1494 if(ret > 0) RETURN(RC_FAIL);
Lev Walkin0a8aa602006-09-18 20:05:55 +00001495 if(ret < 0) RETURN(RC_WMORE);
Lev Walkin59b176e2005-11-26 11:25:14 +00001496 st->size += len_bytes;
1497 } while(repeat);
1498 st->buf[st->size] = 0; /* nul-terminate */
1499
1500 return rval;
1501}
Lev Walkindc06f6b2004-10-20 15:50:55 +00001502
Lev Walkin523de9e2006-08-18 01:34:18 +00001503asn_enc_rval_t
Lev Walkin20696a42017-10-17 21:27:33 -07001504OCTET_STRING_encode_uper(const asn_TYPE_descriptor_t *td,
1505 const asn_per_constraints_t *constraints,
1506 const void *sptr, asn_per_outp_t *po) {
Lev Walkind84f6032017-10-03 16:33:59 -07001507 const asn_OCTET_STRING_specifics_t *specs = td->specifics
1508 ? (const asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +08001509 : &asn_SPC_OCTET_STRING_specs;
Lev Walkin494fb702017-08-07 20:07:00 -07001510 const asn_per_constraints_t *pc = constraints ? constraints
Lev Walkina5972be2017-09-29 23:15:58 -07001511 : td->encoding_constraints.per_constraints;
Lev Walkin494fb702017-08-07 20:07:00 -07001512 const asn_per_constraint_t *cval;
1513 const asn_per_constraint_t *csiz;
Lev Walkina5b02882017-10-01 22:48:44 -07001514 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001515 asn_enc_rval_t er = { 0, 0, 0 };
Lev Walkin523de9e2006-08-18 01:34:18 +00001516 int inext = 0; /* Lies not within extension root */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001517 unsigned int unit_bits;
1518 unsigned int canonical_unit_bits;
Lev Walkin9d1b45f2017-10-01 17:04:48 -07001519 size_t size_in_units;
Lev Walkin523de9e2006-08-18 01:34:18 +00001520 const uint8_t *buf;
1521 int ret;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001522 enum {
Lev Walkin3a4689a2006-11-24 11:20:27 +00001523 OS__BPC_CHAR = 1,
1524 OS__BPC_U16 = 2,
1525 OS__BPC_U32 = 4
1526 } bpc; /* Bytes per character */
1527 int ct_extensible;
Lev Walkin523de9e2006-08-18 01:34:18 +00001528
Lev Walkin2ddd3082006-10-16 12:32:14 +00001529 if(!st || (!st->buf && st->size))
Lev Walkin7c1dc052016-03-14 03:08:15 -07001530 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001531
Lev Walkin3a4689a2006-11-24 11:20:27 +00001532 if(pc) {
1533 cval = &pc->value;
1534 csiz = &pc->size;
1535 } else {
1536 cval = &asn_DEF_OCTET_STRING_constraints.value;
1537 csiz = &asn_DEF_OCTET_STRING_constraints.size;
1538 }
1539 ct_extensible = csiz->flags & APC_EXTENSIBLE;
1540
1541 switch(specs->subvariant) {
1542 default:
1543 case ASN_OSUBV_ANY:
Lev Walkin3a4689a2006-11-24 11:20:27 +00001544 case ASN_OSUBV_BIT:
Lev Walkina5b02882017-10-01 22:48:44 -07001545 ASN__ENCODE_FAILED;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001546 case ASN_OSUBV_STR:
1547 canonical_unit_bits = unit_bits = 8;
1548 if(cval->flags & APC_CONSTRAINED)
1549 unit_bits = cval->range_bits;
1550 bpc = OS__BPC_CHAR;
Lev Walkin9d1b45f2017-10-01 17:04:48 -07001551 size_in_units = st->size;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001552 break;
1553 case ASN_OSUBV_U16:
1554 canonical_unit_bits = unit_bits = 16;
1555 if(cval->flags & APC_CONSTRAINED)
1556 unit_bits = cval->range_bits;
1557 bpc = OS__BPC_U16;
Lev Walkin9d1b45f2017-10-01 17:04:48 -07001558 size_in_units = st->size >> 1;
Lev Walkin4ef01a22017-09-06 22:54:39 -07001559 if(st->size & 1) {
1560 ASN_DEBUG("%s string size is not modulo 2", td->name);
1561 ASN__ENCODE_FAILED;
1562 }
Lev Walkin3a4689a2006-11-24 11:20:27 +00001563 break;
1564 case ASN_OSUBV_U32:
1565 canonical_unit_bits = unit_bits = 32;
1566 if(cval->flags & APC_CONSTRAINED)
1567 unit_bits = cval->range_bits;
1568 bpc = OS__BPC_U32;
Lev Walkin9d1b45f2017-10-01 17:04:48 -07001569 size_in_units = st->size >> 2;
Lev Walkin4ef01a22017-09-06 22:54:39 -07001570 if(st->size & 3) {
1571 ASN_DEBUG("%s string size is not modulo 4", td->name);
1572 ASN__ENCODE_FAILED;
1573 }
Lev Walkin3a4689a2006-11-24 11:20:27 +00001574 break;
Lev Walkin725883b2006-10-09 12:07:58 +00001575 }
1576
Lev Walkin9d1b45f2017-10-01 17:04:48 -07001577 ASN_DEBUG("Encoding %s into %zu units of %d bits"
Lev Walkin725883b2006-10-09 12:07:58 +00001578 " (%ld..%ld, effective %d)%s",
Lev Walkin9d1b45f2017-10-01 17:04:48 -07001579 td->name, size_in_units, unit_bits,
Lev Walkin3a4689a2006-11-24 11:20:27 +00001580 csiz->lower_bound, csiz->upper_bound,
1581 csiz->effective_bits, ct_extensible ? " EXT" : "");
Lev Walkin523de9e2006-08-18 01:34:18 +00001582
Lev Walkind4c16732013-03-28 05:00:47 -07001583 /* Figure out whether size lies within PER visible constraint */
Lev Walkin523de9e2006-08-18 01:34:18 +00001584
Lev Walkin9d1b45f2017-10-01 17:04:48 -07001585 if(csiz->effective_bits >= 0) {
1586 if((ssize_t)size_in_units < csiz->lower_bound
1587 || (ssize_t)size_in_units > csiz->upper_bound) {
1588 if(ct_extensible) {
1589 csiz = &asn_DEF_OCTET_STRING_constraints.size;
1590 unit_bits = canonical_unit_bits;
1591 inext = 1;
1592 } else {
1593 ASN__ENCODE_FAILED;
1594 }
1595 }
1596 } else {
1597 inext = 0;
1598 }
Lev Walkin523de9e2006-08-18 01:34:18 +00001599
Lev Walkin9d1b45f2017-10-01 17:04:48 -07001600 if(ct_extensible) {
Lev Walkin523de9e2006-08-18 01:34:18 +00001601 /* Declare whether length is [not] within extension root */
1602 if(per_put_few_bits(po, inext, 1))
Lev Walkin7c1dc052016-03-14 03:08:15 -07001603 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001604 }
1605
Lev Walkin9d1b45f2017-10-01 17:04:48 -07001606 if(csiz->effective_bits >= 0 && !inext) {
1607 ASN_DEBUG("Encoding %zu bytes (%ld), length in %d bits", st->size,
1608 size_in_units - csiz->lower_bound, csiz->effective_bits);
1609 ret = per_put_few_bits(po, size_in_units - csiz->lower_bound,
1610 csiz->effective_bits);
1611 if(ret) ASN__ENCODE_FAILED;
Lev Walkina5b02882017-10-01 22:48:44 -07001612 ret = OCTET_STRING_per_put_characters(po, st->buf, size_in_units, bpc,
1613 unit_bits, cval->lower_bound,
1614 cval->upper_bound, pc);
Lev Walkin9d1b45f2017-10-01 17:04:48 -07001615 if(ret) ASN__ENCODE_FAILED;
1616 ASN__ENCODED_OK(er);
1617 }
Lev Walkin523de9e2006-08-18 01:34:18 +00001618
Lev Walkin9d1b45f2017-10-01 17:04:48 -07001619 ASN_DEBUG("Encoding %zu bytes", st->size);
Lev Walkin523de9e2006-08-18 01:34:18 +00001620
Lev Walkin9d1b45f2017-10-01 17:04:48 -07001621 buf = st->buf;
Lev Walkin5d947a82017-10-03 01:04:03 -07001622 ASN_DEBUG("Encoding %zu in units", size_in_units);
1623 do {
1624 int need_eom = 0;
1625 ssize_t may_save = uper_put_length(po, size_in_units, &need_eom);
1626 if(may_save < 0) ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001627
Lev Walkin5d947a82017-10-03 01:04:03 -07001628 ASN_DEBUG("Encoding %zd of %zu%s", may_save, size_in_units,
1629 need_eom ? ",+EOM" : "");
Lev Walkin523de9e2006-08-18 01:34:18 +00001630
Lev Walkin5d947a82017-10-03 01:04:03 -07001631 ret = OCTET_STRING_per_put_characters(po, buf, may_save, bpc, unit_bits,
Lev Walkina5b02882017-10-01 22:48:44 -07001632 cval->lower_bound,
1633 cval->upper_bound, pc);
Lev Walkin9d1b45f2017-10-01 17:04:48 -07001634 if(ret) ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001635
Lev Walkin5d947a82017-10-03 01:04:03 -07001636 buf += may_save * bpc;
1637 size_in_units -= may_save;
1638 assert(!(may_save & 0x07) || !size_in_units);
1639 if(need_eom && uper_put_length(po, 0, 0))
1640 ASN__ENCODE_FAILED; /* End of Message length */
1641 } while(size_in_units);
Lev Walkin523de9e2006-08-18 01:34:18 +00001642
Lev Walkin9d1b45f2017-10-01 17:04:48 -07001643 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +00001644}
1645
Lev Walkin62d76872017-08-05 22:49:42 -07001646#endif /* ASN_DISABLE_PER_SUPPORT */
1647
Lev Walkinf15320b2004-06-03 03:38:44 +00001648int
Lev Walkin20696a42017-10-17 21:27:33 -07001649OCTET_STRING_print(const asn_TYPE_descriptor_t *td, const void *sptr,
1650 int ilevel, asn_app_consume_bytes_f *cb, void *app_key) {
1651 const char * const h2c = "0123456789ABCDEF";
Lev Walkinc2346572004-08-11 09:07:36 +00001652 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +00001653 char scratch[16 * 3 + 4];
1654 char *p = scratch;
1655 uint8_t *buf;
1656 uint8_t *end;
1657 size_t i;
Lev Walkinf15320b2004-06-03 03:38:44 +00001658
Lev Walkind9bd7752004-06-05 08:17:50 +00001659 (void)td; /* Unused argument */
1660
Lev Walkin2ddd3082006-10-16 12:32:14 +00001661 if(!st || (!st->buf && st->size))
1662 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001663
1664 /*
1665 * Dump the contents of the buffer in hexadecimal.
1666 */
1667 buf = st->buf;
1668 end = buf + st->size;
1669 for(i = 0; buf < end; buf++, i++) {
1670 if(!(i % 16) && (i || st->size > 16)) {
Lev Walkin8e8078a2004-09-26 13:10:40 +00001671 if(cb(scratch, p - scratch, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +00001672 return -1;
Lev Walkin8e8078a2004-09-26 13:10:40 +00001673 _i_INDENT(1);
Lev Walkinf15320b2004-06-03 03:38:44 +00001674 p = scratch;
1675 }
1676 *p++ = h2c[(*buf >> 4) & 0x0F];
1677 *p++ = h2c[*buf & 0x0F];
Lev Walkina9cc46e2004-09-22 16:06:28 +00001678 *p++ = 0x20;
Lev Walkinf15320b2004-06-03 03:38:44 +00001679 }
1680
Lev Walkincc6a9102004-09-23 22:06:26 +00001681 if(p > scratch) {
1682 p--; /* Remove the tail space */
Lev Walkin8e8078a2004-09-26 13:10:40 +00001683 if(cb(scratch, p - scratch, app_key) < 0)
Lev Walkincc6a9102004-09-23 22:06:26 +00001684 return -1;
1685 }
1686
1687 return 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001688}
1689
1690int
Lev Walkin20696a42017-10-17 21:27:33 -07001691OCTET_STRING_print_utf8(const asn_TYPE_descriptor_t *td, const void *sptr,
1692 int ilevel, asn_app_consume_bytes_f *cb,
1693 void *app_key) {
1694 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +00001695
Lev Walkind9bd7752004-06-05 08:17:50 +00001696 (void)td; /* Unused argument */
1697 (void)ilevel; /* Unused argument */
1698
Lev Walkin2ddd3082006-10-16 12:32:14 +00001699 if(st && (st->buf || !st->size)) {
Lev Walkin8e8078a2004-09-26 13:10:40 +00001700 return (cb(st->buf, st->size, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001701 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +00001702 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001703 }
1704}
1705
1706void
Lev Walkinf6853ce2017-08-11 00:50:27 -07001707OCTET_STRING_free(const asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkin8d99d7b2017-08-25 01:06:00 -07001708 enum asn_struct_free_method method) {
1709 OCTET_STRING_t *st = (OCTET_STRING_t *)sptr;
Lev Walkind84f6032017-10-03 16:33:59 -07001710 const asn_OCTET_STRING_specifics_t *specs;
Lev Walkin034ee192016-03-14 02:28:33 -07001711 asn_struct_ctx_t *ctx;
Lev Walkinb54577a2004-11-08 10:47:12 +00001712 struct _stack *stck;
Lev Walkinf15320b2004-06-03 03:38:44 +00001713
1714 if(!td || !st)
1715 return;
1716
Lev Walkin034ee192016-03-14 02:28:33 -07001717 specs = td->specifics
Lev Walkind84f6032017-10-03 16:33:59 -07001718 ? (const asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +08001719 : &asn_SPC_OCTET_STRING_specs;
Lev Walkin034ee192016-03-14 02:28:33 -07001720 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
1721
Lev Walkinf15320b2004-06-03 03:38:44 +00001722 ASN_DEBUG("Freeing %s as OCTET STRING", td->name);
1723
1724 if(st->buf) {
1725 FREEMEM(st->buf);
Lev Walkin2ddd3082006-10-16 12:32:14 +00001726 st->buf = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001727 }
1728
1729 /*
1730 * Remove decode-time stack.
1731 */
Lev Walkinb54577a2004-11-08 10:47:12 +00001732 stck = (struct _stack *)ctx->ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +00001733 if(stck) {
1734 while(stck->tail) {
1735 struct _stack_el *sel = stck->tail;
1736 stck->tail = sel->prev;
1737 FREEMEM(sel);
1738 }
1739 FREEMEM(stck);
1740 }
1741
Lev Walkin8d99d7b2017-08-25 01:06:00 -07001742 switch(method) {
1743 case ASFM_FREE_EVERYTHING:
1744 FREEMEM(sptr);
1745 break;
1746 case ASFM_FREE_UNDERLYING:
1747 break;
1748 case ASFM_FREE_UNDERLYING_AND_RESET:
1749 memset(sptr, 0,
Lev Walkin2ed11832017-10-14 01:41:57 -07001750 td->specifics
1751 ? ((const asn_OCTET_STRING_specifics_t *)(td->specifics))
1752 ->struct_size
1753 : sizeof(OCTET_STRING_t));
Lev Walkin8d99d7b2017-08-25 01:06:00 -07001754 break;
1755 }
Lev Walkinf15320b2004-06-03 03:38:44 +00001756}
1757
1758/*
1759 * Conversion routines.
1760 */
1761int
1762OCTET_STRING_fromBuf(OCTET_STRING_t *st, const char *str, int len) {
1763 void *buf;
1764
1765 if(st == 0 || (str == 0 && len)) {
1766 errno = EINVAL;
1767 return -1;
1768 }
1769
1770 /*
1771 * Clear the OCTET STRING.
1772 */
1773 if(str == NULL) {
Lev Walkin7b284812005-12-17 11:43:25 +00001774 FREEMEM(st->buf);
1775 st->buf = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001776 st->size = 0;
1777 return 0;
1778 }
1779
1780 /* Determine the original string size, if not explicitly given */
1781 if(len < 0)
1782 len = strlen(str);
1783
1784 /* Allocate and fill the memory */
1785 buf = MALLOC(len + 1);
Lev Walkin7b284812005-12-17 11:43:25 +00001786 if(buf == NULL)
Lev Walkinf15320b2004-06-03 03:38:44 +00001787 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +00001788
1789 memcpy(buf, str, len);
Lev Walkin7b284812005-12-17 11:43:25 +00001790 ((uint8_t *)buf)[len] = '\0'; /* Couldn't use memcpy(len+1)! */
1791 FREEMEM(st->buf);
1792 st->buf = (uint8_t *)buf;
1793 st->size = len;
Lev Walkinf15320b2004-06-03 03:38:44 +00001794
1795 return 0;
1796}
1797
1798OCTET_STRING_t *
Lev Walkina5972be2017-09-29 23:15:58 -07001799OCTET_STRING_new_fromBuf(const asn_TYPE_descriptor_t *td, const char *str,
1800 int len) {
1801 const asn_OCTET_STRING_specifics_t *specs =
1802 td->specifics ? (const asn_OCTET_STRING_specifics_t *)td->specifics
1803 : &asn_SPC_OCTET_STRING_specs;
1804 OCTET_STRING_t *st;
Lev Walkinf15320b2004-06-03 03:38:44 +00001805
Lev Walkinbbd93252004-10-12 05:57:23 +00001806 st = (OCTET_STRING_t *)CALLOC(1, specs->struct_size);
Lev Walkinf15320b2004-06-03 03:38:44 +00001807 if(st && str && OCTET_STRING_fromBuf(st, str, len)) {
Lev Walkin419f6752006-09-13 04:02:00 +00001808 FREEMEM(st);
Lev Walkinf15320b2004-06-03 03:38:44 +00001809 st = NULL;
1810 }
1811
1812 return st;
1813}
1814
Lev Walkincd2f48e2017-08-10 02:14:59 -07001815/*
1816 * Lexicographically compare the common prefix of both strings,
1817 * and if it is the same return -1 for the smallest string.
1818 */
1819int
1820OCTET_STRING_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
1821 const void *bptr) {
Lev Walkina5b02882017-10-01 22:48:44 -07001822 const asn_OCTET_STRING_specifics_t *specs = td->specifics;
Lev Walkincd2f48e2017-08-10 02:14:59 -07001823 const OCTET_STRING_t *a = aptr;
1824 const OCTET_STRING_t *b = bptr;
1825
Lev Walkina5b02882017-10-01 22:48:44 -07001826 assert(!specs || specs->subvariant != ASN_OSUBV_BIT);
Lev Walkincd2f48e2017-08-10 02:14:59 -07001827
1828 if(a && b) {
1829 size_t common_prefix_size = a->size <= b->size ? a->size : b->size;
1830 int ret = memcmp(a->buf, b->buf, common_prefix_size);
1831 if(ret == 0) {
1832 /* Figure out which string with equal prefixes is longer. */
1833 if(a->size < b->size) {
1834 return -1;
1835 } else if(a->size > b->size) {
1836 return 1;
1837 } else {
1838 return 0;
1839 }
1840 } else {
Lev Walkin2ed11832017-10-14 01:41:57 -07001841 return ret < 0 ? -1 : 1;
Lev Walkincd2f48e2017-08-10 02:14:59 -07001842 }
1843 } else if(!a && !b) {
1844 return 0;
1845 } else if(!a) {
1846 return -1;
1847 } else {
1848 return 1;
1849 }
1850
1851}
1852
Lev Walkina5972be2017-09-29 23:15:58 -07001853/*
1854 * Biased function for randomizing character values around their limits.
1855 */
1856static uint32_t
1857OCTET_STRING__random_char(unsigned long lb, unsigned long ub) {
1858 assert(lb <= ub);
1859 switch(asn_random_between(0, 16)) {
1860 case 0:
1861 if(lb < ub) return lb + 1;
1862 /* Fall through */
1863 case 1:
1864 return lb;
1865 case 2:
1866 if(lb < ub) return ub - 1;
1867 /* Fall through */
1868 case 3:
1869 return ub;
1870 default:
1871 return asn_random_between(lb, ub);
1872 }
1873}
1874
Lev Walkin349b3402017-10-14 00:23:09 -07001875
1876size_t
1877OCTET_STRING_random_length_constrained(
1878 const asn_TYPE_descriptor_t *td,
1879 const asn_encoding_constraints_t *constraints, size_t max_length) {
1880 const unsigned lengths[] = {0, 1, 2, 3, 4, 8,
1881 126, 127, 128, 16383, 16384, 16385,
1882 65534, 65535, 65536, 65537};
1883 size_t rnd_len;
1884
1885 /* Figure out how far we should go */
1886 rnd_len = lengths[asn_random_between(
1887 0, sizeof(lengths) / sizeof(lengths[0]) - 1)];
1888
1889 if(!constraints) constraints = &td->encoding_constraints;
1890 if(constraints->per_constraints) {
1891 const asn_per_constraint_t *pc =
1892 &td->encoding_constraints.per_constraints->size;
1893 if(pc->flags & APC_CONSTRAINED) {
1894 long suggested_upper_bound = pc->upper_bound < (ssize_t)max_length
1895 ? pc->upper_bound
1896 : max_length;
1897 if(max_length <= (size_t)pc->lower_bound) {
1898 return pc->lower_bound;
1899 }
1900 if(pc->flags & APC_EXTENSIBLE) {
1901 switch(asn_random_between(0, 5)) {
1902 case 0:
1903 if(pc->lower_bound > 0) {
1904 rnd_len = pc->lower_bound - 1;
1905 break;
1906 }
1907 /* Fall through */
1908 case 1:
1909 rnd_len = pc->upper_bound + 1;
1910 break;
1911 case 2:
1912 /* Keep rnd_len from the table */
1913 if(rnd_len <= max_length) {
1914 break;
1915 }
1916 /* Fall through */
1917 default:
1918 rnd_len = asn_random_between(pc->lower_bound,
1919 suggested_upper_bound);
1920 }
1921 } else {
1922 rnd_len =
1923 asn_random_between(pc->lower_bound, suggested_upper_bound);
1924 }
1925 } else {
1926 rnd_len = asn_random_between(0, max_length);
1927 }
1928 } else if(rnd_len > max_length) {
1929 rnd_len = asn_random_between(0, max_length);
1930 }
1931
1932 return rnd_len;
1933}
1934
Lev Walkina5972be2017-09-29 23:15:58 -07001935asn_random_fill_result_t
1936OCTET_STRING_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
1937 const asn_encoding_constraints_t *constraints,
1938 size_t max_length) {
Lev Walkind84f6032017-10-03 16:33:59 -07001939 const asn_OCTET_STRING_specifics_t *specs = td->specifics
1940 ? (const asn_OCTET_STRING_specifics_t *)td->specifics
Lev Walkina5972be2017-09-29 23:15:58 -07001941 : &asn_SPC_OCTET_STRING_specs;
1942 asn_random_fill_result_t result_ok = {ARFILL_OK, 1};
1943 asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0};
1944 asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0};
Lev Walkina5972be2017-09-29 23:15:58 -07001945 unsigned int unit_bytes = 1;
1946 unsigned long clb = 0; /* Lower bound on char */
1947 unsigned long cub = 255; /* Higher bound on char value */
1948 uint8_t *buf;
1949 uint8_t *bend;
1950 uint8_t *b;
1951 size_t rnd_len;
1952 OCTET_STRING_t *st;
1953
Lev Walkin349b3402017-10-14 00:23:09 -07001954 if(max_length == 0 && !*sptr) return result_skipped;
Lev Walkina5972be2017-09-29 23:15:58 -07001955
1956 switch(specs->subvariant) {
1957 default:
1958 case ASN_OSUBV_ANY:
1959 return result_failed;
1960 case ASN_OSUBV_BIT:
1961 /* Handled by BIT_STRING itself. */
1962 return result_failed;
Lev Walkina5b02882017-10-01 22:48:44 -07001963 case ASN_OSUBV_STR:
Lev Walkina5972be2017-09-29 23:15:58 -07001964 unit_bytes = 1;
1965 clb = 0;
1966 cub = 255;
Lev Walkina5b02882017-10-01 22:48:44 -07001967 break;
1968 case ASN_OSUBV_U16:
Lev Walkina5972be2017-09-29 23:15:58 -07001969 unit_bytes = 2;
1970 clb = 0;
1971 cub = 65535;
Lev Walkina5b02882017-10-01 22:48:44 -07001972 break;
1973 case ASN_OSUBV_U32:
Lev Walkina5972be2017-09-29 23:15:58 -07001974 unit_bytes = 4;
1975 clb = 0;
1976 cub = 0x10FFFF;
Lev Walkina5b02882017-10-01 22:48:44 -07001977 break;
1978 }
Lev Walkina5972be2017-09-29 23:15:58 -07001979
1980 if(!constraints) constraints = &td->encoding_constraints;
1981 if(constraints->per_constraints) {
1982 const asn_per_constraint_t *pc =
1983 &td->encoding_constraints.per_constraints->value;
1984 if(pc->flags & APC_SEMI_CONSTRAINED) {
1985 clb = pc->lower_bound;
1986 } else if(pc->flags & APC_CONSTRAINED) {
1987 clb = pc->lower_bound;
1988 cub = pc->upper_bound;
1989 }
1990 }
1991
Lev Walkin349b3402017-10-14 00:23:09 -07001992 rnd_len =
1993 OCTET_STRING_random_length_constrained(td, constraints, max_length);
Lev Walkina5972be2017-09-29 23:15:58 -07001994
1995 buf = CALLOC(unit_bytes, rnd_len + 1);
1996 if(!buf) return result_failed;
1997
1998 bend = &buf[unit_bytes * rnd_len];
1999
2000 switch(unit_bytes) {
2001 case 1:
2002 for(b = buf; b < bend; b += unit_bytes) {
2003 *(uint8_t *)b = OCTET_STRING__random_char(clb, cub);
2004 }
2005 *(uint8_t *)b = 0;
2006 break;
2007 case 2:
2008 for(b = buf; b < bend; b += unit_bytes) {
Lev Walkin22cef6c2017-10-16 20:33:07 -07002009 uint32_t code = OCTET_STRING__random_char(clb, cub);
2010 b[0] = code >> 8;
2011 b[1] = code;
Lev Walkina5972be2017-09-29 23:15:58 -07002012 }
2013 *(uint16_t *)b = 0;
2014 break;
2015 case 4:
2016 for(b = buf; b < bend; b += unit_bytes) {
Lev Walkin22cef6c2017-10-16 20:33:07 -07002017 uint32_t code = OCTET_STRING__random_char(clb, cub);
2018 b[0] = code >> 24;
2019 b[1] = code >> 16;
2020 b[2] = code >> 8;
2021 b[3] = code;
Lev Walkina5972be2017-09-29 23:15:58 -07002022 }
2023 *(uint32_t *)b = 0;
2024 break;
2025 }
2026
2027 if(*sptr) {
2028 st = *sptr;
2029 FREEMEM(st->buf);
2030 } else {
2031 st = (OCTET_STRING_t *)(*sptr = CALLOC(1, specs->struct_size));
2032 if(!st) {
2033 FREEMEM(buf);
2034 return result_failed;
2035 }
2036 }
2037
2038 st->buf = buf;
2039 st->size = unit_bytes * rnd_len;
2040
2041 result_ok.length = st->size;
2042 return result_ok;
2043}