ISSUE ID:- RICAPP-216
[ric-app/bouncer.git] / Bouncer / e2sm_kpm / lib / UTF8String.c
1 /*-
2  * Copyright (c) 2003, 2004, 2006 Lev Walkin <vlm@lionet.info>.
3  * All rights reserved.
4  * Redistribution and modifications are permitted subject to BSD license.
5  */
6 #include <asn_internal.h>
7 #include <UTF8String.h>
8
9 /*
10  * UTF8String basic type description.
11  */
12 static const ber_tlv_tag_t asn_DEF_UTF8String_tags[] = {
13         (ASN_TAG_CLASS_UNIVERSAL | (12 << 2)),  /* [UNIVERSAL 12] IMPLICIT ...*/
14         (ASN_TAG_CLASS_UNIVERSAL | (4 << 2)),   /* ... OCTET STRING */
15 };
16 asn_TYPE_operation_t asn_OP_UTF8String = {
17         OCTET_STRING_free,
18         UTF8String_print,
19         OCTET_STRING_compare,
20         OCTET_STRING_decode_ber,    /* Implemented in terms of OCTET STRING */
21         OCTET_STRING_encode_der,
22         OCTET_STRING_decode_xer_utf8,
23         OCTET_STRING_encode_xer_utf8,
24 #ifdef  ASN_DISABLE_OER_SUPPORT
25         0,
26         0,
27 #else
28         OCTET_STRING_decode_oer,
29         OCTET_STRING_encode_oer,
30 #endif  /* ASN_DISABLE_OER_SUPPORT */
31 #ifdef  ASN_DISABLE_PER_SUPPORT
32         0,
33         0,
34         0,
35         0,
36 #else
37         OCTET_STRING_decode_uper,
38         OCTET_STRING_encode_uper,
39         OCTET_STRING_decode_aper,
40         OCTET_STRING_encode_aper,
41 #endif  /* ASN_DISABLE_PER_SUPPORT */
42         UTF8String_random_fill,
43         0       /* Use generic outmost tag fetcher */
44 };
45 asn_TYPE_descriptor_t asn_DEF_UTF8String = {
46         "UTF8String",
47         "UTF8String",
48         &asn_OP_UTF8String,
49         asn_DEF_UTF8String_tags,
50         sizeof(asn_DEF_UTF8String_tags)
51           / sizeof(asn_DEF_UTF8String_tags[0]) - 1,
52         asn_DEF_UTF8String_tags,
53         sizeof(asn_DEF_UTF8String_tags)
54           / sizeof(asn_DEF_UTF8String_tags[0]),
55         { 0, 0, UTF8String_constraint },
56         0, 0,   /* No members */
57         0       /* No specifics */
58 };
59
60 /*
61  * This is the table of length expectations.
62  * The second half of this table is only applicable to the long sequences.
63  */
64 static const int UTF8String_ht[2][16] = {
65         { /* 0x0 ... 0x7 */
66           /* 0000..0111 */
67           1, 1, 1, 1, 1, 1, 1, 1,
68           /* 1000..1011(0), 1100..1101(2), 1110(3), 1111(-1) */
69           0, 0, 0, 0, 2, 2, 3, -1 },
70         { /* 0xF0 .. 0xF7 */
71           /* 11110000..11110111 */
72           4, 4, 4, 4, 4, 4, 4, 4,
73           5, 5, 5, 5, 6, 6, -1, -1 }
74 };
75 static const int32_t UTF8String_mv[7] = { 0, 0,
76         0x00000080,
77         0x00000800,
78         0x00010000,
79         0x00200000,
80         0x04000000
81 };
82
83 /* Internal aliases for return codes */
84 #define U8E_TRUNC       -1      /* UTF-8 sequence truncated */
85 #define U8E_ILLSTART    -2      /* Illegal UTF-8 sequence start */
86 #define U8E_NOTCONT     -3      /* Continuation expectation failed */
87 #define U8E_NOTMIN      -4      /* Not minimal length encoding */
88 #define U8E_EINVAL      -5      /* Invalid arguments */
89
90 int
91 UTF8String_constraint(const asn_TYPE_descriptor_t *td, const void *sptr,
92                       asn_app_constraint_failed_f *ctfailcb, void *app_key) {
93     ssize_t len = UTF8String_length((const UTF8String_t *)sptr);
94         switch(len) {
95         case U8E_EINVAL:
96                 ASN__CTFAIL(app_key, td, sptr,
97                         "%s: value not given", td->name);
98                 break;
99         case U8E_TRUNC:
100                 ASN__CTFAIL(app_key, td, sptr,
101                         "%s: truncated UTF-8 sequence (%s:%d)",
102                         td->name, __FILE__, __LINE__);
103                 break;
104         case U8E_ILLSTART:
105                 ASN__CTFAIL(app_key, td, sptr,
106                         "%s: UTF-8 illegal start of encoding (%s:%d)",
107                         td->name, __FILE__, __LINE__);
108                 break;
109         case U8E_NOTCONT:
110                 ASN__CTFAIL(app_key, td, sptr,
111                         "%s: UTF-8 not continuation (%s:%d)",
112                         td->name, __FILE__, __LINE__);
113                 break;
114         case U8E_NOTMIN:
115                 ASN__CTFAIL(app_key, td, sptr,
116                         "%s: UTF-8 not minimal sequence (%s:%d)",
117                         td->name, __FILE__, __LINE__);
118                 break;
119         }
120         return (len < 0) ? -1 : 0;
121 }
122
123 static ssize_t
124 UTF8String__process(const UTF8String_t *st, uint32_t *dst, size_t dstlen) {
125         size_t length;
126         uint8_t *buf = st->buf;
127         uint8_t *end = buf + st->size;
128         uint32_t *dstend = dst + dstlen;
129
130         for(length = 0; buf < end; length++) {
131                 int ch = *buf;
132                 uint8_t *cend;
133                 int32_t value;
134                 int want;
135
136                 /* Compute the sequence length */
137                 want = UTF8String_ht[0][ch >> 4];
138                 switch(want) {
139                 case -1:
140                         /* Second half of the table, long sequence */
141                         want = UTF8String_ht[1][ch & 0x0F];
142                         if(want != -1) break;
143                         /* Fall through */
144                 case 0:
145                         return U8E_ILLSTART;
146                 }
147
148                 /* assert(want >= 1 && want <= 6) */
149
150                 /* Check character sequence length */
151                 if(buf + want > end) return U8E_TRUNC;
152
153                 value = ch & (0xff >> want);
154                 cend = buf + want;
155                 for(buf++; buf < cend; buf++) {
156                         ch = *buf;
157                         if(ch < 0x80 || ch > 0xbf) return U8E_NOTCONT;
158                         value = (value << 6) | (ch & 0x3F);
159                 }
160                 if(value < UTF8String_mv[want])
161                         return U8E_NOTMIN;
162                 if(dst < dstend)
163                         *dst++ = value; /* Record value */
164         }
165
166         if(dst < dstend) *dst = 0;      /* zero-terminate */
167
168         return length;
169 }
170
171
172 ssize_t
173 UTF8String_length(const UTF8String_t *st) {
174         if(st && st->buf) {
175                 return UTF8String__process(st, 0, 0);
176         } else {
177                 return U8E_EINVAL;
178         }
179 }
180
181 size_t
182 UTF8String_to_wcs(const UTF8String_t *st, uint32_t *dst, size_t dstlen) {
183         if(st && st->buf) {
184                 ssize_t ret = UTF8String__process(st, dst, dstlen);
185                 return (ret < 0) ? 0 : ret;
186         } else {
187                 return 0;
188         }
189 }
190
191 int
192 UTF8String_print(const asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
193                  asn_app_consume_bytes_f *cb, void *app_key) {
194     const UTF8String_t *st = (const UTF8String_t *)sptr;
195
196         (void)td;       /* Unused argument */
197         (void)ilevel;   /* Unused argument */
198
199         if(st && st->buf) {
200                 return (cb(st->buf, st->size, app_key) < 0) ? -1 : 0;
201         } else {
202                 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
203         }
204 }
205
206
207 /*
208  * Biased function for randomizing UTF-8 sequences.
209  */
210 static size_t
211 UTF8String__random_char(uint8_t *b, size_t size) {
212     static const struct rnd_value {
213         const char *value;
214         size_t size;
215     } values[] = {{"\0", 1},
216                   {"\x01", 1},
217                   {"\x7f", 1},
218                   {"\xc2\xa2", 2},
219                   {"\xe2\x82\xac", 3},
220                   {"\xf0\x90\x8d\x88", 4},
221                   {"\xf4\x8f\xbf\xbf", 4}};
222
223     const struct rnd_value *v;
224     size_t max_idx = 0;
225
226     switch(size) {
227     case 0:
228         assert(size != 0);
229         return 0;
230     case 1:
231         max_idx = 2;
232         break;
233     case 2:
234         max_idx = 3;
235         break;
236     default:
237     case 4:
238         max_idx = sizeof(values) / sizeof(values[0]) - 1;
239         break;
240     }
241
242     v = &values[asn_random_between(0, max_idx)];
243     memcpy(b, v->value, v->size);
244     return v->size;
245 }
246
247 asn_random_fill_result_t
248 UTF8String_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
249                        const asn_encoding_constraints_t *constraints,
250                        size_t max_length) {
251     asn_random_fill_result_t result_ok = {ARFILL_OK, 1};
252     asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0};
253     asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0};
254     uint8_t *buf;
255     uint8_t *bend;
256     uint8_t *b;
257     size_t rnd_len;
258     size_t idx;
259     UTF8String_t *st;
260
261     if(max_length == 0 && !*sptr) return result_skipped;
262
263     /* Figure out how far we should go */
264     rnd_len = OCTET_STRING_random_length_constrained(td, constraints,
265                                                      max_length / 4);
266
267     buf = CALLOC(4, rnd_len + 1);
268     if(!buf) return result_failed;
269
270     bend = &buf[4 * rnd_len];
271
272     for(b = buf, idx = 0; idx < rnd_len; idx++) {
273         b += UTF8String__random_char(b, (bend - b));
274     }
275     *(uint8_t *)b = 0;
276
277     if(*sptr) {
278         st = *sptr;
279         FREEMEM(st->buf);
280     } else {
281         st = (OCTET_STRING_t *)(*sptr = CALLOC(1, sizeof(UTF8String_t)));
282         if(!st) {
283             FREEMEM(buf);
284             return result_failed;
285         }
286     }
287
288     st->buf = buf;
289     st->size = b - buf;
290
291     assert(UTF8String_length(st) == (ssize_t)rnd_len);
292
293     return result_ok;
294 }