blob: de78c5e6c5f709a0023c17b6a9cbb41eeac86f72 [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001/*-
Lev Walkinc7549b12006-09-17 01:27:44 +00002 * Copyright (c) 2003, 2004, 2006 Lev Walkin <vlm@lionet.info>.
3 * 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 <UTF8String.h>
8
9/*
10 * UTF8String basic type description.
11 */
Wim Lewis18c2ec92014-07-29 11:30:10 -070012static const ber_tlv_tag_t asn_DEF_UTF8String_tags[] = {
Lev Walkin188ed2c2004-09-13 08:31:01 +000013 (ASN_TAG_CLASS_UNIVERSAL | (12 << 2)), /* [UNIVERSAL 12] IMPLICIT ...*/
14 (ASN_TAG_CLASS_UNIVERSAL | (4 << 2)), /* ... OCTET STRING */
Lev Walkinf15320b2004-06-03 03:38:44 +000015};
Lev Walkin5e033762004-09-29 13:26:15 +000016asn_TYPE_descriptor_t asn_DEF_UTF8String = {
Lev Walkinf15320b2004-06-03 03:38:44 +000017 "UTF8String",
Lev Walkindc06f6b2004-10-20 15:50:55 +000018 "UTF8String",
Lev Walkina9cc46e2004-09-22 16:06:28 +000019 OCTET_STRING_free,
20 UTF8String_print,
Lev Walkincd2f48e2017-08-10 02:14:59 -070021 OCTET_STRING_compare,
Lev Walkinf15320b2004-06-03 03:38:44 +000022 UTF8String_constraint, /* Check for invalid codes, etc. */
23 OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
Lev Walkindc06f6b2004-10-20 15:50:55 +000024 OCTET_STRING_encode_der,
25 OCTET_STRING_decode_xer_utf8,
26 OCTET_STRING_encode_xer_utf8,
Lev Walkincc159472017-07-06 08:26:36 -070027#ifdef ASN_DISABLE_OER_SUPPORT
28 0,
29 0,
30#else
Lev Walkin1d76f3c2017-07-25 07:58:05 -070031 OCTET_STRING_decode_oer,
32 OCTET_STRING_encode_oer,
Lev Walkincc159472017-07-06 08:26:36 -070033#endif /* ASN_DISABLE_OER_SUPPORT */
Lev Walkinb33425f2017-07-14 14:59:52 +040034#ifdef ASN_DISABLE_PER_SUPPORT
35 0,
36 0,
37#else
38 OCTET_STRING_decode_uper,
39 OCTET_STRING_encode_uper,
40#endif /* ASN_DISABLE_PER_SUPPORT */
Lev Walkinf15320b2004-06-03 03:38:44 +000041 0, /* Use generic outmost tag fetcher */
Lev Walkin5e033762004-09-29 13:26:15 +000042 asn_DEF_UTF8String_tags,
43 sizeof(asn_DEF_UTF8String_tags)
44 / sizeof(asn_DEF_UTF8String_tags[0]) - 1,
45 asn_DEF_UTF8String_tags,
46 sizeof(asn_DEF_UTF8String_tags)
47 / sizeof(asn_DEF_UTF8String_tags[0]),
Lev Walkin76780762017-07-07 10:07:30 -070048 0, /* No OER visible constraints */
Lev Walkin59b176e2005-11-26 11:25:14 +000049 0, /* No PER visible constraints */
Lev Walkin449f8322004-08-20 13:23:42 +000050 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +000051 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +000052};
53
Lev Walkine18ca712004-10-02 11:37:38 +000054/*
55 * This is the table of length expectations.
Lev Walkin6cf9f712005-03-02 13:30:25 +000056 * The second half of this table is only applicable to the long sequences.
Lev Walkine18ca712004-10-02 11:37:38 +000057 */
Wim Lewis18c2ec92014-07-29 11:30:10 -070058static const int UTF8String_ht[2][16] = {
Lev Walkine18ca712004-10-02 11:37:38 +000059 { /* 0x0 ... 0x7 */
60 /* 0000..0111 */
61 1, 1, 1, 1, 1, 1, 1, 1,
62 /* 1000..1011(0), 1100..1101(2), 1110(3), 1111(-1) */
63 0, 0, 0, 0, 2, 2, 3, -1 },
64 { /* 0xF0 .. 0xF7 */
65 /* 11110000..11110111 */
66 4, 4, 4, 4, 4, 4, 4, 4,
67 5, 5, 5, 5, 6, 6, -1, -1 }
Lev Walkinf15320b2004-06-03 03:38:44 +000068};
Wim Lewis18c2ec92014-07-29 11:30:10 -070069static const int32_t UTF8String_mv[7] = { 0, 0,
Lev Walkinedc7d592004-10-02 15:55:07 +000070 0x00000080,
71 0x00000800,
72 0x00010000,
73 0x00200000,
74 0x04000000
75};
76
77/* Internal aliases for return codes */
78#define U8E_TRUNC -1 /* UTF-8 sequence truncated */
79#define U8E_ILLSTART -2 /* Illegal UTF-8 sequence start */
80#define U8E_NOTCONT -3 /* Continuation expectation failed */
81#define U8E_NOTMIN -4 /* Not minimal length encoding */
82#define U8E_EINVAL -5 /* Invalid arguments */
Lev Walkinf15320b2004-06-03 03:38:44 +000083
84int
Lev Walkin5e033762004-09-29 13:26:15 +000085UTF8String_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkin1eded352006-07-13 11:19:01 +000086 asn_app_constraint_failed_f *ctfailcb, void *app_key) {
Lev Walkinedc7d592004-10-02 15:55:07 +000087 ssize_t len = UTF8String_length((const UTF8String_t *)sptr);
88 switch(len) {
89 case U8E_EINVAL:
Lev Walkin7c1dc052016-03-14 03:08:15 -070090 ASN__CTFAIL(app_key, td, sptr,
Lev Walkinedc7d592004-10-02 15:55:07 +000091 "%s: value not given", td->name);
92 break;
93 case U8E_TRUNC:
Lev Walkin7c1dc052016-03-14 03:08:15 -070094 ASN__CTFAIL(app_key, td, sptr,
Lev Walkinedc7d592004-10-02 15:55:07 +000095 "%s: truncated UTF-8 sequence (%s:%d)",
96 td->name, __FILE__, __LINE__);
97 break;
98 case U8E_ILLSTART:
Lev Walkin7c1dc052016-03-14 03:08:15 -070099 ASN__CTFAIL(app_key, td, sptr,
Lev Walkinedc7d592004-10-02 15:55:07 +0000100 "%s: UTF-8 illegal start of encoding (%s:%d)",
101 td->name, __FILE__, __LINE__);
102 break;
103 case U8E_NOTCONT:
Lev Walkin7c1dc052016-03-14 03:08:15 -0700104 ASN__CTFAIL(app_key, td, sptr,
Lev Walkinedc7d592004-10-02 15:55:07 +0000105 "%s: UTF-8 not continuation (%s:%d)",
106 td->name, __FILE__, __LINE__);
107 break;
108 case U8E_NOTMIN:
Lev Walkin7c1dc052016-03-14 03:08:15 -0700109 ASN__CTFAIL(app_key, td, sptr,
Lev Walkinedc7d592004-10-02 15:55:07 +0000110 "%s: UTF-8 not minimal sequence (%s:%d)",
111 td->name, __FILE__, __LINE__);
112 break;
113 }
114 return (len < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000115}
116
Lev Walkin9a6f65b2004-10-23 11:20:06 +0000117static ssize_t
118UTF8String__process(const UTF8String_t *st, uint32_t *dst, size_t dstlen) {
119 size_t length;
120 uint8_t *buf = st->buf;
121 uint8_t *end = buf + st->size;
Lev Walkinc7549b12006-09-17 01:27:44 +0000122 uint32_t *dstend = dst + dstlen;
Lev Walkinf15320b2004-06-03 03:38:44 +0000123
Lev Walkin9a6f65b2004-10-23 11:20:06 +0000124 for(length = 0; buf < end; length++) {
125 int ch = *buf;
126 uint8_t *cend;
127 int32_t value;
128 int want;
Lev Walkinf15320b2004-06-03 03:38:44 +0000129
Lev Walkin9a6f65b2004-10-23 11:20:06 +0000130 /* Compute the sequence length */
131 want = UTF8String_ht[0][ch >> 4];
132 switch(want) {
133 case -1:
134 /* Second half of the table, long sequence */
135 want = UTF8String_ht[1][ch & 0x0F];
136 if(want != -1) break;
137 /* Fall through */
138 case 0:
139 return U8E_ILLSTART;
Lev Walkinf15320b2004-06-03 03:38:44 +0000140 }
141
Lev Walkin9a6f65b2004-10-23 11:20:06 +0000142 /* assert(want >= 1 && want <= 6) */
143
144 /* Check character sequence length */
145 if(buf + want > end) return U8E_TRUNC;
146
Lev Walkinc7549b12006-09-17 01:27:44 +0000147 value = ch & (0xff >> want);
Lev Walkin9a6f65b2004-10-23 11:20:06 +0000148 cend = buf + want;
149 for(buf++; buf < cend; buf++) {
150 ch = *buf;
151 if(ch < 0x80 || ch > 0xbf) return U8E_NOTCONT;
152 value = (value << 6) | (ch & 0x3F);
153 }
154 if(value < UTF8String_mv[want])
155 return U8E_NOTMIN;
Lev Walkinc7549b12006-09-17 01:27:44 +0000156 if(dst < dstend)
157 *dst++ = value; /* Record value */
Lev Walkin9a6f65b2004-10-23 11:20:06 +0000158 }
159
Lev Walkinc7549b12006-09-17 01:27:44 +0000160 if(dst < dstend) *dst = 0; /* zero-terminate */
Lev Walkin9a6f65b2004-10-23 11:20:06 +0000161
162 return length;
163}
164
165
166ssize_t
167UTF8String_length(const UTF8String_t *st) {
168 if(st && st->buf) {
169 return UTF8String__process(st, 0, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000170 } else {
Lev Walkinedc7d592004-10-02 15:55:07 +0000171 return U8E_EINVAL;
Lev Walkinf15320b2004-06-03 03:38:44 +0000172 }
173}
174
Lev Walkin9a6f65b2004-10-23 11:20:06 +0000175size_t
176UTF8String_to_wcs(const UTF8String_t *st, uint32_t *dst, size_t dstlen) {
177 if(st && st->buf) {
178 ssize_t ret = UTF8String__process(st, dst, dstlen);
179 return (ret < 0) ? 0 : ret;
180 } else {
181 return 0;
182 }
183}
184
Lev Walkinf15320b2004-06-03 03:38:44 +0000185int
Lev Walkin5e033762004-09-29 13:26:15 +0000186UTF8String_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +0000187 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +0000188 const UTF8String_t *st = (const UTF8String_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000189
Lev Walkind9bd7752004-06-05 08:17:50 +0000190 (void)td; /* Unused argument */
191 (void)ilevel; /* Unused argument */
192
Lev Walkinf15320b2004-06-03 03:38:44 +0000193 if(st && st->buf) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000194 return (cb(st->buf, st->size, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000195 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000196 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000197 }
198}