blob: 149889665fe627ed9422a88a66a1e8b1ade2c38d [file] [log] [blame]
Lev Walkinf15320b2004-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[] = {
Lev Walkin188ed2c2004-09-13 08:31:01 +000011 (ASN_TAG_CLASS_UNIVERSAL | (28 << 2)), /* [UNIVERSAL 28] IMPLICIT ...*/
12 (ASN_TAG_CLASS_UNIVERSAL | (4 << 2)) /* ... OCTET STRING */
Lev Walkinf15320b2004-06-03 03:38:44 +000013};
14asn1_TYPE_descriptor_t asn1_DEF_UniversalString = {
15 "UniversalString",
16 asn_generic_no_constraint,
17 OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
18 OCTET_STRING_encode_der, /* Implemented in terms of OCTET STRING */
19 UniversalString_print, /* Convert into UTF8 and print */
20 OCTET_STRING_free,
21 0, /* Use generic outmost tag fetcher */
22 asn1_DEF_UniversalString_tags,
23 sizeof(asn1_DEF_UniversalString_tags)
Lev Walkin188ed2c2004-09-13 08:31:01 +000024 / sizeof(asn1_DEF_UniversalString_tags[0]) - 1,
25 asn1_DEF_UniversalString_tags,
26 sizeof(asn1_DEF_UniversalString_tags)
Lev Walkinf15320b2004-06-03 03:38:44 +000027 / sizeof(asn1_DEF_UniversalString_tags[0]),
Lev Walkinf15320b2004-06-03 03:38:44 +000028 -1, /* Both ways are fine */
Lev Walkin449f8322004-08-20 13:23:42 +000029 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +000030 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +000031};
32
33
34int
35UniversalString_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
36 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +000037 const UniversalString_t *st = (const UniversalString_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +000038 uint32_t *wchar;
39 uint32_t *wend;
40 char scratch[128]; /* Scratchpad buffer */
41 char *p;
42
Lev Walkind9bd7752004-06-05 08:17:50 +000043 (void)td; /* Unused argument */
44 (void)ilevel; /* Unused argument */
45
Lev Walkinf15320b2004-06-03 03:38:44 +000046 if(!st || !st->buf) return cb("<absent>", 8, app_key);
47
48 wchar = (uint32_t *)st->buf;
49 wend = (uint32_t *)(st->buf + st->size);
50 for(p = scratch; wchar < wend; wchar++) {
51 uint32_t wc = (((uint8_t *)wchar)[0] << 24)
52 | (((uint8_t *)wchar)[1] << 16)
53 | (((uint8_t *)wchar)[2] << 8)
54 | ((uint8_t *)wchar)[3]; /* 4 bytes */
55 if(sizeof(scratch) - (p - scratch) < 6) {
56 if(cb(scratch, p - scratch, app_key))
57 return -1;
58 p = scratch;
59 }
60 if(wc < 0x80) {
61 *p++ = (char)wc;
62 } else if(wc < 0x800) {
63 *p++ = 0xc0 | ((wc >> 6));
64 *p++ = 0x80 | ((wc & 0x3f));
65 } else if(wc < 0x10000) {
66 *p++ = 0xe0 | ((wc >> 12));
67 *p++ = 0x80 | ((wc >> 6) & 0x3f);
68 *p++ = 0x80 | ((wc & 0x3f));
69 } else if(wc < 0x200000) {
70 *p++ = 0xf0 | ((wc >> 18));
71 *p++ = 0x80 | ((wc >> 12) & 0x3f);
72 *p++ = 0x80 | ((wc >> 6) & 0x3f);
73 *p++ = 0x80 | ((wc & 0x3f));
74 } else if(wc < 0x4000000) {
75 *p++ = 0xf8 | ((wc >> 24));
76 *p++ = 0x80 | ((wc >> 18) & 0x3f);
77 *p++ = 0x80 | ((wc >> 12) & 0x3f);
78 *p++ = 0x80 | ((wc >> 6) & 0x3f);
79 *p++ = 0x80 | ((wc & 0x3f));
80 } else {
81 *p++ = 0xfc | ((wc >> 30) & 0x1);
82 *p++ = 0x80 | ((wc >> 24) & 0x3f);
83 *p++ = 0x80 | ((wc >> 18) & 0x3f);
84 *p++ = 0x80 | ((wc >> 12) & 0x3f);
85 *p++ = 0x80 | ((wc >> 6) & 0x3f);
86 *p++ = 0x80 | ((wc & 0x3f));
87 }
88 }
89
90 return cb(scratch, p - scratch, app_key);
91}