From 50e51fdf981b60605bc4f7711de170173358eb12 Mon Sep 17 00:00:00 2001
From: "plvines (corpora)" <paul.vines@baesystems.com>
Date: Wed, 12 Feb 2020 15:50:04 +0000
Subject: [PATCH] Added warning logging function, macro, and to junk

---
 pdf.c | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)

diff --git a/pdf.c b/pdf.c
index df57b4d..678788d 100644
--- a/pdf.c
+++ b/pdf.c
@@ -17,6 +17,12 @@
 #define IN(STR)		h_in((const uint8_t *)(STR), sizeof(STR) - 1)
 #define NOT_IN(STR)	h_not_in((const uint8_t *)(STR), sizeof(STR) - 1)
 
+#ifdef LOG
+#define WARN(P,MSG)     h_action(P, act_warn, (char *) MSG)
+#else
+#define WARN(P,MSG)     P
+#endif
+
 
 /*
  * some helpers
@@ -29,6 +35,14 @@ HParser *p_return_1;
 
 /* a combinator to parse a given character but return a different value */
 
+HParsedToken *
+act_warn(const HParseResult *p, void *warnstring)
+{
+  fprintf(stderr, "WARNING: %s\n", (char *) warnstring);
+  return (HParsedToken *) p->ast;
+}
+
+
 HParsedToken *
 act_return_uint(const HParseResult *p, void *u)
 {
@@ -667,7 +681,8 @@ init_parser(struct Env *aux)
 		// NB: lws before xref offset is allowed, cf. p.48 (example 4)
 	H_RULE(xr_td,	SEQ(xrefs, KW("trailer"), ws, dict));
 
-	H_RULE(junk,    h_many1(h_butnot(h_ch_range(0, 255), objdef)));
+	H_RULE(hdr_junk,    WARN(h_many1(h_butnot(h_ch_range(0, 255), objdef)),
+                             "Junk after header"));
 	H_RULE(tail,	SEQ(body, h_optional(xr_td), startxr));
 		// XXX the real world likes to omit 'startxr' from all but the
 		// last trailer. we should accept-and-warn in that case.
-- 
GitLab