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 | */ |
| 5 | #include <BIT_STRING.h> |
| 6 | |
| 7 | /* |
| 8 | * BIT STRING basic type description. |
| 9 | */ |
| 10 | static ber_tlv_tag_t asn1_DEF_BIT_STRING_tags[] = { |
| 11 | (ASN_TAG_CLASS_UNIVERSAL | (3 << 2)) |
| 12 | }; |
| 13 | asn1_TYPE_descriptor_t asn1_DEF_BIT_STRING = { |
| 14 | "BIT STRING", |
| 15 | BIT_STRING_constraint, |
| 16 | OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */ |
| 17 | OCTET_STRING_encode_der, /* Implemented in terms of OCTET STRING */ |
| 18 | BIT_STRING_print, |
| 19 | OCTET_STRING_free, /* Implemented in terms of OCTET STRING */ |
| 20 | 0, /* Use generic outmost tag fetcher */ |
| 21 | asn1_DEF_BIT_STRING_tags, |
| 22 | sizeof(asn1_DEF_BIT_STRING_tags) |
| 23 | / sizeof(asn1_DEF_BIT_STRING_tags[0]), |
| 24 | 1, /* Single UNIVERSAL tag may be implicitly overriden */ |
| 25 | -1, /* Both ways are fine */ |
| 26 | (void *)-1 /* Special indicator that this is a BIT STRING */ |
| 27 | }; |
| 28 | |
| 29 | /* |
| 30 | * BIT STRING generic constraint. |
| 31 | */ |
| 32 | int |
| 33 | BIT_STRING_constraint(asn1_TYPE_descriptor_t *td, const void *sptr, |
| 34 | asn_app_consume_bytes_f *app_errlog, void *app_key) { |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 35 | const BIT_STRING_t *st = (const BIT_STRING_t *)sptr; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 36 | |
| 37 | if(st && st->buf) { |
| 38 | if(st->size) { |
| 39 | if(st->size == 1 && st->buf[0] != 0) { |
vlm | e3f0f28 | 2004-08-11 09:44:13 +0000 | [diff] [blame] | 40 | _ASN_ERRLOG(app_errlog, app_key, |
| 41 | "%s: invalid padding byte", |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 42 | td->name); |
| 43 | return -1; |
| 44 | } |
| 45 | } else { |
vlm | e3f0f28 | 2004-08-11 09:44:13 +0000 | [diff] [blame] | 46 | _ASN_ERRLOG(app_errlog, app_key, |
| 47 | "%s: no padding byte", td->name); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 48 | return -1; |
| 49 | } |
| 50 | } else { |
vlm | e3f0f28 | 2004-08-11 09:44:13 +0000 | [diff] [blame] | 51 | _ASN_ERRLOG(app_errlog, app_key, |
| 52 | "%s: value not given", td->name); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 53 | return -1; |
| 54 | } |
| 55 | |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | /* |
| 60 | * BIT STRING specific contents printer. |
| 61 | */ |
| 62 | int |
| 63 | BIT_STRING_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel, |
| 64 | asn_app_consume_bytes_f *cb, void *app_key) { |
vlm | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 65 | static const char *h2c = "0123456789ABCDEF"; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 66 | char scratch[64]; |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 67 | const BIT_STRING_t *st = (const BIT_STRING_t *)sptr; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 68 | uint8_t *buf; |
| 69 | uint8_t *end; |
| 70 | char *p = scratch; |
| 71 | |
vlm | b42843a | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 72 | (void)td; /* Unused argument */ |
| 73 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 74 | if(!st || !st->buf) return cb("<absent>", 8, app_key); |
| 75 | |
| 76 | ilevel += 4; |
| 77 | buf = st->buf; |
| 78 | end = buf + st->size; |
| 79 | |
| 80 | /* |
| 81 | * Hexadecimal dump. |
| 82 | */ |
| 83 | for(buf++; buf < end; buf++) { |
vlm | 6d8518c | 2004-07-22 16:23:33 +0000 | [diff] [blame] | 84 | if(((buf - st->buf) - 1) % 16 == 0 && (st->size > 16)) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 85 | int i; |
| 86 | /* Indentation */ |
| 87 | if(cb("\n", 1, app_key)) return -1; |
| 88 | for(i = 0; i < ilevel; i++) cb(" ", 1, app_key); |
| 89 | /* Dump the string */ |
| 90 | if(cb(scratch, p - scratch, app_key)) return -1; |
| 91 | p = scratch; |
| 92 | } |
| 93 | *p++ = h2c[*buf >> 4]; |
| 94 | *p++ = h2c[*buf & 0x0F]; |
| 95 | *p++ = 0x20; |
| 96 | } |
| 97 | |
| 98 | /* Dump the incomplete 16-bytes row */ |
| 99 | return cb(scratch, p - scratch, app_key); |
| 100 | } |
| 101 | |