Coverage Report

Created: 2025-11-11 06:48

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/dng_validate_fuzzer.cpp
Line
Count
Source
1
/*****************************************************************************/
2
// Copyright 2006-2012 Adobe Systems Incorporated
3
// All Rights Reserved.
4
//
5
// NOTICE:  Adobe permits you to use, modify, and distribute this file in
6
// accordance with the terms of the Adobe license agreement accompanying it.
7
/*****************************************************************************/
8
9
/* $Id: //mondo/dng_sdk_1_4/dng_sdk/source/dng_validate.cpp#2 $ */ 
10
/* $DateTime: 2012/06/14 20:24:41 $ */
11
/* $Change: 835078 $ */
12
/* $Author: tknoll $ */
13
14
// Process exit codes
15
// ------------------
16
//
17
// As usual, 0 indicates success.
18
//
19
// If an exception occurs, the exit code will be equal to:
20
//
21
//    DNG SDK error code - 100000 + 100
22
//
23
// For example, the error dng_error_memory, which has a DNG SDK error code of
24
// 100005, is returned as an exit code of 105.
25
//
26
// This convention accounts for the fact that the shell truncates process exit
27
// codes to 8 bits and that the exit code 1 is used by ASAN to signal that a
28
// memory error occurred (so mapping the first DNG SDK error code to an exit
29
// code of 1 would not be a good idea).
30
31
/*****************************************************************************/
32
33
#include "dng_color_space.h"
34
#include "dng_date_time.h"
35
#include "dng_exceptions.h"
36
#include "dng_file_stream.h"
37
#include "dng_globals.h"
38
#include "dng_host.h"
39
#include "dng_ifd.h"
40
#include "dng_image_writer.h"
41
#include "dng_info.h"
42
#include "dng_linearization_info.h"
43
#include "dng_mosaic_info.h"
44
#include "dng_negative.h"
45
#include "dng_preview.h"
46
#include "dng_render.h"
47
#include "dng_simple_image.h"
48
#include "dng_tag_codes.h"
49
#include "dng_tag_types.h"
50
#include "dng_tag_values.h"
51
52
#if qDNGUseXMP
53
#include "dng_xmp.h"
54
#include "dng_xmp_sdk.h"
55
#endif
56
57
/*****************************************************************************/
58
59
#if qDNGValidateTarget
60
    
61
/*****************************************************************************/
62
63
7.11k
#define kDNGValidateVersion "1.4"
64
    
65
/*****************************************************************************/
66
67
 bool gFourColorBayer = false;
68
    
69
 int32 gMosaicPlane = -1;
70
71
 uint32 gPreferredSize = 0;
72
 uint32 gMinimumSize   = 0;
73
 uint32 gMaximumSize   = 0;
74
75
 uint32 gProxyDNGSize = 0;
76
77
 const dng_color_space *gFinalSpace = &dng_space_sRGB::Get ();
78
79
 uint32 gFinalPixelType = ttByte;
80
81
 dng_string gDumpStage1;
82
 dng_string gDumpStage2;
83
 dng_string gDumpStage3;
84
 dng_string gDumpTIF;
85
 dng_string gDumpDNG;
86
87
/*****************************************************************************/
88
89
 dng_error_code dng_validate (const char *filename)
