aboutsummaryrefslogtreecommitdiffstats
path: root/skeletons/UniversalString.c
blob: 149889665fe627ed9422a88a66a1e8b1ade2c38d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*-
 * Copyright (c) 2003 Lev Walkin <vlm@lionet.info>. All rights reserved.
 * Redistribution and modifications are permitted subject to BSD license.
 */
#include <UniversalString.h>

/*
 * UniversalString basic type description.
 */
static ber_tlv_tag_t asn1_DEF_UniversalString_tags[] = {
	(ASN_TAG_CLASS_UNIVERSAL | (28 << 2)),	/* [UNIVERSAL 28] IMPLICIT ...*/
	(ASN_TAG_CLASS_UNIVERSAL | (4 << 2))	/* ... OCTET STRING */
};
asn1_TYPE_descriptor_t asn1_DEF_UniversalString = {
	"UniversalString",
	asn_generic_no_constraint,
	OCTET_STRING_decode_ber,    /* Implemented in terms of OCTET STRING */
	OCTET_STRING_encode_der,    /* Implemented in terms of OCTET STRING */
	UniversalString_print,      /* Convert into UTF8 and print */
	OCTET_STRING_free,
	0, /* Use generic outmost tag fetcher */
	asn1_DEF_UniversalString_tags,
	sizeof(asn1_DEF_UniversalString_tags)
	  / sizeof(asn1_DEF_UniversalString_tags[0]) - 1,
	asn1_DEF_UniversalString_tags,
	sizeof(asn1_DEF_UniversalString_tags)
	  / sizeof(asn1_DEF_UniversalString_tags[0]),
	-1,	/* Both ways are fine */
	0, 0,	/* No members */
	0	/* No specifics */
};


int
UniversalString_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
	asn_app_consume_bytes_f *cb, void *app_key) {
	const UniversalString_t *st = (const UniversalString_t *)sptr;
	uint32_t *wchar;
	uint32_t *wend;
	char scratch[128];			/* Scratchpad buffer */
	char *p;

	(void)td;	/* Unused argument */
	(void)ilevel;	/* Unused argument */

	if(!st || !st->buf) return cb("<absent>", 8, app_key);

	wchar = (uint32_t *)st->buf;
	wend = (uint32_t *)(st->buf + st->size);
	for(p = scratch; wchar < wend; wchar++) {
		uint32_t wc =    (((uint8_t *)wchar)[0] << 24)
				| (((uint8_t *)wchar)[1] << 16)
				| (((uint8_t *)wchar)[2] << 8)
				|  ((uint8_t *)wchar)[3];	/* 4 bytes */
		if(sizeof(scratch) - (p - scratch) < 6) {
			if(cb(scratch, p - scratch, app_key))
				return -1;
			p = scratch;
		}
		if(wc < 0x80) {
			*p++ = (char)wc;
		} else if(wc < 0x800) {
			*p++ = 0xc0 | ((wc >> 6));
			*p++ = 0x80 | ((wc & 0x3f));
		} else if(wc < 0x10000) {
			*p++ = 0xe0 | ((wc >> 12));
			*p++ = 0x80 | ((wc >> 6) & 0x3f);
			*p++ = 0x80 | ((wc & 0x3f));
		} else if(wc < 0x200000) {
			*p++ = 0xf0 | ((wc >> 18));
			*p++ = 0x80 | ((wc >> 12) & 0x3f);
			*p++ = 0x80 | ((wc >> 6) & 0x3f);
			*p++ = 0x80 | ((wc & 0x3f));
		} else if(wc < 0x4000000) {
			*p++ = 0xf8 | ((wc >> 24));
			*p++ = 0x80 | ((wc >> 18) & 0x3f);
			*p++ = 0x80 | ((wc >> 12) & 0x3f);
			*p++ = 0x80 | ((wc >> 6) & 0x3f);
			*p++ = 0x80 | ((wc & 0x3f));
		} else {
			*p++ = 0xfc | ((wc >> 30) & 0x1);
			*p++ = 0x80 | ((wc >> 24) & 0x3f);
			*p++ = 0x80 | ((wc >> 18) & 0x3f);
			*p++ = 0x80 | ((wc >> 12) & 0x3f);
			*p++ = 0x80 | ((wc >> 6) & 0x3f);
			*p++ = 0x80 | ((wc & 0x3f));
		}
	}

	return cb(scratch, p - scratch, app_key);
}