-
Notifications
You must be signed in to change notification settings - Fork 15
/
warc.c
2334 lines (2015 loc) · 72.8 KB
/
warc.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
/* Utility functions for writing WARC files.
Copyright (C) 2011-2012, 2015, 2018-2022 Free Software Foundation,
Inc.
This file is part of GNU Wget.
GNU Wget 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 3 of the License, or (at
your option) any later version.
GNU Wget 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 Wget. If not, see <http://www.gnu.org/licenses/>.
Additional permission under GNU GPL version 3 section 7
If you modify this program, or any covered work, by linking or
combining it with the OpenSSL project's OpenSSL library (or a
modified version of that library), containing parts covered by the
terms of the OpenSSL or SSLeay licenses, the Free Software Foundation
grants you additional permission to convey the resulting work.
Corresponding Source for a non-source form of such a combination
shall include the source code for the parts of OpenSSL used as well
as that of the covered work. */
#include "wget.h"
#include "hash.h"
#include "utils.h"
#include "version.h"
#include "dirname.h"
#include "url.h"
#include "luahooks.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <tmpdir.h>
#include <sha1.h>
#include <base32.h>
#include <unistd.h>
#ifdef HAVE_LIBZ
#include <zlib.h>
#endif
#ifdef HAVE_ZSTD
#include <zstd.h>
#endif
#ifdef HAVE_LIBUUID
#include <uuid/uuid.h>
#elif HAVE_UUID_CREATE
#include <uuid.h>
#endif
#include "warc.h"
#include "exits.h"
#ifdef WINDOWS
/* we need this on Windows to have O_TEMPORARY defined */
# include <fcntl.h>
# include <rpc.h>
#endif
#ifndef O_TEMPORARY
#define O_TEMPORARY 0
#endif
#include "warc.h"
#include "exits.h"
/* The log file (a temporary file that contains a copy
of the wget log). */
static FILE *warc_log_fp;
/* The manifest file (a temporary file that contains the
warcinfo uuid of every file in this crawl). */
static FILE *warc_manifest_fp;
/* The current WARC file (or NULL, if WARC is disabled). */
static FILE *warc_current_file;
#ifdef HAVE_LIBZ
/* The gzip stream for the current WARC file
(or NULL, if WARC or gzip is disabled). */
static gzFile warc_current_gzfile;
#endif
#ifdef HAVE_ZSTD
static ZSTD_CCtx *warc_current_zst_context;
static ZSTD_CDict *warc_current_zst_dict;
static FILE *warc_current_zst_dict_file;
static bool warc_current_zst_full_record;
static void *warc_current_zst_dict_buffer;
static size_t warc_current_zst_buffer_in_size;
static void *warc_current_zst_buffer_in;
static size_t warc_current_zst_buffer_out_size;
static void *warc_current_zst_buffer_out;
#endif
#if defined(HAVE_LIBZ) || defined(HAVE_ZSTD)
/* The offset of the current compressed record in the WARC file. */
static off_t warc_current_compressed_file_offset;
/* The uncompressed size (so far) of the current record. */
static off_t warc_current_compressed_file_uncompressed_size;
# endif
/* This is true until a warc_write_* method fails. */
static bool warc_write_ok;
/* The current CDX file (or NULL, if CDX is disabled). */
static FILE *warc_current_cdx_file;
/* The record id of the warcinfo record of the current WARC file. */
static char warc_current_warcinfo_uuid_str[48];
/* The file name of the current WARC file. */
static char *warc_current_filename;
/* The serial number of the current WARC file. This number is
incremented each time a new file is opened and is used in the
WARC file's filename. */
static int warc_current_file_number;
/* The table of CDX records, if deduplication is enabled. */
static struct hash_table * warc_dedup_table;
static bool warc_start_new_file (bool meta);
struct warc_dedup_record
{
char *uri;
char *date;
char *uuid;
char *digest[SHA1_DIGEST_SIZE];
};
struct warc_dedup_key
{
char *uri;
char digest[SHA1_DIGEST_SIZE];
};
/* Calculates the hash of the warc_dedup_key for the hash table. If URL agnostic
deduplication is enabled, a SHA1 hash is calculated over the url, after which
a final SHA1 hash is calculated over the combination of the digest and the
hash of the URL. If not URL agnostic deduplication is used the digest is
used as final SHA1 hash.
The first bytes of the SHA1 hash are used as unsigned long hash for the hash
table. */
static unsigned long
warc_hash_sha1_digest (const void *key)
{
const struct warc_dedup_key *dedup_key = key;
unsigned long v = 0;
/* If URL agnostic deduplication is enabled, only the payload SHA1 hash is
used for the hash table index. If this is not enabled, the URI is hashed
as well and a hash of the concatenated digest and URI hashes is used for
the hash table index. */
if (opt.warc_dedup_url_agnostic)
memcpy (&v, dedup_key->digest, sizeof (unsigned long));
else
{
char digest[SHA1_DIGEST_SIZE*2];
char compare_digest[SHA1_DIGEST_SIZE];
memcpy(digest, dedup_key->digest, SHA1_DIGEST_SIZE);
sha1_buffer(dedup_key->uri, strlen(dedup_key->uri),
digest + SHA1_DIGEST_SIZE);
sha1_buffer(digest, SHA1_DIGEST_SIZE*2, compare_digest);
memcpy (&v, compare_digest, sizeof (unsigned long));
}
return v;
}
/* Checks the exact identity of the data from the hash table. If URL agnostic
deduplication is enabled, only the digest is compared, else both the URL and
the digest are compared. */
static int
warc_cmp_sha1_digest (const void *key1, const void *key2)
{
const struct warc_dedup_key *record1 = key1;
const struct warc_dedup_key *record2 = key2;
return memcmp (record1->digest, record2->digest, SHA1_DIGEST_SIZE) == 0 &&
(strcmp (record1->uri, record2->uri) == 0 || opt.warc_dedup_url_agnostic);
}
/* Writes SIZE bytes from BUFFER to the current WARC file,
through gzwrite or zstd if compression is enabled.
Returns the number of uncompressed bytes written. */
static size_t
warc_write_buffer (const char *buffer, size_t size)
{
#ifdef HAVE_LIBZ
if (warc_current_gzfile)
{
warc_current_compressed_file_uncompressed_size += size;
return gzwrite (warc_current_gzfile, buffer, size);
}
#endif
#ifdef HAVE_ZSTD
#ifdef HAVE_LIBZ
else
#endif
if (warc_current_zst_context)
{
warc_current_compressed_file_uncompressed_size += size;
ZSTD_inBuffer input = { buffer, size, 0 };
ZSTD_EndDirective mode = warc_current_zst_full_record ? ZSTD_e_end : ZSTD_e_continue;
bool finished;
do
{
ZSTD_outBuffer output = { warc_current_zst_buffer_out,
warc_current_zst_buffer_out_size, 0 };
size_t remaining = ZSTD_compressStream2 (warc_current_zst_context,
&output, &input, mode);
if (ZSTD_isError(remaining))
{
logprintf (LOG_NOTQUIET,
_("Error compressing data.\n"));
warc_write_ok = false;
return 0;
}
size_t written = fwrite(warc_current_zst_buffer_out, 1, output.pos,
warc_current_file);
if (written != output.pos)
{
logprintf (LOG_NOTQUIET,
_("Error writing to WARC ZST file.\n"));
warc_write_ok = false;
return 0;
}
finished = warc_current_zst_full_record ? (remaining == 0) : (input.pos == input.size);
} while (! finished);
if (input.pos != input.size)
{
logprintf (LOG_NOTQUIET,
_("Error reading all compressed data from buffer.\n"));
warc_write_ok = false;
return 0;
}
return size;
}
else
#endif
if (fwrite (buffer, 1, size, warc_current_file) != size)
{
warc_write_ok = false;
return 0;
}
else
return size;
}
/* Writes STR to the current WARC file.
Returns false and set warc_write_ok to false if there
is an error. */
static bool
warc_write_string (const char *str)
{
size_t n;
if (!warc_write_ok)
return false;
n = strlen (str);
if (n != warc_write_buffer (str, n))
warc_write_ok = false;
return warc_write_ok;
}
#define EXTRA_GZIP_HEADER_SIZE 14
#define GZIP_STATIC_HEADER_SIZE 10
#define FLG_FEXTRA 0x04
#define OFF_FLG 3
/* Starts a new WARC record. Writes the version header.
If opt.warc_maxsize is set and the current file is becoming
too large, this will open a new WARC file.
If compression is enabled, this will start a new
gzip stream in the current WARC file.
Returns false and set warc_write_ok to false if there
is an error. */
static bool
warc_write_start_record (void)
{
if (!warc_write_ok)
return false;
if (fflush (warc_current_file) != 0)
{
warc_write_ok = false;
return false;
}
if (opt.warc_maxsize > 0)
{
warc_current_compressed_file_offset = ftello (warc_current_file);
if (warc_current_compressed_file_offset < 0)
{
warc_write_ok = false;
return false;
}
if (warc_current_compressed_file_offset >= opt.warc_maxsize)
warc_start_new_file (false);
}
#if defined(HAVE_LIBZ) || defined(HAVE_ZSTD)
/* Start a GZIP or ZSTD stream, if required. */
if (opt.warc_compression_enabled)
{
warc_current_compressed_file_offset = ftello (warc_current_file);
if (warc_current_compressed_file_offset < 0)
{
warc_write_ok = false;
return false;
}
warc_current_compressed_file_uncompressed_size = 0;
#ifdef HAVE_ZSTD
if (opt.warc_compression_use_zstd)
{
warc_current_zst_full_record = false;
}
#ifdef HAVE_LIBZ
else
#endif
#endif
#ifdef HAVE_LIBZ
{
int dup_fd;
/* Record the starting offset of the new record. */
/* Reserve space for the extra GZIP header field.
In warc_write_end_record we will fill this space
with information about the uncompressed and
compressed size of the record. */
if (fseek (warc_current_file, EXTRA_GZIP_HEADER_SIZE, SEEK_CUR) < 0)
{
logprintf (LOG_NOTQUIET, _("Error setting WARC file position.\n"));
warc_write_ok = false;
return false;
}
if (fflush (warc_current_file) != 0)
{
logprintf (LOG_NOTQUIET, _("Error flushing WARC file to disk.\n"));
warc_write_ok = false;
return false;
}
/* Start a new GZIP or ZSTD stream. */
dup_fd = dup (fileno (warc_current_file));
if (dup_fd < 0)
{
logprintf (LOG_NOTQUIET,
_("Error duplicating WARC file file descriptor.\n"));
warc_write_ok = false;
return false;
}
warc_current_gzfile = gzdopen (dup_fd, "wb9");
if (warc_current_gzfile == NULL)
{
logprintf (LOG_NOTQUIET,
_("Error opening GZIP stream to WARC file.\n"));
close (dup_fd);
warc_write_ok = false;
return false;
}
}
#endif
}
#endif
warc_write_string ("WARC/1.1\r\n");
return warc_write_ok;
}
/* Writes a WARC header to the current WARC record.
This method may be run after warc_write_start_record and
before warc_write_block_from_file. */
static bool
warc_write_header (const char *name, const char *value)
{
if (value)
{
warc_write_string (name);
warc_write_string (": ");
warc_write_string (value);
warc_write_string ("\r\n");
}
return warc_write_ok;
}
/* Copies the contents of DATA_IN to the WARC record.
Adds a Content-Length header to the WARC record.
Run this method after warc_write_header,
then run warc_write_end_record. */
static bool
warc_write_block_from_file (FILE *data_in)
{
/* Add the Content-Length header. */
off_t content_length_i;
char content_length[MAX_INT_TO_STRING_LEN(off_t)];
size_t buffer_size;
#ifdef HAVE_ZSTD
if (warc_current_zst_context != NULL)
buffer_size = warc_current_zst_buffer_out_size;
else
#endif
buffer_size = BUFSIZ;
char buffer[buffer_size];
size_t s;
if (fseeko (data_in, 0L, SEEK_END) != 0)
warc_write_ok = false;
content_length_i = ftello (data_in);
if (content_length_i < 0)
warc_write_ok = false;
number_to_string (content_length, content_length_i);
warc_write_header ("Content-Length", content_length);
/* End of the WARC header section. */
warc_write_string ("\r\n");
if (fseeko (data_in, 0L, SEEK_SET) != 0)
warc_write_ok = false;
/* Copy the data in the file to the WARC record. */
while (warc_write_ok && (s = fread (buffer, 1, buffer_size, data_in)) > 0)
{
if (warc_write_buffer (buffer, s) < s)
warc_write_ok = false;
}
return warc_write_ok;
}
/* Run this method to close the current WARC record.
If compression is enabled, this method closes the
current GZIP stream and fills the extra GZIP header
with the uncompressed and compressed length of the
record. */
static bool
warc_write_end_record (void)
{
#ifdef HAVE_ZSTD
if (opt.warc_compression_use_zstd)
warc_current_zst_full_record = true;
#endif
if (warc_write_buffer ("\r\n\r\n", 4) != 4)
{
warc_write_ok = false;
return false;
}
#ifdef HAVE_LIBZ
/* We start a new gzip stream for each record. */
if (warc_write_ok && warc_current_gzfile)
{
char extra_header[EXTRA_GZIP_HEADER_SIZE];
char static_header[GZIP_STATIC_HEADER_SIZE];
off_t current_offset, uncompressed_size, compressed_size;
size_t result;
if (gzclose (warc_current_gzfile) != Z_OK)
{
warc_write_ok = false;
return false;
}
if (fflush (warc_current_file) != 0
|| fseeko (warc_current_file, 0, SEEK_END) != 0)
{
warc_write_ok = false;
return false;
}
/* The WARC standard suggests that we add 'skip length' data in the
extra header field of the GZIP stream.
In warc_write_start_record we reserved space for this extra header.
This extra space starts at warc_current_compressed_file_offset and fills
EXTRA_GZIP_HEADER_SIZE bytes. The static GZIP header starts at
warc_current_compressed_file_offset + EXTRA_GZIP_HEADER_SIZE.
We need to do three things:
1. Move the static GZIP header to warc_current_compressed_file_offset;
2. Set the FEXTRA flag in the GZIP header;
3. Write the extra GZIP header after the static header, that is,
starting at warc_current_compressed_file_offset + GZIP_STATIC_HEADER_SIZE.
*/
/* Calculate the uncompressed and compressed sizes. */
current_offset = ftello (warc_current_file);
if (current_offset < 0)
{
warc_write_ok = false;
return false;
}
uncompressed_size = current_offset - warc_current_compressed_file_offset;
compressed_size = warc_current_compressed_file_uncompressed_size;
/* Go back to the static GZIP header. */
result = fseeko (warc_current_file, warc_current_compressed_file_offset
+ EXTRA_GZIP_HEADER_SIZE, SEEK_SET);
if (result != 0)
{
warc_write_ok = false;
return false;
}
/* Read the header. */
if (fread (static_header, 1, GZIP_STATIC_HEADER_SIZE,
warc_current_file) != GZIP_STATIC_HEADER_SIZE)
{
warc_write_ok = false;
return false;
}
/* Set the FEXTRA flag in the flags byte of the header. */
static_header[OFF_FLG] = static_header[OFF_FLG] | FLG_FEXTRA;
/* Write the header back to the file, but starting at
warc_current_compressed_file_offset. */
if (fseeko (warc_current_file, warc_current_compressed_file_offset,
SEEK_SET) != 0
|| fwrite (static_header, 1, GZIP_STATIC_HEADER_SIZE,
warc_current_file) != GZIP_STATIC_HEADER_SIZE)
{
warc_write_ok = false;
return false;
}
/* Prepare the extra GZIP header. */
/* XLEN, the length of the extra header fields. */
extra_header[0] = ((EXTRA_GZIP_HEADER_SIZE - 2) & 255);
extra_header[1] = ((EXTRA_GZIP_HEADER_SIZE - 2) >> 8) & 255;
/* The extra header field identifier for the WARC skip length. */
extra_header[2] = 's';
extra_header[3] = 'l';
/* The size of the field value (8 bytes). */
extra_header[4] = (8 & 255);
extra_header[5] = ((8 >> 8) & 255);
/* The size of the uncompressed record. */
extra_header[6] = (uncompressed_size & 255);
extra_header[7] = (uncompressed_size >> 8) & 255;
extra_header[8] = (uncompressed_size >> 16) & 255;
extra_header[9] = (uncompressed_size >> 24) & 255;
/* The size of the compressed record. */
extra_header[10] = (compressed_size & 255);
extra_header[11] = (compressed_size >> 8) & 255;
extra_header[12] = (compressed_size >> 16) & 255;
extra_header[13] = (compressed_size >> 24) & 255;
/* Write the extra header after the static header. */
if (fseeko (warc_current_file, warc_current_compressed_file_offset
+ GZIP_STATIC_HEADER_SIZE, SEEK_SET) != 0
|| fwrite (extra_header, 1, EXTRA_GZIP_HEADER_SIZE,
warc_current_file) != EXTRA_GZIP_HEADER_SIZE
/* Done, move back to the end of the file. */
|| fflush (warc_current_file) != 0
|| fseeko (warc_current_file, 0, SEEK_END) != 0)
{
warc_write_ok = false;
return false;
}
if (ferror (warc_current_file))
{
warc_write_ok = false;
return false;
}
}
#endif
return warc_write_ok;
}
/* Writes the WARC-Date header for the given timestamp to
the current WARC record.
If timestamp is NULL, the current time will be used. */
static char *
warc_write_date_header (const char *timestamp)
{
char *current_timestamp;
if (timestamp)
current_timestamp = xstrdup (timestamp);
else
{
current_timestamp = xmalloc (21);
warc_timestamp (current_timestamp, 21);
}
if (! warc_write_header ("WARC-Date", current_timestamp))
return NULL;
return current_timestamp;
}
/* Writes the WARC-IP-Address header for the given IP to
the current WARC record. If IP is NULL, no header will
be written. */
static bool
warc_write_ip_header (const ip_address *ip)
{
if (ip != NULL)
return warc_write_header ("WARC-IP-Address", print_address (ip));
else
return warc_write_ok;
}
/* warc_sha1_stream_with_payload is a modified copy of sha1_stream
from gnulib/sha1.c. This version calculates two digests in one go.
Compute SHA1 message digests for bytes read from STREAM. The
digest of the complete file will be written into the 16 bytes
beginning at RES_BLOCK.
If payload_offset >= 0, a second digest will be calculated of the
portion of the file starting at payload_offset and continuing to
the end of the file. The digest number will be written into the
16 bytes beginning ad RES_PAYLOAD. */
static int
warc_sha1_stream_with_payload (FILE *stream, void *res_block, void *res_payload,
off_t payload_offset)
{
#define BLOCKSIZE 32768
struct sha1_ctx ctx_block;
struct sha1_ctx ctx_payload;
off_t pos;
off_t sum;
char *buffer = xmalloc (BLOCKSIZE + 72);
/* Initialize the computation context. */
sha1_init_ctx (&ctx_block);
if (payload_offset >= 0)
sha1_init_ctx (&ctx_payload);
pos = 0;
/* Iterate over full file contents. */
while (1)
{
/* We read the file in blocks of BLOCKSIZE bytes. One call of the
computation function processes the whole buffer so that with the
next round of the loop another block can be read. */
off_t n;
sum = 0;
/* Read block. Take care for partial reads. */
while (1)
{
n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
sum += n;
pos += n;
if (sum == BLOCKSIZE)
break;
if (n == 0)
{
/* Check for the error flag IF N == 0, so that we don't
exit the loop after a partial read due to e.g., EAGAIN
or EWOULDBLOCK. */
if (ferror (stream))
{
xfree (buffer);
return 1;
}
goto process_partial_block;
}
/* We've read at least one byte, so ignore errors. But always
check for EOF, since feof may be true even though N > 0.
Otherwise, we could end up calling fread after EOF. */
if (feof (stream))
goto process_partial_block;
}
/* Process buffer with BLOCKSIZE bytes. Note that
BLOCKSIZE % 64 == 0
*/
sha1_process_block (buffer, BLOCKSIZE, &ctx_block);
if (payload_offset >= 0 && payload_offset < pos)
{
/* At least part of the buffer contains data from payload. */
off_t start_of_payload = payload_offset - (pos - BLOCKSIZE);
if (start_of_payload <= 0)
/* All bytes in the buffer belong to the payload. */
start_of_payload = 0;
/* Process the payload part of the buffer.
Note: we can't use sha1_process_block here even if we
process the complete buffer. Because the payload doesn't
have to start with a full block, there may still be some
bytes left from the previous buffer. Therefore, we need
to continue with sha1_process_bytes. */
sha1_process_bytes (buffer + start_of_payload,
BLOCKSIZE - start_of_payload, &ctx_payload);
}
}
process_partial_block:;
/* Process any remaining bytes. */
if (sum > 0)
{
sha1_process_bytes (buffer, sum, &ctx_block);
if (payload_offset >= 0 && payload_offset < pos)
{
/* At least part of the buffer contains data from payload. */
off_t start_of_payload = payload_offset - (pos - sum);
if (start_of_payload <= 0)
/* All bytes in the buffer belong to the payload. */
start_of_payload = 0;
/* Process the payload part of the buffer. */
sha1_process_bytes (buffer + start_of_payload,
sum - start_of_payload, &ctx_payload);
}
}
/* Construct result in desired memory. */
sha1_finish_ctx (&ctx_block, res_block);
if (payload_offset >= 0)
sha1_finish_ctx (&ctx_payload, res_payload);
xfree (buffer);
return 0;
#undef BLOCKSIZE
}
/* Converts the SHA1 digest to a base32-encoded string.
"sha1:DIGEST\0" (Allocates a new string for the response.) */
static char *
warc_base32_sha1_digest (const char *sha1_digest, char *sha1_base32, size_t sha1_base32_size)
{
if (sha1_base32_size >= BASE32_LENGTH(SHA1_DIGEST_SIZE) + 5 + 1)
{
memcpy (sha1_base32, "sha1:", 5);
base32_encode (sha1_digest, SHA1_DIGEST_SIZE, sha1_base32 + 5,
sha1_base32_size - 5);
}
else
*sha1_base32 = 0;
return sha1_base32;
}
#ifdef HAVE_SSL
/* Converts a protocol to string used in the WARC protocol header. */
const char *
warc_protocol_to_string (enum secure_protocol protocol)
{
switch (protocol)
{
case secure_protocol_sslv2:
return "ssl/2";
case secure_protocol_sslv3:
return "ssl/3";
case secure_protocol_tlsv1:
return "tls/1.0";
case secure_protocol_tlsv1_1:
return "tls/1.1";
case secure_protocol_tlsv1_2:
return "tls/1.2";
case secure_protocol_tlsv1_3:
return "tls/1.3";
default:
abort ();
}
}
#endif
/* Sets the digest headers of the record.
This method will calculate the block digest and, if payload_offset >= 0,
will also calculate the payload digest of the payload starting at the
provided offset. */
static void
warc_write_digest_headers (FILE *file, long payload_offset)
{
if (opt.warc_digests_enabled)
{
/* Calculate the block and payload digests. */
char sha1_res_block[SHA1_DIGEST_SIZE];
char sha1_res_payload[SHA1_DIGEST_SIZE];
rewind (file);
if (warc_sha1_stream_with_payload (file, sha1_res_block,
sha1_res_payload, payload_offset) == 0)
{
char digest[BASE32_LENGTH(SHA1_DIGEST_SIZE) + 1 + 5];
warc_write_header ("WARC-Block-Digest",
warc_base32_sha1_digest (sha1_res_block, digest, sizeof(digest)));
if (payload_offset >= 0)
warc_write_header ("WARC-Payload-Digest",
warc_base32_sha1_digest (sha1_res_payload, digest, sizeof(digest)));
}
}
}
/* Fills timestamp with the current time and date.
The UTC time is formatted following ISO 8601, as required
for use in the WARC-Date header.
The timestamp will be 21 characters long. */
char *
warc_timestamp (char *timestamp, size_t timestamp_size)
{
time_t rawtime = time (NULL);
struct tm * timeinfo = gmtime (&rawtime);
if (strftime (timestamp, timestamp_size, "%Y-%m-%dT%H:%M:%SZ", timeinfo) == 0
&& timestamp_size > 0)
*timestamp = 0;
return timestamp;
}
/* Fills urn_str with a UUID in the format required
for the WARC-Record-Id header.
The string will be 47 characters long. */
#if HAVE_LIBUUID
void
warc_uuid_str (char *urn_str, size_t urn_size)
{
char uuid_str[37];
uuid_t record_id;
uuid_generate (record_id);
uuid_unparse (record_id, uuid_str);
if (snprintf (urn_str, urn_size, "<urn:uuid:%s>", uuid_str) < 0)
warc_write_ok = false;
}
#elif HAVE_UUID_CREATE
void
warc_uuid_str (char *urn_str, size_t urn_size)
{
char *uuid_str;
uuid_t record_id;
uuid_create (&record_id, NULL);
uuid_to_string (&record_id, &uuid_str, NULL);
if (snprintf (urn_str, urn_size, "<urn:uuid:%s>", uuid_str) < 0)
warc_write_ok = false;
xfree (uuid_str);
}
#else
# ifdef WINDOWS
typedef RPC_STATUS (RPC_ENTRY * UuidCreate_proc) (UUID *);
typedef RPC_STATUS (RPC_ENTRY * UuidToString_proc) (UUID *, unsigned char **);
typedef RPC_STATUS (RPC_ENTRY * RpcStringFree_proc) (unsigned char **);
static int
windows_uuid_str (char *urn_str, size_t urn_size)
{
static UuidCreate_proc pfn_UuidCreate = NULL;
static UuidToString_proc pfn_UuidToString = NULL;
static RpcStringFree_proc pfn_RpcStringFree = NULL;
static int rpc_uuid_avail = -1;
/* Rpcrt4.dll is not available on older versions of Windows, so we
need to test its availability at run time. */
if (rpc_uuid_avail == -1)
{
HMODULE hm_rpcrt4 = LoadLibrary ("Rpcrt4.dll");
if (hm_rpcrt4)
{
pfn_UuidCreate =
(UuidCreate_proc) GetProcAddress (hm_rpcrt4, "UuidCreate");
pfn_UuidToString =
(UuidToString_proc) GetProcAddress (hm_rpcrt4, "UuidToStringA");
pfn_RpcStringFree =
(RpcStringFree_proc) GetProcAddress (hm_rpcrt4, "RpcStringFreeA");
if (pfn_UuidCreate && pfn_UuidToString && pfn_RpcStringFree)
rpc_uuid_avail = 1;
else
rpc_uuid_avail = 0;
}
else
rpc_uuid_avail = 0;
}
if (rpc_uuid_avail)
{
BYTE *uuid_str;
UUID uuid;
if (pfn_UuidCreate (&uuid) == RPC_S_OK)
{
if (pfn_UuidToString (&uuid, &uuid_str) == RPC_S_OK)
{
if (snprintf (urn_str, urn_size, "<urn:uuid:%s>", uuid_str) < 0)
warc_write_ok = false;
pfn_RpcStringFree (&uuid_str);
return 1;
}
}
}
return 0;
}
#endif
/* Fills urn_str with a UUID based on random numbers in the format
required for the WARC-Record-Id header.
(See RFC 4122, UUID version 4.)
Note: this is a fallback method, it is much better to use the
methods provided by libuuid.
The string will be 47 characters long. */
void
warc_uuid_str (char *urn_str, size_t urn_size)
{
/* RFC 4122, a version 4 UUID with only random numbers */
unsigned char uuid_data[16];
int i;
#ifdef WINDOWS
/* If the native method fails (expected on older Windows versions),
use the fallback below. */
if (windows_uuid_str (urn_str, urn_size))
return;
#endif
for (i=0; i<16; i++)
uuid_data[i] = random_number (255);
/* Set the four most significant bits (bits 12 through 15) of the
time_hi_and_version field to the 4-bit version number */
uuid_data[6] = (uuid_data[6] & 0x0F) | 0x40;
/* Set the two most significant bits (bits 6 and 7) of the
clock_seq_hi_and_reserved to zero and one, respectively. */
uuid_data[8] = (uuid_data[8] & 0xBF) | 0x80;
if (snprintf (urn_str, urn_size,
"<urn:uuid:%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x>",
uuid_data[0], uuid_data[1], uuid_data[2], uuid_data[3], uuid_data[4],
uuid_data[5], uuid_data[6], uuid_data[7], uuid_data[8], uuid_data[9],
uuid_data[10], uuid_data[11], uuid_data[12], uuid_data[13], uuid_data[14],
uuid_data[15]) < 0)
warc_write_ok = false;
}
#endif
/* Write a warcinfo record to the current file.
Updates warc_current_warcinfo_uuid_str. */
static bool
warc_write_warcinfo_record (const char *filename)
{
FILE *warc_tmp;
char timestamp[22];
char *filename_basename;
int i;
/* Write warc-info record as the first record of the file. */
/* We add the record id of this info record to the other records in the
file. */
warc_uuid_str (warc_current_warcinfo_uuid_str, sizeof (warc_current_warcinfo_uuid_str));
warc_timestamp (timestamp, sizeof(timestamp));