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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
|
/***************************************************************************
* _______ _______ _______ *
* | ___ |____ | ___ | *
* | | |_| / /| | |_| *
* | |_____ / / | |_____ *
* |_____ | / / |_____ | *
* _ | | / / _ | | *
* | |___| |/ /____| |___| | *
* |_______|_______|_______| *
* *
* Wiimms SZS Tools *
* https://szs.wiimm.de/ *
* *
***************************************************************************
* *
* This file is part of the SZS project. *
* Visit https://szs.wiimm.de/ for project details and sources. *
* *
* Copyright (c) 2011-2022 by Dirk Clemens <wiimm@wiimm.de> *
* *
***************************************************************************
* *
* 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. *
* *
* See file gpl-2.0.txt or http://www.gnu.org/licenses/gpl-2.0.txt *
* *
***************************************************************************/
#include <time.h>
#include <dirent.h>
#include <libgen.h>
#include "crypt.h"
#include "dclib-basics.h"
#include "dclib-numeric.h"
#include "lib-checksum.h"
#include "lib-szs.h"
//
///////////////////////////////////////////////////////////////////////////////
/////////////// struct sha1_size_t ///////////////
///////////////////////////////////////////////////////////////////////////////
void CreateSSChecksum ( char *buf, uint bufsize, const sha1_size_t *ss )
{
DASSERT(buf);
DASSERT(bufsize>1);
DASSERT(ss);
if (opt_db64)
{
PRINT("TAB64=%.65s\n",TableEncode64BySetup);
const int coding = opt_coding64 == ENCODE_OFF ? ENCODE_BASE64URL : opt_coding64;
EncodeByMode(buf,bufsize,(ccp)ss,sizeof(*ss),coding);
//DEL EncodeBase64(buf,bufsize,ss,sizeof(*ss),
//DEL TableEncode64BySetup, false,0,0 );
}
else if (opt_base64)
{
PRINT("TAB64=%.65s\n",TableEncode64BySetup);
EncodeBase64(buf,bufsize,ss->hash,sizeof(ss->hash),
TableEncode64BySetup, false,0,0);
}
else if (opt_id)
{
sha1_id_t id;
SHA1_to_ID(id,ss->hash);
StringCopyS(buf,bufsize,(ccp)id);
}
else if ( bufsize > 2*sizeof(ss->hash) )
{
int i;
for ( i = 0; i < sizeof(ss->hash); i++ )
sprintf(buf+2*i,"%02x",ss->hash[i]);
}
else
*buf = 0;
}
///////////////////////////////////////////////////////////////////////////////
void CreateSSChecksumBySZS ( char *buf, uint bufsize, const szs_file_t *szs )
{
DASSERT(buf);
DASSERT(bufsize>10);
if ( !szs || !szs->data )
{
*buf = 0;
return;
}
sha1_size_t info;
SHA1(szs->data,szs->size,info.hash);
info.size = htonl(szs->size);
CreateSSChecksum(buf,bufsize,&info);
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// normed DB coding (independent of options)
void CreateSSChecksumDB ( char *buf, uint bufsize, const sha1_size_t *ss )
{
DASSERT(buf);
DASSERT(bufsize>CHECKSUM_DB_SIZE);
DASSERT(ss);
const int coding = opt_coding64 == ENCODE_OFF ? ENCODE_BASE64URL : opt_coding64;
EncodeByMode(buf,bufsize,(ccp)ss,sizeof(*ss),coding);
//DEL EncodeBase64(buf,bufsize,ss,sizeof(*ss),TableEncode64url,false,0,0);
}
///////////////////////////////////////////////////////////////////////////////
void CreateSSChecksumDBByData ( char *buf, uint bufsize, cvp data, uint size )
{
DASSERT(data||!size);
DASSERT(bufsize>CHECKSUM_DB_SIZE);
if (!data)
*buf = 0;
else
{
sha1_size_t info;
SHA1(data,size,info.hash);
info.size = htonl(size);
CreateSSChecksumDB(buf,bufsize,&info);
}
}
///////////////////////////////////////////////////////////////////////////////
void CreateSSChecksumDBBySZS ( char *buf, uint bufsize, const szs_file_t *szs )
{
DASSERT(buf);
DASSERT(bufsize>CHECKSUM_DB_SIZE);
if (szs)
CreateSSChecksumDBByData(buf,bufsize,szs->data,szs->size);
else
*buf = 0;
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
enumError GetSSByFile ( sha1_size_t *ss, ccp path1, ccp path2 )
{
DASSERT(ss);
memset(ss,0,sizeof(*ss));
u8 *data = 0;
uint size = 0;
enumError err = OpenReadFILE(path1,path2,false,&data,&size,0,0);
if (err)
memset(ss,0,sizeof(*ss));
else
{
SHA1(data,size,ss->hash);
ss->size = htonl(size);
}
FREE(data);
return err;
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
int IsSSChecksum ( sha1_size_t *res, ccp source, int slen )
{
// slen < 0 => strlen(source)
// returns: 0:fail, 1:SHA1, 2:DB64
ccp end = source + ( slen < 0 ? strlen(source) : slen );
// skip leading controls
while ( source < end && (uchar)*source <= ' ' )
source++;
ccp start = source;
while ( source < end && (uchar)*source > ' ' )
source++;
sha1_size_t temp;
if (!res)
res = &temp;
const uint len = source - start;
if ( len == 40 )
{
ccp ptr;
for ( ptr = start; ptr < source; ptr++ )
{
const u8 type = TableNumbers[(u8)*ptr];
if ( type >= 16 )
break;
}
if ( ptr == source )
{
Sha1Hex2Bin(res->hash,start,source);
return 1;
}
}
else if ( len == CHECKSUM_DB_SIZE )
{
char buf[sizeof(sha1_size_t)+12];
uint len64 = DecodeBase64(buf,sizeof(buf),start,len,TableDecode64url,false,0);
if ( len64 == sizeof(sha1_size_t) )
{
memcpy(res,buf,sizeof(*res));
return 2;
}
}
// no a checksum
memset(res,0,sizeof(sha1_size_t));
return 0;
}
//
///////////////////////////////////////////////////////////////////////////////
/////////////// SZS cache support ///////////////
///////////////////////////////////////////////////////////////////////////////
ccp szs_cache_dir = 0;
ParamField_t szs_cache = {0};
static bool szs_cache_loaded = false;
static bool szs_cache_dirty = false;
static u64 szs_cache_last_scan = 0;
///////////////////////////////////////////////////////////////////////////////
void SetupSZSCache ( ccp dir_name, bool use_dirname )
{
PRINT("SetupSZSCache(%s,%d)\n",dir_name,use_dirname);
char buf[PATH_MAX];
if ( use_dirname && dir_name && !IsDirectory(dir_name,false) )
{
StringCopyS(buf,sizeof(buf),dir_name);
if (!opt_cname)
opt_cname = STRDUP(basename(buf));
dir_name = dirname(buf);
PRINT(" > new dir_name: %s\n",dir_name);
}
if ( dir_name && szs_cache_dir && !strcmp(dir_name,szs_cache_dir) )
return;
FreeString(szs_cache_dir);
szs_cache_dir = IsDirectory(dir_name,false) ? STRDUP(dir_name) : 0;
ResetParamField(&szs_cache);
szs_cache.free_data = true;
szs_cache_loaded = false;
PRINT("SZS_CACHE_DIR = %s\n",szs_cache_dir);
}
///////////////////////////////////////////////////////////////////////////////
void ScanSZSCache ( ccp dir_name, bool purge )
{
SetupSZSCache(dir_name,false);
if (!szs_cache_dir)
return;
szs_cache_loaded = szs_cache_dirty = true;
szs_cache_last_scan = time(0);
DIR * dir = opendir(dir_name);
if (dir)
{
for(;;)
{
struct dirent * dent = readdir(dir);
if (!dent)
break;
if ( *dent->d_name != '.' )
{
noPRINT("> %s\n",dent->d_name);
StoreSZSCache(dent->d_name,0,purge,0);
}
}
closedir(dir);
}
}
///////////////////////////////////////////////////////////////////////////////
enumError LoadSZSCache()
{
if ( szs_cache_loaded || !szs_cache_dir )
return ERR_OK;
szs_cache_loaded = true;
szs_cache_last_scan = 0;
char buf[PATH_MAX];
ccp path = PathCatPP(buf,sizeof(buf),szs_cache_dir,SZS_CACHE_FNAME);
PRINT("LoadSZSCache() %s\n",path);
File_t F;
enumError err = OpenFile(&F,true,path,FM_SILENT,0,0);
if (err)
return err;
while (fgets(buf,sizeof(buf)-1,F.f))
{
char *ptr = buf;
while ( *(uchar*)ptr <= ' ' )
ptr++;
if ( *ptr == '#' || *ptr == '!' )
continue;
if ( *ptr == '@' )
{
char name_buf[100], *name = name_buf;
ptr++;
for(;;)
{
const char ch = *ptr;
if ( !isalnum(ch) && ch != '_' && ch != '-' && ch != '.' )
break;
ptr++;
if ( name < name_buf + sizeof(name_buf) - 1 )
*name++ = toupper(ch);
}
while ( *ptr > 0 && *ptr <= ' ' )
ptr++;
if ( *ptr == '=' )
{
ptr++;
while ( *ptr > 0 && *ptr <= ' ' )
ptr++;
}
if ( name > name_buf )
{
*name = 0;
if (!strcmp(name_buf,"LAST-CACHE-SCAN"))
szs_cache_last_scan = strtoul(ptr,0,10);
}
continue;
}
uint len = strlen(ptr) - 1;
while ( len > 0 && (uchar)ptr[len] <= ' ' )
len--;
ptr[len+1] = 0;
if ( len <= CHECKSUM_DB_SIZE || ptr[CHECKSUM_DB_SIZE] != ' ' )
continue;
char *fname = ptr + CHECKSUM_DB_SIZE;
*fname++ = 0;
while ( *(uchar*)fname <= ' ' )
fname++;
if (!*fname)
continue;
StoreSZSCache(fname,ptr,false,0);
}
CloseFile(&F,0);
return ERR_OK;
}
///////////////////////////////////////////////////////////////////////////////
static int cmp_szs_cache ( const void * va, const void * vb )
{
DASSERT(va);
DASSERT(vb);
const ParamFieldItem_t *a = *(ParamFieldItem_t**)va;
const ParamFieldItem_t *b = *(ParamFieldItem_t**)vb;
DASSERT(a);
DASSERT(b);
return strcasecmp((ccp)a->data,(ccp)b->data);
}
//-----------------------------------------------------------------------------
enumError SaveSZSCache()
{
if ( !szs_cache_dir || !szs_cache_dirty )
return ERR_NOTHING_TO_DO;
szs_cache_dirty = false;
char path_buf[PATH_MAX];
ccp path = PathCatPP(path_buf,sizeof(path_buf),szs_cache_dir,SZS_CACHE_FNAME);
PRINT("SaveSZSCache() N=%u, %s\n",szs_cache.used,path);
File_t F;
enumError err = CreateFile(&F,true,path,FM_REMOVE);
if (err)
return err;
char tbuf[50];
if (szs_cache_last_scan)
{
time_t tim = szs_cache_last_scan;
struct tm *tm = localtime(&tim);
strftime(tbuf,sizeof(tbuf),"%F %T %z",tm);
}
else
StringCopyS(tbuf,sizeof(tbuf),"-");
fprintf(F.f,"#SZS-CACHE\n"
"# %u file%s cached\n"
"\n"
"@WSZST-VERSION = %s\n"
"@WSZST-REVISION = %u\n"
"@LAST-CACHE-SCAN = %llu = %s\n"
"\n"
,szs_cache.used ,szs_cache.used==1 ? "" : "s"
,VERSION
,REVISION_NUM
,szs_cache_last_scan,tbuf
);
if ( szs_cache.used )
{
ParamFieldItem_t **list = CALLOC(szs_cache.used,sizeof(*list));
uint i;
for ( i = 0; i < szs_cache.used; i++ )
list[i] = szs_cache.field+i;
if ( szs_cache.used > 1 )
qsort( list, szs_cache.used, sizeof(*list), cmp_szs_cache );
for ( i = 0; i < szs_cache.used; i++ )
{
const ParamFieldItem_t *it = list[i];
fprintf(F.f,"%s %s\n",it->key,(ccp)it->data);
}
FREE(list);
}
CloseFile(&F,0);
return ERR_OK;
}
///////////////////////////////////////////////////////////////////////////////
// only basename(fname) is used.
// if checksum==0: load file and calc checksum!
ParamFieldItem_t * StoreSZSCache
(
// return NULL or pointer to cache item
ccp fname, // NULL or filename, only basename(fname) is used
ccp checksum, // DB checksum; if NULL: load file and calc checksum
bool rename_file, // true: rename existing file
bool *r_exists // not NULL: store a status here
// => true: file result->data exists
)
{
if (r_exists)
*r_exists = false;
if (!szs_cache_loaded)
LoadSZSCache();
if (fname)
{
ccp slash = strrchr(fname,'/');
if (slash)
fname = slash+1;
slash = strrchr(fname,'\\');
if (slash)
fname = slash+1;
}
if (!*fname)
fname = 0;
PRINT("StoreSZSCache(%s,%s,%d,)\n",fname,checksum,rename_file);
ccp path = 0;
char path_buf[PATH_MAX], oldpath_buf[PATH_MAX], checksum_buf[CHECKSUM_DB_SIZE+1];
if (!checksum)
{
if (!fname)
return 0;
path = PathCatPP(path_buf,sizeof(path_buf),szs_cache_dir,fname);
noPRINT(">> %s\n",path);
szs_file_t szs;
InitializeSZS(&szs);
enumError err = LoadSZS(&szs,path,true,true,true);
if ( err > ERR_WARNING || err == ERR_NOT_EXISTS || !IsCompressedFF(szs.fform_file) )
{
ResetSZS(&szs);
return 0;
}
CreateSSChecksumDBBySZS(checksum_buf,sizeof(checksum_buf),&szs);
ResetSZS(&szs);
checksum = checksum_buf;
}
bool found;
ParamFieldItem_t *it = FindInsertParamField(&szs_cache,checksum,false,0,&found);
if (found)
{
DASSERT(it);
ccp oldpath = PathCatPP(oldpath_buf,sizeof(oldpath_buf),szs_cache_dir,(ccp)it->data);
struct stat st;
if ( stat(oldpath,&st) || !S_ISREG(st.st_mode) )
return it;
if (r_exists)
*r_exists = true;
if ( !rename_file || !fname || !strcmp(fname,(ccp)it->data) )
return it;
if (!path)
path = PathCatPP(path_buf,sizeof(path_buf),szs_cache_dir,fname);
PRINT("RENAME: %s -> %s\n",oldpath,path);
if (rename(oldpath,path))
return it;
FreeString(it->data);
it->data = NULL;
}
if (it)
{
PRINT("USE: %s\n",fname);
it->data = STRDUP(fname);
}
szs_cache_dirty = true;
return it;
}
//
///////////////////////////////////////////////////////////////////////////////
/////////////// END ///////////////
///////////////////////////////////////////////////////////////////////////////
|