-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.c
More file actions
215 lines (190 loc) · 5.27 KB
/
util.c
File metadata and controls
215 lines (190 loc) · 5.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* src/util.c
*
* Data conversion and handling utilities
*
* Copyright (C) 2018-2021 SCANOSS.COM
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include "util.h"
#include "limits.h"
#include "debug.h"
//#include "license.h"
#include <scanoss/ldb.h>
#include "crc32c.h"
/* Case insensitive string start comparison,
returns true if a starts with b or viceversa */
bool stristart(char *a, char *b)
{
if (!*a || !*b) return false;
while (*a && *b) if (tolower(*a++) != tolower(*b++)) return false;
return true;
}
/* Reverse an uint32 number */
void uint32_reverse(uint8_t *data)
{
uint8_t tmp = data[0];
data[0] = data[3];
data[3] = tmp;
tmp = data[1];
data[1] = data[2];
data[2] = tmp;
}
/* Compares two MD5 checksums */
bool md5cmp(uint8_t *md51, uint8_t *md52)
{
for (int i = 0; i < 16; i++)
if (md51[i] != md52[i])
return false;
return true;
}
/* Trim str */
void trim(char *str)
{
int i = 0;
/* Left trim */
int len = strlen(str);
for (i = 0; i < len; i++) if (!isspace(str[i])) break;
if (i) memmove(str, str + i, strlen(str + i) + 1);
/* Right trim */
len = strlen(str);
for (i = len - 1; i >= 0 ; i--) if (!isspace(str[i])) break;
str[i + 1] = 0;
}
/* Trim string at first non-printable char */
void printable_only(char *text)
{
for (int i = 0; i < strlen(text); i++)
if (text[i] < '"' || text[i] > 'z') text[i] = 0;
}
/* Returns the pair md5 of "component/vendor" */
void vendor_component_md5(char *component, char *vendor, uint8_t *out)
{
char pair[1024] = "\0";
if (strlen(component) + strlen(vendor) + 2 >= 1024) return;
/* Calculate pair_md5 */
sprintf(pair, "%s/%s", component, vendor);
for (int i = 0; i < strlen(pair); i++) pair[i] = tolower(pair[i]);
MD5((uint8_t *)pair, strlen(pair), out);
/* Log pair_md5 */
char hex[MD5_LEN * 2 + 1] = "\0";
ldb_bin_to_hex(out, MD5_LEN, hex);
scanlog("vendor/component: %s = %s\n", pair, hex);
}
/* Returns the pair md5 of "component/vendor" */
void vendor_component_md5(char *component, char *vendor, uint8_t *out)
{
char pair[1024] = "\0";
if (strlen(component) + strlen(vendor) + 2 >= 1024) return;
/* Calculate pair_md5 */
sprintf(pair, "%s/%s", component, vendor);
for (int i = 0; i < strlen(pair); i++) pair[i] = tolower(pair[i]);
MD5((uint8_t *)pair, strlen(pair), out);
/* Log pair_md5 */
char hex[MD5_LEN * 2 + 1] = "\0";
ldb_bin_to_hex(out, MD5_LEN, hex);
scanlog("vendor/component: %s = %s\n", pair, hex);
}
/* Removes chr from str */
void remove_char(char *str, char chr)
{
char *s = str;
while (*s)
{
if (*s == chr) memmove(s, s + 1, strlen(s + 1) + 1);
else s++;
}
}
/* Cleans str from unprintable characters or quotes */
void string_clean(char *str)
{
char *s = str;
while (*s)
{
if (*s < ' ' || *s == '"') *s = ' ';
else s++;
}
}
/* Returns the current date stamp */
char *datestamp()
{
time_t timestamp;
struct tm *times;
time(×tamp);
times = localtime(×tamp);
char *stamp = malloc(MAX_ARGLN);
strftime(stamp, MAX_ARGLN, "%FT%T%z", times);
return stamp;
}
/* Prints a "created" JSON element with the current datestamp */
void print_datestamp()
{
char *stamp = datestamp();
printf("%s", stamp);
free(stamp);
}
/* Returns a string with a hex representation of md5 */
char *md5_hex(uint8_t *md5)
{
char *out = calloc(2 * MD5_LEN + 1, 1);
for (int i = 0; i < MD5_LEN; i++) sprintf(out + strlen(out), "%02x", md5[i]);
return out;
}
/* Returns the CRC32C for a string */
uint32_t string_crc32c(char *str)
{
return calc_crc32c (str, strlen(str));
}
/* Check if a crc is found in the list (add it if not) */
bool add_CRC(uint32_t *list, uint32_t crc)
{
for (int i = 0; i < CRC_LIST_LEN; i++)
{
if (list[i] == 0)
{
list [i] = crc;
return false;
}
if (list[i] == crc) return true;
}
return false;
}
/* Check if a component is found in component_list (add it if not) */
// bool add_component(match_data *match)
// {
// char *purl = match->purl;
// char *vendor = match->vendor;
// char *component = match->component;
// char *version = match->version;
// char *latest_version = match->latest_version;
// /* Init component list */
// for (int i = 0; i < CRC_LIST_LEN; i++)
// {
// if (!strcmp(component_list[i].purl, purl)) return true;
// if (!*component_list[i].purl)
// {
// get_license(*match, component_list[i].license);
// strcpy(component_list[i].vendor, vendor);
// strcpy(component_list[i].component, component);
// strcpy(component_list[i].version, version);
// strcpy(component_list[i].latest_version, latest_version);
// strcpy(component_list[i].purl, purl);
// return false;
// }
// }
// return false;
// }