blob: e92f27e5e930a9a29a9cff1bac7fa75b8a75f7c3 [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, Chiu419ac3a2016-05-14 15:27:19 +080015static asn_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};
Lev Walkin5e033762004-09-29 13:26:15 +000020asn_TYPE_descriptor_t asn_DEF_BIT_STRING = {
Lev Walkinf15320b2004-06-03 03:38:44 +000021 "BIT STRING",
Lev Walkindc06f6b2004-10-20 15:50:55 +000022 "BIT_STRING",
Lev Walkina9cc46e2004-09-22 16:06:28 +000023 OCTET_STRING_free, /* Implemented in terms of OCTET STRING */
24 BIT_STRING_print,
Lev Walkinf15320b2004-06-03 03:38:44 +000025 BIT_STRING_constraint,
26 OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
27 OCTET_STRING_encode_der, /* Implemented in terms of OCTET STRING */
Lev Walkindc06f6b2004-10-20 15:50:55 +000028 OCTET_STRING_decode_xer_binary,
Lev Walkina9cc46e2004-09-22 16:06:28 +000029 BIT_STRING_encode_xer,
Lev Walkin80334ed2017-07-06 07:28:21 -070030#ifdef ASN_DISABLE_PER_SUPPORT
31 0,
32 0,
33#else
Lev Walkin59b176e2005-11-26 11:25:14 +000034 OCTET_STRING_decode_uper, /* Unaligned PER decoder */
Lev Walkin523de9e2006-08-18 01:34:18 +000035 OCTET_STRING_encode_uper, /* Unaligned PER encoder */
Lev Walkin80334ed2017-07-06 07:28:21 -070036#endif /* ASN_DISABLE_PER_SUPPORT */
Lev Walkinf15320b2004-06-03 03:38:44 +000037 0, /* Use generic outmost tag fetcher */
Lev Walkin5e033762004-09-29 13:26:15 +000038 asn_DEF_BIT_STRING_tags,
39 sizeof(asn_DEF_BIT_STRING_tags)
40 / sizeof(asn_DEF_BIT_STRING_tags[0]),
41 asn_DEF_BIT_STRING_tags, /* Same as above */
42 sizeof(asn_DEF_BIT_STRING_tags)
43 / sizeof(asn_DEF_BIT_STRING_tags[0]),
Lev Walkin59b176e2005-11-26 11:25:14 +000044 0, /* No PER visible constraints */
Lev Walkin449f8322004-08-20 13:23:42 +000045 0, 0, /* No members */
Bi-Ruei, Chiu419ac3a2016-05-14 15:27:19 +080046 &asn_SPC_BIT_STRING_specs
Lev Walkinf15320b2004-06-03 03:38:44 +000047};
48
49/*
50 * BIT STRING generic constraint.
51 */
52int
Lev Walkin5e033762004-09-29 13:26:15 +000053BIT_STRING_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkin1eded352006-07-13 11:19:01 +000054 asn_app_constraint_failed_f *ctfailcb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +000055 const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +000056
57 if(st && st->buf) {
Lev Walkin9abdf262007-02-21 19:27:45 +000058 if((st->size == 0 && st->bits_unused)
59 || st->bits_unused < 0 || st->bits_unused > 7) {
Lev Walkin7c1dc052016-03-14 03:08:15 -070060 ASN__CTFAIL(app_key, td, sptr,
Lev Walkinbbd93252004-10-12 05:57:23 +000061 "%s: invalid padding byte (%s:%d)",
Lev Walkin16835b62004-08-22 13:47:59 +000062 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +000063 return -1;
64 }
65 } else {
Lev Walkin7c1dc052016-03-14 03:08:15 -070066 ASN__CTFAIL(app_key, td, sptr,
Lev Walkin16835b62004-08-22 13:47:59 +000067 "%s: value not given (%s:%d)",
68 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +000069 return -1;
70 }
71
72 return 0;
73}
74
Lev Walkina9cc46e2004-09-22 16:06:28 +000075static char *_bit_pattern[16] = {
76 "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111",
77 "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"
78};
79
80asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +000081BIT_STRING_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +000082 int ilevel, enum xer_encoder_flags_e flags,
83 asn_app_consume_bytes_f *cb, void *app_key) {
84 asn_enc_rval_t er;
85 char scratch[128];
86 char *p = scratch;
87 char *scend = scratch + (sizeof(scratch) - 10);
88 const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
Lev Walkincc6a9102004-09-23 22:06:26 +000089 int xcan = (flags & XER_F_CANONICAL);
Lev Walkina9cc46e2004-09-22 16:06:28 +000090 uint8_t *buf;
91 uint8_t *end;
92
93 if(!st || !st->buf)
Lev Walkin7c1dc052016-03-14 03:08:15 -070094 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +000095
96 er.encoded = 0;
97
98 buf = st->buf;
99 end = buf + st->size - 1; /* Last byte is special */
100
101 /*
102 * Binary dump
103 */
Lev Walkinbbd93252004-10-12 05:57:23 +0000104 for(; buf < end; buf++) {
Lev Walkina9cc46e2004-09-22 16:06:28 +0000105 int v = *buf;
Lev Walkinbbd93252004-10-12 05:57:23 +0000106 int nline = xcan?0:(((buf - st->buf) % 8) == 0);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000107 if(p >= scend || nline) {
108 er.encoded += p - scratch;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700109 ASN__CALLBACK(scratch, p - scratch);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000110 p = scratch;
Lev Walkinc6cac8e2016-03-14 02:57:07 -0700111 if(nline) ASN__TEXT_INDENT(1, ilevel);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000112 }
113 memcpy(p + 0, _bit_pattern[v >> 4], 4);
114 memcpy(p + 4, _bit_pattern[v & 0x0f], 4);
115 p += 8;
116 }
117
Lev Walkinbbd93252004-10-12 05:57:23 +0000118 if(!xcan && ((buf - st->buf) % 8) == 0)
Lev Walkinc6cac8e2016-03-14 02:57:07 -0700119 ASN__TEXT_INDENT(1, ilevel);
Lev Walkin938c6fe2004-09-24 20:56:25 +0000120 er.encoded += p - scratch;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700121 ASN__CALLBACK(scratch, p - scratch);
Lev Walkin938c6fe2004-09-24 20:56:25 +0000122 p = scratch;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000123
Lev Walkin938c6fe2004-09-24 20:56:25 +0000124 if(buf == end) {
Lev Walkina9cc46e2004-09-22 16:06:28 +0000125 int v = *buf;
Lev Walkinbbd93252004-10-12 05:57:23 +0000126 int ubits = st->bits_unused;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000127 int i;
Lev Walkinbbd93252004-10-12 05:57:23 +0000128 for(i = 7; i >= ubits; i--)
Lev Walkin5e033762004-09-29 13:26:15 +0000129 *p++ = (v & (1 << i)) ? 0x31 : 0x30;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000130 er.encoded += p - scratch;
Lev Walkin7c1dc052016-03-14 03:08:15 -0700131 ASN__CALLBACK(scratch, p - scratch);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000132 }
133
Lev Walkinc6cac8e2016-03-14 02:57:07 -0700134 if(!xcan) ASN__TEXT_INDENT(1, ilevel - 1);
Lev Walkincc6a9102004-09-23 22:06:26 +0000135
Lev Walkin7c1dc052016-03-14 03:08:15 -0700136 ASN__ENCODED_OK(er);
Lev Walkin942fd082004-10-03 09:13:02 +0000137cb_failed:
Lev Walkin7c1dc052016-03-14 03:08:15 -0700138 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000139}
140
141
Lev Walkinf15320b2004-06-03 03:38:44 +0000142/*
143 * BIT STRING specific contents printer.
144 */
145int
Lev Walkin5e033762004-09-29 13:26:15 +0000146BIT_STRING_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +0000147 asn_app_consume_bytes_f *cb, void *app_key) {
Wim Lewis59e8d282014-08-04 12:39:35 -0700148 const char * const h2c = "0123456789ABCDEF";
Lev Walkinf15320b2004-06-03 03:38:44 +0000149 char scratch[64];
Lev Walkinc2346572004-08-11 09:07:36 +0000150 const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000151 uint8_t *buf;
152 uint8_t *end;
153 char *p = scratch;
154
Lev Walkind9bd7752004-06-05 08:17:50 +0000155 (void)td; /* Unused argument */
156
Lev Walkin8e8078a2004-09-26 13:10:40 +0000157 if(!st || !st->buf)
158 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000159
Lev Walkin8e8078a2004-09-26 13:10:40 +0000160 ilevel++;
Lev Walkinf15320b2004-06-03 03:38:44 +0000161 buf = st->buf;
162 end = buf + st->size;
163
164 /*
165 * Hexadecimal dump.
166 */
Lev Walkinbbd93252004-10-12 05:57:23 +0000167 for(; buf < end; buf++) {
168 if((buf - st->buf) % 16 == 0 && (st->size > 16)
169 && buf != st->buf) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000170 _i_INDENT(1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000171 /* Dump the string */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000172 if(cb(scratch, p - scratch, app_key) < 0) return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000173 p = scratch;
174 }
175 *p++ = h2c[*buf >> 4];
176 *p++ = h2c[*buf & 0x0F];
177 *p++ = 0x20;
178 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000179
Lev Walkincc6a9102004-09-23 22:06:26 +0000180 if(p > scratch) {
181 p--; /* Eat the tailing space */
182
Lev Walkinbbd93252004-10-12 05:57:23 +0000183 if((st->size > 16)) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000184 _i_INDENT(1);
Lev Walkincc6a9102004-09-23 22:06:26 +0000185 }
186
187 /* Dump the incomplete 16-bytes row */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000188 if(cb(scratch, p - scratch, app_key) < 0)
Lev Walkincc6a9102004-09-23 22:06:26 +0000189 return -1;
190 }
191
192 return 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000193}
194