Debian Codegraph @

pdf/pdf_file.h

raw
1 /* Copyright (C) 2018-2024 Artifex Software, Inc.
2 All Rights Reserved.
3
4 This software is provided AS-IS with no warranty, either express or
5 implied.
6
7 This software is distributed under license and may not be copied,
8 modified or distributed except as expressly authorized under the terms
9 of the license contained in the file LICENSE in this distribution.
10
11 Refer to licensing information at http://www.artifex.com or contact
12 Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
13 CA 94129, USA, for further information.
14 */
15
16 /* File and decompression filter code */
17
18 /*
19 * Note: file and stream handling means using the Ghostscript graphics library stream code
20 * for the decompression filters (and some others not strictly decompression).
21 *
22 * We can think of three 'layers' of access to the raw PDF file and to streams. The
23 * PDF 'parser' is responsible for all the file format access, such as finding the xref,
24 * reading the xref table, dereferencing objects etc. The basic file opening and consistency
25 * checks are in ghostpdf.c, the xref table and object dereferencing is in pdf_xref.c, this includes
26 * handling compressed object streams and compressed xref streams. Currently all this code operates on the
27 * raw file, we always read the xref up front so compressed xrefs aren't a problem, compressed object
28 * streams are read from the start of the stream every time we need an uncached object.
29 *
30 * The next level down is the PDF 'interpreter', which parses content streams, this code is in
31 * pdf_int.c and operates on one of our PDF stream objects, this may include chained compression
32 * (and possibly decryption) filters. This code reads PDF tokens, such as strings, numbers and
33 * PDF operators and stores them on our stack. This always works with a stream, because content
34 * streams are usually compressed. If there are no compression filters then it will be the main
35 * file stream but we absolutely must never rely on that.
36 *
37 * Finally there are the PDF operators themselves, in general thse do not access the stream at all
38 * because all operands are presented on the stack. External content streams such as image XObjects
39 * and Patterns are handled as a genuinely separate stream. The sole exception is inline images.
40 *
41 * So generally speakinc the 'parser' operates on the raw file, the 'interpreter' operates on a
42 * PDF stream, and the individual operators don't need to access either.
43 *
44 * When we want to execute another stream we store the current file position, and then move the
45 * underlying file pointer. When the substream is completed we move the file pointer back. Because we
46 * haven't read any bytes from the original filter chain in the interim its state wil be undisturbed,
47 * so as long as the file pointer is properly restored we can pick up exactly where we left off. This
48 * neatly allows us to execute substreams without having to decompress the entire parent stream.
49 * (see below)
50 *
51 * Our own PDF stream is defined mainly so that we can 'unread' a few bytes in the PDF interpreter.
52 * While scanning for tokens we often read a byte which terminates an object, but also starts a new one,
53 * its convenient to be able to 'rewind' the stream to handle this. If the PDF file were uncompressed
54 * that wouldn't be a problem, we could use fseek(), but we can't go backwards in a compressed stream
55 * without returning to the start of the stream, resetting the decompression filter, and reading up
56 * to the point where we wanted to seek. This is because decompression filters generally have 'state'
57 * which is modified by the data as it is read. Once some data has been decompressed the state has
58 * been altered, and there is generally no way to reverse that change.
59 * Rewinding the stream to the start and re-decompressing would of course be terribly slow. Instead we define
60 * a buffer and when we want to rewind the stream a little we 'unread' bytes (we need to supply the
61 * decompressed byte(s) back to the stream via a 'pdfi_unread' call, it doesn't keep track of these). Currently this has a
62 * fixed buffer of 256 bytes, attempting to unread a total of more than that will result in an ioerror.
63 *
64 * The implication of this is that the PDF interpreter, and the operators, can only progress forwards through
65 * a stream. There is limited support for rewinding a stream, provided the code buffers up the data it wants
66 * to 'unread', and it doesn't exceed 256 bytes.
67 */
68
69 #ifndef PDF_FILES
70 #define PDF_FILES
71 /*
72 * A pdf_c_stream object maintains an 'original' stream memeber. This is only used when closing a file/filter.
73 * When we apply filters to a file we supply the stream that we use as the basis for the new stream, this is
74 * then stored as the 'original' member. When we close the file, we close all the chained streams until we
75 * reach the 'original' member and then exit.
76 *
77 * This allows us to close filters applied to the PDF file without closing the underlying file/filter which was
78 * in effect at the time we created the new stream.
79 */
80
81 int pdfi_filter(pdf_context *ctx, pdf_stream *stream_obj, pdf_c_stream *source, pdf_c_stream **new_stream, bool inline_image);
82 /* pdfi_filter_no_decryption is a special function used by the xref parsing when dealing with XRefStms and should not be used
83 * for anything else. The pdfi_filter routine will apply decryption as required.
84 */
85 int pdfi_filter_no_decryption(pdf_context *ctx, pdf_stream *d, pdf_c_stream *source, pdf_c_stream **new_stream, bool inline_image);
86 void pdfi_close_file(pdf_context *ctx, pdf_c_stream *s);
87 int pdfi_read_bytes(pdf_context *ctx, byte *Buffer, uint32_t size, uint32_t count, pdf_c_stream *s);
88 int pdfi_read_byte(pdf_context *ctx, pdf_c_stream *s);
89 int pdfi_unread(pdf_context *ctx, pdf_c_stream *s, byte *Buffer, uint32_t size);
90 int pdfi_unread_byte(pdf_context *ctx, pdf_c_stream *s, char c);
91 int pdfi_seek(pdf_context *ctx, pdf_c_stream *s, gs_offset_t offset, uint32_t origin);
92 gs_offset_t pdfi_unread_tell(pdf_context *ctx);
93 gs_offset_t pdfi_tell(pdf_c_stream *s);
94
95 int pdfi_apply_SubFileDecode_filter(pdf_context *ctx, int EODCount, const char *EODString, pdf_c_stream *source, pdf_c_stream **new_stream, bool inline_image);
96 int pdfi_open_memory_stream(pdf_context *ctx, unsigned int size, byte **Buffer, pdf_c_stream *source, pdf_c_stream **new_stream);
97 int pdfi_close_memory_stream(pdf_context *ctx, byte *Buffer, pdf_c_stream *source);
98 int pdfi_open_memory_stream_from_stream(pdf_context *ctx, unsigned int size, byte **Buffer, pdf_c_stream *source, pdf_c_stream **new_pdf_stream, bool retain_ownership);
99 int pdfi_open_memory_stream_from_filtered_stream(pdf_context *ctx, pdf_stream *stream_dict, byte **Buffer, pdf_c_stream **new_pdf_stream, bool retain_ownership);
100 int pdfi_open_memory_stream_from_memory(pdf_context *ctx, unsigned int size, byte *Buffer, pdf_c_stream **new_pdf_stream, bool retain_ownership);
101 int pdfi_stream_to_buffer(pdf_context *ctx, pdf_stream *stream_dict, byte **buf, int64_t *bufferlen);
102
103 int pdfi_apply_Arc4_filter(pdf_context *ctx, pdf_string *Key, pdf_c_stream *source, pdf_c_stream **new_stream);
104 int pdfi_apply_AES_filter(pdf_context *ctx, pdf_string *Key, bool use_padding, pdf_c_stream *source, pdf_c_stream **new_stream);
105 int pdfi_apply_imscale_filter(pdf_context *ctx, pdf_string *Key, int width, int height, pdf_c_stream *source, pdf_c_stream **new_stream);
106
107 #ifdef UNUSED_FILTER
108 int pdfi_apply_SHA256_filter(pdf_context *ctx, pdf_c_stream *source, pdf_c_stream **new_stream);
109 #endif
110
111 int pdfi_open_resource_file(pdf_context *ctx, const char *fname, const int fnamelen, stream **s);
112 bool pdfi_resource_file_exists(pdf_context *ctx, const char *fname, const int fnamelen);
113 int pdfi_open_font_file(pdf_context *ctx, const char *fname, const int fnamelen, stream **s);
114 bool pdfi_font_file_exists(pdf_context *ctx, const char *fname, const int fnamelen);
115
116 #endif /* PDF_FILES */