ReactOS 0.4.16-dev-257-g6aa11ac
tif_dirwrite.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 1988-1997 Sam Leffler
3 * Copyright (c) 1991-1997 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.
27 *
28 * Directory Write Support Routines.
29 */
30#include <precomp.h>
31#include <float.h>
32
33#ifdef HAVE_IEEEFP
34#define TIFFCvtNativeToIEEEFloat(tif, n, fp)
35#define TIFFCvtNativeToIEEEDouble(tif, n, dp)
36#else
37extern void TIFFCvtNativeToIEEEFloat(TIFF* tif, uint32 n, float* fp);
38extern void TIFFCvtNativeToIEEEDouble(TIFF* tif, uint32 n, double* dp);
39#endif
40
41static int TIFFWriteDirectorySec(TIFF* tif, int isimage, int imagedone, uint64* pdiroff);
42
44#if 0
45static int TIFFWriteDirectoryTagSampleformatPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value);
46#endif
47
50#ifdef notdef
51static int TIFFWriteDirectoryTagByte(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint8 value);
52#endif
54#if 0
55static int TIFFWriteDirectoryTagBytePerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint8 value);
56#endif
57#ifdef notdef
58static int TIFFWriteDirectoryTagSbyte(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int8 value);
59#endif
61#if 0
62static int TIFFWriteDirectoryTagSbytePerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int8 value);
63#endif
67#ifdef notdef
68static int TIFFWriteDirectoryTagSshort(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int16 value);
69#endif
71#if 0
72static int TIFFWriteDirectoryTagSshortPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int16 value);
73#endif
76#if 0
77static int TIFFWriteDirectoryTagLongPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 value);
78#endif
79#ifdef notdef
80static int TIFFWriteDirectoryTagSlong(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int32 value);
81#endif
83#if 0
84static int TIFFWriteDirectoryTagSlongPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int32 value);
85#endif
86#ifdef notdef
87static int TIFFWriteDirectoryTagLong8(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint64 value);
88#endif
90#ifdef notdef
91static int TIFFWriteDirectoryTagSlong8(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int64 value);
92#endif
94static int TIFFWriteDirectoryTagRational(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value);
97#ifdef notdef
98static int TIFFWriteDirectoryTagFloat(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, float value);
99#endif
101#if 0
102static int TIFFWriteDirectoryTagFloatPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, float value);
103#endif
104#ifdef notdef
105static int TIFFWriteDirectoryTagDouble(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value);
106#endif
108#if 0
109static int TIFFWriteDirectoryTagDoublePerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value);
110#endif
112#ifdef notdef
113static int TIFFWriteDirectoryTagIfd8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value);
114#endif
118#ifdef notdef
119static int TIFFWriteDirectoryTagShortLongLong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value);
120#endif
123static int TIFFWriteDirectoryTagSubifd(TIFF* tif, uint32* ndir, TIFFDirEntry* dir);
124
127#ifdef notdef
128static int TIFFWriteDirectoryTagCheckedByte(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint8 value);
129#endif
131#ifdef notdef
132static int TIFFWriteDirectoryTagCheckedSbyte(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int8 value);
133#endif
137#ifdef notdef
138static int TIFFWriteDirectoryTagCheckedSshort(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int16 value);
139#endif
143#ifdef notdef
144static int TIFFWriteDirectoryTagCheckedSlong(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int32 value);
145#endif
147#ifdef notdef
148static int TIFFWriteDirectoryTagCheckedLong8(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint64 value);
149#endif
151#ifdef notdef
152static int TIFFWriteDirectoryTagCheckedSlong8(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int64 value);
153#endif
158#ifdef notdef
159static int TIFFWriteDirectoryTagCheckedFloat(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, float value);
160#endif
162#ifdef notdef
163static int TIFFWriteDirectoryTagCheckedDouble(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value);
164#endif
168
169static int TIFFWriteDirectoryTagData(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint16 datatype, uint32 count, uint32 datalength, void* data);
170
171static int TIFFLinkDirectory(TIFF*);
172
173/*
174 * Write the contents of the current directory
175 * to the specified file. This routine doesn't
176 * handle overwriting a directory with auxiliary
177 * storage that's been changed.
178 */
179int
181{
183}
184
185/*
186 * This is an advanced writing function that must be used in a particular
187 * sequence, and generally together with TIFFForceStrileArrayWriting(),
188 * to make its intended effect. Its aim is to modify the location
189 * where the [Strip/Tile][Offsets/ByteCounts] arrays are located in the file.
190 * More precisely, when TIFFWriteCheck() will be called, the tag entries for
191 * those arrays will be written with type = count = offset = 0 as a temporary
192 * value.
193 *
194 * Its effect is only valid for the current directory, and before
195 * TIFFWriteDirectory() is first called, and will be reset when
196 * changing directory.
197 *
198 * The typical sequence of calls is:
199 * TIFFOpen()
200 * [ TIFFCreateDirectory(tif) ]
201 * Set fields with calls to TIFFSetField(tif, ...)
202 * TIFFDeferStrileArrayWriting(tif)
203 * TIFFWriteCheck(tif, ...)
204 * TIFFWriteDirectory(tif)
205 * ... potentially create other directories and come back to the above directory
206 * TIFFForceStrileArrayWriting(tif): emit the arrays at the end of file
207 *
208 * Returns 1 in case of success, 0 otherwise.
209 */
211{
212 static const char module[] = "TIFFDeferStrileArrayWriting";
213 if (tif->tif_mode == O_RDONLY)
214 {
216 "File opened in read-only mode");
217 return 0;
218 }
219 if( tif->tif_diroff != 0 )
220 {
222 "Directory has already been written");
223 return 0;
224 }
225
227 return 1;
228}
229
230/*
231 * Similar to TIFFWriteDirectory(), writes the directory out
232 * but leaves all data structures in memory so that it can be
233 * written again. This will make a partially written TIFF file
234 * readable before it is successfully completed/closed.
235 */
236int
238{
239 int rc;
240 /* Setup the strips arrays, if they haven't already been. */
241 if (tif->tif_dir.td_stripoffset_p == NULL)
242 (void) TIFFSetupStrips(tif);
245 return rc;
246}
247
248int
250{
251 return TIFFWriteDirectorySec(tif,FALSE,FALSE,pdiroff);
252}
253
254/*
255 * Similar to TIFFWriteDirectory(), but if the directory has already
256 * been written once, it is relocated to the end of the file, in case it
257 * has changed in size. Note that this will result in the loss of the
258 * previously used directory space.
259 */
260int
262{
263 static const char module[] = "TIFFRewriteDirectory";
264
265 /* We don't need to do anything special if it hasn't been written. */
266 if( tif->tif_diroff == 0 )
267 return TIFFWriteDirectory( tif );
268
269 /*
270 * Find and zero the pointer to this directory, so that TIFFLinkDirectory
271 * will cause it to be added after this directories current pre-link.
272 */
273
274 if (!(tif->tif_flags&TIFF_BIGTIFF))
275 {
276 if (tif->tif_header.classic.tiff_diroff == tif->tif_diroff)
277 {
279 tif->tif_diroff = 0;
280
281 TIFFSeekFile(tif,4,SEEK_SET);
282 if (!WriteOK(tif, &(tif->tif_header.classic.tiff_diroff),4))
283 {
285 "Error updating TIFF header");
286 return (0);
287 }
288 }
289 else
290 {
291 uint32 nextdir;
292 nextdir = tif->tif_header.classic.tiff_diroff;
293 while(1) {
294 uint16 dircount;
295 uint32 nextnextdir;
296
297 if (!SeekOK(tif, nextdir) ||
298 !ReadOK(tif, &dircount, 2)) {
300 "Error fetching directory count");
301 return (0);
302 }
303 if (tif->tif_flags & TIFF_SWAB)
304 TIFFSwabShort(&dircount);
305 (void) TIFFSeekFile(tif,
306 nextdir+2+dircount*12, SEEK_SET);
307 if (!ReadOK(tif, &nextnextdir, 4)) {
309 "Error fetching directory link");
310 return (0);
311 }
312 if (tif->tif_flags & TIFF_SWAB)
313 TIFFSwabLong(&nextnextdir);
314 if (nextnextdir==tif->tif_diroff)
315 {
316 uint32 m;
317 m=0;
318 (void) TIFFSeekFile(tif,
319 nextdir+2+dircount*12, SEEK_SET);
320 if (!WriteOK(tif, &m, 4)) {
322 "Error writing directory link");
323 return (0);
324 }
325 tif->tif_diroff=0;
326 break;
327 }
328 nextdir=nextnextdir;
329 }
330 }
331 }
332 else
333 {
334 if (tif->tif_header.big.tiff_diroff == tif->tif_diroff)
335 {
336 tif->tif_header.big.tiff_diroff = 0;
337 tif->tif_diroff = 0;
338
339 TIFFSeekFile(tif,8,SEEK_SET);
340 if (!WriteOK(tif, &(tif->tif_header.big.tiff_diroff),8))
341 {
343 "Error updating TIFF header");
344 return (0);
345 }
346 }
347 else
348 {
349 uint64 nextdir;
350 nextdir = tif->tif_header.big.tiff_diroff;
351 while(1) {
352 uint64 dircount64;
353 uint16 dircount;
354 uint64 nextnextdir;
355
356 if (!SeekOK(tif, nextdir) ||
357 !ReadOK(tif, &dircount64, 8)) {
359 "Error fetching directory count");
360 return (0);
361 }
362 if (tif->tif_flags & TIFF_SWAB)
363 TIFFSwabLong8(&dircount64);
364 if (dircount64>0xFFFF)
365 {
367 "Sanity check on tag count failed, likely corrupt TIFF");
368 return (0);
369 }
370 dircount=(uint16)dircount64;
371 (void) TIFFSeekFile(tif,
372 nextdir+8+dircount*20, SEEK_SET);
373 if (!ReadOK(tif, &nextnextdir, 8)) {
375 "Error fetching directory link");
376 return (0);
377 }
378 if (tif->tif_flags & TIFF_SWAB)
379 TIFFSwabLong8(&nextnextdir);
380 if (nextnextdir==tif->tif_diroff)
381 {
382 uint64 m;
383 m=0;
384 (void) TIFFSeekFile(tif,
385 nextdir+8+dircount*20, SEEK_SET);
386 if (!WriteOK(tif, &m, 8)) {
388 "Error writing directory link");
389 return (0);
390 }
391 tif->tif_diroff=0;
392 break;
393 }
394 nextdir=nextnextdir;
395 }
396 }
397 }
398
399 /*
400 * Now use TIFFWriteDirectory() normally.
401 */
402
403 return TIFFWriteDirectory( tif );
404}
405
406static int
407TIFFWriteDirectorySec(TIFF* tif, int isimage, int imagedone, uint64* pdiroff)
408{
409 static const char module[] = "TIFFWriteDirectorySec";
410 uint32 ndir;
412 uint32 dirsize;
413 void* dirmem;
414 uint32 m;
415 if (tif->tif_mode == O_RDONLY)
416 return (1);
417
418 _TIFFFillStriles( tif );
419
420 /*
421 * Clear write state so that subsequent images with
422 * different characteristics get the right buffers
423 * setup for them.
424 */
425 if (imagedone)
426 {
427 if (tif->tif_flags & TIFF_POSTENCODE)
428 {
429 tif->tif_flags &= ~TIFF_POSTENCODE;
430 if (!(*tif->tif_postencode)(tif))
431 {
433 "Error post-encoding before directory write");
434 return (0);
435 }
436 }
437 (*tif->tif_close)(tif); /* shutdown encoder */
438 /*
439 * Flush any data that might have been written
440 * by the compression close+cleanup routines. But
441 * be careful not to write stuff if we didn't add data
442 * in the previous steps as the "rawcc" data may well be
443 * a previously read tile/strip in mixed read/write mode.
444 */
445 if (tif->tif_rawcc > 0
446 && (tif->tif_flags & TIFF_BEENWRITING) != 0 )
447 {
448 if( !TIFFFlushData1(tif) )
449 {
451 "Error flushing data before directory write");
452 return (0);
453 }
454 }
455 if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata)
456 {
458 tif->tif_rawdata = NULL;
459 tif->tif_rawcc = 0;
460 tif->tif_rawdatasize = 0;
461 tif->tif_rawdataoff = 0;
462 tif->tif_rawdataloaded = 0;
463 }
465 }
466 dir=NULL;
467 dirmem=NULL;
468 dirsize=0;
469 while (1)
470 {
471 ndir=0;
472 if (isimage)
473 {
475 {
477 goto bad;
479 goto bad;
480 }
482 {
484 goto bad;
486 goto bad;
487 }
489 {
491 goto bad;
493 goto bad;
494 }
496 {
498 goto bad;
500 goto bad;
501 }
503 {
505 goto bad;
506 }
508 {
510 goto bad;
511 }
513 {
515 goto bad;
516 }
518 {
520 goto bad;
521 }
523 {
525 goto bad;
526 }
528 {
530 goto bad;
531 }
533 {
535 goto bad;
536 }
538 {
540 goto bad;
541 }
543 {
545 goto bad;
546 }
548 {
550 goto bad;
551 }
553 {
555 goto bad;
556 }
558 {
560 goto bad;
561 }
563 {
565 goto bad;
566 }
568 {
570 goto bad;
571 }
573 {
574 if (!isTiled(tif))
575 {
577 goto bad;
578 }
579 else
580 {
582 goto bad;
583 }
584 }
586 {
587 if (!isTiled(tif))
588 {
589 /* td_stripoffset_p might be NULL in an odd OJPEG case. See
590 * tif_dirread.c around line 3634.
591 * XXX: OJPEG hack.
592 * If a) compression is OJPEG, b) it's not a tiled TIFF,
593 * and c) the number of strips is 1,
594 * then we tolerate the absence of stripoffsets tag,
595 * because, presumably, all required data is in the
596 * JpegInterchangeFormat stream.
597 * We can get here when using tiffset on such a file.
598 * See http://bugzilla.maptools.org/show_bug.cgi?id=2500
599 */
600 if (tif->tif_dir.td_stripoffset_p != NULL &&
602 goto bad;
603 }
604 else
605 {
607 goto bad;
608 }
609 }
611 {
612 if (!TIFFWriteDirectoryTagColormap(tif,&ndir,dir))
613 goto bad;
614 }
616 {
617 if (tif->tif_dir.td_extrasamples)
618 {
619 uint16 na;
620 uint16* nb;
623 goto bad;
624 }
625 }
627 {
629 goto bad;
630 }
632 {
634 goto bad;
635 }
637 {
639 goto bad;
640 }
642 {
644 goto bad;
645 }
647 {
649 goto bad;
650 }
652 {
654 goto bad;
655 }
657 {
659 goto bad;
660 }
662 {
664 goto bad;
665 }
667 {
669 goto bad;
670 }
672 {
674 goto bad;
675 }
677 {
679 goto bad;
680 }
681 if (TIFFFieldSet(tif,FIELD_SUBIFD))
682 {
683 if (!TIFFWriteDirectoryTagSubifd(tif,&ndir,dir))
684 goto bad;
685 }
686 {
687 uint32 n;
688 for (n=0; n<tif->tif_nfields; n++) {
689 const TIFFField* o;
690 o = tif->tif_fields[n];
691 if ((o->field_bit>=FIELD_CODEC)&&(TIFFFieldSet(tif,o->field_bit)))
692 {
693 switch (o->get_field_type)
694 {
696 {
697 uint32 pa;
698 char* pb;
701 assert(o->field_passcount==0);
702 TIFFGetField(tif,o->field_tag,&pb);
703 pa=(uint32)(strlen(pb));
704 if (!TIFFWriteDirectoryTagAscii(tif,&ndir,dir,(uint16)o->field_tag,pa,pb))
705 goto bad;
706 }
707 break;
709 {
710 uint16 p;
712 assert(o->field_readcount==1);
713 assert(o->field_passcount==0);
714 TIFFGetField(tif,o->field_tag,&p);
715 if (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,(uint16)o->field_tag,p))
716 goto bad;
717 }
718 break;
720 {
721 uint32 p;
723 assert(o->field_readcount==1);
724 assert(o->field_passcount==0);
725 TIFFGetField(tif,o->field_tag,&p);
726 if (!TIFFWriteDirectoryTagLong(tif,&ndir,dir,(uint16)o->field_tag,p))
727 goto bad;
728 }
729 break;
731 {
732 uint32 pa;
733 void* pb;
736 assert(o->field_passcount==1);
737 TIFFGetField(tif,o->field_tag,&pa,&pb);
739 goto bad;
740 }
741 break;
742 default:
744 "Cannot write tag %d (%s)",
745 TIFFFieldTag(o),
746 o->field_name ? o->field_name : "unknown");
747 goto bad;
748 }
749 }
750 }
751 }
752 }
753 for (m=0; m<(uint32)(tif->tif_dir.td_customValueCount); m++)
754 {
758 {
759 case TIFF_ASCII:
761 goto bad;
762 break;
763 case TIFF_UNDEFINED:
765 goto bad;
766 break;
767 case TIFF_BYTE:
769 goto bad;
770 break;
771 case TIFF_SBYTE:
773 goto bad;
774 break;
775 case TIFF_SHORT:
777 goto bad;
778 break;
779 case TIFF_SSHORT:
781 goto bad;
782 break;
783 case TIFF_LONG:
785 goto bad;
786 break;
787 case TIFF_SLONG:
789 goto bad;
790 break;
791 case TIFF_LONG8:
793 goto bad;
794 break;
795 case TIFF_SLONG8:
797 goto bad;
798 break;
799 case TIFF_RATIONAL:
801 goto bad;
802 break;
803 case TIFF_SRATIONAL:
805 goto bad;
806 break;
807 case TIFF_FLOAT:
809 goto bad;
810 break;
811 case TIFF_DOUBLE:
813 goto bad;
814 break;
815 case TIFF_IFD:
817 goto bad;
818 break;
819 case TIFF_IFD8:
821 goto bad;
822 break;
823 default:
824 assert(0); /* we should never get here */
825 break;
826 }
827 }
828 if (dir!=NULL)
829 break;
830 dir=_TIFFmalloc(ndir*sizeof(TIFFDirEntry));
831 if (dir==NULL)
832 {
833 TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
834 goto bad;
835 }
836 if (isimage)
837 {
838 if ((tif->tif_diroff==0)&&(!TIFFLinkDirectory(tif)))
839 goto bad;
840 }
841 else
842 tif->tif_diroff=(TIFFSeekFile(tif,0,SEEK_END)+1)&(~((toff_t)1));
843 if (pdiroff!=NULL)
844 *pdiroff=tif->tif_diroff;
845 if (!(tif->tif_flags&TIFF_BIGTIFF))
846 dirsize=2+ndir*12+4;
847 else
848 dirsize=8+ndir*20+8;
849 tif->tif_dataoff=tif->tif_diroff+dirsize;
850 if (!(tif->tif_flags&TIFF_BIGTIFF))
851 tif->tif_dataoff=(uint32)tif->tif_dataoff;
852 if ((tif->tif_dataoff<tif->tif_diroff)||(tif->tif_dataoff<(uint64)dirsize))
853 {
854 TIFFErrorExt(tif->tif_clientdata,module,"Maximum TIFF file size exceeded");
855 goto bad;
856 }
857 if (tif->tif_dataoff&1)
858 tif->tif_dataoff++;
859 if (isimage)
860 tif->tif_curdir++;
861 }
862 if (isimage)
863 {
864 if (TIFFFieldSet(tif,FIELD_SUBIFD)&&(tif->tif_subifdoff==0))
865 {
866 uint32 na;
867 TIFFDirEntry* nb;
868 for (na=0, nb=dir; ; na++, nb++)
869 {
870 if( na == ndir )
871 {
873 "Cannot find SubIFD tag");
874 goto bad;
875 }
876 if (nb->tdir_tag==TIFFTAG_SUBIFD)
877 break;
878 }
879 if (!(tif->tif_flags&TIFF_BIGTIFF))
880 tif->tif_subifdoff=tif->tif_diroff+2+na*12+8;
881 else
882 tif->tif_subifdoff=tif->tif_diroff+8+na*20+12;
883 }
884 }
885 dirmem=_TIFFmalloc(dirsize);
886 if (dirmem==NULL)
887 {
888 TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
889 goto bad;
890 }
891 if (!(tif->tif_flags&TIFF_BIGTIFF))
892 {
893 uint8* n;
894 uint32 nTmp;
895 TIFFDirEntry* o;
896 n=dirmem;
897 *(uint16*)n=(uint16)ndir;
898 if (tif->tif_flags&TIFF_SWAB)
900 n+=2;
901 o=dir;
902 for (m=0; m<ndir; m++)
903 {
904 *(uint16*)n=o->tdir_tag;
905 if (tif->tif_flags&TIFF_SWAB)
907 n+=2;
908 *(uint16*)n=o->tdir_type;
909 if (tif->tif_flags&TIFF_SWAB)
911 n+=2;
912 nTmp = (uint32)o->tdir_count;
913 _TIFFmemcpy(n,&nTmp,4);
914 if (tif->tif_flags&TIFF_SWAB)
916 n+=4;
917 /* This is correct. The data has been */
918 /* swabbed previously in TIFFWriteDirectoryTagData */
920 n+=4;
921 o++;
922 }
923 nTmp = (uint32)tif->tif_nextdiroff;
924 if (tif->tif_flags&TIFF_SWAB)
925 TIFFSwabLong(&nTmp);
926 _TIFFmemcpy(n,&nTmp,4);
927 }
928 else
929 {
930 uint8* n;
931 TIFFDirEntry* o;
932 n=dirmem;
933 *(uint64*)n=ndir;
934 if (tif->tif_flags&TIFF_SWAB)
936 n+=8;
937 o=dir;
938 for (m=0; m<ndir; m++)
939 {
940 *(uint16*)n=o->tdir_tag;
941 if (tif->tif_flags&TIFF_SWAB)
943 n+=2;
944 *(uint16*)n=o->tdir_type;
945 if (tif->tif_flags&TIFF_SWAB)
947 n+=2;
948 _TIFFmemcpy(n,&o->tdir_count,8);
949 if (tif->tif_flags&TIFF_SWAB)
951 n+=8;
953 n+=8;
954 o++;
955 }
957 if (tif->tif_flags&TIFF_SWAB)
959 }
960 _TIFFfree(dir);
961 dir=NULL;
962 if (!SeekOK(tif,tif->tif_diroff))
963 {
964 TIFFErrorExt(tif->tif_clientdata,module,"IO error writing directory");
965 goto bad;
966 }
967 if (!WriteOK(tif,dirmem,(tmsize_t)dirsize))
968 {
969 TIFFErrorExt(tif->tif_clientdata,module,"IO error writing directory");
970 goto bad;
971 }
972 _TIFFfree(dirmem);
973 if (imagedone)
974 {
976 tif->tif_flags &= ~TIFF_DIRTYDIRECT;
977 tif->tif_flags &= ~TIFF_DIRTYSTRIP;
978 (*tif->tif_cleanup)(tif);
979 /*
980 * Reset directory-related state for subsequent
981 * directories.
982 */
984 }
985 return(1);
986bad:
987 if (dir!=NULL)
988 _TIFFfree(dir);
989 if (dirmem!=NULL)
990 _TIFFfree(dirmem);
991 return(0);
992}
993
995{
996 if( val > 127 )
997 return 127;
998 if( val < -128 || val != val )
999 return -128;
1000 return (int8)val;
1001}
1002
1004{
1005 if( val > 32767 )
1006 return 32767;
1007 if( val < -32768 || val != val )
1008 return -32768;
1009 return (int16)val;
1010}
1011
1013{
1014 if( val > 0x7FFFFFFF )
1015 return 0x7FFFFFFF;
1016 if( val < -0x7FFFFFFF-1 || val != val )
1017 return -0x7FFFFFFF-1;
1018 return (int32)val;
1019}
1020
1022{
1023 if( val < 0 )
1024 return 0;
1025 if( val > 255 || val != val )
1026 return 255;
1027 return (uint8)val;
1028}
1029
1031{
1032 if( val < 0 )
1033 return 0;
1034 if( val > 65535 || val != val )
1035 return 65535;
1036 return (uint16)val;
1037}
1038
1040{
1041 if( val < 0 )
1042 return 0;
1043 if( val > 0xFFFFFFFFU || val != val )
1044 return 0xFFFFFFFFU;
1045 return (uint32)val;
1046}
1047
1048static int
1050{
1051 static const char module[] = "TIFFWriteDirectoryTagSampleformatArray";
1052 void* conv;
1053 uint32 i;
1054 int ok;
1055 conv = _TIFFmalloc(count*sizeof(double));
1056 if (conv == NULL)
1057 {
1058 TIFFErrorExt(tif->tif_clientdata, module, "Out of memory");
1059 return (0);
1060 }
1061
1062 switch (tif->tif_dir.td_sampleformat)
1063 {
1065 if (tif->tif_dir.td_bitspersample<=32)
1066 {
1067 for (i = 0; i < count; ++i)
1068 ((float*)conv)[i] = _TIFFClampDoubleToFloat(value[i]);
1069 ok = TIFFWriteDirectoryTagFloatArray(tif,ndir,dir,tag,count,(float*)conv);
1070 }
1071 else
1072 {
1074 }
1075 break;
1076 case SAMPLEFORMAT_INT:
1077 if (tif->tif_dir.td_bitspersample<=8)
1078 {
1079 for (i = 0; i < count; ++i)
1080 ((int8*)conv)[i] = TIFFClampDoubleToInt8(value[i]);
1082 }
1083 else if (tif->tif_dir.td_bitspersample<=16)
1084 {
1085 for (i = 0; i < count; ++i)
1086 ((int16*)conv)[i] = TIFFClampDoubleToInt16(value[i]);
1088 }
1089 else
1090 {
1091 for (i = 0; i < count; ++i)
1092 ((int32*)conv)[i] = TIFFClampDoubleToInt32(value[i]);
1094 }
1095 break;
1096 case SAMPLEFORMAT_UINT:
1097 if (tif->tif_dir.td_bitspersample<=8)
1098 {
1099 for (i = 0; i < count; ++i)
1100 ((uint8*)conv)[i] = TIFFClampDoubleToUInt8(value[i]);
1102 }
1103 else if (tif->tif_dir.td_bitspersample<=16)
1104 {
1105 for (i = 0; i < count; ++i)
1106 ((uint16*)conv)[i] = TIFFClampDoubleToUInt16(value[i]);
1108 }
1109 else
1110 {
1111 for (i = 0; i < count; ++i)
1112 ((uint32*)conv)[i] = TIFFClampDoubleToUInt32(value[i]);
1114 }
1115 break;
1116 default:
1117 ok = 0;
1118 }
1119
1120 _TIFFfree(conv);
1121 return (ok);
1122}
1123
1124#if 0
1125static int
1126TIFFWriteDirectoryTagSampleformatPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value)
1127{
1128 switch (tif->tif_dir.td_sampleformat)
1129 {
1131 if (tif->tif_dir.td_bitspersample<=32)
1132 return(TIFFWriteDirectoryTagFloatPerSample(tif,ndir,dir,tag,(float)value));
1133 else
1134 return(TIFFWriteDirectoryTagDoublePerSample(tif,ndir,dir,tag,value));
1135 case SAMPLEFORMAT_INT:
1136 if (tif->tif_dir.td_bitspersample<=8)
1137 return(TIFFWriteDirectoryTagSbytePerSample(tif,ndir,dir,tag,(int8)value));
1138 else if (tif->tif_dir.td_bitspersample<=16)
1139 return(TIFFWriteDirectoryTagSshortPerSample(tif,ndir,dir,tag,(int16)value));
1140 else
1141 return(TIFFWriteDirectoryTagSlongPerSample(tif,ndir,dir,tag,(int32)value));
1142 case SAMPLEFORMAT_UINT:
1143 if (tif->tif_dir.td_bitspersample<=8)
1144 return(TIFFWriteDirectoryTagBytePerSample(tif,ndir,dir,tag,(uint8)value));
1145 else if (tif->tif_dir.td_bitspersample<=16)
1147 else
1148 return(TIFFWriteDirectoryTagLongPerSample(tif,ndir,dir,tag,(uint32)value));
1149 default:
1150 return(1);
1151 }
1152}
1153#endif
1154
1155static int
1157{
1158 if (dir==NULL)
1159 {
1160 (*ndir)++;
1161 return(1);
1162 }
1164}
1165
1166static int
1168{
1169 if (dir==NULL)
1170 {
1171 (*ndir)++;
1172 return(1);
1173 }
1175}
1176
1177#ifdef notdef
1178static int
1179TIFFWriteDirectoryTagByte(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint8 value)
1180{
1181 if (dir==NULL)
1182 {
1183 (*ndir)++;
1184 return(1);
1185 }
1186 return(TIFFWriteDirectoryTagCheckedByte(tif,ndir,dir,tag,value));
1187}
1188#endif
1189
1190static int
1192{
1193 if (dir==NULL)
1194 {
1195 (*ndir)++;
1196 return(1);
1197 }
1199}
1200
1201#if 0
1202static int
1203TIFFWriteDirectoryTagBytePerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint8 value)
1204{
1205 static const char module[] = "TIFFWriteDirectoryTagBytePerSample";
1206 uint8* m;
1207 uint8* na;
1208 uint16 nb;
1209 int o;
1210 if (dir==NULL)
1211 {
1212 (*ndir)++;
1213 return(1);
1214 }
1216 if (m==NULL)
1217 {
1218 TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
1219 return(0);
1220 }
1221 for (na=m, nb=0; nb<tif->tif_dir.td_samplesperpixel; na++, nb++)
1222 *na=value;
1224 _TIFFfree(m);
1225 return(o);
1226}
1227#endif
1228
1229#ifdef notdef
1230static int
1231TIFFWriteDirectoryTagSbyte(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int8 value)
1232{
1233 if (dir==NULL)
1234 {
1235 (*ndir)++;
1236 return(1);
1237 }
1238 return(TIFFWriteDirectoryTagCheckedSbyte(tif,ndir,dir,tag,value));
1239}
1240#endif
1241
1242static int
1244{
1245 if (dir==NULL)
1246 {
1247 (*ndir)++;
1248 return(1);
1249 }
1251}
1252
1253#if 0
1254static int
1255TIFFWriteDirectoryTagSbytePerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int8 value)
1256{
1257 static const char module[] = "TIFFWriteDirectoryTagSbytePerSample";
1258 int8* m;
1259 int8* na;
1260 uint16 nb;
1261 int o;
1262 if (dir==NULL)
1263 {
1264 (*ndir)++;
1265 return(1);
1266 }
1268 if (m==NULL)
1269 {
1270 TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
1271 return(0);
1272 }
1273 for (na=m, nb=0; nb<tif->tif_dir.td_samplesperpixel; na++, nb++)
1274 *na=value;
1276 _TIFFfree(m);
1277 return(o);
1278}
1279#endif
1280
1281static int
1283{
1284 if (dir==NULL)
1285 {
1286 (*ndir)++;
1287 return(1);
1288 }
1290}
1291
1292static int
1294{
1295 if (dir==NULL)
1296 {
1297 (*ndir)++;
1298 return(1);
1299 }
1301}
1302
1303static int
1305{
1306 static const char module[] = "TIFFWriteDirectoryTagShortPerSample";
1307 uint16* m;
1308 uint16* na;
1309 uint16 nb;
1310 int o;
1311 if (dir==NULL)
1312 {
1313 (*ndir)++;
1314 return(1);
1315 }
1317 if (m==NULL)
1318 {
1319 TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
1320 return(0);
1321 }
1322 for (na=m, nb=0; nb<tif->tif_dir.td_samplesperpixel; na++, nb++)
1323 *na=value;
1325 _TIFFfree(m);
1326 return(o);
1327}
1328
1329#ifdef notdef
1330static int
1331TIFFWriteDirectoryTagSshort(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int16 value)
1332{
1333 if (dir==NULL)
1334 {
1335 (*ndir)++;
1336 return(1);
1337 }
1338 return(TIFFWriteDirectoryTagCheckedSshort(tif,ndir,dir,tag,value));
1339}
1340#endif
1341
1342static int
1344{
1345 if (dir==NULL)
1346 {
1347 (*ndir)++;
1348 return(1);
1349 }
1351}
1352
1353#if 0
1354static int
1355TIFFWriteDirectoryTagSshortPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int16 value)
1356{
1357 static const char module[] = "TIFFWriteDirectoryTagSshortPerSample";
1358 int16* m;
1359 int16* na;
1360 uint16 nb;
1361 int o;
1362 if (dir==NULL)
1363 {
1364 (*ndir)++;
1365 return(1);
1366 }
1368 if (m==NULL)
1369 {
1370 TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
1371 return(0);
1372 }
1373 for (na=m, nb=0; nb<tif->tif_dir.td_samplesperpixel; na++, nb++)
1374 *na=value;
1376 _TIFFfree(m);
1377 return(o);
1378}
1379#endif
1380
1381static int
1383{
1384 if (dir==NULL)
1385 {
1386 (*ndir)++;
1387 return(1);
1388 }
1390}
1391
1392static int
1394{
1395 if (dir==NULL)
1396 {
1397 (*ndir)++;
1398 return(1);
1399 }
1401}
1402
1403#if 0
1404static int
1405TIFFWriteDirectoryTagLongPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 value)
1406{
1407 static const char module[] = "TIFFWriteDirectoryTagLongPerSample";
1408 uint32* m;
1409 uint32* na;
1410 uint16 nb;
1411 int o;
1412 if (dir==NULL)
1413 {
1414 (*ndir)++;
1415 return(1);
1416 }
1418 if (m==NULL)
1419 {
1420 TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
1421 return(0);
1422 }
1423 for (na=m, nb=0; nb<tif->tif_dir.td_samplesperpixel; na++, nb++)
1424 *na=value;
1426 _TIFFfree(m);
1427 return(o);
1428}
1429#endif
1430
1431#ifdef notdef
1432static int
1433TIFFWriteDirectoryTagSlong(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int32 value)
1434{
1435 if (dir==NULL)
1436 {
1437 (*ndir)++;
1438 return(1);
1439 }
1440 return(TIFFWriteDirectoryTagCheckedSlong(tif,ndir,dir,tag,value));
1441}
1442#endif
1443
1444static int
1446{
1447 if (dir==NULL)
1448 {
1449 (*ndir)++;
1450 return(1);
1451 }
1453}
1454
1455#if 0
1456static int
1457TIFFWriteDirectoryTagSlongPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int32 value)
1458{
1459 static const char module[] = "TIFFWriteDirectoryTagSlongPerSample";
1460 int32* m;
1461 int32* na;
1462 uint16 nb;
1463 int o;
1464 if (dir==NULL)
1465 {
1466 (*ndir)++;
1467 return(1);
1468 }
1470 if (m==NULL)
1471 {
1472 TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
1473 return(0);
1474 }
1475 for (na=m, nb=0; nb<tif->tif_dir.td_samplesperpixel; na++, nb++)
1476 *na=value;
1478 _TIFFfree(m);
1479 return(o);
1480}
1481#endif
1482
1483#ifdef notdef
1484static int
1485TIFFWriteDirectoryTagLong8(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint64 value)
1486{
1487 if (dir==NULL)
1488 {
1489 (*ndir)++;
1490 return(1);
1491 }
1492 return(TIFFWriteDirectoryTagCheckedLong8(tif,ndir,dir,tag,value));
1493}
1494#endif
1495
1496static int
1498{
1499 if (dir==NULL)
1500 {
1501 (*ndir)++;
1502 return(1);
1503 }
1505}
1506
1507#ifdef notdef
1508static int
1509TIFFWriteDirectoryTagSlong8(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int64 value)
1510{
1511 if (dir==NULL)
1512 {
1513 (*ndir)++;
1514 return(1);
1515 }
1516 return(TIFFWriteDirectoryTagCheckedSlong8(tif,ndir,dir,tag,value));
1517}
1518#endif
1519
1520static int
1522{
1523 if (dir==NULL)
1524 {
1525 (*ndir)++;
1526 return(1);
1527 }
1529}
1530
1531static int
1533{
1534 if (dir==NULL)
1535 {
1536 (*ndir)++;
1537 return(1);
1538 }
1540}
1541
1542static int
1544{
1545 if (dir==NULL)
1546 {
1547 (*ndir)++;
1548 return(1);
1549 }
1551}
1552
1553static int
1555{
1556 if (dir==NULL)
1557 {
1558 (*ndir)++;
1559 return(1);
1560 }
1562}
1563
1564#ifdef notdef
1565static int TIFFWriteDirectoryTagFloat(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, float value)
1566{
1567 if (dir==NULL)
1568 {
1569 (*ndir)++;
1570 return(1);
1571 }
1572 return(TIFFWriteDirectoryTagCheckedFloat(tif,ndir,dir,tag,value));
1573}
1574#endif
1575
1577{
1578 if (dir==NULL)
1579 {
1580 (*ndir)++;
1581 return(1);
1582 }
1584}
1585
1586#if 0
1587static int TIFFWriteDirectoryTagFloatPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, float value)
1588{
1589 static const char module[] = "TIFFWriteDirectoryTagFloatPerSample";
1590 float* m;
1591 float* na;
1592 uint16 nb;
1593 int o;
1594 if (dir==NULL)
1595 {
1596 (*ndir)++;
1597 return(1);
1598 }
1599 m=_TIFFmalloc(tif->tif_dir.td_samplesperpixel*sizeof(float));
1600 if (m==NULL)
1601 {
1602 TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
1603 return(0);
1604 }
1605 for (na=m, nb=0; nb<tif->tif_dir.td_samplesperpixel; na++, nb++)
1606 *na=value;
1608 _TIFFfree(m);
1609 return(o);
1610}
1611#endif
1612
1613#ifdef notdef
1614static int TIFFWriteDirectoryTagDouble(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value)
1615{
1616 if (dir==NULL)
1617 {
1618 (*ndir)++;
1619 return(1);
1620 }
1621 return(TIFFWriteDirectoryTagCheckedDouble(tif,ndir,dir,tag,value));
1622}
1623#endif
1624
1626{
1627 if (dir==NULL)
1628 {
1629 (*ndir)++;
1630 return(1);
1631 }
1633}
1634
1635#if 0
1636static int TIFFWriteDirectoryTagDoublePerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value)
1637{
1638 static const char module[] = "TIFFWriteDirectoryTagDoublePerSample";
1639 double* m;
1640 double* na;
1641 uint16 nb;
1642 int o;
1643 if (dir==NULL)
1644 {
1645 (*ndir)++;
1646 return(1);
1647 }
1648 m=_TIFFmalloc(tif->tif_dir.td_samplesperpixel*sizeof(double));
1649 if (m==NULL)
1650 {
1651 TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
1652 return(0);
1653 }
1654 for (na=m, nb=0; nb<tif->tif_dir.td_samplesperpixel; na++, nb++)
1655 *na=value;
1657 _TIFFfree(m);
1658 return(o);
1659}
1660#endif
1661
1662static int
1664{
1665 if (dir==NULL)
1666 {
1667 (*ndir)++;
1668 return(1);
1669 }
1671}
1672
1673#ifdef notdef
1674static int
1675TIFFWriteDirectoryTagIfd8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value)
1676{
1677 if (dir==NULL)
1678 {
1679 (*ndir)++;
1680 return(1);
1681 }
1683}
1684#endif
1685
1686static int
1688{
1689 if (dir==NULL)
1690 {
1691 (*ndir)++;
1692 return(1);
1693 }
1694 if (value<=0xFFFF)
1696 else
1698}
1699
1700static int _WriteAsType(TIFF* tif, uint64 strile_size, uint64 uncompressed_threshold)
1701{
1704 {
1705 return strile_size > uncompressed_threshold;
1706 }
1707 else if ( compression == COMPRESSION_JPEG ||
1714 {
1715 /* For a few select compression types, we assume that in the worst */
1716 /* case the compressed size will be 10 times the uncompressed size */
1717 /* This is overly pessismistic ! */
1718 return strile_size >= uncompressed_threshold / 10;
1719 }
1720 return 1;
1721}
1722
1723static int WriteAsLong8(TIFF* tif, uint64 strile_size)
1724{
1725 return _WriteAsType(tif, strile_size, 0xFFFFFFFFU);
1726}
1727
1728static int WriteAsLong4(TIFF* tif, uint64 strile_size)
1729{
1730 return _WriteAsType(tif, strile_size, 0xFFFFU);
1731}
1732
1733/************************************************************************/
1734/* TIFFWriteDirectoryTagLongLong8Array() */
1735/* */
1736/* Write out LONG8 array and write a SHORT/LONG/LONG8 depending */
1737/* on strile size and Classic/BigTIFF mode. */
1738/************************************************************************/
1739
1740static int
1742{
1743 static const char module[] = "TIFFWriteDirectoryTagLongLong8Array";
1744 int o;
1745 int write_aslong4;
1746
1747 /* is this just a counting pass? */
1748 if (dir==NULL)
1749 {
1750 (*ndir)++;
1751 return(1);
1752 }
1753
1755 {
1756 return TIFFWriteDirectoryTagData(tif, ndir, dir, tag, TIFF_NOTYPE, 0, 0, NULL);
1757 }
1758
1759 if( tif->tif_flags&TIFF_BIGTIFF )
1760 {
1761 int write_aslong8 = 1;
1762 /* In the case of ByteCounts array, we may be able to write them on */
1763 /* LONG if the strip/tilesize is not too big. */
1764 /* Also do that for count > 1 in the case someone would want to create */
1765 /* a single-strip file with a growing height, in which case using */
1766 /* LONG8 will be safer. */
1767 if( count > 1 && tag == TIFFTAG_STRIPBYTECOUNTS )
1768 {
1769 write_aslong8 = WriteAsLong8(tif, TIFFStripSize64(tif));
1770 }
1771 else if( count > 1 && tag == TIFFTAG_TILEBYTECOUNTS )
1772 {
1773 write_aslong8 = WriteAsLong8(tif, TIFFTileSize64(tif));
1774 }
1775 if( write_aslong8 )
1776 {
1778 tag,count,value);
1779 }
1780 }
1781
1782 write_aslong4 = 1;
1783 if( count > 1 && tag == TIFFTAG_STRIPBYTECOUNTS )
1784 {
1785 write_aslong4 = WriteAsLong4(tif, TIFFStripSize64(tif));
1786 }
1787 else if( count > 1 && tag == TIFFTAG_TILEBYTECOUNTS )
1788 {
1789 write_aslong4 = WriteAsLong4(tif, TIFFTileSize64(tif));
1790 }
1791 if( write_aslong4 )
1792 {
1793 /*
1794 ** For classic tiff we want to verify everything is in range for LONG
1795 ** and convert to long format.
1796 */
1797
1798 uint32* p = _TIFFmalloc(count*sizeof(uint32));
1799 uint32* q;
1800 uint64* ma;
1801 uint32 mb;
1802
1803 if (p==NULL)
1804 {
1805 TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
1806 return(0);
1807 }
1808
1809 for (q=p, ma=value, mb=0; mb<count; ma++, mb++, q++)
1810 {
1811 if (*ma>0xFFFFFFFF)
1812 {
1814 "Attempt to write value larger than 0xFFFFFFFF in LONG array.");
1815 _TIFFfree(p);
1816 return(0);
1817 }
1818 *q= (uint32)(*ma);
1819 }
1820
1822 _TIFFfree(p);
1823 }
1824 else
1825 {
1826 uint16* p = _TIFFmalloc(count*sizeof(uint16));
1827 uint16* q;
1828 uint64* ma;
1829 uint32 mb;
1830
1831 if (p==NULL)
1832 {
1833 TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
1834 return(0);
1835 }
1836
1837 for (q=p, ma=value, mb=0; mb<count; ma++, mb++, q++)
1838 {
1839 if (*ma>0xFFFF)
1840 {
1841 /* Should not happen normally given the check we did before */
1843 "Attempt to write value larger than 0xFFFF in SHORT array.");
1844 _TIFFfree(p);
1845 return(0);
1846 }
1847 *q= (uint16)(*ma);
1848 }
1849
1851 _TIFFfree(p);
1852 }
1853
1854 return(o);
1855}
1856
1857/************************************************************************/
1858/* TIFFWriteDirectoryTagIfdIfd8Array() */
1859/* */
1860/* Write either IFD8 or IFD array depending on file type. */
1861/************************************************************************/
1862
1863static int
1865{
1866 static const char module[] = "TIFFWriteDirectoryTagIfdIfd8Array";
1867 uint64* ma;
1868 uint32 mb;
1869 uint32* p;
1870 uint32* q;
1871 int o;
1872
1873 /* is this just a counting pass? */
1874 if (dir==NULL)
1875 {
1876 (*ndir)++;
1877 return(1);
1878 }
1879
1880 /* We always write IFD8 for BigTIFF, no checking needed. */
1881 if( tif->tif_flags&TIFF_BIGTIFF )
1883 tag,count,value);
1884
1885 /*
1886 ** For classic tiff we want to verify everything is in range for IFD
1887 ** and convert to long format.
1888 */
1889
1890 p = _TIFFmalloc(count*sizeof(uint32));
1891 if (p==NULL)
1892 {
1893 TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
1894 return(0);
1895 }
1896
1897 for (q=p, ma=value, mb=0; mb<count; ma++, mb++, q++)
1898 {
1899 if (*ma>0xFFFFFFFF)
1900 {
1902 "Attempt to write value larger than 0xFFFFFFFF in Classic TIFF file.");
1903 _TIFFfree(p);
1904 return(0);
1905 }
1906 *q= (uint32)(*ma);
1907 }
1908
1910 _TIFFfree(p);
1911
1912 return(o);
1913}
1914
1915#ifdef notdef
1916static int
1917TIFFWriteDirectoryTagShortLongLong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value)
1918{
1919 static const char module[] = "TIFFWriteDirectoryTagShortLongLong8Array";
1920 uint64* ma;
1921 uint32 mb;
1922 uint8 n;
1923 int o;
1924 if (dir==NULL)
1925 {
1926 (*ndir)++;
1927 return(1);
1928 }
1929 n=0;
1930 for (ma=value, mb=0; mb<count; ma++, mb++)
1931 {
1932 if ((n==0)&&(*ma>0xFFFF))
1933 n=1;
1934 if ((n==1)&&(*ma>0xFFFFFFFF))
1935 {
1936 n=2;
1937 break;
1938 }
1939 }
1940 if (n==0)
1941 {
1942 uint16* p;
1943 uint16* q;
1944 p=_TIFFmalloc(count*sizeof(uint16));
1945 if (p==NULL)
1946 {
1947 TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
1948 return(0);
1949 }
1950 for (ma=value, mb=0, q=p; mb<count; ma++, mb++, q++)
1951 *q=(uint16)(*ma);
1953 _TIFFfree(p);
1954 }
1955 else if (n==1)
1956 {
1957 uint32* p;
1958 uint32* q;
1959 p=_TIFFmalloc(count*sizeof(uint32));
1960 if (p==NULL)
1961 {
1962 TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
1963 return(0);
1964 }
1965 for (ma=value, mb=0, q=p; mb<count; ma++, mb++, q++)
1966 *q=(uint32)(*ma);
1968 _TIFFfree(p);
1969 }
1970 else
1971 {
1972 assert(n==2);
1974 }
1975 return(o);
1976}
1977#endif
1978static int
1980{
1981 static const char module[] = "TIFFWriteDirectoryTagColormap";
1982 uint32 m;
1983 uint16* n;
1984 int o;
1985 if (dir==NULL)
1986 {
1987 (*ndir)++;
1988 return(1);
1989 }
1990 m=(1<<tif->tif_dir.td_bitspersample);
1991 n=_TIFFmalloc(3*m*sizeof(uint16));
1992 if (n==NULL)
1993 {
1994 TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
1995 return(0);
1996 }
1997 _TIFFmemcpy(&n[0],tif->tif_dir.td_colormap[0],m*sizeof(uint16));
1998 _TIFFmemcpy(&n[m],tif->tif_dir.td_colormap[1],m*sizeof(uint16));
1999 _TIFFmemcpy(&n[2*m],tif->tif_dir.td_colormap[2],m*sizeof(uint16));
2001 _TIFFfree(n);
2002 return(o);
2003}
2004
2005static int
2007{
2008 static const char module[] = "TIFFWriteDirectoryTagTransferfunction";
2009 uint32 m;
2010 uint16 n;
2011 uint16* o;
2012 int p;
2013 if (dir==NULL)
2014 {
2015 (*ndir)++;
2016 return(1);
2017 }
2018 m=(1<<tif->tif_dir.td_bitspersample);
2020 /*
2021 * Check if the table can be written as a single column,
2022 * or if it must be written as 3 columns. Note that we
2023 * write a 3-column tag if there are 2 samples/pixel and
2024 * a single column of data won't suffice--hmm.
2025 */
2026 if (n>3)
2027 n=3;
2028 if (n==3)
2029 {
2030 if (tif->tif_dir.td_transferfunction[2] == NULL ||
2032 n=2;
2033 }
2034 if (n==2)
2035 {
2036 if (tif->tif_dir.td_transferfunction[1] == NULL ||
2038 n=1;
2039 }
2040 if (n==0)
2041 n=1;
2042 o=_TIFFmalloc(n*m*sizeof(uint16));
2043 if (o==NULL)
2044 {
2045 TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
2046 return(0);
2047 }
2048 _TIFFmemcpy(&o[0],tif->tif_dir.td_transferfunction[0],m*sizeof(uint16));
2049 if (n>1)
2050 _TIFFmemcpy(&o[m],tif->tif_dir.td_transferfunction[1],m*sizeof(uint16));
2051 if (n>2)
2052 _TIFFmemcpy(&o[2*m],tif->tif_dir.td_transferfunction[2],m*sizeof(uint16));
2054 _TIFFfree(o);
2055 return(p);
2056}
2057
2058static int
2060{
2061 static const char module[] = "TIFFWriteDirectoryTagSubifd";
2062 uint64 m;
2063 int n;
2064 if (tif->tif_dir.td_nsubifd==0)
2065 return(1);
2066 if (dir==NULL)
2067 {
2068 (*ndir)++;
2069 return(1);
2070 }
2071 m=tif->tif_dataoff;
2072 if (!(tif->tif_flags&TIFF_BIGTIFF))
2073 {
2074 uint32* o;
2075 uint64* pa;
2076 uint32* pb;
2077 uint16 p;
2078 o=_TIFFmalloc(tif->tif_dir.td_nsubifd*sizeof(uint32));
2079 if (o==NULL)
2080 {
2081 TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
2082 return(0);
2083 }
2084 pa=tif->tif_dir.td_subifd;
2085 pb=o;
2086 for (p=0; p < tif->tif_dir.td_nsubifd; p++)
2087 {
2088 assert(pa != 0);
2089
2090 /* Could happen if an classicTIFF has a SubIFD of type LONG8 (which is illegal) */
2091 if( *pa > 0xFFFFFFFFUL)
2092 {
2093 TIFFErrorExt(tif->tif_clientdata,module,"Illegal value for SubIFD tag");
2094 _TIFFfree(o);
2095 return(0);
2096 }
2097 *pb++=(uint32)(*pa++);
2098 }
2100 _TIFFfree(o);
2101 }
2102 else
2104 if (!n)
2105 return(0);
2106 /*
2107 * Total hack: if this directory includes a SubIFD
2108 * tag then force the next <n> directories to be
2109 * written as ``sub directories'' of this one. This
2110 * is used to write things like thumbnails and
2111 * image masks that one wants to keep out of the
2112 * normal directory linkage access mechanism.
2113 */
2115 tif->tif_nsubifd=tif->tif_dir.td_nsubifd;
2116 if (tif->tif_dir.td_nsubifd==1)
2117 tif->tif_subifdoff=0;
2118 else
2119 tif->tif_subifdoff=m;
2120 return(1);
2121}
2122
2123static int
2125{
2126 assert(sizeof(char)==1);
2128}
2129
2130static int
2132{
2133 assert(sizeof(uint8)==1);
2135}
2136
2137#ifdef notdef
2138static int
2139TIFFWriteDirectoryTagCheckedByte(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint8 value)
2140{
2141 assert(sizeof(uint8)==1);
2142 return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_BYTE,1,1,&value));
2143}
2144#endif
2145
2146static int
2148{
2149 assert(sizeof(uint8)==1);
2151}
2152
2153#ifdef notdef
2154static int
2155TIFFWriteDirectoryTagCheckedSbyte(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int8 value)
2156{
2157 assert(sizeof(int8)==1);
2158 return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SBYTE,1,1,&value));
2159}
2160#endif
2161
2162static int
2164{
2165 assert(sizeof(int8)==1);
2167}
2168
2169static int
2171{
2172 uint16 m;
2173 assert(sizeof(uint16)==2);
2174 m=value;
2175 if (tif->tif_flags&TIFF_SWAB)
2176 TIFFSwabShort(&m);
2177 return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SHORT,1,2,&m));
2178}
2179
2180static int
2182{
2183 assert(count<0x80000000);
2184 assert(sizeof(uint16)==2);
2185 if (tif->tif_flags&TIFF_SWAB)
2188}
2189
2190#ifdef notdef
2191static int
2192TIFFWriteDirectoryTagCheckedSshort(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int16 value)
2193{
2194 int16 m;
2195 assert(sizeof(int16)==2);
2196 m=value;
2197 if (tif->tif_flags&TIFF_SWAB)
2198 TIFFSwabShort((uint16*)(&m));
2199 return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SSHORT,1,2,&m));
2200}
2201#endif
2202
2203static int
2205{
2206 assert(count<0x80000000);
2207 assert(sizeof(int16)==2);
2208 if (tif->tif_flags&TIFF_SWAB)
2211}
2212
2213static int
2215{
2216 uint32 m;
2217 assert(sizeof(uint32)==4);
2218 m=value;
2219 if (tif->tif_flags&TIFF_SWAB)
2220 TIFFSwabLong(&m);
2221 return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_LONG,1,4,&m));
2222}
2223
2224static int
2226{
2227 assert(count<0x40000000);
2228 assert(sizeof(uint32)==4);
2229 if (tif->tif_flags&TIFF_SWAB)
2232}
2233
2234#ifdef notdef
2235static int
2236TIFFWriteDirectoryTagCheckedSlong(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int32 value)
2237{
2238 int32 m;
2239 assert(sizeof(int32)==4);
2240 m=value;
2241 if (tif->tif_flags&TIFF_SWAB)
2242 TIFFSwabLong((uint32*)(&m));
2243 return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SLONG,1,4,&m));
2244}
2245#endif
2246
2247static int
2249{
2250 assert(count<0x40000000);
2251 assert(sizeof(int32)==4);
2252 if (tif->tif_flags&TIFF_SWAB)
2255}
2256
2257#ifdef notdef
2258static int
2259TIFFWriteDirectoryTagCheckedLong8(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint64 value)
2260{
2261 uint64 m;
2262 assert(sizeof(uint64)==8);
2263 if( !(tif->tif_flags&TIFF_BIGTIFF) ) {
2264 TIFFErrorExt(tif->tif_clientdata,"TIFFWriteDirectoryTagCheckedLong8","LONG8 not allowed for ClassicTIFF");
2265 return(0);
2266 }
2267 m=value;
2268 if (tif->tif_flags&TIFF_SWAB)
2269 TIFFSwabLong8(&m);
2270 return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_LONG8,1,8,&m));
2271}
2272#endif
2273
2274static int
2276{
2277 assert(count<0x20000000);
2278 assert(sizeof(uint64)==8);
2279 if( !(tif->tif_flags&TIFF_BIGTIFF) ) {
2280 TIFFErrorExt(tif->tif_clientdata,"TIFFWriteDirectoryTagCheckedLong8Array","LONG8 not allowed for ClassicTIFF");
2281 return(0);
2282 }
2283 if (tif->tif_flags&TIFF_SWAB)
2286}
2287
2288#ifdef notdef
2289static int
2290TIFFWriteDirectoryTagCheckedSlong8(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int64 value)
2291{
2292 int64 m;
2293 assert(sizeof(int64)==8);
2294 if( !(tif->tif_flags&TIFF_BIGTIFF) ) {
2295 TIFFErrorExt(tif->tif_clientdata,"TIFFWriteDirectoryTagCheckedSlong8","SLONG8 not allowed for ClassicTIFF");
2296 return(0);
2297 }
2298 m=value;
2299 if (tif->tif_flags&TIFF_SWAB)
2300 TIFFSwabLong8((uint64*)(&m));
2301 return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SLONG8,1,8,&m));
2302}
2303#endif
2304
2305static int
2307{
2308 assert(count<0x20000000);
2309 assert(sizeof(int64)==8);
2310 if( !(tif->tif_flags&TIFF_BIGTIFF) ) {
2311 TIFFErrorExt(tif->tif_clientdata,"TIFFWriteDirectoryTagCheckedSlong8Array","SLONG8 not allowed for ClassicTIFF");
2312 return(0);
2313 }
2314 if (tif->tif_flags&TIFF_SWAB)
2317}
2318
2319static int
2321{
2322 static const char module[] = "TIFFWriteDirectoryTagCheckedRational";
2323 uint32 m[2];
2324 assert(sizeof(uint32)==4);
2325 if( value < 0 )
2326 {
2327 TIFFErrorExt(tif->tif_clientdata,module,"Negative value is illegal");
2328 return 0;
2329 }
2330 else if( value != value )
2331 {
2332 TIFFErrorExt(tif->tif_clientdata,module,"Not-a-number value is illegal");
2333 return 0;
2334 }
2335 else if (value==0.0)
2336 {
2337 m[0]=0;
2338 m[1]=1;
2339 }
2340 else if (value <= 0xFFFFFFFFU && value==(double)(uint32)value)
2341 {
2342 m[0]=(uint32)value;
2343 m[1]=1;
2344 }
2345 else if (value<1.0)
2346 {
2347 m[0]=(uint32)(value*0xFFFFFFFF);
2348 m[1]=0xFFFFFFFF;
2349 }
2350 else
2351 {
2352 m[0]=0xFFFFFFFF;
2353 m[1]=(uint32)(0xFFFFFFFF/value);
2354 }
2355 if (tif->tif_flags&TIFF_SWAB)
2356 {
2357 TIFFSwabLong(&m[0]);
2358 TIFFSwabLong(&m[1]);
2359 }
2360 return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_RATIONAL,1,8,&m[0]));
2361}
2362
2363static int
2365{
2366 static const char module[] = "TIFFWriteDirectoryTagCheckedRationalArray";
2367 uint32* m;
2368 float* na;
2369 uint32* nb;
2370 uint32 nc;
2371 int o;
2372 assert(sizeof(uint32)==4);
2373 m=_TIFFmalloc(count*2*sizeof(uint32));
2374 if (m==NULL)
2375 {
2376 TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
2377 return(0);
2378 }
2379 for (na=value, nb=m, nc=0; nc<count; na++, nb+=2, nc++)
2380 {
2381 if (*na<=0.0 || *na != *na)
2382 {
2383 nb[0]=0;
2384 nb[1]=1;
2385 }
2386 else if (*na >= 0 && *na <= (float)0xFFFFFFFFU &&
2387 *na==(float)(uint32)(*na))
2388 {
2389 nb[0]=(uint32)(*na);
2390 nb[1]=1;
2391 }
2392 else if (*na<1.0)
2393 {
2394 nb[0]=(uint32)((double)(*na)*0xFFFFFFFF);
2395 nb[1]=0xFFFFFFFF;
2396 }
2397 else
2398 {
2399 nb[0]=0xFFFFFFFF;
2400 nb[1]=(uint32)((double)0xFFFFFFFF/(*na));
2401 }
2402 }
2403 if (tif->tif_flags&TIFF_SWAB)
2406 _TIFFfree(m);
2407 return(o);
2408}
2409
2410static int
2412{
2413 static const char module[] = "TIFFWriteDirectoryTagCheckedSrationalArray";
2414 int32* m;
2415 float* na;
2416 int32* nb;
2417 uint32 nc;
2418 int o;
2419 assert(sizeof(int32)==4);
2420 m=_TIFFmalloc(count*2*sizeof(int32));
2421 if (m==NULL)
2422 {
2423 TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
2424 return(0);
2425 }
2426 for (na=value, nb=m, nc=0; nc<count; na++, nb+=2, nc++)
2427 {
2428 if (*na<0.0)
2429 {
2430 if (*na==(int32)(*na))
2431 {
2432 nb[0]=(int32)(*na);
2433 nb[1]=1;
2434 }
2435 else if (*na>-1.0)
2436 {
2437 nb[0]=-(int32)((double)(-*na)*0x7FFFFFFF);
2438 nb[1]=0x7FFFFFFF;
2439 }
2440 else
2441 {
2442 nb[0]=-0x7FFFFFFF;
2443 nb[1]=(int32)((double)0x7FFFFFFF/(-*na));
2444 }
2445 }
2446 else
2447 {
2448 if (*na==(int32)(*na))
2449 {
2450 nb[0]=(int32)(*na);
2451 nb[1]=1;
2452 }
2453 else if (*na<1.0)
2454 {
2455 nb[0]=(int32)((double)(*na)*0x7FFFFFFF);
2456 nb[1]=0x7FFFFFFF;
2457 }
2458 else
2459 {
2460 nb[0]=0x7FFFFFFF;
2461 nb[1]=(int32)((double)0x7FFFFFFF/(*na));
2462 }
2463 }
2464 }
2465 if (tif->tif_flags&TIFF_SWAB)
2468 _TIFFfree(m);
2469 return(o);
2470}
2471
2472#ifdef notdef
2473static int
2474TIFFWriteDirectoryTagCheckedFloat(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, float value)
2475{
2476 float m;
2477 assert(sizeof(float)==4);
2478 m=value;
2480 if (tif->tif_flags&TIFF_SWAB)
2481 TIFFSwabFloat(&m);
2482 return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_FLOAT,1,4,&m));
2483}
2484#endif
2485
2486static int
2488{
2489 assert(count<0x40000000);
2490 assert(sizeof(float)==4);
2492 if (tif->tif_flags&TIFF_SWAB)
2495}
2496
2497#ifdef notdef
2498static int
2499TIFFWriteDirectoryTagCheckedDouble(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value)
2500{
2501 double m;
2502 assert(sizeof(double)==8);
2503 m=value;
2505 if (tif->tif_flags&TIFF_SWAB)
2506 TIFFSwabDouble(&m);
2507 return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_DOUBLE,1,8,&m));
2508}
2509#endif
2510
2511static int
2513{
2514 assert(count<0x20000000);
2515 assert(sizeof(double)==8);
2517 if (tif->tif_flags&TIFF_SWAB)
2520}
2521
2522static int
2524{
2525 assert(count<0x40000000);
2526 assert(sizeof(uint32)==4);
2527 if (tif->tif_flags&TIFF_SWAB)
2530}
2531
2532static int
2534{
2535 assert(count<0x20000000);
2536 assert(sizeof(uint64)==8);
2538 if (tif->tif_flags&TIFF_SWAB)
2541}
2542
2543static int
2545{
2546 static const char module[] = "TIFFWriteDirectoryTagData";
2547 uint32 m;
2548 m=0;
2549 while (m<(*ndir))
2550 {
2551 assert(dir[m].tdir_tag!=tag);
2552 if (dir[m].tdir_tag>tag)
2553 break;
2554 m++;
2555 }
2556 if (m<(*ndir))
2557 {
2558 uint32 n;
2559 for (n=*ndir; n>m; n--)
2560 dir[n]=dir[n-1];
2561 }
2562 dir[m].tdir_tag=tag;
2563 dir[m].tdir_type=datatype;
2564 dir[m].tdir_count=count;
2565 dir[m].tdir_offset.toff_long8 = 0;
2566 if (datalength<=((tif->tif_flags&TIFF_BIGTIFF)?0x8U:0x4U))
2567 {
2568 if( data && datalength )
2569 {
2570 _TIFFmemcpy(&dir[m].tdir_offset,data,datalength);
2571 }
2572 }
2573 else
2574 {
2575 uint64 na,nb;
2576 na=tif->tif_dataoff;
2577 nb=na+datalength;
2578 if (!(tif->tif_flags&TIFF_BIGTIFF))
2579 nb=(uint32)nb;
2580 if ((nb<na)||(nb<datalength))
2581 {
2582 TIFFErrorExt(tif->tif_clientdata,module,"Maximum TIFF file size exceeded");
2583 return(0);
2584 }
2585 if (!SeekOK(tif,na))
2586 {
2587 TIFFErrorExt(tif->tif_clientdata,module,"IO error writing tag data");
2588 return(0);
2589 }
2590 assert(datalength<0x80000000UL);
2591 if (!WriteOK(tif,data,(tmsize_t)datalength))
2592 {
2593 TIFFErrorExt(tif->tif_clientdata,module,"IO error writing tag data");
2594 return(0);
2595 }
2596 tif->tif_dataoff=nb;
2597 if (tif->tif_dataoff&1)
2598 tif->tif_dataoff++;
2599 if (!(tif->tif_flags&TIFF_BIGTIFF))
2600 {
2601 uint32 o;
2602 o=(uint32)na;
2603 if (tif->tif_flags&TIFF_SWAB)
2604 TIFFSwabLong(&o);
2605 _TIFFmemcpy(&dir[m].tdir_offset,&o,4);
2606 }
2607 else
2608 {
2609 dir[m].tdir_offset.toff_long8 = na;
2610 if (tif->tif_flags&TIFF_SWAB)
2611 TIFFSwabLong8(&dir[m].tdir_offset.toff_long8);
2612 }
2613 }
2614 (*ndir)++;
2615 return(1);
2616}
2617
2618/*
2619 * Link the current directory into the directory chain for the file.
2620 */
2621static int
2623{
2624 static const char module[] = "TIFFLinkDirectory";
2625
2626 tif->tif_diroff = (TIFFSeekFile(tif,0,SEEK_END)+1) & (~((toff_t)1));
2627
2628 /*
2629 * Handle SubIFDs
2630 */
2631 if (tif->tif_flags & TIFF_INSUBIFD)
2632 {
2633 if (!(tif->tif_flags&TIFF_BIGTIFF))
2634 {
2635 uint32 m;
2636 m = (uint32)tif->tif_diroff;
2637 if (tif->tif_flags & TIFF_SWAB)
2638 TIFFSwabLong(&m);
2640 if (!WriteOK(tif, &m, 4)) {
2642 "Error writing SubIFD directory link");
2643 return (0);
2644 }
2645 /*
2646 * Advance to the next SubIFD or, if this is
2647 * the last one configured, revert back to the
2648 * normal directory linkage.
2649 */
2650 if (--tif->tif_nsubifd)
2651 tif->tif_subifdoff += 4;
2652 else
2653 tif->tif_flags &= ~TIFF_INSUBIFD;
2654 return (1);
2655 }
2656 else
2657 {
2658 uint64 m;
2659 m = tif->tif_diroff;
2660 if (tif->tif_flags & TIFF_SWAB)
2661 TIFFSwabLong8(&m);
2663 if (!WriteOK(tif, &m, 8)) {
2665 "Error writing SubIFD directory link");
2666 return (0);
2667 }
2668 /*
2669 * Advance to the next SubIFD or, if this is
2670 * the last one configured, revert back to the
2671 * normal directory linkage.
2672 */
2673 if (--tif->tif_nsubifd)
2674 tif->tif_subifdoff += 8;
2675 else
2676 tif->tif_flags &= ~TIFF_INSUBIFD;
2677 return (1);
2678 }
2679 }
2680
2681 if (!(tif->tif_flags&TIFF_BIGTIFF))
2682 {
2683 uint32 m;
2684 uint32 nextdir;
2685 m = (uint32)(tif->tif_diroff);
2686 if (tif->tif_flags & TIFF_SWAB)
2687 TIFFSwabLong(&m);
2688 if (tif->tif_header.classic.tiff_diroff == 0) {
2689 /*
2690 * First directory, overwrite offset in header.
2691 */
2693 (void) TIFFSeekFile(tif,4, SEEK_SET);
2694 if (!WriteOK(tif, &m, 4)) {
2696 "Error writing TIFF header");
2697 return (0);
2698 }
2699 return (1);
2700 }
2701 /*
2702 * Not the first directory, search to the last and append.
2703 */
2704 nextdir = tif->tif_header.classic.tiff_diroff;
2705 while(1) {
2706 uint16 dircount;
2707 uint32 nextnextdir;
2708
2709 if (!SeekOK(tif, nextdir) ||
2710 !ReadOK(tif, &dircount, 2)) {
2712 "Error fetching directory count");
2713 return (0);
2714 }
2715 if (tif->tif_flags & TIFF_SWAB)
2716 TIFFSwabShort(&dircount);
2717 (void) TIFFSeekFile(tif,
2718 nextdir+2+dircount*12, SEEK_SET);
2719 if (!ReadOK(tif, &nextnextdir, 4)) {
2721 "Error fetching directory link");
2722 return (0);
2723 }
2724 if (tif->tif_flags & TIFF_SWAB)
2725 TIFFSwabLong(&nextnextdir);
2726 if (nextnextdir==0)
2727 {
2728 (void) TIFFSeekFile(tif,
2729 nextdir+2+dircount*12, SEEK_SET);
2730 if (!WriteOK(tif, &m, 4)) {
2732 "Error writing directory link");
2733 return (0);
2734 }
2735 break;
2736 }
2737 nextdir=nextnextdir;
2738 }
2739 }
2740 else
2741 {
2742 uint64 m;
2743 uint64 nextdir;
2744 m = tif->tif_diroff;
2745 if (tif->tif_flags & TIFF_SWAB)
2746 TIFFSwabLong8(&m);
2747 if (tif->tif_header.big.tiff_diroff == 0) {
2748 /*
2749 * First directory, overwrite offset in header.
2750 */
2752 (void) TIFFSeekFile(tif,8, SEEK_SET);
2753 if (!WriteOK(tif, &m, 8)) {
2755 "Error writing TIFF header");
2756 return (0);
2757 }
2758 return (1);
2759 }
2760 /*
2761 * Not the first directory, search to the last and append.
2762 */
2763 nextdir = tif->tif_header.big.tiff_diroff;
2764 while(1) {
2765 uint64 dircount64;
2766 uint16 dircount;
2767 uint64 nextnextdir;
2768
2769 if (!SeekOK(tif, nextdir) ||
2770 !ReadOK(tif, &dircount64, 8)) {
2772 "Error fetching directory count");
2773 return (0);
2774 }
2775 if (tif->tif_flags & TIFF_SWAB)
2776 TIFFSwabLong8(&dircount64);
2777 if (dircount64>0xFFFF)
2778 {
2780 "Sanity check on tag count failed, likely corrupt TIFF");
2781 return (0);
2782 }
2783 dircount=(uint16)dircount64;
2784 (void) TIFFSeekFile(tif,
2785 nextdir+8+dircount*20, SEEK_SET);
2786 if (!ReadOK(tif, &nextnextdir, 8)) {
2788 "Error fetching directory link");
2789 return (0);
2790 }
2791 if (tif->tif_flags & TIFF_SWAB)
2792 TIFFSwabLong8(&nextnextdir);
2793 if (nextnextdir==0)
2794 {
2795 (void) TIFFSeekFile(tif,
2796 nextdir+8+dircount*20, SEEK_SET);
2797 if (!WriteOK(tif, &m, 8)) {
2799 "Error writing directory link");
2800 return (0);
2801 }
2802 break;
2803 }
2804 nextdir=nextnextdir;
2805 }
2806 }
2807 return (1);
2808}
2809
2810/************************************************************************/
2811/* TIFFRewriteField() */
2812/* */
2813/* Rewrite a field in the directory on disk without regard to */
2814/* updating the TIFF directory structure in memory. Currently */
2815/* only supported for field that already exist in the on-disk */
2816/* directory. Mainly used for updating stripoffset / */
2817/* stripbytecount values after the directory is already on */
2818/* disk. */
2819/* */
2820/* Returns zero on failure, and one on success. */
2821/************************************************************************/
2822
2823int
2825 tmsize_t count, void* data)
2826{
2827 static const char module[] = "TIFFResetField";
2828 /* const TIFFField* fip = NULL; */
2829 uint16 dircount;
2830 tmsize_t dirsize;
2831 uint8 direntry_raw[20];
2832 uint16 entry_tag = 0;
2833 uint16 entry_type = 0;
2834 uint64 entry_count = 0;
2835 uint64 entry_offset = 0;
2836 int value_in_entry = 0;
2837 uint64 read_offset;
2838 uint8 *buf_to_write = NULL;
2839 TIFFDataType datatype;
2840
2841/* -------------------------------------------------------------------- */
2842/* Find field definition. */
2843/* -------------------------------------------------------------------- */
2844 /*fip =*/ TIFFFindField(tif, tag, TIFF_ANY);
2845
2846/* -------------------------------------------------------------------- */
2847/* Do some checking this is a straight forward case. */
2848/* -------------------------------------------------------------------- */
2849 if( isMapped(tif) )
2850 {
2852 "Memory mapped files not currently supported for this operation." );
2853 return 0;
2854 }
2855
2856 if( tif->tif_diroff == 0 )
2857 {
2859 "Attempt to reset field on directory not already on disk." );
2860 return 0;
2861 }
2862
2863/* -------------------------------------------------------------------- */
2864/* Read the directory entry count. */
2865/* -------------------------------------------------------------------- */
2866 if (!SeekOK(tif, tif->tif_diroff)) {
2868 "%s: Seek error accessing TIFF directory",
2869 tif->tif_name);
2870 return 0;
2871 }
2872
2873 read_offset = tif->tif_diroff;
2874
2875 if (!(tif->tif_flags&TIFF_BIGTIFF))
2876 {
2877 if (!ReadOK(tif, &dircount, sizeof (uint16))) {
2879 "%s: Can not read TIFF directory count",
2880 tif->tif_name);
2881 return 0;
2882 }
2883 if (tif->tif_flags & TIFF_SWAB)
2884 TIFFSwabShort(&dircount);
2885 dirsize = 12;
2886 read_offset += 2;
2887 } else {
2888 uint64 dircount64;
2889 if (!ReadOK(tif, &dircount64, sizeof (uint64))) {
2891 "%s: Can not read TIFF directory count",
2892 tif->tif_name);
2893 return 0;
2894 }
2895 if (tif->tif_flags & TIFF_SWAB)
2896 TIFFSwabLong8(&dircount64);
2897 dircount = (uint16)dircount64;
2898 dirsize = 20;
2899 read_offset += 8;
2900 }
2901
2902/* -------------------------------------------------------------------- */
2903/* Read through directory to find target tag. */
2904/* -------------------------------------------------------------------- */
2905 while( dircount > 0 )
2906 {
2907 if (!ReadOK(tif, direntry_raw, dirsize)) {
2909 "%s: Can not read TIFF directory entry.",
2910 tif->tif_name);
2911 return 0;
2912 }
2913
2914 memcpy( &entry_tag, direntry_raw + 0, sizeof(uint16) );
2915 if (tif->tif_flags&TIFF_SWAB)
2916 TIFFSwabShort( &entry_tag );
2917
2918 if( entry_tag == tag )
2919 break;
2920
2921 read_offset += dirsize;
2922 }
2923
2924 if( entry_tag != tag )
2925 {
2927 "%s: Could not find tag %d.",
2928 tif->tif_name, tag );
2929 return 0;
2930 }
2931
2932/* -------------------------------------------------------------------- */
2933/* Extract the type, count and offset for this entry. */
2934/* -------------------------------------------------------------------- */
2935 memcpy( &entry_type, direntry_raw + 2, sizeof(uint16) );
2936 if (tif->tif_flags&TIFF_SWAB)
2937 TIFFSwabShort( &entry_type );
2938
2939 if (!(tif->tif_flags&TIFF_BIGTIFF))
2940 {
2941 uint32 value;
2942
2943 memcpy( &value, direntry_raw + 4, sizeof(uint32) );
2944 if (tif->tif_flags&TIFF_SWAB)
2945 TIFFSwabLong( &value );
2946 entry_count = value;
2947
2948 memcpy( &value, direntry_raw + 8, sizeof(uint32) );
2949 if (tif->tif_flags&TIFF_SWAB)
2950 TIFFSwabLong( &value );
2951 entry_offset = value;
2952 }
2953 else
2954 {
2955 memcpy( &entry_count, direntry_raw + 4, sizeof(uint64) );
2956 if (tif->tif_flags&TIFF_SWAB)
2957 TIFFSwabLong8( &entry_count );
2958
2959 memcpy( &entry_offset, direntry_raw + 12, sizeof(uint64) );
2960 if (tif->tif_flags&TIFF_SWAB)
2961 TIFFSwabLong8( &entry_offset );
2962 }
2963
2964/* -------------------------------------------------------------------- */
2965/* When a dummy tag was written due to TIFFDeferStrileArrayWriting() */
2966/* -------------------------------------------------------------------- */
2967 if( entry_offset == 0 && entry_count == 0 && entry_type == 0 )
2968 {
2970 {
2971 entry_type = (tif->tif_flags&TIFF_BIGTIFF) ? TIFF_LONG8 : TIFF_LONG;
2972 }
2973 else
2974 {
2975 int write_aslong8 = 1;
2976 if( count > 1 && tag == TIFFTAG_STRIPBYTECOUNTS )
2977 {
2978 write_aslong8 = WriteAsLong8(tif, TIFFStripSize64(tif));
2979 }
2980 else if( count > 1 && tag == TIFFTAG_TILEBYTECOUNTS )
2981 {
2982 write_aslong8 = WriteAsLong8(tif, TIFFTileSize64(tif));
2983 }
2984 if( write_aslong8 )
2985 {
2986 entry_type = TIFF_LONG8;
2987 }
2988 else
2989 {
2990 int write_aslong4 = 1;
2991 if( count > 1 && tag == TIFFTAG_STRIPBYTECOUNTS )
2992 {
2993 write_aslong4 = WriteAsLong4(tif, TIFFStripSize64(tif));
2994 }
2995 else if( count > 1 && tag == TIFFTAG_TILEBYTECOUNTS )
2996 {
2997 write_aslong4 = WriteAsLong4(tif, TIFFTileSize64(tif));
2998 }
2999 if( write_aslong4 )
3000 {
3001 entry_type = TIFF_LONG;
3002 }
3003 else
3004 {
3005 entry_type = TIFF_SHORT;
3006 }
3007 }
3008 }
3009 }
3010
3011/* -------------------------------------------------------------------- */
3012/* What data type do we want to write this as? */
3013/* -------------------------------------------------------------------- */
3014 if( TIFFDataWidth(in_datatype) == 8 && !(tif->tif_flags&TIFF_BIGTIFF) )
3015 {
3016 if( in_datatype == TIFF_LONG8 )
3017 datatype = entry_type == TIFF_SHORT ? TIFF_SHORT : TIFF_LONG;
3018 else if( in_datatype == TIFF_SLONG8 )
3019 datatype = TIFF_SLONG;
3020 else if( in_datatype == TIFF_IFD8 )
3021 datatype = TIFF_IFD;
3022 else
3023 datatype = in_datatype;
3024 }
3025 else
3026 {
3027 if( in_datatype == TIFF_LONG8 &&
3028 (entry_type == TIFF_SHORT || entry_type == TIFF_LONG ||
3029 entry_type == TIFF_LONG8 ) )
3030 datatype = entry_type;
3031 else if( in_datatype == TIFF_SLONG8 &&
3032 (entry_type == TIFF_SLONG || entry_type == TIFF_SLONG8 ) )
3033 datatype = entry_type;
3034 else if( in_datatype == TIFF_IFD8 &&
3035 (entry_type == TIFF_IFD || entry_type == TIFF_IFD8 ) )
3036 datatype = entry_type;
3037 else
3038 datatype = in_datatype;
3039 }
3040
3041/* -------------------------------------------------------------------- */
3042/* Prepare buffer of actual data to write. This includes */
3043/* swabbing as needed. */
3044/* -------------------------------------------------------------------- */
3045 buf_to_write =
3046 (uint8 *)_TIFFCheckMalloc(tif, count, TIFFDataWidth(datatype),
3047 "for field buffer.");
3048 if (!buf_to_write)
3049 return 0;
3050
3051 if( datatype == in_datatype )
3052 memcpy( buf_to_write, data, count * TIFFDataWidth(datatype) );
3053 else if( datatype == TIFF_SLONG && in_datatype == TIFF_SLONG8 )
3054 {
3055 tmsize_t i;
3056
3057 for( i = 0; i < count; i++ )
3058 {
3059 ((int32 *) buf_to_write)[i] =
3060 (int32) ((int64 *) data)[i];
3061 if( (int64) ((int32 *) buf_to_write)[i] != ((int64 *) data)[i] )
3062 {
3063 _TIFFfree( buf_to_write );
3065 "Value exceeds 32bit range of output type." );
3066 return 0;
3067 }
3068 }
3069 }
3070 else if( (datatype == TIFF_LONG && in_datatype == TIFF_LONG8)
3071 || (datatype == TIFF_IFD && in_datatype == TIFF_IFD8) )
3072 {
3073 tmsize_t i;
3074
3075 for( i = 0; i < count; i++ )
3076 {
3077 ((uint32 *) buf_to_write)[i] =
3078 (uint32) ((uint64 *) data)[i];
3079 if( (uint64) ((uint32 *) buf_to_write)[i] != ((uint64 *) data)[i] )
3080 {
3081 _TIFFfree( buf_to_write );
3083 "Value exceeds 32bit range of output type." );
3084 return 0;
3085 }
3086 }
3087 }
3088 else if( datatype == TIFF_SHORT && in_datatype == TIFF_LONG8 )
3089 {
3090 tmsize_t i;
3091
3092 for( i = 0; i < count; i++ )
3093 {
3094 ((uint16 *) buf_to_write)[i] =
3095 (uint16) ((uint64 *) data)[i];
3096 if( (uint64) ((uint16 *) buf_to_write)[i] != ((uint64 *) data)[i] )
3097 {
3098 _TIFFfree( buf_to_write );
3100 "Value exceeds 16bit range of output type." );
3101 return 0;
3102 }
3103 }
3104 }
3105 else
3106 {
3108 "Unhandled type conversion." );
3109 return 0;
3110 }
3111
3112 if( TIFFDataWidth(datatype) > 1 && (tif->tif_flags&TIFF_SWAB) )
3113 {
3114 if( TIFFDataWidth(datatype) == 2 )
3115 TIFFSwabArrayOfShort( (uint16 *) buf_to_write, count );
3116 else if( TIFFDataWidth(datatype) == 4 )
3117 TIFFSwabArrayOfLong( (uint32 *) buf_to_write, count );
3118 else if( TIFFDataWidth(datatype) == 8 )
3119 TIFFSwabArrayOfLong8( (uint64 *) buf_to_write, count );
3120 }
3121
3122/* -------------------------------------------------------------------- */
3123/* Is this a value that fits into the directory entry? */
3124/* -------------------------------------------------------------------- */
3125 if (!(tif->tif_flags&TIFF_BIGTIFF))
3126 {
3127 if( TIFFDataWidth(datatype) * count <= 4 )
3128 {
3129 entry_offset = read_offset + 8;
3130 value_in_entry = 1;
3131 }
3132 }
3133 else
3134 {
3135 if( TIFFDataWidth(datatype) * count <= 8 )
3136 {
3137 entry_offset = read_offset + 12;
3138 value_in_entry = 1;
3139 }
3140 }
3141
3146 {
3147 tif->tif_dir.td_stripoffset_entry.tdir_type = datatype;
3149 }
3154 {
3157 }
3158
3159/* -------------------------------------------------------------------- */
3160/* If the tag type, and count match, then we just write it out */
3161/* over the old values without altering the directory entry at */
3162/* all. */
3163/* -------------------------------------------------------------------- */
3164 if( entry_count == (uint64)count && entry_type == (uint16) datatype )
3165 {
3166 if (!SeekOK(tif, entry_offset)) {
3167 _TIFFfree( buf_to_write );
3169 "%s: Seek error accessing TIFF directory",
3170 tif->tif_name);
3171 return 0;
3172 }
3173 if (!WriteOK(tif, buf_to_write, count*TIFFDataWidth(datatype))) {
3174 _TIFFfree( buf_to_write );
3176 "Error writing directory link");
3177 return (0);
3178 }
3179
3180 _TIFFfree( buf_to_write );
3181 return 1;
3182 }
3183
3184/* -------------------------------------------------------------------- */
3185/* Otherwise, we write the new tag data at the end of the file. */
3186/* -------------------------------------------------------------------- */
3187 if( !value_in_entry )
3188 {
3189 entry_offset = TIFFSeekFile(tif,0,SEEK_END);
3190
3191 if (!WriteOK(tif, buf_to_write, count*TIFFDataWidth(datatype))) {
3192 _TIFFfree( buf_to_write );
3194 "Error writing directory link");
3195 return (0);
3196 }
3197 }
3198 else
3199 {
3200 memcpy( &entry_offset, buf_to_write, count*TIFFDataWidth(datatype));
3201 }
3202
3203 _TIFFfree( buf_to_write );
3204 buf_to_write = 0;
3205
3206/* -------------------------------------------------------------------- */
3207/* Adjust the directory entry. */
3208/* -------------------------------------------------------------------- */
3209 entry_type = datatype;
3210 entry_count = (uint64)count;
3211 memcpy( direntry_raw + 2, &entry_type, sizeof(uint16) );
3212 if (tif->tif_flags&TIFF_SWAB)
3213 TIFFSwabShort( (uint16 *) (direntry_raw + 2) );
3214
3215 if (!(tif->tif_flags&TIFF_BIGTIFF))
3216 {
3217 uint32 value;
3218
3219 value = (uint32) entry_count;
3220 memcpy( direntry_raw + 4, &value, sizeof(uint32) );
3221 if (tif->tif_flags&TIFF_SWAB)
3222 TIFFSwabLong( (uint32 *) (direntry_raw + 4) );
3223
3224 value = (uint32) entry_offset;
3225 memcpy( direntry_raw + 8, &value, sizeof(uint32) );
3226 if (tif->tif_flags&TIFF_SWAB)
3227 TIFFSwabLong( (uint32 *) (direntry_raw + 8) );
3228 }
3229 else
3230 {
3231 memcpy( direntry_raw + 4, &entry_count, sizeof(uint64) );
3232 if (tif->tif_flags&TIFF_SWAB)
3233 TIFFSwabLong8( (uint64 *) (direntry_raw + 4) );
3234
3235 memcpy( direntry_raw + 12, &entry_offset, sizeof(uint64) );
3236 if (tif->tif_flags&TIFF_SWAB)
3237 TIFFSwabLong8( (uint64 *) (direntry_raw + 12) );
3238 }
3239
3240/* -------------------------------------------------------------------- */
3241/* Write the directory entry out to disk. */
3242/* -------------------------------------------------------------------- */
3243 if (!SeekOK(tif, read_offset )) {
3245 "%s: Seek error accessing TIFF directory",
3246 tif->tif_name);
3247 return 0;
3248 }
3249
3250 if (!WriteOK(tif, direntry_raw,dirsize))
3251 {
3253 "%s: Can not write TIFF directory entry.",
3254 tif->tif_name);
3255 return 0;
3256 }
3257
3258 return 1;
3259}
3260/* vim: set ts=8 sts=8 sw=8 noet: */
3261/*
3262 * Local Variables:
3263 * mode: c
3264 * c-basic-offset: 8
3265 * fill-column: 78
3266 * End:
3267 */
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
#define O_RDONLY
Definition: acwin.h:108
unsigned int dir
Definition: maze.c:112
#define ok(value,...)
Definition: atltest.h:57
unsigned short uint16
Definition: types.h:30
unsigned int uint32
Definition: types.h:32
unsigned char uint8
Definition: types.h:28
_In_ fcb _In_ chunk _In_ uint64_t _In_ uint64_t _In_ bool _In_opt_ void _In_opt_ PIRP _In_ LIST_ENTRY _In_ uint8_t compression
Definition: btrfs_drv.h:1365
#define SEEK_END
Definition: cabinet.c:29
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define assert(x)
Definition: debug.h:53
switch(r->id)
Definition: btrfs.c:3046
unsigned long long uint64
Definition: platform.h:18
short int16
Definition: platform.h:11
long int32
Definition: platform.h:12
char int8
Definition: platform.h:10
long long int64
Definition: platform.h:13
GLuint GLuint GLsizei count
Definition: gl.h:1545
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl.h:1950
GLdouble GLdouble GLdouble GLdouble q
Definition: gl.h:2063
GLdouble n
Definition: glext.h:7729
GLuint GLfloat * val
Definition: glext.h:7180
GLfloat GLfloat p
Definition: glext.h:8902
const GLfloat * m
Definition: glext.h:10848
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
#define SEEK_SET
Definition: jmemansi.c:26
if(dx< 0)
Definition: linetemp.h:194
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
static int ** pa
Definition: server.c:126
uint64 toff_long8
Definition: tif_dir.h:59
union TIFFDirEntry::@3463 tdir_offset
uint64 tdir_count
Definition: tif_dir.h:55
uint16 tdir_tag
Definition: tif_dir.h:53
uint16 tdir_type
Definition: tif_dir.h:54
int td_customValueCount
Definition: tif_dir.h:121
float td_xposition
Definition: tif_dir.h:90
uint16 td_resolutionunit
Definition: tif_dir.h:88
uint64 * td_stripbytecount_p
Definition: tif_dir.h:102
uint16 td_sampleformat
Definition: tif_dir.h:76
uint16 td_samplesperpixel
Definition: tif_dir.h:82
uint32 td_imagedepth
Definition: tif_dir.h:72
float td_xresolution
Definition: tif_dir.h:87
int td_inknameslen
Definition: tif_dir.h:118
uint16 td_pagenumber[2]
Definition: tif_dir.h:91
float * td_refblackwhite
Definition: tif_dir.h:116
uint16 td_bitspersample
Definition: tif_dir.h:75
uint32 td_imagewidth
Definition: tif_dir.h:72
uint16 td_maxsamplevalue
Definition: tif_dir.h:84
uint32 td_rowsperstrip
Definition: tif_dir.h:83
uint32 td_tiledepth
Definition: tif_dir.h:73
float td_yresolution
Definition: tif_dir.h:87
uint16 td_photometric
Definition: tif_dir.h:78
uint16 td_fillorder
Definition: tif_dir.h:80
uint32 td_subfiletype
Definition: tif_dir.h:74
double * td_sminsamplevalue
Definition: tif_dir.h:85
uint16 td_minsamplevalue
Definition: tif_dir.h:84
uint16 td_threshholding
Definition: tif_dir.h:79
char * td_inknames
Definition: tif_dir.h:119
TIFFDirEntry td_stripoffset_entry
Definition: tif_dir.h:107
uint16 td_planarconfig
Definition: tif_dir.h:89
uint16 td_nsubifd
Definition: tif_dir.h:109
uint32 td_tilelength
Definition: tif_dir.h:73
uint32 td_nstrips
Definition: tif_dir.h:100
uint16 * td_transferfunction[3]
Definition: tif_dir.h:115
uint16 td_ycbcrpositioning
Definition: tif_dir.h:113
TIFFTagValue * td_customValues
Definition: tif_dir.h:122
double * td_smaxsamplevalue
Definition: tif_dir.h:86
uint16 td_compression
Definition: tif_dir.h:77
unsigned char td_deferstrilearraywriting
Definition: tif_dir.h:124
uint32 td_tilewidth
Definition: tif_dir.h:73
uint32 td_imagelength
Definition: tif_dir.h:72
uint16 td_extrasamples
Definition: tif_dir.h:94
uint64 * td_stripoffset_p
Definition: tif_dir.h:101
uint16 td_halftonehints[2]
Definition: tif_dir.h:93
TIFFDirEntry td_stripbytecount_entry
Definition: tif_dir.h:108
uint16 td_orientation
Definition: tif_dir.h:81
uint16 * td_colormap[3]
Definition: tif_dir.h:92
uint16 td_ycbcrsubsampling[2]
Definition: tif_dir.h:112
uint64 * td_subifd
Definition: tif_dir.h:110
float td_yposition
Definition: tif_dir.h:90
uint64 tiff_diroff
Definition: tiff.h:105
uint32 tiff_diroff
Definition: tiff.h:98
void * value
Definition: tif_dir.h:38
int count
Definition: tif_dir.h:37
const TIFFField * info
Definition: tif_dir.h:36
uint32 field_tag
Definition: tif_dir.h:283
TIFFDataType field_type
Definition: tif_dir.h:286
unsigned char field_passcount
Definition: tif_dir.h:292
char * field_name
Definition: tif_dir.h:293
TIFFSetGetFieldType get_field_type
Definition: tif_dir.h:289
unsigned short field_bit
Definition: tif_dir.h:290
short field_readcount
Definition: tif_dir.h:284
Definition: ecma_167.h:138
Definition: tiffiop.h:115
uint64 tif_subifdoff
Definition: tiffiop.h:166
TIFFHeaderClassic classic
Definition: tiffiop.h:155
TIFFHeaderBig big
Definition: tiffiop.h:156
tmsize_t tif_rawdataloaded
Definition: tiffiop.h:198
tmsize_t tif_rawcc
Definition: tiffiop.h:200
uint64 tif_dataoff
Definition: tiffiop.h:163
size_t tif_nfields
Definition: tiffiop.h:217
uint16 tif_nsubifd
Definition: tiffiop.h:165
TIFFField ** tif_fields
Definition: tiffiop.h:216
thandle_t tif_clientdata
Definition: tiffiop.h:207
char * tif_name
Definition: tiffiop.h:116
uint16 tif_curdir
Definition: tiffiop.h:160
uint32 tif_flags
Definition: tiffiop.h:119
TIFFDirectory tif_dir
Definition: tiffiop.h:151
TIFFBoolMethod tif_postencode
Definition: tiffiop.h:179
union tiff::@3465 tif_header
uint64 tif_nextdiroff
Definition: tiffiop.h:147
TIFFVoidMethod tif_close
Definition: tiffiop.h:186
TIFFVoidMethod tif_cleanup
Definition: tiffiop.h:188
uint64 tif_diroff
Definition: tiffiop.h:146
tmsize_t tif_rawdatasize
Definition: tiffiop.h:196
tmsize_t tif_rawdataoff
Definition: tiffiop.h:197
int tif_mode
Definition: tiffiop.h:118
uint8 * tif_rawdata
Definition: tiffiop.h:195
int TIFFGetFieldDefaulted(TIFF *tif, uint32 tag,...)
Definition: tif_aux.c:345
void * _TIFFCheckMalloc(TIFF *tif, tmsize_t nmemb, tmsize_t elem_size, const char *what)
Definition: tif_aux.c:121
float _TIFFClampDoubleToFloat(double val)
Definition: tif_aux.c:397
void TIFFFreeDirectory(TIFF *tif)
Definition: tif_dir.c:1268
int TIFFCreateDirectory(TIFF *tif)
Definition: tif_dir.c:1327
int TIFFGetField(TIFF *tif, uint32 tag,...)
Definition: tif_dir.c:1232
#define FIELD_RESOLUTION
Definition: tif_dir.h:143
#define FIELD_CODEC
Definition: tif_dir.h:179
#define FIELD_SUBFILETYPE
Definition: tif_dir.h:147
#define FIELD_STRIPOFFSETS
Definition: tif_dir.h:162
#define FIELD_TRANSFERFUNCTION
Definition: tif_dir.h:174
#define FIELD_HALFTONEHINTS
Definition: tif_dir.h:170
#define FIELD_ORIENTATION
Definition: tif_dir.h:153
#define FIELD_MINSAMPLEVALUE
Definition: tif_dir.h:156
#define FIELD_FILLORDER
Definition: tif_dir.h:152
#define FIELD_SMAXSAMPLEVALUE
Definition: tif_dir.h:167
#define FIELD_IMAGEDIMENSIONS
Definition: tif_dir.h:141
#define FIELD_YCBCRSUBSAMPLING
Definition: tif_dir.h:171
#define FIELD_COLORMAP
Definition: tif_dir.h:163
#define FIELD_SUBIFD
Definition: tif_dir.h:176
#define FIELD_EXTRASAMPLES
Definition: tif_dir.h:164
#define FIELD_IMAGEDEPTH
Definition: tif_dir.h:168
#define FIELD_SMINSAMPLEVALUE
Definition: tif_dir.h:166
#define TIFFFieldSet(tif, field)
Definition: tif_dir.h:196
#define FIELD_MAXSAMPLEVALUE
Definition: tif_dir.h:157
#define FIELD_TILEDEPTH
Definition: tif_dir.h:169
#define FIELD_TILEDIMENSIONS
Definition: tif_dir.h:142
#define FIELD_POSITION
Definition: tif_dir.h:144
#define FIELD_ROWSPERSTRIP
Definition: tif_dir.h:155
#define FIELD_COMPRESSION
Definition: tif_dir.h:149
#define FIELD_STRIPBYTECOUNTS
Definition: tif_dir.h:161
#define FIELD_YCBCRPOSITIONING
Definition: tif_dir.h:172
#define FIELD_REFBLACKWHITE
Definition: tif_dir.h:173
#define FIELD_SAMPLEFORMAT
Definition: tif_dir.h:165
#define FIELD_PAGENUMBER
Definition: tif_dir.h:160
#define FIELD_BITSPERSAMPLE
Definition: tif_dir.h:148
@ TIFF_SETGET_C32_UINT8
Definition: tif_dir.h:244
@ TIFF_SETGET_UINT16
Definition: tif_dir.h:208
@ TIFF_SETGET_ASCII
Definition: tif_dir.h:205
@ TIFF_SETGET_UINT32
Definition: tif_dir.h:210
#define FIELD_INKNAMES
Definition: tif_dir.h:175
#define FIELD_RESOLUTIONUNIT
Definition: tif_dir.h:159
#define FIELD_PLANARCONFIG
Definition: tif_dir.h:158
#define FIELD_SAMPLESPERPIXEL
Definition: tif_dir.h:154
#define FIELD_PHOTOMETRIC
Definition: tif_dir.h:150
#define FIELD_THRESHHOLDING
Definition: tif_dir.h:151
int TIFFDataWidth(TIFFDataType type)
Definition: tif_dirinfo.c:438
const TIFFField * TIFFFindField(TIFF *tif, uint32 tag, TIFFDataType dt)
Definition: tif_dirinfo.c:506
uint32 TIFFFieldTag(const TIFFField *fip)
Definition: tif_dirinfo.c:582
int _TIFFFillStriles(TIFF *tif)
Definition: tif_dirread.c:6320
static uint32 TIFFClampDoubleToUInt32(double val)
static int WriteAsLong4(TIFF *tif, uint64 strile_size)
int TIFFCheckpointDirectory(TIFF *tif)
Definition: tif_dirwrite.c:237
static int TIFFWriteDirectoryTagSshortArray(TIFF *tif, uint32 *ndir, TIFFDirEntry *dir, uint16 tag, uint32 count, int16 *value)
static int TIFFLinkDirectory(TIFF *)
static int32 TIFFClampDoubleToInt32(double val)
static int TIFFWriteDirectoryTagLong(TIFF *tif, uint32 *ndir, TIFFDirEntry *dir, uint16 tag, uint32 value)
static int _WriteAsType(TIFF *tif, uint64 strile_size, uint64 uncompressed_threshold)
static int TIFFWriteDirectoryTagIfdIfd8Array(TIFF *tif, uint32 *ndir, TIFFDirEntry *dir, uint16 tag, uint32 count, uint64 *value)
static int TIFFWriteDirectoryTagSampleformatArray(TIFF *tif, uint32 *ndir, TIFFDirEntry *dir, uint16 tag, uint32 count, double *value)
static int TIFFWriteDirectoryTagShortArray(TIFF *tif, uint32 *ndir, TIFFDirEntry *dir, uint16 tag, uint32 count, uint16 *value)
static int TIFFWriteDirectoryTagCheckedLongArray(TIFF *tif, uint32 *ndir, TIFFDirEntry *dir, uint16 tag, uint32 count, uint32 *value)
static int TIFFWriteDirectoryTagCheckedSbyteArray(TIFF *tif, uint32 *ndir, TIFFDirEntry *dir, uint16 tag, uint32 count, int8 *value)
int _TIFFRewriteField(TIFF *tif, uint16 tag, TIFFDataType in_datatype, tmsize_t count, void *data)
static int WriteAsLong8(TIFF *tif, uint64 strile_size)
static int TIFFWriteDirectorySec(TIFF *tif, int isimage, int imagedone, uint64 *pdiroff)
Definition: tif_dirwrite.c:407
static int TIFFWriteDirectoryTagLong8Array(TIFF *tif, uint32 *ndir, TIFFDirEntry *dir, uint16 tag, uint32 count, uint64 *value)
static int TIFFWriteDirectoryTagCheckedLong8Array(TIFF *tif, uint32 *ndir, TIFFDirEntry *dir, uint16 tag, uint32 count, uint64 *value)
static int TIFFWriteDirectoryTagCheckedShortArray(TIFF *tif, uint32 *ndir, TIFFDirEntry *dir, uint16 tag, uint32 count, uint16 *value)
static int TIFFWriteDirectoryTagSrationalArray(TIFF *tif, uint32 *ndir, TIFFDirEntry *dir, uint16 tag, uint32 count, float *value)
void TIFFCvtNativeToIEEEDouble(TIFF *tif, uint32 n, double *dp)
static int TIFFWriteDirectoryTagCheckedLong(TIFF *tif, uint32 *ndir, TIFFDirEntry *dir, uint16 tag, uint32 value)
static int TIFFWriteDirectoryTagCheckedSlong8Array(TIFF *tif, uint32 *ndir, TIFFDirEntry *dir, uint16 tag, uint32 count, int64 *value)
static int TIFFWriteDirectoryTagFloatArray(TIFF *tif, uint32 *ndir, TIFFDirEntry *dir, uint16 tag, uint32 count, float *value)
static int TIFFWriteDirectoryTagCheckedRationalArray(TIFF *tif, uint32 *ndir, TIFFDirEntry *dir, uint16 tag, uint32 count, float *value)
static int TIFFWriteDirectoryTagRational(TIFF *tif, uint32 *ndir, TIFFDirEntry *dir, uint16 tag, double value)
static int TIFFWriteDirectoryTagAscii(TIFF *tif, uint32 *ndir, TIFFDirEntry *dir, uint16 tag, uint32 count, char *value)
void TIFFCvtNativeToIEEEFloat(TIFF *tif, uint32 n, float *fp)
static uint8 TIFFClampDoubleToUInt8(double val)
int TIFFWriteCustomDirectory(TIFF *tif, uint64 *pdiroff)
Definition: tif_dirwrite.c:249
static int TIFFWriteDirectoryTagCheckedIfdArray(TIFF *tif, uint32 *ndir, TIFFDirEntry *dir, uint16 tag, uint32 count, uint32 *value)
static int TIFFWriteDirectoryTagCheckedSrationalArray(TIFF *tif, uint32 *ndir, TIFFDirEntry *dir, uint16 tag, uint32 count, float *value)
static int TIFFWriteDirectoryTagSlongArray(TIFF *tif, uint32 *ndir, TIFFDirEntry *dir, uint16 tag, uint32 count, int32 *value)
static int8 TIFFClampDoubleToInt8(double val)
Definition: tif_dirwrite.c:994
int TIFFRewriteDirectory(TIFF *tif)
Definition: tif_dirwrite.c:261
static int TIFFWriteDirectoryTagData(TIFF *tif, uint32 *ndir, TIFFDirEntry *dir, uint16 tag, uint16 datatype, uint32 count, uint32 datalength, void *data)
static int TIFFWriteDirectoryTagIfdArray(TIFF *tif, uint32 *ndir, TIFFDirEntry *dir, uint16 tag, uint32 count, uint32 *value)
static uint16 TIFFClampDoubleToUInt16(double val)
static int TIFFWriteDirectoryTagTransferfunction(TIFF *tif, uint32 *ndir, TIFFDirEntry *dir)
int TIFFDeferStrileArrayWriting(TIFF *tif)
Definition: tif_dirwrite.c:210
static int TIFFWriteDirectoryTagShortLong(TIFF *tif, uint32 *ndir, TIFFDirEntry *dir, uint16 tag, uint32 value)
static int TIFFWriteDirectoryTagCheckedRational(TIFF *tif, uint32 *ndir, TIFFDirEntry *dir, uint16 tag, double value)
static int TIFFWriteDirectoryTagCheckedByteArray(TIFF *tif, uint32 *ndir, TIFFDirEntry *dir, uint16 tag, uint32 count, uint8 *value)
static int TIFFWriteDirectoryTagSbyteArray(TIFF *tif, uint32 *ndir, TIFFDirEntry *dir, uint16 tag, uint32 count, int8 *value)
static int TIFFWriteDirectoryTagCheckedFloatArray(TIFF *tif, uint32 *ndir, TIFFDirEntry *dir, uint16 tag, uint32 count, float *value)
static int TIFFWriteDirectoryTagRationalArray(TIFF *tif, uint32 *ndir, TIFFDirEntry *dir, uint16 tag, uint32 count, float *value)
static int16 TIFFClampDoubleToInt16(double val)
static int TIFFWriteDirectoryTagSubifd(TIFF *tif, uint32 *ndir, TIFFDirEntry *dir)
static int TIFFWriteDirectoryTagCheckedIfd8Array(TIFF *tif, uint32 *ndir, TIFFDirEntry *dir, uint16 tag, uint32 count, uint64 *value)
static int TIFFWriteDirectoryTagCheckedSshortArray(TIFF *tif, uint32 *ndir, TIFFDirEntry *dir, uint16 tag, uint32 count, int16 *value)
static int TIFFWriteDirectoryTagCheckedDoubleArray(TIFF *tif, uint32 *ndir, TIFFDirEntry *dir, uint16 tag, uint32 count, double *value)
int TIFFWriteDirectory(TIFF *tif)
Definition: tif_dirwrite.c:180
static int TIFFWriteDirectoryTagLongArray(TIFF *tif, uint32 *ndir, TIFFDirEntry *dir, uint16 tag, uint32 count, uint32 *value)
static int TIFFWriteDirectoryTagCheckedUndefinedArray(TIFF *tif, uint32 *ndir, TIFFDirEntry *dir, uint16 tag, uint32 count, uint8 *value)
static int TIFFWriteDirectoryTagByteArray(TIFF *tif, uint32 *ndir, TIFFDirEntry *dir, uint16 tag, uint32 count, uint8 *value)
static int TIFFWriteDirectoryTagSlong8Array(TIFF *tif, uint32 *ndir, TIFFDirEntry *dir, uint16 tag, uint32 count, int64 *value)
static int TIFFWriteDirectoryTagColormap(TIFF *tif, uint32 *ndir, TIFFDirEntry *dir)
static int TIFFWriteDirectoryTagDoubleArray(TIFF *tif, uint32 *ndir, TIFFDirEntry *dir, uint16 tag, uint32 count, double *value)
static int TIFFWriteDirectoryTagUndefinedArray(TIFF *tif, uint32 *ndir, TIFFDirEntry *dir, uint16 tag, uint32 count, uint8 *value)