90
30.5k
  {
91
  
92
  //idating \"%s\"...\n", filename);
93
  
94
30.5k
  try
95
30.5k
    {
96
  
97
30.5k
    dng_file_stream stream (filename);
98
    
99
30.5k
    dng_host host;
100
    
101
30.5k
    host.SetPreferredSize (gPreferredSize);
102
30.5k
    host.SetMinimumSize   (gMinimumSize  );
103
30.5k
    host.SetMaximumSize   (gMaximumSize  );
104
    
105
30.5k
    host.ValidateSizes ();
106
    
107
30.5k
    if (host.MinimumSize ())
108
10.0k
      {
109
      
110
10.0k
      host.SetForPreview (true);
111
      
112
10.0k
      gDumpDNG.Clear ();
113
      
114
10.0k
      }
115
      
116
30.5k
    if (gDumpDNG.NotEmpty ())
117
20.3k
      {
118
      
119
20.3k
      host.SetSaveDNGVersion (dngVersion_SaveDefault);
120
      
121
20.3k
      host.SetSaveLinearDNG (false);
122
      
123
20.3k
      host.SetKeepOriginalFile (false);
124
      
125
20.3k
      }
126
      
127
    // Read into the negative.
128
    
129
30.5k
    AutoPtr<dng_negative> negative;
130
    
131
30.5k
      {
132
      
133
30.5k
      dng_info info;
134
      
135
30.5k
      info.Parse (host, stream);
136
      
137
30.5k
      info.PostParse (host);
138
      
139
30.5k
      if (!info.IsValidDNG ())
140
5.85k
        {
141
5.85k
        return dng_error_bad_format;
142
5.85k
        }
143
        
144
24.7k
      negative.Reset (host.Make_dng_negative ());
145
      
146
24.7k
      negative->Parse (host, stream, info);
147
      
148
24.7k
      negative->PostParse (host, stream, info);
149
      
150
24.7k
        {
151
        
152
24.7k
        dng_timer timer ("Raw image read time");
153
154
24.7k
        negative->ReadStage1Image (host, stream, info);
155
        
156
24.7k
        }
157
        
158
24.7k
      if (info.fMaskIndex != -1)
159
0
        {
160
        
161
0
        dng_timer timer ("Transparency mask read time");
162
163
0
        negative->ReadTransparencyMask (host, stream, info);
164
        
165
0
        }
166
        
167
24.7k
      negative->ValidateRawImageDigest (host);
168
        
169
24.7k
      }
170
           
171
    // Option to write stage 1 image.
172
      
173
24.7k
    if (gDumpStage1.NotEmpty ())
174
5.46k
      {
175
      
176
5.46k
      dng_file_stream stream2 (gDumpStage1.Get (), true);
177
      
178
5.46k
      const dng_image &stage1 = *negative->Stage1Image ();
179
      
180
5.46k
      dng_image_writer writer;
181
      
182
5.46k
      writer.WriteTIFF (host,
183
5.46k
                stream2,
184
5.46k
                stage1,
185
5.46k
                stage1.Planes () >= 3 ? piRGB 
186
5.46k
                            : piBlackIsZero);
187
188
5.46k
      gDumpStage1.Clear ();
189
      
190
5.46k
      }
191
      
192
    // Metadata.
193
      
194
24.7k
    negative->SynchronizeMetadata ();
195
    
196
    // Four color Bayer option.
197
    
198
24.7k
    if (gFourColorBayer)
199
5.27k
      {
200
5.27k
      negative->SetFourColorBayer ();
201
5.27k
      }
202
      
203
    // Build stage 2 image.
204
    
205
24.7k
      {
206
      
207
24.7k
      dng_timer timer ("Linearization time");
208
      
209
24.7k
      negative->BuildStage2Image (host);
210
                     
211
24.7k
      }
212
           
213
24.7k
    if (gDumpStage2.NotEmpty ())
214
4.92k
      {
215
      
216
4.92k
      dng_file_stream stream2 (gDumpStage2.Get (), true);
217
      
218
4.92k
      const dng_image &stage2 = *negative->Stage2Image ();
219
            
220
4.92k
      dng_image_writer writer;
221
      
222
4.92k
      writer.WriteTIFF (host,
223
4.92k
                stream2,
224
4.92k
                stage2,
225
4.92k
                stage2.Planes () >= 3 ? piRGB 
226
4.92k
                            : piBlackIsZero);
227
      
228
4.92k
      gDumpStage2.Clear ();
229
      
230
4.92k
      }
231
      
232
    // Build stage 3 image.
233
      
234
24.7k
      {
235
      
236
24.7k
      dng_timer timer ("Interpolate time");
237
    
238
24.7k
      negative->BuildStage3Image (host,
239
24.7k
                      gMosaicPlane);
240
              
241
24.7k
      }
242
      
243
    // Convert to proxy, if requested.
244
    
245
24.7k
    if (gProxyDNGSize)
246
6.83k
      {
247
      
248
6.83k
      dng_timer timer ("ConvertToProxy time");
249
      
250
6.83k
      dng_image_writer writer;
251
      
252
6.83k
      negative->ConvertToProxy (host,
253
6.83k
                    writer,
254
6.83k
                    gProxyDNGSize);
255
    
256
6.83k
      }
257
      
258
    // Flatten transparency, if required.
259
    
260
24.7k
    if (negative->NeedFlattenTransparency (host))
261
0
      {
262
      
263
0
      dng_timer timer ("FlattenTransparency time");
264
    
265
0
      negative->FlattenTransparency (host);
266
      
267
0
      }
268
      
269
24.7k
    if (gDumpStage3.NotEmpty ())
270
5.13k
      {
271
      
272
5.13k
      dng_file_stream stream2 (gDumpStage3.Get (), true);
273
      
274
5.13k
      const dng_image &stage3 = *negative->Stage3Image ();
275
      
276
5.13k
      dng_image_writer writer;
277
      
278
5.13k
      writer.WriteTIFF (host,
279
5.13k
                stream2,
280
5.13k
                stage3,
281
5.13k
                stage3.Planes () >= 3 ? piRGB 
282
5.13k
                            : piBlackIsZero);
283
      
284
5.13k
      gDumpStage3.Clear ();
285
      
286
5.13k
      }
287
      
288
    // Output DNG file if requested.
289
      
290
24.7k
    if (gDumpDNG.NotEmpty ())
291
5.60k
      {
292
      
293
      // Build the preview list.
294
      
295
5.60k
      dng_preview_list previewList;
296
      
297
5.60k
      dng_date_time_info dateTimeInfo;
298
      
299
5.60k
      CurrentDateTimeAndZone (dateTimeInfo);
300
                        
301
12.7k
      for (uint32 previewIndex = 0; previewIndex < 2; previewIndex++)
302
10.9k
        {
303
        
304
        // Skip preview if writing a compresssed main2 image to save space
305
        // in this example code.
306
        
307
10.9k
        if (negative->RawJPEGImage () != NULL && previewIndex > 0)
308
2.67k
          {
309
2.67k
          break;
310
2.67k
          }
311
          
312
        // Report timing.
313
      
314
8.30k
        dng_timer timer (previewIndex == 0 ? "Build thumbnail time"
315
8.30k
                           : "Build preview time");
316
        
317
        // Render a preview sized image.
318
        
319
8.30k
        AutoPtr<dng_image> previewImage;
320
        
321
8.30k
          {
322
          
323
8.30k
          dng_render render (host, *negative);
324
          
325
8.30k
          render.SetFinalSpace (negative->IsMonochrome () ? dng_space_GrayGamma22::Get ()
326
8.30k
                                  : dng_space_sRGB       ::Get ());
327
          
328
8.30k
          render.SetFinalPixelType (ttByte);
329
          
330
8.30k
          render.SetMaximumSize (previewIndex == 0 ? 256 : 1024);
331
        
332
8.30k
          previewImage.Reset (render.Render ());
333
        
334
8.30k
          }
335
          
336
        // Don't write the preview if it is same size as thumbnail.
337
        
338
8.30k
        if (previewIndex > 0 &&
339
2.67k
          Max_uint32 (previewImage->Bounds ().W (),
340
2.67k
                previewImage->Bounds ().H ()) <= 256)
341
1.18k
          {
342
1.18k
          break;
343
1.18k
          }
344
        
345
        // If we have compressed JPEG data, create a compressed thumbnail.  Otherwise
346
        // save a uncompressed thumbnail.
347
        
348
7.11k
        bool useCompressedPreview = (negative->RawJPEGImage () != NULL) ||
349
4.19k
                      (previewIndex > 0);
350
        
351
7.11k
        AutoPtr<dng_preview> preview (useCompressedPreview ?
352
4.16k
                        (dng_preview *) new dng_jpeg_preview :
353
7.11k
                        (dng_preview *) new dng_image_preview);
354
                        
355
        // Setup up preview info.
356
                  
357
7.11k
        preview->fInfo.fApplicationName   .Set ("dng_validate");
358
7.11k
        preview->fInfo.fApplicationVersion.Set (kDNGValidateVersion);
359
        
360
7.11k
        preview->fInfo.fSettingsName.Set ("Default");
361
362
7.11k
        preview->fInfo.fColorSpace = previewImage->Planes () == 1 ?
363
5.37k
                       previewColorSpace_GrayGamma22 :
364
7.11k
                       previewColorSpace_sRGB;
365
                      
366
7.11k
        preview->fInfo.fDateTime = dateTimeInfo.Encode_ISO_8601 ();
367
        
368
7.11k
        if (!useCompressedPreview)
369
2.70k
          {
370
          
371
2.70k
          dng_image_preview *imagePreview = dynamic_cast<dng_image_preview *> (preview.Get ());
372
        
373
2.70k
          imagePreview->fImage.Reset (previewImage.Release ());
374
          
375
2.70k
          }
376
          
377
4.41k
        else
378
4.41k
          {
379
380
4.41k
          dng_jpeg_preview *jpegPreview = dynamic_cast<dng_jpeg_preview *> (preview.Get ());
381
          
382
4.41k
          int32 quality = (previewIndex == 0 ? 8 : 5);
383
384
4.41k
          dng_image_writer writer;
385
          
386
4.41k
          writer.EncodeJPEGPreview (host,
387
4.41k
                          *previewImage,
388
4.41k
                          *jpegPreview,
389
4.41k
                        quality);
390
                      
391
4.41k
          }
392
    
393
7.11k
        previewList.Append (preview);
394
        
395
7.11k
        }
396
        
397
      // Write DNG file.
398
      
399
5.60k
      dng_file_stream stream2 (gDumpDNG.Get (), true);
400
      
401
5.60k
        {
402
        
403
5.60k
        dng_timer timer ("Write DNG time");
404
      
405
5.60k
        dng_image_writer writer;
406
      
407
5.60k
        writer.WriteDNG (host,
408
5.60k
                 stream2,
409
5.60k
                 *negative.Get (),
410
5.60k
                 &previewList,
411
5.60k
                 dngVersion_Current,
412
5.60k
                 false);
413
414
5.60k
        }
415
        
416
5.60k
      gDumpDNG.Clear ();
417
      
418
5.60k
      }
419
          
420
    // Output TIF file if requested.
421
      
422
24.7k
    if (gDumpTIF.NotEmpty ())
423
5.51k
      {
424
      
425
      // Render final image.
426
        
427
5.51k
      dng_render render (host, *negative);
428
      
429
5.51k
      render.SetFinalSpace     (*gFinalSpace   );
430
5.51k
      render.SetFinalPixelType (gFinalPixelType);
431
      
432
5.51k
      if (host.MinimumSize ())
433
1.36k
        {
434
        
435
1.36k
        dng_point stage3Size = negative->Stage3Image ()->Size ();
436
        
437
1.36k
        render.SetMaximumSize (Max_uint32 (stage3Size.v,
438
1.36k
                           stage3Size.h));
439
440
1.36k
        }
441
      
442
5.51k
      AutoPtr<dng_image> finalImage;
443
      
444
5.51k
        {
445
        
446
5.51k
        dng_timer timer ("Render time");
447
      
448
5.51k
        finalImage.Reset (render.Render ());
449
        
450
5.51k
        }
451
        
452
5.51k
      finalImage->Rotate (negative->Orientation ());
453
      
454
      // Now that Camera Raw supports non-raw formats, we should
455
      // not keep any Camera Raw settings in the XMP around when
456
      // writing rendered files.
457
      
458
      #if qDNGUseXMP
459
      
460
      if (negative->GetXMP ())
461
        {
462
463
        negative->GetXMP ()->RemoveProperties (XMP_NS_CRS);
464
        negative->GetXMP ()->RemoveProperties (XMP_NS_CRSS);
465
        
466
        }
467
      
468
      #endif
469
      
470
      // Write TIF file.
471
      
472
5.51k
      dng_file_stream stream2 (gDumpTIF.Get (), true);
473
      
474
5.51k
        {
475
        
476
5.51k
        dng_timer timer ("Write TIFF time");
477
      
478
5.51k
        dng_image_writer writer;
479
      
480
5.51k
        writer.WriteTIFF (host,
481
5.51k
                  stream2,
482
5.51k
                  *finalImage.Get (),
483
5.51k
                  finalImage->Planes () >= 3 ? piRGB 
484
5.51k
                               : piBlackIsZero,
485
5.51k
                  ccUncompressed,
486
5.51k
                  negative.Get (),
487
5.51k
                  &render.FinalSpace ());
488
                  
489
5.51k
        }
490
        
491
5.51k
      gDumpTIF.Clear ();
492
      
493
5.51k
      }
494
          
495
24.7k
    }
496
  
497
30.5k
  catch (const dng_exception &except)
498
30.5k
    {
499
    
500
17.9k
    return except.ErrorCode ();
501
    
502
17.9k
    }
503
    
504
30.5k
  catch (...)
505
30.5k
    {
506
    
507
0
    return dng_error_unknown;
508
    
509
0
    }
510
    
511
  //idation complete\n");
512
  
513
6.79k
  return dng_error_none;
514
515
30.5k
  }
