blob: 22152ffdcfed96ab706fb21fba5be9fedc7698d3 [file] [log] [blame]
vlmfa67ddc2004-06-03 03:38:44 +00001/*-
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#ifndef _BER_TLV_TAG_H_
6#define _BER_TLV_TAG_H_
7
8enum asn_tag_class {
9 ASN_TAG_CLASS_UNIVERSAL = 0, /* 0b00 */
10 ASN_TAG_CLASS_APPLICATION = 1, /* 0b01 */
11 ASN_TAG_CLASS_CONTEXT = 2, /* 0b10 */
12 ASN_TAG_CLASS_PRIVATE = 3, /* 0b11 */
13};
14typedef unsigned ber_tlv_tag_t; /* BER TAG from Tag-Length-Value */
15
16/*
17 * Tag class is encoded together with tag value for optimization purposes.
18 */
19#define BER_TAG_CLASS(tag) ((tag) & 0x3)
20#define BER_TAG_VALUE(tag) ((tag) >> 2)
21#define BER_TLV_CONSTRUCTED(tagptr) (((*(uint8_t *)tagptr) & 0x20)?1:0)
22
23#define BER_TAGS_EQUAL(tag1, tag2) ((tag1) == (tag2))
24
25/*
26 * Several functions for printing the TAG in the canonical form
27 * (i.e. "[PRIVATE 0]").
28 * Return values correspond to their libc counterparts (if any).
29 */
30ssize_t ber_tlv_tag_snprint(ber_tlv_tag_t tag, char *buf, size_t buflen);
31ssize_t ber_tlv_tag_fwrite(ber_tlv_tag_t tag, FILE *);
32char *ber_tlv_tag_string(ber_tlv_tag_t tag);
33
34
35/*
36 * This function tries to fetch the tag from the input stream.
37 * RETURN VALUES:
38 * 0: More data expected than bufptr contains.
39 * -1: Fatal error deciphering tag.
40 * >0: Number of bytes used from bufptr. tag_r will contain the tag.
41 */
42ssize_t ber_fetch_tag(void *bufptr, size_t size, ber_tlv_tag_t *tag_r);
43
44/*
vlm044e8d42004-08-19 13:27:34 +000045 * This function serializes the tag (T from TLV) in BER format.
vlmfa67ddc2004-06-03 03:38:44 +000046 * It always return number of bytes necessary to represent the tag,
47 * it is a caller's responsibility to check the return value
48 * against the supplied buffer's size.
49 */
vlm044e8d42004-08-19 13:27:34 +000050ssize_t ber_tlv_tag_serialize(ber_tlv_tag_t tag, void *bufptr, size_t size);
vlmfa67ddc2004-06-03 03:38:44 +000051
52#endif /* _BER_TLV_TAG_H_ */