PyMuPDF 1.26.6__cp310-abi3-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (112) hide show
  1. fitz/__init__.py +13 -0
  2. fitz/table.py +2 -0
  3. fitz/utils.py +2 -0
  4. pymupdf/__init__.py +25899 -0
  5. pymupdf/__main__.py +1140 -0
  6. pymupdf/_apply_pages.py +253 -0
  7. pymupdf/_build.py +8 -0
  8. pymupdf/_extra.pyd +0 -0
  9. pymupdf/_mupdf.pyd +0 -0
  10. pymupdf/_wxcolors.py +562 -0
  11. pymupdf/extra.py +220 -0
  12. pymupdf/mupdf-devel/include/mupdf/classes.h +19999 -0
  13. pymupdf/mupdf-devel/include/mupdf/classes2.h +12244 -0
  14. pymupdf/mupdf-devel/include/mupdf/exceptions.h +118 -0
  15. pymupdf/mupdf-devel/include/mupdf/extra.h +189 -0
  16. pymupdf/mupdf-devel/include/mupdf/fitz/archive.h +444 -0
  17. pymupdf/mupdf-devel/include/mupdf/fitz/band-writer.h +117 -0
  18. pymupdf/mupdf-devel/include/mupdf/fitz/barcode.h +138 -0
  19. pymupdf/mupdf-devel/include/mupdf/fitz/bidi.h +90 -0
  20. pymupdf/mupdf-devel/include/mupdf/fitz/bitmap.h +188 -0
  21. pymupdf/mupdf-devel/include/mupdf/fitz/buffer.h +250 -0
  22. pymupdf/mupdf-devel/include/mupdf/fitz/color.h +433 -0
  23. pymupdf/mupdf-devel/include/mupdf/fitz/compress.h +132 -0
  24. pymupdf/mupdf-devel/include/mupdf/fitz/compressed-buffer.h +194 -0
  25. pymupdf/mupdf-devel/include/mupdf/fitz/config.h +302 -0
  26. pymupdf/mupdf-devel/include/mupdf/fitz/context.h +1064 -0
  27. pymupdf/mupdf-devel/include/mupdf/fitz/crypt.h +270 -0
  28. pymupdf/mupdf-devel/include/mupdf/fitz/deskew.h +46 -0
  29. pymupdf/mupdf-devel/include/mupdf/fitz/device.h +652 -0
  30. pymupdf/mupdf-devel/include/mupdf/fitz/display-list.h +142 -0
  31. pymupdf/mupdf-devel/include/mupdf/fitz/document.h +1103 -0
  32. pymupdf/mupdf-devel/include/mupdf/fitz/export.h +52 -0
  33. pymupdf/mupdf-devel/include/mupdf/fitz/filter.h +269 -0
  34. pymupdf/mupdf-devel/include/mupdf/fitz/font.h +858 -0
  35. pymupdf/mupdf-devel/include/mupdf/fitz/geometry.h +885 -0
  36. pymupdf/mupdf-devel/include/mupdf/fitz/getopt.h +134 -0
  37. pymupdf/mupdf-devel/include/mupdf/fitz/glyph-cache.h +96 -0
  38. pymupdf/mupdf-devel/include/mupdf/fitz/glyph.h +81 -0
  39. pymupdf/mupdf-devel/include/mupdf/fitz/hash.h +126 -0
  40. pymupdf/mupdf-devel/include/mupdf/fitz/heap-imp.h +190 -0
  41. pymupdf/mupdf-devel/include/mupdf/fitz/heap.h +143 -0
  42. pymupdf/mupdf-devel/include/mupdf/fitz/image.h +451 -0
  43. pymupdf/mupdf-devel/include/mupdf/fitz/json.h +98 -0
  44. pymupdf/mupdf-devel/include/mupdf/fitz/link.h +130 -0
  45. pymupdf/mupdf-devel/include/mupdf/fitz/log.h +61 -0
  46. pymupdf/mupdf-devel/include/mupdf/fitz/outline.h +254 -0
  47. pymupdf/mupdf-devel/include/mupdf/fitz/output-svg.h +64 -0
  48. pymupdf/mupdf-devel/include/mupdf/fitz/output.h +448 -0
  49. pymupdf/mupdf-devel/include/mupdf/fitz/path.h +477 -0
  50. pymupdf/mupdf-devel/include/mupdf/fitz/pixmap.h +516 -0
  51. pymupdf/mupdf-devel/include/mupdf/fitz/pool.h +102 -0
  52. pymupdf/mupdf-devel/include/mupdf/fitz/separation.h +138 -0
  53. pymupdf/mupdf-devel/include/mupdf/fitz/shade.h +233 -0
  54. pymupdf/mupdf-devel/include/mupdf/fitz/store.h +465 -0
  55. pymupdf/mupdf-devel/include/mupdf/fitz/story-writer.h +209 -0
  56. pymupdf/mupdf-devel/include/mupdf/fitz/story.h +232 -0
  57. pymupdf/mupdf-devel/include/mupdf/fitz/stream.h +646 -0
  58. pymupdf/mupdf-devel/include/mupdf/fitz/string-util.h +335 -0
  59. pymupdf/mupdf-devel/include/mupdf/fitz/structured-text.h +924 -0
  60. pymupdf/mupdf-devel/include/mupdf/fitz/system.h +495 -0
  61. pymupdf/mupdf-devel/include/mupdf/fitz/text.h +210 -0
  62. pymupdf/mupdf-devel/include/mupdf/fitz/track-usage.h +57 -0
  63. pymupdf/mupdf-devel/include/mupdf/fitz/transition.h +76 -0
  64. pymupdf/mupdf-devel/include/mupdf/fitz/tree.h +62 -0
  65. pymupdf/mupdf-devel/include/mupdf/fitz/types.h +41 -0
  66. pymupdf/mupdf-devel/include/mupdf/fitz/util.h +159 -0
  67. pymupdf/mupdf-devel/include/mupdf/fitz/version.h +31 -0
  68. pymupdf/mupdf-devel/include/mupdf/fitz/write-pixmap.h +507 -0
  69. pymupdf/mupdf-devel/include/mupdf/fitz/writer.h +269 -0
  70. pymupdf/mupdf-devel/include/mupdf/fitz/xml.h +435 -0
  71. pymupdf/mupdf-devel/include/mupdf/fitz.h +101 -0
  72. pymupdf/mupdf-devel/include/mupdf/functions.h +13819 -0
  73. pymupdf/mupdf-devel/include/mupdf/helpers/mu-office-lib.h +666 -0
  74. pymupdf/mupdf-devel/include/mupdf/helpers/mu-threads.h +280 -0
  75. pymupdf/mupdf-devel/include/mupdf/helpers/pkcs7-openssl.h +48 -0
  76. pymupdf/mupdf-devel/include/mupdf/html.h +53 -0
  77. pymupdf/mupdf-devel/include/mupdf/internal.h +29 -0
  78. pymupdf/mupdf-devel/include/mupdf/memento.h +425 -0
  79. pymupdf/mupdf-devel/include/mupdf/pdf/annot.h +1015 -0
  80. pymupdf/mupdf-devel/include/mupdf/pdf/clean.h +68 -0
  81. pymupdf/mupdf-devel/include/mupdf/pdf/cmap.h +145 -0
  82. pymupdf/mupdf-devel/include/mupdf/pdf/crypt.h +108 -0
  83. pymupdf/mupdf-devel/include/mupdf/pdf/document.h +902 -0
  84. pymupdf/mupdf-devel/include/mupdf/pdf/event.h +167 -0
  85. pymupdf/mupdf-devel/include/mupdf/pdf/font.h +168 -0
  86. pymupdf/mupdf-devel/include/mupdf/pdf/form.h +381 -0
  87. pymupdf/mupdf-devel/include/mupdf/pdf/image-rewriter.h +78 -0
  88. pymupdf/mupdf-devel/include/mupdf/pdf/interpret.h +518 -0
  89. pymupdf/mupdf-devel/include/mupdf/pdf/javascript.h +43 -0
  90. pymupdf/mupdf-devel/include/mupdf/pdf/name-table.h +621 -0
  91. pymupdf/mupdf-devel/include/mupdf/pdf/object.h +445 -0
  92. pymupdf/mupdf-devel/include/mupdf/pdf/page.h +327 -0
  93. pymupdf/mupdf-devel/include/mupdf/pdf/parse.h +61 -0
  94. pymupdf/mupdf-devel/include/mupdf/pdf/recolor.h +49 -0
  95. pymupdf/mupdf-devel/include/mupdf/pdf/resource.h +144 -0
  96. pymupdf/mupdf-devel/include/mupdf/pdf/xref.h +300 -0
  97. pymupdf/mupdf-devel/include/mupdf/pdf/zugferd.h +49 -0
  98. pymupdf/mupdf-devel/include/mupdf/pdf.h +59 -0
  99. pymupdf/mupdf-devel/include/mupdf/ucdn.h +464 -0
  100. pymupdf/mupdf-devel/lib/libmuthreads.lib +0 -0
  101. pymupdf/mupdf-devel/lib/mupdfcpp64.lib +0 -0
  102. pymupdf/mupdf.py +63667 -0
  103. pymupdf/mupdfcpp64.dll +0 -0
  104. pymupdf/pymupdf.py +2 -0
  105. pymupdf/table.py +2697 -0
  106. pymupdf/utils.py +1169 -0
  107. pymupdf-1.26.6.dist-info/COPYING +1 -0
  108. pymupdf-1.26.6.dist-info/METADATA +83 -0
  109. pymupdf-1.26.6.dist-info/README.md +60 -0
  110. pymupdf-1.26.6.dist-info/RECORD +112 -0
  111. pymupdf-1.26.6.dist-info/WHEEL +4 -0
  112. pymupdf-1.26.6.dist-info/entry_points.txt +3 -0
