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 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731
|
/**
\file util.c
\brief Contains all-purpose tables and functions.
This file contains all general tables (or const variables) and functions definitions
that are need as general tools.
*/
#include "util.h"
#include "iptc.h"
#include <stdlib.h>
#include <stdio.h>
#ifndef OS_WIN32
#ifdef __GNUC__
#include <unistd.h>
#endif
#endif
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
extern struct prefs_struct prefs; /**< preferences struct defined in main.c */
/** \var op_labels
\brief Description table for enum OP values.
This table implements all data for all enum OP values and their relevant
label structures.
*/
struct op_label op_labels[]=
{
{ OP_CHECK, "check", "just check embedded data structures" },
{ OP_DUMP, "dump", "show embedded data (headers only)" },
{ OP_DUMP_FULL, "dump-full", "show all embedded data (including values)" },
{ OP_DUMP_VALUE, "dump-value", "show requested value (headers only)" },
{ OP_FILTER, "filter", "filter embedded data (see -i, -e and --edit)" },
/** \TODO
- purge (iptc, exif, .., *),
- merge (picture file+iptc/exif/photoshop file)
- filter/extract (exif)
*/
/* reserved */
{ OP_EOT, NULL, NULL }
};
/** \var log_levels
\brief Description table for enum log_level values.
This table implements all data for all enum log_level values and their relevant
description structures.
*/
struct log_level_descr log_levels[]=
{
{ LOG_LEVEL_QUIET, "quiet, no output at all" },
{ LOG_LEVEL_ERRORS, "error messages only (default)" },
{ LOG_LEVEL_WARNINGS, "warning and error messages" },
{ LOG_LEVEL_INFO, "informative, warning and error messages" },
{ LOG_LEVEL_DEBUG, "all messages including debug ones" },
/* reserved */
{ LOG_LEVEL_EOT, NULL }
};
/** \var exit_code_labels
\brief Description table for enum EXIT_CODE values.
This table implements all data for all enum EXIT_CODE values and their relevant
label structures.
*/
struct exit_code_label exit_code_labels[]=
{
{ EXIT_CODE_NORMAL, "normal exit" },
{ EXIT_CODE_USAGE_ERROR, "usage error" },
{ EXIT_CODE_ASYNCHRONOUS_SIGNAL, "asynchronous signal termination" },
{ EXIT_CODE_NORMAL_WITH_WARNINGS, "normal exit, with warning(s)" },
{ EXIT_CODE_NORMAL_WITH_ERRORS, "normal exit, with error(s)" },
{ EXIT_CODE_FATAL_ERROR, "fatal error encountered" },
/* reserved */
{ EXIT_EOT, NULL }
};
/** \var rewrite_cache
\brief Write cache buffer.
Pointer to write cache buffer (to be allocated and freed).
*/
/** \var rewrite_cache_offset
\brief Write cache current offset.
The offset is increasing as bytes are stored in the cache. It starts at 0.
*/
/** \var rewrite_cache_size
\brief Write cache size.
Size of the write cache. Usually equal to the input file size. Does not
increase dynamically.
*/
/** \var output_file
\brief Output file handler.
Pointer to the output file handler.
*/
/** \var output_filename
\brief Output filename.
*/
/** \var input_length
\brief Input file length.
*/
unsigned char* rewrite_cache;
unsigned long int rewrite_cache_offset;
unsigned long int rewrite_cache_size;
FILE* output_file;
char* output_filename;
size_t input_length;
/** \brief Get a file size.
\param[in] file: pointer to an file handler (it must have been fopen()'ed).
\return The file length or 0 if an error occurred.
*/
size_t fsize(FILE* file)
{
size_t tmp;
fseek(file, 0, SEEK_END);
tmp=(size_t)ftell(file);
fseek(file, 0, SEEK_SET);
return(tmp);
}
/** \brief Check if a file exists (whatever its type is).
\param[in] filename: filename to check if it exists.
\return True if filename exists, otherwise returns false.
*/
flag fexist(const char* filename)
{
struct stat st;
if (stat(filename, &st)==0)
return(vrai);
return(faux);
}
/** \brief Rename source_filename to target_filename.
\param[in] source_filename: original filename to rename.
\param[in] target_filename: new filename.
\return 0 if the file could be renamed, otherwise return an int (err).
Rename a file. Tries first to remove the destination file if it already
exists. This function doesn't interpret the return code, it's up to the
caller to check if it's 0 (OK) or non-0 (NOK). But if the removal of any
already existing target file fails, it will show an error.
*/
int frename(const char* source_filename, const char* target_filename)
{
int err;
if (fexist(target_filename))
{
#ifdef OS_WIN32
err=remove(target_filename);
if (err!=0)
error("frename: couldn't remove '%s' (error #%d)",
target_filename, err);
#else
err=unlink(target_filename);
if (err!=0)
error("frename: couldn't remove '%s' (error #%d)",
target_filename, err);
#endif
}
err=rename(source_filename, target_filename);
return(err);
}
/** \brief Check if a file is a regular file.
\param[in] filename: file to check if it's a regular file.
\return True if filename is a regular file, otherwise returns false.
*/
flag fisfile(const char* filename)
{
struct stat st;
if (stat(filename, &st)<0)
return(faux);
if (S_ISREG(st.st_mode))
return(vrai);
return(faux);
}
/** \brief Check if a file is to a link.
\param[in] filename: file to check if it's a link.
\return True if filename is a link, otherwise returns false.
*/
flag fislink(const char* filename)
{
struct stat st;
if (stat(filename, &st)<0)
return(faux);
if (S_ISLNK(st.st_mode))
return(vrai);
return(faux);
}
/** \brief Check if a file is a directory.
\param[in] filename: file to check if it's a directory.
\return True if filename is a directory, otherwise returns false.
*/
flag fisdir(const char* filename)
{
struct stat st;
if (stat(filename, &st)<0)
return(faux);
if (S_ISDIR(st.st_mode))
return(vrai);
return(faux);
}
/** \brief Get a file's extension.
\param[in] path: file path/name to look for an extension into.
\return A pointer to the 1st char of the file extension (if none, pointer to \\0).
Return a newly allocated string with the file extension if any
(or "" if none). it's up to the caller to free it using returned pointer.
*/
char* fextension(const char* path)
{
char* extension;
const char* filename;
int dot;
int sep;
/* keep the filename only */
sep=strrpos(path, DIR_SEPARATOR_CHAR);
if (sep==0)
filename=path;
else
filename=path+sep;
dot=strrpos(filename, '.');
if (dot==0)
extension=(char*)strdup("");
else
extension=(char*)strdup(filename+dot);
return(extension);
}
/** \brief Get a file's base name (no path, no extension).
\param[in] path: file path/name to look for a base name into.
\return A pointer to the 1st char of the file base name (if none, pointer to \\0).
Return a newly allocated string with the file base name (the filename
itself, with no path nor extension) if any (or "" if none). it's up to
the caller to free it using returned pointer.
*/
char* fbasename(const char* path)
{
char* basename;
const char* filename;
int dot;
int sep;
/* keep the filename only */
sep=strrpos(path, DIR_SEPARATOR_CHAR);
if (sep==0)
filename=path;
else
filename=path+sep;
dot=strrpos(filename, '.');
if (dot==0)
basename=(char*)strdup(filename);
else
basename=(char*)strndup(filename, dot-1);
return(basename);
}
/** \brief Get a file's base name (no path).
\param[in] path: file path/name to look for a filename into.
\return A pointer to the 1st char of the file name (if none, pointer to \\0).
Return a newly allocated string with the file name (with no path) if any
(or "" if none). it's up to the caller to free it using returned pointer.
*/
char* ffilename(const char* path)
{
char* filename;
int sep;
sep=strrpos(path, DIR_SEPARATOR_CHAR);
if (sep==0)
filename=(char*)strdup(path);
else
filename=(char*)strdup(path+sep);
return(filename);
}
/** \brief Get a file's base name (no filename, only the path remains).
\param[in] path: file path/name to look for a dir name into.
\return A pointer to the 1st char of the dir name (if none, pointer to \\0).
Return a newly allocated string with the dirname (everything but the
filename) if any (or "" if none). it's up to the caller to free the
returned pointer.
*/
char* fdirname(const char* path)
{
char* dirname;
int sep;
sep=strrpos(path, DIR_SEPARATOR_CHAR);
if (sep==0)
dirname=(char*)strdup("");
else
dirname=(char*)strndup(path, sep-1);
return(dirname);
}
/** \brief Get a cleaned-up pathname string (ensure that the path string is canonical).
\param[in] path: pathname to clean
\return A pointer to the 1st char of the dir name (if none, pointer to \\0).
Return a newly allocated string with the clean path (no duplicate or
trailing /). It's up to the caller to free the returned pointer.
*/
char* fcleanpath(const char* path)
{
const char* ptr;
size_t len;
/* skip trailing slashes */
len=strlen(path);
ptr=path+len-1;
while ((ptr>=path) && (*ptr==DIR_SEPARATOR_CHAR))
{
ptr--;
len--;
}
return((char*)strndup(path, len));
}
/** \brief Create a backup (.bak) of a file. Any already existing backup file will be removed.
\param[in] filename File name to backup.
\return Nothing.
Create a backup file by renaming the filename to a .bak one. Any already
existing .bak file gets removed. Errors (non-fatal for file I/O) are
managed here.
*/
void backup_file(const char* filename)
{
char* backup_filename;
int err;
/* build the backup filename */
err=0;
backup_filename=malloc((strlen(filename)+1+4)*sizeof(char));
if (!backup_filename)
fatal_error("backup_file: couldn't allocate space for backup filename");
strcpy(backup_filename, filename);
strcat(backup_filename, ".bak");
debug("backing up '%s' to '%s'", filename, backup_filename);
/* remove any already existing backup file */
if (fisfile(backup_filename) || fislink(backup_filename))
{
err=remove(backup_filename);
if (err!=0)
error("backup_file: couldn't remove previous backup file '%s' (error #%d)",
backup_filename, err);
}
/* rename the original file to back it up */
if (fisfile(filename) || fislink(backup_filename))
{
err=frename(filename, backup_filename);
if (err!=0)
error("backup_file: couldn't create backup '%s' from '%s' (error #%d)",
backup_filename, filename, err);
}
if (err==0)
info("'%s' file created", backup_filename);
/* free resources */
free(backup_filename);
backup_filename=NULL;
}
/** \brief
\param
\return
TODO
*/
unsigned char getbyte(unsigned char* ptr, unsigned long int offset)
{
return(ptr[offset]);
}
/** \brief
\param
\return
TODO
*/
unsigned short int getword(unsigned char* ptr, unsigned long int offset)
{
return((ptr[offset]<<8)|ptr[offset+1]);
}
/** \brief
\param
\return
TODO
*/
unsigned short int getrawword(unsigned char* ptr, unsigned long int offset)
{
return((ptr[offset+1]<<8)|ptr[offset]);
}
/** \brief
\param
\return
TODO
*/
unsigned long int getlong(unsigned char* ptr, unsigned long int offset)
{
return((((ptr[offset]<<8)|ptr[offset+1])<<16)|((ptr[offset+2]<<8)|ptr[offset+3]));
}
/** \brief
\param
\return
TODO
*/
unsigned long int getrawlong(unsigned char* ptr, unsigned long int offset)
{
return((((ptr[offset+3]<<8)|ptr[offset+2])<<16)|((ptr[offset+1]<<8)|ptr[offset]));
}
/** \brief
\param
\return
TODO
*/
unsigned char* getstring(unsigned char* ptr, unsigned long int offset, unsigned char* buf,
const unsigned long int len)
{
register unsigned int i;
for (i=0; i<len; i++)
buf[i]=ptr[offset+i];
buf[i]='\0';
return(buf);
}
/** \brief
\param
\return
TODO
*/
unsigned char* getraw(unsigned char* ptr, unsigned long int offset, unsigned char* buf,
const unsigned long int len)
{
memcpy(buf, ptr+offset, len*sizeof(unsigned char));
return(buf);
}
/** \brief
\param
\return
TODO
*/
void setbyte(unsigned char* ptr, unsigned long int offset, unsigned char byte)
{
ptr[offset]=byte;
}
/** \brief
\param
\return
TODO
*/
void setword(unsigned char* ptr, unsigned long int offset, unsigned short int word)
{
ptr[offset]=(word&0xff00)>>8;
ptr[offset+1]=word&0x00ff;
}
/** \brief
\param
\return
TODO
*/
void setlong(unsigned char* ptr, unsigned long int offset, unsigned long int dword)
{
ptr[offset]=(dword&0xff000000UL)>>24;
ptr[offset+1]=(dword&0x00ff0000UL)>>16;
ptr[offset+2]=(dword&0x0000ff00UL)>>8;
ptr[offset+3]=dword&0x000000ffUL;
}
/** \brief
\param
\return
TODO
*/
void message_fatal_error(char* format, va_list args)
{
if (prefs.log_level>=LOG_LEVEL_ERRORS)
{
context_print_text();
prefs.context_errors++;
prefs.errors++;
fprintf(stderr, "error: ");
vfprintf(stderr, format, args);
fprintf(stderr, "\nfatal error encountered, bailing out\n");
context_print_info();
}
_exit(EXIT_CODE_FATAL_ERROR);
}
/** \brief
\param
\return
TODO
*/
void message_error(char* format, va_list args)
{
if (prefs.log_level>=LOG_LEVEL_ERRORS)
{
context_print_text();
prefs.context_errors++;
prefs.errors++;
fprintf(stderr, "error: ");
vfprintf(stderr, format, args);
fprintf(stderr, "\n");
}
}
/** \brief
\param
\return
TODO
*/
void message_warning(char* format, va_list args)
{
if (prefs.log_level>=LOG_LEVEL_WARNINGS)
{
context_print_text();
prefs.context_warnings++;
prefs.warnings++;
fprintf(stderr, "warning: ");
vfprintf(stderr, format, args);
fprintf(stderr, "\n");
}
}
/** \brief
\param
\return
TODO
*/
void message_info(char* format, va_list args)
{
if (prefs.log_level>=LOG_LEVEL_INFO)
{
fprintf(stdout, "info: ");
vfprintf(stdout, format, args);
fprintf(stdout, "\n");
fflush(stdout);
}
}
/** \brief
\param
\return
TODO
*/
void message_debug(char* format, va_list args)
{
if (prefs.log_level>=LOG_LEVEL_DEBUG)
{
fprintf(stdout, "debug: ");
vfprintf(stdout, format, args);
fprintf(stdout, "\n");
fflush(stdout);
}
}
/** \brief
\param
\return
TODO
*/
void fatal_error(char* format, ...)
{
va_list args;
va_start(args, format);
message_fatal_error(format, args);
va_end(args);
}
/** \brief
\param
\return
TODO
*/
void error(char* format, ...)
{
va_list args;
va_start(args, format);
message_error(format, args);
va_end(args);
}
/** \brief
\param
\return
TODO
*/
void warning(char* format, ...)
{
va_list args;
va_start(args, format);
message_warning(format, args);
va_end(args);
}
/** \brief
\param
\return
TODO
*/
void info(char* format, ...)
{
va_list args;
va_start(args, format);
message_info(format, args);
va_end(args);
}
/** \brief
\param
\return
TODO
*/
void debug(char* format, ...)
{
va_list args;
va_start(args, format);
message_debug(format, args);
va_end(args);
}
/** \brief
\param
\return
TODO
*/
void message(enum SEVERITY severity, char* format, ...)
{
va_list args;
va_start(args, format);
switch (severity)
{
case SEV_FATAL_ERROR:
message_fatal_error(format, args);
break;
case SEV_ERROR:
message_error(format, args);
break;
case SEV_WARNING:
message_warning(format, args);
break;
case SEV_INFO:
message_info(format, args);
break;
case SEV_DEBUG:
message_debug(format, args);
break;
}
va_end(args);
}
/** \brief
\param
\return
TODO
*/
void context_set_text(char* text)
{
prefs.context_text=text;
}
/** \brief
\param
\return
TODO
*/
void context_print_text(void)
{
/* show context information when first warning/error is encounter */
if (prefs.context_text)
if ((prefs.context_warnings+prefs.context_errors)==0)
fprintf(stderr, "in file '%s':\n", prefs.context_text);
}
/** \brief
\param
\return
TODO
*/
void context_print_info(void)
{
if ((prefs.context_warnings+prefs.context_errors)>0)
fprintf(stderr, "%lu warning(s), %lu error(s)\n",
prefs.context_warnings, prefs.context_errors);
if (prefs.fix && (prefs.context_fixes>0))
fprintf(stderr, "%lu fix(es)\n",
prefs.fixes);
}
/** \brief
\param
\return
TODO
*/
void context_reset(void)
{
prefs.context_text=NULL;
prefs.context_warnings=0;
prefs.context_errors=0;
prefs.context_fixes=0;
}
/** \brief
\param
\return
TODO
*/
struct op_label* op_match_label(char* label)
{
register int i;
i=0;
while (op_labels[i].op!=OP_EOT)
{
if (strcmp(op_labels[i].label, label)==0)
return(&op_labels[i]);
i++;
}
return(NULL);
}
/** \brief
\param
\return
TODO
*/
void init_rewrite(void)
{
char* filename;
size_t len;
int fd;
FILE* file;
if (!prefs.rewrite)
fatal_error("init_rewrite: rewrite flag not set");
if (output_file)
fatal_error("init_rewrite: bad output file handler");
if (!output_filename)
fatal_error("init_rewrite: bad output filename");
/* get a temporary filename */
len=strlen(output_filename);
filename=malloc((len+1+6+1)*sizeof(char));
if (!filename)
fatal_error("init_rewrite: couldn't allocate space for init_rewrite");
strcpy(filename, output_filename);
strcat(filename, ".XXXXXX");
fd=mkstemp(filename);
if (fd<0)
fatal_error("init_rewrite: couldn't create a temporary file from '%s'", output_filename);
/* open file for writing */
file=fdopen(fd, "w+b");
if (!file)
{
close(fd);
fatal_error("init_rewrite: couldn't open temporary file '%s' for writing", output_filename);
}
/* write back values to caller */
output_file=file;
free(output_filename); /* free previous allocation (from caller) */
output_filename=(char*)strdup(filename);
free(filename);
/* cached rewriting */
if (prefs.rewrite_cached)
{
rewrite_cache=malloc(input_length*sizeof(char));
if (!rewrite_cache)
fatal_error("init_rewrite: couldn't allocate space for rewrite cache (%lu byte(s))",
rewrite_cache_size);
rewrite_cache_offset=0;
rewrite_cache_size=input_length;
}
}
/** \brief
\param
\return
TODO
*/
size_t dump_rewrite(unsigned char* buffer, unsigned long int length)
/* returns the number of bytes written */
{
size_t len;
if (!prefs.rewrite)
fatal_error("init_rewrite: rewrite flag not set");
debug("dump_rewrite: writing %lu byte(s)", length);
len=0;
if (length>0)
{
if (prefs.rewrite_cached)
{
if (rewrite_cache_offset+length>rewrite_cache_size)
{
/* reallocate more space for cached writing */
rewrite_cache_size+=length+1024;
rewrite_cache=realloc(rewrite_cache, rewrite_cache_size);
if (!rewrite_cache)
fatal_error("dump_rewrite: couldn't rellocate more space for rewrite_cache (%lu byte(s))",
rewrite_cache_size);
}
/* write bytes to cache */
memcpy(rewrite_cache+rewrite_cache_offset, buffer, length);
rewrite_cache_offset+=length;
len=length;
}
else
{
/* dump bytes to file */
len=fwrite(buffer, 1, length, output_file);
if (len!=length)
error("dump_rewrite: error %d while attempting to write %lu byte(s) (%lu written)",
errno, length, len);
}
}
return(len);
}
/** \brief
\param
\return
TODO
*/
void post_rewrite(unsigned char* buffer, struct parser_result* result)
{
unsigned long int len;
if (!prefs.rewrite)
fatal_error("post_rewrite: rewrite flag not set");
if (!result)
fatal_error("post_rewrite: bad pointer to struct result");
/* write remaining bytes */
if (input_length>result->parsed_bytes)
{
len=input_length-result->parsed_bytes;
/*
if (result->parsed_bytes!=result->written_bytes)
len-=(result->parsed_bytes-result->written_bytes);
*/
}
else
len=0;
if (prefs.rewrite_cached)
{
if (len>0)
{
debug("post_rewrite: writing remaining %lu byte(s) to cache", len);
memcpy(rewrite_cache+rewrite_cache_offset, buffer+result->parsed_bytes, len);
rewrite_cache_offset+=len;
}
/* flush cache to disk */
len=fwrite(rewrite_cache, 1, rewrite_cache_offset, output_file);
if (len!=rewrite_cache_offset)
error("post_rewrite: error %d while attempting to flush %lu cached bytes (%lu written)",
errno, rewrite_cache_offset, len);
if (rewrite_cache_offset>input_length)
info("rewritten file is %lu byte(s) bigger than the original",
rewrite_cache_offset-input_length);
else
if (rewrite_cache_offset<input_length)
info("rewritten file is %lu byte(s) smaller than the original",
input_length-rewrite_cache_offset);
else
info("rewritten file and original file have the same size");
}
else
if (len>0)
{
debug("post_rewrite: writing remaining %lu byte(s)", len);
dump_rewrite(buffer+result->parsed_bytes, len);
}
/* deallocate resource */
free(rewrite_cache);
rewrite_cache=NULL;
}
/** \brief
\param
\return
TODO
*/
flag get_unsigned_value(const char* param, unsigned long int* var)
{
unsigned int value;
char* chk;
value=(unsigned int) strtoul(param, &chk, 10);
if (!*chk)
{
*var=value;
return(vrai);
}
return(faux);
}
/** \brief
\param
\return
TODO
*/
flag get_signed_value(const char* param, long int* var)
{
int value;
char* chk;
value=(int)strtol(param, &chk, 10);
if (!*chk)
{
*var=value;
return(vrai);
}
return(faux);
}
/** \brief
\param
\return
TODO
*/
flag get_hexa_value(const char* param, unsigned long int* var)
{
unsigned int value;
char* chk;
value=strtoul(param, &chk, 16);
if (!*chk)
{
*var=value;
return(vrai);
}
return(faux);
}
/** \brief
\param
\return
TODO
*/
size_t strpos(const char* string, const char c)
{
char* ptr;
ptr=(char*)string;
while (*ptr)
if (*ptr==c)
return((size_t)(ptr-string+1));
else
ptr++;
return(0);
}
/** \brief
\param
\return
TODO
*/
size_t strrpos(const char* string, const char c)
{
char* ptr;
ptr=(char*)string+strlen(string)-1;
while (ptr>=string)
if (*ptr==c)
return((size_t)(ptr-string+1));
else
ptr--;
return(0);
}
/** \brief
\param
\return
TODO
*/
void filenames_list_load(char*** filenames_list_ptr, int* filenames_count_ptr, FILE* list_file)
{
char** filenames_list;
char* buffer;
ssize_t len;
unsigned long int filenames_count;
size_t size;
int copy_limit;
/* allocate a minimum amount of copied-lines */
copy_limit=256;
filenames_list=(char**)malloc(copy_limit*sizeof(char*));
if (!filenames_list)
fatal_error("filenames_list_load: couldn't allocate more space for filenames list (%lu byte(s))",
copy_limit*sizeof(char*));
filenames_count=0;
buffer=NULL;
size=32*1024; /* should match any MAX_PATH */
while (((len=getline(&buffer, &size, list_file))!=-1) && buffer)
{
char* ptr;
char* p;
ptr=buffer;
/* skip leading non printable chars if any */
while (*ptr && (*ptr<32))
ptr++;
/* skip trailing non printable chars if any */
p=buffer+len-1;
while ((p>=ptr) && (*p<32))
*p--='\0';
debug("filenames_list_load: line %lu, %lu byte(s)", filenames_count, len);
if (*buffer)
{
/* dynamically realloc copied-lines buffer */
if ((int)filenames_count>=copy_limit)
{
char** ptr;
copy_limit+=256;
ptr=(char**)realloc(filenames_list, copy_limit*sizeof(char*));
if (!ptr)
{
free(filenames_list);
fatal_error("filenames_list_load: couldn't rellocate more space for filenames list (%lu byte(s))",
copy_limit*sizeof(char*));
}
filenames_list=ptr;
}
/* duplicate the filename */
filenames_list[filenames_count]=strndup(buffer, strlen(buffer)+1);
if (!filenames_list[filenames_count])
fatal_error("filenames_list_load: couldn't allocate space for filenames list contents (%lu byte(s))",
(strlen(buffer)+1)*sizeof(char));
filenames_count++;
}
}
if (buffer)
free(buffer);
*filenames_list_ptr=filenames_list;
*filenames_count_ptr=filenames_count;
}
/** \brief
\param
\return
TODO
*/
flag filter_parse_line(char* buffer, size_t len, unsigned long int line)
{
char *ptr;
char* p;
int errors;
errors=0;
ptr=buffer;
/* skip leading non printable chars and whitespaces if any */
while (*ptr && (*ptr<=32))
ptr++;
/* skip trailing non printable chars and whitespaces if any */
p=buffer+len-1;
while ((p>=ptr) && (*p<=32))
*p--='\0';
/* ignore empty lines as well as comments */
if (!*ptr || (*ptr=='#'))
debug("filter_parse_file: ignore blank line or comment at line %lu", line);
else
{
/* IPTC filter masks decoding */
if (strncmp(ptr, "IPTC.", 5)==0)
{
/* possible IPTC filter masks:
IPTC.* all
IPTC.0xhhhh record+dataset (4-digit hexadecimal)
IPTC.0xhhhh-0xhhhh record+dataset range (4-digit hexadecimals)
IPTC.nnn:nnn record+dataset (1-3 digits per decimal value, each value [0-255])
IPTC.nnn:* all datasets from record nnn (1-3 digits per decimal value, each value [0-255])
IPTC.nnn:nnn-nnn record and dataset range (1-3 digits per decimal value, each value [0-255])
for instance:
IPTC.*
IPTC.0x0219
IPTC.0x0200-0x07ff
IPTC.2:*
IPTC.2:25
IPTC.2:0-255
*/
debug("filter_parse_line: found IPTC filter item");
ptr+=5;
if (strcmp(ptr, "*")==0)
iptc_filter_add_range(0x0000, 0xffff);
else
if (strncmp(ptr, "0x", 2)==0)
{
/* hexadecimal */
unsigned long int code;
unsigned long int code2;
p=strstr(ptr, "-0x");
if (p)
{
/* range value */
debug("filter_parse_line: hexadecimal range at line %lu", line);
*p='\0';
/* skip the range separator */
p++;
if ((strlen(ptr)==6) && get_hexa_value(ptr, &code)
&& (strlen(p)==6) && get_hexa_value(p, &code2))
iptc_filter_add_range(code, code2);
else
{
errors++;
error("filter_parse_line: bad hexadecimal range (0xhhhh-0xhhhh) at line %lu",
line);
}
}
else
{
/* unary value */
debug("filter_parse_line: hexadecimal unary value at line %lu", line);
if ((strlen(ptr)==6) && get_hexa_value(ptr, &code))
iptc_filter_add_value(code);
else
{
errors++;
error("filter_parse_line: bad hexadecimal unary value (0xhhhh) at line %lu",
line);
}
}
}
else
{
/* decimal duplet */
unsigned long int record;
unsigned long int dataset;
unsigned long int dataset2;
int pos;
pos=strpos(ptr, ':');
if (pos)
{
debug("filter_parse_line: decimal value at line %lu", line);
ptr[pos-1]='\0';
p=ptr+pos;
/* record value */
if (get_unsigned_value(ptr, &record) && (record<=0xff))
{
if (strcmp(p, "*")==0)
iptc_filter_add_range((unsigned short int)(record<<8),
(unsigned short int)(record<<8)|0xff);
else
{
/* range value */
ptr=p;
pos=strpos(ptr, '-');
if (pos)
{
ptr[pos-1]='\0';
p=ptr+pos;
if (get_unsigned_value(ptr, &dataset) && (dataset<=0xff)
&& get_unsigned_value(p, &dataset2) && (dataset2<=0xff))
iptc_filter_add_range((unsigned short int)(record<<8)|dataset,
(unsigned short int)(record<<8)|dataset2);
else
{
errors++;
error("filter_parse_line: bad range value in decimal duplet (nnn-nnn) at line %lu",
line);
}
}
else
{
/* unary value */
if (get_unsigned_value(p, &dataset) && (dataset<0xff))
iptc_filter_add_value((unsigned short int)((record<<8)|dataset));
else
{
errors++;
error("filter_parse_line: bad dataset value in decimal duplet (nnn) at line %lu",
line);
}
}
}
}
else
{
errors++;
error("filter_parse_line: bad record value in decimal duplet (nnn) at line %lu",
line);
}
}
else
{
errors++;
error("filter_parse_line: bad decimal duplet value (nnn:nnn) at line %lu: %s",
line, ptr);
}
}
}
/* else
if (strncmp(buffer, "Exif.", 5))
{
}
else
if (strncmp(buffer, "Adobe.", 6))
{
}
*/ else
{
errors++;
error("filter_parse_line: invalid filter file contents at line %lu (%lu byte(s))",
line, len);
debug("filter_parse_line: line is: %s", buffer);
}
}
return((errors==0));
}
/** \brief
\param
\return
TODO
*/
void filter_load_line(char* buffer)
{
ssize_t len;
if (!buffer)
error("filter_load_line: bad pointer to buffer");
if (!*buffer)
error("filter_load_line: empty buffer");
iptc_filter_table_init();
len=strlen(buffer);
if (!filter_parse_line(buffer, len, 0))
fatal_error("filter_load_line: bad filter line");
}
/** \brief
\param
\return
TODO
*/
void filter_load_file(FILE* list_file)
{
ssize_t len;
unsigned long int line;
unsigned long int errors;
char* buffer;
size_t size;
if (!list_file)
error("filter_load_file: bad input file handler");
iptc_filter_table_init();
line=0;
errors=0;
buffer=NULL;
size=100;
while (((len=getline(&buffer, &size, list_file))!=-1) && buffer)
{
buffer[len]='\0';
debug("filter_load_file: line %lu, %lu byte(s)", line, len);
if (!filter_parse_line(buffer, len, line))
errors++;
line++;
}
if (buffer)
free(buffer);
debug("filter_load_file: %lu line(s), %lu error(s)", line, errors);
if (errors)
fatal_error("filter_load_file: bad filter file");
}
/** \brief
\param
\return
TODO
*/
flag edit_parse_line(const char* buffer, size_t len, unsigned long int line)
/* strdup/strlen/etc. functions can't be used there, as it's possible to get '\0' char in the
value part of the edit expression (if it contains widechars). all length calculation
accross the whole line of the value part must be based upon 'len'. */
{
const char* const_ptr;
char* buffer_copy;
char *ptr;
char* p;
size_t buffer_length;
int errors;
errors=0;
/* skip trailing cr/lf chars */
buffer_length=len;
const_ptr=buffer+buffer_length-1;
while ((const_ptr>=buffer) && ((*const_ptr=='\n') || (*const_ptr=='\r')))
{
buffer_length--;
const_ptr++;
}
buffer_length++; /* count the trailing '\0' */
/* work on a copy of the buffer (we'll need to write '\0' chars to work on sub-expressions) */
buffer_copy=malloc(buffer_length);
if (!buffer_copy)
fatal_error("edit_parse_line: couldn't allocate buffer_copy for value");
memcpy(buffer_copy, buffer, buffer_length);
buffer_copy[buffer_length-1]='\0';
ptr=buffer_copy;
/* skip leading non printable chars and whitespaces if any */
while (*ptr && (*ptr<=32))
ptr++;
/* ignore empty lines as well as comments */
if (!*ptr || (*ptr=='#'))
debug("edit_parse_file: ignore blank line or comment at line %lu", line);
else
{
/* IPTC edit (add/replace) expression decoding */
if (strncmp(ptr, "IPTC.", 5)==0)
{
/* possible IPTC edit masks:
IPTC.0xhhhh=<type>:<value> record+dataset (4-digit hexadecimal)
IPTC.nnn:nnn=<type>:<value> record+dataset (1-3 digits per decimal value, each value [0-255])
where <type> can be hex or text
where <value> is either a text: ASCII 7-bit string, trailing spaces matter
or a set of hexadecimal digit pairs (matching regex ([0-9a-fA-F]{2})+)
for instance:
IPTC.0x0219=text:keyword1
IPTC.0x0219=hex:
IPTC.2:25=text:keyword1
IPTC.2:25
*/
int pos;
debug("edit_parse_line: found IPTC edit item");
ptr+=5;
pos=strpos(ptr, '=');
if (pos)
{
/* decode type and value */
ptr[pos-1]='\0';
p=ptr+pos;
pos=strpos(p, ':');
if (pos)
{
enum FILTER_EDIT_VALUE_TYPE type=FEV_NONE;
char* typename;
char* value;
char* stored_value=NULL;
size_t stored_value_length=0;
flag stored_value_in_use=faux;
/* get type */
p[pos-1]='\0';
typename=p;
if (strcmp(typename, "hex")==0)
type=FEV_HEX;
else
if (strcmp(typename, "text")==0)
type=FEV_TEXT;
else
{
errors++;
error("edit_parse_line: bad value type '%s' at line %lu, expected 'hex' or 'text'",
p, line);
}
if (errors)
goto edit_parse_line_abort;
/* get value */
value=p+pos;
stored_value_length=buffer_length-(value-buffer_copy)-1; /* don't count the trailing '\0' */
/* check the value according to its type and store/encore it to binary */
if (stored_value_length==0)
{
errors++;
error("edit_parse_line: empty %s value at line %lu",
typename, line);
}
if (type==FEV_HEX)
{
char* hex;
char one_byte[3];
unsigned long int byte_val;
char* bin;
if (stored_value_length%2)
{
errors++;
error("edit_parse_line: invalid odd length (%lu byte(s)) for hex value '%s' at line %lu",
stored_value_length, value, line);
}
hex=value;
while (*hex &&
( (('a'<=*hex) && (*hex<='f'))
|| (('A'<=*hex) && (*hex<='F'))
|| (('0'<=*hex) && (*hex<='9'))))
hex++;
if (*hex!='\0')
{
errors++;
error("edit_parse_line: invalid hex value '%s' at line %lu",
value, line);
}
stored_value_length/=2;
stored_value=malloc(stored_value_length);
if (!stored_value)
fatal_error("edit_parse_line: couldn't allocate storage buffer for hex value");
/* encode hex string value to binary */
one_byte[2]=0;
hex=value;
bin=stored_value;
while (*hex)
{
*one_byte=*hex;
one_byte[1]=hex[1];
if (get_hexa_value(one_byte, &byte_val))
*bin=(unsigned char)byte_val;
hex+=2;
bin++;
}
}
else
if (type==FEV_TEXT)
{
stored_value=malloc(stored_value_length);
if (!stored_value)
fatal_error("edit_parse_line: couldn't allocate storage buffer for text value");
memcpy(stored_value, value, stored_value_length);
}
if (errors)
goto edit_parse_line_abort;
/* decode the record+dataset part */
if (strncmp(ptr, "0x", 2)==0)
{
/* hexadecimal */
unsigned long int code;
/* unary value */
debug("edit_parse_line: hexadecimal unary value at line %lu", line);
if ((strlen(ptr)==6) && get_hexa_value(ptr, &code))
iptc_edit_add_edit(code, stored_value_length,
(unsigned char*)stored_value,
&stored_value_in_use);
else
{
errors++;
error("edit_parse_line: bad hexadecimal unary value (0xhhhh) at line %lu: %s",
line, ptr);
}
}
else
{
/* decimal duplet */
unsigned long int record;
unsigned long int dataset;
int pos;
pos=strpos(ptr, ':');
if (pos)
{
debug("edit_parse_line: decimal value at line %lu", line);
ptr[pos-1]='\0';
p=ptr+pos;
/* record value */
if (get_unsigned_value(ptr, &record) && (record<=0xff))
{
/* unary value */
if (get_unsigned_value(p, &dataset) && (dataset<0xff))
iptc_edit_add_edit((unsigned short int)((record<<8)|dataset),
stored_value_length, (unsigned char*)stored_value,
&stored_value_in_use);
else
{
errors++;
error("edit_parse_line: bad dataset value in decimal duplet (nnn) at line %lu: %s",
line, p);
}
}
else
{
errors++;
error("edit_parse_line: bad record value in decimal duplet (nnn) at line %lu: %s",
line, ptr);
}
}
else
{
errors++;
error("edit_parse_line: bad decimal duplet value (nnn:nnn) at line %lu: %s",
line, ptr);
}
}
edit_parse_line_abort:
if (!stored_value_in_use)
{
free(stored_value);
stored_value=NULL;
}
}
else
{
errors++;
error("edit_parse_line: bad value sub-expression in edit expression at line %lu: %s",
line, ptr);
}
}
else
{
errors++;
error("edit_parse_line: invalid edit expression at line %lu: %s",
line, ptr);
}
}
/* else
if (strncmp(buffer, "Exif.", 5))
{
}
else
if (strncmp(buffer, "Adobe.", 6))
{
}
*/ else
{
errors++;
error("edit_parse_line: invalid edit file contents at line %lu (%lu byte(s))",
line, buffer_length);
debug("edit_parse_line: line is: %s", buffer);
}
}
free(buffer_copy);
return((errors==0));
}
/** \brief
\param
\return
TODO
*/
void edit_load_line(const char* buffer)
{
ssize_t len;
if (!buffer)
error("edit_load_line: bad pointer to buffer");
if (!*buffer)
error("edit_load_line: empty buffer");
iptc_edit_list_init();
/* FIXME: strlen should not be used there (buffer might contain widechars -> possibly '\0' chars) */
len=strlen(buffer);
if (!edit_parse_line(buffer, len, 0))
fatal_error("edit_load_line: bad edit line");
}
/** \brief
\param
\return
TODO
*/
void edit_load_file(FILE* list_file)
{
ssize_t len;
unsigned long int line;
unsigned long int errors;
char* buffer;
size_t size;
if (!list_file)
error("edit_load_file: bad input file handler");
iptc_edit_list_init();
line=0;
errors=0;
buffer=NULL;
size=100;
while (((len=getline(&buffer, &size, list_file))!=-1) && buffer)
{
buffer[len]='\0';
debug("edit_load_file: line %lu, %lu byte(s)", line, len);
if (!edit_parse_line(buffer, len, line))
errors++;
line++;
}
if (buffer)
free(buffer);
debug("edit_load_file: %lu line(s), %lu error(s)", line, errors);
if (errors)
fatal_error("edit_load_file: bad edit file");
}
/** \brief
\param
\return
TODO
*/
void dump_hexa(unsigned char* value, unsigned long int length)
{
unsigned long int i;
printf("[%lu byte(s)]:", length);
if (prefs.wrap_dump>0)
printf("\n");
else
printf(" ");
for (i=1; i<=length; i++)
{
printf("%02x ", value[i-1]);
if (prefs.wrap_dump>0)
/* append a newline char each 32 hexabyte printed */
if ((i%prefs.wrap_dump)==0)
printf("\n");
}
if (prefs.wrap_dump>0)
{
if (length!=prefs.wrap_dump) /* newline if not just appended */
printf("\n");
}
else
printf("\n");
}
|