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