blob: bca34732dcb0b1e8ce90d985c82a5fba4509915a [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 <BIT_STRING.h>
6
7/*
8 * BIT STRING basic type description.
9 */
10static ber_tlv_tag_t asn1_DEF_BIT_STRING_tags[] = {
11 (ASN_TAG_CLASS_UNIVERSAL | (3 << 2))
12};
13asn1_TYPE_descriptor_t asn1_DEF_BIT_STRING = {
14 "BIT STRING",
15 BIT_STRING_constraint,
16 OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
17 OCTET_STRING_encode_der, /* Implemented in terms of OCTET STRING */
18 BIT_STRING_print,
19 OCTET_STRING_free, /* Implemented in terms of OCTET STRING */
20 0, /* Use generic outmost tag fetcher */
21 asn1_DEF_BIT_STRING_tags,
22 sizeof(asn1_DEF_BIT_STRING_tags)
23 / sizeof(asn1_DEF_BIT_STRING_tags[0]),
24 1, /* Single UNIVERSAL tag may be implicitly overriden */
25 -1, /* Both ways are fine */
Lev Walkin449f8322004-08-20 13:23:42 +000026 0, 0, /* No members */
Lev Walkinf15320b2004-06-03 03:38:44 +000027 (void *)-1 /* Special indicator that this is a BIT STRING */
28};
29
30/*
31 * BIT STRING generic constraint.
32 */
33int
34BIT_STRING_constraint(asn1_TYPE_descriptor_t *td, const void *sptr,
35 asn_app_consume_bytes_f *app_errlog, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +000036 const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +000037
38 if(st && st->buf) {
39 if(st->size) {
40 if(st->size == 1 && st->buf[0] != 0) {
Lev Walkinba4e5182004-08-11 09:44:13 +000041 _ASN_ERRLOG(app_errlog, app_key,
Lev Walkin16835b62004-08-22 13:47:59 +000042 "%s: invalid padding byte (%s:%d)",
43 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +000044 return -1;
45 }
46 } else {
Lev Walkinba4e5182004-08-11 09:44:13 +000047 _ASN_ERRLOG(app_errlog, app_key,
Lev Walkin16835b62004-08-22 13:47:59 +000048 "%s: no padding byte (%s:%d)",
49 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +000050 return -1;
51 }
52 } else {
Lev Walkinba4e5182004-08-11 09:44:13 +000053 _ASN_ERRLOG(app_errlog, app_key,
Lev Walkin16835b62004-08-22 13:47:59 +000054 "%s: value not given (%s:%d)",
55 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +000056 return -1;
57 }
58
59 return 0;
60}
61
62/*
63 * BIT STRING specific contents printer.
64 */
65int
66BIT_STRING_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
67 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin4d9528c2004-08-11 08:10:13 +000068 static const char *h2c = "0123456789ABCDEF";
Lev Walkinf15320b2004-06-03 03:38:44 +000069 char scratch[64];
Lev Walkinc2346572004-08-11 09:07:36 +000070 const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +000071 uint8_t *buf;
72 uint8_t *end;
73 char *p = scratch;
74
Lev Walkind9bd7752004-06-05 08:17:50 +000075 (void)td; /* Unused argument */
76
Lev Walkinf15320b2004-06-03 03:38:44 +000077 if(!st || !st->buf) return cb("<absent>", 8, app_key);
78
79 ilevel += 4;
80 buf = st->buf;
81 end = buf + st->size;
82
83 /*
84 * Hexadecimal dump.
85 */
86 for(buf++; buf < end; buf++) {
Lev Walkin4a660bd2004-07-22 16:23:33 +000087 if(((buf - st->buf) - 1) % 16 == 0 && (st->size > 16)) {
Lev Walkinf15320b2004-06-03 03:38:44 +000088 int i;
89 /* Indentation */
90 if(cb("\n", 1, app_key)) return -1;
91 for(i = 0; i < ilevel; i++) cb(" ", 1, app_key);
92 /* Dump the string */
93 if(cb(scratch, p - scratch, app_key)) return -1;
94 p = scratch;
95 }
96 *p++ = h2c[*buf >> 4];
97 *p++ = h2c[*buf & 0x0F];
98 *p++ = 0x20;
99 }
100
101 /* Dump the incomplete 16-bytes row */
102 return cb(scratch, p - scratch, app_key);
103}
104