blob: f2270776693f70c191d74fd7251b9e3ba5d1aef5 [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 Walkinde4825d2004-09-29 13:20:14 +0000181 asn_TYPE_descriptor_t *td,
Lev Walkin59b176e2005-11-26 11:25:14 +0000182 void **sptr, const void *buf_ptr, size_t size, int tag_mode) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000183 asn_OCTET_STRING_specifics_t *specs = td->specifics
Lev Walkindc06f6b2004-10-20 15:50:55 +0000184 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +0800185 : &asn_SPC_OCTET_STRING_specs;
Lev Walkin59b176e2005-11-26 11:25:14 +0000186 BIT_STRING_t *st = (BIT_STRING_t *)*sptr;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000187 asn_dec_rval_t rval;
Lev Walkinde4825d2004-09-29 13:20:14 +0000188 asn_struct_ctx_t *ctx;
Lev Walkinf15320b2004-06-03 03:38:44 +0000189 ssize_t consumed_myself = 0;
Lev Walkinbbd93252004-10-12 05:57:23 +0000190 struct _stack *stck; /* Expectations stack structure */
Lev Walkin5c915992004-09-27 20:54:06 +0000191 struct _stack_el *sel = 0; /* Stack element */
Lev Walkinf15320b2004-06-03 03:38:44 +0000192 int tlv_constr;
Lev Walkin3a4689a2006-11-24 11:20:27 +0000193 enum asn_OS_Subvariant type_variant = specs->subvariant;
Lev Walkinf15320b2004-06-03 03:38:44 +0000194
Lev Walkincc6a9102004-09-23 22:06:26 +0000195 ASN_DEBUG("Decoding %s as %s (frame %ld)",
196 td->name,
Lev Walkin3a4689a2006-11-24 11:20:27 +0000197 (type_variant == ASN_OSUBV_STR) ?
Lev Walkinbbd93252004-10-12 05:57:23 +0000198 "OCTET STRING" : "OS-SpecialCase",
Lev Walkincc6a9102004-09-23 22:06:26 +0000199 (long)size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000200
201 /*
202 * Create the string if does not exist.
203 */
204 if(st == NULL) {
Lev Walkin59b176e2005-11-26 11:25:14 +0000205 st = (BIT_STRING_t *)(*sptr = CALLOC(1, specs->struct_size));
206 if(st == NULL) RETURN(RC_FAIL);
Lev Walkinf15320b2004-06-03 03:38:44 +0000207 }
208
209 /* Restore parsing context */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800210 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000211
212 switch(ctx->phase) {
213 case 0:
214 /*
215 * Check tags.
216 */
Lev Walkinde4825d2004-09-29 13:20:14 +0000217 rval = ber_check_tags(opt_codec_ctx, td, ctx,
Lev Walkin8e8078a2004-09-26 13:10:40 +0000218 buf_ptr, size, tag_mode, -1,
Lev Walkinf15320b2004-06-03 03:38:44 +0000219 &ctx->left, &tlv_constr);
Lev Walkin443d2512004-10-05 06:35:31 +0000220 if(rval.code != RC_OK)
221 return rval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000222
Lev Walkinf15320b2004-06-03 03:38:44 +0000223 if(tlv_constr) {
224 /*
225 * Complex operation, requires stack of expectations.
226 */
227 ctx->ptr = _new_stack();
Lev Walkinb02145b2017-06-28 08:52:07 -0700228 if(!ctx->ptr) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000229 RETURN(RC_FAIL);
230 }
231 } else {
232 /*
233 * Jump into stackless primitive decoding.
234 */
235 _CH_PHASE(ctx, 3);
Lev Walkin3a4689a2006-11-24 11:20:27 +0000236 if(type_variant == ASN_OSUBV_ANY && tag_mode != 1)
Lev Walkin188ed2c2004-09-13 08:31:01 +0000237 APPEND(buf_ptr, rval.consumed);
Lev Walkinf15320b2004-06-03 03:38:44 +0000238 ADVANCE(rval.consumed);
239 goto phase3;
240 }
241
Lev Walkinf15320b2004-06-03 03:38:44 +0000242 NEXT_PHASE(ctx);
243 /* Fall through */
244 case 1:
245 phase1:
246 /*
247 * Fill the stack with expectations.
248 */
Lev Walkin814cca72004-12-15 23:23:53 +0000249 stck = (struct _stack *)ctx->ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000250 sel = stck->cur_ptr;
251 do {
252 ber_tlv_tag_t tlv_tag;
253 ber_tlv_len_t tlv_len;
Lev Walkin188ed2c2004-09-13 08:31:01 +0000254 ber_tlv_tag_t expected_tag;
Lev Walkin5c915992004-09-27 20:54:06 +0000255 ssize_t tl, ll, tlvl;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000256 /* This one works even if (sel->left == -1) */
Simo Sorcecd943c22016-07-25 04:39:40 -0400257 size_t Left = ((!sel||(size_t)sel->left >= size)
258 ?size:(size_t)sel->left);
Lev Walkinf15320b2004-06-03 03:38:44 +0000259
Lev Walkin3990ba62004-09-24 20:57:41 +0000260
Lev Walkinabf68892004-10-26 10:12:14 +0000261 ASN_DEBUG("%p, s->l=%ld, s->wn=%ld, s->g=%ld\n", sel,
Lev Walkin1e3ccbb2004-10-26 10:56:10 +0000262 (long)(sel?sel->left:0),
263 (long)(sel?sel->want_nulls:0),
264 (long)(sel?sel->got:0)
Lev Walkin5c915992004-09-27 20:54:06 +0000265 );
266 if(sel && sel->left <= 0 && sel->want_nulls == 0) {
267 if(sel->prev) {
268 struct _stack_el *prev = sel->prev;
269 if(prev->left != -1) {
270 if(prev->left < sel->got)
271 RETURN(RC_FAIL);
272 prev->left -= sel->got;
273 }
274 prev->got += sel->got;
275 sel = stck->cur_ptr = prev;
276 if(!sel) break;
277 tlv_constr = 1;
278 continue;
279 } else {
280 sel = stck->cur_ptr = 0;
281 break; /* Nothing to wait */
282 }
283 }
284
Lev Walkin3990ba62004-09-24 20:57:41 +0000285 tl = ber_fetch_tag(buf_ptr, Left, &tlv_tag);
Lev Walkinabf68892004-10-26 10:12:14 +0000286 ASN_DEBUG("fetch tag(size=%ld,L=%ld), %sstack, left=%ld, wn=%ld, tl=%ld",
287 (long)size, (long)Left, sel?"":"!",
Lev Walkin1e3ccbb2004-10-26 10:56:10 +0000288 (long)(sel?sel->left:0),
289 (long)(sel?sel->want_nulls:0),
Lev Walkinabf68892004-10-26 10:12:14 +0000290 (long)tl);
Lev Walkinf15320b2004-06-03 03:38:44 +0000291 switch(tl) {
292 case -1: RETURN(RC_FAIL);
293 case 0: RETURN(RC_WMORE);
294 }
295
296 tlv_constr = BER_TLV_CONSTRUCTED(buf_ptr);
297
298 ll = ber_fetch_length(tlv_constr,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000299 (const char *)buf_ptr + tl,Left - tl,&tlv_len);
Lev Walkina6a926a2005-03-02 01:54:28 +0000300 ASN_DEBUG("Got tag=%s, tc=%d, left=%ld, tl=%ld, len=%ld, ll=%ld",
Lev Walkin8d127872004-09-04 04:44:50 +0000301 ber_tlv_tag_string(tlv_tag), tlv_constr,
Lev Walkina6a926a2005-03-02 01:54:28 +0000302 (long)Left, (long)tl, (long)tlv_len, (long)ll);
Lev Walkinf15320b2004-06-03 03:38:44 +0000303 switch(ll) {
304 case -1: RETURN(RC_FAIL);
305 case 0: RETURN(RC_WMORE);
306 }
307
Lev Walkin8d127872004-09-04 04:44:50 +0000308 if(sel && sel->want_nulls
Lev Walkin8c3b8542005-03-10 18:52:02 +0000309 && ((const uint8_t *)buf_ptr)[0] == 0
310 && ((const uint8_t *)buf_ptr)[1] == 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000311 {
Lev Walkin8d127872004-09-04 04:44:50 +0000312
313 ASN_DEBUG("Eat EOC; wn=%d--", sel->want_nulls);
314
Lev Walkin3a4689a2006-11-24 11:20:27 +0000315 if(type_variant == ASN_OSUBV_ANY
Lev Walkin07f388c2004-10-11 11:43:08 +0000316 && (tag_mode != 1 || sel->cont_level))
317 APPEND("\0\0", 2);
Lev Walkin5c915992004-09-27 20:54:06 +0000318
319 ADVANCE(2);
320 sel->got += 2;
321 if(sel->left != -1) {
322 sel->left -= 2; /* assert(sel->left >= 2) */
323 }
324
Lev Walkinf15320b2004-06-03 03:38:44 +0000325 sel->want_nulls--;
326 if(sel->want_nulls == 0) {
327 /* Move to the next expectation */
Lev Walkin5c915992004-09-27 20:54:06 +0000328 sel->left = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000329 tlv_constr = 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000330 }
Lev Walkin8d127872004-09-04 04:44:50 +0000331
332 continue;
Lev Walkin188ed2c2004-09-13 08:31:01 +0000333 }
334
335 /*
336 * Set up expected tags,
337 * depending on ASN.1 type being decoded.
338 */
Lev Walkinbbd93252004-10-12 05:57:23 +0000339 switch(type_variant) {
Lev Walkin3a4689a2006-11-24 11:20:27 +0000340 case ASN_OSUBV_BIT:
Lev Walkin188ed2c2004-09-13 08:31:01 +0000341 /* X.690: 8.6.4.1, NOTE 2 */
342 /* Fall through */
Lev Walkin3a4689a2006-11-24 11:20:27 +0000343 case ASN_OSUBV_STR:
Lev Walkin188ed2c2004-09-13 08:31:01 +0000344 default:
345 if(sel) {
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",
498 sel->prev, sel->want_nulls,
499 (long)sel->left, (long)sel->got, (long)size);
500 if(sel->prev || sel->want_nulls > 1 || sel->left > 0) {
501 RETURN(RC_WMORE);
502 }
503 }
504
Lev Walkinf15320b2004-06-03 03:38:44 +0000505 /*
506 * BIT STRING-specific processing.
507 */
Lev 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 Walkinbbd93252004-10-12 05:57:23 +0000535OCTET_STRING_encode_der(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000536 int tag_mode, ber_tlv_tag_t tag,
537 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000538 asn_enc_rval_t er;
539 asn_OCTET_STRING_specifics_t *specs = td->specifics
Lev Walkindc06f6b2004-10-20 15:50:55 +0000540 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +0800541 : &asn_SPC_OCTET_STRING_specs;
Lev Walkinbbd93252004-10-12 05:57:23 +0000542 BIT_STRING_t *st = (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 Walkinde4825d2004-09-29 13:20:14 +0000597OCTET_STRING_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000598 int ilevel, enum xer_encoder_flags_e flags,
599 asn_app_consume_bytes_f *cb, void *app_key) {
Wim Lewis59e8d282014-08-04 12:39:35 -0700600 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 Walkindc06f6b2004-10-20 15:50:55 +0000749OCTET_STRING_encode_xer_utf8(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000750 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;
753 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 Walkin0fab1a62005-03-09 22:19:25 +0000977static 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 +0000978 OCTET_STRING_t *st = (OCTET_STRING_t *)sptr;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000979 const char *p = (const char *)chunk_buf;
980 const char *pend = p + chunk_size;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000981 uint8_t *buf;
982
983 /* Reallocate buffer */
Lev Walkin494fb702017-08-07 20:07:00 -0700984 size_t new_size = st->size + chunk_size;
985 void *nptr = REALLOC(st->buf, new_size + 1);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000986 if(!nptr) return -1;
987 st->buf = (uint8_t *)nptr;
988 buf = st->buf + st->size;
989
990 /*
991 * Convert series of 0 and 1 into the octet string.
992 */
993 for(; p < pend; p++) {
Lev Walkin0fab1a62005-03-09 22:19:25 +0000994 int ch = *(const unsigned char *)p;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000995 int len; /* Length of the rest of the chunk */
996
997 if(ch != 0x26 /* '&' */) {
998 *buf++ = ch;
999 continue; /* That was easy... */
1000 }
1001
1002 /*
1003 * Process entity reference.
1004 */
Lev Walkin0fab1a62005-03-09 22:19:25 +00001005 len = chunk_size - (p - (const char *)chunk_buf);
Lev Walkindc06f6b2004-10-20 15:50:55 +00001006 if(len == 1 /* "&" */) goto want_more;
1007 if(p[1] == 0x23 /* '#' */) {
Lev Walkin0fab1a62005-03-09 22:19:25 +00001008 const char *pval; /* Pointer to start of digits */
Lev Walkinbc0dce32006-07-27 09:56:13 +00001009 int32_t val = 0; /* Entity reference value */
Lev Walkindc06f6b2004-10-20 15:50:55 +00001010 int base;
1011
1012 if(len == 2 /* "&#" */) goto want_more;
1013 if(p[2] == 0x78 /* 'x' */)
1014 pval = p + 3, base = 16;
1015 else
1016 pval = p + 2, base = 10;
1017 len = OS__strtoent(base, pval, p + len, &val);
1018 if(len == -1) {
1019 /* Invalid charset. Just copy verbatim. */
1020 *buf++ = ch;
1021 continue;
1022 }
1023 if(!len || pval[len-1] != 0x3b) goto want_more;
1024 assert(val > 0);
1025 p += (pval - p) + len - 1; /* Advance past entref */
1026
1027 if(val < 0x80) {
1028 *buf++ = (char)val;
1029 } else if(val < 0x800) {
1030 *buf++ = 0xc0 | ((val >> 6));
1031 *buf++ = 0x80 | ((val & 0x3f));
1032 } else if(val < 0x10000) {
1033 *buf++ = 0xe0 | ((val >> 12));
1034 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1035 *buf++ = 0x80 | ((val & 0x3f));
1036 } else if(val < 0x200000) {
1037 *buf++ = 0xf0 | ((val >> 18));
1038 *buf++ = 0x80 | ((val >> 12) & 0x3f);
1039 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1040 *buf++ = 0x80 | ((val & 0x3f));
1041 } else if(val < 0x4000000) {
1042 *buf++ = 0xf8 | ((val >> 24));
1043 *buf++ = 0x80 | ((val >> 18) & 0x3f);
1044 *buf++ = 0x80 | ((val >> 12) & 0x3f);
1045 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1046 *buf++ = 0x80 | ((val & 0x3f));
1047 } else {
1048 *buf++ = 0xfc | ((val >> 30) & 0x1);
1049 *buf++ = 0x80 | ((val >> 24) & 0x3f);
1050 *buf++ = 0x80 | ((val >> 18) & 0x3f);
1051 *buf++ = 0x80 | ((val >> 12) & 0x3f);
1052 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1053 *buf++ = 0x80 | ((val & 0x3f));
1054 }
1055 } else {
1056 /*
1057 * Ugly, limited parsing of &amp; &gt; &lt;
1058 */
1059 char *sc = (char *)memchr(p, 0x3b, len > 5 ? 5 : len);
1060 if(!sc) goto want_more;
1061 if((sc - p) == 4
1062 && p[1] == 0x61 /* 'a' */
1063 && p[2] == 0x6d /* 'm' */
1064 && p[3] == 0x70 /* 'p' */) {
1065 *buf++ = 0x26;
1066 p = sc;
1067 continue;
1068 }
1069 if((sc - p) == 3) {
1070 if(p[1] == 0x6c) {
1071 *buf = 0x3c; /* '<' */
1072 } else if(p[1] == 0x67) {
1073 *buf = 0x3e; /* '>' */
1074 } else {
1075 /* Unsupported entity reference */
1076 *buf++ = ch;
1077 continue;
1078 }
1079 if(p[2] != 0x74) {
1080 /* Unsupported entity reference */
1081 *buf++ = ch;
1082 continue;
1083 }
1084 buf++;
1085 p = sc;
1086 continue;
1087 }
1088 /* Unsupported entity reference */
1089 *buf++ = ch;
1090 }
1091
1092 continue;
1093 want_more:
1094 if(have_more) {
1095 /*
1096 * We know that no more data (of the same type)
1097 * is coming. Copy the rest verbatim.
1098 */
1099 *buf++ = ch;
1100 continue;
1101 }
Lev Walkin0fab1a62005-03-09 22:19:25 +00001102 chunk_size = (p - (const char *)chunk_buf);
Lev Walkindc06f6b2004-10-20 15:50:55 +00001103 /* Processing stalled: need more data */
Lev Walkinbdaae772005-02-18 12:25:47 +00001104 break;
Lev Walkindc06f6b2004-10-20 15:50:55 +00001105 }
1106
1107 st->size = buf - st->buf;
Lev Walkin494fb702017-08-07 20:07:00 -07001108 assert(st->size <= new_size);
Lev Walkindc06f6b2004-10-20 15:50:55 +00001109 st->buf[st->size] = 0; /* Courtesy termination */
1110
1111 return chunk_size; /* Converted in full */
1112}
1113
1114/*
1115 * Decode OCTET STRING from the XML element's body.
1116 */
1117static asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -07001118OCTET_STRING__decode_xer(const asn_codec_ctx_t *opt_codec_ctx,
Lev Walkindc06f6b2004-10-20 15:50:55 +00001119 asn_TYPE_descriptor_t *td, void **sptr,
Lev Walkin8c3b8542005-03-10 18:52:02 +00001120 const char *opt_mname, const void *buf_ptr, size_t size,
Lev Walkindc06f6b2004-10-20 15:50:55 +00001121 int (*opt_unexpected_tag_decoder)
Lev Walkin0fab1a62005-03-09 22:19:25 +00001122 (void *struct_ptr, const void *chunk_buf, size_t chunk_size),
Lev Walkindc06f6b2004-10-20 15:50:55 +00001123 ssize_t (*body_receiver)
Lev Walkin0fab1a62005-03-09 22:19:25 +00001124 (void *struct_ptr, const void *chunk_buf, size_t chunk_size,
Lev Walkindc06f6b2004-10-20 15:50:55 +00001125 int have_more)
1126) {
Lev Walkind5125642005-02-14 20:08:00 +00001127 OCTET_STRING_t *st = (OCTET_STRING_t *)*sptr;
Lev Walkindc06f6b2004-10-20 15:50:55 +00001128 asn_OCTET_STRING_specifics_t *specs = td->specifics
1129 ? (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 Walkindc06f6b2004-10-20 15:50:55 +00001179 asn_TYPE_descriptor_t *td, void **sptr,
Lev Walkin8c3b8542005-03-10 18:52:02 +00001180 const char *opt_mname, const void *buf_ptr, size_t size) {
Lev Walkindc06f6b2004-10-20 15:50:55 +00001181 return OCTET_STRING__decode_xer(opt_codec_ctx, td, sptr, opt_mname,
1182 buf_ptr, size, 0, OCTET_STRING__convert_hexadecimal);
1183}
1184
1185/*
1186 * Decode OCTET STRING from the binary (0/1) data.
1187 */
1188asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -07001189OCTET_STRING_decode_xer_binary(const asn_codec_ctx_t *opt_codec_ctx,
Lev Walkindc06f6b2004-10-20 15:50:55 +00001190 asn_TYPE_descriptor_t *td, void **sptr,
Lev Walkin8c3b8542005-03-10 18:52:02 +00001191 const char *opt_mname, const void *buf_ptr, size_t size) {
Lev Walkindc06f6b2004-10-20 15:50:55 +00001192 return OCTET_STRING__decode_xer(opt_codec_ctx, td, sptr, opt_mname,
1193 buf_ptr, size, 0, OCTET_STRING__convert_binary);
1194}
1195
1196/*
1197 * Decode OCTET STRING from the string (ASCII/UTF-8) data.
1198 */
1199asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -07001200OCTET_STRING_decode_xer_utf8(const asn_codec_ctx_t *opt_codec_ctx,
Lev Walkindc06f6b2004-10-20 15:50:55 +00001201 asn_TYPE_descriptor_t *td, void **sptr,
Lev Walkin8c3b8542005-03-10 18:52:02 +00001202 const char *opt_mname, const void *buf_ptr, size_t size) {
Lev Walkindc06f6b2004-10-20 15:50:55 +00001203 return OCTET_STRING__decode_xer(opt_codec_ctx, td, sptr, opt_mname,
1204 buf_ptr, size,
1205 OCTET_STRING__handle_control_chars,
1206 OCTET_STRING__convert_entrefs);
1207}
1208
Lev Walkin0368c622017-09-18 00:33:16 -07001209#ifndef ASN_DISABLE_PER_SUPPORT
1210
Lev Walkin725883b2006-10-09 12:07:58 +00001211static int
Lev Walkin3a4689a2006-11-24 11:20:27 +00001212OCTET_STRING_per_get_characters(asn_per_data_t *po, uint8_t *buf,
1213 size_t units, unsigned int bpc, unsigned int unit_bits,
Lev Walkin494fb702017-08-07 20:07:00 -07001214 long lb, long ub, const asn_per_constraints_t *pc) {
Lev Walkin3a4689a2006-11-24 11:20:27 +00001215 uint8_t *end = buf + units * bpc;
Lev Walkin725883b2006-10-09 12:07:58 +00001216
1217 ASN_DEBUG("Expanding %d characters into (%ld..%ld):%d",
Lev Walkin3a4689a2006-11-24 11:20:27 +00001218 (int)units, lb, ub, unit_bits);
Lev Walkin725883b2006-10-09 12:07:58 +00001219
1220 /* X.691: 27.5.4 */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001221 if((unsigned long)ub <= ((unsigned long)2 << (unit_bits - 1))) {
Lev Walkin725883b2006-10-09 12:07:58 +00001222 /* Decode without translation */
1223 lb = 0;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001224 } else if(pc && pc->code2value) {
1225 if(unit_bits > 16)
1226 return 1; /* FATAL: can't have constrained
1227 * UniversalString with more than
1228 * 16 million code points */
1229 for(; buf < end; buf += bpc) {
Lev Walkin725883b2006-10-09 12:07:58 +00001230 int value;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001231 int code = per_get_few_bits(po, unit_bits);
Lev Walkin725883b2006-10-09 12:07:58 +00001232 if(code < 0) return -1; /* WMORE */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001233 value = pc->code2value(code);
Lev Walkin725883b2006-10-09 12:07:58 +00001234 if(value < 0) {
1235 ASN_DEBUG("Code %d (0x%02x) is"
1236 " not in map (%ld..%ld)",
1237 code, code, lb, ub);
1238 return 1; /* FATAL */
1239 }
Lev Walkin3a4689a2006-11-24 11:20:27 +00001240 switch(bpc) {
1241 case 1: *buf = value; break;
1242 case 2: buf[0] = value >> 8; buf[1] = value; break;
1243 case 4: buf[0] = value >> 24; buf[1] = value >> 16;
1244 buf[2] = value >> 8; buf[3] = value; break;
1245 }
Lev Walkin725883b2006-10-09 12:07:58 +00001246 }
1247 return 0;
1248 }
1249
Lev Walkin3a4689a2006-11-24 11:20:27 +00001250 /* Shortcut the no-op copying to the aligned structure */
1251 if(lb == 0 && (unit_bits == 8 * bpc)) {
1252 return per_get_many_bits(po, buf, 0, unit_bits * units);
1253 }
1254
1255 for(; buf < end; buf += bpc) {
Lev Walkin5d947a82017-10-03 01:04:03 -07001256 int32_t code = per_get_few_bits(po, unit_bits);
1257 int32_t ch = code + lb;
Lev Walkin725883b2006-10-09 12:07:58 +00001258 if(code < 0) return -1; /* WMORE */
1259 if(ch > ub) {
1260 ASN_DEBUG("Code %d is out of range (%ld..%ld)",
1261 ch, lb, ub);
1262 return 1; /* FATAL */
1263 }
Lev Walkin3a4689a2006-11-24 11:20:27 +00001264 switch(bpc) {
1265 case 1: *buf = ch; break;
1266 case 2: buf[0] = ch >> 8; buf[1] = ch; break;
1267 case 4: buf[0] = ch >> 24; buf[1] = ch >> 16;
1268 buf[2] = ch >> 8; buf[3] = ch; break;
1269 }
Lev Walkin725883b2006-10-09 12:07:58 +00001270 }
1271
1272 return 0;
1273}
1274
1275static int
Lev Walkin3a4689a2006-11-24 11:20:27 +00001276OCTET_STRING_per_put_characters(asn_per_outp_t *po, const uint8_t *buf,
1277 size_t units, unsigned int bpc, unsigned int unit_bits,
Lev Walkin494fb702017-08-07 20:07:00 -07001278 long lb, long ub, const asn_per_constraints_t *pc) {
Lev Walkin3a4689a2006-11-24 11:20:27 +00001279 const uint8_t *end = buf + units * bpc;
Lev Walkin725883b2006-10-09 12:07:58 +00001280
Lev Walkin3a4689a2006-11-24 11:20:27 +00001281 ASN_DEBUG("Squeezing %d characters into (%ld..%ld):%d (%d bpc)",
1282 (int)units, lb, ub, unit_bits, bpc);
Lev Walkin725883b2006-10-09 12:07:58 +00001283
1284 /* X.691: 27.5.4 */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001285 if((unsigned long)ub <= ((unsigned long)2 << (unit_bits - 1))) {
Lev Walkin725883b2006-10-09 12:07:58 +00001286 /* Encode as is */
1287 lb = 0;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001288 } else if(pc && pc->value2code) {
1289 for(; buf < end; buf += bpc) {
1290 int code;
1291 uint32_t value;
1292 switch(bpc) {
1293 case 1: value = *(const uint8_t *)buf; break;
1294 case 2: value = (buf[0] << 8) | buf[1]; break;
1295 case 4: value = (buf[0] << 24) | (buf[1] << 16)
1296 | (buf[2] << 8) | buf[3]; break;
1297 default: return -1;
1298 }
1299 code = pc->value2code(value);
Lev Walkin725883b2006-10-09 12:07:58 +00001300 if(code < 0) {
1301 ASN_DEBUG("Character %d (0x%02x) is"
1302 " not in map (%ld..%ld)",
1303 *buf, *buf, lb, ub);
1304 return -1;
1305 }
1306 if(per_put_few_bits(po, code, unit_bits))
1307 return -1;
1308 }
1309 }
1310
Lev Walkin3a4689a2006-11-24 11:20:27 +00001311 /* Shortcut the no-op copying to the aligned structure */
1312 if(lb == 0 && (unit_bits == 8 * bpc)) {
1313 return per_put_many_bits(po, buf, unit_bits * units);
1314 }
1315
1316 for(ub -= lb; buf < end; buf += bpc) {
1317 int ch;
1318 uint32_t value;
1319 switch(bpc) {
1320 case 1: value = *(const uint8_t *)buf; break;
1321 case 2: value = (buf[0] << 8) | buf[1]; break;
1322 case 4: value = (buf[0] << 24) | (buf[1] << 16)
1323 | (buf[2] << 8) | buf[3]; break;
1324 default: return -1;
1325 }
1326 ch = value - lb;
Lev Walkin725883b2006-10-09 12:07:58 +00001327 if(ch < 0 || ch > ub) {
1328 ASN_DEBUG("Character %d (0x%02x)"
1329 " is out of range (%ld..%ld)",
1330 *buf, *buf, lb, ub + lb);
1331 return -1;
1332 }
1333 if(per_put_few_bits(po, ch, unit_bits))
1334 return -1;
1335 }
1336
1337 return 0;
1338}
1339
Lev Walkin0368c622017-09-18 00:33:16 -07001340static asn_per_constraints_t asn_DEF_OCTET_STRING_constraints = {
1341 { APC_CONSTRAINED, 8, 8, 0, 255 },
1342 { APC_SEMI_CONSTRAINED, -1, -1, 0, 0 },
1343 0, 0
1344};
Lev Walkin62d76872017-08-05 22:49:42 -07001345
Lev Walkin59b176e2005-11-26 11:25:14 +00001346asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -07001347OCTET_STRING_decode_uper(const asn_codec_ctx_t *opt_codec_ctx,
Lev Walkin494fb702017-08-07 20:07:00 -07001348 asn_TYPE_descriptor_t *td,
1349 const asn_per_constraints_t *constraints, void **sptr,
1350 asn_per_data_t *pd) {
1351 asn_OCTET_STRING_specifics_t *specs = td->specifics
Lev Walkin59b176e2005-11-26 11:25:14 +00001352 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +08001353 : &asn_SPC_OCTET_STRING_specs;
Lev Walkin494fb702017-08-07 20:07:00 -07001354 const asn_per_constraints_t *pc =
Lev Walkina5972be2017-09-29 23:15:58 -07001355 constraints ? constraints : td->encoding_constraints.per_constraints;
Lev Walkin494fb702017-08-07 20:07:00 -07001356 const asn_per_constraint_t *cval;
1357 const asn_per_constraint_t *csiz;
Lev Walkin59b176e2005-11-26 11:25:14 +00001358 asn_dec_rval_t rval = { RC_OK, 0 };
Lev Walkina5b02882017-10-01 22:48:44 -07001359 OCTET_STRING_t *st = (OCTET_STRING_t *)*sptr;
Lev Walkin59b176e2005-11-26 11:25:14 +00001360 ssize_t consumed_myself = 0;
1361 int repeat;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001362 enum {
Lev Walkin3a4689a2006-11-24 11:20:27 +00001363 OS__BPC_CHAR = 1,
1364 OS__BPC_U16 = 2,
1365 OS__BPC_U32 = 4
1366 } bpc; /* Bytes per character */
1367 unsigned int unit_bits;
1368 unsigned int canonical_unit_bits;
Lev Walkin59b176e2005-11-26 11:25:14 +00001369
1370 (void)opt_codec_ctx;
1371
Lev Walkin3a4689a2006-11-24 11:20:27 +00001372 if(pc) {
1373 cval = &pc->value;
1374 csiz = &pc->size;
1375 } else {
1376 cval = &asn_DEF_OCTET_STRING_constraints.value;
1377 csiz = &asn_DEF_OCTET_STRING_constraints.size;
1378 }
1379
1380 switch(specs->subvariant) {
1381 default:
1382 case ASN_OSUBV_ANY:
Lev Walkina5b02882017-10-01 22:48:44 -07001383 case ASN_OSUBV_BIT:
Lev Walkin3a4689a2006-11-24 11:20:27 +00001384 ASN_DEBUG("Unrecognized subvariant %d", specs->subvariant);
1385 RETURN(RC_FAIL);
Lev Walkin3a4689a2006-11-24 11:20:27 +00001386 break;
1387 case ASN_OSUBV_STR:
1388 canonical_unit_bits = unit_bits = 8;
1389 if(cval->flags & APC_CONSTRAINED)
1390 unit_bits = cval->range_bits;
1391 bpc = OS__BPC_CHAR;
1392 break;
1393 case ASN_OSUBV_U16:
1394 canonical_unit_bits = unit_bits = 16;
1395 if(cval->flags & APC_CONSTRAINED)
1396 unit_bits = cval->range_bits;
1397 bpc = OS__BPC_U16;
1398 break;
1399 case ASN_OSUBV_U32:
1400 canonical_unit_bits = unit_bits = 32;
1401 if(cval->flags & APC_CONSTRAINED)
1402 unit_bits = cval->range_bits;
1403 bpc = OS__BPC_U32;
1404 break;
1405 }
1406
Lev Walkin59b176e2005-11-26 11:25:14 +00001407 /*
1408 * Allocate the string.
1409 */
1410 if(!st) {
Lev Walkina5b02882017-10-01 22:48:44 -07001411 st = (OCTET_STRING_t *)(*sptr = CALLOC(1, specs->struct_size));
Lev Walkin59b176e2005-11-26 11:25:14 +00001412 if(!st) RETURN(RC_FAIL);
1413 }
1414
Lev Walkin725883b2006-10-09 12:07:58 +00001415 ASN_DEBUG("PER Decoding %s size %ld .. %ld bits %d",
Lev Walkin3a4689a2006-11-24 11:20:27 +00001416 csiz->flags & APC_EXTENSIBLE ? "extensible" : "non-extensible",
1417 csiz->lower_bound, csiz->upper_bound, csiz->effective_bits);
Lev Walkince676dd2005-12-20 22:34:55 +00001418
Lev Walkin3a4689a2006-11-24 11:20:27 +00001419 if(csiz->flags & APC_EXTENSIBLE) {
Lev Walkin59b176e2005-11-26 11:25:14 +00001420 int inext = per_get_few_bits(pd, 1);
Lev Walkin0a8aa602006-09-18 20:05:55 +00001421 if(inext < 0) RETURN(RC_WMORE);
Lev Walkin3a4689a2006-11-24 11:20:27 +00001422 if(inext) {
1423 csiz = &asn_DEF_OCTET_STRING_constraints.size;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001424 unit_bits = canonical_unit_bits;
1425 }
Lev Walkin59b176e2005-11-26 11:25:14 +00001426 }
1427
Lev Walkin3a4689a2006-11-24 11:20:27 +00001428 if(csiz->effective_bits >= 0) {
Lev Walkin59b176e2005-11-26 11:25:14 +00001429 FREEMEM(st->buf);
Lev Walkin3a4689a2006-11-24 11:20:27 +00001430 if(bpc) {
1431 st->size = csiz->upper_bound * bpc;
Lev Walkin59b176e2005-11-26 11:25:14 +00001432 } else {
Lev Walkin3a4689a2006-11-24 11:20:27 +00001433 st->size = (csiz->upper_bound + 7) >> 3;
Lev Walkin59b176e2005-11-26 11:25:14 +00001434 }
1435 st->buf = (uint8_t *)MALLOC(st->size + 1);
1436 if(!st->buf) { st->size = 0; RETURN(RC_FAIL); }
1437 }
1438
1439 /* X.691, #16.5: zero-length encoding */
1440 /* X.691, #16.6: short fixed length encoding (up to 2 octets) */
1441 /* X.691, #16.7: long fixed length encoding (up to 64K octets) */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001442 if(csiz->effective_bits == 0) {
Lev Walkin725883b2006-10-09 12:07:58 +00001443 int ret;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001444 if(bpc) {
1445 ASN_DEBUG("Encoding OCTET STRING size %ld",
1446 csiz->upper_bound);
1447 ret = OCTET_STRING_per_get_characters(pd, st->buf,
1448 csiz->upper_bound, bpc, unit_bits,
1449 cval->lower_bound, cval->upper_bound, pc);
Lev Walkin725883b2006-10-09 12:07:58 +00001450 if(ret > 0) RETURN(RC_FAIL);
1451 } else {
Lev Walkin3a4689a2006-11-24 11:20:27 +00001452 ASN_DEBUG("Encoding BIT STRING size %ld",
1453 csiz->upper_bound);
Lev Walkin725883b2006-10-09 12:07:58 +00001454 ret = per_get_many_bits(pd, st->buf, 0,
Lev Walkin3a4689a2006-11-24 11:20:27 +00001455 unit_bits * csiz->upper_bound);
Lev Walkin725883b2006-10-09 12:07:58 +00001456 }
Lev Walkin0a8aa602006-09-18 20:05:55 +00001457 if(ret < 0) RETURN(RC_WMORE);
Lev Walkin3a4689a2006-11-24 11:20:27 +00001458 consumed_myself += unit_bits * csiz->upper_bound;
Lev Walkin59b176e2005-11-26 11:25:14 +00001459 st->buf[st->size] = 0;
Lev Walkin59b176e2005-11-26 11:25:14 +00001460 RETURN(RC_OK);
1461 }
1462
1463 st->size = 0;
1464 do {
Lev Walkin3a4689a2006-11-24 11:20:27 +00001465 ssize_t raw_len;
Lev Walkin59b176e2005-11-26 11:25:14 +00001466 ssize_t len_bytes;
Lev Walkin59b176e2005-11-26 11:25:14 +00001467 void *p;
1468 int ret;
1469
1470 /* Get the PER length */
Lev Walkin9d1b45f2017-10-01 17:04:48 -07001471 raw_len = uper_get_length(pd, csiz->effective_bits, csiz->lower_bound,
1472 &repeat);
Lev Walkin3a4689a2006-11-24 11:20:27 +00001473 if(raw_len < 0) RETURN(RC_WMORE);
Lev Walkin5d947a82017-10-03 01:04:03 -07001474 if(raw_len == 0 && st->buf) break;
Lev Walkin59b176e2005-11-26 11:25:14 +00001475
Lev Walkin0a8aa602006-09-18 20:05:55 +00001476 ASN_DEBUG("Got PER length eb %ld, len %ld, %s (%s)",
Lev Walkin3a4689a2006-11-24 11:20:27 +00001477 (long)csiz->effective_bits, (long)raw_len,
Lev Walkin0a8aa602006-09-18 20:05:55 +00001478 repeat ? "repeat" : "once", td->name);
Lev Walkina5b02882017-10-01 22:48:44 -07001479 len_bytes = raw_len * bpc;
Lev Walkin59b176e2005-11-26 11:25:14 +00001480 p = REALLOC(st->buf, st->size + len_bytes + 1);
1481 if(!p) RETURN(RC_FAIL);
1482 st->buf = (uint8_t *)p;
1483
Lev Walkina5b02882017-10-01 22:48:44 -07001484 ret = OCTET_STRING_per_get_characters(pd, &st->buf[st->size], raw_len,
1485 bpc, unit_bits, cval->lower_bound,
1486 cval->upper_bound, pc);
1487 if(ret > 0) RETURN(RC_FAIL);
Lev Walkin0a8aa602006-09-18 20:05:55 +00001488 if(ret < 0) RETURN(RC_WMORE);
Lev Walkin59b176e2005-11-26 11:25:14 +00001489 st->size += len_bytes;
1490 } while(repeat);
1491 st->buf[st->size] = 0; /* nul-terminate */
1492
1493 return rval;
1494}
Lev Walkindc06f6b2004-10-20 15:50:55 +00001495
Lev Walkin523de9e2006-08-18 01:34:18 +00001496asn_enc_rval_t
1497OCTET_STRING_encode_uper(asn_TYPE_descriptor_t *td,
Lev Walkin494fb702017-08-07 20:07:00 -07001498 const asn_per_constraints_t *constraints, void *sptr,
1499 asn_per_outp_t *po) {
1500 asn_OCTET_STRING_specifics_t *specs = td->specifics
Lev Walkin523de9e2006-08-18 01:34:18 +00001501 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +08001502 : &asn_SPC_OCTET_STRING_specs;
Lev Walkin494fb702017-08-07 20:07:00 -07001503 const asn_per_constraints_t *pc = constraints ? constraints
Lev Walkina5972be2017-09-29 23:15:58 -07001504 : td->encoding_constraints.per_constraints;
Lev Walkin494fb702017-08-07 20:07:00 -07001505 const asn_per_constraint_t *cval;
1506 const asn_per_constraint_t *csiz;
Lev Walkina5b02882017-10-01 22:48:44 -07001507 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001508 asn_enc_rval_t er = { 0, 0, 0 };
Lev Walkin523de9e2006-08-18 01:34:18 +00001509 int inext = 0; /* Lies not within extension root */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001510 unsigned int unit_bits;
1511 unsigned int canonical_unit_bits;
Lev Walkin9d1b45f2017-10-01 17:04:48 -07001512 size_t size_in_units;
Lev Walkin523de9e2006-08-18 01:34:18 +00001513 const uint8_t *buf;
1514 int ret;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001515 enum {
Lev Walkin3a4689a2006-11-24 11:20:27 +00001516 OS__BPC_CHAR = 1,
1517 OS__BPC_U16 = 2,
1518 OS__BPC_U32 = 4
1519 } bpc; /* Bytes per character */
1520 int ct_extensible;
Lev Walkin523de9e2006-08-18 01:34:18 +00001521
Lev Walkin2ddd3082006-10-16 12:32:14 +00001522 if(!st || (!st->buf && st->size))
Lev Walkin7c1dc052016-03-14 03:08:15 -07001523 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001524
Lev Walkin3a4689a2006-11-24 11:20:27 +00001525 if(pc) {
1526 cval = &pc->value;
1527 csiz = &pc->size;
1528 } else {
1529 cval = &asn_DEF_OCTET_STRING_constraints.value;
1530 csiz = &asn_DEF_OCTET_STRING_constraints.size;
1531 }
1532 ct_extensible = csiz->flags & APC_EXTENSIBLE;
1533
1534 switch(specs->subvariant) {
1535 default:
1536 case ASN_OSUBV_ANY:
Lev Walkin3a4689a2006-11-24 11:20:27 +00001537 case ASN_OSUBV_BIT:
Lev Walkina5b02882017-10-01 22:48:44 -07001538 ASN__ENCODE_FAILED;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001539 case ASN_OSUBV_STR:
1540 canonical_unit_bits = unit_bits = 8;
1541 if(cval->flags & APC_CONSTRAINED)
1542 unit_bits = cval->range_bits;
1543 bpc = OS__BPC_CHAR;
Lev Walkin9d1b45f2017-10-01 17:04:48 -07001544 size_in_units = st->size;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001545 break;
1546 case ASN_OSUBV_U16:
1547 canonical_unit_bits = unit_bits = 16;
1548 if(cval->flags & APC_CONSTRAINED)
1549 unit_bits = cval->range_bits;
1550 bpc = OS__BPC_U16;
Lev Walkin9d1b45f2017-10-01 17:04:48 -07001551 size_in_units = st->size >> 1;
Lev Walkin4ef01a22017-09-06 22:54:39 -07001552 if(st->size & 1) {
1553 ASN_DEBUG("%s string size is not modulo 2", td->name);
1554 ASN__ENCODE_FAILED;
1555 }
Lev Walkin3a4689a2006-11-24 11:20:27 +00001556 break;
1557 case ASN_OSUBV_U32:
1558 canonical_unit_bits = unit_bits = 32;
1559 if(cval->flags & APC_CONSTRAINED)
1560 unit_bits = cval->range_bits;
1561 bpc = OS__BPC_U32;
Lev Walkin9d1b45f2017-10-01 17:04:48 -07001562 size_in_units = st->size >> 2;
Lev Walkin4ef01a22017-09-06 22:54:39 -07001563 if(st->size & 3) {
1564 ASN_DEBUG("%s string size is not modulo 4", td->name);
1565 ASN__ENCODE_FAILED;
1566 }
Lev Walkin3a4689a2006-11-24 11:20:27 +00001567 break;
Lev Walkin725883b2006-10-09 12:07:58 +00001568 }
1569
Lev Walkin9d1b45f2017-10-01 17:04:48 -07001570 ASN_DEBUG("Encoding %s into %zu units of %d bits"
Lev Walkin725883b2006-10-09 12:07:58 +00001571 " (%ld..%ld, effective %d)%s",
Lev Walkin9d1b45f2017-10-01 17:04:48 -07001572 td->name, size_in_units, unit_bits,
Lev Walkin3a4689a2006-11-24 11:20:27 +00001573 csiz->lower_bound, csiz->upper_bound,
1574 csiz->effective_bits, ct_extensible ? " EXT" : "");
Lev Walkin523de9e2006-08-18 01:34:18 +00001575
Lev Walkind4c16732013-03-28 05:00:47 -07001576 /* Figure out whether size lies within PER visible constraint */
Lev Walkin523de9e2006-08-18 01:34:18 +00001577
Lev Walkin9d1b45f2017-10-01 17:04:48 -07001578 if(csiz->effective_bits >= 0) {
1579 if((ssize_t)size_in_units < csiz->lower_bound
1580 || (ssize_t)size_in_units > csiz->upper_bound) {
1581 if(ct_extensible) {
1582 csiz = &asn_DEF_OCTET_STRING_constraints.size;
1583 unit_bits = canonical_unit_bits;
1584 inext = 1;
1585 } else {
1586 ASN__ENCODE_FAILED;
1587 }
1588 }
1589 } else {
1590 inext = 0;
1591 }
Lev Walkin523de9e2006-08-18 01:34:18 +00001592
Lev Walkin9d1b45f2017-10-01 17:04:48 -07001593 if(ct_extensible) {
Lev Walkin523de9e2006-08-18 01:34:18 +00001594 /* Declare whether length is [not] within extension root */
1595 if(per_put_few_bits(po, inext, 1))
Lev Walkin7c1dc052016-03-14 03:08:15 -07001596 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001597 }
1598
Lev Walkin9d1b45f2017-10-01 17:04:48 -07001599 if(csiz->effective_bits >= 0 && !inext) {
1600 ASN_DEBUG("Encoding %zu bytes (%ld), length in %d bits", st->size,
1601 size_in_units - csiz->lower_bound, csiz->effective_bits);
1602 ret = per_put_few_bits(po, size_in_units - csiz->lower_bound,
1603 csiz->effective_bits);
1604 if(ret) ASN__ENCODE_FAILED;
Lev Walkina5b02882017-10-01 22:48:44 -07001605 ret = OCTET_STRING_per_put_characters(po, st->buf, size_in_units, bpc,
1606 unit_bits, cval->lower_bound,
1607 cval->upper_bound, pc);
Lev Walkin9d1b45f2017-10-01 17:04:48 -07001608 if(ret) ASN__ENCODE_FAILED;
1609 ASN__ENCODED_OK(er);
1610 }
Lev Walkin523de9e2006-08-18 01:34:18 +00001611
Lev Walkin9d1b45f2017-10-01 17:04:48 -07001612 ASN_DEBUG("Encoding %zu bytes", st->size);
Lev Walkin523de9e2006-08-18 01:34:18 +00001613
Lev Walkin9d1b45f2017-10-01 17:04:48 -07001614 buf = st->buf;
Lev Walkin5d947a82017-10-03 01:04:03 -07001615 ASN_DEBUG("Encoding %zu in units", size_in_units);
1616 do {
1617 int need_eom = 0;
1618 ssize_t may_save = uper_put_length(po, size_in_units, &need_eom);
1619 if(may_save < 0) ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001620
Lev Walkin5d947a82017-10-03 01:04:03 -07001621 ASN_DEBUG("Encoding %zd of %zu%s", may_save, size_in_units,
1622 need_eom ? ",+EOM" : "");
Lev Walkin523de9e2006-08-18 01:34:18 +00001623
Lev Walkin5d947a82017-10-03 01:04:03 -07001624 ret = OCTET_STRING_per_put_characters(po, buf, may_save, bpc, unit_bits,
Lev Walkina5b02882017-10-01 22:48:44 -07001625 cval->lower_bound,
1626 cval->upper_bound, pc);
Lev Walkin9d1b45f2017-10-01 17:04:48 -07001627 if(ret) ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001628
Lev Walkin5d947a82017-10-03 01:04:03 -07001629 buf += may_save * bpc;
1630 size_in_units -= may_save;
1631 assert(!(may_save & 0x07) || !size_in_units);
1632 if(need_eom && uper_put_length(po, 0, 0))
1633 ASN__ENCODE_FAILED; /* End of Message length */
1634 } while(size_in_units);
Lev Walkin523de9e2006-08-18 01:34:18 +00001635
Lev Walkin9d1b45f2017-10-01 17:04:48 -07001636 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +00001637}
1638
Lev Walkin62d76872017-08-05 22:49:42 -07001639#endif /* ASN_DISABLE_PER_SUPPORT */
1640
Lev Walkinf15320b2004-06-03 03:38:44 +00001641int
Lev Walkinde4825d2004-09-29 13:20:14 +00001642OCTET_STRING_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +00001643 asn_app_consume_bytes_f *cb, void *app_key) {
Wim Lewis59e8d282014-08-04 12:39:35 -07001644 const char * const h2c = "0123456789ABCDEF";
Lev Walkinc2346572004-08-11 09:07:36 +00001645 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +00001646 char scratch[16 * 3 + 4];
1647 char *p = scratch;
1648 uint8_t *buf;
1649 uint8_t *end;
1650 size_t i;
Lev Walkinf15320b2004-06-03 03:38:44 +00001651
Lev Walkind9bd7752004-06-05 08:17:50 +00001652 (void)td; /* Unused argument */
1653
Lev Walkin2ddd3082006-10-16 12:32:14 +00001654 if(!st || (!st->buf && st->size))
1655 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001656
1657 /*
1658 * Dump the contents of the buffer in hexadecimal.
1659 */
1660 buf = st->buf;
1661 end = buf + st->size;
1662 for(i = 0; buf < end; buf++, i++) {
1663 if(!(i % 16) && (i || st->size > 16)) {
Lev Walkin8e8078a2004-09-26 13:10:40 +00001664 if(cb(scratch, p - scratch, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +00001665 return -1;
Lev Walkin8e8078a2004-09-26 13:10:40 +00001666 _i_INDENT(1);
Lev Walkinf15320b2004-06-03 03:38:44 +00001667 p = scratch;
1668 }
1669 *p++ = h2c[(*buf >> 4) & 0x0F];
1670 *p++ = h2c[*buf & 0x0F];
Lev Walkina9cc46e2004-09-22 16:06:28 +00001671 *p++ = 0x20;
Lev Walkinf15320b2004-06-03 03:38:44 +00001672 }
1673
Lev Walkincc6a9102004-09-23 22:06:26 +00001674 if(p > scratch) {
1675 p--; /* Remove the tail space */
Lev Walkin8e8078a2004-09-26 13:10:40 +00001676 if(cb(scratch, p - scratch, app_key) < 0)
Lev Walkincc6a9102004-09-23 22:06:26 +00001677 return -1;
1678 }
1679
1680 return 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001681}
1682
1683int
Lev Walkindc06f6b2004-10-20 15:50:55 +00001684OCTET_STRING_print_utf8(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +00001685 int ilevel, asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +00001686 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +00001687
Lev Walkind9bd7752004-06-05 08:17:50 +00001688 (void)td; /* Unused argument */
1689 (void)ilevel; /* Unused argument */
1690
Lev Walkin2ddd3082006-10-16 12:32:14 +00001691 if(st && (st->buf || !st->size)) {
Lev Walkin8e8078a2004-09-26 13:10:40 +00001692 return (cb(st->buf, st->size, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001693 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +00001694 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001695 }
1696}
1697
1698void
Lev Walkinf6853ce2017-08-11 00:50:27 -07001699OCTET_STRING_free(const asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkin8d99d7b2017-08-25 01:06:00 -07001700 enum asn_struct_free_method method) {
1701 OCTET_STRING_t *st = (OCTET_STRING_t *)sptr;
Lev Walkin034ee192016-03-14 02:28:33 -07001702 asn_OCTET_STRING_specifics_t *specs;
1703 asn_struct_ctx_t *ctx;
Lev Walkinb54577a2004-11-08 10:47:12 +00001704 struct _stack *stck;
Lev Walkinf15320b2004-06-03 03:38:44 +00001705
1706 if(!td || !st)
1707 return;
1708
Lev Walkin034ee192016-03-14 02:28:33 -07001709 specs = td->specifics
1710 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +08001711 : &asn_SPC_OCTET_STRING_specs;
Lev Walkin034ee192016-03-14 02:28:33 -07001712 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
1713
Lev Walkinf15320b2004-06-03 03:38:44 +00001714 ASN_DEBUG("Freeing %s as OCTET STRING", td->name);
1715
1716 if(st->buf) {
1717 FREEMEM(st->buf);
Lev Walkin2ddd3082006-10-16 12:32:14 +00001718 st->buf = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001719 }
1720
1721 /*
1722 * Remove decode-time stack.
1723 */
Lev Walkinb54577a2004-11-08 10:47:12 +00001724 stck = (struct _stack *)ctx->ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +00001725 if(stck) {
1726 while(stck->tail) {
1727 struct _stack_el *sel = stck->tail;
1728 stck->tail = sel->prev;
1729 FREEMEM(sel);
1730 }
1731 FREEMEM(stck);
1732 }
1733
Lev Walkin8d99d7b2017-08-25 01:06:00 -07001734 switch(method) {
1735 case ASFM_FREE_EVERYTHING:
1736 FREEMEM(sptr);
1737 break;
1738 case ASFM_FREE_UNDERLYING:
1739 break;
1740 case ASFM_FREE_UNDERLYING_AND_RESET:
1741 memset(sptr, 0,
1742 ((asn_OCTET_STRING_specifics_t *)(td->specifics))->struct_size);
1743 break;
1744 }
Lev Walkinf15320b2004-06-03 03:38:44 +00001745}
1746
1747/*
1748 * Conversion routines.
1749 */
1750int
1751OCTET_STRING_fromBuf(OCTET_STRING_t *st, const char *str, int len) {
1752 void *buf;
1753
1754 if(st == 0 || (str == 0 && len)) {
1755 errno = EINVAL;
1756 return -1;
1757 }
1758
1759 /*
1760 * Clear the OCTET STRING.
1761 */
1762 if(str == NULL) {
Lev Walkin7b284812005-12-17 11:43:25 +00001763 FREEMEM(st->buf);
1764 st->buf = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001765 st->size = 0;
1766 return 0;
1767 }
1768
1769 /* Determine the original string size, if not explicitly given */
1770 if(len < 0)
1771 len = strlen(str);
1772
1773 /* Allocate and fill the memory */
1774 buf = MALLOC(len + 1);
Lev Walkin7b284812005-12-17 11:43:25 +00001775 if(buf == NULL)
Lev Walkinf15320b2004-06-03 03:38:44 +00001776 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +00001777
1778 memcpy(buf, str, len);
Lev Walkin7b284812005-12-17 11:43:25 +00001779 ((uint8_t *)buf)[len] = '\0'; /* Couldn't use memcpy(len+1)! */
1780 FREEMEM(st->buf);
1781 st->buf = (uint8_t *)buf;
1782 st->size = len;
Lev Walkinf15320b2004-06-03 03:38:44 +00001783
1784 return 0;
1785}
1786
1787OCTET_STRING_t *
Lev Walkina5972be2017-09-29 23:15:58 -07001788OCTET_STRING_new_fromBuf(const asn_TYPE_descriptor_t *td, const char *str,
1789 int len) {
1790 const asn_OCTET_STRING_specifics_t *specs =
1791 td->specifics ? (const asn_OCTET_STRING_specifics_t *)td->specifics
1792 : &asn_SPC_OCTET_STRING_specs;
1793 OCTET_STRING_t *st;
Lev Walkinf15320b2004-06-03 03:38:44 +00001794
Lev Walkinbbd93252004-10-12 05:57:23 +00001795 st = (OCTET_STRING_t *)CALLOC(1, specs->struct_size);
Lev Walkinf15320b2004-06-03 03:38:44 +00001796 if(st && str && OCTET_STRING_fromBuf(st, str, len)) {
Lev Walkin419f6752006-09-13 04:02:00 +00001797 FREEMEM(st);
Lev Walkinf15320b2004-06-03 03:38:44 +00001798 st = NULL;
1799 }
1800
1801 return st;
1802}
1803
Lev Walkincd2f48e2017-08-10 02:14:59 -07001804/*
1805 * Lexicographically compare the common prefix of both strings,
1806 * and if it is the same return -1 for the smallest string.
1807 */
1808int
1809OCTET_STRING_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
1810 const void *bptr) {
Lev Walkina5b02882017-10-01 22:48:44 -07001811 const asn_OCTET_STRING_specifics_t *specs = td->specifics;
Lev Walkincd2f48e2017-08-10 02:14:59 -07001812 const OCTET_STRING_t *a = aptr;
1813 const OCTET_STRING_t *b = bptr;
1814
Lev Walkina5b02882017-10-01 22:48:44 -07001815 assert(!specs || specs->subvariant != ASN_OSUBV_BIT);
Lev Walkincd2f48e2017-08-10 02:14:59 -07001816
1817 if(a && b) {
1818 size_t common_prefix_size = a->size <= b->size ? a->size : b->size;
1819 int ret = memcmp(a->buf, b->buf, common_prefix_size);
1820 if(ret == 0) {
1821 /* Figure out which string with equal prefixes is longer. */
1822 if(a->size < b->size) {
1823 return -1;
1824 } else if(a->size > b->size) {
1825 return 1;
1826 } else {
1827 return 0;
1828 }
1829 } else {
1830 return ret;
1831 }
1832 } else if(!a && !b) {
1833 return 0;
1834 } else if(!a) {
1835 return -1;
1836 } else {
1837 return 1;
1838 }
1839
1840}
1841
Lev Walkina5972be2017-09-29 23:15:58 -07001842/*
1843 * Biased function for randomizing character values around their limits.
1844 */
1845static uint32_t
1846OCTET_STRING__random_char(unsigned long lb, unsigned long ub) {
1847 assert(lb <= ub);
1848 switch(asn_random_between(0, 16)) {
1849 case 0:
1850 if(lb < ub) return lb + 1;
1851 /* Fall through */
1852 case 1:
1853 return lb;
1854 case 2:
1855 if(lb < ub) return ub - 1;
1856 /* Fall through */
1857 case 3:
1858 return ub;
1859 default:
1860 return asn_random_between(lb, ub);
1861 }
1862}
1863
1864asn_random_fill_result_t
1865OCTET_STRING_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
1866 const asn_encoding_constraints_t *constraints,
1867 size_t max_length) {
1868 asn_OCTET_STRING_specifics_t *specs = td->specifics
1869 ? (asn_OCTET_STRING_specifics_t *)td->specifics
1870 : &asn_SPC_OCTET_STRING_specs;
1871 asn_random_fill_result_t result_ok = {ARFILL_OK, 1};
1872 asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0};
1873 asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0};
1874 static unsigned lengths[] = {0, 1, 2, 3, 4, 8,
1875 126, 127, 128, 16383, 16384, 16385,
1876 65534, 65535, 65536, 65537};
1877 unsigned int unit_bytes = 1;
1878 unsigned long clb = 0; /* Lower bound on char */
1879 unsigned long cub = 255; /* Higher bound on char value */
1880 uint8_t *buf;
1881 uint8_t *bend;
1882 uint8_t *b;
1883 size_t rnd_len;
1884 OCTET_STRING_t *st;
1885
1886 if(max_length == 0) return result_skipped;
1887
1888 switch(specs->subvariant) {
1889 default:
1890 case ASN_OSUBV_ANY:
1891 return result_failed;
1892 case ASN_OSUBV_BIT:
1893 /* Handled by BIT_STRING itself. */
1894 return result_failed;
Lev Walkina5b02882017-10-01 22:48:44 -07001895 case ASN_OSUBV_STR:
Lev Walkina5972be2017-09-29 23:15:58 -07001896 unit_bytes = 1;
1897 clb = 0;
1898 cub = 255;
Lev Walkina5b02882017-10-01 22:48:44 -07001899 break;
1900 case ASN_OSUBV_U16:
Lev Walkina5972be2017-09-29 23:15:58 -07001901 unit_bytes = 2;
1902 clb = 0;
1903 cub = 65535;
Lev Walkina5b02882017-10-01 22:48:44 -07001904 break;
1905 case ASN_OSUBV_U32:
Lev Walkina5972be2017-09-29 23:15:58 -07001906 unit_bytes = 4;
1907 clb = 0;
1908 cub = 0x10FFFF;
Lev Walkina5b02882017-10-01 22:48:44 -07001909 break;
1910 }
Lev Walkina5972be2017-09-29 23:15:58 -07001911
1912 if(!constraints) constraints = &td->encoding_constraints;
1913 if(constraints->per_constraints) {
1914 const asn_per_constraint_t *pc =
1915 &td->encoding_constraints.per_constraints->value;
1916 if(pc->flags & APC_SEMI_CONSTRAINED) {
1917 clb = pc->lower_bound;
1918 } else if(pc->flags & APC_CONSTRAINED) {
1919 clb = pc->lower_bound;
1920 cub = pc->upper_bound;
1921 }
1922 }
1923
1924 /* Figure out how far we should go */
1925 rnd_len = lengths[asn_random_between(
1926 0, sizeof(lengths) / sizeof(lengths[0]) - 1)];
1927 if(constraints->per_constraints) {
1928 const asn_per_constraint_t *pc =
1929 &td->encoding_constraints.per_constraints->size;
1930 if(pc->flags & APC_CONSTRAINED) {
Lev Walkina5b02882017-10-01 22:48:44 -07001931 long suggested_upper_bound = pc->upper_bound < (ssize_t)max_length
1932 ? pc->upper_bound
1933 : max_length;
Lev Walkina5972be2017-09-29 23:15:58 -07001934 if(max_length < (size_t)pc->lower_bound) {
1935 return result_skipped;
1936 }
Lev Walkin9d1b45f2017-10-01 17:04:48 -07001937 if(pc->flags & APC_EXTENSIBLE) {
1938 switch(asn_random_between(0, 5)) {
1939 case 0:
1940 if(pc->lower_bound > 0) {
1941 rnd_len = pc->lower_bound - 1;
1942 break;
1943 }
1944 /* Fall through */
1945 case 1:
1946 rnd_len = pc->upper_bound + 1;
1947 break;
1948 case 2:
1949 /* Keep rnd_len from the table */
1950 if(rnd_len < max_length) {
1951 break;
1952 }
1953 /* Fall through */
1954 default:
1955 rnd_len = asn_random_between(pc->lower_bound,
1956 suggested_upper_bound);
1957 }
1958 } else {
1959 rnd_len =
1960 asn_random_between(pc->lower_bound, suggested_upper_bound);
1961 }
Lev Walkina5972be2017-09-29 23:15:58 -07001962 } else {
1963 rnd_len = asn_random_between(0, max_length - 1);
1964 }
1965 } else if(rnd_len >= max_length) {
1966 rnd_len = asn_random_between(0, max_length - 1);
1967 }
1968
1969 buf = CALLOC(unit_bytes, rnd_len + 1);
1970 if(!buf) return result_failed;
1971
1972 bend = &buf[unit_bytes * rnd_len];
1973
1974 switch(unit_bytes) {
1975 case 1:
1976 for(b = buf; b < bend; b += unit_bytes) {
1977 *(uint8_t *)b = OCTET_STRING__random_char(clb, cub);
1978 }
1979 *(uint8_t *)b = 0;
1980 break;
1981 case 2:
1982 for(b = buf; b < bend; b += unit_bytes) {
1983 *(uint16_t *)b = OCTET_STRING__random_char(clb, cub);
1984 }
1985 *(uint16_t *)b = 0;
1986 break;
1987 case 4:
1988 for(b = buf; b < bend; b += unit_bytes) {
1989 *(uint32_t *)b = OCTET_STRING__random_char(clb, cub);
1990 }
1991 *(uint32_t *)b = 0;
1992 break;
1993 }
1994
1995 if(*sptr) {
1996 st = *sptr;
1997 FREEMEM(st->buf);
1998 } else {
1999 st = (OCTET_STRING_t *)(*sptr = CALLOC(1, specs->struct_size));
2000 if(!st) {
2001 FREEMEM(buf);
2002 return result_failed;
2003 }
2004 }
2005
2006 st->buf = buf;
2007 st->size = unit_bytes * rnd_len;
2008
2009 result_ok.length = st->size;
2010 return result_ok;
2011}