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