blob: 7d75e079bb7d80399acd1157972beefb2ea31ae6 [file] [log] [blame]
vlmfa67ddc2004-06-03 03:38:44 +00001/*-
2 * Copyright (c) 2003 Lev Walkin <vlm@lionet.info>. All rights reserved.
3 * Redistribution and modifications are permitted subject to BSD license.
4 */
5#include <UniversalString.h>
6
7/*
8 * UniversalString basic type description.
9 */
10static ber_tlv_tag_t asn1_DEF_UniversalString_tags[] = {
11 (ASN_TAG_CLASS_UNIVERSAL | (28 << 2))
12};
13asn1_TYPE_descriptor_t asn1_DEF_UniversalString = {
14 "UniversalString",
15 asn_generic_no_constraint,
16 OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
17 OCTET_STRING_encode_der, /* Implemented in terms of OCTET STRING */
18 UniversalString_print, /* Convert into UTF8 and print */
19 OCTET_STRING_free,
20 0, /* Use generic outmost tag fetcher */
21 asn1_DEF_UniversalString_tags,
22 sizeof(asn1_DEF_UniversalString_tags)
23 / sizeof(asn1_DEF_UniversalString_tags[0]),
24 1, /* Single UNIVERSAL tag may be implicitly overriden */
25 -1, /* Both ways are fine */
26};
27
28
29int
30UniversalString_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
31 asn_app_consume_bytes_f *cb, void *app_key) {
32 const UniversalString_t *st = sptr;
33 uint32_t *wchar;
34 uint32_t *wend;
35 char scratch[128]; /* Scratchpad buffer */
36 char *p;
37
38 if(!st || !st->buf) return cb("<absent>", 8, app_key);
39
40 wchar = (uint32_t *)st->buf;
41 wend = (uint32_t *)(st->buf + st->size);
42 for(p = scratch; wchar < wend; wchar++) {
43 uint32_t wc = (((uint8_t *)wchar)[0] << 24)
44 | (((uint8_t *)wchar)[1] << 16)
45 | (((uint8_t *)wchar)[2] << 8)
46 | ((uint8_t *)wchar)[3]; /* 4 bytes */
47 if(sizeof(scratch) - (p - scratch) < 6) {
48 if(cb(scratch, p - scratch, app_key))
49 return -1;
50 p = scratch;
51 }
52 if(wc < 0x80) {
53 *p++ = (char)wc;
54 } else if(wc < 0x800) {
55 *p++ = 0xc0 | ((wc >> 6));
56 *p++ = 0x80 | ((wc & 0x3f));
57 } else if(wc < 0x10000) {
58 *p++ = 0xe0 | ((wc >> 12));
59 *p++ = 0x80 | ((wc >> 6) & 0x3f);
60 *p++ = 0x80 | ((wc & 0x3f));
61 } else if(wc < 0x200000) {
62 *p++ = 0xf0 | ((wc >> 18));
63 *p++ = 0x80 | ((wc >> 12) & 0x3f);
64 *p++ = 0x80 | ((wc >> 6) & 0x3f);
65 *p++ = 0x80 | ((wc & 0x3f));
66 } else if(wc < 0x4000000) {
67 *p++ = 0xf8 | ((wc >> 24));
68 *p++ = 0x80 | ((wc >> 18) & 0x3f);
69 *p++ = 0x80 | ((wc >> 12) & 0x3f);
70 *p++ = 0x80 | ((wc >> 6) & 0x3f);
71 *p++ = 0x80 | ((wc & 0x3f));
72 } else {
73 *p++ = 0xfc | ((wc >> 30) & 0x1);
74 *p++ = 0x80 | ((wc >> 24) & 0x3f);
75 *p++ = 0x80 | ((wc >> 18) & 0x3f);
76 *p++ = 0x80 | ((wc >> 12) & 0x3f);
77 *p++ = 0x80 | ((wc >> 6) & 0x3f);
78 *p++ = 0x80 | ((wc & 0x3f));
79 }
80 }
81
82 return cb(scratch, p - scratch, app_key);
83}