Moving in e2sim originally from it/test/simulators
[sim/e2-interface.git] / e2sim / ASN1c / ber_tlv_tag.h
1 /*****************************************************************************
2 #                                                                            *
3 # Copyright 2019 AT&T Intellectual Property                                  *
4 #                                                                            *
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                                    *
8 #                                                                            *
9 #      http://www.apache.org/licenses/LICENSE-2.0                            *
10 #                                                                            *
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.                                             *
16 #                                                                            *
17 ******************************************************************************/
18
19 /*-
20  * Copyright (c) 2003-2017 Lev Walkin <vlm@lionet.info>. All rights reserved.
21  * Redistribution and modifications are permitted subject to BSD license.
22  */
23 #ifndef _BER_TLV_TAG_H_
24 #define _BER_TLV_TAG_H_
25
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29
30 enum asn_tag_class {
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 */
35 };
36 typedef unsigned ber_tlv_tag_t; /* BER TAG from Tag-Length-Value */
37
38 /*
39  * Tag class is encoded together with tag value for optimization purposes.
40  */
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)
44
45 #define BER_TAGS_EQUAL(tag1, tag2)      ((tag1) == (tag2))
46
47 /*
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).
51  */
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);
55
56
57 /*
58  * This function tries to fetch the tag from the input stream.
59  * RETURN VALUES:
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.
63  */
64 ssize_t ber_fetch_tag(const void *bufptr, size_t size, ber_tlv_tag_t *tag_r);
65
66 /*
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.
71  */
72 size_t ber_tlv_tag_serialize(ber_tlv_tag_t tag, void *bufptr, size_t size);
73
74 #ifdef __cplusplus
75 }
76 #endif
77
78 #endif  /* _BER_TLV_TAG_H_ */