516
517
/*****************************************************************************/
518
519
int main2 (int argc, char *argv [])
520
0
  {
521
  
522
0
  try
523
0
    {
524
525
0
    if (argc == 1)
526
0
      {
527
528
0
      fprintf (stderr,
529
0
           "\n"
530
0
           "dng_validate, version " kDNGValidateVersion " "
531
0
           #if qDNG64Bit
532
0
           "(64-bit)"
533
           #else
534
           "(32-bit)"
535
           #endif
536
0
           "\n"
537
0
           "Copyright 2005-2012 Adobe Systems, Inc.\n"
538
0
           "\n"
539
0
           "Usage:  %s [options] file1 file2 ...\n"
540
0
           "\n"
541
0
           "Valid options:\n"
542
0
           "-v            Verbose mode\n"
543
0
           "-d <num>      Dump line limit (implies -v)\n"
544
0
           "-b4           Use four-color Bayer interpolation\n"
545
0
           "-s <num>      Use this sample of multi-sample CFAs\n"
546
0
           "-size <num>   Preferred preview image size\n"
547
0
           "-min <num>    Minimum preview image size\n"
548
0
           "-max <num>    Maximum preview image size\n" 
549
0
           "-proxy <num>  Target size for proxy DNG\n"
550
0
           "-cs1          Color space: \"sRGB\" (default)\n"
551
0
           "-cs2          Color space: \"Adobe RGB\"\n"
552
0
           "-cs3          Color space: \"ProPhoto RGB\"\n"
553
0
           "-cs4          Color space: \"ColorMatch RGB\"\n"
554
0
           "-cs5          Color space: \"Gray Gamma 1.8\"\n"
555
0
           "-cs6          Color space: \"Gray Gamma 2.2\"\n"
556
0
           "-16           16-bits/channel output\n"
557
0
           "-1 <file>     Write stage 1 image to \"<file>.tif\"\n"
558
0
           "-2 <file>     Write stage 2 image to \"<file>.tif\"\n"
559
0
           "-3 <file>     Write stage 3 image to \"<file>.tif\"\n"
560
0
           "-tif <file>   Write TIF image to \"<file>.tif\"\n"
561
0
           "-dng <file>   Write DNG image to \"<file>.dng\"\n"
562
0
           "\n",
563
0
           argv [0]);
564
           
565
0
      return 1;
566
      
567
0
      }
568
      
569
0
    int index;
570
    
571
0
    for (index = 1; index < argc && argv [index] [0] == '-'; index++)
572
0
      {
573
      
574
0
      dng_string option;
575
      
576
0
      option.Set (&argv [index] [1]);
577
      
578
0
      if (option.Matches ("v", true))
579
0
        {
580
0
        gVerbose = true;
581
0
        }
582
        
583
0
      else if (option.Matches ("d", true))
584
0
        {
585
          
586
0
        gVerbose = true;
587
        
588
0
        gDumpLineLimit = 0;
589
        
590
0
        if (index + 1 < argc)
591
0
          {
592
0
          gDumpLineLimit = atoi (argv [++index]);
593
0
          }
594
          
595
0
        if (!gDumpLineLimit)
596
0
          {
597
0
          fprintf (stderr, "*** Invalid number after -d\n");
598
0
          return 1;
599
0
          }
600
          
601
0
        }
602
        
603
0
      else if (option.Matches ("s", true))
604
0
        {
605
        
606
0
        if (index + 1 < argc)
607
0
          {
608
0
          gMosaicPlane = atoi (argv [++index]);
609
0
          }
610
          
611
0
        else
612
0
          {
613
0
          fprintf (stderr, "*** Missing number after -s\n");
614
0
          return 1;
615
0
          }
616
          
617
0
        }
618
          
619
0
      else if (option.Matches ("b4", true))
620
0
        {
621
0
        gFourColorBayer = true;
622
0
        }
623
          
624
0
      else if (option.Matches ("size", true))
625
0
        {
626
        
627
0
        if (index + 1 < argc)
628
0
          {
629
0
          gPreferredSize = (uint32) atoi (argv [++index]);
630
0
          }
631
          
632
0
        else
633
0
          {
634
0
          fprintf (stderr, "*** Missing number after -size\n");
635
0
          return 1;
636
0
          }
637
          
638
0
        }
639
          
640
0
      else if (option.Matches ("min", true))
641
0
        {
642
        
643
0
        if (index + 1 < argc)
644
0
          {
645
0
          gMinimumSize = (uint32) atoi (argv [++index]);
646
0
          }
647
          
648
0
        else
649
0
          {
650
0
          fprintf (stderr, "*** Missing number after -min\n");
651
0
          return 1;
652
0
          }
653
          
654
0
        }
655
          
656
0
      else if (option.Matches ("max", true))
657
0
        {
658
        
659
0
        if (index + 1 < argc)
660
0
          {
661
0
          gMaximumSize = (uint32) atoi (argv [++index]);
662
0
          }
663
          
664
0
        else
665
0
          {
666
0
          fprintf (stderr, "*** Missing number after -max\n");
667
0
          return 1;
668
0
          }
669
          
670
0
        }
671
          
672
0
      else if (option.Matches ("proxy", true))
673
0
        {
674
        
675
0
        if (index + 1 < argc)
676
0
          {
677
0
          gProxyDNGSize = (uint32) atoi (argv [++index]);
678
0
          }
679
          
680
0
        else
681
0
          {
682
0
          fprintf (stderr, "*** Missing number after -proxy\n");
683
0
          return 1;
684
0
          }
685
686
0
        }
687
          
688
0
      else if (option.Matches ("cs1", true))
689
0
        {
690
        
691
0
        gFinalSpace = &dng_space_sRGB::Get ();
692
        
693
0
        }
694
          
695
0
      else if (option.Matches ("cs2", true))
696
0
        {
697
        
698
0
        gFinalSpace = &dng_space_AdobeRGB::Get ();
699
        
700
0
        }
701
          
702
0
      else if (option.Matches ("cs3", true))
703
0
        {
704
        
705
0
        gFinalSpace = &dng_space_ProPhoto::Get ();
706
        
707
0
        }
708
          
709
0
      else if (option.Matches ("cs4", true))
710
0
        {
711
        
712
0
        gFinalSpace = &dng_space_ColorMatch::Get ();
713
        
714
0
        }
715
          
716
0
      else if (option.Matches ("cs5", true))
717
0
        {
718
        
719
0
        gFinalSpace = &dng_space_GrayGamma18::Get ();
720
        
721
0
        }
722
          
723
0
      else if (option.Matches ("cs6", true))
724
0
        {
725
        
726
0
        gFinalSpace = &dng_space_GrayGamma22::Get ();
727
        
728
0
        }
729
          
730
0
      else if (option.Matches ("16"))
731
0
        {
732
        
733
0
        gFinalPixelType = ttShort;
734
        
735
0
        }
736
          
737
0
      else if (option.Matches ("1"))
738
0
        {
739
        
740
0
        gDumpStage1.Clear ();
741
        
742
0
        if (index + 1 < argc)
743
0
          {
744
0
          gDumpStage1.Set (argv [++index]);
745
0
          }
746
          
747
0
        if (gDumpStage1.IsEmpty () || gDumpStage1.StartsWith ("-"))
748
0
          {
749
0
          fprintf (stderr, "*** Missing file name after -1\n");
750
0
          return 1;
751
0
          }
752
        
753
0
        if (!gDumpStage1.EndsWith (".tif"))
754
0
          {
755
0
          gDumpStage1.Append (".tif");
756
0
          }
757
        
758
0
        }
759
          
760
0
      else if (option.Matches ("2"))
761
0
        {
762
        
763
0
        gDumpStage2.Clear ();
764
        
765
0
        if (index + 1 < argc)
766
0
          {
767
0
          gDumpStage2.Set (argv [++index]);
768
0
          }
769
          
770
0
        if (gDumpStage2.IsEmpty () || gDumpStage2.StartsWith ("-"))
771
0
          {
772
0
          fprintf (stderr, "*** Missing file name after -2\n");
773
0
          return 1;
774
0
          }
775
        
776
0
        if (!gDumpStage2.EndsWith (".tif"))
777
0
          {
778
0
          gDumpStage2.Append (".tif");
779
0
          }
780
        
781
0
        }
782
          
783
0
      else if (option.Matches ("3"))
784
0
        {
785
        
786
0
        gDumpStage3.Clear ();
787
        
788
0
        if (index + 1 < argc)
789
0
          {
790
0
          gDumpStage3.Set (argv [++index]);
791
0
          }
792
          
793
0
        if (gDumpStage3.IsEmpty () || gDumpStage3.StartsWith ("-"))
794
0
          {
795
0
          fprintf (stderr, "*** Missing file name after -3\n");
796
0
          return 1;
797
0
          }
798
        
799
0
        if (!gDumpStage3.EndsWith (".tif"))
800
0
          {
801
0
          gDumpStage3.Append (".tif");
802
0
          }
803
        
804
0
        }
805
        
806
0
      else if (option.Matches ("tif", true))
807
0
        {
808
        
809
0
        gDumpTIF.Clear ();
810
        
811
0
        if (index + 1 < argc)
812
0
          {
813
0
          gDumpTIF.Set (argv [++index]);
814
0
          }
815
          
816
0
        if (gDumpTIF.IsEmpty () || gDumpTIF.StartsWith ("-"))
817
0
          {
818
0
          fprintf (stderr, "*** Missing file name after -tif\n");
819
0
          return 1;
820
0
          }
821
        
822
0
        if (!gDumpTIF.EndsWith (".tif"))
823
0
          {
824
0
          gDumpTIF.Append (".tif");
825
0
          }
826
        
827
0
        }
828
        
829
0
      else if (option.Matches ("dng", true))
830
0
        {
831
        
832
0
        gDumpDNG.Clear ();
833
        
834
0
        if (index + 1 < argc)
835
0
          {
836
0
          gDumpDNG.Set (argv [++index]);
837
0
          }
838
          
839
0
        if (gDumpDNG.IsEmpty () || gDumpDNG.StartsWith ("-"))
840
0
          {
841
0
          fprintf (stderr, "*** Missing file name after -dng\n");
842
0
          return 1;
843
0
          }
844
        
845
0
        if (!gDumpDNG.EndsWith (".dng"))
846
0
          {
847
0
          gDumpDNG.Append (".dng");
848
0
          }
849
        
850
0
        }
851
        
852
0
      else
853
0
        {
854
0
        fprintf (stderr, "*** Unknown option \"-%s\"\n", option.Get ());
855
0
        return 1;
856
0
        }
857
        
858
0
      }
859
          
860
0
    if (index == argc)
861
0
      {
862
0
      fprintf (stderr, "*** No file specified\n");
863
0
      return 1;
864
0
      }
865
    
866
    #if qDNGUseXMP
867
      
868
    dng_xmp_sdk::InitializeSDK ();
869
    
870
    #endif
871
      
872
0
    int result = 0;
873
    
874
0
    while (index < argc)
875
0
      {
876
      
877
0
      dng_error_code error_code = dng_validate (argv [index++]);
878
0
      if (error_code != dng_error_none)
879
0
        {
880
        
881
0
        result = error_code - dng_error_unknown + 100;
882
        
883
0
        }
884
      
885
0
      }
886
    
887
    #if qDNGUseXMP
888
    
889
    dng_xmp_sdk::TerminateSDK ();
890
    
891
    #endif
892
      
893
0
    return result;
894
    
895
0
    }
896
    
897
0
  catch (...)
898
0
    {
899
    
900
0
    }
901
    
902
0
  fprintf (stderr, "*** Exception thrown in main2 routine\n");
903
  
904
0
  return 1;
905
  
906
0
  }
