/src/skia/third_party/externals/dng_sdk/source/dng_jpeg_memory_source.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | #include "dng_jpeg_memory_source.h" |
2 | | |
3 | | #if qDNGUseLibJPEG |
4 | | |
5 | | #include "dng_safe_arithmetic.h" |
6 | | |
7 | | namespace { |
8 | | |
9 | 0 | void InitSource(j_decompress_ptr cinfo) { |
10 | | // No initialization necessary. |
11 | 0 | } |
12 | | |
13 | 0 | boolean FillInputBuffer(j_decompress_ptr cinfo) { |
14 | | // We already filled the buffer with all of the data when the source was |
15 | | // initialized, so we can't get any more data. |
16 | 0 | ERREXIT(cinfo, JERR_INPUT_EOF); |
17 | 0 | return FALSE; |
18 | 0 | } |
19 | | |
20 | 0 | void SkipInputData(j_decompress_ptr cinfo, long num_bytes) { |
21 | 0 | if (num_bytes > 0) { |
22 | | // Convert num_bytes to a size_t. |
23 | | // We've established that num_bytes is positive, to it's safe to cast it |
24 | | // to an unsigned long. |
25 | 0 | size_t num_bytes_as_size_t = 0; |
26 | 0 | try { |
27 | 0 | ConvertUnsigned(static_cast<unsigned long>(num_bytes), |
28 | 0 | &num_bytes_as_size_t); |
29 | 0 | } catch (const dng_exception &e) { |
30 | 0 | ERREXIT(cinfo, JERR_INPUT_EOF); |
31 | 0 | return; |
32 | 0 | } |
33 | | |
34 | 0 | jpeg_source_mgr *source_manager = |
35 | 0 | reinterpret_cast<jpeg_source_mgr *>(cinfo->src); |
36 | | |
37 | | // Advance the current position by the given number of bytes. |
38 | 0 | if (num_bytes_as_size_t <= source_manager->bytes_in_buffer) { |
39 | 0 | source_manager->bytes_in_buffer -= num_bytes_as_size_t; |
40 | 0 | source_manager->next_input_byte += num_bytes_as_size_t; |
41 | 0 | } else { |
42 | | // Tried to read beyond the end of the buffer. |
43 | 0 | ERREXIT(cinfo, JERR_INPUT_EOF); |
44 | 0 | return; |
45 | 0 | } |
46 | 0 | } |
47 | 0 | } |
48 | | |
49 | 0 | boolean ResyncToRestart(j_decompress_ptr cinfo, int desired) { |
50 | | // Can't resync. |
51 | 0 | return FALSE; |
52 | 0 | } |
53 | | |
54 | 0 | void TermSource(j_decompress_ptr cinfo) { |
55 | | // No termination necessary. |
56 | 0 | } |
57 | | |
58 | | } // namespace |
59 | | |
60 | 0 | jpeg_source_mgr CreateJpegMemorySource(const uint8 *buffer, size_t size) { |
61 | 0 | jpeg_source_mgr source; |
62 | |
|
63 | 0 | source.next_input_byte = reinterpret_cast<const JOCTET *>(buffer); |
64 | 0 | source.bytes_in_buffer = size; |
65 | | |
66 | | // Initialize function pointers. |
67 | 0 | source.init_source = InitSource; |
68 | 0 | source.fill_input_buffer = FillInputBuffer; |
69 | 0 | source.skip_input_data = SkipInputData; |
70 | 0 | source.resync_to_restart = ResyncToRestart; |
71 | 0 | source.term_source = TermSource; |
72 | |
|
73 | 0 | return source; |
74 | 0 | } |
75 | | |
76 | | #endif // qDNGUseLibJPEG |