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