blob: f5f1ccece38d4a3e971fcb870289bb4d1eebe107 [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001/*-
Lev Walkin0a8aa602006-09-18 20:05:55 +00002 * Copyright (c) 2003, 2004, 2005, 2006 Lev Walkin <vlm@lionet.info>.
Lev Walkin75b1bef2005-04-25 19:38:21 +00003 * All rights reserved.
Lev Walkinf15320b2004-06-03 03:38:44 +00004 * Redistribution and modifications are permitted subject to BSD license.
5 */
Lev Walkina9cc46e2004-09-22 16:06:28 +00006#include <asn_internal.h>
Lev Walkinf15320b2004-06-03 03:38:44 +00007#include <OCTET_STRING.h>
Lev Walkinbbd93252004-10-12 05:57:23 +00008#include <BIT_STRING.h> /* for .bits_unused member */
Lev Walkinf15320b2004-06-03 03:38:44 +00009#include <errno.h>
10
11/*
12 * OCTET STRING basic type description.
13 */
Wim Lewis18c2ec92014-07-29 11:30:10 -070014static const ber_tlv_tag_t asn_DEF_OCTET_STRING_tags[] = {
Lev Walkinf15320b2004-06-03 03:38:44 +000015 (ASN_TAG_CLASS_UNIVERSAL | (4 << 2))
16};
Bi-Ruei, Chiu1fa31c92016-05-16 13:50:09 +080017asn_OCTET_STRING_specifics_t asn_SPC_OCTET_STRING_specs = {
Lev Walkinbbd93252004-10-12 05:57:23 +000018 sizeof(OCTET_STRING_t),
19 offsetof(OCTET_STRING_t, _asn_ctx),
Lev Walkin3a4689a2006-11-24 11:20:27 +000020 ASN_OSUBV_STR
Lev Walkinbbd93252004-10-12 05:57:23 +000021};
Vasil Velichkovd82f77b2016-07-19 19:34:09 +030022static asn_per_constraints_t asn_DEF_OCTET_STRING_constraints = {
Lev Walkin3a4689a2006-11-24 11:20:27 +000023 { APC_CONSTRAINED, 8, 8, 0, 255 },
24 { APC_SEMI_CONSTRAINED, -1, -1, 0, 0 },
25 0, 0
Lev Walkin59b176e2005-11-26 11:25:14 +000026};
Lev Walkinde4825d2004-09-29 13:20:14 +000027asn_TYPE_descriptor_t asn_DEF_OCTET_STRING = {
Lev Walkindc06f6b2004-10-20 15:50:55 +000028 "OCTET STRING", /* Canonical name */
29 "OCTET_STRING", /* XML tag name */
Lev Walkina9cc46e2004-09-22 16:06:28 +000030 OCTET_STRING_free,
31 OCTET_STRING_print, /* non-ascii stuff, generally */
Lev Walkinf15320b2004-06-03 03:38:44 +000032 asn_generic_no_constraint,
33 OCTET_STRING_decode_ber,
34 OCTET_STRING_encode_der,
Lev Walkindc06f6b2004-10-20 15:50:55 +000035 OCTET_STRING_decode_xer_hex,
Lev Walkina9cc46e2004-09-22 16:06:28 +000036 OCTET_STRING_encode_xer,
Lev Walkincc159472017-07-06 08:26:36 -070037#ifdef ASN_DISABLE_OER_SUPPORT
38 0,
39 0,
40#else
41 0,
42 0,
43#endif /* ASN_DISABLE_OER_SUPPORT */
Lev Walkinb33425f2017-07-14 14:59:52 +040044#ifdef ASN_DISABLE_PER_SUPPORT
45 0,
46 0,
47#else
48 OCTET_STRING_decode_uper, /* Unaligned PER decoder */
49 OCTET_STRING_encode_uper, /* Unaligned PER encoder */
50#endif /* ASN_DISABLE_PER_SUPPORT */
Lev Walkinf15320b2004-06-03 03:38:44 +000051 0, /* Use generic outmost tag fetcher */
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 Walkin3990ba62004-09-24 20:57:41 +0000127 ber_tlv_len_t left; /* What's left to read (or -1) */
Lev Walkin5c915992004-09-27 20:54:06 +0000128 ber_tlv_len_t got; /* What was actually processed */
Lev Walkin188ed2c2004-09-13 08:31:01 +0000129 int cont_level; /* Depth of subcontainment */
Lev Walkinf15320b2004-06-03 03:38:44 +0000130 int want_nulls; /* Want null "end of content" octets? */
131 int bits_chopped; /* Flag in BIT STRING mode */
Lev Walkin5c915992004-09-27 20:54:06 +0000132 ber_tlv_tag_t tag; /* For debugging purposes */
Lev Walkinf15320b2004-06-03 03:38:44 +0000133 struct _stack_el *prev;
134 struct _stack_el *next;
135};
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 Walkinde4825d2004-09-29 13:20:14 +0000181OCTET_STRING_decode_ber(asn_codec_ctx_t *opt_codec_ctx,
182 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) {
347 int level = sel->cont_level;
348 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 Walkinabf68892004-10-26 10:12:14 +0000410 ASN_DEBUG("+EXPECT2 got=%ld left=%ld, wn=%d, clvl=%d",
411 (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 Walkin3a4689a2006-11-24 11:20:27 +0000509 if(type_variant == ASN_OSUBV_BIT && st->size) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000510 /* Finalize BIT STRING: zero out unused bits. */
Lev Walkinbbd93252004-10-12 05:57:23 +0000511 st->buf[st->size-1] &= 0xff << st->bits_unused;
Lev Walkinf15320b2004-06-03 03:38:44 +0000512 }
513
Lev Walkinabf68892004-10-26 10:12:14 +0000514 ASN_DEBUG("Took %ld bytes to encode %s: [%s]:%ld",
515 (long)consumed_myself, td->name,
Lev Walkin3a4689a2006-11-24 11:20:27 +0000516 (type_variant == ASN_OSUBV_STR) ? (char *)st->buf : "<data>",
Lev Walkinabf68892004-10-26 10:12:14 +0000517 (long)st->size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000518
Lev Walkinf15320b2004-06-03 03:38:44 +0000519
Lev Walkin5c915992004-09-27 20:54:06 +0000520 RETURN(RC_OK);
Lev Walkinf15320b2004-06-03 03:38:44 +0000521}
522
523/*
524 * Encode OCTET STRING type using DER.
525 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000526asn_enc_rval_t
Lev Walkinbbd93252004-10-12 05:57:23 +0000527OCTET_STRING_encode_der(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000528 int tag_mode, ber_tlv_tag_t tag,
529 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000530 asn_enc_rval_t er;
531 asn_OCTET_STRING_specifics_t *specs = td->specifics
Lev Walkindc06f6b2004-10-20 15:50:55 +0000532 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +0800533 : &asn_SPC_OCTET_STRING_specs;
Lev Walkinbbd93252004-10-12 05:57:23 +0000534 BIT_STRING_t *st = (BIT_STRING_t *)sptr;
Lev Walkin3a4689a2006-11-24 11:20:27 +0000535 enum asn_OS_Subvariant type_variant = specs->subvariant;
Lev Walkinbbd93252004-10-12 05:57:23 +0000536 int fix_last_byte = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000537
538 ASN_DEBUG("%s %s as OCTET STRING",
Lev Walkin1f670c12004-09-02 12:57:25 +0000539 cb?"Estimating":"Encoding", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000540
541 /*
Lev Walkinbbd93252004-10-12 05:57:23 +0000542 * Write tags.
Lev Walkinf15320b2004-06-03 03:38:44 +0000543 */
Lev Walkin3a4689a2006-11-24 11:20:27 +0000544 if(type_variant != ASN_OSUBV_ANY || tag_mode == 1) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000545 er.encoded = der_write_tags(td,
Lev Walkin3a4689a2006-11-24 11:20:27 +0000546 (type_variant == ASN_OSUBV_BIT) + st->size,
547 tag_mode, type_variant == ASN_OSUBV_ANY, tag,
548 cb, app_key);
Lev Walkinbbd93252004-10-12 05:57:23 +0000549 if(er.encoded == -1) {
550 er.failed_type = td;
551 er.structure_ptr = sptr;
552 return er;
Lev Walkinf15320b2004-06-03 03:38:44 +0000553 }
Lev Walkinbbd93252004-10-12 05:57:23 +0000554 } else {
555 /* Disallow: [<tag>] IMPLICIT ANY */
Lev Walkin3a4689a2006-11-24 11:20:27 +0000556 assert(type_variant != ASN_OSUBV_ANY || tag_mode != -1);
Lev Walkinbbd93252004-10-12 05:57:23 +0000557 er.encoded = 0;
558 }
559
560 if(!cb) {
Lev Walkin3a4689a2006-11-24 11:20:27 +0000561 er.encoded += (type_variant == ASN_OSUBV_BIT) + st->size;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700562 ASN__ENCODED_OK(er);
Lev Walkinf15320b2004-06-03 03:38:44 +0000563 }
564
Lev Walkin07f388c2004-10-11 11:43:08 +0000565 /*
Lev Walkinbbd93252004-10-12 05:57:23 +0000566 * Prepare to deal with the last octet of BIT STRING.
Lev Walkin07f388c2004-10-11 11:43:08 +0000567 */
Lev Walkin3a4689a2006-11-24 11:20:27 +0000568 if(type_variant == ASN_OSUBV_BIT) {
Lev Walkinbbd93252004-10-12 05:57:23 +0000569 uint8_t b = st->bits_unused & 0x07;
570 if(b && st->size) fix_last_byte = 1;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700571 ASN__CALLBACK(&b, 1);
Lev Walkinbbd93252004-10-12 05:57:23 +0000572 er.encoded++;
Lev Walkinf15320b2004-06-03 03:38:44 +0000573 }
574
Lev Walkinbbd93252004-10-12 05:57:23 +0000575 /* Invoke callback for the main part of the buffer */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700576 ASN__CALLBACK(st->buf, st->size - fix_last_byte);
Lev Walkinf15320b2004-06-03 03:38:44 +0000577
Lev Walkinbbd93252004-10-12 05:57:23 +0000578 /* The last octet should be stripped off the unused bits */
579 if(fix_last_byte) {
580 uint8_t b = st->buf[st->size-1] & (0xff << st->bits_unused);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700581 ASN__CALLBACK(&b, 1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000582 }
583
Lev Walkinbbd93252004-10-12 05:57:23 +0000584 er.encoded += st->size;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700585 ASN__ENCODED_OK(er);
Lev Walkinbbd93252004-10-12 05:57:23 +0000586cb_failed:
Lev Walkin7c1dc052016-03-14 03:08:15 -0700587 ASN__ENCODE_FAILED;
Lev Walkinf15320b2004-06-03 03:38:44 +0000588}
589
Lev Walkina9cc46e2004-09-22 16:06:28 +0000590asn_enc_rval_t
Lev Walkinde4825d2004-09-29 13:20:14 +0000591OCTET_STRING_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000592 int ilevel, enum xer_encoder_flags_e flags,
593 asn_app_consume_bytes_f *cb, void *app_key) {
Wim Lewis59e8d282014-08-04 12:39:35 -0700594 const char * const h2c = "0123456789ABCDEF";
Lev Walkina9cc46e2004-09-22 16:06:28 +0000595 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
596 asn_enc_rval_t er;
597 char scratch[16 * 3 + 4];
598 char *p = scratch;
599 uint8_t *buf;
600 uint8_t *end;
601 size_t i;
602
Lev Walkin2ddd3082006-10-16 12:32:14 +0000603 if(!st || (!st->buf && st->size))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700604 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000605
606 er.encoded = 0;
607
608 /*
609 * Dump the contents of the buffer in hexadecimal.
610 */
611 buf = st->buf;
612 end = buf + st->size;
613 if(flags & XER_F_CANONICAL) {
614 char *scend = scratch + (sizeof(scratch) - 2);
615 for(; buf < end; buf++) {
616 if(p >= scend) {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700617 ASN__CALLBACK(scratch, p - scratch);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000618 er.encoded += p - scratch;
619 p = scratch;
620 }
621 *p++ = h2c[(*buf >> 4) & 0x0F];
622 *p++ = h2c[*buf & 0x0F];
623 }
Lev Walkincc6a9102004-09-23 22:06:26 +0000624
Lev Walkin7c1dc052016-03-14 03:08:15 -0700625 ASN__CALLBACK(scratch, p-scratch); /* Dump the rest */
Lev Walkincc6a9102004-09-23 22:06:26 +0000626 er.encoded += p - scratch;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000627 } else {
628 for(i = 0; buf < end; buf++, i++) {
629 if(!(i % 16) && (i || st->size > 16)) {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700630 ASN__CALLBACK(scratch, p-scratch);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000631 er.encoded += (p-scratch);
632 p = scratch;
Lev Walkinc6cac8e2016-03-14 02:57:07 -0700633 ASN__TEXT_INDENT(1, ilevel);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000634 }
635 *p++ = h2c[(*buf >> 4) & 0x0F];
636 *p++ = h2c[*buf & 0x0F];
637 *p++ = 0x20;
638 }
Lev Walkincc6a9102004-09-23 22:06:26 +0000639 if(p - scratch) {
640 p--; /* Remove the tail space */
Lev Walkin7c1dc052016-03-14 03:08:15 -0700641 ASN__CALLBACK(scratch, p-scratch); /* Dump the rest */
Lev Walkincc6a9102004-09-23 22:06:26 +0000642 er.encoded += p - scratch;
643 if(st->size > 16)
Lev Walkinc6cac8e2016-03-14 02:57:07 -0700644 ASN__TEXT_INDENT(1, ilevel-1);
Lev Walkincc6a9102004-09-23 22:06:26 +0000645 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000646 }
647
Lev Walkin7c1dc052016-03-14 03:08:15 -0700648 ASN__ENCODED_OK(er);
Lev Walkin942fd082004-10-03 09:13:02 +0000649cb_failed:
Lev Walkin7c1dc052016-03-14 03:08:15 -0700650 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000651}
652
Wim Lewis18c2ec92014-07-29 11:30:10 -0700653static const struct OCTET_STRING__xer_escape_table_s {
654 const char *string;
Lev Walkin9ca81892004-10-03 10:54:25 +0000655 int size;
656} OCTET_STRING__xer_escape_table[] = {
657#define OSXET(s) { s, sizeof(s) - 1 }
658 OSXET("\074\156\165\154\057\076"), /* <nul/> */
659 OSXET("\074\163\157\150\057\076"), /* <soh/> */
660 OSXET("\074\163\164\170\057\076"), /* <stx/> */
661 OSXET("\074\145\164\170\057\076"), /* <etx/> */
662 OSXET("\074\145\157\164\057\076"), /* <eot/> */
663 OSXET("\074\145\156\161\057\076"), /* <enq/> */
664 OSXET("\074\141\143\153\057\076"), /* <ack/> */
665 OSXET("\074\142\145\154\057\076"), /* <bel/> */
666 OSXET("\074\142\163\057\076"), /* <bs/> */
667 OSXET("\011"), /* \t */
668 OSXET("\012"), /* \n */
669 OSXET("\074\166\164\057\076"), /* <vt/> */
670 OSXET("\074\146\146\057\076"), /* <ff/> */
671 OSXET("\015"), /* \r */
672 OSXET("\074\163\157\057\076"), /* <so/> */
673 OSXET("\074\163\151\057\076"), /* <si/> */
674 OSXET("\074\144\154\145\057\076"), /* <dle/> */
675 OSXET("\074\144\143\061\057\076"), /* <de1/> */
676 OSXET("\074\144\143\062\057\076"), /* <de2/> */
677 OSXET("\074\144\143\063\057\076"), /* <de3/> */
678 OSXET("\074\144\143\064\057\076"), /* <de4/> */
679 OSXET("\074\156\141\153\057\076"), /* <nak/> */
680 OSXET("\074\163\171\156\057\076"), /* <syn/> */
681 OSXET("\074\145\164\142\057\076"), /* <etb/> */
682 OSXET("\074\143\141\156\057\076"), /* <can/> */
683 OSXET("\074\145\155\057\076"), /* <em/> */
684 OSXET("\074\163\165\142\057\076"), /* <sub/> */
685 OSXET("\074\145\163\143\057\076"), /* <esc/> */
686 OSXET("\074\151\163\064\057\076"), /* <is4/> */
687 OSXET("\074\151\163\063\057\076"), /* <is3/> */
688 OSXET("\074\151\163\062\057\076"), /* <is2/> */
689 OSXET("\074\151\163\061\057\076"), /* <is1/> */
690 { 0, 0 }, /* " " */
691 { 0, 0 }, /* ! */
692 { 0, 0 }, /* \" */
693 { 0, 0 }, /* # */
694 { 0, 0 }, /* $ */
695 { 0, 0 }, /* % */
696 OSXET("\046\141\155\160\073"), /* &amp; */
697 { 0, 0 }, /* ' */
698 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, /* ()*+,-./ */
699 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, /* 01234567 */
700 {0,0},{0,0},{0,0},{0,0}, /* 89:; */
701 OSXET("\046\154\164\073"), /* &lt; */
702 { 0, 0 }, /* = */
703 OSXET("\046\147\164\073"), /* &gt; */
704};
705
Lev Walkindc06f6b2004-10-20 15:50:55 +0000706static int
Lev Walkin0fab1a62005-03-09 22:19:25 +0000707OS__check_escaped_control_char(const void *buf, int size) {
Lev Walkindc06f6b2004-10-20 15:50:55 +0000708 size_t i;
709 /*
710 * Inefficient algorithm which translates the escape sequences
711 * defined above into characters. Returns -1 if not found.
712 * TODO: replace by a faster algorithm (bsearch(), hash or
713 * nested table lookups).
714 */
715 for(i = 0; i < 32 /* Don't spend time on the bottom half */; i++) {
Wim Lewis18c2ec92014-07-29 11:30:10 -0700716 const struct OCTET_STRING__xer_escape_table_s *el;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000717 el = &OCTET_STRING__xer_escape_table[i];
718 if(el->size == size && memcmp(buf, el->string, size) == 0)
719 return i;
720 }
721 return -1;
722}
723
724static int
Lev Walkin0fab1a62005-03-09 22:19:25 +0000725OCTET_STRING__handle_control_chars(void *struct_ptr, const void *chunk_buf, size_t chunk_size) {
Lev Walkindc06f6b2004-10-20 15:50:55 +0000726 /*
727 * This might be one of the escape sequences
728 * for control characters. Check it out.
729 * #11.15.5
730 */
731 int control_char = OS__check_escaped_control_char(chunk_buf,chunk_size);
732 if(control_char >= 0) {
733 OCTET_STRING_t *st = (OCTET_STRING_t *)struct_ptr;
734 void *p = REALLOC(st->buf, st->size + 2);
735 if(p) {
736 st->buf = (uint8_t *)p;
737 st->buf[st->size++] = control_char;
738 st->buf[st->size] = '\0'; /* nul-termination */
739 return 0;
740 }
741 }
742
743 return -1; /* No, it's not */
744}
745
Lev Walkina9cc46e2004-09-22 16:06:28 +0000746asn_enc_rval_t
Lev Walkindc06f6b2004-10-20 15:50:55 +0000747OCTET_STRING_encode_xer_utf8(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000748 int ilevel, enum xer_encoder_flags_e flags,
749 asn_app_consume_bytes_f *cb, void *app_key) {
750 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
751 asn_enc_rval_t er;
Lev Walkin9ca81892004-10-03 10:54:25 +0000752 uint8_t *buf, *end;
753 uint8_t *ss; /* Sequence start */
754 ssize_t encoded_len = 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000755
756 (void)ilevel; /* Unused argument */
757 (void)flags; /* Unused argument */
758
Lev Walkin2ddd3082006-10-16 12:32:14 +0000759 if(!st || (!st->buf && st->size))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700760 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000761
Lev Walkin9ca81892004-10-03 10:54:25 +0000762 buf = st->buf;
763 end = buf + st->size;
764 for(ss = buf; buf < end; buf++) {
Lev Walkin443d2512004-10-05 06:35:31 +0000765 unsigned int ch = *buf;
Lev Walkin9ca81892004-10-03 10:54:25 +0000766 int s_len; /* Special encoding sequence length */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000767
Lev Walkin9ca81892004-10-03 10:54:25 +0000768 /*
769 * Escape certain characters: X.680/11.15
770 */
771 if(ch < sizeof(OCTET_STRING__xer_escape_table)
772 /sizeof(OCTET_STRING__xer_escape_table[0])
773 && (s_len = OCTET_STRING__xer_escape_table[ch].size)) {
774 if(((buf - ss) && cb(ss, buf - ss, app_key) < 0)
775 || cb(OCTET_STRING__xer_escape_table[ch].string, s_len,
776 app_key) < 0)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700777 ASN__ENCODE_FAILED;
Lev Walkin9ca81892004-10-03 10:54:25 +0000778 encoded_len += (buf - ss) + s_len;
779 ss = buf + 1;
780 }
781 }
782
783 encoded_len += (buf - ss);
784 if((buf - ss) && cb(ss, buf - ss, app_key) < 0)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700785 ASN__ENCODE_FAILED;
Lev Walkin9ca81892004-10-03 10:54:25 +0000786
787 er.encoded = encoded_len;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700788 ASN__ENCODED_OK(er);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000789}
790
Lev Walkindc06f6b2004-10-20 15:50:55 +0000791/*
792 * Convert from hexadecimal format (cstring): "AB CD EF"
793 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000794static 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 +0000795 OCTET_STRING_t *st = (OCTET_STRING_t *)sptr;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000796 const char *chunk_stop = (const char *)chunk_buf;
797 const char *p = chunk_stop;
798 const char *pend = p + chunk_size;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000799 unsigned int clv = 0;
800 int half = 0; /* Half bit */
801 uint8_t *buf;
802
803 /* Reallocate buffer according to high cap estimation */
804 ssize_t _ns = st->size + (chunk_size + 1) / 2;
805 void *nptr = REALLOC(st->buf, _ns + 1);
806 if(!nptr) return -1;
807 st->buf = (uint8_t *)nptr;
808 buf = st->buf + st->size;
809
810 /*
811 * If something like " a b c " appears here, the " a b":3 will be
812 * converted, and the rest skipped. That is, unless buf_size is greater
813 * than chunk_size, then it'll be equivalent to "ABC0".
814 */
815 for(; p < pend; p++) {
Lev Walkin0fab1a62005-03-09 22:19:25 +0000816 int ch = *(const unsigned char *)p;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000817 switch(ch) {
818 case 0x09: case 0x0a: case 0x0c: case 0x0d:
819 case 0x20:
820 /* Ignore whitespace */
821 continue;
822 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: /*01234*/
823 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: /*56789*/
824 clv = (clv << 4) + (ch - 0x30);
825 break;
826 case 0x41: case 0x42: case 0x43: /* ABC */
827 case 0x44: case 0x45: case 0x46: /* DEF */
Lev Walkin3ae21bd2005-02-21 14:43:48 +0000828 clv = (clv << 4) + (ch - 0x41 + 10);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000829 break;
830 case 0x61: case 0x62: case 0x63: /* abc */
831 case 0x64: case 0x65: case 0x66: /* def */
Lev Walkin3ae21bd2005-02-21 14:43:48 +0000832 clv = (clv << 4) + (ch - 0x61 + 10);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000833 break;
834 default:
835 *buf = 0; /* JIC */
836 return -1;
837 }
838 if(half++) {
839 half = 0;
840 *buf++ = clv;
841 chunk_stop = p + 1;
842 }
843 }
844
845 /*
846 * Check partial decoding.
847 */
848 if(half) {
849 if(have_more) {
850 /*
851 * Partial specification is fine,
852 * because no more more PXER_TEXT data is available.
853 */
854 *buf++ = clv << 4;
855 chunk_stop = p;
856 }
857 } else {
858 chunk_stop = p;
859 }
860
861 st->size = buf - st->buf; /* Adjust the buffer size */
862 assert(st->size <= _ns);
863 st->buf[st->size] = 0; /* Courtesy termination */
864
Lev Walkin0fab1a62005-03-09 22:19:25 +0000865 return (chunk_stop - (const char *)chunk_buf); /* Converted size */
Lev Walkindc06f6b2004-10-20 15:50:55 +0000866}
867
868/*
869 * Convert from binary format: "00101011101"
870 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000871static 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 +0000872 BIT_STRING_t *st = (BIT_STRING_t *)sptr;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000873 const char *p = (const char *)chunk_buf;
874 const char *pend = p + chunk_size;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000875 int bits_unused = st->bits_unused & 0x7;
876 uint8_t *buf;
877
878 /* Reallocate buffer according to high cap estimation */
879 ssize_t _ns = st->size + (chunk_size + 7) / 8;
880 void *nptr = REALLOC(st->buf, _ns + 1);
881 if(!nptr) return -1;
882 st->buf = (uint8_t *)nptr;
883 buf = st->buf + st->size;
884
885 (void)have_more;
886
887 if(bits_unused == 0)
888 bits_unused = 8;
889 else if(st->size)
890 buf--;
891
892 /*
893 * Convert series of 0 and 1 into the octet string.
894 */
895 for(; p < pend; p++) {
Lev Walkin0fab1a62005-03-09 22:19:25 +0000896 int ch = *(const unsigned char *)p;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000897 switch(ch) {
898 case 0x09: case 0x0a: case 0x0c: case 0x0d:
899 case 0x20:
900 /* Ignore whitespace */
901 break;
902 case 0x30:
903 case 0x31:
904 if(bits_unused-- <= 0) {
905 *++buf = 0; /* Clean the cell */
906 bits_unused = 7;
907 }
908 *buf |= (ch&1) << bits_unused;
909 break;
910 default:
911 st->bits_unused = bits_unused;
912 return -1;
913 }
914 }
915
916 if(bits_unused == 8) {
917 st->size = buf - st->buf;
918 st->bits_unused = 0;
919 } else {
920 st->size = buf - st->buf + 1;
921 st->bits_unused = bits_unused;
922 }
923
924 assert(st->size <= _ns);
925 st->buf[st->size] = 0; /* Courtesy termination */
926
927 return chunk_size; /* Converted in full */
928}
929
930/*
931 * Something like strtod(), but with stricter rules.
932 */
933static int
Lev Walkin0fab1a62005-03-09 22:19:25 +0000934OS__strtoent(int base, const char *buf, const char *end, int32_t *ret_value) {
Lev Walkin33700162004-10-26 09:03:31 +0000935 int32_t val = 0;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000936 const char *p;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000937
938 for(p = buf; p < end; p++) {
939 int ch = *p;
Lev Walkinb0f3db62005-07-03 05:30:15 +0000940
941 /* Strange huge value */
942 if((val * base + base) < 0)
943 return -1;
944
Lev Walkindc06f6b2004-10-20 15:50:55 +0000945 switch(ch) {
946 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: /*01234*/
947 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: /*56789*/
948 val = val * base + (ch - 0x30);
949 break;
950 case 0x41: case 0x42: case 0x43: /* ABC */
951 case 0x44: case 0x45: case 0x46: /* DEF */
Lev Walkin3ae21bd2005-02-21 14:43:48 +0000952 val = val * base + (ch - 0x41 + 10);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000953 break;
954 case 0x61: case 0x62: case 0x63: /* abc */
955 case 0x64: case 0x65: case 0x66: /* def */
Lev Walkin3ae21bd2005-02-21 14:43:48 +0000956 val = val * base + (ch - 0x61 + 10);
Lev Walkindc06f6b2004-10-20 15:50:55 +0000957 break;
958 case 0x3b: /* ';' */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000959 *ret_value = val;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000960 return (p - buf) + 1;
961 default:
962 return -1; /* Character set error */
963 }
964 }
965
Lev Walkinb0f3db62005-07-03 05:30:15 +0000966 *ret_value = -1;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000967 return (p - buf);
968}
969
970/*
971 * Convert from the plain UTF-8 format, expanding entity references: "2 &lt; 3"
972 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000973static 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 +0000974 OCTET_STRING_t *st = (OCTET_STRING_t *)sptr;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000975 const char *p = (const char *)chunk_buf;
976 const char *pend = p + chunk_size;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000977 uint8_t *buf;
978
979 /* Reallocate buffer */
980 ssize_t _ns = st->size + chunk_size;
981 void *nptr = REALLOC(st->buf, _ns + 1);
982 if(!nptr) return -1;
983 st->buf = (uint8_t *)nptr;
984 buf = st->buf + st->size;
985
986 /*
987 * Convert series of 0 and 1 into the octet string.
988 */
989 for(; p < pend; p++) {
Lev Walkin0fab1a62005-03-09 22:19:25 +0000990 int ch = *(const unsigned char *)p;
Lev Walkindc06f6b2004-10-20 15:50:55 +0000991 int len; /* Length of the rest of the chunk */
992
993 if(ch != 0x26 /* '&' */) {
994 *buf++ = ch;
995 continue; /* That was easy... */
996 }
997
998 /*
999 * Process entity reference.
1000 */
Lev Walkin0fab1a62005-03-09 22:19:25 +00001001 len = chunk_size - (p - (const char *)chunk_buf);
Lev Walkindc06f6b2004-10-20 15:50:55 +00001002 if(len == 1 /* "&" */) goto want_more;
1003 if(p[1] == 0x23 /* '#' */) {
Lev Walkin0fab1a62005-03-09 22:19:25 +00001004 const char *pval; /* Pointer to start of digits */
Lev Walkinbc0dce32006-07-27 09:56:13 +00001005 int32_t val = 0; /* Entity reference value */
Lev Walkindc06f6b2004-10-20 15:50:55 +00001006 int base;
1007
1008 if(len == 2 /* "&#" */) goto want_more;
1009 if(p[2] == 0x78 /* 'x' */)
1010 pval = p + 3, base = 16;
1011 else
1012 pval = p + 2, base = 10;
1013 len = OS__strtoent(base, pval, p + len, &val);
1014 if(len == -1) {
1015 /* Invalid charset. Just copy verbatim. */
1016 *buf++ = ch;
1017 continue;
1018 }
1019 if(!len || pval[len-1] != 0x3b) goto want_more;
1020 assert(val > 0);
1021 p += (pval - p) + len - 1; /* Advance past entref */
1022
1023 if(val < 0x80) {
1024 *buf++ = (char)val;
1025 } else if(val < 0x800) {
1026 *buf++ = 0xc0 | ((val >> 6));
1027 *buf++ = 0x80 | ((val & 0x3f));
1028 } else if(val < 0x10000) {
1029 *buf++ = 0xe0 | ((val >> 12));
1030 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1031 *buf++ = 0x80 | ((val & 0x3f));
1032 } else if(val < 0x200000) {
1033 *buf++ = 0xf0 | ((val >> 18));
1034 *buf++ = 0x80 | ((val >> 12) & 0x3f);
1035 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1036 *buf++ = 0x80 | ((val & 0x3f));
1037 } else if(val < 0x4000000) {
1038 *buf++ = 0xf8 | ((val >> 24));
1039 *buf++ = 0x80 | ((val >> 18) & 0x3f);
1040 *buf++ = 0x80 | ((val >> 12) & 0x3f);
1041 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1042 *buf++ = 0x80 | ((val & 0x3f));
1043 } else {
1044 *buf++ = 0xfc | ((val >> 30) & 0x1);
1045 *buf++ = 0x80 | ((val >> 24) & 0x3f);
1046 *buf++ = 0x80 | ((val >> 18) & 0x3f);
1047 *buf++ = 0x80 | ((val >> 12) & 0x3f);
1048 *buf++ = 0x80 | ((val >> 6) & 0x3f);
1049 *buf++ = 0x80 | ((val & 0x3f));
1050 }
1051 } else {
1052 /*
1053 * Ugly, limited parsing of &amp; &gt; &lt;
1054 */
1055 char *sc = (char *)memchr(p, 0x3b, len > 5 ? 5 : len);
1056 if(!sc) goto want_more;
1057 if((sc - p) == 4
1058 && p[1] == 0x61 /* 'a' */
1059 && p[2] == 0x6d /* 'm' */
1060 && p[3] == 0x70 /* 'p' */) {
1061 *buf++ = 0x26;
1062 p = sc;
1063 continue;
1064 }
1065 if((sc - p) == 3) {
1066 if(p[1] == 0x6c) {
1067 *buf = 0x3c; /* '<' */
1068 } else if(p[1] == 0x67) {
1069 *buf = 0x3e; /* '>' */
1070 } else {
1071 /* Unsupported entity reference */
1072 *buf++ = ch;
1073 continue;
1074 }
1075 if(p[2] != 0x74) {
1076 /* Unsupported entity reference */
1077 *buf++ = ch;
1078 continue;
1079 }
1080 buf++;
1081 p = sc;
1082 continue;
1083 }
1084 /* Unsupported entity reference */
1085 *buf++ = ch;
1086 }
1087
1088 continue;
1089 want_more:
1090 if(have_more) {
1091 /*
1092 * We know that no more data (of the same type)
1093 * is coming. Copy the rest verbatim.
1094 */
1095 *buf++ = ch;
1096 continue;
1097 }
Lev Walkin0fab1a62005-03-09 22:19:25 +00001098 chunk_size = (p - (const char *)chunk_buf);
Lev Walkindc06f6b2004-10-20 15:50:55 +00001099 /* Processing stalled: need more data */
Lev Walkinbdaae772005-02-18 12:25:47 +00001100 break;
Lev Walkindc06f6b2004-10-20 15:50:55 +00001101 }
1102
1103 st->size = buf - st->buf;
1104 assert(st->size <= _ns);
1105 st->buf[st->size] = 0; /* Courtesy termination */
1106
1107 return chunk_size; /* Converted in full */
1108}
1109
1110/*
1111 * Decode OCTET STRING from the XML element's body.
1112 */
1113static asn_dec_rval_t
1114OCTET_STRING__decode_xer(asn_codec_ctx_t *opt_codec_ctx,
1115 asn_TYPE_descriptor_t *td, void **sptr,
Lev Walkin8c3b8542005-03-10 18:52:02 +00001116 const char *opt_mname, const void *buf_ptr, size_t size,
Lev Walkindc06f6b2004-10-20 15:50:55 +00001117 int (*opt_unexpected_tag_decoder)
Lev Walkin0fab1a62005-03-09 22:19:25 +00001118 (void *struct_ptr, const void *chunk_buf, size_t chunk_size),
Lev Walkindc06f6b2004-10-20 15:50:55 +00001119 ssize_t (*body_receiver)
Lev Walkin0fab1a62005-03-09 22:19:25 +00001120 (void *struct_ptr, const void *chunk_buf, size_t chunk_size,
Lev Walkindc06f6b2004-10-20 15:50:55 +00001121 int have_more)
1122) {
Lev Walkind5125642005-02-14 20:08:00 +00001123 OCTET_STRING_t *st = (OCTET_STRING_t *)*sptr;
Lev Walkindc06f6b2004-10-20 15:50:55 +00001124 asn_OCTET_STRING_specifics_t *specs = td->specifics
1125 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +08001126 : &asn_SPC_OCTET_STRING_specs;
Lev Walkindc06f6b2004-10-20 15:50:55 +00001127 const char *xml_tag = opt_mname ? opt_mname : td->xml_tag;
1128 asn_struct_ctx_t *ctx; /* Per-structure parser context */
Lev Walkinc61f3862005-02-14 17:21:22 +00001129 asn_dec_rval_t rval; /* Return value from the decoder */
1130 int st_allocated;
Lev Walkindc06f6b2004-10-20 15:50:55 +00001131
1132 /*
1133 * Create the string if does not exist.
1134 */
Lev Walkinc61f3862005-02-14 17:21:22 +00001135 if(!st) {
Lev Walkin8484ed82004-12-14 13:31:01 +00001136 st = (OCTET_STRING_t *)CALLOC(1, specs->struct_size);
Lev Walkin8484ed82004-12-14 13:31:01 +00001137 *sptr = (void *)st;
Lev Walkinc61f3862005-02-14 17:21:22 +00001138 if(!st) goto sta_failed;
1139 st_allocated = 1;
Lev Walkinbdaae772005-02-18 12:25:47 +00001140 } else {
1141 st_allocated = 0;
1142 }
Lev Walkinc61f3862005-02-14 17:21:22 +00001143 if(!st->buf) {
1144 /* This is separate from above section */
1145 st->buf = (uint8_t *)CALLOC(1, 1);
1146 if(!st->buf) {
1147 if(st_allocated) {
1148 *sptr = 0;
1149 goto stb_failed;
1150 } else {
1151 goto sta_failed;
1152 }
Lev Walkindc06f6b2004-10-20 15:50:55 +00001153 }
1154 }
1155
1156 /* Restore parsing context */
Lev Walkinaa61a0f2014-01-13 23:08:47 -08001157 ctx = (asn_struct_ctx_t *)(((char *)*sptr) + specs->ctx_offset);
Lev Walkindc06f6b2004-10-20 15:50:55 +00001158
1159 return xer_decode_general(opt_codec_ctx, ctx, *sptr, xml_tag,
1160 buf_ptr, size, opt_unexpected_tag_decoder, body_receiver);
Lev Walkinc61f3862005-02-14 17:21:22 +00001161
1162stb_failed:
1163 FREEMEM(st);
1164sta_failed:
1165 rval.code = RC_FAIL;
1166 rval.consumed = 0;
1167 return rval;
Lev Walkindc06f6b2004-10-20 15:50:55 +00001168}
1169
1170/*
1171 * Decode OCTET STRING from the hexadecimal data.
1172 */
1173asn_dec_rval_t
1174OCTET_STRING_decode_xer_hex(asn_codec_ctx_t *opt_codec_ctx,
1175 asn_TYPE_descriptor_t *td, void **sptr,
Lev Walkin8c3b8542005-03-10 18:52:02 +00001176 const char *opt_mname, const void *buf_ptr, size_t size) {
Lev Walkindc06f6b2004-10-20 15:50:55 +00001177 return OCTET_STRING__decode_xer(opt_codec_ctx, td, sptr, opt_mname,
1178 buf_ptr, size, 0, OCTET_STRING__convert_hexadecimal);
1179}
1180
1181/*
1182 * Decode OCTET STRING from the binary (0/1) data.
1183 */
1184asn_dec_rval_t
1185OCTET_STRING_decode_xer_binary(asn_codec_ctx_t *opt_codec_ctx,
1186 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_binary);
1190}
1191
1192/*
1193 * Decode OCTET STRING from the string (ASCII/UTF-8) data.
1194 */
1195asn_dec_rval_t
1196OCTET_STRING_decode_xer_utf8(asn_codec_ctx_t *opt_codec_ctx,
1197 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,
1201 OCTET_STRING__handle_control_chars,
1202 OCTET_STRING__convert_entrefs);
1203}
1204
Lev Walkin725883b2006-10-09 12:07:58 +00001205static int
Lev Walkin3a4689a2006-11-24 11:20:27 +00001206OCTET_STRING_per_get_characters(asn_per_data_t *po, uint8_t *buf,
1207 size_t units, unsigned int bpc, unsigned int unit_bits,
1208 long lb, long ub, asn_per_constraints_t *pc) {
1209 uint8_t *end = buf + units * bpc;
Lev Walkin725883b2006-10-09 12:07:58 +00001210
1211 ASN_DEBUG("Expanding %d characters into (%ld..%ld):%d",
Lev Walkin3a4689a2006-11-24 11:20:27 +00001212 (int)units, lb, ub, unit_bits);
Lev Walkin725883b2006-10-09 12:07:58 +00001213
1214 /* X.691: 27.5.4 */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001215 if((unsigned long)ub <= ((unsigned long)2 << (unit_bits - 1))) {
Lev Walkin725883b2006-10-09 12:07:58 +00001216 /* Decode without translation */
1217 lb = 0;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001218 } else if(pc && pc->code2value) {
1219 if(unit_bits > 16)
1220 return 1; /* FATAL: can't have constrained
1221 * UniversalString with more than
1222 * 16 million code points */
1223 for(; buf < end; buf += bpc) {
Lev Walkin725883b2006-10-09 12:07:58 +00001224 int value;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001225 int code = per_get_few_bits(po, unit_bits);
Lev Walkin725883b2006-10-09 12:07:58 +00001226 if(code < 0) return -1; /* WMORE */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001227 value = pc->code2value(code);
Lev Walkin725883b2006-10-09 12:07:58 +00001228 if(value < 0) {
1229 ASN_DEBUG("Code %d (0x%02x) is"
1230 " not in map (%ld..%ld)",
1231 code, code, lb, ub);
1232 return 1; /* FATAL */
1233 }
Lev Walkin3a4689a2006-11-24 11:20:27 +00001234 switch(bpc) {
1235 case 1: *buf = value; break;
1236 case 2: buf[0] = value >> 8; buf[1] = value; break;
1237 case 4: buf[0] = value >> 24; buf[1] = value >> 16;
1238 buf[2] = value >> 8; buf[3] = value; break;
1239 }
Lev Walkin725883b2006-10-09 12:07:58 +00001240 }
1241 return 0;
1242 }
1243
Lev Walkin3a4689a2006-11-24 11:20:27 +00001244 /* Shortcut the no-op copying to the aligned structure */
1245 if(lb == 0 && (unit_bits == 8 * bpc)) {
1246 return per_get_many_bits(po, buf, 0, unit_bits * units);
1247 }
1248
1249 for(; buf < end; buf += bpc) {
Lev Walkin725883b2006-10-09 12:07:58 +00001250 int code = per_get_few_bits(po, unit_bits);
1251 int ch = code + lb;
1252 if(code < 0) return -1; /* WMORE */
1253 if(ch > ub) {
1254 ASN_DEBUG("Code %d is out of range (%ld..%ld)",
1255 ch, lb, ub);
1256 return 1; /* FATAL */
1257 }
Lev Walkin3a4689a2006-11-24 11:20:27 +00001258 switch(bpc) {
1259 case 1: *buf = ch; break;
1260 case 2: buf[0] = ch >> 8; buf[1] = ch; break;
1261 case 4: buf[0] = ch >> 24; buf[1] = ch >> 16;
1262 buf[2] = ch >> 8; buf[3] = ch; break;
1263 }
Lev Walkin725883b2006-10-09 12:07:58 +00001264 }
1265
1266 return 0;
1267}
1268
1269static int
Lev Walkin3a4689a2006-11-24 11:20:27 +00001270OCTET_STRING_per_put_characters(asn_per_outp_t *po, const uint8_t *buf,
1271 size_t units, unsigned int bpc, unsigned int unit_bits,
1272 long lb, long ub, asn_per_constraints_t *pc) {
1273 const uint8_t *end = buf + units * bpc;
Lev Walkin725883b2006-10-09 12:07:58 +00001274
Lev Walkin3a4689a2006-11-24 11:20:27 +00001275 ASN_DEBUG("Squeezing %d characters into (%ld..%ld):%d (%d bpc)",
1276 (int)units, lb, ub, unit_bits, bpc);
Lev Walkin725883b2006-10-09 12:07:58 +00001277
1278 /* X.691: 27.5.4 */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001279 if((unsigned long)ub <= ((unsigned long)2 << (unit_bits - 1))) {
Lev Walkin725883b2006-10-09 12:07:58 +00001280 /* Encode as is */
1281 lb = 0;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001282 } else if(pc && pc->value2code) {
1283 for(; buf < end; buf += bpc) {
1284 int code;
1285 uint32_t value;
1286 switch(bpc) {
1287 case 1: value = *(const uint8_t *)buf; break;
1288 case 2: value = (buf[0] << 8) | buf[1]; break;
1289 case 4: value = (buf[0] << 24) | (buf[1] << 16)
1290 | (buf[2] << 8) | buf[3]; break;
1291 default: return -1;
1292 }
1293 code = pc->value2code(value);
Lev Walkin725883b2006-10-09 12:07:58 +00001294 if(code < 0) {
1295 ASN_DEBUG("Character %d (0x%02x) is"
1296 " not in map (%ld..%ld)",
1297 *buf, *buf, lb, ub);
1298 return -1;
1299 }
1300 if(per_put_few_bits(po, code, unit_bits))
1301 return -1;
1302 }
1303 }
1304
Lev Walkin3a4689a2006-11-24 11:20:27 +00001305 /* Shortcut the no-op copying to the aligned structure */
1306 if(lb == 0 && (unit_bits == 8 * bpc)) {
1307 return per_put_many_bits(po, buf, unit_bits * units);
1308 }
1309
1310 for(ub -= lb; buf < end; buf += bpc) {
1311 int ch;
1312 uint32_t value;
1313 switch(bpc) {
1314 case 1: value = *(const uint8_t *)buf; break;
1315 case 2: value = (buf[0] << 8) | buf[1]; break;
1316 case 4: value = (buf[0] << 24) | (buf[1] << 16)
1317 | (buf[2] << 8) | buf[3]; break;
1318 default: return -1;
1319 }
1320 ch = value - lb;
Lev Walkin725883b2006-10-09 12:07:58 +00001321 if(ch < 0 || ch > ub) {
1322 ASN_DEBUG("Character %d (0x%02x)"
1323 " is out of range (%ld..%ld)",
1324 *buf, *buf, lb, ub + lb);
1325 return -1;
1326 }
1327 if(per_put_few_bits(po, ch, unit_bits))
1328 return -1;
1329 }
1330
1331 return 0;
1332}
1333
Lev Walkin59b176e2005-11-26 11:25:14 +00001334asn_dec_rval_t
1335OCTET_STRING_decode_uper(asn_codec_ctx_t *opt_codec_ctx,
1336 asn_TYPE_descriptor_t *td, asn_per_constraints_t *constraints,
1337 void **sptr, asn_per_data_t *pd) {
1338
1339 asn_OCTET_STRING_specifics_t *specs = td->specifics
1340 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +08001341 : &asn_SPC_OCTET_STRING_specs;
Lev Walkin725883b2006-10-09 12:07:58 +00001342 asn_per_constraints_t *pc = constraints ? constraints
1343 : td->per_constraints;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001344 asn_per_constraint_t *cval;
1345 asn_per_constraint_t *csiz;
Lev Walkin59b176e2005-11-26 11:25:14 +00001346 asn_dec_rval_t rval = { RC_OK, 0 };
1347 BIT_STRING_t *st = (BIT_STRING_t *)*sptr;
1348 ssize_t consumed_myself = 0;
1349 int repeat;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001350 enum {
1351 OS__BPC_BIT = 0,
1352 OS__BPC_CHAR = 1,
1353 OS__BPC_U16 = 2,
1354 OS__BPC_U32 = 4
1355 } bpc; /* Bytes per character */
1356 unsigned int unit_bits;
1357 unsigned int canonical_unit_bits;
Lev Walkin59b176e2005-11-26 11:25:14 +00001358
1359 (void)opt_codec_ctx;
1360
Lev Walkin3a4689a2006-11-24 11:20:27 +00001361 if(pc) {
1362 cval = &pc->value;
1363 csiz = &pc->size;
1364 } else {
1365 cval = &asn_DEF_OCTET_STRING_constraints.value;
1366 csiz = &asn_DEF_OCTET_STRING_constraints.size;
1367 }
1368
1369 switch(specs->subvariant) {
1370 default:
1371 case ASN_OSUBV_ANY:
1372 ASN_DEBUG("Unrecognized subvariant %d", specs->subvariant);
1373 RETURN(RC_FAIL);
1374 case ASN_OSUBV_BIT:
1375 canonical_unit_bits = unit_bits = 1;
1376 bpc = OS__BPC_BIT;
1377 break;
1378 case ASN_OSUBV_STR:
1379 canonical_unit_bits = unit_bits = 8;
1380 if(cval->flags & APC_CONSTRAINED)
1381 unit_bits = cval->range_bits;
1382 bpc = OS__BPC_CHAR;
1383 break;
1384 case ASN_OSUBV_U16:
1385 canonical_unit_bits = unit_bits = 16;
1386 if(cval->flags & APC_CONSTRAINED)
1387 unit_bits = cval->range_bits;
1388 bpc = OS__BPC_U16;
1389 break;
1390 case ASN_OSUBV_U32:
1391 canonical_unit_bits = unit_bits = 32;
1392 if(cval->flags & APC_CONSTRAINED)
1393 unit_bits = cval->range_bits;
1394 bpc = OS__BPC_U32;
1395 break;
1396 }
1397
Lev Walkin59b176e2005-11-26 11:25:14 +00001398 /*
1399 * Allocate the string.
1400 */
1401 if(!st) {
1402 st = (BIT_STRING_t *)(*sptr = CALLOC(1, specs->struct_size));
1403 if(!st) RETURN(RC_FAIL);
1404 }
1405
Lev Walkin725883b2006-10-09 12:07:58 +00001406 ASN_DEBUG("PER Decoding %s size %ld .. %ld bits %d",
Lev Walkin3a4689a2006-11-24 11:20:27 +00001407 csiz->flags & APC_EXTENSIBLE ? "extensible" : "non-extensible",
1408 csiz->lower_bound, csiz->upper_bound, csiz->effective_bits);
Lev Walkince676dd2005-12-20 22:34:55 +00001409
Lev Walkin3a4689a2006-11-24 11:20:27 +00001410 if(csiz->flags & APC_EXTENSIBLE) {
Lev Walkin59b176e2005-11-26 11:25:14 +00001411 int inext = per_get_few_bits(pd, 1);
Lev Walkin0a8aa602006-09-18 20:05:55 +00001412 if(inext < 0) RETURN(RC_WMORE);
Lev Walkin3a4689a2006-11-24 11:20:27 +00001413 if(inext) {
1414 csiz = &asn_DEF_OCTET_STRING_constraints.size;
1415 cval = &asn_DEF_OCTET_STRING_constraints.value;
1416 unit_bits = canonical_unit_bits;
1417 }
Lev Walkin59b176e2005-11-26 11:25:14 +00001418 }
1419
Lev Walkin3a4689a2006-11-24 11:20:27 +00001420 if(csiz->effective_bits >= 0) {
Lev Walkin59b176e2005-11-26 11:25:14 +00001421 FREEMEM(st->buf);
Lev Walkin3a4689a2006-11-24 11:20:27 +00001422 if(bpc) {
1423 st->size = csiz->upper_bound * bpc;
Lev Walkin59b176e2005-11-26 11:25:14 +00001424 } else {
Lev Walkin3a4689a2006-11-24 11:20:27 +00001425 st->size = (csiz->upper_bound + 7) >> 3;
Lev Walkin59b176e2005-11-26 11:25:14 +00001426 }
1427 st->buf = (uint8_t *)MALLOC(st->size + 1);
1428 if(!st->buf) { st->size = 0; RETURN(RC_FAIL); }
1429 }
1430
1431 /* X.691, #16.5: zero-length encoding */
1432 /* X.691, #16.6: short fixed length encoding (up to 2 octets) */
1433 /* X.691, #16.7: long fixed length encoding (up to 64K octets) */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001434 if(csiz->effective_bits == 0) {
Lev Walkin725883b2006-10-09 12:07:58 +00001435 int ret;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001436 if(bpc) {
1437 ASN_DEBUG("Encoding OCTET STRING size %ld",
1438 csiz->upper_bound);
1439 ret = OCTET_STRING_per_get_characters(pd, st->buf,
1440 csiz->upper_bound, bpc, unit_bits,
1441 cval->lower_bound, cval->upper_bound, pc);
Lev Walkin725883b2006-10-09 12:07:58 +00001442 if(ret > 0) RETURN(RC_FAIL);
1443 } else {
Lev Walkin3a4689a2006-11-24 11:20:27 +00001444 ASN_DEBUG("Encoding BIT STRING size %ld",
1445 csiz->upper_bound);
Lev Walkin725883b2006-10-09 12:07:58 +00001446 ret = per_get_many_bits(pd, st->buf, 0,
Lev Walkin3a4689a2006-11-24 11:20:27 +00001447 unit_bits * csiz->upper_bound);
Lev Walkin725883b2006-10-09 12:07:58 +00001448 }
Lev Walkin0a8aa602006-09-18 20:05:55 +00001449 if(ret < 0) RETURN(RC_WMORE);
Lev Walkin3a4689a2006-11-24 11:20:27 +00001450 consumed_myself += unit_bits * csiz->upper_bound;
Lev Walkin59b176e2005-11-26 11:25:14 +00001451 st->buf[st->size] = 0;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001452 if(bpc == 0) {
1453 int ubs = (csiz->upper_bound & 0x7);
1454 st->bits_unused = ubs ? 8 - ubs : 0;
1455 }
Lev Walkin59b176e2005-11-26 11:25:14 +00001456 RETURN(RC_OK);
1457 }
1458
1459 st->size = 0;
1460 do {
Lev Walkin3a4689a2006-11-24 11:20:27 +00001461 ssize_t raw_len;
Lev Walkin59b176e2005-11-26 11:25:14 +00001462 ssize_t len_bytes;
1463 ssize_t len_bits;
1464 void *p;
1465 int ret;
1466
1467 /* Get the PER length */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001468 raw_len = uper_get_length(pd, csiz->effective_bits, &repeat);
1469 if(raw_len < 0) RETURN(RC_WMORE);
1470 raw_len += csiz->lower_bound;
Lev Walkin59b176e2005-11-26 11:25:14 +00001471
Lev Walkin0a8aa602006-09-18 20:05:55 +00001472 ASN_DEBUG("Got PER length eb %ld, len %ld, %s (%s)",
Lev Walkin3a4689a2006-11-24 11:20:27 +00001473 (long)csiz->effective_bits, (long)raw_len,
Lev Walkin0a8aa602006-09-18 20:05:55 +00001474 repeat ? "repeat" : "once", td->name);
Lev Walkin3a4689a2006-11-24 11:20:27 +00001475 if(bpc) {
1476 len_bytes = raw_len * bpc;
1477 len_bits = len_bytes * unit_bits;
1478 } else {
1479 len_bits = raw_len;
Lev Walkin59b176e2005-11-26 11:25:14 +00001480 len_bytes = (len_bits + 7) >> 3;
1481 if(len_bits & 0x7)
1482 st->bits_unused = 8 - (len_bits & 0x7);
1483 /* len_bits be multiple of 16K if repeat is set */
Lev Walkin59b176e2005-11-26 11:25:14 +00001484 }
1485 p = REALLOC(st->buf, st->size + len_bytes + 1);
1486 if(!p) RETURN(RC_FAIL);
1487 st->buf = (uint8_t *)p;
1488
Lev Walkin3a4689a2006-11-24 11:20:27 +00001489 if(bpc) {
1490 ret = OCTET_STRING_per_get_characters(pd,
1491 &st->buf[st->size], raw_len, bpc, unit_bits,
1492 cval->lower_bound, cval->upper_bound, pc);
Lev Walkin725883b2006-10-09 12:07:58 +00001493 if(ret > 0) RETURN(RC_FAIL);
1494 } else {
1495 ret = per_get_many_bits(pd, &st->buf[st->size],
1496 0, len_bits);
1497 }
Lev Walkin0a8aa602006-09-18 20:05:55 +00001498 if(ret < 0) RETURN(RC_WMORE);
Lev Walkin59b176e2005-11-26 11:25:14 +00001499 st->size += len_bytes;
1500 } while(repeat);
1501 st->buf[st->size] = 0; /* nul-terminate */
1502
1503 return rval;
1504}
Lev Walkindc06f6b2004-10-20 15:50:55 +00001505
Lev Walkin523de9e2006-08-18 01:34:18 +00001506asn_enc_rval_t
1507OCTET_STRING_encode_uper(asn_TYPE_descriptor_t *td,
1508 asn_per_constraints_t *constraints, void *sptr, asn_per_outp_t *po) {
1509
1510 asn_OCTET_STRING_specifics_t *specs = td->specifics
1511 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +08001512 : &asn_SPC_OCTET_STRING_specs;
Lev Walkin725883b2006-10-09 12:07:58 +00001513 asn_per_constraints_t *pc = constraints ? constraints
1514 : td->per_constraints;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001515 asn_per_constraint_t *cval;
1516 asn_per_constraint_t *csiz;
Lev Walkin523de9e2006-08-18 01:34:18 +00001517 const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001518 asn_enc_rval_t er = { 0, 0, 0 };
Lev Walkin523de9e2006-08-18 01:34:18 +00001519 int inext = 0; /* Lies not within extension root */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001520 unsigned int unit_bits;
1521 unsigned int canonical_unit_bits;
1522 unsigned int sizeinunits;
Lev Walkin523de9e2006-08-18 01:34:18 +00001523 const uint8_t *buf;
1524 int ret;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001525 enum {
1526 OS__BPC_BIT = 0,
1527 OS__BPC_CHAR = 1,
1528 OS__BPC_U16 = 2,
1529 OS__BPC_U32 = 4
1530 } bpc; /* Bytes per character */
1531 int ct_extensible;
Lev Walkin523de9e2006-08-18 01:34:18 +00001532
Lev Walkin2ddd3082006-10-16 12:32:14 +00001533 if(!st || (!st->buf && st->size))
Lev Walkin7c1dc052016-03-14 03:08:15 -07001534 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001535
Lev Walkin3a4689a2006-11-24 11:20:27 +00001536 if(pc) {
1537 cval = &pc->value;
1538 csiz = &pc->size;
1539 } else {
1540 cval = &asn_DEF_OCTET_STRING_constraints.value;
1541 csiz = &asn_DEF_OCTET_STRING_constraints.size;
1542 }
1543 ct_extensible = csiz->flags & APC_EXTENSIBLE;
1544
1545 switch(specs->subvariant) {
1546 default:
1547 case ASN_OSUBV_ANY:
Lev Walkin7c1dc052016-03-14 03:08:15 -07001548 ASN__ENCODE_FAILED;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001549 case ASN_OSUBV_BIT:
1550 canonical_unit_bits = unit_bits = 1;
1551 bpc = OS__BPC_BIT;
1552 sizeinunits = st->size * 8 - (st->bits_unused & 0x07);
Lev Walkin523de9e2006-08-18 01:34:18 +00001553 ASN_DEBUG("BIT STRING of %d bytes, %d bits unused",
1554 sizeinunits, st->bits_unused);
Lev Walkin3a4689a2006-11-24 11:20:27 +00001555 break;
1556 case ASN_OSUBV_STR:
1557 canonical_unit_bits = unit_bits = 8;
1558 if(cval->flags & APC_CONSTRAINED)
1559 unit_bits = cval->range_bits;
1560 bpc = OS__BPC_CHAR;
1561 sizeinunits = st->size;
1562 break;
1563 case ASN_OSUBV_U16:
1564 canonical_unit_bits = unit_bits = 16;
1565 if(cval->flags & APC_CONSTRAINED)
1566 unit_bits = cval->range_bits;
1567 bpc = OS__BPC_U16;
1568 sizeinunits = st->size / 2;
1569 break;
1570 case ASN_OSUBV_U32:
1571 canonical_unit_bits = unit_bits = 32;
1572 if(cval->flags & APC_CONSTRAINED)
1573 unit_bits = cval->range_bits;
1574 bpc = OS__BPC_U32;
1575 sizeinunits = st->size / 4;
1576 break;
Lev Walkin725883b2006-10-09 12:07:58 +00001577 }
1578
Lev Walkin00918812006-09-18 21:19:32 +00001579 ASN_DEBUG("Encoding %s into %d units of %d bits"
Lev Walkin725883b2006-10-09 12:07:58 +00001580 " (%ld..%ld, effective %d)%s",
Lev Walkin00918812006-09-18 21:19:32 +00001581 td->name, sizeinunits, unit_bits,
Lev Walkin3a4689a2006-11-24 11:20:27 +00001582 csiz->lower_bound, csiz->upper_bound,
1583 csiz->effective_bits, ct_extensible ? " EXT" : "");
Lev Walkin523de9e2006-08-18 01:34:18 +00001584
Lev Walkind4c16732013-03-28 05:00:47 -07001585 /* Figure out whether size lies within PER visible constraint */
Lev Walkin523de9e2006-08-18 01:34:18 +00001586
Lev Walkin3a4689a2006-11-24 11:20:27 +00001587 if(csiz->effective_bits >= 0) {
1588 if((int)sizeinunits < csiz->lower_bound
1589 || (int)sizeinunits > csiz->upper_bound) {
Lev Walkin523de9e2006-08-18 01:34:18 +00001590 if(ct_extensible) {
Lev Walkin3a4689a2006-11-24 11:20:27 +00001591 cval = &asn_DEF_OCTET_STRING_constraints.value;
1592 csiz = &asn_DEF_OCTET_STRING_constraints.size;
1593 unit_bits = canonical_unit_bits;
Lev Walkin523de9e2006-08-18 01:34:18 +00001594 inext = 1;
1595 } else
Lev Walkin7c1dc052016-03-14 03:08:15 -07001596 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001597 }
1598 } else {
1599 inext = 0;
1600 }
1601
1602 if(ct_extensible) {
1603 /* Declare whether length is [not] within extension root */
1604 if(per_put_few_bits(po, inext, 1))
Lev Walkin7c1dc052016-03-14 03:08:15 -07001605 ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001606 }
1607
1608 /* X.691, #16.5: zero-length encoding */
1609 /* X.691, #16.6: short fixed length encoding (up to 2 octets) */
1610 /* X.691, #16.7: long fixed length encoding (up to 64K octets) */
Lev Walkin3a4689a2006-11-24 11:20:27 +00001611 if(csiz->effective_bits >= 0) {
Lev Walkin523de9e2006-08-18 01:34:18 +00001612 ASN_DEBUG("Encoding %d bytes (%ld), length in %d bits",
Lev Walkin3a4689a2006-11-24 11:20:27 +00001613 st->size, sizeinunits - csiz->lower_bound,
1614 csiz->effective_bits);
1615 ret = per_put_few_bits(po, sizeinunits - csiz->lower_bound,
1616 csiz->effective_bits);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001617 if(ret) ASN__ENCODE_FAILED;
Lev Walkin3a4689a2006-11-24 11:20:27 +00001618 if(bpc) {
1619 ret = OCTET_STRING_per_put_characters(po, st->buf,
1620 sizeinunits, bpc, unit_bits,
1621 cval->lower_bound, cval->upper_bound, pc);
Lev Walkin725883b2006-10-09 12:07:58 +00001622 } else {
1623 ret = per_put_many_bits(po, st->buf,
1624 sizeinunits * unit_bits);
1625 }
Lev Walkin7c1dc052016-03-14 03:08:15 -07001626 if(ret) ASN__ENCODE_FAILED;
1627 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +00001628 }
1629
1630 ASN_DEBUG("Encoding %d bytes", st->size);
1631
1632 if(sizeinunits == 0) {
1633 if(uper_put_length(po, 0))
Lev Walkin7c1dc052016-03-14 03:08:15 -07001634 ASN__ENCODE_FAILED;
1635 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +00001636 }
1637
1638 buf = st->buf;
1639 while(sizeinunits) {
1640 ssize_t maySave = uper_put_length(po, sizeinunits);
Lev Walkin7c1dc052016-03-14 03:08:15 -07001641 if(maySave < 0) ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001642
Lev Walkin345e4c62006-10-19 02:44:08 +00001643 ASN_DEBUG("Encoding %ld of %ld",
1644 (long)maySave, (long)sizeinunits);
Lev Walkin523de9e2006-08-18 01:34:18 +00001645
Lev Walkin3a4689a2006-11-24 11:20:27 +00001646 if(bpc) {
1647 ret = OCTET_STRING_per_put_characters(po, buf,
1648 maySave, 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, buf, maySave * unit_bits);
1652 }
Lev Walkin7c1dc052016-03-14 03:08:15 -07001653 if(ret) ASN__ENCODE_FAILED;
Lev Walkin523de9e2006-08-18 01:34:18 +00001654
Lev Walkin3a4689a2006-11-24 11:20:27 +00001655 if(bpc)
1656 buf += maySave * bpc;
Lev Walkin523de9e2006-08-18 01:34:18 +00001657 else
Lev Walkin3a4689a2006-11-24 11:20:27 +00001658 buf += maySave >> 3;
Lev Walkin523de9e2006-08-18 01:34:18 +00001659 sizeinunits -= maySave;
1660 assert(!(maySave & 0x07) || !sizeinunits);
1661 }
1662
Lev Walkin7c1dc052016-03-14 03:08:15 -07001663 ASN__ENCODED_OK(er);
Lev Walkin523de9e2006-08-18 01:34:18 +00001664}
1665
Lev Walkinf15320b2004-06-03 03:38:44 +00001666int
Lev Walkinde4825d2004-09-29 13:20:14 +00001667OCTET_STRING_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +00001668 asn_app_consume_bytes_f *cb, void *app_key) {
Wim Lewis59e8d282014-08-04 12:39:35 -07001669 const char * const h2c = "0123456789ABCDEF";
Lev Walkinc2346572004-08-11 09:07:36 +00001670 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +00001671 char scratch[16 * 3 + 4];
1672 char *p = scratch;
1673 uint8_t *buf;
1674 uint8_t *end;
1675 size_t i;
Lev Walkinf15320b2004-06-03 03:38:44 +00001676
Lev Walkind9bd7752004-06-05 08:17:50 +00001677 (void)td; /* Unused argument */
1678
Lev Walkin2ddd3082006-10-16 12:32:14 +00001679 if(!st || (!st->buf && st->size))
1680 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001681
1682 /*
1683 * Dump the contents of the buffer in hexadecimal.
1684 */
1685 buf = st->buf;
1686 end = buf + st->size;
1687 for(i = 0; buf < end; buf++, i++) {
1688 if(!(i % 16) && (i || st->size > 16)) {
Lev Walkin8e8078a2004-09-26 13:10:40 +00001689 if(cb(scratch, p - scratch, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +00001690 return -1;
Lev Walkin8e8078a2004-09-26 13:10:40 +00001691 _i_INDENT(1);
Lev Walkinf15320b2004-06-03 03:38:44 +00001692 p = scratch;
1693 }
1694 *p++ = h2c[(*buf >> 4) & 0x0F];
1695 *p++ = h2c[*buf & 0x0F];
Lev Walkina9cc46e2004-09-22 16:06:28 +00001696 *p++ = 0x20;
Lev Walkinf15320b2004-06-03 03:38:44 +00001697 }
1698
Lev Walkincc6a9102004-09-23 22:06:26 +00001699 if(p > scratch) {
1700 p--; /* Remove the tail space */
Lev Walkin8e8078a2004-09-26 13:10:40 +00001701 if(cb(scratch, p - scratch, app_key) < 0)
Lev Walkincc6a9102004-09-23 22:06:26 +00001702 return -1;
1703 }
1704
1705 return 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001706}
1707
1708int
Lev Walkindc06f6b2004-10-20 15:50:55 +00001709OCTET_STRING_print_utf8(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +00001710 int ilevel, asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +00001711 const OCTET_STRING_t *st = (const OCTET_STRING_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +00001712
Lev Walkind9bd7752004-06-05 08:17:50 +00001713 (void)td; /* Unused argument */
1714 (void)ilevel; /* Unused argument */
1715
Lev Walkin2ddd3082006-10-16 12:32:14 +00001716 if(st && (st->buf || !st->size)) {
Lev Walkin8e8078a2004-09-26 13:10:40 +00001717 return (cb(st->buf, st->size, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001718 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +00001719 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001720 }
1721}
1722
1723void
Lev Walkinde4825d2004-09-29 13:20:14 +00001724OCTET_STRING_free(asn_TYPE_descriptor_t *td, void *sptr, int contents_only) {
Lev Walkinc2346572004-08-11 09:07:36 +00001725 OCTET_STRING_t *st = (OCTET_STRING_t *)sptr;
Lev Walkin034ee192016-03-14 02:28:33 -07001726 asn_OCTET_STRING_specifics_t *specs;
1727 asn_struct_ctx_t *ctx;
Lev Walkinb54577a2004-11-08 10:47:12 +00001728 struct _stack *stck;
Lev Walkinf15320b2004-06-03 03:38:44 +00001729
1730 if(!td || !st)
1731 return;
1732
Lev Walkin034ee192016-03-14 02:28:33 -07001733 specs = td->specifics
1734 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +08001735 : &asn_SPC_OCTET_STRING_specs;
Lev Walkin034ee192016-03-14 02:28:33 -07001736 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
1737
Lev Walkinf15320b2004-06-03 03:38:44 +00001738 ASN_DEBUG("Freeing %s as OCTET STRING", td->name);
1739
1740 if(st->buf) {
1741 FREEMEM(st->buf);
Lev Walkin2ddd3082006-10-16 12:32:14 +00001742 st->buf = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001743 }
1744
1745 /*
1746 * Remove decode-time stack.
1747 */
Lev Walkinb54577a2004-11-08 10:47:12 +00001748 stck = (struct _stack *)ctx->ptr;
Lev Walkinf15320b2004-06-03 03:38:44 +00001749 if(stck) {
1750 while(stck->tail) {
1751 struct _stack_el *sel = stck->tail;
1752 stck->tail = sel->prev;
1753 FREEMEM(sel);
1754 }
1755 FREEMEM(stck);
1756 }
1757
1758 if(!contents_only) {
1759 FREEMEM(st);
1760 }
1761}
1762
1763/*
1764 * Conversion routines.
1765 */
1766int
1767OCTET_STRING_fromBuf(OCTET_STRING_t *st, const char *str, int len) {
1768 void *buf;
1769
1770 if(st == 0 || (str == 0 && len)) {
1771 errno = EINVAL;
1772 return -1;
1773 }
1774
1775 /*
1776 * Clear the OCTET STRING.
1777 */
1778 if(str == NULL) {
Lev Walkin7b284812005-12-17 11:43:25 +00001779 FREEMEM(st->buf);
1780 st->buf = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +00001781 st->size = 0;
1782 return 0;
1783 }
1784
1785 /* Determine the original string size, if not explicitly given */
1786 if(len < 0)
1787 len = strlen(str);
1788
1789 /* Allocate and fill the memory */
1790 buf = MALLOC(len + 1);
Lev Walkin7b284812005-12-17 11:43:25 +00001791 if(buf == NULL)
Lev Walkinf15320b2004-06-03 03:38:44 +00001792 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +00001793
1794 memcpy(buf, str, len);
Lev Walkin7b284812005-12-17 11:43:25 +00001795 ((uint8_t *)buf)[len] = '\0'; /* Couldn't use memcpy(len+1)! */
1796 FREEMEM(st->buf);
1797 st->buf = (uint8_t *)buf;
1798 st->size = len;
Lev Walkinf15320b2004-06-03 03:38:44 +00001799
1800 return 0;
1801}
1802
1803OCTET_STRING_t *
Lev Walkinbbd93252004-10-12 05:57:23 +00001804OCTET_STRING_new_fromBuf(asn_TYPE_descriptor_t *td, const char *str, int len) {
1805 asn_OCTET_STRING_specifics_t *specs = td->specifics
Lev Walkindc06f6b2004-10-20 15:50:55 +00001806 ? (asn_OCTET_STRING_specifics_t *)td->specifics
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +08001807 : &asn_SPC_OCTET_STRING_specs;
Lev Walkinf15320b2004-06-03 03:38:44 +00001808 OCTET_STRING_t *st;
1809
Lev Walkinbbd93252004-10-12 05:57:23 +00001810 st = (OCTET_STRING_t *)CALLOC(1, specs->struct_size);
Lev Walkinf15320b2004-06-03 03:38:44 +00001811 if(st && str && OCTET_STRING_fromBuf(st, str, len)) {
Lev Walkin419f6752006-09-13 04:02:00 +00001812 FREEMEM(st);
Lev Walkinf15320b2004-06-03 03:38:44 +00001813 st = NULL;
1814 }
1815
1816 return st;
1817}
1818