blob: 7e576a1e85248ae1bfdf5f5140bc1f132f4e000a [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 */
26 (void *)-1 /* Special indicator that this is a BIT STRING */
27};
28
29/*
30 * BIT STRING generic constraint.
31 */
32int
33BIT_STRING_constraint(asn1_TYPE_descriptor_t *td, const void *sptr,
34 asn_app_consume_bytes_f *app_errlog, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +000035 const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +000036
37 if(st && st->buf) {
38 if(st->size) {
39 if(st->size == 1 && st->buf[0] != 0) {
Lev Walkinba4e5182004-08-11 09:44:13 +000040 _ASN_ERRLOG(app_errlog, app_key,
41 "%s: invalid padding byte",
Lev Walkinf15320b2004-06-03 03:38:44 +000042 td->name);
43 return -1;
44 }
45 } else {
Lev Walkinba4e5182004-08-11 09:44:13 +000046 _ASN_ERRLOG(app_errlog, app_key,
47 "%s: no padding byte", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +000048 return -1;
49 }
50 } else {
Lev Walkinba4e5182004-08-11 09:44:13 +000051 _ASN_ERRLOG(app_errlog, app_key,
52 "%s: value not given", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +000053 return -1;
54 }
55
56 return 0;
57}
58
59/*
60 * BIT STRING specific contents printer.
61 */
62int
63BIT_STRING_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
64 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin4d9528c2004-08-11 08:10:13 +000065 static const char *h2c = "0123456789ABCDEF";
Lev Walkinf15320b2004-06-03 03:38:44 +000066 char scratch[64];
Lev Walkinc2346572004-08-11 09:07:36 +000067 const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +000068 uint8_t *buf;
69 uint8_t *end;
70 char *p = scratch;
71
Lev Walkind9bd7752004-06-05 08:17:50 +000072 (void)td; /* Unused argument */
73
Lev Walkinf15320b2004-06-03 03:38:44 +000074 if(!st || !st->buf) return cb("<absent>", 8, app_key);
75
76 ilevel += 4;
77 buf = st->buf;
78 end = buf + st->size;
79
80 /*
81 * Hexadecimal dump.
82 */
83 for(buf++; buf < end; buf++) {
Lev Walkin4a660bd2004-07-22 16:23:33 +000084 if(((buf - st->buf) - 1) % 16 == 0 && (st->size > 16)) {
Lev Walkinf15320b2004-06-03 03:38:44 +000085 int i;
86 /* Indentation */
87 if(cb("\n", 1, app_key)) return -1;
88 for(i = 0; i < ilevel; i++) cb(" ", 1, app_key);
89 /* Dump the string */
90 if(cb(scratch, p - scratch, app_key)) return -1;
91 p = scratch;
92 }
93 *p++ = h2c[*buf >> 4];
94 *p++ = h2c[*buf & 0x0F];
95 *p++ = 0x20;
96 }
97
98 /* Dump the incomplete 16-bytes row */
99 return cb(scratch, p - scratch, app_key);
100}
101