blob: 4ec15b82687f3f719bdc984a04a45e11fad30d36 [file] [log] [blame]
Lev Walkinf15320b2004-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 */
Lev Walkind9bd7752004-06-05 08:17:50 +000026 0 /* No specifics */
Lev Walkinf15320b2004-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;
42 len = UTF8String_length(sptr, td->name, app_errlog, app_key);
43 if(len > 0) len = 0;
44 return len;
45}
46
47ssize_t
48UTF8String_length(const UTF8String_t *st, const char *opt_type_name,
49 asn_app_consume_bytes_f *app_errlog, void *app_key) {
50
51 if(st && st->buf) {
52 size_t length = 0;
53 uint8_t *buf = st->buf;
54 uint8_t *end = buf + st->size;
55 int want; /* Number of bytes wanted */
56
57 for(want = 0; buf < end; buf++) {
58 uint8_t ch = *buf;
59 int w = _UTF8String_h1[ch >> 4];
60 if(want) { /* Continuation expected */
61 if(w) {
62 _ASN_ERRLOG("%s: UTF-8 expectation "
63 "failed at byte %d",
64 opt_type_name,
65 (buf - st->buf) + 1);
66 return -1;
67 }
68 want--;
69 } else {
70 switch(w) {
71 case -1: /* Long UTF-8 */
72 w = _UTF8String_h2[ch & 0xF0];
73 if(w != -1)
74 break;
75 /* Fall through */
76 case 0:
77 _ASN_ERRLOG(
78 "%s: UTF-8 expectation"
79 "failed at byte %d",
80 opt_type_name,
81 (buf - st->buf) + 1);
82 return -1;
83 }
84 want = w - 1; /* Expect this much */
85 }
86 if(!want) length++;
87 }
88
89 /* If still want something, then something is wrong */
90 if(want) {
91 _ASN_ERRLOG("%s: truncated UTF-8 sequence",
92 opt_type_name);
93 return -1;
94 }
95
96 return length;
97 } else {
98 _ASN_ERRLOG("%s: value not given", opt_type_name);
99 return -1;
100 }
101}
102
103int
104UTF8String_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
105 asn_app_consume_bytes_f *cb, void *app_key) {
106 const UTF8String_t *st = sptr;
107
Lev Walkind9bd7752004-06-05 08:17:50 +0000108 (void)td; /* Unused argument */
109 (void)ilevel; /* Unused argument */
110
Lev Walkinf15320b2004-06-03 03:38:44 +0000111 if(st && st->buf) {
112 return cb(st->buf, st->size, app_key);
113 } else {
114 return cb("<absent>", 8, app_key);
115 }
116}