123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808 |
- #include "tiffiop.h"
- #include <limits.h>
- static int _tiffDummyMapProc(thandle_t fd, void **pbase, toff_t *psize)
- {
- (void)fd;
- (void)pbase;
- (void)psize;
- return (0);
- }
- static void _tiffDummyUnmapProc(thandle_t fd, void *base, toff_t size)
- {
- (void)fd;
- (void)base;
- (void)size;
- }
- int _TIFFgetMode(TIFFOpenOptions *opts, thandle_t clientdata, const char *mode,
- const char *module)
- {
- int m = -1;
- switch (mode[0])
- {
- case 'r':
- m = O_RDONLY;
- if (mode[1] == '+')
- m = O_RDWR;
- break;
- case 'w':
- case 'a':
- m = O_RDWR | O_CREAT;
- if (mode[0] == 'w')
- m |= O_TRUNC;
- break;
- default:
- _TIFFErrorEarly(opts, clientdata, module, "\"%s\": Bad mode", mode);
- break;
- }
- return (m);
- }
- TIFFOpenOptions *TIFFOpenOptionsAlloc()
- {
- TIFFOpenOptions *opts =
- (TIFFOpenOptions *)_TIFFcalloc(1, sizeof(TIFFOpenOptions));
- return opts;
- }
- void TIFFOpenOptionsFree(TIFFOpenOptions *opts) { _TIFFfree(opts); }
- void TIFFOpenOptionsSetMaxSingleMemAlloc(TIFFOpenOptions *opts,
- tmsize_t max_single_mem_alloc)
- {
- opts->max_single_mem_alloc = max_single_mem_alloc;
- }
- void TIFFOpenOptionsSetErrorHandlerExtR(TIFFOpenOptions *opts,
- TIFFErrorHandlerExtR handler,
- void *errorhandler_user_data)
- {
- opts->errorhandler = handler;
- opts->errorhandler_user_data = errorhandler_user_data;
- }
- void TIFFOpenOptionsSetWarningHandlerExtR(TIFFOpenOptions *opts,
- TIFFErrorHandlerExtR handler,
- void *warnhandler_user_data)
- {
- opts->warnhandler = handler;
- opts->warnhandler_user_data = warnhandler_user_data;
- }
- static void _TIFFEmitErrorAboveMaxSingleMemAlloc(TIFF *tif,
- const char *pszFunction,
- tmsize_t s)
- {
- TIFFErrorExtR(tif, pszFunction,
- "Memory allocation of %" PRIu64
- " bytes is beyond the %" PRIu64
- " byte limit defined in open options",
- (uint64_t)s, (uint64_t)tif->tif_max_single_mem_alloc);
- }
- void *_TIFFmallocExt(TIFF *tif, tmsize_t s)
- {
- if (tif != NULL && tif->tif_max_single_mem_alloc > 0 &&
- s > tif->tif_max_single_mem_alloc)
- {
- _TIFFEmitErrorAboveMaxSingleMemAlloc(tif, "_TIFFmallocExt", s);
- return NULL;
- }
- return _TIFFmalloc(s);
- }
- void *_TIFFcallocExt(TIFF *tif, tmsize_t nmemb, tmsize_t siz)
- {
- if (tif != NULL && tif->tif_max_single_mem_alloc > 0)
- {
- if (nmemb <= 0 || siz <= 0 || nmemb > TIFF_TMSIZE_T_MAX / siz)
- return NULL;
- if (nmemb * siz > tif->tif_max_single_mem_alloc)
- {
- _TIFFEmitErrorAboveMaxSingleMemAlloc(tif, "_TIFFcallocExt",
- nmemb * siz);
- return NULL;
- }
- }
- return _TIFFcalloc(nmemb, siz);
- }
- void *_TIFFreallocExt(TIFF *tif, void *p, tmsize_t s)
- {
- if (tif != NULL && tif->tif_max_single_mem_alloc > 0 &&
- s > tif->tif_max_single_mem_alloc)
- {
- _TIFFEmitErrorAboveMaxSingleMemAlloc(tif, "_TIFFreallocExt", s);
- return NULL;
- }
- return _TIFFrealloc(p, s);
- }
- void _TIFFfreeExt(TIFF *tif, void *p)
- {
- (void)tif;
- _TIFFfree(p);
- }
- 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)
- {
- return TIFFClientOpenExt(name, mode, clientdata, readproc, writeproc,
- seekproc, closeproc, sizeproc, mapproc, unmapproc,
- NULL);
- }
- TIFF *TIFFClientOpenExt(const char *name, const char *mode,
- thandle_t clientdata, TIFFReadWriteProc readproc,
- TIFFReadWriteProc writeproc, TIFFSeekProc seekproc,
- TIFFCloseProc closeproc, TIFFSizeProc sizeproc,
- TIFFMapFileProc mapproc, TIFFUnmapFileProc unmapproc,
- TIFFOpenOptions *opts)
- {
- static const char module[] = "TIFFClientOpenExt";
- TIFF *tif;
- int m;
- const char *cp;
-
- assert(sizeof(uint8_t) == 1);
- assert(sizeof(int8_t) == 1);
- assert(sizeof(uint16_t) == 2);
- assert(sizeof(int16_t) == 2);
- assert(sizeof(uint32_t) == 4);
- assert(sizeof(int32_t) == 4);
- assert(sizeof(uint64_t) == 8);
- assert(sizeof(int64_t) == 8);
- {
- union
- {
- uint8_t a8[2];
- uint16_t a16;
- } n;
- n.a8[0] = 1;
- n.a8[1] = 0;
- (void)n;
- #ifdef WORDS_BIGENDIAN
- assert(n.a16 == 256);
- #else
- assert(n.a16 == 1);
- #endif
- }
- m = _TIFFgetMode(opts, clientdata, mode, module);
- if (m == -1)
- goto bad2;
- tmsize_t size_to_alloc = (tmsize_t)(sizeof(TIFF) + strlen(name) + 1);
- if (opts && opts->max_single_mem_alloc > 0 &&
- size_to_alloc > opts->max_single_mem_alloc)
- {
- _TIFFErrorEarly(opts, clientdata, module,
- "%s: Memory allocation of %" PRIu64
- " bytes is beyond the %" PRIu64
- " byte limit defined in open options",
- name, (uint64_t)size_to_alloc,
- (uint64_t)opts->max_single_mem_alloc);
- goto bad2;
- }
- tif = (TIFF *)_TIFFmallocExt(NULL, size_to_alloc);
- if (tif == NULL)
- {
- _TIFFErrorEarly(opts, clientdata, module,
- "%s: Out of memory (TIFF structure)", name);
- goto bad2;
- }
- _TIFFmemset(tif, 0, sizeof(*tif));
- tif->tif_name = (char *)tif + sizeof(TIFF);
- strcpy(tif->tif_name, name);
- tif->tif_mode = m & ~(O_CREAT | O_TRUNC);
- tif->tif_curdir = TIFF_NON_EXISTENT_DIR_NUMBER;
- tif->tif_curoff = 0;
- tif->tif_curstrip = (uint32_t)-1;
- tif->tif_row = (uint32_t)-1;
- tif->tif_clientdata = clientdata;
- tif->tif_readproc = readproc;
- tif->tif_writeproc = writeproc;
- tif->tif_seekproc = seekproc;
- tif->tif_closeproc = closeproc;
- tif->tif_sizeproc = sizeproc;
- tif->tif_mapproc = mapproc ? mapproc : _tiffDummyMapProc;
- tif->tif_unmapproc = unmapproc ? unmapproc : _tiffDummyUnmapProc;
- if (opts)
- {
- tif->tif_errorhandler = opts->errorhandler;
- tif->tif_errorhandler_user_data = opts->errorhandler_user_data;
- tif->tif_warnhandler = opts->warnhandler;
- tif->tif_warnhandler_user_data = opts->warnhandler_user_data;
- tif->tif_max_single_mem_alloc = opts->max_single_mem_alloc;
- }
- if (!readproc || !writeproc || !seekproc || !closeproc || !sizeproc)
- {
- TIFFErrorExtR(tif, module,
- "One of the client procedures is NULL pointer.");
- _TIFFfreeExt(NULL, tif);
- goto bad2;
- }
- _TIFFSetDefaultCompressionState(tif);
-
- tif->tif_flags = FILLORDER_MSB2LSB;
- if (m == O_RDONLY)
- tif->tif_flags |= TIFF_MAPPED;
- #ifdef STRIPCHOP_DEFAULT
- if (m == O_RDONLY || m == O_RDWR)
- tif->tif_flags |= STRIPCHOP_DEFAULT;
- #endif
-
- for (cp = mode; *cp; cp++)
- switch (*cp)
- {
- case 'b':
- #ifndef WORDS_BIGENDIAN
- if (m & O_CREAT)
- tif->tif_flags |= TIFF_SWAB;
- #endif
- break;
- case 'l':
- #ifdef WORDS_BIGENDIAN
- if ((m & O_CREAT))
- tif->tif_flags |= TIFF_SWAB;
- #endif
- break;
- case 'B':
- tif->tif_flags =
- (tif->tif_flags & ~TIFF_FILLORDER) | FILLORDER_MSB2LSB;
- break;
- case 'L':
- tif->tif_flags =
- (tif->tif_flags & ~TIFF_FILLORDER) | FILLORDER_LSB2MSB;
- break;
- case 'H':
- TIFFWarningExtR(tif, name,
- "H(ost) mode is deprecated. Since "
- "libtiff 4.5.1, it is an alias of 'B' / "
- "FILLORDER_MSB2LSB.");
- tif->tif_flags =
- (tif->tif_flags & ~TIFF_FILLORDER) | FILLORDER_MSB2LSB;
- break;
- case 'M':
- if (m == O_RDONLY)
- tif->tif_flags |= TIFF_MAPPED;
- break;
- case 'm':
- if (m == O_RDONLY)
- tif->tif_flags &= ~TIFF_MAPPED;
- break;
- case 'C':
- if (m == O_RDONLY)
- tif->tif_flags |= TIFF_STRIPCHOP;
- break;
- case 'c':
- if (m == O_RDONLY)
- tif->tif_flags &= ~TIFF_STRIPCHOP;
- break;
- case 'h':
- tif->tif_flags |= TIFF_HEADERONLY;
- break;
- case '8':
- if (m & O_CREAT)
- tif->tif_flags |= TIFF_BIGTIFF;
- break;
- case 'D':
- tif->tif_flags |= TIFF_DEFERSTRILELOAD;
- break;
- case 'O':
- if (m == O_RDONLY)
- tif->tif_flags |=
- (TIFF_LAZYSTRILELOAD | TIFF_DEFERSTRILELOAD);
- break;
- }
- #ifdef DEFER_STRILE_LOAD
-
-
-
- tif->tif_flags |= TIFF_DEFERSTRILELOAD;
- #endif
-
- if ((m & O_TRUNC) ||
- !ReadOK(tif, &tif->tif_header, sizeof(TIFFHeaderClassic)))
- {
- if (tif->tif_mode == O_RDONLY)
- {
- TIFFErrorExtR(tif, name, "Cannot read TIFF header");
- goto bad;
- }
- #ifdef WORDS_BIGENDIAN
- tif->tif_header.common.tiff_magic =
- (tif->tif_flags & TIFF_SWAB) ? TIFF_LITTLEENDIAN : TIFF_BIGENDIAN;
- #else
- tif->tif_header.common.tiff_magic =
- (tif->tif_flags & TIFF_SWAB) ? TIFF_BIGENDIAN : TIFF_LITTLEENDIAN;
- #endif
- if (!(tif->tif_flags & TIFF_BIGTIFF))
- {
- tif->tif_header.common.tiff_version = TIFF_VERSION_CLASSIC;
- tif->tif_header.classic.tiff_diroff = 0;
- if (tif->tif_flags & TIFF_SWAB)
- TIFFSwabShort(&tif->tif_header.common.tiff_version);
- tif->tif_header_size = sizeof(TIFFHeaderClassic);
- }
- else
- {
- tif->tif_header.common.tiff_version = TIFF_VERSION_BIG;
- tif->tif_header.big.tiff_offsetsize = 8;
- tif->tif_header.big.tiff_unused = 0;
- tif->tif_header.big.tiff_diroff = 0;
- if (tif->tif_flags & TIFF_SWAB)
- {
- TIFFSwabShort(&tif->tif_header.common.tiff_version);
- TIFFSwabShort(&tif->tif_header.big.tiff_offsetsize);
- }
- tif->tif_header_size = sizeof(TIFFHeaderBig);
- }
-
- TIFFSeekFile(tif, 0, SEEK_SET);
- if (!WriteOK(tif, &tif->tif_header, (tmsize_t)(tif->tif_header_size)))
- {
- TIFFErrorExtR(tif, name, "Error writing TIFF header");
- goto bad;
- }
-
- if (tif->tif_header.common.tiff_magic == TIFF_BIGENDIAN)
- {
- #ifndef WORDS_BIGENDIAN
- tif->tif_flags |= TIFF_SWAB;
- #endif
- }
- else
- {
- #ifdef WORDS_BIGENDIAN
- tif->tif_flags |= TIFF_SWAB;
- #endif
- }
-
- if (!TIFFDefaultDirectory(tif))
- goto bad;
- tif->tif_diroff = 0;
- tif->tif_lastdiroff = 0;
- tif->tif_setdirectory_force_absolute = FALSE;
- return (tif);
- }
-
- if (tif->tif_header.common.tiff_magic != TIFF_BIGENDIAN &&
- tif->tif_header.common.tiff_magic != TIFF_LITTLEENDIAN
- #if MDI_SUPPORT
- &&
- #if HOST_BIGENDIAN
- tif->tif_header.common.tiff_magic != MDI_BIGENDIAN
- #else
- tif->tif_header.common.tiff_magic != MDI_LITTLEENDIAN
- #endif
- )
- {
- TIFFErrorExtR(tif, name,
- "Not a TIFF or MDI file, bad magic number %" PRIu16
- " (0x%" PRIx16 ")",
- #else
- )
- {
- TIFFErrorExtR(tif, name,
- "Not a TIFF file, bad magic number %" PRIu16
- " (0x%" PRIx16 ")",
- #endif
- tif->tif_header.common.tiff_magic,
- tif->tif_header.common.tiff_magic);
- goto bad;
- }
- if (tif->tif_header.common.tiff_magic == TIFF_BIGENDIAN)
- {
- #ifndef WORDS_BIGENDIAN
- tif->tif_flags |= TIFF_SWAB;
- #endif
- }
- else
- {
- #ifdef WORDS_BIGENDIAN
- tif->tif_flags |= TIFF_SWAB;
- #endif
- }
- if (tif->tif_flags & TIFF_SWAB)
- TIFFSwabShort(&tif->tif_header.common.tiff_version);
- if ((tif->tif_header.common.tiff_version != TIFF_VERSION_CLASSIC) &&
- (tif->tif_header.common.tiff_version != TIFF_VERSION_BIG))
- {
- TIFFErrorExtR(tif, name,
- "Not a TIFF file, bad version number %" PRIu16
- " (0x%" PRIx16 ")",
- tif->tif_header.common.tiff_version,
- tif->tif_header.common.tiff_version);
- goto bad;
- }
- if (tif->tif_header.common.tiff_version == TIFF_VERSION_CLASSIC)
- {
- if (tif->tif_flags & TIFF_SWAB)
- TIFFSwabLong(&tif->tif_header.classic.tiff_diroff);
- tif->tif_header_size = sizeof(TIFFHeaderClassic);
- }
- else
- {
- if (!ReadOK(tif,
- ((uint8_t *)(&tif->tif_header) + sizeof(TIFFHeaderClassic)),
- (sizeof(TIFFHeaderBig) - sizeof(TIFFHeaderClassic))))
- {
- TIFFErrorExtR(tif, name, "Cannot read TIFF header");
- goto bad;
- }
- if (tif->tif_flags & TIFF_SWAB)
- {
- TIFFSwabShort(&tif->tif_header.big.tiff_offsetsize);
- TIFFSwabLong8(&tif->tif_header.big.tiff_diroff);
- }
- if (tif->tif_header.big.tiff_offsetsize != 8)
- {
- TIFFErrorExtR(tif, name,
- "Not a TIFF file, bad BigTIFF offsetsize %" PRIu16
- " (0x%" PRIx16 ")",
- tif->tif_header.big.tiff_offsetsize,
- tif->tif_header.big.tiff_offsetsize);
- goto bad;
- }
- if (tif->tif_header.big.tiff_unused != 0)
- {
- TIFFErrorExtR(tif, name,
- "Not a TIFF file, bad BigTIFF unused %" PRIu16
- " (0x%" PRIx16 ")",
- tif->tif_header.big.tiff_unused,
- tif->tif_header.big.tiff_unused);
- goto bad;
- }
- tif->tif_header_size = sizeof(TIFFHeaderBig);
- tif->tif_flags |= TIFF_BIGTIFF;
- }
- tif->tif_flags |= TIFF_MYBUFFER;
- tif->tif_rawcp = tif->tif_rawdata = 0;
- tif->tif_rawdatasize = 0;
- tif->tif_rawdataoff = 0;
- tif->tif_rawdataloaded = 0;
- switch (mode[0])
- {
- case 'r':
- if (!(tif->tif_flags & TIFF_BIGTIFF))
- tif->tif_nextdiroff = tif->tif_header.classic.tiff_diroff;
- else
- tif->tif_nextdiroff = tif->tif_header.big.tiff_diroff;
-
- if (tif->tif_flags & TIFF_MAPPED)
- {
- toff_t n;
- if (TIFFMapFileContents(tif, (void **)(&tif->tif_base), &n))
- {
- tif->tif_size = (tmsize_t)n;
- assert((toff_t)tif->tif_size == n);
- }
- else
- tif->tif_flags &= ~TIFF_MAPPED;
- }
-
- if (tif->tif_flags & TIFF_HEADERONLY)
- return (tif);
-
- if (TIFFReadDirectory(tif))
- {
- return (tif);
- }
- break;
- case 'a':
-
- if (!TIFFDefaultDirectory(tif))
- goto bad;
- return (tif);
- }
- bad:
- tif->tif_mode = O_RDONLY;
- TIFFCleanup(tif);
- bad2:
- return ((TIFF *)0);
- }
- const char *TIFFFileName(TIFF *tif) { return (tif->tif_name); }
- const char *TIFFSetFileName(TIFF *tif, const char *name)
- {
- const char *old_name = tif->tif_name;
- tif->tif_name = (char *)name;
- return (old_name);
- }
- int TIFFFileno(TIFF *tif) { return (tif->tif_fd); }
- int TIFFSetFileno(TIFF *tif, int fd)
- {
- int old_fd = tif->tif_fd;
- tif->tif_fd = fd;
- return old_fd;
- }
- thandle_t TIFFClientdata(TIFF *tif) { return (tif->tif_clientdata); }
- thandle_t TIFFSetClientdata(TIFF *tif, thandle_t newvalue)
- {
- thandle_t m = tif->tif_clientdata;
- tif->tif_clientdata = newvalue;
- return m;
- }
- int TIFFGetMode(TIFF *tif) { return (tif->tif_mode); }
- int TIFFSetMode(TIFF *tif, int mode)
- {
- int old_mode = tif->tif_mode;
- tif->tif_mode = mode;
- return (old_mode);
- }
- int TIFFIsTiled(TIFF *tif) { return (isTiled(tif)); }
- uint32_t TIFFCurrentRow(TIFF *tif) { return (tif->tif_row); }
- tdir_t TIFFCurrentDirectory(TIFF *tif) { return (tif->tif_curdir); }
- uint32_t TIFFCurrentStrip(TIFF *tif) { return (tif->tif_curstrip); }
- uint32_t TIFFCurrentTile(TIFF *tif) { return (tif->tif_curtile); }
- int TIFFIsByteSwapped(TIFF *tif) { return ((tif->tif_flags & TIFF_SWAB) != 0); }
- int TIFFIsUpSampled(TIFF *tif) { return (isUpSampled(tif)); }
- int TIFFIsMSB2LSB(TIFF *tif) { return (isFillOrder(tif, FILLORDER_MSB2LSB)); }
- int TIFFIsBigEndian(TIFF *tif)
- {
- return (tif->tif_header.common.tiff_magic == TIFF_BIGENDIAN);
- }
- int TIFFIsBigTIFF(TIFF *tif)
- {
- return (tif->tif_header.common.tiff_version == TIFF_VERSION_BIG);
- }
- TIFFReadWriteProc TIFFGetReadProc(TIFF *tif) { return (tif->tif_readproc); }
- TIFFReadWriteProc TIFFGetWriteProc(TIFF *tif) { return (tif->tif_writeproc); }
- TIFFSeekProc TIFFGetSeekProc(TIFF *tif) { return (tif->tif_seekproc); }
- TIFFCloseProc TIFFGetCloseProc(TIFF *tif) { return (tif->tif_closeproc); }
- TIFFSizeProc TIFFGetSizeProc(TIFF *tif) { return (tif->tif_sizeproc); }
- TIFFMapFileProc TIFFGetMapFileProc(TIFF *tif) { return (tif->tif_mapproc); }
- TIFFUnmapFileProc TIFFGetUnmapFileProc(TIFF *tif)
- {
- return (tif->tif_unmapproc);
- }
|