Debian Codegraph @

texk/xdvipsk/thirdparty/libtiff/tools/tiffset.c

raw
1 /******************************************************************************
2 * Project: libtiff tools
3 * Purpose: Mainline for setting metadata in existing TIFF files.
4 * Author: Frank Warmerdam, [email protected]
5 *
6 ******************************************************************************
7 * Copyright (c) 2000, Frank Warmerdam
8 *
9 * Permission to use, copy, modify, distribute, and sell this software and
10 * its documentation for any purpose is hereby granted without fee, provided
11 * that (i) the above copyright notices and this permission notice appear in
12 * all copies of the software and related documentation, and (ii) the names of
13 * Sam Leffler and Silicon Graphics may not be used in any advertising or
14 * publicity relating to the software without the specific, prior written
15 * permission of Sam Leffler and Silicon Graphics.
16 *
17 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
19 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
20 *
21 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
22 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
23 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
24 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
25 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
26 * OF THIS SOFTWARE.
27 ******************************************************************************
28 */
29
30 #include "tif_config.h"
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35
36 #include "tiffio.h"
37
38 #ifdef NEED_LIBPORT
39 #include "libport.h"
40 #endif
41
42 #ifndef EXIT_SUCCESS
43 #define EXIT_SUCCESS 0
44 #endif
45 #ifndef EXIT_FAILURE
46 #define EXIT_FAILURE 1
47 #endif
48
49 /*
50 Visual Studio does not support _fseeki64 and _ftelli64 until the 2005 release.
51 Without these interfaces, files over 2GB in size are not supported for
52 Windows.
53 For MinGW, __MSVCRT_VERSION__ must be at least 0x800 to expose these
54 interfaces. The MinGW compiler must support the requested version. MinGW
55 does not distribute the CRT (it is supplied by Microsoft) so the correct CRT
56 must be available on the target computer in order for the program to run.
57 */
58 #if defined(_WIN32) && !(defined(_MSC_VER) && _MSC_VER < 1400) && \
59 !(defined(__MSVCRT_VERSION__) && __MSVCRT_VERSION__ < 0x800)
60 #define TIFFfseek(stream, offset, whence) \
61 _fseeki64(stream, /* __int64 */ offset, whence)
62 #define TIFFftell(stream) /* __int64 */ _ftelli64(stream)
63 #pragma message("...... _fseeki64 defined ....")
64 #else
65 #define TIFFfseek(stream, offset, whence) fseek(stream, offset, whence)
66 #define TIFFftell(stream) ftell(stream)
67 #endif
68
69 #define DEFAULT_MAX_MALLOC (256 * 1024 * 1024)
70
71 /* malloc size limit (in bytes)
72 * disabled when set to 0 */
73 static tmsize_t maxMalloc = DEFAULT_MAX_MALLOC;
74
75 static const char usageMsg[] =
76 "Set the value of a TIFF header to a specified value\n\n"
77 "usage: tiffset [options] filename\n"
78 "where options are:\n"
79 " -s <tagname> [count] <value>... set the tag value\n"
80 " -u <tagname> to unset the tag\n"
81 " -d <dirno> set the directory\n"
82 " -sd <diroff> set the subdirectory\n"
83 " -sf <tagname> <filename> read the tag value from file (for ASCII tags "
84 "only)\n"
85 " -m <size> set maximum memory allocation size (MiB). 0 to disable "
86 "limit. Must be first parameter.\n"
87 " -h this help screen\n"
88 " The options can be repeated and are processed sequentially.\n";
89
90 static void usage(int code)
91 {
92 FILE *out = (code == EXIT_SUCCESS) ? stdout : stderr;
93
94 fprintf(out, "%s\n\n", TIFFGetVersion());
95 fprintf(out, "%s", usageMsg);
96 exit(code);
97 }
98
99 static const TIFFField *GetField(TIFF *tiff, const char *tagname)
100 {
101 const TIFFField *fip;
102
103 if (atoi(tagname) > 0)
104 fip = TIFFFieldWithTag(tiff, (ttag_t)atoi(tagname));
105 else
106 fip = TIFFFieldWithName(tiff, tagname);
107
108 if (!fip)
109 {
110 fprintf(stderr, "Field name \"%s\" is not recognised.\n", tagname);
111 return (TIFFField *)NULL;
112 }
113
114 return fip;
115 }
116
117 /**
118 * This custom malloc function enforce a maximum allocation size
119 */
120 static void *limitMalloc(tmsize_t s)
121 {
122 if (maxMalloc && (s > maxMalloc))
123 {
124 fprintf(stderr,
125 "MemoryLimitError: allocation of %" TIFF_SSIZE_FORMAT
126 " bytes is forbidden. Limit is %" TIFF_SSIZE_FORMAT ".\n",
127 s, maxMalloc);
128 fprintf(stderr, " use -m option to change limit.\n");
129 return NULL;
130 }
131 return _TIFFmalloc(s);
132 }
133
134 int main(int argc, char *argv[])
135 {
136 TIFF *tiff;
137 int arg_index;
138
139 if (argc < 2)
140 usage(EXIT_FAILURE);
141
142 tiff = TIFFOpen(argv[argc - 1], "r+");
143 if (tiff == NULL)
144 return EXIT_FAILURE;
145
146 for (arg_index = 1; arg_index < argc - 1; arg_index++)
147 {
148 if (strcmp(argv[arg_index], "-d") == 0 && arg_index < argc - 2)
149 {
150 arg_index++;
151 if (TIFFSetDirectory(tiff, atoi(argv[arg_index])) != 1)
152 {
153 fprintf(stderr, "Failed to set directory=%s\n",
154 argv[arg_index]);
155 return EXIT_FAILURE;
156 }
157 arg_index++;
158 }
159 if (strcmp(argv[arg_index], "-sd") == 0 && arg_index < argc - 2)
160 {
161 arg_index++;
162 if (TIFFSetSubDirectory(tiff, atoi(argv[arg_index])) != 1)
163 {
164 fprintf(stderr, "Failed to set sub directory=%s\n",
165 argv[arg_index]);
166 return EXIT_FAILURE;
167 }
168 arg_index++;
169 }
170 /* Add unset option to tiffset -- Zach Baker ([email protected])
171 * 11/14/2012 */
172 if (strcmp(argv[arg_index], "-u") == 0 && arg_index < argc - 2)
173 {
174 const TIFFField *fip;
175 const char *tagname;
176 arg_index++;
177 tagname = argv[arg_index];
178 fip = GetField(tiff, tagname);
179 if (!fip)
180 return EXIT_FAILURE;
181
182 if (TIFFUnsetField(tiff, TIFFFieldTag(fip)) != 1)
183 {
184 fprintf(stderr, "Failed to unset %s\n", TIFFFieldName(fip));
185 }
186 arg_index++;
187 }
188 else if (strcmp(argv[arg_index], "-s") == 0 && arg_index < argc - 3)
189 {
190 const TIFFField *fip;
191 const char *tagname;
192
193 arg_index++;
194 tagname = argv[arg_index];
195 fip = GetField(tiff, tagname);
196
197 if (!fip)
198 return 3;
199
200 arg_index++;
201 if (TIFFFieldDataType(fip) == TIFF_ASCII)
202 {
203 if (TIFFFieldPassCount(fip))
204 {
205 size_t len;
206 len = strlen(argv[arg_index]) + 1;
207 if (len > UINT16_MAX ||
208 TIFFSetField(tiff, TIFFFieldTag(fip), (uint16_t)len,
209 argv[arg_index]) != 1)
210 fprintf(stderr, "Failed to set %s=%s\n",
211 TIFFFieldName(fip), argv[arg_index]);
212 }
213 else
214 {
215 if (TIFFSetField(tiff, TIFFFieldTag(fip),
216 argv[arg_index]) != 1)
217 fprintf(stderr, "Failed to set %s=%s\n",
218 TIFFFieldName(fip), argv[arg_index]);
219 }
220 }
221 else if (TIFFFieldWriteCount(fip) > 0 ||
222 TIFFFieldWriteCount(fip) == TIFF_VARIABLE)
223 {
224 int ret = 1;
225 short wc;
226
227 if (TIFFFieldWriteCount(fip) == TIFF_VARIABLE)
228 wc = atoi(argv[arg_index++]);
229 else
230 wc = TIFFFieldWriteCount(fip);
231
232 if (argc - arg_index < wc)
233 {
234 fprintf(stderr,
235 "Number of tag values is not enough. "
236 "Expected %d values for %s tag, got %d\n",
237 wc, TIFFFieldName(fip), argc - arg_index);
238 return EXIT_FAILURE;
239 }
240
241 if (wc > 1 || TIFFFieldWriteCount(fip) == TIFF_VARIABLE)
242 {
243 int i, size;
244 void *array;
245
246 switch (TIFFFieldDataType(fip))
247 {
248 /*
249 * XXX: We can't use TIFFDataWidth()
250 * to determine the space needed to store
251 * the value. For TIFF_RATIONAL values
252 * TIFFDataWidth() returns 8, but we use 4-byte
253 * float to represent rationals.
254 */
255 case TIFF_BYTE:
256 case TIFF_ASCII:
257 case TIFF_SBYTE:
258 case TIFF_UNDEFINED:
259 default:
260 size = 1;
261 break;
262
263 case TIFF_SHORT:
264 case TIFF_SSHORT:
265 size = 2;
266 break;
267
268 case TIFF_LONG:
269 case TIFF_SLONG:
270 case TIFF_FLOAT:
271 case TIFF_IFD:
272 case TIFF_RATIONAL:
273 case TIFF_SRATIONAL:
274 size = 4;
275 break;
276
277 case TIFF_LONG8:
278 case TIFF_SLONG8:
279 case TIFF_IFD8:
280 case TIFF_DOUBLE:
281 size = 8;
282 break;
283 }
284
285 array = limitMalloc((tmsize_t)wc * size);
286 if (!array)
287 {
288 fprintf(stderr, "No space for %s tag\n", tagname);
289 return EXIT_FAILURE;
290 }
291
292 switch (TIFFFieldDataType(fip))
293 {
294 case TIFF_BYTE:
295 for (i = 0; i < wc; i++)
296 ((uint8_t *)array)[i] =
297 atoi(argv[arg_index + i]);
298 break;
299 case TIFF_SHORT:
300 for (i = 0; i < wc; i++)
301 ((uint16_t *)array)[i] =
302 atoi(argv[arg_index + i]);
303 break;
304 case TIFF_SBYTE:
305 for (i = 0; i < wc; i++)
306 ((int8_t *)array)[i] =
307 atoi(argv[arg_index + i]);
308 break;
309 case TIFF_SSHORT:
310 for (i = 0; i < wc; i++)
311 ((int16_t *)array)[i] =
312 atoi(argv[arg_index + i]);
313 break;
314 case TIFF_LONG:
315 for (i = 0; i < wc; i++)
316 ((uint32_t *)array)[i] =
317 atol(argv[arg_index + i]);
318 break;
319 case TIFF_SLONG:
320 case TIFF_IFD:
321 for (i = 0; i < wc; i++)
322 ((int32_t *)array)[i] =
323 atol(argv[arg_index + i]);
324 break;
325 case TIFF_LONG8:
326 for (i = 0; i < wc; i++)
327 ((uint64_t *)array)[i] = strtoll(
328 argv[arg_index + i], (char **)NULL, 10);
329 break;
330 case TIFF_SLONG8:
331 case TIFF_IFD8:
332 for (i = 0; i < wc; i++)
333 ((int64_t *)array)[i] = strtoll(
334 argv[arg_index + i], (char **)NULL, 10);
335 break;
336 case TIFF_DOUBLE:
337 for (i = 0; i < wc; i++)
338 ((double *)array)[i] =
339 atof(argv[arg_index + i]);
340 break;
341 case TIFF_RATIONAL:
342 case TIFF_SRATIONAL:
343 case TIFF_FLOAT:
344 for (i = 0; i < wc; i++)
345 ((float *)array)[i] =
346 (float)atof(argv[arg_index + i]);
347 break;
348 default:
349 break;
350 }
351
352 if (TIFFFieldPassCount(fip))
353 {
354 ret = TIFFSetField(tiff, TIFFFieldTag(fip), wc, array);
355 }
356 else if (TIFFFieldTag(fip) == TIFFTAG_PAGENUMBER ||
357 TIFFFieldTag(fip) == TIFFTAG_HALFTONEHINTS ||
358 TIFFFieldTag(fip) == TIFFTAG_YCBCRSUBSAMPLING ||
359 TIFFFieldTag(fip) == TIFFTAG_DOTRANGE)
360 {
361 if (TIFFFieldDataType(fip) == TIFF_BYTE)
362 {
363 ret = TIFFSetField(tiff, TIFFFieldTag(fip),
364 ((uint8_t *)array)[0],
365 ((uint8_t *)array)[1]);
366 }
367 else if (TIFFFieldDataType(fip) == TIFF_SHORT)
368 {
369 ret = TIFFSetField(tiff, TIFFFieldTag(fip),
370 ((uint16_t *)array)[0],
371 ((uint16_t *)array)[1]);
372 }
373 }
374 else
375 {
376 ret = TIFFSetField(tiff, TIFFFieldTag(fip), array);
377 }
378
379 _TIFFfree(array);
380 }
381 else
382 {
383 switch (TIFFFieldDataType(fip))
384 {
385 case TIFF_BYTE:
386 case TIFF_SHORT:
387 case TIFF_SBYTE:
388 case TIFF_SSHORT:
389 ret = TIFFSetField(tiff, TIFFFieldTag(fip),
390 atoi(argv[arg_index++]));
391 break;
392 case TIFF_LONG:
393 case TIFF_SLONG:
394 case TIFF_IFD:
395 ret = TIFFSetField(tiff, TIFFFieldTag(fip),
396 atol(argv[arg_index++]));
397 break;
398 case TIFF_LONG8:
399 case TIFF_SLONG8:
400 case TIFF_IFD8:
401 ret = TIFFSetField(
402 tiff, TIFFFieldTag(fip),
403 strtoll(argv[arg_index++], (char **)NULL, 10));
404 break;
405 case TIFF_DOUBLE:
406 ret = TIFFSetField(tiff, TIFFFieldTag(fip),
407 atof(argv[arg_index++]));
408 break;
409 case TIFF_RATIONAL:
410 case TIFF_SRATIONAL:
411 case TIFF_FLOAT:
412 ret = TIFFSetField(tiff, TIFFFieldTag(fip),
413 (float)atof(argv[arg_index++]));
414 break;
415 default:
416 break;
417 }
418 }
419
420 if (ret != 1)
421 fprintf(stderr, "Failed to set %s\n", TIFFFieldName(fip));
422 arg_index += wc;
423 }
424 }
425 else if (strcmp(argv[arg_index], "-sf") == 0 && arg_index < argc - 3)
426 {
427 FILE *fp;
428 const TIFFField *fip;
429 char *text;
430 size_t len;
431 int ret;
432 int64_t fsize;
433
434 arg_index++;
435 fip = GetField(tiff, argv[arg_index]);
436
437 if (!fip)
438 return EXIT_FAILURE;
439
440 if (TIFFFieldDataType(fip) != TIFF_ASCII)
441 {
442 fprintf(stderr,
443 "Only ASCII tags can be set from file. "
444 "%s is not ASCII tag.\n",
445 TIFFFieldName(fip));
446 return EXIT_FAILURE;
447 }
448
449 arg_index++;
450 fp = fopen(argv[arg_index], "rt");
451 if (fp == NULL)
452 {
453 perror(argv[arg_index]);
454 continue;
455 }
456
457 /* Get file size and enlarge it for some space in buffer.
458 * Maximum ASCII tag size is limited by uint32_t count.
459 */
460 TIFFfseek(fp, 0L, SEEK_END);
461 fsize = TIFFftell(fp) + 1;
462 rewind(fp);
463
464 /* for x32 tmsize_t is only int32_t. The - 1 is just here to make
465 * Coverity Scan happy on 64 bit builds where the condition would
466 * be always true otherwise.
467 */
468 if (fsize > TIFF_TMSIZE_T_MAX - 1 || fsize <= 0)
469 {
470 fprintf(
471 stderr,
472 "Contents of %s is too large to store in an ASCII tag.\n",
473 argv[arg_index]);
474 fclose(fp);
475 continue;
476 }
477 text = (char *)limitMalloc((tmsize_t)fsize);
478 if (text == NULL)
479 {
480 fprintf(stderr, "Memory allocation error\n");
481 fclose(fp);
482 continue;
483 }
484 len = fread(text, 1, (size_t)(fsize - 1), fp);
485 text[len] = '\0';
486
487 fclose(fp);
488
489 if (TIFFFieldPassCount(fip))
490 {
491 ret =
492 TIFFSetField(tiff, TIFFFieldTag(fip), (uint16_t)len, text);
493 }
494 else
495 {
496 ret = TIFFSetField(tiff, TIFFFieldTag(fip), text);
497 }
498 if (!ret)
499 {
500 fprintf(stderr, "Failed to set %s from file %s\n",
501 TIFFFieldName(fip), argv[arg_index]);
502 }
503
504 _TIFFfree(text);
505 arg_index++;
506 }
507 else if (strcmp(argv[arg_index], "-m") == 0)
508 {
509 arg_index++;
510 maxMalloc = (tmsize_t)strtoul(argv[arg_index], NULL, 0) << 20;
511 }
512 else if (strcmp(argv[arg_index], "-h") == 0 ||
513 strcmp(argv[arg_index], "--help") == 0)
514 {
515 usage(EXIT_SUCCESS);
516 }
517 else
518 {
519 fprintf(stderr,
520 "Unrecognised option: %s or too few parameters left "
521 "for this option.\n",
522 argv[arg_index]);
523 usage(EXIT_FAILURE);
524 }
525 }
526
527 TIFFRewriteDirectory(tiff);
528 TIFFClose(tiff);
529 return EXIT_SUCCESS;
530 }