blob: 934a424aaaeff516561c8dd6c5cda23242b65ef8 [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 Walkina9cc46e2004-09-22 16:06:28 +000017 OCTET_STRING_free,
18 UTF8String_print,
Lev Walkinf15320b2004-06-03 03:38:44 +000019 UTF8String_constraint, /* Check for invalid codes, etc. */
20 OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
21 OCTET_STRING_encode_der, /* Implemented in terms of OCTET STRING */
Lev Walkina9cc46e2004-09-22 16:06:28 +000022 0, /* Not implemented yet */
23 OCTET_STRING_encode_xer_ascii, /* Already in UTF-8 format */
Lev Walkinf15320b2004-06-03 03:38:44 +000024 0, /* Use generic outmost tag fetcher */
Lev Walkin5e033762004-09-29 13:26:15 +000025 asn_DEF_UTF8String_tags,
26 sizeof(asn_DEF_UTF8String_tags)
27 / sizeof(asn_DEF_UTF8String_tags[0]) - 1,
28 asn_DEF_UTF8String_tags,
29 sizeof(asn_DEF_UTF8String_tags)
30 / sizeof(asn_DEF_UTF8String_tags[0]),
Lev Walkin449f8322004-08-20 13:23:42 +000031 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +000032 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +000033};
34
Lev Walkine18ca712004-10-02 11:37:38 +000035/*
36 * This is the table of length expectations.
37 * The second half of this table is only applicable to the long sequentes.
38 */
39static int UTF8String_ht[2][16] = {
40 { /* 0x0 ... 0x7 */
41 /* 0000..0111 */
42 1, 1, 1, 1, 1, 1, 1, 1,
43 /* 1000..1011(0), 1100..1101(2), 1110(3), 1111(-1) */
44 0, 0, 0, 0, 2, 2, 3, -1 },
45 { /* 0xF0 .. 0xF7 */
46 /* 11110000..11110111 */
47 4, 4, 4, 4, 4, 4, 4, 4,
48 5, 5, 5, 5, 6, 6, -1, -1 }
Lev Walkinf15320b2004-06-03 03:38:44 +000049};
Lev Walkinedc7d592004-10-02 15:55:07 +000050static int32_t UTF8String_mv[7] = { 0, 0,
51 0x00000080,
52 0x00000800,
53 0x00010000,
54 0x00200000,
55 0x04000000
56};
57
58/* Internal aliases for return codes */
59#define U8E_TRUNC -1 /* UTF-8 sequence truncated */
60#define U8E_ILLSTART -2 /* Illegal UTF-8 sequence start */
61#define U8E_NOTCONT -3 /* Continuation expectation failed */
62#define U8E_NOTMIN -4 /* Not minimal length encoding */
63#define U8E_EINVAL -5 /* Invalid arguments */
Lev Walkinf15320b2004-06-03 03:38:44 +000064
65int
Lev Walkin5e033762004-09-29 13:26:15 +000066UTF8String_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +000067 asn_app_consume_bytes_f *app_errlog, void *app_key) {
Lev Walkinedc7d592004-10-02 15:55:07 +000068 ssize_t len = UTF8String_length((const UTF8String_t *)sptr);
69 switch(len) {
70 case U8E_EINVAL:
71 _ASN_ERRLOG(app_errlog, app_key,
72 "%s: value not given", td->name);
73 break;
74 case U8E_TRUNC:
75 _ASN_ERRLOG(app_errlog, app_key,
76 "%s: truncated UTF-8 sequence (%s:%d)",
77 td->name, __FILE__, __LINE__);
78 break;
79 case U8E_ILLSTART:
80 _ASN_ERRLOG(app_errlog, app_key,
81 "%s: UTF-8 illegal start of encoding (%s:%d)",
82 td->name, __FILE__, __LINE__);
83 break;
84 case U8E_NOTCONT:
85 _ASN_ERRLOG(app_errlog, app_key,
86 "%s: UTF-8 not continuation (%s:%d)",
87 td->name, __FILE__, __LINE__);
88 break;
89 case U8E_NOTMIN:
90 _ASN_ERRLOG(app_errlog, app_key,
91 "%s: UTF-8 not minimal sequence (%s:%d)",
92 td->name, __FILE__, __LINE__);
93 break;
94 }
95 return (len < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +000096}
97
98ssize_t
Lev Walkinedc7d592004-10-02 15:55:07 +000099UTF8String_length(const UTF8String_t *st) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000100
101 if(st && st->buf) {
Lev Walkinbbd66b02004-10-02 12:29:44 +0000102 size_t length;
Lev Walkinf15320b2004-06-03 03:38:44 +0000103 uint8_t *buf = st->buf;
104 uint8_t *end = buf + st->size;
Lev Walkinf15320b2004-06-03 03:38:44 +0000105
Lev Walkinbbd66b02004-10-02 12:29:44 +0000106 for(length = 0; buf < end; length++) {
107 int ch = *buf;
Lev Walkinedc7d592004-10-02 15:55:07 +0000108 uint8_t *cend;
109 int32_t value;
110 int want;
111
112 /* Compute the sequence length */
113 want = UTF8String_ht[0][ch >> 4];
Lev Walkinbbd66b02004-10-02 12:29:44 +0000114 switch(want) {
Lev Walkinedc7d592004-10-02 15:55:07 +0000115 case -1:
116 /* Second half of the table, long sequence */
Lev Walkinbbd66b02004-10-02 12:29:44 +0000117 want = UTF8String_ht[1][ch & 0x0F];
Lev Walkinedc7d592004-10-02 15:55:07 +0000118 if(want != -1) break;
119 /* Fall through */
120 case 0:
121 return U8E_ILLSTART;
Lev Walkinf15320b2004-06-03 03:38:44 +0000122 }
Lev Walkinedc7d592004-10-02 15:55:07 +0000123
124 /* assert(want >= 1 && want <= 6) */
125
126 /* Check character sequence length */
127 if(buf + want > end) return U8E_TRUNC;
128
129 value = ch & (0xff >> (want + 1));
130 cend = buf + want;
131 for(buf++; buf < cend; buf++) {
132 ch = *buf;
133 if(ch < 0x80 || ch > 0xbf) return U8E_NOTCONT;
134 value = (value << 6) | (ch & 0x3F);
Lev Walkinbbd66b02004-10-02 12:29:44 +0000135 }
Lev Walkinedc7d592004-10-02 15:55:07 +0000136 if(value < UTF8String_mv[want])
137 return U8E_NOTMIN;
Lev Walkinf15320b2004-06-03 03:38:44 +0000138 }
139
140 return length;
141 } else {
Lev Walkinedc7d592004-10-02 15:55:07 +0000142 return U8E_EINVAL;
Lev Walkinf15320b2004-06-03 03:38:44 +0000143 }
144}
145
146int
Lev Walkin5e033762004-09-29 13:26:15 +0000147UTF8String_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +0000148 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +0000149 const UTF8String_t *st = (const UTF8String_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000150
Lev Walkind9bd7752004-06-05 08:17:50 +0000151 (void)td; /* Unused argument */
152 (void)ilevel; /* Unused argument */
153
Lev Walkinf15320b2004-06-03 03:38:44 +0000154 if(st && st->buf) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000155 return (cb(st->buf, st->size, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000156 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000157 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000158 }
159}