• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  * Core Directory Tag Support.
29  */
30 #include "tiffiop.h"
31 #include <stdlib.h>
32 
33 /*
34  * NOTE: THIS ARRAY IS ASSUMED TO BE SORTED BY TAG.
35  *
36  * NOTE: The second field (field_readcount) and third field (field_writecount)
37  *       sometimes use the values TIFF_VARIABLE (-1), TIFF_VARIABLE2 (-3)
38  *       and TIFF_SPP (-2). The macros should be used but would throw off
39  *       the formatting of the code, so please interpret the -1, -2 and -3
40  *       values accordingly.
41  */
42 
43 /* const object should be initialized */
44 #ifdef _MSC_VER
45 #pragma warning( push )
46 #pragma warning( disable : 4132 )
47 #endif
48 static const TIFFFieldArray tiffFieldArray;
49 static const TIFFFieldArray exifFieldArray;
50 #ifdef _MSC_VER
51 #pragma warning( pop )
52 #endif
53 
54 static const TIFFField
55 tiffFields[] = {
56 	{ TIFFTAG_SUBFILETYPE, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, TIFF_SETGET_UNDEFINED, FIELD_SUBFILETYPE, 1, 0, "SubfileType", NULL },
57 	{ TIFFTAG_OSUBFILETYPE, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UNDEFINED, TIFF_SETGET_UNDEFINED, FIELD_SUBFILETYPE, 1, 0, "OldSubfileType", NULL },
58 	{ TIFFTAG_IMAGEWIDTH, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, TIFF_SETGET_UNDEFINED, FIELD_IMAGEDIMENSIONS, 0, 0, "ImageWidth", NULL },
59 	{ TIFFTAG_IMAGELENGTH, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, TIFF_SETGET_UNDEFINED, FIELD_IMAGEDIMENSIONS, 1, 0, "ImageLength", NULL },
60 	{ TIFFTAG_BITSPERSAMPLE, -1, -1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_BITSPERSAMPLE, 0, 0, "BitsPerSample", NULL },
61 	{ TIFFTAG_COMPRESSION, -1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_COMPRESSION, 0, 0, "Compression", NULL },
62 	{ TIFFTAG_PHOTOMETRIC, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_PHOTOMETRIC, 0, 0, "PhotometricInterpretation", NULL },
63 	{ TIFFTAG_THRESHHOLDING, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_THRESHHOLDING, 1, 0, "Threshholding", NULL },
64 	{ TIFFTAG_CELLWIDTH, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UNDEFINED, TIFF_SETGET_UNDEFINED, FIELD_IGNORE, 1, 0, "CellWidth", NULL },
65 	{ TIFFTAG_CELLLENGTH, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UNDEFINED, TIFF_SETGET_UNDEFINED, FIELD_IGNORE, 1, 0, "CellLength", NULL },
66 	{ TIFFTAG_FILLORDER, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_FILLORDER, 0, 0, "FillOrder", NULL },
67 	{ TIFFTAG_DOCUMENTNAME, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "DocumentName", NULL },
68 	{ TIFFTAG_IMAGEDESCRIPTION, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "ImageDescription", NULL },
69 	{ TIFFTAG_MAKE, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "Make", NULL },
70 	{ TIFFTAG_MODEL, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "Model", NULL },
71 	{ TIFFTAG_STRIPOFFSETS, -1, -1, TIFF_LONG8, 0, TIFF_SETGET_UNDEFINED, TIFF_SETGET_UNDEFINED, FIELD_STRIPOFFSETS, 0, 0, "StripOffsets", NULL },
72 	{ TIFFTAG_ORIENTATION, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_ORIENTATION, 0, 0, "Orientation", NULL },
73 	{ TIFFTAG_SAMPLESPERPIXEL, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_SAMPLESPERPIXEL, 0, 0, "SamplesPerPixel", NULL },
74 	{ TIFFTAG_ROWSPERSTRIP, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, TIFF_SETGET_UNDEFINED, FIELD_ROWSPERSTRIP, 0, 0, "RowsPerStrip", NULL },
75 	{ TIFFTAG_STRIPBYTECOUNTS, -1, -1, TIFF_LONG8, 0, TIFF_SETGET_UNDEFINED, TIFF_SETGET_UNDEFINED, FIELD_STRIPBYTECOUNTS, 0, 0, "StripByteCounts", NULL },
76 	{ TIFFTAG_MINSAMPLEVALUE, -2, -1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_MINSAMPLEVALUE, 1, 0, "MinSampleValue", NULL },
77 	{ TIFFTAG_MAXSAMPLEVALUE, -2, -1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_MAXSAMPLEVALUE, 1, 0, "MaxSampleValue", NULL },
78 	{ TIFFTAG_XRESOLUTION, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_DOUBLE, TIFF_SETGET_UNDEFINED, FIELD_RESOLUTION, 1, 0, "XResolution", NULL },
79 	{ TIFFTAG_YRESOLUTION, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_DOUBLE, TIFF_SETGET_UNDEFINED, FIELD_RESOLUTION, 1, 0, "YResolution", NULL },
80 	{ TIFFTAG_PLANARCONFIG, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_PLANARCONFIG, 0, 0, "PlanarConfiguration", NULL },
81 	{ TIFFTAG_PAGENAME, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "PageName", NULL },
82 	{ TIFFTAG_XPOSITION, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_DOUBLE, TIFF_SETGET_UNDEFINED, FIELD_POSITION, 1, 0, "XPosition", NULL },
83 	{ TIFFTAG_YPOSITION, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_DOUBLE, TIFF_SETGET_UNDEFINED, FIELD_POSITION, 1, 0, "YPosition", NULL },
84 	{ TIFFTAG_FREEOFFSETS, -1, -1, TIFF_LONG8, 0, TIFF_SETGET_UNDEFINED, TIFF_SETGET_UNDEFINED, FIELD_IGNORE, 0, 0, "FreeOffsets", NULL },
85 	{ TIFFTAG_FREEBYTECOUNTS, -1, -1, TIFF_LONG8, 0, TIFF_SETGET_UNDEFINED, TIFF_SETGET_UNDEFINED, FIELD_IGNORE, 0, 0, "FreeByteCounts", NULL },
86 	{ TIFFTAG_GRAYRESPONSEUNIT, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UNDEFINED, TIFF_SETGET_UNDEFINED, FIELD_IGNORE, 1, 0, "GrayResponseUnit", NULL },
87 	{ TIFFTAG_GRAYRESPONSECURVE, -1, -1, TIFF_SHORT, 0, TIFF_SETGET_UNDEFINED, TIFF_SETGET_UNDEFINED, FIELD_IGNORE, 1, 0, "GrayResponseCurve", NULL },
88 	{ TIFFTAG_RESOLUTIONUNIT, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_RESOLUTIONUNIT, 1, 0, "ResolutionUnit", NULL },
89 	{ TIFFTAG_PAGENUMBER, 2, 2, TIFF_SHORT, 0, TIFF_SETGET_UINT16_PAIR, TIFF_SETGET_UNDEFINED, FIELD_PAGENUMBER, 1, 0, "PageNumber", NULL },
90 	{ TIFFTAG_COLORRESPONSEUNIT, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UNDEFINED, TIFF_SETGET_UNDEFINED, FIELD_IGNORE, 1, 0, "ColorResponseUnit", NULL },
91 	{ TIFFTAG_TRANSFERFUNCTION, -1, -1, TIFF_SHORT, 0, TIFF_SETGET_OTHER, TIFF_SETGET_UNDEFINED, FIELD_TRANSFERFUNCTION, 1, 0, "TransferFunction", NULL },
92 	{ TIFFTAG_SOFTWARE, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "Software", NULL },
93 	{ TIFFTAG_DATETIME, 20, 20, TIFF_ASCII, 0, TIFF_SETGET_ASCII, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "DateTime", NULL },
94 	{ TIFFTAG_ARTIST, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "Artist", NULL },
95 	{ TIFFTAG_HOSTCOMPUTER, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "HostComputer", NULL },
96 	{ TIFFTAG_WHITEPOINT, 2, 2, TIFF_RATIONAL, 0, TIFF_SETGET_C0_FLOAT, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "WhitePoint", NULL },
97 	{ TIFFTAG_PRIMARYCHROMATICITIES, 6, 6, TIFF_RATIONAL, 0, TIFF_SETGET_C0_FLOAT, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "PrimaryChromaticities", NULL },
98 	{ TIFFTAG_COLORMAP, -1, -1, TIFF_SHORT, 0, TIFF_SETGET_OTHER, TIFF_SETGET_UNDEFINED, FIELD_COLORMAP, 1, 0, "ColorMap", NULL },
99 	{ TIFFTAG_HALFTONEHINTS, 2, 2, TIFF_SHORT, 0, TIFF_SETGET_UINT16_PAIR, TIFF_SETGET_UNDEFINED, FIELD_HALFTONEHINTS, 1, 0, "HalftoneHints", NULL },
100 	{ TIFFTAG_TILEWIDTH, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, TIFF_SETGET_UNDEFINED, FIELD_TILEDIMENSIONS, 0, 0, "TileWidth", NULL },
101 	{ TIFFTAG_TILELENGTH, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, TIFF_SETGET_UNDEFINED, FIELD_TILEDIMENSIONS, 0, 0, "TileLength", NULL },
102 	{ TIFFTAG_TILEOFFSETS, -1, 1, TIFF_LONG8, 0, TIFF_SETGET_UNDEFINED, TIFF_SETGET_UNDEFINED, FIELD_STRIPOFFSETS, 0, 0, "TileOffsets", NULL },
103 	{ TIFFTAG_TILEBYTECOUNTS, -1, 1, TIFF_LONG8, 0, TIFF_SETGET_UNDEFINED, TIFF_SETGET_UNDEFINED, FIELD_STRIPBYTECOUNTS, 0, 0, "TileByteCounts", NULL },
104 	{ TIFFTAG_SUBIFD, -1, -1, TIFF_IFD8, 0, TIFF_SETGET_C16_IFD8, TIFF_SETGET_UNDEFINED, FIELD_SUBIFD, 1, 1, "SubIFD", (TIFFFieldArray*) &tiffFieldArray },
105 	{ TIFFTAG_INKSET, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "InkSet", NULL },
106 	{ TIFFTAG_INKNAMES, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_C16_ASCII, TIFF_SETGET_UNDEFINED, FIELD_INKNAMES, 1, 1, "InkNames", NULL },
107 	{ TIFFTAG_NUMBEROFINKS, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "NumberOfInks", NULL },
108 	{ TIFFTAG_DOTRANGE, 2, 2, TIFF_SHORT, 0, TIFF_SETGET_UINT16_PAIR, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "DotRange", NULL },
109 	{ TIFFTAG_TARGETPRINTER, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "TargetPrinter", NULL },
110 	{ TIFFTAG_EXTRASAMPLES, -1, -1, TIFF_SHORT, 0, TIFF_SETGET_C16_UINT16, TIFF_SETGET_UNDEFINED, FIELD_EXTRASAMPLES, 0, 1, "ExtraSamples", NULL },
111 	{ TIFFTAG_SAMPLEFORMAT, -1, -1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_SAMPLEFORMAT, 0, 0, "SampleFormat", NULL },
112 	{ TIFFTAG_SMINSAMPLEVALUE, -2, -1, TIFF_ANY, 0, TIFF_SETGET_DOUBLE, TIFF_SETGET_UNDEFINED, FIELD_SMINSAMPLEVALUE, 1, 0, "SMinSampleValue", NULL },
113 	{ TIFFTAG_SMAXSAMPLEVALUE, -2, -1, TIFF_ANY, 0, TIFF_SETGET_DOUBLE, TIFF_SETGET_UNDEFINED, FIELD_SMAXSAMPLEVALUE, 1, 0, "SMaxSampleValue", NULL },
114 	{ TIFFTAG_CLIPPATH, -1, -3, TIFF_BYTE, 0, TIFF_SETGET_UNDEFINED, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 1, "ClipPath", NULL },
115 	{ TIFFTAG_XCLIPPATHUNITS, 1, 1, TIFF_SLONG, 0, TIFF_SETGET_UNDEFINED, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "XClipPathUnits", NULL },
116 	{ TIFFTAG_XCLIPPATHUNITS, 1, 1, TIFF_SBYTE, 0, TIFF_SETGET_UNDEFINED, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "XClipPathUnits", NULL },
117 	{ TIFFTAG_YCLIPPATHUNITS, 1, 1, TIFF_SLONG, 0, TIFF_SETGET_UNDEFINED, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "YClipPathUnits", NULL },
118 	{ TIFFTAG_YCBCRCOEFFICIENTS, 3, 3, TIFF_RATIONAL, 0, TIFF_SETGET_C0_FLOAT, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "YCbCrCoefficients", NULL },
119 	{ TIFFTAG_YCBCRSUBSAMPLING, 2, 2, TIFF_SHORT, 0, TIFF_SETGET_UINT16_PAIR, TIFF_SETGET_UNDEFINED, FIELD_YCBCRSUBSAMPLING, 0, 0, "YCbCrSubsampling", NULL },
120 	{ TIFFTAG_YCBCRPOSITIONING, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_YCBCRPOSITIONING, 0, 0, "YCbCrPositioning", NULL },
121 	{ TIFFTAG_REFERENCEBLACKWHITE, 6, 6, TIFF_RATIONAL, 0, TIFF_SETGET_C0_FLOAT, TIFF_SETGET_UNDEFINED, FIELD_REFBLACKWHITE, 1, 0, "ReferenceBlackWhite", NULL },
122 	{ TIFFTAG_XMLPACKET, -3, -3, TIFF_BYTE, 0, TIFF_SETGET_C32_UINT8, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 1, "XMLPacket", NULL },
123 	/* begin SGI tags */
124 	{ TIFFTAG_MATTEING, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_EXTRASAMPLES, 0, 0, "Matteing", NULL },
125 	{ TIFFTAG_DATATYPE, -2, -1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_SAMPLEFORMAT, 0, 0, "DataType", NULL },
126 	{ TIFFTAG_IMAGEDEPTH, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, TIFF_SETGET_UNDEFINED, FIELD_IMAGEDEPTH, 0, 0, "ImageDepth", NULL },
127 	{ TIFFTAG_TILEDEPTH, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, TIFF_SETGET_UNDEFINED, FIELD_TILEDEPTH, 0, 0, "TileDepth", NULL },
128 	/* end SGI tags */
129 	/* begin Pixar tags */
130 	{ TIFFTAG_PIXAR_IMAGEFULLWIDTH, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "ImageFullWidth", NULL },
131 	{ TIFFTAG_PIXAR_IMAGEFULLLENGTH, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "ImageFullLength", NULL },
132 	{ TIFFTAG_PIXAR_TEXTUREFORMAT, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "TextureFormat", NULL },
133 	{ TIFFTAG_PIXAR_WRAPMODES, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "TextureWrapModes", NULL },
134 	{ TIFFTAG_PIXAR_FOVCOT, 1, 1, TIFF_FLOAT, 0, TIFF_SETGET_FLOAT, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "FieldOfViewCotangent", NULL },
135 	{ TIFFTAG_PIXAR_MATRIX_WORLDTOSCREEN, 16, 16, TIFF_FLOAT, 0, TIFF_SETGET_C0_FLOAT, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "MatrixWorldToScreen", NULL },
136 	{ TIFFTAG_PIXAR_MATRIX_WORLDTOCAMERA, 16, 16, TIFF_FLOAT, 0, TIFF_SETGET_C0_FLOAT, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "MatrixWorldToCamera", NULL },
137 	{ TIFFTAG_CFAREPEATPATTERNDIM, 2, 2, TIFF_SHORT, 0, TIFF_SETGET_C0_UINT16, TIFF_SETGET_UNDEFINED,	FIELD_CUSTOM, 0,	0,	"CFARepeatPatternDim", NULL },
138 	{ TIFFTAG_CFAPATTERN,	4, 4,	TIFF_BYTE, 0, TIFF_SETGET_C0_UINT8, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0,	0,	"CFAPattern" , NULL},
139 	{ TIFFTAG_COPYRIGHT, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "Copyright", NULL },
140 	/* end Pixar tags */
141 	{ TIFFTAG_RICHTIFFIPTC, -3, -3, TIFF_LONG, 0, TIFF_SETGET_C32_UINT32, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 1, "RichTIFFIPTC", NULL },
142 	{ TIFFTAG_PHOTOSHOP, -3, -3, TIFF_BYTE, 0, TIFF_SETGET_C32_UINT8, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 1, "Photoshop", NULL },
143 	{ TIFFTAG_EXIFIFD, 1, 1, TIFF_IFD8, 0, TIFF_SETGET_IFD8, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "EXIFIFDOffset", (TIFFFieldArray*) &exifFieldArray },
144 	{ TIFFTAG_ICCPROFILE, -3, -3, TIFF_UNDEFINED, 0, TIFF_SETGET_C32_UINT8, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 1, "ICC Profile", NULL },
145 	{ TIFFTAG_GPSIFD, 1, 1, TIFF_IFD8, 0, TIFF_SETGET_IFD8, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "GPSIFDOffset", NULL },
146 	{ TIFFTAG_FAXRECVPARAMS, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, TIFF_SETGET_UINT32, FIELD_CUSTOM, TRUE, FALSE, "FaxRecvParams", NULL },
147 	{ TIFFTAG_FAXSUBADDRESS, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, TIFF_SETGET_ASCII, FIELD_CUSTOM, TRUE, FALSE, "FaxSubAddress", NULL },
148 	{ TIFFTAG_FAXRECVTIME, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, TIFF_SETGET_UINT32, FIELD_CUSTOM, TRUE, FALSE, "FaxRecvTime", NULL },
149 	{ TIFFTAG_FAXDCS, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, TIFF_SETGET_ASCII, FIELD_CUSTOM, TRUE, FALSE, "FaxDcs", NULL },
150 	{ TIFFTAG_STONITS, 1, 1, TIFF_DOUBLE, 0, TIFF_SETGET_DOUBLE, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "StoNits", NULL },
151 	{ TIFFTAG_INTEROPERABILITYIFD, 1, 1, TIFF_IFD8, 0, TIFF_SETGET_UNDEFINED, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "InteroperabilityIFDOffset", NULL },
152 	/* begin DNG tags */
153 	{ TIFFTAG_DNGVERSION, 4, 4, TIFF_BYTE, 0, TIFF_SETGET_C0_UINT8, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "DNGVersion", NULL },
154 	{ TIFFTAG_DNGBACKWARDVERSION, 4, 4, TIFF_BYTE, 0, TIFF_SETGET_C0_UINT8, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "DNGBackwardVersion", NULL },
155 	{ TIFFTAG_UNIQUECAMERAMODEL, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "UniqueCameraModel", NULL },
156 	{ TIFFTAG_LOCALIZEDCAMERAMODEL, -1, -1, TIFF_BYTE, 0, TIFF_SETGET_C16_UINT8, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 1, "LocalizedCameraModel", NULL },
157 	{ TIFFTAG_CFAPLANECOLOR, -1, -1, TIFF_BYTE, 0, TIFF_SETGET_C16_UINT8, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 1, "CFAPlaneColor", NULL },
158 	{ TIFFTAG_CFALAYOUT, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "CFALayout", NULL },
159 	{ TIFFTAG_LINEARIZATIONTABLE, -1, -1, TIFF_SHORT, 0, TIFF_SETGET_C16_UINT16, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 1, "LinearizationTable", NULL },
160 	{ TIFFTAG_BLACKLEVELREPEATDIM, 2, 2, TIFF_SHORT, 0, TIFF_SETGET_C0_UINT16, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "BlackLevelRepeatDim", NULL },
161 	{ TIFFTAG_BLACKLEVEL, -1, -1, TIFF_RATIONAL, 0, TIFF_SETGET_C16_FLOAT, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 1, "BlackLevel", NULL },
162 	{ TIFFTAG_BLACKLEVELDELTAH, -1, -1, TIFF_SRATIONAL, 0, TIFF_SETGET_C16_FLOAT, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 1, "BlackLevelDeltaH", NULL },
163 	{ TIFFTAG_BLACKLEVELDELTAV, -1, -1, TIFF_SRATIONAL, 0, TIFF_SETGET_C16_FLOAT, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 1, "BlackLevelDeltaV", NULL },
164 	{ TIFFTAG_WHITELEVEL, -1, -1, TIFF_LONG, 0, TIFF_SETGET_C16_UINT32, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 1, "WhiteLevel", NULL },
165 	{ TIFFTAG_DEFAULTSCALE, 2, 2, TIFF_RATIONAL, 0, TIFF_SETGET_C0_FLOAT, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "DefaultScale", NULL },
166 	{ TIFFTAG_BESTQUALITYSCALE, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_DOUBLE, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "BestQualityScale", NULL },
167 	{ TIFFTAG_DEFAULTCROPORIGIN, 2, 2, TIFF_RATIONAL, 0, TIFF_SETGET_C0_FLOAT, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "DefaultCropOrigin", NULL },
168 	{ TIFFTAG_DEFAULTCROPSIZE, 2, 2, TIFF_RATIONAL, 0, TIFF_SETGET_C0_FLOAT, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "DefaultCropSize", NULL },
169 	{ TIFFTAG_COLORMATRIX1, -1, -1, TIFF_SRATIONAL, 0, TIFF_SETGET_C16_FLOAT, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 1, "ColorMatrix1", NULL },
170 	{ TIFFTAG_COLORMATRIX2, -1, -1, TIFF_SRATIONAL, 0, TIFF_SETGET_C16_FLOAT, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 1, "ColorMatrix2", NULL },
171 	{ TIFFTAG_CAMERACALIBRATION1, -1, -1, TIFF_SRATIONAL, 0, TIFF_SETGET_C16_FLOAT, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 1, "CameraCalibration1", NULL },
172 	{ TIFFTAG_CAMERACALIBRATION2, -1, -1, TIFF_SRATIONAL, 0, TIFF_SETGET_C16_FLOAT, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 1, "CameraCalibration2", NULL },
173 	{ TIFFTAG_REDUCTIONMATRIX1, -1, -1, TIFF_SRATIONAL, 0, TIFF_SETGET_C16_FLOAT, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 1, "ReductionMatrix1", NULL },
174 	{ TIFFTAG_REDUCTIONMATRIX2, -1, -1, TIFF_SRATIONAL, 0, TIFF_SETGET_C16_FLOAT, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 1, "ReductionMatrix2", NULL },
175 	{ TIFFTAG_ANALOGBALANCE, -1, -1, TIFF_RATIONAL, 0, TIFF_SETGET_C16_FLOAT, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 1, "AnalogBalance", NULL },
176 	{ TIFFTAG_ASSHOTNEUTRAL, -1, -1, TIFF_RATIONAL, 0, TIFF_SETGET_C16_FLOAT, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 1, "AsShotNeutral", NULL },
177 	{ TIFFTAG_ASSHOTWHITEXY, 2, 2, TIFF_RATIONAL, 0, TIFF_SETGET_C0_FLOAT, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "AsShotWhiteXY", NULL },
178 	{ TIFFTAG_BASELINEEXPOSURE, 1, 1, TIFF_SRATIONAL, 0, TIFF_SETGET_DOUBLE, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "BaselineExposure", NULL },
179 	{ TIFFTAG_BASELINENOISE, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_DOUBLE, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "BaselineNoise", NULL },
180 	{ TIFFTAG_BASELINESHARPNESS, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_DOUBLE, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "BaselineSharpness", NULL },
181 	{ TIFFTAG_BAYERGREENSPLIT, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "BayerGreenSplit", NULL },
182 	{ TIFFTAG_LINEARRESPONSELIMIT, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_DOUBLE, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "LinearResponseLimit", NULL },
183 	{ TIFFTAG_CAMERASERIALNUMBER, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "CameraSerialNumber", NULL },
184 	{ TIFFTAG_LENSINFO, 4, 4, TIFF_RATIONAL, 0, TIFF_SETGET_C0_FLOAT, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "LensInfo", NULL },
185 	{ TIFFTAG_CHROMABLURRADIUS, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_DOUBLE, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "ChromaBlurRadius", NULL },
186 	{ TIFFTAG_ANTIALIASSTRENGTH, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_DOUBLE, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "AntiAliasStrength", NULL },
187 	{ TIFFTAG_SHADOWSCALE, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_DOUBLE, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "ShadowScale", NULL },
188 	{ TIFFTAG_DNGPRIVATEDATA, -1, -1, TIFF_BYTE, 0, TIFF_SETGET_C16_UINT8, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 1, "DNGPrivateData", NULL },
189 	{ TIFFTAG_MAKERNOTESAFETY, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "MakerNoteSafety", NULL },
190 	{ TIFFTAG_CALIBRATIONILLUMINANT1, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "CalibrationIlluminant1", NULL },
191 	{ TIFFTAG_CALIBRATIONILLUMINANT2, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "CalibrationIlluminant2", NULL },
192 	{ TIFFTAG_RAWDATAUNIQUEID, 16, 16, TIFF_BYTE, 0, TIFF_SETGET_C0_UINT8, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "RawDataUniqueID", NULL },
193 	{ TIFFTAG_ORIGINALRAWFILENAME, -1, -1, TIFF_BYTE, 0, TIFF_SETGET_C16_UINT8, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 1, "OriginalRawFileName", NULL },
194 	{ TIFFTAG_ORIGINALRAWFILEDATA, -1, -1, TIFF_UNDEFINED, 0, TIFF_SETGET_C16_UINT8, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 1, "OriginalRawFileData", NULL },
195 	{ TIFFTAG_ACTIVEAREA, 4, 4, TIFF_LONG, 0, TIFF_SETGET_C0_UINT32, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "ActiveArea", NULL },
196 	{ TIFFTAG_MASKEDAREAS, -1, -1, TIFF_LONG, 0, TIFF_SETGET_C16_UINT32, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 1, "MaskedAreas", NULL },
197 	{ TIFFTAG_ASSHOTICCPROFILE, -1, -1, TIFF_UNDEFINED, 0, TIFF_SETGET_C16_UINT8, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 1, "AsShotICCProfile", NULL },
198 	{ TIFFTAG_ASSHOTPREPROFILEMATRIX, -1, -1, TIFF_SRATIONAL, 0, TIFF_SETGET_C16_FLOAT, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 1, "AsShotPreProfileMatrix", NULL },
199 	{ TIFFTAG_CURRENTICCPROFILE, -1, -1, TIFF_UNDEFINED, 0, TIFF_SETGET_C16_UINT8, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 1, "CurrentICCProfile", NULL },
200 	{ TIFFTAG_CURRENTPREPROFILEMATRIX, -1, -1, TIFF_SRATIONAL, 0, TIFF_SETGET_C16_FLOAT, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 1, "CurrentPreProfileMatrix", NULL },
201 	{ TIFFTAG_PERSAMPLE, 0, 0, TIFF_SHORT, 0, TIFF_SETGET_UNDEFINED, TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, TRUE, FALSE, "PerSample", NULL},
202 	/* end DNG tags */
203 	/* begin TIFF/FX tags */
204         { TIFFTAG_INDEXED, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "Indexed", NULL },
205         { TIFFTAG_GLOBALPARAMETERSIFD, 1, 1, TIFF_IFD8, 0, TIFF_SETGET_IFD8, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "GlobalParametersIFD", NULL },
206         { TIFFTAG_PROFILETYPE, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "ProfileType", NULL },
207         { TIFFTAG_FAXPROFILE, 1, 1, TIFF_BYTE, 0, TIFF_SETGET_UINT8, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "FaxProfile", NULL },
208         { TIFFTAG_CODINGMETHODS, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "CodingMethods", NULL },
209         { TIFFTAG_VERSIONYEAR, 4, 4, TIFF_BYTE, 0, TIFF_SETGET_C0_UINT8, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "VersionYear", NULL },
210         { TIFFTAG_MODENUMBER, 1, 1, TIFF_BYTE, 0, TIFF_SETGET_UINT8, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "ModeNumber", NULL },
211         { TIFFTAG_DECODE, -1, -1, TIFF_SRATIONAL, 0, TIFF_SETGET_C16_FLOAT, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 1, "Decode", NULL },
212         { TIFFTAG_IMAGEBASECOLOR, -1, -1, TIFF_SHORT, 0, TIFF_SETGET_C16_UINT16, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 1, "ImageBaseColor", NULL },
213         { TIFFTAG_T82OPTIONS, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "T82Options", NULL },
214         { TIFFTAG_STRIPROWCOUNTS, -1, -1, TIFF_LONG, 0, TIFF_SETGET_C16_UINT32, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 1, "StripRowCounts", NULL },
215         { TIFFTAG_IMAGELAYER, 2, 2, TIFF_LONG, 0, TIFF_SETGET_C0_UINT32, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, "ImageLayer", NULL },
216 	/* end TIFF/FX tags */
217 	/* begin pseudo tags */
218 };
219 
220 static const TIFFField
221 exifFields[] = {
222 	{ EXIFTAG_EXPOSURETIME, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_DOUBLE, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "ExposureTime", NULL },
223 	{ EXIFTAG_FNUMBER, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_DOUBLE, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "FNumber", NULL },
224 	{ EXIFTAG_EXPOSUREPROGRAM, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "ExposureProgram", NULL },
225 	{ EXIFTAG_SPECTRALSENSITIVITY, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "SpectralSensitivity", NULL },
226 	{ EXIFTAG_ISOSPEEDRATINGS, -1, -1, TIFF_SHORT, 0, TIFF_SETGET_C16_UINT16, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 1, "ISOSpeedRatings", NULL },
227 	{ EXIFTAG_OECF, -1, -1, TIFF_UNDEFINED, 0, TIFF_SETGET_C16_UINT8, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 1, "OptoelectricConversionFactor", NULL },
228 	{ EXIFTAG_EXIFVERSION, 4, 4, TIFF_UNDEFINED, 0, TIFF_SETGET_C0_UINT8, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "ExifVersion", NULL },
229 	{ EXIFTAG_DATETIMEORIGINAL, 20, 20, TIFF_ASCII, 0, TIFF_SETGET_ASCII, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "DateTimeOriginal", NULL },
230 	{ EXIFTAG_DATETIMEDIGITIZED, 20, 20, TIFF_ASCII, 0, TIFF_SETGET_ASCII, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "DateTimeDigitized", NULL },
231 	{ EXIFTAG_COMPONENTSCONFIGURATION, 4, 4, TIFF_UNDEFINED, 0, TIFF_SETGET_C0_UINT8, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "ComponentsConfiguration", NULL },
232 	{ EXIFTAG_COMPRESSEDBITSPERPIXEL, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_DOUBLE, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "CompressedBitsPerPixel", NULL },
233 	{ EXIFTAG_SHUTTERSPEEDVALUE, 1, 1, TIFF_SRATIONAL, 0, TIFF_SETGET_DOUBLE, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "ShutterSpeedValue", NULL },
234 	{ EXIFTAG_APERTUREVALUE, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_DOUBLE, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "ApertureValue", NULL },
235 	{ EXIFTAG_BRIGHTNESSVALUE, 1, 1, TIFF_SRATIONAL, 0, TIFF_SETGET_DOUBLE, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "BrightnessValue", NULL },
236 	{ EXIFTAG_EXPOSUREBIASVALUE, 1, 1, TIFF_SRATIONAL, 0, TIFF_SETGET_DOUBLE, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "ExposureBiasValue", NULL },
237 	{ EXIFTAG_MAXAPERTUREVALUE, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_DOUBLE, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "MaxApertureValue", NULL },
238 	{ EXIFTAG_SUBJECTDISTANCE, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_DOUBLE, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "SubjectDistance", NULL },
239 	{ EXIFTAG_METERINGMODE, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "MeteringMode", NULL },
240 	{ EXIFTAG_LIGHTSOURCE, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "LightSource", NULL },
241 	{ EXIFTAG_FLASH, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "Flash", NULL },
242 	{ EXIFTAG_FOCALLENGTH, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_DOUBLE, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "FocalLength", NULL },
243 	{ EXIFTAG_SUBJECTAREA, -1, -1, TIFF_SHORT, 0, TIFF_SETGET_C16_UINT16, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 1, "SubjectArea", NULL },
244 	{ EXIFTAG_MAKERNOTE, -1, -1, TIFF_UNDEFINED, 0, TIFF_SETGET_C16_UINT8, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 1, "MakerNote", NULL },
245 	{ EXIFTAG_USERCOMMENT, -1, -1, TIFF_UNDEFINED, 0, TIFF_SETGET_C16_UINT8, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 1, "UserComment", NULL },
246 	{ EXIFTAG_SUBSECTIME, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "SubSecTime", NULL },
247 	{ EXIFTAG_SUBSECTIMEORIGINAL, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "SubSecTimeOriginal", NULL },
248 	{ EXIFTAG_SUBSECTIMEDIGITIZED, -1, -1, TIFF_ASCII, 0, TIFF_SETGET_ASCII, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "SubSecTimeDigitized", NULL },
249 	{ EXIFTAG_FLASHPIXVERSION, 4, 4, TIFF_UNDEFINED, 0, TIFF_SETGET_C0_UINT8, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "FlashpixVersion", NULL },
250 	{ EXIFTAG_COLORSPACE, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "ColorSpace", NULL },
251 	{ EXIFTAG_PIXELXDIMENSION, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "PixelXDimension", NULL },
252 	{ EXIFTAG_PIXELYDIMENSION, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "PixelYDimension", NULL },
253 	{ EXIFTAG_RELATEDSOUNDFILE, 13, 13, TIFF_ASCII, 0, TIFF_SETGET_ASCII, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "RelatedSoundFile", NULL },
254 	{ EXIFTAG_FLASHENERGY, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_DOUBLE, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "FlashEnergy", NULL },
255 	{ EXIFTAG_SPATIALFREQUENCYRESPONSE, -1, -1, TIFF_UNDEFINED, 0, TIFF_SETGET_C16_UINT8, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 1, "SpatialFrequencyResponse", NULL },
256 	{ EXIFTAG_FOCALPLANEXRESOLUTION, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_DOUBLE, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "FocalPlaneXResolution", NULL },
257 	{ EXIFTAG_FOCALPLANEYRESOLUTION, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_DOUBLE, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "FocalPlaneYResolution", NULL },
258 	{ EXIFTAG_FOCALPLANERESOLUTIONUNIT, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "FocalPlaneResolutionUnit", NULL },
259 	{ EXIFTAG_SUBJECTLOCATION, 2, 2, TIFF_SHORT, 0, TIFF_SETGET_C0_UINT16, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "SubjectLocation", NULL },
260 	{ EXIFTAG_EXPOSUREINDEX, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_DOUBLE, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "ExposureIndex", NULL },
261 	{ EXIFTAG_SENSINGMETHOD, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "SensingMethod", NULL },
262 	{ EXIFTAG_FILESOURCE, 1, 1, TIFF_UNDEFINED, 0, TIFF_SETGET_UINT8, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "FileSource", NULL },
263 	{ EXIFTAG_SCENETYPE, 1, 1, TIFF_UNDEFINED, 0, TIFF_SETGET_UINT8, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "SceneType", NULL },
264 	{ EXIFTAG_CFAPATTERN, -1, -1, TIFF_UNDEFINED, 0, TIFF_SETGET_C16_UINT8, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 1, "CFAPattern", NULL },
265 	{ EXIFTAG_CUSTOMRENDERED, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "CustomRendered", NULL },
266 	{ EXIFTAG_EXPOSUREMODE, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "ExposureMode", NULL },
267 	{ EXIFTAG_WHITEBALANCE, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "WhiteBalance", NULL },
268 	{ EXIFTAG_DIGITALZOOMRATIO, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_DOUBLE, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "DigitalZoomRatio", NULL },
269 	{ EXIFTAG_FOCALLENGTHIN35MMFILM, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "FocalLengthIn35mmFilm", NULL },
270 	{ EXIFTAG_SCENECAPTURETYPE, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "SceneCaptureType", NULL },
271 	{ EXIFTAG_GAINCONTROL, 1, 1, TIFF_RATIONAL, 0, TIFF_SETGET_DOUBLE, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "GainControl", NULL },
272 	{ EXIFTAG_CONTRAST, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "Contrast", NULL },
273 	{ EXIFTAG_SATURATION, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "Saturation", NULL },
274 	{ EXIFTAG_SHARPNESS, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "Sharpness", NULL },
275 	{ EXIFTAG_DEVICESETTINGDESCRIPTION, -1, -1, TIFF_UNDEFINED, 0, TIFF_SETGET_C16_UINT8, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 1, "DeviceSettingDescription", NULL },
276 	{ EXIFTAG_SUBJECTDISTANCERANGE, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "SubjectDistanceRange", NULL },
277 	{ EXIFTAG_IMAGEUNIQUEID, 33, 33, TIFF_ASCII, 0, TIFF_SETGET_ASCII, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, "ImageUniqueID", NULL }
278 };
279 
280 static const TIFFFieldArray
281 tiffFieldArray = { tfiatImage, 0, TIFFArrayCount(tiffFields), (TIFFField*) tiffFields };
282 static const TIFFFieldArray
283 exifFieldArray = { tfiatExif, 0, TIFFArrayCount(exifFields), (TIFFField*) exifFields };
284 
285 /*
286  *  We have our own local lfind() equivalent to avoid subtle differences
287  *  in types passed to lfind() on different systems.
288  */
289 
290 static void *
td_lfind(const void * key,const void * base,size_t * nmemb,size_t size,int (* compar)(const void *,const void *))291 td_lfind(const void *key, const void *base, size_t *nmemb, size_t size,
292          int(*compar)(const void *, const void *))
293 {
294     char *element, *end;
295 
296     end = (char *)base + *nmemb * size;
297     for (element = (char *)base; element < end; element += size)
298         if (!compar(key, element))		/* key found */
299             return element;
300 
301     return NULL;
302 }
303 
304 const TIFFFieldArray*
_TIFFGetFields(void)305 _TIFFGetFields(void)
306 {
307 	return(&tiffFieldArray);
308 }
309 
310 const TIFFFieldArray*
_TIFFGetExifFields(void)311 _TIFFGetExifFields(void)
312 {
313 	return(&exifFieldArray);
314 }
315 
316 void
_TIFFSetupFields(TIFF * tif,const TIFFFieldArray * fieldarray)317 _TIFFSetupFields(TIFF* tif, const TIFFFieldArray* fieldarray)
318 {
319 	if (tif->tif_fields && tif->tif_nfields > 0) {
320 		uint32 i;
321 
322 		for (i = 0; i < tif->tif_nfields; i++) {
323 			TIFFField *fld = tif->tif_fields[i];
324 			if (fld->field_bit == FIELD_CUSTOM &&
325 				strncmp("Tag ", fld->field_name, 4) == 0) {
326 					_TIFFfree(fld->field_name);
327 					_TIFFfree(fld);
328 				}
329 		}
330 
331 		_TIFFfree(tif->tif_fields);
332 		tif->tif_fields = NULL;
333 		tif->tif_nfields = 0;
334 	}
335 	if (!_TIFFMergeFields(tif, fieldarray->fields, fieldarray->count)) {
336 		TIFFErrorExt(tif->tif_clientdata, "_TIFFSetupFields",
337 			     "Setting up field info failed");
338 	}
339 }
340 
341 static int
tagCompare(const void * a,const void * b)342 tagCompare(const void* a, const void* b)
343 {
344 	const TIFFField* ta = *(const TIFFField**) a;
345 	const TIFFField* tb = *(const TIFFField**) b;
346 	/* NB: be careful of return values for 16-bit platforms */
347 	if (ta->field_tag != tb->field_tag)
348 		return (int)ta->field_tag - (int)tb->field_tag;
349 	else
350 		return (ta->field_type == TIFF_ANY) ?
351 			0 : ((int)tb->field_type - (int)ta->field_type);
352 }
353 
354 static int
tagNameCompare(const void * a,const void * b)355 tagNameCompare(const void* a, const void* b)
356 {
357 	const TIFFField* ta = *(const TIFFField**) a;
358 	const TIFFField* tb = *(const TIFFField**) b;
359 	int ret = strcmp(ta->field_name, tb->field_name);
360 
361 	if (ret)
362 		return ret;
363 	else
364 		return (ta->field_type == TIFF_ANY) ?
365 			0 : ((int)tb->field_type - (int)ta->field_type);
366 }
367 
368 int
_TIFFMergeFields(TIFF * tif,const TIFFField info[],uint32 n)369 _TIFFMergeFields(TIFF* tif, const TIFFField info[], uint32 n)
370 {
371 	static const char module[] = "_TIFFMergeFields";
372 	static const char reason[] = "for fields array";
373 	/* TIFFField** tp; */
374 	uint32 i;
375 
376         tif->tif_foundfield = NULL;
377 
378 	if (tif->tif_fields && tif->tif_nfields > 0) {
379 		tif->tif_fields = (TIFFField**)
380 			_TIFFCheckRealloc(tif, tif->tif_fields,
381 					  (tif->tif_nfields + n),
382 					  sizeof(TIFFField *), reason);
383 	} else {
384 		tif->tif_fields = (TIFFField **)
385 			_TIFFCheckMalloc(tif, n, sizeof(TIFFField *),
386 					 reason);
387 	}
388 	if (!tif->tif_fields) {
389 		TIFFErrorExt(tif->tif_clientdata, module,
390 			     "Failed to allocate fields array");
391 		return 0;
392 	}
393 
394 	/* tp = tif->tif_fields + tif->tif_nfields; */
395 	for (i = 0; i < n; i++) {
396 		const TIFFField *fip =
397 			TIFFFindField(tif, info[i].field_tag, TIFF_ANY);
398 
399                 /* only add definitions that aren't already present */
400 		if (!fip) {
401                         tif->tif_fields[tif->tif_nfields] = (TIFFField *) (info+i);
402                         tif->tif_nfields++;
403                 }
404 	}
405 
406         /* Sort the field info by tag number */
407 	qsort(tif->tif_fields, tif->tif_nfields,
408 	      sizeof(TIFFField *), tagCompare);
409 
410 	return n;
411 }
412 
413 void
_TIFFPrintFieldInfo(TIFF * tif,FILE * fd)414 _TIFFPrintFieldInfo(TIFF* tif, FILE* fd)
415 {
416 	uint32 i;
417 
418 	fprintf(fd, "%s: \n", tif->tif_name);
419 	for (i = 0; i < tif->tif_nfields; i++) {
420 		const TIFFField* fip = tif->tif_fields[i];
421 		fprintf(fd, "field[%2d] %5lu, %2d, %2d, %d, %2d, %5s, %5s, %s\n"
422 			, (int)i
423 			, (unsigned long) fip->field_tag
424 			, fip->field_readcount, fip->field_writecount
425 			, fip->field_type
426 			, fip->field_bit
427 			, fip->field_oktochange ? "TRUE" : "FALSE"
428 			, fip->field_passcount ? "TRUE" : "FALSE"
429 			, fip->field_name
430 		);
431 	}
432 }
433 
434 /*
435  * Return size of TIFFDataType in bytes
436  */
437 int
TIFFDataWidth(TIFFDataType type)438 TIFFDataWidth(TIFFDataType type)
439 {
440 	switch(type)
441 	{
442 		case 0:  /* nothing */
443 		case TIFF_BYTE:
444 		case TIFF_ASCII:
445 		case TIFF_SBYTE:
446 		case TIFF_UNDEFINED:
447 			return 1;
448 		case TIFF_SHORT:
449 		case TIFF_SSHORT:
450 			return 2;
451 		case TIFF_LONG:
452 		case TIFF_SLONG:
453 		case TIFF_FLOAT:
454 		case TIFF_IFD:
455 			return 4;
456 		case TIFF_RATIONAL:
457 		case TIFF_SRATIONAL:
458 		case TIFF_DOUBLE:
459 		case TIFF_LONG8:
460 		case TIFF_SLONG8:
461 		case TIFF_IFD8:
462 			return 8;
463 		default:
464 			return 0; /* will return 0 for unknown types */
465 	}
466 }
467 
468 /*
469  * Return size of TIFFDataType in bytes.
470  *
471  * XXX: We need a separate function to determine the space needed
472  * to store the value. For TIFF_RATIONAL values TIFFDataWidth() returns 8,
473  * but we use 4-byte float to represent rationals.
474  */
475 int
_TIFFDataSize(TIFFDataType type)476 _TIFFDataSize(TIFFDataType type)
477 {
478 	switch (type)
479 	{
480 		case TIFF_BYTE:
481 		case TIFF_SBYTE:
482 		case TIFF_ASCII:
483 		case TIFF_UNDEFINED:
484 		    return 1;
485 		case TIFF_SHORT:
486 		case TIFF_SSHORT:
487 		    return 2;
488 		case TIFF_LONG:
489 		case TIFF_SLONG:
490 		case TIFF_FLOAT:
491 		case TIFF_IFD:
492 		case TIFF_RATIONAL:
493 		case TIFF_SRATIONAL:
494 		    return 4;
495 		case TIFF_DOUBLE:
496 		case TIFF_LONG8:
497 		case TIFF_SLONG8:
498 		case TIFF_IFD8:
499 		    return 8;
500 		default:
501 		    return 0;
502 	}
503 }
504 
505 const TIFFField*
TIFFFindField(TIFF * tif,uint32 tag,TIFFDataType dt)506 TIFFFindField(TIFF* tif, uint32 tag, TIFFDataType dt)
507 {
508 	TIFFField key = {0, 0, 0, TIFF_NOTYPE, 0, 0, 0, 0, 0, 0, NULL, NULL};
509 	TIFFField* pkey = &key;
510 	const TIFFField **ret;
511 	if (tif->tif_foundfield && tif->tif_foundfield->field_tag == tag &&
512 	    (dt == TIFF_ANY || dt == tif->tif_foundfield->field_type))
513 		return tif->tif_foundfield;
514 
515 	/* If we are invoked with no field information, then just return. */
516 	if (!tif->tif_fields)
517 		return NULL;
518 
519 	/* NB: use sorted search (e.g. binary search) */
520 
521 	key.field_tag = tag;
522 	key.field_type = dt;
523 
524 	ret = (const TIFFField **) bsearch(&pkey, tif->tif_fields,
525 					   tif->tif_nfields,
526 					   sizeof(TIFFField *), tagCompare);
527 	return tif->tif_foundfield = (ret ? *ret : NULL);
528 }
529 
530 static const TIFFField*
_TIFFFindFieldByName(TIFF * tif,const char * field_name,TIFFDataType dt)531 _TIFFFindFieldByName(TIFF* tif, const char *field_name, TIFFDataType dt)
532 {
533 	TIFFField key = {0, 0, 0, TIFF_NOTYPE, 0, 0, 0, 0, 0, 0, NULL, NULL};
534 	TIFFField* pkey = &key;
535 	const TIFFField **ret;
536 	if (tif->tif_foundfield
537 	    && streq(tif->tif_foundfield->field_name, field_name)
538 	    && (dt == TIFF_ANY || dt == tif->tif_foundfield->field_type))
539 		return (tif->tif_foundfield);
540 
541 	/* If we are invoked with no field information, then just return. */
542 	if (!tif->tif_fields)
543 		return NULL;
544 
545 	/* NB: use linear search since list is sorted by key#, not name */
546 
547 	key.field_name = (char *)field_name;
548 	key.field_type = dt;
549 
550 	ret = (const TIFFField **)
551             td_lfind(&pkey, tif->tif_fields, &tif->tif_nfields,
552                      sizeof(TIFFField *), tagNameCompare);
553 
554 	return tif->tif_foundfield = (ret ? *ret : NULL);
555 }
556 
557 const TIFFField*
TIFFFieldWithTag(TIFF * tif,uint32 tag)558 TIFFFieldWithTag(TIFF* tif, uint32 tag)
559 {
560 	const TIFFField* fip = TIFFFindField(tif, tag, TIFF_ANY);
561 	if (!fip) {
562 		TIFFErrorExt(tif->tif_clientdata, "TIFFFieldWithTag",
563 			     "Internal error, unknown tag 0x%x",
564 			     (unsigned int) tag);
565 	}
566 	return (fip);
567 }
568 
569 const TIFFField*
TIFFFieldWithName(TIFF * tif,const char * field_name)570 TIFFFieldWithName(TIFF* tif, const char *field_name)
571 {
572 	const TIFFField* fip =
573 		_TIFFFindFieldByName(tif, field_name, TIFF_ANY);
574 	if (!fip) {
575 		TIFFErrorExt(tif->tif_clientdata, "TIFFFieldWithName",
576 			     "Internal error, unknown tag %s", field_name);
577 	}
578 	return (fip);
579 }
580 
581 uint32
TIFFFieldTag(const TIFFField * fip)582 TIFFFieldTag(const TIFFField* fip)
583 {
584 	return fip->field_tag;
585 }
586 
587 const char *
TIFFFieldName(const TIFFField * fip)588 TIFFFieldName(const TIFFField* fip)
589 {
590 	return fip->field_name;
591 }
592 
593 TIFFDataType
TIFFFieldDataType(const TIFFField * fip)594 TIFFFieldDataType(const TIFFField* fip)
595 {
596 	return fip->field_type;
597 }
598 
599 int
TIFFFieldPassCount(const TIFFField * fip)600 TIFFFieldPassCount(const TIFFField* fip)
601 {
602 	return fip->field_passcount;
603 }
604 
605 int
TIFFFieldReadCount(const TIFFField * fip)606 TIFFFieldReadCount(const TIFFField* fip)
607 {
608 	return fip->field_readcount;
609 }
610 
611 int
TIFFFieldWriteCount(const TIFFField * fip)612 TIFFFieldWriteCount(const TIFFField* fip)
613 {
614 	return fip->field_writecount;
615 }
616 
617 const TIFFField*
_TIFFFindOrRegisterField(TIFF * tif,uint32 tag,TIFFDataType dt)618 _TIFFFindOrRegisterField(TIFF *tif, uint32 tag, TIFFDataType dt)
619 
620 {
621 	const TIFFField *fld;
622 
623 	fld = TIFFFindField(tif, tag, dt);
624 	if (fld == NULL) {
625 		fld = _TIFFCreateAnonField(tif, tag, dt);
626 		if (!_TIFFMergeFields(tif, fld, 1))
627 			return NULL;
628 	}
629 
630 	return fld;
631 }
632 
633 TIFFField*
_TIFFCreateAnonField(TIFF * tif,uint32 tag,TIFFDataType field_type)634 _TIFFCreateAnonField(TIFF *tif, uint32 tag, TIFFDataType field_type)
635 {
636 	TIFFField *fld;
637 	(void) tif;
638 
639 	fld = (TIFFField *) _TIFFmalloc(sizeof (TIFFField));
640 	if (fld == NULL)
641 	    return NULL;
642 	_TIFFmemset(fld, 0, sizeof(TIFFField));
643 
644 	fld->field_tag = tag;
645 	fld->field_readcount = TIFF_VARIABLE2;
646 	fld->field_writecount = TIFF_VARIABLE2;
647 	fld->field_type = field_type;
648 	fld->reserved = 0;
649 	switch (field_type)
650 	{
651 		case TIFF_BYTE:
652 		case TIFF_UNDEFINED:
653 			fld->set_field_type = TIFF_SETGET_C32_UINT8;
654 			fld->get_field_type = TIFF_SETGET_C32_UINT8;
655 			break;
656 		case TIFF_ASCII:
657 			fld->set_field_type = TIFF_SETGET_C32_ASCII;
658 			fld->get_field_type = TIFF_SETGET_C32_ASCII;
659 			break;
660 		case TIFF_SHORT:
661 			fld->set_field_type = TIFF_SETGET_C32_UINT16;
662 			fld->get_field_type = TIFF_SETGET_C32_UINT16;
663 			break;
664 		case TIFF_LONG:
665 			fld->set_field_type = TIFF_SETGET_C32_UINT32;
666 			fld->get_field_type = TIFF_SETGET_C32_UINT32;
667 			break;
668 		case TIFF_RATIONAL:
669 		case TIFF_SRATIONAL:
670 		case TIFF_FLOAT:
671 			fld->set_field_type = TIFF_SETGET_C32_FLOAT;
672 			fld->get_field_type = TIFF_SETGET_C32_FLOAT;
673 			break;
674 		case TIFF_SBYTE:
675 			fld->set_field_type = TIFF_SETGET_C32_SINT8;
676 			fld->get_field_type = TIFF_SETGET_C32_SINT8;
677 			break;
678 		case TIFF_SSHORT:
679 			fld->set_field_type = TIFF_SETGET_C32_SINT16;
680 			fld->get_field_type = TIFF_SETGET_C32_SINT16;
681 			break;
682 		case TIFF_SLONG:
683 			fld->set_field_type = TIFF_SETGET_C32_SINT32;
684 			fld->get_field_type = TIFF_SETGET_C32_SINT32;
685 			break;
686 		case TIFF_DOUBLE:
687 			fld->set_field_type = TIFF_SETGET_C32_DOUBLE;
688 			fld->get_field_type = TIFF_SETGET_C32_DOUBLE;
689 			break;
690 		case TIFF_IFD:
691 		case TIFF_IFD8:
692 			fld->set_field_type = TIFF_SETGET_C32_IFD8;
693 			fld->get_field_type = TIFF_SETGET_C32_IFD8;
694 			break;
695 		case TIFF_LONG8:
696 			fld->set_field_type = TIFF_SETGET_C32_UINT64;
697 			fld->get_field_type = TIFF_SETGET_C32_UINT64;
698 			break;
699 		case TIFF_SLONG8:
700 			fld->set_field_type = TIFF_SETGET_C32_SINT64;
701 			fld->get_field_type = TIFF_SETGET_C32_SINT64;
702 			break;
703 		default:
704 			fld->set_field_type = TIFF_SETGET_UNDEFINED;
705 			fld->get_field_type = TIFF_SETGET_UNDEFINED;
706 			break;
707 	}
708 	fld->field_bit = FIELD_CUSTOM;
709 	fld->field_oktochange = TRUE;
710 	fld->field_passcount = TRUE;
711 	fld->field_name = (char *) _TIFFmalloc(32);
712 	if (fld->field_name == NULL) {
713 	    _TIFFfree(fld);
714 	    return NULL;
715 	}
716 	fld->field_subfields = NULL;
717 
718 	/*
719 	 * note that this name is a special sign to TIFFClose() and
720 	 * _TIFFSetupFields() to free the field
721 	 */
722 	(void) snprintf(fld->field_name, 32, "Tag %d", (int) tag);
723 
724 	return fld;
725 }
726 
727 /****************************************************************************
728  *               O B S O L E T E D    I N T E R F A C E S
729  *
730  * Don't use this stuff in your applications, it may be removed in the future
731  * libtiff versions.
732  ****************************************************************************/
733 
734 static TIFFSetGetFieldType
_TIFFSetGetType(TIFFDataType type,short count,unsigned char passcount)735 _TIFFSetGetType(TIFFDataType type, short count, unsigned char passcount)
736 {
737 	if (type == TIFF_ASCII && count == TIFF_VARIABLE && passcount == 0)
738 		return TIFF_SETGET_ASCII;
739 
740 	else if (count == 1 && passcount == 0) {
741 		switch (type)
742 		{
743 			case TIFF_BYTE:
744 			case TIFF_UNDEFINED:
745 				return TIFF_SETGET_UINT8;
746 			case TIFF_ASCII:
747 				return TIFF_SETGET_ASCII;
748 			case TIFF_SHORT:
749 				return TIFF_SETGET_UINT16;
750 			case TIFF_LONG:
751 				return TIFF_SETGET_UINT32;
752 			case TIFF_RATIONAL:
753 			case TIFF_SRATIONAL:
754 			case TIFF_FLOAT:
755 				return TIFF_SETGET_FLOAT;
756 			case TIFF_SBYTE:
757 				return TIFF_SETGET_SINT8;
758 			case TIFF_SSHORT:
759 				return TIFF_SETGET_SINT16;
760 			case TIFF_SLONG:
761 				return TIFF_SETGET_SINT32;
762 			case TIFF_DOUBLE:
763 				return TIFF_SETGET_DOUBLE;
764 			case TIFF_IFD:
765 			case TIFF_IFD8:
766 				return TIFF_SETGET_IFD8;
767 			case TIFF_LONG8:
768 				return TIFF_SETGET_UINT64;
769 			case TIFF_SLONG8:
770 				return TIFF_SETGET_SINT64;
771 			default:
772 				return TIFF_SETGET_UNDEFINED;
773 		}
774 	}
775 
776 	else if (count >= 1 && passcount == 0) {
777 		switch (type)
778 		{
779 			case TIFF_BYTE:
780 			case TIFF_UNDEFINED:
781 				return TIFF_SETGET_C0_UINT8;
782 			case TIFF_ASCII:
783 				return TIFF_SETGET_C0_ASCII;
784 			case TIFF_SHORT:
785 				return TIFF_SETGET_C0_UINT16;
786 			case TIFF_LONG:
787 				return TIFF_SETGET_C0_UINT32;
788 			case TIFF_RATIONAL:
789 			case TIFF_SRATIONAL:
790 			case TIFF_FLOAT:
791 				return TIFF_SETGET_C0_FLOAT;
792 			case TIFF_SBYTE:
793 				return TIFF_SETGET_C0_SINT8;
794 			case TIFF_SSHORT:
795 				return TIFF_SETGET_C0_SINT16;
796 			case TIFF_SLONG:
797 				return TIFF_SETGET_C0_SINT32;
798 			case TIFF_DOUBLE:
799 				return TIFF_SETGET_C0_DOUBLE;
800 			case TIFF_IFD:
801 			case TIFF_IFD8:
802 				return TIFF_SETGET_C0_IFD8;
803 			case TIFF_LONG8:
804 				return TIFF_SETGET_C0_UINT64;
805 			case TIFF_SLONG8:
806 				return TIFF_SETGET_C0_SINT64;
807 			default:
808 				return TIFF_SETGET_UNDEFINED;
809 		}
810 	}
811 
812 	else if (count == TIFF_VARIABLE && passcount == 1) {
813 		switch (type)
814 		{
815 			case TIFF_BYTE:
816 			case TIFF_UNDEFINED:
817 				return TIFF_SETGET_C16_UINT8;
818 			case TIFF_ASCII:
819 				return TIFF_SETGET_C16_ASCII;
820 			case TIFF_SHORT:
821 				return TIFF_SETGET_C16_UINT16;
822 			case TIFF_LONG:
823 				return TIFF_SETGET_C16_UINT32;
824 			case TIFF_RATIONAL:
825 			case TIFF_SRATIONAL:
826 			case TIFF_FLOAT:
827 				return TIFF_SETGET_C16_FLOAT;
828 			case TIFF_SBYTE:
829 				return TIFF_SETGET_C16_SINT8;
830 			case TIFF_SSHORT:
831 				return TIFF_SETGET_C16_SINT16;
832 			case TIFF_SLONG:
833 				return TIFF_SETGET_C16_SINT32;
834 			case TIFF_DOUBLE:
835 				return TIFF_SETGET_C16_DOUBLE;
836 			case TIFF_IFD:
837 			case TIFF_IFD8:
838 				return TIFF_SETGET_C16_IFD8;
839 			case TIFF_LONG8:
840 				return TIFF_SETGET_C16_UINT64;
841 			case TIFF_SLONG8:
842 				return TIFF_SETGET_C16_SINT64;
843 			default:
844 				return TIFF_SETGET_UNDEFINED;
845 		}
846 	}
847 
848 	else if (count == TIFF_VARIABLE2 && passcount == 1) {
849 		switch (type)
850 		{
851 			case TIFF_BYTE:
852 			case TIFF_UNDEFINED:
853 				return TIFF_SETGET_C32_UINT8;
854 			case TIFF_ASCII:
855 				return TIFF_SETGET_C32_ASCII;
856 			case TIFF_SHORT:
857 				return TIFF_SETGET_C32_UINT16;
858 			case TIFF_LONG:
859 				return TIFF_SETGET_C32_UINT32;
860 			case TIFF_RATIONAL:
861 			case TIFF_SRATIONAL:
862 			case TIFF_FLOAT:
863 				return TIFF_SETGET_C32_FLOAT;
864 			case TIFF_SBYTE:
865 				return TIFF_SETGET_C32_SINT8;
866 			case TIFF_SSHORT:
867 				return TIFF_SETGET_C32_SINT16;
868 			case TIFF_SLONG:
869 				return TIFF_SETGET_C32_SINT32;
870 			case TIFF_DOUBLE:
871 				return TIFF_SETGET_C32_DOUBLE;
872 			case TIFF_IFD:
873 			case TIFF_IFD8:
874 				return TIFF_SETGET_C32_IFD8;
875 			case TIFF_LONG8:
876 				return TIFF_SETGET_C32_UINT64;
877 			case TIFF_SLONG8:
878 				return TIFF_SETGET_C32_SINT64;
879 			default:
880 				return TIFF_SETGET_UNDEFINED;
881 		}
882 	}
883 
884 	return TIFF_SETGET_UNDEFINED;
885 }
886 
887 int
TIFFMergeFieldInfo(TIFF * tif,const TIFFFieldInfo info[],uint32 n)888 TIFFMergeFieldInfo(TIFF* tif, const TIFFFieldInfo info[], uint32 n)
889 {
890 	static const char module[] = "TIFFMergeFieldInfo";
891 	static const char reason[] = "for fields array";
892 	TIFFField *tp;
893 	size_t nfields;
894 	uint32 i;
895 
896 	if (tif->tif_nfieldscompat > 0) {
897 		tif->tif_fieldscompat = (TIFFFieldArray *)
898 			_TIFFCheckRealloc(tif, tif->tif_fieldscompat,
899 					  tif->tif_nfieldscompat + 1,
900 					  sizeof(TIFFFieldArray), reason);
901 	} else {
902 		tif->tif_fieldscompat = (TIFFFieldArray *)
903 			_TIFFCheckMalloc(tif, 1, sizeof(TIFFFieldArray),
904 					 reason);
905 	}
906 	if (!tif->tif_fieldscompat) {
907 		TIFFErrorExt(tif->tif_clientdata, module,
908 			     "Failed to allocate fields array");
909 		return -1;
910 	}
911 	nfields = tif->tif_nfieldscompat++;
912 
913 	tif->tif_fieldscompat[nfields].type = tfiatOther;
914 	tif->tif_fieldscompat[nfields].allocated_size = n;
915 	tif->tif_fieldscompat[nfields].count = n;
916 	tif->tif_fieldscompat[nfields].fields =
917 		(TIFFField *)_TIFFCheckMalloc(tif, n, sizeof(TIFFField),
918 					      reason);
919 	if (!tif->tif_fieldscompat[nfields].fields) {
920 		TIFFErrorExt(tif->tif_clientdata, module,
921 			     "Failed to allocate fields array");
922 		return -1;
923 	}
924 
925 	tp = tif->tif_fieldscompat[nfields].fields;
926 	for (i = 0; i < n; i++) {
927 		tp->field_tag = info[i].field_tag;
928 		tp->field_readcount = info[i].field_readcount;
929 		tp->field_writecount = info[i].field_writecount;
930 		tp->field_type = info[i].field_type;
931 		tp->reserved = 0;
932 		tp->set_field_type =
933 		     _TIFFSetGetType(info[i].field_type,
934 				info[i].field_readcount,
935 				info[i].field_passcount);
936 		tp->get_field_type =
937 		     _TIFFSetGetType(info[i].field_type,
938 				info[i].field_readcount,
939 				info[i].field_passcount);
940 		tp->field_bit = info[i].field_bit;
941 		tp->field_oktochange = info[i].field_oktochange;
942 		tp->field_passcount = info[i].field_passcount;
943 		tp->field_name = info[i].field_name;
944 		tp->field_subfields = NULL;
945 		tp++;
946 	}
947 
948 	if (!_TIFFMergeFields(tif, tif->tif_fieldscompat[nfields].fields, n)) {
949 		TIFFErrorExt(tif->tif_clientdata, module,
950 			     "Setting up field info failed");
951 		return -1;
952 	}
953 
954 	return 0;
955 }
956 
957 int
_TIFFCheckFieldIsValidForCodec(TIFF * tif,ttag_t tag)958 _TIFFCheckFieldIsValidForCodec(TIFF *tif, ttag_t tag)
959 {
960 	/* Filter out non-codec specific tags */
961 	switch (tag) {
962 	    /* Shared tags */
963 	    case TIFFTAG_PREDICTOR:
964 	    /* JPEG tags */
965 	    case TIFFTAG_JPEGTABLES:
966 	    /* OJPEG tags */
967 	    case TIFFTAG_JPEGIFOFFSET:
968 	    case TIFFTAG_JPEGIFBYTECOUNT:
969 	    case TIFFTAG_JPEGQTABLES:
970 	    case TIFFTAG_JPEGDCTABLES:
971 	    case TIFFTAG_JPEGACTABLES:
972 	    case TIFFTAG_JPEGPROC:
973 	    case TIFFTAG_JPEGRESTARTINTERVAL:
974 	    /* CCITT* */
975 	    case TIFFTAG_BADFAXLINES:
976 	    case TIFFTAG_CLEANFAXDATA:
977 	    case TIFFTAG_CONSECUTIVEBADFAXLINES:
978 	    case TIFFTAG_GROUP3OPTIONS:
979 	    case TIFFTAG_GROUP4OPTIONS:
980 	    /* LERC */
981 	    case TIFFTAG_LERC_PARAMETERS:
982 		break;
983 	    default:
984 		return 1;
985 	}
986 	/* Check if codec specific tags are allowed for the current
987 	 * compression scheme (codec) */
988 	switch (tif->tif_dir.td_compression) {
989 	    case COMPRESSION_LZW:
990 		if (tag == TIFFTAG_PREDICTOR)
991 		    return 1;
992 		break;
993 	    case COMPRESSION_PACKBITS:
994 		/* No codec-specific tags */
995 		break;
996 	    case COMPRESSION_THUNDERSCAN:
997 		/* No codec-specific tags */
998 		break;
999 	    case COMPRESSION_NEXT:
1000 		/* No codec-specific tags */
1001 		break;
1002 	    case COMPRESSION_JPEG:
1003 		if (tag == TIFFTAG_JPEGTABLES)
1004 		    return 1;
1005 		break;
1006 	    case COMPRESSION_OJPEG:
1007 		switch (tag) {
1008 		    case TIFFTAG_JPEGIFOFFSET:
1009 		    case TIFFTAG_JPEGIFBYTECOUNT:
1010 		    case TIFFTAG_JPEGQTABLES:
1011 		    case TIFFTAG_JPEGDCTABLES:
1012 		    case TIFFTAG_JPEGACTABLES:
1013 		    case TIFFTAG_JPEGPROC:
1014 		    case TIFFTAG_JPEGRESTARTINTERVAL:
1015 			return 1;
1016 		}
1017 		break;
1018 	    case COMPRESSION_CCITTRLE:
1019 	    case COMPRESSION_CCITTRLEW:
1020 	    case COMPRESSION_CCITTFAX3:
1021 	    case COMPRESSION_CCITTFAX4:
1022 		switch (tag) {
1023 		    case TIFFTAG_BADFAXLINES:
1024 		    case TIFFTAG_CLEANFAXDATA:
1025 		    case TIFFTAG_CONSECUTIVEBADFAXLINES:
1026 			return 1;
1027 		    case TIFFTAG_GROUP3OPTIONS:
1028 			if (tif->tif_dir.td_compression == COMPRESSION_CCITTFAX3)
1029 			    return 1;
1030 			break;
1031 		    case TIFFTAG_GROUP4OPTIONS:
1032 			if (tif->tif_dir.td_compression == COMPRESSION_CCITTFAX4)
1033 			    return 1;
1034 			break;
1035 		}
1036 		break;
1037 	    case COMPRESSION_JBIG:
1038 		/* No codec-specific tags */
1039 		break;
1040 	    case COMPRESSION_DEFLATE:
1041 	    case COMPRESSION_ADOBE_DEFLATE:
1042 		if (tag == TIFFTAG_PREDICTOR)
1043 		    return 1;
1044 		break;
1045 	   case COMPRESSION_PIXARLOG:
1046 		if (tag == TIFFTAG_PREDICTOR)
1047 		    return 1;
1048 		break;
1049 	    case COMPRESSION_SGILOG:
1050 	    case COMPRESSION_SGILOG24:
1051 		/* No codec-specific tags */
1052 		break;
1053 	    case COMPRESSION_LZMA:
1054 		if (tag == TIFFTAG_PREDICTOR)
1055 		    return 1;
1056 		break;
1057 	    case COMPRESSION_ZSTD:
1058 		if (tag == TIFFTAG_PREDICTOR)
1059 		    return 1;
1060 		break;
1061 	    case COMPRESSION_LERC:
1062 		if (tag == TIFFTAG_LERC_PARAMETERS)
1063 		    return 1;
1064 		break;
1065 		  case COMPRESSION_WEBP:
1066 		if (tag == TIFFTAG_PREDICTOR)
1067 				return 1;
1068 		break;
1069 	}
1070 	return 0;
1071 }
1072 
1073 /* vim: set ts=8 sts=8 sw=8 noet: */
1074 
1075 /*
1076  * Local Variables:
1077  * mode: c
1078  * c-basic-offset: 8
1079  * fill-column: 78
1080  * End:
1081  */
1082