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.
114
122
/* Locate a TLV encoded data object in BUFFER of LENGTH and
115
123
return a pointer to value as well as its length in NBYTES. Return
124
NULL if it was not found or if the object does not fit into the buffer. */
125
const unsigned char *
126
find_tlv (const unsigned char *buffer, size_t length,
127
int tag, size_t *nbytes)
129
const unsigned char *p;
131
p = do_find_tlv (buffer, length, tag, nbytes, 0);
132
if (p && *nbytes > (length - (p-buffer)))
133
p = NULL; /* Object longer than buffer. */
139
/* Locate a TLV encoded data object in BUFFER of LENGTH and
140
return a pointer to value as well as its length in NBYTES. Return
116
141
NULL if it was not found. Note, that the function does not check
117
142
whether the value fits into the provided buffer. */
118
143
const unsigned char *
119
find_tlv (const unsigned char *buffer, size_t length,
120
int tag, size_t *nbytes)
144
find_tlv_unchecked (const unsigned char *buffer, size_t length,
145
int tag, size_t *nbytes)
122
147
return do_find_tlv (buffer, length, tag, nbytes, 0);
128
151
/* ASN.1 BER parser: Parse BUFFER of length SIZE and return the tag
129
152
and the length part from the TLV triplet. Update BUFFER and SIZE
234
/* FIXME: The following function should not go into this file but for
235
now it is easier to keep it here. */
237
/* Return the next token of an canconical encoded S-expression. BUF
238
is the pointer to the S-expression and BUFLEN is a pointer to the
239
length of this S-expression (used to validate the syntax). Both
240
are updated to reflect the new position. The token itself is
241
returned as a pointer into the orginal buffer at TOK and TOKLEN.
242
If a parentheses is the next token, TOK will be set to NULL.
243
TOKLEN is checked to be within the bounds. On error a error code
244
is returned and all pointers should are not guaranteed to point to
245
a meanigful value. DEPTH should be initialized to 0 and will
246
reflect on return the actual depth of the tree. To detect the end
247
of the S-expression it is advisable to check DEPTH after a
251
while (!(err = parse_sexp (&buf, &buflen, &depth, &tok, &toklen))
253
process_token (tok, toklen);
258
parse_sexp (unsigned char const **buf, size_t *buflen,
259
int *depth, unsigned char const **tok, size_t *toklen)
261
const unsigned char *s;
269
return *depth ? gpg_error (GPG_ERR_INV_SEXP) : 0;
281
return gpg_error (GPG_ERR_INV_SEXP);
289
for (vlen=0; n && *s && *s != ':' && (*s >= '0' && *s <= '9'); s++, n--)
290
vlen = vlen*10 + (*s - '0');
292
return gpg_error (GPG_ERR_INV_SEXP);
295
return gpg_error (GPG_ERR_INV_SEXP);