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  */
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 };
18 #if !defined(ASN_DISABLE_PRINT_SUPPORT)
20 #else
21  0,
22 #endif /* !defined(ASN_DISABLE_PRINT_SUPPORT) */
25 #if !defined(ASN_DISABLE_BER_SUPPORT)
26  OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
28 #else
29  0,
30  0,
31 #endif /* !defined(ASN_DISABLE_BER_SUPPORT) */
32 #if !defined(ASN_DISABLE_XER_SUPPORT)
35 #else
36  0,
37  0,
38 #endif /* !defined(ASN_DISABLE_XER_SUPPORT) */
39 #if !defined(ASN_DISABLE_JER_SUPPORT)
42 #else
43  0,
44  0,
45 #endif /* !defined(ASN_DISABLE_JER_SUPPORT) */
46 #if !defined(ASN_DISABLE_OER_SUPPORT)
49 #else
50  0,
51  0,
52 #endif /* !defined(ASN_DISABLE_OER_SUPPORT) */
53 #if !defined(ASN_DISABLE_UPER_SUPPORT)
56 #else
57  0,
58  0,
59 #endif /* !defined(ASN_DISABLE_UPER_SUPPORT) */
60 #if !defined(ASN_DISABLE_APER_SUPPORT)
63 #else
64  0,
65  0,
66 #endif /* !defined(ASN_DISABLE_APER_SUPPORT) */
67 #if !defined(ASN_DISABLE_RFILL_SUPPORT)
69 #else
70  0,
71 #endif /* !defined(ASN_DISABLE_RFILL_SUPPORT) */
72  0 /* Use generic outmost tag fetcher */
73 };
75  "UTF8String",
76  "UTF8String",
80  / sizeof(asn_DEF_UTF8String_tags[0]) - 1,
83  / sizeof(asn_DEF_UTF8String_tags[0]),
84  {
85 #if !defined(ASN_DISABLE_OER_SUPPORT)
86  0,
87 #endif /* !defined(ASN_DISABLE_OER_SUPPORT) */
88 #if !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT)
89  0,
90 #endif /* !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT) */
91 #if !defined(ASN_DISABLE_JER_SUPPORT)
92  0,
93 #endif /* !defined(ASN_DISABLE_JER_SUPPORT) */
95  },
96  0, 0, /* No members */
97  0 /* No specifics */
98 };
99 
100 /*
101  * This is the table of length expectations.
102  * The second half of this table is only applicable to the long sequences.
103  */
104 static const int UTF8String_ht[2][16] = {
105  { /* 0x0 ... 0x7 */
106  /* 0000..0111 */
107  1, 1, 1, 1, 1, 1, 1, 1,
108  /* 1000..1011(0), 1100..1101(2), 1110(3), 1111(-1) */
109  0, 0, 0, 0, 2, 2, 3, -1 },
110  { /* 0xF0 .. 0xF7 */
111  /* 11110000..11110111 */
112  4, 4, 4, 4, 4, 4, 4, 4,
113  5, 5, 5, 5, 6, 6, -1, -1 }
114 };
115 static const int32_t UTF8String_mv[7] = { 0, 0,
116  0x00000080,
117  0x00000800,
118  0x00010000,
119  0x00200000,
120  0x04000000
121 };
122 
123 /* Internal aliases for return codes */
124 #define U8E_TRUNC -1 /* UTF-8 sequence truncated */
125 #define U8E_ILLSTART -2 /* Illegal UTF-8 sequence start */
126 #define U8E_NOTCONT -3 /* Continuation expectation failed */
127 #define U8E_NOTMIN -4 /* Not minimal length encoding */
128 #define U8E_EINVAL -5 /* Invalid arguments */
129 
130 int
131 UTF8String_constraint(const asn_TYPE_descriptor_t *td, const void *sptr,
132  asn_app_constraint_failed_f *ctfailcb, void *app_key) {
133  ssize_t len = UTF8String_length((const UTF8String_t *)sptr);
134  switch(len) {
135  case U8E_EINVAL:
136  ASN__CTFAIL(app_key, td, sptr,
137  "%s: value not given", td->name);
138  break;
139  case U8E_TRUNC:
140  ASN__CTFAIL(app_key, td, sptr,
141  "%s: truncated UTF-8 sequence (%s:%d)",
142  td->name, __FILE__, __LINE__);
143  break;
144  case U8E_ILLSTART:
145  ASN__CTFAIL(app_key, td, sptr,
146  "%s: UTF-8 illegal start of encoding (%s:%d)",
147  td->name, __FILE__, __LINE__);
148  break;
149  case U8E_NOTCONT:
150  ASN__CTFAIL(app_key, td, sptr,
151  "%s: UTF-8 not continuation (%s:%d)",
152  td->name, __FILE__, __LINE__);
153  break;
154  case U8E_NOTMIN:
155  ASN__CTFAIL(app_key, td, sptr,
156  "%s: UTF-8 not minimal sequence (%s:%d)",
157  td->name, __FILE__, __LINE__);
158  break;
159  }
160  return (len < 0) ? -1 : 0;
161 }
162 
163 static ssize_t
164 UTF8String__process(const UTF8String_t *st, uint32_t *dst, size_t dstlen) {
165  size_t length = 0;
166  uint8_t *buf = (st == NULL)? NULL : st->buf;
167  uint8_t *end = (buf == NULL)? NULL : buf + st->size;
168  uint32_t *dstend = (dst == NULL)? NULL : dst + dstlen;
169 
170  for(length = 0; buf < end; length++) {
171  int ch = *buf;
172  uint8_t *cend;
173  int32_t value;
174  int want;
175 
176  /* Compute the sequence length */
177  want = UTF8String_ht[0][ch >> 4];
178  switch(want) {
179  case -1:
180  /* Second half of the table, long sequence */
181  want = UTF8String_ht[1][ch & 0x0F];
182  if(want != -1) break;
183  /* Fall through */
184  case 0:
185  return U8E_ILLSTART;
186  }
187 
188  /* assert(want >= 1 && want <= 6) */
189 
190  /* Check character sequence length */
191  if(buf + want > end) return U8E_TRUNC;
192 
193  value = ch & (0xff >> want);
194  cend = buf + want;
195  for(buf++; buf < cend; buf++) {
196  ch = *buf;
197  if(ch < 0x80 || ch > 0xbf) return U8E_NOTCONT;
198  value = (value << 6) | (ch & 0x3F);
199  }
200  if(value < UTF8String_mv[want])
201  return U8E_NOTMIN;
202  if(dst < dstend)
203  *dst++ = value; /* Record value */
204  }
205 
206  if(dst < dstend) *dst = 0; /* zero-terminate */
207 
208  return length;
209 }
210 
211 
212 ssize_t
214  if(st && st->buf) {
215  return UTF8String__process(st, 0, 0);
216  } else {
217  return U8E_EINVAL;
218  }
219 }
220 
221 size_t
222 UTF8String_to_wcs(const UTF8String_t *st, uint32_t *dst, size_t dstlen) {
223  if(st && st->buf) {
224  ssize_t ret = UTF8String__process(st, dst, dstlen);
225  return (ret < 0) ? 0 : ret;
226  } else {
227  return 0;
228  }
229 }
230 
asn_DEF_UTF8String_tags
static const ber_tlv_tag_t asn_DEF_UTF8String_tags[]
Definition: UTF8String.c:12
UTF8String_ht
static const int UTF8String_ht[2][16]
Definition: UTF8String.c:104
asn_TYPE_operation_s
Definition: constr_TYPE.h:184
OCTET_STRING_free
asn_struct_free_f OCTET_STRING_free
Definition: OCTET_STRING.h:24
asn_app_constraint_failed_f
void() asn_app_constraint_failed_f(void *application_specific_key, const struct asn_TYPE_descriptor_s *type_descriptor_which_failed, const void *structure_which_failed_ptr, const char *error_message_format,...) CC_PRINTFLIKE(4
Definition: asn_application.h:167
ber_tlv_tag_t
unsigned ber_tlv_tag_t
Definition: ber_tlv_tag.h:18
OCTET_STRING_encode_der
der_type_encoder_f OCTET_STRING_encode_der
Definition: OCTET_STRING.h:38
U8E_NOTCONT
#define U8E_NOTCONT
Definition: UTF8String.c:126
UTF8String_mv
static const int32_t UTF8String_mv[7]
Definition: UTF8String.c:115
ASN_TAG_CLASS_UNIVERSAL
@ ASN_TAG_CLASS_UNIVERSAL
Definition: ber_tlv_tag.h:13
asn_TYPE_descriptor_s::name
const char * name
Definition: constr_TYPE.h:225
UTF8String_length
ssize_t UTF8String_length(const UTF8String_t *st)
Definition: UTF8String.c:213
asn_DEF_UTF8String
asn_TYPE_descriptor_t asn_DEF_UTF8String
Definition: UTF8String.c:74
OCTET_STRING_decode_oer
oer_type_decoder_f OCTET_STRING_decode_oer
Definition: OCTET_STRING.h:57
OCTET_STRING::buf
uint8_t * buf
Definition: OCTET_STRING.h:15
OCTET_STRING_encode_aper
per_type_encoder_f OCTET_STRING_encode_aper
Definition: OCTET_STRING.h:67
UTF8String.h
UTF8String_print
asn_struct_print_f UTF8String_print
Definition: UTF8String.h:22
asn_TYPE_descriptor_s
Definition: constr_TYPE.h:224
OCTET_STRING_decode_aper
per_type_decoder_f OCTET_STRING_decode_aper
Definition: OCTET_STRING.h:66
U8E_NOTMIN
#define U8E_NOTMIN
Definition: UTF8String.c:127
U8E_EINVAL
#define U8E_EINVAL
Definition: UTF8String.c:128
OCTET_STRING_encode_xer_utf8
xer_type_encoder_f OCTET_STRING_encode_xer_utf8
Definition: OCTET_STRING.h:46
UTF8String_random_fill
asn_random_fill_f UTF8String_random_fill
Definition: UTF8String.h:55
UTF8String_to_wcs
size_t UTF8String_to_wcs(const UTF8String_t *st, uint32_t *dst, size_t dstlen)
Definition: UTF8String.c:222
UTF8String__process
static ssize_t UTF8String__process(const UTF8String_t *st, uint32_t *dst, size_t dstlen)
Definition: UTF8String.c:164
OCTET_STRING
Definition: OCTET_STRING.h:14
OCTET_STRING_encode_oer
oer_type_encoder_f OCTET_STRING_encode_oer
Definition: OCTET_STRING.h:58
asn_internal.h
ASN__CTFAIL
#define ASN__CTFAIL
Definition: constraints.h:57
OCTET_STRING_decode_ber
ber_type_decoder_f OCTET_STRING_decode_ber
Definition: OCTET_STRING.h:37
OCTET_STRING_decode_uper
per_type_decoder_f OCTET_STRING_decode_uper
Definition: OCTET_STRING.h:62
OCTET_STRING_copy
asn_struct_copy_f OCTET_STRING_copy
Definition: OCTET_STRING.h:32
OCTET_STRING_decode_xer_utf8
xer_type_decoder_f OCTET_STRING_decode_xer_utf8
Definition: OCTET_STRING.h:44
OCTET_STRING_decode_jer_utf8
jer_type_decoder_f OCTET_STRING_decode_jer_utf8
Definition: OCTET_STRING.h:51
U8E_TRUNC
#define U8E_TRUNC
Definition: UTF8String.c:124
OCTET_STRING_encode_uper
per_type_encoder_f OCTET_STRING_encode_uper
Definition: OCTET_STRING.h:63
OCTET_STRING_encode_jer_utf8
jer_type_encoder_f OCTET_STRING_encode_jer_utf8
Definition: OCTET_STRING.h:53
UTF8String_constraint
int UTF8String_constraint(const asn_TYPE_descriptor_t *td, const void *sptr, asn_app_constraint_failed_f *ctfailcb, void *app_key)
Definition: UTF8String.c:131
U8E_ILLSTART
#define U8E_ILLSTART
Definition: UTF8String.c:125
OCTET_STRING_compare
asn_struct_compare_f OCTET_STRING_compare
Definition: OCTET_STRING.h:31
OCTET_STRING::size
size_t size
Definition: OCTET_STRING.h:16
asn_OP_UTF8String
asn_TYPE_operation_t asn_OP_UTF8String
Definition: UTF8String.c:16


etsi_its_spatem_ts_coding
Author(s): Jean-Pierre Busch , Guido Küppers , Lennart Reiher
autogenerated on Sun May 18 2025 02:29:29