blob: 7220a6748aea006d00d84fa88e938d01ebe392e6 [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 */
11static ber_tlv_tag_t asn1_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};
15asn1_TYPE_descriptor_t asn1_DEF_UTF8String = {
16 "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 */
25 asn1_DEF_UTF8String_tags,
26 sizeof(asn1_DEF_UTF8String_tags)
vlm72425de2004-09-13 08:31:01 +000027 / sizeof(asn1_DEF_UTF8String_tags[0]) - 1,
28 asn1_DEF_UTF8String_tags,
29 sizeof(asn1_DEF_UTF8String_tags)
vlmfa67ddc2004-06-03 03:38:44 +000030 / sizeof(asn1_DEF_UTF8String_tags[0]),
vlmfa67ddc2004-06-03 03:38:44 +000031 -1, /* Both ways are fine */
vlme413c122004-08-20 13:23:42 +000032 0, 0, /* No members */
vlmb42843a2004-06-05 08:17:50 +000033 0 /* No specifics */
vlmfa67ddc2004-06-03 03:38:44 +000034};
35
36static int _UTF8String_h1[16] = {
37 1, 1, 1, 1, 1, 1, 1, 1, /* 0x0 ... 0x7 */
38 0, 0, 0, 0, 2, 2, 3, -1
39};
40static int _UTF8String_h2[16] = {
41 4, 4, 4, 4, 4, 4, 4, 4, /* 0xF0 .. 0xF7 */
42 5, 5, 5, 5, 6, 6, -1, -1
43};
44
45int
46UTF8String_constraint(asn1_TYPE_descriptor_t *td, const void *sptr,
47 asn_app_consume_bytes_f *app_errlog, void *app_key) {
48 ssize_t len;
vlmda674682004-08-11 09:07:36 +000049 len = UTF8String_length((const UTF8String_t *)sptr, td->name,
50 app_errlog, app_key);
vlmfa67ddc2004-06-03 03:38:44 +000051 if(len > 0) len = 0;
52 return len;
53}
54
55ssize_t
56UTF8String_length(const UTF8String_t *st, const char *opt_type_name,
57 asn_app_consume_bytes_f *app_errlog, void *app_key) {
58
59 if(st && st->buf) {
60 size_t length = 0;
61 uint8_t *buf = st->buf;
62 uint8_t *end = buf + st->size;
63 int want; /* Number of bytes wanted */
64
65 for(want = 0; buf < end; buf++) {
66 uint8_t ch = *buf;
67 int w = _UTF8String_h1[ch >> 4];
68 if(want) { /* Continuation expected */
69 if(w) {
vlme3f0f282004-08-11 09:44:13 +000070 _ASN_ERRLOG(app_errlog, app_key,
71 "%s: UTF-8 expectation "
vlm758530a2004-08-22 13:47:59 +000072 "failed at byte %d (%s:%d)",
vlmfa67ddc2004-06-03 03:38:44 +000073 opt_type_name,
vlm758530a2004-08-22 13:47:59 +000074 (buf - st->buf) + 1,
75 __FILE__, __LINE__);
vlmfa67ddc2004-06-03 03:38:44 +000076 return -1;
77 }
78 want--;
79 } else {
80 switch(w) {
81 case -1: /* Long UTF-8 */
82 w = _UTF8String_h2[ch & 0xF0];
83 if(w != -1)
84 break;
85 /* Fall through */
86 case 0:
vlme3f0f282004-08-11 09:44:13 +000087 _ASN_ERRLOG(app_errlog, app_key,
vlmfa67ddc2004-06-03 03:38:44 +000088 "%s: UTF-8 expectation"
vlm758530a2004-08-22 13:47:59 +000089 "failed at byte %d (%s:%d)",
vlmfa67ddc2004-06-03 03:38:44 +000090 opt_type_name,
vlm758530a2004-08-22 13:47:59 +000091 (buf - st->buf) + 1,
92 __FILE__, __LINE__);
vlmfa67ddc2004-06-03 03:38:44 +000093 return -1;
94 }
95 want = w - 1; /* Expect this much */
96 }
97 if(!want) length++;
98 }
99
100 /* If still want something, then something is wrong */
101 if(want) {
vlme3f0f282004-08-11 09:44:13 +0000102 _ASN_ERRLOG(app_errlog, app_key,
vlm758530a2004-08-22 13:47:59 +0000103 "%s: truncated UTF-8 sequence (%s:%d)",
104 opt_type_name, __FILE__, __LINE__);
vlmfa67ddc2004-06-03 03:38:44 +0000105 return -1;
106 }
107
108 return length;
109 } else {
vlme3f0f282004-08-11 09:44:13 +0000110 _ASN_ERRLOG(app_errlog, app_key,
111 "%s: value not given", opt_type_name);
vlmfa67ddc2004-06-03 03:38:44 +0000112 return -1;
113 }
114}
115
116int
117UTF8String_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
118 asn_app_consume_bytes_f *cb, void *app_key) {
vlmda674682004-08-11 09:07:36 +0000119 const UTF8String_t *st = (const UTF8String_t *)sptr;
vlmfa67ddc2004-06-03 03:38:44 +0000120
vlmb42843a2004-06-05 08:17:50 +0000121 (void)td; /* Unused argument */
122 (void)ilevel; /* Unused argument */
123
vlmfa67ddc2004-06-03 03:38:44 +0000124 if(st && st->buf) {
125 return cb(st->buf, st->size, app_key);
126 } else {
127 return cb("<absent>", 8, app_key);
128 }
129}