1
1
/* tlv.c - Tag-Length-Value Utilities
2
* Copyright (C) 2003, 2004 Free Software Foundation, Inc.
2
* Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
4
4
* This file is part of GnuPG.
16
16
* You should have received a copy of the GNU General Public License
17
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21
22
#include <config.h>
115
124
/* Locate a TLV encoded data object in BUFFER of LENGTH and
116
125
return a pointer to value as well as its length in NBYTES. Return
126
NULL if it was not found or if the object does not fit into the buffer. */
127
const unsigned char *
128
find_tlv (const unsigned char *buffer, size_t length,
129
int tag, size_t *nbytes)
131
const unsigned char *p;
133
p = do_find_tlv (buffer, length, tag, nbytes, 0);
134
if (p && *nbytes > (length - (p-buffer)))
135
p = NULL; /* Object longer than buffer. */
141
/* Locate a TLV encoded data object in BUFFER of LENGTH and
142
return a pointer to value as well as its length in NBYTES. Return
117
143
NULL if it was not found. Note, that the function does not check
118
144
whether the value fits into the provided buffer. */
119
145
const unsigned char *
120
find_tlv (const unsigned char *buffer, size_t length,
121
int tag, size_t *nbytes)
146
find_tlv_unchecked (const unsigned char *buffer, size_t length,
147
int tag, size_t *nbytes)
123
149
return do_find_tlv (buffer, length, tag, nbytes, 0);
129
153
/* ASN.1 BER parser: Parse BUFFER of length SIZE and return the tag
130
154
and the length part from the TLV triplet. Update BUFFER and SIZE
236
/* FIXME: The following function should not go into this file but for
237
now it is easier to keep it here. */
239
/* Return the next token of an canconical encoded S-expression. BUF
240
is the pointer to the S-expression and BUFLEN is a pointer to the
241
length of this S-expression (used to validate the syntax). Both
242
are updated to reflect the new position. The token itself is
243
returned as a pointer into the orginal buffer at TOK and TOKLEN.
244
If a parentheses is the next token, TOK will be set to NULL.
245
TOKLEN is checked to be within the bounds. On error a error code
246
is returned and all pointers should are not guaranteed to point to
247
a meanigful value. DEPTH should be initialized to 0 and will
248
reflect on return the actual depth of the tree. To detect the end
249
of the S-expression it is advisable to check DEPTH after a
253
while (!(err = parse_sexp (&buf, &buflen, &depth, &tok, &toklen))
255
process_token (tok, toklen);
260
parse_sexp (unsigned char const **buf, size_t *buflen,
261
int *depth, unsigned char const **tok, size_t *toklen)
263
const unsigned char *s;
271
return *depth ? gpg_error (GPG_ERR_INV_SEXP) : 0;
283
return gpg_error (GPG_ERR_INV_SEXP);
291
for (vlen=0; n && *s && *s != ':' && (*s >= '0' && *s <= '9'); s++, n--)
292
vlen = vlen*10 + (*s - '0');
294
return gpg_error (GPG_ERR_INV_SEXP);
297
return gpg_error (GPG_ERR_INV_SEXP);