csexpr parsing problem

Werner Koch wk at gnupg.org
Tue Dec 12 08:41:33 CET 2017

On Mon, 11 Dec 2017 20:51, dr.munga at gmail.com said:

> (10:public-key(3:rsa(1:n513: ... that is a csexpr.
> I've noticed that the numbers 10, 3, 1 and 513, that are supposedly
> the number of bytes to follow, are always the same. BUT the size of the

Right.  Did you read the specification os S-expressions?  Just in case I
will attach them.

> entire csexpr is sometimes different. The server itself tells me the
> number of bytes sent and I get for example :
> 2017-12-08 16:21:36 gpg-agent[17137] DBG: returning public key
> 2017-12-08 16:21:36 gpg-agent[17137] DBG: chan_10 -> [ 44 20 28 31 30
> 3a 70 75 62 6c 69 63 2d 6b

That is log output and not intended for further processing.

> Attached you can find the dump of the csexpr as I receive it from the agent
> and a small python parser... Any insight would be greatly appreciate.

Your dump seems to e araw dump from Assuan.  In Assuan LFs are used as
record delimiters and thus percent escaping is used to protec LFs in raw
data.  Thus you need to unescape the received data lines.  Libassuan
does this for you but percent-unescaping is trivial because it can be
done in-place:

static size_t
do_unescape_inplace (char *string, int withplus, int nulrepl)
  unsigned char *p, *p0;

  p = p0 = string;
  while (*string)
      if (*string == '%' && string[1] && string[2])
          *p = xtoi_2 (string);
          if (!*p) 
            *p = nulrepl;  /* Optional replace a nul.  */
      else if (*string == '+' && withplus) /* Optional '+'->' ' unescaping.*/
        *p = ' ';
        *p = *string;

  return (p - p0);

> - is the answer sent back by the server of a fixed size or not ?

No, it is a canonical encoded s-expression.

> - are the size in the cexpr (513:...) to be interpreted as "read 513 bytes from
>   here" ?

Yes.  But please use a correct s-expression parser and don't assume a
fixed prefix.



Die Gedanken sind frei.  Ausnahmen regelt ein Bundesgesetz.
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: sexp.txt
URL: <https://lists.gnupg.org/pipermail/gnupg-devel/attachments/20171212/ac51ec8e/attachment-0001.txt>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 227 bytes
Desc: not available
URL: <https://lists.gnupg.org/pipermail/gnupg-devel/attachments/20171212/ac51ec8e/attachment-0001.sig>

More information about the Gnupg-devel mailing list