This repository has been archived by the owner on Oct 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 123
/
filecheck.c
353 lines (335 loc) · 8.63 KB
/
filecheck.c
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/**
* @file
*
* @brief Source for filecheck plugin
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*
*/
#ifndef HAVE_KDBCONFIG
#include "kdbconfig.h"
#endif
#include "filecheck.h"
#include <errno.h>
#include <iconv.h>
#include <kdberrors.h>
#include <kdbhelper.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static inline char * LEString (Lineending index)
{
static char * strings[] = { "NA", "CR", "LF", "CRLF", "LFCR" };
if (index > NUM_TYPES) return NULL;
return strings[index];
}
static inline Lineending strToLE (const char * str)
{
uint8_t counter = 0;
for (; counter < NUM_TYPES; ++counter)
{
if (!strcmp (LEString (counter), str)) return counter;
}
return NA;
}
int elektraFilecheckOpen (Plugin * handle ELEKTRA_UNUSED, Key * errorKey ELEKTRA_UNUSED)
{
// plugin initialization logic
KeySet * config = elektraPluginGetConfig (handle);
checkStruct * checkConf = (checkStruct *) elektraMalloc (sizeof (checkStruct));
checkConf->checkLineEnding = ksLookupByName (config, "/check/lineending", 0) != NULL;
checkConf->validLE = strToLE (keyString (ksLookupByName (config, "/valid/lineending", 0)));
checkConf->rejectNullByte = ksLookupByName (config, "/reject/null", 0) != NULL;
checkConf->checkEncoding = ksLookupByName (config, "/check/encoding", 0) != NULL;
checkConf->encoding = (char *) keyString (ksLookupByName (config, "/valid/encoding", 0));
checkConf->rejectBom = ksLookupByName (config, "/reject/bom", 0) != NULL;
checkConf->rejectUnprintable = ksLookupByName (config, "reject/unprintable", 0) != NULL;
elektraPluginSetData (handle, checkConf);
return 1; // success
}
int elektraFilecheckClose (Plugin * handle ELEKTRA_UNUSED, Key * errorKey ELEKTRA_UNUSED)
{
// free all plugin resources and shut it down
checkStruct * checkConf = (checkStruct *) elektraPluginGetData (handle);
if (checkConf) elektraFree (checkConf);
return 1; // success
}
static int checkNull (const uint8_t * line, ssize_t bytes)
{
int i;
for (i = 0; i < bytes; ++i)
{
if (line[i] == 0)
{
return i;
}
}
return 0;
}
static int checkBom (const uint8_t * line)
{
uint8_t i, j;
uint8_t found = 0;
for (i = 0; i < BOM_COUNT; ++i)
{
found = 1;
for (j = 0; j < BOM_SIZE_MAX && BOMS[i][j] != INTERNAL_BOM_DELIMITER; ++j)
{
if (line[j] != BOMS[i][j])
{
found = 0;
}
}
if (found) break;
}
if (found)
return -1;
else
return 0;
}
static int validateLineEnding (const uint8_t * line, Lineending * valid, int reset)
{
static uint8_t lastByte = 0;
if (reset || !line)
{
lastByte = 0;
return 0;
}
Lineending found = NA;
uint8_t fc;
uint16_t i = 0;
if (lastByte != 0)
{
fc = lastByte;
}
else
{
fc = line[0];
i = 1;
}
const ssize_t lineLength = (elektraStrLen ((char *) line) - 1);
for (; i < lineLength; ++i)
{
found = NA;
uint8_t sc = line[i];
switch (fc)
{
case LF_BYTE:
if (sc == CR_BYTE)
found = LFCR;
else if (sc == LF_BYTE)
found = LF;
else
found = LF;
break;
case CR_BYTE:
if (sc == LF_BYTE)
found = CRLF;
else if (sc == CR_BYTE)
found = CR;
else
found = CR;
break;
}
fc = sc;
lastByte = sc;
if ((found == CRLF || found == LFCR) && (i < (lineLength - 2)))
{
fc = line[i + 1];
lastByte = fc;
++i;
}
if (*valid != NA)
{
if (found != NA && found != *valid)
{
return i;
}
}
else
{
*valid = found;
}
}
// because we work an pairs of 2 bytes we need handle the last byte explicitly
if (found == NA)
{
if (fc == CR_BYTE && *valid != CR)
{
return i;
}
if (fc == LF_BYTE && *valid != LF)
{
return i;
}
}
return 0;
}
static int validateEncoding (const uint8_t * line, iconv_t conv, size_t bytesRead)
{
char * ptr = (char *) line;
char outBuffer[LINE_BYTES];
char * outPtr = outBuffer;
size_t inBytes = bytesRead;
size_t outSize = sizeof (outBuffer);
size_t ret = iconv (conv, &ptr, &inBytes, &outPtr, &outSize);
if (ret == (size_t) -1 && errno == EILSEQ)
{
return ((uint8_t *) ptr - line);
}
return 0;
}
static int checkUnprintable (const uint8_t * line)
{
unsigned int i;
for (i = 0; i < elektraStrLen ((char *) line); ++i)
{ // \n is > 0x7E too
if (line[i] < 0x20 || line[i] > 0x7E || line[i] == '\r') return i;
}
return 0;
}
static long checkFile (Key * parentKey, const char * filename, checkStruct * checkConf)
{
FILE * fp = fopen (filename, "rb");
if (fp == NULL)
{
ELEKTRA_SET_RESOURCE_ERRORF (parentKey, "Couldn't open file %s. Reason: %s", filename, strerror (errno));
return -1;
}
iconv_t conv = NULL;
if (checkConf->checkEncoding)
{
if (checkConf->encoding != NULL)
{
conv = iconv_open (checkConf->encoding, checkConf->encoding);
}
else
{
conv = iconv_open ("UTF-8", "UTF-8");
}
if (conv == (iconv_t) (-1))
{
ELEKTRA_SET_INSTALLATION_ERRORF (parentKey, "Couldn't initialize iconv with encoding %s\n", checkConf->encoding);
fclose (fp);
return -2;
}
}
uint8_t line[LINE_BYTES];
int iconv_ret = 0;
int bom_ret = 0;
int le_ret = 0;
int null_ret = 0;
int unprintable_ret = 0;
uint8_t firstLine = 1;
int retVal = 0;
if (checkConf->checkLineEnding) validateLineEnding (NULL, NULL, 1);
while (!feof (fp))
{
memset (line, 0, sizeof (line));
size_t bytesRead = fread (line, 1, sizeof (line), fp);
if (checkConf->checkLineEnding)
{
le_ret = validateLineEnding (line, &(checkConf->validLE), 0);
if (le_ret)
{
ELEKTRA_SET_VALIDATION_SYNTACTIC_ERRORF (parentKey, "Invalid lineending at position %zd in file %s",
bytesRead + le_ret, filename);
retVal = -1;
break;
}
}
if (checkConf->rejectNullByte)
{
null_ret = checkNull (line, bytesRead);
if (null_ret)
{
ELEKTRA_SET_VALIDATION_SYNTACTIC_ERRORF (parentKey, "Found null-byte at position %zd in file %s",
bytesRead + null_ret, filename);
retVal = -1;
break;
}
}
if (checkConf->checkEncoding)
{
iconv_ret = validateEncoding (line, conv, bytesRead);
if (iconv_ret)
{
ELEKTRA_SET_VALIDATION_SYNTACTIC_ERRORF (parentKey, "Invalid encoding at position %zd in file %s",
bytesRead + iconv_ret, filename);
retVal = -1;
break;
}
}
if (firstLine && checkConf->rejectBom)
{
bom_ret = checkBom (line);
if (bom_ret)
{
ELEKTRA_SET_VALIDATION_SYNTACTIC_ERRORF (parentKey, "Found no Byte Order Mark (BOM) in file %s", filename);
retVal = -1;
break;
}
firstLine = 0;
}
if (checkConf->rejectUnprintable)
{
unprintable_ret = checkUnprintable (line);
if (unprintable_ret)
{
ELEKTRA_SET_VALIDATION_SYNTACTIC_ERRORF (parentKey, "Unprintable character at position %zd in file %s",
bytesRead + unprintable_ret, filename);
retVal = -1;
break;
}
}
}
if (checkConf->checkEncoding)
{
iconv_close (conv);
}
fclose (fp);
return retVal;
}
int elektraFilecheckGet (Plugin * handle ELEKTRA_UNUSED, KeySet * returned ELEKTRA_UNUSED, Key * parentKey ELEKTRA_UNUSED)
{
if (!strcmp (keyName (parentKey), "system:/elektra/modules/filecheck"))
{
KeySet * contract = ksNew (
30, keyNew ("system:/elektra/modules/filecheck", KEY_VALUE, "filecheck plugin waits for your orders", KEY_END),
keyNew ("system:/elektra/modules/filecheck/exports", KEY_END),
keyNew ("system:/elektra/modules/filecheck/exports/open", KEY_FUNC, elektraFilecheckOpen, KEY_END),
keyNew ("system:/elektra/modules/filecheck/exports/close", KEY_FUNC, elektraFilecheckClose, KEY_END),
keyNew ("system:/elektra/modules/filecheck/exports/get", KEY_FUNC, elektraFilecheckGet, KEY_END),
keyNew ("system:/elektra/modules/filecheck/exports/commit", KEY_FUNC, elektraFilecheckCommit, KEY_END),
#include ELEKTRA_README
keyNew ("system:/elektra/modules/filecheck/infos/version", KEY_VALUE, PLUGINVERSION, KEY_END), KS_END);
ksAppend (returned, contract);
ksDel (contract);
return 1; // success
}
// get all keys
checkStruct * checkConf = elektraPluginGetData (handle);
const char * filename = keyString (parentKey);
int ret = checkFile (parentKey, filename, checkConf);
if (ret != 0) return -1;
return 1; // success
}
int elektraFilecheckCommit (Plugin * handle ELEKTRA_UNUSED, KeySet * returned ELEKTRA_UNUSED, Key * parentKey ELEKTRA_UNUSED)
{
// get all keys
checkStruct * checkConf = elektraPluginGetData (handle);
const char * filename = keyString (parentKey);
int ret = checkFile (parentKey, filename, checkConf);
if (ret != 0) return -1;
return 1; // success
}
Plugin * ELEKTRA_PLUGIN_EXPORT
{
// clang-format off
return elektraPluginExport ("filecheck",
ELEKTRA_PLUGIN_OPEN, &elektraFilecheckOpen,
ELEKTRA_PLUGIN_CLOSE, &elektraFilecheckClose,
ELEKTRA_PLUGIN_GET, &elektraFilecheckGet,
ELEKTRA_PLUGIN_COMMIT, &elektraFilecheckCommit,
ELEKTRA_PLUGIN_END);
}