diff options
author | Werner Koch <[email protected]> | 2017-03-29 08:02:40 +0000 |
---|---|---|
committer | Werner Koch <[email protected]> | 2017-03-29 08:06:29 +0000 |
commit | 0526c99164d3531b5ec763ffc672407eb24b2296 (patch) | |
tree | 0deb94a79a1cb2b916548e5d5cbb58114402dc2e /g10/parse-packet.c | |
parent | gpg: Export ring trust packets in backup mode. (diff) | |
download | gnupg-0526c99164d3531b5ec763ffc672407eb24b2296.tar.gz gnupg-0526c99164d3531b5ec763ffc672407eb24b2296.zip |
gpg: Change parse_packet to take a context.
* g10/packet.h (struct parse_packet_ctx_s): New.
(parse_packet_ctx_t): New type.
(init_parse_packet): New macro.
* g10/parse-packet.c (parse_packet, dbg_parse_packet): Change to take
a parse context. Change all callers to provide a context instead of
directly supplying the input stream.
(search_packet, dbg_search_packet): Ditto.
(copy_all_packets, dbg_copy_all_packets): Init an use a parse context.
(copy_some_packets, dbg_copy_some_packets): Ditto.
(skip_some_packets, dbg_skip_some_packets): Ditto.
--
We will need this change to handle ring packets inside the parser.
Signed-off-by: Werner Koch <[email protected]>
Diffstat (limited to 'g10/parse-packet.c')
-rw-r--r-- | g10/parse-packet.c | 94 |
1 files changed, 63 insertions, 31 deletions
diff --git a/g10/parse-packet.c b/g10/parse-packet.c index 06b286bd7..7766a45f6 100644 --- a/g10/parse-packet.c +++ b/g10/parse-packet.c @@ -48,7 +48,7 @@ static int mpi_print_mode; static int list_mode; static estream_t listfp; -static int parse (IOBUF inp, PACKET * pkt, int onlykeypkts, +static int parse (parse_packet_ctx_t ctx, PACKET *pkt, int onlykeypkts, off_t * retpos, int *skip, IOBUF out, int do_skip #ifdef DEBUG_PARSE_PACKET , const char *dbg_w, const char *dbg_f, int dbg_l @@ -263,26 +263,27 @@ unknown_pubkey_warning (int algo) #ifdef DEBUG_PARSE_PACKET int -dbg_parse_packet (IOBUF inp, PACKET *pkt, const char *dbg_f, int dbg_l) +dbg_parse_packet (parse_packet_ctx_t ctx, PACKET *pkt, + const char *dbg_f, int dbg_l) { int skip, rc; do { - rc = parse (inp, pkt, 0, NULL, &skip, NULL, 0, "parse", dbg_f, dbg_l); + rc = parse (ctx, pkt, 0, NULL, &skip, NULL, 0, "parse", dbg_f, dbg_l); } while (skip && ! rc); return rc; } #else /*!DEBUG_PARSE_PACKET*/ int -parse_packet (IOBUF inp, PACKET * pkt) +parse_packet (parse_packet_ctx_t ctx, PACKET *pkt) { int skip, rc; do { - rc = parse (inp, pkt, 0, NULL, &skip, NULL, 0); + rc = parse (ctx, pkt, 0, NULL, &skip, NULL, 0); } while (skip && ! rc); return rc; @@ -296,29 +297,30 @@ parse_packet (IOBUF inp, PACKET * pkt) */ #ifdef DEBUG_PARSE_PACKET int -dbg_search_packet (IOBUF inp, PACKET * pkt, off_t * retpos, int with_uid, +dbg_search_packet (parse_packet_ctx_t ctx, PACKET *pkt, + off_t * retpos, int with_uid, const char *dbg_f, int dbg_l) { int skip, rc; do { - rc = - parse (inp, pkt, with_uid ? 2 : 1, retpos, &skip, NULL, 0, "search", - dbg_f, dbg_l); + rc = parse (ctx, pkt, with_uid ? 2 : 1, retpos, &skip, NULL, 0, "search", + dbg_f, dbg_l); } while (skip && ! rc); return rc; } #else /*!DEBUG_PARSE_PACKET*/ int -search_packet (IOBUF inp, PACKET * pkt, off_t * retpos, int with_uid) +search_packet (parse_packet_ctx_t ctx, PACKET *pkt, + off_t * retpos, int with_uid) { int skip, rc; do { - rc = parse (inp, pkt, with_uid ? 2 : 1, retpos, &skip, NULL, 0); + rc = parse (ctx, pkt, with_uid ? 2 : 1, retpos, &skip, NULL, 0); } while (skip && ! rc); return rc; @@ -331,38 +333,45 @@ search_packet (IOBUF inp, PACKET * pkt, off_t * retpos, int with_uid) */ #ifdef DEBUG_PARSE_PACKET int -dbg_copy_all_packets (IOBUF inp, IOBUF out, const char *dbg_f, int dbg_l) +dbg_copy_all_packets (iobuf_t inp, iobuf_t out, const char *dbg_f, int dbg_l) { PACKET pkt; + struct parse_packet_ctx_s parsectx; int skip, rc = 0; if (! out) log_bug ("copy_all_packets: OUT may not be NULL.\n"); + init_parse_packet (&parsectx, inp); + do { init_packet (&pkt); } while (! (rc = - parse (inp, &pkt, 0, NULL, &skip, out, 0, "copy", dbg_f, dbg_l))); + parse (&parsectx, &pkt, 0, NULL, &skip, out, 0, "copy", + dbg_f, dbg_l))); return rc; } #else /*!DEBUG_PARSE_PACKET*/ int -copy_all_packets (IOBUF inp, IOBUF out) +copy_all_packets (iobuf_t inp, iobuf_t out) { PACKET pkt; + struct parse_packet_ctx_s parsectx; int skip, rc = 0; if (! out) log_bug ("copy_all_packets: OUT may not be NULL.\n"); + init_parse_packet (&parsectx, inp); + do { init_packet (&pkt); } - while (!(rc = parse (inp, &pkt, 0, NULL, &skip, out, 0))); + while (!(rc = parse (&parsectx, &pkt, 0, NULL, &skip, out, 0))); return rc; } #endif /*!DEBUG_PARSE_PACKET*/ @@ -375,34 +384,44 @@ copy_all_packets (IOBUF inp, IOBUF out) */ #ifdef DEBUG_PARSE_PACKET int -dbg_copy_some_packets (IOBUF inp, IOBUF out, off_t stopoff, +dbg_copy_some_packets (iobuf_t inp, iobuf_t out, off_t stopoff, const char *dbg_f, int dbg_l) { + int rc = 0; PACKET pkt; - int skip, rc = 0; + int skip; + struct parse_packet_ctx_s parsectx; + + init_parse_packet (&parsectx, inp); + do { if (iobuf_tell (inp) >= stopoff) return 0; init_packet (&pkt); } - while (!(rc = parse (inp, &pkt, 0, NULL, &skip, out, 0, + while (!(rc = parse (&parsectx, &pkt, 0, NULL, &skip, out, 0, "some", dbg_f, dbg_l))); return rc; } #else /*!DEBUG_PARSE_PACKET*/ int -copy_some_packets (IOBUF inp, IOBUF out, off_t stopoff) +copy_some_packets (iobuf_t inp, iobuf_t out, off_t stopoff) { + int rc = 0; PACKET pkt; - int skip, rc = 0; + struct parse_packet_ctx_s parsectx; + int skip; + + init_parse_packet (&parsectx, inp); + do { if (iobuf_tell (inp) >= stopoff) return 0; init_packet (&pkt); } - while (!(rc = parse (inp, &pkt, 0, NULL, &skip, out, 0))); + while (!(rc = parse (&parsectx, &pkt, 0, NULL, &skip, out, 0))); return rc; } #endif /*!DEBUG_PARSE_PACKET*/ @@ -413,29 +432,38 @@ copy_some_packets (IOBUF inp, IOBUF out, off_t stopoff) */ #ifdef DEBUG_PARSE_PACKET int -dbg_skip_some_packets (IOBUF inp, unsigned n, const char *dbg_f, int dbg_l) +dbg_skip_some_packets (iobuf_t inp, unsigned n, const char *dbg_f, int dbg_l) { - int skip, rc = 0; + int rc = 0; + int skip; PACKET pkt; + struct parse_packet_ctx_s parsectx; + + init_parse_packet (&parsectx, inp); for (; n && !rc; n--) { init_packet (&pkt); - rc = parse (inp, &pkt, 0, NULL, &skip, NULL, 1, "skip", dbg_f, dbg_l); + rc = parse (&parsectx, &pkt, 0, NULL, &skip, NULL, 1, "skip", + dbg_f, dbg_l); } return rc; } #else /*!DEBUG_PARSE_PACKET*/ int -skip_some_packets (IOBUF inp, unsigned n) +skip_some_packets (iobuf_t inp, unsigned int n) { - int skip, rc = 0; + int rc = 0; + int skip; PACKET pkt; + struct parse_packet_ctx_s parsectx; + + init_parse_packet (&parsectx, inp); for (; n && !rc; n--) { init_packet (&pkt); - rc = parse (inp, &pkt, 0, NULL, &skip, NULL, 1); + rc = parse (&parsectx, &pkt, 0, NULL, &skip, NULL, 1); } return rc; } @@ -466,18 +494,20 @@ skip_some_packets (IOBUF inp, unsigned n) Note: ONLYKEYPKTS and DO_SKIP are only respected if OUT is NULL, i.e., the packets are not simply being copied. - If RETPOS is not NULL, then the position of INP (as returned by - iobuf_tell) is saved there before any data is read from INP. + If RETPOS is not NULL, then the position of CTX->INP (as returned by + iobuf_tell) is saved there before any data is read from CTX->INP. */ static int -parse (IOBUF inp, PACKET * pkt, int onlykeypkts, off_t * retpos, +parse (parse_packet_ctx_t ctx, PACKET *pkt, int onlykeypkts, off_t * retpos, int *skip, IOBUF out, int do_skip #ifdef DEBUG_PARSE_PACKET , const char *dbg_w, const char *dbg_f, int dbg_l #endif ) { - int rc = 0, c, ctb, pkttype, lenbytes; + int rc = 0; + iobuf_t inp; + int c, ctb, pkttype, lenbytes; unsigned long pktlen; byte hdr[8]; int hdrlen; @@ -486,6 +516,8 @@ parse (IOBUF inp, PACKET * pkt, int onlykeypkts, off_t * retpos, off_t pos; *skip = 0; + inp = ctx->inp; + log_assert (!pkt->pkt.generic); if (retpos || list_mode) { |