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