blob: c3788d834cee47d1c6dc4c9c2a88dfee53bb0e25 [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 */
Lev Walkina9cc46e2004-09-22 16:06:28 +00005#include <asn_internal.h>
Lev Walkinf15320b2004-06-03 03:38:44 +00006#include <BIT_STRING.h>
7
8/*
9 * BIT STRING basic type description.
10 */
11static ber_tlv_tag_t asn1_DEF_BIT_STRING_tags[] = {
12 (ASN_TAG_CLASS_UNIVERSAL | (3 << 2))
13};
14asn1_TYPE_descriptor_t asn1_DEF_BIT_STRING = {
15 "BIT STRING",
Lev Walkina9cc46e2004-09-22 16:06:28 +000016 OCTET_STRING_free, /* Implemented in terms of OCTET STRING */
17 BIT_STRING_print,
Lev Walkinf15320b2004-06-03 03:38:44 +000018 BIT_STRING_constraint,
19 OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
20 OCTET_STRING_encode_der, /* Implemented in terms of OCTET STRING */
Lev Walkina9cc46e2004-09-22 16:06:28 +000021 0, /* Not implemented yet */
22 BIT_STRING_encode_xer,
Lev Walkinf15320b2004-06-03 03:38:44 +000023 0, /* Use generic outmost tag fetcher */
24 asn1_DEF_BIT_STRING_tags,
25 sizeof(asn1_DEF_BIT_STRING_tags)
26 / sizeof(asn1_DEF_BIT_STRING_tags[0]),
Lev Walkin188ed2c2004-09-13 08:31:01 +000027 asn1_DEF_BIT_STRING_tags, /* Same as above */
28 sizeof(asn1_DEF_BIT_STRING_tags)
29 / sizeof(asn1_DEF_BIT_STRING_tags[0]),
Lev Walkinf15320b2004-06-03 03:38:44 +000030 -1, /* Both ways are fine */
Lev Walkin449f8322004-08-20 13:23:42 +000031 0, 0, /* No members */
Lev Walkinf15320b2004-06-03 03:38:44 +000032 (void *)-1 /* Special indicator that this is a BIT STRING */
33};
34
35/*
36 * BIT STRING generic constraint.
37 */
38int
39BIT_STRING_constraint(asn1_TYPE_descriptor_t *td, const void *sptr,
40 asn_app_consume_bytes_f *app_errlog, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +000041 const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +000042
43 if(st && st->buf) {
44 if(st->size) {
45 if(st->size == 1 && st->buf[0] != 0) {
Lev Walkinba4e5182004-08-11 09:44:13 +000046 _ASN_ERRLOG(app_errlog, app_key,
Lev Walkin16835b62004-08-22 13:47:59 +000047 "%s: invalid padding byte (%s:%d)",
48 td->name, __FILE__, __LINE__);
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,
Lev Walkin16835b62004-08-22 13:47:59 +000053 "%s: no padding byte (%s:%d)",
54 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +000055 return -1;
56 }
57 } else {
Lev Walkinba4e5182004-08-11 09:44:13 +000058 _ASN_ERRLOG(app_errlog, app_key,
Lev Walkin16835b62004-08-22 13:47:59 +000059 "%s: value not given (%s:%d)",
60 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +000061 return -1;
62 }
63
64 return 0;
65}
66
Lev Walkina9cc46e2004-09-22 16:06:28 +000067static char *_bit_pattern[16] = {
68 "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111",
69 "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"
70};
71
72asn_enc_rval_t
73BIT_STRING_encode_xer(asn1_TYPE_descriptor_t *td, void *sptr,
74 int ilevel, enum xer_encoder_flags_e flags,
75 asn_app_consume_bytes_f *cb, void *app_key) {
76 asn_enc_rval_t er;
77 char scratch[128];
78 char *p = scratch;
79 char *scend = scratch + (sizeof(scratch) - 10);
80 const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
Lev Walkincc6a9102004-09-23 22:06:26 +000081 int xcan = (flags & XER_F_CANONICAL);
Lev Walkina9cc46e2004-09-22 16:06:28 +000082 uint8_t *buf;
83 uint8_t *end;
84
85 if(!st || !st->buf)
86 _ASN_ENCODE_FAILED;
87
88 er.encoded = 0;
89
90 buf = st->buf;
91 end = buf + st->size - 1; /* Last byte is special */
92
93 /*
94 * Binary dump
95 */
96 for(buf++; buf < end; buf++) {
97 int v = *buf;
Lev Walkincc6a9102004-09-23 22:06:26 +000098 int nline = xcan?0:((((buf - st->buf) - 1) % 8) == 0);
Lev Walkina9cc46e2004-09-22 16:06:28 +000099 if(p >= scend || nline) {
100 er.encoded += p - scratch;
101 _ASN_CALLBACK(scratch, p - scratch);
102 p = scratch;
103 if(nline) _i_ASN_TEXT_INDENT(1, ilevel);
104 }
105 memcpy(p + 0, _bit_pattern[v >> 4], 4);
106 memcpy(p + 4, _bit_pattern[v & 0x0f], 4);
107 p += 8;
108 }
109
110 er.encoded += p - scratch;
Lev Walkincc6a9102004-09-23 22:06:26 +0000111 if(!xcan && (((buf - st->buf) - 1) % 8) == 0)
112 _i_ASN_TEXT_INDENT(1, ilevel);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000113
114 if(buf < end + 1) {
115 int v = *buf;
116 int mbit = st->buf[0]; /* bits to skip from the right */
117 int i;
118 for(i = 7; i >= mbit; i--)
119 *p++ = (v & (1 << i)) ? '1' : '0';
120 er.encoded += p - scratch;
121 _ASN_CALLBACK(scratch, p - scratch);
122 }
123
Lev Walkincc6a9102004-09-23 22:06:26 +0000124 if(!xcan && ((st->size - 1) % 8) == 0)
125 _i_ASN_TEXT_INDENT(1, ilevel - 1);
126
Lev Walkina9cc46e2004-09-22 16:06:28 +0000127 return er;
128}
129
130
Lev Walkinf15320b2004-06-03 03:38:44 +0000131/*
132 * BIT STRING specific contents printer.
133 */
134int
135BIT_STRING_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
136 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin4d9528c2004-08-11 08:10:13 +0000137 static const char *h2c = "0123456789ABCDEF";
Lev Walkinf15320b2004-06-03 03:38:44 +0000138 char scratch[64];
Lev Walkinc2346572004-08-11 09:07:36 +0000139 const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000140 uint8_t *buf;
141 uint8_t *end;
142 char *p = scratch;
143
Lev Walkind9bd7752004-06-05 08:17:50 +0000144 (void)td; /* Unused argument */
145
Lev Walkinf15320b2004-06-03 03:38:44 +0000146 if(!st || !st->buf) return cb("<absent>", 8, app_key);
147
148 ilevel += 4;
149 buf = st->buf;
150 end = buf + st->size;
151
152 /*
153 * Hexadecimal dump.
154 */
155 for(buf++; buf < end; buf++) {
Lev Walkincc6a9102004-09-23 22:06:26 +0000156 if(((buf - st->buf) - 1) % 16 == 0 && (st->size > 17)
157 && buf != st->buf+1) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000158 int i;
159 /* Indentation */
160 if(cb("\n", 1, app_key)) return -1;
161 for(i = 0; i < ilevel; i++) cb(" ", 1, app_key);
162 /* Dump the string */
163 if(cb(scratch, p - scratch, app_key)) return -1;
164 p = scratch;
165 }
166 *p++ = h2c[*buf >> 4];
167 *p++ = h2c[*buf & 0x0F];
168 *p++ = 0x20;
169 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000170
Lev Walkincc6a9102004-09-23 22:06:26 +0000171 if(p > scratch) {
172 p--; /* Eat the tailing space */
173
174 if((st->size > 17)) {
175 int i;
176 if(cb("\n", 1, app_key)) return -1;
177 for(i = 0; i < ilevel; i++) cb(" ", 1, app_key);
178 }
179
180 /* Dump the incomplete 16-bytes row */
181 if(cb(scratch, p - scratch, app_key))
182 return -1;
183 }
184
185 return 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000186}
187