-
Notifications
You must be signed in to change notification settings - Fork 317
Expand file tree
/
Copy pathepsimage.cpp
More file actions
1131 lines (1070 loc) · 43.4 KB
/
epsimage.cpp
File metadata and controls
1131 lines (1070 loc) · 43.4 KB
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
// SPDX-License-Identifier: GPL-2.0-or-later
/*
File: epsimage.cpp
Author(s): Michael Ulbrich (mul) <mul@rentapacs.de>
Volker Grabsch (vog) <vog@notjusthosting.com>
History: 7-Mar-2011, vog: created
*/
// *****************************************************************************
// included header files
#include "epsimage.hpp"
#include "basicio.hpp"
#include "config.h"
#include "enforce.hpp"
#include "error.hpp"
#include "futils.hpp"
#include "image.hpp"
#include "version.hpp"
// + standard includes
#include <algorithm>
#include <array>
#include <cstring>
#include <limits>
#include <sstream>
#include <string>
// *****************************************************************************
namespace {
using namespace Exiv2;
using namespace Exiv2::Internal;
// signature of DOS EPS
constexpr auto dosEpsSignature = std::string_view("\xC5\xD0\xD3\xC6");
// first line of EPS
constexpr std::array epsFirstLine{
std::string_view("%!PS-Adobe-3.0 EPSF-3.0"),
std::string_view("%!PS-Adobe-3.0 EPSF-3.0 "), // OpenOffice
std::string_view("%!PS-Adobe-3.1 EPSF-3.0"), // Illustrator
};
// blank EPS file
constexpr auto epsBlank = std::string_view(
"%!PS-Adobe-3.0 EPSF-3.0\n"
"%%BoundingBox: 0 0 0 0\n");
// list of all valid XMP headers
constexpr std::string_view xmpHeaders[] = {
// We do not enforce the trailing "?>" here, because the XMP specification
// permits additional attributes after begin="..." and id="...".
// normal headers
"<?xpacket begin=\"\xef\xbb\xbf\" id=\"W5M0MpCehiHzreSzNTczkc9d\"",
"<?xpacket begin=\"\xef\xbb\xbf\" id='W5M0MpCehiHzreSzNTczkc9d'",
"<?xpacket begin='\xef\xbb\xbf' id=\"W5M0MpCehiHzreSzNTczkc9d\"",
"<?xpacket begin='\xef\xbb\xbf' id='W5M0MpCehiHzreSzNTczkc9d'",
// deprecated headers (empty begin attribute, UTF-8 only)
"<?xpacket begin=\"\" id=\"W5M0MpCehiHzreSzNTczkc9d\"",
"<?xpacket begin=\"\" id='W5M0MpCehiHzreSzNTczkc9d'",
"<?xpacket begin='' id=\"W5M0MpCehiHzreSzNTczkc9d\"",
"<?xpacket begin='' id='W5M0MpCehiHzreSzNTczkc9d'",
};
// list of all valid XMP trailers
using XmpTrailer = std::pair<std::string_view, bool>;
constexpr auto xmpTrailers = std::array{
// We do not enforce the trailing "?>" here, because the XMP specification
// permits additional attributes after end="...".
XmpTrailer("<?xpacket end=\"r\"", true),
XmpTrailer("<?xpacket end='r'", true),
XmpTrailer("<?xpacket end=\"w\"", false),
XmpTrailer("<?xpacket end='w'", false),
};
// closing part of all valid XMP trailers
constexpr auto xmpTrailerEnd = std::string_view("?>");
//! Write data into temp file, taking care of errors
void writeTemp(BasicIo& tempIo, const byte* data, size_t size) {
if (size == 0)
return;
if (tempIo.write(data, size) != size) {
#ifndef SUPPRESS_WARNINGS
EXV_WARNING << "Failed to write to temporary file.\n";
#endif
throw Error(ErrorCode::kerImageWriteFailed);
}
}
//! Write data into temp file, taking care of errors
void writeTemp(BasicIo& tempIo, const std::string& data) {
writeTemp(tempIo, reinterpret_cast<const byte*>(data.data()), data.size());
}
//! Get the current write position of temp file, taking care of errors
uint32_t posTemp(const BasicIo& tempIo) {
const size_t pos = tempIo.tell();
enforce(pos <= std::numeric_limits<uint32_t>::max(), ErrorCode::kerImageWriteFailed);
return static_cast<uint32_t>(pos);
}
//! Check whether a string contains only white space characters
bool onlyWhitespaces(const std::string& s) {
// According to the DSC 3.0 specification, 4.4 Parsing Rules,
// only spaces and tabs are considered to be white space characters.
return s.find_first_not_of(" \t") == std::string::npos;
}
//! Read the next line of a buffer, allow for changing line ending style
size_t readLine(std::string& line, const byte* data, size_t startPos, size_t size) {
line.clear();
size_t pos = startPos;
// step through line
while (pos < size && data[pos] != '\r' && data[pos] != '\n') {
line += data[pos];
pos++;
}
// skip line ending, if present
if (pos >= size)
return pos;
pos++;
if (pos >= size)
return pos;
if (data[pos - 1] == '\r' && data[pos] == '\n')
pos++;
return pos;
}
//! Read the previous line of a buffer, allow for changing line ending style
size_t readPrevLine(std::string& line, const byte* data, size_t startPos, size_t size) {
line.clear();
size_t pos = startPos;
if (pos > size)
return pos;
// skip line ending of previous line, if present
if (pos <= 0)
return pos;
if (data[pos - 1] == '\r' || data[pos - 1] == '\n') {
pos--;
if (pos <= 0)
return pos;
if (data[pos - 1] == '\r' && data[pos] == '\n') {
pos--;
if (pos <= 0)
return pos;
}
}
// step through previous line
while (pos >= 1 && data[pos - 1] != '\r' && data[pos - 1] != '\n') {
pos--;
line += data[pos];
}
std::reverse(line.begin(), line.end());
return pos;
}
//! Find an XMP block
void findXmp(size_t& xmpPos, size_t& xmpSize, const byte* data, size_t startPos, size_t size, bool write) {
// search for valid XMP header
xmpSize = 0;
for (xmpPos = startPos; xmpPos < size; xmpPos++) {
if (data[xmpPos] != '\x00' && data[xmpPos] != '<')
continue;
for (auto&& header : xmpHeaders) {
if (xmpPos + header.size() > size)
continue;
if (memcmp(data + xmpPos, header.data(), header.size()) != 0)
continue;
#ifdef DEBUG
EXV_DEBUG << "findXmp: Found XMP header at position: " << xmpPos << "\n";
#endif
// search for valid XMP trailer
for (size_t trailerPos = xmpPos + header.size(); trailerPos < size; trailerPos++) {
if (data[trailerPos] != '\x00' && data[trailerPos] != '<')
continue;
for (const auto& [trailer, readOnly] : xmpTrailers) {
if (trailerPos + trailer.size() > size)
continue;
if (memcmp(data + trailerPos, trailer.data(), trailer.size()) != 0)
continue;
#ifdef DEBUG
EXV_DEBUG << "findXmp: Found XMP trailer at position: " << trailerPos << "\n";
#endif
if (readOnly) {
#ifndef SUPPRESS_WARNINGS
EXV_WARNING << "Unable to handle read-only XMP metadata yet. Please provide your "
"sample EPS file to the Exiv2 project: http://dev.exiv2.org/projects/exiv2\n";
#endif
throw Error(write ? ErrorCode::kerImageWriteFailed : ErrorCode::kerFailedToReadImageData);
}
// search for end of XMP trailer
for (size_t trailerEndPos = trailerPos + trailer.size(); trailerEndPos + xmpTrailerEnd.size() <= size;
trailerEndPos++) {
if (memcmp(data + trailerEndPos, xmpTrailerEnd.data(), xmpTrailerEnd.size()) == 0) {
xmpSize = (trailerEndPos + xmpTrailerEnd.size()) - xmpPos;
return;
}
}
#ifndef SUPPRESS_WARNINGS
EXV_WARNING << "Found XMP header but incomplete XMP trailer.\n";
#endif
throw Error(write ? ErrorCode::kerImageWriteFailed : ErrorCode::kerFailedToReadImageData);
}
}
#ifndef SUPPRESS_WARNINGS
EXV_WARNING << "Found XMP header but no XMP trailer.\n";
#endif
throw Error(write ? ErrorCode::kerImageWriteFailed : ErrorCode::kerFailedToReadImageData);
}
}
}
//! Unified implementation of reading and writing EPS metadata
void readWriteEpsMetadata(BasicIo& io, std::string& xmpPacket, NativePreviewList& nativePreviews, bool write) {
// open input file
if (io.open() != 0) {
throw Error(ErrorCode::kerDataSourceOpenFailed, io.path(), strError());
}
IoCloser closer(io);
// read from input file via memory map
const byte* data = io.mmap();
// default positions and sizes
const size_t size = io.size();
size_t posEps = 0;
size_t posEndEps = size;
uint32_t posWmf = 0;
uint32_t sizeWmf = 0;
uint32_t posTiff = 0;
uint32_t sizeTiff = 0;
ErrorCode errcode = write ? ErrorCode::kerImageWriteFailed : ErrorCode::kerFailedToReadImageData;
// check for DOS EPS
const bool dosEps =
(size >= dosEpsSignature.size() && memcmp(data, dosEpsSignature.data(), dosEpsSignature.size()) == 0);
if (dosEps) {
#ifdef DEBUG
EXV_DEBUG << "readWriteEpsMetadata: Found DOS EPS signature\n";
#endif
enforce(size >= 30, errcode);
posEps = getULong(data + 4, littleEndian);
posEndEps = getULong(data + 8, littleEndian) + posEps;
posWmf = getULong(data + 12, littleEndian);
sizeWmf = getULong(data + 16, littleEndian);
posTiff = getULong(data + 20, littleEndian);
sizeTiff = getULong(data + 24, littleEndian);
#ifdef DEBUG
EXV_DEBUG << "readWriteEpsMetadata: EPS section at position " << posEps << ", size " << (posEndEps - posEps)
<< "\n";
EXV_DEBUG << "readWriteEpsMetadata: WMF section at position " << posWmf << ", size " << sizeWmf << "\n";
EXV_DEBUG << "readWriteEpsMetadata: TIFF section at position " << posTiff << ", size " << sizeTiff << "\n";
#endif
if (uint16_t checksum = getUShort(data + 28, littleEndian); checksum != 0xFFFF) {
#ifdef DEBUG
EXV_DEBUG << "readWriteEpsMetadata: DOS EPS checksum is not FFFF\n";
#endif
}
if ((posWmf != 0 || sizeWmf != 0) && (posTiff != 0 || sizeTiff != 0)) {
#ifndef SUPPRESS_WARNINGS
EXV_WARNING << "DOS EPS file has both WMF and TIFF section. Only one of those is allowed.\n";
#endif
if (write)
throw Error(ErrorCode::kerImageWriteFailed);
}
if (sizeWmf == 0 && sizeTiff == 0) {
#ifndef SUPPRESS_WARNINGS
EXV_WARNING << "DOS EPS file has neither WMF nor TIFF section. Exactly one of those is required.\n";
#endif
if (write)
throw Error(ErrorCode::kerImageWriteFailed);
}
enforce(30 <= posEps, errcode);
enforce(sizeWmf == 0 || 30 <= posWmf, errcode);
enforce(sizeTiff == 0 || 30 <= posTiff, errcode);
enforce(posEps <= posEndEps && posEndEps <= size, errcode);
enforce(posWmf <= size && sizeWmf <= size - posWmf, errcode);
enforce(posTiff <= size && sizeTiff <= size - posTiff, errcode);
}
// check first line
std::string firstLine;
const size_t posSecondLine = readLine(firstLine, data, posEps, posEndEps);
#ifdef DEBUG
EXV_DEBUG << "readWriteEpsMetadata: First line: " << firstLine << "\n";
#endif
auto it = std::find(epsFirstLine.begin(), epsFirstLine.end(), firstLine);
if (it == epsFirstLine.end()) {
throw Error(ErrorCode::kerNotAnImage, "EPS");
}
// determine line ending style of the first line
if (posSecondLine >= posEndEps) {
#ifndef SUPPRESS_WARNINGS
EXV_WARNING << "Premature end of file after first line.\n";
#endif
throw Error(write ? ErrorCode::kerImageWriteFailed : ErrorCode::kerFailedToReadImageData);
}
const std::string lineEnding(reinterpret_cast<const char*>(data + posEps + firstLine.size()),
posSecondLine - (posEps + firstLine.size()));
#ifdef DEBUG
if (lineEnding == "\n") {
EXV_DEBUG << "readWriteEpsMetadata: Line ending style: Unix (LF)\n";
} else if (lineEnding == "\r") {
EXV_DEBUG << "readWriteEpsMetadata: Line ending style: Mac (CR)\n";
} else if (lineEnding == "\r\n") {
EXV_DEBUG << "readWriteEpsMetadata: Line ending style: DOS (CR LF)\n";
} else {
EXV_DEBUG << "readWriteEpsMetadata: Line ending style: (unknown)\n";
}
#endif
// scan comments
size_t posLanguageLevel = posEndEps;
size_t posContainsXmp = posEndEps;
size_t posPages = posEndEps;
size_t posExiv2Version = posEndEps;
size_t posExiv2Website = posEndEps;
size_t posEndComments = posEndEps;
size_t posAi7Thumbnail = posEndEps;
size_t posAi7ThumbnailEndData = posEndEps;
size_t posBeginPhotoshop = posEndEps;
size_t posEndPhotoshop = posEndEps;
size_t posPage = posEndEps;
size_t posBeginPageSetup = posEndEps;
size_t posEndPageSetup = posEndEps;
size_t posPageTrailer = posEndEps;
size_t posEof = posEndEps;
std::vector<std::pair<size_t, size_t>> removableEmbeddings;
size_t depth = 0;
const size_t maxDepth = std::numeric_limits<size_t>::max();
bool illustrator8 = false;
bool corelDraw = false;
bool implicitPage = false;
bool implicitPageSetup = false;
bool implicitPageTrailer = false;
bool inDefaultsPreviewPrologSetup = false;
bool inRemovableEmbedding = false;
std::string removableEmbeddingEndLine;
size_t removableEmbeddingsWithUnmarkedTrailer = 0;
for (size_t pos = posEps; pos < posEof;) {
const size_t startPos = pos;
std::string line;
pos = readLine(line, data, startPos, posEndEps);
#ifdef DEBUG
bool significantLine = true;
#endif
// nested documents
if (posPage == posEndEps && (line.starts_with("%%IncludeDocument:") || line.starts_with("%%BeginDocument:"))) {
#ifndef SUPPRESS_WARNINGS
EXV_WARNING << "Nested document at invalid position: " << startPos << "\n";
#endif
throw Error(write ? ErrorCode::kerImageWriteFailed : ErrorCode::kerFailedToReadImageData);
}
if (line.starts_with("%%BeginDocument:")) {
if (depth == maxDepth) {
#ifndef SUPPRESS_WARNINGS
EXV_WARNING << "Document too deeply nested at position: " << startPos << "\n";
#endif
throw Error(write ? ErrorCode::kerImageWriteFailed : ErrorCode::kerFailedToReadImageData);
}
depth++;
} else if (line.starts_with("%%EndDocument")) {
if (depth == 0) {
#ifndef SUPPRESS_WARNINGS
EXV_WARNING << "Unmatched EndDocument at position: " << startPos << "\n";
#endif
throw Error(write ? ErrorCode::kerImageWriteFailed : ErrorCode::kerFailedToReadImageData);
}
depth--;
} else {
#ifdef DEBUG
significantLine = false;
#endif
}
#ifdef DEBUG
if (significantLine) {
EXV_DEBUG << "readWriteEpsMetadata: Found significant line \"" << line << "\" at position: " << startPos << "\n";
}
significantLine = true;
#endif
if (depth != 0)
continue;
// explicit "Begin" comments
if (line.starts_with("%%BeginPreview:")) {
inDefaultsPreviewPrologSetup = true;
} else if (line == "%%BeginDefaults") {
inDefaultsPreviewPrologSetup = true;
} else if (line == "%%BeginProlog") {
inDefaultsPreviewPrologSetup = true;
} else if (line == "%%BeginSetup") {
inDefaultsPreviewPrologSetup = true;
} else if (posPage == posEndEps && line.starts_with("%%Page:")) {
posPage = startPos;
} else if (posPage != posEndEps && line.starts_with("%%Page:")) {
if (implicitPage) {
#ifndef SUPPRESS_WARNINGS
EXV_WARNING << "Page at position " << startPos << " conflicts with implicit page at position: " << posPage
<< "\n";
#endif
throw Error(write ? ErrorCode::kerImageWriteFailed : ErrorCode::kerFailedToReadImageData);
}
#ifndef SUPPRESS_WARNINGS
EXV_WARNING << "Unable to handle multiple PostScript pages. Found second page at position: " << startPos << "\n";
#endif
throw Error(write ? ErrorCode::kerImageWriteFailed : ErrorCode::kerFailedToReadImageData);
} else if (line == "%%BeginPageSetup") {
posBeginPageSetup = startPos;
} else if (!inRemovableEmbedding && line == "%Exiv2BeginXMP: Before %%EndPageSetup") {
inRemovableEmbedding = true;
removableEmbeddings.emplace_back(startPos, startPos);
removableEmbeddingEndLine = "%Exiv2EndXMP";
} else if (!inRemovableEmbedding && line == "%Exiv2BeginXMP: After %%PageTrailer") {
inRemovableEmbedding = true;
removableEmbeddings.emplace_back(startPos, startPos);
removableEmbeddingEndLine = "%Exiv2EndXMP";
} else if (!inRemovableEmbedding && line == "%ADOBeginClientInjection: PageSetup End \"AI11EPS\"") {
inRemovableEmbedding = true;
removableEmbeddings.emplace_back(startPos, startPos);
removableEmbeddingEndLine = "%ADOEndClientInjection: PageSetup End \"AI11EPS\"";
} else if (!inRemovableEmbedding && line == "%ADOBeginClientInjection: PageTrailer Start \"AI11EPS\"") {
inRemovableEmbedding = true;
removableEmbeddings.emplace_back(startPos, startPos);
removableEmbeddingEndLine = "%ADOEndClientInjection: PageTrailer Start \"AI11EPS\"";
} else if (!inRemovableEmbedding && line == "%begin_xml_code") {
inRemovableEmbedding = true;
removableEmbeddings.emplace_back(startPos, startPos);
removableEmbeddingEndLine = "%end_xml_code";
removableEmbeddingsWithUnmarkedTrailer++;
} else {
#ifdef DEBUG
significantLine = false;
#endif
}
#ifdef DEBUG
if (significantLine) {
EXV_DEBUG << "readWriteEpsMetadata: Found significant line \"" << line << "\" at position: " << startPos << "\n";
}
significantLine = true;
#endif
// implicit comments
if (line == "%%EOF" || line == "%begin_xml_code" || line.size() < 2 || line.front() != '%' || '\x21' > line[1] ||
line[1] > '\x7e') {
if (posEndComments == posEndEps) {
posEndComments = startPos;
#ifdef DEBUG
EXV_DEBUG << "readWriteEpsMetadata: Found implicit EndComments at position: " << startPos << "\n";
#endif
}
}
if (posPage == posEndEps && posEndComments != posEndEps && !inDefaultsPreviewPrologSetup && !inRemovableEmbedding &&
!onlyWhitespaces(line)) {
posPage = startPos;
implicitPage = true;
#ifdef DEBUG
EXV_DEBUG << "readWriteEpsMetadata: Found implicit Page at position: " << startPos << "\n";
#endif
}
if (posBeginPageSetup == posEndEps &&
(implicitPage || (posPage != posEndEps && !inRemovableEmbedding && !line.empty() && line.front() != '%'))) {
posBeginPageSetup = startPos;
implicitPageSetup = true;
#ifdef DEBUG
EXV_DEBUG << "readWriteEpsMetadata: Found implicit BeginPageSetup at position: " << startPos << "\n";
#endif
}
if (posEndPageSetup == posEndEps && implicitPageSetup && !inRemovableEmbedding && !line.empty() &&
line.front() != '%') {
posEndPageSetup = startPos;
#ifdef DEBUG
EXV_DEBUG << "readWriteEpsMetadata: Found implicit EndPageSetup at position: " << startPos << "\n";
#endif
}
if (!line.empty() && line.front() != '%')
continue; // performance optimization
if (line == "%%EOF" || line == "%%Trailer" || line == "%%PageTrailer") {
if (posBeginPageSetup == posEndEps) {
posBeginPageSetup = startPos;
implicitPageSetup = true;
#ifdef DEBUG
EXV_DEBUG << "readWriteEpsMetadata: Found implicit BeginPageSetup at position: " << startPos << "\n";
#endif
}
if (posEndPageSetup == posEndEps) {
posEndPageSetup = startPos;
implicitPageSetup = true;
#ifdef DEBUG
EXV_DEBUG << "readWriteEpsMetadata: Found implicit EndPageSetup at position: " << startPos << "\n";
#endif
}
}
if ((line == "%%EOF" || line == "%%Trailer") && posPageTrailer == posEndEps) {
posPageTrailer = startPos;
implicitPageTrailer = true;
#ifdef DEBUG
EXV_DEBUG << "readWriteEpsMetadata: Found implicit PageTrailer at position: " << startPos << "\n";
#endif
}
// remaining explicit comments
if (posEndComments == posEndEps && posLanguageLevel == posEndEps && line.starts_with("%%LanguageLevel:")) {
posLanguageLevel = startPos;
} else if (posEndComments == posEndEps && posContainsXmp == posEndEps && line.starts_with("%ADO_ContainsXMP:")) {
posContainsXmp = startPos;
} else if (posEndComments == posEndEps && posPages == posEndEps && line.starts_with("%%Pages:")) {
posPages = startPos;
} else if (posEndComments == posEndEps && posExiv2Version == posEndEps && line.starts_with("%Exiv2Version:")) {
posExiv2Version = startPos;
} else if (posEndComments == posEndEps && posExiv2Website == posEndEps && line.starts_with("%Exiv2Website:")) {
posExiv2Website = startPos;
} else if (posEndComments == posEndEps && line.starts_with("%%Creator: Adobe Illustrator") &&
firstLine == "%!PS-Adobe-3.0 EPSF-3.0") {
illustrator8 = true;
} else if (posEndComments == posEndEps && line.starts_with("%AI7_Thumbnail:")) {
posAi7Thumbnail = startPos;
} else if (posEndComments == posEndEps && posAi7Thumbnail != posEndEps && posAi7ThumbnailEndData == posEndEps &&
line == "%%EndData") {
posAi7ThumbnailEndData = startPos;
} else if (posEndComments == posEndEps && line == "%%EndComments") {
posEndComments = startPos;
} else if (inDefaultsPreviewPrologSetup && line.starts_with("%%BeginResource: procset wCorel")) {
corelDraw = true;
} else if (line == "%%EndPreview") {
inDefaultsPreviewPrologSetup = false;
} else if (line == "%%EndDefaults") {
inDefaultsPreviewPrologSetup = false;
} else if (line == "%%EndProlog") {
inDefaultsPreviewPrologSetup = false;
} else if (line == "%%EndSetup") {
inDefaultsPreviewPrologSetup = false;
} else if (posEndPageSetup == posEndEps && line == "%%EndPageSetup") {
posEndPageSetup = startPos;
} else if (posPageTrailer == posEndEps && line == "%%PageTrailer") {
posPageTrailer = startPos;
} else if (posBeginPhotoshop == posEndEps && line.starts_with("%BeginPhotoshop:")) {
posBeginPhotoshop = pos;
} else if (posBeginPhotoshop != posEndEps && posEndPhotoshop == posEndEps && line == "%EndPhotoshop") {
posEndPhotoshop = startPos;
} else if (inRemovableEmbedding && line == removableEmbeddingEndLine) {
inRemovableEmbedding = false;
removableEmbeddings.back().second = pos;
} else if (line == "%%EOF") {
posEof = startPos;
} else {
#ifdef DEBUG
significantLine = false;
#endif
}
#ifdef DEBUG
if (significantLine) {
EXV_DEBUG << "readWriteEpsMetadata: Found significant line \"" << line << "\" at position: " << startPos << "\n";
}
#endif
}
// check for unfinished nested documents
if (depth != 0) {
#ifndef SUPPRESS_WARNINGS
EXV_WARNING << "Unmatched BeginDocument (" << depth << "x)\n";
#endif
throw Error(write ? ErrorCode::kerImageWriteFailed : ErrorCode::kerFailedToReadImageData);
}
// look for the unmarked trailers of some removable XMP embeddings
size_t posXmpTrailerEnd = posEof;
for (size_t i = 0; i < removableEmbeddingsWithUnmarkedTrailer; i++) {
std::string line1;
const size_t posLine1 = readPrevLine(line1, data, posXmpTrailerEnd, posEndEps);
std::string line2;
const size_t posLine2 = readPrevLine(line2, data, posLine1, posEndEps);
size_t posXmpTrailer;
if (line1 == "[/EMC pdfmark") { // Exiftool style
posXmpTrailer = posLine1;
} else if (line1 == "[/NamespacePop pdfmark" &&
line2 ==
"[{nextImage} 1 dict begin /Metadata {photoshop_metadata_stream} def currentdict end /PUT "
"pdfmark") { // Photoshop style
posXmpTrailer = posLine2;
} else {
#ifndef SUPPRESS_WARNINGS
EXV_WARNING << "Unable to find XMP embedding trailer ending at position: " << posXmpTrailerEnd << "\n";
#endif
if (write)
throw Error(ErrorCode::kerImageWriteFailed);
break;
}
removableEmbeddings.emplace_back(posXmpTrailer, posXmpTrailerEnd);
#ifdef DEBUG
auto [r, s] = removableEmbeddings.back();
EXV_DEBUG << "readWriteEpsMetadata: Recognized unmarked trailer of removable XMP embedding at [" << r << "," << s
<< ")\n";
#endif
posXmpTrailerEnd = posXmpTrailer;
}
// interpret comment "%ADO_ContainsXMP:"
std::string line;
readLine(line, data, posContainsXmp, posEndEps);
bool containsXmp;
if (line == "%ADO_ContainsXMP: MainFirst" || line == "%ADO_ContainsXMP:MainFirst") {
containsXmp = true;
} else if (line.empty() || line == "%ADO_ContainsXMP: NoMain" || line == "%ADO_ContainsXMP:NoMain") {
containsXmp = false;
} else {
#ifndef SUPPRESS_WARNINGS
EXV_WARNING << "Invalid line \"" << line << "\" at position: " << posContainsXmp << "\n";
#endif
throw Error(write ? ErrorCode::kerImageWriteFailed : ErrorCode::kerFailedToReadImageData);
}
const bool deleteXmp = (write && xmpPacket.empty());
bool fixBeginXmlPacket = false;
bool useFlexibleEmbedding = false;
size_t xmpPos = posEndEps;
size_t xmpSize = 0;
if (containsXmp) {
// search for XMP metadata
findXmp(xmpPos, xmpSize, data, posEps, posEndEps, write);
if (xmpPos == posEndEps) {
#ifndef SUPPRESS_WARNINGS
EXV_WARNING << "Unable to find XMP metadata as announced at position: " << posContainsXmp << "\n";
#endif
}
// check embedding of XMP metadata
const size_t posLineAfterXmp = readLine(line, data, xmpPos + xmpSize, posEndEps);
if (!line.empty()) {
#ifndef SUPPRESS_WARNINGS
EXV_WARNING << "Unexpected " << line.size() << " bytes of data after XMP at position: " << (xmpPos + xmpSize)
<< "\n";
#endif
} else if (!deleteXmp) {
readLine(line, data, posLineAfterXmp, posEndEps);
if (line == "% &&end XMP packet marker&&" || line == "% &&end XMP packet marker&&") {
useFlexibleEmbedding = true;
}
}
}
if (useFlexibleEmbedding) {
#ifdef DEBUG
EXV_DEBUG << "readWriteEpsMetadata: Using flexible XMP embedding\n";
#endif
const size_t posBeginXmlPacket = readPrevLine(line, data, xmpPos, posEndEps);
if (line.starts_with("%begin_xml_packet:")) {
#ifdef DEBUG
EXV_DEBUG << "readWriteEpsMetadata: XMP embedding contains %begin_xml_packet\n";
#endif
if (write) {
fixBeginXmlPacket = true;
xmpSize += (xmpPos - posBeginXmlPacket);
xmpPos = posBeginXmlPacket;
}
} else if (posBeginPhotoshop != posEndEps) {
#ifndef SUPPRESS_WARNINGS
EXV_WARNING << "Missing %begin_xml_packet in Photoshop EPS at position: " << xmpPos << "\n";
#endif
if (write)
throw Error(ErrorCode::kerImageWriteFailed);
}
}
if (!useFlexibleEmbedding) {
// check if there are irremovable XMP metadata blocks before EndPageSetup
size_t posOtherXmp = containsXmp ? xmpPos : posEps;
size_t sizeOtherXmp = 0;
for (;;) {
findXmp(posOtherXmp, sizeOtherXmp, data, posOtherXmp + sizeOtherXmp, posEndPageSetup, write);
if (posOtherXmp >= posEndPageSetup)
break;
bool isRemovableEmbedding = false;
for (const auto& [r, s] : removableEmbeddings) {
if (r <= posOtherXmp && posOtherXmp < s) {
isRemovableEmbedding = true;
break;
}
}
if (!isRemovableEmbedding) {
#ifndef SUPPRESS_WARNINGS
EXV_WARNING << "XMP metadata block is not removable at position: " << posOtherXmp << "\n";
#endif
if (write)
throw Error(ErrorCode::kerImageWriteFailed);
break;
}
}
}
if (!write) {
// copy XMP metadata
xmpPacket.assign(reinterpret_cast<const char*>(data + xmpPos), xmpSize);
// native previews
nativePreviews.clear();
if (posAi7ThumbnailEndData != posEndEps) {
NativePreview nativePreview;
std::string dummy;
std::string lineAi7Thumbnail;
const size_t posBeginData = readLine(lineAi7Thumbnail, data, posAi7Thumbnail, posEndEps);
std::istringstream lineStreamAi7Thumbnail(lineAi7Thumbnail);
lineStreamAi7Thumbnail >> dummy;
lineStreamAi7Thumbnail >> nativePreview.width_;
lineStreamAi7Thumbnail >> nativePreview.height_;
std::string depthStr;
lineStreamAi7Thumbnail >> depthStr;
std::string lineBeginData;
const size_t posAfterBeginData = readLine(lineBeginData, data, posBeginData, posEndEps);
std::istringstream lineStreamBeginData(lineBeginData);
std::string beginData;
lineStreamBeginData >> beginData;
lineStreamBeginData >> dummy;
std::string type;
lineStreamBeginData >> type;
nativePreview.position_ = static_cast<long>(posAfterBeginData);
nativePreview.size_ = static_cast<uint32_t>(posAi7ThumbnailEndData - posAfterBeginData);
nativePreview.filter_ = "hex-ai7thumbnail-pnm";
nativePreview.mimeType_ = "image/x-portable-anymap";
if (depthStr != "8") {
#ifndef SUPPRESS_WARNINGS
EXV_WARNING << "Unable to handle Illustrator thumbnail depth: " << depthStr << "\n";
#endif
} else if (beginData != "%%BeginData:") {
#ifndef SUPPRESS_WARNINGS
EXV_WARNING << "Unable to handle Illustrator thumbnail data section: " << lineBeginData << "\n";
#endif
} else if (type != "Hex") {
#ifndef SUPPRESS_WARNINGS
EXV_WARNING << "Unable to handle Illustrator thumbnail data type: " << type << "\n";
#endif
} else {
nativePreviews.push_back(std::move(nativePreview));
}
}
if (posEndPhotoshop != posEndEps) {
auto sizePhotoshop = posEndPhotoshop - posBeginPhotoshop;
NativePreview nativePreview{posBeginPhotoshop, sizePhotoshop, 0, 0, "hex-irb", "image/jpeg"};
nativePreviews.push_back(std::move(nativePreview));
}
if (sizeWmf != 0) {
NativePreview nativePreview{posWmf, sizeWmf, 0, 0, "", "image/x-wmf"};
nativePreviews.push_back(std::move(nativePreview));
}
if (sizeTiff != 0) {
NativePreview nativePreview{posTiff, sizeTiff, 0, 0, "", "image/tiff"};
nativePreviews.push_back(std::move(nativePreview));
}
} else {
// check for Adobe Illustrator 8.0 or older
if (illustrator8) {
#ifndef SUPPRESS_WARNINGS
EXV_WARNING << "Unable to write to EPS files created by Adobe Illustrator 8.0 or older.\n";
#endif
throw Error(ErrorCode::kerImageWriteFailed);
}
// create temporary output file
MemIo tempIo;
if (!tempIo.isopen()) {
#ifndef SUPPRESS_WARNINGS
EXV_WARNING << "Unable to create temporary file for writing.\n";
#endif
throw Error(ErrorCode::kerImageWriteFailed);
}
#ifdef DEBUG
EXV_DEBUG << "readWriteEpsMetadata: Created temporary file " << tempIo.path() << "\n";
#endif
// sort all positions
std::vector<size_t> positions;
positions.push_back(posLanguageLevel);
positions.push_back(posContainsXmp);
positions.push_back(posPages);
positions.push_back(posExiv2Version);
positions.push_back(posExiv2Website);
positions.push_back(posEndComments);
positions.push_back(posPage);
positions.push_back(posBeginPageSetup);
positions.push_back(posEndPageSetup);
positions.push_back(posPageTrailer);
positions.push_back(posEof);
positions.push_back(posEndEps);
if (useFlexibleEmbedding) {
positions.push_back(xmpPos);
}
for (const auto& [r, s] : removableEmbeddings) {
positions.push_back(r);
}
std::sort(positions.begin(), positions.end());
// assemble result EPS document
if (dosEps) {
// DOS EPS header will be written afterwards
writeTemp(tempIo, std::string(30, '\x00'));
}
const std::string containsXmpLine = deleteXmp ? "%ADO_ContainsXMP: NoMain" : "%ADO_ContainsXMP: MainFirst";
const uint32_t posEpsNew = posTemp(tempIo);
size_t prevPos = posEps;
size_t prevSkipPos = prevPos;
for (const auto& pos : positions) {
if (pos == prevPos)
continue;
#ifdef DEBUG
EXV_DEBUG << "readWriteEpsMetadata: Writing at " << pos << "\n";
#endif
if (pos < prevSkipPos) {
#ifndef SUPPRESS_WARNINGS
EXV_WARNING << "Internal error while assembling the result EPS document: "
"Unable to continue at position "
<< pos << " after skipping to position " << prevSkipPos << "\n";
#endif
throw Error(ErrorCode::kerImageWriteFailed);
}
writeTemp(tempIo, data + prevSkipPos, pos - prevSkipPos);
const size_t posLineEnd = readLine(line, data, pos, posEndEps);
size_t skipPos = pos;
// add last line ending if necessary
if (pos == posEndEps && pos >= 1 && data[pos - 1] != '\r' && data[pos - 1] != '\n') {
writeTemp(tempIo, lineEnding);
#ifdef DEBUG
EXV_DEBUG << "readWriteEpsMetadata: Added missing line ending of last line\n";
#endif
}
// update and complement DSC comments
if (pos == posLanguageLevel && posLanguageLevel != posEndEps && !deleteXmp && !useFlexibleEmbedding &&
(line == "%%LanguageLevel:1" || line == "%%LanguageLevel: 1")) {
writeTemp(tempIo, "%%LanguageLevel: 2" + lineEnding);
skipPos = posLineEnd;
#ifdef DEBUG
EXV_DEBUG << "readWriteEpsMetadata: Skipping to " << skipPos << " at " << __FILE__ << ":" << __LINE__ << "\n";
#endif
}
if (pos == posContainsXmp && posContainsXmp != posEndEps && line != containsXmpLine) {
writeTemp(tempIo, containsXmpLine + lineEnding);
skipPos = posLineEnd;
#ifdef DEBUG
EXV_DEBUG << "readWriteEpsMetadata: Skipping to " << skipPos << " at " << __FILE__ << ":" << __LINE__ << "\n";
#endif
}
if (pos == posExiv2Version && posExiv2Version != posEndEps) {
writeTemp(tempIo, "%Exiv2Version: " + versionNumberHexString() + lineEnding);
skipPos = posLineEnd;
#ifdef DEBUG
EXV_DEBUG << "readWriteEpsMetadata: Skipping to " << skipPos << " at " << __FILE__ << ":" << __LINE__ << "\n";
#endif
}
if (pos == posExiv2Website && posExiv2Website != posEndEps) {
writeTemp(tempIo, "%Exiv2Website: http://www.exiv2.org/" + lineEnding);
skipPos = posLineEnd;
#ifdef DEBUG
EXV_DEBUG << "readWriteEpsMetadata: Skipping to " << skipPos << " at " << __FILE__ << ":" << __LINE__ << "\n";
#endif
}
if (pos == posEndComments) {
if (posLanguageLevel == posEndEps && !deleteXmp && !useFlexibleEmbedding) {
writeTemp(tempIo, "%%LanguageLevel: 2" + lineEnding);
}
if (posContainsXmp == posEndEps) {
writeTemp(tempIo, containsXmpLine + lineEnding);
}
if (posPages == posEndEps) {
writeTemp(tempIo, "%%Pages: 1" + lineEnding);
}
if (posExiv2Version == posEndEps) {
writeTemp(tempIo, "%Exiv2Version: " + versionNumberHexString() + lineEnding);
}
if (posExiv2Website == posEndEps) {
writeTemp(tempIo, "%Exiv2Website: http://www.exiv2.org/" + lineEnding);
}
readLine(line, data, posEndComments, posEndEps);
if (line != "%%EndComments") {
writeTemp(tempIo, "%%EndComments" + lineEnding);
}
}
if (pos == posPage && !line.starts_with("%%Page:")) {
writeTemp(tempIo, "%%Page: 1 1" + lineEnding);
writeTemp(tempIo, "%%EndPageComments" + lineEnding);
}
if (pos == posBeginPageSetup && line != "%%BeginPageSetup") {
writeTemp(tempIo, "%%BeginPageSetup" + lineEnding);
}
// insert XMP metadata into existing flexible embedding
if (useFlexibleEmbedding && pos == xmpPos) {
if (fixBeginXmlPacket) {
writeTemp(tempIo, "%begin_xml_packet: " + toString(xmpPacket.size()) + lineEnding);
}
writeTemp(tempIo, xmpPacket);
skipPos += xmpSize;
#ifdef DEBUG
EXV_DEBUG << "readWriteEpsMetadata: Skipping to " << skipPos << " at " << __FILE__ << ":" << __LINE__ << "\n";
#endif
}
if (!useFlexibleEmbedding) {
// remove preceding embedding(s)
for (const auto& [p, s] : removableEmbeddings) {
if (pos == p) {
skipPos = s;
#ifdef DEBUG
EXV_DEBUG << "readWriteEpsMetadata: Skipping to " << skipPos << " at " << __FILE__ << ":" << __LINE__
<< "\n";
#endif
break;
}
}
// insert XMP metadata with new flexible embedding, if necessary
if (pos == posEndPageSetup && !deleteXmp) {
writeTemp(tempIo, "%Exiv2BeginXMP: Before %%EndPageSetup" + lineEnding);
if (corelDraw) {
writeTemp(tempIo, "%Exiv2Notice: The following line is needed by CorelDRAW." + lineEnding);
writeTemp(tempIo, "@rs" + lineEnding);
}
if (posBeginPhotoshop != posEndEps) {
writeTemp(tempIo, "%Exiv2Notice: The following line is needed by Photoshop." + lineEnding);
writeTemp(tempIo, "%begin_xml_code" + lineEnding);
}
writeTemp(tempIo, "/currentdistillerparams where" + lineEnding);
writeTemp(tempIo, "{pop currentdistillerparams /CoreDistVersion get 5000 lt} {true} ifelse" + lineEnding);
writeTemp(tempIo, "{userdict /Exiv2_pdfmark /cleartomark load put" + lineEnding);
writeTemp(tempIo, " userdict /Exiv2_metafile_pdfmark {flushfile cleartomark} bind put}" + lineEnding);
writeTemp(tempIo, "{userdict /Exiv2_pdfmark /pdfmark load put" + lineEnding);
writeTemp(tempIo, " userdict /Exiv2_metafile_pdfmark {/PUT pdfmark} bind put} ifelse" + lineEnding);
writeTemp(tempIo, "[/NamespacePush Exiv2_pdfmark" + lineEnding);
writeTemp(tempIo, "[/_objdef {Exiv2_metadata_stream} /type /stream /OBJ Exiv2_pdfmark" + lineEnding);
writeTemp(tempIo, "[{Exiv2_metadata_stream} 2 dict begin" + lineEnding);
writeTemp(tempIo,
" /Type /Metadata def /Subtype /XML def currentdict end /PUT Exiv2_pdfmark" + lineEnding);
writeTemp(tempIo, "[{Exiv2_metadata_stream}" + lineEnding);
writeTemp(tempIo, " currentfile 0 (% &&end XMP packet marker&&)" + lineEnding);
writeTemp(tempIo, " /SubFileDecode filter Exiv2_metafile_pdfmark" + lineEnding);
if (posBeginPhotoshop != posEndEps) {
writeTemp(tempIo,
"%Exiv2Notice: The following line is needed by Photoshop. "
"Parameter must be exact size of XMP metadata." +
lineEnding);
writeTemp(tempIo, "%begin_xml_packet: " + toString(xmpPacket.size()) + lineEnding);
}
writeTemp(tempIo, xmpPacket);
writeTemp(tempIo, lineEnding);
writeTemp(tempIo, "% &&end XMP packet marker&&" + lineEnding);
writeTemp(tempIo, "[/Document 1 dict begin" + lineEnding);
writeTemp(tempIo,
" /Metadata {Exiv2_metadata_stream} def currentdict end /BDC Exiv2_pdfmark" + lineEnding);
if (posBeginPhotoshop != posEndEps) {
writeTemp(tempIo, "%Exiv2Notice: The following line is needed by Photoshop." + lineEnding);
writeTemp(tempIo, "%end_xml_code" + lineEnding);
}
if (corelDraw) {
writeTemp(tempIo, "%Exiv2Notice: The following line is needed by CorelDRAW." + lineEnding);
writeTemp(tempIo, "@sv" + lineEnding);
}
writeTemp(tempIo, "%Exiv2EndXMP" + lineEnding);
}
}
if (pos == posEndPageSetup && line != "%%EndPageSetup") {
writeTemp(tempIo, "%%EndPageSetup" + lineEnding);
}
if (!useFlexibleEmbedding && pos == posPageTrailer && !deleteXmp) {
if (!implicitPageTrailer) {
skipPos = posLineEnd;
#ifdef DEBUG
EXV_DEBUG << "readWriteEpsMetadata: Skipping to " << skipPos << " at " << __FILE__ << ":" << __LINE__ << "\n";
#endif
}
writeTemp(tempIo, "%%PageTrailer" + lineEnding);
writeTemp(tempIo, "%Exiv2BeginXMP: After %%PageTrailer" + lineEnding);
writeTemp(tempIo, "[/EMC Exiv2_pdfmark" + lineEnding);
writeTemp(tempIo, "[/NamespacePop Exiv2_pdfmark" + lineEnding);
writeTemp(tempIo, "%Exiv2EndXMP" + lineEnding);
}
// add EOF comment if necessary
if (pos == posEndEps && posEof == posEndEps) {
writeTemp(tempIo, "%%EOF" + lineEnding);
}
prevPos = pos;
prevSkipPos = skipPos;
}
const uint32_t posEndEpsNew = posTemp(tempIo);
#ifdef DEBUG
EXV_DEBUG << "readWriteEpsMetadata: New EPS size: " << (posEndEpsNew - posEpsNew) << "\n";
#endif
if (dosEps) {
// write WMF and/or TIFF section if present
writeTemp(tempIo, data + posWmf, sizeWmf);
writeTemp(tempIo, data + posTiff, sizeTiff);
#ifdef DEBUG
EXV_DEBUG << "readWriteEpsMetadata: New DOS EPS total size: " << posTemp(tempIo) << "\n";
#endif
// write DOS EPS header
if (tempIo.seek(0, BasicIo::beg) != 0) {
#ifndef SUPPRESS_WARNINGS
EXV_WARNING << "Internal error while seeking in temporary file.\n";
#endif