blob: fdd13d871e0b94bf34b1c68042ed1d7b02505a4c [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 */
26};
27
28static int _UTF8String_h1[16] = {
29 1, 1, 1, 1, 1, 1, 1, 1, /* 0x0 ... 0x7 */
30 0, 0, 0, 0, 2, 2, 3, -1
31};
32static int _UTF8String_h2[16] = {
33 4, 4, 4, 4, 4, 4, 4, 4, /* 0xF0 .. 0xF7 */
34 5, 5, 5, 5, 6, 6, -1, -1
35};
36
37int
38UTF8String_constraint(asn1_TYPE_descriptor_t *td, const void *sptr,
39 asn_app_consume_bytes_f *app_errlog, void *app_key) {
40 ssize_t len;
41 len = UTF8String_length(sptr, td->name, app_errlog, app_key);
42 if(len > 0) len = 0;
43 return len;
44}
45
46ssize_t
47UTF8String_length(const UTF8String_t *st, const char *opt_type_name,
48 asn_app_consume_bytes_f *app_errlog, void *app_key) {
49
50 if(st && st->buf) {
51 size_t length = 0;
52 uint8_t *buf = st->buf;
53 uint8_t *end = buf + st->size;
54 int want; /* Number of bytes wanted */
55
56 for(want = 0; buf < end; buf++) {
57 uint8_t ch = *buf;
58 int w = _UTF8String_h1[ch >> 4];
59 if(want) { /* Continuation expected */
60 if(w) {
61 _ASN_ERRLOG("%s: UTF-8 expectation "
62 "failed at byte %d",
63 opt_type_name,
64 (buf - st->buf) + 1);
65 return -1;
66 }
67 want--;
68 } else {
69 switch(w) {
70 case -1: /* Long UTF-8 */
71 w = _UTF8String_h2[ch & 0xF0];
72 if(w != -1)
73 break;
74 /* Fall through */
75 case 0:
76 _ASN_ERRLOG(
77 "%s: UTF-8 expectation"
78 "failed at byte %d",
79 opt_type_name,
80 (buf - st->buf) + 1);
81 return -1;
82 }
83 want = w - 1; /* Expect this much */
84 }
85 if(!want) length++;
86 }
87
88 /* If still want something, then something is wrong */
89 if(want) {
90 _ASN_ERRLOG("%s: truncated UTF-8 sequence",
91 opt_type_name);
92 return -1;
93 }
94
95 return length;
96 } else {
97 _ASN_ERRLOG("%s: value not given", opt_type_name);
98 return -1;
99 }
100}
101
102int
103UTF8String_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
104 asn_app_consume_bytes_f *cb, void *app_key) {
105 const UTF8String_t *st = sptr;
106
107 if(st && st->buf) {
108 return cb(st->buf, st->size, app_key);
109 } else {
110 return cb("<absent>", 8, app_key);
111 }
112}