blob: 7a6de6d0b754a760e2d2331f1ab428a573a563ed [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};
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080016asn_TYPE_operation_t asn_OP_UTF8String = {
Lev Walkina9cc46e2004-09-22 16:06:28 +000017 OCTET_STRING_free,
18 UTF8String_print,
Lev Walkincd2f48e2017-08-10 02:14:59 -070019 OCTET_STRING_compare,
Lev Walkinf15320b2004-06-03 03:38:44 +000020 OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
Lev Walkindc06f6b2004-10-20 15:50:55 +000021 OCTET_STRING_encode_der,
22 OCTET_STRING_decode_xer_utf8,
23 OCTET_STRING_encode_xer_utf8,
Lev Walkincc159472017-07-06 08:26:36 -070024#ifdef ASN_DISABLE_OER_SUPPORT
25 0,
26 0,
27#else
Lev Walkin1d76f3c2017-07-25 07:58:05 -070028 OCTET_STRING_decode_oer,
29 OCTET_STRING_encode_oer,
Lev Walkincc159472017-07-06 08:26:36 -070030#endif /* ASN_DISABLE_OER_SUPPORT */
Lev Walkinb33425f2017-07-14 14:59:52 +040031#ifdef ASN_DISABLE_PER_SUPPORT
32 0,
33 0,
34#else
35 OCTET_STRING_decode_uper,
36 OCTET_STRING_encode_uper,
37#endif /* ASN_DISABLE_PER_SUPPORT */
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080038 0 /* Use generic outmost tag fetcher */
39};
40asn_TYPE_descriptor_t asn_DEF_UTF8String = {
41 "UTF8String",
42 "UTF8String",
43 &asn_OP_UTF8String,
44 UTF8String_constraint, /* Check for invalid codes, etc. */
Lev Walkin5e033762004-09-29 13:26:15 +000045 asn_DEF_UTF8String_tags,
46 sizeof(asn_DEF_UTF8String_tags)
47 / sizeof(asn_DEF_UTF8String_tags[0]) - 1,
48 asn_DEF_UTF8String_tags,
49 sizeof(asn_DEF_UTF8String_tags)
50 / sizeof(asn_DEF_UTF8String_tags[0]),
Lev Walkin76780762017-07-07 10:07:30 -070051 0, /* No OER visible constraints */
Lev Walkin59b176e2005-11-26 11:25:14 +000052 0, /* No PER visible constraints */
Lev Walkin449f8322004-08-20 13:23:42 +000053 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +000054 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +000055};
56
Lev Walkine18ca712004-10-02 11:37:38 +000057/*
58 * This is the table of length expectations.
Lev Walkin6cf9f712005-03-02 13:30:25 +000059 * The second half of this table is only applicable to the long sequences.
Lev Walkine18ca712004-10-02 11:37:38 +000060 */
Wim Lewis18c2ec92014-07-29 11:30:10 -070061static const int UTF8String_ht[2][16] = {
Lev Walkine18ca712004-10-02 11:37:38 +000062 { /* 0x0 ... 0x7 */
63 /* 0000..0111 */
64 1, 1, 1, 1, 1, 1, 1, 1,
65 /* 1000..1011(0), 1100..1101(2), 1110(3), 1111(-1) */
66 0, 0, 0, 0, 2, 2, 3, -1 },
67 { /* 0xF0 .. 0xF7 */
68 /* 11110000..11110111 */
69 4, 4, 4, 4, 4, 4, 4, 4,
70 5, 5, 5, 5, 6, 6, -1, -1 }
Lev Walkinf15320b2004-06-03 03:38:44 +000071};
Wim Lewis18c2ec92014-07-29 11:30:10 -070072static const int32_t UTF8String_mv[7] = { 0, 0,
Lev Walkinedc7d592004-10-02 15:55:07 +000073 0x00000080,
74 0x00000800,
75 0x00010000,
76 0x00200000,
77 0x04000000
78};
79
80/* Internal aliases for return codes */
81#define U8E_TRUNC -1 /* UTF-8 sequence truncated */
82#define U8E_ILLSTART -2 /* Illegal UTF-8 sequence start */
83#define U8E_NOTCONT -3 /* Continuation expectation failed */
84#define U8E_NOTMIN -4 /* Not minimal length encoding */
85#define U8E_EINVAL -5 /* Invalid arguments */
Lev Walkinf15320b2004-06-03 03:38:44 +000086
87int
Lev Walkin5e033762004-09-29 13:26:15 +000088UTF8String_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkin1eded352006-07-13 11:19:01 +000089 asn_app_constraint_failed_f *ctfailcb, void *app_key) {
Lev Walkinedc7d592004-10-02 15:55:07 +000090 ssize_t len = UTF8String_length((const UTF8String_t *)sptr);
91 switch(len) {
92 case U8E_EINVAL:
Lev Walkin7c1dc052016-03-14 03:08:15 -070093 ASN__CTFAIL(app_key, td, sptr,
Lev Walkinedc7d592004-10-02 15:55:07 +000094 "%s: value not given", td->name);
95 break;
96 case U8E_TRUNC:
Lev Walkin7c1dc052016-03-14 03:08:15 -070097 ASN__CTFAIL(app_key, td, sptr,
Lev Walkinedc7d592004-10-02 15:55:07 +000098 "%s: truncated UTF-8 sequence (%s:%d)",
99 td->name, __FILE__, __LINE__);
100 break;
101 case U8E_ILLSTART:
Lev Walkin7c1dc052016-03-14 03:08:15 -0700102 ASN__CTFAIL(app_key, td, sptr,
Lev Walkinedc7d592004-10-02 15:55:07 +0000103 "%s: UTF-8 illegal start of encoding (%s:%d)",
104 td->name, __FILE__, __LINE__);
105 break;
106 case U8E_NOTCONT:
Lev Walkin7c1dc052016-03-14 03:08:15 -0700107 ASN__CTFAIL(app_key, td, sptr,
Lev Walkinedc7d592004-10-02 15:55:07 +0000108 "%s: UTF-8 not continuation (%s:%d)",
109 td->name, __FILE__, __LINE__);
110 break;
111 case U8E_NOTMIN:
Lev Walkin7c1dc052016-03-14 03:08:15 -0700112 ASN__CTFAIL(app_key, td, sptr,
Lev Walkinedc7d592004-10-02 15:55:07 +0000113 "%s: UTF-8 not minimal sequence (%s:%d)",
114 td->name, __FILE__, __LINE__);
115 break;
116 }
117 return (len < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000118}
119
Lev Walkin9a6f65b2004-10-23 11:20:06 +0000120static ssize_t
121UTF8String__process(const UTF8String_t *st, uint32_t *dst, size_t dstlen) {
122 size_t length;
123 uint8_t *buf = st->buf;
124 uint8_t *end = buf + st->size;
Lev Walkinc7549b12006-09-17 01:27:44 +0000125 uint32_t *dstend = dst + dstlen;
Lev Walkinf15320b2004-06-03 03:38:44 +0000126
Lev Walkin9a6f65b2004-10-23 11:20:06 +0000127 for(length = 0; buf < end; length++) {
128 int ch = *buf;
129 uint8_t *cend;
130 int32_t value;
131 int want;
Lev Walkinf15320b2004-06-03 03:38:44 +0000132
Lev Walkin9a6f65b2004-10-23 11:20:06 +0000133 /* Compute the sequence length */
134 want = UTF8String_ht[0][ch >> 4];
135 switch(want) {
136 case -1:
137 /* Second half of the table, long sequence */
138 want = UTF8String_ht[1][ch & 0x0F];
139 if(want != -1) break;
140 /* Fall through */
141 case 0:
142 return U8E_ILLSTART;
Lev Walkinf15320b2004-06-03 03:38:44 +0000143 }
144
Lev Walkin9a6f65b2004-10-23 11:20:06 +0000145 /* assert(want >= 1 && want <= 6) */
146
147 /* Check character sequence length */
148 if(buf + want > end) return U8E_TRUNC;
149
Lev Walkinc7549b12006-09-17 01:27:44 +0000150 value = ch & (0xff >> want);
Lev Walkin9a6f65b2004-10-23 11:20:06 +0000151 cend = buf + want;
152 for(buf++; buf < cend; buf++) {
153 ch = *buf;
154 if(ch < 0x80 || ch > 0xbf) return U8E_NOTCONT;
155 value = (value << 6) | (ch & 0x3F);
156 }
157 if(value < UTF8String_mv[want])
158 return U8E_NOTMIN;
Lev Walkinc7549b12006-09-17 01:27:44 +0000159 if(dst < dstend)
160 *dst++ = value; /* Record value */
Lev Walkin9a6f65b2004-10-23 11:20:06 +0000161 }
162
Lev Walkinc7549b12006-09-17 01:27:44 +0000163 if(dst < dstend) *dst = 0; /* zero-terminate */
Lev Walkin9a6f65b2004-10-23 11:20:06 +0000164
165 return length;
166}
167
168
169ssize_t
170UTF8String_length(const UTF8String_t *st) {
171 if(st && st->buf) {
172 return UTF8String__process(st, 0, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000173 } else {
Lev Walkinedc7d592004-10-02 15:55:07 +0000174 return U8E_EINVAL;
Lev Walkinf15320b2004-06-03 03:38:44 +0000175 }
176}
177
Lev Walkin9a6f65b2004-10-23 11:20:06 +0000178size_t
179UTF8String_to_wcs(const UTF8String_t *st, uint32_t *dst, size_t dstlen) {
180 if(st && st->buf) {
181 ssize_t ret = UTF8String__process(st, dst, dstlen);
182 return (ret < 0) ? 0 : ret;
183 } else {
184 return 0;
185 }
186}
187
Lev Walkinf15320b2004-06-03 03:38:44 +0000188int
Lev Walkin5e033762004-09-29 13:26:15 +0000189UTF8String_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +0000190 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +0000191 const UTF8String_t *st = (const UTF8String_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000192
Lev Walkind9bd7752004-06-05 08:17:50 +0000193 (void)td; /* Unused argument */
194 (void)ilevel; /* Unused argument */
195
Lev Walkinf15320b2004-06-03 03:38:44 +0000196 if(st && st->buf) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000197 return (cb(st->buf, st->size, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000198 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000199 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000200 }
201}