blob: 844c6f243691cd77a99f6ad01c9000fbd59951a0 [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001/*-
Lev Walkin0a8aa602006-09-18 20:05:55 +00002 * Copyright (c) 2003, 2004, 2005, 2006 Lev Walkin <vlm@lionet.info>.
Lev Walkin75b1bef2005-04-25 19:38:21 +00003 * All rights reserved.
Lev Walkinf15320b2004-06-03 03:38:44 +00004 * Redistribution and modifications are permitted subject to BSD license.
5 */
Lev Walkina9cc46e2004-09-22 16:06:28 +00006#include <asn_internal.h>
Lev Walkinf15320b2004-06-03 03:38:44 +00007#include <OCTET_STRING.h>
Lev Walkinbbd93252004-10-12 05:57:23 +00008#include <BIT_STRING.h> /* for .bits_unused member */
Lev Walkinf15320b2004-06-03 03:38:44 +00009#include <errno.h>
10
11/*
12 * OCTET STRING basic type description.
13 */
Wim Lewis18c2ec92014-07-29 11:30:10 -070014static const ber_tlv_tag_t asn_DEF_OCTET_STRING_tags[] = {
Lev Walkinf15320b2004-06-03 03:38:44 +000015 (ASN_TAG_CLASS_UNIVERSAL | (4 << 2))
16};
Bi-Ruei, Chiu1fa31c92016-05-16 13:50:09 +080017asn_OCTET_STRING_specifics_t asn_SPC_OCTET_STRING_specs = {
Lev Walkinbbd93252004-10-12 05:57:23 +000018 sizeof(OCTET_STRING_t),
19 offsetof(OCTET_STRING_t, _asn_ctx),
Lev Walkin3a4689a2006-11-24 11:20:27 +000020 ASN_OSUBV_STR
Lev Walkinbbd93252004-10-12 05:57:23 +000021};
Vasil Velichkovd82f77b2016-07-19 19:34:09 +030022static asn_per_constraints_t asn_DEF_OCTET_STRING_constraints = {
Lev Walkin3a4689a2006-11-24 11:20:27 +000023 { APC_CONSTRAINED, 8, 8, 0, 255 },
24 { APC_SEMI_CONSTRAINED, -1, -1, 0, 0 },
25 0, 0
Lev Walkin59b176e2005-11-26 11:25:14 +000026};
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080027asn_TYPE_operation_t asn_OP_OCTET_STRING = {
Lev Walkina9cc46e2004-09-22 16:06:28 +000028 OCTET_STRING_free,
Lev Walkincd2f48e2017-08-10 02:14:59 -070029 OCTET_STRING_print, /* OCTET STRING generally means a non-ascii sequence */
30 OCTET_STRING_compare,
Lev Walkinf15320b2004-06-03 03:38:44 +000031 OCTET_STRING_decode_ber,
32 OCTET_STRING_encode_der,
Lev Walkindc06f6b2004-10-20 15:50:55 +000033 OCTET_STRING_decode_xer_hex,
Lev Walkina9cc46e2004-09-22 16:06:28 +000034 OCTET_STRING_encode_xer,
Lev Walkincc159472017-07-06 08:26:36 -070035#ifdef ASN_DISABLE_OER_SUPPORT
36 0,
37 0,
38#else
Lev Walkin2535bd52017-08-27 23:45:56 -070039 OCTET_STRING_decode_oer,
40 OCTET_STRING_encode_oer,
Lev Walkincc159472017-07-06 08:26:36 -070041#endif /* ASN_DISABLE_OER_SUPPORT */
Lev Walkinb33425f2017-07-14 14:59:52 +040042#ifdef ASN_DISABLE_PER_SUPPORT
43 0,
44 0,
45#else
46 OCTET_STRING_decode_uper, /* Unaligned PER decoder */
47 OCTET_STRING_encode_uper, /* Unaligned PER encoder */
48#endif /* ASN_DISABLE_PER_SUPPORT */
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080049 0 /* Use generic outmost tag fetcher */
50};
51asn_TYPE_descriptor_t asn_DEF_OCTET_STRING = {
52 "OCTET STRING", /* Canonical name */
53 "OCTET_STRING", /* XML tag name */
54 &asn_OP_OCTET_STRING,
55 asn_generic_no_constraint,
Lev Walkinde4825d2004-09-29 13:20:14 +000056 asn_DEF_OCTET_STRING_tags,
57 sizeof(asn_DEF_OCTET_STRING_tags)
58 / sizeof(asn_DEF_OCTET_STRING_tags[0]),
59 asn_DEF_OCTET_STRING_tags, /* Same as above */
60 sizeof(asn_DEF_OCTET_STRING_tags)
61 / sizeof(asn_DEF_OCTET_STRING_tags[0]),
Lev Walkin76780762017-07-07 10:07:30 -070062 0, /* No OER visible constraints */
Lev Walkin59b176e2005-11-26 11:25:14 +000063 0, /* No PER visible constraints */
Lev Walkin449f8322004-08-20 13:23:42 +000064 0, 0, /* No members */
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +080065 &asn_SPC_OCTET_STRING_specs
Lev Walkinf15320b2004-06-03 03:38:44 +000066};
67
Lev Walkincc6a9102004-09-23 22:06:26 +000068#undef _CH_PHASE
69#undef NEXT_PHASE
70#undef PREV_PHASE
Lev Walkin75b1bef2005-04-25 19:38:21 +000071#define _CH_PHASE(ctx, inc) do { \
72 if(ctx->phase == 0) \
73 ctx->context = 0; \
74 ctx->phase += inc; \
Lev Walkinf15320b2004-06-03 03:38:44 +000075 } while(0)
76#define NEXT_PHASE(ctx) _CH_PHASE(ctx, +1)
77#define PREV_PHASE(ctx) _CH_PHASE(ctx, -1)
78
Lev Walkincc6a9102004-09-23 22:06:26 +000079#undef ADVANCE
Lev Walkin75b1bef2005-04-25 19:38:21 +000080#define ADVANCE(num_bytes) do { \
81 size_t num = (num_bytes); \
82 buf_ptr = ((const char *)buf_ptr) + num; \
83 size -= num; \
84 consumed_myself += num; \
Lev Walkinf15320b2004-06-03 03:38:44 +000085 } while(0)
86
Lev Walkincc6a9102004-09-23 22:06:26 +000087#undef RETURN
Lev Walkin75b1bef2005-04-25 19:38:21 +000088#define RETURN(_code) do { \
Lev Walkin59b176e2005-11-26 11:25:14 +000089 asn_dec_rval_t tmprval; \
90 tmprval.code = _code; \
91 tmprval.consumed = consumed_myself; \
92 return tmprval; \
Lev Walkinf15320b2004-06-03 03:38:44 +000093 } while(0)
94
Lev Walkincc6a9102004-09-23 22:06:26 +000095#undef APPEND
Lev Walkind9bd7752004-06-05 08:17:50 +000096#define APPEND(bufptr, bufsize) do { \
Lev Walkin75b1bef2005-04-25 19:38:21 +000097 size_t _bs = (bufsize); /* Append size */ \
98 size_t _ns = ctx->context; /* Allocated now */ \
99 size_t _es = st->size + _bs; /* Expected size */ \
100 /* int is really a typeof(st->size): */ \
101 if((int)_es < 0) RETURN(RC_FAIL); \
102 if(_ns <= _es) { \
Lev Walkind9bd7752004-06-05 08:17:50 +0000103 void *ptr; \
Lev Walkin188ed2c2004-09-13 08:31:01 +0000104 /* Be nice and round to the memory allocator */ \
Lev Walkin75b1bef2005-04-25 19:38:21 +0000105 do { _ns = _ns ? _ns << 1 : 16; } \
106 while(_ns <= _es); \
107 /* int is really a typeof(st->size): */ \
108 if((int)_ns < 0) RETURN(RC_FAIL); \
Lev Walkind9bd7752004-06-05 08:17:50 +0000109 ptr = REALLOC(st->buf, _ns); \
110 if(ptr) { \
Lev Walkinc2346572004-08-11 09:07:36 +0000111 st->buf = (uint8_t *)ptr; \
Lev Walkin75b1bef2005-04-25 19:38:21 +0000112 ctx->context = _ns; \
Lev Walkind9bd7752004-06-05 08:17:50 +0000113 } else { \
114 RETURN(RC_FAIL); \
115 } \
Lev Walkin7f85ef42005-07-02 20:24:27 +0000116 ASN_DEBUG("Reallocating into %ld", (long)_ns); \
Lev Walkind9bd7752004-06-05 08:17:50 +0000117 } \
Lev Walkin8d127872004-09-04 04:44:50 +0000118 memcpy(st->buf + st->size, bufptr, _bs); \
Lev Walkind9bd7752004-06-05 08:17:50 +0000119 /* Convenient nul-termination */ \
Lev Walkin75b1bef2005-04-25 19:38:21 +0000120 st->buf[_es] = '\0'; \
121 st->size = _es; \
Lev Walkinf15320b2004-06-03 03:38:44 +0000122 } while(0)
123
124/*
125 * The main reason why ASN.1 is still alive is that too much time and effort
126 * is necessary for learning it more or less adequately, thus creating a gut
127 * necessity to demonstrate that aquired skill everywhere afterwards.
128 * No, I am not going to explain what the following stuff is.
129 */
130struct _stack_el {
Lev Walkin494fb702017-08-07 20:07:00 -0700131 ber_tlv_len_t left; /* What's left to read (or -1) */
132 ber_tlv_len_t got; /* What was actually processed */
Lev Walkin24bf3f62017-08-10 17:58:43 -0700133 unsigned cont_level; /* Depth of subcontainment */
Lev Walkin494fb702017-08-07 20:07:00 -0700134 int want_nulls; /* Want null "end of content" octets? */
135 int bits_chopped; /* Flag in BIT STRING mode */
136 ber_tlv_tag_t tag; /* For debugging purposes */
137 struct _stack_el *prev;
138 struct _stack_el *next;
Lev Walkinf15320b2004-06-03 03:38:44 +0000139};
140struct _stack {
141 struct _stack_el *tail;
142 struct _stack_el *cur_ptr;
143};
144
145static struct _stack_el *
Lev Walkin3990ba62004-09-24 20:57:41 +0000146OS__add_stack_el(struct _stack *st) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000147 struct _stack_el *nel;
148
Lev Walkin188ed2c2004-09-13 08:31:01 +0000149 /*
150 * Reuse the old stack frame or allocate a new one.
151 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000152 if(st->cur_ptr && st->cur_ptr->next) {
153 nel = st->cur_ptr->next;
Lev Walkinf15320b2004-06-03 03:38:44 +0000154 nel->bits_chopped = 0;
Lev Walkin5c915992004-09-27 20:54:06 +0000155 nel->got = 0;
156 /* Retain the nel->cont_level, it's correct. */
Lev Walkinf15320b2004-06-03 03:38:44 +0000157 } else {
Lev Walkin814cca72004-12-15 23:23:53 +0000158 nel = (struct _stack_el *)CALLOC(1, sizeof(struct _stack_el));
Lev Walkinf15320b2004-06-03 03:38:44 +0000159 if(nel == NULL)
160 return NULL;
161
162 if(st->tail) {
Lev Walkin188ed2c2004-09-13 08:31:01 +0000163 /* Increase a subcontainment depth */
164 nel->cont_level = st->tail->cont_level + 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000165 st->tail->next = nel;
166 }
167 nel->prev = st->tail;
168 st->tail = nel;
169 }
170
171 st->cur_ptr = nel;
172
173 return nel;
174}
175
176static struct _stack *
johvik28268d92017-05-09 10:27:52 +0200177_new_stack(void) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000178 return (struct _stack *)CALLOC(1, sizeof(struct _stack));
Lev Walkinf15320b2004-06-03 03:38:44 +0000179}
180
181/*
182 * Decode OCTET STRING type.
183 */
Lev Walkindc06f6b2004-10-20 15:50:55 +0000184asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -0700185OCTET_STRING_decode_ber(const asn_codec_ctx_t *opt_codec_ctx,
Lev Walkinde4825d2004-09-29 13:20:14 +0000186 asn_TYPE_descriptor_t *td,
Lev Walkin59b176e2005-11-26 11:25:14 +0000187 void **sptr, const void *buf_ptr, size_t size, int tag_mode) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000188 asn_OCTET_STRING_specifics_t *specs = td->specifics
Lev Walkindc06f6b2004-10-20 15:50:55 +0000189 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +0800190 : &asn_SPC_OCTET_STRING_specs;
Lev Walkin59b176e2005-11-26 11:25:14 +0000191 BIT_STRING_t *st = (BIT_STRING_t *)*sptr;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000192 asn_dec_rval_t rval;
Lev Walkinde4825d2004-09-29 13:20:14 +0000193 asn_struct_ctx_t *ctx;
Lev Walkinf15320b2004-06-03 03:38:44 +0000194 ssize_t consumed_myself = 0;
Lev Walkinbbd93252004-10-12 05:57:23 +0000195 struct _stack *stck; /* Expectations stack structure */
Lev Walkin5c915992004-09-27 20:54:06 +0000196 struct _stack_el *sel = 0; /* Stack element */
Lev Walkinf15320b2004-06-03 03:38:44 +0000197 int tlv_constr;
Lev Walkin3a4689a2006-11-24 11:20:27 +0000198 enum asn_OS_Subvariant type_variant = specs->subvariant;
Lev Walkinf15320b2004-06-03 03:38:44 +0000199
Lev Walkincc6a9102004-09-23 22:06:26 +0000200 ASN_DEBUG("Decoding %s as %s (frame %ld)",
201 td->name,
Lev Walkin3a4689a2006-11-24 11:20:27 +0000202 (type_variant == ASN_OSUBV_STR) ?
Lev Walkinbbd93252004-10-12 05:57:23 +0000203 "OCTET STRING" : "OS-SpecialCase",
Lev Walkincc6a9102004-09-23 22:06:26 +0000204 (long)size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000205
206 /*
207 * Create the string if does not exist.
208 */
209 if(st == NULL) {
Lev Walkin59b176e2005-11-26 11:25:14 +0000210 st = (BIT_STRING_t *)(*sptr = CALLOC(1, specs->struct_size));
211 if(st == NULL) RETURN(RC_FAIL);
Lev Walkinf15320b2004-06-03 03:38:44 +0000212 }
213
214 /* Restore parsing context */
Lev Walkinaa61a0f2014-01-13 23:08:47 -0800215 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000216
217 switch(ctx->phase) {
218 case 0:
219 /*
220 * Check tags.
221 */
Lev Walkinde4825d2004-09-29 13:20:14 +0000222 rval = ber_check_tags(opt_codec_ctx, td, ctx,
Lev Walkin8e8078a2004-09-26 13:10:40 +0000223 buf_ptr, size, tag_mode, -1,
Lev Walkinf15320b2004-06-03 03:38:44 +0000224 &ctx->left, &tlv_constr);
Lev Walkin443d2512004-10-05 06:35:31 +0000225 if(rval.code != RC_OK)
226 return rval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000227
Lev Walkinf15320b2004-06-03 03:38:44 +0000228 if(tlv_constr) {
229 /*
230 * Complex operation, requires stack of expectations.
231 */
232 ctx->ptr = _new_stack();
Lev Walkinb02145b2017-06-28 08:52:07 -0700233 if(!ctx->ptr) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000234 RETURN(RC_FAIL);
235 }
236 } else {
237 /*
238 * Jump into stackless primitive decoding.
239 */
240 _CH_PHASE(ctx, 3);
Lev Walkin3a4689a2006-11-24 11:20:27 +0000241 if(type_variant == ASN_OSUBV_ANY && tag_mode != 1)
Lev Walkin188ed2c2004-09-13 08:31:01 +0000242 APPEND(buf_ptr, rval.consumed);
Lev Walkinf15320b2004-06-03 03:38:44 +0000243 ADVANCE(rval.consumed);
244 goto phase3;
245 }
246
Lev Walkinf15320b2004-06-03 03:38:44 +0000247 NEXT_PHASE(ctx);
248 /* Fall through */
249 case 1:
250 phase1:
251 /*
252 * Fill the stack with expectations.
253 */
Lev Walkin814cca72004-12-15 23:23:53 +0000254 stck = (struct _stack *)ctx->ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000255 sel = stck->cur_ptr;
256 do {
257 ber_tlv_tag_t tlv_tag;
258 ber_tlv_len_t tlv_len;
Lev Walkin188ed2c2004-09-13 08:31:01 +0000259 ber_tlv_tag_t expected_tag;
Lev Walkin5c915992004-09-27 20:54:06 +0000260 ssize_t tl, ll, tlvl;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000261 /* This one works even if (sel->left == -1) */
Simo Sorcecd943c22016-07-25 04:39:40 -0400262 size_t Left = ((!sel||(size_t)sel->left >= size)
263 ?size:(size_t)sel->left);
Lev Walkinf15320b2004-06-03 03:38:44 +0000264
Lev Walkin3990ba62004-09-24 20:57:41 +0000265
Lev Walkinabf68892004-10-26 10:12:14 +0000266 ASN_DEBUG("%p, s->l=%ld, s->wn=%ld, s->g=%ld\n", sel,
Lev Walkin1e3ccbb2004-10-26 10:56:10 +0000267 (long)(sel?sel->left:0),
268 (long)(sel?sel->want_nulls:0),
269 (long)(sel?sel->got:0)
Lev Walkin5c915992004-09-27 20:54:06 +0000270 );
271 if(sel && sel->left <= 0 && sel->want_nulls == 0) {
272 if(sel->prev) {
273 struct _stack_el *prev = sel->prev;
274 if(prev->left != -1) {
275 if(prev->left < sel->got)
276 RETURN(RC_FAIL);
277 prev->left -= sel->got;
278 }
279 prev->got += sel->got;
280 sel = stck->cur_ptr = prev;
281 if(!sel) break;
282 tlv_constr = 1;
283 continue;
284 } else {
285 sel = stck->cur_ptr = 0;
286 break; /* Nothing to wait */
287 }
288 }
289
Lev Walkin3990ba62004-09-24 20:57:41 +0000290 tl = ber_fetch_tag(buf_ptr, Left, &tlv_tag);
Lev Walkinabf68892004-10-26 10:12:14 +0000291 ASN_DEBUG("fetch tag(size=%ld,L=%ld), %sstack, left=%ld, wn=%ld, tl=%ld",
292 (long)size, (long)Left, sel?"":"!",
Lev Walkin1e3ccbb2004-10-26 10:56:10 +0000293 (long)(sel?sel->left:0),
294 (long)(sel?sel->want_nulls:0),
Lev Walkinabf68892004-10-26 10:12:14 +0000295 (long)tl);
Lev Walkinf15320b2004-06-03 03:38:44 +0000296 switch(tl) {
297 case -1: RETURN(RC_FAIL);
298 case 0: RETURN(RC_WMORE);
299 }
300
301 tlv_constr = BER_TLV_CONSTRUCTED(buf_ptr);
302
303 ll = ber_fetch_length(tlv_constr,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000304 (const char *)buf_ptr + tl,Left - tl,&tlv_len);
Lev Walkina6a926a2005-03-02 01:54:28 +0000305 ASN_DEBUG("Got tag=%s, tc=%d, left=%ld, tl=%ld, len=%ld, ll=%ld",
Lev Walkin8d127872004-09-04 04:44:50 +0000306 ber_tlv_tag_string(tlv_tag), tlv_constr,
Lev Walkina6a926a2005-03-02 01:54:28 +0000307 (long)Left, (long)tl, (long)tlv_len, (long)ll);
Lev Walkinf15320b2004-06-03 03:38:44 +0000308 switch(ll) {
309 case -1: RETURN(RC_FAIL);
310 case 0: RETURN(RC_WMORE);
311 }
312
Lev Walkin8d127872004-09-04 04:44:50 +0000313 if(sel && sel->want_nulls
Lev Walkin8c3b8542005-03-10 18:52:02 +0000314 && ((const uint8_t *)buf_ptr)[0] == 0
315 && ((const uint8_t *)buf_ptr)[1] == 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000316 {
Lev Walkin8d127872004-09-04 04:44:50 +0000317
318 ASN_DEBUG("Eat EOC; wn=%d--", sel->want_nulls);
319
Lev Walkin3a4689a2006-11-24 11:20:27 +0000320 if(type_variant == ASN_OSUBV_ANY
Lev Walkin07f388c2004-10-11 11:43:08 +0000321 && (tag_mode != 1 || sel->cont_level))
322 APPEND("\0\0", 2);
Lev Walkin5c915992004-09-27 20:54:06 +0000323
324 ADVANCE(2);
325 sel->got += 2;
326 if(sel->left != -1) {
327 sel->left -= 2; /* assert(sel->left >= 2) */
328 }
329
Lev Walkinf15320b2004-06-03 03:38:44 +0000330 sel->want_nulls--;
331 if(sel->want_nulls == 0) {
332 /* Move to the next expectation */
Lev Walkin5c915992004-09-27 20:54:06 +0000333 sel->left = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000334 tlv_constr = 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000335 }
Lev Walkin8d127872004-09-04 04:44:50 +0000336
337 continue;
Lev Walkin188ed2c2004-09-13 08:31:01 +0000338 }
339
340 /*
341 * Set up expected tags,
342 * depending on ASN.1 type being decoded.
343 */
Lev Walkinbbd93252004-10-12 05:57:23 +0000344 switch(type_variant) {
Lev Walkin3a4689a2006-11-24 11:20:27 +0000345 case ASN_OSUBV_BIT:
Lev Walkin188ed2c2004-09-13 08:31:01 +0000346 /* X.690: 8.6.4.1, NOTE 2 */
347 /* Fall through */
Lev Walkin3a4689a2006-11-24 11:20:27 +0000348 case ASN_OSUBV_STR:
Lev Walkin188ed2c2004-09-13 08:31:01 +0000349 default:
350 if(sel) {
Lev Walkin24bf3f62017-08-10 17:58:43 -0700351 unsigned level = sel->cont_level;
Lev Walkin188ed2c2004-09-13 08:31:01 +0000352 if(level < td->all_tags_count) {
353 expected_tag = td->all_tags[level];
354 break;
355 } else if(td->all_tags_count) {
356 expected_tag = td->all_tags
357 [td->all_tags_count - 1];
358 break;
359 }
360 /* else, Fall through */
361 }
362 /* Fall through */
Lev Walkin3a4689a2006-11-24 11:20:27 +0000363 case ASN_OSUBV_ANY:
Lev Walkin188ed2c2004-09-13 08:31:01 +0000364 expected_tag = tlv_tag;
365 break;
366 }
367
368
369 if(tlv_tag != expected_tag) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000370 char buf[2][32];
371 ber_tlv_tag_snprint(tlv_tag,
372 buf[0], sizeof(buf[0]));
373 ber_tlv_tag_snprint(td->tags[td->tags_count-1],
374 buf[1], sizeof(buf[1]));
375 ASN_DEBUG("Tag does not match expectation: %s != %s",
376 buf[0], buf[1]);
377 RETURN(RC_FAIL);
378 }
379
Lev Walkinde4825d2004-09-29 13:20:14 +0000380 tlvl = tl + ll; /* Combined length of T and L encoding */
381 if((tlv_len + tlvl) < 0) {
382 /* tlv_len value is too big */
383 ASN_DEBUG("TLV encoding + length (%ld) is too big",
384 (long)tlv_len);
385 RETURN(RC_FAIL);
386 }
387
Lev Walkinf15320b2004-06-03 03:38:44 +0000388 /*
389 * Append a new expectation.
390 */
Lev Walkin3990ba62004-09-24 20:57:41 +0000391 sel = OS__add_stack_el(stck);
Lev Walkin5c915992004-09-27 20:54:06 +0000392 if(!sel) RETURN(RC_FAIL);
Lev Walkinf15320b2004-06-03 03:38:44 +0000393
Lev Walkin5c915992004-09-27 20:54:06 +0000394 sel->tag = tlv_tag;
395
396 sel->want_nulls = (tlv_len==-1);
397 if(sel->prev && sel->prev->left != -1) {
398 /* Check that the parent frame is big enough */
399 if(sel->prev->left < tlvl + (tlv_len==-1?0:tlv_len))
400 RETURN(RC_FAIL);
401 if(tlv_len == -1)
402 sel->left = sel->prev->left - tlvl;
403 else
404 sel->left = tlv_len;
405 } else {
406 sel->left = tlv_len;
407 }
Lev Walkin3a4689a2006-11-24 11:20:27 +0000408 if(type_variant == ASN_OSUBV_ANY
Lev Walkin07f388c2004-10-11 11:43:08 +0000409 && (tag_mode != 1 || sel->cont_level))
410 APPEND(buf_ptr, tlvl);
Lev Walkin5c915992004-09-27 20:54:06 +0000411 sel->got += tlvl;
412 ADVANCE(tlvl);
413
Lev Walkin24bf3f62017-08-10 17:58:43 -0700414 ASN_DEBUG("+EXPECT2 got=%ld left=%ld, wn=%d, clvl=%u",
Lev Walkinabf68892004-10-26 10:12:14 +0000415 (long)sel->got, (long)sel->left,
416 sel->want_nulls, sel->cont_level);
Lev Walkin5c915992004-09-27 20:54:06 +0000417
Lev Walkinf15320b2004-06-03 03:38:44 +0000418 } while(tlv_constr);
419 if(sel == NULL) {
420 /* Finished operation, "phase out" */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000421 ASN_DEBUG("Phase out");
Lev Walkinf15320b2004-06-03 03:38:44 +0000422 _CH_PHASE(ctx, +3);
423 break;
424 }
425
426 NEXT_PHASE(ctx);
427 /* Fall through */
428 case 2:
Lev Walkin814cca72004-12-15 23:23:53 +0000429 stck = (struct _stack *)ctx->ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000430 sel = stck->cur_ptr;
Lev Walkin5c915992004-09-27 20:54:06 +0000431 ASN_DEBUG("Phase 2: Need %ld bytes, size=%ld, alrg=%ld, wn=%d",
432 (long)sel->left, (long)size, (long)sel->got,
433 sel->want_nulls);
Lev Walkinf15320b2004-06-03 03:38:44 +0000434 {
435 ber_tlv_len_t len;
436
437 assert(sel->left >= 0);
438
Lev Walkinec1ffd42004-08-18 04:53:32 +0000439 len = ((ber_tlv_len_t)size < sel->left)
440 ? (ber_tlv_len_t)size : sel->left;
Lev Walkinf15320b2004-06-03 03:38:44 +0000441 if(len > 0) {
Lev Walkin3a4689a2006-11-24 11:20:27 +0000442 if(type_variant == ASN_OSUBV_BIT
Lev Walkin188ed2c2004-09-13 08:31:01 +0000443 && sel->bits_chopped == 0) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000444 /* Put the unused-bits-octet away */
Lev Walkin8c3b8542005-03-10 18:52:02 +0000445 st->bits_unused = *(const uint8_t *)buf_ptr;
446 APPEND(((const char *)buf_ptr+1), (len - 1));
Lev Walkinf15320b2004-06-03 03:38:44 +0000447 sel->bits_chopped = 1;
448 } else {
449 APPEND(buf_ptr, len);
450 }
451 ADVANCE(len);
452 sel->left -= len;
Lev Walkin5c915992004-09-27 20:54:06 +0000453 sel->got += len;
Lev Walkinf15320b2004-06-03 03:38:44 +0000454 }
455
Lev Walkin5c915992004-09-27 20:54:06 +0000456 if(sel->left) {
457 ASN_DEBUG("OS left %ld, size = %ld, wn=%d\n",
458 (long)sel->left, (long)size, sel->want_nulls);
Lev Walkinf15320b2004-06-03 03:38:44 +0000459 RETURN(RC_WMORE);
Lev Walkinf15320b2004-06-03 03:38:44 +0000460 }
Lev Walkin5c915992004-09-27 20:54:06 +0000461
462 PREV_PHASE(ctx);
463 goto phase1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000464 }
465 break;
466 case 3:
467 phase3:
468 /*
469 * Primitive form, no stack required.
470 */
Lev Walkinbbd93252004-10-12 05:57:23 +0000471 assert(ctx->left >= 0);
472
Lev Walkind9bd7752004-06-05 08:17:50 +0000473 if(size < (size_t)ctx->left) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000474 if(!size) RETURN(RC_WMORE);
Lev Walkin3a4689a2006-11-24 11:20:27 +0000475 if(type_variant == ASN_OSUBV_BIT && !ctx->context) {
Lev Walkin8c3b8542005-03-10 18:52:02 +0000476 st->bits_unused = *(const uint8_t *)buf_ptr;
Lev Walkinbbd93252004-10-12 05:57:23 +0000477 ctx->left--;
478 ADVANCE(1);
479 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000480 APPEND(buf_ptr, size);
Lev Walkin75b1bef2005-04-25 19:38:21 +0000481 assert(ctx->context > 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000482 ctx->left -= size;
483 ADVANCE(size);
484 RETURN(RC_WMORE);
485 } else {
Lev Walkin3a4689a2006-11-24 11:20:27 +0000486 if(type_variant == ASN_OSUBV_BIT
Lev Walkin75b1bef2005-04-25 19:38:21 +0000487 && !ctx->context && ctx->left) {
Lev Walkin8c3b8542005-03-10 18:52:02 +0000488 st->bits_unused = *(const uint8_t *)buf_ptr;
Lev Walkinbbd93252004-10-12 05:57:23 +0000489 ctx->left--;
490 ADVANCE(1);
491 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000492 APPEND(buf_ptr, ctx->left);
493 ADVANCE(ctx->left);
494 ctx->left = 0;
495
496 NEXT_PHASE(ctx);
497 }
498 break;
499 }
500
Lev Walkin5c915992004-09-27 20:54:06 +0000501 if(sel) {
502 ASN_DEBUG("3sel p=%p, wn=%d, l=%ld, g=%ld, size=%ld",
503 sel->prev, sel->want_nulls,
504 (long)sel->left, (long)sel->got, (long)size);
505 if(sel->prev || sel->want_nulls > 1 || sel->left > 0) {
506 RETURN(RC_WMORE);
507 }
508 }
509
Lev Walkinf15320b2004-06-03 03:38:44 +0000510 /*
511 * BIT STRING-specific processing.
512 */
Lev Walkin3a4689a2006-11-24 11:20:27 +0000513 if(type_variant == ASN_OSUBV_BIT && st->size) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000514 /* Finalize BIT STRING: zero out unused bits. */
Lev Walkinbbd93252004-10-12 05:57:23 +0000515 st->buf[st->size-1] &= 0xff << st->bits_unused;
Lev Walkinf15320b2004-06-03 03:38:44 +0000516 }
517
Lev Walkinabf68892004-10-26 10:12:14 +0000518 ASN_DEBUG("Took %ld bytes to encode %s: [%s]:%ld",
519 (long)consumed_myself, td->name,
Lev Walkin3a4689a2006-11-24 11:20:27 +0000520 (type_variant == ASN_OSUBV_STR) ? (char *)st->buf : "<data>",
Lev Walkinabf68892004-10-26 10:12:14 +0000521 (long)st->size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000522
Lev Walkinf15320b2004-06-03 03:38:44 +0000523
Lev Walkin5c915992004-09-27 20:54:06 +0000524 RETURN(RC_OK);
Lev Walkinf15320b2004-06-03 03:38:44 +0000525}
526
527/*
528 * Encode OCTET STRING type using DER.
529 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000530asn_enc_rval_t
Lev Walkinbbd93252004-10-12 05:57:23 +0000531OCTET_STRING_encode_der(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000532 int tag_mode, ber_tlv_tag_t tag,
533 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000534 asn_enc_rval_t er;
535 asn_OCTET_STRING_specifics_t *specs = td->specifics
Lev Walkindc06f6b2004-10-20 15:50:55 +0000536 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +0800537 : &asn_SPC_OCTET_STRING_specs;
Lev Walkinbbd93252004-10-12 05:57:23 +0000538 BIT_STRING_t *st = (BIT_STRING_t *)sptr;
Lev Walkin3a4689a2006-11-24 11:20:27 +0000539 enum asn_OS_Subvariant type_variant = specs->subvariant;
Lev Walkinbbd93252004-10-12 05:57:23 +0000540 int fix_last_byte = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000541
542 ASN_DEBUG("%s %s as OCTET STRING",
Lev Walkin1f670c12004-09-02 12:57:25 +0000543 cb?"Estimating":"Encoding", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000544
545 /*
Lev Walkinbbd93252004-10-12 05:57:23 +0000546 * Write tags.
Lev Walkinf15320b2004-06-03 03:38:44 +0000547 */
Lev Walkin3a4689a2006-11-24 11:20:27 +0000548 if(type_variant != ASN_OSUBV_ANY || tag_mode == 1) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000549 er.encoded = der_write_tags(td,
Lev Walkin3a4689a2006-11-24 11:20:27 +0000550 (type_variant == ASN_OSUBV_BIT) + st->size,
551 tag_mode, type_variant == ASN_OSUBV_ANY, tag,
552 cb, app_key);
Lev Walkinbbd93252004-10-12 05:57:23 +0000553 if(er.encoded == -1) {
554 er.failed_type = td;
555 er.structure_ptr = sptr;
556 return er;
Lev Walkinf15320b2004-06-03 03:38:44 +0000557 }
Lev Walkinbbd93252004-10-12 05:57:23 +0000558 } else {
559 /* Disallow: [<tag>] IMPLICIT ANY */
Lev Walkin3a4689a2006-11-24 11:20:27 +0000560 assert(type_variant != ASN_OSUBV_ANY || tag_mode != -1);
Lev Walkinbbd93252004-10-12 05:57:23 +0000561 er.encoded = 0;
562 }
563
564 if(!cb) {
Lev Walkin3a4689a2006-11-24 11:20:27 +0000565 er.encoded += (type_variant == ASN_OSUBV_BIT) + st->size;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700566 ASN__ENCODED_OK(er);
Lev Walkinf15320b2004-06-03 03:38:44 +0000567 }
568
Lev Walkin07f388c2004-10-11 11:43:08 +0000569 /*
Lev Walkinbbd93252004-10-12 05:57:23 +0000570 * Prepare to deal with the last octet of BIT STRING.
Lev Walkin07f388c2004-10-11 11:43:08 +0000571 */
Lev Walkin3a4689a2006-11-24 11:20:27 +0000572 if(type_variant == ASN_OSUBV_BIT) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000573 uint8_t b = st->bits_unused & 0x07;
574 if(b && st->size) fix_last_byte = 1;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700575 ASN__CALLBACK(&b, 1);
Lev Walkinbbd93252004-10-12 05:57:23 +0000576 er.encoded++;
Lev Walkinf15320b2004-06-03 03:38:44 +0000577 }
578
Lev Walkinbbd93252004-10-12 05:57:23 +0000579 /* Invoke callback for the main part of the buffer */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700580 ASN__CALLBACK(st->buf, st->size - fix_last_byte);
Lev Walkinf15320b2004-06-03 03:38:44 +0000581
Lev Walkinbbd93252004-10-12 05:57:23 +0000582 /* The last octet should be stripped off the unused bits */
583 if(fix_last_byte) {
584 uint8_t b = st->buf[st->size-1] & (0xff << st->bits_unused);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700585 ASN__CALLBACK(&b, 1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000586 }
587
Lev Walkinbbd93252004-10-12 05:57:23 +0000588 er.encoded += st->size;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700589 ASN__ENCODED_OK(er);
Lev Walkinbbd93252004-10-12 05:57:23 +0000590cb_failed:
Lev Walkin7c1dc052016-03-14 03:08:15 -0700591 ASN__ENCODE_FAILED;
Lev Walkinf15320b2004-06-03 03:38:44 +0000592}
593
Lev Walkina9cc46e2004-09-22 16:06:28 +0000594asn_enc_rval_t
Lev Walkinde4825d2004-09-29 13:20:14 +0000595OCTET_STRING_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000596 int ilevel, enum xer_encoder_flags_e flags,
597 asn_app_consume_bytes_f *cb, void *app_key) {
Wim Lewis59e8d282014-08-04 12:39:35 -0700598 const char * const h2c = "0123456789ABCDEF";
Lev Walkina9cc46e2004-09-22 16:06:28 +0000599 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
600 asn_enc_rval_t er;
601 char scratch[16 * 3 + 4];
602 char *p = scratch;
603 uint8_t *buf;
604 uint8_t *end;
605 size_t i;
606
Lev Walkin2ddd3082006-10-16 12:32:14 +0000607 if(!st || (!st->buf && st->size))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700608 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000609
610 er.encoded = 0;
611
612 /*
613 * Dump the contents of the buffer in hexadecimal.
614 */
615 buf = st->buf;
616 end = buf + st->size;
617 if(flags & XER_F_CANONICAL) {
618 char *scend = scratch + (sizeof(scratch) - 2);
619 for(; buf < end; buf++) {
620 if(p >= scend) {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700621 ASN__CALLBACK(scratch, p - scratch);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000622 er.encoded += p - scratch;
623 p = scratch;
624 }
625 *p++ = h2c[(*buf >> 4) & 0x0F];
626 *p++ = h2c[*buf & 0x0F];
627 }
Lev Walkincc6a9102004-09-23 22:06:26 +0000628
Lev Walkin7c1dc052016-03-14 03:08:15 -0700629 ASN__CALLBACK(scratch, p-scratch); /* Dump the rest */
Lev Walkincc6a9102004-09-23 22:06:26 +0000630 er.encoded += p - scratch;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000631 } else {
632 for(i = 0; buf < end; buf++, i++) {
633 if(!(i % 16) && (i || st->size > 16)) {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700634 ASN__CALLBACK(scratch, p-scratch);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000635 er.encoded += (p-scratch);
636 p = scratch;
Lev Walkinc6cac8e2016-03-14 02:57:07 -0700637 ASN__TEXT_INDENT(1, ilevel);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000638 }
639 *p++ = h2c[(*buf >> 4) & 0x0F];
640 *p++ = h2c[*buf & 0x0F];
641 *p++ = 0x20;
642 }
Lev Walkincc6a9102004-09-23 22:06:26 +0000643 if(p - scratch) {
644 p--; /* Remove the tail space */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700645 ASN__CALLBACK(scratch, p-scratch); /* Dump the rest */
Lev Walkincc6a9102004-09-23 22:06:26 +0000646 er.encoded += p - scratch;
647 if(st->size > 16)
Lev Walkinc6cac8e2016-03-14 02:57:07 -0700648 ASN__TEXT_INDENT(1, ilevel-1);
Lev Walkincc6a9102004-09-23 22:06:26 +0000649 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000650 }
651
Lev Walkin7c1dc052016-03-14 03:08:15 -0700652 ASN__ENCODED_OK(er);
Lev Walkin942fd082004-10-03 09:13:02 +0000653cb_failed:
Lev Walkin7c1dc052016-03-14 03:08:15 -0700654 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000655}
656
Wim Lewis18c2ec92014-07-29 11:30:10 -0700657static const struct OCTET_STRING__xer_escape_table_s {
658 const char *string;
Lev Walkin9ca81892004-10-03 10:54:25 +0000659 int size;
660} OCTET_STRING__xer_escape_table[] = {
661#define OSXET(s) { s, sizeof(s) - 1 }
662 OSXET("\074\156\165\154\057\076"), /* <nul/> */
663 OSXET("\074\163\157\150\057\076"), /* <soh/> */
664 OSXET("\074\163\164\170\057\076"), /* <stx/> */
665 OSXET("\074\145\164\170\057\076"), /* <etx/> */
666 OSXET("\074\145\157\164\057\076"), /* <eot/> */
667 OSXET("\074\145\156\161\057\076"), /* <enq/> */
668 OSXET("\074\141\143\153\057\076"), /* <ack/> */
669 OSXET("\074\142\145\154\057\076"), /* <bel/> */
670 OSXET("\074\142\163\057\076"), /* <bs/> */
671 OSXET("\011"), /* \t */
672 OSXET("\012"), /* \n */
673 OSXET("\074\166\164\057\076"), /* <vt/> */
674 OSXET("\074\146\146\057\076"), /* <ff/> */
675 OSXET("\015"), /* \r */
676 OSXET("\074\163\157\057\076"), /* <so/> */
677 OSXET("\074\163\151\057\076"), /* <si/> */
678 OSXET("\074\144\154\145\057\076"), /* <dle/> */
679 OSXET("\074\144\143\061\057\076"), /* <de1/> */
680 OSXET("\074\144\143\062\057\076"), /* <de2/> */
681 OSXET("\074\144\143\063\057\076"), /* <de3/> */
682 OSXET("\074\144\143\064\057\076"), /* <de4/> */
683 OSXET("\074\156\141\153\057\076"), /* <nak/> */
684 OSXET("\074\163\171\156\057\076"), /* <syn/> */
685 OSXET("\074\145\164\142\057\076"), /* <etb/> */
686 OSXET("\074\143\141\156\057\076"), /* <can/> */
687 OSXET("\074\145\155\057\076"), /* <em/> */
688 OSXET("\074\163\165\142\057\076"), /* <sub/> */
689 OSXET("\074\145\163\143\057\076"), /* <esc/> */
690 OSXET("\074\151\163\064\057\076"), /* <is4/> */
691 OSXET("\074\151\163\063\057\076"), /* <is3/> */
692 OSXET("\074\151\163\062\057\076"), /* <is2/> */
693 OSXET("\074\151\163\061\057\076"), /* <is1/> */
694 { 0, 0 }, /* " " */
695 { 0, 0 }, /* ! */
696 { 0, 0 }, /* \" */
697 { 0, 0 }, /* # */
698 { 0, 0 }, /* $ */
699 { 0, 0 }, /* % */
700 OSXET("\046\141\155\160\073"), /* &amp; */
701 { 0, 0 }, /* ' */
702 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, /* ()*+,-./ */
703 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, /* 01234567 */
704 {0,0},{0,0},{0,0},{0,0}, /* 89:; */
705 OSXET("\046\154\164\073"), /* &lt; */
706 { 0, 0 }, /* = */
707 OSXET("\046\147\164\073"), /* &gt; */
708};
709
Lev Walkindc06f6b2004-10-20 15:50:55 +0000710static int
Lev Walkin0fab1a62005-03-09 22:19:25 +0000711OS__check_escaped_control_char(const void *buf, int size) {
Lev Walkindc06f6b2004-10-20 15:50:55 +0000712 size_t i;
713 /*
714 * Inefficient algorithm which translates the escape sequences
715 * defined above into characters. Returns -1 if not found.
716 * TODO: replace by a faster algorithm (bsearch(), hash or
717 * nested table lookups).
718 */
719 for(i = 0; i < 32 /* Don't spend time on the bottom half */; i++) {
Wim Lewis18c2ec92014-07-29 11:30:10 -0700720 const struct OCTET_STRING__xer_escape_table_s *el;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000721 el = &OCTET_STRING__xer_escape_table[i];
722 if(el->size == size && memcmp(buf, el->string, size) == 0)
723 return i;
724 }
725 return -1;
726}
727
728static int
Lev Walkin0fab1a62005-03-09 22:19:25 +0000729OCTET_STRING__handle_control_chars(void *struct_ptr, const void *chunk_buf, size_t chunk_size) {
Lev Walkindc06f6b2004-10-20 15:50:55 +0000730 /*
731 * This might be one of the escape sequences
732 * for control characters. Check it out.
733 * #11.15.5
734 */
735 int control_char = OS__check_escaped_control_char(chunk_buf,chunk_size);
736 if(control_char >= 0) {
737 OCTET_STRING_t *st = (OCTET_STRING_t *)struct_ptr;
738 void *p = REALLOC(st->buf, st->size + 2);
739 if(p) {
740 st->buf = (uint8_t *)p;
741 st->buf[st->size++] = control_char;
742 st->buf[st->size] = '\0'; /* nul-termination */
743 return 0;
744 }
745 }
746
747 return -1; /* No, it's not */
748}
749
Lev Walkina9cc46e2004-09-22 16:06:28 +0000750asn_enc_rval_t
Lev Walkindc06f6b2004-10-20 15:50:55 +0000751OCTET_STRING_encode_xer_utf8(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000752 int ilevel, enum xer_encoder_flags_e flags,
753 asn_app_consume_bytes_f *cb, void *app_key) {
754 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
755 asn_enc_rval_t er;
Lev Walkin9ca81892004-10-03 10:54:25 +0000756 uint8_t *buf, *end;
757 uint8_t *ss; /* Sequence start */
758 ssize_t encoded_len = 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000759
760 (void)ilevel; /* Unused argument */
761 (void)flags; /* Unused argument */
762
Lev Walkin2ddd3082006-10-16 12:32:14 +0000763 if(!st || (!st->buf && st->size))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700764 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000765
Lev Walkin9ca81892004-10-03 10:54:25 +0000766 buf = st->buf;
767 end = buf + st->size;
768 for(ss = buf; buf < end; buf++) {
Lev Walkin443d2512004-10-05 06:35:31 +0000769 unsigned int ch = *buf;
Lev Walkin9ca81892004-10-03 10:54:25 +0000770 int s_len; /* Special encoding sequence length */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000771
Lev Walkin9ca81892004-10-03 10:54:25 +0000772 /*
773 * Escape certain characters: X.680/11.15
774 */
775 if(ch < sizeof(OCTET_STRING__xer_escape_table)
776 /sizeof(OCTET_STRING__xer_escape_table[0])
777 && (s_len = OCTET_STRING__xer_escape_table[ch].size)) {
778 if(((buf - ss) && cb(ss, buf - ss, app_key) < 0)
779 || cb(OCTET_STRING__xer_escape_table[ch].string, s_len,
780 app_key) < 0)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700781 ASN__ENCODE_FAILED;
Lev Walkin9ca81892004-10-03 10:54:25 +0000782 encoded_len += (buf - ss) + s_len;
783 ss = buf + 1;
784 }
785 }
786
787 encoded_len += (buf - ss);
788 if((buf - ss) && cb(ss, buf - ss, app_key) < 0)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700789 ASN__ENCODE_FAILED;
Lev Walkin9ca81892004-10-03 10:54:25 +0000790
791 er.encoded = encoded_len;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700792 ASN__ENCODED_OK(er);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000793}
794
Lev Walkindc06f6b2004-10-20 15:50:55 +0000795/*
796 * Convert from hexadecimal format (cstring): "AB CD EF"
797 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000798static ssize_t OCTET_STRING__convert_hexadecimal(void *sptr, const void *chunk_buf, size_t chunk_size, int have_more) {
Lev Walkindc06f6b2004-10-20 15:50:55 +0000799 OCTET_STRING_t *st = (OCTET_STRING_t *)sptr;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000800 const char *chunk_stop = (const char *)chunk_buf;
801 const char *p = chunk_stop;
802 const char *pend = p + chunk_size;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000803 unsigned int clv = 0;
804 int half = 0; /* Half bit */
805 uint8_t *buf;
806
807 /* Reallocate buffer according to high cap estimation */
Lev Walkin494fb702017-08-07 20:07:00 -0700808 size_t new_size = st->size + (chunk_size + 1) / 2;
809 void *nptr = REALLOC(st->buf, new_size + 1);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000810 if(!nptr) return -1;
811 st->buf = (uint8_t *)nptr;
812 buf = st->buf + st->size;
813
814 /*
815 * If something like " a b c " appears here, the " a b":3 will be
816 * converted, and the rest skipped. That is, unless buf_size is greater
817 * than chunk_size, then it'll be equivalent to "ABC0".
818 */
819 for(; p < pend; p++) {
Lev Walkin0fab1a62005-03-09 22:19:25 +0000820 int ch = *(const unsigned char *)p;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000821 switch(ch) {
822 case 0x09: case 0x0a: case 0x0c: case 0x0d:
823 case 0x20:
824 /* Ignore whitespace */
825 continue;
826 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: /*01234*/
827 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: /*56789*/
828 clv = (clv << 4) + (ch - 0x30);
829 break;
830 case 0x41: case 0x42: case 0x43: /* ABC */
831 case 0x44: case 0x45: case 0x46: /* DEF */
Lev Walkin3ae21bd2005-02-21 14:43:48 +0000832 clv = (clv << 4) + (ch - 0x41 + 10);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000833 break;
834 case 0x61: case 0x62: case 0x63: /* abc */
835 case 0x64: case 0x65: case 0x66: /* def */
Lev Walkin3ae21bd2005-02-21 14:43:48 +0000836 clv = (clv << 4) + (ch - 0x61 + 10);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000837 break;
838 default:
839 *buf = 0; /* JIC */
840 return -1;
841 }
842 if(half++) {
843 half = 0;
844 *buf++ = clv;
845 chunk_stop = p + 1;
846 }
847 }
848
849 /*
850 * Check partial decoding.
851 */
852 if(half) {
853 if(have_more) {
854 /*
855 * Partial specification is fine,
856 * because no more more PXER_TEXT data is available.
857 */
858 *buf++ = clv << 4;
859 chunk_stop = p;
860 }
861 } else {
862 chunk_stop = p;
863 }
864
865 st->size = buf - st->buf; /* Adjust the buffer size */
Lev Walkin494fb702017-08-07 20:07:00 -0700866 assert(st->size <= new_size);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000867 st->buf[st->size] = 0; /* Courtesy termination */
868
Lev Walkin0fab1a62005-03-09 22:19:25 +0000869 return (chunk_stop - (const char *)chunk_buf); /* Converted size */
Lev Walkindc06f6b2004-10-20 15:50:55 +0000870}
871
872/*
873 * Convert from binary format: "00101011101"
874 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000875static ssize_t OCTET_STRING__convert_binary(void *sptr, const void *chunk_buf, size_t chunk_size, int have_more) {
Lev Walkindc06f6b2004-10-20 15:50:55 +0000876 BIT_STRING_t *st = (BIT_STRING_t *)sptr;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000877 const char *p = (const char *)chunk_buf;
878 const char *pend = p + chunk_size;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000879 int bits_unused = st->bits_unused & 0x7;
880 uint8_t *buf;
881
882 /* Reallocate buffer according to high cap estimation */
Lev Walkin494fb702017-08-07 20:07:00 -0700883 size_t new_size = st->size + (chunk_size + 7) / 8;
884 void *nptr = REALLOC(st->buf, new_size + 1);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000885 if(!nptr) return -1;
886 st->buf = (uint8_t *)nptr;
887 buf = st->buf + st->size;
888
889 (void)have_more;
890
891 if(bits_unused == 0)
892 bits_unused = 8;
893 else if(st->size)
894 buf--;
895
896 /*
897 * Convert series of 0 and 1 into the octet string.
898 */
899 for(; p < pend; p++) {
Lev Walkin0fab1a62005-03-09 22:19:25 +0000900 int ch = *(const unsigned char *)p;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000901 switch(ch) {
902 case 0x09: case 0x0a: case 0x0c: case 0x0d:
903 case 0x20:
904 /* Ignore whitespace */
905 break;
906 case 0x30:
907 case 0x31:
908 if(bits_unused-- <= 0) {
909 *++buf = 0; /* Clean the cell */
910 bits_unused = 7;
911 }
912 *buf |= (ch&1) << bits_unused;
913 break;
914 default:
915 st->bits_unused = bits_unused;
916 return -1;
917 }
918 }
919
920 if(bits_unused == 8) {
921 st->size = buf - st->buf;
922 st->bits_unused = 0;
923 } else {
924 st->size = buf - st->buf + 1;
925 st->bits_unused = bits_unused;
926 }
927
Lev Walkin494fb702017-08-07 20:07:00 -0700928 assert(st->size <= new_size);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000929 st->buf[st->size] = 0; /* Courtesy termination */
930
931 return chunk_size; /* Converted in full */
932}
933
934/*
935 * Something like strtod(), but with stricter rules.
936 */
937static int
Lev Walkin0fab1a62005-03-09 22:19:25 +0000938OS__strtoent(int base, const char *buf, const char *end, int32_t *ret_value) {
Lev Walkin33700162004-10-26 09:03:31 +0000939 int32_t val = 0;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000940 const char *p;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000941
942 for(p = buf; p < end; p++) {
943 int ch = *p;
Lev Walkinb0f3db62005-07-03 05:30:15 +0000944
945 /* Strange huge value */
946 if((val * base + base) < 0)
947 return -1;
948
Lev Walkindc06f6b2004-10-20 15:50:55 +0000949 switch(ch) {
950 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: /*01234*/
951 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: /*56789*/
952 val = val * base + (ch - 0x30);
953 break;
954 case 0x41: case 0x42: case 0x43: /* ABC */
955 case 0x44: case 0x45: case 0x46: /* DEF */
Lev Walkin3ae21bd2005-02-21 14:43:48 +0000956 val = val * base + (ch - 0x41 + 10);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000957 break;
958 case 0x61: case 0x62: case 0x63: /* abc */
959 case 0x64: case 0x65: case 0x66: /* def */
Lev Walkin3ae21bd2005-02-21 14:43:48 +0000960 val = val * base + (ch - 0x61 + 10);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000961 break;
962 case 0x3b: /* ';' */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000963 *ret_value = val;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000964 return (p - buf) + 1;
965 default:
966 return -1; /* Character set error */
967 }
968 }
969
Lev Walkinb0f3db62005-07-03 05:30:15 +0000970 *ret_value = -1;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000971 return (p - buf);
972}
973
974/*
975 * Convert from the plain UTF-8 format, expanding entity references: "2 &lt; 3"
976 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000977static ssize_t OCTET_STRING__convert_entrefs(void *sptr, const void *chunk_buf, size_t chunk_size, int have_more) {
Lev Walkindc06f6b2004-10-20 15:50:55 +0000978 OCTET_STRING_t *st = (OCTET_STRING_t *)sptr;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000979 const char *p = (const char *)chunk_buf;
980 const char *pend = p + chunk_size;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000981 uint8_t *buf;
982
983 /* Reallocate buffer */
Lev Walkin494fb702017-08-07 20:07:00 -0700984 size_t new_size = st->size + chunk_size;
985 void *nptr = REALLOC(st->buf, new_size + 1);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000986 if(!nptr) return -1;
987 st->buf = (uint8_t *)nptr;
988 buf = st->buf + st->size;
989
990 /*
991 * Convert series of 0 and 1 into the octet string.
992 */
993 for(; p < pend; p++) {
Lev Walkin0fab1a62005-03-09 22:19:25 +0000994 int ch = *(const unsigned char *)p;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000995 int len; /* Length of the rest of the chunk */
996
997 if(ch != 0x26 /* '&' */) {
998 *buf++ = ch;
999 continue; /* That was easy... */
1000 }
1001
1002 /*
1003 * Process entity reference.
1004 */
Lev Walkin0fab1a62005-03-09 22:19:25 +00001005 len = chunk_size - (p - (const char *)chunk_buf);
Lev Walkindc06f6b2004-10-20 15:50:55 +00001006 if(len == 1 /* "&" */) goto want_more;
1007 if(p[1] == 0x23 /* '#' */) {
Lev Walkin0fab1a62005-03-09 22:19:25 +00001008 const char *pval; /* Pointer to start of digits */
Lev Walkinbc0dce32006-07-27 09:56:13 +00001009 int32_t val = 0; /* Entity reference value */
Lev Walkindc06f6b2004-10-20 15:50:55 +00001010 int base;
1011
1012 if(len == 2 /* "&#" */) goto want_more;
1013 if(p[2] == 0x78 /* 'x' */)
1014 pval = p + 3, base = 16;
1015 else
1016 pval = p + 2, base = 10;
1017 len = OS__strtoent(base, pval, p + len, &val);
1018 if(len == -1) {
1019 /* Invalid charset. Just copy verbatim. */
1020 *buf++ = ch;
1021 continue;
1022 }
1023 if(!len || pval[len-1] != 0x3b) goto want_more;
1024 assert(val > 0);
1025 p += (pval - p) + len - 1; /* Advance past entref */
1026
1027 if(val < 0x80) {
1028 *buf++ = (char)val;
1029 } else if(val < 0x800) {
1030 *buf++ = 0xc0 | ((val >> 6));
1031 *buf++ = 0x80 | ((val & 0x3f));
1032 } else if(val < 0x10000) {
1033 *buf++ = 0xe0 | ((val >> 12));
1034 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1035 *buf++ = 0x80 | ((val & 0x3f));
1036 } else if(val < 0x200000) {
1037 *buf++ = 0xf0 | ((val >> 18));
1038 *buf++ = 0x80 | ((val >> 12) & 0x3f);
1039 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1040 *buf++ = 0x80 | ((val & 0x3f));
1041 } else if(val < 0x4000000) {
1042 *buf++ = 0xf8 | ((val >> 24));
1043 *buf++ = 0x80 | ((val >> 18) & 0x3f);
1044 *buf++ = 0x80 | ((val >> 12) & 0x3f);
1045 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1046 *buf++ = 0x80 | ((val & 0x3f));
1047 } else {
1048 *buf++ = 0xfc | ((val >> 30) & 0x1);
1049 *buf++ = 0x80 | ((val >> 24) & 0x3f);
1050 *buf++ = 0x80 | ((val >> 18) & 0x3f);
1051 *buf++ = 0x80 | ((val >> 12) & 0x3f);
1052 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1053 *buf++ = 0x80 | ((val & 0x3f));
1054 }
1055 } else {
1056 /*
1057 * Ugly, limited parsing of &amp; &gt; &lt;
1058 */
1059 char *sc = (char *)memchr(p, 0x3b, len > 5 ? 5 : len);
1060 if(!sc) goto want_more;
1061 if((sc - p) == 4
1062 && p[1] == 0x61 /* 'a' */
1063 && p[2] == 0x6d /* 'm' */
1064 && p[3] == 0x70 /* 'p' */) {
1065 *buf++ = 0x26;
1066 p = sc;
1067 continue;
1068 }
1069 if((sc - p) == 3) {
1070 if(p[1] == 0x6c) {
1071 *buf = 0x3c; /* '<' */
1072 } else if(p[1] == 0x67) {
1073 *buf = 0x3e; /* '>' */
1074 } else {
1075 /* Unsupported entity reference */
1076 *buf++ = ch;
1077 continue;
1078 }
1079 if(p[2] != 0x74) {
1080 /* Unsupported entity reference */
1081 *buf++ = ch;
1082 continue;
1083 }
1084 buf++;
1085 p = sc;
1086 continue;
1087 }
1088 /* Unsupported entity reference */
1089 *buf++ = ch;
1090 }
1091
1092 continue;
1093 want_more:
1094 if(have_more) {
1095 /*
1096 * We know that no more data (of the same type)
1097 * is coming. Copy the rest verbatim.
1098 */
1099 *buf++ = ch;
1100 continue;
1101 }
Lev Walkin0fab1a62005-03-09 22:19:25 +00001102 chunk_size = (p - (const char *)chunk_buf);
Lev Walkindc06f6b2004-10-20 15:50:55 +00001103 /* Processing stalled: need more data */
Lev Walkinbdaae772005-02-18 12:25:47 +00001104 break;
Lev Walkindc06f6b2004-10-20 15:50:55 +00001105 }
1106
1107 st->size = buf - st->buf;
Lev Walkin494fb702017-08-07 20:07:00 -07001108 assert(st->size <= new_size);
Lev Walkindc06f6b2004-10-20 15:50:55 +00001109 st->buf[st->size] = 0; /* Courtesy termination */
1110
1111 return chunk_size; /* Converted in full */
1112}
1113
1114/*
1115 * Decode OCTET STRING from the XML element's body.
1116 */
1117static asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -07001118OCTET_STRING__decode_xer(const asn_codec_ctx_t *opt_codec_ctx,
Lev Walkindc06f6b2004-10-20 15:50:55 +00001119 asn_TYPE_descriptor_t *td, void **sptr,
Lev Walkin8c3b8542005-03-10 18:52:02 +00001120 const char *opt_mname, const void *buf_ptr, size_t size,
Lev Walkindc06f6b2004-10-20 15:50:55 +00001121 int (*opt_unexpected_tag_decoder)
Lev Walkin0fab1a62005-03-09 22:19:25 +00001122 (void *struct_ptr, const void *chunk_buf, size_t chunk_size),
Lev Walkindc06f6b2004-10-20 15:50:55 +00001123 ssize_t (*body_receiver)
Lev Walkin0fab1a62005-03-09 22:19:25 +00001124 (void *struct_ptr, const void *chunk_buf, size_t chunk_size,
Lev Walkindc06f6b2004-10-20 15:50:55 +00001125 int have_more)
1126) {
Lev Walkind5125642005-02-14 20:08:00 +00001127 OCTET_STRING_t *st = (OCTET_STRING_t *)*sptr;
Lev Walkindc06f6b2004-10-20 15:50:55 +00001128 asn_OCTET_STRING_specifics_t *specs = td->specifics
1129 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +08001130 : &asn_SPC_OCTET_STRING_specs;
Lev Walkindc06f6b2004-10-20 15:50:55 +00001131 const char *xml_tag = opt_mname ? opt_mname : td->xml_tag;
1132 asn_struct_ctx_t *ctx; /* Per-structure parser context */
Lev Walkinc61f3862005-02-14 17:21:22 +00001133 asn_dec_rval_t rval; /* Return value from the decoder */
1134 int st_allocated;
Lev Walkindc06f6b2004-10-20 15:50:55 +00001135
1136 /*
1137 * Create the string if does not exist.
1138 */
Lev Walkinc61f3862005-02-14 17:21:22 +00001139 if(!st) {
Lev Walkin8484ed82004-12-14 13:31:01 +00001140 st = (OCTET_STRING_t *)CALLOC(1, specs->struct_size);
Lev Walkin8484ed82004-12-14 13:31:01 +00001141 *sptr = (void *)st;
Lev Walkinc61f3862005-02-14 17:21:22 +00001142 if(!st) goto sta_failed;
1143 st_allocated = 1;
Lev Walkinbdaae772005-02-18 12:25:47 +00001144 } else {
1145 st_allocated = 0;
1146 }
Lev Walkinc61f3862005-02-14 17:21:22 +00001147 if(!st->buf) {
1148 /* This is separate from above section */
1149 st->buf = (uint8_t *)CALLOC(1, 1);
1150 if(!st->buf) {
1151 if(st_allocated) {
1152 *sptr = 0;
1153 goto stb_failed;
1154 } else {
1155 goto sta_failed;
1156 }
Lev Walkindc06f6b2004-10-20 15:50:55 +00001157 }
1158 }
1159
1160 /* Restore parsing context */
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001161 ctx = (asn_struct_ctx_t *)(((char *)*sptr) + specs->ctx_offset);
Lev Walkindc06f6b2004-10-20 15:50:55 +00001162
1163 return xer_decode_general(opt_codec_ctx, ctx, *sptr, xml_tag,
1164 buf_ptr, size, opt_unexpected_tag_decoder, body_receiver);
Lev Walkinc61f3862005-02-14 17:21:22 +00001165
1166stb_failed:
1167 FREEMEM(st);
1168sta_failed:
1169 rval.code = RC_FAIL;
1170 rval.consumed = 0;
1171 return rval;
Lev Walkindc06f6b2004-10-20 15:50:55 +00001172}
1173
1174/*
1175 * Decode OCTET STRING from the hexadecimal data.
1176 */
1177asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -07001178OCTET_STRING_decode_xer_hex(const asn_codec_ctx_t *opt_codec_ctx,
Lev Walkindc06f6b2004-10-20 15:50:55 +00001179 asn_TYPE_descriptor_t *td, void **sptr,
Lev Walkin8c3b8542005-03-10 18:52:02 +00001180 const char *opt_mname, const void *buf_ptr, size_t size) {
Lev Walkindc06f6b2004-10-20 15:50:55 +00001181 return OCTET_STRING__decode_xer(opt_codec_ctx, td, sptr, opt_mname,
1182 buf_ptr, size, 0, OCTET_STRING__convert_hexadecimal);
1183}
1184
1185/*
1186 * Decode OCTET STRING from the binary (0/1) data.
1187 */
1188asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -07001189OCTET_STRING_decode_xer_binary(const asn_codec_ctx_t *opt_codec_ctx,
Lev Walkindc06f6b2004-10-20 15:50:55 +00001190 asn_TYPE_descriptor_t *td, void **sptr,
Lev Walkin8c3b8542005-03-10 18:52:02 +00001191 const char *opt_mname, const void *buf_ptr, size_t size) {
Lev Walkindc06f6b2004-10-20 15:50:55 +00001192 return OCTET_STRING__decode_xer(opt_codec_ctx, td, sptr, opt_mname,
1193 buf_ptr, size, 0, OCTET_STRING__convert_binary);
1194}
1195
1196/*
1197 * Decode OCTET STRING from the string (ASCII/UTF-8) data.
1198 */
1199asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -07001200OCTET_STRING_decode_xer_utf8(const asn_codec_ctx_t *opt_codec_ctx,
Lev Walkindc06f6b2004-10-20 15:50:55 +00001201 asn_TYPE_descriptor_t *td, void **sptr,
Lev Walkin8c3b8542005-03-10 18:52:02 +00001202 const char *opt_mname, const void *buf_ptr, size_t size) {
Lev Walkindc06f6b2004-10-20 15:50:55 +00001203 return OCTET_STRING__decode_xer(opt_codec_ctx, td, sptr, opt_mname,
1204 buf_ptr, size,
1205 OCTET_STRING__handle_control_chars,
1206 OCTET_STRING__convert_entrefs);
1207}
1208
Lev Walkin725883b2006-10-09 12:07:58 +00001209static int
Lev Walkin3a4689a2006-11-24 11:20:27 +00001210OCTET_STRING_per_get_characters(asn_per_data_t *po, uint8_t *buf,
1211 size_t units, unsigned int bpc, unsigned int unit_bits,
Lev Walkin494fb702017-08-07 20:07:00 -07001212 long lb, long ub, const asn_per_constraints_t *pc) {
Lev Walkin3a4689a2006-11-24 11:20:27 +00001213 uint8_t *end = buf + units * bpc;
Lev Walkin725883b2006-10-09 12:07:58 +00001214
1215 ASN_DEBUG("Expanding %d characters into (%ld..%ld):%d",
Lev Walkin3a4689a2006-11-24 11:20:27 +00001216 (int)units, lb, ub, unit_bits);
Lev Walkin725883b2006-10-09 12:07:58 +00001217
1218 /* X.691: 27.5.4 */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001219 if((unsigned long)ub <= ((unsigned long)2 << (unit_bits - 1))) {
Lev Walkin725883b2006-10-09 12:07:58 +00001220 /* Decode without translation */
1221 lb = 0;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001222 } else if(pc && pc->code2value) {
1223 if(unit_bits > 16)
1224 return 1; /* FATAL: can't have constrained
1225 * UniversalString with more than
1226 * 16 million code points */
1227 for(; buf < end; buf += bpc) {
Lev Walkin725883b2006-10-09 12:07:58 +00001228 int value;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001229 int code = per_get_few_bits(po, unit_bits);
Lev Walkin725883b2006-10-09 12:07:58 +00001230 if(code < 0) return -1; /* WMORE */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001231 value = pc->code2value(code);
Lev Walkin725883b2006-10-09 12:07:58 +00001232 if(value < 0) {
1233 ASN_DEBUG("Code %d (0x%02x) is"
1234 " not in map (%ld..%ld)",
1235 code, code, lb, ub);
1236 return 1; /* FATAL */
1237 }
Lev Walkin3a4689a2006-11-24 11:20:27 +00001238 switch(bpc) {
1239 case 1: *buf = value; break;
1240 case 2: buf[0] = value >> 8; buf[1] = value; break;
1241 case 4: buf[0] = value >> 24; buf[1] = value >> 16;
1242 buf[2] = value >> 8; buf[3] = value; break;
1243 }
Lev Walkin725883b2006-10-09 12:07:58 +00001244 }
1245 return 0;
1246 }
1247
Lev Walkin3a4689a2006-11-24 11:20:27 +00001248 /* Shortcut the no-op copying to the aligned structure */
1249 if(lb == 0 && (unit_bits == 8 * bpc)) {
1250 return per_get_many_bits(po, buf, 0, unit_bits * units);
1251 }
1252
1253 for(; buf < end; buf += bpc) {
Lev Walkin725883b2006-10-09 12:07:58 +00001254 int code = per_get_few_bits(po, unit_bits);
1255 int ch = code + lb;
1256 if(code < 0) return -1; /* WMORE */
1257 if(ch > ub) {
1258 ASN_DEBUG("Code %d is out of range (%ld..%ld)",
1259 ch, lb, ub);
1260 return 1; /* FATAL */
1261 }
Lev Walkin3a4689a2006-11-24 11:20:27 +00001262 switch(bpc) {
1263 case 1: *buf = ch; break;
1264 case 2: buf[0] = ch >> 8; buf[1] = ch; break;
1265 case 4: buf[0] = ch >> 24; buf[1] = ch >> 16;
1266 buf[2] = ch >> 8; buf[3] = ch; break;
1267 }
Lev Walkin725883b2006-10-09 12:07:58 +00001268 }
1269
1270 return 0;
1271}
1272
1273static int
Lev Walkin3a4689a2006-11-24 11:20:27 +00001274OCTET_STRING_per_put_characters(asn_per_outp_t *po, const uint8_t *buf,
1275 size_t units, unsigned int bpc, unsigned int unit_bits,
Lev Walkin494fb702017-08-07 20:07:00 -07001276 long lb, long ub, const asn_per_constraints_t *pc) {
Lev Walkin3a4689a2006-11-24 11:20:27 +00001277 const uint8_t *end = buf + units * bpc;
Lev Walkin725883b2006-10-09 12:07:58 +00001278
Lev Walkin3a4689a2006-11-24 11:20:27 +00001279 ASN_DEBUG("Squeezing %d characters into (%ld..%ld):%d (%d bpc)",
1280 (int)units, lb, ub, unit_bits, bpc);
Lev Walkin725883b2006-10-09 12:07:58 +00001281
1282 /* X.691: 27.5.4 */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001283 if((unsigned long)ub <= ((unsigned long)2 << (unit_bits - 1))) {
Lev Walkin725883b2006-10-09 12:07:58 +00001284 /* Encode as is */
1285 lb = 0;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001286 } else if(pc && pc->value2code) {
1287 for(; buf < end; buf += bpc) {
1288 int code;
1289 uint32_t value;
1290 switch(bpc) {
1291 case 1: value = *(const uint8_t *)buf; break;
1292 case 2: value = (buf[0] << 8) | buf[1]; break;
1293 case 4: value = (buf[0] << 24) | (buf[1] << 16)
1294 | (buf[2] << 8) | buf[3]; break;
1295 default: return -1;
1296 }
1297 code = pc->value2code(value);
Lev Walkin725883b2006-10-09 12:07:58 +00001298 if(code < 0) {
1299 ASN_DEBUG("Character %d (0x%02x) is"
1300 " not in map (%ld..%ld)",
1301 *buf, *buf, lb, ub);
1302 return -1;
1303 }
1304 if(per_put_few_bits(po, code, unit_bits))
1305 return -1;
1306 }
1307 }
1308
Lev Walkin3a4689a2006-11-24 11:20:27 +00001309 /* Shortcut the no-op copying to the aligned structure */
1310 if(lb == 0 && (unit_bits == 8 * bpc)) {
1311 return per_put_many_bits(po, buf, unit_bits * units);
1312 }
1313
1314 for(ub -= lb; buf < end; buf += bpc) {
1315 int ch;
1316 uint32_t value;
1317 switch(bpc) {
1318 case 1: value = *(const uint8_t *)buf; break;
1319 case 2: value = (buf[0] << 8) | buf[1]; break;
1320 case 4: value = (buf[0] << 24) | (buf[1] << 16)
1321 | (buf[2] << 8) | buf[3]; break;
1322 default: return -1;
1323 }
1324 ch = value - lb;
Lev Walkin725883b2006-10-09 12:07:58 +00001325 if(ch < 0 || ch > ub) {
1326 ASN_DEBUG("Character %d (0x%02x)"
1327 " is out of range (%ld..%ld)",
1328 *buf, *buf, lb, ub + lb);
1329 return -1;
1330 }
1331 if(per_put_few_bits(po, ch, unit_bits))
1332 return -1;
1333 }
1334
1335 return 0;
1336}
1337
Lev Walkin62d76872017-08-05 22:49:42 -07001338#ifndef ASN_DISABLE_PER_SUPPORT
1339
Lev Walkin59b176e2005-11-26 11:25:14 +00001340asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -07001341OCTET_STRING_decode_uper(const asn_codec_ctx_t *opt_codec_ctx,
Lev Walkin494fb702017-08-07 20:07:00 -07001342 asn_TYPE_descriptor_t *td,
1343 const asn_per_constraints_t *constraints, void **sptr,
1344 asn_per_data_t *pd) {
1345 asn_OCTET_STRING_specifics_t *specs = td->specifics
Lev Walkin59b176e2005-11-26 11:25:14 +00001346 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +08001347 : &asn_SPC_OCTET_STRING_specs;
Lev Walkin494fb702017-08-07 20:07:00 -07001348 const asn_per_constraints_t *pc =
1349 constraints ? constraints : td->per_constraints;
1350 const asn_per_constraint_t *cval;
1351 const asn_per_constraint_t *csiz;
Lev Walkin59b176e2005-11-26 11:25:14 +00001352 asn_dec_rval_t rval = { RC_OK, 0 };
1353 BIT_STRING_t *st = (BIT_STRING_t *)*sptr;
1354 ssize_t consumed_myself = 0;
1355 int repeat;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001356 enum {
1357 OS__BPC_BIT = 0,
1358 OS__BPC_CHAR = 1,
1359 OS__BPC_U16 = 2,
1360 OS__BPC_U32 = 4
1361 } bpc; /* Bytes per character */
1362 unsigned int unit_bits;
1363 unsigned int canonical_unit_bits;
Lev Walkin59b176e2005-11-26 11:25:14 +00001364
1365 (void)opt_codec_ctx;
1366
Lev Walkin3a4689a2006-11-24 11:20:27 +00001367 if(pc) {
1368 cval = &pc->value;
1369 csiz = &pc->size;
1370 } else {
1371 cval = &asn_DEF_OCTET_STRING_constraints.value;
1372 csiz = &asn_DEF_OCTET_STRING_constraints.size;
1373 }
1374
1375 switch(specs->subvariant) {
1376 default:
1377 case ASN_OSUBV_ANY:
1378 ASN_DEBUG("Unrecognized subvariant %d", specs->subvariant);
1379 RETURN(RC_FAIL);
1380 case ASN_OSUBV_BIT:
1381 canonical_unit_bits = unit_bits = 1;
1382 bpc = OS__BPC_BIT;
1383 break;
1384 case ASN_OSUBV_STR:
1385 canonical_unit_bits = unit_bits = 8;
1386 if(cval->flags & APC_CONSTRAINED)
1387 unit_bits = cval->range_bits;
1388 bpc = OS__BPC_CHAR;
1389 break;
1390 case ASN_OSUBV_U16:
1391 canonical_unit_bits = unit_bits = 16;
1392 if(cval->flags & APC_CONSTRAINED)
1393 unit_bits = cval->range_bits;
1394 bpc = OS__BPC_U16;
1395 break;
1396 case ASN_OSUBV_U32:
1397 canonical_unit_bits = unit_bits = 32;
1398 if(cval->flags & APC_CONSTRAINED)
1399 unit_bits = cval->range_bits;
1400 bpc = OS__BPC_U32;
1401 break;
1402 }
1403
Lev Walkin59b176e2005-11-26 11:25:14 +00001404 /*
1405 * Allocate the string.
1406 */
1407 if(!st) {
1408 st = (BIT_STRING_t *)(*sptr = CALLOC(1, specs->struct_size));
1409 if(!st) RETURN(RC_FAIL);
1410 }
1411
Lev Walkin725883b2006-10-09 12:07:58 +00001412 ASN_DEBUG("PER Decoding %s size %ld .. %ld bits %d",
Lev Walkin3a4689a2006-11-24 11:20:27 +00001413 csiz->flags & APC_EXTENSIBLE ? "extensible" : "non-extensible",
1414 csiz->lower_bound, csiz->upper_bound, csiz->effective_bits);
Lev Walkince676dd2005-12-20 22:34:55 +00001415
Lev Walkin3a4689a2006-11-24 11:20:27 +00001416 if(csiz->flags & APC_EXTENSIBLE) {
Lev Walkin59b176e2005-11-26 11:25:14 +00001417 int inext = per_get_few_bits(pd, 1);
Lev Walkin0a8aa602006-09-18 20:05:55 +00001418 if(inext < 0) RETURN(RC_WMORE);
Lev Walkin3a4689a2006-11-24 11:20:27 +00001419 if(inext) {
1420 csiz = &asn_DEF_OCTET_STRING_constraints.size;
1421 cval = &asn_DEF_OCTET_STRING_constraints.value;
1422 unit_bits = canonical_unit_bits;
1423 }
Lev Walkin59b176e2005-11-26 11:25:14 +00001424 }
1425
Lev Walkin3a4689a2006-11-24 11:20:27 +00001426 if(csiz->effective_bits >= 0) {
Lev Walkin59b176e2005-11-26 11:25:14 +00001427 FREEMEM(st->buf);
Lev Walkin3a4689a2006-11-24 11:20:27 +00001428 if(bpc) {
1429 st->size = csiz->upper_bound * bpc;
Lev Walkin59b176e2005-11-26 11:25:14 +00001430 } else {
Lev Walkin3a4689a2006-11-24 11:20:27 +00001431 st->size = (csiz->upper_bound + 7) >> 3;
Lev Walkin59b176e2005-11-26 11:25:14 +00001432 }
1433 st->buf = (uint8_t *)MALLOC(st->size + 1);
1434 if(!st->buf) { st->size = 0; RETURN(RC_FAIL); }
1435 }
1436
1437 /* X.691, #16.5: zero-length encoding */
1438 /* X.691, #16.6: short fixed length encoding (up to 2 octets) */
1439 /* X.691, #16.7: long fixed length encoding (up to 64K octets) */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001440 if(csiz->effective_bits == 0) {
Lev Walkin725883b2006-10-09 12:07:58 +00001441 int ret;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001442 if(bpc) {
1443 ASN_DEBUG("Encoding OCTET STRING size %ld",
1444 csiz->upper_bound);
1445 ret = OCTET_STRING_per_get_characters(pd, st->buf,
1446 csiz->upper_bound, bpc, unit_bits,
1447 cval->lower_bound, cval->upper_bound, pc);
Lev Walkin725883b2006-10-09 12:07:58 +00001448 if(ret > 0) RETURN(RC_FAIL);
1449 } else {
Lev Walkin3a4689a2006-11-24 11:20:27 +00001450 ASN_DEBUG("Encoding BIT STRING size %ld",
1451 csiz->upper_bound);
Lev Walkin725883b2006-10-09 12:07:58 +00001452 ret = per_get_many_bits(pd, st->buf, 0,
Lev Walkin3a4689a2006-11-24 11:20:27 +00001453 unit_bits * csiz->upper_bound);
Lev Walkin725883b2006-10-09 12:07:58 +00001454 }
Lev Walkin0a8aa602006-09-18 20:05:55 +00001455 if(ret < 0) RETURN(RC_WMORE);
Lev Walkin3a4689a2006-11-24 11:20:27 +00001456 consumed_myself += unit_bits * csiz->upper_bound;
Lev Walkin59b176e2005-11-26 11:25:14 +00001457 st->buf[st->size] = 0;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001458 if(bpc == 0) {
1459 int ubs = (csiz->upper_bound & 0x7);
1460 st->bits_unused = ubs ? 8 - ubs : 0;
1461 }
Lev Walkin59b176e2005-11-26 11:25:14 +00001462 RETURN(RC_OK);
1463 }
1464
1465 st->size = 0;
1466 do {
Lev Walkin3a4689a2006-11-24 11:20:27 +00001467 ssize_t raw_len;
Lev Walkin59b176e2005-11-26 11:25:14 +00001468 ssize_t len_bytes;
1469 ssize_t len_bits;
1470 void *p;
1471 int ret;
1472
1473 /* Get the PER length */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001474 raw_len = uper_get_length(pd, csiz->effective_bits, &repeat);
1475 if(raw_len < 0) RETURN(RC_WMORE);
1476 raw_len += csiz->lower_bound;
Lev Walkin59b176e2005-11-26 11:25:14 +00001477
Lev Walkin0a8aa602006-09-18 20:05:55 +00001478 ASN_DEBUG("Got PER length eb %ld, len %ld, %s (%s)",
Lev Walkin3a4689a2006-11-24 11:20:27 +00001479 (long)csiz->effective_bits, (long)raw_len,
Lev Walkin0a8aa602006-09-18 20:05:55 +00001480 repeat ? "repeat" : "once", td->name);
Lev Walkin3a4689a2006-11-24 11:20:27 +00001481 if(bpc) {
1482 len_bytes = raw_len * bpc;
1483 len_bits = len_bytes * unit_bits;
1484 } else {
1485 len_bits = raw_len;
Lev Walkin59b176e2005-11-26 11:25:14 +00001486 len_bytes = (len_bits + 7) >> 3;
1487 if(len_bits & 0x7)
1488 st->bits_unused = 8 - (len_bits & 0x7);
1489 /* len_bits be multiple of 16K if repeat is set */
Lev Walkin59b176e2005-11-26 11:25:14 +00001490 }
1491 p = REALLOC(st->buf, st->size + len_bytes + 1);
1492 if(!p) RETURN(RC_FAIL);
1493 st->buf = (uint8_t *)p;
1494
Lev Walkin3a4689a2006-11-24 11:20:27 +00001495 if(bpc) {
1496 ret = OCTET_STRING_per_get_characters(pd,
1497 &st->buf[st->size], raw_len, bpc, unit_bits,
1498 cval->lower_bound, cval->upper_bound, pc);
Lev Walkin725883b2006-10-09 12:07:58 +00001499 if(ret > 0) RETURN(RC_FAIL);
1500 } else {
1501 ret = per_get_many_bits(pd, &st->buf[st->size],
1502 0, len_bits);
1503 }
Lev Walkin0a8aa602006-09-18 20:05:55 +00001504 if(ret < 0) RETURN(RC_WMORE);
Lev Walkin59b176e2005-11-26 11:25:14 +00001505 st->size += len_bytes;
1506 } while(repeat);
1507 st->buf[st->size] = 0; /* nul-terminate */
1508
1509 return rval;
1510}
Lev Walkindc06f6b2004-10-20 15:50:55 +00001511
Lev Walkin523de9e2006-08-18 01:34:18 +00001512asn_enc_rval_t
1513OCTET_STRING_encode_uper(asn_TYPE_descriptor_t *td,
Lev Walkin494fb702017-08-07 20:07:00 -07001514 const asn_per_constraints_t *constraints, void *sptr,
1515 asn_per_outp_t *po) {
1516 asn_OCTET_STRING_specifics_t *specs = td->specifics
Lev Walkin523de9e2006-08-18 01:34:18 +00001517 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +08001518 : &asn_SPC_OCTET_STRING_specs;
Lev Walkin494fb702017-08-07 20:07:00 -07001519 const asn_per_constraints_t *pc = constraints ? constraints
Lev Walkin725883b2006-10-09 12:07:58 +00001520 : td->per_constraints;
Lev Walkin494fb702017-08-07 20:07:00 -07001521 const asn_per_constraint_t *cval;
1522 const asn_per_constraint_t *csiz;
Lev Walkin523de9e2006-08-18 01:34:18 +00001523 const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001524 asn_enc_rval_t er = { 0, 0, 0 };
Lev Walkin523de9e2006-08-18 01:34:18 +00001525 int inext = 0; /* Lies not within extension root */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001526 unsigned int unit_bits;
1527 unsigned int canonical_unit_bits;
1528 unsigned int sizeinunits;
Lev Walkin523de9e2006-08-18 01:34:18 +00001529 const uint8_t *buf;
1530 int ret;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001531 enum {
1532 OS__BPC_BIT = 0,
1533 OS__BPC_CHAR = 1,
1534 OS__BPC_U16 = 2,
1535 OS__BPC_U32 = 4
1536 } bpc; /* Bytes per character */
1537 int ct_extensible;
Lev Walkin523de9e2006-08-18 01:34:18 +00001538
Lev Walkin2ddd3082006-10-16 12:32:14 +00001539 if(!st || (!st->buf && st->size))
Lev Walkin7c1dc052016-03-14 03:08:15 -07001540 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001541
Lev Walkin3a4689a2006-11-24 11:20:27 +00001542 if(pc) {
1543 cval = &pc->value;
1544 csiz = &pc->size;
1545 } else {
1546 cval = &asn_DEF_OCTET_STRING_constraints.value;
1547 csiz = &asn_DEF_OCTET_STRING_constraints.size;
1548 }
1549 ct_extensible = csiz->flags & APC_EXTENSIBLE;
1550
1551 switch(specs->subvariant) {
1552 default:
1553 case ASN_OSUBV_ANY:
Lev Walkin7c1dc052016-03-14 03:08:15 -07001554 ASN__ENCODE_FAILED;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001555 case ASN_OSUBV_BIT:
1556 canonical_unit_bits = unit_bits = 1;
1557 bpc = OS__BPC_BIT;
1558 sizeinunits = st->size * 8 - (st->bits_unused & 0x07);
Lev Walkin523de9e2006-08-18 01:34:18 +00001559 ASN_DEBUG("BIT STRING of %d bytes, %d bits unused",
1560 sizeinunits, st->bits_unused);
Lev Walkin3a4689a2006-11-24 11:20:27 +00001561 break;
1562 case ASN_OSUBV_STR:
1563 canonical_unit_bits = unit_bits = 8;
1564 if(cval->flags & APC_CONSTRAINED)
1565 unit_bits = cval->range_bits;
1566 bpc = OS__BPC_CHAR;
1567 sizeinunits = st->size;
1568 break;
1569 case ASN_OSUBV_U16:
1570 canonical_unit_bits = unit_bits = 16;
1571 if(cval->flags & APC_CONSTRAINED)
1572 unit_bits = cval->range_bits;
1573 bpc = OS__BPC_U16;
Lev Walkin4ef01a22017-09-06 22:54:39 -07001574 sizeinunits = st->size >> 1;
1575 if(st->size & 1) {
1576 ASN_DEBUG("%s string size is not modulo 2", td->name);
1577 ASN__ENCODE_FAILED;
1578 }
Lev Walkin3a4689a2006-11-24 11:20:27 +00001579 break;
1580 case ASN_OSUBV_U32:
1581 canonical_unit_bits = unit_bits = 32;
1582 if(cval->flags & APC_CONSTRAINED)
1583 unit_bits = cval->range_bits;
1584 bpc = OS__BPC_U32;
Lev Walkin4ef01a22017-09-06 22:54:39 -07001585 sizeinunits = st->size >> 2;
1586 if(st->size & 3) {
1587 ASN_DEBUG("%s string size is not modulo 4", td->name);
1588 ASN__ENCODE_FAILED;
1589 }
Lev Walkin3a4689a2006-11-24 11:20:27 +00001590 break;
Lev Walkin725883b2006-10-09 12:07:58 +00001591 }
1592
Lev Walkin00918812006-09-18 21:19:32 +00001593 ASN_DEBUG("Encoding %s into %d units of %d bits"
Lev Walkin725883b2006-10-09 12:07:58 +00001594 " (%ld..%ld, effective %d)%s",
Lev Walkin00918812006-09-18 21:19:32 +00001595 td->name, sizeinunits, unit_bits,
Lev Walkin3a4689a2006-11-24 11:20:27 +00001596 csiz->lower_bound, csiz->upper_bound,
1597 csiz->effective_bits, ct_extensible ? " EXT" : "");
Lev Walkin523de9e2006-08-18 01:34:18 +00001598
Lev Walkind4c16732013-03-28 05:00:47 -07001599 /* Figure out whether size lies within PER visible constraint */
Lev Walkin523de9e2006-08-18 01:34:18 +00001600
Lev Walkin3a4689a2006-11-24 11:20:27 +00001601 if(csiz->effective_bits >= 0) {
1602 if((int)sizeinunits < csiz->lower_bound
1603 || (int)sizeinunits > csiz->upper_bound) {
Lev Walkin523de9e2006-08-18 01:34:18 +00001604 if(ct_extensible) {
Lev Walkin3a4689a2006-11-24 11:20:27 +00001605 cval = &asn_DEF_OCTET_STRING_constraints.value;
1606 csiz = &asn_DEF_OCTET_STRING_constraints.size;
1607 unit_bits = canonical_unit_bits;
Lev Walkin523de9e2006-08-18 01:34:18 +00001608 inext = 1;
Lev Walkin4ef01a22017-09-06 22:54:39 -07001609 } else {
Lev Walkin7c1dc052016-03-14 03:08:15 -07001610 ASN__ENCODE_FAILED;
Lev Walkin4ef01a22017-09-06 22:54:39 -07001611 }
Lev Walkin523de9e2006-08-18 01:34:18 +00001612 }
1613 } else {
1614 inext = 0;
1615 }
1616
1617 if(ct_extensible) {
1618 /* Declare whether length is [not] within extension root */
1619 if(per_put_few_bits(po, inext, 1))
Lev Walkin7c1dc052016-03-14 03:08:15 -07001620 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001621 }
1622
1623 /* X.691, #16.5: zero-length encoding */
1624 /* X.691, #16.6: short fixed length encoding (up to 2 octets) */
1625 /* X.691, #16.7: long fixed length encoding (up to 64K octets) */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001626 if(csiz->effective_bits >= 0) {
Lev Walkin24810022017-08-25 12:16:11 -07001627 ASN_DEBUG("Encoding %zu bytes (%ld), length in %d bits",
Lev Walkin3a4689a2006-11-24 11:20:27 +00001628 st->size, sizeinunits - csiz->lower_bound,
1629 csiz->effective_bits);
1630 ret = per_put_few_bits(po, sizeinunits - csiz->lower_bound,
1631 csiz->effective_bits);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001632 if(ret) ASN__ENCODE_FAILED;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001633 if(bpc) {
1634 ret = OCTET_STRING_per_put_characters(po, st->buf,
1635 sizeinunits, bpc, unit_bits,
1636 cval->lower_bound, cval->upper_bound, pc);
Lev Walkin725883b2006-10-09 12:07:58 +00001637 } else {
1638 ret = per_put_many_bits(po, st->buf,
1639 sizeinunits * unit_bits);
1640 }
Lev Walkin7c1dc052016-03-14 03:08:15 -07001641 if(ret) ASN__ENCODE_FAILED;
1642 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +00001643 }
1644
Lev Walkin24810022017-08-25 12:16:11 -07001645 ASN_DEBUG("Encoding %zu bytes", st->size);
Lev Walkin523de9e2006-08-18 01:34:18 +00001646
1647 if(sizeinunits == 0) {
1648 if(uper_put_length(po, 0))
Lev Walkin7c1dc052016-03-14 03:08:15 -07001649 ASN__ENCODE_FAILED;
1650 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +00001651 }
1652
1653 buf = st->buf;
1654 while(sizeinunits) {
1655 ssize_t maySave = uper_put_length(po, sizeinunits);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001656 if(maySave < 0) ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001657
Lev Walkin345e4c62006-10-19 02:44:08 +00001658 ASN_DEBUG("Encoding %ld of %ld",
1659 (long)maySave, (long)sizeinunits);
Lev Walkin523de9e2006-08-18 01:34:18 +00001660
Lev Walkin3a4689a2006-11-24 11:20:27 +00001661 if(bpc) {
1662 ret = OCTET_STRING_per_put_characters(po, buf,
1663 maySave, bpc, unit_bits,
1664 cval->lower_bound, cval->upper_bound, pc);
Lev Walkin725883b2006-10-09 12:07:58 +00001665 } else {
1666 ret = per_put_many_bits(po, buf, maySave * unit_bits);
1667 }
Lev Walkin7c1dc052016-03-14 03:08:15 -07001668 if(ret) ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001669
Lev Walkin3a4689a2006-11-24 11:20:27 +00001670 if(bpc)
1671 buf += maySave * bpc;
Lev Walkin523de9e2006-08-18 01:34:18 +00001672 else
Lev Walkin3a4689a2006-11-24 11:20:27 +00001673 buf += maySave >> 3;
Lev Walkin523de9e2006-08-18 01:34:18 +00001674 sizeinunits -= maySave;
1675 assert(!(maySave & 0x07) || !sizeinunits);
1676 }
1677
Lev Walkin7c1dc052016-03-14 03:08:15 -07001678 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +00001679}
1680
Lev Walkin62d76872017-08-05 22:49:42 -07001681#endif /* ASN_DISABLE_PER_SUPPORT */
1682
Lev Walkinf15320b2004-06-03 03:38:44 +00001683int
Lev Walkinde4825d2004-09-29 13:20:14 +00001684OCTET_STRING_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +00001685 asn_app_consume_bytes_f *cb, void *app_key) {
Wim Lewis59e8d282014-08-04 12:39:35 -07001686 const char * const h2c = "0123456789ABCDEF";
Lev Walkinc2346572004-08-11 09:07:36 +00001687 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +00001688 char scratch[16 * 3 + 4];
1689 char *p = scratch;
1690 uint8_t *buf;
1691 uint8_t *end;
1692 size_t i;
Lev Walkinf15320b2004-06-03 03:38:44 +00001693
Lev Walkind9bd7752004-06-05 08:17:50 +00001694 (void)td; /* Unused argument */
1695
Lev Walkin2ddd3082006-10-16 12:32:14 +00001696 if(!st || (!st->buf && st->size))
1697 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001698
1699 /*
1700 * Dump the contents of the buffer in hexadecimal.
1701 */
1702 buf = st->buf;
1703 end = buf + st->size;
1704 for(i = 0; buf < end; buf++, i++) {
1705 if(!(i % 16) && (i || st->size > 16)) {
Lev Walkin8e8078a2004-09-26 13:10:40 +00001706 if(cb(scratch, p - scratch, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +00001707 return -1;
Lev Walkin8e8078a2004-09-26 13:10:40 +00001708 _i_INDENT(1);
Lev Walkinf15320b2004-06-03 03:38:44 +00001709 p = scratch;
1710 }
1711 *p++ = h2c[(*buf >> 4) & 0x0F];
1712 *p++ = h2c[*buf & 0x0F];
Lev Walkina9cc46e2004-09-22 16:06:28 +00001713 *p++ = 0x20;
Lev Walkinf15320b2004-06-03 03:38:44 +00001714 }
1715
Lev Walkincc6a9102004-09-23 22:06:26 +00001716 if(p > scratch) {
1717 p--; /* Remove the tail space */
Lev Walkin8e8078a2004-09-26 13:10:40 +00001718 if(cb(scratch, p - scratch, app_key) < 0)
Lev Walkincc6a9102004-09-23 22:06:26 +00001719 return -1;
1720 }
1721
1722 return 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001723}
1724
1725int
Lev Walkindc06f6b2004-10-20 15:50:55 +00001726OCTET_STRING_print_utf8(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +00001727 int ilevel, asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +00001728 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +00001729
Lev Walkind9bd7752004-06-05 08:17:50 +00001730 (void)td; /* Unused argument */
1731 (void)ilevel; /* Unused argument */
1732
Lev Walkin2ddd3082006-10-16 12:32:14 +00001733 if(st && (st->buf || !st->size)) {
Lev Walkin8e8078a2004-09-26 13:10:40 +00001734 return (cb(st->buf, st->size, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001735 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +00001736 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001737 }
1738}
1739
1740void
Lev Walkinf6853ce2017-08-11 00:50:27 -07001741OCTET_STRING_free(const asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkin8d99d7b2017-08-25 01:06:00 -07001742 enum asn_struct_free_method method) {
1743 OCTET_STRING_t *st = (OCTET_STRING_t *)sptr;
Lev Walkin034ee192016-03-14 02:28:33 -07001744 asn_OCTET_STRING_specifics_t *specs;
1745 asn_struct_ctx_t *ctx;
Lev Walkinb54577a2004-11-08 10:47:12 +00001746 struct _stack *stck;
Lev Walkinf15320b2004-06-03 03:38:44 +00001747
1748 if(!td || !st)
1749 return;
1750
Lev Walkin034ee192016-03-14 02:28:33 -07001751 specs = td->specifics
1752 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +08001753 : &asn_SPC_OCTET_STRING_specs;
Lev Walkin034ee192016-03-14 02:28:33 -07001754 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
1755
Lev Walkinf15320b2004-06-03 03:38:44 +00001756 ASN_DEBUG("Freeing %s as OCTET STRING", td->name);
1757
1758 if(st->buf) {
1759 FREEMEM(st->buf);
Lev Walkin2ddd3082006-10-16 12:32:14 +00001760 st->buf = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001761 }
1762
1763 /*
1764 * Remove decode-time stack.
1765 */
Lev Walkinb54577a2004-11-08 10:47:12 +00001766 stck = (struct _stack *)ctx->ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +00001767 if(stck) {
1768 while(stck->tail) {
1769 struct _stack_el *sel = stck->tail;
1770 stck->tail = sel->prev;
1771 FREEMEM(sel);
1772 }
1773 FREEMEM(stck);
1774 }
1775
Lev Walkin8d99d7b2017-08-25 01:06:00 -07001776 switch(method) {
1777 case ASFM_FREE_EVERYTHING:
1778 FREEMEM(sptr);
1779 break;
1780 case ASFM_FREE_UNDERLYING:
1781 break;
1782 case ASFM_FREE_UNDERLYING_AND_RESET:
1783 memset(sptr, 0,
1784 ((asn_OCTET_STRING_specifics_t *)(td->specifics))->struct_size);
1785 break;
1786 }
Lev Walkinf15320b2004-06-03 03:38:44 +00001787}
1788
1789/*
1790 * Conversion routines.
1791 */
1792int
1793OCTET_STRING_fromBuf(OCTET_STRING_t *st, const char *str, int len) {
1794 void *buf;
1795
1796 if(st == 0 || (str == 0 && len)) {
1797 errno = EINVAL;
1798 return -1;
1799 }
1800
1801 /*
1802 * Clear the OCTET STRING.
1803 */
1804 if(str == NULL) {
Lev Walkin7b284812005-12-17 11:43:25 +00001805 FREEMEM(st->buf);
1806 st->buf = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001807 st->size = 0;
1808 return 0;
1809 }
1810
1811 /* Determine the original string size, if not explicitly given */
1812 if(len < 0)
1813 len = strlen(str);
1814
1815 /* Allocate and fill the memory */
1816 buf = MALLOC(len + 1);
Lev Walkin7b284812005-12-17 11:43:25 +00001817 if(buf == NULL)
Lev Walkinf15320b2004-06-03 03:38:44 +00001818 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +00001819
1820 memcpy(buf, str, len);
Lev Walkin7b284812005-12-17 11:43:25 +00001821 ((uint8_t *)buf)[len] = '\0'; /* Couldn't use memcpy(len+1)! */
1822 FREEMEM(st->buf);
1823 st->buf = (uint8_t *)buf;
1824 st->size = len;
Lev Walkinf15320b2004-06-03 03:38:44 +00001825
1826 return 0;
1827}
1828
1829OCTET_STRING_t *
Lev Walkinbbd93252004-10-12 05:57:23 +00001830OCTET_STRING_new_fromBuf(asn_TYPE_descriptor_t *td, const char *str, int len) {
1831 asn_OCTET_STRING_specifics_t *specs = td->specifics
Lev Walkindc06f6b2004-10-20 15:50:55 +00001832 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +08001833 : &asn_SPC_OCTET_STRING_specs;
Lev Walkinf15320b2004-06-03 03:38:44 +00001834 OCTET_STRING_t *st;
1835
Lev Walkinbbd93252004-10-12 05:57:23 +00001836 st = (OCTET_STRING_t *)CALLOC(1, specs->struct_size);
Lev Walkinf15320b2004-06-03 03:38:44 +00001837 if(st && str && OCTET_STRING_fromBuf(st, str, len)) {
Lev Walkin419f6752006-09-13 04:02:00 +00001838 FREEMEM(st);
Lev Walkinf15320b2004-06-03 03:38:44 +00001839 st = NULL;
1840 }
1841
1842 return st;
1843}
1844
Lev Walkincd2f48e2017-08-10 02:14:59 -07001845/*
1846 * Lexicographically compare the common prefix of both strings,
1847 * and if it is the same return -1 for the smallest string.
1848 */
1849int
1850OCTET_STRING_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
1851 const void *bptr) {
1852 const OCTET_STRING_t *a = aptr;
1853 const OCTET_STRING_t *b = bptr;
1854
1855 (void)td;
1856
1857 if(a && b) {
1858 size_t common_prefix_size = a->size <= b->size ? a->size : b->size;
1859 int ret = memcmp(a->buf, b->buf, common_prefix_size);
1860 if(ret == 0) {
1861 /* Figure out which string with equal prefixes is longer. */
1862 if(a->size < b->size) {
1863 return -1;
1864 } else if(a->size > b->size) {
1865 return 1;
1866 } else {
Lev Walkin86526e32017-09-06 23:16:13 -07001867 asn_OCTET_STRING_specifics_t *specs = td->specifics;
1868 if(specs && specs->subvariant == ASN_OSUBV_BIT) {
1869 const BIT_STRING_t *ba = aptr;
1870 const BIT_STRING_t *bb = bptr;
1871 if(ba->bits_unused > bb->bits_unused) {
1872 return -1;
1873 } else if(ba->bits_unused < bb->bits_unused) {
1874 return 1;
1875 } else {
1876 return 0;
1877 }
1878 }
Lev Walkincd2f48e2017-08-10 02:14:59 -07001879 return 0;
1880 }
1881 } else {
1882 return ret;
1883 }
1884 } else if(!a && !b) {
1885 return 0;
1886 } else if(!a) {
1887 return -1;
1888 } else {
1889 return 1;
1890 }
1891
1892}
1893