1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209
|
#include <stdlib.h>
#include <stdio.h>
#if defined(OS_UNIX) || defined(OS_LINUX)
#include <unistd.h>
#endif
#include <errno.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#ifndef PRODUCT_ID
#error PRODUCT_ID must be defined
#endif
#ifndef PRODUCT_VERSION
#error PRODUCT_VERSION must be defined
#endif
#include "jfif.h"
#include "iptc.h"
#include "exif.h"
#include "util.h"
#include "adobe.h"
extern struct op_label op_labels[];
extern struct log_level_descr log_levels[];
extern struct exit_code_label exit_code_labels[];
extern unsigned char* rewrite_cache;
extern unsigned long int rewrite_cache_offset;
extern unsigned long int rewrite_cache_size;
extern FILE* output_file;
extern char* output_filename;
extern size_t input_length;
extern unsigned char* iptc_extract_buffer;
extern unsigned long int iptc_extract_buffer_size;
/* global vars */
struct prefs_struct prefs;
char* program_name;
char* default_iptc_extension="iptc";
/*char* default_iptc_extension="exv";*/
void version(void)
{
printf(PRODUCT_ID " " PRODUCT_VERSION "\n");
exit(EXIT_CODE_NORMAL);
}
void usage(enum EXIT_CODE ret)
{
char spaces[19];
int i;
/* command-line usage */
printf("usage:\n");
printf(" %s [<option> ..] <op> [<expr>] <file> [<file> ..]\n", program_name);
printf(" %s [<option> ..] <op> [<expr>] @<file>\n", program_name);
printf("usage of form <op> <expr> .. only concerns op mode dump-value, see below.\n");
printf("\n");
printf("iuse @<file> to read a list of files from that file. the file must contain exactly\n");
printf("one filename to process per line (don't escape anything, filenames are taken as-is).\n");
/* various general or specific options */
printf("\n");
printf("general options:\n");
printf(" -v --version show version number then exit\n");
printf(" -h --help show this help then exit\n");
printf(" --list list all supported embedded data structures\n");
printf(" -l --log-level <num> define verbosity (see possible values below)\n");
printf(" -c --check-compliance perform full checks to test the strict validity and\n");
printf(" compliance of structures to official formats\n");
printf("\n");
printf("filtering and extracting options:\n");
printf(" -b --backup create backup files of (re)written files if necessary\n");
printf(" -d --target-dir <dir> target directory for all written files\n");
printf(" -f --fix fix metadata inconsistencies when possible\n");
printf("\n");
printf("extracting options:\n");
printf(" -x --extract <type> extract and save metadata to a standalone file (see\n");
printf(" below for a list of metadata that can be extracted)\n");
printf(" -t --ext <extension> define what filename extension to use when writing\n");
printf(" metadata that is extracted (default: .%s)\n",
default_iptc_extension);
/* printf(" metadata that is extracted (default: .%s or .%s)\n",
default_iptc_extension, default_exif_extension);*/
printf(" -a --append-ext append extension to original filename when saving\n");
printf(" the extracted metadata (default: replace original file\n");
printf(" extension)\n");
printf("\n");
printf("filtering options:\n");
printf(" -i --include <expr> a filter expression describing the datasets to keep\n");
printf(" -i --include @<file> or a file containing a list of datasets to keep,\n");
printf(" all other datasets will be filtered out.\n");
printf(" if not used, all datasets will be kept\n");
printf(" -e --exclude <expr> a filter expression describing the datasets to filter out\n");
printf(" -e --exclude @<file> or a file containing a list of datasets to filter out,\n");
printf(" all other datasets will be kept. if not used all\n");
printf(" datasets will be kept\n");
printf(" --edit <expr> a filter edit expression describing the datasets to add\n");
printf(" --edit @<file> or a file containing a list of datasets to add.\n");
printf(" datasets won't be inserted if that breaks compliance\n");
printf(" to the standards\n");
printf(" --test don't override original file(s), create *.rewrite file(s)\n");
printf(" instead\n");
/* printf(" -o --overwrite allow overwriting already existing field values in embedded\n");
printf(" data (with --edit only). if -o is not used, attempting to\n");
printf(" replace an existing value will fail with a warning\n"); */
/* TODO: add a switch to force applying record size restrictions (pad or crop) */
printf("-i and -e can't be mixed together. --edit can be used in conjonction to -i or -e,\n");
printf("in that case, the edits will be processed after the include or exclude filter.\n");
printf("\n");
printf("dump options:\n");
printf(" -w --wrap <num> max column for wrapping (num must be in range\n");
printf(" [%d-%d]). default is to dump unwrapped\n",
MIN_WRAP_DUMP, MAX_WRAP_DUMP);
/* supported operations */
printf("\n");
printf("possible ops:\n");
i=0;
while (op_labels[i].op!=OP_EOT)
{
int j;
for (j=0; j<(int)(18-strlen(op_labels[i].label)); j++)
spaces[j]=' ';
spaces[j]='\0';
printf(" %s%s%s\n", op_labels[i].label, spaces, op_labels[i].description);
i++;
}
/* dump-value expressions */
printf("\n");
printf("dump-value expressions:\n");
printf(" IPTC.<record>\n");
printf("same as filter include/exclude expression, see below:\n");
/* filter expressions */
printf("\n");
printf("filter include/exclude expressions:\n");
printf(" IPTC.<record>\n");
printf("where <record> can be either:\n");
printf(" all: *\n");
printf(" hexadecimal unary: 0xhhhh\n");
printf(" hexadecimal range: 0xhhhh-0xhhhh\n");
printf(" decimal unary: nnn:nnn\n");
printf(" decimal ranges: nnn:nnn-nnn\n");
printf(" nnn:*\n");
printf("examples:\n");
printf(" IPTC.0x0219\n");
printf(" IPTC.0x0300-0x0364\n");
printf(" IPTC.3:0-100\n");
/* filter edit expressions */
printf("\n");
printf("filter edit expressions:\n");
printf(" IPTC.<record>=<type>:<value>\n");
printf("where <record> can be either:\n");
printf(" hexadecimal unary: 0xhhhh\n");
printf(" decimal unary: nnn:nnn\n");
printf("where <type> can be either:\n");
printf(" hex\n");
printf(" text\n");
printf("and <value> is:\n");
printf(" hex: [a-zA-Z0-9] pairs\n");
printf(" text: any char (even widechars) on the line is taken as text, until\n");
printf(" a newline is found\n");
printf("examples:\n");
printf(" IPTC.0x0200=hex:0002\n");
printf(" IPTC.0x0219=text:this is a keyword\n");
/* metadata extraction */
printf("\n");
printf("supported metadata for extraction:\n");
printf(" iptc IPTC datasets\n");
/* printf(" exif Exif datasets\n");*/
/* verbosity (log) levels */
printf("\n");
printf("possible log levels:\n");
i=0;
while (log_levels[i].level!=LOG_LEVEL_EOT)
{
printf(" %2d %s\n", i, log_levels[i].description);
i++;
}
/* supported file formats */
printf("\n");
printf("supported input files:\n");
printf(" JPEG files (commonly .jpeg, .jpg, .jpe, .jfif, .jif)\n");
printf(" Adobe Photoshop files (commonly .psd, .pdd, .ffo)\n");
printf(" FotoStation files (commonly .fdp, .ipt)\n");
printf(" standalone IPTC metadata (commonly .iptc)\n");
/* printf(" standalone Exif metadata (commonly .exv)\n");*/
/* exit codes */
printf("\n");
printf("exit codes:\n");
i=0;
while (exit_code_labels[i].code!=EXIT_EOT)
{
printf(" %d %s\n", exit_code_labels[i].code, exit_code_labels[i].label);
i++;
}
exit(ret);
}
void signal_handler(int sig)
{
fprintf(stderr, "signal %d received\n", sig);
fflush(stderr);
_exit(EXIT_CODE_ASYNCHRONOUS_SIGNAL);
}
int main(int argc, char** argv)
{
char** filenames_list;
unsigned char tmp[7]; /* max length of file signature sequence + 1 */
struct parser_result result;
FILE* input_file;
unsigned char* buffer;
char* ptr;
char* input_filename;
char* include_expression;
char* exclude_expression;
char* edit_expression;
char* extract_extension;
char* target_dirname;
char* iptc_filename;
char* op;
char* arg;
char* dump_value;
size_t ret;
enum EXIT_CODE exit_code;
int filenames_count;
int i;
/* signal re-hooking */
#ifdef SIGHUP
signal(SIGHUP, signal_handler); /* some unix window manager's logout */
#endif
#ifdef SIGQUIT
signal(SIGQUIT, signal_handler);
#endif
#ifdef SIGUSR1
signal(SIGUSR1, signal_handler);
#endif
#ifdef SIGINT
signal(SIGINT, signal_handler);
#endif
#ifdef SIGTERM
signal(SIGTERM, signal_handler);
#endif
/* variables init */
prefs.log_level=LOG_LEVEL_WARNINGS;
prefs.technical=false;
prefs.warnings=0;
prefs.errors=0;
prefs.fixes=0;
prefs.wrap_dump=DEFAULT_WRAP_DUMP;
prefs.rewrite=false;
prefs.rewrite_create_backup=false;
prefs.filter_mode=FILTER_NONE;
prefs.test_rewrite=false;
#ifdef ALWAYS_CACHE_REWRITE
prefs.rewrite_cached=true;
#else
prefs.rewrite_cached=false;
#endif
prefs.extract_iptc=false;
prefs.extract_extension_append=false;
prefs.fix=false;
/* prefs.edit_overwrite=false; */
context_reset();
input_filename=NULL;
output_filename=NULL;
iptc_filename=NULL;
include_expression=NULL;
exclude_expression=NULL;
edit_expression=NULL;
extract_extension=NULL;
target_dirname=NULL;
input_file=NULL;
output_file=NULL;
rewrite_cache=NULL;
rewrite_cache_offset=0;
rewrite_cache_size=0;
iptc_extract_buffer=NULL;
iptc_extract_buffer_size=0;
buffer=NULL;
filenames_list=NULL;
filenames_count=0;
dump_value=NULL;
#ifdef OS_WIN32
program_name=*argv;
#else
/* strip pathname from argv[0] */
ptr=*argv;
ptr+=strlen(ptr)-1;
while ((ptr>*argv))
if (*ptr==DIR_SEPARATOR_CHAR)
{
/* make ptr to point to the first char of the filename itself */
ptr++;
break;
}
else
--ptr;
program_name=ptr;
#endif
/* parse command line options */
argc--;
for (i=1; i<=argc; i++)
{
arg=argv[i];
if ((strcmp(arg, "--help")==0) || (strcmp(arg, "-h")==0))
usage(0);
else
if ((strcmp(arg, "--version")==0) || (strcmp(arg, "-v")==0))
version();
else
if (strcmp(arg, "--list")==0)
{
iptc_dump_list();
exit(EXIT_CODE_NORMAL);
}
else
if ((strcmp(arg, "--log-level")==0) || (strcmp(arg, "-l")==0))
{
if (i<argc)
{
unsigned long int value;
i++;
if (get_unsigned_value(argv[i], &value))
{
if (((int)value<MIN_LOG_LEVEL) || ((int)value>MAX_LOG_LEVEL))
{
error("invalid log level value %d, not in range [%d-%d]",
value, MIN_LOG_LEVEL, MAX_LOG_LEVEL);
usage(EXIT_CODE_USAGE_ERROR);
}
prefs.log_level=(int)value;
}
else
{
error("invalid value for option '%s': %s", arg, argv[i]);
usage(EXIT_CODE_USAGE_ERROR);
}
}
else
{
error("missing value to argument '%s'", arg);
usage(EXIT_CODE_USAGE_ERROR);
}
}
else
if ((strcmp(arg, "--check-compliance")==0) || (strcmp(arg, "-c")==0))
prefs.technical=true;
else
if ((strcmp(arg, "--extract")==0) || (strcmp(arg, "-x")==0))
{
if (i<argc)
{
i++;
if (strcasecmp(argv[i], "iptc")==0)
prefs.extract_iptc=true;
else
{
error("unsupported metadata type to argument '%s'", arg);
usage(EXIT_CODE_USAGE_ERROR);
}
}
else
{
error("missing metadata type to argument '%s'", arg);
usage(EXIT_CODE_USAGE_ERROR);
}
}
else
if ((strcmp(arg, "--backup")==0) || (strcmp(arg, "-b")==0))
prefs.rewrite_create_backup=true;
else
if ((strcmp(arg, "--target-dir")==0) || (strcmp(arg, "-d")==0))
{
if (i<argc)
{
i++;
target_dirname=fcleanpath(argv[i]);
}
else
{
error("missing dirname to argument '%s'", arg);
usage(EXIT_CODE_USAGE_ERROR);
}
}
else
if ((strcmp(arg, "--fix")==0) || (strcmp(arg, "-f")==0))
prefs.fix=true;
else
if ((strcmp(arg, "--ext")==0) || (strcmp(arg, "-t")==0))
{
if (i<argc)
{
i++;
extract_extension=argv[i];
}
else
{
error("missing extension to argument '%s'", arg);
usage(EXIT_CODE_USAGE_ERROR);
}
}
else
if ((strcmp(arg, "--append-ext")==0) || (strcmp(arg, "-a")==0))
prefs.extract_extension_append=true;
else
if ((strcmp(arg, "--include")==0) || (strcmp(arg, "-i")==0))
{
if (i<argc)
{
i++;
include_expression=argv[i];
}
else
{
error("missing filename or expression to argument '%s'", arg);
usage(EXIT_CODE_USAGE_ERROR);
}
}
else
if ((strcmp(arg, "--exclude")==0) || (strcmp(arg, "-e")==0))
{
if (i<argc)
{
i++;
exclude_expression=argv[i];
}
else
{
error("missing filename or expression to argument '%s'", arg);
usage(EXIT_CODE_USAGE_ERROR);
}
}
else
if (strcmp(arg, "--edit")==0)
{
if (i<argc)
{
i++;
edit_expression=argv[i];
}
else
{
error("missing filename or expression to argument '%s'", arg);
usage(EXIT_CODE_USAGE_ERROR);
}
}
else
if (strcmp(arg, "--test")==0)
prefs.test_rewrite=true;
else
if ((strcmp(arg, "--wrap")==0) || (strcmp(arg, "-w")==0))
{
if (i<argc)
{
unsigned long int value;
i++;
if (get_unsigned_value(argv[i], &value))
{
if (((int)value<MIN_WRAP_DUMP) || ((int)value>MAX_WRAP_DUMP))
{
error("invalid wrap dump value %d, not in range [%d-%d]",
value, MIN_WRAP_DUMP, MAX_WRAP_DUMP);
usage(EXIT_CODE_USAGE_ERROR);
}
prefs.wrap_dump=(unsigned short int)value;
}
else
{
error("invalid value for option '%s': %s", arg, argv[i]);
usage(EXIT_CODE_USAGE_ERROR);
}
}
else
{
error("missing filename to argument '%s'", arg);
usage(EXIT_CODE_USAGE_ERROR);
}
}
/* else
if ((strcmp(arg, "--overwrite")==0) || (strcmp(arg, "-o")==0))
prefs.edit_overwrite=true; */
else
if ((strcmp(arg, "--")==0))
break;
else
if (*arg=='-')
{
error("unrecognized option '%s'", arg);
usage(EXIT_CODE_USAGE_ERROR);
}
else
break;
}
/* get mode */
filenames_list=argv;
filenames_count=argc;
if ((argc-i+1)>=2) /* 2 parameters or more */
{
struct op_label* label;
op=argv[i++];
label=op_match_label(op);
if (!label)
{
error("unknown op mode '%s'", op);
usage(EXIT_CODE_USAGE_ERROR);
}
prefs.op=label->op;
info("operating mode: %s", op);
if (prefs.op==OP_FILTER)
prefs.rewrite=true;
/* extra parameter (value) for dump-value mode */
if (prefs.op==OP_DUMP_VALUE)
{
if ((argc-i+1)>=2)
{
dump_value=argv[i++];
/* setup filters (even if we don't rewrite/filter */
filter_load_line(dump_value);
prefs.filter_mode|=FILTER_INCLUDE;
}
else
{
error("missing parameter(s) for dump-value mode");
usage(EXIT_CODE_USAGE_ERROR);
}
}
/* detect @<file> pattern */
if (*argv[i]=='@')
{
FILE* list_file;
char* filename;
/* any further file in command-line will be ignored */
if ((argc-i+1)>1)
warning("ignoring extra parameters in command-line after @<file>");
/* open filenames list file */
filename=argv[i];
filename++;
list_file=fopen(filename, "r");
if (!list_file)
fatal_error("could not open file '%s'", filename);
/* load the filenames list from file */
filenames_list=NULL;
filenames_count=0;
filenames_list_load(&filenames_list, &filenames_count, list_file);
filenames_count--; /* if 0 filenames -> count will be set to -1 */
i=0;
/* free local tmp resource */
fclose(list_file);
}
}
else
{
error("missing parameter(s)");
usage(EXIT_CODE_USAGE_ERROR);
}
/* latest verifications around the passed options coherence */
if (prefs.fix && (!prefs.extract_iptc && !prefs.rewrite))
{
error("nothing to fix if not extracting or filtering");
usage(EXIT_CODE_USAGE_ERROR);
}
if (prefs.rewrite_create_backup && (!prefs.extract_iptc && !prefs.rewrite))
{
error("nothing to backup if not extracting or filtering");
usage(EXIT_CODE_USAGE_ERROR);
}
if (prefs.test_rewrite && !prefs.rewrite)
{
error("test rewrite not possible if not filtering");
usage(EXIT_CODE_USAGE_ERROR);
}
if (extract_extension && !prefs.extract_iptc)
{
error("nothing to do with extension '%s' when not extracting",
extract_extension);
usage(EXIT_CODE_USAGE_ERROR);
}
if (prefs.extract_extension_append && !prefs.extract_iptc)
{
error("no extension to append when not extracting");
usage(EXIT_CODE_USAGE_ERROR);
}
if (include_expression && exclude_expression)
{
if (prefs.op!=OP_FILTER)
error("don't know what to do with extraneous filename or expression for operating mode %s",
op_labels[prefs.op].label);
else
error("cannot mix include and exclude filenames or expressions at the same time");
usage(EXIT_CODE_USAGE_ERROR);
}
/* some adjustments according to passed options */
if (prefs.extract_iptc && !extract_extension)
/* default file extension to assume when extracting IPTC chunk */
extract_extension=default_iptc_extension;
if ((prefs.extract_iptc || prefs.rewrite) && target_dirname)
{
/* check if target dir does exist */
if (!fexist(target_dirname))
{
int err;
/* create the missing target dir */
err=mkdir(target_dirname, 0x1ff);
if (err<0)
fatal_error("couldn't create target directory '%s' (error #%d)",
target_dirname, errno);
}
else
if (!fisdir(target_dirname))
fatal_error("'%s' doesn't seem to be a directory", target_dirname);
}
/* load include/exclude/edit files for filtering */
if (include_expression)
{
prefs.filter_mode|=FILTER_INCLUDE;
if (*include_expression=='@')
{
FILE* include_file;
char* include_filename;
include_filename=include_expression+1;
include_file=fopen(include_filename, "r");
if (!include_file)
fatal_error("could not open file '%s'", include_filename);
debug("loading include file");
filter_load_file(include_file);
fclose(include_file);
}
else
{
debug("evaluating include filter expression");
filter_load_line(include_expression);
}
#if 0
iptc_filter_table_dump();
#endif
/* always cache rewriting */
prefs.rewrite_cached=true;
}
else
if (exclude_expression)
{
prefs.filter_mode|=FILTER_EXCLUDE;
if (*exclude_expression=='@')
{
FILE* exclude_file;
char* exclude_filename;
exclude_filename=exclude_expression+1;
exclude_file=fopen(exclude_filename, "r");
if (!exclude_file)
fatal_error("could not open file '%s'", exclude_filename);
debug("loading exclude file");
filter_load_file(exclude_file);
fclose(exclude_file);
}
else
{
debug("evaluating exclude filter expression");
filter_load_line(exclude_expression);
}
#if 0
iptc_filter_table_dump();
#endif
/* force cached rewriting when some filters are set */
if (iptc_filters()>0)
prefs.rewrite_cached=true;
}
if (edit_expression)
{
prefs.filter_mode|=FILTER_EDIT;
if (*edit_expression=='@')
{
FILE* edit_file;
char* edit_filename;
edit_filename=edit_expression+1;
edit_file=fopen(edit_filename, "r");
if (!edit_file)
fatal_error("could not open file '%s'", edit_filename);
debug("loading edit file");
edit_load_file(edit_file);
fclose(edit_file);
}
else
{
debug("evaluating edit filter expression");
edit_load_line(edit_expression);
}
#if 1
iptc_edit_list_dump();
#endif
/* force cached rewriting when some edits are set */
if (iptc_edits()>0)
prefs.rewrite_cached=true;
}
if (prefs.op==OP_FILTER)
{
if (prefs.filter_mode==FILTER_NONE)
error("unexpected filter mode: none (not set)");
else
info("filter mode:%s%s%s",
(prefs.filter_mode&FILTER_EXCLUDE)?" exclude":"",
(prefs.filter_mode&FILTER_INCLUDE)?" include":"",
(prefs.filter_mode&FILTER_EDIT)?" edit":"");
}
while (i<=filenames_count)
{
bool proceed;
proceed=true;
/* reset rewrite flag according to the current mode (it might has
been discarded for previous file */
if (prefs.op==OP_FILTER)
prefs.rewrite=true;
if (prefs.extract_iptc)
{
iptc_extract_buffer=NULL;
iptc_extract_buffer_size=0;
}
/* open the input file */
input_filename=filenames_list[i];
context_reset();
context_set_text(input_filename);
input_file=fopen(input_filename, "rb");
if (!input_file)
{
error("could not open file '%s'", input_filename);
i++;
context_print_info();
continue;
}
/* output filename will be based on the input filename */
if (prefs.rewrite)
{
if (target_dirname)
{
char* pathname;
char* filename;
size_t sz;
/* output file's path */
sz=0;
if (target_dirname)
pathname=strdup(target_dirname);
else
pathname=fdirname(input_filename);
if (pathname && *pathname)
sz+=strlen(pathname)+1;
/* output filename (get rid of any extension) */
filename=ffilename(input_filename);
sz+=strlen(filename);
sz++;
output_filename=malloc(sz*sizeof(char));
if (!output_filename)
fatal_error("couldn't allocate space for output filename");
*output_filename='\0';
if (pathname && *pathname)
{
strcat(output_filename, pathname);
strcat(output_filename, DIR_SEPARATOR_STRING);
}
strcat(output_filename, filename);
/* free resources */
free(filename);
filename=NULL;
free(pathname);
pathname=NULL;
}
else
output_filename=strdup(input_filename);
}
else
output_filename=NULL;
/* output filename for extracted IPTC data */
if (prefs.extract_iptc)
{
char* pathname;
char* filename;
size_t sz;
/* output file's path */
sz=0;
if (target_dirname)
pathname=strdup(target_dirname);
else
pathname=fdirname(input_filename);
if (pathname && *pathname)
sz+=strlen(pathname)+1;
/* output filename (get rid of any extension) */
if (prefs.extract_extension_append)
filename=ffilename(input_filename);
else
filename=fbasename(input_filename);
sz+=strlen(filename);
/* filename to store the IPTC chunk to */
sz+=1+strlen(extract_extension)+1;
iptc_filename=malloc(sz*sizeof(char));
if (!iptc_filename)
fatal_error("couldn't allocate space for IPTC chunk extraction filename");
*iptc_filename='\0';
if (pathname && *pathname)
{
strcat(iptc_filename, pathname);
strcat(iptc_filename, DIR_SEPARATOR_STRING);
}
strcat(iptc_filename, filename);
strcat(iptc_filename, ".");
strcat(iptc_filename, extract_extension);
free(filename);
filename=NULL;
free(pathname);
pathname=NULL;
}
/* check for concurrential write filenames */
if (prefs.extract_iptc && prefs.rewrite)
{
#ifdef OS_WIN32
if (strcasecmp(output_filename, iptc_filename)==0)
#else
if (strcmp(output_filename, iptc_filename)==0)
#endif
{
error("output filenames clash: filtering and extraction target files are identical (%s)\nthis would lead to unpredictable results",
output_filename);
proceed=false;
}
}
if (proceed)
{
/* read the whole file contents in a newly allocated buffer (TODO: optim this to use
less memory */
input_length=fsize(input_file);
buffer=malloc(input_length*sizeof(unsigned char));
if (!buffer)
fatal_error("could not allocate %lu bytes", input_length*sizeof(unsigned char));
ret=fread((void*)buffer, 1, input_length, input_file);
if ((ret!=input_length) && (ferror(input_file)<0))
error("read error #%d in file '%s', offset %lu: ret=%lu (expected=%lu)",
errno, input_filename, ret, input_length);
/* show input and output filenames */
info("input file: '%s'", input_filename);
if (prefs.rewrite)
info("output file: '%s'%s", output_filename,
fexist(output_filename)?" (already exists)":"");
if (prefs.extract_iptc)
info("IPTC extract output file: '%s'%s", iptc_filename,
fexist(iptc_filename)?" (already exists)":"");
result.parsed_bytes=0;
result.written_bytes=0;
result.ret=true;
if (input_length==0)
warning("empty file, skipped");
else
/* detect and parse contents */
if (strcmp((char*)getstring(buffer, 0, tmp, 5), GIF87_MARKER_STRING)==0)
{
debug("GIF87 marker found");
info("GIF87 contents expected (unsupported)");
prefs.rewrite=false;
}
else
if (strcmp((char*)getstring(buffer, 0, tmp, 5), GIF89_MARKER_STRING)==0)
{
debug("GIF89 marker found");
info("GIF89 contents expected (unsupported)");
prefs.rewrite=false;
}
else
if (strcmp((char*)getstring(buffer, 0, tmp, 6), EXIF_MARKER_STRING)==0)
{
debug("Exif marker found");
info("Exif contents expected");
exif_parse(buffer, input_length, &result);
if (!result.ret)
debug("Exif parser has reported error(s)");
}
else
if (getlong(buffer, 0)==ADOBE_8BPS_MARKER)
{
debug("8BPS marker found");
info("Adobe Photoshop contents expected");
if (prefs.rewrite)
init_rewrite();
adobe_8bps_parse(buffer, input_length, &result);
if (!result.ret)
debug("8BPS parser has reported error(s)");
}
else
if (getlong(buffer, 0)==ADOBE_8BIM_MARKER)
{
debug("8BIM marker found");
info("Adobe Photoshop contents expected");
/* it seems not possible make a distinction between the generic
8BIM format and the Photoshop 4.0's one (it's only possible if the 8BIM chunk is
embedded in a JFIF APP0 chunk)
*/
if (prefs.rewrite)
init_rewrite();
adobe_8bim_parse(buffer, input_length, 0, &result);
if (!result.ret)
debug("8BIM parser has reported error(s)");
}
else
if (getlong(buffer, 0)==TIFF_LITTLE_ENDIAN_MARKER_STRING)
{
debug("TIFF (little endian) marker found");
info("TIFF (little endian) contents expected (unsupported)");
prefs.rewrite=false;
}
else
if (getlong(buffer, 0)==TIFF_BIG_ENDIAN_MARKER_STRING)
{
debug("TIFF (big endian) marker found");
info("TIFF (big endian) contents expected (unsupported)");
prefs.rewrite=false;
}
else
if (getlong(buffer, 0)==PNG_MARKER_STRING)
{
debug("PNG marker found");
info("PNG contents expected (unsupported)");
prefs.rewrite=false;
}
else
if (strcmp((char*)getstring(buffer, 0, tmp, 4), DJVU_MARKER_STRING)==0)
{
debug("DJVU marker found");
info("DJVU contents expected (unsupported)");
prefs.rewrite=false;
}
else
if (getword(buffer, 0)==((JFIF_TAG<<8)|JFIF_SOI))
{
debug("SOI tag found");
info("JPEG/JFIF contents expected");
if (prefs.rewrite)
init_rewrite();
jfif_parse(buffer, input_length, &result);
if (!result.ret)
debug("JFIF parser has reported error(s)");
}
else
if (strcmp((char*)getstring(buffer, 0, tmp, 2), BMP_MARKER_STRING)==0)
{
debug("Windows Bitmap marker found");
info("Windows Bitmap contents expected (unsupported)");
prefs.rewrite=false;
}
else
if (getbyte(buffer, 0)==IPTC_MARKER)
{
debug("IPTC marker found");
info("IPTC contents expected");
if (prefs.rewrite)
init_rewrite();
iptc_parse(buffer, input_length, true, &result);
if (!result.ret)
debug("IPTC parser has reported error(s)");
}
else
if (getbyte(buffer, 0)==ADOBE_FFO_MARKER)
{
debug("FF0 marker found");
info("Adobe FF0 contents expected");
if (prefs.rewrite)
init_rewrite();
adobe_ffo_parse(buffer, input_length, true, &result);
if (!result.ret)
debug("FF0 parser has reported error(s)");
}
else
{
error("not a supported file (contents not recognized)");
prefs.rewrite=false;
}
if (result.parsed_bytes>input_length)
error("%lu byte(s) have been parsed whereas the file length is %lu",
result.parsed_bytes, input_length);
if (result.parsed_bytes<input_length)
debug("%lu remaining byte(s)", input_length-result.parsed_bytes);
/* close input file */
fclose(input_file);
input_file=NULL;
if (prefs.rewrite)
{
int err;
size_t len;
post_rewrite(buffer, &result);
if (!result.ret)
{
warning("at least one parser error has been encountered,\nwritten file might be corrupted (unreliable data)");
/* unlink(output_filename); */
}
len=fsize(output_file);
if (output_file)
{
fclose(output_file);
output_file=NULL;
}
/* rename the temp file to override the original filename */
if (prefs.test_rewrite)
{
char* rewrite_filename;
size_t len;
len=strlen(output_filename)-7; /* get rid of the trailing random chars from mkstemp */
rewrite_filename=malloc((len+1+7+1)*sizeof(char));
if (!rewrite_filename)
fatal_error("couldn't allocate space for rewrite filename");
strncpy(rewrite_filename, output_filename, len);
strncpy(rewrite_filename+len, ".rewrite", 1+7);
rewrite_filename[len+1+7]='\0';
/* create a backup of the target file by renaming it */
if (prefs.rewrite_create_backup)
backup_file(rewrite_filename);
err=frename(output_filename, rewrite_filename);
if (err!=0)
error("couldn't rename '%s' to '%s' (error #%d)",
output_filename, rewrite_filename, err);
free(rewrite_filename);
rewrite_filename=NULL;
}
else
{
char* target_filename;
/* get rid of the trailing random chars from mkstemp */
target_filename=strndup(output_filename, strlen(output_filename)-7);
/* create a backup of the target file (it can be the input filename itself if no
target dir has been set) by renaming it */
if (prefs.rewrite_create_backup)
backup_file(target_filename);
err=frename(output_filename, target_filename);
if (err!=0)
error("couldn't rename '%s' to '%s' (error #%d)",
output_filename, target_filename, err);
else
info("written %d byte(s) to file '%s'", len, target_filename);
free(target_filename);
target_filename=NULL;
}
}
if (prefs.extract_iptc)
{
if (iptc_extract_buffer)
{
FILE* iptc_file;
unsigned long int len;
if (prefs.rewrite_create_backup)
backup_file(iptc_filename);
/* store the extracted IPTC chunk to file */
iptc_file=fopen(iptc_filename, "w+b");
if (!iptc_file)
fatal_error("couldn't create file '%s' for IPTC chunk extraction",
iptc_filename);
len=fwrite(iptc_extract_buffer, 1, iptc_extract_buffer_size, iptc_file);
if (len!=iptc_extract_buffer_size)
error("error %d while attempting to save %lu byte(s) from the IPTC chunk (%lu written)",
errno, iptc_extract_buffer_size, len);
debug("IPTC chunk extracted and saved: %lu byte(s) to file '%s'",
len, iptc_filename);
info("written %d byte(s) to file '%s'", len, iptc_filename);
/* free resources */
fclose(iptc_file);
iptc_file=NULL;
free(iptc_extract_buffer);
iptc_extract_buffer=NULL;
iptc_extract_buffer_size=0;
}
else
error("unexpected uninitialized IPTC extract buffer");
}
/* free resources */
if (buffer)
{
free(buffer);
buffer=NULL;
}
}
/* free resources */
if (iptc_filename)
{
free(iptc_filename);
iptc_filename=NULL;
}
if (output_filename)
{
free(output_filename);
output_filename=NULL;
}
i++;
context_print_info();
}
/* free resources */
if (prefs.op==OP_DUMP_VALUE)
iptc_filter_table_reset();
else
if (prefs.rewrite)
{
iptc_filter_table_reset();
iptc_edit_list_reset();
}
if (target_dirname)
{
free(target_dirname);
target_dirname=NULL;
}
if ((filenames_list!=argv) && filenames_list)
{
for (i=0; i<=filenames_count; i++)
{
free(filenames_list[i]);
filenames_list[i]=NULL;
}
free(filenames_list);
filenames_list=NULL;
}
info("total: %lu warning(s), %lu error(s)", prefs.warnings, prefs.errors);
if (prefs.fix)
info("total: %lu fix(es)", prefs.fixes);
exit_code=EXIT_CODE_NORMAL;
if (prefs.errors)
exit_code=EXIT_CODE_NORMAL_WITH_ERRORS;
else
if (prefs.warnings)
exit_code=EXIT_CODE_NORMAL_WITH_WARNINGS;
exit(exit_code);
}
|