Line  | Count  | Source  | 
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 <stdint.h>  | 
14  |  | #include <stdlib.h>  | 
15  |  | #include "lcms2.h"  | 
16  |  |  | 
17  | 198  | wchar_t* generateWideString(const char* characters, const uint8_t *data){ | 
18  | 198  |     if (!characters){ | 
19  | 0  |         return NULL;  | 
20  | 0  |     }  | 
21  |  |       | 
22  | 198  |     char stringToWide[10];  | 
23  | 1.98k  |     for (int i = 0; i < 9; i++){ | 
24  | 1.78k  |         stringToWide[i] = characters[data[i] % 95];  | 
25  | 1.78k  |     }  | 
26  | 198  |     stringToWide[9] = '\0';  | 
27  |  |       | 
28  | 198  |     int requiredSize = mbstowcs(NULL, stringToWide, 0);  | 
29  | 198  |     wchar_t* wideString = (wchar_t *)malloc((requiredSize + 1) * sizeof(wchar_t));  | 
30  | 198  |     mbstowcs(wideString, stringToWide, requiredSize + 1);  | 
31  | 198  |     return wideString;  | 
32  | 198  | }  | 
33  |  |  | 
34  | 75  | int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { | 
35  | 75  |     if (size < 27){ | 
36  | 9  |         return 0;  | 
37  | 9  |     }  | 
38  |  |  | 
39  | 66  |     cmsContext context = cmsCreateContext(NULL, (void *)data);  | 
40  | 66  |     if (!context) { | 
41  | 0  |         return 0;  | 
42  | 0  |     }  | 
43  |  |  | 
44  |  |     // Create a Dictionary handle  | 
45  | 66  |     cmsHANDLE hDict = cmsDictAlloc(context);  | 
46  | 66  |     if (!hDict) { | 
47  | 0  |         return 0;  | 
48  | 0  |     }  | 
49  |  |       | 
50  |  |  | 
51  | 66  |     cmsMLU *mlu = cmsMLUalloc(hDict, 0);  | 
52  | 66  |     if (!mlu) { | 
53  | 0  |         return 0;  | 
54  | 0  |     }  | 
55  |  |       | 
56  | 66  |     char* characters = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"; | 
57  | 66  |     wchar_t* wideString = generateWideString(characters, data);  | 
58  | 66  |     cmsMLUsetWide(mlu, "en", "US", wideString);  | 
59  | 66  |     free(wideString);  | 
60  |  |       | 
61  |  |       | 
62  | 66  |     char ObtainedLanguage[3], ObtainedCountry[3];  | 
63  | 66  |     ObtainedLanguage[0] = characters[*(data+1) % 95];  | 
64  | 66  |     ObtainedLanguage[1] = characters[*(data+2) % 95];  | 
65  | 66  |     ObtainedLanguage[2] = characters[*(data) % 95];  | 
66  |  |  | 
67  | 66  |     ObtainedCountry[0] = characters[*(data+2) % 95];  | 
68  | 66  |     ObtainedCountry[1] = characters[*data % 95];  | 
69  | 66  |     ObtainedCountry[2] = characters[*(data+1) % 95];  | 
70  | 66  |     cmsMLUgetTranslation(mlu, "en", "US",ObtainedLanguage,ObtainedCountry);  | 
71  | 66  |     cmsMLUtranslationsCount(mlu);  | 
72  | 66  |     cmsMLUtranslationsCodes(mlu, *((uint32_t *)data), ObtainedLanguage, ObtainedCountry);  | 
73  |  |  | 
74  | 66  |     cmsMLU* displayName = mlu;  | 
75  | 66  |     cmsMLU* displayValue = mlu;  | 
76  |  |  | 
77  |  |     //cmsDictAddEntry  | 
78  | 66  |     wchar_t* name = generateWideString(characters, data + 9);  | 
79  | 66  |     wchar_t* value = generateWideString(characters, data + 18);  | 
80  | 66  |     cmsDictAddEntry(hDict, name, value, displayName, displayValue);  | 
81  | 66  |     free(name);  | 
82  | 66  |     free(value);  | 
83  |  |  | 
84  |  |     //cmsDictDup  | 
85  | 66  |     cmsHANDLE ResultDictDup = cmsDictDup(hDict);  | 
86  | 66  |     if (ResultDictDup) { | 
87  | 66  |         cmsDictFree(ResultDictDup);  | 
88  | 66  |     }  | 
89  |  |     // Iterate over the Dictionary entries  | 
90  | 66  |     const cmsDICTentry* entry = cmsDictGetEntryList(hDict);  | 
91  | 66  |     cmsDictNextEntry(entry);  | 
92  | 66  |     cmsMLUfree(mlu);  | 
93  | 66  |     cmsDictFree(hDict);  | 
94  | 66  |     return 0;  | 
95  | 66  | }  |