907
  
908
/*****************************************************************************/
909
910
#endif
911
  
912
/*****************************************************************************/
913
/* Copyright 2021 Google LLC
914
Licensed under the Apache License, Version 2.0 (the "License");
915
you may not use this file except in compliance with the License.
916
You may obtain a copy of the License at
917
      http://www.apache.org/licenses/LICENSE-2.0
918
Unless required by applicable law or agreed to in writing, software
919
distributed under the License is distributed on an "AS IS" BASIS,
920
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
921
See the License for the specific language governing permissions and
922
limitations under the License.
923
*/
924
#include <fuzzer/FuzzedDataProvider.h>
925
#include <stdio.h>
926
#include <stdlib.h>
927
#include <unistd.h>
928
929
12.5k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
930
12.5k
  FuzzedDataProvider provider(data, size);
931
932
  // Set the various sizes
933
12.5k
  gPreferredSize = provider.ConsumeIntegral<uint32_t>();
934
12.5k
  gMinimumSize = provider.ConsumeIntegral<uint32_t>();
935
12.5k
  gMaximumSize = provider.ConsumeIntegral<uint32_t>();
936
937
12.5k
  gDumpDNG.Clear();
938
12.5k
  char dumpDNGFilename[256];
939
12.5k
  if (provider.ConsumeBool()) {
940
6.29k
    sprintf(dumpDNGFilename, "/tmp/libfuzzer-dng.%d.dng", getpid());
941
6.29k
    gDumpDNG.Set(dumpDNGFilename); 
942
6.29k
  }
