blob: e3f73881868a3bfb5e1411650d8cf419dd2be1a7 [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 */
Lev Walkin5e033762004-09-29 13:26:15 +000012static 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 Walkinf15320b2004-06-03 03:38:44 +000021 UTF8String_constraint, /* Check for invalid codes, etc. */
22 OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
Lev Walkindc06f6b2004-10-20 15:50:55 +000023 OCTET_STRING_encode_der,
24 OCTET_STRING_decode_xer_utf8,
25 OCTET_STRING_encode_xer_utf8,
Lev Walkin523de9e2006-08-18 01:34:18 +000026 0, 0,
Lev Walkinf15320b2004-06-03 03:38:44 +000027 0, /* Use generic outmost tag fetcher */
Lev Walkin5e033762004-09-29 13:26:15 +000028 asn_DEF_UTF8String_tags,
29 sizeof(asn_DEF_UTF8String_tags)
30 / sizeof(asn_DEF_UTF8String_tags[0]) - 1,
31 asn_DEF_UTF8String_tags,
32 sizeof(asn_DEF_UTF8String_tags)
33 / sizeof(asn_DEF_UTF8String_tags[0]),
Lev Walkin59b176e2005-11-26 11:25:14 +000034 0, /* No PER visible constraints */
Lev Walkin449f8322004-08-20 13:23:42 +000035 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +000036 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +000037};
38
Lev Walkine18ca712004-10-02 11:37:38 +000039/*
40 * This is the table of length expectations.
Lev Walkin6cf9f712005-03-02 13:30:25 +000041 * The second half of this table is only applicable to the long sequences.
Lev Walkine18ca712004-10-02 11:37:38 +000042 */
43static int UTF8String_ht[2][16] = {
44 { /* 0x0 ... 0x7 */
45 /* 0000..0111 */
46 1, 1, 1, 1, 1, 1, 1, 1,
47 /* 1000..1011(0), 1100..1101(2), 1110(3), 1111(-1) */
48 0, 0, 0, 0, 2, 2, 3, -1 },
49 { /* 0xF0 .. 0xF7 */
50 /* 11110000..11110111 */
51 4, 4, 4, 4, 4, 4, 4, 4,
52 5, 5, 5, 5, 6, 6, -1, -1 }
Lev Walkinf15320b2004-06-03 03:38:44 +000053};
Lev Walkinedc7d592004-10-02 15:55:07 +000054static int32_t UTF8String_mv[7] = { 0, 0,
55 0x00000080,
56 0x00000800,
57 0x00010000,
58 0x00200000,
59 0x04000000
60};
61
62/* Internal aliases for return codes */
63#define U8E_TRUNC -1 /* UTF-8 sequence truncated */
64#define U8E_ILLSTART -2 /* Illegal UTF-8 sequence start */
65#define U8E_NOTCONT -3 /* Continuation expectation failed */
66#define U8E_NOTMIN -4 /* Not minimal length encoding */
67#define U8E_EINVAL -5 /* Invalid arguments */
Lev Walkinf15320b2004-06-03 03:38:44 +000068
69int
Lev Walkin5e033762004-09-29 13:26:15 +000070UTF8String_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkin1eded352006-07-13 11:19:01 +000071 asn_app_constraint_failed_f *ctfailcb, void *app_key) {
Lev Walkinedc7d592004-10-02 15:55:07 +000072 ssize_t len = UTF8String_length((const UTF8String_t *)sptr);
73 switch(len) {
74 case U8E_EINVAL:
Lev Walkin1eded352006-07-13 11:19:01 +000075 _ASN_CTFAIL(app_key, td,
Lev Walkinedc7d592004-10-02 15:55:07 +000076 "%s: value not given", td->name);
77 break;
78 case U8E_TRUNC:
Lev Walkin1eded352006-07-13 11:19:01 +000079 _ASN_CTFAIL(app_key, td,
Lev Walkinedc7d592004-10-02 15:55:07 +000080 "%s: truncated UTF-8 sequence (%s:%d)",
81 td->name, __FILE__, __LINE__);
82 break;
83 case U8E_ILLSTART:
Lev Walkin1eded352006-07-13 11:19:01 +000084 _ASN_CTFAIL(app_key, td,
Lev Walkinedc7d592004-10-02 15:55:07 +000085 "%s: UTF-8 illegal start of encoding (%s:%d)",
86 td->name, __FILE__, __LINE__);
87 break;
88 case U8E_NOTCONT:
Lev Walkin1eded352006-07-13 11:19:01 +000089 _ASN_CTFAIL(app_key, td,
Lev Walkinedc7d592004-10-02 15:55:07 +000090 "%s: UTF-8 not continuation (%s:%d)",
91 td->name, __FILE__, __LINE__);
92 break;
93 case U8E_NOTMIN:
Lev Walkin1eded352006-07-13 11:19:01 +000094 _ASN_CTFAIL(app_key, td,
Lev Walkinedc7d592004-10-02 15:55:07 +000095 "%s: UTF-8 not minimal sequence (%s:%d)",
96 td->name, __FILE__, __LINE__);
97 break;
98 }
99 return (len < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000100}
101
Lev Walkin9a6f65b2004-10-23 11:20:06 +0000102static ssize_t
103UTF8String__process(const UTF8String_t *st, uint32_t *dst, size_t dstlen) {
104 size_t length;
105 uint8_t *buf = st->buf;
106 uint8_t *end = buf + st->size;
Lev Walkinc7549b12006-09-17 01:27:44 +0000107 uint32_t *dstend = dst + dstlen;
Lev Walkinf15320b2004-06-03 03:38:44 +0000108
Lev Walkin9a6f65b2004-10-23 11:20:06 +0000109 for(length = 0; buf < end; length++) {
110 int ch = *buf;
111 uint8_t *cend;
112 int32_t value;
113 int want;
Lev Walkinf15320b2004-06-03 03:38:44 +0000114
Lev Walkin9a6f65b2004-10-23 11:20:06 +0000115 /* Compute the sequence length */
116 want = UTF8String_ht[0][ch >> 4];
117 switch(want) {
118 case -1:
119 /* Second half of the table, long sequence */
120 want = UTF8String_ht[1][ch & 0x0F];
121 if(want != -1) break;
122 /* Fall through */
123 case 0:
124 return U8E_ILLSTART;
Lev Walkinf15320b2004-06-03 03:38:44 +0000125 }
126
Lev Walkin9a6f65b2004-10-23 11:20:06 +0000127 /* assert(want >= 1 && want <= 6) */
128
129 /* Check character sequence length */
130 if(buf + want > end) return U8E_TRUNC;
131
Lev Walkinc7549b12006-09-17 01:27:44 +0000132 value = ch & (0xff >> want);
Lev Walkin9a6f65b2004-10-23 11:20:06 +0000133 cend = buf + want;
134 for(buf++; buf < cend; buf++) {
135 ch = *buf;
136 if(ch < 0x80 || ch > 0xbf) return U8E_NOTCONT;
137 value = (value << 6) | (ch & 0x3F);
138 }
139 if(value < UTF8String_mv[want])
140 return U8E_NOTMIN;
Lev Walkinc7549b12006-09-17 01:27:44 +0000141 if(dst < dstend)
142 *dst++ = value; /* Record value */
Lev Walkin9a6f65b2004-10-23 11:20:06 +0000143 }
144
Lev Walkinc7549b12006-09-17 01:27:44 +0000145 if(dst < dstend) *dst = 0; /* zero-terminate */
Lev Walkin9a6f65b2004-10-23 11:20:06 +0000146
147 return length;
148}
149
150
151ssize_t
152UTF8String_length(const UTF8String_t *st) {
153 if(st && st->buf) {
154 return UTF8String__process(st, 0, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000155 } else {
Lev Walkinedc7d592004-10-02 15:55:07 +0000156 return U8E_EINVAL;
Lev Walkinf15320b2004-06-03 03:38:44 +0000157 }
158}
159
Lev Walkin9a6f65b2004-10-23 11:20:06 +0000160size_t
161UTF8String_to_wcs(const UTF8String_t *st, uint32_t *dst, size_t dstlen) {
162 if(st && st->buf) {
163 ssize_t ret = UTF8String__process(st, dst, dstlen);
164 return (ret < 0) ? 0 : ret;
165 } else {
166 return 0;
167 }
168}
169
Lev Walkinf15320b2004-06-03 03:38:44 +0000170int
Lev Walkin5e033762004-09-29 13:26:15 +0000171UTF8String_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +0000172 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +0000173 const UTF8String_t *st = (const UTF8String_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000174
Lev Walkind9bd7752004-06-05 08:17:50 +0000175 (void)td; /* Unused argument */
176 (void)ilevel; /* Unused argument */
177
Lev Walkinf15320b2004-06-03 03:38:44 +0000178 if(st && st->buf) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000179 return (cb(st->buf, st->size, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000180 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000181 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000182 }
183}