-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExifData.cpp
More file actions
548 lines (508 loc) · 15.7 KB
/
ExifData.cpp
File metadata and controls
548 lines (508 loc) · 15.7 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
/*
* ExifData.cpp
* Implementation of Exif data file.
*
* Created on: October 1, 2016
* Author: Isaiah Torain
* Updated on: October 27, 2016
*/
#include <fstream>
#include <string>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <vector>
#include <cstdint>
#include <algorithm>
#include "ExifData.h"
using namespace std;
// IFDEntry constructor
IFDEntry::IFDEntry() {
this->mTag = -1;
this->mFieldType = -1;
this->mCount = -1;
this->mOffsetPtr = -1;
}
// Calculated field using the count and size of field type. NOT PORTABLE
void IFDEntry::setLength() {
switch (this->mFieldType) {
case 0x1:
this->mLength = this->mCount * sizeof(uint8_t); //
break;
case 0x2:
this->mLength = this->mCount * sizeof(byte);
break;
case 0x3:
this->mLength = this->mCount * sizeof(uint16_t);
break;
case 0x4:
this->mLength = this->mCount * sizeof(uint32_t);
break;
case 0x5:
this->mLength = this->mCount * (2 * sizeof(uint32_t));
break;
case 0x6:
this->mLength = this->mCount * sizeof(int8_t);
break;
case 0x7:
this->mLength = this->mCount * sizeof(byte);
break;
case 0x8:
this->mLength = this->mCount * sizeof(int16_t);
break;
case 0x9:
this->mLength = this->mCount * sizeof(int32_t);
break;
case 0xa:
this->mLength = this->mCount * (2 * sizeof(uint32_t));
break;
case 0xb:
this->mLength = this->mCount * sizeof(float);
break;
case 0xc:
this->mLength = this->mCount * sizeof(double);
break;
default:
this->mLength = this->mCount * sizeof(byte);
break;
}
}
// Parse out all the entry information besides the value
void IFDEntry::parseIFDInfo(const byte* buffer, const byte* tiff_head) {
this->mTag = static_cast<uint16_t>(hexToInt(buffer, this->mEndianFlag, 2));
this->mRawTag[0] = buffer[0]; // first byte of the entry tag
this->mRawTag[1] = buffer[1]; // second byte of the entry tag
this->mFieldType = static_cast<uint16_t>(hexToInt(buffer + 2, this->mEndianFlag, 2));
this->mCount = static_cast<uint32_t>(hexToInt(buffer + 4, this->mEndianFlag));
this->mOffsetPtr = static_cast<uint32_t>(hexToInt(buffer + 8, this->mEndianFlag));
this->mRawValueOffset[0] = buffer[8];
this->mRawValueOffset[1] = buffer[9];
this->mRawValueOffset[2] = buffer[10];
this->mRawValueOffset[3] = buffer[11];
setLength();
parseValue(buffer + 8, tiff_head);
}
// Helper function to parse relevant value type
void IFDEntry::parseValue(const byte* buf, const byte* tiff_head) {
switch (this->mFieldType) {
case 0x1:
this->mParsedUByte = vector<uint8_t>(this->mCount);
parseUByte(buf, tiff_head);
break;
case 0x2:
this->mParsedChar = string();
parseChar(buf, tiff_head);
break;
case 0x3:
this->mParsedUShort = vector<uint16_t>(this->mCount);
parseUShort(buf, tiff_head);
break;
case 0x4:
this->mParsedULong = vector<uint32_t>(this->mCount);
parseULong(buf, tiff_head);
break;
case 0x5:
this->mParsedRational = vector<Rational>();
parseRational(buf, tiff_head);
break;
case 0x6:
this->mParsedByte = vector<int8_t>(this->mCount);
parseByte(buf, tiff_head);
break;
case 0x7:
this->mParsedChar = string();
parseChar(buf, tiff_head);
break;
case 0x8:
this->mParsedShort = vector<int16_t>(this->mCount);
parseShort(buf, tiff_head);
break;
case 0x9:
this->mParsedLong = vector<int32_t>(this->mCount);
parseLong(buf, tiff_head);
break;
case 0xa:
this->mParsedRational = vector<Rational>();
parseRational(buf, tiff_head);
break;
case 0xb:
this->mParsedFloat = vector<float>(this->mCount);
parseFloat(buf, tiff_head);
break;
case 0xc:
this->mParsedDouble = vector<double>(this->mCount);
parseDouble(buf, tiff_head);
break;
default:
this->mParsedUByte = vector<uint8_t>(this->mCount);
parseUByte(buf, tiff_head);
break;
}
}
// Add unsigned btye values to vector
void IFDEntry::parseUByte(const byte* buf, const byte* tiff_head) {
if (this->mLength <= 4) {
if (this->mEndianFlag) {
this->mParsedUByte.push_back(static_cast<uint8_t>(this->mOffsetPtr)); // store value in vector
}
else {
this->mOffsetPtr = static_cast<uint32_t>(hexToInt(buf, true)); // when value is stored in 4 byte offset it is stored as little endian
this->mParsedUByte.push_back(static_cast<uint8_t>(this->mOffsetPtr));
}
}
else {
buf = tiff_head + this->mOffsetPtr; // Offset of data values are from start of file
for (int i = 0; i < this->mCount; i++) {
this->mParsedUByte.push_back(buf[i]);
}
}
}
// Add unsigned 2 byte shorts to vector
void IFDEntry::parseUShort(const byte* buf, const byte* tiff_head) {
if (this->mLength <= 4) {
if (this->mEndianFlag) {
this->mParsedUShort.push_back(static_cast<uint16_t>(this->mOffsetPtr)); // store value in vector
}
else {
this->mOffsetPtr = static_cast<uint32_t>(hexToInt(buf, true)); // when value is stored in 4 byte offset it is stored as little endian
this->mParsedUShort.push_back(static_cast<uint16_t>(this->mOffsetPtr));
}
}
else {
buf = tiff_head + this->mOffsetPtr; // Offset of data values are from start of file
for (int i = 0; i < this->mCount; i++) {
uint16_t val = static_cast<uint16_t>(hexToInt(buf, this->mEndianFlag, 2));
this->mParsedUShort.push_back(val);
buf += 2;
}
}
}
// Add unsigned 4 byte longs to vector
void IFDEntry::parseULong(const byte* buf, const byte* tiff_head) {
if (this->mLength <= 4) {
if (this->mEndianFlag) {
this->mParsedULong.push_back(static_cast<uint32_t>(this->mOffsetPtr)); // store value in vector
}
else {
this->mOffsetPtr = static_cast<uint32_t>(hexToInt(buf, true));
this->mParsedULong.push_back(this->mOffsetPtr);
}
}
else {
buf = tiff_head + this->mOffsetPtr; // Offset of data values are from start of file
for (int i = 0; i < this->mCount; i++) {
uint32_t val = static_cast<uint32_t>(hexToInt(buf, this->mEndianFlag));
this->mParsedULong.push_back(val);
buf += 4;
}
}
}
// add unsigned byte to vector
void IFDEntry::parseChar(const byte* buf, const byte* tiff_head) {
if (this->mLength <= 4) {
for (int i = 0; i < this->mCount; i++) {
this->mParsedChar.push_back(buf[i]); // store value in vector
}
}
else {
buf = tiff_head + this->mOffsetPtr; // Offset of data values are from start of file
for (int i = 0; i < this->mCount; i++) {
this->mParsedChar.push_back(buf[i]);
}
}
}
// add a signed or unsigned rational
void IFDEntry::parseRational(const byte* buf, const byte* tiff_head) {
buf = tiff_head + this->mOffsetPtr; // Offset of data values are from start of file
for (int i = 0; i < this->mCount; i++) {
Rational r;
r.numerator = static_cast<uint32_t>(hexToInt(buf, this->mEndianFlag));
r.denominator = static_cast<uint32_t>(hexToInt(buf + 4, this->mEndianFlag));
this->mParsedRational.push_back(r);
buf += 8;
}
}
// add a float value to vector
void IFDEntry::parseFloat(const byte* buf, const byte* tiff_head) {
if (this->mLength <= 4) {
float val = hexToFloat(this->mRawValueOffset, this->mEndianFlag); // store value in vector
this->mParsedFloat.push_back(val);
}
else {
buf = tiff_head + this->mOffsetPtr; // Offset of data values are from start of file
for (int i = 0; i < this->mCount; i++) {
float val = hexToFloat(buf, this->mEndianFlag);
this->mParsedUShort.push_back(val);
buf += 4;
}
}
}
// add a double value to vector
void IFDEntry::parseDouble(const byte* buf, const byte* tiff_head) {
buf = tiff_head + this->mOffsetPtr; // Offset of data values are from start of file
for (int i = 0; i < this->mCount; i++) {
double num;
copy(buf, buf + sizeof(double), reinterpret_cast<byte*>(&num));
this->mParsedDouble.push_back(num);
buf += 8;
}
}
// add an unsigned byte to vector
void IFDEntry::parseByte(const byte* buf, const byte* tiff_head) {
if (this->mLength <= 4) {
if (this->mEndianFlag) {
this->mParsedByte.push_back(static_cast<int8_t>(this->mOffsetPtr)); // store value in vector
}
else {
this->mOffsetPtr = static_cast<uint32_t>(hexToInt(buf, true)); // when value is stored in 4 byte offset it is stored as little endian
this->mParsedUByte.push_back(static_cast<int8_t>(this->mOffsetPtr));
}
}
else {
buf = tiff_head + this->mOffsetPtr; // Offset of data values are from start of file
for (int i = 0; i < this->mCount; i++) {
this->mParsedByte.push_back(buf[i]);
}
}
}
void IFDEntry::parseShort(const byte* buf, const byte* tiff_head) {
if (this->mLength <= 4) {
if (this->mEndianFlag) {
this->mParsedShort.push_back(static_cast<int16_t>(this->mOffsetPtr)); // store value in vector
}
else {
this->mOffsetPtr = static_cast<int32_t>(hexToInt(buf, true)); // when value is stored in 4 byte offset it is stored as little endian
this->mParsedUShort.push_back(static_cast<int16_t>(this->mOffsetPtr));
}
}
else {
buf = tiff_head + this->mOffsetPtr; // Offset of data values are from start of file
for (int i = 0; i < this->mCount; i++) {
int16_t val = static_cast<int16_t>(hexToInt(buf, this->mEndianFlag, 2));
this->mParsedShort.push_back(val);
buf += 2;
}
}
}
void IFDEntry::parseLong(const byte* buf, const byte* tiff_head) {
if (this->mLength <= 4) {
if (this->mEndianFlag) {
this->mParsedLong.push_back(static_cast<int32_t>(this->mOffsetPtr)); // store value in vector
}
else {
this->mOffsetPtr = static_cast<int32_t>(hexToInt(buf, true)); // when value is stored in 4 byte offset it is stored as little endian
this->mParsedUShort.push_back(this->mOffsetPtr);
}
}
else {
buf = tiff_head + this->mOffsetPtr; // Offset of data values are from start of file
for (int i = 0; i < this->mCount; i++) {
int32_t val = static_cast<int32_t>(hexToInt(buf, this->mEndianFlag));
this->mParsedLong.push_back(val);
buf += 4;
}
}
}
// IFD constructor
IFD::IFD() {
this->mOffset = 0;
this->mNumEntries = 0;
this->mNextIFD = 0;
this->mContainsSubDirectories = false;
}
// IFD constructor to read values from the data in memory
IFD::IFD(const unsigned int ptr, bool endianFlag, const byte* tiff_head) {
const byte* entry_head = tiff_head + ptr;
uint16_t numOfEntries = static_cast<uint16_t>(hexToInt(entry_head, endianFlag, 2));
entry_head += (2 + (numOfEntries * 12));
this->mOffset = ptr;
this->mEndianFlag = endianFlag;
this->mNumEntries = numOfEntries;
this->mNextIFD = hexToInt(entry_head, this->mEndianFlag);
this->mContainsSubDirectories = false;
}
// Read entries from data in memory
bool IFD::readEntries(const byte* data, const byte* tiff_head) {
bool success = true;
this->mEntries = vector<IFDEntry>();
int i = 0;
if (this->mNumEntries == 0) {
success = false;
return success;
}
while (i < this->mNumEntries) {
IFDEntry entry = IFDEntry();
entry.mEndianFlag = this->mEndianFlag;
entry.parseIFDInfo(data, tiff_head);
if (entry.mTag == 0x8825 || entry.mTag == 0x8769) {
entry.mOffsetPtr = hexToInt(entry.mRawValueOffset, this->mEndianFlag); // since this technically fits into 4 bytes the value has to be recalculated.
this->mContainsSubDirectories = true;
}
this->mEntries.push_back(entry);
data += 12;
i++;
}
return success;
}
//**** Constructors ****//
ExifData::ExifData() {
this->mSize = 0;
this->mData = NULL;
}
//**************************//
//**** Desstructor ****//
ExifData::~ExifData() {
delete this->mData;
this->mData = NULL;
}
//**********************//
//****** Member Functions ******//
// Read in all data to memory
bool ExifData::readIn(ifstream& istr) {
byte item;
this->mSize = istr.tellg();
this->mData = new byte[this->mSize];
istr.seekg(0, ios::beg);
istr.read((char*)this->mData, this->mSize); //read in entire file into memory have to cast to byte to use read
this->mData = (byte*)this->mData; // cast back to unsigned characters
if (istr.fail()) {
cerr << "Error: binary reading in failed. " << endl;
return false;
}
if (!this->readHeader(istr, this->mData)) {
return false;
}
else {
return true;
}
}
// Function to read in exif header data
bool ExifData::readHeader(ifstream& istr, byte* const block) {
bool success = false;
byte* currentByteValue = block; // Keep a pointer to the memory location
byte little_endian [] = {'I', 'I'}; // Tiff specification for little-endian
byte big_endian [] = {'M', 'M'}; // Tiff specification for big-endian
byte tiff_identifier [] = {'4', '2'}; // How to identify Tiff spec
if (block[0] == little_endian[0] && block[1] == little_endian[1]) {
this->mEndianFlag = true; // True is for little endian
}
else if (block[0] == big_endian[0] && block[1] == big_endian[1]) {
this->mEndianFlag = false; // false is for big endian
}
if (!block[2] == tiff_identifier[0] && !block[3] == tiff_identifier[1]) {
cerr << "Error: This is not a tiff file!" << endl; // change this because byte order could be different
return success;
}
// grab the offset
currentByteValue += 4;
int offset = hexToInt(currentByteValue, this->mEndianFlag);
if (offset == -1) {
cerr << "Error: Received an invalid offset!" << endl;
return success;
}
else {
success = readIFDs(offset); // read IFDs
}
return success;
}
bool ExifData::readIFDs(int offset_ptr) {
bool success = true;
byte* currentByteValue = this->mData + offset_ptr;
IFD temp = IFD((unsigned int)offset_ptr, this->mEndianFlag, this->mData);
currentByteValue += 2;
if (!temp.readEntries(currentByteValue, this->mData)) {
cerr << "Error parsing IFD! Data may be corrupt." << endl;
success = false;
return success;
}
unsigned int nextIFDPtr = temp.mNextIFD;
this->mIFDs.push_back(temp);
if (temp.mContainsSubDirectories) {
if (!readSubIFDs(temp)) {
cerr << "Error parsing sub IFD! Data may be corrupt." << endl;
success = false;
return success;
}
}
while (nextIFDPtr != 0) { // Dont know how many IFDs so I have to parse each one and go until the last 4 bytes are 0000.
temp = IFD((unsigned int)nextIFDPtr, this->mEndianFlag, this->mData);
currentByteValue = this->mData + nextIFDPtr + 2;
if (!temp.readEntries(currentByteValue, this->mData)) {
cerr << "Error parsing IFD! Data may be corrupt." << endl;
success = false;
break;
}
nextIFDPtr = temp.mNextIFD;
this->mIFDs.push_back(temp);
if (temp.mContainsSubDirectories) {
if (!readSubIFDs(temp)) {
cerr << "Error parsing sub IFD! Data may be corrupt." << endl;
success = false;
return success;
}
}
}
return success;
}
// Read in the sub IFDs GPS and Exif data
bool ExifData::readSubIFDs(const IFD& subIFD) {
bool success = true;
for (auto& entry : subIFD.mEntries) {
if (entry.mTag == 0x8825 || entry.mTag == 0x8769) {
// Create a sub IFD and parse it's entries.
IFD temp = IFD(entry.mOffsetPtr, this->mEndianFlag, this->mData);
if (!temp.readEntries(this->mData + entry.mOffsetPtr + 2, this->mData)) {;
cerr << "Error reading in sub IFD!" << endl;
success = false;
break;
}
this->mIFDs.push_back(temp);
}
}
return success;
}
//******************************//
int hexToInt(const byte* data, bool flag, int size) {
int num = -1;
if (flag) { // litte endian
if (size == 2) {
num = ((data[1] << 8) | data[0]);
}
else {
num = ((data[3] << 24) |
(data[2] << 16) |
(data[1] << 8) |
(data[0]));
}
}
else { // big endian
if (size == 2) {
num = ((data[0] << 8) | data[1]);
}
else {
num = ((data[0] << 24) |
(data[1] << 16) |
(data[2] << 8) |
(data[3]));
}
}
return num;
}
float hexToFloat(const byte* data, bool flag) {
float num;
if (flag) { // little endian
*((byte*)(&num) + 3) = data[0];
*((byte*)(&num) + 2) = data[1];
*((byte*)(&num) + 1) = data[2];
*((byte*)(&num) + 0) = data[3];
return num;
}
else { // big endian
*((byte*)(&num) + 3) = data[3];
*((byte*)(&num) + 2) = data[2];
*((byte*)(&num) + 1) = data[1];
*((byte*)(&num) + 0) = data[0];
return num;
}
}
;