Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | /*- |
Lev Walkin | 20696a4 | 2017-10-17 21:27:33 -0700 | [diff] [blame] | 2 | * Copyright (c) 2003-2017 Lev Walkin <vlm@lionet.info>. All rights reserved. |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 3 | * Redistribution and modifications are permitted subject to BSD license. |
| 4 | */ |
| 5 | #ifndef _BER_TLV_TAG_H_ |
| 6 | #define _BER_TLV_TAG_H_ |
| 7 | |
Lev Walkin | 21b41ac | 2005-07-24 09:03:44 +0000 | [diff] [blame] | 8 | #ifdef __cplusplus |
| 9 | extern "C" { |
| 10 | #endif |
| 11 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 12 | enum asn_tag_class { |
| 13 | ASN_TAG_CLASS_UNIVERSAL = 0, /* 0b00 */ |
| 14 | ASN_TAG_CLASS_APPLICATION = 1, /* 0b01 */ |
| 15 | ASN_TAG_CLASS_CONTEXT = 2, /* 0b10 */ |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 16 | ASN_TAG_CLASS_PRIVATE = 3 /* 0b11 */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 17 | }; |
| 18 | typedef unsigned ber_tlv_tag_t; /* BER TAG from Tag-Length-Value */ |
| 19 | |
| 20 | /* |
| 21 | * Tag class is encoded together with tag value for optimization purposes. |
| 22 | */ |
| 23 | #define BER_TAG_CLASS(tag) ((tag) & 0x3) |
| 24 | #define BER_TAG_VALUE(tag) ((tag) >> 2) |
Lev Walkin | 8c3b854 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 25 | #define BER_TLV_CONSTRUCTED(tagptr) (((*(const uint8_t *)tagptr)&0x20)?1:0) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 26 | |
| 27 | #define BER_TAGS_EQUAL(tag1, tag2) ((tag1) == (tag2)) |
| 28 | |
| 29 | /* |
| 30 | * Several functions for printing the TAG in the canonical form |
| 31 | * (i.e. "[PRIVATE 0]"). |
| 32 | * Return values correspond to their libc counterparts (if any). |
| 33 | */ |
| 34 | ssize_t ber_tlv_tag_snprint(ber_tlv_tag_t tag, char *buf, size_t buflen); |
| 35 | ssize_t ber_tlv_tag_fwrite(ber_tlv_tag_t tag, FILE *); |
| 36 | char *ber_tlv_tag_string(ber_tlv_tag_t tag); |
| 37 | |
| 38 | |
| 39 | /* |
| 40 | * This function tries to fetch the tag from the input stream. |
| 41 | * RETURN VALUES: |
| 42 | * 0: More data expected than bufptr contains. |
| 43 | * -1: Fatal error deciphering tag. |
| 44 | * >0: Number of bytes used from bufptr. tag_r will contain the tag. |
| 45 | */ |
Lev Walkin | 8c3b854 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 46 | ssize_t ber_fetch_tag(const void *bufptr, size_t size, ber_tlv_tag_t *tag_r); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 47 | |
| 48 | /* |
Lev Walkin | 2ca9543 | 2004-08-19 13:27:34 +0000 | [diff] [blame] | 49 | * This function serializes the tag (T from TLV) in BER format. |
Lev Walkin | 91b29c5 | 2004-09-24 20:59:13 +0000 | [diff] [blame] | 50 | * It always returns number of bytes necessary to represent the tag, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 51 | * it is a caller's responsibility to check the return value |
| 52 | * against the supplied buffer's size. |
| 53 | */ |
Lev Walkin | 91b29c5 | 2004-09-24 20:59:13 +0000 | [diff] [blame] | 54 | size_t ber_tlv_tag_serialize(ber_tlv_tag_t tag, void *bufptr, size_t size); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 55 | |
Lev Walkin | 21b41ac | 2005-07-24 09:03:44 +0000 | [diff] [blame] | 56 | #ifdef __cplusplus |
| 57 | } |
| 58 | #endif |
| 59 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 60 | #endif /* _BER_TLV_TAG_H_ */ |