blob: 06e1559e8b6a9568059a941415fe11758d7e9cb7 [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 */
5#include <UTF8String.h>
6
7/*
8 * UTF8String basic type description.
9 */
10static ber_tlv_tag_t asn1_DEF_UTF8String_tags[] = {
11 (ASN_TAG_CLASS_UNIVERSAL | (12 << 2))
12};
13asn1_TYPE_descriptor_t asn1_DEF_UTF8String = {
14 "UTF8String",
15 UTF8String_constraint, /* Check for invalid codes, etc. */
16 OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
17 OCTET_STRING_encode_der, /* Implemented in terms of OCTET STRING */
18 UTF8String_print,
19 OCTET_STRING_free,
20 0, /* Use generic outmost tag fetcher */
21 asn1_DEF_UTF8String_tags,
22 sizeof(asn1_DEF_UTF8String_tags)
23 / sizeof(asn1_DEF_UTF8String_tags[0]),
24 1, /* Single UNIVERSAL tag may be implicitly overriden */
25 -1, /* Both ways are fine */
vlmb42843a2004-06-05 08:17:50 +000026 0 /* No specifics */
vlmfa67ddc2004-06-03 03:38:44 +000027};
28
29static int _UTF8String_h1[16] = {
30 1, 1, 1, 1, 1, 1, 1, 1, /* 0x0 ... 0x7 */
31 0, 0, 0, 0, 2, 2, 3, -1
32};
33static int _UTF8String_h2[16] = {
34 4, 4, 4, 4, 4, 4, 4, 4, /* 0xF0 .. 0xF7 */
35 5, 5, 5, 5, 6, 6, -1, -1
36};
37
38int
39UTF8String_constraint(asn1_TYPE_descriptor_t *td, const void *sptr,
40 asn_app_consume_bytes_f *app_errlog, void *app_key) {
41 ssize_t len;
vlmda674682004-08-11 09:07:36 +000042 len = UTF8String_length((const UTF8String_t *)sptr, td->name,
43 app_errlog, app_key);
vlmfa67ddc2004-06-03 03:38:44 +000044 if(len > 0) len = 0;
45 return len;
46}
47
48ssize_t
49UTF8String_length(const UTF8String_t *st, const char *opt_type_name,
50 asn_app_consume_bytes_f *app_errlog, void *app_key) {
51
52 if(st && st->buf) {
53 size_t length = 0;
54 uint8_t *buf = st->buf;
55 uint8_t *end = buf + st->size;
56 int want; /* Number of bytes wanted */
57
58 for(want = 0; buf < end; buf++) {
59 uint8_t ch = *buf;
60 int w = _UTF8String_h1[ch >> 4];
61 if(want) { /* Continuation expected */
62 if(w) {
vlme3f0f282004-08-11 09:44:13 +000063 _ASN_ERRLOG(app_errlog, app_key,
64 "%s: UTF-8 expectation "
vlmfa67ddc2004-06-03 03:38:44 +000065 "failed at byte %d",
66 opt_type_name,
67 (buf - st->buf) + 1);
68 return -1;
69 }
70 want--;
71 } else {
72 switch(w) {
73 case -1: /* Long UTF-8 */
74 w = _UTF8String_h2[ch & 0xF0];
75 if(w != -1)
76 break;
77 /* Fall through */
78 case 0:
vlme3f0f282004-08-11 09:44:13 +000079 _ASN_ERRLOG(app_errlog, app_key,
vlmfa67ddc2004-06-03 03:38:44 +000080 "%s: UTF-8 expectation"
81 "failed at byte %d",
82 opt_type_name,
83 (buf - st->buf) + 1);
84 return -1;
85 }
86 want = w - 1; /* Expect this much */
87 }
88 if(!want) length++;
89 }
90
91 /* If still want something, then something is wrong */
92 if(want) {
vlme3f0f282004-08-11 09:44:13 +000093 _ASN_ERRLOG(app_errlog, app_key,
94 "%s: truncated UTF-8 sequence",
vlmfa67ddc2004-06-03 03:38:44 +000095 opt_type_name);
96 return -1;
97 }
98
99 return length;
100 } else {
vlme3f0f282004-08-11 09:44:13 +0000101 _ASN_ERRLOG(app_errlog, app_key,
102 "%s: value not given", opt_type_name);
vlmfa67ddc2004-06-03 03:38:44 +0000103 return -1;
104 }
105}
106
107int
108UTF8String_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
109 asn_app_consume_bytes_f *cb, void *app_key) {
vlmda674682004-08-11 09:07:36 +0000110 const UTF8String_t *st = (const UTF8String_t *)sptr;
vlmfa67ddc2004-06-03 03:38:44 +0000111
vlmb42843a2004-06-05 08:17:50 +0000112 (void)td; /* Unused argument */
113 (void)ilevel; /* Unused argument */
114
vlmfa67ddc2004-06-03 03:38:44 +0000115 if(st && st->buf) {
116 return cb(st->buf, st->size, app_key);
117 } else {
118 return cb("<absent>", 8, app_key);
119 }
120}