OLD | NEW |
1 // Copyright 2016 The PDFium Authors. All rights reserved. | 1 // Copyright 2016 The PDFium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef TESTING_LIBFUZZER_XFA_CODEC_FUZZER_H_ | 5 #ifndef TESTING_LIBFUZZER_XFA_CODEC_FUZZER_H_ |
6 #define TESTING_LIBFUZZER_XFA_CODEC_FUZZER_H_ | 6 #define TESTING_LIBFUZZER_XFA_CODEC_FUZZER_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 | 9 |
10 #include "core/fxcodec/codec/ccodec_progressivedecoder.h" | 10 #include "core/fxcodec/codec/ccodec_progressivedecoder.h" |
11 #include "core/fxcodec/fx_codec.h" | 11 #include "core/fxcodec/fx_codec.h" |
12 #include "core/fxcrt/fx_stream.h" | 12 #include "core/fxcrt/fx_stream.h" |
13 | 13 |
14 class XFACodecFuzzer { | 14 class XFACodecFuzzer { |
15 public: | 15 public: |
16 static int Fuzz(const uint8_t* data, size_t size, FXCODEC_IMAGE_TYPE type) { | 16 static int Fuzz(const uint8_t* data, size_t size, FXCODEC_IMAGE_TYPE type) { |
17 std::unique_ptr<CCodec_ModuleMgr> mgr(new CCodec_ModuleMgr()); | 17 std::unique_ptr<CCodec_ModuleMgr> mgr(new CCodec_ModuleMgr()); |
18 std::unique_ptr<CCodec_ProgressiveDecoder> decoder( | 18 std::unique_ptr<CCodec_ProgressiveDecoder> decoder( |
19 mgr->CreateProgressiveDecoder()); | 19 mgr->CreateProgressiveDecoder()); |
20 Reader source(data, size); | 20 CFX_RetainPtr<Reader> source(new Reader(data, size)); |
21 | 21 FXCODEC_STATUS status = decoder->LoadImageInfo(source, type, nullptr, true); |
22 FXCODEC_STATUS status = | |
23 decoder->LoadImageInfo(&source, type, nullptr, true); | |
24 if (status != FXCODEC_STATUS_FRAME_READY) | 22 if (status != FXCODEC_STATUS_FRAME_READY) |
25 return 0; | 23 return 0; |
26 | 24 |
27 std::unique_ptr<CFX_DIBitmap> bitmap(new CFX_DIBitmap); | 25 std::unique_ptr<CFX_DIBitmap> bitmap(new CFX_DIBitmap); |
28 bitmap->Create(decoder->GetWidth(), decoder->GetHeight(), FXDIB_Argb); | 26 bitmap->Create(decoder->GetWidth(), decoder->GetHeight(), FXDIB_Argb); |
29 | 27 |
30 int32_t frames; | 28 int32_t frames; |
31 if (decoder->GetFrames(frames) != FXCODEC_STATUS_DECODE_READY || | 29 if (decoder->GetFrames(frames) != FXCODEC_STATUS_DECODE_READY || |
32 frames == 0) | 30 frames == 0) |
33 return 0; | 31 return 0; |
34 | 32 |
35 status = decoder->StartDecode(bitmap.get(), 0, 0, bitmap->GetWidth(), | 33 status = decoder->StartDecode(bitmap.get(), 0, 0, bitmap->GetWidth(), |
36 bitmap->GetHeight()); | 34 bitmap->GetHeight()); |
37 while (status == FXCODEC_STATUS_DECODE_TOBECONTINUE) | 35 while (status == FXCODEC_STATUS_DECODE_TOBECONTINUE) |
38 status = decoder->ContinueDecode(); | 36 status = decoder->ContinueDecode(); |
39 | 37 |
40 return 0; | 38 return 0; |
41 } | 39 } |
42 | 40 |
43 private: | 41 private: |
44 class Reader : public IFX_SeekableReadStream { | 42 class Reader : public IFX_SeekableReadStream { |
45 public: | 43 public: |
46 Reader(const uint8_t* data, size_t size) : m_data(data), m_size(size) {} | 44 Reader(const uint8_t* data, size_t size) : m_data(data), m_size(size) {} |
47 ~Reader() {} | 45 ~Reader() {} |
48 | 46 |
49 void Release() override {} | |
50 | |
51 bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override { | 47 bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override { |
52 if (offset < 0 || static_cast<size_t>(offset) >= m_size) | 48 if (offset < 0 || static_cast<size_t>(offset) >= m_size) |
53 return false; | 49 return false; |
54 if (offset + size > m_size) | 50 if (offset + size > m_size) |
55 size = m_size - offset; | 51 size = m_size - offset; |
56 if (size == 0) | 52 if (size == 0) |
57 return false; | 53 return false; |
58 | 54 |
59 memcpy(buffer, m_data + offset, size); | 55 memcpy(buffer, m_data + offset, size); |
60 return true; | 56 return true; |
61 } | 57 } |
62 | 58 |
63 FX_FILESIZE GetSize() override { return static_cast<FX_FILESIZE>(m_size); } | 59 FX_FILESIZE GetSize() override { return static_cast<FX_FILESIZE>(m_size); } |
64 | 60 |
65 private: | 61 private: |
66 const uint8_t* const m_data; | 62 const uint8_t* const m_data; |
67 size_t m_size; | 63 size_t m_size; |
68 }; | 64 }; |
69 }; | 65 }; |
70 | 66 |
71 #endif // TESTING_LIBFUZZER_XFA_CODEC_FUZZER_H_ | 67 #endif // TESTING_LIBFUZZER_XFA_CODEC_FUZZER_H_ |
OLD | NEW |