1 /* sexp-parse.h - S-Exp helper functions
2 * Copyright (C) 2002, 2003, 2007 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/>.
23 #include <gpg-error.h>
25 /* Return the length of the next S-Exp part and update the pointer to
26 the first data byte. 0 is returned on error */
28 snext (unsigned char const **buf
)
30 const unsigned char *s
;
34 for (n
=0; *s
&& *s
!= ':' && (*s
>= '0' && *s
<= '9'); s
++)
35 n
= n
*10 + (*s
- '0');
37 return 0; /* we don't allow empty lengths */
42 /* Skip over the S-Expression BUF points to and update BUF to point to
43 the chacter right behind. DEPTH gives the initial number of open
44 lists and may be passed as a positive number to skip over the
45 remainder of an S-Expression if the current position is somewhere
46 in an S-Expression. The function may return an error code if it
47 encounters an impossible condition. */
48 static inline gpg_error_t
49 sskip (unsigned char const **buf
, int *depth
)
51 const unsigned char *s
= *buf
;
70 return gpg_error (GPG_ERR_INV_SEXP
);
73 return gpg_error (GPG_ERR_INV_SEXP
);
83 /* Check whether the the string at the address BUF points to matches
84 the token. Return true on match and update BUF to point behind the
85 token. Return false and dont update tha buffer if it does not
88 smatch (unsigned char const **buf
, size_t buflen
, const char *token
)
90 size_t toklen
= strlen (token
);
92 if (buflen
!= toklen
|| memcmp (*buf
, token
, toklen
))
98 /* Format VALUE for use as the length indicatior of an S-expression.
99 The caller needs to provide a buffer HELP_BUFFER wth a length of
100 HELP_BUFLEN. The return value is a pointer into HELP_BUFFER with
101 the formatted length string. The colon and a trailing nul are
102 appended. HELP_BUFLEN must be at least 3 - a more useful value is
103 15. If LENGTH is not NULL, the LENGTH of the resulting string
104 (excluding the terminating nul) is stored at that address. */
106 smklen (char *help_buffer
, size_t help_buflen
, size_t value
, size_t *length
)
108 char *p
= help_buffer
+ help_buflen
;
110 if (help_buflen
>= 3)
116 *--p
= '0' + (value
% 10);
119 while (value
&& p
> help_buffer
);
123 *length
= (help_buffer
+ help_buflen
) - p
;
128 #endif /*SEXP_PARSE_H*/