943
944
12.5k
  gDumpStage1.Clear();
945
12.5k
  char dumpStage1Filename[256];
946
12.5k
  if (provider.ConsumeBool()) {
947
5.09k
    sprintf(dumpStage1Filename, "/tmp/libfuzzer-stage1.%d.dng", getpid());
948
5.09k
    gDumpStage1.Set(dumpStage1Filename); 
949
5.09k
  }
950
  
951
952
12.5k
  gDumpStage2.Clear();
953
12.5k
  char dumpStage2Filename[256];
954
12.5k
  if (provider.ConsumeBool()) {
955
4.52k
    sprintf(dumpStage2Filename, "/tmp/libfuzzer-stage2.%d.dng", getpid());
956
4.52k
    gDumpStage2.Set(dumpStage2Filename); 
957
4.52k
  }
958
959
12.5k
  gDumpStage3.Clear();
960
12.5k
  char dumpStage3Filename[256];
961
12.5k
  if (provider.ConsumeBool()) {
962
4.86k
    sprintf(dumpStage3Filename, "/tmp/libfuzzer-stage3.%d.dng", getpid());
963
4.86k
    gDumpStage3.Set(dumpStage3Filename); 
964
4.86k
  }
965
966
12.5k
  gDumpTIF.Clear();
967
12.5k
  char dumpTifFilename[256];
