LibTIFF 4.7.2
TIFF library public API and implementation reference
Loading...
Searching...
No Matches
tif_stream.cxx
Go to the documentation of this file.
1/*
2 * Copyright (c) 1988-1996 Sam Leffler
3 * Copyright (c) 1991-1996 Silicon Graphics, Inc.
4 *
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that (i) the above copyright notices and this permission notice appear in
8 * all copies of the software and related documentation, and (ii) the names of
9 * Sam Leffler and Silicon Graphics may not be used in any advertising or
10 * publicity relating to the software without the specific, prior written
11 * permission of Sam Leffler and Silicon Graphics.
12 *
13 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
14 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
15 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
16 *
17 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
18 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
19 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
20 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
21 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
22 * OF THIS SOFTWARE.
23 */
24
25/*
26 * TIFF Library UNIX-specific Routines.
27 */
35#include "tiffiop.h"
36#include <iostream>
37
38using namespace std;
39
40/*
41 ISO C++ uses a 'std::streamsize' type to define counts. This makes
42 it similar to, (but perhaps not the same as) size_t.
43
44 The std::ios::pos_type is used to represent stream positions as used
45 by tellg(), tellp(), seekg(), and seekp(). This makes it similar to
46 (but perhaps not the same as) 'off_t'. The std::ios::streampos type
47 is used for character streams, but is documented to not be an
48 integral type anymore, so it should *not* be assigned to an integral
49 type.
50
51 The std::ios::off_type is used to specify relative offsets needed by
52 the variants of seekg() and seekp() which accept a relative offset
53 argument.
54
55 Useful prototype knowledge:
56
57 Obtain read position
58 ios::pos_type basic_istream::tellg()
59
60 Set read position
61 basic_istream& basic_istream::seekg(ios::pos_type)
62 basic_istream& basic_istream::seekg(ios::off_type, ios_base::seekdir)
63
64 Read data
65 basic_istream& istream::read(char *str, streamsize count)
66
67 Number of characters read in last unformatted read
68 streamsize istream::gcount();
69
70 Obtain write position
71 ios::pos_type basic_ostream::tellp()
72
73 Set write position
74 basic_ostream& basic_ostream::seekp(ios::pos_type)
75 basic_ostream& basic_ostream::seekp(ios::off_type, ios_base::seekdir)
76
77 Write data
78 basic_ostream& ostream::write(const char *str, streamsize count)
79*/
80
81struct tiffis_data;
82struct tiffos_data;
83
84extern "C"
85{
86
99 static tmsize_t _tiffosReadProc(thandle_t fd, void *buf, tmsize_t size);
116 static tmsize_t _tiffisReadProc(thandle_t fd, void *buf, tmsize_t size);
133 static tmsize_t _tiffosWriteProc(thandle_t fd, void *buf, tmsize_t size);
146 static tmsize_t _tiffisWriteProc(thandle_t fd, void *buf, tmsize_t size);
168 static uint64_t _tiffosSeekProc(thandle_t fd, uint64_t off, int whence);
187 static uint64_t _tiffisSeekProc(thandle_t fd, uint64_t off, int whence);
204 static uint64_t _tiffosSizeProc(thandle_t fd);
221 static uint64_t _tiffisSizeProc(thandle_t fd);
233 static int _tiffosCloseProc(thandle_t fd);
245 static int _tiffisCloseProc(thandle_t fd);
258 static int _tiffDummyMapProcCxx(thandle_t fd, void **base, toff_t *size);
270 static void _tiffDummyUnmapProcCxx(thandle_t fd, void *base, toff_t size);
290 static TIFF *_tiffStreamOpen(const char *name, const char *mode, void *fd);
291
301 {
302 istream
304 ios::pos_type start_pos;
306 };
307
317 {
318 ostream
320 ios::pos_type start_pos;
322 };
323
324 static tmsize_t _tiffosReadProc(thandle_t, void *, tmsize_t) { return 0; }
325
326 static tmsize_t _tiffisReadProc(thandle_t fd, void *buf, tmsize_t size)
327 {
328 tiffis_data *data = reinterpret_cast<tiffis_data *>(fd);
329
330 // Verify that type does not overflow.
331 streamsize request_size = size;
332 if (static_cast<tmsize_t>(request_size) != size)
333 return static_cast<tmsize_t>(-1);
334
335 data->stream->read((char *)buf, request_size);
336
337 return static_cast<tmsize_t>(data->stream->gcount());
338 }
339
340 static tmsize_t _tiffosWriteProc(thandle_t fd, void *buf, tmsize_t size)
341 {
342 tiffos_data *data = reinterpret_cast<tiffos_data *>(fd);
343 ostream *os = data->stream;
344 ios::pos_type pos = os->tellp();
345
346 // Verify that type does not overflow.
347 streamsize request_size = size;
348 if (static_cast<tmsize_t>(request_size) != size)
349 return static_cast<tmsize_t>(-1);
350
351 os->write(reinterpret_cast<const char *>(buf), request_size);
352
353 return static_cast<tmsize_t>(os->tellp() - pos);
354 }
355
356 static tmsize_t _tiffisWriteProc(thandle_t, void *, tmsize_t) { return 0; }
357
358 static uint64_t _tiffosSeekProc(thandle_t fd, uint64_t off, int whence)
359 {
360 tiffos_data *data = reinterpret_cast<tiffos_data *>(fd);
361 ostream *os = data->stream;
362
363 // if the stream has already failed, don't do anything
364 if (os->fail())
365 return static_cast<uint64_t>(-1);
366
367 switch (whence)
368 {
369 case SEEK_SET:
370 {
371 // Compute 64-bit offset
372 uint64_t new_offset =
373 static_cast<uint64_t>(data->start_pos) + off;
374
375 // Verify that value does not overflow
376 ios::off_type offset = static_cast<ios::off_type>(new_offset);
377 if (static_cast<uint64_t>(offset) != new_offset)
378 return static_cast<uint64_t>(-1);
379
380 os->seekp(offset, ios::beg);
381 break;
382 }
383 case SEEK_CUR:
384 {
385 // Verify that value does not overflow
386 ios::off_type offset = static_cast<ios::off_type>(off);
387 if (static_cast<uint64_t>(offset) != off)
388 return static_cast<uint64_t>(-1);
389
390 os->seekp(offset, ios::cur);
391 break;
392 }
393 case SEEK_END:
394 {
395 // Verify that value does not overflow
396 ios::off_type offset = static_cast<ios::off_type>(off);
397 if (static_cast<uint64_t>(offset) != off)
398 return static_cast<uint64_t>(-1);
399
400 os->seekp(offset, ios::end);
401 break;
402 }
403 }
404
405 // Attempt to workaround problems with seeking past the end of the
406 // stream. ofstream doesn't have a problem with this but
407 // ostrstream/ostringstream does. In that situation, add intermediate
408 // '\0' characters.
409 if (os->fail())
410 {
411 ios::iostate old_state;
412 ios::pos_type origin;
413
414 old_state = os->rdstate();
415 // reset the fail bit or else tellp() won't work below
416 os->clear(os->rdstate() & ~ios::failbit);
417 switch (whence)
418 {
419 case SEEK_SET:
420 default:
421 origin = data->start_pos;
422 break;
423 case SEEK_CUR:
424 origin = os->tellp();
425 break;
426 case SEEK_END:
427 os->seekp(0, ios::end);
428 origin = os->tellp();
429 break;
430 }
431 // restore original stream state
432 os->clear(old_state);
433
434 // only do something if desired seek position is valid
435 if ((static_cast<uint64_t>(origin) + off) >
436 static_cast<uint64_t>(data->start_pos))
437 {
438 uint64_t num_fill;
439
440 // clear the fail bit
441 os->clear(os->rdstate() & ~ios::failbit);
442
443 // extend the stream to the expected size
444 os->seekp(0, ios::end);
445 num_fill = (static_cast<uint64_t>(origin)) + off - os->tellp();
446 for (uint64_t i = 0; i < num_fill; i++)
447 os->put('\0');
448
449 // retry the seek
450 os->seekp(static_cast<ios::off_type>(
451 static_cast<uint64_t>(origin) + off),
452 ios::beg);
453 }
454 }
455
456 return static_cast<uint64_t>(os->tellp());
457 }
458
459 static uint64_t _tiffisSeekProc(thandle_t fd, uint64_t off, int whence)
460 {
461 tiffis_data *data = reinterpret_cast<tiffis_data *>(fd);
462
463 if (data->start_pos == ios::pos_type(-1))
464 return static_cast<uint64_t>(-1);
465
466 switch (whence)
467 {
468 case SEEK_SET:
469 {
470 // Compute 64-bit offset
471 uint64_t new_offset =
472 static_cast<uint64_t>(data->start_pos) + off;
473
474 // Verify that value does not overflow
475 ios::off_type offset = static_cast<ios::off_type>(new_offset);
476 if (static_cast<uint64_t>(offset) != new_offset)
477 return static_cast<uint64_t>(-1);
478
479 data->stream->seekg(offset, ios::beg);
480 break;
481 }
482 case SEEK_CUR:
483 {
484 // Verify that value does not overflow
485 ios::off_type offset = static_cast<ios::off_type>(off);
486 if (static_cast<uint64_t>(offset) != off)
487 return static_cast<uint64_t>(-1);
488
489 data->stream->seekg(offset, ios::cur);
490 break;
491 }
492 case SEEK_END:
493 {
494 // Verify that value does not overflow
495 ios::off_type offset = static_cast<ios::off_type>(off);
496 if (static_cast<uint64_t>(offset) != off)
497 return static_cast<uint64_t>(-1);
498
499 data->stream->seekg(offset, ios::end);
500 break;
501 }
502 }
503
504 ios::pos_type pos = data->stream->tellg();
505 if (pos == ios::pos_type(-1))
506 return static_cast<uint64_t>(-1);
507
508 ios::off_type relative_pos = pos - data->start_pos;
509 if (relative_pos < 0)
510 return static_cast<uint64_t>(-1);
511
512 return static_cast<uint64_t>(relative_pos);
513 }
514
515 static uint64_t _tiffosSizeProc(thandle_t fd)
516 {
517 tiffos_data *data = reinterpret_cast<tiffos_data *>(fd);
518 ostream *os = data->stream;
519 ios::pos_type pos = os->tellp();
520 ios::pos_type len;
521
522 // Failed stream positions must not be converted to unsigned sizes.
523 if (pos == ios::pos_type(-1))
524 return 0;
525
526 os->seekp(0, ios::end);
527 if (os->fail())
528 {
529 ios::iostate old_state = os->rdstate();
530 // Temporarily clear failbit to restore the original position.
531 os->clear(os->rdstate() & ~ios::failbit);
532 os->seekp(pos);
533 os->clear(old_state);
534 return 0;
535 }
536 len = os->tellp();
537 if (len == ios::pos_type(-1))
538 {
539 ios::iostate old_state = os->rdstate();
540 // Temporarily clear failbit to restore the original position.
541 os->clear(os->rdstate() & ~ios::failbit);
542 os->seekp(pos);
543 os->clear(old_state);
544 return 0;
545 }
546 os->seekp(pos);
547
548 return static_cast<uint64_t>(len);
549 }
550
551 static uint64_t _tiffisSizeProc(thandle_t fd)
552 {
553 tiffis_data *data = reinterpret_cast<tiffis_data *>(fd);
554 ios::pos_type pos = data->stream->tellg();
555 ios::pos_type len;
556
557 // Failed stream positions must not be converted to unsigned sizes.
558 if (pos == ios::pos_type(-1))
559 return 0;
560
561 data->stream->seekg(0, ios::end);
562 if (data->stream->fail())
563 {
564 ios::iostate old_state = data->stream->rdstate();
565 // Temporarily clear failbit to restore the original position.
566 data->stream->clear(data->stream->rdstate() & ~ios::failbit);
567 data->stream->seekg(pos);
568 data->stream->clear(old_state);
569 return 0;
570 }
571 len = data->stream->tellg();
572 if (len == ios::pos_type(-1))
573 {
574 ios::iostate old_state = data->stream->rdstate();
575 // Temporarily clear failbit to restore the original position.
576 data->stream->clear(data->stream->rdstate() & ~ios::failbit);
577 data->stream->seekg(pos);
578 data->stream->clear(old_state);
579 return 0;
580 }
581 data->stream->seekg(pos);
582
583 return static_cast<uint64_t>(len);
584 }
585
587 {
588 // Our stream was not allocated by us, so it shouldn't be closed by us.
589 delete reinterpret_cast<tiffos_data *>(fd);
590 return 0;
591 }
592
594 {
595 // Our stream was not allocated by us, so it shouldn't be closed by us.
596 delete reinterpret_cast<tiffis_data *>(fd);
597 return 0;
598 }
599
600 static int _tiffDummyMapProcCxx(thandle_t, void **base, toff_t *size)
601 {
602 (void)base;
603 (void)size;
604 return (0);
605 }
606
607 static void _tiffDummyUnmapProcCxx(thandle_t, void *base, toff_t size)
608 {
609 (void)base;
610 (void)size;
611 }
612
613 /*
614 * Open a TIFF file descriptor for read/writing.
615 */
616 static TIFF *_tiffStreamOpen(const char *name, const char *mode, void *fd)
617 {
618 TIFF *tif;
619
620 if (strchr(mode, 'w'))
621 {
622 tiffos_data *data = new tiffos_data;
623 data->stream = reinterpret_cast<ostream *>(fd);
624 data->start_pos = data->stream->tellp();
625
626 // Open for writing.
627 tif = TIFFClientOpen(
628 name, mode, reinterpret_cast<thandle_t>(data), _tiffosReadProc,
631 if (!tif)
632 {
633 delete data;
634 }
635 }
636 else
637 {
638 tiffis_data *data = new tiffis_data;
639 data->stream = reinterpret_cast<istream *>(fd);
640 data->start_pos = data->stream->tellg();
641 // Open for reading.
642 tif = TIFFClientOpen(
643 name, mode, reinterpret_cast<thandle_t>(data), _tiffisReadProc,
646 if (!tif)
647 {
648 delete data;
649 }
650 }
651
652 return (tif);
653 }
654
655} /* extern "C" */
656
657TIFF *TIFFStreamOpen(const char *name, std::ostream *os)
658{
659 // If os is either a ostrstream or ostringstream, and has no data
660 // written to it yet, then tellp() will return -1 which will break us.
661 // We workaround this by writing out a dummy character and
662 // then seek back to the beginning.
663 if (!os->fail() && static_cast<int>(os->tellp()) < 0)
664 {
665 *os << '\0';
666 os->seekp(0);
667 }
668
669 // NB: We don't support mapped files with streams so add 'm'
670 return _tiffStreamOpen(name, "wm", os);
671}
672
673TIFF *TIFFStreamOpen(const char *name, std::istream *is)
674{
675 // NB: We don't support mapped files with streams so add 'm'
676 return _tiffStreamOpen(name, "rm", is);
677}
static TIFF * _tiffStreamOpen(const char *name, const char *mode, void *fd)
Allocate a stream context and attach it through TIFFClientOpen().
static tmsize_t _tiffisWriteProc(thandle_t fd, void *buf, tmsize_t size)
Refuse writes on an input-stream adapter.
static int _tiffDummyMapProcCxx(thandle_t fd, void **base, toff_t *size)
Decline mapping for the C++ stream adapter.
static uint64_t _tiffisSeekProc(thandle_t fd, uint64_t off, int whence)
Seek the borrowed input stream relative to its recorded TIFF origin.
static void _tiffDummyUnmapProcCxx(thandle_t fd, void *base, toff_t size)
Provide a no-op unmapper for the C++ stream adapter.
static int _tiffisCloseProc(thandle_t fd)
Delete a stream adapter without closing the borrowed stream.
static uint64_t _tiffosSeekProc(thandle_t fd, uint64_t off, int whence)
Seek the borrowed output stream, with a zero-fill extension fallback.
static uint64_t _tiffisSizeProc(thandle_t fd)
Query the absolute end position of the borrowed stream.
static tmsize_t _tiffosWriteProc(thandle_t fd, void *buf, tmsize_t size)
Write bytes to the borrowed output stream.
static tmsize_t _tiffisReadProc(thandle_t fd, void *buf, tmsize_t size)
Read bytes from the borrowed input stream.
static tmsize_t _tiffosReadProc(thandle_t fd, void *buf, tmsize_t size)
Refuse reads on an output-stream adapter.
static int _tiffosCloseProc(thandle_t fd)
Delete a stream adapter without closing the borrowed stream.
static uint64_t _tiffosSizeProc(thandle_t fd)
Query the absolute end position of the borrowed stream.
TIFF * TIFFClientOpen(const char *name, const char *mode, thandle_t clientdata, TIFFReadWriteProc readproc, TIFFReadWriteProc writeproc, TIFFSeekProc seekproc, TIFFCloseProc closeproc, TIFFSizeProc sizeproc, TIFFMapFileProc mapproc, TIFFUnmapFileProc unmapproc)
Initialise a TIFF handle using client-supplied I/O callbacks.
Definition tif_open.c:315
TIFF * TIFFStreamOpen(const char *name, std::ostream *os)
Open a TIFF writer over a caller-owned C++ stream.
TIFF_SSIZE_T tmsize_t
Represent a signed memory or I/O size in bytes.
Definition tiffio.h:89
uint64_t toff_t
Represent an unsigned file byte offset independently of off_t.
Definition tiffio.h:94
void * thandle_t
Carry an opaque client I/O context through library callbacks.
Definition tiffio.h:161
Hold private state for one open TIFF library instance.
Definition tiffiop.h:389
Track a borrowed input stream and its initial position.
ios::pos_type start_pos
Initial tell position, possibly the failure value -1.
istream * stream
Borrowed stream that outlives this adapter.
Track a borrowed output stream and its initial position.
ostream * stream
Borrowed stream that outlives this adapter.
ios::pos_type start_pos
Initial tell position, possibly the failure value -1.
Declare private library state, callbacks, and implementation helpers.