@@ -0,0 +1,646 @@
1
+ // Copyright (C) 2004-2021 Artifex Software, Inc.
2
+ //
3
+ // This file is part of MuPDF.
4
+ //
5
+ // MuPDF is free software: you can redistribute it and/or modify it under the
6
+ // terms of the GNU Affero General Public License as published by the Free
7
+ // Software Foundation, either version 3 of the License, or (at your option)
8
+ // any later version.
9
+ //
10
+ // MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY
11
+ // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12
+ // FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
13
+ // details.
14
+ //
15
+ // You should have received a copy of the GNU Affero General Public License
16
+ // along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
17
+ //
18
+ // Alternative licensing terms are available from the licensor.
19
+ // For commercial licensing, see <https://www.artifex.com/> or contact
20
+ // Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
21
+ // CA 94129, USA, for further information.
22
+
23
+ #ifndef MUPDF_FITZ_STREAM_H
24
+ #define MUPDF_FITZ_STREAM_H
25
+
26
+ #include "mupdf/fitz/system.h"
27
+ #include "mupdf/fitz/context.h"
28
+ #include "mupdf/fitz/buffer.h"
29
+
30
+ /**
31
+ Return true if the named file exists and is readable.
32
+ */
33
+ int fz_file_exists(fz_context *ctx, const char *path);
34
+
35
+ /**
36
+ fz_stream is a buffered reader capable of seeking in both
37
+ directions.
38
+
39
+ Streams are reference counted, so references must be dropped
40
+ by a call to fz_drop_stream.
41
+
42
+ Only the data between rp and wp is valid.
43
+ */
44
+ typedef struct fz_stream fz_stream;
45
+
46
+ /**
47
+ Open the named file and wrap it in a stream.
48
+
49
+ filename: Path to a file. On non-Windows machines the filename
50
+ should be exactly as it would be passed to fopen(2). On Windows
51
+ machines, the path should be UTF-8 encoded so that non-ASCII
52
+ characters can be represented. Other platforms do the encoding
53
+ as standard anyway (and in most cases, particularly for MacOS
54
+ and Linux, the encoding they use is UTF-8 anyway).
55
+ */
56
+ fz_stream *fz_open_file(fz_context *ctx, const char *filename);
57
+
58
+ /**
59
+ Do the same as fz_open_file, but delete the file upon close.
60
+ */
61
+ fz_stream *fz_open_file_autodelete(fz_context *ctx, const char *filename);
62
+
63
+ /**
64
+ Open the named file and wrap it in a stream.
65
+
66
+ Does the same as fz_open_file, but in the event the file
67
+ does not open, it will return NULL rather than throw an
68
+ exception.
69
+ */
70
+ fz_stream *fz_try_open_file(fz_context *ctx, const char *name);
71
+
72
+ #ifdef _WIN32
73
+ /**
74
+ Open the named file and wrap it in a stream.
75
+
76
+ This function is only available when compiling for Win32.
77
+
78
+ filename: Wide character path to the file as it would be given
79
+ to _wfopen().
80
+ */
81
+ fz_stream *fz_open_file_w(fz_context *ctx, const wchar_t *filename);
82
+ #endif /* _WIN32 */
83
+
84
+ /**
85
+ Return the filename (UTF-8 encoded) from which a stream was opened.
86
+
87
+ Returns NULL if the filename is not available (or the stream was
88
+ opened from a source other than a file).
89
+ */
90
+ const char *fz_stream_filename(fz_context *ctx, fz_stream *stm);
91
+
92
+ /**
93
+ Open a block of memory as a stream.
94
+
95
+ data: Pointer to start of data block. Ownership of the data
96
+ block is NOT passed in.
97
+
98
+ len: Number of bytes in data block.
99
+
100
+ Returns pointer to newly created stream. May throw exceptions on
101
+ failure to allocate.
102
+ */
103
+ fz_stream *fz_open_memory(fz_context *ctx, const unsigned char *data, size_t len);
104
+
105
+ /**
106
+ Open a buffer as a stream.
107
+
108
+ buf: The buffer to open. Ownership of the buffer is NOT passed
109
+ in (this function takes its own reference).
110
+
111
+ Returns pointer to newly created stream. May throw exceptions on
112
+ failure to allocate.
113
+ */
114
+ fz_stream *fz_open_buffer(fz_context *ctx, fz_buffer *buf);
115
+
116
+ /**
117
+ Attach a filter to a stream that will store any
118
+ characters read from the stream into the supplied buffer.
119
+
120
+ chain: The underlying stream to leech from.
121
+
122
+ buf: The buffer into which the read data should be appended.
123
+ The buffer will be resized as required.
124
+
125
+ Returns pointer to newly created stream. May throw exceptions on
126
+ failure to allocate.
127
+ */
128
+ fz_stream *fz_open_leecher(fz_context *ctx, fz_stream *chain, fz_buffer *buf);
129
+
130
+ /**
131
+ Increments the reference count for a stream. Returns the same
132
+ pointer.
133
+
134
+ Never throws exceptions.
135
+ */
136
+ fz_stream *fz_keep_stream(fz_context *ctx, fz_stream *stm);
137
+
138
+ /**
139
+ Decrements the reference count for a stream.
140
+
141
+ When the reference count for the stream hits zero, frees the
142
+ storage used for the fz_stream itself, and (usually)
143
+ releases the underlying resources that the stream is based upon
144
+ (depends on the method used to open the stream initially).
145
+ */
146
+ void fz_drop_stream(fz_context *ctx, fz_stream *stm);
147
+
148
+ /**
149
+ return the current reading position within a stream
150
+ */
151
+ int64_t fz_tell(fz_context *ctx, fz_stream *stm);
152
+
153
+ /**
154
+ Seek within a stream.
155
+
156
+ stm: The stream to seek within.
157
+
158
+ offset: The offset to seek to.
159
+
160
+ whence: From where the offset is measured (see fseek).
161
+ SEEK_SET - start of stream.
162
+ SEEK_CUR - current position.
163
+ SEEK_END - end of stream.
164
+
165
+ */
166
+ void fz_seek(fz_context *ctx, fz_stream *stm, int64_t offset, int whence);
167
+
168
+ /**
169
+ Read from a stream into a given data block.
170
+
171
+ stm: The stream to read from.
172
+
173
+ data: The data block to read into.
174
+
175
+ len: The length of the data block (in bytes).
176
+
177
+ Returns the number of bytes read. May throw exceptions.
178
+ */
179
+ size_t fz_read(fz_context *ctx, fz_stream *stm, unsigned char *data, size_t len);
180
+
181
+ /**
182
+ Read from a stream discarding data.
183
+
184
+ stm: The stream to read from.
185
+
186
+ len: The number of bytes to read.
187
+
188
+ Returns the number of bytes read. May throw exceptions.
189
+ */
190
+ size_t fz_skip(fz_context *ctx, fz_stream *stm, size_t len);
191
+
192
+ /**
193
+ Read all of a stream into a buffer.
194
+
195
+ stm: The stream to read from
196
+
197
+ initial: Suggested initial size for the buffer.
198
+
199
+ Returns a buffer created from reading from the stream. May throw
200
+ exceptions on failure to allocate.
201
+ */
202
+ fz_buffer *fz_read_all(fz_context *ctx, fz_stream *stm, size_t initial);
203
+
204
+ /**
205
+ Read all the contents of a file into a buffer.
206
+ */
207
+ fz_buffer *fz_read_file(fz_context *ctx, const char *filename);
208
+
209
+ /**
210
+ Read all the contents of a file into a buffer.
211
+
212
+ Returns NULL if the file does not exist, otherwise
213
+ behaves exactly as fz_read_file.
214
+ */
215
+ fz_buffer *fz_try_read_file(fz_context *ctx, const char *filename);
216
+
217
+ /**
218
+ fz_read_[u]int(16|24|32|64)(_le)?
219
+
220
+ Read a 16/32/64 bit signed/unsigned integer from stream,
221
+ in big or little-endian byte orders.
222
+
223
+ Throws an exception if EOF is encountered.
224
+ */
225
+ uint16_t fz_read_uint16(fz_context *ctx, fz_stream *stm);
226
+ uint32_t fz_read_uint24(fz_context *ctx, fz_stream *stm);
227
+ uint32_t fz_read_uint32(fz_context *ctx, fz_stream *stm);
228
+ uint64_t fz_read_uint64(fz_context *ctx, fz_stream *stm);
229
+
230
+ uint16_t fz_read_uint16_le(fz_context *ctx, fz_stream *stm);
231
+ uint32_t fz_read_uint24_le(fz_context *ctx, fz_stream *stm);
232
+ uint32_t fz_read_uint32_le(fz_context *ctx, fz_stream *stm);
233
+ uint64_t fz_read_uint64_le(fz_context *ctx, fz_stream *stm);
234
+
235
+ int16_t fz_read_int16(fz_context *ctx, fz_stream *stm);
236
+ int32_t fz_read_int32(fz_context *ctx, fz_stream *stm);
237
+ int64_t fz_read_int64(fz_context *ctx, fz_stream *stm);
238
+
239
+ int16_t fz_read_int16_le(fz_context *ctx, fz_stream *stm);
240
+ int32_t fz_read_int32_le(fz_context *ctx, fz_stream *stm);
241
+ int64_t fz_read_int64_le(fz_context *ctx, fz_stream *stm);
242
+
243
+ float fz_read_float_le(fz_context *ctx, fz_stream *stm);
244
+ float fz_read_float(fz_context *ctx, fz_stream *stm);
245
+
246
+ /**
247
+ Read a null terminated string from the stream into
248
+ a buffer of a given length. The buffer will be null terminated.
249
+ Throws on failure (including the failure to fit the entire
250
+ string including the terminator into the buffer).
251
+ */
252
+ void fz_read_string(fz_context *ctx, fz_stream *stm, char *buffer, int len);
253
+
254
+ /**
255
+ Read a utf-8 rune from a stream.
256
+
257
+ In the event of encountering badly formatted utf-8 codes
258
+ (such as a leading code with an unexpected number of following
259
+ codes) no error/exception is given, but undefined values may be
260
+ returned.
261
+ */
262
+ int fz_read_rune(fz_context *ctx, fz_stream *in);
263
+
264
+ /**
265
+ Read a utf-16 rune from a stream. (little endian and
266
+ big endian respectively).
267
+
268
+ In the event of encountering badly formatted utf-16 codes
269
+ (mismatched surrogates) no error/exception is given, but
270
+ undefined values may be returned.
271
+ */
272
+ int fz_read_utf16_le(fz_context *ctx, fz_stream *stm);
273
+ int fz_read_utf16_be(fz_context *ctx, fz_stream *stm);
274
+
275
+ /**
276
+ A function type for use when implementing
277
+ fz_streams. The supplied function of this type is called
278
+ whenever data is required, and the current buffer is empty.
279
+
280
+ stm: The stream to operate on.
281
+
282
+ max: a hint as to the maximum number of bytes that the caller
283
+ needs to be ready immediately. Can safely be ignored.
284
+
285
+ Returns -1 if there is no more data in the stream. Otherwise,
286
+ the function should find its internal state using stm->state,
287
+ refill its buffer, update stm->rp and stm->wp to point to the
288
+ start and end of the new data respectively, and then
289
+ "return *stm->rp++".
290
+ */
291
+ typedef int (fz_stream_next_fn)(fz_context *ctx, fz_stream *stm, size_t max);
292
+
293
+ /**
294
+ A function type for use when implementing
295
+ fz_streams. The supplied function of this type is called
296
+ when the stream is dropped, to release the stream specific
297
+ state information.
298
+
299
+ state: The stream state to release.
300
+ */
301
+ typedef void (fz_stream_drop_fn)(fz_context *ctx, void *state);
302
+
303
+ /**
304
+ A function type for use when implementing
305
+ fz_streams. The supplied function of this type is called when
306
+ fz_seek is requested, and the arguments are as defined for
307
+ fz_seek.
308
+
309
+ The stream can find it's private state in stm->state.
310
+ */
311
+ typedef void (fz_stream_seek_fn)(fz_context *ctx, fz_stream *stm, int64_t offset, int whence);
312
+
313
+ struct fz_stream
314
+ {
315
+ int refs;
316
+ int error;
317
+ int eof;
318
+ int progressive;
319
+ int64_t pos;
320
+ int avail;
321
+ int bits;
322
+ unsigned char *rp, *wp;
323
+ void *state;
324
+ fz_stream_next_fn *next;
325
+ fz_stream_drop_fn *drop;
326
+ fz_stream_seek_fn *seek;
327
+ };
328
+
329
+ /**
330
+ Create a new stream object with the given
331
+ internal state and function pointers.
332
+
333
+ state: Internal state (opaque to everything but implementation).
334
+
335
+ next: Should provide the next set of bytes (up to max) of stream
336
+ data. Return the number of bytes read, or EOF when there is no
337
+ more data.
338
+
339
+ drop: Should clean up and free the internal state. May not
340
+ throw exceptions.
341
+ */
342
+ fz_stream *fz_new_stream(fz_context *ctx, void *state, fz_stream_next_fn *next, fz_stream_drop_fn *drop);
343
+
344
+ /**
345
+ Attempt to read a stream into a buffer. If truncated
346
+ is NULL behaves as fz_read_all, sets a truncated flag in case of
347
+ error.
348
+
349
+ stm: The stream to read from.
350
+
351
+ initial: Suggested initial size for the buffer.
352
+
353
+ truncated: Flag to store success/failure indication in.
354
+
355
+ worst_case: 0 for unknown, otherwise an upper bound for the
356
+ size of the stream.
357
+
358
+ Returns a buffer created from reading from the stream.
359
+ */
360
+ fz_buffer *fz_read_best(fz_context *ctx, fz_stream *stm, size_t initial, int *truncated, size_t worst_case);
361
+
362
+ /**
363
+ Read a line from stream into the buffer until either a
364
+ terminating newline or EOF, which it replaces with a null byte
365
+ ('\0').
366
+
367
+ Returns buf on success, and NULL when end of file occurs while
368
+ no characters have been read.
369
+ */
370
+ char *fz_read_line(fz_context *ctx, fz_stream *stm, char *buf, size_t max);
371
+
372
+ /**
373
+ Skip over a given string in a stream. Return 0 if successfully
374
+ skipped, non-zero otherwise. As many characters will be skipped
375
+ over as matched in the string.
376
+ */
377
+ int fz_skip_string(fz_context *ctx, fz_stream *stm, const char *str);
378
+
379
+ /**
380
+ Skip over whitespace (bytes <= 32) in a stream.
381
+ */
382
+ void fz_skip_space(fz_context *ctx, fz_stream *stm);
383
+
384
+ /**
385
+ Ask how many bytes are available immediately from
386
+ a given stream.
387
+
388
+ stm: The stream to read from.
389
+
390
+ max: A hint for the underlying stream; the maximum number of
391
+ bytes that we are sure we will want to read. If you do not know
392
+ this number, give 1.
393
+
394
+ Returns the number of bytes immediately available between the
395
+ read and write pointers. This number is guaranteed only to be 0
396
+ if we have hit EOF. The number of bytes returned here need have
397
+ no relation to max (could be larger, could be smaller).
398
+ */
399
+ static inline size_t fz_available(fz_context *ctx, fz_stream *stm, size_t max)
400
+ {
401
+ size_t len = stm->wp - stm->rp;
402
+ int c = EOF;
403
+
404
+ if (len)
405
+ return len;
406
+ if (stm->eof)
407
+ return 0;
408
+
409
+ fz_try(ctx)
410
+ c = stm->next(ctx, stm, max);
411
+ fz_catch(ctx)
412
+ {
413
+ fz_rethrow_if(ctx, FZ_ERROR_TRYLATER);
414
+ fz_report_error(ctx);
415
+ fz_warn(ctx, "read error; treating as end of file");
416
+ stm->error = 1;
417
+ c = EOF;
418
+ }
419
+ if (c == EOF)
420
+ {
421
+ stm->eof = 1;
422
+ return 0;
423
+ }
424
+ stm->rp--;
425
+ return stm->wp - stm->rp;
426
+ }
427
+
428
+ /**
429
+ Read the next byte from a stream.
430
+
431
+ stm: The stream t read from.
432
+
433
+ Returns -1 for end of stream, or the next byte. May
434
+ throw exceptions.
435
+ */
436
+ static inline int fz_read_byte(fz_context *ctx, fz_stream *stm)
437
+ {
438
+ int c = EOF;
439
+
440
+ if (stm->rp != stm->wp)
441
+ return *stm->rp++;
442
+ if (stm->eof)
443
+ return EOF;
444
+ fz_try(ctx)
445
+ c = stm->next(ctx, stm, 1);
446
+ fz_catch(ctx)
447
+ {
448
+ fz_rethrow_if(ctx, FZ_ERROR_TRYLATER);
449
+ fz_report_error(ctx);
450
+ fz_warn(ctx, "read error; treating as end of file");
451
+ stm->error = 1;
452
+ c = EOF;
453
+ }
454
+ if (c == EOF)
455
+ stm->eof = 1;
456
+ return c;
457
+ }
458
+
459
+ /**
460
+ Peek at the next byte in a stream.
461
+
462
+ stm: The stream to peek at.
463
+
464
+ Returns -1 for EOF, or the next byte that will be read.
465
+ */
466
+ static inline int fz_peek_byte(fz_context *ctx, fz_stream *stm)
467
+ {
468
+ int c = EOF;
469
+
470
+ if (stm->rp != stm->wp)
471
+ return *stm->rp;
472
+ if (stm->eof)
473
+ return EOF;
474
+
475
+ fz_try(ctx)
476
+ {
477
+ c = stm->next(ctx, stm, 1);
478
+ if (c != EOF)
479
+ stm->rp--;
480
+ }
481
+ fz_catch(ctx)
482
+ {
483
+ fz_rethrow_if(ctx, FZ_ERROR_TRYLATER);
484
+ fz_report_error(ctx);
485
+ fz_warn(ctx, "read error; treating as end of file");
486
+ stm->error = 1;
487
+ c = EOF;
488
+ }
489
+ if (c == EOF)
490
+ stm->eof = 1;
491
+ return c;
492
+ }
493
+
494
+ /**
495
+ Unread the single last byte successfully
496
+ read from a stream. Do not call this without having
497
+ successfully read a byte.
498
+
499
+ stm: The stream to operate upon.
500
+ */
501
+ static inline void fz_unread_byte(fz_context *ctx FZ_UNUSED, fz_stream *stm)
502
+ {
503
+ stm->rp--;
504
+ }
505
+
506
+ /**
507
+ Query if the stream has reached EOF (during normal bytewise
508
+ reading).
509
+
510
+ See fz_is_eof_bits for the equivalent function for bitwise
511
+ reading.
512
+ */
513
+ static inline int fz_is_eof(fz_context *ctx, fz_stream *stm)
514
+ {
515
+ if (stm->rp == stm->wp)
516
+ {
517
+ if (stm->eof)
518
+ return 1;
519
+ return fz_peek_byte(ctx, stm) == EOF;
520
+ }
521
+ return 0;
522
+ }
523
+
524
+ /**
525
+ Read the next n bits from a stream (assumed to
526
+ be packed most significant bit first).
527
+
528
+ stm: The stream to read from.
529
+
530
+ n: The number of bits to read, between 1 and 8*sizeof(int)
531
+ inclusive.
532
+
533
+ Returns -1 for EOF, or the required number of bits.
534
+ */
535
+ static inline unsigned int fz_read_bits(fz_context *ctx, fz_stream *stm, int n)
536
+ {
537
+ int x;
538
+
539
+ if (n <= stm->avail)
540
+ {
541
+ stm->avail -= n;
542
+ x = (stm->bits >> stm->avail) & ((1 << n) - 1);
543
+ }
544
+ else
545
+ {
546
+ x = stm->bits & ((1 << stm->avail) - 1);
547
+ n -= stm->avail;
548
+ stm->avail = 0;
549
+
550
+ while (n > 8)
551
+ {
552
+ x = (x << 8) | fz_read_byte(ctx, stm);
553
+ n -= 8;
554
+ }
555
+
556
+ if (n > 0)
557
+ {
558
+ stm->bits = fz_read_byte(ctx, stm);
559
+ stm->avail = 8 - n;
560
+ x = (x << n) | (stm->bits >> stm->avail);
561
+ }
562
+ }
563
+
564
+ return x;
565
+ }
566
+
567
+ /**
568
+ Read the next n bits from a stream (assumed to
569
+ be packed least significant bit first).
570
+
571
+ stm: The stream to read from.
572
+
573
+ n: The number of bits to read, between 1 and 8*sizeof(int)
574
+ inclusive.
575
+
576
+ Returns (unsigned int)-1 for EOF, or the required number of bits.
577
+ */
578
+ static inline unsigned int fz_read_rbits(fz_context *ctx, fz_stream *stm, int n)
579
+ {
580
+ int x;
581
+
582
+ if (n <= stm->avail)
583
+ {
584
+ x = stm->bits & ((1 << n) - 1);
585
+ stm->avail -= n;
586
+ stm->bits = stm->bits >> n;
587
+ }
588
+ else
589
+ {
590
+ unsigned int used = 0;
591
+
592
+ x = stm->bits & ((1 << stm->avail) - 1);
593
+ n -= stm->avail;
594
+ used = stm->avail;
595
+ stm->avail = 0;
596
+
597
+ while (n > 8)
598
+ {
599
+ x = (fz_read_byte(ctx, stm) << used) | x;
600
+ n -= 8;
601
+ used += 8;
602
+ }
603
+
604
+ if (n > 0)
605
+ {
606
+ stm->bits = fz_read_byte(ctx, stm);
607
+ x = ((stm->bits & ((1 << n) - 1)) << used) | x;
608
+ stm->avail = 8 - n;
609
+ stm->bits = stm->bits >> n;
610
+ }
611
+ }
612
+
613
+ return x;
614
+ }
615
+
616
+ /**
617
+ Called after reading bits to tell the stream
618
+ that we are about to return to reading bytewise. Resyncs
619
+ the stream to whole byte boundaries.
620
+ */
621
+ static inline void fz_sync_bits(fz_context *ctx FZ_UNUSED, fz_stream *stm)
622
+ {
623
+ stm->avail = 0;
624
+ }
625
+
626
+ /**
627
+ Query if the stream has reached EOF (during bitwise
628
+ reading).
629
+
630
+ See fz_is_eof for the equivalent function for bytewise
631
+ reading.
632
+ */
633
+ static inline int fz_is_eof_bits(fz_context *ctx, fz_stream *stm)
634
+ {
635
+ return fz_is_eof(ctx, stm) && (stm->avail == 0 || stm->bits == EOF);
636
+ }
637
+
638
+ /* Implementation details: subject to change. */
639
+
640
+ /**
641
+ Create a stream from a FILE * that will not be closed
642
+ when the stream is dropped.
643
+ */
644
+ fz_stream *fz_open_file_ptr_no_close(fz_context *ctx, FILE *file);
645
+
646
+ #endif