968
12.5k
  if (provider.ConsumeBool()) {
969
5.91k
    sprintf(dumpTifFilename, "/tmp/libfuzzer-tif.%d.tif", getpid());
970
5.91k
    gDumpTIF.Set(dumpTifFilename); 
971
5.91k
  }
972
973
12.5k
  gProxyDNGSize = provider.ConsumeIntegral<uint32_t>();
974
12.5k
  gMosaicPlane = provider.ConsumeIntegral<int32_t>();
975
976
977
12.5k
  gFourColorBayer = provider.ConsumeBool();
978
979
12.5k
  switch (provider.ConsumeIntegralInRange(0, 7)) {
980
5.40k
    case 0:
981
5.40k
      gFinalSpace = &dng_space_sRGB::Get ();
982
5.40k
      break;
983
1.58k
    case 1:
984
1.58k
      gFinalSpace = &dng_space_AdobeRGB::Get ();
985
1.58k
      break;
986
771
    case 2:
987
771
      gFinalSpace = &dng_space_ProPhoto::Get ();
988
771
      break;
989
700
    case 3:
990
700
      gFinalSpace = &dng_space_ColorMatch::Get ();
991
700
      break;
992
589
    case 4:
993
589
      gFinalSpace = &dng_space_GrayGamma18::Get ();
994
589
      break;
995
878
    case 5:
996
878
      gFinalSpace = &dng_space_GrayGamma22::Get ();
997
878
      break;
998
2.63k
    default:
999
2.63k
      gFinalSpace = &dng_space_sRGB::Get ();
1000
2.63k
      break;
1001
12.5k
  }
1002
1003
12.5k
  char filename[256];
1004
12.5k
  sprintf(filename, "/tmp/libfuzzer.%d", getpid());
1005
12.5k
  std::string restData = provider.ConsumeRemainingBytesAsString();
1006
12.5k
  if (restData.size() > 0) {
1007
12.5k
    FILE *fp = fopen(filename, "wb");
1008
12.5k
    if (!fp) {
1009
0
        return 0;
1010
0
    }
1011
12.5k
    fwrite(restData.c_str(), restData.size(), 1, fp);
1012
12.5k
    fclose(fp);
1013
1014
    // Target
1015
12.5k
    dng_validate(filename);
1016
1017
    // cleanup file
1018
12.5k
    unlink(filename);
1019
12.5k
  }
1020
12.5k
  return 0;
1021
12.5k
}