Harald Welte | d523a69 | 2015-08-30 23:11:19 +0200 | [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 <asn_internal.h> |
| 6 | #include <ber_tlv_tag.h> |
| 7 | #include <errno.h> |
| 8 | |
| 9 | ssize_t |
| 10 | ber_fetch_tag(const void *ptr, size_t size, ber_tlv_tag_t *tag_r) { |
| 11 | ber_tlv_tag_t val; |
| 12 | ber_tlv_tag_t tclass; |
| 13 | size_t skipped; |
| 14 | |
| 15 | if(size == 0) |
| 16 | return 0; |
| 17 | |
| 18 | val = *(const uint8_t *)ptr; |
| 19 | tclass = (val >> 6); |
| 20 | if((val &= 0x1F) != 0x1F) { |
| 21 | /* |
| 22 | * Simple form: everything encoded in a single octet. |
| 23 | * Tag Class is encoded using two least significant bits. |
| 24 | */ |
| 25 | *tag_r = (val << 2) | tclass; |
| 26 | return 1; |
| 27 | } |
| 28 | |
| 29 | /* |
| 30 | * Each octet contains 7 bits of useful information. |
| 31 | * The MSB is 0 if it is the last octet of the tag. |
| 32 | */ |
| 33 | for(val = 0, ptr = ((const char *)ptr) + 1, skipped = 2; |
| 34 | skipped <= size; |
| 35 | ptr = ((const char *)ptr) + 1, skipped++) { |
| 36 | unsigned int oct = *(const uint8_t *)ptr; |
| 37 | if(oct & 0x80) { |
| 38 | val = (val << 7) | (oct & 0x7F); |
| 39 | /* |
| 40 | * Make sure there are at least 9 bits spare |
| 41 | * at the MS side of a value. |
| 42 | */ |
| 43 | if(val >> ((8 * sizeof(val)) - 9)) { |
| 44 | /* |
| 45 | * We would not be able to accomodate |
| 46 | * any more tag bits. |
| 47 | */ |
| 48 | return -1; |
| 49 | } |
| 50 | } else { |
| 51 | val = (val << 7) | oct; |
| 52 | *tag_r = (val << 2) | tclass; |
| 53 | return skipped; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | return 0; /* Want more */ |
| 58 | } |
| 59 | |
| 60 | |
| 61 | ssize_t |
| 62 | ber_tlv_tag_fwrite(ber_tlv_tag_t tag, FILE *f) { |
| 63 | char buf[sizeof("[APPLICATION ]") + 32]; |
| 64 | ssize_t ret; |
| 65 | |
| 66 | ret = ber_tlv_tag_snprint(tag, buf, sizeof(buf)); |
| 67 | if(ret >= (ssize_t)sizeof(buf) || ret < 2) { |
| 68 | errno = EPERM; |
| 69 | return -1; |
| 70 | } |
| 71 | |
| 72 | return fwrite(buf, 1, ret, f); |
| 73 | } |
| 74 | |
| 75 | ssize_t |
| 76 | ber_tlv_tag_snprint(ber_tlv_tag_t tag, char *buf, size_t size) { |
| 77 | char *type = 0; |
| 78 | int ret; |
| 79 | |
| 80 | switch(tag & 0x3) { |
| 81 | case ASN_TAG_CLASS_UNIVERSAL: type = "UNIVERSAL "; break; |
| 82 | case ASN_TAG_CLASS_APPLICATION: type = "APPLICATION "; break; |
| 83 | case ASN_TAG_CLASS_CONTEXT: type = ""; break; |
| 84 | case ASN_TAG_CLASS_PRIVATE: type = "PRIVATE "; break; |
| 85 | } |
| 86 | |
| 87 | ret = snprintf(buf, size, "[%s%u]", type, ((unsigned)tag) >> 2); |
| 88 | if(ret <= 0 && size) buf[0] = '\0'; /* against broken libc's */ |
| 89 | |
| 90 | return ret; |
| 91 | } |
| 92 | |
| 93 | char * |
| 94 | ber_tlv_tag_string(ber_tlv_tag_t tag) { |
| 95 | static char buf[sizeof("[APPLICATION ]") + 32]; |
| 96 | |
| 97 | (void)ber_tlv_tag_snprint(tag, buf, sizeof(buf)); |
| 98 | |
| 99 | return buf; |
| 100 | } |
| 101 | |
| 102 | |
| 103 | size_t |
| 104 | ber_tlv_tag_serialize(ber_tlv_tag_t tag, void *bufp, size_t size) { |
| 105 | int tclass = BER_TAG_CLASS(tag); |
| 106 | ber_tlv_tag_t tval = BER_TAG_VALUE(tag); |
| 107 | uint8_t *buf = (uint8_t *)bufp; |
| 108 | uint8_t *end; |
| 109 | size_t required_size; |
| 110 | size_t i; |
| 111 | |
| 112 | if(tval <= 30) { |
| 113 | /* Encoded in 1 octet */ |
| 114 | if(size) buf[0] = (tclass << 6) | tval; |
| 115 | return 1; |
| 116 | } else if(size) { |
| 117 | *buf++ = (tclass << 6) | 0x1F; |
| 118 | size--; |
| 119 | } |
| 120 | |
| 121 | /* |
| 122 | * Compute the size of the subsequent bytes. |
| 123 | */ |
| 124 | for(required_size = 1, i = 7; i < 8 * sizeof(tval); i += 7) { |
| 125 | if(tval >> i) |
| 126 | required_size++; |
| 127 | else |
| 128 | break; |
| 129 | } |
| 130 | |
| 131 | if(size < required_size) |
| 132 | return required_size + 1; |
| 133 | |
| 134 | /* |
| 135 | * Fill in the buffer, space permitting. |
| 136 | */ |
| 137 | end = buf + required_size - 1; |
| 138 | for(i -= 7; buf < end; i -= 7, buf++) |
| 139 | *buf = 0x80 | ((tval >> i) & 0x7F); |
| 140 | *buf = (tval & 0x7F); /* Last octet without high bit */ |
| 141 | |
| 142 | return required_size + 1; |
| 143 | } |
| 144 | |