blob: 3cd574d5badb175482edda6cb3ba34e57533dcc0 [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001/*-
2 * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
3 * Redistribution and modifications are permitted subject to BSD license.
4 */
Lev Walkina9cc46e2004-09-22 16:06:28 +00005#include <asn_internal.h>
Lev Walkinf15320b2004-06-03 03:38:44 +00006#include <UTF8String.h>
7
8/*
9 * UTF8String basic type description.
10 */
Lev Walkin5e033762004-09-29 13:26:15 +000011static ber_tlv_tag_t asn_DEF_UTF8String_tags[] = {
Lev Walkin188ed2c2004-09-13 08:31:01 +000012 (ASN_TAG_CLASS_UNIVERSAL | (12 << 2)), /* [UNIVERSAL 12] IMPLICIT ...*/
13 (ASN_TAG_CLASS_UNIVERSAL | (4 << 2)), /* ... OCTET STRING */
Lev Walkinf15320b2004-06-03 03:38:44 +000014};
Lev Walkin5e033762004-09-29 13:26:15 +000015asn_TYPE_descriptor_t asn_DEF_UTF8String = {
Lev Walkinf15320b2004-06-03 03:38:44 +000016 "UTF8String",
Lev Walkindc06f6b2004-10-20 15:50:55 +000017 "UTF8String",
Lev Walkina9cc46e2004-09-22 16:06:28 +000018 OCTET_STRING_free,
19 UTF8String_print,
Lev Walkinf15320b2004-06-03 03:38:44 +000020 UTF8String_constraint, /* Check for invalid codes, etc. */
21 OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
Lev Walkindc06f6b2004-10-20 15:50:55 +000022 OCTET_STRING_encode_der,
23 OCTET_STRING_decode_xer_utf8,
24 OCTET_STRING_encode_xer_utf8,
Lev Walkin523de9e2006-08-18 01:34:18 +000025 0, 0,
Lev Walkinf15320b2004-06-03 03:38:44 +000026 0, /* Use generic outmost tag fetcher */
Lev Walkin5e033762004-09-29 13:26:15 +000027 asn_DEF_UTF8String_tags,
28 sizeof(asn_DEF_UTF8String_tags)
29 / sizeof(asn_DEF_UTF8String_tags[0]) - 1,
30 asn_DEF_UTF8String_tags,
31 sizeof(asn_DEF_UTF8String_tags)
32 / sizeof(asn_DEF_UTF8String_tags[0]),
Lev Walkin59b176e2005-11-26 11:25:14 +000033 0, /* No PER visible constraints */
Lev Walkin449f8322004-08-20 13:23:42 +000034 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +000035 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +000036};
37
Lev Walkine18ca712004-10-02 11:37:38 +000038/*
39 * This is the table of length expectations.
Lev Walkin6cf9f712005-03-02 13:30:25 +000040 * The second half of this table is only applicable to the long sequences.
Lev Walkine18ca712004-10-02 11:37:38 +000041 */
42static int UTF8String_ht[2][16] = {
43 { /* 0x0 ... 0x7 */
44 /* 0000..0111 */
45 1, 1, 1, 1, 1, 1, 1, 1,
46 /* 1000..1011(0), 1100..1101(2), 1110(3), 1111(-1) */
47 0, 0, 0, 0, 2, 2, 3, -1 },
48 { /* 0xF0 .. 0xF7 */
49 /* 11110000..11110111 */
50 4, 4, 4, 4, 4, 4, 4, 4,
51 5, 5, 5, 5, 6, 6, -1, -1 }
Lev Walkinf15320b2004-06-03 03:38:44 +000052};
Lev Walkinedc7d592004-10-02 15:55:07 +000053static int32_t UTF8String_mv[7] = { 0, 0,
54 0x00000080,
55 0x00000800,
56 0x00010000,
57 0x00200000,
58 0x04000000
59};
60
61/* Internal aliases for return codes */
62#define U8E_TRUNC -1 /* UTF-8 sequence truncated */
63#define U8E_ILLSTART -2 /* Illegal UTF-8 sequence start */
64#define U8E_NOTCONT -3 /* Continuation expectation failed */
65#define U8E_NOTMIN -4 /* Not minimal length encoding */
66#define U8E_EINVAL -5 /* Invalid arguments */
Lev Walkinf15320b2004-06-03 03:38:44 +000067
68int
Lev Walkin5e033762004-09-29 13:26:15 +000069UTF8String_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkin1eded352006-07-13 11:19:01 +000070 asn_app_constraint_failed_f *ctfailcb, void *app_key) {
Lev Walkinedc7d592004-10-02 15:55:07 +000071 ssize_t len = UTF8String_length((const UTF8String_t *)sptr);
72 switch(len) {
73 case U8E_EINVAL:
Lev Walkin1eded352006-07-13 11:19:01 +000074 _ASN_CTFAIL(app_key, td,
Lev Walkinedc7d592004-10-02 15:55:07 +000075 "%s: value not given", td->name);
76 break;
77 case U8E_TRUNC:
Lev Walkin1eded352006-07-13 11:19:01 +000078 _ASN_CTFAIL(app_key, td,
Lev Walkinedc7d592004-10-02 15:55:07 +000079 "%s: truncated UTF-8 sequence (%s:%d)",
80 td->name, __FILE__, __LINE__);
81 break;
82 case U8E_ILLSTART:
Lev Walkin1eded352006-07-13 11:19:01 +000083 _ASN_CTFAIL(app_key, td,
Lev Walkinedc7d592004-10-02 15:55:07 +000084 "%s: UTF-8 illegal start of encoding (%s:%d)",
85 td->name, __FILE__, __LINE__);
86 break;
87 case U8E_NOTCONT:
Lev Walkin1eded352006-07-13 11:19:01 +000088 _ASN_CTFAIL(app_key, td,
Lev Walkinedc7d592004-10-02 15:55:07 +000089 "%s: UTF-8 not continuation (%s:%d)",
90 td->name, __FILE__, __LINE__);
91 break;
92 case U8E_NOTMIN:
Lev Walkin1eded352006-07-13 11:19:01 +000093 _ASN_CTFAIL(app_key, td,
Lev Walkinedc7d592004-10-02 15:55:07 +000094 "%s: UTF-8 not minimal sequence (%s:%d)",
95 td->name, __FILE__, __LINE__);
96 break;
97 }
98 return (len < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +000099}
100
Lev Walkin9a6f65b2004-10-23 11:20:06 +0000101static ssize_t
102UTF8String__process(const UTF8String_t *st, uint32_t *dst, size_t dstlen) {
103 size_t length;
104 uint8_t *buf = st->buf;
105 uint8_t *end = buf + st->size;
Lev Walkinf15320b2004-06-03 03:38:44 +0000106
Lev Walkin9a6f65b2004-10-23 11:20:06 +0000107 for(length = 0; buf < end; length++) {
108 int ch = *buf;
109 uint8_t *cend;
110 int32_t value;
111 int want;
Lev Walkinf15320b2004-06-03 03:38:44 +0000112
Lev Walkin9a6f65b2004-10-23 11:20:06 +0000113 /* Compute the sequence length */
114 want = UTF8String_ht[0][ch >> 4];
115 switch(want) {
116 case -1:
117 /* Second half of the table, long sequence */
118 want = UTF8String_ht[1][ch & 0x0F];
119 if(want != -1) break;
120 /* Fall through */
121 case 0:
122 return U8E_ILLSTART;
Lev Walkinf15320b2004-06-03 03:38:44 +0000123 }
124
Lev Walkin9a6f65b2004-10-23 11:20:06 +0000125 /* assert(want >= 1 && want <= 6) */
126
127 /* Check character sequence length */
128 if(buf + want > end) return U8E_TRUNC;
129
130 value = ch & (0xff >> (want + 1));
131 cend = buf + want;
132 for(buf++; buf < cend; buf++) {
133 ch = *buf;
134 if(ch < 0x80 || ch > 0xbf) return U8E_NOTCONT;
135 value = (value << 6) | (ch & 0x3F);
136 }
137 if(value < UTF8String_mv[want])
138 return U8E_NOTMIN;
139 if(dstlen) *dst++ = value; /* Record value */
140 }
141
142 if(dstlen) *dst = 0; /* zero-terminate */
143
144 return length;
145}
146
147
148ssize_t
149UTF8String_length(const UTF8String_t *st) {
150 if(st && st->buf) {
151 return UTF8String__process(st, 0, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000152 } else {
Lev Walkinedc7d592004-10-02 15:55:07 +0000153 return U8E_EINVAL;
Lev Walkinf15320b2004-06-03 03:38:44 +0000154 }
155}
156
Lev Walkin9a6f65b2004-10-23 11:20:06 +0000157size_t
158UTF8String_to_wcs(const UTF8String_t *st, uint32_t *dst, size_t dstlen) {
159 if(st && st->buf) {
160 ssize_t ret = UTF8String__process(st, dst, dstlen);
161 return (ret < 0) ? 0 : ret;
162 } else {
163 return 0;
164 }
165}
166
Lev Walkinf15320b2004-06-03 03:38:44 +0000167int
Lev Walkin5e033762004-09-29 13:26:15 +0000168UTF8String_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +0000169 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +0000170 const UTF8String_t *st = (const UTF8String_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000171
Lev Walkind9bd7752004-06-05 08:17:50 +0000172 (void)td; /* Unused argument */
173 (void)ilevel; /* Unused argument */
174
Lev Walkinf15320b2004-06-03 03:38:44 +0000175 if(st && st->buf) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000176 return (cb(st->buf, st->size, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000177 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000178 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000179 }
180}