/src/cms_virtual_profile_fuzzer.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Copyright 2023 Google LLC |
2 | | Licensed under the Apache License, Version 2.0 (the "License"); |
3 | | you may not use this file except in compliance with the License. |
4 | | You may obtain a copy of the License at |
5 | | http://www.apache.org/licenses/LICENSE-2.0 |
6 | | Unless required by applicable law or agreed to in writing, software |
7 | | distributed under the License is distributed on an "AS IS" BASIS, |
8 | | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
9 | | See the License for the specific language governing permissions and |
10 | | limitations under the License. |
11 | | */ |
12 | | |
13 | | #include "lcms2.h" |
14 | | #include <stdint.h> |
15 | | |
16 | 4.97k | int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
17 | | |
18 | 4.97k | if (size < 16) { |
19 | 3 | return 0; |
20 | 3 | } |
21 | | |
22 | 4.97k | cmsHPROFILE hInProfile = cmsOpenProfileFromMem(data, size); |
23 | 4.97k | if (!hInProfile) { |
24 | 637 | return 0; |
25 | 637 | } |
26 | | |
27 | 4.33k | cmsHPROFILE hOutProfile = cmsCreate_sRGBProfile(); |
28 | 4.33k | if (!hOutProfile) { |
29 | 0 | cmsCloseProfile(hInProfile); |
30 | 0 | return 0; |
31 | 0 | } |
32 | 4.33k | cmsColorSpaceSignature srcCS = cmsGetColorSpace(hInProfile); |
33 | 4.33k | cmsUInt32Number nSrcComponents = cmsChannelsOf(srcCS); |
34 | 4.33k | cmsUInt32Number srcFormat; |
35 | 4.33k | if (srcCS == cmsSigLabData) { |
36 | 76 | srcFormat = |
37 | 76 | COLORSPACE_SH(PT_Lab) | CHANNELS_SH(nSrcComponents) | BYTES_SH(0); |
38 | 4.25k | } else { |
39 | 4.25k | srcFormat = |
40 | 4.25k | COLORSPACE_SH(PT_ANY) | CHANNELS_SH(nSrcComponents) | BYTES_SH(1); |
41 | 4.25k | } |
42 | 4.33k | cmsHTRANSFORM hTransform = cmsCreateTransform( |
43 | 4.33k | hInProfile, srcFormat, hOutProfile, TYPE_BGR_8, |
44 | 4.33k | *((const uint32_t *)data + 3) % 16, *((const uint32_t *)data + 2)); |
45 | | |
46 | 4.33k | cmsCloseProfile(hInProfile); |
47 | 4.33k | cmsCloseProfile(hOutProfile); |
48 | 4.33k | if (!hTransform) { |
49 | 3.71k | return 0; |
50 | 3.71k | } |
51 | | |
52 | 619 | cmsFloat64Number version; |
53 | 619 | if (*((const uint32_t *)data + 3) % 2 == 0) { |
54 | 474 | version = 3.4; |
55 | 474 | } else { |
56 | 145 | version = 4.4; |
57 | 145 | } |
58 | | |
59 | | // cmsTransform2DeviceLink |
60 | 619 | cmsHPROFILE devicelinkProfile = cmsTransform2DeviceLink( |
61 | 619 | hTransform, version, *((const uint32_t *)data + 2)); |
62 | | |
63 | | // clean up |
64 | 619 | cmsDeleteTransform(hTransform); |
65 | 619 | if (devicelinkProfile) { |
66 | 539 | cmsCloseProfile(devicelinkProfile); |
67 | 539 | } |
68 | | |
69 | | // cmsCreateLinearizationDeviceLink |
70 | 619 | cmsToneCurve *tone = cmsBuildGamma(NULL, *((const uint32_t *)data + 3)); |
71 | 619 | if (!tone) { |
72 | 0 | return 0; |
73 | 0 | } |
74 | | // 15 curves, so it can handle all color spaces |
75 | 619 | cmsToneCurve *rgb_curves[15] = {tone, tone, tone, tone, tone, |
76 | 619 | tone, tone, tone, tone, tone, |
77 | 619 | tone, tone, tone, tone, tone}; |
78 | 619 | cmsHPROFILE linearizationDeviceLinkProfile = |
79 | 619 | cmsCreateLinearizationDeviceLink(srcCS, rgb_curves); |
80 | | |
81 | 619 | cmsFreeToneCurve(tone); |
82 | | |
83 | 619 | if (linearizationDeviceLinkProfile) { |
84 | 610 | cmsCloseProfile(linearizationDeviceLinkProfile); |
85 | 610 | } |
86 | | |
87 | 619 | return 0; |
88 | 619 | } |