mupdf
Loading...
Searching...
No Matches
stream.h
Go to the documentation of this file.
1// Copyright (C) 2004-2026 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
33int fz_file_exists(fz_context *ctx, const char *path);
34
44typedef struct fz_stream fz_stream;
45
56fz_stream *fz_open_file(fz_context *ctx, const char *filename);
57
61fz_stream *fz_open_file_autodelete(fz_context *ctx, const char *filename);
62
70fz_stream *fz_try_open_file(fz_context *ctx, const char *name);
71
72#ifdef _WIN32
81fz_stream *fz_open_file_w(fz_context *ctx, const wchar_t *filename);
82#endif /* _WIN32 */
83
90const char *fz_stream_filename(fz_context *ctx, fz_stream *stm);
91
103fz_stream *fz_open_memory(fz_context *ctx, const unsigned char *data, size_t len);
104
115
129
137
147
151int64_t fz_tell(fz_context *ctx, fz_stream *stm);
152
166void fz_seek(fz_context *ctx, fz_stream *stm, int64_t offset, int whence);
167
179size_t fz_read(fz_context *ctx, fz_stream *stm, unsigned char *data, size_t len);
180
190size_t fz_skip(fz_context *ctx, fz_stream *stm, size_t len);
191
202fz_buffer *fz_read_all(fz_context *ctx, fz_stream *stm, size_t initial);
203
207fz_buffer *fz_read_file(fz_context *ctx, const char *filename);
208
215fz_buffer *fz_try_read_file(fz_context *ctx, const char *filename);
216
221char *fz_read_text_file(fz_context *ctx, const char *filename);
222
235
240
244
248
251
258void fz_read_string(fz_context *ctx, fz_stream *stm, char *buffer, int len);
259
269
280
297typedef int (fz_stream_next_fn)(fz_context *ctx, fz_stream *stm, size_t max);
298
307typedef void (fz_stream_drop_fn)(fz_context *ctx, void *state);
308
317typedef void (fz_stream_seek_fn)(fz_context *ctx, fz_stream *stm, int64_t offset, int whence);
318
334
349
366fz_buffer *fz_read_best(fz_context *ctx, fz_stream *stm, size_t initial, int *truncated, size_t worst_case);
367
376char *fz_read_line(fz_context *ctx, fz_stream *stm, char *buf, size_t max);
377
383int fz_skip_string(fz_context *ctx, fz_stream *stm, const char *str);
384
389
405static inline size_t fz_available(fz_context *ctx, fz_stream *stm, size_t max)
406{
407 size_t len = stm->wp - stm->rp;
408 int c = EOF;
409
410 if (len)
411 return len;
412 if (stm->eof)
413 return 0;
414
415 fz_try(ctx)
416 c = stm->next(ctx, stm, max);
417 fz_catch(ctx)
418 {
420 fz_report_error(ctx);
421 fz_warn(ctx, "read error; treating as end of file");
422 stm->error = 1;
423 c = EOF;
424 }
425 if (c == EOF)
426 {
427 stm->eof = 1;
428 return 0;
429 }
430 stm->rp--;
431 return stm->wp - stm->rp;
432}
433
442static inline int fz_read_byte(fz_context *ctx, fz_stream *stm)
443{
444 int c = EOF;
445
446 if (stm->rp != stm->wp)
447 return *stm->rp++;
448 if (stm->eof)
449 return EOF;
450 fz_try(ctx)
451 c = stm->next(ctx, stm, 1);
452 fz_catch(ctx)
453 {
455 fz_report_error(ctx);
456 fz_warn(ctx, "read error; treating as end of file");
457 stm->error = 1;
458 c = EOF;
459 }
460 if (c == EOF)
461 stm->eof = 1;
462 return c;
463}
464
472static inline int fz_peek_byte(fz_context *ctx, fz_stream *stm)
473{
474 int c = EOF;
475
476 if (stm->rp != stm->wp)
477 return *stm->rp;
478 if (stm->eof)
479 return EOF;
480
481 fz_try(ctx)
482 {
483 c = stm->next(ctx, stm, 1);
484 if (c != EOF)
485 stm->rp--;
486 }
487 fz_catch(ctx)
488 {
490 fz_report_error(ctx);
491 fz_warn(ctx, "read error; treating as end of file");
492 stm->error = 1;
493 c = EOF;
494 }
495 if (c == EOF)
496 stm->eof = 1;
497 return c;
498}
499
507static inline void fz_unread_byte(fz_context *ctx FZ_UNUSED, fz_stream *stm)
508{
509 stm->rp--;
510}
511
519static inline int fz_is_eof(fz_context *ctx, fz_stream *stm)
520{
521 if (stm->rp == stm->wp)
522 {
523 if (stm->eof)
524 return 1;
525 return fz_peek_byte(ctx, stm) == EOF;
526 }
527 return 0;
528}
529
541static inline unsigned int fz_read_bits(fz_context *ctx, fz_stream *stm, int n)
542{
543 unsigned int x;
544
545 if (n <= stm->avail)
546 {
547 stm->avail -= n;
548 x = (stm->bits >> stm->avail) & ((1 << n) - 1);
549 }
550 else
551 {
552 x = stm->bits & ((1 << stm->avail) - 1);
553 n -= stm->avail;
554 stm->avail = 0;
555
556 while (n > 8)
557 {
558 x = (x << 8) | fz_read_byte(ctx, stm);
559 n -= 8;
560 }
561
562 if (n > 0)
563 {
564 stm->bits = fz_read_byte(ctx, stm);
565 stm->avail = 8 - n;
566 x = (x << n) | (stm->bits >> stm->avail);
567 }
568 }
569
570 return x;
571}
572
584static inline unsigned int fz_read_rbits(fz_context *ctx, fz_stream *stm, int n)
585{
586 int x;
587
588 if (n <= stm->avail)
589 {
590 x = stm->bits & ((1 << n) - 1);
591 stm->avail -= n;
592 stm->bits = stm->bits >> n;
593 }
594 else
595 {
596 unsigned int used = 0;
597
598 x = stm->bits & ((1 << stm->avail) - 1);
599 n -= stm->avail;
600 used = stm->avail;
601 stm->avail = 0;
602
603 while (n > 8)
604 {
605 x = (fz_read_byte(ctx, stm) << used) | x;
606 n -= 8;
607 used += 8;
608 }
609
610 if (n > 0)
611 {
612 stm->bits = fz_read_byte(ctx, stm);
613 x = ((stm->bits & ((1 << n) - 1)) << used) | x;
614 stm->avail = 8 - n;
615 stm->bits = stm->bits >> n;
616 }
617 }
618
619 return x;
620}
621
627static inline void fz_sync_bits(fz_context *ctx FZ_UNUSED, fz_stream *stm)
628{
629 stm->avail = 0;
630}
631
639static inline int fz_is_eof_bits(fz_context *ctx, fz_stream *stm)
640{
641 return fz_is_eof(ctx, stm) && (stm->avail == 0 || stm->bits == EOF);
642}
643
644/* Implementation details: subject to change. */
645
651
652#endif
void fz_rethrow_if(fz_context *ctx, int errcode)
#define fz_catch(ctx)
Definition context.h:65
#define fz_try(ctx)
Definition context.h:63
@ FZ_ERROR_TRYLATER
Definition context.h:232
void fz_report_error(fz_context *ctx)
void fz_warn(fz_context *ctx, const char *fmt,...) FZ_PRINTFLIKE(2
fz_stream * fz_open_memory(fz_context *ctx, const unsigned char *data, size_t len)
fz_stream * fz_open_file_autodelete(fz_context *ctx, const char *filename)
fz_buffer * fz_read_file(fz_context *ctx, const char *filename)
const char * fz_stream_filename(fz_context *ctx, fz_stream *stm)
int16_t fz_read_int16(fz_context *ctx, fz_stream *stm)
int64_t fz_tell(fz_context *ctx, fz_stream *stm)
void fz_skip_space(fz_context *ctx, fz_stream *stm)
uint32_t fz_read_uint32_le(fz_context *ctx, fz_stream *stm)
fz_stream * fz_new_stream(fz_context *ctx, void *state, fz_stream_next_fn *next, fz_stream_drop_fn *drop)
int64_t fz_read_int64_le(fz_context *ctx, fz_stream *stm)
fz_stream * fz_open_leecher(fz_context *ctx, fz_stream *chain, fz_buffer *buf)
float fz_read_float(fz_context *ctx, fz_stream *stm)
void fz_stream_seek_fn(fz_context *ctx, fz_stream *stm, int64_t offset, int whence)
Definition stream.h:317
float fz_read_float_le(fz_context *ctx, fz_stream *stm)
int32_t fz_read_int32_le(fz_context *ctx, fz_stream *stm)
int64_t fz_read_int64(fz_context *ctx, fz_stream *stm)
uint32_t fz_read_uint24(fz_context *ctx, fz_stream *stm)
fz_buffer * fz_read_best(fz_context *ctx, fz_stream *stm, size_t initial, int *truncated, size_t worst_case)
uint64_t fz_read_uint64(fz_context *ctx, fz_stream *stm)
int fz_stream_next_fn(fz_context *ctx, fz_stream *stm, size_t max)
Definition stream.h:297
char * fz_read_text_file(fz_context *ctx, const char *filename)
void fz_drop_stream(fz_context *ctx, fz_stream *stm)
int fz_read_rune(fz_context *ctx, fz_stream *in)
fz_stream * fz_keep_stream(fz_context *ctx, fz_stream *stm)
void fz_seek(fz_context *ctx, fz_stream *stm, int64_t offset, int whence)
fz_buffer * fz_try_read_file(fz_context *ctx, const char *filename)
void fz_stream_drop_fn(fz_context *ctx, void *state)
Definition stream.h:307
uint32_t fz_read_uint32(fz_context *ctx, fz_stream *stm)
uint64_t fz_read_uint64_le(fz_context *ctx, fz_stream *stm)
void fz_read_string(fz_context *ctx, fz_stream *stm, char *buffer, int len)
fz_stream * fz_open_buffer(fz_context *ctx, fz_buffer *buf)
int fz_skip_string(fz_context *ctx, fz_stream *stm, const char *str)
fz_stream * fz_open_file_ptr_no_close(fz_context *ctx, FILE *file)
char * fz_read_line(fz_context *ctx, fz_stream *stm, char *buf, size_t max)
int fz_file_exists(fz_context *ctx, const char *path)
fz_stream * fz_try_open_file(fz_context *ctx, const char *name)
int fz_read_utf16_be(fz_context *ctx, fz_stream *stm)
fz_buffer * fz_read_all(fz_context *ctx, fz_stream *stm, size_t initial)
uint16_t fz_read_uint16(fz_context *ctx, fz_stream *stm)
uint32_t fz_read_uint24_le(fz_context *ctx, fz_stream *stm)
int fz_read_utf16_le(fz_context *ctx, fz_stream *stm)
size_t fz_read(fz_context *ctx, fz_stream *stm, unsigned char *data, size_t len)
int16_t fz_read_int16_le(fz_context *ctx, fz_stream *stm)
uint16_t fz_read_uint16_le(fz_context *ctx, fz_stream *stm)
int32_t fz_read_int32(fz_context *ctx, fz_stream *stm)
size_t fz_skip(fz_context *ctx, fz_stream *stm, size_t len)
fz_stream * fz_open_file(fz_context *ctx, const char *filename)
Definition buffer.h:41
Definition context.h:886
Definition stream.h:320
void * state
Definition stream.h:329
unsigned char * rp
Definition stream.h:328
int error
Definition stream.h:322
int refs
Definition stream.h:321
fz_stream_next_fn * next
Definition stream.h:330
fz_stream_seek_fn * seek
Definition stream.h:332
int progressive
Definition stream.h:324
int bits
Definition stream.h:327
unsigned char * wp
Definition stream.h:328
int eof
Definition stream.h:323
int64_t pos
Definition stream.h:325
fz_stream_drop_fn * drop
Definition stream.h:331
int avail
Definition stream.h:326
#define FZ_UNUSED
Definition system.h:278
#define EOF
Definition system.h:165