blob: 9d6bc9f639178328b26dc60570ea847167124703 [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 asn_generic_no_constraint,
32 OCTET_STRING_decode_ber,
33 OCTET_STRING_encode_der,
Lev Walkindc06f6b2004-10-20 15:50:55 +000034 OCTET_STRING_decode_xer_hex,
Lev Walkina9cc46e2004-09-22 16:06:28 +000035 OCTET_STRING_encode_xer,
Lev Walkincc159472017-07-06 08:26:36 -070036#ifdef ASN_DISABLE_OER_SUPPORT
37 0,
38 0,
39#else
Lev Walkin2535bd52017-08-27 23:45:56 -070040 OCTET_STRING_decode_oer,
41 OCTET_STRING_encode_oer,
Lev Walkincc159472017-07-06 08:26:36 -070042#endif /* ASN_DISABLE_OER_SUPPORT */
Lev Walkinb33425f2017-07-14 14:59:52 +040043#ifdef ASN_DISABLE_PER_SUPPORT
44 0,
45 0,
46#else
47 OCTET_STRING_decode_uper, /* Unaligned PER decoder */
48 OCTET_STRING_encode_uper, /* Unaligned PER encoder */
49#endif /* ASN_DISABLE_PER_SUPPORT */
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080050 0 /* Use generic outmost tag fetcher */
51};
52asn_TYPE_descriptor_t asn_DEF_OCTET_STRING = {
53 "OCTET STRING", /* Canonical name */
54 "OCTET_STRING", /* XML tag name */
55 &asn_OP_OCTET_STRING,
56 asn_generic_no_constraint,
Lev Walkinde4825d2004-09-29 13:20:14 +000057 asn_DEF_OCTET_STRING_tags,
58 sizeof(asn_DEF_OCTET_STRING_tags)
59 / sizeof(asn_DEF_OCTET_STRING_tags[0]),
60 asn_DEF_OCTET_STRING_tags, /* Same as above */
61 sizeof(asn_DEF_OCTET_STRING_tags)
62 / sizeof(asn_DEF_OCTET_STRING_tags[0]),
Lev Walkin76780762017-07-07 10:07:30 -070063 0, /* No OER visible constraints */
Lev Walkin59b176e2005-11-26 11:25:14 +000064 0, /* No PER visible constraints */
Lev Walkin449f8322004-08-20 13:23:42 +000065 0, 0, /* No members */
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +080066 &asn_SPC_OCTET_STRING_specs
Lev Walkinf15320b2004-06-03 03:38:44 +000067};
68
Lev Walkincc6a9102004-09-23 22:06:26 +000069#undef _CH_PHASE
70#undef NEXT_PHASE
71#undef PREV_PHASE
Lev Walkin75b1bef2005-04-25 19:38:21 +000072#define _CH_PHASE(ctx, inc) do { \
73 if(ctx->phase == 0) \
74 ctx->context = 0; \
75 ctx->phase += inc; \
Lev Walkinf15320b2004-06-03 03:38:44 +000076 } while(0)
77#define NEXT_PHASE(ctx) _CH_PHASE(ctx, +1)
78#define PREV_PHASE(ctx) _CH_PHASE(ctx, -1)
79
Lev Walkincc6a9102004-09-23 22:06:26 +000080#undef ADVANCE
Lev Walkin75b1bef2005-04-25 19:38:21 +000081#define ADVANCE(num_bytes) do { \
82 size_t num = (num_bytes); \
83 buf_ptr = ((const char *)buf_ptr) + num; \
84 size -= num; \
85 consumed_myself += num; \
Lev Walkinf15320b2004-06-03 03:38:44 +000086 } while(0)
87
Lev Walkincc6a9102004-09-23 22:06:26 +000088#undef RETURN
Lev Walkin75b1bef2005-04-25 19:38:21 +000089#define RETURN(_code) do { \
Lev Walkin59b176e2005-11-26 11:25:14 +000090 asn_dec_rval_t tmprval; \
91 tmprval.code = _code; \
92 tmprval.consumed = consumed_myself; \
93 return tmprval; \
Lev Walkinf15320b2004-06-03 03:38:44 +000094 } while(0)
95
Lev Walkincc6a9102004-09-23 22:06:26 +000096#undef APPEND
Lev Walkind9bd7752004-06-05 08:17:50 +000097#define APPEND(bufptr, bufsize) do { \
Lev Walkin75b1bef2005-04-25 19:38:21 +000098 size_t _bs = (bufsize); /* Append size */ \
99 size_t _ns = ctx->context; /* Allocated now */ \
100 size_t _es = st->size + _bs; /* Expected size */ \
101 /* int is really a typeof(st->size): */ \
102 if((int)_es < 0) RETURN(RC_FAIL); \
103 if(_ns <= _es) { \
Lev Walkind9bd7752004-06-05 08:17:50 +0000104 void *ptr; \
Lev Walkin188ed2c2004-09-13 08:31:01 +0000105 /* Be nice and round to the memory allocator */ \
Lev Walkin75b1bef2005-04-25 19:38:21 +0000106 do { _ns = _ns ? _ns << 1 : 16; } \
107 while(_ns <= _es); \
108 /* int is really a typeof(st->size): */ \
109 if((int)_ns < 0) RETURN(RC_FAIL); \
Lev Walkind9bd7752004-06-05 08:17:50 +0000110 ptr = REALLOC(st->buf, _ns); \
111 if(ptr) { \
Lev Walkinc2346572004-08-11 09:07:36 +0000112 st->buf = (uint8_t *)ptr; \
Lev Walkin75b1bef2005-04-25 19:38:21 +0000113 ctx->context = _ns; \
Lev Walkind9bd7752004-06-05 08:17:50 +0000114 } else { \
115 RETURN(RC_FAIL); \
116 } \
Lev Walkin7f85ef42005-07-02 20:24:27 +0000117 ASN_DEBUG("Reallocating into %ld", (long)_ns); \
Lev Walkind9bd7752004-06-05 08:17:50 +0000118 } \
Lev Walkin8d127872004-09-04 04:44:50 +0000119 memcpy(st->buf + st->size, bufptr, _bs); \
Lev Walkind9bd7752004-06-05 08:17:50 +0000120 /* Convenient nul-termination */ \
Lev Walkin75b1bef2005-04-25 19:38:21 +0000121 st->buf[_es] = '\0'; \
122 st->size = _es; \
Lev Walkinf15320b2004-06-03 03:38:44 +0000123 } while(0)
124
125/*
126 * The main reason why ASN.1 is still alive is that too much time and effort
127 * is necessary for learning it more or less adequately, thus creating a gut
128 * necessity to demonstrate that aquired skill everywhere afterwards.
129 * No, I am not going to explain what the following stuff is.
130 */
131struct _stack_el {
Lev Walkin494fb702017-08-07 20:07:00 -0700132 ber_tlv_len_t left; /* What's left to read (or -1) */
133 ber_tlv_len_t got; /* What was actually processed */
Lev Walkin24bf3f62017-08-10 17:58:43 -0700134 unsigned cont_level; /* Depth of subcontainment */
Lev Walkin494fb702017-08-07 20:07:00 -0700135 int want_nulls; /* Want null "end of content" octets? */
136 int bits_chopped; /* Flag in BIT STRING mode */
137 ber_tlv_tag_t tag; /* For debugging purposes */
138 struct _stack_el *prev;
139 struct _stack_el *next;
Lev Walkinf15320b2004-06-03 03:38:44 +0000140};
141struct _stack {
142 struct _stack_el *tail;
143 struct _stack_el *cur_ptr;
144};
145
146static struct _stack_el *
Lev Walkin3990ba62004-09-24 20:57:41 +0000147OS__add_stack_el(struct _stack *st) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000148 struct _stack_el *nel;
149
Lev Walkin188ed2c2004-09-13 08:31:01 +0000150 /*
151 * Reuse the old stack frame or allocate a new one.
152 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000153 if(st->cur_ptr && st->cur_ptr->next) {
154 nel = st->cur_ptr->next;
Lev Walkinf15320b2004-06-03 03:38:44 +0000155 nel->bits_chopped = 0;
Lev Walkin5c915992004-09-27 20:54:06 +0000156 nel->got = 0;
157 /* Retain the nel->cont_level, it's correct. */
Lev Walkinf15320b2004-06-03 03:38:44 +0000158 } else {
Lev Walkin814cca72004-12-15 23:23:53 +0000159 nel = (struct _stack_el *)CALLOC(1, sizeof(struct _stack_el));
Lev Walkinf15320b2004-06-03 03:38:44 +0000160 if(nel == NULL)
161 return NULL;
162
163 if(st->tail) {
Lev Walkin188ed2c2004-09-13 08:31:01 +0000164 /* Increase a subcontainment depth */
165 nel->cont_level = st->tail->cont_level + 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000166 st->tail->next = nel;
167 }
168 nel->prev = st->tail;
169 st->tail = nel;
170 }
171
172 st->cur_ptr = nel;
173
174 return nel;
175}
176
177static struct _stack *
johvik28268d92017-05-09 10:27:52 +0200178_new_stack(void) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000179 return (struct _stack *)CALLOC(1, sizeof(struct _stack));
Lev Walkinf15320b2004-06-03 03:38:44 +0000180}
181
182/*
183 * Decode OCTET STRING type.
184 */
Lev Walkindc06f6b2004-10-20 15:50:55 +0000185asn_dec_rval_t
Lev Walkinde4825d2004-09-29 13:20:14 +0000186OCTET_STRING_decode_ber(asn_codec_ctx_t *opt_codec_ctx,
187 asn_TYPE_descriptor_t *td,
Lev Walkin59b176e2005-11-26 11:25:14 +0000188 void **sptr, const void *buf_ptr, size_t size, int tag_mode) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000189 asn_OCTET_STRING_specifics_t *specs = td->specifics
Lev Walkindc06f6b2004-10-20 15:50:55 +0000190 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +0800191 : &asn_SPC_OCTET_STRING_specs;
Lev Walkin59b176e2005-11-26 11:25:14 +0000192 BIT_STRING_t *st = (BIT_STRING_t *)*sptr;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000193 asn_dec_rval_t rval;
Lev Walkinde4825d2004-09-29 13:20:14 +0000194 asn_struct_ctx_t *ctx;
Lev Walkinf15320b2004-06-03 03:38:44 +0000195 ssize_t consumed_myself = 0;
Lev Walkinbbd93252004-10-12 05:57:23 +0000196 struct _stack *stck; /* Expectations stack structure */
Lev Walkin5c915992004-09-27 20:54:06 +0000197 struct _stack_el *sel = 0; /* Stack element */
Lev Walkinf15320b2004-06-03 03:38:44 +0000198 int tlv_constr;
Lev Walkin3a4689a2006-11-24 11:20:27 +0000199 enum asn_OS_Subvariant type_variant = specs->subvariant;
Lev Walkinf15320b2004-06-03 03:38:44 +0000200
Lev Walkincc6a9102004-09-23 22:06:26 +0000201 ASN_DEBUG("Decoding %s as %s (frame %ld)",
202 td->name,
Lev Walkin3a4689a2006-11-24 11:20:27 +0000203 (type_variant == ASN_OSUBV_STR) ?
Lev Walkinbbd93252004-10-12 05:57:23 +0000204 "OCTET STRING" : "OS-SpecialCase",
Lev Walkincc6a9102004-09-23 22:06:26 +0000205 (long)size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000206
207 /*
208 * Create the string if does not exist.
209 */
210 if(st == NULL) {
Lev Walkin59b176e2005-11-26 11:25:14 +0000211 st = (BIT_STRING_t *)(*sptr = CALLOC(1, specs->struct_size));
212 if(st == NULL) RETURN(RC_FAIL);
Lev Walkinf15320b2004-06-03 03:38:44 +0000213 }
214
215 /* Restore parsing context */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800216 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000217
218 switch(ctx->phase) {
219 case 0:
220 /*
221 * Check tags.
222 */
Lev Walkinde4825d2004-09-29 13:20:14 +0000223 rval = ber_check_tags(opt_codec_ctx, td, ctx,
Lev Walkin8e8078a2004-09-26 13:10:40 +0000224 buf_ptr, size, tag_mode, -1,
Lev Walkinf15320b2004-06-03 03:38:44 +0000225 &ctx->left, &tlv_constr);
Lev Walkin443d2512004-10-05 06:35:31 +0000226 if(rval.code != RC_OK)
227 return rval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000228
Lev Walkinf15320b2004-06-03 03:38:44 +0000229 if(tlv_constr) {
230 /*
231 * Complex operation, requires stack of expectations.
232 */
233 ctx->ptr = _new_stack();
Lev Walkinb02145b2017-06-28 08:52:07 -0700234 if(!ctx->ptr) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000235 RETURN(RC_FAIL);
236 }
237 } else {
238 /*
239 * Jump into stackless primitive decoding.
240 */
241 _CH_PHASE(ctx, 3);
Lev Walkin3a4689a2006-11-24 11:20:27 +0000242 if(type_variant == ASN_OSUBV_ANY && tag_mode != 1)
Lev Walkin188ed2c2004-09-13 08:31:01 +0000243 APPEND(buf_ptr, rval.consumed);
Lev Walkinf15320b2004-06-03 03:38:44 +0000244 ADVANCE(rval.consumed);
245 goto phase3;
246 }
247
Lev Walkinf15320b2004-06-03 03:38:44 +0000248 NEXT_PHASE(ctx);
249 /* Fall through */
250 case 1:
251 phase1:
252 /*
253 * Fill the stack with expectations.
254 */
Lev Walkin814cca72004-12-15 23:23:53 +0000255 stck = (struct _stack *)ctx->ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000256 sel = stck->cur_ptr;
257 do {
258 ber_tlv_tag_t tlv_tag;
259 ber_tlv_len_t tlv_len;
Lev Walkin188ed2c2004-09-13 08:31:01 +0000260 ber_tlv_tag_t expected_tag;
Lev Walkin5c915992004-09-27 20:54:06 +0000261 ssize_t tl, ll, tlvl;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000262 /* This one works even if (sel->left == -1) */
Simo Sorcecd943c22016-07-25 04:39:40 -0400263 size_t Left = ((!sel||(size_t)sel->left >= size)
264 ?size:(size_t)sel->left);
Lev Walkinf15320b2004-06-03 03:38:44 +0000265
Lev Walkin3990ba62004-09-24 20:57:41 +0000266
Lev Walkinabf68892004-10-26 10:12:14 +0000267 ASN_DEBUG("%p, s->l=%ld, s->wn=%ld, s->g=%ld\n", sel,
Lev Walkin1e3ccbb2004-10-26 10:56:10 +0000268 (long)(sel?sel->left:0),
269 (long)(sel?sel->want_nulls:0),
270 (long)(sel?sel->got:0)
Lev Walkin5c915992004-09-27 20:54:06 +0000271 );
272 if(sel && sel->left <= 0 && sel->want_nulls == 0) {
273 if(sel->prev) {
274 struct _stack_el *prev = sel->prev;
275 if(prev->left != -1) {
276 if(prev->left < sel->got)
277 RETURN(RC_FAIL);
278 prev->left -= sel->got;
279 }
280 prev->got += sel->got;
281 sel = stck->cur_ptr = prev;
282 if(!sel) break;
283 tlv_constr = 1;
284 continue;
285 } else {
286 sel = stck->cur_ptr = 0;
287 break; /* Nothing to wait */
288 }
289 }
290
Lev Walkin3990ba62004-09-24 20:57:41 +0000291 tl = ber_fetch_tag(buf_ptr, Left, &tlv_tag);
Lev Walkinabf68892004-10-26 10:12:14 +0000292 ASN_DEBUG("fetch tag(size=%ld,L=%ld), %sstack, left=%ld, wn=%ld, tl=%ld",
293 (long)size, (long)Left, sel?"":"!",
Lev Walkin1e3ccbb2004-10-26 10:56:10 +0000294 (long)(sel?sel->left:0),
295 (long)(sel?sel->want_nulls:0),
Lev Walkinabf68892004-10-26 10:12:14 +0000296 (long)tl);
Lev Walkinf15320b2004-06-03 03:38:44 +0000297 switch(tl) {
298 case -1: RETURN(RC_FAIL);
299 case 0: RETURN(RC_WMORE);
300 }
301
302 tlv_constr = BER_TLV_CONSTRUCTED(buf_ptr);
303
304 ll = ber_fetch_length(tlv_constr,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000305 (const char *)buf_ptr + tl,Left - tl,&tlv_len);
Lev Walkina6a926a2005-03-02 01:54:28 +0000306 ASN_DEBUG("Got tag=%s, tc=%d, left=%ld, tl=%ld, len=%ld, ll=%ld",
Lev Walkin8d127872004-09-04 04:44:50 +0000307 ber_tlv_tag_string(tlv_tag), tlv_constr,
Lev Walkina6a926a2005-03-02 01:54:28 +0000308 (long)Left, (long)tl, (long)tlv_len, (long)ll);
Lev Walkinf15320b2004-06-03 03:38:44 +0000309 switch(ll) {
310 case -1: RETURN(RC_FAIL);
311 case 0: RETURN(RC_WMORE);
312 }
313
Lev Walkin8d127872004-09-04 04:44:50 +0000314 if(sel && sel->want_nulls
Lev Walkin8c3b8542005-03-10 18:52:02 +0000315 && ((const uint8_t *)buf_ptr)[0] == 0
316 && ((const uint8_t *)buf_ptr)[1] == 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000317 {
Lev Walkin8d127872004-09-04 04:44:50 +0000318
319 ASN_DEBUG("Eat EOC; wn=%d--", sel->want_nulls);
320
Lev Walkin3a4689a2006-11-24 11:20:27 +0000321 if(type_variant == ASN_OSUBV_ANY
Lev Walkin07f388c2004-10-11 11:43:08 +0000322 && (tag_mode != 1 || sel->cont_level))
323 APPEND("\0\0", 2);
Lev Walkin5c915992004-09-27 20:54:06 +0000324
325 ADVANCE(2);
326 sel->got += 2;
327 if(sel->left != -1) {
328 sel->left -= 2; /* assert(sel->left >= 2) */
329 }
330
Lev Walkinf15320b2004-06-03 03:38:44 +0000331 sel->want_nulls--;
332 if(sel->want_nulls == 0) {
333 /* Move to the next expectation */
Lev Walkin5c915992004-09-27 20:54:06 +0000334 sel->left = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000335 tlv_constr = 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000336 }
Lev Walkin8d127872004-09-04 04:44:50 +0000337
338 continue;
Lev Walkin188ed2c2004-09-13 08:31:01 +0000339 }
340
341 /*
342 * Set up expected tags,
343 * depending on ASN.1 type being decoded.
344 */
Lev Walkinbbd93252004-10-12 05:57:23 +0000345 switch(type_variant) {
Lev Walkin3a4689a2006-11-24 11:20:27 +0000346 case ASN_OSUBV_BIT:
Lev Walkin188ed2c2004-09-13 08:31:01 +0000347 /* X.690: 8.6.4.1, NOTE 2 */
348 /* Fall through */
Lev Walkin3a4689a2006-11-24 11:20:27 +0000349 case ASN_OSUBV_STR:
Lev Walkin188ed2c2004-09-13 08:31:01 +0000350 default:
351 if(sel) {
Lev Walkin24bf3f62017-08-10 17:58:43 -0700352 unsigned level = sel->cont_level;
Lev Walkin188ed2c2004-09-13 08:31:01 +0000353 if(level < td->all_tags_count) {
354 expected_tag = td->all_tags[level];
355 break;
356 } else if(td->all_tags_count) {
357 expected_tag = td->all_tags
358 [td->all_tags_count - 1];
359 break;
360 }
361 /* else, Fall through */
362 }
363 /* Fall through */
Lev Walkin3a4689a2006-11-24 11:20:27 +0000364 case ASN_OSUBV_ANY:
Lev Walkin188ed2c2004-09-13 08:31:01 +0000365 expected_tag = tlv_tag;
366 break;
367 }
368
369
370 if(tlv_tag != expected_tag) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000371 char buf[2][32];
372 ber_tlv_tag_snprint(tlv_tag,
373 buf[0], sizeof(buf[0]));
374 ber_tlv_tag_snprint(td->tags[td->tags_count-1],
375 buf[1], sizeof(buf[1]));
376 ASN_DEBUG("Tag does not match expectation: %s != %s",
377 buf[0], buf[1]);
378 RETURN(RC_FAIL);
379 }
380
Lev Walkinde4825d2004-09-29 13:20:14 +0000381 tlvl = tl + ll; /* Combined length of T and L encoding */
382 if((tlv_len + tlvl) < 0) {
383 /* tlv_len value is too big */
384 ASN_DEBUG("TLV encoding + length (%ld) is too big",
385 (long)tlv_len);
386 RETURN(RC_FAIL);
387 }
388
Lev Walkinf15320b2004-06-03 03:38:44 +0000389 /*
390 * Append a new expectation.
391 */
Lev Walkin3990ba62004-09-24 20:57:41 +0000392 sel = OS__add_stack_el(stck);
Lev Walkin5c915992004-09-27 20:54:06 +0000393 if(!sel) RETURN(RC_FAIL);
Lev Walkinf15320b2004-06-03 03:38:44 +0000394
Lev Walkin5c915992004-09-27 20:54:06 +0000395 sel->tag = tlv_tag;
396
397 sel->want_nulls = (tlv_len==-1);
398 if(sel->prev && sel->prev->left != -1) {
399 /* Check that the parent frame is big enough */
400 if(sel->prev->left < tlvl + (tlv_len==-1?0:tlv_len))
401 RETURN(RC_FAIL);
402 if(tlv_len == -1)
403 sel->left = sel->prev->left - tlvl;
404 else
405 sel->left = tlv_len;
406 } else {
407 sel->left = tlv_len;
408 }
Lev Walkin3a4689a2006-11-24 11:20:27 +0000409 if(type_variant == ASN_OSUBV_ANY
Lev Walkin07f388c2004-10-11 11:43:08 +0000410 && (tag_mode != 1 || sel->cont_level))
411 APPEND(buf_ptr, tlvl);
Lev Walkin5c915992004-09-27 20:54:06 +0000412 sel->got += tlvl;
413 ADVANCE(tlvl);
414
Lev Walkin24bf3f62017-08-10 17:58:43 -0700415 ASN_DEBUG("+EXPECT2 got=%ld left=%ld, wn=%d, clvl=%u",
Lev Walkinabf68892004-10-26 10:12:14 +0000416 (long)sel->got, (long)sel->left,
417 sel->want_nulls, sel->cont_level);
Lev Walkin5c915992004-09-27 20:54:06 +0000418
Lev Walkinf15320b2004-06-03 03:38:44 +0000419 } while(tlv_constr);
420 if(sel == NULL) {
421 /* Finished operation, "phase out" */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000422 ASN_DEBUG("Phase out");
Lev Walkinf15320b2004-06-03 03:38:44 +0000423 _CH_PHASE(ctx, +3);
424 break;
425 }
426
427 NEXT_PHASE(ctx);
428 /* Fall through */
429 case 2:
Lev Walkin814cca72004-12-15 23:23:53 +0000430 stck = (struct _stack *)ctx->ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000431 sel = stck->cur_ptr;
Lev Walkin5c915992004-09-27 20:54:06 +0000432 ASN_DEBUG("Phase 2: Need %ld bytes, size=%ld, alrg=%ld, wn=%d",
433 (long)sel->left, (long)size, (long)sel->got,
434 sel->want_nulls);
Lev Walkinf15320b2004-06-03 03:38:44 +0000435 {
436 ber_tlv_len_t len;
437
438 assert(sel->left >= 0);
439
Lev Walkinec1ffd42004-08-18 04:53:32 +0000440 len = ((ber_tlv_len_t)size < sel->left)
441 ? (ber_tlv_len_t)size : sel->left;
Lev Walkinf15320b2004-06-03 03:38:44 +0000442 if(len > 0) {
Lev Walkin3a4689a2006-11-24 11:20:27 +0000443 if(type_variant == ASN_OSUBV_BIT
Lev Walkin188ed2c2004-09-13 08:31:01 +0000444 && sel->bits_chopped == 0) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000445 /* Put the unused-bits-octet away */
Lev Walkin8c3b8542005-03-10 18:52:02 +0000446 st->bits_unused = *(const uint8_t *)buf_ptr;
447 APPEND(((const char *)buf_ptr+1), (len - 1));
Lev Walkinf15320b2004-06-03 03:38:44 +0000448 sel->bits_chopped = 1;
449 } else {
450 APPEND(buf_ptr, len);
451 }
452 ADVANCE(len);
453 sel->left -= len;
Lev Walkin5c915992004-09-27 20:54:06 +0000454 sel->got += len;
Lev Walkinf15320b2004-06-03 03:38:44 +0000455 }
456
Lev Walkin5c915992004-09-27 20:54:06 +0000457 if(sel->left) {
458 ASN_DEBUG("OS left %ld, size = %ld, wn=%d\n",
459 (long)sel->left, (long)size, sel->want_nulls);
Lev Walkinf15320b2004-06-03 03:38:44 +0000460 RETURN(RC_WMORE);
Lev Walkinf15320b2004-06-03 03:38:44 +0000461 }
Lev Walkin5c915992004-09-27 20:54:06 +0000462
463 PREV_PHASE(ctx);
464 goto phase1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000465 }
466 break;
467 case 3:
468 phase3:
469 /*
470 * Primitive form, no stack required.
471 */
Lev Walkinbbd93252004-10-12 05:57:23 +0000472 assert(ctx->left >= 0);
473
Lev Walkind9bd7752004-06-05 08:17:50 +0000474 if(size < (size_t)ctx->left) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000475 if(!size) RETURN(RC_WMORE);
Lev Walkin3a4689a2006-11-24 11:20:27 +0000476 if(type_variant == ASN_OSUBV_BIT && !ctx->context) {
Lev Walkin8c3b8542005-03-10 18:52:02 +0000477 st->bits_unused = *(const uint8_t *)buf_ptr;
Lev Walkinbbd93252004-10-12 05:57:23 +0000478 ctx->left--;
479 ADVANCE(1);
480 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000481 APPEND(buf_ptr, size);
Lev Walkin75b1bef2005-04-25 19:38:21 +0000482 assert(ctx->context > 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000483 ctx->left -= size;
484 ADVANCE(size);
485 RETURN(RC_WMORE);
486 } else {
Lev Walkin3a4689a2006-11-24 11:20:27 +0000487 if(type_variant == ASN_OSUBV_BIT
Lev Walkin75b1bef2005-04-25 19:38:21 +0000488 && !ctx->context && ctx->left) {
Lev Walkin8c3b8542005-03-10 18:52:02 +0000489 st->bits_unused = *(const uint8_t *)buf_ptr;
Lev Walkinbbd93252004-10-12 05:57:23 +0000490 ctx->left--;
491 ADVANCE(1);
492 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000493 APPEND(buf_ptr, ctx->left);
494 ADVANCE(ctx->left);
495 ctx->left = 0;
496
497 NEXT_PHASE(ctx);
498 }
499 break;
500 }
501
Lev Walkin5c915992004-09-27 20:54:06 +0000502 if(sel) {
503 ASN_DEBUG("3sel p=%p, wn=%d, l=%ld, g=%ld, size=%ld",
504 sel->prev, sel->want_nulls,
505 (long)sel->left, (long)sel->got, (long)size);
506 if(sel->prev || sel->want_nulls > 1 || sel->left > 0) {
507 RETURN(RC_WMORE);
508 }
509 }
510
Lev Walkinf15320b2004-06-03 03:38:44 +0000511 /*
512 * BIT STRING-specific processing.
513 */
Lev Walkin3a4689a2006-11-24 11:20:27 +0000514 if(type_variant == ASN_OSUBV_BIT && st->size) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000515 /* Finalize BIT STRING: zero out unused bits. */
Lev Walkinbbd93252004-10-12 05:57:23 +0000516 st->buf[st->size-1] &= 0xff << st->bits_unused;
Lev Walkinf15320b2004-06-03 03:38:44 +0000517 }
518
Lev Walkinabf68892004-10-26 10:12:14 +0000519 ASN_DEBUG("Took %ld bytes to encode %s: [%s]:%ld",
520 (long)consumed_myself, td->name,
Lev Walkin3a4689a2006-11-24 11:20:27 +0000521 (type_variant == ASN_OSUBV_STR) ? (char *)st->buf : "<data>",
Lev Walkinabf68892004-10-26 10:12:14 +0000522 (long)st->size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000523
Lev Walkinf15320b2004-06-03 03:38:44 +0000524
Lev Walkin5c915992004-09-27 20:54:06 +0000525 RETURN(RC_OK);
Lev Walkinf15320b2004-06-03 03:38:44 +0000526}
527
528/*
529 * Encode OCTET STRING type using DER.
530 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000531asn_enc_rval_t
Lev Walkinbbd93252004-10-12 05:57:23 +0000532OCTET_STRING_encode_der(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000533 int tag_mode, ber_tlv_tag_t tag,
534 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000535 asn_enc_rval_t er;
536 asn_OCTET_STRING_specifics_t *specs = td->specifics
Lev Walkindc06f6b2004-10-20 15:50:55 +0000537 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +0800538 : &asn_SPC_OCTET_STRING_specs;
Lev Walkinbbd93252004-10-12 05:57:23 +0000539 BIT_STRING_t *st = (BIT_STRING_t *)sptr;
Lev Walkin3a4689a2006-11-24 11:20:27 +0000540 enum asn_OS_Subvariant type_variant = specs->subvariant;
Lev Walkinbbd93252004-10-12 05:57:23 +0000541 int fix_last_byte = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000542
543 ASN_DEBUG("%s %s as OCTET STRING",
Lev Walkin1f670c12004-09-02 12:57:25 +0000544 cb?"Estimating":"Encoding", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000545
546 /*
Lev Walkinbbd93252004-10-12 05:57:23 +0000547 * Write tags.
Lev Walkinf15320b2004-06-03 03:38:44 +0000548 */
Lev Walkin3a4689a2006-11-24 11:20:27 +0000549 if(type_variant != ASN_OSUBV_ANY || tag_mode == 1) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000550 er.encoded = der_write_tags(td,
Lev Walkin3a4689a2006-11-24 11:20:27 +0000551 (type_variant == ASN_OSUBV_BIT) + st->size,
552 tag_mode, type_variant == ASN_OSUBV_ANY, tag,
553 cb, app_key);
Lev Walkinbbd93252004-10-12 05:57:23 +0000554 if(er.encoded == -1) {
555 er.failed_type = td;
556 er.structure_ptr = sptr;
557 return er;
Lev Walkinf15320b2004-06-03 03:38:44 +0000558 }
Lev Walkinbbd93252004-10-12 05:57:23 +0000559 } else {
560 /* Disallow: [<tag>] IMPLICIT ANY */
Lev Walkin3a4689a2006-11-24 11:20:27 +0000561 assert(type_variant != ASN_OSUBV_ANY || tag_mode != -1);
Lev Walkinbbd93252004-10-12 05:57:23 +0000562 er.encoded = 0;
563 }
564
565 if(!cb) {
Lev Walkin3a4689a2006-11-24 11:20:27 +0000566 er.encoded += (type_variant == ASN_OSUBV_BIT) + st->size;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700567 ASN__ENCODED_OK(er);
Lev Walkinf15320b2004-06-03 03:38:44 +0000568 }
569
Lev Walkin07f388c2004-10-11 11:43:08 +0000570 /*
Lev Walkinbbd93252004-10-12 05:57:23 +0000571 * Prepare to deal with the last octet of BIT STRING.
Lev Walkin07f388c2004-10-11 11:43:08 +0000572 */
Lev Walkin3a4689a2006-11-24 11:20:27 +0000573 if(type_variant == ASN_OSUBV_BIT) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000574 uint8_t b = st->bits_unused & 0x07;
575 if(b && st->size) fix_last_byte = 1;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700576 ASN__CALLBACK(&b, 1);
Lev Walkinbbd93252004-10-12 05:57:23 +0000577 er.encoded++;
Lev Walkinf15320b2004-06-03 03:38:44 +0000578 }
579
Lev Walkinbbd93252004-10-12 05:57:23 +0000580 /* Invoke callback for the main part of the buffer */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700581 ASN__CALLBACK(st->buf, st->size - fix_last_byte);
Lev Walkinf15320b2004-06-03 03:38:44 +0000582
Lev Walkinbbd93252004-10-12 05:57:23 +0000583 /* The last octet should be stripped off the unused bits */
584 if(fix_last_byte) {
585 uint8_t b = st->buf[st->size-1] & (0xff << st->bits_unused);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700586 ASN__CALLBACK(&b, 1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000587 }
588
Lev Walkinbbd93252004-10-12 05:57:23 +0000589 er.encoded += st->size;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700590 ASN__ENCODED_OK(er);
Lev Walkinbbd93252004-10-12 05:57:23 +0000591cb_failed:
Lev Walkin7c1dc052016-03-14 03:08:15 -0700592 ASN__ENCODE_FAILED;
Lev Walkinf15320b2004-06-03 03:38:44 +0000593}
594
Lev Walkina9cc46e2004-09-22 16:06:28 +0000595asn_enc_rval_t
Lev Walkinde4825d2004-09-29 13:20:14 +0000596OCTET_STRING_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000597 int ilevel, enum xer_encoder_flags_e flags,
598 asn_app_consume_bytes_f *cb, void *app_key) {
Wim Lewis59e8d282014-08-04 12:39:35 -0700599 const char * const h2c = "0123456789ABCDEF";
Lev Walkina9cc46e2004-09-22 16:06:28 +0000600 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
601 asn_enc_rval_t er;
602 char scratch[16 * 3 + 4];
603 char *p = scratch;
604 uint8_t *buf;
605 uint8_t *end;
606 size_t i;
607
Lev Walkin2ddd3082006-10-16 12:32:14 +0000608 if(!st || (!st->buf && st->size))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700609 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000610
611 er.encoded = 0;
612
613 /*
614 * Dump the contents of the buffer in hexadecimal.
615 */
616 buf = st->buf;
617 end = buf + st->size;
618 if(flags & XER_F_CANONICAL) {
619 char *scend = scratch + (sizeof(scratch) - 2);
620 for(; buf < end; buf++) {
621 if(p >= scend) {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700622 ASN__CALLBACK(scratch, p - scratch);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000623 er.encoded += p - scratch;
624 p = scratch;
625 }
626 *p++ = h2c[(*buf >> 4) & 0x0F];
627 *p++ = h2c[*buf & 0x0F];
628 }
Lev Walkincc6a9102004-09-23 22:06:26 +0000629
Lev Walkin7c1dc052016-03-14 03:08:15 -0700630 ASN__CALLBACK(scratch, p-scratch); /* Dump the rest */
Lev Walkincc6a9102004-09-23 22:06:26 +0000631 er.encoded += p - scratch;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000632 } else {
633 for(i = 0; buf < end; buf++, i++) {
634 if(!(i % 16) && (i || st->size > 16)) {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700635 ASN__CALLBACK(scratch, p-scratch);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000636 er.encoded += (p-scratch);
637 p = scratch;
Lev Walkinc6cac8e2016-03-14 02:57:07 -0700638 ASN__TEXT_INDENT(1, ilevel);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000639 }
640 *p++ = h2c[(*buf >> 4) & 0x0F];
641 *p++ = h2c[*buf & 0x0F];
642 *p++ = 0x20;
643 }
Lev Walkincc6a9102004-09-23 22:06:26 +0000644 if(p - scratch) {
645 p--; /* Remove the tail space */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700646 ASN__CALLBACK(scratch, p-scratch); /* Dump the rest */
Lev Walkincc6a9102004-09-23 22:06:26 +0000647 er.encoded += p - scratch;
648 if(st->size > 16)
Lev Walkinc6cac8e2016-03-14 02:57:07 -0700649 ASN__TEXT_INDENT(1, ilevel-1);
Lev Walkincc6a9102004-09-23 22:06:26 +0000650 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000651 }
652
Lev Walkin7c1dc052016-03-14 03:08:15 -0700653 ASN__ENCODED_OK(er);
Lev Walkin942fd082004-10-03 09:13:02 +0000654cb_failed:
Lev Walkin7c1dc052016-03-14 03:08:15 -0700655 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000656}
657
Wim Lewis18c2ec92014-07-29 11:30:10 -0700658static const struct OCTET_STRING__xer_escape_table_s {
659 const char *string;
Lev Walkin9ca81892004-10-03 10:54:25 +0000660 int size;
661} OCTET_STRING__xer_escape_table[] = {
662#define OSXET(s) { s, sizeof(s) - 1 }
663 OSXET("\074\156\165\154\057\076"), /* <nul/> */
664 OSXET("\074\163\157\150\057\076"), /* <soh/> */
665 OSXET("\074\163\164\170\057\076"), /* <stx/> */
666 OSXET("\074\145\164\170\057\076"), /* <etx/> */
667 OSXET("\074\145\157\164\057\076"), /* <eot/> */
668 OSXET("\074\145\156\161\057\076"), /* <enq/> */
669 OSXET("\074\141\143\153\057\076"), /* <ack/> */
670 OSXET("\074\142\145\154\057\076"), /* <bel/> */
671 OSXET("\074\142\163\057\076"), /* <bs/> */
672 OSXET("\011"), /* \t */
673 OSXET("\012"), /* \n */
674 OSXET("\074\166\164\057\076"), /* <vt/> */
675 OSXET("\074\146\146\057\076"), /* <ff/> */
676 OSXET("\015"), /* \r */
677 OSXET("\074\163\157\057\076"), /* <so/> */
678 OSXET("\074\163\151\057\076"), /* <si/> */
679 OSXET("\074\144\154\145\057\076"), /* <dle/> */
680 OSXET("\074\144\143\061\057\076"), /* <de1/> */
681 OSXET("\074\144\143\062\057\076"), /* <de2/> */
682 OSXET("\074\144\143\063\057\076"), /* <de3/> */
683 OSXET("\074\144\143\064\057\076"), /* <de4/> */
684 OSXET("\074\156\141\153\057\076"), /* <nak/> */
685 OSXET("\074\163\171\156\057\076"), /* <syn/> */
686 OSXET("\074\145\164\142\057\076"), /* <etb/> */
687 OSXET("\074\143\141\156\057\076"), /* <can/> */
688 OSXET("\074\145\155\057\076"), /* <em/> */
689 OSXET("\074\163\165\142\057\076"), /* <sub/> */
690 OSXET("\074\145\163\143\057\076"), /* <esc/> */
691 OSXET("\074\151\163\064\057\076"), /* <is4/> */
692 OSXET("\074\151\163\063\057\076"), /* <is3/> */
693 OSXET("\074\151\163\062\057\076"), /* <is2/> */
694 OSXET("\074\151\163\061\057\076"), /* <is1/> */
695 { 0, 0 }, /* " " */
696 { 0, 0 }, /* ! */
697 { 0, 0 }, /* \" */
698 { 0, 0 }, /* # */
699 { 0, 0 }, /* $ */
700 { 0, 0 }, /* % */
701 OSXET("\046\141\155\160\073"), /* &amp; */
702 { 0, 0 }, /* ' */
703 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, /* ()*+,-./ */
704 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, /* 01234567 */
705 {0,0},{0,0},{0,0},{0,0}, /* 89:; */
706 OSXET("\046\154\164\073"), /* &lt; */
707 { 0, 0 }, /* = */
708 OSXET("\046\147\164\073"), /* &gt; */
709};
710
Lev Walkindc06f6b2004-10-20 15:50:55 +0000711static int
Lev Walkin0fab1a62005-03-09 22:19:25 +0000712OS__check_escaped_control_char(const void *buf, int size) {
Lev Walkindc06f6b2004-10-20 15:50:55 +0000713 size_t i;
714 /*
715 * Inefficient algorithm which translates the escape sequences
716 * defined above into characters. Returns -1 if not found.
717 * TODO: replace by a faster algorithm (bsearch(), hash or
718 * nested table lookups).
719 */
720 for(i = 0; i < 32 /* Don't spend time on the bottom half */; i++) {
Wim Lewis18c2ec92014-07-29 11:30:10 -0700721 const struct OCTET_STRING__xer_escape_table_s *el;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000722 el = &OCTET_STRING__xer_escape_table[i];
723 if(el->size == size && memcmp(buf, el->string, size) == 0)
724 return i;
725 }
726 return -1;
727}
728
729static int
Lev Walkin0fab1a62005-03-09 22:19:25 +0000730OCTET_STRING__handle_control_chars(void *struct_ptr, const void *chunk_buf, size_t chunk_size) {
Lev Walkindc06f6b2004-10-20 15:50:55 +0000731 /*
732 * This might be one of the escape sequences
733 * for control characters. Check it out.
734 * #11.15.5
735 */
736 int control_char = OS__check_escaped_control_char(chunk_buf,chunk_size);
737 if(control_char >= 0) {
738 OCTET_STRING_t *st = (OCTET_STRING_t *)struct_ptr;
739 void *p = REALLOC(st->buf, st->size + 2);
740 if(p) {
741 st->buf = (uint8_t *)p;
742 st->buf[st->size++] = control_char;
743 st->buf[st->size] = '\0'; /* nul-termination */
744 return 0;
745 }
746 }
747
748 return -1; /* No, it's not */
749}
750
Lev Walkina9cc46e2004-09-22 16:06:28 +0000751asn_enc_rval_t
Lev Walkindc06f6b2004-10-20 15:50:55 +0000752OCTET_STRING_encode_xer_utf8(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000753 int ilevel, enum xer_encoder_flags_e flags,
754 asn_app_consume_bytes_f *cb, void *app_key) {
755 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
756 asn_enc_rval_t er;
Lev Walkin9ca81892004-10-03 10:54:25 +0000757 uint8_t *buf, *end;
758 uint8_t *ss; /* Sequence start */
759 ssize_t encoded_len = 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000760
761 (void)ilevel; /* Unused argument */
762 (void)flags; /* Unused argument */
763
Lev Walkin2ddd3082006-10-16 12:32:14 +0000764 if(!st || (!st->buf && st->size))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700765 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000766
Lev Walkin9ca81892004-10-03 10:54:25 +0000767 buf = st->buf;
768 end = buf + st->size;
769 for(ss = buf; buf < end; buf++) {
Lev Walkin443d2512004-10-05 06:35:31 +0000770 unsigned int ch = *buf;
Lev Walkin9ca81892004-10-03 10:54:25 +0000771 int s_len; /* Special encoding sequence length */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000772
Lev Walkin9ca81892004-10-03 10:54:25 +0000773 /*
774 * Escape certain characters: X.680/11.15
775 */
776 if(ch < sizeof(OCTET_STRING__xer_escape_table)
777 /sizeof(OCTET_STRING__xer_escape_table[0])
778 && (s_len = OCTET_STRING__xer_escape_table[ch].size)) {
779 if(((buf - ss) && cb(ss, buf - ss, app_key) < 0)
780 || cb(OCTET_STRING__xer_escape_table[ch].string, s_len,
781 app_key) < 0)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700782 ASN__ENCODE_FAILED;
Lev Walkin9ca81892004-10-03 10:54:25 +0000783 encoded_len += (buf - ss) + s_len;
784 ss = buf + 1;
785 }
786 }
787
788 encoded_len += (buf - ss);
789 if((buf - ss) && cb(ss, buf - ss, app_key) < 0)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700790 ASN__ENCODE_FAILED;
Lev Walkin9ca81892004-10-03 10:54:25 +0000791
792 er.encoded = encoded_len;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700793 ASN__ENCODED_OK(er);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000794}
795
Lev Walkindc06f6b2004-10-20 15:50:55 +0000796/*
797 * Convert from hexadecimal format (cstring): "AB CD EF"
798 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000799static 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 +0000800 OCTET_STRING_t *st = (OCTET_STRING_t *)sptr;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000801 const char *chunk_stop = (const char *)chunk_buf;
802 const char *p = chunk_stop;
803 const char *pend = p + chunk_size;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000804 unsigned int clv = 0;
805 int half = 0; /* Half bit */
806 uint8_t *buf;
807
808 /* Reallocate buffer according to high cap estimation */
Lev Walkin494fb702017-08-07 20:07:00 -0700809 size_t new_size = st->size + (chunk_size + 1) / 2;
810 void *nptr = REALLOC(st->buf, new_size + 1);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000811 if(!nptr) return -1;
812 st->buf = (uint8_t *)nptr;
813 buf = st->buf + st->size;
814
815 /*
816 * If something like " a b c " appears here, the " a b":3 will be
817 * converted, and the rest skipped. That is, unless buf_size is greater
818 * than chunk_size, then it'll be equivalent to "ABC0".
819 */
820 for(; p < pend; p++) {
Lev Walkin0fab1a62005-03-09 22:19:25 +0000821 int ch = *(const unsigned char *)p;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000822 switch(ch) {
823 case 0x09: case 0x0a: case 0x0c: case 0x0d:
824 case 0x20:
825 /* Ignore whitespace */
826 continue;
827 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: /*01234*/
828 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: /*56789*/
829 clv = (clv << 4) + (ch - 0x30);
830 break;
831 case 0x41: case 0x42: case 0x43: /* ABC */
832 case 0x44: case 0x45: case 0x46: /* DEF */
Lev Walkin3ae21bd2005-02-21 14:43:48 +0000833 clv = (clv << 4) + (ch - 0x41 + 10);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000834 break;
835 case 0x61: case 0x62: case 0x63: /* abc */
836 case 0x64: case 0x65: case 0x66: /* def */
Lev Walkin3ae21bd2005-02-21 14:43:48 +0000837 clv = (clv << 4) + (ch - 0x61 + 10);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000838 break;
839 default:
840 *buf = 0; /* JIC */
841 return -1;
842 }
843 if(half++) {
844 half = 0;
845 *buf++ = clv;
846 chunk_stop = p + 1;
847 }
848 }
849
850 /*
851 * Check partial decoding.
852 */
853 if(half) {
854 if(have_more) {
855 /*
856 * Partial specification is fine,
857 * because no more more PXER_TEXT data is available.
858 */
859 *buf++ = clv << 4;
860 chunk_stop = p;
861 }
862 } else {
863 chunk_stop = p;
864 }
865
866 st->size = buf - st->buf; /* Adjust the buffer size */
Lev Walkin494fb702017-08-07 20:07:00 -0700867 assert(st->size <= new_size);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000868 st->buf[st->size] = 0; /* Courtesy termination */
869
Lev Walkin0fab1a62005-03-09 22:19:25 +0000870 return (chunk_stop - (const char *)chunk_buf); /* Converted size */
Lev Walkindc06f6b2004-10-20 15:50:55 +0000871}
872
873/*
874 * Convert from binary format: "00101011101"
875 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000876static 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 +0000877 BIT_STRING_t *st = (BIT_STRING_t *)sptr;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000878 const char *p = (const char *)chunk_buf;
879 const char *pend = p + chunk_size;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000880 int bits_unused = st->bits_unused & 0x7;
881 uint8_t *buf;
882
883 /* Reallocate buffer according to high cap estimation */
Lev Walkin494fb702017-08-07 20:07:00 -0700884 size_t new_size = st->size + (chunk_size + 7) / 8;
885 void *nptr = REALLOC(st->buf, new_size + 1);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000886 if(!nptr) return -1;
887 st->buf = (uint8_t *)nptr;
888 buf = st->buf + st->size;
889
890 (void)have_more;
891
892 if(bits_unused == 0)
893 bits_unused = 8;
894 else if(st->size)
895 buf--;
896
897 /*
898 * Convert series of 0 and 1 into the octet string.
899 */
900 for(; p < pend; p++) {
Lev Walkin0fab1a62005-03-09 22:19:25 +0000901 int ch = *(const unsigned char *)p;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000902 switch(ch) {
903 case 0x09: case 0x0a: case 0x0c: case 0x0d:
904 case 0x20:
905 /* Ignore whitespace */
906 break;
907 case 0x30:
908 case 0x31:
909 if(bits_unused-- <= 0) {
910 *++buf = 0; /* Clean the cell */
911 bits_unused = 7;
912 }
913 *buf |= (ch&1) << bits_unused;
914 break;
915 default:
916 st->bits_unused = bits_unused;
917 return -1;
918 }
919 }
920
921 if(bits_unused == 8) {
922 st->size = buf - st->buf;
923 st->bits_unused = 0;
924 } else {
925 st->size = buf - st->buf + 1;
926 st->bits_unused = bits_unused;
927 }
928
Lev Walkin494fb702017-08-07 20:07:00 -0700929 assert(st->size <= new_size);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000930 st->buf[st->size] = 0; /* Courtesy termination */
931
932 return chunk_size; /* Converted in full */
933}
934
935/*
936 * Something like strtod(), but with stricter rules.
937 */
938static int
Lev Walkin0fab1a62005-03-09 22:19:25 +0000939OS__strtoent(int base, const char *buf, const char *end, int32_t *ret_value) {
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
946 /* Strange huge value */
947 if((val * base + base) < 0)
948 return -1;
949
Lev Walkindc06f6b2004-10-20 15:50:55 +0000950 switch(ch) {
951 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: /*01234*/
952 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: /*56789*/
953 val = val * base + (ch - 0x30);
954 break;
955 case 0x41: case 0x42: case 0x43: /* ABC */
956 case 0x44: case 0x45: case 0x46: /* DEF */
Lev Walkin3ae21bd2005-02-21 14:43:48 +0000957 val = val * base + (ch - 0x41 + 10);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000958 break;
959 case 0x61: case 0x62: case 0x63: /* abc */
960 case 0x64: case 0x65: case 0x66: /* def */
Lev Walkin3ae21bd2005-02-21 14:43:48 +0000961 val = val * base + (ch - 0x61 + 10);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000962 break;
963 case 0x3b: /* ';' */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000964 *ret_value = val;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000965 return (p - buf) + 1;
966 default:
967 return -1; /* Character set error */
968 }
969 }
970
Lev Walkinb0f3db62005-07-03 05:30:15 +0000971 *ret_value = -1;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000972 return (p - buf);
973}
974
975/*
976 * Convert from the plain UTF-8 format, expanding entity references: "2 &lt; 3"
977 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000978static ssize_t OCTET_STRING__convert_entrefs(void *sptr, const void *chunk_buf, size_t chunk_size, int have_more) {
Lev Walkindc06f6b2004-10-20 15:50:55 +0000979 OCTET_STRING_t *st = (OCTET_STRING_t *)sptr;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000980 const char *p = (const char *)chunk_buf;
981 const char *pend = p + chunk_size;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000982 uint8_t *buf;
983
984 /* Reallocate buffer */
Lev Walkin494fb702017-08-07 20:07:00 -0700985 size_t new_size = st->size + chunk_size;
986 void *nptr = REALLOC(st->buf, new_size + 1);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000987 if(!nptr) return -1;
988 st->buf = (uint8_t *)nptr;
989 buf = st->buf + st->size;
990
991 /*
992 * Convert series of 0 and 1 into the octet string.
993 */
994 for(; p < pend; p++) {
Lev Walkin0fab1a62005-03-09 22:19:25 +0000995 int ch = *(const unsigned char *)p;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000996 int len; /* Length of the rest of the chunk */
997
998 if(ch != 0x26 /* '&' */) {
999 *buf++ = ch;
1000 continue; /* That was easy... */
1001 }
1002
1003 /*
1004 * Process entity reference.
1005 */
Lev Walkin0fab1a62005-03-09 22:19:25 +00001006 len = chunk_size - (p - (const char *)chunk_buf);
Lev Walkindc06f6b2004-10-20 15:50:55 +00001007 if(len == 1 /* "&" */) goto want_more;
1008 if(p[1] == 0x23 /* '#' */) {
Lev Walkin0fab1a62005-03-09 22:19:25 +00001009 const char *pval; /* Pointer to start of digits */
Lev Walkinbc0dce32006-07-27 09:56:13 +00001010 int32_t val = 0; /* Entity reference value */
Lev Walkindc06f6b2004-10-20 15:50:55 +00001011 int base;
1012
1013 if(len == 2 /* "&#" */) goto want_more;
1014 if(p[2] == 0x78 /* 'x' */)
1015 pval = p + 3, base = 16;
1016 else
1017 pval = p + 2, base = 10;
1018 len = OS__strtoent(base, pval, p + len, &val);
1019 if(len == -1) {
1020 /* Invalid charset. Just copy verbatim. */
1021 *buf++ = ch;
1022 continue;
1023 }
1024 if(!len || pval[len-1] != 0x3b) goto want_more;
1025 assert(val > 0);
1026 p += (pval - p) + len - 1; /* Advance past entref */
1027
1028 if(val < 0x80) {
1029 *buf++ = (char)val;
1030 } else if(val < 0x800) {
1031 *buf++ = 0xc0 | ((val >> 6));
1032 *buf++ = 0x80 | ((val & 0x3f));
1033 } else if(val < 0x10000) {
1034 *buf++ = 0xe0 | ((val >> 12));
1035 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1036 *buf++ = 0x80 | ((val & 0x3f));
1037 } else if(val < 0x200000) {
1038 *buf++ = 0xf0 | ((val >> 18));
1039 *buf++ = 0x80 | ((val >> 12) & 0x3f);
1040 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1041 *buf++ = 0x80 | ((val & 0x3f));
1042 } else if(val < 0x4000000) {
1043 *buf++ = 0xf8 | ((val >> 24));
1044 *buf++ = 0x80 | ((val >> 18) & 0x3f);
1045 *buf++ = 0x80 | ((val >> 12) & 0x3f);
1046 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1047 *buf++ = 0x80 | ((val & 0x3f));
1048 } else {
1049 *buf++ = 0xfc | ((val >> 30) & 0x1);
1050 *buf++ = 0x80 | ((val >> 24) & 0x3f);
1051 *buf++ = 0x80 | ((val >> 18) & 0x3f);
1052 *buf++ = 0x80 | ((val >> 12) & 0x3f);
1053 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1054 *buf++ = 0x80 | ((val & 0x3f));
1055 }
1056 } else {
1057 /*
1058 * Ugly, limited parsing of &amp; &gt; &lt;
1059 */
1060 char *sc = (char *)memchr(p, 0x3b, len > 5 ? 5 : len);
1061 if(!sc) goto want_more;
1062 if((sc - p) == 4
1063 && p[1] == 0x61 /* 'a' */
1064 && p[2] == 0x6d /* 'm' */
1065 && p[3] == 0x70 /* 'p' */) {
1066 *buf++ = 0x26;
1067 p = sc;
1068 continue;
1069 }
1070 if((sc - p) == 3) {
1071 if(p[1] == 0x6c) {
1072 *buf = 0x3c; /* '<' */
1073 } else if(p[1] == 0x67) {
1074 *buf = 0x3e; /* '>' */
1075 } else {
1076 /* Unsupported entity reference */
1077 *buf++ = ch;
1078 continue;
1079 }
1080 if(p[2] != 0x74) {
1081 /* Unsupported entity reference */
1082 *buf++ = ch;
1083 continue;
1084 }
1085 buf++;
1086 p = sc;
1087 continue;
1088 }
1089 /* Unsupported entity reference */
1090 *buf++ = ch;
1091 }
1092
1093 continue;
1094 want_more:
1095 if(have_more) {
1096 /*
1097 * We know that no more data (of the same type)
1098 * is coming. Copy the rest verbatim.
1099 */
1100 *buf++ = ch;
1101 continue;
1102 }
Lev Walkin0fab1a62005-03-09 22:19:25 +00001103 chunk_size = (p - (const char *)chunk_buf);
Lev Walkindc06f6b2004-10-20 15:50:55 +00001104 /* Processing stalled: need more data */
Lev Walkinbdaae772005-02-18 12:25:47 +00001105 break;
Lev Walkindc06f6b2004-10-20 15:50:55 +00001106 }
1107
1108 st->size = buf - st->buf;
Lev Walkin494fb702017-08-07 20:07:00 -07001109 assert(st->size <= new_size);
Lev Walkindc06f6b2004-10-20 15:50:55 +00001110 st->buf[st->size] = 0; /* Courtesy termination */
1111
1112 return chunk_size; /* Converted in full */
1113}
1114
1115/*
1116 * Decode OCTET STRING from the XML element's body.
1117 */
1118static asn_dec_rval_t
1119OCTET_STRING__decode_xer(asn_codec_ctx_t *opt_codec_ctx,
1120 asn_TYPE_descriptor_t *td, void **sptr,
Lev Walkin8c3b8542005-03-10 18:52:02 +00001121 const char *opt_mname, const void *buf_ptr, size_t size,
Lev Walkindc06f6b2004-10-20 15:50:55 +00001122 int (*opt_unexpected_tag_decoder)
Lev Walkin0fab1a62005-03-09 22:19:25 +00001123 (void *struct_ptr, const void *chunk_buf, size_t chunk_size),
Lev Walkindc06f6b2004-10-20 15:50:55 +00001124 ssize_t (*body_receiver)
Lev Walkin0fab1a62005-03-09 22:19:25 +00001125 (void *struct_ptr, const void *chunk_buf, size_t chunk_size,
Lev Walkindc06f6b2004-10-20 15:50:55 +00001126 int have_more)
1127) {
Lev Walkind5125642005-02-14 20:08:00 +00001128 OCTET_STRING_t *st = (OCTET_STRING_t *)*sptr;
Lev Walkindc06f6b2004-10-20 15:50:55 +00001129 asn_OCTET_STRING_specifics_t *specs = td->specifics
1130 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +08001131 : &asn_SPC_OCTET_STRING_specs;
Lev Walkindc06f6b2004-10-20 15:50:55 +00001132 const char *xml_tag = opt_mname ? opt_mname : td->xml_tag;
1133 asn_struct_ctx_t *ctx; /* Per-structure parser context */
Lev Walkinc61f3862005-02-14 17:21:22 +00001134 asn_dec_rval_t rval; /* Return value from the decoder */
1135 int st_allocated;
Lev Walkindc06f6b2004-10-20 15:50:55 +00001136
1137 /*
1138 * Create the string if does not exist.
1139 */
Lev Walkinc61f3862005-02-14 17:21:22 +00001140 if(!st) {
Lev Walkin8484ed82004-12-14 13:31:01 +00001141 st = (OCTET_STRING_t *)CALLOC(1, specs->struct_size);
Lev Walkin8484ed82004-12-14 13:31:01 +00001142 *sptr = (void *)st;
Lev Walkinc61f3862005-02-14 17:21:22 +00001143 if(!st) goto sta_failed;
1144 st_allocated = 1;
Lev Walkinbdaae772005-02-18 12:25:47 +00001145 } else {
1146 st_allocated = 0;
1147 }
Lev Walkinc61f3862005-02-14 17:21:22 +00001148 if(!st->buf) {
1149 /* This is separate from above section */
1150 st->buf = (uint8_t *)CALLOC(1, 1);
1151 if(!st->buf) {
1152 if(st_allocated) {
1153 *sptr = 0;
1154 goto stb_failed;
1155 } else {
1156 goto sta_failed;
1157 }
Lev Walkindc06f6b2004-10-20 15:50:55 +00001158 }
1159 }
1160
1161 /* Restore parsing context */
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001162 ctx = (asn_struct_ctx_t *)(((char *)*sptr) + specs->ctx_offset);
Lev Walkindc06f6b2004-10-20 15:50:55 +00001163
1164 return xer_decode_general(opt_codec_ctx, ctx, *sptr, xml_tag,
1165 buf_ptr, size, opt_unexpected_tag_decoder, body_receiver);
Lev Walkinc61f3862005-02-14 17:21:22 +00001166
1167stb_failed:
1168 FREEMEM(st);
1169sta_failed:
1170 rval.code = RC_FAIL;
1171 rval.consumed = 0;
1172 return rval;
Lev Walkindc06f6b2004-10-20 15:50:55 +00001173}
1174
1175/*
1176 * Decode OCTET STRING from the hexadecimal data.
1177 */
1178asn_dec_rval_t
1179OCTET_STRING_decode_xer_hex(asn_codec_ctx_t *opt_codec_ctx,
1180 asn_TYPE_descriptor_t *td, void **sptr,
Lev Walkin8c3b8542005-03-10 18:52:02 +00001181 const char *opt_mname, const void *buf_ptr, size_t size) {
Lev Walkindc06f6b2004-10-20 15:50:55 +00001182 return OCTET_STRING__decode_xer(opt_codec_ctx, td, sptr, opt_mname,
1183 buf_ptr, size, 0, OCTET_STRING__convert_hexadecimal);
1184}
1185
1186/*
1187 * Decode OCTET STRING from the binary (0/1) data.
1188 */
1189asn_dec_rval_t
1190OCTET_STRING_decode_xer_binary(asn_codec_ctx_t *opt_codec_ctx,
1191 asn_TYPE_descriptor_t *td, void **sptr,
Lev Walkin8c3b8542005-03-10 18:52:02 +00001192 const char *opt_mname, const void *buf_ptr, size_t size) {
Lev Walkindc06f6b2004-10-20 15:50:55 +00001193 return OCTET_STRING__decode_xer(opt_codec_ctx, td, sptr, opt_mname,
1194 buf_ptr, size, 0, OCTET_STRING__convert_binary);
1195}
1196
1197/*
1198 * Decode OCTET STRING from the string (ASCII/UTF-8) data.
1199 */
1200asn_dec_rval_t
1201OCTET_STRING_decode_xer_utf8(asn_codec_ctx_t *opt_codec_ctx,
1202 asn_TYPE_descriptor_t *td, void **sptr,
Lev Walkin8c3b8542005-03-10 18:52:02 +00001203 const char *opt_mname, const void *buf_ptr, size_t size) {
Lev Walkindc06f6b2004-10-20 15:50:55 +00001204 return OCTET_STRING__decode_xer(opt_codec_ctx, td, sptr, opt_mname,
1205 buf_ptr, size,
1206 OCTET_STRING__handle_control_chars,
1207 OCTET_STRING__convert_entrefs);
1208}
1209
Lev Walkin725883b2006-10-09 12:07:58 +00001210static int
Lev Walkin3a4689a2006-11-24 11:20:27 +00001211OCTET_STRING_per_get_characters(asn_per_data_t *po, uint8_t *buf,
1212 size_t units, unsigned int bpc, unsigned int unit_bits,
Lev Walkin494fb702017-08-07 20:07:00 -07001213 long lb, long ub, const asn_per_constraints_t *pc) {
Lev Walkin3a4689a2006-11-24 11:20:27 +00001214 uint8_t *end = buf + units * bpc;
Lev Walkin725883b2006-10-09 12:07:58 +00001215
1216 ASN_DEBUG("Expanding %d characters into (%ld..%ld):%d",
Lev Walkin3a4689a2006-11-24 11:20:27 +00001217 (int)units, lb, ub, unit_bits);
Lev Walkin725883b2006-10-09 12:07:58 +00001218
1219 /* X.691: 27.5.4 */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001220 if((unsigned long)ub <= ((unsigned long)2 << (unit_bits - 1))) {
Lev Walkin725883b2006-10-09 12:07:58 +00001221 /* Decode without translation */
1222 lb = 0;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001223 } else if(pc && pc->code2value) {
1224 if(unit_bits > 16)
1225 return 1; /* FATAL: can't have constrained
1226 * UniversalString with more than
1227 * 16 million code points */
1228 for(; buf < end; buf += bpc) {
Lev Walkin725883b2006-10-09 12:07:58 +00001229 int value;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001230 int code = per_get_few_bits(po, unit_bits);
Lev Walkin725883b2006-10-09 12:07:58 +00001231 if(code < 0) return -1; /* WMORE */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001232 value = pc->code2value(code);
Lev Walkin725883b2006-10-09 12:07:58 +00001233 if(value < 0) {
1234 ASN_DEBUG("Code %d (0x%02x) is"
1235 " not in map (%ld..%ld)",
1236 code, code, lb, ub);
1237 return 1; /* FATAL */
1238 }
Lev Walkin3a4689a2006-11-24 11:20:27 +00001239 switch(bpc) {
1240 case 1: *buf = value; break;
1241 case 2: buf[0] = value >> 8; buf[1] = value; break;
1242 case 4: buf[0] = value >> 24; buf[1] = value >> 16;
1243 buf[2] = value >> 8; buf[3] = value; break;
1244 }
Lev Walkin725883b2006-10-09 12:07:58 +00001245 }
1246 return 0;
1247 }
1248
Lev Walkin3a4689a2006-11-24 11:20:27 +00001249 /* Shortcut the no-op copying to the aligned structure */
1250 if(lb == 0 && (unit_bits == 8 * bpc)) {
1251 return per_get_many_bits(po, buf, 0, unit_bits * units);
1252 }
1253
1254 for(; buf < end; buf += bpc) {
Lev Walkin725883b2006-10-09 12:07:58 +00001255 int code = per_get_few_bits(po, unit_bits);
1256 int ch = code + lb;
1257 if(code < 0) return -1; /* WMORE */
1258 if(ch > ub) {
1259 ASN_DEBUG("Code %d is out of range (%ld..%ld)",
1260 ch, lb, ub);
1261 return 1; /* FATAL */
1262 }
Lev Walkin3a4689a2006-11-24 11:20:27 +00001263 switch(bpc) {
1264 case 1: *buf = ch; break;
1265 case 2: buf[0] = ch >> 8; buf[1] = ch; break;
1266 case 4: buf[0] = ch >> 24; buf[1] = ch >> 16;
1267 buf[2] = ch >> 8; buf[3] = ch; break;
1268 }
Lev Walkin725883b2006-10-09 12:07:58 +00001269 }
1270
1271 return 0;
1272}
1273
1274static int
Lev Walkin3a4689a2006-11-24 11:20:27 +00001275OCTET_STRING_per_put_characters(asn_per_outp_t *po, const uint8_t *buf,
1276 size_t units, unsigned int bpc, unsigned int unit_bits,
Lev Walkin494fb702017-08-07 20:07:00 -07001277 long lb, long ub, const asn_per_constraints_t *pc) {
Lev Walkin3a4689a2006-11-24 11:20:27 +00001278 const uint8_t *end = buf + units * bpc;
Lev Walkin725883b2006-10-09 12:07:58 +00001279
Lev Walkin3a4689a2006-11-24 11:20:27 +00001280 ASN_DEBUG("Squeezing %d characters into (%ld..%ld):%d (%d bpc)",
1281 (int)units, lb, ub, unit_bits, bpc);
Lev Walkin725883b2006-10-09 12:07:58 +00001282
1283 /* X.691: 27.5.4 */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001284 if((unsigned long)ub <= ((unsigned long)2 << (unit_bits - 1))) {
Lev Walkin725883b2006-10-09 12:07:58 +00001285 /* Encode as is */
1286 lb = 0;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001287 } else if(pc && pc->value2code) {
1288 for(; buf < end; buf += bpc) {
1289 int code;
1290 uint32_t value;
1291 switch(bpc) {
1292 case 1: value = *(const uint8_t *)buf; break;
1293 case 2: value = (buf[0] << 8) | buf[1]; break;
1294 case 4: value = (buf[0] << 24) | (buf[1] << 16)
1295 | (buf[2] << 8) | buf[3]; break;
1296 default: return -1;
1297 }
1298 code = pc->value2code(value);
Lev Walkin725883b2006-10-09 12:07:58 +00001299 if(code < 0) {
1300 ASN_DEBUG("Character %d (0x%02x) is"
1301 " not in map (%ld..%ld)",
1302 *buf, *buf, lb, ub);
1303 return -1;
1304 }
1305 if(per_put_few_bits(po, code, unit_bits))
1306 return -1;
1307 }
1308 }
1309
Lev Walkin3a4689a2006-11-24 11:20:27 +00001310 /* Shortcut the no-op copying to the aligned structure */
1311 if(lb == 0 && (unit_bits == 8 * bpc)) {
1312 return per_put_many_bits(po, buf, unit_bits * units);
1313 }
1314
1315 for(ub -= lb; buf < end; buf += bpc) {
1316 int ch;
1317 uint32_t value;
1318 switch(bpc) {
1319 case 1: value = *(const uint8_t *)buf; break;
1320 case 2: value = (buf[0] << 8) | buf[1]; break;
1321 case 4: value = (buf[0] << 24) | (buf[1] << 16)
1322 | (buf[2] << 8) | buf[3]; break;
1323 default: return -1;
1324 }
1325 ch = value - lb;
Lev Walkin725883b2006-10-09 12:07:58 +00001326 if(ch < 0 || ch > ub) {
1327 ASN_DEBUG("Character %d (0x%02x)"
1328 " is out of range (%ld..%ld)",
1329 *buf, *buf, lb, ub + lb);
1330 return -1;
1331 }
1332 if(per_put_few_bits(po, ch, unit_bits))
1333 return -1;
1334 }
1335
1336 return 0;
1337}
1338
Lev Walkin62d76872017-08-05 22:49:42 -07001339#ifndef ASN_DISABLE_PER_SUPPORT
1340
Lev Walkin59b176e2005-11-26 11:25:14 +00001341asn_dec_rval_t
1342OCTET_STRING_decode_uper(asn_codec_ctx_t *opt_codec_ctx,
Lev Walkin494fb702017-08-07 20:07:00 -07001343 asn_TYPE_descriptor_t *td,
1344 const asn_per_constraints_t *constraints, void **sptr,
1345 asn_per_data_t *pd) {
1346 asn_OCTET_STRING_specifics_t *specs = td->specifics
Lev Walkin59b176e2005-11-26 11:25:14 +00001347 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +08001348 : &asn_SPC_OCTET_STRING_specs;
Lev Walkin494fb702017-08-07 20:07:00 -07001349 const asn_per_constraints_t *pc =
1350 constraints ? constraints : td->per_constraints;
1351 const asn_per_constraint_t *cval;
1352 const asn_per_constraint_t *csiz;
Lev Walkin59b176e2005-11-26 11:25:14 +00001353 asn_dec_rval_t rval = { RC_OK, 0 };
1354 BIT_STRING_t *st = (BIT_STRING_t *)*sptr;
1355 ssize_t consumed_myself = 0;
1356 int repeat;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001357 enum {
1358 OS__BPC_BIT = 0,
1359 OS__BPC_CHAR = 1,
1360 OS__BPC_U16 = 2,
1361 OS__BPC_U32 = 4
1362 } bpc; /* Bytes per character */
1363 unsigned int unit_bits;
1364 unsigned int canonical_unit_bits;
Lev Walkin59b176e2005-11-26 11:25:14 +00001365
1366 (void)opt_codec_ctx;
1367
Lev Walkin3a4689a2006-11-24 11:20:27 +00001368 if(pc) {
1369 cval = &pc->value;
1370 csiz = &pc->size;
1371 } else {
1372 cval = &asn_DEF_OCTET_STRING_constraints.value;
1373 csiz = &asn_DEF_OCTET_STRING_constraints.size;
1374 }
1375
1376 switch(specs->subvariant) {
1377 default:
1378 case ASN_OSUBV_ANY:
1379 ASN_DEBUG("Unrecognized subvariant %d", specs->subvariant);
1380 RETURN(RC_FAIL);
1381 case ASN_OSUBV_BIT:
1382 canonical_unit_bits = unit_bits = 1;
1383 bpc = OS__BPC_BIT;
1384 break;
1385 case ASN_OSUBV_STR:
1386 canonical_unit_bits = unit_bits = 8;
1387 if(cval->flags & APC_CONSTRAINED)
1388 unit_bits = cval->range_bits;
1389 bpc = OS__BPC_CHAR;
1390 break;
1391 case ASN_OSUBV_U16:
1392 canonical_unit_bits = unit_bits = 16;
1393 if(cval->flags & APC_CONSTRAINED)
1394 unit_bits = cval->range_bits;
1395 bpc = OS__BPC_U16;
1396 break;
1397 case ASN_OSUBV_U32:
1398 canonical_unit_bits = unit_bits = 32;
1399 if(cval->flags & APC_CONSTRAINED)
1400 unit_bits = cval->range_bits;
1401 bpc = OS__BPC_U32;
1402 break;
1403 }
1404
Lev Walkin59b176e2005-11-26 11:25:14 +00001405 /*
1406 * Allocate the string.
1407 */
1408 if(!st) {
1409 st = (BIT_STRING_t *)(*sptr = CALLOC(1, specs->struct_size));
1410 if(!st) RETURN(RC_FAIL);
1411 }
1412
Lev Walkin725883b2006-10-09 12:07:58 +00001413 ASN_DEBUG("PER Decoding %s size %ld .. %ld bits %d",
Lev Walkin3a4689a2006-11-24 11:20:27 +00001414 csiz->flags & APC_EXTENSIBLE ? "extensible" : "non-extensible",
1415 csiz->lower_bound, csiz->upper_bound, csiz->effective_bits);
Lev Walkince676dd2005-12-20 22:34:55 +00001416
Lev Walkin3a4689a2006-11-24 11:20:27 +00001417 if(csiz->flags & APC_EXTENSIBLE) {
Lev Walkin59b176e2005-11-26 11:25:14 +00001418 int inext = per_get_few_bits(pd, 1);
Lev Walkin0a8aa602006-09-18 20:05:55 +00001419 if(inext < 0) RETURN(RC_WMORE);
Lev Walkin3a4689a2006-11-24 11:20:27 +00001420 if(inext) {
1421 csiz = &asn_DEF_OCTET_STRING_constraints.size;
1422 cval = &asn_DEF_OCTET_STRING_constraints.value;
1423 unit_bits = canonical_unit_bits;
1424 }
Lev Walkin59b176e2005-11-26 11:25:14 +00001425 }
1426
Lev Walkin3a4689a2006-11-24 11:20:27 +00001427 if(csiz->effective_bits >= 0) {
Lev Walkin59b176e2005-11-26 11:25:14 +00001428 FREEMEM(st->buf);
Lev Walkin3a4689a2006-11-24 11:20:27 +00001429 if(bpc) {
1430 st->size = csiz->upper_bound * bpc;
Lev Walkin59b176e2005-11-26 11:25:14 +00001431 } else {
Lev Walkin3a4689a2006-11-24 11:20:27 +00001432 st->size = (csiz->upper_bound + 7) >> 3;
Lev Walkin59b176e2005-11-26 11:25:14 +00001433 }
1434 st->buf = (uint8_t *)MALLOC(st->size + 1);
1435 if(!st->buf) { st->size = 0; RETURN(RC_FAIL); }
1436 }
1437
1438 /* X.691, #16.5: zero-length encoding */
1439 /* X.691, #16.6: short fixed length encoding (up to 2 octets) */
1440 /* X.691, #16.7: long fixed length encoding (up to 64K octets) */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001441 if(csiz->effective_bits == 0) {
Lev Walkin725883b2006-10-09 12:07:58 +00001442 int ret;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001443 if(bpc) {
1444 ASN_DEBUG("Encoding OCTET STRING size %ld",
1445 csiz->upper_bound);
1446 ret = OCTET_STRING_per_get_characters(pd, st->buf,
1447 csiz->upper_bound, bpc, unit_bits,
1448 cval->lower_bound, cval->upper_bound, pc);
Lev Walkin725883b2006-10-09 12:07:58 +00001449 if(ret > 0) RETURN(RC_FAIL);
1450 } else {
Lev Walkin3a4689a2006-11-24 11:20:27 +00001451 ASN_DEBUG("Encoding BIT STRING size %ld",
1452 csiz->upper_bound);
Lev Walkin725883b2006-10-09 12:07:58 +00001453 ret = per_get_many_bits(pd, st->buf, 0,
Lev Walkin3a4689a2006-11-24 11:20:27 +00001454 unit_bits * csiz->upper_bound);
Lev Walkin725883b2006-10-09 12:07:58 +00001455 }
Lev Walkin0a8aa602006-09-18 20:05:55 +00001456 if(ret < 0) RETURN(RC_WMORE);
Lev Walkin3a4689a2006-11-24 11:20:27 +00001457 consumed_myself += unit_bits * csiz->upper_bound;
Lev Walkin59b176e2005-11-26 11:25:14 +00001458 st->buf[st->size] = 0;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001459 if(bpc == 0) {
1460 int ubs = (csiz->upper_bound & 0x7);
1461 st->bits_unused = ubs ? 8 - ubs : 0;
1462 }
Lev Walkin59b176e2005-11-26 11:25:14 +00001463 RETURN(RC_OK);
1464 }
1465
1466 st->size = 0;
1467 do {
Lev Walkin3a4689a2006-11-24 11:20:27 +00001468 ssize_t raw_len;
Lev Walkin59b176e2005-11-26 11:25:14 +00001469 ssize_t len_bytes;
1470 ssize_t len_bits;
1471 void *p;
1472 int ret;
1473
1474 /* Get the PER length */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001475 raw_len = uper_get_length(pd, csiz->effective_bits, &repeat);
1476 if(raw_len < 0) RETURN(RC_WMORE);
1477 raw_len += csiz->lower_bound;
Lev Walkin59b176e2005-11-26 11:25:14 +00001478
Lev Walkin0a8aa602006-09-18 20:05:55 +00001479 ASN_DEBUG("Got PER length eb %ld, len %ld, %s (%s)",
Lev Walkin3a4689a2006-11-24 11:20:27 +00001480 (long)csiz->effective_bits, (long)raw_len,
Lev Walkin0a8aa602006-09-18 20:05:55 +00001481 repeat ? "repeat" : "once", td->name);
Lev Walkin3a4689a2006-11-24 11:20:27 +00001482 if(bpc) {
1483 len_bytes = raw_len * bpc;
1484 len_bits = len_bytes * unit_bits;
1485 } else {
1486 len_bits = raw_len;
Lev Walkin59b176e2005-11-26 11:25:14 +00001487 len_bytes = (len_bits + 7) >> 3;
1488 if(len_bits & 0x7)
1489 st->bits_unused = 8 - (len_bits & 0x7);
1490 /* len_bits be multiple of 16K if repeat is set */
Lev Walkin59b176e2005-11-26 11:25:14 +00001491 }
1492 p = REALLOC(st->buf, st->size + len_bytes + 1);
1493 if(!p) RETURN(RC_FAIL);
1494 st->buf = (uint8_t *)p;
1495
Lev Walkin3a4689a2006-11-24 11:20:27 +00001496 if(bpc) {
1497 ret = OCTET_STRING_per_get_characters(pd,
1498 &st->buf[st->size], raw_len, bpc, unit_bits,
1499 cval->lower_bound, cval->upper_bound, pc);
Lev Walkin725883b2006-10-09 12:07:58 +00001500 if(ret > 0) RETURN(RC_FAIL);
1501 } else {
1502 ret = per_get_many_bits(pd, &st->buf[st->size],
1503 0, len_bits);
1504 }
Lev Walkin0a8aa602006-09-18 20:05:55 +00001505 if(ret < 0) RETURN(RC_WMORE);
Lev Walkin59b176e2005-11-26 11:25:14 +00001506 st->size += len_bytes;
1507 } while(repeat);
1508 st->buf[st->size] = 0; /* nul-terminate */
1509
1510 return rval;
1511}
Lev Walkindc06f6b2004-10-20 15:50:55 +00001512
Lev Walkin523de9e2006-08-18 01:34:18 +00001513asn_enc_rval_t
1514OCTET_STRING_encode_uper(asn_TYPE_descriptor_t *td,
Lev Walkin494fb702017-08-07 20:07:00 -07001515 const asn_per_constraints_t *constraints, void *sptr,
1516 asn_per_outp_t *po) {
1517 asn_OCTET_STRING_specifics_t *specs = td->specifics
Lev Walkin523de9e2006-08-18 01:34:18 +00001518 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +08001519 : &asn_SPC_OCTET_STRING_specs;
Lev Walkin494fb702017-08-07 20:07:00 -07001520 const asn_per_constraints_t *pc = constraints ? constraints
Lev Walkin725883b2006-10-09 12:07:58 +00001521 : td->per_constraints;
Lev Walkin494fb702017-08-07 20:07:00 -07001522 const asn_per_constraint_t *cval;
1523 const asn_per_constraint_t *csiz;
Lev Walkin523de9e2006-08-18 01:34:18 +00001524 const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001525 asn_enc_rval_t er = { 0, 0, 0 };
Lev Walkin523de9e2006-08-18 01:34:18 +00001526 int inext = 0; /* Lies not within extension root */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001527 unsigned int unit_bits;
1528 unsigned int canonical_unit_bits;
1529 unsigned int sizeinunits;
Lev Walkin523de9e2006-08-18 01:34:18 +00001530 const uint8_t *buf;
1531 int ret;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001532 enum {
1533 OS__BPC_BIT = 0,
1534 OS__BPC_CHAR = 1,
1535 OS__BPC_U16 = 2,
1536 OS__BPC_U32 = 4
1537 } bpc; /* Bytes per character */
1538 int ct_extensible;
Lev Walkin523de9e2006-08-18 01:34:18 +00001539
Lev Walkin2ddd3082006-10-16 12:32:14 +00001540 if(!st || (!st->buf && st->size))
Lev Walkin7c1dc052016-03-14 03:08:15 -07001541 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001542
Lev Walkin3a4689a2006-11-24 11:20:27 +00001543 if(pc) {
1544 cval = &pc->value;
1545 csiz = &pc->size;
1546 } else {
1547 cval = &asn_DEF_OCTET_STRING_constraints.value;
1548 csiz = &asn_DEF_OCTET_STRING_constraints.size;
1549 }
1550 ct_extensible = csiz->flags & APC_EXTENSIBLE;
1551
1552 switch(specs->subvariant) {
1553 default:
1554 case ASN_OSUBV_ANY:
Lev Walkin7c1dc052016-03-14 03:08:15 -07001555 ASN__ENCODE_FAILED;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001556 case ASN_OSUBV_BIT:
1557 canonical_unit_bits = unit_bits = 1;
1558 bpc = OS__BPC_BIT;
1559 sizeinunits = st->size * 8 - (st->bits_unused & 0x07);
Lev Walkin523de9e2006-08-18 01:34:18 +00001560 ASN_DEBUG("BIT STRING of %d bytes, %d bits unused",
1561 sizeinunits, st->bits_unused);
Lev Walkin3a4689a2006-11-24 11:20:27 +00001562 break;
1563 case ASN_OSUBV_STR:
1564 canonical_unit_bits = unit_bits = 8;
1565 if(cval->flags & APC_CONSTRAINED)
1566 unit_bits = cval->range_bits;
1567 bpc = OS__BPC_CHAR;
1568 sizeinunits = st->size;
1569 break;
1570 case ASN_OSUBV_U16:
1571 canonical_unit_bits = unit_bits = 16;
1572 if(cval->flags & APC_CONSTRAINED)
1573 unit_bits = cval->range_bits;
1574 bpc = OS__BPC_U16;
1575 sizeinunits = st->size / 2;
1576 break;
1577 case ASN_OSUBV_U32:
1578 canonical_unit_bits = unit_bits = 32;
1579 if(cval->flags & APC_CONSTRAINED)
1580 unit_bits = cval->range_bits;
1581 bpc = OS__BPC_U32;
1582 sizeinunits = st->size / 4;
1583 break;
Lev Walkin725883b2006-10-09 12:07:58 +00001584 }
1585
Lev Walkin00918812006-09-18 21:19:32 +00001586 ASN_DEBUG("Encoding %s into %d units of %d bits"
Lev Walkin725883b2006-10-09 12:07:58 +00001587 " (%ld..%ld, effective %d)%s",
Lev Walkin00918812006-09-18 21:19:32 +00001588 td->name, sizeinunits, unit_bits,
Lev Walkin3a4689a2006-11-24 11:20:27 +00001589 csiz->lower_bound, csiz->upper_bound,
1590 csiz->effective_bits, ct_extensible ? " EXT" : "");
Lev Walkin523de9e2006-08-18 01:34:18 +00001591
Lev Walkind4c16732013-03-28 05:00:47 -07001592 /* Figure out whether size lies within PER visible constraint */
Lev Walkin523de9e2006-08-18 01:34:18 +00001593
Lev Walkin3a4689a2006-11-24 11:20:27 +00001594 if(csiz->effective_bits >= 0) {
1595 if((int)sizeinunits < csiz->lower_bound
1596 || (int)sizeinunits > csiz->upper_bound) {
Lev Walkin523de9e2006-08-18 01:34:18 +00001597 if(ct_extensible) {
Lev Walkin3a4689a2006-11-24 11:20:27 +00001598 cval = &asn_DEF_OCTET_STRING_constraints.value;
1599 csiz = &asn_DEF_OCTET_STRING_constraints.size;
1600 unit_bits = canonical_unit_bits;
Lev Walkin523de9e2006-08-18 01:34:18 +00001601 inext = 1;
1602 } else
Lev Walkin7c1dc052016-03-14 03:08:15 -07001603 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001604 }
1605 } else {
1606 inext = 0;
1607 }
1608
1609 if(ct_extensible) {
1610 /* Declare whether length is [not] within extension root */
1611 if(per_put_few_bits(po, inext, 1))
Lev Walkin7c1dc052016-03-14 03:08:15 -07001612 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001613 }
1614
1615 /* X.691, #16.5: zero-length encoding */
1616 /* X.691, #16.6: short fixed length encoding (up to 2 octets) */
1617 /* X.691, #16.7: long fixed length encoding (up to 64K octets) */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001618 if(csiz->effective_bits >= 0) {
Lev Walkin24810022017-08-25 12:16:11 -07001619 ASN_DEBUG("Encoding %zu bytes (%ld), length in %d bits",
Lev Walkin3a4689a2006-11-24 11:20:27 +00001620 st->size, sizeinunits - csiz->lower_bound,
1621 csiz->effective_bits);
1622 ret = per_put_few_bits(po, sizeinunits - csiz->lower_bound,
1623 csiz->effective_bits);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001624 if(ret) ASN__ENCODE_FAILED;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001625 if(bpc) {
1626 ret = OCTET_STRING_per_put_characters(po, st->buf,
1627 sizeinunits, bpc, unit_bits,
1628 cval->lower_bound, cval->upper_bound, pc);
Lev Walkin725883b2006-10-09 12:07:58 +00001629 } else {
1630 ret = per_put_many_bits(po, st->buf,
1631 sizeinunits * unit_bits);
1632 }
Lev Walkin7c1dc052016-03-14 03:08:15 -07001633 if(ret) ASN__ENCODE_FAILED;
1634 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +00001635 }
1636
Lev Walkin24810022017-08-25 12:16:11 -07001637 ASN_DEBUG("Encoding %zu bytes", st->size);
Lev Walkin523de9e2006-08-18 01:34:18 +00001638
1639 if(sizeinunits == 0) {
1640 if(uper_put_length(po, 0))
Lev Walkin7c1dc052016-03-14 03:08:15 -07001641 ASN__ENCODE_FAILED;
1642 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +00001643 }
1644
1645 buf = st->buf;
1646 while(sizeinunits) {
1647 ssize_t maySave = uper_put_length(po, sizeinunits);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001648 if(maySave < 0) ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001649
Lev Walkin345e4c62006-10-19 02:44:08 +00001650 ASN_DEBUG("Encoding %ld of %ld",
1651 (long)maySave, (long)sizeinunits);
Lev Walkin523de9e2006-08-18 01:34:18 +00001652
Lev Walkin3a4689a2006-11-24 11:20:27 +00001653 if(bpc) {
1654 ret = OCTET_STRING_per_put_characters(po, buf,
1655 maySave, bpc, unit_bits,
1656 cval->lower_bound, cval->upper_bound, pc);
Lev Walkin725883b2006-10-09 12:07:58 +00001657 } else {
1658 ret = per_put_many_bits(po, buf, maySave * unit_bits);
1659 }
Lev Walkin7c1dc052016-03-14 03:08:15 -07001660 if(ret) ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001661
Lev Walkin3a4689a2006-11-24 11:20:27 +00001662 if(bpc)
1663 buf += maySave * bpc;
Lev Walkin523de9e2006-08-18 01:34:18 +00001664 else
Lev Walkin3a4689a2006-11-24 11:20:27 +00001665 buf += maySave >> 3;
Lev Walkin523de9e2006-08-18 01:34:18 +00001666 sizeinunits -= maySave;
1667 assert(!(maySave & 0x07) || !sizeinunits);
1668 }
1669
Lev Walkin7c1dc052016-03-14 03:08:15 -07001670 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +00001671}
1672
Lev Walkin62d76872017-08-05 22:49:42 -07001673#endif /* ASN_DISABLE_PER_SUPPORT */
1674
Lev Walkinf15320b2004-06-03 03:38:44 +00001675int
Lev Walkinde4825d2004-09-29 13:20:14 +00001676OCTET_STRING_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +00001677 asn_app_consume_bytes_f *cb, void *app_key) {
Wim Lewis59e8d282014-08-04 12:39:35 -07001678 const char * const h2c = "0123456789ABCDEF";
Lev Walkinc2346572004-08-11 09:07:36 +00001679 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +00001680 char scratch[16 * 3 + 4];
1681 char *p = scratch;
1682 uint8_t *buf;
1683 uint8_t *end;
1684 size_t i;
Lev Walkinf15320b2004-06-03 03:38:44 +00001685
Lev Walkind9bd7752004-06-05 08:17:50 +00001686 (void)td; /* Unused argument */
1687
Lev Walkin2ddd3082006-10-16 12:32:14 +00001688 if(!st || (!st->buf && st->size))
1689 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001690
1691 /*
1692 * Dump the contents of the buffer in hexadecimal.
1693 */
1694 buf = st->buf;
1695 end = buf + st->size;
1696 for(i = 0; buf < end; buf++, i++) {
1697 if(!(i % 16) && (i || st->size > 16)) {
Lev Walkin8e8078a2004-09-26 13:10:40 +00001698 if(cb(scratch, p - scratch, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +00001699 return -1;
Lev Walkin8e8078a2004-09-26 13:10:40 +00001700 _i_INDENT(1);
Lev Walkinf15320b2004-06-03 03:38:44 +00001701 p = scratch;
1702 }
1703 *p++ = h2c[(*buf >> 4) & 0x0F];
1704 *p++ = h2c[*buf & 0x0F];
Lev Walkina9cc46e2004-09-22 16:06:28 +00001705 *p++ = 0x20;
Lev Walkinf15320b2004-06-03 03:38:44 +00001706 }
1707
Lev Walkincc6a9102004-09-23 22:06:26 +00001708 if(p > scratch) {
1709 p--; /* Remove the tail space */
Lev Walkin8e8078a2004-09-26 13:10:40 +00001710 if(cb(scratch, p - scratch, app_key) < 0)
Lev Walkincc6a9102004-09-23 22:06:26 +00001711 return -1;
1712 }
1713
1714 return 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001715}
1716
1717int
Lev Walkindc06f6b2004-10-20 15:50:55 +00001718OCTET_STRING_print_utf8(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +00001719 int ilevel, asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +00001720 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +00001721
Lev Walkind9bd7752004-06-05 08:17:50 +00001722 (void)td; /* Unused argument */
1723 (void)ilevel; /* Unused argument */
1724
Lev Walkin2ddd3082006-10-16 12:32:14 +00001725 if(st && (st->buf || !st->size)) {
Lev Walkin8e8078a2004-09-26 13:10:40 +00001726 return (cb(st->buf, st->size, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001727 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +00001728 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001729 }
1730}
1731
1732void
Lev Walkinf6853ce2017-08-11 00:50:27 -07001733OCTET_STRING_free(const asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkin8d99d7b2017-08-25 01:06:00 -07001734 enum asn_struct_free_method method) {
1735 OCTET_STRING_t *st = (OCTET_STRING_t *)sptr;
Lev Walkin034ee192016-03-14 02:28:33 -07001736 asn_OCTET_STRING_specifics_t *specs;
1737 asn_struct_ctx_t *ctx;
Lev Walkinb54577a2004-11-08 10:47:12 +00001738 struct _stack *stck;
Lev Walkinf15320b2004-06-03 03:38:44 +00001739
1740 if(!td || !st)
1741 return;
1742
Lev Walkin034ee192016-03-14 02:28:33 -07001743 specs = td->specifics
1744 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +08001745 : &asn_SPC_OCTET_STRING_specs;
Lev Walkin034ee192016-03-14 02:28:33 -07001746 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
1747
Lev Walkinf15320b2004-06-03 03:38:44 +00001748 ASN_DEBUG("Freeing %s as OCTET STRING", td->name);
1749
1750 if(st->buf) {
1751 FREEMEM(st->buf);
Lev Walkin2ddd3082006-10-16 12:32:14 +00001752 st->buf = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001753 }
1754
1755 /*
1756 * Remove decode-time stack.
1757 */
Lev Walkinb54577a2004-11-08 10:47:12 +00001758 stck = (struct _stack *)ctx->ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +00001759 if(stck) {
1760 while(stck->tail) {
1761 struct _stack_el *sel = stck->tail;
1762 stck->tail = sel->prev;
1763 FREEMEM(sel);
1764 }
1765 FREEMEM(stck);
1766 }
1767
Lev Walkin8d99d7b2017-08-25 01:06:00 -07001768 switch(method) {
1769 case ASFM_FREE_EVERYTHING:
1770 FREEMEM(sptr);
1771 break;
1772 case ASFM_FREE_UNDERLYING:
1773 break;
1774 case ASFM_FREE_UNDERLYING_AND_RESET:
1775 memset(sptr, 0,
1776 ((asn_OCTET_STRING_specifics_t *)(td->specifics))->struct_size);
1777 break;
1778 }
Lev Walkinf15320b2004-06-03 03:38:44 +00001779}
1780
1781/*
1782 * Conversion routines.
1783 */
1784int
1785OCTET_STRING_fromBuf(OCTET_STRING_t *st, const char *str, int len) {
1786 void *buf;
1787
1788 if(st == 0 || (str == 0 && len)) {
1789 errno = EINVAL;
1790 return -1;
1791 }
1792
1793 /*
1794 * Clear the OCTET STRING.
1795 */
1796 if(str == NULL) {
Lev Walkin7b284812005-12-17 11:43:25 +00001797 FREEMEM(st->buf);
1798 st->buf = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001799 st->size = 0;
1800 return 0;
1801 }
1802
1803 /* Determine the original string size, if not explicitly given */
1804 if(len < 0)
1805 len = strlen(str);
1806
1807 /* Allocate and fill the memory */
1808 buf = MALLOC(len + 1);
Lev Walkin7b284812005-12-17 11:43:25 +00001809 if(buf == NULL)
Lev Walkinf15320b2004-06-03 03:38:44 +00001810 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +00001811
1812 memcpy(buf, str, len);
Lev Walkin7b284812005-12-17 11:43:25 +00001813 ((uint8_t *)buf)[len] = '\0'; /* Couldn't use memcpy(len+1)! */
1814 FREEMEM(st->buf);
1815 st->buf = (uint8_t *)buf;
1816 st->size = len;
Lev Walkinf15320b2004-06-03 03:38:44 +00001817
1818 return 0;
1819}
1820
1821OCTET_STRING_t *
Lev Walkinbbd93252004-10-12 05:57:23 +00001822OCTET_STRING_new_fromBuf(asn_TYPE_descriptor_t *td, const char *str, int len) {
1823 asn_OCTET_STRING_specifics_t *specs = td->specifics
Lev Walkindc06f6b2004-10-20 15:50:55 +00001824 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +08001825 : &asn_SPC_OCTET_STRING_specs;
Lev Walkinf15320b2004-06-03 03:38:44 +00001826 OCTET_STRING_t *st;
1827
Lev Walkinbbd93252004-10-12 05:57:23 +00001828 st = (OCTET_STRING_t *)CALLOC(1, specs->struct_size);
Lev Walkinf15320b2004-06-03 03:38:44 +00001829 if(st && str && OCTET_STRING_fromBuf(st, str, len)) {
Lev Walkin419f6752006-09-13 04:02:00 +00001830 FREEMEM(st);
Lev Walkinf15320b2004-06-03 03:38:44 +00001831 st = NULL;
1832 }
1833
1834 return st;
1835}
1836
Lev Walkincd2f48e2017-08-10 02:14:59 -07001837/*
1838 * Lexicographically compare the common prefix of both strings,
1839 * and if it is the same return -1 for the smallest string.
1840 */
1841int
1842OCTET_STRING_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
1843 const void *bptr) {
1844 const OCTET_STRING_t *a = aptr;
1845 const OCTET_STRING_t *b = bptr;
1846
1847 (void)td;
1848
1849 if(a && b) {
1850 size_t common_prefix_size = a->size <= b->size ? a->size : b->size;
1851 int ret = memcmp(a->buf, b->buf, common_prefix_size);
1852 if(ret == 0) {
1853 /* Figure out which string with equal prefixes is longer. */
1854 if(a->size < b->size) {
1855 return -1;
1856 } else if(a->size > b->size) {
1857 return 1;
1858 } else {
1859 return 0;
1860 }
1861 } else {
1862 return ret;
1863 }
1864 } else if(!a && !b) {
1865 return 0;
1866 } else if(!a) {
1867 return -1;
1868 } else {
1869 return 1;
1870 }
1871
1872}
1873