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> |
| 7 | |
| 8 | /* |
| 9 | * BIT STRING basic type description. |
| 10 | */ |
| 11 | static ber_tlv_tag_t asn1_DEF_BIT_STRING_tags[] = { |
| 12 | (ASN_TAG_CLASS_UNIVERSAL | (3 << 2)) |
| 13 | }; |
| 14 | asn1_TYPE_descriptor_t asn1_DEF_BIT_STRING = { |
| 15 | "BIT STRING", |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 16 | OCTET_STRING_free, /* Implemented in terms of OCTET STRING */ |
| 17 | BIT_STRING_print, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 18 | 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 */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 21 | 0, /* Not implemented yet */ |
| 22 | BIT_STRING_encode_xer, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 23 | 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]), |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 27 | asn1_DEF_BIT_STRING_tags, /* Same as above */ |
| 28 | sizeof(asn1_DEF_BIT_STRING_tags) |
| 29 | / sizeof(asn1_DEF_BIT_STRING_tags[0]), |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 30 | -1, /* Both ways are fine */ |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 31 | 0, 0, /* No members */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 32 | (void *)-1 /* Special indicator that this is a BIT STRING */ |
| 33 | }; |
| 34 | |
| 35 | /* |
| 36 | * BIT STRING generic constraint. |
| 37 | */ |
| 38 | int |
| 39 | BIT_STRING_constraint(asn1_TYPE_descriptor_t *td, const void *sptr, |
| 40 | asn_app_consume_bytes_f *app_errlog, void *app_key) { |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 41 | const BIT_STRING_t *st = (const BIT_STRING_t *)sptr; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 42 | |
| 43 | if(st && st->buf) { |
| 44 | if(st->size) { |
| 45 | if(st->size == 1 && st->buf[0] != 0) { |
Lev Walkin | ba4e518 | 2004-08-11 09:44:13 +0000 | [diff] [blame] | 46 | _ASN_ERRLOG(app_errlog, app_key, |
Lev Walkin | 16835b6 | 2004-08-22 13:47:59 +0000 | [diff] [blame] | 47 | "%s: invalid padding byte (%s:%d)", |
| 48 | td->name, __FILE__, __LINE__); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 49 | return -1; |
| 50 | } |
| 51 | } else { |
Lev Walkin | ba4e518 | 2004-08-11 09:44:13 +0000 | [diff] [blame] | 52 | _ASN_ERRLOG(app_errlog, app_key, |
Lev Walkin | 16835b6 | 2004-08-22 13:47:59 +0000 | [diff] [blame] | 53 | "%s: no padding byte (%s:%d)", |
| 54 | td->name, __FILE__, __LINE__); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 55 | return -1; |
| 56 | } |
| 57 | } else { |
Lev Walkin | ba4e518 | 2004-08-11 09:44:13 +0000 | [diff] [blame] | 58 | _ASN_ERRLOG(app_errlog, app_key, |
Lev Walkin | 16835b6 | 2004-08-22 13:47:59 +0000 | [diff] [blame] | 59 | "%s: value not given (%s:%d)", |
| 60 | td->name, __FILE__, __LINE__); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 61 | return -1; |
| 62 | } |
| 63 | |
| 64 | return 0; |
| 65 | } |
| 66 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 67 | static char *_bit_pattern[16] = { |
| 68 | "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", |
| 69 | "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111" |
| 70 | }; |
| 71 | |
| 72 | asn_enc_rval_t |
| 73 | BIT_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; |
Lev Walkin | cc6a910 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 81 | int xcan = (flags & XER_F_CANONICAL); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 82 | uint8_t *buf; |
| 83 | uint8_t *end; |
| 84 | |
| 85 | if(!st || !st->buf) |
| 86 | _ASN_ENCODE_FAILED; |
| 87 | |
| 88 | er.encoded = 0; |
| 89 | |
| 90 | buf = st->buf; |
| 91 | end = buf + st->size - 1; /* Last byte is special */ |
| 92 | |
| 93 | /* |
| 94 | * Binary dump |
| 95 | */ |
| 96 | for(buf++; buf < end; buf++) { |
| 97 | int v = *buf; |
Lev Walkin | cc6a910 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 98 | int nline = xcan?0:((((buf - st->buf) - 1) % 8) == 0); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 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 | |
Lev Walkin | cc6a910 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 110 | if(!xcan && (((buf - st->buf) - 1) % 8) == 0) |
| 111 | _i_ASN_TEXT_INDENT(1, ilevel); |
Lev Walkin | 938c6fe | 2004-09-24 20:56:25 +0000 | [diff] [blame] | 112 | er.encoded += p - scratch; |
| 113 | _ASN_CALLBACK(scratch, p - scratch); |
| 114 | p = scratch; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 115 | |
Lev Walkin | 938c6fe | 2004-09-24 20:56:25 +0000 | [diff] [blame] | 116 | if(buf == end) { |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 117 | int v = *buf; |
| 118 | int mbit = st->buf[0]; /* bits to skip from the right */ |
| 119 | int i; |
| 120 | for(i = 7; i >= mbit; i--) |
| 121 | *p++ = (v & (1 << i)) ? '1' : '0'; |
| 122 | er.encoded += p - scratch; |
| 123 | _ASN_CALLBACK(scratch, p - scratch); |
| 124 | } |
| 125 | |
Lev Walkin | 938c6fe | 2004-09-24 20:56:25 +0000 | [diff] [blame] | 126 | if(!xcan) _i_ASN_TEXT_INDENT(1, ilevel - 1); |
Lev Walkin | cc6a910 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 127 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 128 | return er; |
| 129 | } |
| 130 | |
| 131 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 132 | /* |
| 133 | * BIT STRING specific contents printer. |
| 134 | */ |
| 135 | int |
| 136 | BIT_STRING_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel, |
| 137 | asn_app_consume_bytes_f *cb, void *app_key) { |
Lev Walkin | 4d9528c | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 138 | static const char *h2c = "0123456789ABCDEF"; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 139 | char scratch[64]; |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 140 | const BIT_STRING_t *st = (const BIT_STRING_t *)sptr; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 141 | uint8_t *buf; |
| 142 | uint8_t *end; |
| 143 | char *p = scratch; |
| 144 | |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 145 | (void)td; /* Unused argument */ |
| 146 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 147 | if(!st || !st->buf) return cb("<absent>", 8, app_key); |
| 148 | |
| 149 | ilevel += 4; |
| 150 | buf = st->buf; |
| 151 | end = buf + st->size; |
| 152 | |
| 153 | /* |
| 154 | * Hexadecimal dump. |
| 155 | */ |
| 156 | for(buf++; buf < end; buf++) { |
Lev Walkin | cc6a910 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 157 | if(((buf - st->buf) - 1) % 16 == 0 && (st->size > 17) |
| 158 | && buf != st->buf+1) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 159 | int i; |
| 160 | /* Indentation */ |
| 161 | if(cb("\n", 1, app_key)) return -1; |
| 162 | for(i = 0; i < ilevel; i++) cb(" ", 1, app_key); |
| 163 | /* Dump the string */ |
| 164 | if(cb(scratch, p - scratch, app_key)) return -1; |
| 165 | p = scratch; |
| 166 | } |
| 167 | *p++ = h2c[*buf >> 4]; |
| 168 | *p++ = h2c[*buf & 0x0F]; |
| 169 | *p++ = 0x20; |
| 170 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 171 | |
Lev Walkin | cc6a910 | 2004-09-23 22:06:26 +0000 | [diff] [blame] | 172 | if(p > scratch) { |
| 173 | p--; /* Eat the tailing space */ |
| 174 | |
| 175 | if((st->size > 17)) { |
| 176 | int i; |
| 177 | if(cb("\n", 1, app_key)) return -1; |
| 178 | for(i = 0; i < ilevel; i++) cb(" ", 1, app_key); |
| 179 | } |
| 180 | |
| 181 | /* Dump the incomplete 16-bytes row */ |
| 182 | if(cb(scratch, p - scratch, app_key)) |
| 183 | return -1; |
| 184 | } |
| 185 | |
| 186 | return 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 187 | } |
| 188 | |