-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwarcsum.c
1388 lines (1283 loc) · 37.3 KB
/
warcsum.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
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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "warcsum.h"
double time_hash = 0, time_inflate = 0, time_parse = 0;
/*
* Initializes hash_ctx struct
*/
int
hash_init (void** hash_ctx, int hash)
{
/* TIME */
time_t now_hash;
time (&now_hash);
/* END OF TIME */
// fprintf (stderr, "HASH INIT\n");
switch (hash)
{
case 1:
*hash_ctx = calloc (1, sizeof (MD5_CTX));
return MD5_Init ((MD5_CTX*) * hash_ctx);
case 2:
*hash_ctx = calloc (1, sizeof (SHA_CTX));
return SHA1_Init ((SHA_CTX*) * hash_ctx);
case 3:
*hash_ctx = calloc (1, sizeof (SHA256_CTX));
return SHA256_Init ((SHA256_CTX*) * hash_ctx);
case 4:
*hash_ctx = calloc (1, sizeof (SHA512_CTX));
return SHA512_Init ((SHA512_CTX*) * hash_ctx);
default:
fprintf (stderr, "Unknown hash algorithm: %d!!\n"
"How did you get here?!\n\n", hash);
exit (EXIT_FAILURE);
}
/* TIME */
time_t then_hash;
time (&then_hash);
time_hash += difftime (then_hash, now_hash);
/* END OF TIME */
}
/*
* Update hash struct with input buffer
*/
int
hash_update (unsigned char* buffer, int hash,
int input_length, void* hash_ctx)
{
/* TIME */
time_t now_hash;
time (&now_hash);
/* END OF TIME */
input_length = input_length < 0 ? 0 : input_length;
switch (hash)
{
case 1: // calculate md5
return MD5_Update ((MD5_CTX*) hash_ctx, buffer, input_length);
case 2: // calculate sha1
return SHA1_Update ((SHA_CTX*) hash_ctx, buffer, input_length);
case 3: // calculate sha256
return SHA256_Update ((SHA256_CTX*) hash_ctx, buffer, input_length);
case 4: // calculate sha256
return SHA512_Update ((SHA512_CTX*) hash_ctx, buffer, input_length);
default:
fprintf (stderr, "Unknown hash algorithm: %d!!\n"
"How did you get here?!\n\n", hash);
exit (EXIT_FAILURE);
}
/* TIME */
time_t then_hash;
time (&then_hash);
time_hash += difftime (then_hash, now_hash);
/* END OF TIME */
}
/*
* Finalize hash and produce digest in hex
*/
int
hash_final (void* hash_ctx, int hash, char* computed_digest,
struct cli_args args)
{
/* TIME */
time_t now_hash;
time (&now_hash);
/* END OF TIME */
int j = 0;
int i;
int ret;
unsigned char result[DIGEST_LENGTH];
switch (hash)
{
case 1:
ret = MD5_Final (result, (MD5_CTX*) hash_ctx);
for (i = 0; i < MD5_DIGEST_LENGTH; i++, j += 2)
{
char temp[3];
snprintf (temp, sizeof (temp), "%02x", result[i]);
computed_digest[j] = temp[0];
computed_digest[j + 1] = temp[1];
}
computed_digest[j] = '\0';
if (args.verbose == 2)
{
fprintf (stderr, "Hash: MD5 \n");
}
break;
case 2:
ret = SHA1_Final (result, (SHA_CTX*) hash_ctx);
for (i = 0; i < SHA_DIGEST_LENGTH; i++, j += 2)
{
char temp[3];
snprintf (temp, sizeof (temp), "%02x", result[i]);
computed_digest[j] = temp[0];
computed_digest[j + 1] = temp[1];
}
computed_digest[j] = '\0';
if (args.verbose == 2)
{
fprintf (stderr, "Hash: SHA1 \n");
}
break;
case 3:
ret = SHA256_Final (result, (SHA256_CTX*) hash_ctx);
for (i = 0; i < SHA256_DIGEST_LENGTH; i++, j += 2)
{
char temp[3];
snprintf (temp, sizeof (temp), "%02x", result[i]);
computed_digest[j] = temp[0];
computed_digest[j + 1] = temp[1];
}
if (args.verbose == 2)
{
fprintf (stderr, "Hash: SHA256 \n");
}
computed_digest[j] = '\0';
break;
case 4:
ret = SHA512_Final (result, (SHA512_CTX*) hash_ctx);
for (i = 0; i < SHA512_DIGEST_LENGTH; i++, j += 2)
{
char temp[3];
snprintf (temp, sizeof (temp), "%02x", result[i]);
computed_digest[j] = temp[0];
computed_digest[j + 1] = temp[1];
}
if (args.verbose == 2)
{
fprintf (stderr, "Hash: SHA512 \n");
}
computed_digest[j] = '\0';
break;
default:
fprintf (stderr, "Unknown hash algorithm: %d!!\n"
"How did you get here?!\n\n", hash);
exit (EXIT_FAILURE);
}
free (hash_ctx);
/* TIME */
time_t then_hash;
time (&then_hash);
time_hash += difftime (then_hash, now_hash);
/* END OF TIME */
return ret;
}
/*
* Converts 5 bits integers to binary char array
*/
int
int_to_bin (int n, char* out)
{
int i = 5;
while (i--)
{
if (n % 2)
{
out[i] = '1';
}
else
{
out[i] = '0';
}
n /= 2;
}
return 0;
}
/*
* Converts base32 numbers following RFC 4648 to hexadecimal numbers
*/
int
base32_to_hex (char* in, char* out)
{
char binary[BINARY_SHA1_LENGTH];
/* If strlen(in) != 32 then digest was divided between 2 chunks */
if (strlen (in) != 32)
{
return -1;
}
/* base32 to binary */
int i;
for (i = 0; i < strlen (in); i++)
{
if ((in[i] >= 'A' && in[i] <= 'Z'))
{
char c[6];
c[5] = '\0';
int_to_bin (in[i] - 'A', c);
strcpy (&binary[i * 5], c);
}
else if (in[i] >= '2' && in[i] <= '7')
{
char c[6];
c[5] = '\0';
int_to_bin (in[i] - ('2' - 26), c);
strcpy (&binary[i * 5], c);
}
else
{
assert (0);
}
}
assert (strlen (binary) == 160);
/* binary to hex */
int s = 0;
for (i = 0; i < strlen (binary); i += 4, s++)
{
char temp[4];
memcpy (temp, &binary[i], 4);
int j;
int t = 0;
for (j = 3; j >= 0; j--)
{
if (temp[j] == '1')
{
t |= (1 << (3 - j));
}
}
out[s] = t < 10 ? t + '0' : t - 10 + 'a';
}
out[40] = '\0';
return 0;
}
/*
* Compares 2 char*s and returns 0 if equal, 1 otherwise
*/
short
strcmp_case_insensitive (char* a, const char* b)
{
if (strlen (a) != strlen (b))
{
return 1;
}
else
{
int i = 0;
while (a[i])
{
if (tolower (a[i]) != tolower (b[i]))
{
return 1;
}
i++;
}
return 0;
}
}
/*
* Process warcheader to check if http response, then extract DATE and URI
*/
int
process_warcheader (z_stream *z, void* vp)
{
struct warcsum_struct *attrs = (struct warcsum_struct*) vp;
int return_value = 0;
int read_bytes = -1;
char precomputed_digest[DIGEST_LENGTH];
char precomputed_hash[KEY_LENGTH];
char type[WARC_TYPE_LENGTH];
char content_type[CONTENT_TYPE_LENGTH];
char* str;
ssize_t read_length;
size_t ZERO;
short payload_digest_set = 0;
short is_warc_member = 1;
/*
* fmemopen is used to handle strings in memory as files for easier reading
* line by line
*/
FILE* member_file;
member_file = fmemopen (z->next_out,
attrs->effective_out - z->avail_out, "r");
/* allocate URI dynamically to handle large URLs*/
char* value = calloc (attrs->effective_out, sizeof (char));
attrs->URI = calloc (attrs->effective_out, sizeof (char));
/* Something went wrong creating member_file */
if (member_file == NULL)
{
fprintf (stderr, "Could not process header\n");
return -1;
}
/* TIME */
time_t now_parse;
time (&now_parse);
/* END OF TIME */
/* Read line and remove \n from its end. */
ZERO = 0;
str = NULL;
read_length = getline (&str, &ZERO, member_file);
if (str[read_length - 1] == '\n')
{
str[read_length - 1] = '\0';
}
/* Check if it has WARC header */
// comparison with WARC_HEADER length added to handle single line files
if (str != NULL && strcmp_case_insensitive (str, WARC_HEADER)
&& attrs->effective_out - z->avail_out > strlen (WARC_HEADER))
{
if (attrs->args.verbose == 2)
{
fprintf (stderr, "Not a WARC member!!\n");
}
/* TIME */
time_t then_parse;
time (&then_parse);
time_parse += difftime (then_parse, now_parse);
/* END OF TIME */
is_warc_member = 0;
return_value = -1;
}
ZERO = 0;
free (str);
read_length = getline (&str, &ZERO, member_file);
if (str[read_length - 1] == '\n')
{
str[read_length - 1] = '\0';
}
/* Process WARC header line by line*/
while (!feof (member_file) && strcmp_case_insensitive (str, "\r")
&& strcmp_case_insensitive (str, ""))
{
char key[KEY_LENGTH];
value[0] = '\0';
char *pch;
pch = strtok (str, " \n");
int i;
/* parse header line to (key: value) */
for (i = 0; pch != NULL; i++)
{
if (i == 0)
{
memcpy (key, pch, strlen (pch) - 1);
key[strlen (pch) - 1] = '\0';
}
else if (i == 1)
{
strcpy (value, pch);
}
pch = strtok (NULL, " \n\r");
}
free (pch);
/* check key and fill header warcsum_struct variables */
if (!strcmp_case_insensitive (key, WARC_TYPE))
{
strcpy (type, value);
if (attrs->args.verbose == 2)
{
fprintf (stderr, "WARC type: %s \n", value);
}
}
else if (!strcmp_case_insensitive (key, WARC_PAYLOAD_DIGEST)
&& !attrs->args.force_recalculate_digest)
{
payload_digest_set = 1;
char* pch;
pch = strtok (value, ":\r\n ");
int i;
for (i = 0; pch != NULL; i++)
{
if (i == 0)
{
memcpy (precomputed_hash, pch, strlen (pch));
precomputed_hash[strlen (pch)] = '\0';
}
else if (strcmp_case_insensitive (pch, ""))
{
memcpy (precomputed_digest, pch, strlen (pch));
precomputed_digest[strlen (pch)] = '\0';
}
pch = strtok (NULL, ":");
}
if (!strcmp_case_insensitive (precomputed_hash, attrs->args.hash_char))
{
attrs->recalculate_hash = 1;
}
if (attrs->args.verbose == 2)
{
fprintf (stderr, "WARC payload digest: %s:%s \n", precomputed_hash,
precomputed_digest);
}
free (pch);
}
else if (!strcmp_case_insensitive (key, WARC_DATE))
{
strcpy (attrs->DATE, value);
if (attrs->args.verbose == 2)
{
fprintf (stderr, "WARC date: %s \n", value);
}
}
else if (!strcmp_case_insensitive (key, WARC_TARGET_URI))
{
strcpy (attrs->URI, value);
if (attrs->args.verbose == 2)
{
fprintf (stderr, "WARC target uri: %s \n", value);
}
}
else if (!strcmp_case_insensitive (key, CONTENT_TYPE))
{
char* pch;
pch = strtok (value, ";\r\n ");
if (pch != NULL)
{
memcpy (content_type, pch, strlen (pch));
content_type[strlen (pch)] = '\0';
if (attrs->args.verbose == 2)
{
fprintf (stderr, "Content-Type: %s \n", content_type);
}
}
}
ZERO = 0;
free (str);
read_length = getline (&str, &ZERO, member_file);
if (str[read_length - 1] == '\n')
{
str[read_length - 1] = '\0';
}
}
free (value);
free (str);
read_bytes = ftell (member_file);
fclose (member_file);
/* if end of warc header (empty line) was not encountered,
* then need_double */
if (read_bytes == attrs->effective_out - z->avail_out && is_warc_member)
{
attrs->need_double = 1;
return_value = -1;
}
/* if warc member is not response */
if (strcmp_case_insensitive (type, "response")
&& strcmp_case_insensitive (type, ""))
{
if (attrs->args.verbose == 2)
{
fprintf (stderr, "WARC-Type is not \"response\" \n");
}
/* TIME */
time_t then_parse;
time (&then_parse);
time_parse += difftime (then_parse, now_parse);
/* END OF TIME */
return_value = -1;
}
/* if warc response is not application/http */
if (strcmp_case_insensitive (content_type, "application/http")
&& strcmp_case_insensitive (type, ""))
{
if (attrs->args.verbose == 2)
{
fprintf (stderr, "Content-type is not \"application/http\" \n");
}
/* TIME */
time_t then_parse;
time (&then_parse);
time_parse += difftime (then_parse, now_parse);
/* END OF TIME */
return_value = -1;
}
/*
* if digest is calculated and don't need to recalculate
* then fix it to hexadecimal
*/
if (return_value != -1 && !attrs->recalculate_hash)
{
int converted = base32_to_hex (precomputed_digest, attrs->stored_digest);
if (converted == -1)
{
return_value = -1;
}
else
{
if (attrs->args.verbose == 2)
{
fprintf (stderr, "Stored digest:\t%s:%s \n",
attrs->args.hash_char, attrs->stored_digest);
}
}
}
return return_value == -1 ? -1 : read_bytes;
}
/* Read the http header and discard it totally */
int
process_httpheader (z_stream *z, void *vp, int header_offset)
{
struct warcsum_struct *ws = (struct warcsum_struct*) vp;
int read_bytes = -1;
int return_value = 0;
char* str;
ssize_t read_length;
size_t ZERO;
FILE* member_file;
ZERO = 0;
member_file = fmemopen (&z->next_out[header_offset],
ws->effective_out - z->avail_out - header_offset,
"r");
if (member_file == NULL)
{
fprintf (stderr, "Could not process HTTP header\n");
return_value = -1;
}
else
{
read_length = getline (&str, &ZERO, member_file);
if (str[read_length - 1] == '\n')
{
str[read_length - 1] = '\0';
}
/* read HTTP header till first empty line and discard it */
while ((str != NULL && (strcmp_case_insensitive (str, "\r")
&& strcmp_case_insensitive (str, ""))))
{
ZERO = 0;
free (str);
read_length = getline (&str, &ZERO, member_file);
if (read_length && str[read_length - 1] == '\n')
{
str[read_length - 1] = '\0';
}
}
free (str);
read_bytes = ftell (member_file);
fclose (member_file);
}
return return_value == -1 ? -1 : read_bytes;
}
/* Process WARC header and extract: URI, Date */
int
process_header (z_stream *z, void* vp)
{
struct warcsum_struct *mm = (struct warcsum_struct*) vp;
int warc_header_length = 0;
int http_header_length = 0;
warc_header_length = process_warcheader (z, vp);
if (warc_header_length == -1)
{
return -1;
}
if (mm->effective_out - z->avail_out > warc_header_length)
{
http_header_length = process_httpheader (z, vp, warc_header_length);
}
if (http_header_length + warc_header_length
== mm->effective_out - z->avail_out)
{
mm->need_double = 1;
return -1;
}
else
{
return http_header_length + warc_header_length;
}
}
/*
* if provided chunk is at beginning of member,
* process header then hash payload
* else hash payload.
* Used as callback function by inflate member.
*/
void
process_chunk (z_stream* z, int chunk, void* vp)
{
// mm points to vp with casting for more readable and debuggable code.
struct warcsum_struct* ws = ((struct warcsum_struct*) vp);
// next_out_length holds inflated buffer size
int next_out_length = ws->effective_out - z->avail_out;
/* TIME */
time_t now_parse;
time (&now_parse);
/* END OF TIME */
int read_bytes = 0;
/*
* Last 4 bytes of a chunk are not hashed with the rest of the chunk to make
* sure they are not part of "\r\n\r\n" which is the separator between members
* in multi member WARC files.
*/
switch (chunk)
{
case CHUNK_FIRST: // first chunk of the member
// first process header
read_bytes = process_header (z, vp);
// if header is valid (warc file, warc response and http response)
if (read_bytes != -1)
{
ws->response = 1;
}
// if header is valid
if (ws->response)
{
// if recalculate digest
if (ws->recalculate_hash)
{
/*
* if warc payload length is greater than 4 chars, then save the
* last 4 chars in last_4 for handling \r\n\r\n
* else save all payload in last_4 and update size_last_4 with the
* number of chars saved
*/
if ((next_out_length - read_bytes) >= 4)
{
hash_update (&z->next_out[read_bytes], ws->computed_hash,
next_out_length - read_bytes - 4, ws->hash_ctx);
memcpy (ws->last_4, &z->next_out[next_out_length - 4], 4);
ws->size_last_4 = 4;
}
else
{
memcpy (ws->last_4, &z->next_out[read_bytes],
next_out_length - read_bytes);
ws->size_last_4 = next_out_length - read_bytes;
}
}
}
break;
case CHUNK_MIDDLE: // neither first nor last chunk of member
if (ws->response) // if header is processed and member is warc response
{
/* MIDDLE CHUNK:
* check if chunk length is >= 4 => hash last_4
* + all member except last 4 bytes
* o.w. follow comments in else section
*/
if (next_out_length >= 4)
{
if (ws->recalculate_hash)
{
// hash last 4 bytes from previous chunk
hash_update (ws->last_4, ws->computed_hash,
ws->size_last_4, ws->hash_ctx);
// hash current chunk except for last 4 bytes
hash_update (z->next_out, ws->computed_hash,
next_out_length - 4, ws->hash_ctx);
memcpy (ws->last_4, &z->next_out[next_out_length - 4], 4);
ws->size_last_4 = 4;
}
}
else
{
if (ws->recalculate_hash)
{
// length of chars to be hashed from last_4
int to_be_hashed = next_out_length + ws->size_last_4 - 4;
// hash last_4[0..to_be_hashed]
hash_update (ws->last_4, ws->computed_hash,
to_be_hashed, ws->hash_ctx);
// last_4 <- (4 - next_out_length) chars of last_4 + next_out
// push last_4[n..4] back to last_4[0..(4-n)]
// memmove used instead of memcpy
// due to overlapping source and destination
memmove (ws->last_4,
&ws->last_4[ws->size_last_4 - (4 - next_out_length)],
4 - next_out_length);
// append current chunk to last4
// (last_4[0..(4-n)] + current chunk)
memcpy (&ws->last_4[4 - next_out_length],
z->next_out,
next_out_length);
ws->size_last_4 = 4;
}
}
}
break;
case CHUNK_LAST:
/*
* LAST CHUNK:
* check if next_out_length >= 4, hash last_4 of previous chunk,
* then hash current chunk except for last 4 bytes of it "\r\n\r\n"
*
* o.w. hash last_4[0..n] of last chunk, where n in next_out_length
*/
if (ws->response) // if header is processed
{
if (ws->recalculate_hash)
{
if (next_out_length >= 4)
{
// hash last_4
hash_update (ws->last_4, ws->computed_hash,
ws->size_last_4, ws->hash_ctx);
// hash current chunk [0..(n-4)] where n is next_out_length
hash_update (z->next_out, ws->computed_hash,
next_out_length - 4, ws->hash_ctx);
}
else
{
// hash last_4[0..n] from previous chunk,
// where n is next_out_length + size_last_4 - 4
hash_update (ws->last_4, ws->computed_hash,
ws->size_last_4 + next_out_length - 4,
ws->hash_ctx);
}
}
}
break;
case CHUNK_FIRST_LAST:
/* Member fits in 1 chunk
* process header
* hash chunk[0..(n-4)] where n is next_out_length
*/
read_bytes = process_header (z, vp); // process header
// if header is valid (warc file, warc response and http response)
if (read_bytes != -1)
{
ws->response = 1;
}
// skip member if empty and argument skip empty is set
if (ws->response && (next_out_length - read_bytes <= 4) && ws->args.skip_empty)
{
ws->response = 0;
}
if (ws->response)
{
if (ws->recalculate_hash)
{
// hash chunk[0..(n-4)], where n is next_out_length
hash_update (&z->next_out[read_bytes], ws->computed_hash,
next_out_length - read_bytes - 4, ws->hash_ctx);
}
}
break;
}
/* TIME */
time_t then_parse;
time (&then_parse);
time_parse += difftime (then_parse, now_parse);
/* END OF TIME */
}
/* Processes next member from warc.gz file pointer */
int
process_member (FILE* f_in, FILE* f_out, z_stream *z,
struct warcsum_struct* ws)
{
if (ws->args.verbose == 2)
{
fprintf (stderr, "\n\n");
fprintf (stderr, "OFFSET: %ld\n", ftell (f_in));
}
/* Reset mydata */
ws->response = 0;
ws->last_4[0] = '\0';
ws->START = ftell (f_in);
/* Reset z_stream */
// for notes on '31' refer to libgzmulti code
inflateReset2 (z, 31);
/* Initialize hash_ctx struct */
hash_init (&ws->hash_ctx, ws->args.hash_code);
/* call inflateMember from libgzMulti */
int err = inflateMember (z, f_in, ws->effective_in,
ws->effective_out, process_chunk, ws);
/* Finalize hash_ctx and produce calculated digest */
hash_final (ws->hash_ctx, ws->args.hash_code,
ws->computed_digest, ws->args);
/* store position of end of member in compressed file
* requested by warccolres to download member
*/
ws->END = ftell (f_in);
if (ws->response) // if processed member was response
{
char final_digest[DIGEST_LENGTH];
char final_hash[HASH_LENGTH];
// if calculated hash was chosen
if (ws->recalculate_hash)
{
strcpy (final_digest, ws->computed_digest);
}
else // if stored hash was chosen
{
strcpy (final_digest, ws->stored_digest);
}
strcpy (final_hash, ws->args.hash_char);
snprintf (ws->manifest, sizeof (ws->manifest), "%s %u %u %s %s %s:%s\n", ws->WARCFILE_NAME,
ws->START, ws->END - ws->START, ws->URI,
ws->DATE, final_hash, final_digest);
if (ws->args.verbose == 2)
{
fprintf (stderr, "%s\n", ws->manifest);
}
// write digest to digests file
fwrite (ws->manifest, 1, strlen (ws->manifest), f_out);
free (ws->URI);
return 0;
}
else
{
free (ws->URI);
return 1;
}
}
/* Process warc.gz file */
int
process_file (char *in, FILE* f_out, z_stream* z, struct warcsum_struct* ws)
{
if (ws->args.verbose)
{
fprintf (stderr, "%s\n", in);
}
/* Open file */
int file_size;
FILE* f_in;
f_in = fopen (in, "r");
if (f_in == NULL)
{
fprintf (stderr, "Unable to open file: %s\n", in);
return 1;
}
/* check if regular file */
int fd = fileno (f_in);
struct stat ss = {0};
if (-1 == fstat (fd, &ss))
{
perror ("fstat() failed");
return 1;
}
else if (!S_ISREG (ss.st_mode))
{
fprintf (stderr, "%s is not a regular file.\n", in);
fclose (f_in);
return 1;
}
/* Calculate file size */
fseek (f_in, 0, SEEK_END);
file_size = ftell (f_in);
fseek (f_in, 0, SEEK_SET);
/* Get warc file name without full path to be used in digest file */
char temp_FILENAME[FILE_NAME_LENGTH];
strcpy (temp_FILENAME, in);
char* pch;
pch = strtok (temp_FILENAME, "/\\");
int i;
for (i = 0; pch != NULL; i++)
{
strcpy (ws->WARCFILE_NAME, pch);
pch = strtok (NULL, "/\\");
}
/* initialize effective_in/out with real_in/out for first member
*/
ws->effective_in = ws->args.real_in;
ws->effective_out = ws->args.real_out;
// fseek (f_in, 97285811, SEEK_SET);
// process member by member from the file, till end of file
do
{
/* reprocessing the same member with larger chunk size
* for fitting the header in the chunk
*/
if (ws->need_double)
{
if (ws->args.verbose == 2)
{
fprintf (stderr, "Chunk size not sufficient\n"
"Doubling chunk size\n"
"%u %u\n",
ws->START, ws->effective_out);
}
short doubled = 0;
// if can double effective_in_out without overflowing:
// double effective_in/out
// o.w. set to UINT_MAX
if (ws->effective_out * 2 > ws->effective_out)
{
ws->effective_out *= 2;
doubled++;
}
else if (ws->effective_out != UINT_MAX)
{
ws->effective_out = UINT_MAX;
doubled++;
}
if (ws->effective_in * 2 > ws->effective_in)
{
ws->effective_in *= 2;
doubled++;
}
else if (ws->effective_in != UINT_MAX)
{
ws->effective_in = UINT_MAX;
doubled++;
}
if (doubled)
{
// fseek back to start of member
fseek (f_in, ws->START, SEEK_SET);
reset (z, ws);
}
else
{
fprintf (stderr, "Both in buffer and out buffer reached maximum "
"allowed size!\n"
"Skipping member.\n");
}
}
else
{
/* reset effective_in/out in case they were changed
* due to not fitting header in last member
*/
ws->effective_in = ws->args.real_in;
ws->effective_out = ws->args.real_out;
reset (z, ws);