diff --git a/pdf.c b/pdf.c index 3b282184f21c92d3d3c6b73983f07c53cb54b3bf..1761f1414ca65700c8f8cea42ddf9ed29f4e7fe2 100644 --- a/pdf.c +++ b/pdf.c @@ -4415,11 +4415,6 @@ process_page_content(struct Env *aux) -/* - * ******************************************************************** - * Start xref parsing - * ******************************************************************** - */ /* * decode the bytes in 'b' according to metadata in the stream dictionary 'd' * and parse the result with 'p'. @@ -4876,6 +4871,73 @@ kxstream(HAllocator *mm__, const HParsedToken *x, void *env) } +/* + * main program + */ + +#include <stdio.h> +#include <string.h> /* strtok() */ +#include <inttypes.h> +#include <fcntl.h> /* open() */ +#include <unistd.h> /* lseek(), getopt() */ +#include <sys/mman.h> /* mmap() */ +#include <errno.h> + +/* used with getopt() */ +extern char *optarg; +extern int optind; + +/* command line arguments */ +const char *progname; +const char *infile; +const char *xfile, *Xfile; +int qflag, vflag, dflag; +int onum = -1, ogen = -1; /* -1 means unspecified */ + +/* helper: parse 's' as a natural number. returns -1 on error. */ +int +strtonat(const char *s) +{ + char *p; + long l; + + errno = 0; + l = strtol(s, &p, 10); + if (errno != 0) + return -1; + if (p == s || *p != '\0') /* no parse */ + return -1; + if (l < 0 || l > INT_MAX) /* out of range */ + return -1; + return l; +} + +void +usage(void) +{ + fprintf(stderr, "usage: %s [-qsv] [-d what] [-x out.txt] file [oid]\n", + progname); + exit(2); +} + +void +dumpstream(FILE *f, const HParsedToken *obj) +{ + HParseResult *res; + HBytes data; + + // XXX properly verify that obj is a stream (needs custom token type) + if (obj->token_type != TT_SEQUENCE || obj->seq->used != 2 || + obj->seq->elements[1]->token_type != TT_HParseResult) + errx(2, "%s: requested object is not a stream", infile); + + res = H_INDEX(HParseResult, obj, 1); + assert(res != NULL); + assert(res->ast != NULL); + data = H_CAST_BYTES(res->ast); + + fwrite(data.token, 1, data.len, f); +} /* * This helper implements the standard backwards parsing strategy to read all @@ -4887,8 +4949,6 @@ kxstream(HAllocator *mm__, const HParsedToken *x, void *env) * number of elements. */ // XXX review changes to this function. see git -L /^parse_xrefs/,/^}/:pdf.c' -const char *infile = NULL; - void parse_xrefs(struct Env *aux) { @@ -4981,82 +5041,6 @@ end: aux->nxrefs = n; } -/* - * ******************************************************************** - * End xref parsing - * ******************************************************************** - */ - - - - -/* - * main program - */ - -#include <stdio.h> -#include <string.h> /* strtok() */ -#include <inttypes.h> -#include <fcntl.h> /* open() */ -#include <unistd.h> /* lseek(), getopt() */ -#include <sys/mman.h> /* mmap() */ -#include <errno.h> - -/* used with getopt() */ -extern char *optarg; -extern int optind; - -/* command line arguments */ -const char *progname; -const char *xfile, *Xfile; -int qflag, vflag, dflag; -int onum = -1, ogen = -1; /* -1 means unspecified */ - -/* helper: parse 's' as a natural number. returns -1 on error. */ -int -strtonat(const char *s) -{ - char *p; - long l; - - errno = 0; - l = strtol(s, &p, 10); - if (errno != 0) - return -1; - if (p == s || *p != '\0') /* no parse */ - return -1; - if (l < 0 || l > INT_MAX) /* out of range */ - return -1; - return l; -} - -void -usage(void) -{ - fprintf(stderr, "usage: %s [-qsv] [-d what] [-x out.txt] file [oid]\n", - progname); - exit(2); -} - -void -dumpstream(FILE *f, const HParsedToken *obj) -{ - HParseResult *res; - HBytes data; - - // XXX properly verify that obj is a stream (needs custom token type) - if (obj->token_type != TT_SEQUENCE || obj->seq->used != 2 || - obj->seq->elements[1]->token_type != TT_HParseResult) - errx(2, "%s: requested object is not a stream", infile); - - res = H_INDEX(HParseResult, obj, 1); - assert(res != NULL); - assert(res->ast != NULL); - data = H_CAST_BYTES(res->ast); - - fwrite(data.token, 1, data.len, f); -} - int main(int argc, char *argv[]) {