Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | /*- |
Lev Walkin | c7549b1 | 2006-09-17 01:27:44 +0000 | [diff] [blame] | 2 | * Copyright (c) 2003, 2004, 2006 Lev Walkin <vlm@lionet.info>. |
| 3 | * All rights reserved. |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 4 | * Redistribution and modifications are permitted subject to BSD license. |
| 5 | */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 6 | #include <asn_internal.h> |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 7 | #include <UTF8String.h> |
| 8 | |
| 9 | /* |
| 10 | * UTF8String basic type description. |
| 11 | */ |
Wim Lewis | 18c2ec9 | 2014-07-29 11:30:10 -0700 | [diff] [blame] | 12 | static const ber_tlv_tag_t asn_DEF_UTF8String_tags[] = { |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 13 | (ASN_TAG_CLASS_UNIVERSAL | (12 << 2)), /* [UNIVERSAL 12] IMPLICIT ...*/ |
| 14 | (ASN_TAG_CLASS_UNIVERSAL | (4 << 2)), /* ... OCTET STRING */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 15 | }; |
Bi-Ruei, Chiu | 1f87ac0 | 2017-08-20 01:25:45 +0800 | [diff] [blame] | 16 | asn_TYPE_operation_t asn_OP_UTF8String = { |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 17 | OCTET_STRING_free, |
| 18 | UTF8String_print, |
Lev Walkin | cd2f48e | 2017-08-10 02:14:59 -0700 | [diff] [blame] | 19 | OCTET_STRING_compare, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 20 | UTF8String_constraint, /* Check for invalid codes, etc. */ |
| 21 | OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */ |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 22 | OCTET_STRING_encode_der, |
| 23 | OCTET_STRING_decode_xer_utf8, |
| 24 | OCTET_STRING_encode_xer_utf8, |
Lev Walkin | cc15947 | 2017-07-06 08:26:36 -0700 | [diff] [blame] | 25 | #ifdef ASN_DISABLE_OER_SUPPORT |
| 26 | 0, |
| 27 | 0, |
| 28 | #else |
Lev Walkin | 1d76f3c | 2017-07-25 07:58:05 -0700 | [diff] [blame] | 29 | OCTET_STRING_decode_oer, |
| 30 | OCTET_STRING_encode_oer, |
Lev Walkin | cc15947 | 2017-07-06 08:26:36 -0700 | [diff] [blame] | 31 | #endif /* ASN_DISABLE_OER_SUPPORT */ |
Lev Walkin | b33425f | 2017-07-14 14:59:52 +0400 | [diff] [blame] | 32 | #ifdef ASN_DISABLE_PER_SUPPORT |
| 33 | 0, |
| 34 | 0, |
| 35 | #else |
| 36 | OCTET_STRING_decode_uper, |
| 37 | OCTET_STRING_encode_uper, |
| 38 | #endif /* ASN_DISABLE_PER_SUPPORT */ |
Bi-Ruei, Chiu | 1f87ac0 | 2017-08-20 01:25:45 +0800 | [diff] [blame] | 39 | 0 /* Use generic outmost tag fetcher */ |
| 40 | }; |
| 41 | asn_TYPE_descriptor_t asn_DEF_UTF8String = { |
| 42 | "UTF8String", |
| 43 | "UTF8String", |
| 44 | &asn_OP_UTF8String, |
| 45 | UTF8String_constraint, /* Check for invalid codes, etc. */ |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 46 | asn_DEF_UTF8String_tags, |
| 47 | sizeof(asn_DEF_UTF8String_tags) |
| 48 | / sizeof(asn_DEF_UTF8String_tags[0]) - 1, |
| 49 | asn_DEF_UTF8String_tags, |
| 50 | sizeof(asn_DEF_UTF8String_tags) |
| 51 | / sizeof(asn_DEF_UTF8String_tags[0]), |
Lev Walkin | 7678076 | 2017-07-07 10:07:30 -0700 | [diff] [blame] | 52 | 0, /* No OER visible constraints */ |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 53 | 0, /* No PER visible constraints */ |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 54 | 0, 0, /* No members */ |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 55 | 0 /* No specifics */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 56 | }; |
| 57 | |
Lev Walkin | e18ca71 | 2004-10-02 11:37:38 +0000 | [diff] [blame] | 58 | /* |
| 59 | * This is the table of length expectations. |
Lev Walkin | 6cf9f71 | 2005-03-02 13:30:25 +0000 | [diff] [blame] | 60 | * The second half of this table is only applicable to the long sequences. |
Lev Walkin | e18ca71 | 2004-10-02 11:37:38 +0000 | [diff] [blame] | 61 | */ |
Wim Lewis | 18c2ec9 | 2014-07-29 11:30:10 -0700 | [diff] [blame] | 62 | static const int UTF8String_ht[2][16] = { |
Lev Walkin | e18ca71 | 2004-10-02 11:37:38 +0000 | [diff] [blame] | 63 | { /* 0x0 ... 0x7 */ |
| 64 | /* 0000..0111 */ |
| 65 | 1, 1, 1, 1, 1, 1, 1, 1, |
| 66 | /* 1000..1011(0), 1100..1101(2), 1110(3), 1111(-1) */ |
| 67 | 0, 0, 0, 0, 2, 2, 3, -1 }, |
| 68 | { /* 0xF0 .. 0xF7 */ |
| 69 | /* 11110000..11110111 */ |
| 70 | 4, 4, 4, 4, 4, 4, 4, 4, |
| 71 | 5, 5, 5, 5, 6, 6, -1, -1 } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 72 | }; |
Wim Lewis | 18c2ec9 | 2014-07-29 11:30:10 -0700 | [diff] [blame] | 73 | static const int32_t UTF8String_mv[7] = { 0, 0, |
Lev Walkin | edc7d59 | 2004-10-02 15:55:07 +0000 | [diff] [blame] | 74 | 0x00000080, |
| 75 | 0x00000800, |
| 76 | 0x00010000, |
| 77 | 0x00200000, |
| 78 | 0x04000000 |
| 79 | }; |
| 80 | |
| 81 | /* Internal aliases for return codes */ |
| 82 | #define U8E_TRUNC -1 /* UTF-8 sequence truncated */ |
| 83 | #define U8E_ILLSTART -2 /* Illegal UTF-8 sequence start */ |
| 84 | #define U8E_NOTCONT -3 /* Continuation expectation failed */ |
| 85 | #define U8E_NOTMIN -4 /* Not minimal length encoding */ |
| 86 | #define U8E_EINVAL -5 /* Invalid arguments */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 87 | |
| 88 | int |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 89 | UTF8String_constraint(asn_TYPE_descriptor_t *td, const void *sptr, |
Lev Walkin | 1eded35 | 2006-07-13 11:19:01 +0000 | [diff] [blame] | 90 | asn_app_constraint_failed_f *ctfailcb, void *app_key) { |
Lev Walkin | edc7d59 | 2004-10-02 15:55:07 +0000 | [diff] [blame] | 91 | ssize_t len = UTF8String_length((const UTF8String_t *)sptr); |
| 92 | switch(len) { |
| 93 | case U8E_EINVAL: |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 94 | ASN__CTFAIL(app_key, td, sptr, |
Lev Walkin | edc7d59 | 2004-10-02 15:55:07 +0000 | [diff] [blame] | 95 | "%s: value not given", td->name); |
| 96 | break; |
| 97 | case U8E_TRUNC: |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 98 | ASN__CTFAIL(app_key, td, sptr, |
Lev Walkin | edc7d59 | 2004-10-02 15:55:07 +0000 | [diff] [blame] | 99 | "%s: truncated UTF-8 sequence (%s:%d)", |
| 100 | td->name, __FILE__, __LINE__); |
| 101 | break; |
| 102 | case U8E_ILLSTART: |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 103 | ASN__CTFAIL(app_key, td, sptr, |
Lev Walkin | edc7d59 | 2004-10-02 15:55:07 +0000 | [diff] [blame] | 104 | "%s: UTF-8 illegal start of encoding (%s:%d)", |
| 105 | td->name, __FILE__, __LINE__); |
| 106 | break; |
| 107 | case U8E_NOTCONT: |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 108 | ASN__CTFAIL(app_key, td, sptr, |
Lev Walkin | edc7d59 | 2004-10-02 15:55:07 +0000 | [diff] [blame] | 109 | "%s: UTF-8 not continuation (%s:%d)", |
| 110 | td->name, __FILE__, __LINE__); |
| 111 | break; |
| 112 | case U8E_NOTMIN: |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 113 | ASN__CTFAIL(app_key, td, sptr, |
Lev Walkin | edc7d59 | 2004-10-02 15:55:07 +0000 | [diff] [blame] | 114 | "%s: UTF-8 not minimal sequence (%s:%d)", |
| 115 | td->name, __FILE__, __LINE__); |
| 116 | break; |
| 117 | } |
| 118 | return (len < 0) ? -1 : 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Lev Walkin | 9a6f65b | 2004-10-23 11:20:06 +0000 | [diff] [blame] | 121 | static ssize_t |
| 122 | UTF8String__process(const UTF8String_t *st, uint32_t *dst, size_t dstlen) { |
| 123 | size_t length; |
| 124 | uint8_t *buf = st->buf; |
| 125 | uint8_t *end = buf + st->size; |
Lev Walkin | c7549b1 | 2006-09-17 01:27:44 +0000 | [diff] [blame] | 126 | uint32_t *dstend = dst + dstlen; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 127 | |
Lev Walkin | 9a6f65b | 2004-10-23 11:20:06 +0000 | [diff] [blame] | 128 | for(length = 0; buf < end; length++) { |
| 129 | int ch = *buf; |
| 130 | uint8_t *cend; |
| 131 | int32_t value; |
| 132 | int want; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 133 | |
Lev Walkin | 9a6f65b | 2004-10-23 11:20:06 +0000 | [diff] [blame] | 134 | /* Compute the sequence length */ |
| 135 | want = UTF8String_ht[0][ch >> 4]; |
| 136 | switch(want) { |
| 137 | case -1: |
| 138 | /* Second half of the table, long sequence */ |
| 139 | want = UTF8String_ht[1][ch & 0x0F]; |
| 140 | if(want != -1) break; |
| 141 | /* Fall through */ |
| 142 | case 0: |
| 143 | return U8E_ILLSTART; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Lev Walkin | 9a6f65b | 2004-10-23 11:20:06 +0000 | [diff] [blame] | 146 | /* assert(want >= 1 && want <= 6) */ |
| 147 | |
| 148 | /* Check character sequence length */ |
| 149 | if(buf + want > end) return U8E_TRUNC; |
| 150 | |
Lev Walkin | c7549b1 | 2006-09-17 01:27:44 +0000 | [diff] [blame] | 151 | value = ch & (0xff >> want); |
Lev Walkin | 9a6f65b | 2004-10-23 11:20:06 +0000 | [diff] [blame] | 152 | cend = buf + want; |
| 153 | for(buf++; buf < cend; buf++) { |
| 154 | ch = *buf; |
| 155 | if(ch < 0x80 || ch > 0xbf) return U8E_NOTCONT; |
| 156 | value = (value << 6) | (ch & 0x3F); |
| 157 | } |
| 158 | if(value < UTF8String_mv[want]) |
| 159 | return U8E_NOTMIN; |
Lev Walkin | c7549b1 | 2006-09-17 01:27:44 +0000 | [diff] [blame] | 160 | if(dst < dstend) |
| 161 | *dst++ = value; /* Record value */ |
Lev Walkin | 9a6f65b | 2004-10-23 11:20:06 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Lev Walkin | c7549b1 | 2006-09-17 01:27:44 +0000 | [diff] [blame] | 164 | if(dst < dstend) *dst = 0; /* zero-terminate */ |
Lev Walkin | 9a6f65b | 2004-10-23 11:20:06 +0000 | [diff] [blame] | 165 | |
| 166 | return length; |
| 167 | } |
| 168 | |
| 169 | |
| 170 | ssize_t |
| 171 | UTF8String_length(const UTF8String_t *st) { |
| 172 | if(st && st->buf) { |
| 173 | return UTF8String__process(st, 0, 0); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 174 | } else { |
Lev Walkin | edc7d59 | 2004-10-02 15:55:07 +0000 | [diff] [blame] | 175 | return U8E_EINVAL; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | |
Lev Walkin | 9a6f65b | 2004-10-23 11:20:06 +0000 | [diff] [blame] | 179 | size_t |
| 180 | UTF8String_to_wcs(const UTF8String_t *st, uint32_t *dst, size_t dstlen) { |
| 181 | if(st && st->buf) { |
| 182 | ssize_t ret = UTF8String__process(st, dst, dstlen); |
| 183 | return (ret < 0) ? 0 : ret; |
| 184 | } else { |
| 185 | return 0; |
| 186 | } |
| 187 | } |
| 188 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 189 | int |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 190 | UTF8String_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 191 | asn_app_consume_bytes_f *cb, void *app_key) { |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 192 | const UTF8String_t *st = (const UTF8String_t *)sptr; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 193 | |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 194 | (void)td; /* Unused argument */ |
| 195 | (void)ilevel; /* Unused argument */ |
| 196 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 197 | if(st && st->buf) { |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 198 | return (cb(st->buf, st->size, app_key) < 0) ? -1 : 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 199 | } else { |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 200 | return (cb("<absent>", 8, app_key) < 0) ? -1 : 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 201 | } |
| 202 | } |