-
Notifications
You must be signed in to change notification settings - Fork 317
Expand file tree
/
Copy pathwebpimage.cpp
More file actions
810 lines (685 loc) · 29.1 KB
/
webpimage.cpp
File metadata and controls
810 lines (685 loc) · 29.1 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
// SPDX-License-Identifier: GPL-2.0-or-later
/*
Google's WEBP container spec can be found at the link below:
https://developers.google.com/speed/webp/docs/riff_container
*/
// included header files
#include "webpimage.hpp"
#include "basicio.hpp"
#include "config.h"
#include "convert.hpp"
#include "enforce.hpp"
#include "futils.hpp"
#include "image_int.hpp"
#include "safe_op.hpp"
#include "types.hpp"
#include <array>
#include <cstring>
#include <iostream>
#ifdef EXIV2_DEBUG_MESSAGES
namespace {
std::string binaryToHex(const uint8_t* data, size_t size) {
std::stringstream hexOutput;
auto tl = size / 16 * 16;
auto tl_offset = size - tl;
for (size_t loop = 0; loop < size; loop++) {
if (data[loop] < 16) {
hexOutput << "0";
}
hexOutput << std::hex << static_cast<int>(data[loop]);
if ((loop % 8) == 7) {
hexOutput << " ";
}
if ((loop % 16) == 15 || loop == (tl + tl_offset - 1)) {
int max = 15;
if (loop >= tl) {
max = static_cast<int>(tl_offset) - 1;
for (int offset = 0; offset < static_cast<int>(16 - tl_offset); offset++) {
if ((offset % 8) == 7) {
hexOutput << " ";
}
hexOutput << " ";
}
}
hexOutput << " ";
for (int offset = max; offset >= 0; offset--) {
if (offset == (max - 8)) {
hexOutput << " ";
}
uint8_t c = '.';
if (data[loop - offset] >= 0x20 && data[loop - offset] <= 0x7E) {
c = data[loop - offset];
}
hexOutput << static_cast<char>(c);
}
hexOutput << '\n';
}
}
hexOutput << '\n' << '\n' << '\n';
return hexOutput.str();
}
} // namespace
#endif
// *****************************************************************************
// class member definitions
namespace Exiv2 {
WebPImage::WebPImage(BasicIo::UniquePtr io) : Image(ImageType::webp, mdNone, std::move(io)) {
} // WebPImage::WebPImage
std::string WebPImage::mimeType() const {
return "image/webp";
}
void WebPImage::setIptcData(const IptcData& /*iptcData*/) {
// not supported
// just quietly ignore the request
// throw(Error(ErrorCode::kerInvalidSettingForImage, "IPTC metadata", "WebP"));
}
void WebPImage::setComment(const std::string&) {
// not supported
throw(Error(ErrorCode::kerInvalidSettingForImage, "Image comment", "WebP"));
}
/* =========================================== */
void WebPImage::writeMetadata() {
if (io_->open() != 0) {
throw Error(ErrorCode::kerDataSourceOpenFailed, io_->path(), strError());
}
IoCloser closer(*io_);
MemIo tempIo;
doWriteMetadata(tempIo); // may throw
io_->close();
io_->transfer(tempIo); // may throw
} // WebPImage::writeMetadata
void WebPImage::doWriteMetadata(BasicIo& outIo) {
if (!io_->isopen())
throw Error(ErrorCode::kerInputDataReadFailed);
if (!outIo.isopen())
throw Error(ErrorCode::kerImageWriteFailed);
#ifdef EXIV2_DEBUG_MESSAGES
std::cout << "Writing metadata" << '\n';
#endif
byte data[WEBP_TAG_SIZE * 3];
DataBuf chunkId(WEBP_TAG_SIZE + 1);
chunkId.write_uint8(WEBP_TAG_SIZE, '\0');
io_->readOrThrow(data, WEBP_TAG_SIZE * 3, Exiv2::ErrorCode::kerCorruptedMetadata);
uint64_t filesize = Exiv2::getULong(data + WEBP_TAG_SIZE, littleEndian);
/* Set up header */
if (outIo.write(data, WEBP_TAG_SIZE * 3) != WEBP_TAG_SIZE * 3)
throw Error(ErrorCode::kerImageWriteFailed);
/* Parse Chunks */
bool has_size = false;
bool has_xmp = false;
bool has_exif = false;
bool has_vp8x = false;
bool has_alpha = false;
bool has_icc = iccProfileDefined();
uint32_t width = 0;
uint32_t height = 0;
std::array<byte, WEBP_TAG_SIZE> size_buff;
Blob blob;
if (!exifData_.empty()) {
ExifParser::encode(blob, littleEndian, exifData_);
if (!blob.empty()) {
has_exif = true;
}
}
if (!xmpData_.empty() && !writeXmpFromPacket()) {
XmpParser::encode(xmpPacket_, xmpData_, XmpParser::useCompactFormat | XmpParser::omitAllFormatting);
}
has_xmp = !xmpPacket_.empty();
std::string xmp(xmpPacket_);
/* Verify for a VP8X Chunk First before writing in
case we have any exif or xmp data, also check
for any chunks with alpha frame/layer set */
while (!io_->eof() && io_->tell() < filesize) {
io_->readOrThrow(chunkId.data(), WEBP_TAG_SIZE, Exiv2::ErrorCode::kerCorruptedMetadata);
io_->readOrThrow(size_buff.data(), WEBP_TAG_SIZE, Exiv2::ErrorCode::kerCorruptedMetadata);
const uint32_t size_u32 = Exiv2::getULong(size_buff.data(), littleEndian);
// Check that `size_u32` is within bounds.
Internal::enforce(size_u32 <= io_->size() - io_->tell(), Exiv2::ErrorCode::kerCorruptedMetadata);
DataBuf payload(size_u32);
if (!payload.empty()) {
io_->readOrThrow(payload.data(), payload.size(), Exiv2::ErrorCode::kerCorruptedMetadata);
if (payload.size() % 2) {
byte c = 0;
io_->readOrThrow(&c, 1, Exiv2::ErrorCode::kerCorruptedMetadata);
}
}
/* Chunk with information about features
used in the file. */
if (equalsWebPTag(chunkId, WEBP_CHUNK_HEADER_VP8X) && !has_vp8x) {
has_vp8x = true;
}
if (equalsWebPTag(chunkId, WEBP_CHUNK_HEADER_VP8X) && !has_size) {
Internal::enforce(size_u32 >= 10, Exiv2::ErrorCode::kerCorruptedMetadata);
has_size = true;
std::array<byte, WEBP_TAG_SIZE> size_buf;
// Fetch width - stored in 24bits
std::copy_n(payload.begin() + 4, 3, size_buf.begin());
size_buf.back() = 0;
width = Exiv2::getULong(size_buf.data(), littleEndian) + 1;
// Fetch height - stored in 24bits
std::copy_n(payload.begin() + 7, 3, size_buf.begin());
size_buf.back() = 0;
height = Exiv2::getULong(size_buf.data(), littleEndian) + 1;
}
/* Chunk with animation control data. */
#ifdef __CHECK_FOR_ALPHA__ // Maybe in the future
if (equalsWebPTag(chunkId, WEBP_CHUNK_HEADER_ANIM) && !has_alpha) {
has_alpha = true;
}
#endif
/* Chunk with lossy image data. */
#ifdef __CHECK_FOR_ALPHA__ // Maybe in the future
if (equalsWebPTag(chunkId, WEBP_CHUNK_HEADER_VP8) && !has_alpha) {
has_alpha = true;
}
#endif
if (equalsWebPTag(chunkId, WEBP_CHUNK_HEADER_VP8) && !has_size) {
Internal::enforce(size_u32 >= 10, Exiv2::ErrorCode::kerCorruptedMetadata);
has_size = true;
std::array<byte, 2> size_buf;
/* Refer to this https://tools.ietf.org/html/rfc6386
for height and width reference for VP8 chunks */
// Fetch width - stored in 16bits
std::copy_n(payload.begin() + 6, 2, size_buf.begin());
width = Exiv2::getUShort(size_buf.data(), littleEndian) & 0x3fff;
// Fetch height - stored in 16bits
std::copy_n(payload.begin() + 8, 2, size_buf.begin());
height = Exiv2::getUShort(size_buf.data(), littleEndian) & 0x3fff;
}
/* Chunk with lossless image data. */
if (equalsWebPTag(chunkId, WEBP_CHUNK_HEADER_VP8L) && !has_alpha) {
Internal::enforce(size_u32 >= 5, Exiv2::ErrorCode::kerCorruptedMetadata);
if ((payload.read_uint8(4) & WEBP_VP8X_ALPHA_BIT) == WEBP_VP8X_ALPHA_BIT) {
has_alpha = true;
}
}
if (equalsWebPTag(chunkId, WEBP_CHUNK_HEADER_VP8L) && !has_size) {
Internal::enforce(size_u32 >= 5, Exiv2::ErrorCode::kerCorruptedMetadata);
has_size = true;
std::array<byte, 2> size_buf_w;
std::array<byte, 3> size_buf_h;
/* For VP8L chunks width & height are stored in 28 bits
of a 32 bit field requires bitshifting to get actual
sizes. Width and height are split even into 14 bits
each. Refer to this https://goo.gl/bpgMJf */
// Fetch width - 14 bits wide
std::copy_n(payload.begin() + 1, 2, size_buf_w.begin());
size_buf_w.back() &= 0x3F;
width = Exiv2::getUShort(size_buf_w.data(), littleEndian) + 1;
// Fetch height - 14 bits wide
std::copy_n(payload.begin() + 2, 3, size_buf_h.begin());
size_buf_h[0] = ((size_buf_h[0] >> 6) & 0x3) | ((size_buf_h[1] & 0x3FU) << 0x2);
size_buf_h[1] = ((size_buf_h[1] >> 6) & 0x3) | ((size_buf_h[2] & 0xFU) << 0x2);
height = Exiv2::getUShort(size_buf_h.data(), littleEndian) + 1;
}
/* Chunk with animation frame. */
if (equalsWebPTag(chunkId, WEBP_CHUNK_HEADER_ANMF) && !has_alpha) {
Internal::enforce(size_u32 >= 6, Exiv2::ErrorCode::kerCorruptedMetadata);
if ((payload.read_uint8(5) & 0x2) == 0x2) {
has_alpha = true;
}
}
if (equalsWebPTag(chunkId, WEBP_CHUNK_HEADER_ANMF) && !has_size) {
Internal::enforce(size_u32 >= 12, Exiv2::ErrorCode::kerCorruptedMetadata);
has_size = true;
std::array<byte, WEBP_TAG_SIZE> size_buf;
// Fetch width - stored in 24bits
std::copy_n(payload.begin() + 6, 3, size_buf.begin());
size_buf.back() = 0;
width = Exiv2::getULong(size_buf.data(), littleEndian) + 1;
// Fetch height - stored in 24bits
std::copy_n(payload.begin() + 9, 3, size_buf.begin());
size_buf.back() = 0;
height = Exiv2::getULong(size_buf.data(), littleEndian) + 1;
}
/* Chunk with alpha data. */
if (equalsWebPTag(chunkId, "ALPH") && !has_alpha) {
has_alpha = true;
}
}
/* Inject a VP8X chunk if one isn't available. */
if (!has_vp8x) {
inject_VP8X(outIo, has_xmp, has_exif, has_alpha, has_icc, width, height);
}
io_->seek(12, BasicIo::beg);
while (!io_->eof() && io_->tell() < filesize) {
io_->readOrThrow(chunkId.data(), 4, Exiv2::ErrorCode::kerCorruptedMetadata);
io_->readOrThrow(size_buff.data(), 4, Exiv2::ErrorCode::kerCorruptedMetadata);
const uint32_t size_u32 = Exiv2::getULong(size_buff.data(), littleEndian);
// Check that `size_u32` is within bounds.
Internal::enforce(size_u32 <= io_->size() - io_->tell(), Exiv2::ErrorCode::kerCorruptedMetadata);
DataBuf payload(size_u32);
io_->readOrThrow(payload.data(), size_u32, Exiv2::ErrorCode::kerCorruptedMetadata);
if (io_->tell() % 2)
io_->seek(+1, BasicIo::cur); // skip pad
if (equalsWebPTag(chunkId, WEBP_CHUNK_HEADER_VP8X)) {
Internal::enforce(size_u32 >= 1, Exiv2::ErrorCode::kerCorruptedMetadata);
if (has_icc) {
const uint8_t x = payload.read_uint8(0);
payload.write_uint8(0, x | WEBP_VP8X_ICC_BIT);
} else {
const uint8_t x = payload.read_uint8(0);
payload.write_uint8(0, x & ~WEBP_VP8X_ICC_BIT);
}
if (has_xmp) {
const uint8_t x = payload.read_uint8(0);
payload.write_uint8(0, x | WEBP_VP8X_XMP_BIT);
} else {
const uint8_t x = payload.read_uint8(0);
payload.write_uint8(0, x & ~WEBP_VP8X_XMP_BIT);
}
if (has_exif) {
const uint8_t x = payload.read_uint8(0);
payload.write_uint8(0, x | WEBP_VP8X_EXIF_BIT);
} else {
const uint8_t x = payload.read_uint8(0);
payload.write_uint8(0, x & ~WEBP_VP8X_EXIF_BIT);
}
if (outIo.write(chunkId.c_data(), WEBP_TAG_SIZE) != WEBP_TAG_SIZE)
throw Error(ErrorCode::kerImageWriteFailed);
if (outIo.write(size_buff.data(), WEBP_TAG_SIZE) != WEBP_TAG_SIZE)
throw Error(ErrorCode::kerImageWriteFailed);
if (outIo.write(payload.c_data(), payload.size()) != payload.size())
throw Error(ErrorCode::kerImageWriteFailed);
if (outIo.tell() % 2 && outIo.write(&WEBP_PAD_ODD, 1) != 1)
throw Error(ErrorCode::kerImageWriteFailed);
if (has_icc) {
if (outIo.write(reinterpret_cast<const byte*>(WEBP_CHUNK_HEADER_ICCP), WEBP_TAG_SIZE) != WEBP_TAG_SIZE)
throw Error(ErrorCode::kerImageWriteFailed);
ul2Data(data, static_cast<uint32_t>(iccProfile_.size()), littleEndian);
if (outIo.write(data, WEBP_TAG_SIZE) != WEBP_TAG_SIZE)
throw Error(ErrorCode::kerImageWriteFailed);
if (outIo.write(iccProfile_.c_data(), iccProfile_.size()) != iccProfile_.size()) {
throw Error(ErrorCode::kerImageWriteFailed);
}
has_icc = false;
}
} else if (equalsWebPTag(chunkId, WEBP_CHUNK_HEADER_ICCP)) {
// Skip it altogether handle it prior to here :)
} else if (equalsWebPTag(chunkId, WEBP_CHUNK_HEADER_EXIF)) {
// Skip and add new data afterwards
} else if (equalsWebPTag(chunkId, WEBP_CHUNK_HEADER_XMP)) {
// Skip and add new data afterwards
} else {
if (outIo.write(chunkId.c_data(), WEBP_TAG_SIZE) != WEBP_TAG_SIZE)
throw Error(ErrorCode::kerImageWriteFailed);
if (outIo.write(size_buff.data(), WEBP_TAG_SIZE) != WEBP_TAG_SIZE)
throw Error(ErrorCode::kerImageWriteFailed);
if (outIo.write(payload.c_data(), payload.size()) != payload.size())
throw Error(ErrorCode::kerImageWriteFailed);
}
// Encoder required to pad odd sized data with a null byte
if (outIo.tell() % 2 && outIo.write(&WEBP_PAD_ODD, 1) != 1)
throw Error(ErrorCode::kerImageWriteFailed);
}
if (has_exif) {
if (outIo.write(reinterpret_cast<const byte*>(WEBP_CHUNK_HEADER_EXIF), WEBP_TAG_SIZE) != WEBP_TAG_SIZE)
throw Error(ErrorCode::kerImageWriteFailed);
us2Data(data, static_cast<uint16_t>(blob.size()) + 8, bigEndian);
ul2Data(data, static_cast<uint32_t>(blob.size()), littleEndian);
if (outIo.write(data, WEBP_TAG_SIZE) != WEBP_TAG_SIZE)
throw Error(ErrorCode::kerImageWriteFailed);
if (outIo.write(blob.data(), blob.size()) != blob.size()) {
throw Error(ErrorCode::kerImageWriteFailed);
}
if (outIo.tell() % 2 && outIo.write(&WEBP_PAD_ODD, 1) != 1)
throw Error(ErrorCode::kerImageWriteFailed);
}
if (has_xmp) {
if (outIo.write(reinterpret_cast<const byte*>(WEBP_CHUNK_HEADER_XMP), WEBP_TAG_SIZE) != WEBP_TAG_SIZE)
throw Error(ErrorCode::kerImageWriteFailed);
ul2Data(data, static_cast<uint32_t>(xmpPacket().size()), littleEndian);
if (outIo.write(data, WEBP_TAG_SIZE) != WEBP_TAG_SIZE)
throw Error(ErrorCode::kerImageWriteFailed);
if (outIo.write(reinterpret_cast<const byte*>(xmp.data()), xmp.size()) != xmp.size()) {
throw Error(ErrorCode::kerImageWriteFailed);
}
if (outIo.tell() % 2 && outIo.write(&WEBP_PAD_ODD, 1) != 1)
throw Error(ErrorCode::kerImageWriteFailed);
}
// Fix File Size Payload Data
outIo.seek(0, BasicIo::beg);
filesize = outIo.size() - 8;
outIo.seek(4, BasicIo::beg);
ul2Data(data, static_cast<uint32_t>(filesize), littleEndian);
if (outIo.write(data, WEBP_TAG_SIZE) != WEBP_TAG_SIZE)
throw Error(ErrorCode::kerImageWriteFailed);
} // WebPImage::writeMetadata
/* =========================================== */
void WebPImage::printStructure(std::ostream& out, PrintStructureOption option, size_t depth) {
if (io_->open() != 0) {
throw Error(ErrorCode::kerDataSourceOpenFailed, io_->path(), strError());
}
// Ensure this is the correct image type
if (!isWebPType(*io_, true)) {
if (io_->error() || io_->eof())
throw Error(ErrorCode::kerFailedToReadImageData);
throw Error(ErrorCode::kerNotAnImage, "WEBP");
}
bool bPrint = option == kpsBasic || option == kpsRecursive;
if (bPrint || option == kpsXMP || option == kpsIccProfile || option == kpsIptcErase) {
byte data[WEBP_TAG_SIZE * 2];
io_->read(data, WEBP_TAG_SIZE * 2);
uint64_t filesize = Exiv2::getULong(data + WEBP_TAG_SIZE, littleEndian);
DataBuf chunkId(5);
chunkId.write_uint8(4, '\0');
if (bPrint) {
out << Internal::indent(depth) << "STRUCTURE OF WEBP FILE: " << io().path() << '\n';
out << Internal::indent(depth) << " Chunk | Length | Offset | Payload" << '\n';
}
io_->seek(0, BasicIo::beg); // rewind
while (!io_->eof() && io_->tell() < filesize) {
auto offset = io_->tell();
byte size_buff[WEBP_TAG_SIZE];
io_->read(chunkId.data(), WEBP_TAG_SIZE);
io_->read(size_buff, WEBP_TAG_SIZE);
const uint32_t size = Exiv2::getULong(size_buff, littleEndian);
// Check that `size` is within bounds.
Internal::enforce(!offset || size <= io_->size() - io_->tell(), Exiv2::ErrorCode::kerCorruptedMetadata);
DataBuf payload(offset ? size : WEBP_TAG_SIZE); // header is different from chunks
io_->read(payload.data(), payload.size());
if (bPrint) {
out << Internal::indent(depth) << stringFormat(" {} | {:8} | {:8} | ", chunkId.c_str(), size, offset)
<< Internal::binaryToString(makeSlice(payload, 0, payload.size() > 32 ? 32 : payload.size())) << '\n';
}
if (equalsWebPTag(chunkId, WEBP_CHUNK_HEADER_EXIF) && option == kpsRecursive) {
// create memio object with the payload, then print the structure
MemIo p(payload.c_data(), payload.size());
printTiffStructure(p, out, option, depth + 1);
}
bool bPrintPayload = (equalsWebPTag(chunkId, WEBP_CHUNK_HEADER_XMP) && option == kpsXMP) ||
(equalsWebPTag(chunkId, WEBP_CHUNK_HEADER_ICCP) && option == kpsIccProfile);
if (bPrintPayload) {
out.write(payload.c_str(), payload.size());
}
if (offset && io_->tell() % 2)
io_->seek(+1, BasicIo::cur); // skip padding byte on sub-chunks
}
}
}
/* =========================================== */
void WebPImage::readMetadata() {
if (io_->open() != 0)
throw Error(ErrorCode::kerDataSourceOpenFailed, io_->path(), strError());
IoCloser closer(*io_);
// Ensure that this is the correct image type
if (!isWebPType(*io_, true)) {
if (io_->error() || io_->eof())
throw Error(ErrorCode::kerFailedToReadImageData);
throw Error(ErrorCode::kerNotAJpeg);
}
clearMetadata();
byte data[12];
DataBuf chunkId(5);
chunkId.write_uint8(4, '\0');
io_->readOrThrow(data, WEBP_TAG_SIZE * 3, Exiv2::ErrorCode::kerCorruptedMetadata);
const uint32_t filesize = Safe::add(Exiv2::getULong(data + WEBP_TAG_SIZE, littleEndian), 8U);
Internal::enforce(filesize <= io_->size(), Exiv2::ErrorCode::kerCorruptedMetadata);
WebPImage::decodeChunks(filesize);
} // WebPImage::readMetadata
void WebPImage::decodeChunks(uint32_t filesize) {
DataBuf chunkId(5);
std::array<byte, WEBP_TAG_SIZE> size_buff;
bool has_canvas_data = false;
#ifdef EXIV2_DEBUG_MESSAGES
std::cout << "Reading metadata" << '\n';
#endif
chunkId.write_uint8(4, '\0');
while (!io_->eof() && io_->tell() < filesize) {
io_->readOrThrow(chunkId.data(), WEBP_TAG_SIZE, Exiv2::ErrorCode::kerCorruptedMetadata);
io_->readOrThrow(size_buff.data(), WEBP_TAG_SIZE, Exiv2::ErrorCode::kerCorruptedMetadata);
const uint32_t size = Exiv2::getULong(size_buff.data(), littleEndian);
// Check that `size` is within bounds.
Internal::enforce(io_->tell() <= filesize, Exiv2::ErrorCode::kerCorruptedMetadata);
Internal::enforce(size <= (filesize - io_->tell()), Exiv2::ErrorCode::kerCorruptedMetadata);
if (DataBuf payload(size); payload.empty()) {
io_->seek(size, BasicIo::cur);
} else if (equalsWebPTag(chunkId, WEBP_CHUNK_HEADER_VP8X) && !has_canvas_data) {
Internal::enforce(size >= 10, Exiv2::ErrorCode::kerCorruptedMetadata);
has_canvas_data = true;
std::array<byte, WEBP_TAG_SIZE> size_buf;
io_->readOrThrow(payload.data(), payload.size(), Exiv2::ErrorCode::kerCorruptedMetadata);
// Fetch width
std::copy_n(payload.begin() + 4, 3, size_buf.begin());
size_buf.back() = 0;
pixelWidth_ = Exiv2::getULong(size_buf.data(), littleEndian) + 1;
// Fetch height
std::copy_n(payload.begin() + 7, 3, size_buf.begin());
size_buf.back() = 0;
pixelHeight_ = Exiv2::getULong(size_buf.data(), littleEndian) + 1;
} else if (equalsWebPTag(chunkId, WEBP_CHUNK_HEADER_VP8) && !has_canvas_data) {
Internal::enforce(size >= 10, Exiv2::ErrorCode::kerCorruptedMetadata);
has_canvas_data = true;
io_->readOrThrow(payload.data(), payload.size(), Exiv2::ErrorCode::kerCorruptedMetadata);
std::array<byte, WEBP_TAG_SIZE> size_buf;
// Fetch width""
std::copy_n(payload.begin() + 6, 2, size_buf.begin());
size_buf[2] = 0;
size_buf[3] = 0;
pixelWidth_ = Exiv2::getULong(size_buf.data(), littleEndian) & 0x3fff;
// Fetch height
std::copy_n(payload.begin() + 8, 2, size_buf.begin());
size_buf[2] = 0;
size_buf[3] = 0;
pixelHeight_ = Exiv2::getULong(size_buf.data(), littleEndian) & 0x3fff;
} else if (equalsWebPTag(chunkId, WEBP_CHUNK_HEADER_VP8L) && !has_canvas_data) {
Internal::enforce(size >= 5, Exiv2::ErrorCode::kerCorruptedMetadata);
has_canvas_data = true;
std::array<byte, 2> size_buf_w;
std::array<byte, 3> size_buf_h;
io_->readOrThrow(payload.data(), payload.size(), Exiv2::ErrorCode::kerCorruptedMetadata);
// Fetch width
std::copy_n(payload.begin() + 1, 2, size_buf_w.begin());
size_buf_w.back() &= 0x3F;
pixelWidth_ = Exiv2::getUShort(size_buf_w.data(), littleEndian) + 1;
// Fetch height
std::copy_n(payload.begin() + 2, 3, size_buf_h.begin());
size_buf_h[0] = ((size_buf_h[0] >> 6) & 0x3) | ((size_buf_h[1] & 0x3FU) << 0x2);
size_buf_h[1] = ((size_buf_h[1] >> 6) & 0x3) | ((size_buf_h[2] & 0xFU) << 0x2);
pixelHeight_ = Exiv2::getUShort(size_buf_h.data(), littleEndian) + 1;
} else if (equalsWebPTag(chunkId, WEBP_CHUNK_HEADER_ANMF) && !has_canvas_data) {
Internal::enforce(size >= 12, Exiv2::ErrorCode::kerCorruptedMetadata);
has_canvas_data = true;
std::array<byte, WEBP_TAG_SIZE> size_buf;
io_->readOrThrow(payload.data(), payload.size(), Exiv2::ErrorCode::kerCorruptedMetadata);
// Fetch width
std::copy_n(payload.begin() + 6, 3, size_buf.begin());
size_buf.back() = 0;
pixelWidth_ = Exiv2::getULong(size_buf.data(), littleEndian) + 1;
// Fetch height
std::copy_n(payload.begin() + 9, 3, size_buf.begin());
size_buf.back() = 0;
pixelHeight_ = Exiv2::getULong(size_buf.data(), littleEndian) + 1;
} else if (equalsWebPTag(chunkId, WEBP_CHUNK_HEADER_ICCP)) {
io_->readOrThrow(payload.data(), payload.size(), Exiv2::ErrorCode::kerCorruptedMetadata);
this->setIccProfile(std::move(payload));
} else if (equalsWebPTag(chunkId, WEBP_CHUNK_HEADER_EXIF)) {
io_->readOrThrow(payload.data(), payload.size(), Exiv2::ErrorCode::kerCorruptedMetadata);
std::array<byte, 2> size_buff2;
// 4 meaningful bytes + 2 padding bytes
auto exifLongHeader = std::array<byte, 6>{0xFF, 0x01, 0xFF, 0xE1, 0x00, 0x00};
auto exifShortHeader = std::array<byte, 6>{0x45, 0x78, 0x69, 0x66, 0x00, 0x00};
const byte exifTiffLEHeader[] = {0x49, 0x49, 0x2A}; // "MM*"
const byte exifTiffBEHeader[] = {0x4D, 0x4D, 0x00, 0x2A}; // "II\0*"
size_t offset = 0;
bool s_header = false;
bool le_header = false;
bool be_header = false;
size_t pos = getHeaderOffset(payload.c_data(), payload.size(), exifLongHeader.data(), 4);
if (pos == std::string::npos) {
pos = getHeaderOffset(payload.c_data(), payload.size(), exifLongHeader.data(), 6);
if (pos != std::string::npos) {
s_header = true;
}
}
if (pos == std::string::npos) {
pos = getHeaderOffset(payload.c_data(), payload.size(), exifTiffLEHeader, 3);
if (pos != std::string::npos) {
le_header = true;
}
}
if (pos == std::string::npos) {
pos = getHeaderOffset(payload.c_data(), payload.size(), exifTiffBEHeader, 4);
if (pos != std::string::npos) {
be_header = true;
}
}
if (s_header) {
offset += 6;
}
if (be_header || le_header) {
offset += 12;
}
const size_t sizePayload = Safe::add(payload.size(), offset);
DataBuf rawExifData(sizePayload);
if (s_header) {
us2Data(size_buff2.data(), static_cast<uint16_t>(sizePayload - 6), bigEndian);
std::copy_n(exifLongHeader.begin(), 4, rawExifData.begin());
std::copy(size_buff2.begin(), size_buff2.end(), rawExifData.begin() + 4);
}
if (be_header || le_header) {
us2Data(size_buff2.data(), static_cast<uint16_t>(sizePayload - 6), bigEndian);
std::copy_n(exifLongHeader.begin(), 4, rawExifData.begin());
std::copy(size_buff2.begin(), size_buff2.end(), rawExifData.begin() + 4);
std::copy(exifShortHeader.begin(), exifShortHeader.end(), rawExifData.begin() + 6);
}
std::copy(payload.begin(), payload.end(), rawExifData.begin() + offset);
#ifdef EXIV2_DEBUG_MESSAGES
std::cout << "Display Hex Dump [size:" << sizePayload << "]" << '\n';
std::cout << binaryToHex(rawExifData.c_data(), sizePayload);
#endif
if (pos != std::string::npos) {
XmpData xmpData;
ByteOrder bo = ExifParser::decode(exifData_, payload.c_data(pos), payload.size() - pos);
setByteOrder(bo);
} else {
#ifndef SUPPRESS_WARNINGS
EXV_WARNING << "Failed to decode Exif metadata." << '\n';
#endif
exifData_.clear();
}
} else if (equalsWebPTag(chunkId, WEBP_CHUNK_HEADER_XMP)) {
io_->readOrThrow(payload.data(), payload.size(), Exiv2::ErrorCode::kerCorruptedMetadata);
xmpPacket_.assign(payload.c_str(), payload.size());
if (!xmpPacket_.empty() && XmpParser::decode(xmpData_, xmpPacket_)) {
#ifndef SUPPRESS_WARNINGS
EXV_WARNING << "Failed to decode XMP metadata." << '\n';
#endif
} else {
#ifdef EXIV2_DEBUG_MESSAGES
std::cout << "Display Hex Dump [size:" << payload.size() << "]" << '\n';
std::cout << binaryToHex(payload.c_data(), payload.size());
#endif
}
} else {
io_->seek(size, BasicIo::cur);
}
if (io_->tell() % 2)
io_->seek(+1, BasicIo::cur);
}
}
/* =========================================== */
Image::UniquePtr newWebPInstance(BasicIo::UniquePtr io, bool /*create*/) {
auto image = std::make_unique<WebPImage>(std::move(io));
if (!image->good()) {
return nullptr;
}
return image;
}
bool isWebPType(BasicIo& iIo, bool /*advance*/) {
if (iIo.size() < 12) {
return false;
}
const int32_t len = 4;
constexpr std::array<byte, len> RiffImageId{'R', 'I', 'F', 'F'};
constexpr std::array<byte, len> WebPImageId{'W', 'E', 'B', 'P'};
std::array<byte, len> webp;
std::array<byte, len> data;
std::array<byte, len> riff;
iIo.readOrThrow(riff.data(), len, Exiv2::ErrorCode::kerCorruptedMetadata);
iIo.readOrThrow(data.data(), len, Exiv2::ErrorCode::kerCorruptedMetadata);
iIo.readOrThrow(webp.data(), len, Exiv2::ErrorCode::kerCorruptedMetadata);
bool matched_riff = riff == RiffImageId;
bool matched_webp = webp == WebPImageId;
iIo.seek(-12, BasicIo::cur);
return matched_riff && matched_webp;
}
/*!
@brief Function used to check equality of a Tags with a
particular string (ignores case while comparing).
@param buf Data buffer that will contain Tag to compare
@param str char* Pointer to string
@return Returns true if the buffer value is equal to string.
*/
bool WebPImage::equalsWebPTag(const Exiv2::DataBuf& buf, const char* str) {
for (int i = 0; i < 4; i++)
if (toupper(buf.read_uint8(i)) != str[i])
return false;
return true;
}
/*!
@brief Function used to add missing EXIF & XMP flags
to the feature section.
@param iIo get BasicIo pointer to inject data
@param has_xmp Verify if we have xmp data and set required flag
@param has_exif Verify if we have exif data and set required flag
*/
void WebPImage::inject_VP8X(BasicIo& iIo, bool has_xmp, bool has_exif, bool has_alpha, bool has_icc, uint32_t width,
uint32_t height) const {
const byte size[4] = {0x0A, 0x00, 0x00, 0x00};
byte data[10] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
iIo.write(reinterpret_cast<const byte*>(WEBP_CHUNK_HEADER_VP8X), WEBP_TAG_SIZE);
iIo.write(size, WEBP_TAG_SIZE);
if (has_alpha) {
data[0] |= WEBP_VP8X_ALPHA_BIT;
}
if (has_icc) {
data[0] |= WEBP_VP8X_ICC_BIT;
}
if (has_xmp) {
data[0] |= WEBP_VP8X_XMP_BIT;
}
if (has_exif) {
data[0] |= WEBP_VP8X_EXIF_BIT;
}
/* set width - stored in 24bits*/
Internal::enforce(width > 0, Exiv2::ErrorCode::kerCorruptedMetadata);
uint32_t w = width - 1;
data[4] = w & 0xFF;
data[5] = (w >> 8) & 0xFF;
data[6] = (w >> 16) & 0xFF;
/* set height - stored in 24bits */
Internal::enforce(height > 0, Exiv2::ErrorCode::kerCorruptedMetadata);
uint32_t h = height - 1;
data[7] = h & 0xFF;
data[8] = (h >> 8) & 0xFF;
data[9] = (h >> 16) & 0xFF;
iIo.write(data, 10);
/* Handle inject an icc profile right after VP8X chunk */
if (has_icc) {
byte size_buff[WEBP_TAG_SIZE];
ul2Data(size_buff, static_cast<uint32_t>(iccProfile_.size()), littleEndian);
if (iIo.write(reinterpret_cast<const byte*>(WEBP_CHUNK_HEADER_ICCP), WEBP_TAG_SIZE) != WEBP_TAG_SIZE)
throw Error(ErrorCode::kerImageWriteFailed);
if (iIo.write(size_buff, WEBP_TAG_SIZE) != WEBP_TAG_SIZE)
throw Error(ErrorCode::kerImageWriteFailed);
if (iIo.write(iccProfile_.c_data(), iccProfile_.size()) != iccProfile_.size())
throw Error(ErrorCode::kerImageWriteFailed);
if (iIo.tell() % 2 && iIo.write(&WEBP_PAD_ODD, 1) != 1)
throw Error(ErrorCode::kerImageWriteFailed);
}
}
size_t WebPImage::getHeaderOffset(const byte* data, size_t data_size, const byte* header, size_t header_size) {
size_t pos = std::string::npos; // error value
if (data_size < header_size) {
return pos;
}
for (size_t i = 0; i < data_size - header_size; i++) {
if (memcmp(header, &data[i], header_size) == 0) {
pos = i;
break;
}
}
return pos;
}
} // namespace Exiv2