blob: d1015934da339d09b9149a9936f888527cee03c3 [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001/*-
Lev Walkin0a8aa602006-09-18 20:05:55 +00002 * Copyright (c) 2003, 2004, 2005, 2006 Lev Walkin <vlm@lionet.info>.
Lev Walkin75b1bef2005-04-25 19:38:21 +00003 * All rights reserved.
Lev Walkinf15320b2004-06-03 03:38:44 +00004 * Redistribution and modifications are permitted subject to BSD license.
5 */
Lev Walkina9cc46e2004-09-22 16:06:28 +00006#include <asn_internal.h>
Lev Walkinf15320b2004-06-03 03:38:44 +00007#include <OCTET_STRING.h>
Lev Walkinbbd93252004-10-12 05:57:23 +00008#include <BIT_STRING.h> /* for .bits_unused member */
Lev Walkinf15320b2004-06-03 03:38:44 +00009#include <errno.h>
10
11/*
12 * OCTET STRING basic type description.
13 */
Wim Lewis18c2ec92014-07-29 11:30:10 -070014static const ber_tlv_tag_t asn_DEF_OCTET_STRING_tags[] = {
Lev Walkinf15320b2004-06-03 03:38:44 +000015 (ASN_TAG_CLASS_UNIVERSAL | (4 << 2))
16};
Bi-Ruei, Chiu1fa31c92016-05-16 13:50:09 +080017asn_OCTET_STRING_specifics_t asn_SPC_OCTET_STRING_specs = {
Lev Walkinbbd93252004-10-12 05:57:23 +000018 sizeof(OCTET_STRING_t),
19 offsetof(OCTET_STRING_t, _asn_ctx),
Lev Walkin3a4689a2006-11-24 11:20:27 +000020 ASN_OSUBV_STR
Lev Walkinbbd93252004-10-12 05:57:23 +000021};
Vasil Velichkovd82f77b2016-07-19 19:34:09 +030022static asn_per_constraints_t asn_DEF_OCTET_STRING_constraints = {
Lev Walkin3a4689a2006-11-24 11:20:27 +000023 { APC_CONSTRAINED, 8, 8, 0, 255 },
24 { APC_SEMI_CONSTRAINED, -1, -1, 0, 0 },
25 0, 0
Lev Walkin59b176e2005-11-26 11:25:14 +000026};
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080027asn_TYPE_operation_t asn_OP_OCTET_STRING = {
Lev Walkina9cc46e2004-09-22 16:06:28 +000028 OCTET_STRING_free,
Lev Walkincd2f48e2017-08-10 02:14:59 -070029 OCTET_STRING_print, /* OCTET STRING generally means a non-ascii sequence */
30 OCTET_STRING_compare,
Lev Walkinf15320b2004-06-03 03:38:44 +000031 OCTET_STRING_decode_ber,
32 OCTET_STRING_encode_der,
Lev Walkindc06f6b2004-10-20 15:50:55 +000033 OCTET_STRING_decode_xer_hex,
Lev Walkina9cc46e2004-09-22 16:06:28 +000034 OCTET_STRING_encode_xer,
Lev Walkincc159472017-07-06 08:26:36 -070035#ifdef ASN_DISABLE_OER_SUPPORT
36 0,
37 0,
38#else
Lev Walkin2535bd52017-08-27 23:45:56 -070039 OCTET_STRING_decode_oer,
40 OCTET_STRING_encode_oer,
Lev Walkincc159472017-07-06 08:26:36 -070041#endif /* ASN_DISABLE_OER_SUPPORT */
Lev Walkinb33425f2017-07-14 14:59:52 +040042#ifdef ASN_DISABLE_PER_SUPPORT
43 0,
44 0,
45#else
46 OCTET_STRING_decode_uper, /* Unaligned PER decoder */
47 OCTET_STRING_encode_uper, /* Unaligned PER encoder */
48#endif /* ASN_DISABLE_PER_SUPPORT */
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080049 0 /* Use generic outmost tag fetcher */
50};
51asn_TYPE_descriptor_t asn_DEF_OCTET_STRING = {
52 "OCTET STRING", /* Canonical name */
53 "OCTET_STRING", /* XML tag name */
54 &asn_OP_OCTET_STRING,
55 asn_generic_no_constraint,
Lev Walkinde4825d2004-09-29 13:20:14 +000056 asn_DEF_OCTET_STRING_tags,
57 sizeof(asn_DEF_OCTET_STRING_tags)
58 / sizeof(asn_DEF_OCTET_STRING_tags[0]),
59 asn_DEF_OCTET_STRING_tags, /* Same as above */
60 sizeof(asn_DEF_OCTET_STRING_tags)
61 / sizeof(asn_DEF_OCTET_STRING_tags[0]),
Lev Walkin76780762017-07-07 10:07:30 -070062 0, /* No OER visible constraints */
Lev Walkin59b176e2005-11-26 11:25:14 +000063 0, /* No PER visible constraints */
Lev Walkin449f8322004-08-20 13:23:42 +000064 0, 0, /* No members */
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +080065 &asn_SPC_OCTET_STRING_specs
Lev Walkinf15320b2004-06-03 03:38:44 +000066};
67
Lev Walkincc6a9102004-09-23 22:06:26 +000068#undef _CH_PHASE
69#undef NEXT_PHASE
70#undef PREV_PHASE
Lev Walkin75b1bef2005-04-25 19:38:21 +000071#define _CH_PHASE(ctx, inc) do { \
72 if(ctx->phase == 0) \
73 ctx->context = 0; \
74 ctx->phase += inc; \
Lev Walkinf15320b2004-06-03 03:38:44 +000075 } while(0)
76#define NEXT_PHASE(ctx) _CH_PHASE(ctx, +1)
77#define PREV_PHASE(ctx) _CH_PHASE(ctx, -1)
78
Lev Walkincc6a9102004-09-23 22:06:26 +000079#undef ADVANCE
Lev Walkin75b1bef2005-04-25 19:38:21 +000080#define ADVANCE(num_bytes) do { \
81 size_t num = (num_bytes); \
82 buf_ptr = ((const char *)buf_ptr) + num; \
83 size -= num; \
84 consumed_myself += num; \
Lev Walkinf15320b2004-06-03 03:38:44 +000085 } while(0)
86
Lev Walkincc6a9102004-09-23 22:06:26 +000087#undef RETURN
Lev Walkin75b1bef2005-04-25 19:38:21 +000088#define RETURN(_code) do { \
Lev Walkin59b176e2005-11-26 11:25:14 +000089 asn_dec_rval_t tmprval; \
90 tmprval.code = _code; \
91 tmprval.consumed = consumed_myself; \
92 return tmprval; \
Lev Walkinf15320b2004-06-03 03:38:44 +000093 } while(0)
94
Lev Walkincc6a9102004-09-23 22:06:26 +000095#undef APPEND
Lev Walkind9bd7752004-06-05 08:17:50 +000096#define APPEND(bufptr, bufsize) do { \
Lev Walkin75b1bef2005-04-25 19:38:21 +000097 size_t _bs = (bufsize); /* Append size */ \
98 size_t _ns = ctx->context; /* Allocated now */ \
99 size_t _es = st->size + _bs; /* Expected size */ \
100 /* int is really a typeof(st->size): */ \
101 if((int)_es < 0) RETURN(RC_FAIL); \
102 if(_ns <= _es) { \
Lev Walkind9bd7752004-06-05 08:17:50 +0000103 void *ptr; \
Lev Walkin188ed2c2004-09-13 08:31:01 +0000104 /* Be nice and round to the memory allocator */ \
Lev Walkin75b1bef2005-04-25 19:38:21 +0000105 do { _ns = _ns ? _ns << 1 : 16; } \
106 while(_ns <= _es); \
107 /* int is really a typeof(st->size): */ \
108 if((int)_ns < 0) RETURN(RC_FAIL); \
Lev Walkind9bd7752004-06-05 08:17:50 +0000109 ptr = REALLOC(st->buf, _ns); \
110 if(ptr) { \
Lev Walkinc2346572004-08-11 09:07:36 +0000111 st->buf = (uint8_t *)ptr; \
Lev Walkin75b1bef2005-04-25 19:38:21 +0000112 ctx->context = _ns; \
Lev Walkind9bd7752004-06-05 08:17:50 +0000113 } else { \
114 RETURN(RC_FAIL); \
115 } \
Lev Walkin7f85ef42005-07-02 20:24:27 +0000116 ASN_DEBUG("Reallocating into %ld", (long)_ns); \
Lev Walkind9bd7752004-06-05 08:17:50 +0000117 } \
Lev Walkin8d127872004-09-04 04:44:50 +0000118 memcpy(st->buf + st->size, bufptr, _bs); \
Lev Walkind9bd7752004-06-05 08:17:50 +0000119 /* Convenient nul-termination */ \
Lev Walkin75b1bef2005-04-25 19:38:21 +0000120 st->buf[_es] = '\0'; \
121 st->size = _es; \
Lev Walkinf15320b2004-06-03 03:38:44 +0000122 } while(0)
123
124/*
125 * The main reason why ASN.1 is still alive is that too much time and effort
126 * is necessary for learning it more or less adequately, thus creating a gut
127 * necessity to demonstrate that aquired skill everywhere afterwards.
128 * No, I am not going to explain what the following stuff is.
129 */
130struct _stack_el {
Lev Walkin494fb702017-08-07 20:07:00 -0700131 ber_tlv_len_t left; /* What's left to read (or -1) */
132 ber_tlv_len_t got; /* What was actually processed */
Lev Walkin24bf3f62017-08-10 17:58:43 -0700133 unsigned cont_level; /* Depth of subcontainment */
Lev Walkin494fb702017-08-07 20:07:00 -0700134 int want_nulls; /* Want null "end of content" octets? */
135 int bits_chopped; /* Flag in BIT STRING mode */
136 ber_tlv_tag_t tag; /* For debugging purposes */
137 struct _stack_el *prev;
138 struct _stack_el *next;
Lev Walkinf15320b2004-06-03 03:38:44 +0000139};
140struct _stack {
141 struct _stack_el *tail;
142 struct _stack_el *cur_ptr;
143};
144
145static struct _stack_el *
Lev Walkin3990ba62004-09-24 20:57:41 +0000146OS__add_stack_el(struct _stack *st) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000147 struct _stack_el *nel;
148
Lev Walkin188ed2c2004-09-13 08:31:01 +0000149 /*
150 * Reuse the old stack frame or allocate a new one.
151 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000152 if(st->cur_ptr && st->cur_ptr->next) {
153 nel = st->cur_ptr->next;
Lev Walkinf15320b2004-06-03 03:38:44 +0000154 nel->bits_chopped = 0;
Lev Walkin5c915992004-09-27 20:54:06 +0000155 nel->got = 0;
156 /* Retain the nel->cont_level, it's correct. */
Lev Walkinf15320b2004-06-03 03:38:44 +0000157 } else {
Lev Walkin814cca72004-12-15 23:23:53 +0000158 nel = (struct _stack_el *)CALLOC(1, sizeof(struct _stack_el));
Lev Walkinf15320b2004-06-03 03:38:44 +0000159 if(nel == NULL)
160 return NULL;
161
162 if(st->tail) {
Lev Walkin188ed2c2004-09-13 08:31:01 +0000163 /* Increase a subcontainment depth */
164 nel->cont_level = st->tail->cont_level + 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000165 st->tail->next = nel;
166 }
167 nel->prev = st->tail;
168 st->tail = nel;
169 }
170
171 st->cur_ptr = nel;
172
173 return nel;
174}
175
176static struct _stack *
johvik28268d92017-05-09 10:27:52 +0200177_new_stack(void) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000178 return (struct _stack *)CALLOC(1, sizeof(struct _stack));
Lev Walkinf15320b2004-06-03 03:38:44 +0000179}
180
181/*
182 * Decode OCTET STRING type.
183 */
Lev Walkindc06f6b2004-10-20 15:50:55 +0000184asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -0700185OCTET_STRING_decode_ber(const asn_codec_ctx_t *opt_codec_ctx,
Lev Walkinde4825d2004-09-29 13:20:14 +0000186 asn_TYPE_descriptor_t *td,
Lev Walkin59b176e2005-11-26 11:25:14 +0000187 void **sptr, const void *buf_ptr, size_t size, int tag_mode) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000188 asn_OCTET_STRING_specifics_t *specs = td->specifics
Lev Walkindc06f6b2004-10-20 15:50:55 +0000189 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +0800190 : &asn_SPC_OCTET_STRING_specs;
Lev Walkin59b176e2005-11-26 11:25:14 +0000191 BIT_STRING_t *st = (BIT_STRING_t *)*sptr;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000192 asn_dec_rval_t rval;
Lev Walkinde4825d2004-09-29 13:20:14 +0000193 asn_struct_ctx_t *ctx;
Lev Walkinf15320b2004-06-03 03:38:44 +0000194 ssize_t consumed_myself = 0;
Lev Walkinbbd93252004-10-12 05:57:23 +0000195 struct _stack *stck; /* Expectations stack structure */
Lev Walkin5c915992004-09-27 20:54:06 +0000196 struct _stack_el *sel = 0; /* Stack element */
Lev Walkinf15320b2004-06-03 03:38:44 +0000197 int tlv_constr;
Lev Walkin3a4689a2006-11-24 11:20:27 +0000198 enum asn_OS_Subvariant type_variant = specs->subvariant;
Lev Walkinf15320b2004-06-03 03:38:44 +0000199
Lev Walkincc6a9102004-09-23 22:06:26 +0000200 ASN_DEBUG("Decoding %s as %s (frame %ld)",
201 td->name,
Lev Walkin3a4689a2006-11-24 11:20:27 +0000202 (type_variant == ASN_OSUBV_STR) ?
Lev Walkinbbd93252004-10-12 05:57:23 +0000203 "OCTET STRING" : "OS-SpecialCase",
Lev Walkincc6a9102004-09-23 22:06:26 +0000204 (long)size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000205
206 /*
207 * Create the string if does not exist.
208 */
209 if(st == NULL) {
Lev Walkin59b176e2005-11-26 11:25:14 +0000210 st = (BIT_STRING_t *)(*sptr = CALLOC(1, specs->struct_size));
211 if(st == NULL) RETURN(RC_FAIL);
Lev Walkinf15320b2004-06-03 03:38:44 +0000212 }
213
214 /* Restore parsing context */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800215 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000216
217 switch(ctx->phase) {
218 case 0:
219 /*
220 * Check tags.
221 */
Lev Walkinde4825d2004-09-29 13:20:14 +0000222 rval = ber_check_tags(opt_codec_ctx, td, ctx,
Lev Walkin8e8078a2004-09-26 13:10:40 +0000223 buf_ptr, size, tag_mode, -1,
Lev Walkinf15320b2004-06-03 03:38:44 +0000224 &ctx->left, &tlv_constr);
Lev Walkin443d2512004-10-05 06:35:31 +0000225 if(rval.code != RC_OK)
226 return rval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000227
Lev Walkinf15320b2004-06-03 03:38:44 +0000228 if(tlv_constr) {
229 /*
230 * Complex operation, requires stack of expectations.
231 */
232 ctx->ptr = _new_stack();
Lev Walkinb02145b2017-06-28 08:52:07 -0700233 if(!ctx->ptr) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000234 RETURN(RC_FAIL);
235 }
236 } else {
237 /*
238 * Jump into stackless primitive decoding.
239 */
240 _CH_PHASE(ctx, 3);
Lev Walkin3a4689a2006-11-24 11:20:27 +0000241 if(type_variant == ASN_OSUBV_ANY && tag_mode != 1)
Lev Walkin188ed2c2004-09-13 08:31:01 +0000242 APPEND(buf_ptr, rval.consumed);
Lev Walkinf15320b2004-06-03 03:38:44 +0000243 ADVANCE(rval.consumed);
244 goto phase3;
245 }
246
Lev Walkinf15320b2004-06-03 03:38:44 +0000247 NEXT_PHASE(ctx);
248 /* Fall through */
249 case 1:
250 phase1:
251 /*
252 * Fill the stack with expectations.
253 */
Lev Walkin814cca72004-12-15 23:23:53 +0000254 stck = (struct _stack *)ctx->ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000255 sel = stck->cur_ptr;
256 do {
257 ber_tlv_tag_t tlv_tag;
258 ber_tlv_len_t tlv_len;
Lev Walkin188ed2c2004-09-13 08:31:01 +0000259 ber_tlv_tag_t expected_tag;
Lev Walkin5c915992004-09-27 20:54:06 +0000260 ssize_t tl, ll, tlvl;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000261 /* This one works even if (sel->left == -1) */
Simo Sorcecd943c22016-07-25 04:39:40 -0400262 size_t Left = ((!sel||(size_t)sel->left >= size)
263 ?size:(size_t)sel->left);
Lev Walkinf15320b2004-06-03 03:38:44 +0000264
Lev Walkin3990ba62004-09-24 20:57:41 +0000265
Lev Walkinabf68892004-10-26 10:12:14 +0000266 ASN_DEBUG("%p, s->l=%ld, s->wn=%ld, s->g=%ld\n", sel,
Lev Walkin1e3ccbb2004-10-26 10:56:10 +0000267 (long)(sel?sel->left:0),
268 (long)(sel?sel->want_nulls:0),
269 (long)(sel?sel->got:0)
Lev Walkin5c915992004-09-27 20:54:06 +0000270 );
271 if(sel && sel->left <= 0 && sel->want_nulls == 0) {
272 if(sel->prev) {
273 struct _stack_el *prev = sel->prev;
274 if(prev->left != -1) {
275 if(prev->left < sel->got)
276 RETURN(RC_FAIL);
277 prev->left -= sel->got;
278 }
279 prev->got += sel->got;
280 sel = stck->cur_ptr = prev;
281 if(!sel) break;
282 tlv_constr = 1;
283 continue;
284 } else {
285 sel = stck->cur_ptr = 0;
286 break; /* Nothing to wait */
287 }
288 }
289
Lev Walkin3990ba62004-09-24 20:57:41 +0000290 tl = ber_fetch_tag(buf_ptr, Left, &tlv_tag);
Lev Walkinabf68892004-10-26 10:12:14 +0000291 ASN_DEBUG("fetch tag(size=%ld,L=%ld), %sstack, left=%ld, wn=%ld, tl=%ld",
292 (long)size, (long)Left, sel?"":"!",
Lev Walkin1e3ccbb2004-10-26 10:56:10 +0000293 (long)(sel?sel->left:0),
294 (long)(sel?sel->want_nulls:0),
Lev Walkinabf68892004-10-26 10:12:14 +0000295 (long)tl);
Lev Walkinf15320b2004-06-03 03:38:44 +0000296 switch(tl) {
297 case -1: RETURN(RC_FAIL);
298 case 0: RETURN(RC_WMORE);
299 }
300
301 tlv_constr = BER_TLV_CONSTRUCTED(buf_ptr);
302
303 ll = ber_fetch_length(tlv_constr,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000304 (const char *)buf_ptr + tl,Left - tl,&tlv_len);
Lev Walkina6a926a2005-03-02 01:54:28 +0000305 ASN_DEBUG("Got tag=%s, tc=%d, left=%ld, tl=%ld, len=%ld, ll=%ld",
Lev Walkin8d127872004-09-04 04:44:50 +0000306 ber_tlv_tag_string(tlv_tag), tlv_constr,
Lev Walkina6a926a2005-03-02 01:54:28 +0000307 (long)Left, (long)tl, (long)tlv_len, (long)ll);
Lev Walkinf15320b2004-06-03 03:38:44 +0000308 switch(ll) {
309 case -1: RETURN(RC_FAIL);
310 case 0: RETURN(RC_WMORE);
311 }
312
Lev Walkin8d127872004-09-04 04:44:50 +0000313 if(sel && sel->want_nulls
Lev Walkin8c3b8542005-03-10 18:52:02 +0000314 && ((const uint8_t *)buf_ptr)[0] == 0
315 && ((const uint8_t *)buf_ptr)[1] == 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000316 {
Lev Walkin8d127872004-09-04 04:44:50 +0000317
318 ASN_DEBUG("Eat EOC; wn=%d--", sel->want_nulls);
319
Lev Walkin3a4689a2006-11-24 11:20:27 +0000320 if(type_variant == ASN_OSUBV_ANY
Lev Walkin07f388c2004-10-11 11:43:08 +0000321 && (tag_mode != 1 || sel->cont_level))
322 APPEND("\0\0", 2);
Lev Walkin5c915992004-09-27 20:54:06 +0000323
324 ADVANCE(2);
325 sel->got += 2;
326 if(sel->left != -1) {
327 sel->left -= 2; /* assert(sel->left >= 2) */
328 }
329
Lev Walkinf15320b2004-06-03 03:38:44 +0000330 sel->want_nulls--;
331 if(sel->want_nulls == 0) {
332 /* Move to the next expectation */
Lev Walkin5c915992004-09-27 20:54:06 +0000333 sel->left = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000334 tlv_constr = 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000335 }
Lev Walkin8d127872004-09-04 04:44:50 +0000336
337 continue;
Lev Walkin188ed2c2004-09-13 08:31:01 +0000338 }
339
340 /*
341 * Set up expected tags,
342 * depending on ASN.1 type being decoded.
343 */
Lev Walkinbbd93252004-10-12 05:57:23 +0000344 switch(type_variant) {
Lev Walkin3a4689a2006-11-24 11:20:27 +0000345 case ASN_OSUBV_BIT:
Lev Walkin188ed2c2004-09-13 08:31:01 +0000346 /* X.690: 8.6.4.1, NOTE 2 */
347 /* Fall through */
Lev Walkin3a4689a2006-11-24 11:20:27 +0000348 case ASN_OSUBV_STR:
Lev Walkin188ed2c2004-09-13 08:31:01 +0000349 default:
350 if(sel) {
Lev Walkin24bf3f62017-08-10 17:58:43 -0700351 unsigned level = sel->cont_level;
Lev Walkin188ed2c2004-09-13 08:31:01 +0000352 if(level < td->all_tags_count) {
353 expected_tag = td->all_tags[level];
354 break;
355 } else if(td->all_tags_count) {
356 expected_tag = td->all_tags
357 [td->all_tags_count - 1];
358 break;
359 }
360 /* else, Fall through */
361 }
362 /* Fall through */
Lev Walkin3a4689a2006-11-24 11:20:27 +0000363 case ASN_OSUBV_ANY:
Lev Walkin188ed2c2004-09-13 08:31:01 +0000364 expected_tag = tlv_tag;
365 break;
366 }
367
368
369 if(tlv_tag != expected_tag) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000370 char buf[2][32];
371 ber_tlv_tag_snprint(tlv_tag,
372 buf[0], sizeof(buf[0]));
373 ber_tlv_tag_snprint(td->tags[td->tags_count-1],
374 buf[1], sizeof(buf[1]));
375 ASN_DEBUG("Tag does not match expectation: %s != %s",
376 buf[0], buf[1]);
377 RETURN(RC_FAIL);
378 }
379
Lev Walkinde4825d2004-09-29 13:20:14 +0000380 tlvl = tl + ll; /* Combined length of T and L encoding */
381 if((tlv_len + tlvl) < 0) {
382 /* tlv_len value is too big */
383 ASN_DEBUG("TLV encoding + length (%ld) is too big",
384 (long)tlv_len);
385 RETURN(RC_FAIL);
386 }
387
Lev Walkinf15320b2004-06-03 03:38:44 +0000388 /*
389 * Append a new expectation.
390 */
Lev Walkin3990ba62004-09-24 20:57:41 +0000391 sel = OS__add_stack_el(stck);
Lev Walkin5c915992004-09-27 20:54:06 +0000392 if(!sel) RETURN(RC_FAIL);
Lev Walkinf15320b2004-06-03 03:38:44 +0000393
Lev Walkin5c915992004-09-27 20:54:06 +0000394 sel->tag = tlv_tag;
395
396 sel->want_nulls = (tlv_len==-1);
397 if(sel->prev && sel->prev->left != -1) {
398 /* Check that the parent frame is big enough */
399 if(sel->prev->left < tlvl + (tlv_len==-1?0:tlv_len))
400 RETURN(RC_FAIL);
401 if(tlv_len == -1)
402 sel->left = sel->prev->left - tlvl;
403 else
404 sel->left = tlv_len;
405 } else {
406 sel->left = tlv_len;
407 }
Lev Walkin3a4689a2006-11-24 11:20:27 +0000408 if(type_variant == ASN_OSUBV_ANY
Lev Walkin07f388c2004-10-11 11:43:08 +0000409 && (tag_mode != 1 || sel->cont_level))
410 APPEND(buf_ptr, tlvl);
Lev Walkin5c915992004-09-27 20:54:06 +0000411 sel->got += tlvl;
412 ADVANCE(tlvl);
413
Lev Walkin24bf3f62017-08-10 17:58:43 -0700414 ASN_DEBUG("+EXPECT2 got=%ld left=%ld, wn=%d, clvl=%u",
Lev Walkinabf68892004-10-26 10:12:14 +0000415 (long)sel->got, (long)sel->left,
416 sel->want_nulls, sel->cont_level);
Lev Walkin5c915992004-09-27 20:54:06 +0000417
Lev Walkinf15320b2004-06-03 03:38:44 +0000418 } while(tlv_constr);
419 if(sel == NULL) {
420 /* Finished operation, "phase out" */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000421 ASN_DEBUG("Phase out");
Lev Walkinf15320b2004-06-03 03:38:44 +0000422 _CH_PHASE(ctx, +3);
423 break;
424 }
425
426 NEXT_PHASE(ctx);
427 /* Fall through */
428 case 2:
Lev Walkin814cca72004-12-15 23:23:53 +0000429 stck = (struct _stack *)ctx->ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000430 sel = stck->cur_ptr;
Lev Walkin5c915992004-09-27 20:54:06 +0000431 ASN_DEBUG("Phase 2: Need %ld bytes, size=%ld, alrg=%ld, wn=%d",
432 (long)sel->left, (long)size, (long)sel->got,
433 sel->want_nulls);
Lev Walkinf15320b2004-06-03 03:38:44 +0000434 {
435 ber_tlv_len_t len;
436
437 assert(sel->left >= 0);
438
Lev Walkinec1ffd42004-08-18 04:53:32 +0000439 len = ((ber_tlv_len_t)size < sel->left)
440 ? (ber_tlv_len_t)size : sel->left;
Lev Walkinf15320b2004-06-03 03:38:44 +0000441 if(len > 0) {
Lev Walkin3a4689a2006-11-24 11:20:27 +0000442 if(type_variant == ASN_OSUBV_BIT
Lev Walkin188ed2c2004-09-13 08:31:01 +0000443 && sel->bits_chopped == 0) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000444 /* Put the unused-bits-octet away */
Lev Walkin8c3b8542005-03-10 18:52:02 +0000445 st->bits_unused = *(const uint8_t *)buf_ptr;
446 APPEND(((const char *)buf_ptr+1), (len - 1));
Lev Walkinf15320b2004-06-03 03:38:44 +0000447 sel->bits_chopped = 1;
448 } else {
449 APPEND(buf_ptr, len);
450 }
451 ADVANCE(len);
452 sel->left -= len;
Lev Walkin5c915992004-09-27 20:54:06 +0000453 sel->got += len;
Lev Walkinf15320b2004-06-03 03:38:44 +0000454 }
455
Lev Walkin5c915992004-09-27 20:54:06 +0000456 if(sel->left) {
457 ASN_DEBUG("OS left %ld, size = %ld, wn=%d\n",
458 (long)sel->left, (long)size, sel->want_nulls);
Lev Walkinf15320b2004-06-03 03:38:44 +0000459 RETURN(RC_WMORE);
Lev Walkinf15320b2004-06-03 03:38:44 +0000460 }
Lev Walkin5c915992004-09-27 20:54:06 +0000461
462 PREV_PHASE(ctx);
463 goto phase1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000464 }
465 break;
466 case 3:
467 phase3:
468 /*
469 * Primitive form, no stack required.
470 */
Lev Walkinbbd93252004-10-12 05:57:23 +0000471 assert(ctx->left >= 0);
472
Lev Walkind9bd7752004-06-05 08:17:50 +0000473 if(size < (size_t)ctx->left) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000474 if(!size) RETURN(RC_WMORE);
Lev Walkin3a4689a2006-11-24 11:20:27 +0000475 if(type_variant == ASN_OSUBV_BIT && !ctx->context) {
Lev Walkin8c3b8542005-03-10 18:52:02 +0000476 st->bits_unused = *(const uint8_t *)buf_ptr;
Lev Walkinbbd93252004-10-12 05:57:23 +0000477 ctx->left--;
478 ADVANCE(1);
479 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000480 APPEND(buf_ptr, size);
Lev Walkin75b1bef2005-04-25 19:38:21 +0000481 assert(ctx->context > 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000482 ctx->left -= size;
483 ADVANCE(size);
484 RETURN(RC_WMORE);
485 } else {
Lev Walkin3a4689a2006-11-24 11:20:27 +0000486 if(type_variant == ASN_OSUBV_BIT
Lev Walkin75b1bef2005-04-25 19:38:21 +0000487 && !ctx->context && ctx->left) {
Lev Walkin8c3b8542005-03-10 18:52:02 +0000488 st->bits_unused = *(const uint8_t *)buf_ptr;
Lev Walkinbbd93252004-10-12 05:57:23 +0000489 ctx->left--;
490 ADVANCE(1);
491 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000492 APPEND(buf_ptr, ctx->left);
493 ADVANCE(ctx->left);
494 ctx->left = 0;
495
496 NEXT_PHASE(ctx);
497 }
498 break;
499 }
500
Lev Walkin5c915992004-09-27 20:54:06 +0000501 if(sel) {
502 ASN_DEBUG("3sel p=%p, wn=%d, l=%ld, g=%ld, size=%ld",
503 sel->prev, sel->want_nulls,
504 (long)sel->left, (long)sel->got, (long)size);
505 if(sel->prev || sel->want_nulls > 1 || sel->left > 0) {
506 RETURN(RC_WMORE);
507 }
508 }
509
Lev Walkinf15320b2004-06-03 03:38:44 +0000510 /*
511 * BIT STRING-specific processing.
512 */
Lev Walkin3a4689a2006-11-24 11:20:27 +0000513 if(type_variant == ASN_OSUBV_BIT && st->size) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000514 /* Finalize BIT STRING: zero out unused bits. */
Lev Walkinbbd93252004-10-12 05:57:23 +0000515 st->buf[st->size-1] &= 0xff << st->bits_unused;
Lev Walkinf15320b2004-06-03 03:38:44 +0000516 }
517
Lev Walkinabf68892004-10-26 10:12:14 +0000518 ASN_DEBUG("Took %ld bytes to encode %s: [%s]:%ld",
519 (long)consumed_myself, td->name,
Lev Walkin3a4689a2006-11-24 11:20:27 +0000520 (type_variant == ASN_OSUBV_STR) ? (char *)st->buf : "<data>",
Lev Walkinabf68892004-10-26 10:12:14 +0000521 (long)st->size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000522
Lev Walkinf15320b2004-06-03 03:38:44 +0000523
Lev Walkin5c915992004-09-27 20:54:06 +0000524 RETURN(RC_OK);
Lev Walkinf15320b2004-06-03 03:38:44 +0000525}
526
527/*
528 * Encode OCTET STRING type using DER.
529 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000530asn_enc_rval_t
Lev Walkinbbd93252004-10-12 05:57:23 +0000531OCTET_STRING_encode_der(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000532 int tag_mode, ber_tlv_tag_t tag,
533 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000534 asn_enc_rval_t er;
535 asn_OCTET_STRING_specifics_t *specs = td->specifics
Lev Walkindc06f6b2004-10-20 15:50:55 +0000536 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +0800537 : &asn_SPC_OCTET_STRING_specs;
Lev Walkinbbd93252004-10-12 05:57:23 +0000538 BIT_STRING_t *st = (BIT_STRING_t *)sptr;
Lev Walkin3a4689a2006-11-24 11:20:27 +0000539 enum asn_OS_Subvariant type_variant = specs->subvariant;
Lev Walkinbbd93252004-10-12 05:57:23 +0000540 int fix_last_byte = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000541
542 ASN_DEBUG("%s %s as OCTET STRING",
Lev Walkin1f670c12004-09-02 12:57:25 +0000543 cb?"Estimating":"Encoding", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000544
545 /*
Lev Walkinbbd93252004-10-12 05:57:23 +0000546 * Write tags.
Lev Walkinf15320b2004-06-03 03:38:44 +0000547 */
Lev Walkin3a4689a2006-11-24 11:20:27 +0000548 if(type_variant != ASN_OSUBV_ANY || tag_mode == 1) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000549 er.encoded = der_write_tags(td,
Lev Walkin3a4689a2006-11-24 11:20:27 +0000550 (type_variant == ASN_OSUBV_BIT) + st->size,
551 tag_mode, type_variant == ASN_OSUBV_ANY, tag,
552 cb, app_key);
Lev Walkinbbd93252004-10-12 05:57:23 +0000553 if(er.encoded == -1) {
554 er.failed_type = td;
555 er.structure_ptr = sptr;
556 return er;
Lev Walkinf15320b2004-06-03 03:38:44 +0000557 }
Lev Walkinbbd93252004-10-12 05:57:23 +0000558 } else {
559 /* Disallow: [<tag>] IMPLICIT ANY */
Lev Walkin3a4689a2006-11-24 11:20:27 +0000560 assert(type_variant != ASN_OSUBV_ANY || tag_mode != -1);
Lev Walkinbbd93252004-10-12 05:57:23 +0000561 er.encoded = 0;
562 }
563
564 if(!cb) {
Lev Walkin3a4689a2006-11-24 11:20:27 +0000565 er.encoded += (type_variant == ASN_OSUBV_BIT) + st->size;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700566 ASN__ENCODED_OK(er);
Lev Walkinf15320b2004-06-03 03:38:44 +0000567 }
568
Lev Walkin07f388c2004-10-11 11:43:08 +0000569 /*
Lev Walkinbbd93252004-10-12 05:57:23 +0000570 * Prepare to deal with the last octet of BIT STRING.
Lev Walkin07f388c2004-10-11 11:43:08 +0000571 */
Lev Walkin3a4689a2006-11-24 11:20:27 +0000572 if(type_variant == ASN_OSUBV_BIT) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000573 uint8_t b = st->bits_unused & 0x07;
574 if(b && st->size) fix_last_byte = 1;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700575 ASN__CALLBACK(&b, 1);
Lev Walkinbbd93252004-10-12 05:57:23 +0000576 er.encoded++;
Lev Walkinf15320b2004-06-03 03:38:44 +0000577 }
578
Lev Walkinbbd93252004-10-12 05:57:23 +0000579 /* Invoke callback for the main part of the buffer */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700580 ASN__CALLBACK(st->buf, st->size - fix_last_byte);
Lev Walkinf15320b2004-06-03 03:38:44 +0000581
Lev Walkinbbd93252004-10-12 05:57:23 +0000582 /* The last octet should be stripped off the unused bits */
583 if(fix_last_byte) {
584 uint8_t b = st->buf[st->size-1] & (0xff << st->bits_unused);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700585 ASN__CALLBACK(&b, 1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000586 }
587
Lev Walkinbbd93252004-10-12 05:57:23 +0000588 er.encoded += st->size;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700589 ASN__ENCODED_OK(er);
Lev Walkinbbd93252004-10-12 05:57:23 +0000590cb_failed:
Lev Walkin7c1dc052016-03-14 03:08:15 -0700591 ASN__ENCODE_FAILED;
Lev Walkinf15320b2004-06-03 03:38:44 +0000592}
593
Lev Walkina9cc46e2004-09-22 16:06:28 +0000594asn_enc_rval_t
Lev Walkinde4825d2004-09-29 13:20:14 +0000595OCTET_STRING_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000596 int ilevel, enum xer_encoder_flags_e flags,
597 asn_app_consume_bytes_f *cb, void *app_key) {
Wim Lewis59e8d282014-08-04 12:39:35 -0700598 const char * const h2c = "0123456789ABCDEF";
Lev Walkina9cc46e2004-09-22 16:06:28 +0000599 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
600 asn_enc_rval_t er;
601 char scratch[16 * 3 + 4];
602 char *p = scratch;
603 uint8_t *buf;
604 uint8_t *end;
605 size_t i;
606
Lev Walkin2ddd3082006-10-16 12:32:14 +0000607 if(!st || (!st->buf && st->size))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700608 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000609
610 er.encoded = 0;
611
612 /*
613 * Dump the contents of the buffer in hexadecimal.
614 */
615 buf = st->buf;
616 end = buf + st->size;
617 if(flags & XER_F_CANONICAL) {
618 char *scend = scratch + (sizeof(scratch) - 2);
619 for(; buf < end; buf++) {
620 if(p >= scend) {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700621 ASN__CALLBACK(scratch, p - scratch);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000622 er.encoded += p - scratch;
623 p = scratch;
624 }
625 *p++ = h2c[(*buf >> 4) & 0x0F];
626 *p++ = h2c[*buf & 0x0F];
627 }
Lev Walkincc6a9102004-09-23 22:06:26 +0000628
Lev Walkin7c1dc052016-03-14 03:08:15 -0700629 ASN__CALLBACK(scratch, p-scratch); /* Dump the rest */
Lev Walkincc6a9102004-09-23 22:06:26 +0000630 er.encoded += p - scratch;
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 er.encoded += (p-scratch);
636 p = scratch;
Lev Walkinc6cac8e2016-03-14 02:57:07 -0700637 ASN__TEXT_INDENT(1, ilevel);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000638 }
639 *p++ = h2c[(*buf >> 4) & 0x0F];
640 *p++ = h2c[*buf & 0x0F];
641 *p++ = 0x20;
642 }
Lev Walkincc6a9102004-09-23 22:06:26 +0000643 if(p - scratch) {
644 p--; /* Remove the tail space */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700645 ASN__CALLBACK(scratch, p-scratch); /* Dump the rest */
Lev Walkincc6a9102004-09-23 22:06:26 +0000646 er.encoded += p - scratch;
647 if(st->size > 16)
Lev Walkinc6cac8e2016-03-14 02:57:07 -0700648 ASN__TEXT_INDENT(1, ilevel-1);
Lev Walkincc6a9102004-09-23 22:06:26 +0000649 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000650 }
651
Lev Walkin7c1dc052016-03-14 03:08:15 -0700652 ASN__ENCODED_OK(er);
Lev Walkin942fd082004-10-03 09:13:02 +0000653cb_failed:
Lev Walkin7c1dc052016-03-14 03:08:15 -0700654 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000655}
656
Wim Lewis18c2ec92014-07-29 11:30:10 -0700657static const struct OCTET_STRING__xer_escape_table_s {
658 const char *string;
Lev Walkin9ca81892004-10-03 10:54:25 +0000659 int size;
660} OCTET_STRING__xer_escape_table[] = {
661#define OSXET(s) { s, sizeof(s) - 1 }
662 OSXET("\074\156\165\154\057\076"), /* <nul/> */
663 OSXET("\074\163\157\150\057\076"), /* <soh/> */
664 OSXET("\074\163\164\170\057\076"), /* <stx/> */
665 OSXET("\074\145\164\170\057\076"), /* <etx/> */
666 OSXET("\074\145\157\164\057\076"), /* <eot/> */
667 OSXET("\074\145\156\161\057\076"), /* <enq/> */
668 OSXET("\074\141\143\153\057\076"), /* <ack/> */
669 OSXET("\074\142\145\154\057\076"), /* <bel/> */
670 OSXET("\074\142\163\057\076"), /* <bs/> */
671 OSXET("\011"), /* \t */
672 OSXET("\012"), /* \n */
673 OSXET("\074\166\164\057\076"), /* <vt/> */
674 OSXET("\074\146\146\057\076"), /* <ff/> */
675 OSXET("\015"), /* \r */
676 OSXET("\074\163\157\057\076"), /* <so/> */
677 OSXET("\074\163\151\057\076"), /* <si/> */
678 OSXET("\074\144\154\145\057\076"), /* <dle/> */
679 OSXET("\074\144\143\061\057\076"), /* <de1/> */
680 OSXET("\074\144\143\062\057\076"), /* <de2/> */
681 OSXET("\074\144\143\063\057\076"), /* <de3/> */
682 OSXET("\074\144\143\064\057\076"), /* <de4/> */
683 OSXET("\074\156\141\153\057\076"), /* <nak/> */
684 OSXET("\074\163\171\156\057\076"), /* <syn/> */
685 OSXET("\074\145\164\142\057\076"), /* <etb/> */
686 OSXET("\074\143\141\156\057\076"), /* <can/> */
687 OSXET("\074\145\155\057\076"), /* <em/> */
688 OSXET("\074\163\165\142\057\076"), /* <sub/> */
689 OSXET("\074\145\163\143\057\076"), /* <esc/> */
690 OSXET("\074\151\163\064\057\076"), /* <is4/> */
691 OSXET("\074\151\163\063\057\076"), /* <is3/> */
692 OSXET("\074\151\163\062\057\076"), /* <is2/> */
693 OSXET("\074\151\163\061\057\076"), /* <is1/> */
694 { 0, 0 }, /* " " */
695 { 0, 0 }, /* ! */
696 { 0, 0 }, /* \" */
697 { 0, 0 }, /* # */
698 { 0, 0 }, /* $ */
699 { 0, 0 }, /* % */
700 OSXET("\046\141\155\160\073"), /* &amp; */
701 { 0, 0 }, /* ' */
702 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, /* ()*+,-./ */
703 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, /* 01234567 */
704 {0,0},{0,0},{0,0},{0,0}, /* 89:; */
705 OSXET("\046\154\164\073"), /* &lt; */
706 { 0, 0 }, /* = */
707 OSXET("\046\147\164\073"), /* &gt; */
708};
709
Lev Walkindc06f6b2004-10-20 15:50:55 +0000710static int
Lev Walkin0fab1a62005-03-09 22:19:25 +0000711OS__check_escaped_control_char(const void *buf, int size) {
Lev Walkindc06f6b2004-10-20 15:50:55 +0000712 size_t i;
713 /*
714 * Inefficient algorithm which translates the escape sequences
715 * defined above into characters. Returns -1 if not found.
716 * TODO: replace by a faster algorithm (bsearch(), hash or
717 * nested table lookups).
718 */
719 for(i = 0; i < 32 /* Don't spend time on the bottom half */; i++) {
Wim Lewis18c2ec92014-07-29 11:30:10 -0700720 const struct OCTET_STRING__xer_escape_table_s *el;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000721 el = &OCTET_STRING__xer_escape_table[i];
722 if(el->size == size && memcmp(buf, el->string, size) == 0)
723 return i;
724 }
725 return -1;
726}
727
728static int
Lev Walkin0fab1a62005-03-09 22:19:25 +0000729OCTET_STRING__handle_control_chars(void *struct_ptr, const void *chunk_buf, size_t chunk_size) {
Lev Walkindc06f6b2004-10-20 15:50:55 +0000730 /*
731 * This might be one of the escape sequences
732 * for control characters. Check it out.
733 * #11.15.5
734 */
735 int control_char = OS__check_escaped_control_char(chunk_buf,chunk_size);
736 if(control_char >= 0) {
737 OCTET_STRING_t *st = (OCTET_STRING_t *)struct_ptr;
738 void *p = REALLOC(st->buf, st->size + 2);
739 if(p) {
740 st->buf = (uint8_t *)p;
741 st->buf[st->size++] = control_char;
742 st->buf[st->size] = '\0'; /* nul-termination */
743 return 0;
744 }
745 }
746
747 return -1; /* No, it's not */
748}
749
Lev Walkina9cc46e2004-09-22 16:06:28 +0000750asn_enc_rval_t
Lev Walkindc06f6b2004-10-20 15:50:55 +0000751OCTET_STRING_encode_xer_utf8(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000752 int ilevel, enum xer_encoder_flags_e flags,
753 asn_app_consume_bytes_f *cb, void *app_key) {
754 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
755 asn_enc_rval_t er;
Lev Walkin9ca81892004-10-03 10:54:25 +0000756 uint8_t *buf, *end;
757 uint8_t *ss; /* Sequence start */
758 ssize_t encoded_len = 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000759
760 (void)ilevel; /* Unused argument */
761 (void)flags; /* Unused argument */
762
Lev Walkin2ddd3082006-10-16 12:32:14 +0000763 if(!st || (!st->buf && st->size))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700764 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000765
Lev Walkin9ca81892004-10-03 10:54:25 +0000766 buf = st->buf;
767 end = buf + st->size;
768 for(ss = buf; buf < end; buf++) {
Lev Walkin443d2512004-10-05 06:35:31 +0000769 unsigned int ch = *buf;
Lev Walkin9ca81892004-10-03 10:54:25 +0000770 int s_len; /* Special encoding sequence length */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000771
Lev Walkin9ca81892004-10-03 10:54:25 +0000772 /*
773 * Escape certain characters: X.680/11.15
774 */
775 if(ch < sizeof(OCTET_STRING__xer_escape_table)
776 /sizeof(OCTET_STRING__xer_escape_table[0])
777 && (s_len = OCTET_STRING__xer_escape_table[ch].size)) {
778 if(((buf - ss) && cb(ss, buf - ss, app_key) < 0)
779 || cb(OCTET_STRING__xer_escape_table[ch].string, s_len,
780 app_key) < 0)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700781 ASN__ENCODE_FAILED;
Lev Walkin9ca81892004-10-03 10:54:25 +0000782 encoded_len += (buf - ss) + s_len;
783 ss = buf + 1;
784 }
785 }
786
787 encoded_len += (buf - ss);
788 if((buf - ss) && cb(ss, buf - ss, app_key) < 0)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700789 ASN__ENCODE_FAILED;
Lev Walkin9ca81892004-10-03 10:54:25 +0000790
791 er.encoded = encoded_len;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700792 ASN__ENCODED_OK(er);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000793}
794
Lev Walkindc06f6b2004-10-20 15:50:55 +0000795/*
796 * Convert from hexadecimal format (cstring): "AB CD EF"
797 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000798static 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 +0000799 OCTET_STRING_t *st = (OCTET_STRING_t *)sptr;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000800 const char *chunk_stop = (const char *)chunk_buf;
801 const char *p = chunk_stop;
802 const char *pend = p + chunk_size;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000803 unsigned int clv = 0;
804 int half = 0; /* Half bit */
805 uint8_t *buf;
806
807 /* Reallocate buffer according to high cap estimation */
Lev Walkin494fb702017-08-07 20:07:00 -0700808 size_t new_size = st->size + (chunk_size + 1) / 2;
809 void *nptr = REALLOC(st->buf, new_size + 1);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000810 if(!nptr) return -1;
811 st->buf = (uint8_t *)nptr;
812 buf = st->buf + st->size;
813
814 /*
815 * If something like " a b c " appears here, the " a b":3 will be
816 * converted, and the rest skipped. That is, unless buf_size is greater
817 * than chunk_size, then it'll be equivalent to "ABC0".
818 */
819 for(; p < pend; p++) {
Lev Walkin0fab1a62005-03-09 22:19:25 +0000820 int ch = *(const unsigned char *)p;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000821 switch(ch) {
822 case 0x09: case 0x0a: case 0x0c: case 0x0d:
823 case 0x20:
824 /* Ignore whitespace */
825 continue;
826 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: /*01234*/
827 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: /*56789*/
828 clv = (clv << 4) + (ch - 0x30);
829 break;
830 case 0x41: case 0x42: case 0x43: /* ABC */
831 case 0x44: case 0x45: case 0x46: /* DEF */
Lev Walkin3ae21bd2005-02-21 14:43:48 +0000832 clv = (clv << 4) + (ch - 0x41 + 10);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000833 break;
834 case 0x61: case 0x62: case 0x63: /* abc */
835 case 0x64: case 0x65: case 0x66: /* def */
Lev Walkin3ae21bd2005-02-21 14:43:48 +0000836 clv = (clv << 4) + (ch - 0x61 + 10);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000837 break;
838 default:
839 *buf = 0; /* JIC */
840 return -1;
841 }
842 if(half++) {
843 half = 0;
844 *buf++ = clv;
845 chunk_stop = p + 1;
846 }
847 }
848
849 /*
850 * Check partial decoding.
851 */
852 if(half) {
853 if(have_more) {
854 /*
855 * Partial specification is fine,
856 * because no more more PXER_TEXT data is available.
857 */
858 *buf++ = clv << 4;
859 chunk_stop = p;
860 }
861 } else {
862 chunk_stop = p;
863 }
864
865 st->size = buf - st->buf; /* Adjust the buffer size */
Lev Walkin494fb702017-08-07 20:07:00 -0700866 assert(st->size <= new_size);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000867 st->buf[st->size] = 0; /* Courtesy termination */
868
Lev Walkin0fab1a62005-03-09 22:19:25 +0000869 return (chunk_stop - (const char *)chunk_buf); /* Converted size */
Lev Walkindc06f6b2004-10-20 15:50:55 +0000870}
871
872/*
873 * Convert from binary format: "00101011101"
874 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000875static 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 +0000876 BIT_STRING_t *st = (BIT_STRING_t *)sptr;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000877 const char *p = (const char *)chunk_buf;
878 const char *pend = p + chunk_size;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000879 int bits_unused = st->bits_unused & 0x7;
880 uint8_t *buf;
881
882 /* Reallocate buffer according to high cap estimation */
Lev Walkin494fb702017-08-07 20:07:00 -0700883 size_t new_size = st->size + (chunk_size + 7) / 8;
884 void *nptr = REALLOC(st->buf, new_size + 1);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000885 if(!nptr) return -1;
886 st->buf = (uint8_t *)nptr;
887 buf = st->buf + st->size;
888
889 (void)have_more;
890
891 if(bits_unused == 0)
892 bits_unused = 8;
893 else if(st->size)
894 buf--;
895
896 /*
897 * Convert series of 0 and 1 into the octet string.
898 */
899 for(; p < pend; p++) {
Lev Walkin0fab1a62005-03-09 22:19:25 +0000900 int ch = *(const unsigned char *)p;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000901 switch(ch) {
902 case 0x09: case 0x0a: case 0x0c: case 0x0d:
903 case 0x20:
904 /* Ignore whitespace */
905 break;
906 case 0x30:
907 case 0x31:
908 if(bits_unused-- <= 0) {
909 *++buf = 0; /* Clean the cell */
910 bits_unused = 7;
911 }
912 *buf |= (ch&1) << bits_unused;
913 break;
914 default:
915 st->bits_unused = bits_unused;
916 return -1;
917 }
918 }
919
920 if(bits_unused == 8) {
921 st->size = buf - st->buf;
922 st->bits_unused = 0;
923 } else {
924 st->size = buf - st->buf + 1;
925 st->bits_unused = bits_unused;
926 }
927
Lev Walkin494fb702017-08-07 20:07:00 -0700928 assert(st->size <= new_size);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000929 st->buf[st->size] = 0; /* Courtesy termination */
930
931 return chunk_size; /* Converted in full */
932}
933
934/*
935 * Something like strtod(), but with stricter rules.
936 */
937static int
Lev Walkin0fab1a62005-03-09 22:19:25 +0000938OS__strtoent(int base, const char *buf, const char *end, int32_t *ret_value) {
Lev Walkinbeedbde2017-09-17 22:54:51 -0700939 const int32_t last_unicode_codepoint = 0x10ffff;
Lev Walkin33700162004-10-26 09:03:31 +0000940 int32_t val = 0;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000941 const char *p;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000942
943 for(p = buf; p < end; p++) {
944 int ch = *p;
Lev Walkinb0f3db62005-07-03 05:30:15 +0000945
Lev Walkindc06f6b2004-10-20 15:50:55 +0000946 switch(ch) {
947 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: /*01234*/
948 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: /*56789*/
949 val = val * base + (ch - 0x30);
950 break;
951 case 0x41: case 0x42: case 0x43: /* ABC */
952 case 0x44: case 0x45: case 0x46: /* DEF */
Lev Walkin3ae21bd2005-02-21 14:43:48 +0000953 val = val * base + (ch - 0x41 + 10);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000954 break;
955 case 0x61: case 0x62: case 0x63: /* abc */
956 case 0x64: case 0x65: case 0x66: /* def */
Lev Walkin3ae21bd2005-02-21 14:43:48 +0000957 val = val * base + (ch - 0x61 + 10);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000958 break;
959 case 0x3b: /* ';' */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000960 *ret_value = val;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000961 return (p - buf) + 1;
962 default:
963 return -1; /* Character set error */
964 }
Lev Walkinbeedbde2017-09-17 22:54:51 -0700965
966 /* Value exceeds the Unicode range. */
967 if(val > last_unicode_codepoint) {
968 return -1;
969 }
Lev Walkindc06f6b2004-10-20 15:50:55 +0000970 }
971
Lev Walkinb0f3db62005-07-03 05:30:15 +0000972 *ret_value = -1;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000973 return (p - buf);
974}
975
976/*
977 * Convert from the plain UTF-8 format, expanding entity references: "2 &lt; 3"
978 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000979static 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 +0000980 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 Walkinafbf2a92017-09-12 23:30:27 -07001120OCTET_STRING__decode_xer(const asn_codec_ctx_t *opt_codec_ctx,
Lev Walkindc06f6b2004-10-20 15:50:55 +00001121 asn_TYPE_descriptor_t *td, void **sptr,
Lev Walkin8c3b8542005-03-10 18:52:02 +00001122 const char *opt_mname, const void *buf_ptr, size_t size,
Lev Walkindc06f6b2004-10-20 15:50:55 +00001123 int (*opt_unexpected_tag_decoder)
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 ssize_t (*body_receiver)
Lev Walkin0fab1a62005-03-09 22:19:25 +00001126 (void *struct_ptr, const void *chunk_buf, size_t chunk_size,
Lev Walkindc06f6b2004-10-20 15:50:55 +00001127 int have_more)
1128) {
Lev Walkind5125642005-02-14 20:08:00 +00001129 OCTET_STRING_t *st = (OCTET_STRING_t *)*sptr;
Lev Walkindc06f6b2004-10-20 15:50:55 +00001130 asn_OCTET_STRING_specifics_t *specs = td->specifics
1131 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +08001132 : &asn_SPC_OCTET_STRING_specs;
Lev Walkindc06f6b2004-10-20 15:50:55 +00001133 const char *xml_tag = opt_mname ? opt_mname : td->xml_tag;
1134 asn_struct_ctx_t *ctx; /* Per-structure parser context */
Lev Walkinc61f3862005-02-14 17:21:22 +00001135 asn_dec_rval_t rval; /* Return value from the decoder */
1136 int st_allocated;
Lev Walkindc06f6b2004-10-20 15:50:55 +00001137
1138 /*
1139 * Create the string if does not exist.
1140 */
Lev Walkinc61f3862005-02-14 17:21:22 +00001141 if(!st) {
Lev Walkin8484ed82004-12-14 13:31:01 +00001142 st = (OCTET_STRING_t *)CALLOC(1, specs->struct_size);
Lev Walkin8484ed82004-12-14 13:31:01 +00001143 *sptr = (void *)st;
Lev Walkinc61f3862005-02-14 17:21:22 +00001144 if(!st) goto sta_failed;
1145 st_allocated = 1;
Lev Walkinbdaae772005-02-18 12:25:47 +00001146 } else {
1147 st_allocated = 0;
1148 }
Lev Walkinc61f3862005-02-14 17:21:22 +00001149 if(!st->buf) {
1150 /* This is separate from above section */
1151 st->buf = (uint8_t *)CALLOC(1, 1);
1152 if(!st->buf) {
1153 if(st_allocated) {
1154 *sptr = 0;
1155 goto stb_failed;
1156 } else {
1157 goto sta_failed;
1158 }
Lev Walkindc06f6b2004-10-20 15:50:55 +00001159 }
1160 }
1161
1162 /* Restore parsing context */
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001163 ctx = (asn_struct_ctx_t *)(((char *)*sptr) + specs->ctx_offset);
Lev Walkindc06f6b2004-10-20 15:50:55 +00001164
1165 return xer_decode_general(opt_codec_ctx, ctx, *sptr, xml_tag,
1166 buf_ptr, size, opt_unexpected_tag_decoder, body_receiver);
Lev Walkinc61f3862005-02-14 17:21:22 +00001167
1168stb_failed:
1169 FREEMEM(st);
1170sta_failed:
1171 rval.code = RC_FAIL;
1172 rval.consumed = 0;
1173 return rval;
Lev Walkindc06f6b2004-10-20 15:50:55 +00001174}
1175
1176/*
1177 * Decode OCTET STRING from the hexadecimal data.
1178 */
1179asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -07001180OCTET_STRING_decode_xer_hex(const asn_codec_ctx_t *opt_codec_ctx,
Lev Walkindc06f6b2004-10-20 15:50:55 +00001181 asn_TYPE_descriptor_t *td, void **sptr,
Lev Walkin8c3b8542005-03-10 18:52:02 +00001182 const char *opt_mname, const void *buf_ptr, size_t size) {
Lev Walkindc06f6b2004-10-20 15:50:55 +00001183 return OCTET_STRING__decode_xer(opt_codec_ctx, td, sptr, opt_mname,
1184 buf_ptr, size, 0, OCTET_STRING__convert_hexadecimal);
1185}
1186
1187/*
1188 * Decode OCTET STRING from the binary (0/1) data.
1189 */
1190asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -07001191OCTET_STRING_decode_xer_binary(const asn_codec_ctx_t *opt_codec_ctx,
Lev Walkindc06f6b2004-10-20 15:50:55 +00001192 asn_TYPE_descriptor_t *td, void **sptr,
Lev Walkin8c3b8542005-03-10 18:52:02 +00001193 const char *opt_mname, const void *buf_ptr, size_t size) {
Lev Walkindc06f6b2004-10-20 15:50:55 +00001194 return OCTET_STRING__decode_xer(opt_codec_ctx, td, sptr, opt_mname,
1195 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 Walkindc06f6b2004-10-20 15:50:55 +00001203 asn_TYPE_descriptor_t *td, void **sptr,
Lev Walkin8c3b8542005-03-10 18:52:02 +00001204 const char *opt_mname, const void *buf_ptr, size_t size) {
Lev Walkindc06f6b2004-10-20 15:50:55 +00001205 return OCTET_STRING__decode_xer(opt_codec_ctx, td, sptr, opt_mname,
1206 buf_ptr, size,
1207 OCTET_STRING__handle_control_chars,
1208 OCTET_STRING__convert_entrefs);
1209}
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 Walkin725883b2006-10-09 12:07:58 +00001256 int code = per_get_few_bits(po, unit_bits);
1257 int ch = code + lb;
1258 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 Walkin62d76872017-08-05 22:49:42 -07001340#ifndef ASN_DISABLE_PER_SUPPORT
1341
Lev Walkin59b176e2005-11-26 11:25:14 +00001342asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -07001343OCTET_STRING_decode_uper(const asn_codec_ctx_t *opt_codec_ctx,
Lev Walkin494fb702017-08-07 20:07:00 -07001344 asn_TYPE_descriptor_t *td,
1345 const asn_per_constraints_t *constraints, void **sptr,
1346 asn_per_data_t *pd) {
1347 asn_OCTET_STRING_specifics_t *specs = td->specifics
Lev Walkin59b176e2005-11-26 11:25:14 +00001348 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +08001349 : &asn_SPC_OCTET_STRING_specs;
Lev Walkin494fb702017-08-07 20:07:00 -07001350 const asn_per_constraints_t *pc =
1351 constraints ? constraints : td->per_constraints;
1352 const asn_per_constraint_t *cval;
1353 const asn_per_constraint_t *csiz;
Lev Walkin59b176e2005-11-26 11:25:14 +00001354 asn_dec_rval_t rval = { RC_OK, 0 };
1355 BIT_STRING_t *st = (BIT_STRING_t *)*sptr;
1356 ssize_t consumed_myself = 0;
1357 int repeat;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001358 enum {
1359 OS__BPC_BIT = 0,
1360 OS__BPC_CHAR = 1,
1361 OS__BPC_U16 = 2,
1362 OS__BPC_U32 = 4
1363 } bpc; /* Bytes per character */
1364 unsigned int unit_bits;
1365 unsigned int canonical_unit_bits;
Lev Walkin59b176e2005-11-26 11:25:14 +00001366
1367 (void)opt_codec_ctx;
1368
Lev Walkin3a4689a2006-11-24 11:20:27 +00001369 if(pc) {
1370 cval = &pc->value;
1371 csiz = &pc->size;
1372 } else {
1373 cval = &asn_DEF_OCTET_STRING_constraints.value;
1374 csiz = &asn_DEF_OCTET_STRING_constraints.size;
1375 }
1376
1377 switch(specs->subvariant) {
1378 default:
1379 case ASN_OSUBV_ANY:
1380 ASN_DEBUG("Unrecognized subvariant %d", specs->subvariant);
1381 RETURN(RC_FAIL);
1382 case ASN_OSUBV_BIT:
1383 canonical_unit_bits = unit_bits = 1;
1384 bpc = OS__BPC_BIT;
1385 break;
1386 case ASN_OSUBV_STR:
1387 canonical_unit_bits = unit_bits = 8;
1388 if(cval->flags & APC_CONSTRAINED)
1389 unit_bits = cval->range_bits;
1390 bpc = OS__BPC_CHAR;
1391 break;
1392 case ASN_OSUBV_U16:
1393 canonical_unit_bits = unit_bits = 16;
1394 if(cval->flags & APC_CONSTRAINED)
1395 unit_bits = cval->range_bits;
1396 bpc = OS__BPC_U16;
1397 break;
1398 case ASN_OSUBV_U32:
1399 canonical_unit_bits = unit_bits = 32;
1400 if(cval->flags & APC_CONSTRAINED)
1401 unit_bits = cval->range_bits;
1402 bpc = OS__BPC_U32;
1403 break;
1404 }
1405
Lev Walkin59b176e2005-11-26 11:25:14 +00001406 /*
1407 * Allocate the string.
1408 */
1409 if(!st) {
1410 st = (BIT_STRING_t *)(*sptr = CALLOC(1, specs->struct_size));
1411 if(!st) RETURN(RC_FAIL);
1412 }
1413
Lev Walkin725883b2006-10-09 12:07:58 +00001414 ASN_DEBUG("PER Decoding %s size %ld .. %ld bits %d",
Lev Walkin3a4689a2006-11-24 11:20:27 +00001415 csiz->flags & APC_EXTENSIBLE ? "extensible" : "non-extensible",
1416 csiz->lower_bound, csiz->upper_bound, csiz->effective_bits);
Lev Walkince676dd2005-12-20 22:34:55 +00001417
Lev Walkin3a4689a2006-11-24 11:20:27 +00001418 if(csiz->flags & APC_EXTENSIBLE) {
Lev Walkin59b176e2005-11-26 11:25:14 +00001419 int inext = per_get_few_bits(pd, 1);
Lev Walkin0a8aa602006-09-18 20:05:55 +00001420 if(inext < 0) RETURN(RC_WMORE);
Lev Walkin3a4689a2006-11-24 11:20:27 +00001421 if(inext) {
1422 csiz = &asn_DEF_OCTET_STRING_constraints.size;
1423 cval = &asn_DEF_OCTET_STRING_constraints.value;
1424 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 Walkin3a4689a2006-11-24 11:20:27 +00001460 if(bpc == 0) {
1461 int ubs = (csiz->upper_bound & 0x7);
1462 st->bits_unused = ubs ? 8 - ubs : 0;
1463 }
Lev Walkin59b176e2005-11-26 11:25:14 +00001464 RETURN(RC_OK);
1465 }
1466
1467 st->size = 0;
1468 do {
Lev Walkin3a4689a2006-11-24 11:20:27 +00001469 ssize_t raw_len;
Lev Walkin59b176e2005-11-26 11:25:14 +00001470 ssize_t len_bytes;
1471 ssize_t len_bits;
1472 void *p;
1473 int ret;
1474
1475 /* Get the PER length */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001476 raw_len = uper_get_length(pd, csiz->effective_bits, &repeat);
1477 if(raw_len < 0) RETURN(RC_WMORE);
1478 raw_len += csiz->lower_bound;
Lev Walkin59b176e2005-11-26 11:25:14 +00001479
Lev Walkin0a8aa602006-09-18 20:05:55 +00001480 ASN_DEBUG("Got PER length eb %ld, len %ld, %s (%s)",
Lev Walkin3a4689a2006-11-24 11:20:27 +00001481 (long)csiz->effective_bits, (long)raw_len,
Lev Walkin0a8aa602006-09-18 20:05:55 +00001482 repeat ? "repeat" : "once", td->name);
Lev Walkin3a4689a2006-11-24 11:20:27 +00001483 if(bpc) {
1484 len_bytes = raw_len * bpc;
1485 len_bits = len_bytes * unit_bits;
1486 } else {
1487 len_bits = raw_len;
Lev Walkin59b176e2005-11-26 11:25:14 +00001488 len_bytes = (len_bits + 7) >> 3;
1489 if(len_bits & 0x7)
1490 st->bits_unused = 8 - (len_bits & 0x7);
1491 /* len_bits be multiple of 16K if repeat is set */
Lev Walkin59b176e2005-11-26 11:25:14 +00001492 }
1493 p = REALLOC(st->buf, st->size + len_bytes + 1);
1494 if(!p) RETURN(RC_FAIL);
1495 st->buf = (uint8_t *)p;
1496
Lev Walkin3a4689a2006-11-24 11:20:27 +00001497 if(bpc) {
1498 ret = OCTET_STRING_per_get_characters(pd,
1499 &st->buf[st->size], raw_len, bpc, unit_bits,
1500 cval->lower_bound, cval->upper_bound, pc);
Lev Walkin725883b2006-10-09 12:07:58 +00001501 if(ret > 0) RETURN(RC_FAIL);
1502 } else {
1503 ret = per_get_many_bits(pd, &st->buf[st->size],
1504 0, len_bits);
1505 }
Lev Walkin0a8aa602006-09-18 20:05:55 +00001506 if(ret < 0) RETURN(RC_WMORE);
Lev Walkin59b176e2005-11-26 11:25:14 +00001507 st->size += len_bytes;
1508 } while(repeat);
1509 st->buf[st->size] = 0; /* nul-terminate */
1510
1511 return rval;
1512}
Lev Walkindc06f6b2004-10-20 15:50:55 +00001513
Lev Walkin523de9e2006-08-18 01:34:18 +00001514asn_enc_rval_t
1515OCTET_STRING_encode_uper(asn_TYPE_descriptor_t *td,
Lev Walkin494fb702017-08-07 20:07:00 -07001516 const asn_per_constraints_t *constraints, void *sptr,
1517 asn_per_outp_t *po) {
1518 asn_OCTET_STRING_specifics_t *specs = td->specifics
Lev Walkin523de9e2006-08-18 01:34:18 +00001519 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +08001520 : &asn_SPC_OCTET_STRING_specs;
Lev Walkin494fb702017-08-07 20:07:00 -07001521 const asn_per_constraints_t *pc = constraints ? constraints
Lev Walkin725883b2006-10-09 12:07:58 +00001522 : td->per_constraints;
Lev Walkin494fb702017-08-07 20:07:00 -07001523 const asn_per_constraint_t *cval;
1524 const asn_per_constraint_t *csiz;
Lev Walkin523de9e2006-08-18 01:34:18 +00001525 const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001526 asn_enc_rval_t er = { 0, 0, 0 };
Lev Walkin523de9e2006-08-18 01:34:18 +00001527 int inext = 0; /* Lies not within extension root */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001528 unsigned int unit_bits;
1529 unsigned int canonical_unit_bits;
1530 unsigned int sizeinunits;
Lev Walkin523de9e2006-08-18 01:34:18 +00001531 const uint8_t *buf;
1532 int ret;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001533 enum {
1534 OS__BPC_BIT = 0,
1535 OS__BPC_CHAR = 1,
1536 OS__BPC_U16 = 2,
1537 OS__BPC_U32 = 4
1538 } bpc; /* Bytes per character */
1539 int ct_extensible;
Lev Walkin523de9e2006-08-18 01:34:18 +00001540
Lev Walkin2ddd3082006-10-16 12:32:14 +00001541 if(!st || (!st->buf && st->size))
Lev Walkin7c1dc052016-03-14 03:08:15 -07001542 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001543
Lev Walkin3a4689a2006-11-24 11:20:27 +00001544 if(pc) {
1545 cval = &pc->value;
1546 csiz = &pc->size;
1547 } else {
1548 cval = &asn_DEF_OCTET_STRING_constraints.value;
1549 csiz = &asn_DEF_OCTET_STRING_constraints.size;
1550 }
1551 ct_extensible = csiz->flags & APC_EXTENSIBLE;
1552
1553 switch(specs->subvariant) {
1554 default:
1555 case ASN_OSUBV_ANY:
Lev Walkin7c1dc052016-03-14 03:08:15 -07001556 ASN__ENCODE_FAILED;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001557 case ASN_OSUBV_BIT:
1558 canonical_unit_bits = unit_bits = 1;
1559 bpc = OS__BPC_BIT;
1560 sizeinunits = st->size * 8 - (st->bits_unused & 0x07);
Lev Walkin523de9e2006-08-18 01:34:18 +00001561 ASN_DEBUG("BIT STRING of %d bytes, %d bits unused",
1562 sizeinunits, st->bits_unused);
Lev Walkin3a4689a2006-11-24 11:20:27 +00001563 break;
1564 case ASN_OSUBV_STR:
1565 canonical_unit_bits = unit_bits = 8;
1566 if(cval->flags & APC_CONSTRAINED)
1567 unit_bits = cval->range_bits;
1568 bpc = OS__BPC_CHAR;
1569 sizeinunits = st->size;
1570 break;
1571 case ASN_OSUBV_U16:
1572 canonical_unit_bits = unit_bits = 16;
1573 if(cval->flags & APC_CONSTRAINED)
1574 unit_bits = cval->range_bits;
1575 bpc = OS__BPC_U16;
Lev Walkin4ef01a22017-09-06 22:54:39 -07001576 sizeinunits = st->size >> 1;
1577 if(st->size & 1) {
1578 ASN_DEBUG("%s string size is not modulo 2", td->name);
1579 ASN__ENCODE_FAILED;
1580 }
Lev Walkin3a4689a2006-11-24 11:20:27 +00001581 break;
1582 case ASN_OSUBV_U32:
1583 canonical_unit_bits = unit_bits = 32;
1584 if(cval->flags & APC_CONSTRAINED)
1585 unit_bits = cval->range_bits;
1586 bpc = OS__BPC_U32;
Lev Walkin4ef01a22017-09-06 22:54:39 -07001587 sizeinunits = st->size >> 2;
1588 if(st->size & 3) {
1589 ASN_DEBUG("%s string size is not modulo 4", td->name);
1590 ASN__ENCODE_FAILED;
1591 }
Lev Walkin3a4689a2006-11-24 11:20:27 +00001592 break;
Lev Walkin725883b2006-10-09 12:07:58 +00001593 }
1594
Lev Walkin00918812006-09-18 21:19:32 +00001595 ASN_DEBUG("Encoding %s into %d units of %d bits"
Lev Walkin725883b2006-10-09 12:07:58 +00001596 " (%ld..%ld, effective %d)%s",
Lev Walkin00918812006-09-18 21:19:32 +00001597 td->name, sizeinunits, unit_bits,
Lev Walkin3a4689a2006-11-24 11:20:27 +00001598 csiz->lower_bound, csiz->upper_bound,
1599 csiz->effective_bits, ct_extensible ? " EXT" : "");
Lev Walkin523de9e2006-08-18 01:34:18 +00001600
Lev Walkind4c16732013-03-28 05:00:47 -07001601 /* Figure out whether size lies within PER visible constraint */
Lev Walkin523de9e2006-08-18 01:34:18 +00001602
Lev Walkin3a4689a2006-11-24 11:20:27 +00001603 if(csiz->effective_bits >= 0) {
1604 if((int)sizeinunits < csiz->lower_bound
1605 || (int)sizeinunits > csiz->upper_bound) {
Lev Walkin523de9e2006-08-18 01:34:18 +00001606 if(ct_extensible) {
Lev Walkin3a4689a2006-11-24 11:20:27 +00001607 cval = &asn_DEF_OCTET_STRING_constraints.value;
1608 csiz = &asn_DEF_OCTET_STRING_constraints.size;
1609 unit_bits = canonical_unit_bits;
Lev Walkin523de9e2006-08-18 01:34:18 +00001610 inext = 1;
Lev Walkin4ef01a22017-09-06 22:54:39 -07001611 } else {
Lev Walkin7c1dc052016-03-14 03:08:15 -07001612 ASN__ENCODE_FAILED;
Lev Walkin4ef01a22017-09-06 22:54:39 -07001613 }
Lev Walkin523de9e2006-08-18 01:34:18 +00001614 }
1615 } else {
1616 inext = 0;
1617 }
1618
1619 if(ct_extensible) {
1620 /* Declare whether length is [not] within extension root */
1621 if(per_put_few_bits(po, inext, 1))
Lev Walkin7c1dc052016-03-14 03:08:15 -07001622 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001623 }
1624
1625 /* X.691, #16.5: zero-length encoding */
1626 /* X.691, #16.6: short fixed length encoding (up to 2 octets) */
1627 /* X.691, #16.7: long fixed length encoding (up to 64K octets) */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001628 if(csiz->effective_bits >= 0) {
Lev Walkin24810022017-08-25 12:16:11 -07001629 ASN_DEBUG("Encoding %zu bytes (%ld), length in %d bits",
Lev Walkin3a4689a2006-11-24 11:20:27 +00001630 st->size, sizeinunits - csiz->lower_bound,
1631 csiz->effective_bits);
1632 ret = per_put_few_bits(po, sizeinunits - csiz->lower_bound,
1633 csiz->effective_bits);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001634 if(ret) ASN__ENCODE_FAILED;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001635 if(bpc) {
1636 ret = OCTET_STRING_per_put_characters(po, st->buf,
1637 sizeinunits, bpc, unit_bits,
1638 cval->lower_bound, cval->upper_bound, pc);
Lev Walkin725883b2006-10-09 12:07:58 +00001639 } else {
1640 ret = per_put_many_bits(po, st->buf,
1641 sizeinunits * unit_bits);
1642 }
Lev Walkin7c1dc052016-03-14 03:08:15 -07001643 if(ret) ASN__ENCODE_FAILED;
1644 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +00001645 }
1646
Lev Walkin24810022017-08-25 12:16:11 -07001647 ASN_DEBUG("Encoding %zu bytes", st->size);
Lev Walkin523de9e2006-08-18 01:34:18 +00001648
1649 if(sizeinunits == 0) {
1650 if(uper_put_length(po, 0))
Lev Walkin7c1dc052016-03-14 03:08:15 -07001651 ASN__ENCODE_FAILED;
1652 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +00001653 }
1654
1655 buf = st->buf;
1656 while(sizeinunits) {
1657 ssize_t maySave = uper_put_length(po, sizeinunits);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001658 if(maySave < 0) ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001659
Lev Walkin345e4c62006-10-19 02:44:08 +00001660 ASN_DEBUG("Encoding %ld of %ld",
1661 (long)maySave, (long)sizeinunits);
Lev Walkin523de9e2006-08-18 01:34:18 +00001662
Lev Walkin3a4689a2006-11-24 11:20:27 +00001663 if(bpc) {
1664 ret = OCTET_STRING_per_put_characters(po, buf,
1665 maySave, bpc, unit_bits,
1666 cval->lower_bound, cval->upper_bound, pc);
Lev Walkin725883b2006-10-09 12:07:58 +00001667 } else {
1668 ret = per_put_many_bits(po, buf, maySave * unit_bits);
1669 }
Lev Walkin7c1dc052016-03-14 03:08:15 -07001670 if(ret) ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001671
Lev Walkin3a4689a2006-11-24 11:20:27 +00001672 if(bpc)
1673 buf += maySave * bpc;
Lev Walkin523de9e2006-08-18 01:34:18 +00001674 else
Lev Walkin3a4689a2006-11-24 11:20:27 +00001675 buf += maySave >> 3;
Lev Walkin523de9e2006-08-18 01:34:18 +00001676 sizeinunits -= maySave;
1677 assert(!(maySave & 0x07) || !sizeinunits);
1678 }
1679
Lev Walkin7c1dc052016-03-14 03:08:15 -07001680 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +00001681}
1682
Lev Walkin62d76872017-08-05 22:49:42 -07001683#endif /* ASN_DISABLE_PER_SUPPORT */
1684
Lev Walkinf15320b2004-06-03 03:38:44 +00001685int
Lev Walkinde4825d2004-09-29 13:20:14 +00001686OCTET_STRING_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +00001687 asn_app_consume_bytes_f *cb, void *app_key) {
Wim Lewis59e8d282014-08-04 12:39:35 -07001688 const char * const h2c = "0123456789ABCDEF";
Lev Walkinc2346572004-08-11 09:07:36 +00001689 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +00001690 char scratch[16 * 3 + 4];
1691 char *p = scratch;
1692 uint8_t *buf;
1693 uint8_t *end;
1694 size_t i;
Lev Walkinf15320b2004-06-03 03:38:44 +00001695
Lev Walkind9bd7752004-06-05 08:17:50 +00001696 (void)td; /* Unused argument */
1697
Lev Walkin2ddd3082006-10-16 12:32:14 +00001698 if(!st || (!st->buf && st->size))
1699 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001700
1701 /*
1702 * Dump the contents of the buffer in hexadecimal.
1703 */
1704 buf = st->buf;
1705 end = buf + st->size;
1706 for(i = 0; buf < end; buf++, i++) {
1707 if(!(i % 16) && (i || st->size > 16)) {
Lev Walkin8e8078a2004-09-26 13:10:40 +00001708 if(cb(scratch, p - scratch, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +00001709 return -1;
Lev Walkin8e8078a2004-09-26 13:10:40 +00001710 _i_INDENT(1);
Lev Walkinf15320b2004-06-03 03:38:44 +00001711 p = scratch;
1712 }
1713 *p++ = h2c[(*buf >> 4) & 0x0F];
1714 *p++ = h2c[*buf & 0x0F];
Lev Walkina9cc46e2004-09-22 16:06:28 +00001715 *p++ = 0x20;
Lev Walkinf15320b2004-06-03 03:38:44 +00001716 }
1717
Lev Walkincc6a9102004-09-23 22:06:26 +00001718 if(p > scratch) {
1719 p--; /* Remove the tail space */
Lev Walkin8e8078a2004-09-26 13:10:40 +00001720 if(cb(scratch, p - scratch, app_key) < 0)
Lev Walkincc6a9102004-09-23 22:06:26 +00001721 return -1;
1722 }
1723
1724 return 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001725}
1726
1727int
Lev Walkindc06f6b2004-10-20 15:50:55 +00001728OCTET_STRING_print_utf8(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +00001729 int ilevel, asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +00001730 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +00001731
Lev Walkind9bd7752004-06-05 08:17:50 +00001732 (void)td; /* Unused argument */
1733 (void)ilevel; /* Unused argument */
1734
Lev Walkin2ddd3082006-10-16 12:32:14 +00001735 if(st && (st->buf || !st->size)) {
Lev Walkin8e8078a2004-09-26 13:10:40 +00001736 return (cb(st->buf, st->size, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001737 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +00001738 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001739 }
1740}
1741
1742void
Lev Walkinf6853ce2017-08-11 00:50:27 -07001743OCTET_STRING_free(const asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkin8d99d7b2017-08-25 01:06:00 -07001744 enum asn_struct_free_method method) {
1745 OCTET_STRING_t *st = (OCTET_STRING_t *)sptr;
Lev Walkin034ee192016-03-14 02:28:33 -07001746 asn_OCTET_STRING_specifics_t *specs;
1747 asn_struct_ctx_t *ctx;
Lev Walkinb54577a2004-11-08 10:47:12 +00001748 struct _stack *stck;
Lev Walkinf15320b2004-06-03 03:38:44 +00001749
1750 if(!td || !st)
1751 return;
1752
Lev Walkin034ee192016-03-14 02:28:33 -07001753 specs = td->specifics
1754 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +08001755 : &asn_SPC_OCTET_STRING_specs;
Lev Walkin034ee192016-03-14 02:28:33 -07001756 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
1757
Lev Walkinf15320b2004-06-03 03:38:44 +00001758 ASN_DEBUG("Freeing %s as OCTET STRING", td->name);
1759
1760 if(st->buf) {
1761 FREEMEM(st->buf);
Lev Walkin2ddd3082006-10-16 12:32:14 +00001762 st->buf = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001763 }
1764
1765 /*
1766 * Remove decode-time stack.
1767 */
Lev Walkinb54577a2004-11-08 10:47:12 +00001768 stck = (struct _stack *)ctx->ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +00001769 if(stck) {
1770 while(stck->tail) {
1771 struct _stack_el *sel = stck->tail;
1772 stck->tail = sel->prev;
1773 FREEMEM(sel);
1774 }
1775 FREEMEM(stck);
1776 }
1777
Lev Walkin8d99d7b2017-08-25 01:06:00 -07001778 switch(method) {
1779 case ASFM_FREE_EVERYTHING:
1780 FREEMEM(sptr);
1781 break;
1782 case ASFM_FREE_UNDERLYING:
1783 break;
1784 case ASFM_FREE_UNDERLYING_AND_RESET:
1785 memset(sptr, 0,
1786 ((asn_OCTET_STRING_specifics_t *)(td->specifics))->struct_size);
1787 break;
1788 }
Lev Walkinf15320b2004-06-03 03:38:44 +00001789}
1790
1791/*
1792 * Conversion routines.
1793 */
1794int
1795OCTET_STRING_fromBuf(OCTET_STRING_t *st, const char *str, int len) {
1796 void *buf;
1797
1798 if(st == 0 || (str == 0 && len)) {
1799 errno = EINVAL;
1800 return -1;
1801 }
1802
1803 /*
1804 * Clear the OCTET STRING.
1805 */
1806 if(str == NULL) {
Lev Walkin7b284812005-12-17 11:43:25 +00001807 FREEMEM(st->buf);
1808 st->buf = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001809 st->size = 0;
1810 return 0;
1811 }
1812
1813 /* Determine the original string size, if not explicitly given */
1814 if(len < 0)
1815 len = strlen(str);
1816
1817 /* Allocate and fill the memory */
1818 buf = MALLOC(len + 1);
Lev Walkin7b284812005-12-17 11:43:25 +00001819 if(buf == NULL)
Lev Walkinf15320b2004-06-03 03:38:44 +00001820 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +00001821
1822 memcpy(buf, str, len);
Lev Walkin7b284812005-12-17 11:43:25 +00001823 ((uint8_t *)buf)[len] = '\0'; /* Couldn't use memcpy(len+1)! */
1824 FREEMEM(st->buf);
1825 st->buf = (uint8_t *)buf;
1826 st->size = len;
Lev Walkinf15320b2004-06-03 03:38:44 +00001827
1828 return 0;
1829}
1830
1831OCTET_STRING_t *
Lev Walkinbbd93252004-10-12 05:57:23 +00001832OCTET_STRING_new_fromBuf(asn_TYPE_descriptor_t *td, const char *str, int len) {
1833 asn_OCTET_STRING_specifics_t *specs = td->specifics
Lev Walkindc06f6b2004-10-20 15:50:55 +00001834 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +08001835 : &asn_SPC_OCTET_STRING_specs;
Lev Walkinf15320b2004-06-03 03:38:44 +00001836 OCTET_STRING_t *st;
1837
Lev Walkinbbd93252004-10-12 05:57:23 +00001838 st = (OCTET_STRING_t *)CALLOC(1, specs->struct_size);
Lev Walkinf15320b2004-06-03 03:38:44 +00001839 if(st && str && OCTET_STRING_fromBuf(st, str, len)) {
Lev Walkin419f6752006-09-13 04:02:00 +00001840 FREEMEM(st);
Lev Walkinf15320b2004-06-03 03:38:44 +00001841 st = NULL;
1842 }
1843
1844 return st;
1845}
1846
Lev Walkincd2f48e2017-08-10 02:14:59 -07001847/*
1848 * Lexicographically compare the common prefix of both strings,
1849 * and if it is the same return -1 for the smallest string.
1850 */
1851int
1852OCTET_STRING_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
1853 const void *bptr) {
1854 const OCTET_STRING_t *a = aptr;
1855 const OCTET_STRING_t *b = bptr;
1856
1857 (void)td;
1858
1859 if(a && b) {
1860 size_t common_prefix_size = a->size <= b->size ? a->size : b->size;
1861 int ret = memcmp(a->buf, b->buf, common_prefix_size);
1862 if(ret == 0) {
1863 /* Figure out which string with equal prefixes is longer. */
1864 if(a->size < b->size) {
1865 return -1;
1866 } else if(a->size > b->size) {
1867 return 1;
1868 } else {
Lev Walkin86526e32017-09-06 23:16:13 -07001869 asn_OCTET_STRING_specifics_t *specs = td->specifics;
1870 if(specs && specs->subvariant == ASN_OSUBV_BIT) {
1871 const BIT_STRING_t *ba = aptr;
1872 const BIT_STRING_t *bb = bptr;
1873 if(ba->bits_unused > bb->bits_unused) {
1874 return -1;
1875 } else if(ba->bits_unused < bb->bits_unused) {
1876 return 1;
1877 } else {
1878 return 0;
1879 }
1880 }
Lev Walkincd2f48e2017-08-10 02:14:59 -07001881 return 0;
1882 }
1883 } else {
1884 return ret;
1885 }
1886 } else if(!a && !b) {
1887 return 0;
1888 } else if(!a) {
1889 return -1;
1890 } else {
1891 return 1;
1892 }
1893
1894}
1895