asn1c/skeletons/UTF8String.c

185 lines
4.7 KiB
C
Raw Normal View History

2004-06-03 03:38:44 +00:00
/*-
2006-09-17 01:27:44 +00:00
* Copyright (c) 2003, 2004, 2006 Lev Walkin <vlm@lionet.info>.
* All rights reserved.
2004-06-03 03:38:44 +00:00
* Redistribution and modifications are permitted subject to BSD license.
*/
2004-09-22 16:06:28 +00:00
#include <asn_internal.h>
2004-06-03 03:38:44 +00:00
#include <UTF8String.h>
/*
* UTF8String basic type description.
*/
2004-09-29 13:26:15 +00:00
static ber_tlv_tag_t asn_DEF_UTF8String_tags[] = {
(ASN_TAG_CLASS_UNIVERSAL | (12 << 2)), /* [UNIVERSAL 12] IMPLICIT ...*/
(ASN_TAG_CLASS_UNIVERSAL | (4 << 2)), /* ... OCTET STRING */
2004-06-03 03:38:44 +00:00
};
2004-09-29 13:26:15 +00:00
asn_TYPE_descriptor_t asn_DEF_UTF8String = {
2004-10-20 15:50:55 +00:00
"UTF8String",
2004-06-03 03:38:44 +00:00
"UTF8String",
2004-09-22 16:06:28 +00:00
OCTET_STRING_free,
UTF8String_print,
2004-06-03 03:38:44 +00:00
UTF8String_constraint, /* Check for invalid codes, etc. */
OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
2004-10-20 15:50:55 +00:00
OCTET_STRING_encode_der,
OCTET_STRING_decode_xer_utf8,
OCTET_STRING_encode_xer_utf8,
2006-10-09 12:07:58 +00:00
OCTET_STRING_decode_uper,
OCTET_STRING_encode_uper,
2004-06-03 03:38:44 +00:00
0, /* Use generic outmost tag fetcher */
2004-09-29 13:26:15 +00:00
asn_DEF_UTF8String_tags,
sizeof(asn_DEF_UTF8String_tags)
/ sizeof(asn_DEF_UTF8String_tags[0]) - 1,
asn_DEF_UTF8String_tags,
sizeof(asn_DEF_UTF8String_tags)
/ sizeof(asn_DEF_UTF8String_tags[0]),
2005-11-26 11:25:14 +00:00
0, /* No PER visible constraints */
2004-08-20 13:23:42 +00:00
0, 0, /* No members */
2004-06-05 08:17:50 +00:00
0 /* No specifics */
2004-06-03 03:38:44 +00:00
};
/*
* This is the table of length expectations.
2005-03-02 13:30:25 +00:00
* The second half of this table is only applicable to the long sequences.
*/
static int UTF8String_ht[2][16] = {
{ /* 0x0 ... 0x7 */
/* 0000..0111 */
1, 1, 1, 1, 1, 1, 1, 1,
/* 1000..1011(0), 1100..1101(2), 1110(3), 1111(-1) */
0, 0, 0, 0, 2, 2, 3, -1 },
{ /* 0xF0 .. 0xF7 */
/* 11110000..11110111 */
4, 4, 4, 4, 4, 4, 4, 4,
5, 5, 5, 5, 6, 6, -1, -1 }
2004-06-03 03:38:44 +00:00
};
2004-10-02 15:55:07 +00:00
static int32_t UTF8String_mv[7] = { 0, 0,
0x00000080,
0x00000800,
0x00010000,
0x00200000,
0x04000000
};
/* Internal aliases for return codes */
#define U8E_TRUNC -1 /* UTF-8 sequence truncated */
#define U8E_ILLSTART -2 /* Illegal UTF-8 sequence start */
#define U8E_NOTCONT -3 /* Continuation expectation failed */
#define U8E_NOTMIN -4 /* Not minimal length encoding */
#define U8E_EINVAL -5 /* Invalid arguments */
2004-06-03 03:38:44 +00:00
int
2004-09-29 13:26:15 +00:00
UTF8String_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
2006-07-13 11:19:01 +00:00
asn_app_constraint_failed_f *ctfailcb, void *app_key) {
2004-10-02 15:55:07 +00:00
ssize_t len = UTF8String_length((const UTF8String_t *)sptr);
switch(len) {
case U8E_EINVAL:
2006-10-16 12:18:41 +00:00
_ASN_CTFAIL(app_key, td, sptr,
2004-10-02 15:55:07 +00:00
"%s: value not given", td->name);
break;
case U8E_TRUNC:
2006-10-16 12:18:41 +00:00
_ASN_CTFAIL(app_key, td, sptr,
2004-10-02 15:55:07 +00:00
"%s: truncated UTF-8 sequence (%s:%d)",
td->name, __FILE__, __LINE__);
break;
case U8E_ILLSTART:
2006-10-16 12:18:41 +00:00
_ASN_CTFAIL(app_key, td, sptr,
2004-10-02 15:55:07 +00:00
"%s: UTF-8 illegal start of encoding (%s:%d)",
td->name, __FILE__, __LINE__);
break;
case U8E_NOTCONT:
2006-10-16 12:18:41 +00:00
_ASN_CTFAIL(app_key, td, sptr,
2004-10-02 15:55:07 +00:00
"%s: UTF-8 not continuation (%s:%d)",
td->name, __FILE__, __LINE__);
break;
case U8E_NOTMIN:
2006-10-16 12:18:41 +00:00
_ASN_CTFAIL(app_key, td, sptr,
2004-10-02 15:55:07 +00:00
"%s: UTF-8 not minimal sequence (%s:%d)",
td->name, __FILE__, __LINE__);
break;
}
return (len < 0) ? -1 : 0;
2004-06-03 03:38:44 +00:00
}
2004-10-23 11:20:06 +00:00
static ssize_t
UTF8String__process(const UTF8String_t *st, uint32_t *dst, size_t dstlen) {
size_t length;
uint8_t *buf = st->buf;
uint8_t *end = buf + st->size;
2006-09-17 01:27:44 +00:00
uint32_t *dstend = dst + dstlen;
2004-06-03 03:38:44 +00:00
2004-10-23 11:20:06 +00:00
for(length = 0; buf < end; length++) {
int ch = *buf;
uint8_t *cend;
int32_t value;
int want;
/* Compute the sequence length */
want = UTF8String_ht[0][ch >> 4];
switch(want) {
case -1:
/* Second half of the table, long sequence */
want = UTF8String_ht[1][ch & 0x0F];
if(want != -1) break;
/* Fall through */
case 0:
return U8E_ILLSTART;
2004-06-03 03:38:44 +00:00
}
2004-10-23 11:20:06 +00:00
/* assert(want >= 1 && want <= 6) */
/* Check character sequence length */
if(buf + want > end) return U8E_TRUNC;
2006-09-17 01:27:44 +00:00
value = ch & (0xff >> want);
2004-10-23 11:20:06 +00:00
cend = buf + want;
for(buf++; buf < cend; buf++) {
ch = *buf;
if(ch < 0x80 || ch > 0xbf) return U8E_NOTCONT;
value = (value << 6) | (ch & 0x3F);
}
if(value < UTF8String_mv[want])
return U8E_NOTMIN;
2006-09-17 01:27:44 +00:00
if(dst < dstend)
*dst++ = value; /* Record value */
2004-10-23 11:20:06 +00:00
}
2006-09-17 01:27:44 +00:00
if(dst < dstend) *dst = 0; /* zero-terminate */
2004-10-23 11:20:06 +00:00
return length;
}
ssize_t
UTF8String_length(const UTF8String_t *st) {
if(st && st->buf) {
return UTF8String__process(st, 0, 0);
2004-06-03 03:38:44 +00:00
} else {
2004-10-02 15:55:07 +00:00
return U8E_EINVAL;
2004-06-03 03:38:44 +00:00
}
}
2004-10-23 11:20:06 +00:00
size_t
UTF8String_to_wcs(const UTF8String_t *st, uint32_t *dst, size_t dstlen) {
if(st && st->buf) {
ssize_t ret = UTF8String__process(st, dst, dstlen);
return (ret < 0) ? 0 : ret;
} else {
return 0;
}
}
2004-06-03 03:38:44 +00:00
int
2004-09-29 13:26:15 +00:00
UTF8String_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
2004-06-03 03:38:44 +00:00
asn_app_consume_bytes_f *cb, void *app_key) {
2004-08-11 09:07:36 +00:00
const UTF8String_t *st = (const UTF8String_t *)sptr;
2004-06-03 03:38:44 +00:00
2004-06-05 08:17:50 +00:00
(void)td; /* Unused argument */
(void)ilevel; /* Unused argument */
2004-06-03 03:38:44 +00:00
if(st && st->buf) {
return (cb(st->buf, st->size, app_key) < 0) ? -1 : 0;
2004-06-03 03:38:44 +00:00
} else {
return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
2004-06-03 03:38:44 +00:00
}
}