Coverage Report

Created: 2026-11-12 07:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/mupdf/source/fitz/stream-read.c
Line
Count
Source
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
#include "mupdf/fitz.h"
24
25
#include <string.h>
26
27
7.82k
#define MIN_BOMB (100 << 20)
28
29
size_t
30
fz_read(fz_context *ctx, fz_stream *stm, unsigned char *buf, size_t len)
31
25.9k
{
32
25.9k
  size_t count, n;
33
34
25.9k
  count = 0;
35
25.9k
  do
36
254k
  {
37
254k
    n = fz_available(ctx, stm, len);
38
254k
    if (n > len)
39
14.7k
      n = len;
40
254k
    if (n == 0)
41
7.84k
      break;
42
43
246k
    memcpy(buf, stm->rp, n);
44
246k
    stm->rp += n;
45
246k
    buf += n;
46
246k
    count += n;
47
246k
    len -= n;
48
246k
  }
49
246k
  while (len > 0);
50
51
25.9k
  return count;
52
25.9k
}
53
54
static unsigned char skip_buf[4096];
55
56
size_t fz_skip(fz_context *ctx, fz_stream *stm, size_t len)
57
0
{
58
0
  size_t count, l, total = 0;
59
60
0
  while (len)
61
0
  {
62
0
    l = len;
63
0
    if (l > sizeof(skip_buf))
64
0
      l = sizeof(skip_buf);
65
0
    count = fz_read(ctx, stm, skip_buf, l);
66
0
    total += count;
67
0
    if (count < l)
68
0
      break;
69
0
    len -= count;
70
0
  }
71
0
  return total;
72
0
}
73
74
fz_buffer *
75
fz_read_all(fz_context *ctx, fz_stream *stm, size_t initial)
76
5
{
77
5
  return fz_read_best(ctx, stm, initial, NULL, 0);
78
5
}
79
80
fz_buffer *
81
fz_read_best(fz_context *ctx, fz_stream *stm, size_t initial, int *truncated, size_t worst_case)
82
3.91k
{
83
3.91k
  fz_buffer *buf = NULL;
84
3.91k
  int check_bomb = (initial > 0);
85
3.91k
  size_t n;
86
87
3.91k
  fz_var(buf);
88
89
3.91k
  if (truncated)
90
0
    *truncated = 0;
91
92
3.91k
  if (worst_case == 0)
93
705
    worst_case = initial * 200;
94
3.91k
  if (worst_case < MIN_BOMB)
95
3.90k
    worst_case = MIN_BOMB;
96
97
7.83k
  fz_try(ctx)
98
7.83k
  {
99
3.91k
    if (initial < 1024)
100
1.58k
      initial = 1024;
101
102
3.91k
    buf = fz_new_buffer(ctx, initial+1);
103
104
8.00k
    while (1)
105
8.00k
    {
106
8.00k
      if (buf->len == buf->cap)
107
176
        fz_grow_buffer(ctx, buf);
108
109
8.00k
      if (check_bomb && buf->len > worst_case)
110
0
        fz_throw(ctx, FZ_ERROR_FORMAT, "compression bomb detected");
111
112
8.00k
      n = fz_read(ctx, stm, buf->data + buf->len, buf->cap - buf->len);
113
8.00k
      if (n == 0)
114
3.91k
        break;
115
116
4.09k
      buf->len += n;
117
4.09k
    }
118
3.91k
  }
119
7.83k
  fz_catch(ctx)
120
0
  {
121
0
    if (fz_caught(ctx) == FZ_ERROR_TRYLATER || fz_caught(ctx) == FZ_ERROR_SYSTEM)
122
0
    {
123
0
      fz_drop_buffer(ctx, buf);
124
0
      fz_rethrow(ctx);
125
0
    }
126
0
    if (truncated)
127
0
    {
128
0
      *truncated = 1;
129
0
      fz_report_error(ctx);
130
0
    }
131
0
    else
132
0
    {
133
0
      fz_drop_buffer(ctx, buf);
134
0
      fz_rethrow(ctx);
135
0
    }
136
0
  }
137
138
3.91k
  return buf;
139
3.91k
}
140
141
char *
142
fz_read_line(fz_context *ctx, fz_stream *stm, char *mem, size_t n)
143
47
{
144
47
  char *s = mem;
145
47
  int c = EOF;
146
250
  while (n > 1)
147
250
  {
148
250
    c = fz_read_byte(ctx, stm);
149
250
    if (c == EOF)
150
0
      break;
151
250
    if (c == '\r') {
152
12
      c = fz_peek_byte(ctx, stm);
153
12
      if (c == '\n')
154
12
        fz_read_byte(ctx, stm);
155
12
      break;
156
12
    }
157
238
    if (c == '\n')
158
35
      break;
159
203
    *s++ = c;
160
203
    n--;
161
203
  }
162
47
  if (n)
163
47
    *s = '\0';
164
47
  return (s == mem && c == EOF) ? NULL : mem;
165
47
}
166
167
int64_t
168
fz_tell(fz_context *ctx, fz_stream *stm)
169
1.39M
{
170
1.39M
  return stm->pos - (stm->wp - stm->rp);
171
1.39M
}
172
173
void
174
fz_seek(fz_context *ctx, fz_stream *stm, int64_t offset, int whence)
175
161k
{
176
161k
  stm->avail = 0; /* Reset bit reading */
177
161k
  if (stm->seek)
178
61.5k
  {
179
61.5k
    if (whence == 1)
180
0
    {
181
0
      offset += fz_tell(ctx, stm);
182
0
      whence = 0;
183
0
    }
184
61.5k
    stm->seek(ctx, stm, offset, whence);
185
61.5k
    stm->eof = 0;
186
61.5k
  }
187
99.5k
  else if (whence != 2)
188
99.5k
  {
189
99.5k
    if (whence == 0)
190
99.5k
      offset -= fz_tell(ctx, stm);
191
99.5k
    if (offset < 0)
192
0
      fz_warn(ctx, "cannot seek backwards");
193
    /* dog slow, but rare enough */
194
99.6k
    while (offset-- > 0)
195
145
    {
196
145
      if (fz_read_byte(ctx, stm) == EOF)
197
0
      {
198
0
        fz_warn(ctx, "seek failed");
199
0
        break;
200
0
      }
201
145
    }
202
99.5k
  }
203
0
  else
204
0
    fz_warn(ctx, "cannot seek");
205
161k
}
206
207
fz_buffer *
208
fz_read_file(fz_context *ctx, const char *filename)
209
0
{
210
0
  fz_stream *stm;
211
0
  fz_buffer *buf = NULL;
212
213
0
  fz_var(buf);
214
215
0
  stm = fz_open_file(ctx, filename);
216
0
  fz_try(ctx)
217
0
  {
218
0
    buf = fz_read_all(ctx, stm, 0);
219
0
  }
220
0
  fz_always(ctx)
221
0
  {
222
0
    fz_drop_stream(ctx, stm);
223
0
  }
224
0
  fz_catch(ctx)
225
0
  {
226
0
    fz_rethrow(ctx);
227
0
  }
228
229
0
  return buf;
230
0
}
231
232
char *
233
fz_read_text_file(fz_context *ctx, const char *filename)
234
0
{
235
0
  fz_buffer *buf;
236
0
  unsigned char *data;
237
238
0
  buf = fz_read_file(ctx, filename);
239
0
  fz_try(ctx)
240
0
  {
241
0
    fz_terminate_buffer(ctx, buf);
242
0
    (void) fz_buffer_extract(ctx, buf, &data);
243
0
  }
244
0
  fz_always(ctx)
245
0
    fz_drop_buffer(ctx, buf);
246
0
  fz_catch(ctx)
247
0
    fz_rethrow(ctx);
248
0
  return (char*)data;
249
0
}
250
251
fz_buffer *
252
fz_try_read_file(fz_context *ctx, const char *filename)
253
0
{
254
0
  fz_stream *stm;
255
0
  fz_buffer *buf = NULL;
256
257
0
  fz_var(buf);
258
259
0
  stm = fz_try_open_file(ctx, filename);
260
0
  if (stm == NULL)
261
0
    return NULL;
262
0
  fz_try(ctx)
263
0
  {
264
0
    buf = fz_read_all(ctx, stm, 0);
265
0
  }
266
0
  fz_always(ctx)
267
0
  {
268
0
    fz_drop_stream(ctx, stm);
269
0
  }
270
0
  fz_catch(ctx)
271
0
  {
272
0
    fz_rethrow(ctx);
273
0
  }
274
275
0
  return buf;
276
0
}
277
278
uint16_t fz_read_uint16(fz_context *ctx, fz_stream *stm)
279
0
{
280
0
  int a = fz_read_byte(ctx, stm);
281
0
  int b = fz_read_byte(ctx, stm);
282
0
  if (a == EOF || b == EOF)
283
0
    fz_throw(ctx, FZ_ERROR_FORMAT, "premature end of file in int16");
284
0
  return ((uint16_t)a<<8) | ((uint16_t)b);
285
0
}
286
287
uint32_t fz_read_uint24(fz_context *ctx, fz_stream *stm)
288
0
{
289
0
  int a = fz_read_byte(ctx, stm);
290
0
  int b = fz_read_byte(ctx, stm);
291
0
  int c = fz_read_byte(ctx, stm);
292
0
  if (a == EOF || b == EOF || c == EOF)
293
0
    fz_throw(ctx, FZ_ERROR_FORMAT, "premature end of file in int24");
294
0
  return ((uint32_t)a<<16) | ((uint32_t)b<<8) | ((uint32_t)c);
295
0
}
296
297
uint32_t fz_read_uint32(fz_context *ctx, fz_stream *stm)
298
0
{
299
0
  int a = fz_read_byte(ctx, stm);
300
0
  int b = fz_read_byte(ctx, stm);
301
0
  int c = fz_read_byte(ctx, stm);
302
0
  int d = fz_read_byte(ctx, stm);
303
0
  if (a == EOF || b == EOF || c == EOF || d == EOF)
304
0
    fz_throw(ctx, FZ_ERROR_FORMAT, "premature end of file in int32");
305
0
  return ((uint32_t)a<<24) | ((uint32_t)b<<16) | ((uint32_t)c<<8) | ((uint32_t)d);
306
0
}
307
308
uint64_t fz_read_uint64(fz_context *ctx, fz_stream *stm)
309
0
{
310
0
  int a = fz_read_byte(ctx, stm);
311
0
  int b = fz_read_byte(ctx, stm);
312
0
  int c = fz_read_byte(ctx, stm);
313
0
  int d = fz_read_byte(ctx, stm);
314
0
  int e = fz_read_byte(ctx, stm);
315
0
  int f = fz_read_byte(ctx, stm);
316
0
  int g = fz_read_byte(ctx, stm);
317
0
  int h = fz_read_byte(ctx, stm);
318
0
  if (a == EOF || b == EOF || c == EOF || d == EOF || e == EOF || f == EOF || g == EOF || h == EOF)
319
0
    fz_throw(ctx, FZ_ERROR_FORMAT, "premature end of file in int64");
320
0
  return ((uint64_t)a<<56) | ((uint64_t)b<<48) | ((uint64_t)c<<40) | ((uint64_t)d<<32)
321
0
    | ((uint64_t)e<<24) | ((uint64_t)f<<16) | ((uint64_t)g<<8) | ((uint64_t)h);
322
0
}
323
324
uint16_t fz_read_uint16_le(fz_context *ctx, fz_stream *stm)
325
0
{
326
0
  int a = fz_read_byte(ctx, stm);
327
0
  int b = fz_read_byte(ctx, stm);
328
0
  if (a == EOF || b == EOF)
329
0
    fz_throw(ctx, FZ_ERROR_FORMAT, "premature end of file in int16");
330
0
  return ((uint16_t)a) | ((uint16_t)b<<8);
331
0
}
332
333
uint32_t fz_read_uint24_le(fz_context *ctx, fz_stream *stm)
334
0
{
335
0
  int a = fz_read_byte(ctx, stm);
336
0
  int b = fz_read_byte(ctx, stm);
337
0
  int c = fz_read_byte(ctx, stm);
338
0
  if (a == EOF || b == EOF || c == EOF)
339
0
    fz_throw(ctx, FZ_ERROR_FORMAT, "premature end of file in int24");
340
0
  return ((uint32_t)a) | ((uint32_t)b<<8) | ((uint32_t)c<<16);
341
0
}
342
343
uint32_t fz_read_uint32_le(fz_context *ctx, fz_stream *stm)
344
0
{
345
0
  int a = fz_read_byte(ctx, stm);
346
0
  int b = fz_read_byte(ctx, stm);
347
0
  int c = fz_read_byte(ctx, stm);
348
0
  int d = fz_read_byte(ctx, stm);
349
0
  if (a == EOF || b == EOF || c == EOF || d == EOF)
350
0
    fz_throw(ctx, FZ_ERROR_FORMAT, "premature end of file in int32");
351
0
  return ((uint32_t)a) | ((uint32_t)b<<8) | ((uint32_t)c<<16) | ((uint32_t)d<<24);
352
0
}
353
354
uint64_t fz_read_uint64_le(fz_context *ctx, fz_stream *stm)
355
0
{
356
0
  int a = fz_read_byte(ctx, stm);
357
0
  int b = fz_read_byte(ctx, stm);
358
0
  int c = fz_read_byte(ctx, stm);
359
0
  int d = fz_read_byte(ctx, stm);
360
0
  int e = fz_read_byte(ctx, stm);
361
0
  int f = fz_read_byte(ctx, stm);
362
0
  int g = fz_read_byte(ctx, stm);
363
0
  int h = fz_read_byte(ctx, stm);
364
0
  if (a == EOF || b == EOF || c == EOF || d == EOF || e == EOF || f == EOF || g == EOF || h == EOF)
365
0
    fz_throw(ctx, FZ_ERROR_FORMAT, "premature end of file in int64");
366
0
  return ((uint64_t)a) | ((uint64_t)b<<8) | ((uint64_t)c<<16) | ((uint64_t)d<<24)
367
0
    | ((uint64_t)e<<32) | ((uint64_t)f<<40) | ((uint64_t)g<<48) | ((uint64_t)h<<56);
368
0
}
369
370
0
int16_t fz_read_int16(fz_context *ctx, fz_stream *stm) { return (int16_t)fz_read_uint16(ctx, stm); }
371
0
int32_t fz_read_int32(fz_context *ctx, fz_stream *stm) { return (int32_t)fz_read_uint32(ctx, stm); }
372
0
int64_t fz_read_int64(fz_context *ctx, fz_stream *stm) { return (int64_t)fz_read_uint64(ctx, stm); }
373
374
0
int16_t fz_read_int16_le(fz_context *ctx, fz_stream *stm) { return (int16_t)fz_read_uint16_le(ctx, stm); }
375
0
int32_t fz_read_int32_le(fz_context *ctx, fz_stream *stm) { return (int32_t)fz_read_uint32_le(ctx, stm); }
376
0
int64_t fz_read_int64_le(fz_context *ctx, fz_stream *stm) { return (int64_t)fz_read_uint64_le(ctx, stm); }
377
378
float
379
fz_read_float_le(fz_context *ctx, fz_stream *stm)
380
0
{
381
0
  uint32_t u;
382
0
  float x;
383
0
  u = fz_read_uint32_le(ctx, stm);
384
0
  memcpy(&x, &u, sizeof x);
385
0
  return x;
386
0
}
387
388
float
389
fz_read_float(fz_context *ctx, fz_stream *stm)
390
0
{
391
0
  uint32_t u;
392
0
  float x;
393
0
  u = fz_read_uint32(ctx, stm);
394
0
  memcpy(&x, &u, sizeof x);
395
0
  return x;
396
0
}
397
398
void fz_read_string(fz_context *ctx, fz_stream *stm, char *buffer, int len)
399
0
{
400
0
  int c;
401
0
  do
402
0
  {
403
0
    if (len <= 0)
404
0
      fz_throw(ctx, FZ_ERROR_FORMAT, "Buffer overrun reading null terminated string");
405
406
0
    c = fz_read_byte(ctx, stm);
407
0
    if (c == EOF)
408
0
      fz_throw(ctx, FZ_ERROR_FORMAT, "EOF reading null terminated string");
409
0
    *buffer++ = c;
410
0
    len--;
411
0
  }
412
0
  while (c != 0);
413
0
}
414
415
int fz_read_rune(fz_context *ctx, fz_stream *in)
416
0
{
417
0
  uint8_t d, e, f;
418
0
  int x;
419
0
  int c = fz_read_byte(ctx, in);
420
0
  if (c == EOF)
421
0
    return EOF;
422
423
0
  if ((c & 0xF8) == 0xF0)
424
0
  {
425
0
    x = fz_read_byte(ctx, in);
426
0
    if (x == EOF)
427
0
      return 0xFFFD;
428
0
    d = (uint8_t)x;
429
0
    c = (c & 7)<<18;
430
0
    if ((d & 0xC0) == 0x80)
431
0
    {
432
0
      x = fz_read_byte(ctx, in);
433
0
      if (x == EOF)
434
0
        return 0xFFFD;
435
0
      e = (uint8_t)x;
436
0
      c += (d & 0x3f)<<12;
437
0
      if ((e & 0xC0) == 0x80)
438
0
      {
439
0
        x = fz_read_byte(ctx, in);
440
0
        if (x == EOF)
441
0
          return 0xFFFD;
442
0
        f = (uint8_t)x;
443
0
        c += (e & 0x3f)<<6;
444
0
        if ((f & 0xC0) == 0x80)
445
0
        {
446
0
          c += f & 0x3f;
447
0
        }
448
0
        else
449
0
          goto bad_byte;
450
0
      }
451
0
      else
452
0
        goto bad_byte;
453
0
    }
454
0
    else
455
0
      goto bad_byte;
456
0
  }
457
0
  else if ((c & 0xF0) == 0xE0)
458
0
  {
459
0
    x = fz_read_byte(ctx, in);
460
0
    if (x == EOF)
461
0
      return 0xFFFD;
462
0
    d = (uint8_t)x;
463
0
    c = (c & 15)<<12;
464
0
    if ((d & 0xC0) == 0x80)
465
0
    {
466
0
      x = fz_read_byte(ctx, in);
467
0
      if (x == EOF)
468
0
        return 0xFFFD;
469
0
      e = (uint8_t)x;
470
0
      c += (d & 0x3f)<<6;
471
0
      if ((e & 0xC0) == 0x80)
472
0
      {
473
0
        c += e & 0x3f;
474
0
      }
475
0
      else
476
0
        goto bad_byte;
477
0
    }
478
0
    else
479
0
      goto bad_byte;
480
0
  }
481
0
  else if ((c & 0xE0) == 0xC0)
482
0
  {
483
0
    x = fz_read_byte(ctx, in);
484
0
    if (x == EOF)
485
0
      return 0xFFFD;
486
0
    d = (uint8_t)x;
487
0
    c = (c & 31)<<6;
488
0
    if ((d & 0xC0) == 0x80)
489
0
    {
490
0
      c += d & 0x3f;
491
0
    }
492
0
    else
493
0
      fz_unread_byte(ctx, in);
494
0
  }
495
0
  else if ((c & 0xc0) == 0x80)
496
0
  {
497
0
bad_byte:
498
0
    fz_unread_byte(ctx, in);
499
0
    return 0xFFFD;
500
0
  }
501
502
0
  return c;
503
504
0
}
505
506
int fz_read_utf16_le(fz_context *ctx, fz_stream *stm)
507
0
{
508
0
  int c = fz_read_byte(ctx, stm);
509
0
  int d, e;
510
511
0
  if (c == EOF)
512
0
    return EOF;
513
514
0
  d = fz_read_byte(ctx, stm);
515
0
  if (d == EOF)
516
0
    return c; /* Might be wrong, but the best we can do. */
517
518
0
  c |= d<<8;
519
520
  /* If it's not a surrogate, we're done. */
521
0
  if (c < 0xd800 || c >= 0xe000)
522
0
    return c;
523
524
  /* It *ought* to be a leading (high) surrogate. If it's not,
525
   * then we're in trouble. */
526
0
  if (c >= 0xdc00)
527
0
    return 0x10000 + c - 0xdc00; /* Imagine the high surrogate was 0. */
528
529
  /* Our stream abstraction only enables us to peek 1 byte ahead, and we'd need
530
   * 2 to tell if it was a low surrogate. Just assume it is. */
531
0
  d = fz_read_byte(ctx, stm);
532
0
  if (d == EOF)
533
0
  {
534
    /* Failure! Imagine the trailing surrogate was 0. */
535
0
    return 0x10000 + ((c - 0xd800)<<10);
536
0
  }
537
0
  e = fz_read_byte(ctx, stm);
538
0
  if (e == EOF)
539
0
  {
540
0
    e = 0xDC; /* Fudge a low surrogate */
541
0
  }
542
543
0
  d |= e<<8;
544
545
0
  if (d < 0xdc00 || d >= 0xe000)
546
0
  {
547
    /* Bad encoding! This is nasty, because we've eaten 2 bytes from the
548
     * stream which ideally we would not have. Serves you right for
549
     * having a broken stream. */
550
0
    return 0x10000 + ((c - 0xd800)<<10); /* Imagine the high surrogate was 0. */
551
0
  }
552
553
0
  c -= 0xd800;
554
0
  d -= 0xdc00;
555
556
0
  return 0x10000 + (c<<10) + d;
557
0
}
558
559
int fz_read_utf16_be(fz_context *ctx, fz_stream *stm)
560
0
{
561
0
  int c = fz_read_byte(ctx, stm);
562
0
  int d, e;
563
564
0
  if (c == EOF)
565
0
    return EOF;
566
567
0
  d = fz_read_byte(ctx, stm);
568
0
  if (d == EOF)
569
0
    return c; /* Might be wrong, but the best we can do. */
570
571
0
  c = (c<<8) | d;
572
573
  /* If it's not a surrogate, we're done. */
574
0
  if (c < 0xd800 || c >= 0xe000)
575
0
    return c;
576
577
  /* It *ought* to be a leading (high) surrogate. If it's not,
578
   * then we're in trouble. */
579
0
  if (c >= 0xdc00)
580
0
    return 0x10000 + c - 0xdc00; /* Imagine the high surrogate was 0. */
581
582
  /* Our stream abstraction only enables us to peek 1 byte ahead, and we'd need
583
   * 2 to tell if it was a low surrogate. Just assume it is. */
584
0
  d = fz_read_byte(ctx, stm);
585
0
  if (d == EOF)
586
0
  {
587
    /* Failure! Imagine the trailing surrogate was 0. */
588
0
    return 0x10000 + ((c - 0xd800)<<10);
589
0
  }
590
591
  /* The next byte ought to be the start of a trailing (low) surrogate. */
592
0
  if (d < 0xdc || d >= 0xe0)
593
0
  {
594
    /* It wasn't. Put the byte back. */
595
0
    fz_unread_byte(ctx, stm);
596
0
    d = 0xdc00; /* Pretend it was a 0 surrogate. */
597
0
  }
598
0
  else
599
0
  {
600
0
    e = fz_read_byte(ctx, stm);
601
0
    if (e == EOF)
602
0
    {
603
0
      e = 0;
604
0
    }
605
0
    d = (d<<8) | e;
606
0
  }
607
608
0
  c -= 0xd800;
609
0
  d -= 0xdc00;
610
611
0
  return 0x10000 + (c<<10) + d;
612
0
}