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
|
// =================================================================================================
// Copyright Adobe
// Copyright 2012 Adobe
// All Rights Reserved
//
// NOTICE: Adobe permits you to use, modify, and distribute this file in accordance with the terms
// of the Adobe license agreement accompanying it.
// =================================================================================================
#include "XMPFiles/source/FormatSupport/PostScript_Support.hpp"
#include "XMP.hpp"
#include <algorithm>
#include <climits>
// =================================================================================================
// PostScript_Support::HasCodesGT127
// =================================
//
// function to detect character codes greater than 127 in a string
bool PostScript_Support::HasCodesGT127(const std::string & value)
{
size_t vallen=value.length();
for (size_t index=0;index<vallen;index++)
{
if ((unsigned char)value[index]>127)
{
return true;
}
}
return false;
}
// =================================================================================================
// PostScript_Support::SkipTabsAndSpaces
// =====================================
//
// function moves the file pointer ahead such that it skips all tabs and spaces
bool PostScript_Support::SkipTabsAndSpaces(XMP_IO* file,IOBuffer& ioBuf)
{
while ( true )
{
if ( ! CheckFileSpace ( file, &ioBuf, 1 ) ) return false;
if ( ! IsSpaceOrTab ( *ioBuf.ptr ) ) break;
++ioBuf.ptr;
}
return true;
}
// =================================================================================================
// PostScript_Support::SkipUntilNewline
// ====================================
//
// function moves the file pointer ahead such that it skips all characters until a newline
bool PostScript_Support::SkipUntilNewline(XMP_IO* file,IOBuffer& ioBuf)
{
char ch;
do
{
if ( ! CheckFileSpace ( file, &ioBuf, 1 ) ) return false;
ch = *ioBuf.ptr;
++ioBuf.ptr;
} while ( ! IsNewline ( ch ) );
if (ch==kCR &&*ioBuf.ptr==kLF)
{
if ( ! CheckFileSpace ( file, &ioBuf, 1 ) ) return false;
++ioBuf.ptr;
}
return true;
}
// =================================================================================================
// RevRefillBuffer and RevCheckFileSpace
// ======================================
//
// These helpers are similar to RefillBuffer and CheckFileSpace with the difference that the it traverses
// the file stream in reverse order
void PostScript_Support::RevRefillBuffer ( XMP_IO* fileRef, IOBuffer* ioBuf )
{
// Refill including part of the current data, seek back to the new buffer origin and read.
size_t reverseSeek = ioBuf->limit - ioBuf->ptr;
if (ioBuf->filePos>kIOBufferSize)
{
ioBuf->filePos = fileRef->Seek ( -((XMP_Int64)(kIOBufferSize+reverseSeek)), kXMP_SeekFromCurrent );
ioBuf->len = fileRef->Read ( &ioBuf->data[0], kIOBufferSize );
ioBuf->ptr = &ioBuf->data[0]+ioBuf->len;
ioBuf->limit = ioBuf->ptr ;
}
else
{
XMP_Int64 rev = (ioBuf->ptr-&ioBuf->data[0]) + ioBuf->filePos;
ioBuf->filePos = fileRef->Seek ( 0, kXMP_SeekFromStart );
ioBuf->len = fileRef->Read ( &ioBuf->data[0], kIOBufferSize );
if ( rev > (XMP_Int64)ioBuf->len )throw XMP_Error ( kXMPErr_ExternalFailure, "Seek failure in FillBuffer" );
ioBuf->ptr = &ioBuf->data[0]+rev;
ioBuf->limit = &ioBuf->data[0]+ioBuf->len;
}
}
bool PostScript_Support::RevCheckFileSpace ( XMP_IO* fileRef, IOBuffer* ioBuf, size_t neededLen )
{
if ( size_t(ioBuf->ptr - &ioBuf->data[0]) < size_t(neededLen) )
{ // ! Avoid VS.Net compare warnings.
PostScript_Support::RevRefillBuffer ( fileRef, ioBuf );
}
return (size_t(ioBuf->ptr - &ioBuf->data[0]) >= size_t(neededLen));
}
// =================================================================================================
// SearchBBoxInTrailer
// ===================
//
// Function searches the Bounding Box in the comments after the %Trailer
// this function gets called when the DSC comment BoundingBox: value is
// (atend)
// returns true if atleast one BoundingBox: is found after %Trailer
inline static bool SearchBBoxInTrailer(XMP_IO* fileRef,IOBuffer& ioBuf)
{
bool bboxfoundintrailer=false;
if ( ! PostScript_Support::SkipTabsAndSpaces( fileRef, ioBuf ) ) return false;
if ( ! IsNewline ( *ioBuf.ptr ) ) return false;
++ioBuf.ptr;
// Scan for all the %%Trailer outside %%BeginDocument: & %%EndDocument comments
while(true)
{
if ( ! CheckFileSpace ( fileRef, &ioBuf, kPSContainsBeginDocString.length() ) ) return false;
if ( CheckBytes ( ioBuf.ptr, Uns8Ptr(kPSContainsTrailerString.c_str()), kPSContainsTrailerString.length() ))
{
//found %%Trailer now search for proper %%BoundingBox
ioBuf.ptr+=kPSContainsTrailerString.length();
//skip chars after %%Trailer till newline
if ( ! PostScript_Support::SkipUntilNewline( fileRef, ioBuf ) ) return false;
while(true)
{
if ( ! CheckFileSpace ( fileRef, &ioBuf, kPSContainsBBoxString.length() ) ) return false;
//check for "%%BoundingBox:"
if (CheckBytes ( ioBuf.ptr, Uns8Ptr(kPSContainsBBoxString.c_str()), kPSContainsBBoxString.length() ) )
{
//found "%%BoundingBox:"
ioBuf.ptr+=kPSContainsBBoxString.length();
// Skip leading spaces and tabs.
if ( ! PostScript_Support::SkipTabsAndSpaces( fileRef, ioBuf ) ) return false;
if ( IsNewline ( *ioBuf.ptr ) ) return false; // Reached the end of the %%BoundingBox comment.
bboxfoundintrailer=true;
break;
}
//skip chars till newline
if ( ! PostScript_Support::SkipUntilNewline( fileRef, ioBuf ) ) return false;
}
break;
}
else if ( CheckBytes ( ioBuf.ptr, Uns8Ptr(kPSContainsBeginDocString.c_str()), kPSContainsBeginDocString.length() ) )
{
//"%%BeginDocument:" Found search for "%%EndDocument"
ioBuf.ptr+=kPSContainsBeginDocString.length();
//skip chars after "%%BeginDocument:" till newline
if ( ! PostScript_Support::SkipUntilNewline( fileRef, ioBuf ) ) return false;
while(true)
{
if ( ! CheckFileSpace ( fileRef, &ioBuf, kPSContainsEndDocString.length() ) ) return false;
//check for "%%EndDocument"
if (CheckBytes ( ioBuf.ptr, Uns8Ptr(kPSContainsEndDocString.c_str()), kPSContainsEndDocString.length() ) )
{
//found "%%EndDocument"
ioBuf.ptr+=kPSContainsEndDocString.length();
break;
}
//skip chars till newline
if ( ! PostScript_Support::SkipUntilNewline( fileRef, ioBuf ) ) return false;
}// while to search %%EndDocument
} //else if to consume a pair of %%BeginDocument: and %%EndDocument
//skip chars till newline
if ( ! PostScript_Support::SkipUntilNewline( fileRef, ioBuf ) ) return false;
}// while for scanning %%BoundingBox after %%Trailer
if (!bboxfoundintrailer) return false;
return true;
}
// =================================================================================================
// PostScript_Support::IsValidPSFile
// =================================
//
// Determines if the file is a valid PostScript or EPS file
// Checks done
// Looks for a valid Poscript header
// For EPS file checks for a valid Bounding Box comment
bool PostScript_Support::IsValidPSFile(XMP_IO* fileRef,XMP_FileFormat &format)
{
IOBuffer ioBuf;
XMP_Int64 psOffset;
size_t psLength;
XMP_Uns32 fileheader,psMajorVer, psMinorVer,epsMajorVer,epsMinorVer;
char ch;
// Check for the binary EPSF preview header.
fileRef->Rewind();
if ( ! CheckFileSpace ( fileRef, &ioBuf, 4 ) ) return false;
fileheader = GetUns32BE ( ioBuf.ptr );
if ( fileheader == 0xC5D0D3C6 )
{
if ( ! CheckFileSpace ( fileRef, &ioBuf, 30 ) ) return false;
psOffset = GetUns32LE ( ioBuf.ptr+4 ); // PostScript offset.
psLength = GetUns32LE ( ioBuf.ptr+8 ); // PostScript length.
FillBuffer ( fileRef, psOffset, &ioBuf ); // Make sure buffer starts at psOffset for length check.
if ( (ioBuf.len < kIOBufferSize) && (ioBuf.len < psLength) ) return false; // Not enough PostScript.
}
// Check the start of the PostScript DSC header comment.
if ( ! CheckFileSpace ( fileRef, &ioBuf, (kPSFileTag.length() + 3 + 1) ) ) return false;
if ( ! CheckBytes ( ioBuf.ptr, Uns8Ptr(kPSFileTag.c_str()), kPSFileTag.length() ) ) return false;
ioBuf.ptr += kPSFileTag.length();
// Check the PostScript DSC major version number.
psMajorVer = 0;
while ( (ioBuf.ptr < ioBuf.limit) && IsNumeric( *ioBuf.ptr ) )
{
psMajorVer = (psMajorVer * 10) + (*ioBuf.ptr - '0');
if ( psMajorVer > 1000 ) return false; // Overflow.
ioBuf.ptr += 1;
}
if ( psMajorVer < 3 ) return false; // The version must be at least 3.0.
if ( ! CheckFileSpace ( fileRef, &ioBuf, 3 ) ) return false;
if ( *ioBuf.ptr != '.' ) return false; // No minor number.
ioBuf.ptr += 1;
// Check the PostScript DSC minor version number.
psMinorVer = 0;
while ( (ioBuf.ptr < ioBuf.limit) && IsNumeric( *ioBuf.ptr ) )
{
psMinorVer = (psMinorVer * 10) + (*ioBuf.ptr - '0');
if ( psMinorVer > 1000 ) return false; // Overflow.
ioBuf.ptr += 1;
}
switch( format )
{
case kXMP_PostScriptFile:
{
// Almost done for plain PostScript, check for whitespace.
if ( ! CheckFileSpace ( fileRef, &ioBuf, 1 ) ) return false;
if ( ! IsWhitespace(*ioBuf.ptr) ) return false;
ioBuf.ptr += 1;
break;
}
case kXMP_UnknownFile:
{
if ( ! SkipTabsAndSpaces ( fileRef, ioBuf ) ) return false;
if ( ! CheckFileSpace ( fileRef, &ioBuf, 5 ) ) return false;
// checked PS header to this point Atkleast a PostScript File
format=kXMP_PostScriptFile;
//return true if no "EPSF-" is found as it is a valid PS atleast
if ( ! CheckBytes ( ioBuf.ptr, Uns8Ptr("EPSF-"), 5 ) ) return true;
}//intentional fall through for further checking of unknown files
// fallthrough
case kXMP_EPSFile:
{
if ( ! SkipTabsAndSpaces ( fileRef, ioBuf ) ) return false;
// Check for the EPSF keyword on the header comment.
if ( ! CheckFileSpace ( fileRef, &ioBuf, 5+3+1 ) ) return false;
if ( ! CheckBytes ( ioBuf.ptr, Uns8Ptr("EPSF-"), 5 ) ) return false;
ioBuf.ptr += 5;
// Check the EPS major version number.
epsMajorVer = 0;
while ( (ioBuf.ptr < ioBuf.limit) &&IsNumeric( *ioBuf.ptr ) ) {
epsMajorVer = (epsMajorVer * 10) + (*ioBuf.ptr - '0');
if ( epsMajorVer > 1000 ) return false; // Overflow.
ioBuf.ptr += 1;
}
if ( epsMajorVer < 3 ) return false; // The version must be at least 3.0.
if ( ! CheckFileSpace ( fileRef, &ioBuf, 3 ) ) return false;
if ( *ioBuf.ptr != '.' ) return false; // No minor number.
ioBuf.ptr += 1;
// Check the EPS minor version number.
epsMinorVer = 0;
while ( (ioBuf.ptr < ioBuf.limit) && IsNumeric( *ioBuf.ptr ) ) {
epsMinorVer = (epsMinorVer * 10) + (*ioBuf.ptr - '0');
if ( epsMinorVer > 1000 ) return false; // Overflow.
ioBuf.ptr += 1;
}
if ( ! SkipTabsAndSpaces ( fileRef, ioBuf ) ) return false;
if ( ! CheckFileSpace ( fileRef, &ioBuf, 1 ) ) return false;
if ( ! IsNewline( *ioBuf.ptr ) ) return false;
ch=*ioBuf.ptr;
ioBuf.ptr += 1;
if (ch==kCR &&*ioBuf.ptr==kLF)
{
if ( ! CheckFileSpace ( fileRef, &ioBuf, 1 ) ) return false;
++ioBuf.ptr;
}
while ( true )
{
if ( ! CheckFileSpace ( fileRef, &ioBuf, kPSContainsBBoxString.length() ) ) return false;
if ( CheckBytes ( ioBuf.ptr, Uns8Ptr(kPSEndCommentString.c_str()), kPSEndCommentString.length() ) //explicit endcommentcheck
|| *ioBuf.ptr!='%' || !(*(ioBuf.ptr+1)>32 && *(ioBuf.ptr+1)<=126 )) // implicit endcomment check
{
// Found "%%EndComments", don't look any further.
return false;
}
else if ( ! CheckBytes ( ioBuf.ptr, Uns8Ptr(kPSContainsBBoxString.c_str()), kPSContainsBBoxString.length() ) )
{
// Not "%%EndComments" or "%%BoundingBox:", skip past the end of this line.
if ( ! SkipUntilNewline ( fileRef, ioBuf ) ) return true;
}
else
{
// Found "%%BoundingBox:", look for llx lly urx ury.
ioBuf.ptr += kPSContainsBBoxString.length();
//Check for atleast a mandatory space b/w "%%BoundingBox:" and llx lly urx ury
if ( ! CheckFileSpace ( fileRef, &ioBuf, 1 ) ) return false;
if ( ! IsSpaceOrTab ( *ioBuf.ptr ) ) return false;
ioBuf.ptr++;
while ( true )
{
// Skip leading spaces and tabs.
if ( ! SkipTabsAndSpaces( fileRef, ioBuf ) ) return false;
if ( IsNewline ( *ioBuf.ptr ) ) return false; // Reached the end of the %%BoundingBox comment.
//if the comment is %%BoundingBox: (atend) go past the %%Trailer to check BBox
bool bboxfoundintrailer=false;
if (*ioBuf.ptr=='(')
{
if ( ! CheckFileSpace ( fileRef, &ioBuf, kPSContainsAtendString.length() ) ) return false;
if ( CheckBytes ( ioBuf.ptr, Uns8Ptr(kPSContainsAtendString.c_str()), kPSContainsAtendString.length() ) )
{
// search for Bounding Box Past Trailer
ioBuf.ptr += kPSContainsAtendString.length();
bboxfoundintrailer=SearchBBoxInTrailer( fileRef, ioBuf );
}
if (!bboxfoundintrailer)
return false;
}//if (*ioBuf.ptr=='(')
int noOfIntegers=0;
// verifies for llx lly urx ury.
while(true)
{
if ( ! CheckFileSpace ( fileRef, &ioBuf, 1 ) ) return false;
if(IsPlusMinusSign(*ioBuf.ptr ))
++ioBuf.ptr;
bool atleastOneNumeric=false;
while ( true)
{
if ( ! CheckFileSpace ( fileRef, &ioBuf, 1 ) ) return false;
if (!IsNumeric ( *ioBuf.ptr ) ) break;
++ioBuf.ptr;
atleastOneNumeric=true;
}
if (!atleastOneNumeric) return false;
if ( ! SkipTabsAndSpaces( fileRef, ioBuf ) ) return false;
noOfIntegers++;
if ( IsNewline ( *ioBuf.ptr ) ) break;
}
if (noOfIntegers!=4)
return false;
format=kXMP_EPSFile;
return true;
}
} //Found "%%BoundingBox:"
} // Outer marker loop.
}
default:
{
return false;
}
}
return true;
}
// =================================================================================================
// PostScript_Support::IsSFDFilterUsed
// =================================
//
// Determines Whether the metadata is embedded using the Sub-FileDecode Approach or no
// In case of Sub-FileDecode filter approach the metaData can be easily extended without
// the need to inject a new XMP packet before the existing Packet.
//
bool PostScript_Support::IsSFDFilterUsed(XMP_IO* &fileRef, XMP_Int64 xpacketOffset)
{
IOBuffer ioBuf;
fileRef->Rewind();
fileRef->Seek((xpacketOffset/kIOBufferSize)*kIOBufferSize,kXMP_SeekFromStart);
if ( ! CheckFileSpace ( fileRef, &ioBuf,xpacketOffset%kIOBufferSize ) ) return false;
ioBuf.ptr+=(xpacketOffset%kIOBufferSize);
//skip white spaces
while(true)
{
if ( ! PostScript_Support::RevCheckFileSpace ( fileRef, &ioBuf, 1 ) ) return false;
if (!IsWhitespace(*ioBuf.ptr)) break;
--ioBuf.ptr;
}
std::string temp;
bool filterFound=false;
while(true)
{
if ( ! PostScript_Support::RevCheckFileSpace ( fileRef, &ioBuf, 1 ) ) return false;
if (*ioBuf.ptr==')')
{
--ioBuf.ptr;
while(true)
{
//get the string till '('
if ( ! PostScript_Support::RevCheckFileSpace ( fileRef, &ioBuf, 1 ) ) return false ;
temp+=*ioBuf.ptr;
--ioBuf.ptr;
if (*ioBuf.ptr=='(')
{
if(filterFound)
{
reverse(temp.begin(), temp.end());
if(!temp.compare("SubFileDecode"))
return true;
}
if ( ! PostScript_Support::RevCheckFileSpace ( fileRef, &ioBuf, 1 ) ) return false ;
--ioBuf.ptr;
temp.clear();
break;
}
}
filterFound=false;
}
else if(*ioBuf.ptr=='[')
{
//end of SubFileDecode Filter parsing
return false;
}
else if(*ioBuf.ptr=='k' )
{
if ( ! PostScript_Support::RevCheckFileSpace ( fileRef, &ioBuf, 4 ) ) return false;
if(IsWhitespace(*(ioBuf.ptr-4))&& *(ioBuf.ptr-3)=='m'
&& *(ioBuf.ptr-2)=='a' && *(ioBuf.ptr-1)=='r' )
//end of SubFileDecode Filter parsing
return false;
while(true)//ignore till any special mark
{
if ( ! PostScript_Support::RevCheckFileSpace ( fileRef, &ioBuf, 4 ) ) return false;
if (IsWhitespace(*(ioBuf.ptr))||*(ioBuf.ptr)=='['||
*(ioBuf.ptr)=='<' ||*(ioBuf.ptr)=='>') break;
--ioBuf.ptr;
}
filterFound=false;
}
else if(*ioBuf.ptr=='<')
{
--ioBuf.ptr;
if ( ! PostScript_Support::RevCheckFileSpace ( fileRef, &ioBuf, 1 ) ) return false;
if (*(ioBuf.ptr)=='<')
{
//end of SubFileDecode Filter parsing
return false;
}
while(true)//ignore till any special mark
{
if ( ! PostScript_Support::RevCheckFileSpace ( fileRef, &ioBuf, 4 ) ) return false;
if (IsWhitespace(*(ioBuf.ptr))||*(ioBuf.ptr)=='['||
*(ioBuf.ptr)=='<' ||*(ioBuf.ptr)=='>') break;
--ioBuf.ptr;
}
filterFound=false;
}
else if(*ioBuf.ptr=='>')
{
--ioBuf.ptr;
if ( ! PostScript_Support::RevCheckFileSpace ( fileRef, &ioBuf, 1 ) ) return false;
if (*(ioBuf.ptr)=='>')//ignore the dictionary
{
--ioBuf.ptr;
XMP_Int16 count=1;
while(true)
{
if ( ! PostScript_Support::RevCheckFileSpace ( fileRef, &ioBuf, 2 ) ) return false;
if(*(ioBuf.ptr)=='<' && *(ioBuf.ptr-1)=='<')
{
count--;
ioBuf.ptr-=2;
}
else if(*(ioBuf.ptr)=='>' && *(ioBuf.ptr-1)=='>')
{
count++;
ioBuf.ptr-=2;
}
else
{
ioBuf.ptr-=1;
}
if(count==0)
break;
}
}
filterFound=false;
}
else
{
while(true)
{
if ( ! PostScript_Support::RevCheckFileSpace ( fileRef, &ioBuf, 1 ) ) return false;
temp+=(*ioBuf.ptr);
--ioBuf.ptr;
if (*ioBuf.ptr=='/')
{
if(filterFound)
{
reverse(temp.begin(), temp.end());
if(!temp.compare("SubFileDecode"))
return true;
}
temp.clear();
filterFound=false;
break;
}
else if(IsWhitespace(*ioBuf.ptr))
{
reverse(temp.begin(), temp.end());
if(!temp.compare("filter")&&!filterFound)
filterFound=true;
else
filterFound=false;
temp.clear();
break;
}
}
if ( ! PostScript_Support::RevCheckFileSpace ( fileRef, &ioBuf, 1 ) ) return false;
--ioBuf.ptr;
}
while(true)
{
if ( ! PostScript_Support::RevCheckFileSpace ( fileRef, &ioBuf, 1 ) ) return false;
if (!IsWhitespace(*ioBuf.ptr)) break;
--ioBuf.ptr;
}
}
return false;
}
// =================================================================================================
// constructDateTime
// =================================
//
// If input string date is of the format D:YYYYMMDDHHmmSSOHH'mm' and valid
// output format YYYY-MM-DDThh:mm:ssTZD is returned
//
static void constructDateTime(const std::string &input,std::string& outDate)
{
std::string date;
XMP_DateTime datetime;
std::string verdate;
size_t start =0;
if(input[0]=='D' && input[1]==':')
start=2;
if (input.length() >=14+start)
{
for(int x=0;x<4;x++)
{
date+=input[start+x];//YYYY
}
date+='-';
start+=4;
for(int x=0;x<2;x++)
{
date+=input[start+x];//MM
}
date+='-';
start+=2;
for(int x=0;x<2;x++)
{
date+=input[start+x];//DD
}
date+='T';
start+=2;
for(int x=0;x<2;x++)
{
date+=input[start+x];//HH
}
date+=':';
start+=2;
for(int x=0;x<2;x++)
{
date+=input[start+x];//MM
}
date+=':';
start+=2;
for(int x=0;x<2;x++)
{
date+=input[start+x];//SS
}
start+=2;
if ((input[start]=='+' || input[start]=='-' )&&input.length() ==19+start)
{
date+=input[start];
start++;
for(int x=0;x<2;x++)
{
date+=input[start+x];//hh
}
date+=':';
start+=2;
for(int x=0;x<2;x++)
{
date+=input[start+x];//mm
}
}
else
{
date+='Z';
}
try
{
SXMPUtils::ConvertToDate ( date.c_str(),&datetime);
SXMPUtils::ConvertFromDate (datetime,&verdate);
}
catch(...)
{
return;
}
outDate=verdate;
}
}
// =================================================================================================
// GetNumber
// =========
//
// Extracts number from a char string
//
static short GetNumber(const char **inString,short noOfChars=SHRT_MAX)
{
const char* inStr=*inString;
short number=0;
while(IsNumeric ( *inStr )&& noOfChars--)
{
number=number*10 +inStr[0]-'0';
inStr++;
}
*inString=inStr;
return number;
}
// =================================================================================================
// tokeniseDateString
// ==================
//
// Parses Date string and tokenizes it for extracting different parts of a date
//
static bool tokeniseDateString(const char* inString,std::vector<PostScript_Support::DateTimeTokens> &tokens )
{
const char* inStr= inString;
PostScript_Support::DateTimeTokens dttoken;
//skip leading whitespace
while ( inStr[0]!='\0' )
{
while( IsSpaceOrTab ( *inStr ) || *inStr =='('
||*inStr ==')' ||*inStr==',')
{
++inStr;
}
if (*inStr=='\0') return true;
dttoken=PostScript_Support::DateTimeTokens();
if (IsNumeric(*inStr))
{
while(IsNumeric(*inStr)||(IsDelimiter(*inStr)&&dttoken.noOfDelimiter==0)||
(dttoken.delimiter==*inStr && dttoken.noOfDelimiter!=0))
{
if (IsDelimiter(*inStr))
{
dttoken.delimiter=inStr[0];
dttoken.noOfDelimiter++;
}
dttoken.token+=*(inStr++);
}
tokens.push_back(dttoken);
}
else if (IsAlpha(*inStr))
{
if(*inStr=='D'&& *(inStr+1)==':')
{
inStr+=2;
while( IsNumeric(*inStr))
{
dttoken.token+=*(inStr++);
}
}
else
{
while( IsAlpha(*inStr))
{
dttoken.token+=*(inStr++);
}
}
tokens.push_back(dttoken);
}
else if (*inStr=='-' ||*inStr=='+')
{
dttoken.token+=*(inStr++);
while( IsNumeric(*inStr)||*inStr==':')
{
if (*inStr==':')
{
dttoken.delimiter=inStr[0];
dttoken.noOfDelimiter++;
}
dttoken.token+=*(inStr++);
}
tokens.push_back(dttoken);
}
else
{
++inStr;
}
}
return true;
}
// =================================================================================================
// SwapMonthDateIfNeeded
// =====================
//
// Swaps month and date value if it creates a possible valid date
//
static void SwapMonthDateIfNeeded(short &day, short&month)
{
if(month>12&& day<13)
{
short temp=month;
month=day;
day=temp;
}
}
// =================================================================================================
// AdjustYearIfNeeded
// =====================
//
// Guess the year for a two digit year in a date
//
static void AdjustYearIfNeeded(short &year)
{
if (year<100)
{
if (year >40)
{
year=1900+year;
}
else
{
year=2000+year;
}
}
}
static bool IsLeapYear ( XMP_Int32 year )
{
if ( year < 0 ) year = -year + 1; // Fold the negative years, assuming there is a year 0.
if ( (year % 4) != 0 ) return false; // Not a multiple of 4.
if ( (year % 100) != 0 ) return true; // A multiple of 4 but not a multiple of 100.
if ( (year % 400) == 0 ) return true; // A multiple of 400.
return false; // A multiple of 100 but not a multiple of 400.
}
// =================================================================================================
// PostScript_Support::ConvertToDate
// =================================
//
// Converts date string from native of Postscript to YYYY-MM-DDThh:mm:ssTZD if a valid date is identified
//
std::string PostScript_Support::ConvertToDate(const char* inString)
{
PostScript_Support::Date date(0,0,0,0,0,0);
std::string dateTimeString;
short number=0;
std::vector<PostScript_Support::DateTimeTokens> tokenzs;
tokeniseDateString(inString,tokenzs );
std::vector<PostScript_Support::DateTimeTokens>:: const_iterator itr=tokenzs.begin();
for(;itr!=tokenzs.end();itr++)
{
// token[0] is invalid on an empty string. -- Hub
if(!itr->token.empty() && (itr->token[0]=='+' ||itr->token[0]=='-'))
{
const char *str=itr->token.c_str();
date.offsetSign=*(str++);
date.offsetHour=GetNumber(&str,2);
if (*str==':')str++;
date.offsetMin=GetNumber(&str,2);
if (!(date.offsetHour<=12 && date.offsetHour>=0
&&date.offsetMin>=0 && date.offsetMin<=59))
{
date.offsetSign='+';
date.offsetHour=0;
date.offsetMin=0;
}
else
{
date.containsOffset= true;
}
}
else if(itr->noOfDelimiter!=0)
{//either a date or time token
if(itr->noOfDelimiter==2 && itr->delimiter=='/')
{
if(date.day==0&& date.month==0 && date.year==0)
{
const char *str=itr->token.c_str();
number=GetNumber(&str);
str++;
if (number<32)
{
date.month=number;
date.day=GetNumber(&str);
SwapMonthDateIfNeeded(date.day, date.month);
str++;
date.year=GetNumber(&str);
AdjustYearIfNeeded(date.year);
}
else
{
date.year=number;
AdjustYearIfNeeded(date.year);
date.month=GetNumber(&str);
str++;
date.day=GetNumber(&str);
SwapMonthDateIfNeeded(date.day, date.month);
}
}
}
else if(itr->noOfDelimiter==2 && itr->delimiter==':')
{
const char *str=itr->token.c_str();
date.hours=GetNumber(&str);
str++;
date.minutes=GetNumber(&str);
str++;
date.seconds=GetNumber(&str);
if (date.hours>23|| date.minutes>59|| date.seconds>59)
{
date.hours=0;
date.minutes=0;
date.seconds=0;
}
}
else if(itr->noOfDelimiter==1 && itr->delimiter==':')
{
const char *str=itr->token.c_str();
date.hours=GetNumber(&str);
str++;
date.minutes=GetNumber(&str);
if (date.hours>23|| date.minutes>59)
{
date.hours=0;
date.minutes=0;
}
}
else if(itr->noOfDelimiter==2 && itr->delimiter=='-')
{
const char *str=itr->token.c_str();
number=GetNumber(&str);
str++;
if (number>31)
{
date.year=number;
AdjustYearIfNeeded(date.year);
date.month=GetNumber(&str);
str++;
date.day=GetNumber(&str);
SwapMonthDateIfNeeded(date.day, date.month);
}
else
{
date.month=number;
date.day=GetNumber(&str);
str++;
SwapMonthDateIfNeeded(date.day, date.month);
date.year=GetNumber(&str);
AdjustYearIfNeeded(date.year);
}
}
else if(itr->noOfDelimiter==2 && itr->delimiter=='.')
{
const char *str=itr->token.c_str();
date.year=GetNumber(&str);
str++;
AdjustYearIfNeeded(date.year);
date.month=GetNumber(&str);
str++;
date.day=GetNumber(&str);
SwapMonthDateIfNeeded(date.day, date.month);
}
}
else if (IsAlpha(itr->token[0]))
{ //this can be a name of the Month
// name of the day is ignored
short month=0;
std::string lowerToken=itr->token;
std::transform(lowerToken.begin(), lowerToken.end(), lowerToken.begin(), ::tolower);
if(!lowerToken.compare("jan")||!lowerToken.compare("january"))
{
month=1;
}
else if (!lowerToken.compare("feb")||!lowerToken.compare("february"))
{
month=2;
}
else if (!lowerToken.compare("mar")||!lowerToken.compare("march"))
{
month=3;
}
else if (!lowerToken.compare("apr")||!lowerToken.compare("april"))
{
month=4;
}
else if (!lowerToken.compare("may"))
{
month=5;
}
else if (!lowerToken.compare("jun")||!lowerToken.compare("june"))
{
month=6;
}
else if (!lowerToken.compare("jul")||!lowerToken.compare("july"))
{
month=7;
}
else if (!lowerToken.compare("aug")||!lowerToken.compare("august"))
{
month=8;
}
else if (!lowerToken.compare("sep")||!lowerToken.compare("september"))
{
month=9;
}
else if (!lowerToken.compare("oct")||!lowerToken.compare("october"))
{
month=10;
}
else if (!lowerToken.compare("nov")||!lowerToken.compare("november"))
{
month=11;
}
else if (!lowerToken.compare("dec")||!lowerToken.compare("december"))
{
month=12;
}
else if (!lowerToken.compare("pm"))
{
if (date.hours<13)
{
date.hours+=12;
}
}
else if (itr->token.length()>14)
{
constructDateTime(itr->token,dateTimeString);
}
if(month!=0 && date.month==0)
{
date.month=month;
if(date.day==0)
{
if(itr!=tokenzs.begin())
{
--itr;
if (itr->noOfDelimiter==0 && IsNumeric(itr->token[0]) )
{
const char * str=itr->token.c_str();
short day= GetNumber(&str);
if (day<=31 &&day >=1)
{
date.day=day;
}
}
++itr;
}
if(itr!=tokenzs.end())
{
++itr;
if (itr<tokenzs.end() && itr->noOfDelimiter==0 && IsNumeric(itr->token[0]) )
{
const char * str=itr->token.c_str();
short day= GetNumber(&str);
if (day<=31 &&day >=1)
{
date.day=day;
}
}
else if (itr == tokenzs.end())
{
break;
}
}
}
}
}
else if (IsNumeric(itr->token[0]) && date.year==0&&itr->token.length()==4)
{//this is the year
const char * str=itr->token.c_str();
date.year=GetNumber(&str);
}
else if (itr->token.length()>=14)
{
constructDateTime(itr->token,dateTimeString);
}
}
if (dateTimeString.length()==0)
{
char dtstr[100];
XMP_DateTime datetime;
if ( date.year < 10000 && date.month < 13 && date.month > 0 && date.day > 0 )
{
bool isValidDate=true;
if ( date.month == 2 )
{
if ( IsLeapYear ( date.year ) )
{
if ( date.day > 29 ) isValidDate=false;
}
else
{
if ( date.day > 28 ) isValidDate=false;
}
}
else if ( date.month == 4 || date.month == 6 || date.month == 9
|| date.month == 11 )
{
if ( date.day > 30 ) isValidDate=false;
}
else
{
if ( date.day > 31 ) isValidDate=false;
}
if( isValidDate && ! ( date == PostScript_Support::Date ( 0, 0, 0, 0, 0, 0 ) ) )
{
if ( date.containsOffset )
{
snprintf(dtstr,sizeof(dtstr),"%04d-%02d-%02dT%02d:%02d:%02d%c%02d:%02d",date.year,date.month,date.day,
date.hours,date.minutes,date.seconds,date.offsetSign,date.offsetHour,date.offsetMin);
}
else
{
snprintf(dtstr,sizeof(dtstr),"%04d-%02d-%02dT%02d:%02d:%02dZ",date.year,date.month,date.day,
date.hours,date.minutes,date.seconds);
}
try
{
SXMPUtils::ConvertToDate ( dtstr, &datetime ) ;
SXMPUtils::ConvertFromDate ( datetime, &dateTimeString ) ;
}
catch(...)
{
}
}
}
}
return dateTimeString;
}
// =================================================================================================
|