blob: b6da3aeab04a3023b07af7ff6ef121b10fa5df8a [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 */
vlm39ba4c42004-09-22 16:06:28 +00005#include <asn_internal.h>
vlmfa67ddc2004-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",
vlm39ba4c42004-09-22 16:06:28 +000016 OCTET_STRING_free, /* Implemented in terms of OCTET STRING */
17 BIT_STRING_print,
vlmfa67ddc2004-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 */
vlm39ba4c42004-09-22 16:06:28 +000021 0, /* Not implemented yet */
22 BIT_STRING_encode_xer,
vlmfa67ddc2004-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]),
vlm72425de2004-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]),
vlmfa67ddc2004-06-03 03:38:44 +000030 -1, /* Both ways are fine */
vlme413c122004-08-20 13:23:42 +000031 0, 0, /* No members */
vlmfa67ddc2004-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) {
vlmda674682004-08-11 09:07:36 +000041 const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
vlmfa67ddc2004-06-03 03:38:44 +000042
43 if(st && st->buf) {
44 if(st->size) {
45 if(st->size == 1 && st->buf[0] != 0) {
vlme3f0f282004-08-11 09:44:13 +000046 _ASN_ERRLOG(app_errlog, app_key,
vlm758530a2004-08-22 13:47:59 +000047 "%s: invalid padding byte (%s:%d)",
48 td->name, __FILE__, __LINE__);
vlmfa67ddc2004-06-03 03:38:44 +000049 return -1;
50 }
51 } else {
vlme3f0f282004-08-11 09:44:13 +000052 _ASN_ERRLOG(app_errlog, app_key,
vlm758530a2004-08-22 13:47:59 +000053 "%s: no padding byte (%s:%d)",
54 td->name, __FILE__, __LINE__);
vlmfa67ddc2004-06-03 03:38:44 +000055 return -1;
56 }
57 } else {
vlme3f0f282004-08-11 09:44:13 +000058 _ASN_ERRLOG(app_errlog, app_key,
vlm758530a2004-08-22 13:47:59 +000059 "%s: value not given (%s:%d)",
60 td->name, __FILE__, __LINE__);
vlmfa67ddc2004-06-03 03:38:44 +000061 return -1;
62 }
63
64 return 0;
65}
66
vlm39ba4c42004-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;
81 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 */
95 for(buf++; buf < end; buf++) {
96 int v = *buf;
97 int nline = (flags & XER_F_CANONICAL)
98 ?0:((((buf - st->buf) - 1) % 16) == 0);
99 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;
111 _ASN_CALLBACK(scratch, p - scratch);
112
113 if(buf < end + 1) {
114 int v = *buf;
115 int mbit = st->buf[0]; /* bits to skip from the right */
116 int i;
117 for(i = 7; i >= mbit; i--)
118 *p++ = (v & (1 << i)) ? '1' : '0';
119 er.encoded += p - scratch;
120 _ASN_CALLBACK(scratch, p - scratch);
121 }
122
123 return er;
124}
125
126
vlmfa67ddc2004-06-03 03:38:44 +0000127/*
128 * BIT STRING specific contents printer.
129 */
130int
131BIT_STRING_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
132 asn_app_consume_bytes_f *cb, void *app_key) {
vlm1ff928d2004-08-11 08:10:13 +0000133 static const char *h2c = "0123456789ABCDEF";
vlmfa67ddc2004-06-03 03:38:44 +0000134 char scratch[64];
vlmda674682004-08-11 09:07:36 +0000135 const BIT_STRING_t *st = (const BIT_STRING_t *)sptr;
vlmfa67ddc2004-06-03 03:38:44 +0000136 uint8_t *buf;
137 uint8_t *end;
138 char *p = scratch;
139
vlmb42843a2004-06-05 08:17:50 +0000140 (void)td; /* Unused argument */
141
vlmfa67ddc2004-06-03 03:38:44 +0000142 if(!st || !st->buf) return cb("<absent>", 8, app_key);
143
144 ilevel += 4;
145 buf = st->buf;
146 end = buf + st->size;
147
148 /*
149 * Hexadecimal dump.
150 */
151 for(buf++; buf < end; buf++) {
vlm6d8518c2004-07-22 16:23:33 +0000152 if(((buf - st->buf) - 1) % 16 == 0 && (st->size > 16)) {
vlmfa67ddc2004-06-03 03:38:44 +0000153 int i;
154 /* Indentation */
155 if(cb("\n", 1, app_key)) return -1;
156 for(i = 0; i < ilevel; i++) cb(" ", 1, app_key);
157 /* Dump the string */
158 if(cb(scratch, p - scratch, app_key)) return -1;
159 p = scratch;
160 }
161 *p++ = h2c[*buf >> 4];
162 *p++ = h2c[*buf & 0x0F];
163 *p++ = 0x20;
164 }
vlm39ba4c42004-09-22 16:06:28 +0000165 if(p > scratch) p--; /* Eat the tailing space */
vlmfa67ddc2004-06-03 03:38:44 +0000166
167 /* Dump the incomplete 16-bytes row */
168 return cb(scratch, p - scratch, app_key);
169}
170