aboutsummaryrefslogtreecommitdiffstats
path: root/skeletons/BIT_STRING.c
blob: a43a22aa5def53d8bc5f6ead2564ab88bd57d841 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*-
 * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
 * Redistribution and modifications are permitted subject to BSD license.
 */
#include <BIT_STRING.h>

/*
 * BIT STRING basic type description.
 */
static ber_tlv_tag_t asn1_DEF_BIT_STRING_tags[] = {
	(ASN_TAG_CLASS_UNIVERSAL | (3 << 2))
};
asn1_TYPE_descriptor_t asn1_DEF_BIT_STRING = {
	"BIT STRING",
	BIT_STRING_constraint,
	OCTET_STRING_decode_ber,   /* Implemented in terms of OCTET STRING */
	OCTET_STRING_encode_der,   /* Implemented in terms of OCTET STRING */
	BIT_STRING_print,
	OCTET_STRING_free,         /* Implemented in terms of OCTET STRING */
	0, /* Use generic outmost tag fetcher */
	asn1_DEF_BIT_STRING_tags,
	sizeof(asn1_DEF_BIT_STRING_tags)
	  / sizeof(asn1_DEF_BIT_STRING_tags[0]),
	asn1_DEF_BIT_STRING_tags,	/* Same as above */
	sizeof(asn1_DEF_BIT_STRING_tags)
	  / sizeof(asn1_DEF_BIT_STRING_tags[0]),
	-1,	/* Both ways are fine */
	0, 0,	/* No members */
	(void *)-1	/* Special indicator that this is a BIT STRING */
};

/*
 * BIT STRING generic constraint.
 */
int
BIT_STRING_constraint(asn1_TYPE_descriptor_t *td, const void *sptr,
		asn_app_consume_bytes_f *app_errlog, void *app_key) {
	const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;

	if(st && st->buf) {
		if(st->size) {
			if(st->size == 1 && st->buf[0] != 0) {
				_ASN_ERRLOG(app_errlog, app_key,
					"%s: invalid padding byte (%s:%d)",
					td->name, __FILE__, __LINE__);
				return -1;
			}
		} else {
			_ASN_ERRLOG(app_errlog, app_key,
				"%s: no padding byte (%s:%d)",
				td->name, __FILE__, __LINE__);
			return -1;
		}
	} else {
		_ASN_ERRLOG(app_errlog, app_key,
			"%s: value not given (%s:%d)",
			td->name, __FILE__, __LINE__);
		return -1;
	}

	return 0;
}

/*
 * BIT STRING specific contents printer.
 */
int
BIT_STRING_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
		asn_app_consume_bytes_f *cb, void *app_key) {
	static const char *h2c = "0123456789ABCDEF";
	char scratch[64];
	const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
	uint8_t *buf;
	uint8_t *end;
	char *p = scratch;

	(void)td;	/* Unused argument */

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

	ilevel += 4;
	buf = st->buf;
	end = buf + st->size;

	/*
	 * Hexadecimal dump.
	 */
	for(buf++; buf < end; buf++) {
		if(((buf - st->buf) - 1) % 16 == 0 && (st->size > 16)) {
			int i;
			/* Indentation */
			if(cb("\n", 1, app_key)) return -1;
			for(i = 0; i < ilevel; i++) cb(" ", 1, app_key);
			/* Dump the string */
			if(cb(scratch, p - scratch, app_key)) return -1;
			p = scratch;
		}
		*p++ = h2c[*buf >> 4];
		*p++ = h2c[*buf & 0x0F];
		*p++ = 0x20;
	}

	/* Dump the incomplete 16-bytes row */
	return cb(scratch, p - scratch, app_key);
}