blob: ac486ec7ae2a6a7d924478566b9cfc2bcbb7d4c6 [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};
Lev Walkin0368c622017-09-18 00:33:16 -070022
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080023asn_TYPE_operation_t asn_OP_OCTET_STRING = {
Lev Walkina9cc46e2004-09-22 16:06:28 +000024 OCTET_STRING_free,
Lev Walkincd2f48e2017-08-10 02:14:59 -070025 OCTET_STRING_print, /* OCTET STRING generally means a non-ascii sequence */
26 OCTET_STRING_compare,
Lev Walkinf15320b2004-06-03 03:38:44 +000027 OCTET_STRING_decode_ber,
28 OCTET_STRING_encode_der,
Lev Walkindc06f6b2004-10-20 15:50:55 +000029 OCTET_STRING_decode_xer_hex,
Lev Walkina9cc46e2004-09-22 16:06:28 +000030 OCTET_STRING_encode_xer,
Lev Walkincc159472017-07-06 08:26:36 -070031#ifdef ASN_DISABLE_OER_SUPPORT
32 0,
33 0,
34#else
Lev Walkin2535bd52017-08-27 23:45:56 -070035 OCTET_STRING_decode_oer,
36 OCTET_STRING_encode_oer,
Lev Walkincc159472017-07-06 08:26:36 -070037#endif /* ASN_DISABLE_OER_SUPPORT */
Lev Walkinb33425f2017-07-14 14:59:52 +040038#ifdef ASN_DISABLE_PER_SUPPORT
39 0,
40 0,
41#else
42 OCTET_STRING_decode_uper, /* Unaligned PER decoder */
43 OCTET_STRING_encode_uper, /* Unaligned PER encoder */
44#endif /* ASN_DISABLE_PER_SUPPORT */
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080045 0 /* Use generic outmost tag fetcher */
46};
47asn_TYPE_descriptor_t asn_DEF_OCTET_STRING = {
48 "OCTET STRING", /* Canonical name */
49 "OCTET_STRING", /* XML tag name */
50 &asn_OP_OCTET_STRING,
51 asn_generic_no_constraint,
Lev Walkinde4825d2004-09-29 13:20:14 +000052 asn_DEF_OCTET_STRING_tags,
53 sizeof(asn_DEF_OCTET_STRING_tags)
54 / sizeof(asn_DEF_OCTET_STRING_tags[0]),
55 asn_DEF_OCTET_STRING_tags, /* Same as above */
56 sizeof(asn_DEF_OCTET_STRING_tags)
57 / sizeof(asn_DEF_OCTET_STRING_tags[0]),
Lev Walkin76780762017-07-07 10:07:30 -070058 0, /* No OER visible constraints */
Lev Walkin59b176e2005-11-26 11:25:14 +000059 0, /* No PER visible constraints */
Lev Walkin449f8322004-08-20 13:23:42 +000060 0, 0, /* No members */
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +080061 &asn_SPC_OCTET_STRING_specs
Lev Walkinf15320b2004-06-03 03:38:44 +000062};
63
Lev Walkincc6a9102004-09-23 22:06:26 +000064#undef _CH_PHASE
65#undef NEXT_PHASE
66#undef PREV_PHASE
Lev Walkin75b1bef2005-04-25 19:38:21 +000067#define _CH_PHASE(ctx, inc) do { \
68 if(ctx->phase == 0) \
69 ctx->context = 0; \
70 ctx->phase += inc; \
Lev Walkinf15320b2004-06-03 03:38:44 +000071 } while(0)
72#define NEXT_PHASE(ctx) _CH_PHASE(ctx, +1)
73#define PREV_PHASE(ctx) _CH_PHASE(ctx, -1)
74
Lev Walkincc6a9102004-09-23 22:06:26 +000075#undef ADVANCE
Lev Walkin75b1bef2005-04-25 19:38:21 +000076#define ADVANCE(num_bytes) do { \
77 size_t num = (num_bytes); \
78 buf_ptr = ((const char *)buf_ptr) + num; \
79 size -= num; \
80 consumed_myself += num; \
Lev Walkinf15320b2004-06-03 03:38:44 +000081 } while(0)
82
Lev Walkincc6a9102004-09-23 22:06:26 +000083#undef RETURN
Lev Walkin75b1bef2005-04-25 19:38:21 +000084#define RETURN(_code) do { \
Lev Walkin59b176e2005-11-26 11:25:14 +000085 asn_dec_rval_t tmprval; \
86 tmprval.code = _code; \
87 tmprval.consumed = consumed_myself; \
88 return tmprval; \
Lev Walkinf15320b2004-06-03 03:38:44 +000089 } while(0)
90
Lev Walkincc6a9102004-09-23 22:06:26 +000091#undef APPEND
Lev Walkind9bd7752004-06-05 08:17:50 +000092#define APPEND(bufptr, bufsize) do { \
Lev Walkin75b1bef2005-04-25 19:38:21 +000093 size_t _bs = (bufsize); /* Append size */ \
94 size_t _ns = ctx->context; /* Allocated now */ \
95 size_t _es = st->size + _bs; /* Expected size */ \
96 /* int is really a typeof(st->size): */ \
97 if((int)_es < 0) RETURN(RC_FAIL); \
98 if(_ns <= _es) { \
Lev Walkind9bd7752004-06-05 08:17:50 +000099 void *ptr; \
Lev Walkin188ed2c2004-09-13 08:31:01 +0000100 /* Be nice and round to the memory allocator */ \
Lev Walkin75b1bef2005-04-25 19:38:21 +0000101 do { _ns = _ns ? _ns << 1 : 16; } \
102 while(_ns <= _es); \
103 /* int is really a typeof(st->size): */ \
104 if((int)_ns < 0) RETURN(RC_FAIL); \
Lev Walkind9bd7752004-06-05 08:17:50 +0000105 ptr = REALLOC(st->buf, _ns); \
106 if(ptr) { \
Lev Walkinc2346572004-08-11 09:07:36 +0000107 st->buf = (uint8_t *)ptr; \
Lev Walkin75b1bef2005-04-25 19:38:21 +0000108 ctx->context = _ns; \
Lev Walkind9bd7752004-06-05 08:17:50 +0000109 } else { \
110 RETURN(RC_FAIL); \
111 } \
Lev Walkin7f85ef42005-07-02 20:24:27 +0000112 ASN_DEBUG("Reallocating into %ld", (long)_ns); \
Lev Walkind9bd7752004-06-05 08:17:50 +0000113 } \
Lev Walkin8d127872004-09-04 04:44:50 +0000114 memcpy(st->buf + st->size, bufptr, _bs); \
Lev Walkind9bd7752004-06-05 08:17:50 +0000115 /* Convenient nul-termination */ \
Lev Walkin75b1bef2005-04-25 19:38:21 +0000116 st->buf[_es] = '\0'; \
117 st->size = _es; \
Lev Walkinf15320b2004-06-03 03:38:44 +0000118 } while(0)
119
120/*
121 * The main reason why ASN.1 is still alive is that too much time and effort
122 * is necessary for learning it more or less adequately, thus creating a gut
123 * necessity to demonstrate that aquired skill everywhere afterwards.
124 * No, I am not going to explain what the following stuff is.
125 */
126struct _stack_el {
Lev Walkin494fb702017-08-07 20:07:00 -0700127 ber_tlv_len_t left; /* What's left to read (or -1) */
128 ber_tlv_len_t got; /* What was actually processed */
Lev Walkin24bf3f62017-08-10 17:58:43 -0700129 unsigned cont_level; /* Depth of subcontainment */
Lev Walkin494fb702017-08-07 20:07:00 -0700130 int want_nulls; /* Want null "end of content" octets? */
131 int bits_chopped; /* Flag in BIT STRING mode */
132 ber_tlv_tag_t tag; /* For debugging purposes */
133 struct _stack_el *prev;
134 struct _stack_el *next;
Lev Walkinf15320b2004-06-03 03:38:44 +0000135};
136struct _stack {
137 struct _stack_el *tail;
138 struct _stack_el *cur_ptr;
139};
140
141static struct _stack_el *
Lev Walkin3990ba62004-09-24 20:57:41 +0000142OS__add_stack_el(struct _stack *st) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000143 struct _stack_el *nel;
144
Lev Walkin188ed2c2004-09-13 08:31:01 +0000145 /*
146 * Reuse the old stack frame or allocate a new one.
147 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000148 if(st->cur_ptr && st->cur_ptr->next) {
149 nel = st->cur_ptr->next;
Lev Walkinf15320b2004-06-03 03:38:44 +0000150 nel->bits_chopped = 0;
Lev Walkin5c915992004-09-27 20:54:06 +0000151 nel->got = 0;
152 /* Retain the nel->cont_level, it's correct. */
Lev Walkinf15320b2004-06-03 03:38:44 +0000153 } else {
Lev Walkin814cca72004-12-15 23:23:53 +0000154 nel = (struct _stack_el *)CALLOC(1, sizeof(struct _stack_el));
Lev Walkinf15320b2004-06-03 03:38:44 +0000155 if(nel == NULL)
156 return NULL;
157
158 if(st->tail) {
Lev Walkin188ed2c2004-09-13 08:31:01 +0000159 /* Increase a subcontainment depth */
160 nel->cont_level = st->tail->cont_level + 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000161 st->tail->next = nel;
162 }
163 nel->prev = st->tail;
164 st->tail = nel;
165 }
166
167 st->cur_ptr = nel;
168
169 return nel;
170}
171
172static struct _stack *
johvik28268d92017-05-09 10:27:52 +0200173_new_stack(void) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000174 return (struct _stack *)CALLOC(1, sizeof(struct _stack));
Lev Walkinf15320b2004-06-03 03:38:44 +0000175}
176
177/*
178 * Decode OCTET STRING type.
179 */
Lev Walkindc06f6b2004-10-20 15:50:55 +0000180asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -0700181OCTET_STRING_decode_ber(const asn_codec_ctx_t *opt_codec_ctx,
Lev Walkinde4825d2004-09-29 13:20:14 +0000182 asn_TYPE_descriptor_t *td,
Lev Walkin59b176e2005-11-26 11:25:14 +0000183 void **sptr, const void *buf_ptr, size_t size, int tag_mode) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000184 asn_OCTET_STRING_specifics_t *specs = td->specifics
Lev Walkindc06f6b2004-10-20 15:50:55 +0000185 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +0800186 : &asn_SPC_OCTET_STRING_specs;
Lev Walkin59b176e2005-11-26 11:25:14 +0000187 BIT_STRING_t *st = (BIT_STRING_t *)*sptr;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000188 asn_dec_rval_t rval;
Lev Walkinde4825d2004-09-29 13:20:14 +0000189 asn_struct_ctx_t *ctx;
Lev Walkinf15320b2004-06-03 03:38:44 +0000190 ssize_t consumed_myself = 0;
Lev Walkinbbd93252004-10-12 05:57:23 +0000191 struct _stack *stck; /* Expectations stack structure */
Lev Walkin5c915992004-09-27 20:54:06 +0000192 struct _stack_el *sel = 0; /* Stack element */
Lev Walkinf15320b2004-06-03 03:38:44 +0000193 int tlv_constr;
Lev Walkin3a4689a2006-11-24 11:20:27 +0000194 enum asn_OS_Subvariant type_variant = specs->subvariant;
Lev Walkinf15320b2004-06-03 03:38:44 +0000195
Lev Walkincc6a9102004-09-23 22:06:26 +0000196 ASN_DEBUG("Decoding %s as %s (frame %ld)",
197 td->name,
Lev Walkin3a4689a2006-11-24 11:20:27 +0000198 (type_variant == ASN_OSUBV_STR) ?
Lev Walkinbbd93252004-10-12 05:57:23 +0000199 "OCTET STRING" : "OS-SpecialCase",
Lev Walkincc6a9102004-09-23 22:06:26 +0000200 (long)size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000201
202 /*
203 * Create the string if does not exist.
204 */
205 if(st == NULL) {
Lev Walkin59b176e2005-11-26 11:25:14 +0000206 st = (BIT_STRING_t *)(*sptr = CALLOC(1, specs->struct_size));
207 if(st == NULL) RETURN(RC_FAIL);
Lev Walkinf15320b2004-06-03 03:38:44 +0000208 }
209
210 /* Restore parsing context */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800211 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000212
213 switch(ctx->phase) {
214 case 0:
215 /*
216 * Check tags.
217 */
Lev Walkinde4825d2004-09-29 13:20:14 +0000218 rval = ber_check_tags(opt_codec_ctx, td, ctx,
Lev Walkin8e8078a2004-09-26 13:10:40 +0000219 buf_ptr, size, tag_mode, -1,
Lev Walkinf15320b2004-06-03 03:38:44 +0000220 &ctx->left, &tlv_constr);
Lev Walkin443d2512004-10-05 06:35:31 +0000221 if(rval.code != RC_OK)
222 return rval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000223
Lev Walkinf15320b2004-06-03 03:38:44 +0000224 if(tlv_constr) {
225 /*
226 * Complex operation, requires stack of expectations.
227 */
228 ctx->ptr = _new_stack();
Lev Walkinb02145b2017-06-28 08:52:07 -0700229 if(!ctx->ptr) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000230 RETURN(RC_FAIL);
231 }
232 } else {
233 /*
234 * Jump into stackless primitive decoding.
235 */
236 _CH_PHASE(ctx, 3);
Lev Walkin3a4689a2006-11-24 11:20:27 +0000237 if(type_variant == ASN_OSUBV_ANY && tag_mode != 1)
Lev Walkin188ed2c2004-09-13 08:31:01 +0000238 APPEND(buf_ptr, rval.consumed);
Lev Walkinf15320b2004-06-03 03:38:44 +0000239 ADVANCE(rval.consumed);
240 goto phase3;
241 }
242
Lev Walkinf15320b2004-06-03 03:38:44 +0000243 NEXT_PHASE(ctx);
244 /* Fall through */
245 case 1:
246 phase1:
247 /*
248 * Fill the stack with expectations.
249 */
Lev Walkin814cca72004-12-15 23:23:53 +0000250 stck = (struct _stack *)ctx->ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000251 sel = stck->cur_ptr;
252 do {
253 ber_tlv_tag_t tlv_tag;
254 ber_tlv_len_t tlv_len;
Lev Walkin188ed2c2004-09-13 08:31:01 +0000255 ber_tlv_tag_t expected_tag;
Lev Walkin5c915992004-09-27 20:54:06 +0000256 ssize_t tl, ll, tlvl;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000257 /* This one works even if (sel->left == -1) */
Simo Sorcecd943c22016-07-25 04:39:40 -0400258 size_t Left = ((!sel||(size_t)sel->left >= size)
259 ?size:(size_t)sel->left);
Lev Walkinf15320b2004-06-03 03:38:44 +0000260
Lev Walkin3990ba62004-09-24 20:57:41 +0000261
Lev Walkinabf68892004-10-26 10:12:14 +0000262 ASN_DEBUG("%p, s->l=%ld, s->wn=%ld, s->g=%ld\n", sel,
Lev Walkin1e3ccbb2004-10-26 10:56:10 +0000263 (long)(sel?sel->left:0),
264 (long)(sel?sel->want_nulls:0),
265 (long)(sel?sel->got:0)
Lev Walkin5c915992004-09-27 20:54:06 +0000266 );
267 if(sel && sel->left <= 0 && sel->want_nulls == 0) {
268 if(sel->prev) {
269 struct _stack_el *prev = sel->prev;
270 if(prev->left != -1) {
271 if(prev->left < sel->got)
272 RETURN(RC_FAIL);
273 prev->left -= sel->got;
274 }
275 prev->got += sel->got;
276 sel = stck->cur_ptr = prev;
277 if(!sel) break;
278 tlv_constr = 1;
279 continue;
280 } else {
281 sel = stck->cur_ptr = 0;
282 break; /* Nothing to wait */
283 }
284 }
285
Lev Walkin3990ba62004-09-24 20:57:41 +0000286 tl = ber_fetch_tag(buf_ptr, Left, &tlv_tag);
Lev Walkinabf68892004-10-26 10:12:14 +0000287 ASN_DEBUG("fetch tag(size=%ld,L=%ld), %sstack, left=%ld, wn=%ld, tl=%ld",
288 (long)size, (long)Left, sel?"":"!",
Lev Walkin1e3ccbb2004-10-26 10:56:10 +0000289 (long)(sel?sel->left:0),
290 (long)(sel?sel->want_nulls:0),
Lev Walkinabf68892004-10-26 10:12:14 +0000291 (long)tl);
Lev Walkinf15320b2004-06-03 03:38:44 +0000292 switch(tl) {
293 case -1: RETURN(RC_FAIL);
294 case 0: RETURN(RC_WMORE);
295 }
296
297 tlv_constr = BER_TLV_CONSTRUCTED(buf_ptr);
298
299 ll = ber_fetch_length(tlv_constr,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000300 (const char *)buf_ptr + tl,Left - tl,&tlv_len);
Lev Walkina6a926a2005-03-02 01:54:28 +0000301 ASN_DEBUG("Got tag=%s, tc=%d, left=%ld, tl=%ld, len=%ld, ll=%ld",
Lev Walkin8d127872004-09-04 04:44:50 +0000302 ber_tlv_tag_string(tlv_tag), tlv_constr,
Lev Walkina6a926a2005-03-02 01:54:28 +0000303 (long)Left, (long)tl, (long)tlv_len, (long)ll);
Lev Walkinf15320b2004-06-03 03:38:44 +0000304 switch(ll) {
305 case -1: RETURN(RC_FAIL);
306 case 0: RETURN(RC_WMORE);
307 }
308
Lev Walkin8d127872004-09-04 04:44:50 +0000309 if(sel && sel->want_nulls
Lev Walkin8c3b8542005-03-10 18:52:02 +0000310 && ((const uint8_t *)buf_ptr)[0] == 0
311 && ((const uint8_t *)buf_ptr)[1] == 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000312 {
Lev Walkin8d127872004-09-04 04:44:50 +0000313
314 ASN_DEBUG("Eat EOC; wn=%d--", sel->want_nulls);
315
Lev Walkin3a4689a2006-11-24 11:20:27 +0000316 if(type_variant == ASN_OSUBV_ANY
Lev Walkin07f388c2004-10-11 11:43:08 +0000317 && (tag_mode != 1 || sel->cont_level))
318 APPEND("\0\0", 2);
Lev Walkin5c915992004-09-27 20:54:06 +0000319
320 ADVANCE(2);
321 sel->got += 2;
322 if(sel->left != -1) {
323 sel->left -= 2; /* assert(sel->left >= 2) */
324 }
325
Lev Walkinf15320b2004-06-03 03:38:44 +0000326 sel->want_nulls--;
327 if(sel->want_nulls == 0) {
328 /* Move to the next expectation */
Lev Walkin5c915992004-09-27 20:54:06 +0000329 sel->left = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000330 tlv_constr = 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000331 }
Lev Walkin8d127872004-09-04 04:44:50 +0000332
333 continue;
Lev Walkin188ed2c2004-09-13 08:31:01 +0000334 }
335
336 /*
337 * Set up expected tags,
338 * depending on ASN.1 type being decoded.
339 */
Lev Walkinbbd93252004-10-12 05:57:23 +0000340 switch(type_variant) {
Lev Walkin3a4689a2006-11-24 11:20:27 +0000341 case ASN_OSUBV_BIT:
Lev Walkin188ed2c2004-09-13 08:31:01 +0000342 /* X.690: 8.6.4.1, NOTE 2 */
343 /* Fall through */
Lev Walkin3a4689a2006-11-24 11:20:27 +0000344 case ASN_OSUBV_STR:
Lev Walkin188ed2c2004-09-13 08:31:01 +0000345 default:
346 if(sel) {
Lev Walkin24bf3f62017-08-10 17:58:43 -0700347 unsigned level = sel->cont_level;
Lev Walkin188ed2c2004-09-13 08:31:01 +0000348 if(level < td->all_tags_count) {
349 expected_tag = td->all_tags[level];
350 break;
351 } else if(td->all_tags_count) {
352 expected_tag = td->all_tags
353 [td->all_tags_count - 1];
354 break;
355 }
356 /* else, Fall through */
357 }
358 /* Fall through */
Lev Walkin3a4689a2006-11-24 11:20:27 +0000359 case ASN_OSUBV_ANY:
Lev Walkin188ed2c2004-09-13 08:31:01 +0000360 expected_tag = tlv_tag;
361 break;
362 }
363
364
365 if(tlv_tag != expected_tag) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000366 char buf[2][32];
367 ber_tlv_tag_snprint(tlv_tag,
368 buf[0], sizeof(buf[0]));
369 ber_tlv_tag_snprint(td->tags[td->tags_count-1],
370 buf[1], sizeof(buf[1]));
371 ASN_DEBUG("Tag does not match expectation: %s != %s",
372 buf[0], buf[1]);
373 RETURN(RC_FAIL);
374 }
375
Lev Walkinde4825d2004-09-29 13:20:14 +0000376 tlvl = tl + ll; /* Combined length of T and L encoding */
377 if((tlv_len + tlvl) < 0) {
378 /* tlv_len value is too big */
379 ASN_DEBUG("TLV encoding + length (%ld) is too big",
380 (long)tlv_len);
381 RETURN(RC_FAIL);
382 }
383
Lev Walkinf15320b2004-06-03 03:38:44 +0000384 /*
385 * Append a new expectation.
386 */
Lev Walkin3990ba62004-09-24 20:57:41 +0000387 sel = OS__add_stack_el(stck);
Lev Walkin5c915992004-09-27 20:54:06 +0000388 if(!sel) RETURN(RC_FAIL);
Lev Walkinf15320b2004-06-03 03:38:44 +0000389
Lev Walkin5c915992004-09-27 20:54:06 +0000390 sel->tag = tlv_tag;
391
392 sel->want_nulls = (tlv_len==-1);
393 if(sel->prev && sel->prev->left != -1) {
394 /* Check that the parent frame is big enough */
395 if(sel->prev->left < tlvl + (tlv_len==-1?0:tlv_len))
396 RETURN(RC_FAIL);
397 if(tlv_len == -1)
398 sel->left = sel->prev->left - tlvl;
399 else
400 sel->left = tlv_len;
401 } else {
402 sel->left = tlv_len;
403 }
Lev Walkin3a4689a2006-11-24 11:20:27 +0000404 if(type_variant == ASN_OSUBV_ANY
Lev Walkin07f388c2004-10-11 11:43:08 +0000405 && (tag_mode != 1 || sel->cont_level))
406 APPEND(buf_ptr, tlvl);
Lev Walkin5c915992004-09-27 20:54:06 +0000407 sel->got += tlvl;
408 ADVANCE(tlvl);
409
Lev Walkin24bf3f62017-08-10 17:58:43 -0700410 ASN_DEBUG("+EXPECT2 got=%ld left=%ld, wn=%d, clvl=%u",
Lev Walkinabf68892004-10-26 10:12:14 +0000411 (long)sel->got, (long)sel->left,
412 sel->want_nulls, sel->cont_level);
Lev Walkin5c915992004-09-27 20:54:06 +0000413
Lev Walkinf15320b2004-06-03 03:38:44 +0000414 } while(tlv_constr);
415 if(sel == NULL) {
416 /* Finished operation, "phase out" */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000417 ASN_DEBUG("Phase out");
Lev Walkinf15320b2004-06-03 03:38:44 +0000418 _CH_PHASE(ctx, +3);
419 break;
420 }
421
422 NEXT_PHASE(ctx);
423 /* Fall through */
424 case 2:
Lev Walkin814cca72004-12-15 23:23:53 +0000425 stck = (struct _stack *)ctx->ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000426 sel = stck->cur_ptr;
Lev Walkin5c915992004-09-27 20:54:06 +0000427 ASN_DEBUG("Phase 2: Need %ld bytes, size=%ld, alrg=%ld, wn=%d",
428 (long)sel->left, (long)size, (long)sel->got,
429 sel->want_nulls);
Lev Walkinf15320b2004-06-03 03:38:44 +0000430 {
431 ber_tlv_len_t len;
432
433 assert(sel->left >= 0);
434
Lev Walkinec1ffd42004-08-18 04:53:32 +0000435 len = ((ber_tlv_len_t)size < sel->left)
436 ? (ber_tlv_len_t)size : sel->left;
Lev Walkinf15320b2004-06-03 03:38:44 +0000437 if(len > 0) {
Lev Walkin3a4689a2006-11-24 11:20:27 +0000438 if(type_variant == ASN_OSUBV_BIT
Lev Walkin188ed2c2004-09-13 08:31:01 +0000439 && sel->bits_chopped == 0) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000440 /* Put the unused-bits-octet away */
Lev Walkin8c3b8542005-03-10 18:52:02 +0000441 st->bits_unused = *(const uint8_t *)buf_ptr;
442 APPEND(((const char *)buf_ptr+1), (len - 1));
Lev Walkinf15320b2004-06-03 03:38:44 +0000443 sel->bits_chopped = 1;
444 } else {
445 APPEND(buf_ptr, len);
446 }
447 ADVANCE(len);
448 sel->left -= len;
Lev Walkin5c915992004-09-27 20:54:06 +0000449 sel->got += len;
Lev Walkinf15320b2004-06-03 03:38:44 +0000450 }
451
Lev Walkin5c915992004-09-27 20:54:06 +0000452 if(sel->left) {
453 ASN_DEBUG("OS left %ld, size = %ld, wn=%d\n",
454 (long)sel->left, (long)size, sel->want_nulls);
Lev Walkinf15320b2004-06-03 03:38:44 +0000455 RETURN(RC_WMORE);
Lev Walkinf15320b2004-06-03 03:38:44 +0000456 }
Lev Walkin5c915992004-09-27 20:54:06 +0000457
458 PREV_PHASE(ctx);
459 goto phase1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000460 }
461 break;
462 case 3:
463 phase3:
464 /*
465 * Primitive form, no stack required.
466 */
Lev Walkinbbd93252004-10-12 05:57:23 +0000467 assert(ctx->left >= 0);
468
Lev Walkind9bd7752004-06-05 08:17:50 +0000469 if(size < (size_t)ctx->left) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000470 if(!size) RETURN(RC_WMORE);
Lev Walkin3a4689a2006-11-24 11:20:27 +0000471 if(type_variant == ASN_OSUBV_BIT && !ctx->context) {
Lev Walkin8c3b8542005-03-10 18:52:02 +0000472 st->bits_unused = *(const uint8_t *)buf_ptr;
Lev Walkinbbd93252004-10-12 05:57:23 +0000473 ctx->left--;
474 ADVANCE(1);
475 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000476 APPEND(buf_ptr, size);
Lev Walkin75b1bef2005-04-25 19:38:21 +0000477 assert(ctx->context > 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000478 ctx->left -= size;
479 ADVANCE(size);
480 RETURN(RC_WMORE);
481 } else {
Lev Walkin3a4689a2006-11-24 11:20:27 +0000482 if(type_variant == ASN_OSUBV_BIT
Lev Walkin75b1bef2005-04-25 19:38:21 +0000483 && !ctx->context && ctx->left) {
Lev Walkin8c3b8542005-03-10 18:52:02 +0000484 st->bits_unused = *(const uint8_t *)buf_ptr;
Lev Walkinbbd93252004-10-12 05:57:23 +0000485 ctx->left--;
486 ADVANCE(1);
487 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000488 APPEND(buf_ptr, ctx->left);
489 ADVANCE(ctx->left);
490 ctx->left = 0;
491
492 NEXT_PHASE(ctx);
493 }
494 break;
495 }
496
Lev Walkin5c915992004-09-27 20:54:06 +0000497 if(sel) {
498 ASN_DEBUG("3sel p=%p, wn=%d, l=%ld, g=%ld, size=%ld",
499 sel->prev, sel->want_nulls,
500 (long)sel->left, (long)sel->got, (long)size);
501 if(sel->prev || sel->want_nulls > 1 || sel->left > 0) {
502 RETURN(RC_WMORE);
503 }
504 }
505
Lev Walkinf15320b2004-06-03 03:38:44 +0000506 /*
507 * BIT STRING-specific processing.
508 */
Lev Walkin0368c622017-09-18 00:33:16 -0700509 if(type_variant == ASN_OSUBV_BIT) {
510 if(st->size) {
511 if(st->bits_unused < 0 || st->bits_unused > 7) {
512 RETURN(RC_FAIL);
513 }
514 /* Finalize BIT STRING: zero out unused bits. */
515 st->buf[st->size-1] &= 0xff << st->bits_unused;
516 } else {
517 if(st->bits_unused) {
518 RETURN(RC_FAIL);
519 }
520 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000521 }
522
Lev Walkinabf68892004-10-26 10:12:14 +0000523 ASN_DEBUG("Took %ld bytes to encode %s: [%s]:%ld",
524 (long)consumed_myself, td->name,
Lev Walkin3a4689a2006-11-24 11:20:27 +0000525 (type_variant == ASN_OSUBV_STR) ? (char *)st->buf : "<data>",
Lev Walkinabf68892004-10-26 10:12:14 +0000526 (long)st->size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000527
Lev Walkinf15320b2004-06-03 03:38:44 +0000528
Lev Walkin5c915992004-09-27 20:54:06 +0000529 RETURN(RC_OK);
Lev Walkinf15320b2004-06-03 03:38:44 +0000530}
531
532/*
533 * Encode OCTET STRING type using DER.
534 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000535asn_enc_rval_t
Lev Walkinbbd93252004-10-12 05:57:23 +0000536OCTET_STRING_encode_der(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000537 int tag_mode, ber_tlv_tag_t tag,
538 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000539 asn_enc_rval_t er;
540 asn_OCTET_STRING_specifics_t *specs = td->specifics
Lev Walkindc06f6b2004-10-20 15:50:55 +0000541 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +0800542 : &asn_SPC_OCTET_STRING_specs;
Lev Walkinbbd93252004-10-12 05:57:23 +0000543 BIT_STRING_t *st = (BIT_STRING_t *)sptr;
Lev Walkin3a4689a2006-11-24 11:20:27 +0000544 enum asn_OS_Subvariant type_variant = specs->subvariant;
Lev Walkinbbd93252004-10-12 05:57:23 +0000545 int fix_last_byte = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000546
547 ASN_DEBUG("%s %s as OCTET STRING",
Lev Walkin1f670c12004-09-02 12:57:25 +0000548 cb?"Estimating":"Encoding", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000549
550 /*
Lev Walkinbbd93252004-10-12 05:57:23 +0000551 * Write tags.
Lev Walkinf15320b2004-06-03 03:38:44 +0000552 */
Lev Walkin3a4689a2006-11-24 11:20:27 +0000553 if(type_variant != ASN_OSUBV_ANY || tag_mode == 1) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000554 er.encoded = der_write_tags(td,
Lev Walkin3a4689a2006-11-24 11:20:27 +0000555 (type_variant == ASN_OSUBV_BIT) + st->size,
556 tag_mode, type_variant == ASN_OSUBV_ANY, tag,
557 cb, app_key);
Lev Walkinbbd93252004-10-12 05:57:23 +0000558 if(er.encoded == -1) {
559 er.failed_type = td;
560 er.structure_ptr = sptr;
561 return er;
Lev Walkinf15320b2004-06-03 03:38:44 +0000562 }
Lev Walkinbbd93252004-10-12 05:57:23 +0000563 } else {
564 /* Disallow: [<tag>] IMPLICIT ANY */
Lev Walkin3a4689a2006-11-24 11:20:27 +0000565 assert(type_variant != ASN_OSUBV_ANY || tag_mode != -1);
Lev Walkinbbd93252004-10-12 05:57:23 +0000566 er.encoded = 0;
567 }
568
569 if(!cb) {
Lev Walkin3a4689a2006-11-24 11:20:27 +0000570 er.encoded += (type_variant == ASN_OSUBV_BIT) + st->size;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700571 ASN__ENCODED_OK(er);
Lev Walkinf15320b2004-06-03 03:38:44 +0000572 }
573
Lev Walkin07f388c2004-10-11 11:43:08 +0000574 /*
Lev Walkinbbd93252004-10-12 05:57:23 +0000575 * Prepare to deal with the last octet of BIT STRING.
Lev Walkin07f388c2004-10-11 11:43:08 +0000576 */
Lev Walkin3a4689a2006-11-24 11:20:27 +0000577 if(type_variant == ASN_OSUBV_BIT) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000578 uint8_t b = st->bits_unused & 0x07;
579 if(b && st->size) fix_last_byte = 1;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700580 ASN__CALLBACK(&b, 1);
Lev Walkinbbd93252004-10-12 05:57:23 +0000581 er.encoded++;
Lev Walkinf15320b2004-06-03 03:38:44 +0000582 }
583
Lev Walkinbbd93252004-10-12 05:57:23 +0000584 /* Invoke callback for the main part of the buffer */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700585 ASN__CALLBACK(st->buf, st->size - fix_last_byte);
Lev Walkinf15320b2004-06-03 03:38:44 +0000586
Lev Walkinbbd93252004-10-12 05:57:23 +0000587 /* The last octet should be stripped off the unused bits */
588 if(fix_last_byte) {
589 uint8_t b = st->buf[st->size-1] & (0xff << st->bits_unused);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700590 ASN__CALLBACK(&b, 1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000591 }
592
Lev Walkinbbd93252004-10-12 05:57:23 +0000593 er.encoded += st->size;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700594 ASN__ENCODED_OK(er);
Lev Walkinbbd93252004-10-12 05:57:23 +0000595cb_failed:
Lev Walkin7c1dc052016-03-14 03:08:15 -0700596 ASN__ENCODE_FAILED;
Lev Walkinf15320b2004-06-03 03:38:44 +0000597}
598
Lev Walkina9cc46e2004-09-22 16:06:28 +0000599asn_enc_rval_t
Lev Walkinde4825d2004-09-29 13:20:14 +0000600OCTET_STRING_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000601 int ilevel, enum xer_encoder_flags_e flags,
602 asn_app_consume_bytes_f *cb, void *app_key) {
Wim Lewis59e8d282014-08-04 12:39:35 -0700603 const char * const h2c = "0123456789ABCDEF";
Lev Walkina9cc46e2004-09-22 16:06:28 +0000604 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
605 asn_enc_rval_t er;
606 char scratch[16 * 3 + 4];
607 char *p = scratch;
608 uint8_t *buf;
609 uint8_t *end;
610 size_t i;
611
Lev Walkin2ddd3082006-10-16 12:32:14 +0000612 if(!st || (!st->buf && st->size))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700613 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000614
615 er.encoded = 0;
616
617 /*
618 * Dump the contents of the buffer in hexadecimal.
619 */
620 buf = st->buf;
621 end = buf + st->size;
622 if(flags & XER_F_CANONICAL) {
623 char *scend = scratch + (sizeof(scratch) - 2);
624 for(; buf < end; buf++) {
625 if(p >= scend) {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700626 ASN__CALLBACK(scratch, p - scratch);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000627 er.encoded += p - scratch;
628 p = scratch;
629 }
630 *p++ = h2c[(*buf >> 4) & 0x0F];
631 *p++ = h2c[*buf & 0x0F];
632 }
Lev Walkincc6a9102004-09-23 22:06:26 +0000633
Lev Walkin7c1dc052016-03-14 03:08:15 -0700634 ASN__CALLBACK(scratch, p-scratch); /* Dump the rest */
Lev Walkincc6a9102004-09-23 22:06:26 +0000635 er.encoded += p - scratch;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000636 } else {
637 for(i = 0; buf < end; buf++, i++) {
638 if(!(i % 16) && (i || st->size > 16)) {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700639 ASN__CALLBACK(scratch, p-scratch);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000640 er.encoded += (p-scratch);
641 p = scratch;
Lev Walkinc6cac8e2016-03-14 02:57:07 -0700642 ASN__TEXT_INDENT(1, ilevel);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000643 }
644 *p++ = h2c[(*buf >> 4) & 0x0F];
645 *p++ = h2c[*buf & 0x0F];
646 *p++ = 0x20;
647 }
Lev Walkincc6a9102004-09-23 22:06:26 +0000648 if(p - scratch) {
649 p--; /* Remove the tail space */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700650 ASN__CALLBACK(scratch, p-scratch); /* Dump the rest */
Lev Walkincc6a9102004-09-23 22:06:26 +0000651 er.encoded += p - scratch;
652 if(st->size > 16)
Lev Walkinc6cac8e2016-03-14 02:57:07 -0700653 ASN__TEXT_INDENT(1, ilevel-1);
Lev Walkincc6a9102004-09-23 22:06:26 +0000654 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000655 }
656
Lev Walkin7c1dc052016-03-14 03:08:15 -0700657 ASN__ENCODED_OK(er);
Lev Walkin942fd082004-10-03 09:13:02 +0000658cb_failed:
Lev Walkin7c1dc052016-03-14 03:08:15 -0700659 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000660}
661
Wim Lewis18c2ec92014-07-29 11:30:10 -0700662static const struct OCTET_STRING__xer_escape_table_s {
663 const char *string;
Lev Walkin9ca81892004-10-03 10:54:25 +0000664 int size;
665} OCTET_STRING__xer_escape_table[] = {
666#define OSXET(s) { s, sizeof(s) - 1 }
667 OSXET("\074\156\165\154\057\076"), /* <nul/> */
668 OSXET("\074\163\157\150\057\076"), /* <soh/> */
669 OSXET("\074\163\164\170\057\076"), /* <stx/> */
670 OSXET("\074\145\164\170\057\076"), /* <etx/> */
671 OSXET("\074\145\157\164\057\076"), /* <eot/> */
672 OSXET("\074\145\156\161\057\076"), /* <enq/> */
673 OSXET("\074\141\143\153\057\076"), /* <ack/> */
674 OSXET("\074\142\145\154\057\076"), /* <bel/> */
675 OSXET("\074\142\163\057\076"), /* <bs/> */
676 OSXET("\011"), /* \t */
677 OSXET("\012"), /* \n */
678 OSXET("\074\166\164\057\076"), /* <vt/> */
679 OSXET("\074\146\146\057\076"), /* <ff/> */
680 OSXET("\015"), /* \r */
681 OSXET("\074\163\157\057\076"), /* <so/> */
682 OSXET("\074\163\151\057\076"), /* <si/> */
683 OSXET("\074\144\154\145\057\076"), /* <dle/> */
684 OSXET("\074\144\143\061\057\076"), /* <de1/> */
685 OSXET("\074\144\143\062\057\076"), /* <de2/> */
686 OSXET("\074\144\143\063\057\076"), /* <de3/> */
687 OSXET("\074\144\143\064\057\076"), /* <de4/> */
688 OSXET("\074\156\141\153\057\076"), /* <nak/> */
689 OSXET("\074\163\171\156\057\076"), /* <syn/> */
690 OSXET("\074\145\164\142\057\076"), /* <etb/> */
691 OSXET("\074\143\141\156\057\076"), /* <can/> */
692 OSXET("\074\145\155\057\076"), /* <em/> */
693 OSXET("\074\163\165\142\057\076"), /* <sub/> */
694 OSXET("\074\145\163\143\057\076"), /* <esc/> */
695 OSXET("\074\151\163\064\057\076"), /* <is4/> */
696 OSXET("\074\151\163\063\057\076"), /* <is3/> */
697 OSXET("\074\151\163\062\057\076"), /* <is2/> */
698 OSXET("\074\151\163\061\057\076"), /* <is1/> */
699 { 0, 0 }, /* " " */
700 { 0, 0 }, /* ! */
701 { 0, 0 }, /* \" */
702 { 0, 0 }, /* # */
703 { 0, 0 }, /* $ */
704 { 0, 0 }, /* % */
705 OSXET("\046\141\155\160\073"), /* &amp; */
706 { 0, 0 }, /* ' */
707 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, /* ()*+,-./ */
708 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, /* 01234567 */
709 {0,0},{0,0},{0,0},{0,0}, /* 89:; */
710 OSXET("\046\154\164\073"), /* &lt; */
711 { 0, 0 }, /* = */
712 OSXET("\046\147\164\073"), /* &gt; */
713};
714
Lev Walkindc06f6b2004-10-20 15:50:55 +0000715static int
Lev Walkin0fab1a62005-03-09 22:19:25 +0000716OS__check_escaped_control_char(const void *buf, int size) {
Lev Walkindc06f6b2004-10-20 15:50:55 +0000717 size_t i;
718 /*
719 * Inefficient algorithm which translates the escape sequences
720 * defined above into characters. Returns -1 if not found.
721 * TODO: replace by a faster algorithm (bsearch(), hash or
722 * nested table lookups).
723 */
724 for(i = 0; i < 32 /* Don't spend time on the bottom half */; i++) {
Wim Lewis18c2ec92014-07-29 11:30:10 -0700725 const struct OCTET_STRING__xer_escape_table_s *el;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000726 el = &OCTET_STRING__xer_escape_table[i];
727 if(el->size == size && memcmp(buf, el->string, size) == 0)
728 return i;
729 }
730 return -1;
731}
732
733static int
Lev Walkin0fab1a62005-03-09 22:19:25 +0000734OCTET_STRING__handle_control_chars(void *struct_ptr, const void *chunk_buf, size_t chunk_size) {
Lev Walkindc06f6b2004-10-20 15:50:55 +0000735 /*
736 * This might be one of the escape sequences
737 * for control characters. Check it out.
738 * #11.15.5
739 */
740 int control_char = OS__check_escaped_control_char(chunk_buf,chunk_size);
741 if(control_char >= 0) {
742 OCTET_STRING_t *st = (OCTET_STRING_t *)struct_ptr;
743 void *p = REALLOC(st->buf, st->size + 2);
744 if(p) {
745 st->buf = (uint8_t *)p;
746 st->buf[st->size++] = control_char;
747 st->buf[st->size] = '\0'; /* nul-termination */
748 return 0;
749 }
750 }
751
752 return -1; /* No, it's not */
753}
754
Lev Walkina9cc46e2004-09-22 16:06:28 +0000755asn_enc_rval_t
Lev Walkindc06f6b2004-10-20 15:50:55 +0000756OCTET_STRING_encode_xer_utf8(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000757 int ilevel, enum xer_encoder_flags_e flags,
758 asn_app_consume_bytes_f *cb, void *app_key) {
759 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
760 asn_enc_rval_t er;
Lev Walkin9ca81892004-10-03 10:54:25 +0000761 uint8_t *buf, *end;
762 uint8_t *ss; /* Sequence start */
763 ssize_t encoded_len = 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000764
765 (void)ilevel; /* Unused argument */
766 (void)flags; /* Unused argument */
767
Lev Walkin2ddd3082006-10-16 12:32:14 +0000768 if(!st || (!st->buf && st->size))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700769 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000770
Lev Walkin9ca81892004-10-03 10:54:25 +0000771 buf = st->buf;
772 end = buf + st->size;
773 for(ss = buf; buf < end; buf++) {
Lev Walkin443d2512004-10-05 06:35:31 +0000774 unsigned int ch = *buf;
Lev Walkin9ca81892004-10-03 10:54:25 +0000775 int s_len; /* Special encoding sequence length */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000776
Lev Walkin9ca81892004-10-03 10:54:25 +0000777 /*
778 * Escape certain characters: X.680/11.15
779 */
780 if(ch < sizeof(OCTET_STRING__xer_escape_table)
781 /sizeof(OCTET_STRING__xer_escape_table[0])
782 && (s_len = OCTET_STRING__xer_escape_table[ch].size)) {
783 if(((buf - ss) && cb(ss, buf - ss, app_key) < 0)
784 || cb(OCTET_STRING__xer_escape_table[ch].string, s_len,
785 app_key) < 0)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700786 ASN__ENCODE_FAILED;
Lev Walkin9ca81892004-10-03 10:54:25 +0000787 encoded_len += (buf - ss) + s_len;
788 ss = buf + 1;
789 }
790 }
791
792 encoded_len += (buf - ss);
793 if((buf - ss) && cb(ss, buf - ss, app_key) < 0)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700794 ASN__ENCODE_FAILED;
Lev Walkin9ca81892004-10-03 10:54:25 +0000795
796 er.encoded = encoded_len;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700797 ASN__ENCODED_OK(er);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000798}
799
Lev Walkindc06f6b2004-10-20 15:50:55 +0000800/*
801 * Convert from hexadecimal format (cstring): "AB CD EF"
802 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000803static 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 +0000804 OCTET_STRING_t *st = (OCTET_STRING_t *)sptr;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000805 const char *chunk_stop = (const char *)chunk_buf;
806 const char *p = chunk_stop;
807 const char *pend = p + chunk_size;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000808 unsigned int clv = 0;
809 int half = 0; /* Half bit */
810 uint8_t *buf;
811
812 /* Reallocate buffer according to high cap estimation */
Lev Walkin494fb702017-08-07 20:07:00 -0700813 size_t new_size = st->size + (chunk_size + 1) / 2;
814 void *nptr = REALLOC(st->buf, new_size + 1);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000815 if(!nptr) return -1;
816 st->buf = (uint8_t *)nptr;
817 buf = st->buf + st->size;
818
819 /*
820 * If something like " a b c " appears here, the " a b":3 will be
821 * converted, and the rest skipped. That is, unless buf_size is greater
822 * than chunk_size, then it'll be equivalent to "ABC0".
823 */
824 for(; p < pend; p++) {
Lev Walkin0fab1a62005-03-09 22:19:25 +0000825 int ch = *(const unsigned char *)p;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000826 switch(ch) {
827 case 0x09: case 0x0a: case 0x0c: case 0x0d:
828 case 0x20:
829 /* Ignore whitespace */
830 continue;
831 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: /*01234*/
832 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: /*56789*/
833 clv = (clv << 4) + (ch - 0x30);
834 break;
835 case 0x41: case 0x42: case 0x43: /* ABC */
836 case 0x44: case 0x45: case 0x46: /* DEF */
Lev Walkin3ae21bd2005-02-21 14:43:48 +0000837 clv = (clv << 4) + (ch - 0x41 + 10);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000838 break;
839 case 0x61: case 0x62: case 0x63: /* abc */
840 case 0x64: case 0x65: case 0x66: /* def */
Lev Walkin3ae21bd2005-02-21 14:43:48 +0000841 clv = (clv << 4) + (ch - 0x61 + 10);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000842 break;
843 default:
844 *buf = 0; /* JIC */
845 return -1;
846 }
847 if(half++) {
848 half = 0;
849 *buf++ = clv;
850 chunk_stop = p + 1;
851 }
852 }
853
854 /*
855 * Check partial decoding.
856 */
857 if(half) {
858 if(have_more) {
859 /*
860 * Partial specification is fine,
861 * because no more more PXER_TEXT data is available.
862 */
863 *buf++ = clv << 4;
864 chunk_stop = p;
865 }
866 } else {
867 chunk_stop = p;
868 }
869
870 st->size = buf - st->buf; /* Adjust the buffer size */
Lev Walkin494fb702017-08-07 20:07:00 -0700871 assert(st->size <= new_size);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000872 st->buf[st->size] = 0; /* Courtesy termination */
873
Lev Walkin0fab1a62005-03-09 22:19:25 +0000874 return (chunk_stop - (const char *)chunk_buf); /* Converted size */
Lev Walkindc06f6b2004-10-20 15:50:55 +0000875}
876
877/*
878 * Convert from binary format: "00101011101"
879 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000880static 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 +0000881 BIT_STRING_t *st = (BIT_STRING_t *)sptr;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000882 const char *p = (const char *)chunk_buf;
883 const char *pend = p + chunk_size;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000884 int bits_unused = st->bits_unused & 0x7;
885 uint8_t *buf;
886
887 /* Reallocate buffer according to high cap estimation */
Lev Walkin494fb702017-08-07 20:07:00 -0700888 size_t new_size = st->size + (chunk_size + 7) / 8;
889 void *nptr = REALLOC(st->buf, new_size + 1);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000890 if(!nptr) return -1;
891 st->buf = (uint8_t *)nptr;
892 buf = st->buf + st->size;
893
894 (void)have_more;
895
896 if(bits_unused == 0)
897 bits_unused = 8;
898 else if(st->size)
899 buf--;
900
901 /*
902 * Convert series of 0 and 1 into the octet string.
903 */
904 for(; p < pend; p++) {
Lev Walkin0fab1a62005-03-09 22:19:25 +0000905 int ch = *(const unsigned char *)p;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000906 switch(ch) {
907 case 0x09: case 0x0a: case 0x0c: case 0x0d:
908 case 0x20:
909 /* Ignore whitespace */
910 break;
911 case 0x30:
912 case 0x31:
913 if(bits_unused-- <= 0) {
914 *++buf = 0; /* Clean the cell */
915 bits_unused = 7;
916 }
917 *buf |= (ch&1) << bits_unused;
918 break;
919 default:
920 st->bits_unused = bits_unused;
921 return -1;
922 }
923 }
924
925 if(bits_unused == 8) {
926 st->size = buf - st->buf;
927 st->bits_unused = 0;
928 } else {
929 st->size = buf - st->buf + 1;
930 st->bits_unused = bits_unused;
931 }
932
Lev Walkin494fb702017-08-07 20:07:00 -0700933 assert(st->size <= new_size);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000934 st->buf[st->size] = 0; /* Courtesy termination */
935
936 return chunk_size; /* Converted in full */
937}
938
939/*
940 * Something like strtod(), but with stricter rules.
941 */
942static int
Lev Walkin0fab1a62005-03-09 22:19:25 +0000943OS__strtoent(int base, const char *buf, const char *end, int32_t *ret_value) {
Lev Walkinbeedbde2017-09-17 22:54:51 -0700944 const int32_t last_unicode_codepoint = 0x10ffff;
Lev Walkin33700162004-10-26 09:03:31 +0000945 int32_t val = 0;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000946 const char *p;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000947
948 for(p = buf; p < end; p++) {
949 int ch = *p;
Lev Walkinb0f3db62005-07-03 05:30:15 +0000950
Lev Walkindc06f6b2004-10-20 15:50:55 +0000951 switch(ch) {
952 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: /*01234*/
953 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: /*56789*/
954 val = val * base + (ch - 0x30);
955 break;
956 case 0x41: case 0x42: case 0x43: /* ABC */
957 case 0x44: case 0x45: case 0x46: /* DEF */
Lev Walkin3ae21bd2005-02-21 14:43:48 +0000958 val = val * base + (ch - 0x41 + 10);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000959 break;
960 case 0x61: case 0x62: case 0x63: /* abc */
961 case 0x64: case 0x65: case 0x66: /* def */
Lev Walkin3ae21bd2005-02-21 14:43:48 +0000962 val = val * base + (ch - 0x61 + 10);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000963 break;
964 case 0x3b: /* ';' */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000965 *ret_value = val;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000966 return (p - buf) + 1;
967 default:
968 return -1; /* Character set error */
969 }
Lev Walkinbeedbde2017-09-17 22:54:51 -0700970
971 /* Value exceeds the Unicode range. */
972 if(val > last_unicode_codepoint) {
973 return -1;
974 }
Lev Walkindc06f6b2004-10-20 15:50:55 +0000975 }
976
Lev Walkinb0f3db62005-07-03 05:30:15 +0000977 *ret_value = -1;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000978 return (p - buf);
979}
980
981/*
982 * Convert from the plain UTF-8 format, expanding entity references: "2 &lt; 3"
983 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000984static 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 +0000985 OCTET_STRING_t *st = (OCTET_STRING_t *)sptr;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000986 const char *p = (const char *)chunk_buf;
987 const char *pend = p + chunk_size;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000988 uint8_t *buf;
989
990 /* Reallocate buffer */
Lev Walkin494fb702017-08-07 20:07:00 -0700991 size_t new_size = st->size + chunk_size;
992 void *nptr = REALLOC(st->buf, new_size + 1);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000993 if(!nptr) return -1;
994 st->buf = (uint8_t *)nptr;
995 buf = st->buf + st->size;
996
997 /*
998 * Convert series of 0 and 1 into the octet string.
999 */
1000 for(; p < pend; p++) {
Lev Walkin0fab1a62005-03-09 22:19:25 +00001001 int ch = *(const unsigned char *)p;
Lev Walkindc06f6b2004-10-20 15:50:55 +00001002 int len; /* Length of the rest of the chunk */
1003
1004 if(ch != 0x26 /* '&' */) {
1005 *buf++ = ch;
1006 continue; /* That was easy... */
1007 }
1008
1009 /*
1010 * Process entity reference.
1011 */
Lev Walkin0fab1a62005-03-09 22:19:25 +00001012 len = chunk_size - (p - (const char *)chunk_buf);
Lev Walkindc06f6b2004-10-20 15:50:55 +00001013 if(len == 1 /* "&" */) goto want_more;
1014 if(p[1] == 0x23 /* '#' */) {
Lev Walkin0fab1a62005-03-09 22:19:25 +00001015 const char *pval; /* Pointer to start of digits */
Lev Walkinbc0dce32006-07-27 09:56:13 +00001016 int32_t val = 0; /* Entity reference value */
Lev Walkindc06f6b2004-10-20 15:50:55 +00001017 int base;
1018
1019 if(len == 2 /* "&#" */) goto want_more;
1020 if(p[2] == 0x78 /* 'x' */)
1021 pval = p + 3, base = 16;
1022 else
1023 pval = p + 2, base = 10;
1024 len = OS__strtoent(base, pval, p + len, &val);
1025 if(len == -1) {
1026 /* Invalid charset. Just copy verbatim. */
1027 *buf++ = ch;
1028 continue;
1029 }
1030 if(!len || pval[len-1] != 0x3b) goto want_more;
1031 assert(val > 0);
1032 p += (pval - p) + len - 1; /* Advance past entref */
1033
1034 if(val < 0x80) {
1035 *buf++ = (char)val;
1036 } else if(val < 0x800) {
1037 *buf++ = 0xc0 | ((val >> 6));
1038 *buf++ = 0x80 | ((val & 0x3f));
1039 } else if(val < 0x10000) {
1040 *buf++ = 0xe0 | ((val >> 12));
1041 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1042 *buf++ = 0x80 | ((val & 0x3f));
1043 } else if(val < 0x200000) {
1044 *buf++ = 0xf0 | ((val >> 18));
1045 *buf++ = 0x80 | ((val >> 12) & 0x3f);
1046 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1047 *buf++ = 0x80 | ((val & 0x3f));
1048 } else if(val < 0x4000000) {
1049 *buf++ = 0xf8 | ((val >> 24));
1050 *buf++ = 0x80 | ((val >> 18) & 0x3f);
1051 *buf++ = 0x80 | ((val >> 12) & 0x3f);
1052 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1053 *buf++ = 0x80 | ((val & 0x3f));
1054 } else {
1055 *buf++ = 0xfc | ((val >> 30) & 0x1);
1056 *buf++ = 0x80 | ((val >> 24) & 0x3f);
1057 *buf++ = 0x80 | ((val >> 18) & 0x3f);
1058 *buf++ = 0x80 | ((val >> 12) & 0x3f);
1059 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1060 *buf++ = 0x80 | ((val & 0x3f));
1061 }
1062 } else {
1063 /*
1064 * Ugly, limited parsing of &amp; &gt; &lt;
1065 */
1066 char *sc = (char *)memchr(p, 0x3b, len > 5 ? 5 : len);
1067 if(!sc) goto want_more;
1068 if((sc - p) == 4
1069 && p[1] == 0x61 /* 'a' */
1070 && p[2] == 0x6d /* 'm' */
1071 && p[3] == 0x70 /* 'p' */) {
1072 *buf++ = 0x26;
1073 p = sc;
1074 continue;
1075 }
1076 if((sc - p) == 3) {
1077 if(p[1] == 0x6c) {
1078 *buf = 0x3c; /* '<' */
1079 } else if(p[1] == 0x67) {
1080 *buf = 0x3e; /* '>' */
1081 } else {
1082 /* Unsupported entity reference */
1083 *buf++ = ch;
1084 continue;
1085 }
1086 if(p[2] != 0x74) {
1087 /* Unsupported entity reference */
1088 *buf++ = ch;
1089 continue;
1090 }
1091 buf++;
1092 p = sc;
1093 continue;
1094 }
1095 /* Unsupported entity reference */
1096 *buf++ = ch;
1097 }
1098
1099 continue;
1100 want_more:
1101 if(have_more) {
1102 /*
1103 * We know that no more data (of the same type)
1104 * is coming. Copy the rest verbatim.
1105 */
1106 *buf++ = ch;
1107 continue;
1108 }
Lev Walkin0fab1a62005-03-09 22:19:25 +00001109 chunk_size = (p - (const char *)chunk_buf);
Lev Walkindc06f6b2004-10-20 15:50:55 +00001110 /* Processing stalled: need more data */
Lev Walkinbdaae772005-02-18 12:25:47 +00001111 break;
Lev Walkindc06f6b2004-10-20 15:50:55 +00001112 }
1113
1114 st->size = buf - st->buf;
Lev Walkin494fb702017-08-07 20:07:00 -07001115 assert(st->size <= new_size);
Lev Walkindc06f6b2004-10-20 15:50:55 +00001116 st->buf[st->size] = 0; /* Courtesy termination */
1117
1118 return chunk_size; /* Converted in full */
1119}
1120
1121/*
1122 * Decode OCTET STRING from the XML element's body.
1123 */
1124static asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -07001125OCTET_STRING__decode_xer(const asn_codec_ctx_t *opt_codec_ctx,
Lev Walkindc06f6b2004-10-20 15:50:55 +00001126 asn_TYPE_descriptor_t *td, void **sptr,
Lev Walkin8c3b8542005-03-10 18:52:02 +00001127 const char *opt_mname, const void *buf_ptr, size_t size,
Lev Walkindc06f6b2004-10-20 15:50:55 +00001128 int (*opt_unexpected_tag_decoder)
Lev Walkin0fab1a62005-03-09 22:19:25 +00001129 (void *struct_ptr, const void *chunk_buf, size_t chunk_size),
Lev Walkindc06f6b2004-10-20 15:50:55 +00001130 ssize_t (*body_receiver)
Lev Walkin0fab1a62005-03-09 22:19:25 +00001131 (void *struct_ptr, const void *chunk_buf, size_t chunk_size,
Lev Walkindc06f6b2004-10-20 15:50:55 +00001132 int have_more)
1133) {
Lev Walkind5125642005-02-14 20:08:00 +00001134 OCTET_STRING_t *st = (OCTET_STRING_t *)*sptr;
Lev Walkindc06f6b2004-10-20 15:50:55 +00001135 asn_OCTET_STRING_specifics_t *specs = td->specifics
1136 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +08001137 : &asn_SPC_OCTET_STRING_specs;
Lev Walkindc06f6b2004-10-20 15:50:55 +00001138 const char *xml_tag = opt_mname ? opt_mname : td->xml_tag;
1139 asn_struct_ctx_t *ctx; /* Per-structure parser context */
Lev Walkinc61f3862005-02-14 17:21:22 +00001140 asn_dec_rval_t rval; /* Return value from the decoder */
1141 int st_allocated;
Lev Walkindc06f6b2004-10-20 15:50:55 +00001142
1143 /*
1144 * Create the string if does not exist.
1145 */
Lev Walkinc61f3862005-02-14 17:21:22 +00001146 if(!st) {
Lev Walkin8484ed82004-12-14 13:31:01 +00001147 st = (OCTET_STRING_t *)CALLOC(1, specs->struct_size);
Lev Walkin8484ed82004-12-14 13:31:01 +00001148 *sptr = (void *)st;
Lev Walkinc61f3862005-02-14 17:21:22 +00001149 if(!st) goto sta_failed;
1150 st_allocated = 1;
Lev Walkinbdaae772005-02-18 12:25:47 +00001151 } else {
1152 st_allocated = 0;
1153 }
Lev Walkinc61f3862005-02-14 17:21:22 +00001154 if(!st->buf) {
1155 /* This is separate from above section */
1156 st->buf = (uint8_t *)CALLOC(1, 1);
1157 if(!st->buf) {
1158 if(st_allocated) {
1159 *sptr = 0;
1160 goto stb_failed;
1161 } else {
1162 goto sta_failed;
1163 }
Lev Walkindc06f6b2004-10-20 15:50:55 +00001164 }
1165 }
1166
1167 /* Restore parsing context */
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001168 ctx = (asn_struct_ctx_t *)(((char *)*sptr) + specs->ctx_offset);
Lev Walkindc06f6b2004-10-20 15:50:55 +00001169
1170 return xer_decode_general(opt_codec_ctx, ctx, *sptr, xml_tag,
1171 buf_ptr, size, opt_unexpected_tag_decoder, body_receiver);
Lev Walkinc61f3862005-02-14 17:21:22 +00001172
1173stb_failed:
1174 FREEMEM(st);
1175sta_failed:
1176 rval.code = RC_FAIL;
1177 rval.consumed = 0;
1178 return rval;
Lev Walkindc06f6b2004-10-20 15:50:55 +00001179}
1180
1181/*
1182 * Decode OCTET STRING from the hexadecimal data.
1183 */
1184asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -07001185OCTET_STRING_decode_xer_hex(const asn_codec_ctx_t *opt_codec_ctx,
Lev Walkindc06f6b2004-10-20 15:50:55 +00001186 asn_TYPE_descriptor_t *td, void **sptr,
Lev Walkin8c3b8542005-03-10 18:52:02 +00001187 const char *opt_mname, const void *buf_ptr, size_t size) {
Lev Walkindc06f6b2004-10-20 15:50:55 +00001188 return OCTET_STRING__decode_xer(opt_codec_ctx, td, sptr, opt_mname,
1189 buf_ptr, size, 0, OCTET_STRING__convert_hexadecimal);
1190}
1191
1192/*
1193 * Decode OCTET STRING from the binary (0/1) data.
1194 */
1195asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -07001196OCTET_STRING_decode_xer_binary(const asn_codec_ctx_t *opt_codec_ctx,
Lev Walkindc06f6b2004-10-20 15:50:55 +00001197 asn_TYPE_descriptor_t *td, void **sptr,
Lev Walkin8c3b8542005-03-10 18:52:02 +00001198 const char *opt_mname, const void *buf_ptr, size_t size) {
Lev Walkindc06f6b2004-10-20 15:50:55 +00001199 return OCTET_STRING__decode_xer(opt_codec_ctx, td, sptr, opt_mname,
1200 buf_ptr, size, 0, OCTET_STRING__convert_binary);
1201}
1202
1203/*
1204 * Decode OCTET STRING from the string (ASCII/UTF-8) data.
1205 */
1206asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -07001207OCTET_STRING_decode_xer_utf8(const asn_codec_ctx_t *opt_codec_ctx,
Lev Walkindc06f6b2004-10-20 15:50:55 +00001208 asn_TYPE_descriptor_t *td, void **sptr,
Lev Walkin8c3b8542005-03-10 18:52:02 +00001209 const char *opt_mname, const void *buf_ptr, size_t size) {
Lev Walkindc06f6b2004-10-20 15:50:55 +00001210 return OCTET_STRING__decode_xer(opt_codec_ctx, td, sptr, opt_mname,
1211 buf_ptr, size,
1212 OCTET_STRING__handle_control_chars,
1213 OCTET_STRING__convert_entrefs);
1214}
1215
Lev Walkin0368c622017-09-18 00:33:16 -07001216#ifndef ASN_DISABLE_PER_SUPPORT
1217
Lev Walkin725883b2006-10-09 12:07:58 +00001218static int
Lev Walkin3a4689a2006-11-24 11:20:27 +00001219OCTET_STRING_per_get_characters(asn_per_data_t *po, uint8_t *buf,
1220 size_t units, unsigned int bpc, unsigned int unit_bits,
Lev Walkin494fb702017-08-07 20:07:00 -07001221 long lb, long ub, const asn_per_constraints_t *pc) {
Lev Walkin3a4689a2006-11-24 11:20:27 +00001222 uint8_t *end = buf + units * bpc;
Lev Walkin725883b2006-10-09 12:07:58 +00001223
1224 ASN_DEBUG("Expanding %d characters into (%ld..%ld):%d",
Lev Walkin3a4689a2006-11-24 11:20:27 +00001225 (int)units, lb, ub, unit_bits);
Lev Walkin725883b2006-10-09 12:07:58 +00001226
1227 /* X.691: 27.5.4 */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001228 if((unsigned long)ub <= ((unsigned long)2 << (unit_bits - 1))) {
Lev Walkin725883b2006-10-09 12:07:58 +00001229 /* Decode without translation */
1230 lb = 0;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001231 } else if(pc && pc->code2value) {
1232 if(unit_bits > 16)
1233 return 1; /* FATAL: can't have constrained
1234 * UniversalString with more than
1235 * 16 million code points */
1236 for(; buf < end; buf += bpc) {
Lev Walkin725883b2006-10-09 12:07:58 +00001237 int value;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001238 int code = per_get_few_bits(po, unit_bits);
Lev Walkin725883b2006-10-09 12:07:58 +00001239 if(code < 0) return -1; /* WMORE */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001240 value = pc->code2value(code);
Lev Walkin725883b2006-10-09 12:07:58 +00001241 if(value < 0) {
1242 ASN_DEBUG("Code %d (0x%02x) is"
1243 " not in map (%ld..%ld)",
1244 code, code, lb, ub);
1245 return 1; /* FATAL */
1246 }
Lev Walkin3a4689a2006-11-24 11:20:27 +00001247 switch(bpc) {
1248 case 1: *buf = value; break;
1249 case 2: buf[0] = value >> 8; buf[1] = value; break;
1250 case 4: buf[0] = value >> 24; buf[1] = value >> 16;
1251 buf[2] = value >> 8; buf[3] = value; break;
1252 }
Lev Walkin725883b2006-10-09 12:07:58 +00001253 }
1254 return 0;
1255 }
1256
Lev Walkin3a4689a2006-11-24 11:20:27 +00001257 /* Shortcut the no-op copying to the aligned structure */
1258 if(lb == 0 && (unit_bits == 8 * bpc)) {
1259 return per_get_many_bits(po, buf, 0, unit_bits * units);
1260 }
1261
1262 for(; buf < end; buf += bpc) {
Lev Walkin725883b2006-10-09 12:07:58 +00001263 int code = per_get_few_bits(po, unit_bits);
1264 int ch = code + lb;
1265 if(code < 0) return -1; /* WMORE */
1266 if(ch > ub) {
1267 ASN_DEBUG("Code %d is out of range (%ld..%ld)",
1268 ch, lb, ub);
1269 return 1; /* FATAL */
1270 }
Lev Walkin3a4689a2006-11-24 11:20:27 +00001271 switch(bpc) {
1272 case 1: *buf = ch; break;
1273 case 2: buf[0] = ch >> 8; buf[1] = ch; break;
1274 case 4: buf[0] = ch >> 24; buf[1] = ch >> 16;
1275 buf[2] = ch >> 8; buf[3] = ch; break;
1276 }
Lev Walkin725883b2006-10-09 12:07:58 +00001277 }
1278
1279 return 0;
1280}
1281
1282static int
Lev Walkin3a4689a2006-11-24 11:20:27 +00001283OCTET_STRING_per_put_characters(asn_per_outp_t *po, const uint8_t *buf,
1284 size_t units, unsigned int bpc, unsigned int unit_bits,
Lev Walkin494fb702017-08-07 20:07:00 -07001285 long lb, long ub, const asn_per_constraints_t *pc) {
Lev Walkin3a4689a2006-11-24 11:20:27 +00001286 const uint8_t *end = buf + units * bpc;
Lev Walkin725883b2006-10-09 12:07:58 +00001287
Lev Walkin3a4689a2006-11-24 11:20:27 +00001288 ASN_DEBUG("Squeezing %d characters into (%ld..%ld):%d (%d bpc)",
1289 (int)units, lb, ub, unit_bits, bpc);
Lev Walkin725883b2006-10-09 12:07:58 +00001290
1291 /* X.691: 27.5.4 */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001292 if((unsigned long)ub <= ((unsigned long)2 << (unit_bits - 1))) {
Lev Walkin725883b2006-10-09 12:07:58 +00001293 /* Encode as is */
1294 lb = 0;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001295 } else if(pc && pc->value2code) {
1296 for(; buf < end; buf += bpc) {
1297 int code;
1298 uint32_t value;
1299 switch(bpc) {
1300 case 1: value = *(const uint8_t *)buf; break;
1301 case 2: value = (buf[0] << 8) | buf[1]; break;
1302 case 4: value = (buf[0] << 24) | (buf[1] << 16)
1303 | (buf[2] << 8) | buf[3]; break;
1304 default: return -1;
1305 }
1306 code = pc->value2code(value);
Lev Walkin725883b2006-10-09 12:07:58 +00001307 if(code < 0) {
1308 ASN_DEBUG("Character %d (0x%02x) is"
1309 " not in map (%ld..%ld)",
1310 *buf, *buf, lb, ub);
1311 return -1;
1312 }
1313 if(per_put_few_bits(po, code, unit_bits))
1314 return -1;
1315 }
1316 }
1317
Lev Walkin3a4689a2006-11-24 11:20:27 +00001318 /* Shortcut the no-op copying to the aligned structure */
1319 if(lb == 0 && (unit_bits == 8 * bpc)) {
1320 return per_put_many_bits(po, buf, unit_bits * units);
1321 }
1322
1323 for(ub -= lb; buf < end; buf += bpc) {
1324 int ch;
1325 uint32_t value;
1326 switch(bpc) {
1327 case 1: value = *(const uint8_t *)buf; break;
1328 case 2: value = (buf[0] << 8) | buf[1]; break;
1329 case 4: value = (buf[0] << 24) | (buf[1] << 16)
1330 | (buf[2] << 8) | buf[3]; break;
1331 default: return -1;
1332 }
1333 ch = value - lb;
Lev Walkin725883b2006-10-09 12:07:58 +00001334 if(ch < 0 || ch > ub) {
1335 ASN_DEBUG("Character %d (0x%02x)"
1336 " is out of range (%ld..%ld)",
1337 *buf, *buf, lb, ub + lb);
1338 return -1;
1339 }
1340 if(per_put_few_bits(po, ch, unit_bits))
1341 return -1;
1342 }
1343
1344 return 0;
1345}
1346
Lev Walkin0368c622017-09-18 00:33:16 -07001347static asn_per_constraints_t asn_DEF_OCTET_STRING_constraints = {
1348 { APC_CONSTRAINED, 8, 8, 0, 255 },
1349 { APC_SEMI_CONSTRAINED, -1, -1, 0, 0 },
1350 0, 0
1351};
Lev Walkin62d76872017-08-05 22:49:42 -07001352
Lev Walkin59b176e2005-11-26 11:25:14 +00001353asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -07001354OCTET_STRING_decode_uper(const asn_codec_ctx_t *opt_codec_ctx,
Lev Walkin494fb702017-08-07 20:07:00 -07001355 asn_TYPE_descriptor_t *td,
1356 const asn_per_constraints_t *constraints, void **sptr,
1357 asn_per_data_t *pd) {
1358 asn_OCTET_STRING_specifics_t *specs = td->specifics
Lev Walkin59b176e2005-11-26 11:25:14 +00001359 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +08001360 : &asn_SPC_OCTET_STRING_specs;
Lev Walkin494fb702017-08-07 20:07:00 -07001361 const asn_per_constraints_t *pc =
1362 constraints ? constraints : td->per_constraints;
1363 const asn_per_constraint_t *cval;
1364 const asn_per_constraint_t *csiz;
Lev Walkin59b176e2005-11-26 11:25:14 +00001365 asn_dec_rval_t rval = { RC_OK, 0 };
1366 BIT_STRING_t *st = (BIT_STRING_t *)*sptr;
1367 ssize_t consumed_myself = 0;
1368 int repeat;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001369 enum {
1370 OS__BPC_BIT = 0,
1371 OS__BPC_CHAR = 1,
1372 OS__BPC_U16 = 2,
1373 OS__BPC_U32 = 4
1374 } bpc; /* Bytes per character */
1375 unsigned int unit_bits;
1376 unsigned int canonical_unit_bits;
Lev Walkin59b176e2005-11-26 11:25:14 +00001377
1378 (void)opt_codec_ctx;
1379
Lev Walkin3a4689a2006-11-24 11:20:27 +00001380 if(pc) {
1381 cval = &pc->value;
1382 csiz = &pc->size;
1383 } else {
1384 cval = &asn_DEF_OCTET_STRING_constraints.value;
1385 csiz = &asn_DEF_OCTET_STRING_constraints.size;
1386 }
1387
1388 switch(specs->subvariant) {
1389 default:
1390 case ASN_OSUBV_ANY:
1391 ASN_DEBUG("Unrecognized subvariant %d", specs->subvariant);
1392 RETURN(RC_FAIL);
1393 case ASN_OSUBV_BIT:
1394 canonical_unit_bits = unit_bits = 1;
1395 bpc = OS__BPC_BIT;
1396 break;
1397 case ASN_OSUBV_STR:
1398 canonical_unit_bits = unit_bits = 8;
1399 if(cval->flags & APC_CONSTRAINED)
1400 unit_bits = cval->range_bits;
1401 bpc = OS__BPC_CHAR;
1402 break;
1403 case ASN_OSUBV_U16:
1404 canonical_unit_bits = unit_bits = 16;
1405 if(cval->flags & APC_CONSTRAINED)
1406 unit_bits = cval->range_bits;
1407 bpc = OS__BPC_U16;
1408 break;
1409 case ASN_OSUBV_U32:
1410 canonical_unit_bits = unit_bits = 32;
1411 if(cval->flags & APC_CONSTRAINED)
1412 unit_bits = cval->range_bits;
1413 bpc = OS__BPC_U32;
1414 break;
1415 }
1416
Lev Walkin59b176e2005-11-26 11:25:14 +00001417 /*
1418 * Allocate the string.
1419 */
1420 if(!st) {
1421 st = (BIT_STRING_t *)(*sptr = CALLOC(1, specs->struct_size));
1422 if(!st) RETURN(RC_FAIL);
1423 }
1424
Lev Walkin725883b2006-10-09 12:07:58 +00001425 ASN_DEBUG("PER Decoding %s size %ld .. %ld bits %d",
Lev Walkin3a4689a2006-11-24 11:20:27 +00001426 csiz->flags & APC_EXTENSIBLE ? "extensible" : "non-extensible",
1427 csiz->lower_bound, csiz->upper_bound, csiz->effective_bits);
Lev Walkince676dd2005-12-20 22:34:55 +00001428
Lev Walkin3a4689a2006-11-24 11:20:27 +00001429 if(csiz->flags & APC_EXTENSIBLE) {
Lev Walkin59b176e2005-11-26 11:25:14 +00001430 int inext = per_get_few_bits(pd, 1);
Lev Walkin0a8aa602006-09-18 20:05:55 +00001431 if(inext < 0) RETURN(RC_WMORE);
Lev Walkin3a4689a2006-11-24 11:20:27 +00001432 if(inext) {
1433 csiz = &asn_DEF_OCTET_STRING_constraints.size;
1434 cval = &asn_DEF_OCTET_STRING_constraints.value;
1435 unit_bits = canonical_unit_bits;
1436 }
Lev Walkin59b176e2005-11-26 11:25:14 +00001437 }
1438
Lev Walkin3a4689a2006-11-24 11:20:27 +00001439 if(csiz->effective_bits >= 0) {
Lev Walkin59b176e2005-11-26 11:25:14 +00001440 FREEMEM(st->buf);
Lev Walkin3a4689a2006-11-24 11:20:27 +00001441 if(bpc) {
1442 st->size = csiz->upper_bound * bpc;
Lev Walkin59b176e2005-11-26 11:25:14 +00001443 } else {
Lev Walkin3a4689a2006-11-24 11:20:27 +00001444 st->size = (csiz->upper_bound + 7) >> 3;
Lev Walkin59b176e2005-11-26 11:25:14 +00001445 }
1446 st->buf = (uint8_t *)MALLOC(st->size + 1);
1447 if(!st->buf) { st->size = 0; RETURN(RC_FAIL); }
1448 }
1449
1450 /* X.691, #16.5: zero-length encoding */
1451 /* X.691, #16.6: short fixed length encoding (up to 2 octets) */
1452 /* X.691, #16.7: long fixed length encoding (up to 64K octets) */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001453 if(csiz->effective_bits == 0) {
Lev Walkin725883b2006-10-09 12:07:58 +00001454 int ret;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001455 if(bpc) {
1456 ASN_DEBUG("Encoding OCTET STRING size %ld",
1457 csiz->upper_bound);
1458 ret = OCTET_STRING_per_get_characters(pd, st->buf,
1459 csiz->upper_bound, bpc, unit_bits,
1460 cval->lower_bound, cval->upper_bound, pc);
Lev Walkin725883b2006-10-09 12:07:58 +00001461 if(ret > 0) RETURN(RC_FAIL);
1462 } else {
Lev Walkin3a4689a2006-11-24 11:20:27 +00001463 ASN_DEBUG("Encoding BIT STRING size %ld",
1464 csiz->upper_bound);
Lev Walkin725883b2006-10-09 12:07:58 +00001465 ret = per_get_many_bits(pd, st->buf, 0,
Lev Walkin3a4689a2006-11-24 11:20:27 +00001466 unit_bits * csiz->upper_bound);
Lev Walkin725883b2006-10-09 12:07:58 +00001467 }
Lev Walkin0a8aa602006-09-18 20:05:55 +00001468 if(ret < 0) RETURN(RC_WMORE);
Lev Walkin3a4689a2006-11-24 11:20:27 +00001469 consumed_myself += unit_bits * csiz->upper_bound;
Lev Walkin59b176e2005-11-26 11:25:14 +00001470 st->buf[st->size] = 0;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001471 if(bpc == 0) {
1472 int ubs = (csiz->upper_bound & 0x7);
1473 st->bits_unused = ubs ? 8 - ubs : 0;
1474 }
Lev Walkin59b176e2005-11-26 11:25:14 +00001475 RETURN(RC_OK);
1476 }
1477
1478 st->size = 0;
1479 do {
Lev Walkin3a4689a2006-11-24 11:20:27 +00001480 ssize_t raw_len;
Lev Walkin59b176e2005-11-26 11:25:14 +00001481 ssize_t len_bytes;
1482 ssize_t len_bits;
1483 void *p;
1484 int ret;
1485
1486 /* Get the PER length */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001487 raw_len = uper_get_length(pd, csiz->effective_bits, &repeat);
1488 if(raw_len < 0) RETURN(RC_WMORE);
1489 raw_len += csiz->lower_bound;
Lev Walkin59b176e2005-11-26 11:25:14 +00001490
Lev Walkin0a8aa602006-09-18 20:05:55 +00001491 ASN_DEBUG("Got PER length eb %ld, len %ld, %s (%s)",
Lev Walkin3a4689a2006-11-24 11:20:27 +00001492 (long)csiz->effective_bits, (long)raw_len,
Lev Walkin0a8aa602006-09-18 20:05:55 +00001493 repeat ? "repeat" : "once", td->name);
Lev Walkin3a4689a2006-11-24 11:20:27 +00001494 if(bpc) {
1495 len_bytes = raw_len * bpc;
1496 len_bits = len_bytes * unit_bits;
1497 } else {
1498 len_bits = raw_len;
Lev Walkin59b176e2005-11-26 11:25:14 +00001499 len_bytes = (len_bits + 7) >> 3;
1500 if(len_bits & 0x7)
1501 st->bits_unused = 8 - (len_bits & 0x7);
1502 /* len_bits be multiple of 16K if repeat is set */
Lev Walkin59b176e2005-11-26 11:25:14 +00001503 }
1504 p = REALLOC(st->buf, st->size + len_bytes + 1);
1505 if(!p) RETURN(RC_FAIL);
1506 st->buf = (uint8_t *)p;
1507
Lev Walkin3a4689a2006-11-24 11:20:27 +00001508 if(bpc) {
1509 ret = OCTET_STRING_per_get_characters(pd,
1510 &st->buf[st->size], raw_len, bpc, unit_bits,
1511 cval->lower_bound, cval->upper_bound, pc);
Lev Walkin725883b2006-10-09 12:07:58 +00001512 if(ret > 0) RETURN(RC_FAIL);
1513 } else {
1514 ret = per_get_many_bits(pd, &st->buf[st->size],
1515 0, len_bits);
1516 }
Lev Walkin0a8aa602006-09-18 20:05:55 +00001517 if(ret < 0) RETURN(RC_WMORE);
Lev Walkin59b176e2005-11-26 11:25:14 +00001518 st->size += len_bytes;
1519 } while(repeat);
1520 st->buf[st->size] = 0; /* nul-terminate */
1521
1522 return rval;
1523}
Lev Walkindc06f6b2004-10-20 15:50:55 +00001524
Lev Walkin523de9e2006-08-18 01:34:18 +00001525asn_enc_rval_t
1526OCTET_STRING_encode_uper(asn_TYPE_descriptor_t *td,
Lev Walkin494fb702017-08-07 20:07:00 -07001527 const asn_per_constraints_t *constraints, void *sptr,
1528 asn_per_outp_t *po) {
1529 asn_OCTET_STRING_specifics_t *specs = td->specifics
Lev Walkin523de9e2006-08-18 01:34:18 +00001530 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +08001531 : &asn_SPC_OCTET_STRING_specs;
Lev Walkin494fb702017-08-07 20:07:00 -07001532 const asn_per_constraints_t *pc = constraints ? constraints
Lev Walkin725883b2006-10-09 12:07:58 +00001533 : td->per_constraints;
Lev Walkin494fb702017-08-07 20:07:00 -07001534 const asn_per_constraint_t *cval;
1535 const asn_per_constraint_t *csiz;
Lev Walkin523de9e2006-08-18 01:34:18 +00001536 const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001537 asn_enc_rval_t er = { 0, 0, 0 };
Lev Walkin523de9e2006-08-18 01:34:18 +00001538 int inext = 0; /* Lies not within extension root */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001539 unsigned int unit_bits;
1540 unsigned int canonical_unit_bits;
1541 unsigned int sizeinunits;
Lev Walkin523de9e2006-08-18 01:34:18 +00001542 const uint8_t *buf;
1543 int ret;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001544 enum {
1545 OS__BPC_BIT = 0,
1546 OS__BPC_CHAR = 1,
1547 OS__BPC_U16 = 2,
1548 OS__BPC_U32 = 4
1549 } bpc; /* Bytes per character */
1550 int ct_extensible;
Lev Walkin523de9e2006-08-18 01:34:18 +00001551
Lev Walkin2ddd3082006-10-16 12:32:14 +00001552 if(!st || (!st->buf && st->size))
Lev Walkin7c1dc052016-03-14 03:08:15 -07001553 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001554
Lev Walkin3a4689a2006-11-24 11:20:27 +00001555 if(pc) {
1556 cval = &pc->value;
1557 csiz = &pc->size;
1558 } else {
1559 cval = &asn_DEF_OCTET_STRING_constraints.value;
1560 csiz = &asn_DEF_OCTET_STRING_constraints.size;
1561 }
1562 ct_extensible = csiz->flags & APC_EXTENSIBLE;
1563
1564 switch(specs->subvariant) {
1565 default:
1566 case ASN_OSUBV_ANY:
Lev Walkin7c1dc052016-03-14 03:08:15 -07001567 ASN__ENCODE_FAILED;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001568 case ASN_OSUBV_BIT:
1569 canonical_unit_bits = unit_bits = 1;
1570 bpc = OS__BPC_BIT;
1571 sizeinunits = st->size * 8 - (st->bits_unused & 0x07);
Lev Walkin523de9e2006-08-18 01:34:18 +00001572 ASN_DEBUG("BIT STRING of %d bytes, %d bits unused",
1573 sizeinunits, st->bits_unused);
Lev Walkin3a4689a2006-11-24 11:20:27 +00001574 break;
1575 case ASN_OSUBV_STR:
1576 canonical_unit_bits = unit_bits = 8;
1577 if(cval->flags & APC_CONSTRAINED)
1578 unit_bits = cval->range_bits;
1579 bpc = OS__BPC_CHAR;
1580 sizeinunits = st->size;
1581 break;
1582 case ASN_OSUBV_U16:
1583 canonical_unit_bits = unit_bits = 16;
1584 if(cval->flags & APC_CONSTRAINED)
1585 unit_bits = cval->range_bits;
1586 bpc = OS__BPC_U16;
Lev Walkin4ef01a22017-09-06 22:54:39 -07001587 sizeinunits = st->size >> 1;
1588 if(st->size & 1) {
1589 ASN_DEBUG("%s string size is not modulo 2", td->name);
1590 ASN__ENCODE_FAILED;
1591 }
Lev Walkin3a4689a2006-11-24 11:20:27 +00001592 break;
1593 case ASN_OSUBV_U32:
1594 canonical_unit_bits = unit_bits = 32;
1595 if(cval->flags & APC_CONSTRAINED)
1596 unit_bits = cval->range_bits;
1597 bpc = OS__BPC_U32;
Lev Walkin4ef01a22017-09-06 22:54:39 -07001598 sizeinunits = st->size >> 2;
1599 if(st->size & 3) {
1600 ASN_DEBUG("%s string size is not modulo 4", td->name);
1601 ASN__ENCODE_FAILED;
1602 }
Lev Walkin3a4689a2006-11-24 11:20:27 +00001603 break;
Lev Walkin725883b2006-10-09 12:07:58 +00001604 }
1605
Lev Walkin00918812006-09-18 21:19:32 +00001606 ASN_DEBUG("Encoding %s into %d units of %d bits"
Lev Walkin725883b2006-10-09 12:07:58 +00001607 " (%ld..%ld, effective %d)%s",
Lev Walkin00918812006-09-18 21:19:32 +00001608 td->name, sizeinunits, unit_bits,
Lev Walkin3a4689a2006-11-24 11:20:27 +00001609 csiz->lower_bound, csiz->upper_bound,
1610 csiz->effective_bits, ct_extensible ? " EXT" : "");
Lev Walkin523de9e2006-08-18 01:34:18 +00001611
Lev Walkind4c16732013-03-28 05:00:47 -07001612 /* Figure out whether size lies within PER visible constraint */
Lev Walkin523de9e2006-08-18 01:34:18 +00001613
Lev Walkin3a4689a2006-11-24 11:20:27 +00001614 if(csiz->effective_bits >= 0) {
1615 if((int)sizeinunits < csiz->lower_bound
1616 || (int)sizeinunits > csiz->upper_bound) {
Lev Walkin523de9e2006-08-18 01:34:18 +00001617 if(ct_extensible) {
Lev Walkin3a4689a2006-11-24 11:20:27 +00001618 cval = &asn_DEF_OCTET_STRING_constraints.value;
1619 csiz = &asn_DEF_OCTET_STRING_constraints.size;
1620 unit_bits = canonical_unit_bits;
Lev Walkin523de9e2006-08-18 01:34:18 +00001621 inext = 1;
Lev Walkin4ef01a22017-09-06 22:54:39 -07001622 } else {
Lev Walkin7c1dc052016-03-14 03:08:15 -07001623 ASN__ENCODE_FAILED;
Lev Walkin4ef01a22017-09-06 22:54:39 -07001624 }
Lev Walkin523de9e2006-08-18 01:34:18 +00001625 }
1626 } else {
1627 inext = 0;
1628 }
1629
1630 if(ct_extensible) {
1631 /* Declare whether length is [not] within extension root */
1632 if(per_put_few_bits(po, inext, 1))
Lev Walkin7c1dc052016-03-14 03:08:15 -07001633 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001634 }
1635
1636 /* X.691, #16.5: zero-length encoding */
1637 /* X.691, #16.6: short fixed length encoding (up to 2 octets) */
1638 /* X.691, #16.7: long fixed length encoding (up to 64K octets) */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001639 if(csiz->effective_bits >= 0) {
Lev Walkin24810022017-08-25 12:16:11 -07001640 ASN_DEBUG("Encoding %zu bytes (%ld), length in %d bits",
Lev Walkin3a4689a2006-11-24 11:20:27 +00001641 st->size, sizeinunits - csiz->lower_bound,
1642 csiz->effective_bits);
1643 ret = per_put_few_bits(po, sizeinunits - csiz->lower_bound,
1644 csiz->effective_bits);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001645 if(ret) ASN__ENCODE_FAILED;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001646 if(bpc) {
1647 ret = OCTET_STRING_per_put_characters(po, st->buf,
1648 sizeinunits, bpc, unit_bits,
1649 cval->lower_bound, cval->upper_bound, pc);
Lev Walkin725883b2006-10-09 12:07:58 +00001650 } else {
1651 ret = per_put_many_bits(po, st->buf,
1652 sizeinunits * unit_bits);
1653 }
Lev Walkin7c1dc052016-03-14 03:08:15 -07001654 if(ret) ASN__ENCODE_FAILED;
1655 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +00001656 }
1657
Lev Walkin24810022017-08-25 12:16:11 -07001658 ASN_DEBUG("Encoding %zu bytes", st->size);
Lev Walkin523de9e2006-08-18 01:34:18 +00001659
1660 if(sizeinunits == 0) {
1661 if(uper_put_length(po, 0))
Lev Walkin7c1dc052016-03-14 03:08:15 -07001662 ASN__ENCODE_FAILED;
1663 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +00001664 }
1665
1666 buf = st->buf;
1667 while(sizeinunits) {
1668 ssize_t maySave = uper_put_length(po, sizeinunits);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001669 if(maySave < 0) ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001670
Lev Walkin345e4c62006-10-19 02:44:08 +00001671 ASN_DEBUG("Encoding %ld of %ld",
1672 (long)maySave, (long)sizeinunits);
Lev Walkin523de9e2006-08-18 01:34:18 +00001673
Lev Walkin3a4689a2006-11-24 11:20:27 +00001674 if(bpc) {
1675 ret = OCTET_STRING_per_put_characters(po, buf,
1676 maySave, bpc, unit_bits,
1677 cval->lower_bound, cval->upper_bound, pc);
Lev Walkin725883b2006-10-09 12:07:58 +00001678 } else {
1679 ret = per_put_many_bits(po, buf, maySave * unit_bits);
1680 }
Lev Walkin7c1dc052016-03-14 03:08:15 -07001681 if(ret) ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001682
Lev Walkin3a4689a2006-11-24 11:20:27 +00001683 if(bpc)
1684 buf += maySave * bpc;
Lev Walkin523de9e2006-08-18 01:34:18 +00001685 else
Lev Walkin3a4689a2006-11-24 11:20:27 +00001686 buf += maySave >> 3;
Lev Walkin523de9e2006-08-18 01:34:18 +00001687 sizeinunits -= maySave;
1688 assert(!(maySave & 0x07) || !sizeinunits);
1689 }
1690
Lev Walkin7c1dc052016-03-14 03:08:15 -07001691 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +00001692}
1693
Lev Walkin62d76872017-08-05 22:49:42 -07001694#endif /* ASN_DISABLE_PER_SUPPORT */
1695
Lev Walkinf15320b2004-06-03 03:38:44 +00001696int
Lev Walkinde4825d2004-09-29 13:20:14 +00001697OCTET_STRING_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +00001698 asn_app_consume_bytes_f *cb, void *app_key) {
Wim Lewis59e8d282014-08-04 12:39:35 -07001699 const char * const h2c = "0123456789ABCDEF";
Lev Walkinc2346572004-08-11 09:07:36 +00001700 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +00001701 char scratch[16 * 3 + 4];
1702 char *p = scratch;
1703 uint8_t *buf;
1704 uint8_t *end;
1705 size_t i;
Lev Walkinf15320b2004-06-03 03:38:44 +00001706
Lev Walkind9bd7752004-06-05 08:17:50 +00001707 (void)td; /* Unused argument */
1708
Lev Walkin2ddd3082006-10-16 12:32:14 +00001709 if(!st || (!st->buf && st->size))
1710 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001711
1712 /*
1713 * Dump the contents of the buffer in hexadecimal.
1714 */
1715 buf = st->buf;
1716 end = buf + st->size;
1717 for(i = 0; buf < end; buf++, i++) {
1718 if(!(i % 16) && (i || st->size > 16)) {
Lev Walkin8e8078a2004-09-26 13:10:40 +00001719 if(cb(scratch, p - scratch, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +00001720 return -1;
Lev Walkin8e8078a2004-09-26 13:10:40 +00001721 _i_INDENT(1);
Lev Walkinf15320b2004-06-03 03:38:44 +00001722 p = scratch;
1723 }
1724 *p++ = h2c[(*buf >> 4) & 0x0F];
1725 *p++ = h2c[*buf & 0x0F];
Lev Walkina9cc46e2004-09-22 16:06:28 +00001726 *p++ = 0x20;
Lev Walkinf15320b2004-06-03 03:38:44 +00001727 }
1728
Lev Walkincc6a9102004-09-23 22:06:26 +00001729 if(p > scratch) {
1730 p--; /* Remove the tail space */
Lev Walkin8e8078a2004-09-26 13:10:40 +00001731 if(cb(scratch, p - scratch, app_key) < 0)
Lev Walkincc6a9102004-09-23 22:06:26 +00001732 return -1;
1733 }
1734
1735 return 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001736}
1737
1738int
Lev Walkindc06f6b2004-10-20 15:50:55 +00001739OCTET_STRING_print_utf8(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +00001740 int ilevel, asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +00001741 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +00001742
Lev Walkind9bd7752004-06-05 08:17:50 +00001743 (void)td; /* Unused argument */
1744 (void)ilevel; /* Unused argument */
1745
Lev Walkin2ddd3082006-10-16 12:32:14 +00001746 if(st && (st->buf || !st->size)) {
Lev Walkin8e8078a2004-09-26 13:10:40 +00001747 return (cb(st->buf, st->size, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001748 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +00001749 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001750 }
1751}
1752
1753void
Lev Walkinf6853ce2017-08-11 00:50:27 -07001754OCTET_STRING_free(const asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkin8d99d7b2017-08-25 01:06:00 -07001755 enum asn_struct_free_method method) {
1756 OCTET_STRING_t *st = (OCTET_STRING_t *)sptr;
Lev Walkin034ee192016-03-14 02:28:33 -07001757 asn_OCTET_STRING_specifics_t *specs;
1758 asn_struct_ctx_t *ctx;
Lev Walkinb54577a2004-11-08 10:47:12 +00001759 struct _stack *stck;
Lev Walkinf15320b2004-06-03 03:38:44 +00001760
1761 if(!td || !st)
1762 return;
1763
Lev Walkin034ee192016-03-14 02:28:33 -07001764 specs = td->specifics
1765 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +08001766 : &asn_SPC_OCTET_STRING_specs;
Lev Walkin034ee192016-03-14 02:28:33 -07001767 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
1768
Lev Walkinf15320b2004-06-03 03:38:44 +00001769 ASN_DEBUG("Freeing %s as OCTET STRING", td->name);
1770
1771 if(st->buf) {
1772 FREEMEM(st->buf);
Lev Walkin2ddd3082006-10-16 12:32:14 +00001773 st->buf = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001774 }
1775
1776 /*
1777 * Remove decode-time stack.
1778 */
Lev Walkinb54577a2004-11-08 10:47:12 +00001779 stck = (struct _stack *)ctx->ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +00001780 if(stck) {
1781 while(stck->tail) {
1782 struct _stack_el *sel = stck->tail;
1783 stck->tail = sel->prev;
1784 FREEMEM(sel);
1785 }
1786 FREEMEM(stck);
1787 }
1788
Lev Walkin8d99d7b2017-08-25 01:06:00 -07001789 switch(method) {
1790 case ASFM_FREE_EVERYTHING:
1791 FREEMEM(sptr);
1792 break;
1793 case ASFM_FREE_UNDERLYING:
1794 break;
1795 case ASFM_FREE_UNDERLYING_AND_RESET:
1796 memset(sptr, 0,
1797 ((asn_OCTET_STRING_specifics_t *)(td->specifics))->struct_size);
1798 break;
1799 }
Lev Walkinf15320b2004-06-03 03:38:44 +00001800}
1801
1802/*
1803 * Conversion routines.
1804 */
1805int
1806OCTET_STRING_fromBuf(OCTET_STRING_t *st, const char *str, int len) {
1807 void *buf;
1808
1809 if(st == 0 || (str == 0 && len)) {
1810 errno = EINVAL;
1811 return -1;
1812 }
1813
1814 /*
1815 * Clear the OCTET STRING.
1816 */
1817 if(str == NULL) {
Lev Walkin7b284812005-12-17 11:43:25 +00001818 FREEMEM(st->buf);
1819 st->buf = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001820 st->size = 0;
1821 return 0;
1822 }
1823
1824 /* Determine the original string size, if not explicitly given */
1825 if(len < 0)
1826 len = strlen(str);
1827
1828 /* Allocate and fill the memory */
1829 buf = MALLOC(len + 1);
Lev Walkin7b284812005-12-17 11:43:25 +00001830 if(buf == NULL)
Lev Walkinf15320b2004-06-03 03:38:44 +00001831 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +00001832
1833 memcpy(buf, str, len);
Lev Walkin7b284812005-12-17 11:43:25 +00001834 ((uint8_t *)buf)[len] = '\0'; /* Couldn't use memcpy(len+1)! */
1835 FREEMEM(st->buf);
1836 st->buf = (uint8_t *)buf;
1837 st->size = len;
Lev Walkinf15320b2004-06-03 03:38:44 +00001838
1839 return 0;
1840}
1841
1842OCTET_STRING_t *
Lev Walkinbbd93252004-10-12 05:57:23 +00001843OCTET_STRING_new_fromBuf(asn_TYPE_descriptor_t *td, const char *str, int len) {
1844 asn_OCTET_STRING_specifics_t *specs = td->specifics
Lev Walkindc06f6b2004-10-20 15:50:55 +00001845 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +08001846 : &asn_SPC_OCTET_STRING_specs;
Lev Walkinf15320b2004-06-03 03:38:44 +00001847 OCTET_STRING_t *st;
1848
Lev Walkinbbd93252004-10-12 05:57:23 +00001849 st = (OCTET_STRING_t *)CALLOC(1, specs->struct_size);
Lev Walkinf15320b2004-06-03 03:38:44 +00001850 if(st && str && OCTET_STRING_fromBuf(st, str, len)) {
Lev Walkin419f6752006-09-13 04:02:00 +00001851 FREEMEM(st);
Lev Walkinf15320b2004-06-03 03:38:44 +00001852 st = NULL;
1853 }
1854
1855 return st;
1856}
1857
Lev Walkincd2f48e2017-08-10 02:14:59 -07001858/*
1859 * Lexicographically compare the common prefix of both strings,
1860 * and if it is the same return -1 for the smallest string.
1861 */
1862int
1863OCTET_STRING_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
1864 const void *bptr) {
1865 const OCTET_STRING_t *a = aptr;
1866 const OCTET_STRING_t *b = bptr;
1867
1868 (void)td;
1869
1870 if(a && b) {
1871 size_t common_prefix_size = a->size <= b->size ? a->size : b->size;
1872 int ret = memcmp(a->buf, b->buf, common_prefix_size);
1873 if(ret == 0) {
1874 /* Figure out which string with equal prefixes is longer. */
1875 if(a->size < b->size) {
1876 return -1;
1877 } else if(a->size > b->size) {
1878 return 1;
1879 } else {
Lev Walkin86526e32017-09-06 23:16:13 -07001880 asn_OCTET_STRING_specifics_t *specs = td->specifics;
1881 if(specs && specs->subvariant == ASN_OSUBV_BIT) {
1882 const BIT_STRING_t *ba = aptr;
1883 const BIT_STRING_t *bb = bptr;
1884 if(ba->bits_unused > bb->bits_unused) {
1885 return -1;
1886 } else if(ba->bits_unused < bb->bits_unused) {
1887 return 1;
1888 } else {
1889 return 0;
1890 }
1891 }
Lev Walkincd2f48e2017-08-10 02:14:59 -07001892 return 0;
1893 }
1894 } else {
1895 return ret;
1896 }
1897 } else if(!a && !b) {
1898 return 0;
1899 } else if(!a) {
1900 return -1;
1901 } else {
1902 return 1;
1903 }
1904
1905}
1906