ITS
UTF8String.c
Go to the documentation of this file.
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  */
13  (ASN_TAG_CLASS_UNIVERSAL | (12 << 2)), /* [UNIVERSAL 12] IMPLICIT ...*/
14  (ASN_TAG_CLASS_UNIVERSAL | (4 << 2)), /* ... OCTET STRING */
15 };
17  "UTF8String",
18  "UTF8String",
21  UTF8String_constraint, /* Check for invalid codes, etc. */
22  OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
28  0, /* Use generic outmost tag fetcher */
31  / sizeof(asn_DEF_UTF8String_tags[0]) - 1,
34  / sizeof(asn_DEF_UTF8String_tags[0]),
35  0, /* No PER visible constraints */
36  0, 0, /* No members */
37  0 /* No specifics */
38 };
39 
40 /*
41  * This is the table of length expectations.
42  * The second half of this table is only applicable to the long sequences.
43  */
44 static int UTF8String_ht[2][16] = {
45  { /* 0x0 ... 0x7 */
46  /* 0000..0111 */
47  1, 1, 1, 1, 1, 1, 1, 1,
48  /* 1000..1011(0), 1100..1101(2), 1110(3), 1111(-1) */
49  0, 0, 0, 0, 2, 2, 3, -1 },
50  { /* 0xF0 .. 0xF7 */
51  /* 11110000..11110111 */
52  4, 4, 4, 4, 4, 4, 4, 4,
53  5, 5, 5, 5, 6, 6, -1, -1 }
54 };
55 static int32_t UTF8String_mv[7] = { 0, 0,
56  0x00000080,
57  0x00000800,
58  0x00010000,
59  0x00200000,
60  0x04000000
61 };
62 
63 /* Internal aliases for return codes */
64 #define U8E_TRUNC -1 /* UTF-8 sequence truncated */
65 #define U8E_ILLSTART -2 /* Illegal UTF-8 sequence start */
66 #define U8E_NOTCONT -3 /* Continuation expectation failed */
67 #define U8E_NOTMIN -4 /* Not minimal length encoding */
68 #define U8E_EINVAL -5 /* Invalid arguments */
69 
70 int
72  asn_app_constraint_failed_f *ctfailcb, void *app_key) {
73  ssize_t len = UTF8String_length((const UTF8String_t *)sptr);
74  switch(len) {
75  case U8E_EINVAL:
76  _ASN_CTFAIL(app_key, td, sptr,
77  "%s: value not given", td->name);
78  break;
79  case U8E_TRUNC:
80  _ASN_CTFAIL(app_key, td, sptr,
81  "%s: truncated UTF-8 sequence (%s:%d)",
82  td->name, __FILE__, __LINE__);
83  break;
84  case U8E_ILLSTART:
85  _ASN_CTFAIL(app_key, td, sptr,
86  "%s: UTF-8 illegal start of encoding (%s:%d)",
87  td->name, __FILE__, __LINE__);
88  break;
89  case U8E_NOTCONT:
90  _ASN_CTFAIL(app_key, td, sptr,
91  "%s: UTF-8 not continuation (%s:%d)",
92  td->name, __FILE__, __LINE__);
93  break;
94  case U8E_NOTMIN:
95  _ASN_CTFAIL(app_key, td, sptr,
96  "%s: UTF-8 not minimal sequence (%s:%d)",
97  td->name, __FILE__, __LINE__);
98  break;
99  }
100  return (len < 0) ? -1 : 0;
101 }
102 
103 static ssize_t
104 UTF8String__process(const UTF8String_t *st, uint32_t *dst, size_t dstlen) {
105  size_t length;
106  uint8_t *buf = st->buf;
107  uint8_t *end = buf + st->size;
108  uint32_t *dstend = dst + dstlen;
109 
110  for(length = 0; buf < end; length++) {
111  int ch = *buf;
112  uint8_t *cend;
113  int32_t value;
114  int want;
115 
116  /* Compute the sequence length */
117  want = UTF8String_ht[0][ch >> 4];
118  switch(want) {
119  case -1:
120  /* Second half of the table, long sequence */
121  want = UTF8String_ht[1][ch & 0x0F];
122  if(want != -1) break;
123  /* Fall through */
124  case 0:
125  return U8E_ILLSTART;
126  }
127 
128  /* assert(want >= 1 && want <= 6) */
129 
130  /* Check character sequence length */
131  if(buf + want > end) return U8E_TRUNC;
132 
133  value = ch & (0xff >> want);
134  cend = buf + want;
135  for(buf++; buf < cend; buf++) {
136  ch = *buf;
137  if(ch < 0x80 || ch > 0xbf) return U8E_NOTCONT;
138  value = (value << 6) | (ch & 0x3F);
139  }
140  if(value < UTF8String_mv[want])
141  return U8E_NOTMIN;
142  if(dst < dstend)
143  *dst++ = value; /* Record value */
144  }
145 
146  if(dst < dstend) *dst = 0; /* zero-terminate */
147 
148  return length;
149 }
150 
151 
152 ssize_t
154  if(st && st->buf) {
155  return UTF8String__process(st, 0, 0);
156  } else {
157  return U8E_EINVAL;
158  }
159 }
160 
161 size_t
162 UTF8String_to_wcs(const UTF8String_t *st, uint32_t *dst, size_t dstlen) {
163  if(st && st->buf) {
164  ssize_t ret = UTF8String__process(st, dst, dstlen);
165  return (ret < 0) ? 0 : ret;
166  } else {
167  return 0;
168  }
169 }
170 
171 int
172 UTF8String_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
173  asn_app_consume_bytes_f *cb, void *app_key) {
174  const UTF8String_t *st = (const UTF8String_t *)sptr;
175 
176  (void)td; /* Unused argument */
177  (void)ilevel; /* Unused argument */
178 
179  if(st && st->buf) {
180  return (cb(st->buf, st->size, app_key) < 0) ? -1 : 0;
181  } else {
182  return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
183  }
184 }
asn_TYPE_descriptor_t asn_DEF_UTF8String
Definition: UTF8String.c:16
static int32_t UTF8String_mv[7]
Definition: UTF8String.c:55
per_type_decoder_f OCTET_STRING_decode_uper
Definition: OCTET_STRING.h:33
per_type_encoder_f OCTET_STRING_encode_uper
Definition: OCTET_STRING.h:34
#define U8E_EINVAL
Definition: UTF8String.c:68
#define U8E_ILLSTART
Definition: UTF8String.c:65
xer_type_decoder_f OCTET_STRING_decode_xer_utf8
Definition: OCTET_STRING.h:30
#define U8E_TRUNC
Definition: UTF8String.c:64
#define U8E_NOTMIN
Definition: UTF8String.c:67
static int UTF8String_ht[2][16]
Definition: UTF8String.c:44
size_t UTF8String_to_wcs(const UTF8String_t *st, uint32_t *dst, size_t dstlen)
Definition: UTF8String.c:162
asn_struct_free_f OCTET_STRING_free
Definition: OCTET_STRING.h:23
static ber_tlv_tag_t asn_DEF_UTF8String_tags[]
Definition: UTF8String.c:12
ssize_t UTF8String_length(const UTF8String_t *st)
Definition: UTF8String.c:153
#define U8E_NOTCONT
Definition: UTF8String.c:66
int UTF8String_constraint(asn_TYPE_descriptor_t *td, const void *sptr, asn_app_constraint_failed_f *ctfailcb, void *app_key)
Definition: UTF8String.c:71
int UTF8String_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel, asn_app_consume_bytes_f *cb, void *app_key)
Definition: UTF8String.c:172
int() asn_app_consume_bytes_f(const void *buffer, size_t size, void *application_specific_key)
void() asn_app_constraint_failed_f(void *application_specific_key, struct asn_TYPE_descriptor_s *type_descriptor_which_failed, const void *structure_which_failed_ptr, const char *error_message_format,...) GCC_PRINTFLIKE(4
ber_type_decoder_f OCTET_STRING_decode_ber
Definition: OCTET_STRING.h:26
xer_type_encoder_f OCTET_STRING_encode_xer_utf8
Definition: OCTET_STRING.h:32
#define _ASN_CTFAIL
Definition: constraints.h:57
uint8_t * buf
Definition: OCTET_STRING.h:15
der_type_encoder_f OCTET_STRING_encode_der
Definition: OCTET_STRING.h:27
static ssize_t UTF8String__process(const UTF8String_t *st, uint32_t *dst, size_t dstlen)
Definition: UTF8String.c:104
unsigned ber_tlv_tag_t
Definition: ber_tlv_tag.h:18