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