blob: 7cc7ebd44b6d1e5267825b8ed8e75e77aa936b54 [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};
Lev Walkinde4825d2004-09-29 13:20:14 +000027asn_TYPE_descriptor_t asn_DEF_OCTET_STRING = {
Lev Walkindc06f6b2004-10-20 15:50:55 +000028 "OCTET STRING", /* Canonical name */
29 "OCTET_STRING", /* XML tag name */
Lev Walkina9cc46e2004-09-22 16:06:28 +000030 OCTET_STRING_free,
Lev Walkincd2f48e2017-08-10 02:14:59 -070031 OCTET_STRING_print, /* OCTET STRING generally means a non-ascii sequence */
32 OCTET_STRING_compare,
Lev Walkinf15320b2004-06-03 03:38:44 +000033 asn_generic_no_constraint,
34 OCTET_STRING_decode_ber,
35 OCTET_STRING_encode_der,
Lev Walkindc06f6b2004-10-20 15:50:55 +000036 OCTET_STRING_decode_xer_hex,
Lev Walkina9cc46e2004-09-22 16:06:28 +000037 OCTET_STRING_encode_xer,
Lev Walkincc159472017-07-06 08:26:36 -070038#ifdef ASN_DISABLE_OER_SUPPORT
39 0,
40 0,
41#else
42 0,
43 0,
44#endif /* ASN_DISABLE_OER_SUPPORT */
Lev Walkinb33425f2017-07-14 14:59:52 +040045#ifdef ASN_DISABLE_PER_SUPPORT
46 0,
47 0,
48#else
49 OCTET_STRING_decode_uper, /* Unaligned PER decoder */
50 OCTET_STRING_encode_uper, /* Unaligned PER encoder */
51#endif /* ASN_DISABLE_PER_SUPPORT */
Lev Walkinf15320b2004-06-03 03:38:44 +000052 0, /* Use generic outmost tag fetcher */
Lev Walkinde4825d2004-09-29 13:20:14 +000053 asn_DEF_OCTET_STRING_tags,
54 sizeof(asn_DEF_OCTET_STRING_tags)
55 / sizeof(asn_DEF_OCTET_STRING_tags[0]),
56 asn_DEF_OCTET_STRING_tags, /* Same as above */
57 sizeof(asn_DEF_OCTET_STRING_tags)
58 / sizeof(asn_DEF_OCTET_STRING_tags[0]),
Lev Walkin76780762017-07-07 10:07:30 -070059 0, /* No OER visible constraints */
Lev Walkin59b176e2005-11-26 11:25:14 +000060 0, /* No PER visible constraints */
Lev Walkin449f8322004-08-20 13:23:42 +000061 0, 0, /* No members */
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +080062 &asn_SPC_OCTET_STRING_specs
Lev Walkinf15320b2004-06-03 03:38:44 +000063};
64
Lev Walkincc6a9102004-09-23 22:06:26 +000065#undef _CH_PHASE
66#undef NEXT_PHASE
67#undef PREV_PHASE
Lev Walkin75b1bef2005-04-25 19:38:21 +000068#define _CH_PHASE(ctx, inc) do { \
69 if(ctx->phase == 0) \
70 ctx->context = 0; \
71 ctx->phase += inc; \
Lev Walkinf15320b2004-06-03 03:38:44 +000072 } while(0)
73#define NEXT_PHASE(ctx) _CH_PHASE(ctx, +1)
74#define PREV_PHASE(ctx) _CH_PHASE(ctx, -1)
75
Lev Walkincc6a9102004-09-23 22:06:26 +000076#undef ADVANCE
Lev Walkin75b1bef2005-04-25 19:38:21 +000077#define ADVANCE(num_bytes) do { \
78 size_t num = (num_bytes); \
79 buf_ptr = ((const char *)buf_ptr) + num; \
80 size -= num; \
81 consumed_myself += num; \
Lev Walkinf15320b2004-06-03 03:38:44 +000082 } while(0)
83
Lev Walkincc6a9102004-09-23 22:06:26 +000084#undef RETURN
Lev Walkin75b1bef2005-04-25 19:38:21 +000085#define RETURN(_code) do { \
Lev Walkin59b176e2005-11-26 11:25:14 +000086 asn_dec_rval_t tmprval; \
87 tmprval.code = _code; \
88 tmprval.consumed = consumed_myself; \
89 return tmprval; \
Lev Walkinf15320b2004-06-03 03:38:44 +000090 } while(0)
91
Lev Walkincc6a9102004-09-23 22:06:26 +000092#undef APPEND
Lev Walkind9bd7752004-06-05 08:17:50 +000093#define APPEND(bufptr, bufsize) do { \
Lev Walkin75b1bef2005-04-25 19:38:21 +000094 size_t _bs = (bufsize); /* Append size */ \
95 size_t _ns = ctx->context; /* Allocated now */ \
96 size_t _es = st->size + _bs; /* Expected size */ \
97 /* int is really a typeof(st->size): */ \
98 if((int)_es < 0) RETURN(RC_FAIL); \
99 if(_ns <= _es) { \
Lev Walkind9bd7752004-06-05 08:17:50 +0000100 void *ptr; \
Lev Walkin188ed2c2004-09-13 08:31:01 +0000101 /* Be nice and round to the memory allocator */ \
Lev Walkin75b1bef2005-04-25 19:38:21 +0000102 do { _ns = _ns ? _ns << 1 : 16; } \
103 while(_ns <= _es); \
104 /* int is really a typeof(st->size): */ \
105 if((int)_ns < 0) RETURN(RC_FAIL); \
Lev Walkind9bd7752004-06-05 08:17:50 +0000106 ptr = REALLOC(st->buf, _ns); \
107 if(ptr) { \
Lev Walkinc2346572004-08-11 09:07:36 +0000108 st->buf = (uint8_t *)ptr; \
Lev Walkin75b1bef2005-04-25 19:38:21 +0000109 ctx->context = _ns; \
Lev Walkind9bd7752004-06-05 08:17:50 +0000110 } else { \
111 RETURN(RC_FAIL); \
112 } \
Lev Walkin7f85ef42005-07-02 20:24:27 +0000113 ASN_DEBUG("Reallocating into %ld", (long)_ns); \
Lev Walkind9bd7752004-06-05 08:17:50 +0000114 } \
Lev Walkin8d127872004-09-04 04:44:50 +0000115 memcpy(st->buf + st->size, bufptr, _bs); \
Lev Walkind9bd7752004-06-05 08:17:50 +0000116 /* Convenient nul-termination */ \
Lev Walkin75b1bef2005-04-25 19:38:21 +0000117 st->buf[_es] = '\0'; \
118 st->size = _es; \
Lev Walkinf15320b2004-06-03 03:38:44 +0000119 } while(0)
120
121/*
122 * The main reason why ASN.1 is still alive is that too much time and effort
123 * is necessary for learning it more or less adequately, thus creating a gut
124 * necessity to demonstrate that aquired skill everywhere afterwards.
125 * No, I am not going to explain what the following stuff is.
126 */
127struct _stack_el {
Lev Walkin494fb702017-08-07 20:07:00 -0700128 ber_tlv_len_t left; /* What's left to read (or -1) */
129 ber_tlv_len_t got; /* What was actually processed */
Lev Walkin24bf3f62017-08-10 17:58:43 -0700130 unsigned cont_level; /* Depth of subcontainment */
Lev Walkin494fb702017-08-07 20:07:00 -0700131 int want_nulls; /* Want null "end of content" octets? */
132 int bits_chopped; /* Flag in BIT STRING mode */
133 ber_tlv_tag_t tag; /* For debugging purposes */
134 struct _stack_el *prev;
135 struct _stack_el *next;
Lev Walkinf15320b2004-06-03 03:38:44 +0000136};
137struct _stack {
138 struct _stack_el *tail;
139 struct _stack_el *cur_ptr;
140};
141
142static struct _stack_el *
Lev Walkin3990ba62004-09-24 20:57:41 +0000143OS__add_stack_el(struct _stack *st) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000144 struct _stack_el *nel;
145
Lev Walkin188ed2c2004-09-13 08:31:01 +0000146 /*
147 * Reuse the old stack frame or allocate a new one.
148 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000149 if(st->cur_ptr && st->cur_ptr->next) {
150 nel = st->cur_ptr->next;
Lev Walkinf15320b2004-06-03 03:38:44 +0000151 nel->bits_chopped = 0;
Lev Walkin5c915992004-09-27 20:54:06 +0000152 nel->got = 0;
153 /* Retain the nel->cont_level, it's correct. */
Lev Walkinf15320b2004-06-03 03:38:44 +0000154 } else {
Lev Walkin814cca72004-12-15 23:23:53 +0000155 nel = (struct _stack_el *)CALLOC(1, sizeof(struct _stack_el));
Lev Walkinf15320b2004-06-03 03:38:44 +0000156 if(nel == NULL)
157 return NULL;
158
159 if(st->tail) {
Lev Walkin188ed2c2004-09-13 08:31:01 +0000160 /* Increase a subcontainment depth */
161 nel->cont_level = st->tail->cont_level + 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000162 st->tail->next = nel;
163 }
164 nel->prev = st->tail;
165 st->tail = nel;
166 }
167
168 st->cur_ptr = nel;
169
170 return nel;
171}
172
173static struct _stack *
johvik28268d92017-05-09 10:27:52 +0200174_new_stack(void) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000175 return (struct _stack *)CALLOC(1, sizeof(struct _stack));
Lev Walkinf15320b2004-06-03 03:38:44 +0000176}
177
178/*
179 * Decode OCTET STRING type.
180 */
Lev Walkindc06f6b2004-10-20 15:50:55 +0000181asn_dec_rval_t
Lev Walkinde4825d2004-09-29 13:20:14 +0000182OCTET_STRING_decode_ber(asn_codec_ctx_t *opt_codec_ctx,
183 asn_TYPE_descriptor_t *td,
Lev Walkin59b176e2005-11-26 11:25:14 +0000184 void **sptr, const void *buf_ptr, size_t size, int tag_mode) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000185 asn_OCTET_STRING_specifics_t *specs = td->specifics
Lev Walkindc06f6b2004-10-20 15:50:55 +0000186 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +0800187 : &asn_SPC_OCTET_STRING_specs;
Lev Walkin59b176e2005-11-26 11:25:14 +0000188 BIT_STRING_t *st = (BIT_STRING_t *)*sptr;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000189 asn_dec_rval_t rval;
Lev Walkinde4825d2004-09-29 13:20:14 +0000190 asn_struct_ctx_t *ctx;
Lev Walkinf15320b2004-06-03 03:38:44 +0000191 ssize_t consumed_myself = 0;
Lev Walkinbbd93252004-10-12 05:57:23 +0000192 struct _stack *stck; /* Expectations stack structure */
Lev Walkin5c915992004-09-27 20:54:06 +0000193 struct _stack_el *sel = 0; /* Stack element */
Lev Walkinf15320b2004-06-03 03:38:44 +0000194 int tlv_constr;
Lev Walkin3a4689a2006-11-24 11:20:27 +0000195 enum asn_OS_Subvariant type_variant = specs->subvariant;
Lev Walkinf15320b2004-06-03 03:38:44 +0000196
Lev Walkincc6a9102004-09-23 22:06:26 +0000197 ASN_DEBUG("Decoding %s as %s (frame %ld)",
198 td->name,
Lev Walkin3a4689a2006-11-24 11:20:27 +0000199 (type_variant == ASN_OSUBV_STR) ?
Lev Walkinbbd93252004-10-12 05:57:23 +0000200 "OCTET STRING" : "OS-SpecialCase",
Lev Walkincc6a9102004-09-23 22:06:26 +0000201 (long)size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000202
203 /*
204 * Create the string if does not exist.
205 */
206 if(st == NULL) {
Lev Walkin59b176e2005-11-26 11:25:14 +0000207 st = (BIT_STRING_t *)(*sptr = CALLOC(1, specs->struct_size));
208 if(st == NULL) RETURN(RC_FAIL);
Lev Walkinf15320b2004-06-03 03:38:44 +0000209 }
210
211 /* Restore parsing context */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800212 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000213
214 switch(ctx->phase) {
215 case 0:
216 /*
217 * Check tags.
218 */
Lev Walkinde4825d2004-09-29 13:20:14 +0000219 rval = ber_check_tags(opt_codec_ctx, td, ctx,
Lev Walkin8e8078a2004-09-26 13:10:40 +0000220 buf_ptr, size, tag_mode, -1,
Lev Walkinf15320b2004-06-03 03:38:44 +0000221 &ctx->left, &tlv_constr);
Lev Walkin443d2512004-10-05 06:35:31 +0000222 if(rval.code != RC_OK)
223 return rval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000224
Lev Walkinf15320b2004-06-03 03:38:44 +0000225 if(tlv_constr) {
226 /*
227 * Complex operation, requires stack of expectations.
228 */
229 ctx->ptr = _new_stack();
Lev Walkinb02145b2017-06-28 08:52:07 -0700230 if(!ctx->ptr) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000231 RETURN(RC_FAIL);
232 }
233 } else {
234 /*
235 * Jump into stackless primitive decoding.
236 */
237 _CH_PHASE(ctx, 3);
Lev Walkin3a4689a2006-11-24 11:20:27 +0000238 if(type_variant == ASN_OSUBV_ANY && tag_mode != 1)
Lev Walkin188ed2c2004-09-13 08:31:01 +0000239 APPEND(buf_ptr, rval.consumed);
Lev Walkinf15320b2004-06-03 03:38:44 +0000240 ADVANCE(rval.consumed);
241 goto phase3;
242 }
243
Lev Walkinf15320b2004-06-03 03:38:44 +0000244 NEXT_PHASE(ctx);
245 /* Fall through */
246 case 1:
247 phase1:
248 /*
249 * Fill the stack with expectations.
250 */
Lev Walkin814cca72004-12-15 23:23:53 +0000251 stck = (struct _stack *)ctx->ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000252 sel = stck->cur_ptr;
253 do {
254 ber_tlv_tag_t tlv_tag;
255 ber_tlv_len_t tlv_len;
Lev Walkin188ed2c2004-09-13 08:31:01 +0000256 ber_tlv_tag_t expected_tag;
Lev Walkin5c915992004-09-27 20:54:06 +0000257 ssize_t tl, ll, tlvl;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000258 /* This one works even if (sel->left == -1) */
Simo Sorcecd943c22016-07-25 04:39:40 -0400259 size_t Left = ((!sel||(size_t)sel->left >= size)
260 ?size:(size_t)sel->left);
Lev Walkinf15320b2004-06-03 03:38:44 +0000261
Lev Walkin3990ba62004-09-24 20:57:41 +0000262
Lev Walkinabf68892004-10-26 10:12:14 +0000263 ASN_DEBUG("%p, s->l=%ld, s->wn=%ld, s->g=%ld\n", sel,
Lev Walkin1e3ccbb2004-10-26 10:56:10 +0000264 (long)(sel?sel->left:0),
265 (long)(sel?sel->want_nulls:0),
266 (long)(sel?sel->got:0)
Lev Walkin5c915992004-09-27 20:54:06 +0000267 );
268 if(sel && sel->left <= 0 && sel->want_nulls == 0) {
269 if(sel->prev) {
270 struct _stack_el *prev = sel->prev;
271 if(prev->left != -1) {
272 if(prev->left < sel->got)
273 RETURN(RC_FAIL);
274 prev->left -= sel->got;
275 }
276 prev->got += sel->got;
277 sel = stck->cur_ptr = prev;
278 if(!sel) break;
279 tlv_constr = 1;
280 continue;
281 } else {
282 sel = stck->cur_ptr = 0;
283 break; /* Nothing to wait */
284 }
285 }
286
Lev Walkin3990ba62004-09-24 20:57:41 +0000287 tl = ber_fetch_tag(buf_ptr, Left, &tlv_tag);
Lev Walkinabf68892004-10-26 10:12:14 +0000288 ASN_DEBUG("fetch tag(size=%ld,L=%ld), %sstack, left=%ld, wn=%ld, tl=%ld",
289 (long)size, (long)Left, sel?"":"!",
Lev Walkin1e3ccbb2004-10-26 10:56:10 +0000290 (long)(sel?sel->left:0),
291 (long)(sel?sel->want_nulls:0),
Lev Walkinabf68892004-10-26 10:12:14 +0000292 (long)tl);
Lev Walkinf15320b2004-06-03 03:38:44 +0000293 switch(tl) {
294 case -1: RETURN(RC_FAIL);
295 case 0: RETURN(RC_WMORE);
296 }
297
298 tlv_constr = BER_TLV_CONSTRUCTED(buf_ptr);
299
300 ll = ber_fetch_length(tlv_constr,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000301 (const char *)buf_ptr + tl,Left - tl,&tlv_len);
Lev Walkina6a926a2005-03-02 01:54:28 +0000302 ASN_DEBUG("Got tag=%s, tc=%d, left=%ld, tl=%ld, len=%ld, ll=%ld",
Lev Walkin8d127872004-09-04 04:44:50 +0000303 ber_tlv_tag_string(tlv_tag), tlv_constr,
Lev Walkina6a926a2005-03-02 01:54:28 +0000304 (long)Left, (long)tl, (long)tlv_len, (long)ll);
Lev Walkinf15320b2004-06-03 03:38:44 +0000305 switch(ll) {
306 case -1: RETURN(RC_FAIL);
307 case 0: RETURN(RC_WMORE);
308 }
309
Lev Walkin8d127872004-09-04 04:44:50 +0000310 if(sel && sel->want_nulls
Lev Walkin8c3b8542005-03-10 18:52:02 +0000311 && ((const uint8_t *)buf_ptr)[0] == 0
312 && ((const uint8_t *)buf_ptr)[1] == 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000313 {
Lev Walkin8d127872004-09-04 04:44:50 +0000314
315 ASN_DEBUG("Eat EOC; wn=%d--", sel->want_nulls);
316
Lev Walkin3a4689a2006-11-24 11:20:27 +0000317 if(type_variant == ASN_OSUBV_ANY
Lev Walkin07f388c2004-10-11 11:43:08 +0000318 && (tag_mode != 1 || sel->cont_level))
319 APPEND("\0\0", 2);
Lev Walkin5c915992004-09-27 20:54:06 +0000320
321 ADVANCE(2);
322 sel->got += 2;
323 if(sel->left != -1) {
324 sel->left -= 2; /* assert(sel->left >= 2) */
325 }
326
Lev Walkinf15320b2004-06-03 03:38:44 +0000327 sel->want_nulls--;
328 if(sel->want_nulls == 0) {
329 /* Move to the next expectation */
Lev Walkin5c915992004-09-27 20:54:06 +0000330 sel->left = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000331 tlv_constr = 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000332 }
Lev Walkin8d127872004-09-04 04:44:50 +0000333
334 continue;
Lev Walkin188ed2c2004-09-13 08:31:01 +0000335 }
336
337 /*
338 * Set up expected tags,
339 * depending on ASN.1 type being decoded.
340 */
Lev Walkinbbd93252004-10-12 05:57:23 +0000341 switch(type_variant) {
Lev Walkin3a4689a2006-11-24 11:20:27 +0000342 case ASN_OSUBV_BIT:
Lev Walkin188ed2c2004-09-13 08:31:01 +0000343 /* X.690: 8.6.4.1, NOTE 2 */
344 /* Fall through */
Lev Walkin3a4689a2006-11-24 11:20:27 +0000345 case ASN_OSUBV_STR:
Lev Walkin188ed2c2004-09-13 08:31:01 +0000346 default:
347 if(sel) {
Lev Walkin24bf3f62017-08-10 17:58:43 -0700348 unsigned level = sel->cont_level;
Lev Walkin188ed2c2004-09-13 08:31:01 +0000349 if(level < td->all_tags_count) {
350 expected_tag = td->all_tags[level];
351 break;
352 } else if(td->all_tags_count) {
353 expected_tag = td->all_tags
354 [td->all_tags_count - 1];
355 break;
356 }
357 /* else, Fall through */
358 }
359 /* Fall through */
Lev Walkin3a4689a2006-11-24 11:20:27 +0000360 case ASN_OSUBV_ANY:
Lev Walkin188ed2c2004-09-13 08:31:01 +0000361 expected_tag = tlv_tag;
362 break;
363 }
364
365
366 if(tlv_tag != expected_tag) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000367 char buf[2][32];
368 ber_tlv_tag_snprint(tlv_tag,
369 buf[0], sizeof(buf[0]));
370 ber_tlv_tag_snprint(td->tags[td->tags_count-1],
371 buf[1], sizeof(buf[1]));
372 ASN_DEBUG("Tag does not match expectation: %s != %s",
373 buf[0], buf[1]);
374 RETURN(RC_FAIL);
375 }
376
Lev Walkinde4825d2004-09-29 13:20:14 +0000377 tlvl = tl + ll; /* Combined length of T and L encoding */
378 if((tlv_len + tlvl) < 0) {
379 /* tlv_len value is too big */
380 ASN_DEBUG("TLV encoding + length (%ld) is too big",
381 (long)tlv_len);
382 RETURN(RC_FAIL);
383 }
384
Lev Walkinf15320b2004-06-03 03:38:44 +0000385 /*
386 * Append a new expectation.
387 */
Lev Walkin3990ba62004-09-24 20:57:41 +0000388 sel = OS__add_stack_el(stck);
Lev Walkin5c915992004-09-27 20:54:06 +0000389 if(!sel) RETURN(RC_FAIL);
Lev Walkinf15320b2004-06-03 03:38:44 +0000390
Lev Walkin5c915992004-09-27 20:54:06 +0000391 sel->tag = tlv_tag;
392
393 sel->want_nulls = (tlv_len==-1);
394 if(sel->prev && sel->prev->left != -1) {
395 /* Check that the parent frame is big enough */
396 if(sel->prev->left < tlvl + (tlv_len==-1?0:tlv_len))
397 RETURN(RC_FAIL);
398 if(tlv_len == -1)
399 sel->left = sel->prev->left - tlvl;
400 else
401 sel->left = tlv_len;
402 } else {
403 sel->left = tlv_len;
404 }
Lev Walkin3a4689a2006-11-24 11:20:27 +0000405 if(type_variant == ASN_OSUBV_ANY
Lev Walkin07f388c2004-10-11 11:43:08 +0000406 && (tag_mode != 1 || sel->cont_level))
407 APPEND(buf_ptr, tlvl);
Lev Walkin5c915992004-09-27 20:54:06 +0000408 sel->got += tlvl;
409 ADVANCE(tlvl);
410
Lev Walkin24bf3f62017-08-10 17:58:43 -0700411 ASN_DEBUG("+EXPECT2 got=%ld left=%ld, wn=%d, clvl=%u",
Lev Walkinabf68892004-10-26 10:12:14 +0000412 (long)sel->got, (long)sel->left,
413 sel->want_nulls, sel->cont_level);
Lev Walkin5c915992004-09-27 20:54:06 +0000414
Lev Walkinf15320b2004-06-03 03:38:44 +0000415 } while(tlv_constr);
416 if(sel == NULL) {
417 /* Finished operation, "phase out" */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000418 ASN_DEBUG("Phase out");
Lev Walkinf15320b2004-06-03 03:38:44 +0000419 _CH_PHASE(ctx, +3);
420 break;
421 }
422
423 NEXT_PHASE(ctx);
424 /* Fall through */
425 case 2:
Lev Walkin814cca72004-12-15 23:23:53 +0000426 stck = (struct _stack *)ctx->ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000427 sel = stck->cur_ptr;
Lev Walkin5c915992004-09-27 20:54:06 +0000428 ASN_DEBUG("Phase 2: Need %ld bytes, size=%ld, alrg=%ld, wn=%d",
429 (long)sel->left, (long)size, (long)sel->got,
430 sel->want_nulls);
Lev Walkinf15320b2004-06-03 03:38:44 +0000431 {
432 ber_tlv_len_t len;
433
434 assert(sel->left >= 0);
435
Lev Walkinec1ffd42004-08-18 04:53:32 +0000436 len = ((ber_tlv_len_t)size < sel->left)
437 ? (ber_tlv_len_t)size : sel->left;
Lev Walkinf15320b2004-06-03 03:38:44 +0000438 if(len > 0) {
Lev Walkin3a4689a2006-11-24 11:20:27 +0000439 if(type_variant == ASN_OSUBV_BIT
Lev Walkin188ed2c2004-09-13 08:31:01 +0000440 && sel->bits_chopped == 0) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000441 /* Put the unused-bits-octet away */
Lev Walkin8c3b8542005-03-10 18:52:02 +0000442 st->bits_unused = *(const uint8_t *)buf_ptr;
443 APPEND(((const char *)buf_ptr+1), (len - 1));
Lev Walkinf15320b2004-06-03 03:38:44 +0000444 sel->bits_chopped = 1;
445 } else {
446 APPEND(buf_ptr, len);
447 }
448 ADVANCE(len);
449 sel->left -= len;
Lev Walkin5c915992004-09-27 20:54:06 +0000450 sel->got += len;
Lev Walkinf15320b2004-06-03 03:38:44 +0000451 }
452
Lev Walkin5c915992004-09-27 20:54:06 +0000453 if(sel->left) {
454 ASN_DEBUG("OS left %ld, size = %ld, wn=%d\n",
455 (long)sel->left, (long)size, sel->want_nulls);
Lev Walkinf15320b2004-06-03 03:38:44 +0000456 RETURN(RC_WMORE);
Lev Walkinf15320b2004-06-03 03:38:44 +0000457 }
Lev Walkin5c915992004-09-27 20:54:06 +0000458
459 PREV_PHASE(ctx);
460 goto phase1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000461 }
462 break;
463 case 3:
464 phase3:
465 /*
466 * Primitive form, no stack required.
467 */
Lev Walkinbbd93252004-10-12 05:57:23 +0000468 assert(ctx->left >= 0);
469
Lev Walkind9bd7752004-06-05 08:17:50 +0000470 if(size < (size_t)ctx->left) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000471 if(!size) RETURN(RC_WMORE);
Lev Walkin3a4689a2006-11-24 11:20:27 +0000472 if(type_variant == ASN_OSUBV_BIT && !ctx->context) {
Lev Walkin8c3b8542005-03-10 18:52:02 +0000473 st->bits_unused = *(const uint8_t *)buf_ptr;
Lev Walkinbbd93252004-10-12 05:57:23 +0000474 ctx->left--;
475 ADVANCE(1);
476 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000477 APPEND(buf_ptr, size);
Lev Walkin75b1bef2005-04-25 19:38:21 +0000478 assert(ctx->context > 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000479 ctx->left -= size;
480 ADVANCE(size);
481 RETURN(RC_WMORE);
482 } else {
Lev Walkin3a4689a2006-11-24 11:20:27 +0000483 if(type_variant == ASN_OSUBV_BIT
Lev Walkin75b1bef2005-04-25 19:38:21 +0000484 && !ctx->context && ctx->left) {
Lev Walkin8c3b8542005-03-10 18:52:02 +0000485 st->bits_unused = *(const uint8_t *)buf_ptr;
Lev Walkinbbd93252004-10-12 05:57:23 +0000486 ctx->left--;
487 ADVANCE(1);
488 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000489 APPEND(buf_ptr, ctx->left);
490 ADVANCE(ctx->left);
491 ctx->left = 0;
492
493 NEXT_PHASE(ctx);
494 }
495 break;
496 }
497
Lev Walkin5c915992004-09-27 20:54:06 +0000498 if(sel) {
499 ASN_DEBUG("3sel p=%p, wn=%d, l=%ld, g=%ld, size=%ld",
500 sel->prev, sel->want_nulls,
501 (long)sel->left, (long)sel->got, (long)size);
502 if(sel->prev || sel->want_nulls > 1 || sel->left > 0) {
503 RETURN(RC_WMORE);
504 }
505 }
506
Lev Walkinf15320b2004-06-03 03:38:44 +0000507 /*
508 * BIT STRING-specific processing.
509 */
Lev Walkin3a4689a2006-11-24 11:20:27 +0000510 if(type_variant == ASN_OSUBV_BIT && st->size) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000511 /* Finalize BIT STRING: zero out unused bits. */
Lev Walkinbbd93252004-10-12 05:57:23 +0000512 st->buf[st->size-1] &= 0xff << st->bits_unused;
Lev Walkinf15320b2004-06-03 03:38:44 +0000513 }
514
Lev Walkinabf68892004-10-26 10:12:14 +0000515 ASN_DEBUG("Took %ld bytes to encode %s: [%s]:%ld",
516 (long)consumed_myself, td->name,
Lev Walkin3a4689a2006-11-24 11:20:27 +0000517 (type_variant == ASN_OSUBV_STR) ? (char *)st->buf : "<data>",
Lev Walkinabf68892004-10-26 10:12:14 +0000518 (long)st->size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000519
Lev Walkinf15320b2004-06-03 03:38:44 +0000520
Lev Walkin5c915992004-09-27 20:54:06 +0000521 RETURN(RC_OK);
Lev Walkinf15320b2004-06-03 03:38:44 +0000522}
523
524/*
525 * Encode OCTET STRING type using DER.
526 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000527asn_enc_rval_t
Lev Walkinbbd93252004-10-12 05:57:23 +0000528OCTET_STRING_encode_der(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000529 int tag_mode, ber_tlv_tag_t tag,
530 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000531 asn_enc_rval_t er;
532 asn_OCTET_STRING_specifics_t *specs = td->specifics
Lev Walkindc06f6b2004-10-20 15:50:55 +0000533 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +0800534 : &asn_SPC_OCTET_STRING_specs;
Lev Walkinbbd93252004-10-12 05:57:23 +0000535 BIT_STRING_t *st = (BIT_STRING_t *)sptr;
Lev Walkin3a4689a2006-11-24 11:20:27 +0000536 enum asn_OS_Subvariant type_variant = specs->subvariant;
Lev Walkinbbd93252004-10-12 05:57:23 +0000537 int fix_last_byte = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000538
539 ASN_DEBUG("%s %s as OCTET STRING",
Lev Walkin1f670c12004-09-02 12:57:25 +0000540 cb?"Estimating":"Encoding", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000541
542 /*
Lev Walkinbbd93252004-10-12 05:57:23 +0000543 * Write tags.
Lev Walkinf15320b2004-06-03 03:38:44 +0000544 */
Lev Walkin3a4689a2006-11-24 11:20:27 +0000545 if(type_variant != ASN_OSUBV_ANY || tag_mode == 1) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000546 er.encoded = der_write_tags(td,
Lev Walkin3a4689a2006-11-24 11:20:27 +0000547 (type_variant == ASN_OSUBV_BIT) + st->size,
548 tag_mode, type_variant == ASN_OSUBV_ANY, tag,
549 cb, app_key);
Lev Walkinbbd93252004-10-12 05:57:23 +0000550 if(er.encoded == -1) {
551 er.failed_type = td;
552 er.structure_ptr = sptr;
553 return er;
Lev Walkinf15320b2004-06-03 03:38:44 +0000554 }
Lev Walkinbbd93252004-10-12 05:57:23 +0000555 } else {
556 /* Disallow: [<tag>] IMPLICIT ANY */
Lev Walkin3a4689a2006-11-24 11:20:27 +0000557 assert(type_variant != ASN_OSUBV_ANY || tag_mode != -1);
Lev Walkinbbd93252004-10-12 05:57:23 +0000558 er.encoded = 0;
559 }
560
561 if(!cb) {
Lev Walkin3a4689a2006-11-24 11:20:27 +0000562 er.encoded += (type_variant == ASN_OSUBV_BIT) + st->size;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700563 ASN__ENCODED_OK(er);
Lev Walkinf15320b2004-06-03 03:38:44 +0000564 }
565
Lev Walkin07f388c2004-10-11 11:43:08 +0000566 /*
Lev Walkinbbd93252004-10-12 05:57:23 +0000567 * Prepare to deal with the last octet of BIT STRING.
Lev Walkin07f388c2004-10-11 11:43:08 +0000568 */
Lev Walkin3a4689a2006-11-24 11:20:27 +0000569 if(type_variant == ASN_OSUBV_BIT) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000570 uint8_t b = st->bits_unused & 0x07;
571 if(b && st->size) fix_last_byte = 1;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700572 ASN__CALLBACK(&b, 1);
Lev Walkinbbd93252004-10-12 05:57:23 +0000573 er.encoded++;
Lev Walkinf15320b2004-06-03 03:38:44 +0000574 }
575
Lev Walkinbbd93252004-10-12 05:57:23 +0000576 /* Invoke callback for the main part of the buffer */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700577 ASN__CALLBACK(st->buf, st->size - fix_last_byte);
Lev Walkinf15320b2004-06-03 03:38:44 +0000578
Lev Walkinbbd93252004-10-12 05:57:23 +0000579 /* The last octet should be stripped off the unused bits */
580 if(fix_last_byte) {
581 uint8_t b = st->buf[st->size-1] & (0xff << st->bits_unused);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700582 ASN__CALLBACK(&b, 1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000583 }
584
Lev Walkinbbd93252004-10-12 05:57:23 +0000585 er.encoded += st->size;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700586 ASN__ENCODED_OK(er);
Lev Walkinbbd93252004-10-12 05:57:23 +0000587cb_failed:
Lev Walkin7c1dc052016-03-14 03:08:15 -0700588 ASN__ENCODE_FAILED;
Lev Walkinf15320b2004-06-03 03:38:44 +0000589}
590
Lev Walkina9cc46e2004-09-22 16:06:28 +0000591asn_enc_rval_t
Lev Walkinde4825d2004-09-29 13:20:14 +0000592OCTET_STRING_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000593 int ilevel, enum xer_encoder_flags_e flags,
594 asn_app_consume_bytes_f *cb, void *app_key) {
Wim Lewis59e8d282014-08-04 12:39:35 -0700595 const char * const h2c = "0123456789ABCDEF";
Lev Walkina9cc46e2004-09-22 16:06:28 +0000596 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
597 asn_enc_rval_t er;
598 char scratch[16 * 3 + 4];
599 char *p = scratch;
600 uint8_t *buf;
601 uint8_t *end;
602 size_t i;
603
Lev Walkin2ddd3082006-10-16 12:32:14 +0000604 if(!st || (!st->buf && st->size))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700605 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000606
607 er.encoded = 0;
608
609 /*
610 * Dump the contents of the buffer in hexadecimal.
611 */
612 buf = st->buf;
613 end = buf + st->size;
614 if(flags & XER_F_CANONICAL) {
615 char *scend = scratch + (sizeof(scratch) - 2);
616 for(; buf < end; buf++) {
617 if(p >= scend) {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700618 ASN__CALLBACK(scratch, p - scratch);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000619 er.encoded += p - scratch;
620 p = scratch;
621 }
622 *p++ = h2c[(*buf >> 4) & 0x0F];
623 *p++ = h2c[*buf & 0x0F];
624 }
Lev Walkincc6a9102004-09-23 22:06:26 +0000625
Lev Walkin7c1dc052016-03-14 03:08:15 -0700626 ASN__CALLBACK(scratch, p-scratch); /* Dump the rest */
Lev Walkincc6a9102004-09-23 22:06:26 +0000627 er.encoded += p - scratch;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000628 } else {
629 for(i = 0; buf < end; buf++, i++) {
630 if(!(i % 16) && (i || st->size > 16)) {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700631 ASN__CALLBACK(scratch, p-scratch);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000632 er.encoded += (p-scratch);
633 p = scratch;
Lev Walkinc6cac8e2016-03-14 02:57:07 -0700634 ASN__TEXT_INDENT(1, ilevel);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000635 }
636 *p++ = h2c[(*buf >> 4) & 0x0F];
637 *p++ = h2c[*buf & 0x0F];
638 *p++ = 0x20;
639 }
Lev Walkincc6a9102004-09-23 22:06:26 +0000640 if(p - scratch) {
641 p--; /* Remove the tail space */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700642 ASN__CALLBACK(scratch, p-scratch); /* Dump the rest */
Lev Walkincc6a9102004-09-23 22:06:26 +0000643 er.encoded += p - scratch;
644 if(st->size > 16)
Lev Walkinc6cac8e2016-03-14 02:57:07 -0700645 ASN__TEXT_INDENT(1, ilevel-1);
Lev Walkincc6a9102004-09-23 22:06:26 +0000646 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000647 }
648
Lev Walkin7c1dc052016-03-14 03:08:15 -0700649 ASN__ENCODED_OK(er);
Lev Walkin942fd082004-10-03 09:13:02 +0000650cb_failed:
Lev Walkin7c1dc052016-03-14 03:08:15 -0700651 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000652}
653
Wim Lewis18c2ec92014-07-29 11:30:10 -0700654static const struct OCTET_STRING__xer_escape_table_s {
655 const char *string;
Lev Walkin9ca81892004-10-03 10:54:25 +0000656 int size;
657} OCTET_STRING__xer_escape_table[] = {
658#define OSXET(s) { s, sizeof(s) - 1 }
659 OSXET("\074\156\165\154\057\076"), /* <nul/> */
660 OSXET("\074\163\157\150\057\076"), /* <soh/> */
661 OSXET("\074\163\164\170\057\076"), /* <stx/> */
662 OSXET("\074\145\164\170\057\076"), /* <etx/> */
663 OSXET("\074\145\157\164\057\076"), /* <eot/> */
664 OSXET("\074\145\156\161\057\076"), /* <enq/> */
665 OSXET("\074\141\143\153\057\076"), /* <ack/> */
666 OSXET("\074\142\145\154\057\076"), /* <bel/> */
667 OSXET("\074\142\163\057\076"), /* <bs/> */
668 OSXET("\011"), /* \t */
669 OSXET("\012"), /* \n */
670 OSXET("\074\166\164\057\076"), /* <vt/> */
671 OSXET("\074\146\146\057\076"), /* <ff/> */
672 OSXET("\015"), /* \r */
673 OSXET("\074\163\157\057\076"), /* <so/> */
674 OSXET("\074\163\151\057\076"), /* <si/> */
675 OSXET("\074\144\154\145\057\076"), /* <dle/> */
676 OSXET("\074\144\143\061\057\076"), /* <de1/> */
677 OSXET("\074\144\143\062\057\076"), /* <de2/> */
678 OSXET("\074\144\143\063\057\076"), /* <de3/> */
679 OSXET("\074\144\143\064\057\076"), /* <de4/> */
680 OSXET("\074\156\141\153\057\076"), /* <nak/> */
681 OSXET("\074\163\171\156\057\076"), /* <syn/> */
682 OSXET("\074\145\164\142\057\076"), /* <etb/> */
683 OSXET("\074\143\141\156\057\076"), /* <can/> */
684 OSXET("\074\145\155\057\076"), /* <em/> */
685 OSXET("\074\163\165\142\057\076"), /* <sub/> */
686 OSXET("\074\145\163\143\057\076"), /* <esc/> */
687 OSXET("\074\151\163\064\057\076"), /* <is4/> */
688 OSXET("\074\151\163\063\057\076"), /* <is3/> */
689 OSXET("\074\151\163\062\057\076"), /* <is2/> */
690 OSXET("\074\151\163\061\057\076"), /* <is1/> */
691 { 0, 0 }, /* " " */
692 { 0, 0 }, /* ! */
693 { 0, 0 }, /* \" */
694 { 0, 0 }, /* # */
695 { 0, 0 }, /* $ */
696 { 0, 0 }, /* % */
697 OSXET("\046\141\155\160\073"), /* &amp; */
698 { 0, 0 }, /* ' */
699 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, /* ()*+,-./ */
700 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, /* 01234567 */
701 {0,0},{0,0},{0,0},{0,0}, /* 89:; */
702 OSXET("\046\154\164\073"), /* &lt; */
703 { 0, 0 }, /* = */
704 OSXET("\046\147\164\073"), /* &gt; */
705};
706
Lev Walkindc06f6b2004-10-20 15:50:55 +0000707static int
Lev Walkin0fab1a62005-03-09 22:19:25 +0000708OS__check_escaped_control_char(const void *buf, int size) {
Lev Walkindc06f6b2004-10-20 15:50:55 +0000709 size_t i;
710 /*
711 * Inefficient algorithm which translates the escape sequences
712 * defined above into characters. Returns -1 if not found.
713 * TODO: replace by a faster algorithm (bsearch(), hash or
714 * nested table lookups).
715 */
716 for(i = 0; i < 32 /* Don't spend time on the bottom half */; i++) {
Wim Lewis18c2ec92014-07-29 11:30:10 -0700717 const struct OCTET_STRING__xer_escape_table_s *el;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000718 el = &OCTET_STRING__xer_escape_table[i];
719 if(el->size == size && memcmp(buf, el->string, size) == 0)
720 return i;
721 }
722 return -1;
723}
724
725static int
Lev Walkin0fab1a62005-03-09 22:19:25 +0000726OCTET_STRING__handle_control_chars(void *struct_ptr, const void *chunk_buf, size_t chunk_size) {
Lev Walkindc06f6b2004-10-20 15:50:55 +0000727 /*
728 * This might be one of the escape sequences
729 * for control characters. Check it out.
730 * #11.15.5
731 */
732 int control_char = OS__check_escaped_control_char(chunk_buf,chunk_size);
733 if(control_char >= 0) {
734 OCTET_STRING_t *st = (OCTET_STRING_t *)struct_ptr;
735 void *p = REALLOC(st->buf, st->size + 2);
736 if(p) {
737 st->buf = (uint8_t *)p;
738 st->buf[st->size++] = control_char;
739 st->buf[st->size] = '\0'; /* nul-termination */
740 return 0;
741 }
742 }
743
744 return -1; /* No, it's not */
745}
746
Lev Walkina9cc46e2004-09-22 16:06:28 +0000747asn_enc_rval_t
Lev Walkindc06f6b2004-10-20 15:50:55 +0000748OCTET_STRING_encode_xer_utf8(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000749 int ilevel, enum xer_encoder_flags_e flags,
750 asn_app_consume_bytes_f *cb, void *app_key) {
751 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
752 asn_enc_rval_t er;
Lev Walkin9ca81892004-10-03 10:54:25 +0000753 uint8_t *buf, *end;
754 uint8_t *ss; /* Sequence start */
755 ssize_t encoded_len = 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000756
757 (void)ilevel; /* Unused argument */
758 (void)flags; /* Unused argument */
759
Lev Walkin2ddd3082006-10-16 12:32:14 +0000760 if(!st || (!st->buf && st->size))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700761 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000762
Lev Walkin9ca81892004-10-03 10:54:25 +0000763 buf = st->buf;
764 end = buf + st->size;
765 for(ss = buf; buf < end; buf++) {
Lev Walkin443d2512004-10-05 06:35:31 +0000766 unsigned int ch = *buf;
Lev Walkin9ca81892004-10-03 10:54:25 +0000767 int s_len; /* Special encoding sequence length */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000768
Lev Walkin9ca81892004-10-03 10:54:25 +0000769 /*
770 * Escape certain characters: X.680/11.15
771 */
772 if(ch < sizeof(OCTET_STRING__xer_escape_table)
773 /sizeof(OCTET_STRING__xer_escape_table[0])
774 && (s_len = OCTET_STRING__xer_escape_table[ch].size)) {
775 if(((buf - ss) && cb(ss, buf - ss, app_key) < 0)
776 || cb(OCTET_STRING__xer_escape_table[ch].string, s_len,
777 app_key) < 0)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700778 ASN__ENCODE_FAILED;
Lev Walkin9ca81892004-10-03 10:54:25 +0000779 encoded_len += (buf - ss) + s_len;
780 ss = buf + 1;
781 }
782 }
783
784 encoded_len += (buf - ss);
785 if((buf - ss) && cb(ss, buf - ss, app_key) < 0)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700786 ASN__ENCODE_FAILED;
Lev Walkin9ca81892004-10-03 10:54:25 +0000787
788 er.encoded = encoded_len;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700789 ASN__ENCODED_OK(er);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000790}
791
Lev Walkindc06f6b2004-10-20 15:50:55 +0000792/*
793 * Convert from hexadecimal format (cstring): "AB CD EF"
794 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000795static 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 +0000796 OCTET_STRING_t *st = (OCTET_STRING_t *)sptr;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000797 const char *chunk_stop = (const char *)chunk_buf;
798 const char *p = chunk_stop;
799 const char *pend = p + chunk_size;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000800 unsigned int clv = 0;
801 int half = 0; /* Half bit */
802 uint8_t *buf;
803
804 /* Reallocate buffer according to high cap estimation */
Lev Walkin494fb702017-08-07 20:07:00 -0700805 size_t new_size = st->size + (chunk_size + 1) / 2;
806 void *nptr = REALLOC(st->buf, new_size + 1);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000807 if(!nptr) return -1;
808 st->buf = (uint8_t *)nptr;
809 buf = st->buf + st->size;
810
811 /*
812 * If something like " a b c " appears here, the " a b":3 will be
813 * converted, and the rest skipped. That is, unless buf_size is greater
814 * than chunk_size, then it'll be equivalent to "ABC0".
815 */
816 for(; p < pend; p++) {
Lev Walkin0fab1a62005-03-09 22:19:25 +0000817 int ch = *(const unsigned char *)p;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000818 switch(ch) {
819 case 0x09: case 0x0a: case 0x0c: case 0x0d:
820 case 0x20:
821 /* Ignore whitespace */
822 continue;
823 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: /*01234*/
824 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: /*56789*/
825 clv = (clv << 4) + (ch - 0x30);
826 break;
827 case 0x41: case 0x42: case 0x43: /* ABC */
828 case 0x44: case 0x45: case 0x46: /* DEF */
Lev Walkin3ae21bd2005-02-21 14:43:48 +0000829 clv = (clv << 4) + (ch - 0x41 + 10);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000830 break;
831 case 0x61: case 0x62: case 0x63: /* abc */
832 case 0x64: case 0x65: case 0x66: /* def */
Lev Walkin3ae21bd2005-02-21 14:43:48 +0000833 clv = (clv << 4) + (ch - 0x61 + 10);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000834 break;
835 default:
836 *buf = 0; /* JIC */
837 return -1;
838 }
839 if(half++) {
840 half = 0;
841 *buf++ = clv;
842 chunk_stop = p + 1;
843 }
844 }
845
846 /*
847 * Check partial decoding.
848 */
849 if(half) {
850 if(have_more) {
851 /*
852 * Partial specification is fine,
853 * because no more more PXER_TEXT data is available.
854 */
855 *buf++ = clv << 4;
856 chunk_stop = p;
857 }
858 } else {
859 chunk_stop = p;
860 }
861
862 st->size = buf - st->buf; /* Adjust the buffer size */
Lev Walkin494fb702017-08-07 20:07:00 -0700863 assert(st->size <= new_size);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000864 st->buf[st->size] = 0; /* Courtesy termination */
865
Lev Walkin0fab1a62005-03-09 22:19:25 +0000866 return (chunk_stop - (const char *)chunk_buf); /* Converted size */
Lev Walkindc06f6b2004-10-20 15:50:55 +0000867}
868
869/*
870 * Convert from binary format: "00101011101"
871 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000872static 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 +0000873 BIT_STRING_t *st = (BIT_STRING_t *)sptr;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000874 const char *p = (const char *)chunk_buf;
875 const char *pend = p + chunk_size;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000876 int bits_unused = st->bits_unused & 0x7;
877 uint8_t *buf;
878
879 /* Reallocate buffer according to high cap estimation */
Lev Walkin494fb702017-08-07 20:07:00 -0700880 size_t new_size = st->size + (chunk_size + 7) / 8;
881 void *nptr = REALLOC(st->buf, new_size + 1);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000882 if(!nptr) return -1;
883 st->buf = (uint8_t *)nptr;
884 buf = st->buf + st->size;
885
886 (void)have_more;
887
888 if(bits_unused == 0)
889 bits_unused = 8;
890 else if(st->size)
891 buf--;
892
893 /*
894 * Convert series of 0 and 1 into the octet string.
895 */
896 for(; p < pend; p++) {
Lev Walkin0fab1a62005-03-09 22:19:25 +0000897 int ch = *(const unsigned char *)p;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000898 switch(ch) {
899 case 0x09: case 0x0a: case 0x0c: case 0x0d:
900 case 0x20:
901 /* Ignore whitespace */
902 break;
903 case 0x30:
904 case 0x31:
905 if(bits_unused-- <= 0) {
906 *++buf = 0; /* Clean the cell */
907 bits_unused = 7;
908 }
909 *buf |= (ch&1) << bits_unused;
910 break;
911 default:
912 st->bits_unused = bits_unused;
913 return -1;
914 }
915 }
916
917 if(bits_unused == 8) {
918 st->size = buf - st->buf;
919 st->bits_unused = 0;
920 } else {
921 st->size = buf - st->buf + 1;
922 st->bits_unused = bits_unused;
923 }
924
Lev Walkin494fb702017-08-07 20:07:00 -0700925 assert(st->size <= new_size);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000926 st->buf[st->size] = 0; /* Courtesy termination */
927
928 return chunk_size; /* Converted in full */
929}
930
931/*
932 * Something like strtod(), but with stricter rules.
933 */
934static int
Lev Walkin0fab1a62005-03-09 22:19:25 +0000935OS__strtoent(int base, const char *buf, const char *end, int32_t *ret_value) {
Lev Walkin33700162004-10-26 09:03:31 +0000936 int32_t val = 0;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000937 const char *p;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000938
939 for(p = buf; p < end; p++) {
940 int ch = *p;
Lev Walkinb0f3db62005-07-03 05:30:15 +0000941
942 /* Strange huge value */
943 if((val * base + base) < 0)
944 return -1;
945
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 }
965 }
966
Lev Walkinb0f3db62005-07-03 05:30:15 +0000967 *ret_value = -1;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000968 return (p - buf);
969}
970
971/*
972 * Convert from the plain UTF-8 format, expanding entity references: "2 &lt; 3"
973 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000974static 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 +0000975 OCTET_STRING_t *st = (OCTET_STRING_t *)sptr;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000976 const char *p = (const char *)chunk_buf;
977 const char *pend = p + chunk_size;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000978 uint8_t *buf;
979
980 /* Reallocate buffer */
Lev Walkin494fb702017-08-07 20:07:00 -0700981 size_t new_size = st->size + chunk_size;
982 void *nptr = REALLOC(st->buf, new_size + 1);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000983 if(!nptr) return -1;
984 st->buf = (uint8_t *)nptr;
985 buf = st->buf + st->size;
986
987 /*
988 * Convert series of 0 and 1 into the octet string.
989 */
990 for(; p < pend; p++) {
Lev Walkin0fab1a62005-03-09 22:19:25 +0000991 int ch = *(const unsigned char *)p;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000992 int len; /* Length of the rest of the chunk */
993
994 if(ch != 0x26 /* '&' */) {
995 *buf++ = ch;
996 continue; /* That was easy... */
997 }
998
999 /*
1000 * Process entity reference.
1001 */
Lev Walkin0fab1a62005-03-09 22:19:25 +00001002 len = chunk_size - (p - (const char *)chunk_buf);
Lev Walkindc06f6b2004-10-20 15:50:55 +00001003 if(len == 1 /* "&" */) goto want_more;
1004 if(p[1] == 0x23 /* '#' */) {
Lev Walkin0fab1a62005-03-09 22:19:25 +00001005 const char *pval; /* Pointer to start of digits */
Lev Walkinbc0dce32006-07-27 09:56:13 +00001006 int32_t val = 0; /* Entity reference value */
Lev Walkindc06f6b2004-10-20 15:50:55 +00001007 int base;
1008
1009 if(len == 2 /* "&#" */) goto want_more;
1010 if(p[2] == 0x78 /* 'x' */)
1011 pval = p + 3, base = 16;
1012 else
1013 pval = p + 2, base = 10;
1014 len = OS__strtoent(base, pval, p + len, &val);
1015 if(len == -1) {
1016 /* Invalid charset. Just copy verbatim. */
1017 *buf++ = ch;
1018 continue;
1019 }
1020 if(!len || pval[len-1] != 0x3b) goto want_more;
1021 assert(val > 0);
1022 p += (pval - p) + len - 1; /* Advance past entref */
1023
1024 if(val < 0x80) {
1025 *buf++ = (char)val;
1026 } else if(val < 0x800) {
1027 *buf++ = 0xc0 | ((val >> 6));
1028 *buf++ = 0x80 | ((val & 0x3f));
1029 } else if(val < 0x10000) {
1030 *buf++ = 0xe0 | ((val >> 12));
1031 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1032 *buf++ = 0x80 | ((val & 0x3f));
1033 } else if(val < 0x200000) {
1034 *buf++ = 0xf0 | ((val >> 18));
1035 *buf++ = 0x80 | ((val >> 12) & 0x3f);
1036 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1037 *buf++ = 0x80 | ((val & 0x3f));
1038 } else if(val < 0x4000000) {
1039 *buf++ = 0xf8 | ((val >> 24));
1040 *buf++ = 0x80 | ((val >> 18) & 0x3f);
1041 *buf++ = 0x80 | ((val >> 12) & 0x3f);
1042 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1043 *buf++ = 0x80 | ((val & 0x3f));
1044 } else {
1045 *buf++ = 0xfc | ((val >> 30) & 0x1);
1046 *buf++ = 0x80 | ((val >> 24) & 0x3f);
1047 *buf++ = 0x80 | ((val >> 18) & 0x3f);
1048 *buf++ = 0x80 | ((val >> 12) & 0x3f);
1049 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1050 *buf++ = 0x80 | ((val & 0x3f));
1051 }
1052 } else {
1053 /*
1054 * Ugly, limited parsing of &amp; &gt; &lt;
1055 */
1056 char *sc = (char *)memchr(p, 0x3b, len > 5 ? 5 : len);
1057 if(!sc) goto want_more;
1058 if((sc - p) == 4
1059 && p[1] == 0x61 /* 'a' */
1060 && p[2] == 0x6d /* 'm' */
1061 && p[3] == 0x70 /* 'p' */) {
1062 *buf++ = 0x26;
1063 p = sc;
1064 continue;
1065 }
1066 if((sc - p) == 3) {
1067 if(p[1] == 0x6c) {
1068 *buf = 0x3c; /* '<' */
1069 } else if(p[1] == 0x67) {
1070 *buf = 0x3e; /* '>' */
1071 } else {
1072 /* Unsupported entity reference */
1073 *buf++ = ch;
1074 continue;
1075 }
1076 if(p[2] != 0x74) {
1077 /* Unsupported entity reference */
1078 *buf++ = ch;
1079 continue;
1080 }
1081 buf++;
1082 p = sc;
1083 continue;
1084 }
1085 /* Unsupported entity reference */
1086 *buf++ = ch;
1087 }
1088
1089 continue;
1090 want_more:
1091 if(have_more) {
1092 /*
1093 * We know that no more data (of the same type)
1094 * is coming. Copy the rest verbatim.
1095 */
1096 *buf++ = ch;
1097 continue;
1098 }
Lev Walkin0fab1a62005-03-09 22:19:25 +00001099 chunk_size = (p - (const char *)chunk_buf);
Lev Walkindc06f6b2004-10-20 15:50:55 +00001100 /* Processing stalled: need more data */
Lev Walkinbdaae772005-02-18 12:25:47 +00001101 break;
Lev Walkindc06f6b2004-10-20 15:50:55 +00001102 }
1103
1104 st->size = buf - st->buf;
Lev Walkin494fb702017-08-07 20:07:00 -07001105 assert(st->size <= new_size);
Lev Walkindc06f6b2004-10-20 15:50:55 +00001106 st->buf[st->size] = 0; /* Courtesy termination */
1107
1108 return chunk_size; /* Converted in full */
1109}
1110
1111/*
1112 * Decode OCTET STRING from the XML element's body.
1113 */
1114static asn_dec_rval_t
1115OCTET_STRING__decode_xer(asn_codec_ctx_t *opt_codec_ctx,
1116 asn_TYPE_descriptor_t *td, void **sptr,
Lev Walkin8c3b8542005-03-10 18:52:02 +00001117 const char *opt_mname, const void *buf_ptr, size_t size,
Lev Walkindc06f6b2004-10-20 15:50:55 +00001118 int (*opt_unexpected_tag_decoder)
Lev Walkin0fab1a62005-03-09 22:19:25 +00001119 (void *struct_ptr, const void *chunk_buf, size_t chunk_size),
Lev Walkindc06f6b2004-10-20 15:50:55 +00001120 ssize_t (*body_receiver)
Lev Walkin0fab1a62005-03-09 22:19:25 +00001121 (void *struct_ptr, const void *chunk_buf, size_t chunk_size,
Lev Walkindc06f6b2004-10-20 15:50:55 +00001122 int have_more)
1123) {
Lev Walkind5125642005-02-14 20:08:00 +00001124 OCTET_STRING_t *st = (OCTET_STRING_t *)*sptr;
Lev Walkindc06f6b2004-10-20 15:50:55 +00001125 asn_OCTET_STRING_specifics_t *specs = td->specifics
1126 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +08001127 : &asn_SPC_OCTET_STRING_specs;
Lev Walkindc06f6b2004-10-20 15:50:55 +00001128 const char *xml_tag = opt_mname ? opt_mname : td->xml_tag;
1129 asn_struct_ctx_t *ctx; /* Per-structure parser context */
Lev Walkinc61f3862005-02-14 17:21:22 +00001130 asn_dec_rval_t rval; /* Return value from the decoder */
1131 int st_allocated;
Lev Walkindc06f6b2004-10-20 15:50:55 +00001132
1133 /*
1134 * Create the string if does not exist.
1135 */
Lev Walkinc61f3862005-02-14 17:21:22 +00001136 if(!st) {
Lev Walkin8484ed82004-12-14 13:31:01 +00001137 st = (OCTET_STRING_t *)CALLOC(1, specs->struct_size);
Lev Walkin8484ed82004-12-14 13:31:01 +00001138 *sptr = (void *)st;
Lev Walkinc61f3862005-02-14 17:21:22 +00001139 if(!st) goto sta_failed;
1140 st_allocated = 1;
Lev Walkinbdaae772005-02-18 12:25:47 +00001141 } else {
1142 st_allocated = 0;
1143 }
Lev Walkinc61f3862005-02-14 17:21:22 +00001144 if(!st->buf) {
1145 /* This is separate from above section */
1146 st->buf = (uint8_t *)CALLOC(1, 1);
1147 if(!st->buf) {
1148 if(st_allocated) {
1149 *sptr = 0;
1150 goto stb_failed;
1151 } else {
1152 goto sta_failed;
1153 }
Lev Walkindc06f6b2004-10-20 15:50:55 +00001154 }
1155 }
1156
1157 /* Restore parsing context */
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001158 ctx = (asn_struct_ctx_t *)(((char *)*sptr) + specs->ctx_offset);
Lev Walkindc06f6b2004-10-20 15:50:55 +00001159
1160 return xer_decode_general(opt_codec_ctx, ctx, *sptr, xml_tag,
1161 buf_ptr, size, opt_unexpected_tag_decoder, body_receiver);
Lev Walkinc61f3862005-02-14 17:21:22 +00001162
1163stb_failed:
1164 FREEMEM(st);
1165sta_failed:
1166 rval.code = RC_FAIL;
1167 rval.consumed = 0;
1168 return rval;
Lev Walkindc06f6b2004-10-20 15:50:55 +00001169}
1170
1171/*
1172 * Decode OCTET STRING from the hexadecimal data.
1173 */
1174asn_dec_rval_t
1175OCTET_STRING_decode_xer_hex(asn_codec_ctx_t *opt_codec_ctx,
1176 asn_TYPE_descriptor_t *td, void **sptr,
Lev Walkin8c3b8542005-03-10 18:52:02 +00001177 const char *opt_mname, const void *buf_ptr, size_t size) {
Lev Walkindc06f6b2004-10-20 15:50:55 +00001178 return OCTET_STRING__decode_xer(opt_codec_ctx, td, sptr, opt_mname,
1179 buf_ptr, size, 0, OCTET_STRING__convert_hexadecimal);
1180}
1181
1182/*
1183 * Decode OCTET STRING from the binary (0/1) data.
1184 */
1185asn_dec_rval_t
1186OCTET_STRING_decode_xer_binary(asn_codec_ctx_t *opt_codec_ctx,
1187 asn_TYPE_descriptor_t *td, void **sptr,
Lev Walkin8c3b8542005-03-10 18:52:02 +00001188 const char *opt_mname, const void *buf_ptr, size_t size) {
Lev Walkindc06f6b2004-10-20 15:50:55 +00001189 return OCTET_STRING__decode_xer(opt_codec_ctx, td, sptr, opt_mname,
1190 buf_ptr, size, 0, OCTET_STRING__convert_binary);
1191}
1192
1193/*
1194 * Decode OCTET STRING from the string (ASCII/UTF-8) data.
1195 */
1196asn_dec_rval_t
1197OCTET_STRING_decode_xer_utf8(asn_codec_ctx_t *opt_codec_ctx,
1198 asn_TYPE_descriptor_t *td, void **sptr,
Lev Walkin8c3b8542005-03-10 18:52:02 +00001199 const char *opt_mname, const void *buf_ptr, size_t size) {
Lev Walkindc06f6b2004-10-20 15:50:55 +00001200 return OCTET_STRING__decode_xer(opt_codec_ctx, td, sptr, opt_mname,
1201 buf_ptr, size,
1202 OCTET_STRING__handle_control_chars,
1203 OCTET_STRING__convert_entrefs);
1204}
1205
Lev Walkin725883b2006-10-09 12:07:58 +00001206static int
Lev Walkin3a4689a2006-11-24 11:20:27 +00001207OCTET_STRING_per_get_characters(asn_per_data_t *po, uint8_t *buf,
1208 size_t units, unsigned int bpc, unsigned int unit_bits,
Lev Walkin494fb702017-08-07 20:07:00 -07001209 long lb, long ub, const asn_per_constraints_t *pc) {
Lev Walkin3a4689a2006-11-24 11:20:27 +00001210 uint8_t *end = buf + units * bpc;
Lev Walkin725883b2006-10-09 12:07:58 +00001211
1212 ASN_DEBUG("Expanding %d characters into (%ld..%ld):%d",
Lev Walkin3a4689a2006-11-24 11:20:27 +00001213 (int)units, lb, ub, unit_bits);
Lev Walkin725883b2006-10-09 12:07:58 +00001214
1215 /* X.691: 27.5.4 */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001216 if((unsigned long)ub <= ((unsigned long)2 << (unit_bits - 1))) {
Lev Walkin725883b2006-10-09 12:07:58 +00001217 /* Decode without translation */
1218 lb = 0;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001219 } else if(pc && pc->code2value) {
1220 if(unit_bits > 16)
1221 return 1; /* FATAL: can't have constrained
1222 * UniversalString with more than
1223 * 16 million code points */
1224 for(; buf < end; buf += bpc) {
Lev Walkin725883b2006-10-09 12:07:58 +00001225 int value;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001226 int code = per_get_few_bits(po, unit_bits);
Lev Walkin725883b2006-10-09 12:07:58 +00001227 if(code < 0) return -1; /* WMORE */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001228 value = pc->code2value(code);
Lev Walkin725883b2006-10-09 12:07:58 +00001229 if(value < 0) {
1230 ASN_DEBUG("Code %d (0x%02x) is"
1231 " not in map (%ld..%ld)",
1232 code, code, lb, ub);
1233 return 1; /* FATAL */
1234 }
Lev Walkin3a4689a2006-11-24 11:20:27 +00001235 switch(bpc) {
1236 case 1: *buf = value; break;
1237 case 2: buf[0] = value >> 8; buf[1] = value; break;
1238 case 4: buf[0] = value >> 24; buf[1] = value >> 16;
1239 buf[2] = value >> 8; buf[3] = value; break;
1240 }
Lev Walkin725883b2006-10-09 12:07:58 +00001241 }
1242 return 0;
1243 }
1244
Lev Walkin3a4689a2006-11-24 11:20:27 +00001245 /* Shortcut the no-op copying to the aligned structure */
1246 if(lb == 0 && (unit_bits == 8 * bpc)) {
1247 return per_get_many_bits(po, buf, 0, unit_bits * units);
1248 }
1249
1250 for(; buf < end; buf += bpc) {
Lev Walkin725883b2006-10-09 12:07:58 +00001251 int code = per_get_few_bits(po, unit_bits);
1252 int ch = code + lb;
1253 if(code < 0) return -1; /* WMORE */
1254 if(ch > ub) {
1255 ASN_DEBUG("Code %d is out of range (%ld..%ld)",
1256 ch, lb, ub);
1257 return 1; /* FATAL */
1258 }
Lev Walkin3a4689a2006-11-24 11:20:27 +00001259 switch(bpc) {
1260 case 1: *buf = ch; break;
1261 case 2: buf[0] = ch >> 8; buf[1] = ch; break;
1262 case 4: buf[0] = ch >> 24; buf[1] = ch >> 16;
1263 buf[2] = ch >> 8; buf[3] = ch; break;
1264 }
Lev Walkin725883b2006-10-09 12:07:58 +00001265 }
1266
1267 return 0;
1268}
1269
1270static int
Lev Walkin3a4689a2006-11-24 11:20:27 +00001271OCTET_STRING_per_put_characters(asn_per_outp_t *po, const uint8_t *buf,
1272 size_t units, unsigned int bpc, unsigned int unit_bits,
Lev Walkin494fb702017-08-07 20:07:00 -07001273 long lb, long ub, const asn_per_constraints_t *pc) {
Lev Walkin3a4689a2006-11-24 11:20:27 +00001274 const uint8_t *end = buf + units * bpc;
Lev Walkin725883b2006-10-09 12:07:58 +00001275
Lev Walkin3a4689a2006-11-24 11:20:27 +00001276 ASN_DEBUG("Squeezing %d characters into (%ld..%ld):%d (%d bpc)",
1277 (int)units, lb, ub, unit_bits, bpc);
Lev Walkin725883b2006-10-09 12:07:58 +00001278
1279 /* X.691: 27.5.4 */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001280 if((unsigned long)ub <= ((unsigned long)2 << (unit_bits - 1))) {
Lev Walkin725883b2006-10-09 12:07:58 +00001281 /* Encode as is */
1282 lb = 0;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001283 } else if(pc && pc->value2code) {
1284 for(; buf < end; buf += bpc) {
1285 int code;
1286 uint32_t value;
1287 switch(bpc) {
1288 case 1: value = *(const uint8_t *)buf; break;
1289 case 2: value = (buf[0] << 8) | buf[1]; break;
1290 case 4: value = (buf[0] << 24) | (buf[1] << 16)
1291 | (buf[2] << 8) | buf[3]; break;
1292 default: return -1;
1293 }
1294 code = pc->value2code(value);
Lev Walkin725883b2006-10-09 12:07:58 +00001295 if(code < 0) {
1296 ASN_DEBUG("Character %d (0x%02x) is"
1297 " not in map (%ld..%ld)",
1298 *buf, *buf, lb, ub);
1299 return -1;
1300 }
1301 if(per_put_few_bits(po, code, unit_bits))
1302 return -1;
1303 }
1304 }
1305
Lev Walkin3a4689a2006-11-24 11:20:27 +00001306 /* Shortcut the no-op copying to the aligned structure */
1307 if(lb == 0 && (unit_bits == 8 * bpc)) {
1308 return per_put_many_bits(po, buf, unit_bits * units);
1309 }
1310
1311 for(ub -= lb; buf < end; buf += bpc) {
1312 int ch;
1313 uint32_t value;
1314 switch(bpc) {
1315 case 1: value = *(const uint8_t *)buf; break;
1316 case 2: value = (buf[0] << 8) | buf[1]; break;
1317 case 4: value = (buf[0] << 24) | (buf[1] << 16)
1318 | (buf[2] << 8) | buf[3]; break;
1319 default: return -1;
1320 }
1321 ch = value - lb;
Lev Walkin725883b2006-10-09 12:07:58 +00001322 if(ch < 0 || ch > ub) {
1323 ASN_DEBUG("Character %d (0x%02x)"
1324 " is out of range (%ld..%ld)",
1325 *buf, *buf, lb, ub + lb);
1326 return -1;
1327 }
1328 if(per_put_few_bits(po, ch, unit_bits))
1329 return -1;
1330 }
1331
1332 return 0;
1333}
1334
Lev Walkin62d76872017-08-05 22:49:42 -07001335#ifndef ASN_DISABLE_PER_SUPPORT
1336
Lev Walkin59b176e2005-11-26 11:25:14 +00001337asn_dec_rval_t
1338OCTET_STRING_decode_uper(asn_codec_ctx_t *opt_codec_ctx,
Lev Walkin494fb702017-08-07 20:07:00 -07001339 asn_TYPE_descriptor_t *td,
1340 const asn_per_constraints_t *constraints, void **sptr,
1341 asn_per_data_t *pd) {
1342 asn_OCTET_STRING_specifics_t *specs = td->specifics
Lev Walkin59b176e2005-11-26 11:25:14 +00001343 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +08001344 : &asn_SPC_OCTET_STRING_specs;
Lev Walkin494fb702017-08-07 20:07:00 -07001345 const asn_per_constraints_t *pc =
1346 constraints ? constraints : td->per_constraints;
1347 const asn_per_constraint_t *cval;
1348 const asn_per_constraint_t *csiz;
Lev Walkin59b176e2005-11-26 11:25:14 +00001349 asn_dec_rval_t rval = { RC_OK, 0 };
1350 BIT_STRING_t *st = (BIT_STRING_t *)*sptr;
1351 ssize_t consumed_myself = 0;
1352 int repeat;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001353 enum {
1354 OS__BPC_BIT = 0,
1355 OS__BPC_CHAR = 1,
1356 OS__BPC_U16 = 2,
1357 OS__BPC_U32 = 4
1358 } bpc; /* Bytes per character */
1359 unsigned int unit_bits;
1360 unsigned int canonical_unit_bits;
Lev Walkin59b176e2005-11-26 11:25:14 +00001361
1362 (void)opt_codec_ctx;
1363
Lev Walkin3a4689a2006-11-24 11:20:27 +00001364 if(pc) {
1365 cval = &pc->value;
1366 csiz = &pc->size;
1367 } else {
1368 cval = &asn_DEF_OCTET_STRING_constraints.value;
1369 csiz = &asn_DEF_OCTET_STRING_constraints.size;
1370 }
1371
1372 switch(specs->subvariant) {
1373 default:
1374 case ASN_OSUBV_ANY:
1375 ASN_DEBUG("Unrecognized subvariant %d", specs->subvariant);
1376 RETURN(RC_FAIL);
1377 case ASN_OSUBV_BIT:
1378 canonical_unit_bits = unit_bits = 1;
1379 bpc = OS__BPC_BIT;
1380 break;
1381 case ASN_OSUBV_STR:
1382 canonical_unit_bits = unit_bits = 8;
1383 if(cval->flags & APC_CONSTRAINED)
1384 unit_bits = cval->range_bits;
1385 bpc = OS__BPC_CHAR;
1386 break;
1387 case ASN_OSUBV_U16:
1388 canonical_unit_bits = unit_bits = 16;
1389 if(cval->flags & APC_CONSTRAINED)
1390 unit_bits = cval->range_bits;
1391 bpc = OS__BPC_U16;
1392 break;
1393 case ASN_OSUBV_U32:
1394 canonical_unit_bits = unit_bits = 32;
1395 if(cval->flags & APC_CONSTRAINED)
1396 unit_bits = cval->range_bits;
1397 bpc = OS__BPC_U32;
1398 break;
1399 }
1400
Lev Walkin59b176e2005-11-26 11:25:14 +00001401 /*
1402 * Allocate the string.
1403 */
1404 if(!st) {
1405 st = (BIT_STRING_t *)(*sptr = CALLOC(1, specs->struct_size));
1406 if(!st) RETURN(RC_FAIL);
1407 }
1408
Lev Walkin725883b2006-10-09 12:07:58 +00001409 ASN_DEBUG("PER Decoding %s size %ld .. %ld bits %d",
Lev Walkin3a4689a2006-11-24 11:20:27 +00001410 csiz->flags & APC_EXTENSIBLE ? "extensible" : "non-extensible",
1411 csiz->lower_bound, csiz->upper_bound, csiz->effective_bits);
Lev Walkince676dd2005-12-20 22:34:55 +00001412
Lev Walkin3a4689a2006-11-24 11:20:27 +00001413 if(csiz->flags & APC_EXTENSIBLE) {
Lev Walkin59b176e2005-11-26 11:25:14 +00001414 int inext = per_get_few_bits(pd, 1);
Lev Walkin0a8aa602006-09-18 20:05:55 +00001415 if(inext < 0) RETURN(RC_WMORE);
Lev Walkin3a4689a2006-11-24 11:20:27 +00001416 if(inext) {
1417 csiz = &asn_DEF_OCTET_STRING_constraints.size;
1418 cval = &asn_DEF_OCTET_STRING_constraints.value;
1419 unit_bits = canonical_unit_bits;
1420 }
Lev Walkin59b176e2005-11-26 11:25:14 +00001421 }
1422
Lev Walkin3a4689a2006-11-24 11:20:27 +00001423 if(csiz->effective_bits >= 0) {
Lev Walkin59b176e2005-11-26 11:25:14 +00001424 FREEMEM(st->buf);
Lev Walkin3a4689a2006-11-24 11:20:27 +00001425 if(bpc) {
1426 st->size = csiz->upper_bound * bpc;
Lev Walkin59b176e2005-11-26 11:25:14 +00001427 } else {
Lev Walkin3a4689a2006-11-24 11:20:27 +00001428 st->size = (csiz->upper_bound + 7) >> 3;
Lev Walkin59b176e2005-11-26 11:25:14 +00001429 }
1430 st->buf = (uint8_t *)MALLOC(st->size + 1);
1431 if(!st->buf) { st->size = 0; RETURN(RC_FAIL); }
1432 }
1433
1434 /* X.691, #16.5: zero-length encoding */
1435 /* X.691, #16.6: short fixed length encoding (up to 2 octets) */
1436 /* X.691, #16.7: long fixed length encoding (up to 64K octets) */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001437 if(csiz->effective_bits == 0) {
Lev Walkin725883b2006-10-09 12:07:58 +00001438 int ret;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001439 if(bpc) {
1440 ASN_DEBUG("Encoding OCTET STRING size %ld",
1441 csiz->upper_bound);
1442 ret = OCTET_STRING_per_get_characters(pd, st->buf,
1443 csiz->upper_bound, bpc, unit_bits,
1444 cval->lower_bound, cval->upper_bound, pc);
Lev Walkin725883b2006-10-09 12:07:58 +00001445 if(ret > 0) RETURN(RC_FAIL);
1446 } else {
Lev Walkin3a4689a2006-11-24 11:20:27 +00001447 ASN_DEBUG("Encoding BIT STRING size %ld",
1448 csiz->upper_bound);
Lev Walkin725883b2006-10-09 12:07:58 +00001449 ret = per_get_many_bits(pd, st->buf, 0,
Lev Walkin3a4689a2006-11-24 11:20:27 +00001450 unit_bits * csiz->upper_bound);
Lev Walkin725883b2006-10-09 12:07:58 +00001451 }
Lev Walkin0a8aa602006-09-18 20:05:55 +00001452 if(ret < 0) RETURN(RC_WMORE);
Lev Walkin3a4689a2006-11-24 11:20:27 +00001453 consumed_myself += unit_bits * csiz->upper_bound;
Lev Walkin59b176e2005-11-26 11:25:14 +00001454 st->buf[st->size] = 0;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001455 if(bpc == 0) {
1456 int ubs = (csiz->upper_bound & 0x7);
1457 st->bits_unused = ubs ? 8 - ubs : 0;
1458 }
Lev Walkin59b176e2005-11-26 11:25:14 +00001459 RETURN(RC_OK);
1460 }
1461
1462 st->size = 0;
1463 do {
Lev Walkin3a4689a2006-11-24 11:20:27 +00001464 ssize_t raw_len;
Lev Walkin59b176e2005-11-26 11:25:14 +00001465 ssize_t len_bytes;
1466 ssize_t len_bits;
1467 void *p;
1468 int ret;
1469
1470 /* Get the PER length */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001471 raw_len = uper_get_length(pd, csiz->effective_bits, &repeat);
1472 if(raw_len < 0) RETURN(RC_WMORE);
1473 raw_len += csiz->lower_bound;
Lev Walkin59b176e2005-11-26 11:25:14 +00001474
Lev Walkin0a8aa602006-09-18 20:05:55 +00001475 ASN_DEBUG("Got PER length eb %ld, len %ld, %s (%s)",
Lev Walkin3a4689a2006-11-24 11:20:27 +00001476 (long)csiz->effective_bits, (long)raw_len,
Lev Walkin0a8aa602006-09-18 20:05:55 +00001477 repeat ? "repeat" : "once", td->name);
Lev Walkin3a4689a2006-11-24 11:20:27 +00001478 if(bpc) {
1479 len_bytes = raw_len * bpc;
1480 len_bits = len_bytes * unit_bits;
1481 } else {
1482 len_bits = raw_len;
Lev Walkin59b176e2005-11-26 11:25:14 +00001483 len_bytes = (len_bits + 7) >> 3;
1484 if(len_bits & 0x7)
1485 st->bits_unused = 8 - (len_bits & 0x7);
1486 /* len_bits be multiple of 16K if repeat is set */
Lev Walkin59b176e2005-11-26 11:25:14 +00001487 }
1488 p = REALLOC(st->buf, st->size + len_bytes + 1);
1489 if(!p) RETURN(RC_FAIL);
1490 st->buf = (uint8_t *)p;
1491
Lev Walkin3a4689a2006-11-24 11:20:27 +00001492 if(bpc) {
1493 ret = OCTET_STRING_per_get_characters(pd,
1494 &st->buf[st->size], raw_len, bpc, unit_bits,
1495 cval->lower_bound, cval->upper_bound, pc);
Lev Walkin725883b2006-10-09 12:07:58 +00001496 if(ret > 0) RETURN(RC_FAIL);
1497 } else {
1498 ret = per_get_many_bits(pd, &st->buf[st->size],
1499 0, len_bits);
1500 }
Lev Walkin0a8aa602006-09-18 20:05:55 +00001501 if(ret < 0) RETURN(RC_WMORE);
Lev Walkin59b176e2005-11-26 11:25:14 +00001502 st->size += len_bytes;
1503 } while(repeat);
1504 st->buf[st->size] = 0; /* nul-terminate */
1505
1506 return rval;
1507}
Lev Walkindc06f6b2004-10-20 15:50:55 +00001508
Lev Walkin523de9e2006-08-18 01:34:18 +00001509asn_enc_rval_t
1510OCTET_STRING_encode_uper(asn_TYPE_descriptor_t *td,
Lev Walkin494fb702017-08-07 20:07:00 -07001511 const asn_per_constraints_t *constraints, void *sptr,
1512 asn_per_outp_t *po) {
1513 asn_OCTET_STRING_specifics_t *specs = td->specifics
Lev Walkin523de9e2006-08-18 01:34:18 +00001514 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +08001515 : &asn_SPC_OCTET_STRING_specs;
Lev Walkin494fb702017-08-07 20:07:00 -07001516 const asn_per_constraints_t *pc = constraints ? constraints
Lev Walkin725883b2006-10-09 12:07:58 +00001517 : td->per_constraints;
Lev Walkin494fb702017-08-07 20:07:00 -07001518 const asn_per_constraint_t *cval;
1519 const asn_per_constraint_t *csiz;
Lev Walkin523de9e2006-08-18 01:34:18 +00001520 const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001521 asn_enc_rval_t er = { 0, 0, 0 };
Lev Walkin523de9e2006-08-18 01:34:18 +00001522 int inext = 0; /* Lies not within extension root */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001523 unsigned int unit_bits;
1524 unsigned int canonical_unit_bits;
1525 unsigned int sizeinunits;
Lev Walkin523de9e2006-08-18 01:34:18 +00001526 const uint8_t *buf;
1527 int ret;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001528 enum {
1529 OS__BPC_BIT = 0,
1530 OS__BPC_CHAR = 1,
1531 OS__BPC_U16 = 2,
1532 OS__BPC_U32 = 4
1533 } bpc; /* Bytes per character */
1534 int ct_extensible;
Lev Walkin523de9e2006-08-18 01:34:18 +00001535
Lev Walkin2ddd3082006-10-16 12:32:14 +00001536 if(!st || (!st->buf && st->size))
Lev Walkin7c1dc052016-03-14 03:08:15 -07001537 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001538
Lev Walkin3a4689a2006-11-24 11:20:27 +00001539 if(pc) {
1540 cval = &pc->value;
1541 csiz = &pc->size;
1542 } else {
1543 cval = &asn_DEF_OCTET_STRING_constraints.value;
1544 csiz = &asn_DEF_OCTET_STRING_constraints.size;
1545 }
1546 ct_extensible = csiz->flags & APC_EXTENSIBLE;
1547
1548 switch(specs->subvariant) {
1549 default:
1550 case ASN_OSUBV_ANY:
Lev Walkin7c1dc052016-03-14 03:08:15 -07001551 ASN__ENCODE_FAILED;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001552 case ASN_OSUBV_BIT:
1553 canonical_unit_bits = unit_bits = 1;
1554 bpc = OS__BPC_BIT;
1555 sizeinunits = st->size * 8 - (st->bits_unused & 0x07);
Lev Walkin523de9e2006-08-18 01:34:18 +00001556 ASN_DEBUG("BIT STRING of %d bytes, %d bits unused",
1557 sizeinunits, st->bits_unused);
Lev Walkin3a4689a2006-11-24 11:20:27 +00001558 break;
1559 case ASN_OSUBV_STR:
1560 canonical_unit_bits = unit_bits = 8;
1561 if(cval->flags & APC_CONSTRAINED)
1562 unit_bits = cval->range_bits;
1563 bpc = OS__BPC_CHAR;
1564 sizeinunits = st->size;
1565 break;
1566 case ASN_OSUBV_U16:
1567 canonical_unit_bits = unit_bits = 16;
1568 if(cval->flags & APC_CONSTRAINED)
1569 unit_bits = cval->range_bits;
1570 bpc = OS__BPC_U16;
1571 sizeinunits = st->size / 2;
1572 break;
1573 case ASN_OSUBV_U32:
1574 canonical_unit_bits = unit_bits = 32;
1575 if(cval->flags & APC_CONSTRAINED)
1576 unit_bits = cval->range_bits;
1577 bpc = OS__BPC_U32;
1578 sizeinunits = st->size / 4;
1579 break;
Lev Walkin725883b2006-10-09 12:07:58 +00001580 }
1581
Lev Walkin00918812006-09-18 21:19:32 +00001582 ASN_DEBUG("Encoding %s into %d units of %d bits"
Lev Walkin725883b2006-10-09 12:07:58 +00001583 " (%ld..%ld, effective %d)%s",
Lev Walkin00918812006-09-18 21:19:32 +00001584 td->name, sizeinunits, unit_bits,
Lev Walkin3a4689a2006-11-24 11:20:27 +00001585 csiz->lower_bound, csiz->upper_bound,
1586 csiz->effective_bits, ct_extensible ? " EXT" : "");
Lev Walkin523de9e2006-08-18 01:34:18 +00001587
Lev Walkind4c16732013-03-28 05:00:47 -07001588 /* Figure out whether size lies within PER visible constraint */
Lev Walkin523de9e2006-08-18 01:34:18 +00001589
Lev Walkin3a4689a2006-11-24 11:20:27 +00001590 if(csiz->effective_bits >= 0) {
1591 if((int)sizeinunits < csiz->lower_bound
1592 || (int)sizeinunits > csiz->upper_bound) {
Lev Walkin523de9e2006-08-18 01:34:18 +00001593 if(ct_extensible) {
Lev Walkin3a4689a2006-11-24 11:20:27 +00001594 cval = &asn_DEF_OCTET_STRING_constraints.value;
1595 csiz = &asn_DEF_OCTET_STRING_constraints.size;
1596 unit_bits = canonical_unit_bits;
Lev Walkin523de9e2006-08-18 01:34:18 +00001597 inext = 1;
1598 } else
Lev Walkin7c1dc052016-03-14 03:08:15 -07001599 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001600 }
1601 } else {
1602 inext = 0;
1603 }
1604
1605 if(ct_extensible) {
1606 /* Declare whether length is [not] within extension root */
1607 if(per_put_few_bits(po, inext, 1))
Lev Walkin7c1dc052016-03-14 03:08:15 -07001608 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001609 }
1610
1611 /* X.691, #16.5: zero-length encoding */
1612 /* X.691, #16.6: short fixed length encoding (up to 2 octets) */
1613 /* X.691, #16.7: long fixed length encoding (up to 64K octets) */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001614 if(csiz->effective_bits >= 0) {
Lev Walkin523de9e2006-08-18 01:34:18 +00001615 ASN_DEBUG("Encoding %d bytes (%ld), length in %d bits",
Lev Walkin3a4689a2006-11-24 11:20:27 +00001616 st->size, sizeinunits - csiz->lower_bound,
1617 csiz->effective_bits);
1618 ret = per_put_few_bits(po, sizeinunits - csiz->lower_bound,
1619 csiz->effective_bits);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001620 if(ret) ASN__ENCODE_FAILED;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001621 if(bpc) {
1622 ret = OCTET_STRING_per_put_characters(po, st->buf,
1623 sizeinunits, bpc, unit_bits,
1624 cval->lower_bound, cval->upper_bound, pc);
Lev Walkin725883b2006-10-09 12:07:58 +00001625 } else {
1626 ret = per_put_many_bits(po, st->buf,
1627 sizeinunits * unit_bits);
1628 }
Lev Walkin7c1dc052016-03-14 03:08:15 -07001629 if(ret) ASN__ENCODE_FAILED;
1630 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +00001631 }
1632
1633 ASN_DEBUG("Encoding %d bytes", st->size);
1634
1635 if(sizeinunits == 0) {
1636 if(uper_put_length(po, 0))
Lev Walkin7c1dc052016-03-14 03:08:15 -07001637 ASN__ENCODE_FAILED;
1638 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +00001639 }
1640
1641 buf = st->buf;
1642 while(sizeinunits) {
1643 ssize_t maySave = uper_put_length(po, sizeinunits);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001644 if(maySave < 0) ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001645
Lev Walkin345e4c62006-10-19 02:44:08 +00001646 ASN_DEBUG("Encoding %ld of %ld",
1647 (long)maySave, (long)sizeinunits);
Lev Walkin523de9e2006-08-18 01:34:18 +00001648
Lev Walkin3a4689a2006-11-24 11:20:27 +00001649 if(bpc) {
1650 ret = OCTET_STRING_per_put_characters(po, buf,
1651 maySave, bpc, unit_bits,
1652 cval->lower_bound, cval->upper_bound, pc);
Lev Walkin725883b2006-10-09 12:07:58 +00001653 } else {
1654 ret = per_put_many_bits(po, buf, maySave * unit_bits);
1655 }
Lev Walkin7c1dc052016-03-14 03:08:15 -07001656 if(ret) ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001657
Lev Walkin3a4689a2006-11-24 11:20:27 +00001658 if(bpc)
1659 buf += maySave * bpc;
Lev Walkin523de9e2006-08-18 01:34:18 +00001660 else
Lev Walkin3a4689a2006-11-24 11:20:27 +00001661 buf += maySave >> 3;
Lev Walkin523de9e2006-08-18 01:34:18 +00001662 sizeinunits -= maySave;
1663 assert(!(maySave & 0x07) || !sizeinunits);
1664 }
1665
Lev Walkin7c1dc052016-03-14 03:08:15 -07001666 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +00001667}
1668
Lev Walkin62d76872017-08-05 22:49:42 -07001669#endif /* ASN_DISABLE_PER_SUPPORT */
1670
Lev Walkinf15320b2004-06-03 03:38:44 +00001671int
Lev Walkinde4825d2004-09-29 13:20:14 +00001672OCTET_STRING_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +00001673 asn_app_consume_bytes_f *cb, void *app_key) {
Wim Lewis59e8d282014-08-04 12:39:35 -07001674 const char * const h2c = "0123456789ABCDEF";
Lev Walkinc2346572004-08-11 09:07:36 +00001675 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +00001676 char scratch[16 * 3 + 4];
1677 char *p = scratch;
1678 uint8_t *buf;
1679 uint8_t *end;
1680 size_t i;
Lev Walkinf15320b2004-06-03 03:38:44 +00001681
Lev Walkind9bd7752004-06-05 08:17:50 +00001682 (void)td; /* Unused argument */
1683
Lev Walkin2ddd3082006-10-16 12:32:14 +00001684 if(!st || (!st->buf && st->size))
1685 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001686
1687 /*
1688 * Dump the contents of the buffer in hexadecimal.
1689 */
1690 buf = st->buf;
1691 end = buf + st->size;
1692 for(i = 0; buf < end; buf++, i++) {
1693 if(!(i % 16) && (i || st->size > 16)) {
Lev Walkin8e8078a2004-09-26 13:10:40 +00001694 if(cb(scratch, p - scratch, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +00001695 return -1;
Lev Walkin8e8078a2004-09-26 13:10:40 +00001696 _i_INDENT(1);
Lev Walkinf15320b2004-06-03 03:38:44 +00001697 p = scratch;
1698 }
1699 *p++ = h2c[(*buf >> 4) & 0x0F];
1700 *p++ = h2c[*buf & 0x0F];
Lev Walkina9cc46e2004-09-22 16:06:28 +00001701 *p++ = 0x20;
Lev Walkinf15320b2004-06-03 03:38:44 +00001702 }
1703
Lev Walkincc6a9102004-09-23 22:06:26 +00001704 if(p > scratch) {
1705 p--; /* Remove the tail space */
Lev Walkin8e8078a2004-09-26 13:10:40 +00001706 if(cb(scratch, p - scratch, app_key) < 0)
Lev Walkincc6a9102004-09-23 22:06:26 +00001707 return -1;
1708 }
1709
1710 return 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001711}
1712
1713int
Lev Walkindc06f6b2004-10-20 15:50:55 +00001714OCTET_STRING_print_utf8(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +00001715 int ilevel, asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +00001716 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +00001717
Lev Walkind9bd7752004-06-05 08:17:50 +00001718 (void)td; /* Unused argument */
1719 (void)ilevel; /* Unused argument */
1720
Lev Walkin2ddd3082006-10-16 12:32:14 +00001721 if(st && (st->buf || !st->size)) {
Lev Walkin8e8078a2004-09-26 13:10:40 +00001722 return (cb(st->buf, st->size, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001723 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +00001724 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001725 }
1726}
1727
1728void
Lev Walkinde4825d2004-09-29 13:20:14 +00001729OCTET_STRING_free(asn_TYPE_descriptor_t *td, void *sptr, int contents_only) {
Lev Walkinc2346572004-08-11 09:07:36 +00001730 OCTET_STRING_t *st = (OCTET_STRING_t *)sptr;
Lev Walkin034ee192016-03-14 02:28:33 -07001731 asn_OCTET_STRING_specifics_t *specs;
1732 asn_struct_ctx_t *ctx;
Lev Walkinb54577a2004-11-08 10:47:12 +00001733 struct _stack *stck;
Lev Walkinf15320b2004-06-03 03:38:44 +00001734
1735 if(!td || !st)
1736 return;
1737
Lev Walkin034ee192016-03-14 02:28:33 -07001738 specs = td->specifics
1739 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +08001740 : &asn_SPC_OCTET_STRING_specs;
Lev Walkin034ee192016-03-14 02:28:33 -07001741 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
1742
Lev Walkinf15320b2004-06-03 03:38:44 +00001743 ASN_DEBUG("Freeing %s as OCTET STRING", td->name);
1744
1745 if(st->buf) {
1746 FREEMEM(st->buf);
Lev Walkin2ddd3082006-10-16 12:32:14 +00001747 st->buf = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001748 }
1749
1750 /*
1751 * Remove decode-time stack.
1752 */
Lev Walkinb54577a2004-11-08 10:47:12 +00001753 stck = (struct _stack *)ctx->ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +00001754 if(stck) {
1755 while(stck->tail) {
1756 struct _stack_el *sel = stck->tail;
1757 stck->tail = sel->prev;
1758 FREEMEM(sel);
1759 }
1760 FREEMEM(stck);
1761 }
1762
1763 if(!contents_only) {
1764 FREEMEM(st);
1765 }
1766}
1767
1768/*
1769 * Conversion routines.
1770 */
1771int
1772OCTET_STRING_fromBuf(OCTET_STRING_t *st, const char *str, int len) {
1773 void *buf;
1774
1775 if(st == 0 || (str == 0 && len)) {
1776 errno = EINVAL;
1777 return -1;
1778 }
1779
1780 /*
1781 * Clear the OCTET STRING.
1782 */
1783 if(str == NULL) {
Lev Walkin7b284812005-12-17 11:43:25 +00001784 FREEMEM(st->buf);
1785 st->buf = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001786 st->size = 0;
1787 return 0;
1788 }
1789
1790 /* Determine the original string size, if not explicitly given */
1791 if(len < 0)
1792 len = strlen(str);
1793
1794 /* Allocate and fill the memory */
1795 buf = MALLOC(len + 1);
Lev Walkin7b284812005-12-17 11:43:25 +00001796 if(buf == NULL)
Lev Walkinf15320b2004-06-03 03:38:44 +00001797 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +00001798
1799 memcpy(buf, str, len);
Lev Walkin7b284812005-12-17 11:43:25 +00001800 ((uint8_t *)buf)[len] = '\0'; /* Couldn't use memcpy(len+1)! */
1801 FREEMEM(st->buf);
1802 st->buf = (uint8_t *)buf;
1803 st->size = len;
Lev Walkinf15320b2004-06-03 03:38:44 +00001804
1805 return 0;
1806}
1807
1808OCTET_STRING_t *
Lev Walkinbbd93252004-10-12 05:57:23 +00001809OCTET_STRING_new_fromBuf(asn_TYPE_descriptor_t *td, const char *str, int len) {
1810 asn_OCTET_STRING_specifics_t *specs = td->specifics
Lev Walkindc06f6b2004-10-20 15:50:55 +00001811 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +08001812 : &asn_SPC_OCTET_STRING_specs;
Lev Walkinf15320b2004-06-03 03:38:44 +00001813 OCTET_STRING_t *st;
1814
Lev Walkinbbd93252004-10-12 05:57:23 +00001815 st = (OCTET_STRING_t *)CALLOC(1, specs->struct_size);
Lev Walkinf15320b2004-06-03 03:38:44 +00001816 if(st && str && OCTET_STRING_fromBuf(st, str, len)) {
Lev Walkin419f6752006-09-13 04:02:00 +00001817 FREEMEM(st);
Lev Walkinf15320b2004-06-03 03:38:44 +00001818 st = NULL;
1819 }
1820
1821 return st;
1822}
1823
Lev Walkincd2f48e2017-08-10 02:14:59 -07001824/*
1825 * Lexicographically compare the common prefix of both strings,
1826 * and if it is the same return -1 for the smallest string.
1827 */
1828int
1829OCTET_STRING_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
1830 const void *bptr) {
1831 const OCTET_STRING_t *a = aptr;
1832 const OCTET_STRING_t *b = bptr;
1833
1834 (void)td;
1835
1836 if(a && b) {
1837 size_t common_prefix_size = a->size <= b->size ? a->size : b->size;
1838 int ret = memcmp(a->buf, b->buf, common_prefix_size);
1839 if(ret == 0) {
1840 /* Figure out which string with equal prefixes is longer. */
1841 if(a->size < b->size) {
1842 return -1;
1843 } else if(a->size > b->size) {
1844 return 1;
1845 } else {
1846 return 0;
1847 }
1848 } else {
1849 return ret;
1850 }
1851 } else if(!a && !b) {
1852 return 0;
1853 } else if(!a) {
1854 return -1;
1855 } else {
1856 return 1;
1857 }
1858
1859}
1860