vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | /*- |
| 2 | * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved. |
| 3 | * Redistribution and modifications are permitted subject to BSD license. |
| 4 | */ |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 5 | #include <asn_internal.h> |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 6 | #include <BIT_STRING.h> |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 7 | #include <asn_internal.h> |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 8 | |
| 9 | /* |
| 10 | * BIT STRING basic type description. |
| 11 | */ |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 12 | static ber_tlv_tag_t asn_DEF_BIT_STRING_tags[] = { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 13 | (ASN_TAG_CLASS_UNIVERSAL | (3 << 2)) |
| 14 | }; |
vlm | 0f1ab76 | 2004-10-12 05:57:23 +0000 | [diff] [blame] | 15 | static 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 | }; |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 20 | asn_TYPE_descriptor_t asn_DEF_BIT_STRING = { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 21 | "BIT STRING", |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 22 | OCTET_STRING_free, /* Implemented in terms of OCTET STRING */ |
| 23 | BIT_STRING_print, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 24 | BIT_STRING_constraint, |
| 25 | OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */ |
| 26 | OCTET_STRING_encode_der, /* Implemented in terms of OCTET STRING */ |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 27 | 0, /* Not implemented yet */ |
| 28 | BIT_STRING_encode_xer, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 29 | 0, /* Use generic outmost tag fetcher */ |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 30 | asn_DEF_BIT_STRING_tags, |
| 31 | sizeof(asn_DEF_BIT_STRING_tags) |
| 32 | / sizeof(asn_DEF_BIT_STRING_tags[0]), |
| 33 | asn_DEF_BIT_STRING_tags, /* Same as above */ |
| 34 | sizeof(asn_DEF_BIT_STRING_tags) |
| 35 | / sizeof(asn_DEF_BIT_STRING_tags[0]), |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 36 | 0, 0, /* No members */ |
vlm | 0f1ab76 | 2004-10-12 05:57:23 +0000 | [diff] [blame] | 37 | &asn_DEF_BIT_STRING_specs |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | /* |
| 41 | * BIT STRING generic constraint. |
| 42 | */ |
| 43 | int |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 44 | BIT_STRING_constraint(asn_TYPE_descriptor_t *td, const void *sptr, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 45 | asn_app_consume_bytes_f *app_errlog, void *app_key) { |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 46 | const BIT_STRING_t *st = (const BIT_STRING_t *)sptr; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 47 | |
| 48 | if(st && st->buf) { |
vlm | 0f1ab76 | 2004-10-12 05:57:23 +0000 | [diff] [blame] | 49 | if(st->size == 1 && st->bits_unused) { |
vlm | e3f0f28 | 2004-08-11 09:44:13 +0000 | [diff] [blame] | 50 | _ASN_ERRLOG(app_errlog, app_key, |
vlm | 0f1ab76 | 2004-10-12 05:57:23 +0000 | [diff] [blame] | 51 | "%s: invalid padding byte (%s:%d)", |
vlm | 758530a | 2004-08-22 13:47:59 +0000 | [diff] [blame] | 52 | td->name, __FILE__, __LINE__); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 53 | return -1; |
| 54 | } |
| 55 | } else { |
vlm | e3f0f28 | 2004-08-11 09:44:13 +0000 | [diff] [blame] | 56 | _ASN_ERRLOG(app_errlog, app_key, |
vlm | 758530a | 2004-08-22 13:47:59 +0000 | [diff] [blame] | 57 | "%s: value not given (%s:%d)", |
| 58 | td->name, __FILE__, __LINE__); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 59 | return -1; |
| 60 | } |
| 61 | |
| 62 | return 0; |
| 63 | } |
| 64 | |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 65 | static char *_bit_pattern[16] = { |
| 66 | "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", |
| 67 | "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111" |
| 68 | }; |
| 69 | |
| 70 | asn_enc_rval_t |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 71 | BIT_STRING_encode_xer(asn_TYPE_descriptor_t *td, void *sptr, |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 72 | int ilevel, enum xer_encoder_flags_e flags, |
| 73 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 74 | asn_enc_rval_t er; |
| 75 | char scratch[128]; |
| 76 | char *p = scratch; |
| 77 | char *scend = scratch + (sizeof(scratch) - 10); |
| 78 | const BIT_STRING_t *st = (const BIT_STRING_t *)sptr; |
vlm | ef1b4c0 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 79 | int xcan = (flags & XER_F_CANONICAL); |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 80 | uint8_t *buf; |
| 81 | uint8_t *end; |
| 82 | |
| 83 | if(!st || !st->buf) |
| 84 | _ASN_ENCODE_FAILED; |
| 85 | |
| 86 | er.encoded = 0; |
| 87 | |
| 88 | buf = st->buf; |
| 89 | end = buf + st->size - 1; /* Last byte is special */ |
| 90 | |
| 91 | /* |
| 92 | * Binary dump |
| 93 | */ |
vlm | 0f1ab76 | 2004-10-12 05:57:23 +0000 | [diff] [blame] | 94 | for(; buf < end; buf++) { |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 95 | int v = *buf; |
vlm | 0f1ab76 | 2004-10-12 05:57:23 +0000 | [diff] [blame] | 96 | int nline = xcan?0:(((buf - st->buf) % 8) == 0); |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 97 | if(p >= scend || nline) { |
| 98 | er.encoded += p - scratch; |
| 99 | _ASN_CALLBACK(scratch, p - scratch); |
| 100 | p = scratch; |
| 101 | if(nline) _i_ASN_TEXT_INDENT(1, ilevel); |
| 102 | } |
| 103 | memcpy(p + 0, _bit_pattern[v >> 4], 4); |
| 104 | memcpy(p + 4, _bit_pattern[v & 0x0f], 4); |
| 105 | p += 8; |
| 106 | } |
| 107 | |
vlm | 0f1ab76 | 2004-10-12 05:57:23 +0000 | [diff] [blame] | 108 | if(!xcan && ((buf - st->buf) % 8) == 0) |
vlm | ef1b4c0 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 109 | _i_ASN_TEXT_INDENT(1, ilevel); |
vlm | 1827a1c | 2004-09-24 20:56:25 +0000 | [diff] [blame] | 110 | er.encoded += p - scratch; |
| 111 | _ASN_CALLBACK(scratch, p - scratch); |
| 112 | p = scratch; |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 113 | |
vlm | 1827a1c | 2004-09-24 20:56:25 +0000 | [diff] [blame] | 114 | if(buf == end) { |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 115 | int v = *buf; |
vlm | 0f1ab76 | 2004-10-12 05:57:23 +0000 | [diff] [blame] | 116 | int ubits = st->bits_unused; |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 117 | int i; |
vlm | 0f1ab76 | 2004-10-12 05:57:23 +0000 | [diff] [blame] | 118 | for(i = 7; i >= ubits; i--) |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 119 | *p++ = (v & (1 << i)) ? 0x31 : 0x30; |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 120 | er.encoded += p - scratch; |
| 121 | _ASN_CALLBACK(scratch, p - scratch); |
| 122 | } |
| 123 | |
vlm | 1827a1c | 2004-09-24 20:56:25 +0000 | [diff] [blame] | 124 | if(!xcan) _i_ASN_TEXT_INDENT(1, ilevel - 1); |
vlm | ef1b4c0 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 125 | |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 126 | return er; |
vlm | 84d551b | 2004-10-03 09:13:02 +0000 | [diff] [blame] | 127 | cb_failed: |
| 128 | _ASN_ENCODE_FAILED; |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 132 | /* |
| 133 | * BIT STRING specific contents printer. |
| 134 | */ |
| 135 | int |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 136 | BIT_STRING_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 137 | asn_app_consume_bytes_f *cb, void *app_key) { |
vlm | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 138 | static const char *h2c = "0123456789ABCDEF"; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 139 | char scratch[64]; |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 140 | const BIT_STRING_t *st = (const BIT_STRING_t *)sptr; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 141 | uint8_t *buf; |
| 142 | uint8_t *end; |
| 143 | char *p = scratch; |
| 144 | |
vlm | b42843a | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 145 | (void)td; /* Unused argument */ |
| 146 | |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 147 | if(!st || !st->buf) |
| 148 | return (cb("<absent>", 8, app_key) < 0) ? -1 : 0; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 149 | |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 150 | ilevel++; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 151 | buf = st->buf; |
| 152 | end = buf + st->size; |
| 153 | |
| 154 | /* |
| 155 | * Hexadecimal dump. |
| 156 | */ |
vlm | 0f1ab76 | 2004-10-12 05:57:23 +0000 | [diff] [blame] | 157 | for(; buf < end; buf++) { |
| 158 | if((buf - st->buf) % 16 == 0 && (st->size > 16) |
| 159 | && buf != st->buf) { |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 160 | _i_INDENT(1); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 161 | /* Dump the string */ |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 162 | if(cb(scratch, p - scratch, app_key) < 0) return -1; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 163 | p = scratch; |
| 164 | } |
| 165 | *p++ = h2c[*buf >> 4]; |
| 166 | *p++ = h2c[*buf & 0x0F]; |
| 167 | *p++ = 0x20; |
| 168 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 169 | |
vlm | ef1b4c0 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 170 | if(p > scratch) { |
| 171 | p--; /* Eat the tailing space */ |
| 172 | |
vlm | 0f1ab76 | 2004-10-12 05:57:23 +0000 | [diff] [blame] | 173 | if((st->size > 16)) { |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 174 | _i_INDENT(1); |
vlm | ef1b4c0 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | /* Dump the incomplete 16-bytes row */ |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 178 | if(cb(scratch, p - scratch, app_key) < 0) |
vlm | ef1b4c0 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 179 | return -1; |
| 180 | } |
| 181 | |
| 182 | return 0; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 183 | } |
| 184 | |