/src/cms_profile_fuzzer.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Copyright 2022 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 <stdint.h> |
14 | | #include <assert.h> |
15 | | #include <stdlib.h> |
16 | | #include <unistd.h> |
17 | | |
18 | | #include "lcms2.h" |
19 | | |
20 | | cmsTagSignature tagsToRead[] = { |
21 | | cmsSigGreenColorantTag, |
22 | | cmsSigGreenMatrixColumnTag, |
23 | | cmsSigGreenTRCTag, |
24 | | cmsSigMeasurementTag, |
25 | | cmsSigNamedColorTag, |
26 | | cmsSigPreview1Tag, |
27 | | cmsSigPs2CRD2Tag, |
28 | | cmsSigPs2CRD3Tag, |
29 | | cmsSigRedTRCTag, |
30 | | }; |
31 | | |
32 | 2.98k | int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
33 | 2.98k | if (size == 0) |
34 | 0 | return 0; |
35 | | |
36 | 2.98k | char filename[256]; |
37 | 2.98k | sprintf(filename, "/tmp/libfuzzer.%d.icc", getpid()); |
38 | 2.98k | FILE *fp = fopen(filename, "wb"); |
39 | 2.98k | if (!fp) { |
40 | 0 | return 0; |
41 | 0 | } |
42 | 2.98k | fwrite(data, size, 1, fp); |
43 | 2.98k | fclose(fp); |
44 | | |
45 | 2.98k | cmsHPROFILE hProfile = cmsOpenProfileFromFile(filename, "r"); |
46 | | // If we have a profile, perform a set of operations |
47 | 2.98k | if (hProfile) { |
48 | 2.23k | char tagBuffer[4]; |
49 | | |
50 | | // Perform multiple tag reads. Read tags twice as behavior matters |
51 | | // if tags have been read before. |
52 | 6.71k | for (int j = 0; j < 2; j++) { |
53 | 44.7k | for (int i = 0; i < sizeof(tagsToRead)/sizeof(tagsToRead[0]); i++) { |
54 | 40.3k | cmsReadRawTag(hProfile, tagsToRead[i], tagBuffer, 4); |
55 | 40.3k | cmsReadRawTag(hProfile, tagsToRead[i], NULL, 0); |
56 | 40.3k | cmsReadRawTag(hProfile, tagsToRead[i], tagBuffer, 4); |
57 | 40.3k | cmsReadTag(hProfile, tagsToRead[i]); |
58 | 40.3k | } |
59 | 4.47k | } |
60 | | |
61 | | // Read profile info |
62 | 2.23k | cmsInfoType info = data[0] % 4; |
63 | 2.23k | char outBuffer[100]; |
64 | | |
65 | 2.23k | cmsGetProfileInfoASCII(hProfile, info, "DEN", "DEN", outBuffer, 100); |
66 | 2.23k | cmsGetTagCount(hProfile); |
67 | 2.23k | if (size > 2) { |
68 | 2.23k | cmsGetTagSignature(hProfile, (cmsUInt32Number)data[1]); |
69 | 2.23k | } |
70 | 2.23k | if (size > 40) { |
71 | 2.23k | cmsTagSignature tag = *((uint32_t *)(data+5)); |
72 | 2.23k | cmsTagLinkedTo(hProfile, tag); |
73 | 2.23k | } |
74 | | |
75 | | // Save to random file |
76 | 2.23k | cmsSaveProfileToFile(hProfile, "random.icc"); |
77 | 2.23k | cmsCloseProfile(hProfile); |
78 | 2.23k | } |
79 | | |
80 | | // Let's write the profile now. |
81 | 2.98k | hProfile = cmsOpenProfileFromFile(filename, "w"); |
82 | 2.98k | if (hProfile) { |
83 | 2.98k | char tagBuffer[4] = {'a', 'a', 'a', 'a'}; |
84 | | |
85 | | // Perform multiple tag reads |
86 | 8.95k | for (int j = 0; j < 2; j++) { |
87 | 59.7k | for (int i = 0; i < sizeof(tagsToRead)/sizeof(tagsToRead[0]); i++) { |
88 | 53.7k | cmsReadRawTag(hProfile, tagsToRead[i], tagBuffer, 4); |
89 | 53.7k | cmsReadRawTag(hProfile, tagsToRead[i], NULL, 0); |
90 | 53.7k | cmsReadRawTag(hProfile, tagsToRead[i], tagBuffer, 4); |
91 | 53.7k | cmsReadTag(hProfile, tagsToRead[i]); |
92 | 53.7k | } |
93 | 5.97k | } |
94 | | |
95 | 29.8k | for (int i = 0; i < sizeof(tagsToRead)/sizeof(tagsToRead[0]); i++) { |
96 | 26.8k | cmsWriteRawTag(hProfile, tagsToRead[i], tagBuffer, 4); |
97 | 26.8k | } |
98 | | |
99 | 8.95k | for (int j = 0; j < 2; j++) { |
100 | 59.7k | for (int i = 0; i < sizeof(tagsToRead)/sizeof(tagsToRead[0]); i++) { |
101 | 53.7k | cmsReadRawTag(hProfile, tagsToRead[i], tagBuffer, 4); |
102 | 53.7k | cmsReadRawTag(hProfile, tagsToRead[i], NULL, 0); |
103 | 53.7k | cmsReadRawTag(hProfile, tagsToRead[i], tagBuffer, 4); |
104 | 53.7k | cmsReadTag(hProfile, tagsToRead[i]); |
105 | 53.7k | } |
106 | 5.97k | } |
107 | | |
108 | 29.8k | for (int i = 0; i < sizeof(tagsToRead)/sizeof(tagsToRead[0]); i++) { |
109 | 26.8k | cmsWriteRawTag(hProfile, tagsToRead[i], tagBuffer, 4); |
110 | 26.8k | } |
111 | | |
112 | | // Save to random file |
113 | 2.98k | cmsSaveProfileToFile(hProfile, "random.icc"); |
114 | 2.98k | cmsCloseProfile(hProfile); |
115 | 2.98k | } |
116 | | |
117 | 2.98k | unlink(filename); |
118 | 2.98k | return 0; |
119 | 2.98k | } |