Debian Codegraph @

texk/xdvipsk/thirdparty/libtiff/contrib/iptcutil/iptcutil.c

raw
1 #include "tif_config.h"
2
3 #include <ctype.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7
8 #ifdef HAVE_STRINGS_H
9 #include <strings.h>
10 #endif
11
12 #ifdef HAVE_IO_H
13 #include <io.h>
14 #endif
15
16 #ifdef HAVE_FCNTL_H
17 #include <fcntl.h>
18 #endif
19
20 #ifdef _WIN32
21 #define STRNICMP strnicmp
22 #else
23 #define STRNICMP strncasecmp
24 #endif
25
26 typedef struct _tag_spec
27 {
28 short id;
29
30 char *name;
31 } tag_spec;
32
33 static tag_spec tags[] = {{5, "Image Name"},
34 {7, "Edit Status"},
35 {10, "Priority"},
36 {15, "Category"},
37 {20, "Supplemental Category"},
38 {22, "Fixture Identifier"},
39 {25, "Keyword"},
40 {30, "Release Date"},
41 {35, "Release Time"},
42 {40, "Special Instructions"},
43 {45, "Reference Service"},
44 {47, "Reference Date"},
45 {50, "Reference Number"},
46 {55, "Created Date"},
47 {60, "Created Time"},
48 {65, "Originating Program"},
49 {70, "Program Version"},
50 {75, "Object Cycle"},
51 {80, "Byline"},
52 {85, "Byline Title"},
53 {90, "City"},
54 {95, "Province State"},
55 {100, "Country Code"},
56 {101, "Country"},
57 {103, "Original Transmission Reference"},
58 {105, "Headline"},
59 {110, "Credit"},
60 {115, "Source"},
61 {116, "Copyright String"},
62 {120, "Caption"},
63 {121, "Local Caption"},
64 {122, "Caption Writer"},
65 {200, "Custom Field 1"},
66 {201, "Custom Field 2"},
67 {202, "Custom Field 3"},
68 {203, "Custom Field 4"},
69 {204, "Custom Field 5"},
70 {205, "Custom Field 6"},
71 {206, "Custom Field 7"},
72 {207, "Custom Field 8"},
73 {208, "Custom Field 9"},
74 {209, "Custom Field 10"},
75 {210, "Custom Field 11"},
76 {211, "Custom Field 12"},
77 {212, "Custom Field 13"},
78 {213, "Custom Field 14"},
79 {214, "Custom Field 15"},
80 {215, "Custom Field 16"},
81 {216, "Custom Field 17"},
82 {217, "Custom Field 18"},
83 {218, "Custom Field 19"},
84 {219, "Custom Field 20"}};
85
86 /*
87 * We format the output using HTML conventions
88 * to preserve control characters and such.
89 */
90 void formatString(FILE *ofile, const char *s, int len)
91 {
92 putc('"', ofile);
93 for (; len > 0; --len, ++s)
94 {
95 int c = *s;
96 switch (c)
97 {
98 case '&':
99 fputs("&amp;", ofile);
100 break;
101 #ifdef HANDLE_GT_LT
102 case '<':
103 fputs("&lt;", ofile);
104 break;
105 case '>':
106 fputs("&gt;", ofile);
107 break;
108 #endif
109 case '"':
110 fputs("&quot;", ofile);
111 break;
112 default:
113 if (iscntrl(c))
114 fprintf(ofile, "&#%d;", c);
115 else
116 putc(*s, ofile);
117 break;
118 }
119 }
120 fputs("\"\n", ofile);
121 }
122
123 typedef struct _html_code
124 {
125 short len;
126 const char *code, val;
127 } html_code;
128
129 static html_code html_codes[] = {
130 #ifdef HANDLE_GT_LT
131 {4, "&lt;", '<'},
132 {4, "&gt;", '>'},
133 #endif
134 {5, "&amp;", '&'},
135 {6, "&quot;", '"'}};
136
137 /*
138 * This routine converts HTML escape sequence
139 * back to the original ASCII representation.
140 * - returns the number of characters dropped.
141 */
142 int convertHTMLcodes(char *s, int len)
143 {
144 if (len <= 0 || s == (char *)NULL || *s == '\0')
145 return 0;
146
147 if (s[1] == '#')
148 {
149 int val, o;
150
151 if (sscanf(s, "&#%d;", &val) == 1)
152 {
153 o = 3;
154 while (s[o] != ';')
155 {
156 o++;
157 if (o > 5)
158 break;
159 }
160 if (o < 5)
161 strcpy(s + 1, s + 1 + o);
162 *s = val;
163 return o;
164 }
165 }
166 else
167 {
168 int i, codes = sizeof(html_codes) / sizeof(html_code);
169
170 for (i = 0; i < codes; i++)
171 {
172 if (html_codes[i].len <= len)
173 if (STRNICMP(s, html_codes[i].code, html_codes[i].len) == 0)
174 {
175 strcpy(s + 1, s + html_codes[i].len);
176 *s = html_codes[i].val;
177 return html_codes[i].len - 1;
178 }
179 }
180 }
181
182 return 0;
183 }
184
185 int formatIPTC(FILE *ifile, FILE *ofile)
186 {
187 unsigned int foundiptc, tagsfound;
188
189 char *readable, *str;
190
191 long tagindx, taglen;
192
193 int i, tagcount = sizeof(tags) / sizeof(tag_spec);
194
195 int c, dataset, recnum;
196
197 foundiptc = 0; /* found the IPTC-Header */
198 tagsfound = 0; /* number of tags found */
199
200 c = getc(ifile);
201 while (c != EOF)
202 {
203 if (c == 0x1c)
204 foundiptc = 1;
205 else
206 {
207 if (foundiptc)
208 {
209 return -1;
210 }
211 else
212 {
213 c = getc(ifile);
214 continue;
215 }
216 }
217
218 /* we found the 0x1c tag and now grab the dataset and record number tags
219 */
220 dataset = getc(ifile);
221 if (dataset == EOF)
222 return -1;
223 recnum = getc(ifile);
224 if (recnum == EOF)
225 return -1;
226 /* try to match this record to one of the ones in our named table */
227 for (i = 0; i < tagcount; i++)
228 {
229 if (tags[i].id == recnum)
230 break;
231 }
232 if (i < tagcount)
233 readable = tags[i].name;
234 else
235 readable = "";
236
237 /* then we decode the length of the block that follows - long or short
238 * fmt */
239 c = getc(ifile);
240 if (c == EOF)
241 return 0;
242 if (c & (unsigned char)0x80)
243 {
244 unsigned char buffer[4];
245
246 for (i = 0; i < 4; i++)
247 {
248 c = getc(ifile);
249 if (c == EOF)
250 return -1;
251 buffer[i] = c;
252 }
253 taglen = (((long)buffer[0]) << 24) | (((long)buffer[1]) << 16) |
254 (((long)buffer[2]) << 8) | (((long)buffer[3]));
255 }
256 else
257 {
258 int x = c;
259
260 taglen = x << 8;
261 x = getc(ifile);
262 if (x == EOF)
263 return -1;
264 taglen |= (long)x;
265 }
266 /* Place limits on tag length */
267 if ((taglen <= 0) || (taglen > 1048576))
268 {
269 printf("Inappropriate IPTC tag length %ld\n", taglen);
270 return -1;
271 }
272 /* make a buffer to hold the tag data and snag it from the input stream
273 */
274 str = (char *)malloc((unsigned int)(taglen + 1));
275 if (str == (char *)NULL)
276 {
277 printf("Memory allocation failed");
278 return 0;
279 }
280 for (tagindx = 0; tagindx < taglen; tagindx++)
281 {
282 c = getc(ifile);
283 if (c == EOF)
284 {
285 free(str);
286 return -1;
287 }
288 str[tagindx] = c;
289 }
290 str[taglen] = 0;
291
292 /* now finish up by formatting this binary data into ASCII equivalent */
293 if (strlen(readable) > 0)
294 fprintf(ofile, "%d#%d#%s=", (unsigned int)dataset,
295 (unsigned int)recnum, readable);
296 else
297 fprintf(ofile, "%d#%d=", (unsigned int)dataset,
298 (unsigned int)recnum);
299 formatString(ofile, str, taglen);
300 free(str);
301
302 tagsfound++;
303
304 c = getc(ifile);
305 }
306 return tagsfound;
307 }
308
309 int tokenizer(unsigned inflag, char *token, int tokmax, char *line, char *white,
310 char *brkchar, char *quote, char eschar, char *brkused, int *next,
311 char *quoted);
312
313 char *super_fgets(char *b, int *blen, FILE *file)
314 {
315 int c, len;
316
317 char *q;
318
319 len = *blen;
320 for (q = b;; q++)
321 {
322 c = fgetc(file);
323 if (c == EOF || c == '\n')
324 break;
325 if ((int)(q - b + 1) >= len)
326 {
327 long tlen;
328
329 tlen = (int)(q - b);
330 len <<= 1;
331 b = (char *)realloc((char *)b, (len + 2));
332 if ((char *)b == (char *)NULL)
333 break;
334 q = b + tlen;
335 }
336 *q = (unsigned char)c;
337 }
338 *blen = 0;
339 if ((unsigned char *)b != (unsigned char *)NULL)
340 {
341 int tlen;
342
343 tlen = (int)(q - b);
344 if (tlen == 0)
345 return (char *)NULL;
346 b[tlen] = '\0';
347 *blen = ++tlen;
348 }
349 return b;
350 }
351
352 #define BUFFER_SZ 4096
353
354 int main(int argc, char *argv[])
355 {
356 /* unsigned int */
357 /* length; */
358
359 /*unsigned char
360 *buffer;*/
361
362 int i, mode; /* iptc binary, or iptc text */
363
364 FILE *ifile = stdin, *ofile = stdout;
365
366 char c,
367 *usage = "usage: iptcutil -t | -b [-i file] [-o file] <input >output";
368
369 if (argc < 2)
370 {
371 puts(usage);
372 return 1;
373 }
374
375 mode = 0;
376 /* length = -1; */
377 /* buffer = (unsigned char *)NULL; */
378
379 for (i = 1; i < argc; i++)
380 {
381 c = argv[i][0];
382 if (c == '-' || c == '/')
383 {
384 c = argv[i][1];
385 switch (c)
386 {
387 case 't':
388 mode = 1;
389 #ifdef _WIN32
390 /* Set "stdout" to binary mode: */
391 _setmode(_fileno(ofile), _O_BINARY);
392 #endif
393 break;
394 case 'b':
395 mode = 0;
396 #ifdef _WIN32
397 /* Set "stdin" to binary mode: */
398 _setmode(_fileno(ifile), _O_BINARY);
399 #endif
400 break;
401 case 'i':
402 if (mode == 0)
403 ifile = fopen(argv[++i], "rb");
404 else
405 ifile = fopen(argv[++i], "rt");
406 if (ifile == (FILE *)NULL)
407 {
408 printf("Unable to open: %s\n", argv[i]);
409 return 1;
410 }
411 break;
412 case 'o':
413 if (mode == 0)
414 ofile = fopen(argv[++i], "wt");
415 else
416 ofile = fopen(argv[++i], "wb");
417 if (ofile == (FILE *)NULL)
418 {
419 printf("Unable to open: %s\n", argv[i]);
420 return 1;
421 }
422 break;
423 default:
424 printf("Unknown option: %s\n", argv[i]);
425 return 1;
426 }
427 }
428 else
429 {
430 puts(usage);
431 return 1;
432 }
433 }
434
435 if (mode == 0) /* handle binary iptc info */
436 formatIPTC(ifile, ofile);
437
438 if (mode == 1) /* handle text form of iptc info */
439 {
440 char brkused, quoted, *line, *token, *newstr;
441
442 int state, next;
443
444 unsigned char recnum = 0, dataset = 0;
445
446 int inputlen = BUFFER_SZ;
447
448 line = (char *)malloc(inputlen);
449 while ((line = super_fgets(line, &inputlen, ifile)) != NULL)
450 {
451 state = 0;
452 next = 0;
453
454 token = (char *)malloc(inputlen);
455 newstr = (char *)malloc(inputlen);
456 while (tokenizer(0, token, inputlen, line, "", "=", "\"", 0,
457 &brkused, &next, &quoted) == 0)
458 {
459 if (state == 0)
460 {
461 int state2, next2;
462
463 char brkused2, quoted2;
464
465 state2 = 0;
466 next2 = 0;
467 while (tokenizer(0, newstr, inputlen, token, "", "#", "", 0,
468 &brkused2, &next2, &quoted2) == 0)
469 {
470 if (state2 == 0)
471 dataset = (unsigned char)atoi(newstr);
472 else if (state2 == 1)
473 recnum = (unsigned char)atoi(newstr);
474 state2++;
475 }
476 }
477 else if (state == 1)
478 {
479 int next2;
480
481 unsigned long len;
482
483 char brkused2, quoted2;
484
485 next2 = 0;
486 len = (unsigned long)strlen(token);
487 while (tokenizer(0, newstr, inputlen, token, "", "&", "", 0,
488 &brkused2, &next2, &quoted2) == 0)
489 {
490 if (brkused2 && next2 > 0)
491 {
492 char *s = &token[next2 - 1];
493
494 len -= convertHTMLcodes(s, (int)strlen(s));
495 }
496 }
497
498 fputc(0x1c, ofile);
499 fputc(dataset, ofile);
500 fputc(recnum, ofile);
501 if (len < 0x10000)
502 {
503 fputc((len >> 8) & 255, ofile);
504 fputc(len & 255, ofile);
505 }
506 else
507 {
508 fputc(((len >> 24) & 255) | 0x80, ofile);
509 fputc((len >> 16) & 255, ofile);
510 fputc((len >> 8) & 255, ofile);
511 fputc(len & 255, ofile);
512 }
513 next2 = 0;
514 while (len--)
515 fputc(token[next2++], ofile);
516 }
517 state++;
518 }
519 free(token);
520 free(newstr);
521 }
522 free(line);
523
524 fclose(ifile);
525 fclose(ofile);
526 }
527
528 return 0;
529 }
530
531 /*
532 This routine is a generalized, finite state token parser. It allows
533 you extract tokens one at a time from a string of characters. The
534 characters used for white space, for break characters, and for quotes
535 can be specified. Also, characters in the string can be preceded by
536 a specifiable escape character which removes any special meaning the
537 character may have.
538
539 There are a lot of formal parameters in this subroutine call, but
540 once you get familiar with them, this routine is fairly easy to use.
541 "#define" macros can be used to generate simpler looking calls for
542 commonly used applications of this routine.
543
544 First, some terminology:
545
546 token: used here, a single unit of information in
547 the form of a group of characters.
548
549 white space: space that gets ignored (except within quotes
550 or when escaped), like blanks and tabs. in
551 addition, white space terminates a non-quoted
552 token.
553
554 break character: a character that separates non-quoted tokens.
555 commas are a common break character. the
556 usage of break characters to signal the end
557 of a token is the same as that of white space,
558 except multiple break characters with nothing
559 or only white space between generate a null
560 token for each two break characters together.
561
562 for example, if blank is set to be the white
563 space and comma is set to be the break
564 character, the line ...
565
566 A, B, C , , DEF
567
568 ... consists of 5 tokens:
569
570 1) "A"
571 2) "B"
572 3) "C"
573 4) "" (the null string)
574 5) "DEF"
575
576 quote character: a character that, when surrounding a group
577 of other characters, causes the group of
578 characters to be treated as a single token,
579 no matter how many white spaces or break
580 characters exist in the group. also, a
581 token always terminates after the closing
582 quote. for example, if ' is the quote
583 character, blank is white space, and comma
584 is the break character, the following
585 string ...
586
587 A, ' B, CD'EF GHI
588
589 ... consists of 4 tokens:
590
591 1) "A"
592 2) " B, CD" (note the blanks & comma)
593 3) "EF"
594 4) "GHI"
595
596 the quote characters themselves do
597 not appear in the resultant tokens. the
598 double quotes are delimiters i use here for
599 documentation purposes only.
600
601 escape character: a character which itself is ignored but
602 which causes the next character to be
603 used as is. ^ and \ are often used as
604 escape characters. an escape in the last
605 position of the string gets treated as a
606 "normal" (i.e., non-quote, non-white,
607 non-break, and non-escape) character.
608 for example, assume white space, break
609 character, and quote are the same as in the
610 above examples, and further, assume that
611 ^ is the escape character. then, in the
612 string ...
613
614 ABC, ' DEF ^' GH' I ^ J K^ L ^
615
616 ... there are 7 tokens:
617
618 1) "ABC"
619 2) " DEF ' GH"
620 3) "I"
621 4) " " (a lone blank)
622 5) "J"
623 6) "K L"
624 7) "^" (passed as is at end of line)
625
626
627 OK, now that you have this background, here's how to call "tokenizer":
628
629 result=tokenizer(flag,token,maxtok,string,white,break,quote,escape,
630 brkused,next,quoted)
631
632 result: 0 if we haven't reached EOS (end of string), and
633 1 if we have (this is an "int").
634
635 flag: right now, only the low order 3 bits are used.
636 1 => convert non-quoted tokens to upper case
637 2 => convert non-quoted tokens to lower case
638 0 => do not convert non-quoted tokens
639 (this is a "char").
640
641 token: a character string containing the returned next token
642 (this is a "char[]").
643
644 maxtok: the maximum size of "token". characters beyond
645 "maxtok" are truncated (this is an "int").
646
647 string: the string to be parsed (this is a "char[]").
648
649 white: a string of the valid white spaces. example:
650
651 char whitesp[]={" \t"};
652
653 blank and tab will be valid white space (this is
654 a "char[]").
655
656 break: a string of the valid break characters. example:
657
658 char breakch[]={";,"};
659
660 semicolon and comma will be valid break characters
661 (this is a "char[]").
662
663 IMPORTANT: do not use the name "break" as a C
664 variable, as this is a reserved word in C.
665
666 quote: a string of the valid quote characters. an example
667 would be
668
669 char whitesp[]={"'\"");
670
671 (this causes single and double quotes to be valid)
672 note that a token starting with one of these characters
673 needs the same quote character to terminate it.
674
675 for example,
676
677 "ABC '
678
679 is unterminated, but
680
681 "DEF" and 'GHI'
682
683 are properly terminated. note that different quote
684 characters can appear on the same line; only for
685 a given token do the quote characters have to be
686 the same (this is a "char[]").
687
688 escape: the escape character (NOT a string ... only one
689 allowed). use zero if none is desired (this is
690 a "char").
691
692 brkused: the break character used to terminate the current
693 token. if the token was quoted, this will be the
694 quote used. if the token is the last one on the
695 line, this will be zero (this is a pointer to a
696 "char").
697
698 next: this variable points to the first character of the
699 next token. it gets reset by "tokenizer" as it steps
700 through the string. set it to 0 upon initialization,
701 and leave it alone after that. you can change it
702 if you want to jump around in the string or re-parse
703 from the beginning, but be careful (this is a
704 pointer to an "int").
705
706 quoted: set to 1 (true) if the token was quoted and 0 (false)
707 if not. you may need this information (for example:
708 in C, a string with quotes around it is a character
709 string, while one without is an identifier).
710
711 (this is a pointer to a "char").
712 */
713
714 /* states */
715
716 #define IN_WHITE 0
717 #define IN_TOKEN 1
718 #define IN_QUOTE 2
719 #define IN_OZONE 3
720
721 int _p_state; /* current state */
722 unsigned _p_flag; /* option flag */
723 char _p_curquote; /* current quote char */
724 int _p_tokpos; /* current token pos */
725
726 /* routine to find character in string ... used only by "tokenizer" */
727
728 int sindex(char ch, char *string)
729 {
730 char *cp;
731 for (cp = string; *cp; ++cp)
732 if (ch == *cp)
733 return (int)(cp - string); /* return position of character */
734 return -1; /* eol ... no match found */
735 }
736
737 /* routine to store a character in a string ... used only by "tokenizer" */
738
739 void chstore(char *string, int max, char ch)
740 {
741 char c;
742 if (_p_tokpos >= 0 && _p_tokpos < max - 1)
743 {
744 if (_p_state == IN_QUOTE)
745 c = ch;
746 else
747 switch (_p_flag & 3)
748 {
749 case 1: /* convert to upper */
750 c = toupper((int)ch);
751 break;
752
753 case 2: /* convert to lower */
754 c = tolower((int)ch);
755 break;
756
757 default: /* use as is */
758 c = ch;
759 break;
760 }
761 string[_p_tokpos++] = c;
762 }
763 return;
764 }
765
766 int tokenizer(unsigned inflag, char *token, int tokmax, char *line, char *white,
767 char *brkchar, char *quote, char eschar, char *brkused, int *next,
768 char *quoted)
769 {
770 int qp;
771 char c, nc;
772
773 *brkused = 0; /* initialize to null */
774 *quoted = 0; /* assume not quoted */
775
776 if (!line[*next]) /* if we're at end of line, indicate such */
777 return 1;
778
779 _p_state = IN_WHITE; /* initialize state */
780 _p_curquote = 0; /* initialize previous quote char */
781 _p_flag = inflag; /* set option flag */
782
783 for (_p_tokpos = 0; (c = line[*next]); ++(*next)) /* main loop */
784 {
785 if ((qp = sindex(c, brkchar)) >= 0) /* break */
786 {
787 switch (_p_state)
788 {
789 case IN_WHITE: /* these are the same here ... */
790 case IN_TOKEN: /* ... just get out */
791 case IN_OZONE: /* ditto */
792 ++(*next);
793 *brkused = brkchar[qp];
794 goto byebye;
795
796 case IN_QUOTE: /* just keep going */
797 chstore(token, tokmax, c);
798 break;
799 }
800 }
801 else if ((qp = sindex(c, quote)) >= 0) /* quote */
802 {
803 switch (_p_state)
804 {
805 case IN_WHITE: /* these are identical, */
806 _p_state = IN_QUOTE; /* change states */
807 _p_curquote = quote[qp]; /* save quote char */
808 *quoted =
809 1; /* set to true as long as something is in quotes */
810 break;
811
812 case IN_QUOTE:
813 if (quote[qp] ==
814 _p_curquote) /* same as the beginning quote? */
815 {
816 _p_state = IN_OZONE;
817 _p_curquote = 0;
818 }
819 else
820 chstore(token, tokmax, c); /* treat as regular char */
821 break;
822
823 case IN_TOKEN:
824 case IN_OZONE:
825 *brkused = c; /* uses quote as break char */
826 goto byebye;
827 }
828 }
829 else if ((qp = sindex(c, white)) >= 0) /* white */
830 {
831 switch (_p_state)
832 {
833 case IN_WHITE:
834 case IN_OZONE:
835 break; /* keep going */
836
837 case IN_TOKEN:
838 _p_state = IN_OZONE;
839 break;
840
841 case IN_QUOTE:
842 chstore(token, tokmax, c); /* it's valid here */
843 break;
844 }
845 }
846 else if (c == eschar) /* escape */
847 {
848 nc = line[(*next) + 1];
849 if (nc == 0) /* end of line */
850 {
851 *brkused = 0;
852 chstore(token, tokmax, c);
853 ++(*next);
854 goto byebye;
855 }
856 switch (_p_state)
857 {
858 case IN_WHITE:
859 --(*next);
860 _p_state = IN_TOKEN;
861 break;
862
863 case IN_TOKEN:
864 case IN_QUOTE:
865 ++(*next);
866 chstore(token, tokmax, nc);
867 break;
868
869 case IN_OZONE:
870 goto byebye;
871 }
872 }
873 else /* anything else is just a real character */
874 {
875 switch (_p_state)
876 {
877 case IN_WHITE:
878 _p_state = IN_TOKEN; /* switch states */
879 /* Fall through */
880
881 case IN_TOKEN: /* these 2 are */
882 case IN_QUOTE: /* identical here */
883 chstore(token, tokmax, c);
884 break;
885
886 case IN_OZONE:
887 goto byebye;
888 }
889 }
890 } /* end of main loop */
891
892 byebye:
893 token[_p_tokpos] = 0; /* make sure token ends with EOS */
894
895 return 0;
896 }