blob: e21594a5104476eb9a32e04c1cb804c7c78d19a7 [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 */
Lev Walkin5e033762004-09-29 13:26:15 +000012static ber_tlv_tag_t asn_DEF_BIT_STRING_tags[] = {
Lev Walkinf15320b2004-06-03 03:38:44 +000013 (ASN_TAG_CLASS_UNIVERSAL | (3 << 2))
14};
Lev Walkinbbd93252004-10-12 05:57:23 +000015static asn_OCTET_STRING_specifics_t asn_DEF_BIT_STRING_specs = {
16 sizeof(BIT_STRING_t),
17 offsetof(BIT_STRING_t, _asn_ctx),
18 1, /* Special indicator that this is a BIT STRING type */
19};
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 Walkinf15320b2004-06-03 03:38:44 +000030 0, /* Use generic outmost tag fetcher */
Lev Walkin5e033762004-09-29 13:26:15 +000031 asn_DEF_BIT_STRING_tags,
32 sizeof(asn_DEF_BIT_STRING_tags)
33 / sizeof(asn_DEF_BIT_STRING_tags[0]),
34 asn_DEF_BIT_STRING_tags, /* Same as above */
35 sizeof(asn_DEF_BIT_STRING_tags)
36 / sizeof(asn_DEF_BIT_STRING_tags[0]),
Lev Walkin449f8322004-08-20 13:23:42 +000037 0, 0, /* No members */
Lev Walkinbbd93252004-10-12 05:57:23 +000038 &asn_DEF_BIT_STRING_specs
Lev Walkinf15320b2004-06-03 03:38:44 +000039};
40
41/*
42 * BIT STRING generic constraint.
43 */
44int
Lev Walkin5e033762004-09-29 13:26:15 +000045BIT_STRING_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +000046 asn_app_consume_bytes_f *app_errlog, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +000047 const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +000048
49 if(st && st->buf) {
Lev Walkinbbd93252004-10-12 05:57:23 +000050 if(st->size == 1 && st->bits_unused) {
Lev Walkinba4e5182004-08-11 09:44:13 +000051 _ASN_ERRLOG(app_errlog, app_key,
Lev Walkinbbd93252004-10-12 05:57:23 +000052 "%s: invalid padding byte (%s:%d)",
Lev Walkin16835b62004-08-22 13:47:59 +000053 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +000054 return -1;
55 }
56 } else {
Lev Walkinba4e5182004-08-11 09:44:13 +000057 _ASN_ERRLOG(app_errlog, app_key,
Lev Walkin16835b62004-08-22 13:47:59 +000058 "%s: value not given (%s:%d)",
59 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +000060 return -1;
61 }
62
63 return 0;
64}
65
Lev Walkina9cc46e2004-09-22 16:06:28 +000066static char *_bit_pattern[16] = {
67 "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111",
68 "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"
69};
70
71asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +000072BIT_STRING_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +000073 int ilevel, enum xer_encoder_flags_e flags,
74 asn_app_consume_bytes_f *cb, void *app_key) {
75 asn_enc_rval_t er;
76 char scratch[128];
77 char *p = scratch;
78 char *scend = scratch + (sizeof(scratch) - 10);
79 const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
Lev Walkincc6a9102004-09-23 22:06:26 +000080 int xcan = (flags & XER_F_CANONICAL);
Lev Walkina9cc46e2004-09-22 16:06:28 +000081 uint8_t *buf;
82 uint8_t *end;
83
84 if(!st || !st->buf)
85 _ASN_ENCODE_FAILED;
86
87 er.encoded = 0;
88
89 buf = st->buf;
90 end = buf + st->size - 1; /* Last byte is special */
91
92 /*
93 * Binary dump
94 */
Lev Walkinbbd93252004-10-12 05:57:23 +000095 for(; buf < end; buf++) {
Lev Walkina9cc46e2004-09-22 16:06:28 +000096 int v = *buf;
Lev Walkinbbd93252004-10-12 05:57:23 +000097 int nline = xcan?0:(((buf - st->buf) % 8) == 0);
Lev Walkina9cc46e2004-09-22 16:06:28 +000098 if(p >= scend || nline) {
99 er.encoded += p - scratch;
100 _ASN_CALLBACK(scratch, p - scratch);
101 p = scratch;
102 if(nline) _i_ASN_TEXT_INDENT(1, ilevel);
103 }
104 memcpy(p + 0, _bit_pattern[v >> 4], 4);
105 memcpy(p + 4, _bit_pattern[v & 0x0f], 4);
106 p += 8;
107 }
108
Lev Walkinbbd93252004-10-12 05:57:23 +0000109 if(!xcan && ((buf - st->buf) % 8) == 0)
Lev Walkincc6a9102004-09-23 22:06:26 +0000110 _i_ASN_TEXT_INDENT(1, ilevel);
Lev Walkin938c6fe2004-09-24 20:56:25 +0000111 er.encoded += p - scratch;
112 _ASN_CALLBACK(scratch, p - scratch);
113 p = scratch;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000114
Lev Walkin938c6fe2004-09-24 20:56:25 +0000115 if(buf == end) {
Lev Walkina9cc46e2004-09-22 16:06:28 +0000116 int v = *buf;
Lev Walkinbbd93252004-10-12 05:57:23 +0000117 int ubits = st->bits_unused;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000118 int i;
Lev Walkinbbd93252004-10-12 05:57:23 +0000119 for(i = 7; i >= ubits; i--)
Lev Walkin5e033762004-09-29 13:26:15 +0000120 *p++ = (v & (1 << i)) ? 0x31 : 0x30;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000121 er.encoded += p - scratch;
122 _ASN_CALLBACK(scratch, p - scratch);
123 }
124
Lev Walkin938c6fe2004-09-24 20:56:25 +0000125 if(!xcan) _i_ASN_TEXT_INDENT(1, ilevel - 1);
Lev Walkincc6a9102004-09-23 22:06:26 +0000126
Lev Walkina9cc46e2004-09-22 16:06:28 +0000127 return er;
Lev Walkin942fd082004-10-03 09:13:02 +0000128cb_failed:
129 _ASN_ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000130}
131
132
Lev Walkinf15320b2004-06-03 03:38:44 +0000133/*
134 * BIT STRING specific contents printer.
135 */
136int
Lev Walkin5e033762004-09-29 13:26:15 +0000137BIT_STRING_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +0000138 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin4d9528c2004-08-11 08:10:13 +0000139 static const char *h2c = "0123456789ABCDEF";
Lev Walkinf15320b2004-06-03 03:38:44 +0000140 char scratch[64];
Lev Walkinc2346572004-08-11 09:07:36 +0000141 const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000142 uint8_t *buf;
143 uint8_t *end;
144 char *p = scratch;
145
Lev Walkind9bd7752004-06-05 08:17:50 +0000146 (void)td; /* Unused argument */
147
Lev Walkin8e8078a2004-09-26 13:10:40 +0000148 if(!st || !st->buf)
149 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000150
Lev Walkin8e8078a2004-09-26 13:10:40 +0000151 ilevel++;
Lev Walkinf15320b2004-06-03 03:38:44 +0000152 buf = st->buf;
153 end = buf + st->size;
154
155 /*
156 * Hexadecimal dump.
157 */
Lev Walkinbbd93252004-10-12 05:57:23 +0000158 for(; buf < end; buf++) {
159 if((buf - st->buf) % 16 == 0 && (st->size > 16)
160 && buf != st->buf) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000161 _i_INDENT(1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000162 /* Dump the string */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000163 if(cb(scratch, p - scratch, app_key) < 0) return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000164 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
Lev Walkinbbd93252004-10-12 05:57:23 +0000174 if((st->size > 16)) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000175 _i_INDENT(1);
Lev Walkincc6a9102004-09-23 22:06:26 +0000176 }
177
178 /* Dump the incomplete 16-bytes row */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000179 if(cb(scratch, p - scratch, app_key) < 0)
Lev Walkincc6a9102004-09-23 22:06:26 +0000180 return -1;
181 }
182
183 return 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000184}
185