blob: a43a22aa5def53d8bc5f6ead2564ab88bd57d841 [file] [log] [blame]
vlmfa67ddc2004-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]),
vlm72425de2004-09-13 08:31:01 +000024 asn1_DEF_BIT_STRING_tags, /* Same as above */
25 sizeof(asn1_DEF_BIT_STRING_tags)
26 / sizeof(asn1_DEF_BIT_STRING_tags[0]),
vlmfa67ddc2004-06-03 03:38:44 +000027 -1, /* Both ways are fine */
vlme413c122004-08-20 13:23:42 +000028 0, 0, /* No members */
vlmfa67ddc2004-06-03 03:38:44 +000029 (void *)-1 /* Special indicator that this is a BIT STRING */
30};
31
32/*
33 * BIT STRING generic constraint.
34 */
35int
36BIT_STRING_constraint(asn1_TYPE_descriptor_t *td, const void *sptr,
37 asn_app_consume_bytes_f *app_errlog, void *app_key) {
vlmda674682004-08-11 09:07:36 +000038 const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
vlmfa67ddc2004-06-03 03:38:44 +000039
40 if(st && st->buf) {
41 if(st->size) {
42 if(st->size == 1 && st->buf[0] != 0) {
vlme3f0f282004-08-11 09:44:13 +000043 _ASN_ERRLOG(app_errlog, app_key,
vlm758530a2004-08-22 13:47:59 +000044 "%s: invalid padding byte (%s:%d)",
45 td->name, __FILE__, __LINE__);
vlmfa67ddc2004-06-03 03:38:44 +000046 return -1;
47 }
48 } else {
vlme3f0f282004-08-11 09:44:13 +000049 _ASN_ERRLOG(app_errlog, app_key,
vlm758530a2004-08-22 13:47:59 +000050 "%s: no padding byte (%s:%d)",
51 td->name, __FILE__, __LINE__);
vlmfa67ddc2004-06-03 03:38:44 +000052 return -1;
53 }
54 } else {
vlme3f0f282004-08-11 09:44:13 +000055 _ASN_ERRLOG(app_errlog, app_key,
vlm758530a2004-08-22 13:47:59 +000056 "%s: value not given (%s:%d)",
57 td->name, __FILE__, __LINE__);
vlmfa67ddc2004-06-03 03:38:44 +000058 return -1;
59 }
60
61 return 0;
62}
63
64/*
65 * BIT STRING specific contents printer.
66 */
67int
68BIT_STRING_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
69 asn_app_consume_bytes_f *cb, void *app_key) {
vlm1ff928d2004-08-11 08:10:13 +000070 static const char *h2c = "0123456789ABCDEF";
vlmfa67ddc2004-06-03 03:38:44 +000071 char scratch[64];
vlmda674682004-08-11 09:07:36 +000072 const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
vlmfa67ddc2004-06-03 03:38:44 +000073 uint8_t *buf;
74 uint8_t *end;
75 char *p = scratch;
76
vlmb42843a2004-06-05 08:17:50 +000077 (void)td; /* Unused argument */
78
vlmfa67ddc2004-06-03 03:38:44 +000079 if(!st || !st->buf) return cb("<absent>", 8, app_key);
80
81 ilevel += 4;
82 buf = st->buf;
83 end = buf + st->size;
84
85 /*
86 * Hexadecimal dump.
87 */
88 for(buf++; buf < end; buf++) {
vlm6d8518c2004-07-22 16:23:33 +000089 if(((buf - st->buf) - 1) % 16 == 0 && (st->size > 16)) {
vlmfa67ddc2004-06-03 03:38:44 +000090 int i;
91 /* Indentation */
92 if(cb("\n", 1, app_key)) return -1;
93 for(i = 0; i < ilevel; i++) cb(" ", 1, app_key);
94 /* Dump the string */
95 if(cb(scratch, p - scratch, app_key)) return -1;
96 p = scratch;
97 }
98 *p++ = h2c[*buf >> 4];
99 *p++ = h2c[*buf & 0x0F];
100 *p++ = 0x20;
101 }
102
103 /* Dump the incomplete 16-bytes row */
104 return cb(scratch, p - scratch, app_key);
105}
106