14
0
Fork 0
mirror of https://gerrit.osmocom.org/asn1c synced 2024-08-19 11:56:40 +00:00
asn1c/skeletons/UTF8String.c

118 lines
3.2 KiB
C
Raw Normal View History

2004-06-03 03:38:44 +00:00
/*-
* Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
* 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-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 */
OCTET_STRING_encode_der, /* Implemented in terms of OCTET STRING */
2004-09-22 16:06:28 +00:00
0, /* Not implemented yet */
OCTET_STRING_encode_xer_ascii, /* Already in UTF-8 format */
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]),
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.
* The second half of this table is only applicable to the long sequentes.
*/
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
};
int
2004-09-29 13:26:15 +00:00
UTF8String_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
2004-06-03 03:38:44 +00:00
asn_app_consume_bytes_f *app_errlog, void *app_key) {
ssize_t len;
2004-08-11 09:07:36 +00:00
len = UTF8String_length((const UTF8String_t *)sptr, td->name,
app_errlog, app_key);
2004-06-03 03:38:44 +00:00
if(len > 0) len = 0;
return len;
}
ssize_t
UTF8String_length(const UTF8String_t *st, const char *opt_type_name,
asn_app_consume_bytes_f *app_errlog, void *app_key) {
if(st && st->buf) {
2004-10-02 12:29:44 +00:00
size_t length;
2004-06-03 03:38:44 +00:00
uint8_t *buf = st->buf;
uint8_t *end = buf + st->size;
2004-10-02 12:29:44 +00:00
for(length = 0; buf < end; length++) {
int ch = *buf;
int 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; /* Fine value */
case 0: /* 10xxxxxx should not appear here */
_ASN_ERRLOG(app_errlog, app_key,
"%s: UTF-8 expectation failed "
"at byte %d (%s:%d)",
opt_type_name,
(buf - st->buf) + 1,
__FILE__, __LINE__);
return -1;
2004-06-03 03:38:44 +00:00
}
2004-10-02 12:29:44 +00:00
if(buf + want > end) {
_ASN_ERRLOG(app_errlog, app_key,
"%s: truncated UTF-8 sequence (%s:%d)",
opt_type_name, __FILE__, __LINE__);
return -1;
}
buf += want;
2004-06-03 03:38:44 +00:00
}
return length;
} else {
2004-08-11 09:44:13 +00:00
_ASN_ERRLOG(app_errlog, app_key,
"%s: value not given", opt_type_name);
2004-06-03 03:38:44 +00:00
return -1;
}
}
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
}
}