blob: 1c94645e7c8f28a4c3c5231e21ded797a33d8217 [file] [log] [blame]
vlmfa67ddc2004-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 */
vlm39ba4c42004-09-22 16:06:28 +00005#include <asn_internal.h>
vlmfa67ddc2004-06-03 03:38:44 +00006#include <UTF8String.h>
7
8/*
9 * UTF8String basic type description.
10 */
vlmef6355b2004-09-29 13:26:15 +000011static ber_tlv_tag_t asn_DEF_UTF8String_tags[] = {
vlm72425de2004-09-13 08:31:01 +000012 (ASN_TAG_CLASS_UNIVERSAL | (12 << 2)), /* [UNIVERSAL 12] IMPLICIT ...*/
13 (ASN_TAG_CLASS_UNIVERSAL | (4 << 2)), /* ... OCTET STRING */
vlmfa67ddc2004-06-03 03:38:44 +000014};
vlmef6355b2004-09-29 13:26:15 +000015asn_TYPE_descriptor_t asn_DEF_UTF8String = {
vlmfa67ddc2004-06-03 03:38:44 +000016 "UTF8String",
vlm39ba4c42004-09-22 16:06:28 +000017 OCTET_STRING_free,
18 UTF8String_print,
vlmfa67ddc2004-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 */
vlm39ba4c42004-09-22 16:06:28 +000022 0, /* Not implemented yet */
23 OCTET_STRING_encode_xer_ascii, /* Already in UTF-8 format */
vlmfa67ddc2004-06-03 03:38:44 +000024 0, /* Use generic outmost tag fetcher */
vlmef6355b2004-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]),
vlme413c122004-08-20 13:23:42 +000031 0, 0, /* No members */
vlmb42843a2004-06-05 08:17:50 +000032 0 /* No specifics */
vlmfa67ddc2004-06-03 03:38:44 +000033};
34
vlm51283292004-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 }
vlmfa67ddc2004-06-03 03:38:44 +000049};
50
51int
vlmef6355b2004-09-29 13:26:15 +000052UTF8String_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
vlmfa67ddc2004-06-03 03:38:44 +000053 asn_app_consume_bytes_f *app_errlog, void *app_key) {
54 ssize_t len;
vlmda674682004-08-11 09:07:36 +000055 len = UTF8String_length((const UTF8String_t *)sptr, td->name,
56 app_errlog, app_key);
vlmfa67ddc2004-06-03 03:38:44 +000057 if(len > 0) len = 0;
58 return len;
59}
60
61ssize_t
62UTF8String_length(const UTF8String_t *st, const char *opt_type_name,
63 asn_app_consume_bytes_f *app_errlog, void *app_key) {
64
65 if(st && st->buf) {
vlm36982ee2004-10-02 12:29:44 +000066 size_t length;
vlmfa67ddc2004-06-03 03:38:44 +000067 uint8_t *buf = st->buf;
68 uint8_t *end = buf + st->size;
vlmfa67ddc2004-06-03 03:38:44 +000069
vlm36982ee2004-10-02 12:29:44 +000070 for(length = 0; buf < end; length++) {
71 int ch = *buf;
72 int want = UTF8String_ht[0][ch >> 4];
73 switch(want) {
74 case -1: /* Second half of the table, long sequence */
75 want = UTF8String_ht[1][ch & 0x0F];
76 if(want != -1)
77 break; /* Fine value */
78 case 0: /* 10xxxxxx should not appear here */
79 _ASN_ERRLOG(app_errlog, app_key,
80 "%s: UTF-8 expectation failed "
81 "at byte %d (%s:%d)",
82 opt_type_name,
83 (buf - st->buf) + 1,
84 __FILE__, __LINE__);
85 return -1;
vlmfa67ddc2004-06-03 03:38:44 +000086 }
vlm36982ee2004-10-02 12:29:44 +000087 if(buf + want > end) {
88 _ASN_ERRLOG(app_errlog, app_key,
89 "%s: truncated UTF-8 sequence (%s:%d)",
90 opt_type_name, __FILE__, __LINE__);
91 return -1;
92 }
93 buf += want;
vlmfa67ddc2004-06-03 03:38:44 +000094 }
95
96 return length;
97 } else {
vlme3f0f282004-08-11 09:44:13 +000098 _ASN_ERRLOG(app_errlog, app_key,
99 "%s: value not given", opt_type_name);
vlmfa67ddc2004-06-03 03:38:44 +0000100 return -1;
101 }
102}
103
104int
vlmef6355b2004-09-29 13:26:15 +0000105UTF8String_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
vlmfa67ddc2004-06-03 03:38:44 +0000106 asn_app_consume_bytes_f *cb, void *app_key) {
vlmda674682004-08-11 09:07:36 +0000107 const UTF8String_t *st = (const UTF8String_t *)sptr;
vlmfa67ddc2004-06-03 03:38:44 +0000108
vlmb42843a2004-06-05 08:17:50 +0000109 (void)td; /* Unused argument */
110 (void)ilevel; /* Unused argument */
111
vlmfa67ddc2004-06-03 03:38:44 +0000112 if(st && st->buf) {
vlm6678cb12004-09-26 13:10:40 +0000113 return (cb(st->buf, st->size, app_key) < 0) ? -1 : 0;
vlmfa67ddc2004-06-03 03:38:44 +0000114 } else {
vlm6678cb12004-09-26 13:10:40 +0000115 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
vlmfa67ddc2004-06-03 03:38:44 +0000116 }
117}