forked from ImpulseAdventure/JPEGsnoop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DbSigs.cpp
1022 lines (854 loc) · 28.8 KB
/
DbSigs.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
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
// JPEGsnoop - JPEG Image Decoder & Analysis Utility
// Copyright (C) 2017 - Calvin Hass
// http://www.impulseadventure.com/photo/jpeg-snoop.html
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
#include "stdafx.h"
#include "DbSigs.h"
#include "General.h"
#include "snoop.h"
#include "Signatures.inl"
#define MAX_BUF_SET_FILE 131072
// TODO:
// - Convert m_sSigListExtra[] to use CObArray instead of managing it
// directly. Otherwise we are inefficient with memory and could potentially
// not allocate enough space.
// Initialize the signature database array sizes
//
// PRE:
// - m_sSigList[]
// - m_sExcMmNoMkrList[]
// - m_sExcMmIsEditList[]
// - m_sSwIjgList[]
// - m_sXComSwList[]
//
// POST:
// - m_nSigListNum
// - m_nSigListExtraNum
// - m_nExcMmNoMkrListNum
// - m_nExcMmIsEditListNum
// - m_nSwIjgListNum
// - m_nXcomSwListNum
// - m_strDbDir
//
CDbSigs::CDbSigs()
{
// Count the built-in database
bool bDone;
m_bFirstRun = false;
bDone = false;
m_nSigListNum = 0;
while (!bDone)
{
if (!_tcscmp(m_sSigList[m_nSigListNum].strXMake,_T("*")))
bDone = true;
else
m_nSigListNum++;
}
// Count number of exceptions in Signatures.inl
bDone = false;
m_nExcMmNoMkrListNum = 0;
while (!bDone)
{
if (!_tcscmp(m_sExcMmNoMkrList[m_nExcMmNoMkrListNum].strXMake,_T("*")))
bDone = true;
else
m_nExcMmNoMkrListNum++;
}
bDone = false;
m_nExcMmIsEditListNum = 0;
while (!bDone)
{
if (!_tcscmp(m_sExcMmIsEditList[m_nExcMmIsEditListNum].strXMake,_T("*")))
bDone = true;
else
m_nExcMmIsEditListNum++;
}
bDone = false;
m_nSwIjgListNum = 0;
while (!bDone) {
if (!_tcscmp(m_sSwIjgList[m_nSwIjgListNum],_T("*")))
bDone = true;
else
m_nSwIjgListNum++;
}
bDone = false;
m_nXcomSwListNum = 0;
while (!bDone) {
if (!_tcscmp(m_sXComSwList[m_nXcomSwListNum],_T("*")))
bDone = true;
else
m_nXcomSwListNum++;
}
// Reset extra database
m_nSigListExtraNum = 0;
// Default to user database dir not set yet
// This will cause a fail if database load/store
// functions are called before SetDbDir()
m_strDbDir = _T("");
}
CDbSigs::~CDbSigs()
{
}
// Is this the first time running the application?
// If so, we skip certain warning messages (such as lack of existing user DB file)
void CDbSigs::SetFirstRun(bool bFirstRun)
{
m_bFirstRun = bFirstRun;
}
unsigned CDbSigs::GetNumSigsInternal()
{
return m_nSigListNum;
}
unsigned CDbSigs::GetNumSigsExtra()
{
return m_nSigListExtraNum;
}
// Read an unsigned integer (4B) from the buffer
bool CDbSigs::BufReadNum(PBYTE pBuf,unsigned &nOut,unsigned nMaxBytes,unsigned &nOffsetBytes)
{
nMaxBytes; // Unreferenced param
ASSERT(pBuf);
// TODO: check for buffer bounds
nOut = (unsigned)pBuf[nOffsetBytes];
nOffsetBytes += sizeof(unsigned);
return true;
}
// Write an unsigned integer (4B) to the buffer
bool CDbSigs::BufWriteNum(PBYTE pBuf,unsigned nIn,unsigned nMaxBytes,unsigned &nOffsetBytes)
{
nMaxBytes; // Unreferenced param
ASSERT(pBuf);
// TODO: check for buffer bounds
PBYTE pBufBase;
unsigned* pBufInt;
pBufBase = &pBuf[nOffsetBytes];
pBufInt = (unsigned*)pBufBase;
pBufInt[0] = nIn;
nOffsetBytes += sizeof(unsigned);
return true;
}
// Attempt to read a line from the buffer
// This is a replacement for CStdioFile::ReadLine()
// Both 16-bit unicode and 8-bit SBCS encoding modes are supported (via bUni)
// Offset parameter is incremented accordingly
// Supports both newline and NULL for string termination
bool CDbSigs::BufReadStr(PBYTE pBuf,CString &strOut,unsigned nMaxBytes,bool bUni,unsigned &nOffsetBytes)
{
ASSERT(pBuf);
CString strOutTmp;
bool bDone;
char chAsc;
wchar_t chUni;
unsigned nCharSz = ((bUni)?sizeof(wchar_t):sizeof(char));
bDone = false;
strOut = _T("");
// Ensure we don't overrun the buffer by calculating the last
// byte index required for each iteration.
for (unsigned nInd=nOffsetBytes;(!bDone)&&(nInd+nCharSz-1<nMaxBytes);nInd+=nCharSz) {
if (bUni) {
chUni = pBuf[nInd];
if ( (chUni != '\n') && (chUni != 0) ) {
strOut += chUni;
} else {
bDone = true;
nOffsetBytes = nInd+nCharSz;
}
} else {
chAsc = pBuf[nInd];
if ( (chAsc != '\n') && (chAsc != 0) ) {
strOut += chAsc;
} else {
bDone = true;
nOffsetBytes = nInd+nCharSz;
}
}
}
if (!bDone) {
nOffsetBytes = nMaxBytes;
// The input was not terminated, so we're still going to return what we got so far
return false;
}
return true;
}
// Return true if we managed to write entire string including terminator
// without overrunning nMaxBytes
bool CDbSigs::BufWriteStr(PBYTE pBuf,CString strIn,unsigned nMaxBytes,bool bUni,unsigned &nOffsetBytes)
{
ASSERT(pBuf);
bool bRet = false;
char chAsc;
wchar_t chUni;
unsigned nCharSz = ((bUni)?sizeof(wchar_t):sizeof(char));
PBYTE pBufBase;
LPWSTR pBufUni;
LPSTR pBufAsc;
pBufBase = pBuf + nOffsetBytes;
pBufUni = (LPWSTR)pBufBase;
pBufAsc = (LPSTR)pBufBase;
#ifdef UNICODE
// Create non-Unicode version of string
// Ref: http://social.msdn.microsoft.com/Forums/vstudio/en-US/85f02321-de88-47d2-98c8-87daa839a98e/how-to-convert-cstring-to-const-char-?forum=vclanguage
// Added constant specifier
LPCSTR pszNonUnicode;
USES_CONVERSION;
// Not specifying code page explicitly but assume content
// should be ASCII. Default code page is probably Windows-1252.
pszNonUnicode = CW2A( strIn.LockBuffer( ) );
strIn.UnlockBuffer( );
#endif
unsigned nStrLen;
unsigned nChInd;
nStrLen = strIn.GetLength();
for (nChInd=0;(nChInd<nStrLen)&&(nOffsetBytes+nCharSz-1<nMaxBytes);nChInd++) {
if (bUni) {
// Normal handling for unicode
chUni = strIn.GetAt(nChInd);
pBufUni[nChInd] = chUni;
} else {
#ifdef UNICODE
// To avoid Warning C4244: Conversion from 'wchar_t' to 'char' possible loss of data
// We need to implement conversion here
// Ref: http://stackoverflow.com/questions/4786292/converting-unicode-strings-and-vice-versa
// Since we have compiled for unicode, the CString character fetch
// will be unicode char. Therefore we need to use ANSI-converted form.
chAsc = pszNonUnicode[nChInd];
#else
// Since we have compiled for non-Unicode, the CString character fetch
// will be single byte char
chAsc = strIn.GetAt(nChInd);
#endif
pBufAsc[nChInd] = chAsc;
}
// Advance pointers
nOffsetBytes += nCharSz;
}
// Now terminate if we have space
if ((nOffsetBytes + nCharSz-1) < nMaxBytes) {
if (bUni) {
chUni = wchar_t(0);
pBufUni[nChInd] = chUni;
} else {
chAsc = char(0);
pBufAsc[nChInd] = chAsc;
}
// Advance pointers
nOffsetBytes += nCharSz;
// Since we managed to include terminator, return is successful
bRet = true;
}
// Return true if we finished the string write (without exceeding nMaxBytes)
// or false otherwise
return bRet;
}
void CDbSigs::DatabaseExtraLoad()
{
CFile *pInFile = NULL;
PBYTE pBuf = NULL;
unsigned nBufLenBytes = 0;
unsigned nBufOffset = 0;
CString strError;
ASSERT(m_strDbDir != _T(""));
if (m_strDbDir == _T("")) {
return;
}
// Retrieve from environment user profile path.
// FIXME: Increase maximum file path size
TCHAR szFilePathName[200];
_stprintf_s(szFilePathName,_T("%s\\%s"),(LPCTSTR)m_strDbDir,DAT_FILE);
// Open the input file
try
{
// Open specified file
pInFile = new CFile(szFilePathName, CFile::modeRead );
}
catch (CFileException* e)
{
TCHAR msg[MAX_BUF_EX_ERR_MSG];
e->GetErrorMessage(msg,MAX_BUF_EX_ERR_MSG);
e->Delete();
// To avoid any user confusion, we disable this warning message if this
// is the first time the program has been run (when we would expect that the user
// database is not present). After first run, the user database will have been
// created.
// Take from SnoopConfig.bEulaAccepted
if (!m_bFirstRun) {
strError.Format(_T("Couldn't find User Signature Database\n\n[%s]\n\nCreating default database"),(LPCTSTR)msg);
OutputDebugString(strError);
AfxMessageBox(strError);
}
pInFile = NULL;
// Now create default database file in current DB location
DatabaseExtraStore();
return;
}
// Allocate the input buffer
pBuf = new BYTE [MAX_BUF_SET_FILE];
ASSERT(pBuf);
// Load the buffer
nBufLenBytes = pInFile->Read(pBuf,MAX_BUF_SET_FILE);
ASSERT(nBufLenBytes>0);
// TODO: Error check for config file longer than max buffer!
ASSERT(nBufLenBytes<MAX_BUF_SET_FILE);
nBufOffset = 0;
CString strLine;
CString strParam;
CString strVal;
CString strTmp;
CString strVer;
CString strSec;
//bool bErr = false;
bool bDone = false;
BOOL bRet;
unsigned nBufOffsetTmp;
bool bFileOk = false;
bool bFileModeUni = false;
bool bValid;
unsigned nNumLoad = 0; // Number of entries to read
CompSig sDbLocalEntry; // Temp entry for load
bool bDbLocalEntryFound; // Temp entry already in built-in DB?
bool bDbLocalTrimmed = false; // Did we trim down the DB?
// Determine if config file is in Unicode or SBCS
// If the file is in SBCS (legacy) then back it up
// and then write-back in unicode mode.
// Test unicode mode
nBufOffsetTmp = 0;
bRet = BufReadStr(pBuf,strLine,nBufLenBytes,true,nBufOffsetTmp);
if (strLine.Compare(_T("JPEGsnoop"))==0) {
bFileOk = true;
bFileModeUni = true;
nBufOffset = nBufOffsetTmp;
}
// Test SBCS mode
nBufOffsetTmp = 0;
bRet = BufReadStr(pBuf,strLine,nBufLenBytes,false,nBufOffsetTmp);
if (strLine.Compare(_T("JPEGsnoop"))==0) {
bFileOk = true;
nBufOffset = nBufOffsetTmp;
}
if (!bFileOk) {
strError.Format(_T("WARNING: User Signature Database corrupt\n[%s]\nProceeding with defaults."),
(LPCTSTR)szFilePathName);
OutputDebugString(strError);
AfxMessageBox(strError);
// Close the file to ensure no sharing violation
if (pInFile) {
pInFile->Close();
delete pInFile;
pInFile = NULL;
}
// Copy old config file
TCHAR szFilePathNameBak[200];
_stprintf_s(szFilePathNameBak,_T("%s\\%s.bak"),(LPCTSTR)m_strDbDir,DAT_FILE);
CopyFile(szFilePathName,szFilePathNameBak,false);
// Now rewrite file in latest version
DatabaseExtraStore();
// Notify user
strTmp.Format(_T("Created default User Signature Database. Backup of old DB in [%s]"),szFilePathNameBak);
AfxMessageBox(strTmp);
return;
}
m_nSigListExtraNum = 0;
// Version
bRet = BufReadStr(pBuf,strVer,nBufLenBytes,bFileModeUni,nBufOffset);
bValid = false;
if (strVer == _T("00")) { bValid = false; }
if (strVer == _T("01")) { bValid = true; }
if (strVer == _T("02")) { bValid = true; }
if (strVer == _T("03")) { bValid = true; }
// Should consider trimming down local database if same entry
// exists in built-in database (for example, user starts running
// new version of tool).
while (!bDone && bValid) {
// Read section header
bRet = BufReadStr(pBuf,strSec,nBufLenBytes,bFileModeUni,nBufOffset);
if (strSec == _T("*DB*")) {
// Read DB count
bRet = BufReadNum(pBuf,nNumLoad,nBufLenBytes,nBufOffset);
// For each entry that we read, we should double-check to see
// if we already have it in the built-in DB. If we do, then
// don't add it to the runtime DB, and mark the local DB as
// needing trimming. If so, rewrite the DB.
for (unsigned ind=0;ind<nNumLoad;ind++) {
sDbLocalEntry.bValid = false;
sDbLocalEntry.strXMake = _T("");
sDbLocalEntry.strXModel = _T("");
sDbLocalEntry.strUmQual = _T("");
sDbLocalEntry.strCSig = _T("");
sDbLocalEntry.strCSigRot = _T("");
sDbLocalEntry.strXSubsamp = _T("");
sDbLocalEntry.strMSwTrim = _T("");
sDbLocalEntry.strMSwDisp = _T("");
bDbLocalEntryFound = false;
if (strVer == _T("01")) {
// For version 01:
bRet = BufReadStr(pBuf,sDbLocalEntry.strXMake,nBufLenBytes,bFileModeUni,nBufOffset);
bRet = BufReadStr(pBuf,sDbLocalEntry.strXModel,nBufLenBytes,bFileModeUni,nBufOffset);
strTmp = _T("");
bRet = BufReadStr(pBuf,strTmp,nBufLenBytes,bFileModeUni,nBufOffset);
sDbLocalEntry.eEditor = static_cast<teEditor>(_tstoi(strTmp));
bRet = BufReadStr(pBuf,sDbLocalEntry.strUmQual,nBufLenBytes,bFileModeUni,nBufOffset);
bRet = BufReadStr(pBuf,sDbLocalEntry.strCSig,nBufLenBytes,bFileModeUni,nBufOffset);
// In older version of DB, these entries won't exist
bRet = BufReadStr(pBuf,sDbLocalEntry.strXSubsamp,nBufLenBytes,bFileModeUni,nBufOffset);
bRet = BufReadStr(pBuf,sDbLocalEntry.strMSwTrim,nBufLenBytes,bFileModeUni,nBufOffset);
bRet = BufReadStr(pBuf,sDbLocalEntry.strMSwDisp,nBufLenBytes,bFileModeUni,nBufOffset);
} else if ( (strVer == _T("02")) || (strVer == _T("03")) ) {
// For version 02 or 03:
// NOTE: Difference between 02 and 03:
// - 02 : SBCS format
// - 03 : Unicode format
bRet = BufReadStr(pBuf,sDbLocalEntry.strXMake,nBufLenBytes,bFileModeUni,nBufOffset);
bRet = BufReadStr(pBuf,sDbLocalEntry.strXModel,nBufLenBytes,bFileModeUni,nBufOffset);
strTmp = _T("");
bRet = BufReadStr(pBuf,strTmp,nBufLenBytes,bFileModeUni,nBufOffset);
sDbLocalEntry.eEditor = static_cast<teEditor>(_tstoi(strTmp));
bRet = BufReadStr(pBuf,sDbLocalEntry.strUmQual,nBufLenBytes,bFileModeUni,nBufOffset);
bRet = BufReadStr(pBuf,sDbLocalEntry.strCSig,nBufLenBytes,bFileModeUni,nBufOffset);
bRet = BufReadStr(pBuf,sDbLocalEntry.strCSigRot,nBufLenBytes,bFileModeUni,nBufOffset);
// In older version of DB, these entries won't exist
bRet = BufReadStr(pBuf,sDbLocalEntry.strXSubsamp,nBufLenBytes,bFileModeUni,nBufOffset);
bRet = BufReadStr(pBuf,sDbLocalEntry.strMSwTrim,nBufLenBytes,bFileModeUni,nBufOffset);
bRet = BufReadStr(pBuf,sDbLocalEntry.strMSwDisp,nBufLenBytes,bFileModeUni,nBufOffset);
}
// -------------------------------------------------------
// Does the entry already exist in the internal DB?
bDbLocalEntryFound = SearchSignatureExactInternal(sDbLocalEntry.strXMake,sDbLocalEntry.strXModel,sDbLocalEntry.strCSig);
if (!bDbLocalEntryFound) {
// Add it!
m_sSigListExtra[m_nSigListExtraNum].bValid = true;
m_sSigListExtra[m_nSigListExtraNum].strXMake = sDbLocalEntry.strXMake;
m_sSigListExtra[m_nSigListExtraNum].strXModel = sDbLocalEntry.strXModel;
m_sSigListExtra[m_nSigListExtraNum].eEditor = sDbLocalEntry.eEditor;
m_sSigListExtra[m_nSigListExtraNum].strUmQual = sDbLocalEntry.strUmQual;
m_sSigListExtra[m_nSigListExtraNum].strCSig = sDbLocalEntry.strCSig;
m_sSigListExtra[m_nSigListExtraNum].strCSigRot = sDbLocalEntry.strCSigRot;
m_sSigListExtra[m_nSigListExtraNum].strXSubsamp = sDbLocalEntry.strXSubsamp;
m_sSigListExtra[m_nSigListExtraNum].strMSwTrim = sDbLocalEntry.strMSwTrim;
m_sSigListExtra[m_nSigListExtraNum].strMSwDisp = sDbLocalEntry.strMSwDisp;
m_nSigListExtraNum++;
} else {
bDbLocalTrimmed = true;
}
}
} else if (strSec == _T("*Z*")) {
bDone = true;
} else {
bValid = false;
} // strSec
} // while
// ----------------------
if (pInFile) {
pInFile->Close();
delete pInFile;
pInFile = NULL;
}
if (pBuf) {
delete [] pBuf;
pBuf = NULL;
}
// If we did make changes to the database (trim), then rewrite it!
// Ensure that we have closed the file above before we rewrite it
// otherwise we could end up with a sharing violation
if (bDbLocalTrimmed) {
DatabaseExtraStore();
}
// Now is this an old config file version? If so,
// create a backup and then rewrite the new version
if (bValid) {
if (strVer != DB_VER_STR) {
// Copy old config file
TCHAR szFilePathNameBak[200];
_stprintf_s(szFilePathNameBak,_T("%s\\%s.bak"),(LPCTSTR)m_strDbDir,DAT_FILE);
CopyFile(szFilePathName,szFilePathNameBak,false);
// Now rewrite file in latest version
DatabaseExtraStore();
// Notify user
strError.Format(_T("Upgraded User Signature Database. Backup in:\n%s"),szFilePathNameBak);
AfxMessageBox(strError);
}
}
}
void CDbSigs::DatabaseExtraClean()
{
m_nSigListExtraNum = 0;
DatabaseExtraStore();
}
unsigned CDbSigs::DatabaseExtraGetNum()
{
return m_nSigListExtraNum;
}
CompSig CDbSigs::DatabaseExtraGet(unsigned nInd)
{
ASSERT(nInd<m_nSigListExtraNum);
return m_sSigListExtra[nInd];
}
void CDbSigs::DatabaseExtraStore()
{
CFile *pOutFile = NULL;
PBYTE pBuf = NULL;
//unsigned nBufLenBytes = 0;
unsigned nBufOffset = 0;
bool bModeUni = true; // Save in Unicode format
ASSERT(m_strDbDir != _T(""));
if (m_strDbDir == _T("")) {
return;
}
// Retrieve from environment user profile path.
TCHAR szFilePathName[200];
_stprintf_s(szFilePathName,_T("%s\\%s"),(LPCTSTR)m_strDbDir,DAT_FILE);
// Open the output file
try
{
// Open specified file
pOutFile = new CFile(szFilePathName, CFile::modeWrite | CFile::typeBinary | CFile::modeCreate );
}
catch (CFileException* e)
{
CString strError;
TCHAR msg[MAX_BUF_EX_ERR_MSG];
e->GetErrorMessage(msg,MAX_BUF_EX_ERR_MSG);
e->Delete();
strError.Format(_T("ERROR: Couldn't open file: [%s]"),(LPCTSTR)msg);
OutputDebugString(strError);
AfxMessageBox(strError);
pOutFile = NULL;
return;
}
// Allocate the output buffer
pBuf = new BYTE [MAX_BUF_SET_FILE];
ASSERT(pBuf);
nBufOffset = 0;
CString strLine;
CString strParam;
CString strVal;
//bool bErr = false;
//bool bDone = false;
BOOL bRet;
unsigned nMaxBufBytes = MAX_BUF_SET_FILE;
bRet = BufWriteStr(pBuf,_T("JPEGsnoop"),nMaxBufBytes,bModeUni,nBufOffset);
bRet = BufWriteStr(pBuf,_T(DB_VER_STR),nMaxBufBytes,bModeUni,nBufOffset);
bRet = BufWriteStr(pBuf,_T("*DB*"),nMaxBufBytes,bModeUni,nBufOffset);
// Determine how many entries will remain (after removing marked
// deleted entries
unsigned nNewExtraNum = m_nSigListExtraNum;
for (unsigned nInd=0;nInd<m_nSigListExtraNum;nInd++) {
if (!m_sSigListExtra[nInd].bValid) {
nNewExtraNum--;
}
}
bRet = BufWriteNum(pBuf,nNewExtraNum,nMaxBufBytes,nBufOffset);
for (unsigned nInd=0;nInd<m_nSigListExtraNum;nInd++) {
// Is it marked for deletion (from Manage dialog?)
if (!m_sSigListExtra[nInd].bValid) {
continue;
}
CString strTmp;
// For version 02?
bRet = BufWriteStr(pBuf,m_sSigListExtra[nInd].strXMake,nMaxBufBytes,bModeUni,nBufOffset);
bRet = BufWriteStr(pBuf,m_sSigListExtra[nInd].strXModel,nMaxBufBytes,bModeUni,nBufOffset);
strTmp.Format(_T("%u"),m_sSigListExtra[nInd].eEditor); // eEditor = u_source
bRet = BufWriteStr(pBuf,strTmp,nMaxBufBytes,bModeUni,nBufOffset);
bRet = BufWriteStr(pBuf,m_sSigListExtra[nInd].strUmQual,nMaxBufBytes,bModeUni,nBufOffset);
bRet = BufWriteStr(pBuf,m_sSigListExtra[nInd].strCSig,nMaxBufBytes,bModeUni,nBufOffset);
bRet = BufWriteStr(pBuf,m_sSigListExtra[nInd].strCSigRot,nMaxBufBytes,bModeUni,nBufOffset);
bRet = BufWriteStr(pBuf,m_sSigListExtra[nInd].strXSubsamp,nMaxBufBytes,bModeUni,nBufOffset);
bRet = BufWriteStr(pBuf,m_sSigListExtra[nInd].strMSwTrim,nMaxBufBytes,bModeUni,nBufOffset);
bRet = BufWriteStr(pBuf,m_sSigListExtra[nInd].strMSwDisp,nMaxBufBytes,bModeUni,nBufOffset);
}
bRet = BufWriteStr(pBuf,_T("*Z*"),nMaxBufBytes,bModeUni,nBufOffset);
// --------------------------
// Now write out the buffer
if (pOutFile) {
pOutFile->Write(pBuf,nBufOffset);
pOutFile->Close();
delete pOutFile;
pOutFile = NULL;
}
if (pBuf) {
delete [] pBuf;
pBuf = NULL;
}
}
void CDbSigs::DatabaseExtraAdd(CString strExifMake,CString strExifModel,
CString strQual,CString strSig,CString strSigRot,CString strCss,
teSource eUserSource,CString strUserSoftware)
{
ASSERT(m_nSigListExtraNum < DBEX_ENTRIES_MAX);
if (m_nSigListExtraNum >= DBEX_ENTRIES_MAX) {
CString strTmp;
strTmp.Format(_T("ERROR: Can only store maximum of %u extra signatures in local DB"),DBEX_ENTRIES_MAX);
AfxMessageBox(strTmp);
return;
}
// Now append it to the local database and resave
m_sSigListExtra[m_nSigListExtraNum].strXMake = strExifMake;
m_sSigListExtra[m_nSigListExtraNum].strXModel = strExifModel;
m_sSigListExtra[m_nSigListExtraNum].strUmQual = strQual;
m_sSigListExtra[m_nSigListExtraNum].strCSig = strSig;
m_sSigListExtra[m_nSigListExtraNum].strCSigRot = strSigRot;
m_sSigListExtra[m_nSigListExtraNum].strXSubsamp = strCss;
if (eUserSource == ENUM_SOURCE_CAM) { // digicam
m_sSigListExtra[m_nSigListExtraNum].eEditor = ENUM_EDITOR_CAM;
m_sSigListExtra[m_nSigListExtraNum].strMSwTrim = _T("");
m_sSigListExtra[m_nSigListExtraNum].strMSwDisp = _T("");
} else if (eUserSource == ENUM_SOURCE_SW) { // software
m_sSigListExtra[m_nSigListExtraNum].eEditor = ENUM_EDITOR_SW;
m_sSigListExtra[m_nSigListExtraNum].strMSwTrim = _T("");
m_sSigListExtra[m_nSigListExtraNum].strMSwDisp = strUserSoftware; // Not quite right perfect
m_sSigListExtra[m_nSigListExtraNum].strXMake = _T("");
m_sSigListExtra[m_nSigListExtraNum].strXModel = _T("");
} else { // user didn't know
m_sSigListExtra[m_nSigListExtraNum].eEditor = ENUM_EDITOR_UNSURE;
m_sSigListExtra[m_nSigListExtraNum].strMSwTrim = _T("");
m_sSigListExtra[m_nSigListExtraNum].strMSwDisp = strUserSoftware; // Not quite right perfect
}
m_nSigListExtraNum++;
// Now resave the database
DatabaseExtraStore();
}
// TODO: Should we include editors in this search?
bool CDbSigs::SearchSignatureExactInternal(CString strMake, CString strModel, CString strSig)
{
bool bFoundExact = false;
bool bDone = false;
unsigned nInd = 0;
while (!bDone) {
if (nInd >= m_nSigListNum) {
bDone = true;
} else {
if ( (m_sSigList[nInd].strXMake == strMake) &&
(m_sSigList[nInd].strXModel == strModel) &&
((m_sSigList[nInd].strCSig == strSig) || (m_sSigList[nInd].strCSigRot == strSig)) )
{
bFoundExact = true;
bDone = true;
}
nInd++;
}
}
return bFoundExact;
}
bool CDbSigs::SearchCom(CString strCom)
{
bool bFound = false;
bool bDone = false;
unsigned nInd = 0;
if (strCom.GetLength() == 0) {
bDone = true;
}
while (!bDone) {
if (nInd >= m_nXcomSwListNum) {
bDone = true;
} else {
if (strCom.Find(m_sXComSwList[nInd]) != -1) {
bFound = true;
bDone = true;
}
nInd++;
}
}
return bFound;
}
// Returns total of built-in plus local DB
unsigned CDbSigs::GetDBNumEntries()
{
return (m_nSigListNum + m_nSigListExtraNum);
}
// Returns total of built-in plus local DB
unsigned CDbSigs::IsDBEntryUser(unsigned nInd)
{
if (nInd < m_nSigListNum) {
return false;
} else {
return true;
}
}
// Return a ptr to the struct containing the indexed entry
bool CDbSigs::GetDBEntry(unsigned nInd,CompSig* pEntry)
{
unsigned nIndOffset;
unsigned nIndMax = GetDBNumEntries();
ASSERT(pEntry);
ASSERT(nInd<nIndMax);
if (nInd < m_nSigListNum) {
pEntry->eEditor = m_sSigList[nInd].eEditor;
pEntry->strXMake = m_sSigList[nInd].strXMake;
pEntry->strXModel = m_sSigList[nInd].strXModel;
pEntry->strUmQual = m_sSigList[nInd].strUmQual;
pEntry->strCSig = m_sSigList[nInd].strCSig;
pEntry->strCSigRot = m_sSigList[nInd].strCSigRot;
pEntry->strXSubsamp = m_sSigList[nInd].strXSubsamp;
pEntry->strMSwTrim = m_sSigList[nInd].strMSwTrim;
pEntry->strMSwDisp = m_sSigList[nInd].strMSwDisp;
return true;
} else {
nIndOffset = nInd-m_nSigListNum;
pEntry->eEditor = m_sSigListExtra[nIndOffset].eEditor;
pEntry->strXMake = m_sSigListExtra[nIndOffset].strXMake;
pEntry->strXModel = m_sSigListExtra[nIndOffset].strXModel;
pEntry->strUmQual = m_sSigListExtra[nIndOffset].strUmQual;
pEntry->strCSig = m_sSigListExtra[nIndOffset].strCSig;
pEntry->strCSigRot = m_sSigListExtra[nIndOffset].strCSigRot;
pEntry->strXSubsamp = m_sSigListExtra[nIndOffset].strXSubsamp;
pEntry->strMSwTrim = m_sSigListExtra[nIndOffset].strMSwTrim;
pEntry->strMSwDisp = m_sSigListExtra[nIndOffset].strMSwDisp;
return true;
}
}
void CDbSigs::SetEntryValid(unsigned nInd,bool bValid)
{
// TODO: Bounds check
ASSERT(nInd < m_nSigListExtraNum);
m_sSigListExtra[nInd].bValid = bValid;
}
unsigned CDbSigs::GetIjgNum()
{
return m_nSwIjgListNum;
}
LPTSTR CDbSigs::GetIjgEntry(unsigned nInd)
{
return m_sSwIjgList[nInd];
}
// Update the directory used for user database
void CDbSigs::SetDbDir(CString strDbDir)
{
m_strDbDir = strDbDir;
}
// Search exceptions for Make/Model in list of ones that don't have Makernotes
bool CDbSigs::LookupExcMmNoMkr(CString strMake,CString strModel)
{
bool bFound = false;
bool bDone = false;
unsigned nInd = 0;
if (strMake.GetLength() == 0) {
bDone = true;
}
while (!bDone) {
if (nInd >= m_nExcMmNoMkrListNum) {
bDone = true;
} else {
// Perform exact match on Make, case sensitive
// Check Make field and possibly Model field (if non-empty)
if (_tcscmp(m_sExcMmNoMkrList[nInd].strXMake,strMake) != 0) {
// Make did not match
} else {
// Make matched, now check to see if we need
// to compare the Model string
if (_tcslen(m_sExcMmNoMkrList[nInd].strXModel) == 0) {
// No need to compare, we're bDone
bFound = true;
bDone = true;
} else {
// Need to check model as well
// Since we may like to do a substring match, support wildcards
// FnInd position of "*" if it exists in DB entry
LPTSTR pWildcard;
unsigned nCompareLen;
pWildcard = _tcschr(m_sExcMmNoMkrList[nInd].strXModel,'*');
if (pWildcard != NULL) {
// Wildcard present
nCompareLen = pWildcard - (m_sExcMmNoMkrList[nInd].strXModel);
} else {
// No wildcard, do full match
nCompareLen = _tcslen(m_sExcMmNoMkrList[nInd].strXModel);
}
if (_tcsnccmp(m_sExcMmNoMkrList[nInd].strXModel,strModel,nCompareLen) != 0) {
// No match
} else {
// Matched as well, we're bDone
bFound = true;
bDone = true;
}
}
}
nInd++;
}
}
return bFound;
}
// Search exceptions for Make/Model in list of ones that are always edited
bool CDbSigs::LookupExcMmIsEdit(CString strMake,CString strModel)
{
bool bFound = false;
bool bDone = false;
unsigned nInd = 0;
if (strMake.GetLength() == 0) {
bDone = true;
}
while (!bDone) {
if (nInd >= m_nExcMmIsEditListNum) {
bDone = true;
} else {
// Perform exact match, case sensitive
// Check Make field and possibly Model field (if non-empty)
if (_tcscmp(m_sExcMmIsEditList[nInd].strXMake,strMake) != 0) {
// Make did not match
} else {
// Make matched, now check to see if we need
// to compare the Model string
if (_tcslen(m_sExcMmIsEditList[nInd].strXModel) == 0) {
// No need to compare, we're bDone
bFound = true;
bDone = true;
} else {
// Need to check model as well
if (_tcscmp(m_sExcMmIsEditList[nInd].strXModel,strModel) != 0) {
// No match
} else {
// Matched as well, we're bDone
bFound = true;
bDone = true;
}
}
}
nInd++;
}
}
return bFound;
}
// -----------------------------------------------------------------------
// Sample string indicator database
// -----------------------------------------------------------------------
// Sample list of software programs that also use the IJG encoder
LPTSTR CDbSigs::m_sSwIjgList[] = {
_T("GIMP"),
_T("IrfanView"),
_T("idImager"),
_T("FastStone Image Viewer"),
_T("NeatImage"),
_T("Paint.NET"),
_T("Photomatix"),
_T("XnView"),
_T("*"),
};