blob: df11c19cf93f00a899982ad88e3ae6ac0213a843 [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,
42 "%s: invalid padding byte",
Lev Walkinf15320b2004-06-03 03:38:44 +000043 td->name);
44 return -1;
45 }
46 } else {
Lev Walkinba4e5182004-08-11 09:44:13 +000047 _ASN_ERRLOG(app_errlog, app_key,
48 "%s: no padding byte", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +000049 return -1;
50 }
51 } else {
Lev Walkinba4e5182004-08-11 09:44:13 +000052 _ASN_ERRLOG(app_errlog, app_key,
53 "%s: value not given", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +000054 return -1;
55 }
56
57 return 0;
58}
59
60/*
61 * BIT STRING specific contents printer.
62 */
63int
64BIT_STRING_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
65 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin4d9528c2004-08-11 08:10:13 +000066 static const char *h2c = "0123456789ABCDEF";
Lev Walkinf15320b2004-06-03 03:38:44 +000067 char scratch[64];
Lev Walkinc2346572004-08-11 09:07:36 +000068 const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +000069 uint8_t *buf;
70 uint8_t *end;
71 char *p = scratch;
72
Lev Walkind9bd7752004-06-05 08:17:50 +000073 (void)td; /* Unused argument */
74
Lev Walkinf15320b2004-06-03 03:38:44 +000075 if(!st || !st->buf) return cb("<absent>", 8, app_key);
76
77 ilevel += 4;
78 buf = st->buf;
79 end = buf + st->size;
80
81 /*
82 * Hexadecimal dump.
83 */
84 for(buf++; buf < end; buf++) {
Lev Walkin4a660bd2004-07-22 16:23:33 +000085 if(((buf - st->buf) - 1) % 16 == 0 && (st->size > 16)) {
Lev Walkinf15320b2004-06-03 03:38:44 +000086 int i;
87 /* Indentation */
88 if(cb("\n", 1, app_key)) return -1;
89 for(i = 0; i < ilevel; i++) cb(" ", 1, app_key);
90 /* Dump the string */
91 if(cb(scratch, p - scratch, app_key)) return -1;
92 p = scratch;
93 }
94 *p++ = h2c[*buf >> 4];
95 *p++ = h2c[*buf & 0x0F];
96 *p++ = 0x20;
97 }
98
99 /* Dump the incomplete 16-bytes row */
100 return cb(scratch, p - scratch, app_key);
101}
102