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-2017 Lev Walkin <vlm@lionet.info>. All rights reserved.
21 * Redistribution and modifications are permitted subject to BSD license.
23 #ifndef _BER_TLV_TAG_H_
24 #define _BER_TLV_TAG_H_
31 ASN_TAG_CLASS_UNIVERSAL = 0, /* 0b00 */
32 ASN_TAG_CLASS_APPLICATION = 1, /* 0b01 */
33 ASN_TAG_CLASS_CONTEXT = 2, /* 0b10 */
34 ASN_TAG_CLASS_PRIVATE = 3 /* 0b11 */
36 typedef unsigned ber_tlv_tag_t; /* BER TAG from Tag-Length-Value */
39 * Tag class is encoded together with tag value for optimization purposes.
41 #define BER_TAG_CLASS(tag) ((tag) & 0x3)
42 #define BER_TAG_VALUE(tag) ((tag) >> 2)
43 #define BER_TLV_CONSTRUCTED(tagptr) (((*(const uint8_t *)tagptr)&0x20)?1:0)
45 #define BER_TAGS_EQUAL(tag1, tag2) ((tag1) == (tag2))
48 * Several functions for printing the TAG in the canonical form
49 * (i.e. "[PRIVATE 0]").
50 * Return values correspond to their libc counterparts (if any).
52 ssize_t ber_tlv_tag_snprint(ber_tlv_tag_t tag, char *buf, size_t buflen);
53 ssize_t ber_tlv_tag_fwrite(ber_tlv_tag_t tag, FILE *);
54 char *ber_tlv_tag_string(ber_tlv_tag_t tag);
58 * This function tries to fetch the tag from the input stream.
60 * 0: More data expected than bufptr contains.
61 * -1: Fatal error deciphering tag.
62 * >0: Number of bytes used from bufptr. tag_r will contain the tag.
64 ssize_t ber_fetch_tag(const void *bufptr, size_t size, ber_tlv_tag_t *tag_r);
67 * This function serializes the tag (T from TLV) in BER format.
68 * It always returns number of bytes necessary to represent the tag,
69 * it is a caller's responsibility to check the return value
70 * against the supplied buffer's size.
72 size_t ber_tlv_tag_serialize(ber_tlv_tag_t tag, void *bufptr, size_t size);
78 #endif /* _BER_TLV_TAG_H_ */