/src/libjpeg-turbo.3.0.x/fuzz/compress12_lossless.cc
Line | Count | Source |
1 | | /* |
2 | | * Copyright (C)2021-2026 D. R. Commander. All Rights Reserved. |
3 | | * |
4 | | * Redistribution and use in source and binary forms, with or without |
5 | | * modification, are permitted provided that the following conditions are met: |
6 | | * |
7 | | * - Redistributions of source code must retain the above copyright notice, |
8 | | * this list of conditions and the following disclaimer. |
9 | | * - Redistributions in binary form must reproduce the above copyright notice, |
10 | | * this list of conditions and the following disclaimer in the documentation |
11 | | * and/or other materials provided with the distribution. |
12 | | * - Neither the name of the libjpeg-turbo Project nor the names of its |
13 | | * contributors may be used to endorse or promote products derived from this |
14 | | * software without specific prior written permission. |
15 | | * |
16 | | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS", |
17 | | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
18 | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
19 | | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE |
20 | | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
21 | | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
22 | | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
23 | | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
24 | | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
25 | | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
26 | | * POSSIBILITY OF SUCH DAMAGE. |
27 | | */ |
28 | | |
29 | | #include "../turbojpeg.h" |
30 | | #include <stdio.h> |
31 | | #include <stdlib.h> |
32 | | #include <stdint.h> |
33 | | #include <string.h> |
34 | | #include <unistd.h> |
35 | | |
36 | | extern "C" short * |
37 | | _tj3LoadImageFromFileHandle12(tjhandle handle, FILE *file, int *width, |
38 | | int align, int *height, int *pixelFormat); |
39 | | |
40 | | |
41 | 44.5k | #define NUMTESTS 7 |
42 | | |
43 | | |
44 | | struct test { |
45 | | enum TJPF pf; |
46 | | int psv, pt; |
47 | | }; |
48 | | |
49 | | |
50 | | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) |
51 | 5.56k | { |
52 | 5.56k | tjhandle handle = NULL; |
53 | 5.56k | short *srcBuf = NULL; |
54 | 5.56k | unsigned char *dstBuf = NULL; |
55 | 5.56k | int width = 0, height = 0, ti; |
56 | 5.56k | FILE *file = NULL; |
57 | 5.56k | struct test tests[NUMTESTS] = { |
58 | 5.56k | { TJPF_RGB, 1, 0 }, |
59 | 5.56k | { TJPF_BGR, 2, 2 }, |
60 | 5.56k | { TJPF_RGBX, 3, 4 }, |
61 | 5.56k | { TJPF_BGRA, 4, 7 }, |
62 | 5.56k | { TJPF_XRGB, 5, 5 }, |
63 | 5.56k | { TJPF_GRAY, 6, 3 }, |
64 | 5.56k | { TJPF_CMYK, 7, 0 } |
65 | 5.56k | }; |
66 | | |
67 | 5.56k | if ((file = fmemopen((void *)data, size, "r")) == NULL) |
68 | 0 | goto bailout; |
69 | | |
70 | 5.56k | if ((handle = tj3Init(TJINIT_COMPRESS)) == NULL) |
71 | 0 | goto bailout; |
72 | | |
73 | 44.5k | for (ti = 0; ti < NUMTESTS; ti++) { |
74 | 38.9k | int pf = tests[ti].pf; |
75 | 38.9k | size_t dstSize = 0, maxBufSize, i, sum = 0; |
76 | | |
77 | | /* Test non-default compression options on specific iterations. */ |
78 | 38.9k | tj3Set(handle, TJPARAM_BOTTOMUP, ti == 0); |
79 | 38.9k | tj3Set(handle, TJPARAM_NOREALLOC, ti != 2); |
80 | 38.9k | tj3Set(handle, TJPARAM_RESTARTROWS, ti == 0 || ti == 6 ? 1 : 0); |
81 | | |
82 | 38.9k | tj3Set(handle, TJPARAM_MAXPIXELS, 1048576); |
83 | | /* tj3LoadImage12() will refuse to load images larger than 1 Megapixel, so |
84 | | we don't need to check the width and height here. */ |
85 | 38.9k | fseek(file, 0, SEEK_SET); |
86 | 38.9k | if ((srcBuf = _tj3LoadImageFromFileHandle12(handle, file, &width, 1, |
87 | 38.9k | &height, &pf)) == NULL) |
88 | 26.0k | continue; |
89 | | |
90 | 12.9k | maxBufSize = tj3JPEGBufSize(width, height, TJSAMP_444); |
91 | 12.9k | if (tj3Get(handle, TJPARAM_NOREALLOC)) { |
92 | 11.0k | if ((dstBuf = (unsigned char *)tj3Alloc(maxBufSize)) == NULL) |
93 | 0 | goto bailout; |
94 | 11.0k | } else |
95 | 1.87k | dstBuf = NULL; |
96 | | |
97 | 12.9k | tj3Set(handle, TJPARAM_LOSSLESS, 1); |
98 | 12.9k | tj3Set(handle, TJPARAM_LOSSLESSPSV, tests[ti].psv); |
99 | 12.9k | tj3Set(handle, TJPARAM_LOSSLESSPT, tests[ti].pt); |
100 | 12.9k | if (tj3Compress12(handle, srcBuf, width, 0, height, pf, &dstBuf, |
101 | 12.9k | &dstSize) == 0) { |
102 | | /* Touch all of the output data in order to catch uninitialized reads |
103 | | when using MemorySanitizer. */ |
104 | 398M | for (i = 0; i < dstSize; i++) |
105 | 398M | sum += dstBuf[i]; |
106 | 12.6k | } |
107 | | |
108 | 12.9k | tj3Free(dstBuf); |
109 | 12.9k | dstBuf = NULL; |
110 | 12.9k | tj3Free(srcBuf); |
111 | 12.9k | srcBuf = NULL; |
112 | | |
113 | | /* Prevent the sum above from being optimized out. This test should never |
114 | | be true, but the compiler doesn't know that. */ |
115 | 12.9k | if (sum > 255 * maxBufSize) |
116 | 0 | goto bailout; |
117 | 12.9k | } |
118 | | |
119 | 5.56k | bailout: |
120 | 5.56k | tj3Free(dstBuf); |
121 | 5.56k | tj3Free(srcBuf); |
122 | 5.56k | if (file) fclose(file); |
123 | 5.56k | tj3Destroy(handle); |
124 | 5.56k | return 0; |
125 | 5.56k | } |