blob: a68621a66c8fb4d578bc848679eefb010c9411d4 [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>
Lev Walkin8e8078a2004-09-26 13:10:40 +00007#include <asn_internal.h>
Lev Walkinf15320b2004-06-03 03:38:44 +00008
9/*
10 * BIT STRING basic type description.
11 */
Wim Lewis18c2ec92014-07-29 11:30:10 -070012static const ber_tlv_tag_t asn_DEF_BIT_STRING_tags[] = {
Lev Walkinf15320b2004-06-03 03:38:44 +000013 (ASN_TAG_CLASS_UNIVERSAL | (3 << 2))
14};
Bi-Ruei, Chiu1fa31c92016-05-16 13:50:09 +080015asn_OCTET_STRING_specifics_t asn_SPC_BIT_STRING_specs = {
Lev Walkinbbd93252004-10-12 05:57:23 +000016 sizeof(BIT_STRING_t),
17 offsetof(BIT_STRING_t, _asn_ctx),
Lev Walkin3a4689a2006-11-24 11:20:27 +000018 ASN_OSUBV_BIT
Lev Walkinbbd93252004-10-12 05:57:23 +000019};
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080020asn_TYPE_operation_t asn_OP_BIT_STRING = {
Lev Walkina9cc46e2004-09-22 16:06:28 +000021 OCTET_STRING_free, /* Implemented in terms of OCTET STRING */
22 BIT_STRING_print,
Lev Walkincd2f48e2017-08-10 02:14:59 -070023 BIT_STRING_compare,
Lev Walkinf15320b2004-06-03 03:38:44 +000024 OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
25 OCTET_STRING_encode_der, /* Implemented in terms of OCTET STRING */
Lev Walkindc06f6b2004-10-20 15:50:55 +000026 OCTET_STRING_decode_xer_binary,
Lev Walkina9cc46e2004-09-22 16:06:28 +000027 BIT_STRING_encode_xer,
Lev Walkincc159472017-07-06 08:26:36 -070028#ifdef ASN_DISABLE_OER_SUPPORT
29 0,
30 0,
31#else
Lev Walkincfc16d32017-08-30 19:15:08 -070032 BIT_STRING_decode_oer,
33 BIT_STRING_encode_oer,
Lev Walkincc159472017-07-06 08:26:36 -070034#endif /* ASN_DISABLE_OER_SUPPORT */
Lev Walkinb33425f2017-07-14 14:59:52 +040035#ifdef ASN_DISABLE_PER_SUPPORT
36 0,
37 0,
38#else
39 OCTET_STRING_decode_uper, /* Unaligned PER decoder */
40 OCTET_STRING_encode_uper, /* Unaligned PER encoder */
41#endif /* ASN_DISABLE_PER_SUPPORT */
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080042 0 /* Use generic outmost tag fetcher */
43};
44asn_TYPE_descriptor_t asn_DEF_BIT_STRING = {
45 "BIT STRING",
46 "BIT_STRING",
47 &asn_OP_BIT_STRING,
48 BIT_STRING_constraint,
Lev Walkin5e033762004-09-29 13:26:15 +000049 asn_DEF_BIT_STRING_tags,
50 sizeof(asn_DEF_BIT_STRING_tags)
51 / sizeof(asn_DEF_BIT_STRING_tags[0]),
52 asn_DEF_BIT_STRING_tags, /* Same as above */
53 sizeof(asn_DEF_BIT_STRING_tags)
54 / sizeof(asn_DEF_BIT_STRING_tags[0]),
Lev Walkin76780762017-07-07 10:07:30 -070055 0, /* No OER visible constraints */
Lev Walkin59b176e2005-11-26 11:25:14 +000056 0, /* No PER visible constraints */
Lev Walkin449f8322004-08-20 13:23:42 +000057 0, 0, /* No members */
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +080058 &asn_SPC_BIT_STRING_specs
Lev Walkinf15320b2004-06-03 03:38:44 +000059};
60
61/*
62 * BIT STRING generic constraint.
63 */
64int
Lev Walkin5e033762004-09-29 13:26:15 +000065BIT_STRING_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkin1eded352006-07-13 11:19:01 +000066 asn_app_constraint_failed_f *ctfailcb, void *app_key) {
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
69 if(st && st->buf) {
Lev Walkin9abdf262007-02-21 19:27:45 +000070 if((st->size == 0 && st->bits_unused)
71 || st->bits_unused < 0 || st->bits_unused > 7) {
Lev Walkin7c1dc052016-03-14 03:08:15 -070072 ASN__CTFAIL(app_key, td, sptr,
Lev Walkinbbd93252004-10-12 05:57:23 +000073 "%s: invalid padding byte (%s:%d)",
Lev Walkin16835b62004-08-22 13:47:59 +000074 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +000075 return -1;
76 }
77 } else {
Lev Walkin7c1dc052016-03-14 03:08:15 -070078 ASN__CTFAIL(app_key, td, sptr,
Lev Walkin16835b62004-08-22 13:47:59 +000079 "%s: value not given (%s:%d)",
80 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +000081 return -1;
82 }
83
84 return 0;
85}
86
johvik5f619fb2017-05-09 09:05:04 +020087static const char *_bit_pattern[16] = {
Lev Walkina9cc46e2004-09-22 16:06:28 +000088 "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111",
89 "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"
90};
91
92asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +000093BIT_STRING_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +000094 int ilevel, enum xer_encoder_flags_e flags,
95 asn_app_consume_bytes_f *cb, void *app_key) {
96 asn_enc_rval_t er;
97 char scratch[128];
98 char *p = scratch;
99 char *scend = scratch + (sizeof(scratch) - 10);
100 const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
Lev Walkincc6a9102004-09-23 22:06:26 +0000101 int xcan = (flags & XER_F_CANONICAL);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000102 uint8_t *buf;
103 uint8_t *end;
104
105 if(!st || !st->buf)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700106 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000107
108 er.encoded = 0;
109
110 buf = st->buf;
111 end = buf + st->size - 1; /* Last byte is special */
112
113 /*
114 * Binary dump
115 */
Lev Walkinbbd93252004-10-12 05:57:23 +0000116 for(; buf < end; buf++) {
Lev Walkina9cc46e2004-09-22 16:06:28 +0000117 int v = *buf;
Lev Walkinbbd93252004-10-12 05:57:23 +0000118 int nline = xcan?0:(((buf - st->buf) % 8) == 0);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000119 if(p >= scend || nline) {
Lev Walkin7c1dc052016-03-14 03:08:15 -0700120 ASN__CALLBACK(scratch, p - scratch);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000121 p = scratch;
Lev Walkinc6cac8e2016-03-14 02:57:07 -0700122 if(nline) ASN__TEXT_INDENT(1, ilevel);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000123 }
124 memcpy(p + 0, _bit_pattern[v >> 4], 4);
125 memcpy(p + 4, _bit_pattern[v & 0x0f], 4);
126 p += 8;
127 }
128
Lev Walkinbbd93252004-10-12 05:57:23 +0000129 if(!xcan && ((buf - st->buf) % 8) == 0)
Lev Walkinc6cac8e2016-03-14 02:57:07 -0700130 ASN__TEXT_INDENT(1, ilevel);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700131 ASN__CALLBACK(scratch, p - scratch);
Lev Walkin938c6fe2004-09-24 20:56:25 +0000132 p = scratch;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000133
Lev Walkin938c6fe2004-09-24 20:56:25 +0000134 if(buf == end) {
Lev Walkina9cc46e2004-09-22 16:06:28 +0000135 int v = *buf;
Lev Walkinbbd93252004-10-12 05:57:23 +0000136 int ubits = st->bits_unused;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000137 int i;
Lev Walkinbbd93252004-10-12 05:57:23 +0000138 for(i = 7; i >= ubits; i--)
Lev Walkin5e033762004-09-29 13:26:15 +0000139 *p++ = (v & (1 << i)) ? 0x31 : 0x30;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700140 ASN__CALLBACK(scratch, p - scratch);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000141 }
142
Lev Walkinc6cac8e2016-03-14 02:57:07 -0700143 if(!xcan) ASN__TEXT_INDENT(1, ilevel - 1);
Lev Walkincc6a9102004-09-23 22:06:26 +0000144
Lev Walkin7c1dc052016-03-14 03:08:15 -0700145 ASN__ENCODED_OK(er);
Lev Walkin942fd082004-10-03 09:13:02 +0000146cb_failed:
Lev Walkin7c1dc052016-03-14 03:08:15 -0700147 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000148}
149
150
Lev Walkinf15320b2004-06-03 03:38:44 +0000151/*
152 * BIT STRING specific contents printer.
153 */
154int
Lev Walkin5e033762004-09-29 13:26:15 +0000155BIT_STRING_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +0000156 asn_app_consume_bytes_f *cb, void *app_key) {
Wim Lewis59e8d282014-08-04 12:39:35 -0700157 const char * const h2c = "0123456789ABCDEF";
Lev Walkinf15320b2004-06-03 03:38:44 +0000158 char scratch[64];
Lev Walkinc2346572004-08-11 09:07:36 +0000159 const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000160 uint8_t *buf;
161 uint8_t *end;
162 char *p = scratch;
163
Lev Walkind9bd7752004-06-05 08:17:50 +0000164 (void)td; /* Unused argument */
165
Lev Walkin8e8078a2004-09-26 13:10:40 +0000166 if(!st || !st->buf)
167 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000168
Lev Walkin8e8078a2004-09-26 13:10:40 +0000169 ilevel++;
Lev Walkinf15320b2004-06-03 03:38:44 +0000170 buf = st->buf;
171 end = buf + st->size;
172
173 /*
174 * Hexadecimal dump.
175 */
Lev Walkinbbd93252004-10-12 05:57:23 +0000176 for(; buf < end; buf++) {
177 if((buf - st->buf) % 16 == 0 && (st->size > 16)
178 && buf != st->buf) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000179 _i_INDENT(1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000180 /* Dump the string */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000181 if(cb(scratch, p - scratch, app_key) < 0) return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000182 p = scratch;
183 }
184 *p++ = h2c[*buf >> 4];
185 *p++ = h2c[*buf & 0x0F];
186 *p++ = 0x20;
187 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000188
Lev Walkincc6a9102004-09-23 22:06:26 +0000189 if(p > scratch) {
190 p--; /* Eat the tailing space */
191
Lev Walkinbbd93252004-10-12 05:57:23 +0000192 if((st->size > 16)) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000193 _i_INDENT(1);
Lev Walkincc6a9102004-09-23 22:06:26 +0000194 }
195
196 /* Dump the incomplete 16-bytes row */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000197 if(cb(scratch, p - scratch, app_key) < 0)
Lev Walkincc6a9102004-09-23 22:06:26 +0000198 return -1;
199 }
200
201 return 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000202}
203
Lev Walkincd2f48e2017-08-10 02:14:59 -0700204/*
205 * Lexicographically compare the common prefix of both strings,
206 * and if it is the same return -1 for the smallest string.
207 */
208int
209BIT_STRING_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
210 const void *bptr) {
211 const BIT_STRING_t *a = aptr;
212 const BIT_STRING_t *b = bptr;
213
214 (void)td;
215
216 if(a && b) {
217 size_t common_prefix_size = a->size <= b->size ? a->size : b->size;
218 int ret = memcmp(a->buf, b->buf, common_prefix_size);
219 if(ret == 0) {
220 /* Figure out which string with equal prefixes is longer. */
221 if(a->size < b->size) {
222 return -1;
223 } else if(a->size > b->size) {
224 return 1;
225 } else {
226 /* Figure out how many unused bits */
227 if(a->bits_unused < b->bits_unused) {
228 return -1;
229 } else if(a->bits_unused > b->bits_unused) {
230 return 1;
231 } else {
232 return 0;
233 }
234 }
235 } else {
236 return ret;
237 }
238 } else if(!a && !b) {
239 return 0;
240 } else if(!a) {
241 return -1;
242 } else {
243 return 1;
244 }
245}
246