blob: 51c9a081ef3ca5dd6a6d7ade719d642ba88c1c52 [file] [log] [blame]
Lev Walkinf15320b2004-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 */
Lev Walkin8e8078a2004-09-26 13:10:40 +000012 ASN_TAG_CLASS_PRIVATE = 3 /* 0b11 */
Lev Walkinf15320b2004-06-03 03:38:44 +000013};
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)
Lev Walkin8c3b8542005-03-10 18:52:02 +000021#define BER_TLV_CONSTRUCTED(tagptr) (((*(const uint8_t *)tagptr)&0x20)?1:0)
Lev Walkinf15320b2004-06-03 03:38:44 +000022
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 */
Lev Walkin8c3b8542005-03-10 18:52:02 +000042ssize_t ber_fetch_tag(const void *bufptr, size_t size, ber_tlv_tag_t *tag_r);
Lev Walkinf15320b2004-06-03 03:38:44 +000043
44/*
Lev Walkin2ca95432004-08-19 13:27:34 +000045 * This function serializes the tag (T from TLV) in BER format.
Lev Walkin91b29c52004-09-24 20:59:13 +000046 * It always returns number of bytes necessary to represent the tag,
Lev Walkinf15320b2004-06-03 03:38:44 +000047 * it is a caller's responsibility to check the return value
48 * against the supplied buffer's size.
49 */
Lev Walkin91b29c52004-09-24 20:59:13 +000050size_t ber_tlv_tag_serialize(ber_tlv_tag_t tag, void *bufptr, size_t size);
Lev Walkinf15320b2004-06-03 03:38:44 +000051
52#endif /* _BER_TLV_TAG_H_ */