forked from Exiv2/exiv2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bigtiffimage.cpp
459 lines (370 loc) · 18.3 KB
/
bigtiffimage.cpp
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
#include "bigtiffimage.hpp"
#include <cassert>
#include <limits>
#include "safe_op.hpp"
#include "exif.hpp"
#include "error.hpp"
#include "image_int.hpp"
#include "enforce.hpp"
namespace Exiv2
{
namespace
{
struct Header
{
enum Format
{
StandardTiff,
BigTiff,
};
Header(): byteOrder_(invalidByteOrder), version_(-1), data_size_(0), dir_offset_(0) {}
Header(const ByteOrder& order, int v, int size, uint64_t offset):
byteOrder_(order),
version_(v),
data_size_(size),
dir_offset_(offset)
{
}
bool isValid() const
{
return version_ != -1;
}
ByteOrder byteOrder() const
{
assert(isValid());
return byteOrder_;
}
int version() const
{
assert(isValid());
return version_;
}
Format format() const
{
assert(isValid());
return version_ == 0x2A? StandardTiff: BigTiff;
}
int dataSize() const
{
assert(isValid());
return data_size_;
}
uint64_t dirOffset() const
{
assert(isValid());
return dir_offset_;
}
private:
ByteOrder byteOrder_;
int version_; // 42 or 43 - regular tiff or big tiff
int data_size_; // 4 or 8
uint64_t dir_offset_;
};
Header readHeader(BasicIo& io)
{
byte header[2] = {0, 0};
io.read(header, 2);
ByteOrder byteOrder = invalidByteOrder;
if (header[0] == 'I' && header[1] == 'I')
byteOrder = littleEndian;
else if (header[0] == 'M' && header[1] == 'M')
byteOrder = bigEndian;
if (byteOrder == invalidByteOrder)
return Header();
byte version[2] = {0, 0};
io.read(version, 2);
const uint16_t magic = getUShort(version, byteOrder);
if (magic != 0x2A && magic != 0x2B)
return Header();
Header result;
if (magic == 0x2A)
{
byte buffer[4];
int read = io.read(buffer, 4);
if (read < 4)
throw Exiv2::Error(kerCorruptedMetadata);
const uint32_t offset = getULong(buffer, byteOrder);
result = Header(byteOrder, magic, 4, offset);
}
else
{
byte buffer[8] = {0, 0, 0, 0, 0, 0, 0, 0};
int read = io.read(buffer, 2);
if (read < 2)
throw Exiv2::Error(kerCorruptedMetadata);
const int size = getUShort(buffer, byteOrder);
if (size == 8)
{
read = io.read(buffer, 2); // null
if (read < 2)
throw Exiv2::Error(kerCorruptedMetadata);
read = io.read(buffer, 8);
if (read < 8)
throw Exiv2::Error(kerCorruptedMetadata);
const uint64_t offset = getULongLong(buffer, byteOrder);
if (offset >= io.size())
throw Exiv2::Error(kerCorruptedMetadata);
result = Header(byteOrder, magic, size, offset);
}
else
throw Exiv2::Error(kerCorruptedMetadata);
}
return result;
}
class BigTiffImage: public Image
{
public:
BigTiffImage(BasicIo::AutoPtr io):
Image(ImageType::bigtiff, mdExif, io),
header_(),
dataSize_(0),
doSwap_(false)
{
header_ = readHeader(Image::io());
assert(header_.isValid());
doSwap_ = (isLittleEndianPlatform() && header_.byteOrder() == bigEndian)
|| (isBigEndianPlatform() && header_.byteOrder() == littleEndian);
dataSize_ = header_.format() == Header::StandardTiff? 4 : 8;
}
virtual ~BigTiffImage() {}
// overrides
void readMetadata()
{
}
void writeMetadata()
{
}
std::string mimeType() const
{
return std::string();
}
void printStructure(std::ostream& os, PrintStructureOption option, int depth)
{
printIFD(os, option, header_.dirOffset(), depth - 1);
}
private:
Header header_;
int dataSize_;
bool doSwap_;
void printIFD(std::ostream& out, PrintStructureOption option, uint64_t dir_offset, int depth)
{
BasicIo& io = Image::io();
depth++;
bool bFirst = true;
// buffer
bool bPrint = true;
do
{
// Read top of directory
io.seek(dir_offset, BasicIo::beg);
const uint64_t entries = readData(header_.format() == Header::StandardTiff? 2: 8);
const bool tooBig = entries > 500;
if ( bFirst && bPrint )
{
out << Internal::indent(depth) << Internal::stringFormat("STRUCTURE OF BIGTIFF FILE ") << io.path() << std::endl;
if (tooBig)
out << Internal::indent(depth) << "entries = " << entries << std::endl;
}
if (tooBig)
break;
// Read the dictionary
for ( uint64_t i = 0; i < entries; i ++ )
{
if ( bFirst && bPrint )
out << Internal::indent(depth)
<< " address | tag | "
<< " type | count | offset | value\n";
bFirst = false;
const uint16_t tag = (uint16_t) readData(2);
const uint16_t type = (uint16_t) readData(2);
const uint64_t count = readData(dataSize_);
const DataBuf data = io.read(dataSize_); // Read data as raw value. what should be done about it will be decided depending on type
std::string sp = "" ; // output spacer
//prepare to print the value
// TODO: figure out what's going on with kount
const uint64_t kount = isStringType(type)? (count > 32 ? 32 : count) // restrict long arrays
: count > 5 ? 5
: count
;
const uint32_t pad = isStringType(type) ? 1 : 0;
const uint32_t size = isStringType(type) ? 1
: is2ByteType(type) ? 2
: is4ByteType(type) ? 4
: is8ByteType(type) ? 8
: 1;
// #55 and #56 memory allocation crash test/data/POC8
// size * count > std::numeric_limits<uint64_t>::max()
// =>
// size > std::numeric_limits<uint64_t>::max() / count
// (don't perform that check when count == 0 => will cause a division by zero exception)
if (count != 0) {
if (size > std::numeric_limits<uint64_t>::max() / count) {
throw Error(kerInvalidMalloc); // we got number bigger than 2^64
}
}
// more than we can handle
if (size * count > std::numeric_limits<uint64_t>::max() - pad)
throw Error(kerInvalidMalloc); // again more than 2^64
const uint64_t allocate = size*count + pad;
if ( allocate > io.size() ) {
throw Error(kerInvalidMalloc);
}
DataBuf buf(static_cast<long>(allocate));
const uint64_t offset = header_.format() == Header::StandardTiff?
byteSwap4(data, 0, doSwap_):
byteSwap8(data, 0, doSwap_);
// big data? Use 'data' as pointer to real data
const bool usePointer = (size_t) count*size > (size_t) dataSize_;
if ( usePointer ) // read into buffer
{
size_t restore = io.tell(); // save
io.seek(offset, BasicIo::beg); // position
io.read(buf.pData_, (long) count * size); // read
io.seek(restore, BasicIo::beg); // restore
}
else // use 'data' as data :)
std::memcpy(buf.pData_, data.pData_, (size_t) count * size); // copy data
if ( bPrint )
{
const uint64_t entrySize = header_.format() == Header::StandardTiff? 12: 20;
const uint64_t address = dir_offset + 2 + i * entrySize;
const std::string offsetString = usePointer?
Internal::stringFormat("%10u", offset):
"";
out << Internal::indent(depth)
<< Internal::stringFormat("%8u | %#06x %-25s |%10s |%9u |%10s | ",
address, tag, tagName(tag).c_str(), typeName(type), count, offsetString.c_str());
if ( isShortType(type) )
{
for ( size_t k = 0 ; k < kount ; k++ )
{
out << sp << byteSwap2(buf, k*size, doSwap_);
sp = " ";
}
}
else if ( isLongType(type) )
{
for ( size_t k = 0 ; k < kount ; k++ )
{
out << sp << byteSwap4(buf, k*size, doSwap_);
sp = " ";
}
}
else if ( isLongLongType(type) )
{
for ( size_t k = 0 ; k < kount ; k++ )
{
out << sp << byteSwap8(buf, k*size, doSwap_);
sp = " ";
}
}
else if ( isRationalType(type) )
{
for ( size_t k = 0 ; k < kount ; k++ )
{
uint32_t a = byteSwap4(buf, k*size+0, doSwap_);
uint32_t b = byteSwap4(buf, k*size+4, doSwap_);
out << sp << a << "/" << b;
sp = " ";
}
}
else if ( isStringType(type) )
out << sp << Internal::binaryToString(makeSlice(buf, 0, static_cast<size_t>(kount)));
sp = kount == count ? "" : " ...";
out << sp << std::endl;
if ( option == kpsRecursive &&
(tag == 0x8769 /* ExifTag */ || tag == 0x014a/*SubIFDs*/ || type == tiffIfd || type == tiffIfd8) )
{
for ( size_t k = 0 ; k < count ; k++ )
{
const size_t restore = io.tell();
const uint64_t ifdOffset = type == tiffIfd8?
byteSwap8(buf, k*size, doSwap_):
byteSwap4(buf, k*size, doSwap_);
printIFD(out, option, ifdOffset, depth);
io.seek(restore, BasicIo::beg);
}
}
else if ( option == kpsRecursive && tag == 0x83bb /* IPTCNAA */ )
{
if (Safe::add(count, offset) > io.size()) {
throw Error(kerCorruptedMetadata);
}
const size_t restore = io.tell();
io.seek(offset, BasicIo::beg); // position
std::vector<byte> bytes(count) ; // allocate memory
const long read_bytes = io.read(bytes.data(), count);
io.seek(restore, BasicIo::beg);
IptcData::printStructure(out, makeSliceUntil(bytes.data(), read_bytes), depth);
}
else if ( option == kpsRecursive && tag == 0x927c /* MakerNote */ && count > 10)
{
size_t restore = io.tell(); // save
long jump= 10 ;
byte bytes[20] ;
const char* chars = (const char*) &bytes[0] ;
io.seek(dir_offset, BasicIo::beg); // position
io.read(bytes,jump ) ; // read
bytes[jump]=0 ;
if ( ::strcmp("Nikon",chars) == 0 )
{
// tag is an embedded tiff
std::vector<byte> nikon_bytes(count - jump);
io.read(&nikon_bytes.at(0), (long)nikon_bytes.size());
MemIo memIo(&nikon_bytes.at(0), (long)count - jump); // create a file
std::cerr << "Nikon makernote" << std::endl;
// printTiffStructure(memIo,out,option,depth);
// TODO: fix it
}
else
{
// tag is an IFD
io.seek(0, BasicIo::beg); // position
std::cerr << "makernote" << std::endl;
printIFD(out,option,offset,depth);
}
io.seek(restore,BasicIo::beg); // restore
}
}
}
const uint64_t nextDirOffset = readData(dataSize_);
dir_offset = tooBig ? 0 : nextDirOffset;
out.flush();
} while (dir_offset != 0);
if ( bPrint )
out << Internal::indent(depth) << "END " << io.path() << std::endl;
depth--;
}
uint64_t readData(int size) const
{
const DataBuf data = Image::io().read(size);
enforce(data.size_ != 0, kerCorruptedMetadata);
uint64_t result = 0;
if (data.size_ == 1)
{}
else if (data.size_ == 2)
result = byteSwap2(data, 0, doSwap_);
else if (data.size_ == 4)
result = byteSwap4(data, 0, doSwap_);
else if (data.size_ == 8)
result = byteSwap8(data, 0, doSwap_);
else
throw Exiv2::Error(kerCorruptedMetadata);
return result;
}
};
}
Image::AutoPtr newBigTiffInstance(BasicIo::AutoPtr io, bool)
{
return Image::AutoPtr(new BigTiffImage(io));
}
bool isBigTiffType(BasicIo& io, bool advance)
{
const long pos = io.tell();
const Header header = readHeader(io);
const bool valid = header.isValid();
if (valid == false || advance == false)
io.seek(pos, BasicIo::beg);
return valid;
}
}