2 * Copyright (c) 2003, 2004, 2006 Lev Walkin <vlm@lionet.info>.
4 * Redistribution and modifications are permitted subject to BSD license.
6 #include <asn_internal.h>
7 #include <UTF8String.h>
10 * UTF8String basic type description.
12 static const ber_tlv_tag_t asn_DEF_UTF8String_tags[] = {
13 (ASN_TAG_CLASS_UNIVERSAL | (12 << 2)), /* [UNIVERSAL 12] IMPLICIT ...*/
14 (ASN_TAG_CLASS_UNIVERSAL | (4 << 2)), /* ... OCTET STRING */
16 asn_TYPE_operation_t asn_OP_UTF8String = {
20 OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
21 OCTET_STRING_encode_der,
22 OCTET_STRING_decode_xer_utf8,
23 OCTET_STRING_encode_xer_utf8,
24 #ifdef ASN_DISABLE_OER_SUPPORT
28 OCTET_STRING_decode_oer,
29 OCTET_STRING_encode_oer,
30 #endif /* ASN_DISABLE_OER_SUPPORT */
31 #ifdef ASN_DISABLE_PER_SUPPORT
37 OCTET_STRING_decode_uper,
38 OCTET_STRING_encode_uper,
39 OCTET_STRING_decode_aper,
40 OCTET_STRING_encode_aper,
41 #endif /* ASN_DISABLE_PER_SUPPORT */
42 UTF8String_random_fill,
43 0 /* Use generic outmost tag fetcher */
45 asn_TYPE_descriptor_t asn_DEF_UTF8String = {
49 asn_DEF_UTF8String_tags,
50 sizeof(asn_DEF_UTF8String_tags)
51 / sizeof(asn_DEF_UTF8String_tags[0]) - 1,
52 asn_DEF_UTF8String_tags,
53 sizeof(asn_DEF_UTF8String_tags)
54 / sizeof(asn_DEF_UTF8String_tags[0]),
55 { 0, 0, UTF8String_constraint },
56 0, 0, /* No members */
61 * This is the table of length expectations.
62 * The second half of this table is only applicable to the long sequences.
64 static const int UTF8String_ht[2][16] = {
67 1, 1, 1, 1, 1, 1, 1, 1,
68 /* 1000..1011(0), 1100..1101(2), 1110(3), 1111(-1) */
69 0, 0, 0, 0, 2, 2, 3, -1 },
71 /* 11110000..11110111 */
72 4, 4, 4, 4, 4, 4, 4, 4,
73 5, 5, 5, 5, 6, 6, -1, -1 }
75 static const int32_t UTF8String_mv[7] = { 0, 0,
83 /* Internal aliases for return codes */
84 #define U8E_TRUNC -1 /* UTF-8 sequence truncated */
85 #define U8E_ILLSTART -2 /* Illegal UTF-8 sequence start */
86 #define U8E_NOTCONT -3 /* Continuation expectation failed */
87 #define U8E_NOTMIN -4 /* Not minimal length encoding */
88 #define U8E_EINVAL -5 /* Invalid arguments */
91 UTF8String_constraint(const asn_TYPE_descriptor_t *td, const void *sptr,
92 asn_app_constraint_failed_f *ctfailcb, void *app_key) {
93 ssize_t len = UTF8String_length((const UTF8String_t *)sptr);
96 ASN__CTFAIL(app_key, td, sptr,
97 "%s: value not given", td->name);
100 ASN__CTFAIL(app_key, td, sptr,
101 "%s: truncated UTF-8 sequence (%s:%d)",
102 td->name, __FILE__, __LINE__);
105 ASN__CTFAIL(app_key, td, sptr,
106 "%s: UTF-8 illegal start of encoding (%s:%d)",
107 td->name, __FILE__, __LINE__);
110 ASN__CTFAIL(app_key, td, sptr,
111 "%s: UTF-8 not continuation (%s:%d)",
112 td->name, __FILE__, __LINE__);
115 ASN__CTFAIL(app_key, td, sptr,
116 "%s: UTF-8 not minimal sequence (%s:%d)",
117 td->name, __FILE__, __LINE__);
120 return (len < 0) ? -1 : 0;
124 UTF8String__process(const UTF8String_t *st, uint32_t *dst, size_t dstlen) {
126 uint8_t *buf = st->buf;
127 uint8_t *end = buf + st->size;
128 uint32_t *dstend = dst + dstlen;
130 for(length = 0; buf < end; length++) {
136 /* Compute the sequence length */
137 want = UTF8String_ht[0][ch >> 4];
140 /* Second half of the table, long sequence */
141 want = UTF8String_ht[1][ch & 0x0F];
142 if(want != -1) break;
148 /* assert(want >= 1 && want <= 6) */
150 /* Check character sequence length */
151 if(buf + want > end) return U8E_TRUNC;
153 value = ch & (0xff >> want);
155 for(buf++; buf < cend; buf++) {
157 if(ch < 0x80 || ch > 0xbf) return U8E_NOTCONT;
158 value = (value << 6) | (ch & 0x3F);
160 if(value < UTF8String_mv[want])
163 *dst++ = value; /* Record value */
166 if(dst < dstend) *dst = 0; /* zero-terminate */
173 UTF8String_length(const UTF8String_t *st) {
175 return UTF8String__process(st, 0, 0);
182 UTF8String_to_wcs(const UTF8String_t *st, uint32_t *dst, size_t dstlen) {
184 ssize_t ret = UTF8String__process(st, dst, dstlen);
185 return (ret < 0) ? 0 : ret;
192 UTF8String_print(const asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
193 asn_app_consume_bytes_f *cb, void *app_key) {
194 const UTF8String_t *st = (const UTF8String_t *)sptr;
196 (void)td; /* Unused argument */
197 (void)ilevel; /* Unused argument */
200 return (cb(st->buf, st->size, app_key) < 0) ? -1 : 0;
202 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
208 * Biased function for randomizing UTF-8 sequences.
211 UTF8String__random_char(uint8_t *b, size_t size) {
212 static const struct rnd_value {
215 } values[] = {{"\0", 1},
220 {"\xf0\x90\x8d\x88", 4},
221 {"\xf4\x8f\xbf\xbf", 4}};
223 const struct rnd_value *v;
238 max_idx = sizeof(values) / sizeof(values[0]) - 1;
242 v = &values[asn_random_between(0, max_idx)];
243 memcpy(b, v->value, v->size);
247 asn_random_fill_result_t
248 UTF8String_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
249 const asn_encoding_constraints_t *constraints,
251 asn_random_fill_result_t result_ok = {ARFILL_OK, 1};
252 asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0};
253 asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0};
261 if(max_length == 0 && !*sptr) return result_skipped;
263 /* Figure out how far we should go */
264 rnd_len = OCTET_STRING_random_length_constrained(td, constraints,
267 buf = CALLOC(4, rnd_len + 1);
268 if(!buf) return result_failed;
270 bend = &buf[4 * rnd_len];
272 for(b = buf, idx = 0; idx < rnd_len; idx++) {
273 b += UTF8String__random_char(b, (bend - b));
281 st = (OCTET_STRING_t *)(*sptr = CALLOC(1, sizeof(UTF8String_t)));
284 return result_failed;
291 assert(UTF8String_length(st) == (ssize_t)rnd_len);