forked from michaelrsweet/pdfio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pdfio-common.c
575 lines (454 loc) · 13.7 KB
/
pdfio-common.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
//
// Common support functions for pdfio.
//
// Copyright © 2021-2024 by Michael R Sweet.
//
// Licensed under Apache License v2.0. See the file "LICENSE" for more
// information.
//
#include "pdfio-private.h"
//
// Local functions...
//
static bool fill_buffer(pdfio_file_t *pdf);
static ssize_t read_buffer(pdfio_file_t *pdf, char *buffer, size_t bytes);
static bool write_buffer(pdfio_file_t *pdf, const void *buffer, size_t bytes);
//
// '_pdfioFileConsume()' - Consume bytes from the file.
//
bool // O - `true` on sucess, `false` on EOF
_pdfioFileConsume(pdfio_file_t *pdf, // I - PDF file
size_t bytes) // I - Bytes to consume
{
PDFIO_DEBUG("_pdfioFileConsume(pdf=%p, bytes=%u)\n", pdf, (unsigned)bytes);
if ((size_t)(pdf->bufend - pdf->bufptr) > bytes)
pdf->bufptr += bytes;
else if (_pdfioFileSeek(pdf, (off_t)bytes, SEEK_CUR) < 0)
return (false);
PDFIO_DEBUG("_pdfioFileConsume: pos=%ld\n", (long)(pdf->bufpos + pdf->bufptr - pdf->buffer));
return (true);
}
//
// '_pdfioFileDefaultError()' - Default error callback.
//
// The default error callback writes the error message to stderr and returns
// `false` to halt.
//
bool // O - `false` to stop
_pdfioFileDefaultError(
pdfio_file_t *pdf, // I - PDF file
const char *message, // I - Error message
void *data) // I - Callback data (unused)
{
(void)data;
fprintf(stderr, "%s: %s\n", pdf->filename, message);
return (false);
}
//
// '_pdfioFileError()' - Display an error message.
//
bool // O - `true` to continue, `false` to stop
_pdfioFileError(pdfio_file_t *pdf, // I - PDF file
const char *format, // I - `printf`-style format string
...) // I - Additional arguments as needed
{
char buffer[8192]; // Message buffer
va_list ap; // Argument pointer
va_start(ap, format);
vsnprintf(buffer, sizeof(buffer), format, ap);
va_end(ap);
PDFIO_DEBUG("_pdfioFileError: %s\n", buffer);
return ((pdf->error_cb)(pdf, buffer, pdf->error_data));
}
//
// '_pdfioFileFlush()' - Flush any pending write data.
//
bool // O - `true` on success, `false` on failure
_pdfioFileFlush(pdfio_file_t *pdf) // I - PDF file
{
PDFIO_DEBUG("_pdfioFileFlush(pdf=%p)\n", pdf);
if (pdf->bufptr > pdf->buffer)
{
if (!write_buffer(pdf, pdf->buffer, (size_t)(pdf->bufptr - pdf->buffer)))
return (false);
pdf->bufpos += pdf->bufptr - pdf->buffer;
}
pdf->bufptr = pdf->buffer;
return (true);
}
//
// '_pdfioFileGetChar()' - Get a character from a PDF file.
//
int // O - Character or `-1` on EOF
_pdfioFileGetChar(pdfio_file_t *pdf) // I - PDF file
{
// If there is a character ready in the buffer, return it now...
if (pdf->bufptr < pdf->bufend)
return (*(pdf->bufptr ++));
// Otherwise try to fill the read buffer...
if (!fill_buffer(pdf))
return (-1);
// Then return the next character in the buffer...
return (*(pdf->bufptr ++));
}
//
// '_pdfioFileGets()' - Read a line from a PDF file.
//
bool // O - `true` on success, `false` on error
_pdfioFileGets(pdfio_file_t *pdf, // I - PDF file
char *buffer, // I - Line buffer
size_t bufsize) // I - Size of line buffer
{
bool eol = false; // End of line?
char *bufptr = buffer, // Pointer into buffer
*bufend = buffer + bufsize - 1; // Pointer to end of buffer
PDFIO_DEBUG("_pdfioFileGets(pdf=%p, buffer=%p, bufsize=%lu) bufpos=%ld, buffer=%p, bufptr=%p, bufend=%p, offset=%lu\n", pdf, buffer, (unsigned long)bufsize, (long)pdf->bufpos, pdf->buffer, pdf->bufptr, pdf->bufend, (unsigned long)(pdf->bufpos + (pdf->bufptr - pdf->buffer)));
while (!eol)
{
// If there are characters ready in the buffer, use them...
while (!eol && pdf->bufptr < pdf->bufend && bufptr < bufend)
{
char ch = *(pdf->bufptr++); // Next character in buffer
if (ch == '\n' || ch == '\r')
{
// CR, LF, or CR + LF end a line...
eol = true;
if (ch == '\r')
{
// Check for a LF after CR
if (pdf->bufptr >= pdf->bufend)
{
if (!fill_buffer(pdf))
break;
}
if (pdf->bufptr < pdf->bufend && *(pdf->bufptr) == '\n')
pdf->bufptr ++;
}
}
else
*bufptr++ = ch;
}
// Fill the read buffer as needed...
if (!eol)
{
if (!fill_buffer(pdf))
break;
}
}
*bufptr = '\0';
PDFIO_DEBUG("_pdfioFileGets: Returning %s, '%s'\n", eol ? "true" : "false", buffer);
return (eol);
}
//
// '_pdfioFilePeek()' - Peek at upcoming data in a PDF file.
//
ssize_t // O - Number of bytes returned
_pdfioFilePeek(pdfio_file_t *pdf, // I - PDF file
void *buffer, // I - Buffer
size_t bytes) // I - Size of bufffer
{
ssize_t total; // Total bytes available
// See how much data is buffered up...
if (pdf->bufptr >= pdf->bufend)
{
// Fill the buffer...
if (!fill_buffer(pdf))
return (-1);
}
if ((total = pdf->bufend - pdf->bufptr) < (ssize_t)bytes && total < (ssize_t)(sizeof(pdf->buffer) / 2))
{
// Yes, try reading more...
ssize_t rbytes; // Bytes read
PDFIO_DEBUG("_pdfioFilePeek: Sliding buffer, total=%ld\n", (long)total);
memmove(pdf->buffer, pdf->bufptr, total);
pdf->bufpos += pdf->bufptr - pdf->buffer;
pdf->bufptr = pdf->buffer;
pdf->bufend = pdf->buffer + total;
// Read until we have bytes or a non-recoverable error...
while ((rbytes = read(pdf->fd, pdf->bufend, sizeof(pdf->buffer) - (size_t)total)) < 0)
{
if (errno != EINTR && errno != EAGAIN)
break;
}
if (rbytes > 0)
{
// Expand the buffer...
pdf->bufend += rbytes;
total += rbytes;
}
}
// Copy anything we have to the buffer...
if (total > (ssize_t)bytes)
total = (ssize_t)bytes;
if (total > 0)
memcpy(buffer, pdf->bufptr, total);
return (total);
}
//
// '_pdfioFilePrintf()' - Write a formatted string to a PDF file.
//
bool // O - `true` on success, `false` on failure
_pdfioFilePrintf(pdfio_file_t *pdf, // I - PDF file
const char *format, // I - `printf`-style format string
...) // I - Additional arguments as needed
{
char buffer[8102]; // String buffer
va_list ap; // Argument list
// Format the string...
va_start(ap, format);
_pdfio_vsnprintf(pdf, buffer, sizeof(buffer), format, ap);
va_end(ap);
// Write it...
return (_pdfioFileWrite(pdf, buffer, strlen(buffer)));
}
//
// '_pdfioFilePuts()' - Write a literal string to a PDF file.
//
bool // O - `true` on success, `false` on failure
_pdfioFilePuts(pdfio_file_t *pdf, // I - PDF file
const char *s) // I - Literal string
{
// Write it...
return (_pdfioFileWrite(pdf, s, strlen(s)));
}
//
// '_pdfioFileRead()' - Read from a PDF file.
//
ssize_t // O - Number of bytes read or `-1` on error
_pdfioFileRead(pdfio_file_t *pdf, // I - PDF file
void *buffer, // I - Read buffer
size_t bytes) // I - Number of bytes to read
{
char *bufptr = (char *)buffer;
// Pointer into buffer
ssize_t total, // Total bytes read
rbytes; // Bytes read this time
// Loop until we have read all of the requested bytes or hit an error...
for (total = 0; bytes > 0; total += rbytes, bytes -= (size_t)rbytes, bufptr += rbytes)
{
// First read from the file buffer...
if ((rbytes = pdf->bufend - pdf->bufptr) > 0)
{
if ((size_t)rbytes > bytes)
rbytes = (ssize_t)bytes;
memcpy(bufptr, pdf->bufptr, rbytes);
pdf->bufptr += rbytes;
continue;
}
// Nothing buffered...
if (bytes > 1024)
{
// Advance current position in file as needed...
if (pdf->bufend)
{
pdf->bufpos += pdf->bufend - pdf->buffer;
pdf->bufptr = pdf->bufend = NULL;
}
// Read directly from the file...
if ((rbytes = read_buffer(pdf, bufptr, bytes)) > 0)
{
pdf->bufpos += rbytes;
continue;
}
else if (rbytes < 0 && (errno == EINTR || errno == EAGAIN))
{
rbytes = 0;
continue;
}
else
break;
}
else
{
// Fill buffer and try again...
if (!fill_buffer(pdf))
break;
}
}
return (total);
}
//
// '_pdfioFileSeek()' - Seek within a PDF file.
//
off_t // O - New offset from beginning of file or `-1` on error
_pdfioFileSeek(pdfio_file_t *pdf, // I - PDF file
off_t offset, // I - Offset
int whence) // I - Offset base
{
PDFIO_DEBUG("_pdfioFileSeek(pdf=%p, offset=%ld, whence=%d) pdf->bufpos=%lu\n", pdf, (long)offset, whence, (unsigned long)(pdf ? pdf->bufpos : 0));
// Adjust offset for relative seeks...
if (whence == SEEK_CUR)
{
offset += pdf->bufpos + (pdf->bufptr - pdf->buffer);
whence = SEEK_SET;
}
if (pdf->mode == _PDFIO_MODE_READ)
{
// Reading, see if we already have the data we need...
if (whence != SEEK_END && offset >= pdf->bufpos && offset < (pdf->bufpos + pdf->bufend - pdf->buffer))
{
// Yes, seek within existing buffer...
pdf->bufptr = pdf->buffer + (offset - pdf->bufpos);
PDFIO_DEBUG("_pdfioFileSeek: Seek within buffer, bufpos=%ld.\n", (long)pdf->bufpos);
PDFIO_DEBUG("_pdfioFileSeek: buffer=%p, bufptr=%p(<%02X%02X...>), bufend=%p\n", pdf->buffer, pdf->bufptr, pdf->bufptr[0] & 255, pdf->bufptr[1] & 255, pdf->bufend);
return (offset);
}
// No, reset the read buffer
pdf->bufptr = pdf->bufend = NULL;
}
else if (pdf->output_cb)
{
_pdfioFileError(pdf, "Unable to seek within output stream.");
return (-1);
}
else
{
// Writing, make sure we write any buffered data...
if (pdf->bufptr > pdf->buffer)
{
if (!write_buffer(pdf, pdf->buffer, (size_t)(pdf->bufptr - pdf->buffer)))
return (-1);
}
pdf->bufptr = pdf->buffer;
}
// Seek within the file...
if ((offset = lseek(pdf->fd, offset, whence)) < 0)
{
_pdfioFileError(pdf, "Unable to seek within file - %s", strerror(errno));
return (-1);
}
PDFIO_DEBUG("_pdfioFileSeek: Reset bufpos=%ld, offset=%lu.\n", (long)pdf->bufpos, (unsigned long)offset);
PDFIO_DEBUG("_pdfioFileSeek: buffer=%p, bufptr=%p, bufend=%p\n", pdf->buffer, pdf->bufptr, pdf->bufend);
pdf->bufpos = offset;
return (offset);
}
//
// '_pdfioFileTell()' - Return the offset within a PDF file.
//
off_t // O - Offset from beginning of file
_pdfioFileTell(pdfio_file_t *pdf) // I - PDF file
{
if (pdf->bufptr)
return (pdf->bufpos + (pdf->bufptr - pdf->buffer));
else
return (pdf->bufpos);
}
//
// '_pdfioFileWrite()' - Write to a PDF file.
//
bool // O - `true` on success and `false` on error
_pdfioFileWrite(pdfio_file_t *pdf, // I - PDF file
const void *buffer, // I - Write buffer
size_t bytes) // I - Bytes to write
{
// See if the data will fit in the write buffer...
if (bytes > (size_t)(pdf->bufend - pdf->bufptr))
{
// No room, flush any current data...
if (!_pdfioFileFlush(pdf))
return (false);
if (bytes >= sizeof(pdf->buffer))
{
// Write directly...
if (!write_buffer(pdf, buffer, bytes))
return (false);
pdf->bufpos += bytes;
return (true);
}
}
// Copy data to the buffer and return...
memcpy(pdf->bufptr, buffer, bytes);
pdf->bufptr += bytes;
return (true);
}
//
// 'fill_buffer()' - Fill the read buffer in a PDF file.
//
static bool // O - `true` on success, `false` on failure
fill_buffer(pdfio_file_t *pdf) // I - PDF file
{
ssize_t bytes; // Bytes read...
// Advance current position in file as needed...
if (pdf->bufend)
pdf->bufpos += pdf->bufend - pdf->buffer;
// Try reading from the file...
if ((bytes = read_buffer(pdf, pdf->buffer, sizeof(pdf->buffer))) <= 0)
{
// EOF or hard error...
pdf->bufptr = pdf->bufend = NULL;
return (false);
}
else
{
// Successful read...
pdf->bufptr = pdf->buffer;
pdf->bufend = pdf->buffer + bytes;
return (true);
}
}
//
// 'read_buffer()' - Read a buffer from a PDF file.
//
static ssize_t // O - Number of bytes read or -1 on error
read_buffer(pdfio_file_t *pdf, // I - PDF file
char *buffer, // I - Buffer
size_t bytes) // I - Number of bytes to read
{
ssize_t rbytes; // Bytes read...
// Read from the file...
while ((rbytes = read(pdf->fd, buffer, bytes)) < 0)
{
// Stop if we have an error that shouldn't be retried...
if (errno != EINTR && errno != EAGAIN)
break;
}
if (rbytes < 0)
{
// Hard error...
_pdfioFileError(pdf, "Unable to read from file - %s", strerror(errno));
}
return (rbytes);
}
//
// 'write_buffer()' - Write a buffer to a PDF file.
//
static bool // O - `true` on success and `false` on error
write_buffer(pdfio_file_t *pdf, // I - PDF file
const void *buffer, // I - Write buffer
size_t bytes) // I - Bytes to write
{
const char *bufptr = (const char *)buffer;
// Pointer into buffer
ssize_t wbytes; // Bytes written...
if (pdf->output_cb)
{
// Write to a stream...
if ((pdf->output_cb)(pdf->output_ctx, buffer, bytes) < 0)
{
_pdfioFileError(pdf, "Unable to write to output callback.");
return (false);
}
}
else
{
// Write to the file...
while (bytes > 0)
{
while ((wbytes = write(pdf->fd, bufptr, bytes)) < 0)
{
// Stop if we have an error that shouldn't be retried...
if (errno != EINTR && errno != EAGAIN)
break;
}
if (wbytes < 0)
{
// Hard error...
_pdfioFileError(pdf, "Unable to write to file - %s", strerror(errno));
return (false);
}
bufptr += wbytes;
bytes -= (size_t)wbytes;
}
}
return (true);
}