blob: 4914b2074e193fe57a0384f5a9166fb49cf6c042 [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) {
35 const BIT_STRING_t *st = sptr;
36
37 if(st && st->buf) {
38 if(st->size) {
39 if(st->size == 1 && st->buf[0] != 0) {
40 _ASN_ERRLOG("%s: invalid padding byte",
41 td->name);
42 return -1;
43 }
44 } else {
45 _ASN_ERRLOG("%s: no padding byte", td->name);
46 return -1;
47 }
48 } else {
49 _ASN_ERRLOG("%s: value not given", td->name);
50 return -1;
51 }
52
53 return 0;
54}
55
56/*
57 * BIT STRING specific contents printer.
58 */
59int
60BIT_STRING_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
61 asn_app_consume_bytes_f *cb, void *app_key) {
62 static char h2c[16] = "0123456789ABCDEF";
63 char scratch[64];
64 const BIT_STRING_t *st = sptr;
65 uint8_t *buf;
66 uint8_t *end;
67 char *p = scratch;
68
Lev Walkind9bd7752004-06-05 08:17:50 +000069 (void)td; /* Unused argument */
70
Lev Walkinf15320b2004-06-03 03:38:44 +000071 if(!st || !st->buf) return cb("<absent>", 8, app_key);
72
73 ilevel += 4;
74 buf = st->buf;
75 end = buf + st->size;
76
77 /*
78 * Hexadecimal dump.
79 */
80 for(buf++; buf < end; buf++) {
81 if(((buf - st->buf) - 1) % 16 == 0) {
82 int i;
83 /* Indentation */
84 if(cb("\n", 1, app_key)) return -1;
85 for(i = 0; i < ilevel; i++) cb(" ", 1, app_key);
86 /* Dump the string */
87 if(cb(scratch, p - scratch, app_key)) return -1;
88 p = scratch;
89 }
90 *p++ = h2c[*buf >> 4];
91 *p++ = h2c[*buf & 0x0F];
92 *p++ = 0x20;
93 }
94
95 /* Dump the incomplete 16-bytes row */
96 return cb(scratch, p - scratch, app_key);
97}
98