chiark / gitweb /
dirmngr: New debug message on correctly initialized libdns.
[gnupg2.git] / common / tlv.h
1 /* tlv.h - Tag-Length-Value Utilities
2  *      Copyright (C) 2004 Free Software Foundation, Inc.
3  *
4  * This file is part of GnuPG.
5  *
6  * This file is free software; you can redistribute it and/or modify
7  * it under the terms of either
8  *
9  *   - the GNU Lesser General Public License as published by the Free
10  *     Software Foundation; either version 3 of the License, or (at
11  *     your option) any later version.
12  *
13  * or
14  *
15  *   - the GNU General Public License as published by the Free
16  *     Software Foundation; either version 2 of the License, or (at
17  *     your option) any later version.
18  *
19  * or both in parallel, as here.
20  *
21  * This file is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, see <https://www.gnu.org/licenses/>.
28  */
29
30 #ifndef SCD_TLV_H
31 #define SCD_TLV_H 1
32
33
34 enum tlv_tag_class {
35   CLASS_UNIVERSAL = 0,
36   CLASS_APPLICATION = 1,
37   CLASS_CONTEXT = 2,
38   CLASS_PRIVATE =3
39 };
40
41 enum tlv_tag_type {
42   TAG_NONE = 0,
43   TAG_BOOLEAN = 1,
44   TAG_INTEGER = 2,
45   TAG_BIT_STRING = 3,
46   TAG_OCTET_STRING = 4,
47   TAG_NULL = 5,
48   TAG_OBJECT_ID = 6,
49   TAG_OBJECT_DESCRIPTOR = 7,
50   TAG_EXTERNAL = 8,
51   TAG_REAL = 9,
52   TAG_ENUMERATED = 10,
53   TAG_EMBEDDED_PDV = 11,
54   TAG_UTF8_STRING = 12,
55   TAG_REALTIVE_OID = 13,
56   TAG_SEQUENCE = 16,
57   TAG_SET = 17,
58   TAG_NUMERIC_STRING = 18,
59   TAG_PRINTABLE_STRING = 19,
60   TAG_TELETEX_STRING = 20,
61   TAG_VIDEOTEX_STRING = 21,
62   TAG_IA5_STRING = 22,
63   TAG_UTC_TIME = 23,
64   TAG_GENERALIZED_TIME = 24,
65   TAG_GRAPHIC_STRING = 25,
66   TAG_VISIBLE_STRING = 26,
67   TAG_GENERAL_STRING = 27,
68   TAG_UNIVERSAL_STRING = 28,
69   TAG_CHARACTER_STRING = 29,
70   TAG_BMP_STRING = 30
71 };
72
73
74 /* Locate a TLV encoded data object in BUFFER of LENGTH and return a
75    pointer to value as well as its length in NBYTES.  Return NULL if
76    it was not found or if the object does not fit into the buffer. */
77 const unsigned char *find_tlv (const unsigned char *buffer, size_t length,
78                                int tag, size_t *nbytes);
79
80
81 /* Locate a TLV encoded data object in BUFFER of LENGTH and return a
82    pointer to value as well as its length in NBYTES.  Return NULL if
83    it was not found.  Note, that the function does not check whether
84    the value fits into the provided buffer.*/
85 const unsigned char *find_tlv_unchecked (const unsigned char *buffer,
86                                          size_t length,
87                                          int tag, size_t *nbytes);
88
89
90 /* ASN.1 BER parser: Parse BUFFER of length SIZE and return the tag
91    and the length part from the TLV triplet.  Update BUFFER and SIZE
92    on success. */
93 gpg_error_t parse_ber_header (unsigned char const **buffer, size_t *size,
94                                int *r_class, int *r_tag,
95                                int *r_constructed,
96                               int *r_ndef, size_t *r_length, size_t *r_nhdr);
97
98
99 /* Return the next token of an canonical encoded S-expression.  BUF
100    is the pointer to the S-expression and BUFLEN is a pointer to the
101    length of this S-expression (used to validate the syntax).  Both
102    are updated to reflect the new position.  The token itself is
103    returned as a pointer into the original buffer at TOK and TOKLEN.
104    If a parentheses is the next token, TOK will be set to NULL.
105    TOKLEN is checked to be within the bounds.  On error an error code
106    is returned and no pointer is not guaranteed to point to
107    a meaningful value.  DEPTH should be initialized to 0 and will
108    reflect on return the actual depth of the tree. To detect the end
109    of the S-expression it is advisable to check DEPTH after a
110    successful return. */
111 gpg_error_t parse_sexp (unsigned char const **buf, size_t *buflen,
112                         int *depth, unsigned char const **tok, size_t *toklen);
113
114
115
116 #endif /* SCD_TLV_H */