1 /*****************************************************************************
3 # Copyright 2019 AT&T Intellectual Property *
5 # Licensed under the Apache License, Version 2.0 (the "License"); *
6 # you may not use this file except in compliance with the License. *
7 # You may obtain a copy of the License at *
9 # http://www.apache.org/licenses/LICENSE-2.0 *
11 # Unless required by applicable law or agreed to in writing, software *
12 # distributed under the License is distributed on an "AS IS" BASIS, *
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
14 # See the License for the specific language governing permissions and *
15 # limitations under the License. *
17 ******************************************************************************/
20 * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
21 * Redistribution and modifications are permitted subject to BSD license.
23 #include <asn_internal.h>
24 #include <ber_tlv_tag.h>
28 ber_fetch_tag(const void *ptr, size_t size, ber_tlv_tag_t *tag_r) {
36 val = *(const uint8_t *)ptr;
38 if((val &= 0x1F) != 0x1F) {
40 * Simple form: everything encoded in a single octet.
41 * Tag Class is encoded using two least significant bits.
43 *tag_r = (val << 2) | tclass;
48 * Each octet contains 7 bits of useful information.
49 * The MSB is 0 if it is the last octet of the tag.
51 for(val = 0, ptr = ((const char *)ptr) + 1, skipped = 2;
53 ptr = ((const char *)ptr) + 1, skipped++) {
54 unsigned int oct = *(const uint8_t *)ptr;
56 val = (val << 7) | (oct & 0x7F);
58 * Make sure there are at least 9 bits spare
59 * at the MS side of a value.
61 if(val >> ((8 * sizeof(val)) - 9)) {
63 * We would not be able to accomodate
69 val = (val << 7) | oct;
70 *tag_r = (val << 2) | tclass;
75 return 0; /* Want more */
80 ber_tlv_tag_fwrite(ber_tlv_tag_t tag, FILE *f) {
81 char buf[sizeof("[APPLICATION ]") + 32];
84 ret = ber_tlv_tag_snprint(tag, buf, sizeof(buf));
85 if(ret >= (ssize_t)sizeof(buf) || ret < 2) {
90 return fwrite(buf, 1, ret, f);
94 ber_tlv_tag_snprint(ber_tlv_tag_t tag, char *buf, size_t size) {
99 case ASN_TAG_CLASS_UNIVERSAL: type = "UNIVERSAL "; break;
100 case ASN_TAG_CLASS_APPLICATION: type = "APPLICATION "; break;
101 case ASN_TAG_CLASS_CONTEXT: type = ""; break;
102 case ASN_TAG_CLASS_PRIVATE: type = "PRIVATE "; break;
105 ret = snprintf(buf, size, "[%s%u]", type, ((unsigned)tag) >> 2);
106 if(ret <= 0 && size) buf[0] = '\0'; /* against broken libc's */
112 ber_tlv_tag_string(ber_tlv_tag_t tag) {
113 static char buf[sizeof("[APPLICATION ]") + 32];
115 (void)ber_tlv_tag_snprint(tag, buf, sizeof(buf));
122 ber_tlv_tag_serialize(ber_tlv_tag_t tag, void *bufp, size_t size) {
123 int tclass = BER_TAG_CLASS(tag);
124 ber_tlv_tag_t tval = BER_TAG_VALUE(tag);
125 uint8_t *buf = (uint8_t *)bufp;
127 size_t required_size;
131 /* Encoded in 1 octet */
132 if(size) buf[0] = (tclass << 6) | tval;
135 *buf++ = (tclass << 6) | 0x1F;
140 * Compute the size of the subsequent bytes.
142 for(required_size = 1, i = 7; i < 8 * sizeof(tval); i += 7) {
149 if(size < required_size)
150 return required_size + 1;
153 * Fill in the buffer, space permitting.
155 end = buf + required_size - 1;
156 for(i -= 7; buf < end; i -= 7, buf++)
157 *buf = 0x80 | ((tval >> i) & 0x7F);
158 *buf = (tval & 0x7F); /* Last octet without high bit */
160 return required_size + 1;