]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - contrib/libarchive/libarchive/archive_read_support_format_warc.c
Fix incomplete merge in r313927:
[FreeBSD/stable/10.git] / contrib / libarchive / libarchive / archive_read_support_format_warc.c
1 /*-
2  * Copyright (c) 2014 Sebastian Freundt
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "archive_platform.h"
27 __FBSDID("$FreeBSD$");
28
29 /**
30  * WARC is standardised by ISO TC46/SC4/WG12 and currently available as
31  * ISO 28500:2009.
32  * For the purposes of this file we used the final draft from:
33  * http://bibnum.bnf.fr/warc/WARC_ISO_28500_version1_latestdraft.pdf
34  *
35  * Todo:
36  * [ ] real-world warcs can contain resources at endpoints ending in /
37  *     e.g. http://bibnum.bnf.fr/warc/
38  *     if you're lucky their response contains a Content-Location: header
39  *     pointing to a unix-compliant filename, in the example above it's
40  *     Content-Location: http://bibnum.bnf.fr/warc/index.html
41  *     however, that's not mandated and github for example doesn't follow
42  *     this convention.
43  *     We need a set of archive options to control what to do with
44  *     entries like these, at the moment care is taken to skip them.
45  *
46  **/
47
48 #ifdef HAVE_SYS_STAT_H
49 #include <sys/stat.h>
50 #endif
51 #ifdef HAVE_ERRNO_H
52 #include <errno.h>
53 #endif
54 #ifdef HAVE_STDLIB_H
55 #include <stdlib.h>
56 #endif
57 #ifdef HAVE_STRING_H
58 #include <string.h>
59 #endif
60 #ifdef HAVE_LIMITS_H
61 #include <limits.h>
62 #endif
63 #ifdef HAVE_CTYPE_H
64 #include <ctype.h>
65 #endif
66 #ifdef HAVE_TIME_H
67 #include <time.h>
68 #endif
69
70 #include "archive.h"
71 #include "archive_entry.h"
72 #include "archive_private.h"
73 #include "archive_read_private.h"
74
75 typedef enum {
76         WT_NONE,
77         /* warcinfo */
78         WT_INFO,
79         /* metadata */
80         WT_META,
81         /* resource */
82         WT_RSRC,
83         /* request, unsupported */
84         WT_REQ,
85         /* response, unsupported */
86         WT_RSP,
87         /* revisit, unsupported */
88         WT_RVIS,
89         /* conversion, unsupported */
90         WT_CONV,
91         /* continuation, unsupported at the moment */
92         WT_CONT,
93         /* invalid type */
94         LAST_WT
95 } warc_type_t;
96
97 typedef struct {
98         size_t len;
99         const char *str;
100 } warc_string_t;
101
102 typedef struct {
103         size_t len;
104         char *str;
105 } warc_strbuf_t;
106
107 struct warc_s {
108         /* content length ahead */
109         size_t cntlen;
110         /* and how much we've processed so far */
111         size_t cntoff;
112         /* and how much we need to consume between calls */
113         size_t unconsumed;
114
115         /* string pool */
116         warc_strbuf_t pool;
117         /* previous version */
118         unsigned int pver;
119         /* stringified format name */
120         struct archive_string sver;
121 };
122
123 static int _warc_bid(struct archive_read *a, int);
124 static int _warc_cleanup(struct archive_read *a);
125 static int _warc_read(struct archive_read*, const void**, size_t*, int64_t*);
126 static int _warc_skip(struct archive_read *a);
127 static int _warc_rdhdr(struct archive_read *a, struct archive_entry *e);
128
129 /* private routines */
130 static unsigned int _warc_rdver(const char buf[10], size_t bsz);
131 static unsigned int _warc_rdtyp(const char *buf, size_t bsz);
132 static warc_string_t _warc_rduri(const char *buf, size_t bsz);
133 static ssize_t _warc_rdlen(const char *buf, size_t bsz);
134 static time_t _warc_rdrtm(const char *buf, size_t bsz);
135 static time_t _warc_rdmtm(const char *buf, size_t bsz);
136 static const char *_warc_find_eoh(const char *buf, size_t bsz);
137 static const char *_warc_find_eol(const char *buf, size_t bsz);
138
139 int
140 archive_read_support_format_warc(struct archive *_a)
141 {
142         struct archive_read *a = (struct archive_read *)_a;
143         struct warc_s *w;
144         int r;
145
146         archive_check_magic(_a, ARCHIVE_READ_MAGIC,
147             ARCHIVE_STATE_NEW, "archive_read_support_format_warc");
148
149         if ((w = calloc(1, sizeof(*w))) == NULL) {
150                 archive_set_error(&a->archive, ENOMEM,
151                     "Can't allocate warc data");
152                 return (ARCHIVE_FATAL);
153         }
154
155         r = __archive_read_register_format(
156                 a, w, "warc",
157                 _warc_bid, NULL, _warc_rdhdr, _warc_read,
158                 _warc_skip, NULL, _warc_cleanup, NULL, NULL);
159
160         if (r != ARCHIVE_OK) {
161                 free(w);
162                 return (r);
163         }
164         return (ARCHIVE_OK);
165 }
166
167 static int
168 _warc_cleanup(struct archive_read *a)
169 {
170         struct warc_s *w = a->format->data;
171
172         if (w->pool.len > 0U) {
173                 free(w->pool.str);
174         }
175         archive_string_free(&w->sver);
176         free(w);
177         a->format->data = NULL;
178         return (ARCHIVE_OK);
179 }
180
181 static int
182 _warc_bid(struct archive_read *a, int best_bid)
183 {
184         const char *hdr;
185         ssize_t nrd;
186         unsigned int ver;
187
188         (void)best_bid; /* UNUSED */
189
190         /* check first line of file, it should be a record already */
191         if ((hdr = __archive_read_ahead(a, 12U, &nrd)) == NULL) {
192                 /* no idea what to do */
193                 return -1;
194         } else if (nrd < 12) {
195                 /* nah, not for us, our magic cookie is at least 12 bytes */
196                 return -1;
197         }
198
199         /* otherwise snarf the record's version number */
200         ver = _warc_rdver(hdr, nrd);
201         if (ver < 1200U || ver > 10000U) {
202                 /* we only support WARC 0.12 to 1.0 */
203                 return -1;
204         }
205
206         /* otherwise be confident */
207         return (64);
208 }
209
210 static int
211 _warc_rdhdr(struct archive_read *a, struct archive_entry *entry)
212 {
213 #define HDR_PROBE_LEN           (12U)
214         struct warc_s *w = a->format->data;
215         unsigned int ver;
216         const char *buf;
217         ssize_t nrd;
218         const char *eoh;
219         /* for the file name, saves some strndup()'ing */
220         warc_string_t fnam;
221         /* warc record type, not that we really use it a lot */
222         warc_type_t ftyp;
223         /* content-length+error monad */
224         ssize_t cntlen;
225         /* record time is the WARC-Date time we reinterpret it as ctime */
226         time_t rtime;
227         /* mtime is the Last-Modified time which will be the entry's mtime */
228         time_t mtime;
229
230 start_over:
231         /* just use read_ahead() they keep track of unconsumed
232          * bits and bobs for us; no need to put an extra shift in
233          * and reproduce that functionality here */
234         buf = __archive_read_ahead(a, HDR_PROBE_LEN, &nrd);
235
236         if (nrd < 0) {
237                 /* no good */
238                 archive_set_error(
239                         &a->archive, ARCHIVE_ERRNO_MISC,
240                         "Bad record header");
241                 return (ARCHIVE_FATAL);
242         } else if (buf == NULL) {
243                 /* there should be room for at least WARC/bla\r\n
244                  * must be EOF therefore */
245                 return (ARCHIVE_EOF);
246         }
247         /* looks good so far, try and find the end of the header now */
248         eoh = _warc_find_eoh(buf, nrd);
249         if (eoh == NULL) {
250                 /* still no good, the header end might be beyond the
251                  * probe we've requested, but then again who'd cram
252                  * so much stuff into the header *and* be 28500-compliant */
253                 archive_set_error(
254                         &a->archive, ARCHIVE_ERRNO_MISC,
255                         "Bad record header");
256                 return (ARCHIVE_FATAL);
257         }
258         ver = _warc_rdver(buf, eoh - buf);
259         /* we currently support WARC 0.12 to 1.0 */
260         if (ver == 0U) {
261                 archive_set_error(
262                         &a->archive, ARCHIVE_ERRNO_MISC,
263                         "Invalid record version");
264                 return (ARCHIVE_FATAL);
265         } else if (ver < 1200U || ver > 10000U) {
266                 archive_set_error(
267                         &a->archive, ARCHIVE_ERRNO_MISC,
268                         "Unsupported record version: %u.%u",
269                         ver / 10000, (ver % 10000) / 100);
270                 return (ARCHIVE_FATAL);
271         }
272         cntlen = _warc_rdlen(buf, eoh - buf);
273         if (cntlen < 0) {
274                 /* nightmare!  the specs say content-length is mandatory
275                  * so I don't feel overly bad stopping the reader here */
276                 archive_set_error(
277                         &a->archive, EINVAL,
278                         "Bad content length");
279                 return (ARCHIVE_FATAL);
280         }
281         rtime = _warc_rdrtm(buf, eoh - buf);
282         if (rtime == (time_t)-1) {
283                 /* record time is mandatory as per WARC/1.0,
284                  * so just barf here, fast and loud */
285                 archive_set_error(
286                         &a->archive, EINVAL,
287                         "Bad record time");
288                 return (ARCHIVE_FATAL);
289         }
290
291         /* let the world know we're a WARC archive */
292         a->archive.archive_format = ARCHIVE_FORMAT_WARC;
293         if (ver != w->pver) {
294                 /* stringify this entry's version */
295                 archive_string_sprintf(&w->sver,
296                         "WARC/%u.%u", ver / 10000, (ver % 10000) / 100);
297                 /* remember the version */
298                 w->pver = ver;
299         }
300         /* start off with the type */
301         ftyp = _warc_rdtyp(buf, eoh - buf);
302         /* and let future calls know about the content */
303         w->cntlen = cntlen;
304         w->cntoff = 0U;
305         mtime = 0;/* Avoid compiling error on some platform. */
306
307         switch (ftyp) {
308         case WT_RSRC:
309         case WT_RSP:
310                 /* only try and read the filename in the cases that are
311                  * guaranteed to have one */
312                 fnam = _warc_rduri(buf, eoh - buf);
313                 /* check the last character in the URI to avoid creating
314                  * directory endpoints as files, see Todo above */
315                 if (fnam.len == 0 || fnam.str[fnam.len - 1] == '/') {
316                         /* break here for now */
317                         fnam.len = 0U;
318                         fnam.str = NULL;
319                         break;
320                 }
321                 /* bang to our string pool, so we save a
322                  * malloc()+free() roundtrip */
323                 if (fnam.len + 1U > w->pool.len) {
324                         w->pool.len = ((fnam.len + 64U) / 64U) * 64U;
325                         w->pool.str = realloc(w->pool.str, w->pool.len);
326                 }
327                 memcpy(w->pool.str, fnam.str, fnam.len);
328                 w->pool.str[fnam.len] = '\0';
329                 /* let no one else know about the pool, it's a secret, shhh */
330                 fnam.str = w->pool.str;
331
332                 /* snarf mtime or deduce from rtime
333                  * this is a custom header added by our writer, it's quite
334                  * hard to believe anyone else would go through with it
335                  * (apart from being part of some http responses of course) */
336                 if ((mtime = _warc_rdmtm(buf, eoh - buf)) == (time_t)-1) {
337                         mtime = rtime;
338                 }
339                 break;
340         default:
341                 fnam.len = 0U;
342                 fnam.str = NULL;
343                 break;
344         }
345
346         /* now eat some of those delicious buffer bits */
347         __archive_read_consume(a, eoh - buf);
348
349         switch (ftyp) {
350         case WT_RSRC:
351         case WT_RSP:
352                 if (fnam.len > 0U) {
353                         /* populate entry object */
354                         archive_entry_set_filetype(entry, AE_IFREG);
355                         archive_entry_copy_pathname(entry, fnam.str);
356                         archive_entry_set_size(entry, cntlen);
357                         archive_entry_set_perm(entry, 0644);
358                         /* rtime is the new ctime, mtime stays mtime */
359                         archive_entry_set_ctime(entry, rtime, 0L);
360                         archive_entry_set_mtime(entry, mtime, 0L);
361                         break;
362                 }
363                 /* FALLTHROUGH */
364         default:
365                 /* consume the content and start over */
366                 _warc_skip(a);
367                 goto start_over;
368         }
369         return (ARCHIVE_OK);
370 }
371
372 static int
373 _warc_read(struct archive_read *a, const void **buf, size_t *bsz, int64_t *off)
374 {
375         struct warc_s *w = a->format->data;
376         const char *rab;
377         ssize_t nrd;
378
379         if (w->cntoff >= w->cntlen) {
380         eof:
381                 /* it's our lucky day, no work, we can leave early */
382                 *buf = NULL;
383                 *bsz = 0U;
384                 *off = w->cntoff + 4U/*for \r\n\r\n separator*/;
385                 w->unconsumed = 0U;
386                 return (ARCHIVE_EOF);
387         }
388
389         rab = __archive_read_ahead(a, 1U, &nrd);
390         if (nrd < 0) {
391                 *bsz = 0U;
392                 /* big catastrophe */
393                 return (int)nrd;
394         } else if (nrd == 0) {
395                 goto eof;
396         } else if ((size_t)nrd > w->cntlen - w->cntoff) {
397                 /* clamp to content-length */
398                 nrd = w->cntlen - w->cntoff;
399         }
400         *off = w->cntoff;
401         *bsz = nrd;
402         *buf = rab;
403
404         w->cntoff += nrd;
405         w->unconsumed = (size_t)nrd;
406         return (ARCHIVE_OK);
407 }
408
409 static int
410 _warc_skip(struct archive_read *a)
411 {
412         struct warc_s *w = a->format->data;
413
414         __archive_read_consume(a, w->cntlen + 4U/*\r\n\r\n separator*/);
415         w->cntlen = 0U;
416         w->cntoff = 0U;
417         return (ARCHIVE_OK);
418 }
419
420 \f
421 /* private routines */
422 static void*
423 deconst(const void *c)
424 {
425         return (char *)0x1 + (((const char *)c) - (const char *)0x1);
426 }
427
428 static char*
429 xmemmem(const char *hay, const size_t haysize,
430         const char *needle, const size_t needlesize)
431 {
432         const char *const eoh = hay + haysize;
433         const char *const eon = needle + needlesize;
434         const char *hp;
435         const char *np;
436         const char *cand;
437         unsigned int hsum;
438         unsigned int nsum;
439         unsigned int eqp;
440
441         /* trivial checks first
442          * a 0-sized needle is defined to be found anywhere in haystack
443          * then run strchr() to find a candidate in HAYSTACK (i.e. a portion
444          * that happens to begin with *NEEDLE) */
445         if (needlesize == 0UL) {
446                 return deconst(hay);
447         } else if ((hay = memchr(hay, *needle, haysize)) == NULL) {
448                 /* trivial */
449                 return NULL;
450         }
451
452         /* First characters of haystack and needle are the same now. Both are
453          * guaranteed to be at least one character long.  Now computes the sum
454          * of characters values of needle together with the sum of the first
455          * needle_len characters of haystack. */
456         for (hp = hay + 1U, np = needle + 1U, hsum = *hay, nsum = *hay, eqp = 1U;
457              hp < eoh && np < eon;
458              hsum ^= *hp, nsum ^= *np, eqp &= *hp == *np, hp++, np++);
459
460         /* HP now references the (NEEDLESIZE + 1)-th character. */
461         if (np < eon) {
462                 /* haystack is smaller than needle, :O */
463                 return NULL;
464         } else if (eqp) {
465                 /* found a match */
466                 return deconst(hay);
467         }
468
469         /* now loop through the rest of haystack,
470          * updating the sum iteratively */
471         for (cand = hay; hp < eoh; hp++) {
472                 hsum ^= *cand++;
473                 hsum ^= *hp;
474
475                 /* Since the sum of the characters is already known to be
476                  * equal at that point, it is enough to check just NEEDLESIZE - 1
477                  * characters for equality,
478                  * also CAND is by design < HP, so no need for range checks */
479                 if (hsum == nsum && memcmp(cand, needle, needlesize - 1U) == 0) {
480                         return deconst(cand);
481                 }
482         }
483         return NULL;
484 }
485
486 static int
487 strtoi_lim(const char *str, const char **ep, int llim, int ulim)
488 {
489         int res = 0;
490         const char *sp;
491         /* we keep track of the number of digits via rulim */
492         int rulim;
493
494         for (sp = str, rulim = ulim > 10 ? ulim : 10;
495              res * 10 <= ulim && rulim && *sp >= '0' && *sp <= '9';
496              sp++, rulim /= 10) {
497                 res *= 10;
498                 res += *sp - '0';
499         }
500         if (sp == str) {
501                 res = -1;
502         } else if (res < llim || res > ulim) {
503                 res = -2;
504         }
505         *ep = (const char*)sp;
506         return res;
507 }
508
509 static time_t
510 time_from_tm(struct tm *t)
511 {
512 #if HAVE_TIMEGM
513         /* Use platform timegm() if available. */
514         return (timegm(t));
515 #elif HAVE__MKGMTIME64
516         return (_mkgmtime64(t));
517 #else
518         /* Else use direct calculation using POSIX assumptions. */
519         /* First, fix up tm_yday based on the year/month/day. */
520         if (mktime(t) == (time_t)-1)
521                 return ((time_t)-1);
522         /* Then we can compute timegm() from first principles. */
523         return (t->tm_sec
524             + t->tm_min * 60
525             + t->tm_hour * 3600
526             + t->tm_yday * 86400
527             + (t->tm_year - 70) * 31536000
528             + ((t->tm_year - 69) / 4) * 86400
529             - ((t->tm_year - 1) / 100) * 86400
530             + ((t->tm_year + 299) / 400) * 86400);
531 #endif
532 }
533
534 static time_t
535 xstrpisotime(const char *s, char **endptr)
536 {
537 /** like strptime() but strictly for ISO 8601 Zulu strings */
538         struct tm tm;
539         time_t res = (time_t)-1;
540
541         /* make sure tm is clean */
542         memset(&tm, 0, sizeof(tm));
543
544         /* as a courtesy to our callers, and since this is a non-standard
545          * routine, we skip leading whitespace */
546         while (isblank((unsigned char)*s))
547                 ++s;
548
549         /* read year */
550         if ((tm.tm_year = strtoi_lim(s, &s, 1583, 4095)) < 0 || *s++ != '-') {
551                 goto out;
552         }
553         /* read month */
554         if ((tm.tm_mon = strtoi_lim(s, &s, 1, 12)) < 0 || *s++ != '-') {
555                 goto out;
556         }
557         /* read day-of-month */
558         if ((tm.tm_mday = strtoi_lim(s, &s, 1, 31)) < 0 || *s++ != 'T') {
559                 goto out;
560         }
561         /* read hour */
562         if ((tm.tm_hour = strtoi_lim(s, &s, 0, 23)) < 0 || *s++ != ':') {
563                 goto out;
564         }
565         /* read minute */
566         if ((tm.tm_min = strtoi_lim(s, &s, 0, 59)) < 0 || *s++ != ':') {
567                 goto out;
568         }
569         /* read second */
570         if ((tm.tm_sec = strtoi_lim(s, &s, 0, 60)) < 0 || *s++ != 'Z') {
571                 goto out;
572         }
573
574         /* massage TM to fulfill some of POSIX' constraints */
575         tm.tm_year -= 1900;
576         tm.tm_mon--;
577
578         /* now convert our custom tm struct to a unix stamp using UTC */
579         res = time_from_tm(&tm);
580
581 out:
582         if (endptr != NULL) {
583                 *endptr = deconst(s);
584         }
585         return res;
586 }
587
588 static unsigned int
589 _warc_rdver(const char *buf, size_t bsz)
590 {
591         static const char magic[] = "WARC/";
592         unsigned int ver = 0U;
593         unsigned int end = 0U;
594
595         if (bsz < 12 || memcmp(buf, magic, sizeof(magic) - 1U) != 0) {
596                 /* buffer too small or invalid magic */
597                 return ver;
598         }
599         /* looks good so far, read the version number for a laugh */
600         buf += sizeof(magic) - 1U;
601
602         if (isdigit(buf[0U]) && (buf[1U] == '.') && isdigit(buf[2U])) {
603                 /* we support a maximum of 2 digits in the minor version */
604                 if (isdigit(buf[3U]))
605                         end = 1U;
606                 /* set up major version */
607                 ver = (buf[0U] - '0') * 10000U;
608                 /* set up minor version */
609                 if (end == 1U) {
610                         ver += (buf[2U] - '0') * 1000U;
611                         ver += (buf[3U] - '0') * 100U;
612                 } else
613                         ver += (buf[2U] - '0') * 100U;
614                 /*
615                  * WARC below version 0.12 has a space-separated header
616                  * WARC 0.12 and above terminates the version with a CRLF
617                  */
618                 if (ver >= 1200U) {
619                         if (memcmp(buf + 3U + end, "\r\n", 2U) != 0)
620                                 ver = 0U;
621                 } else if (ver < 1200U) {
622                         if (!isblank(*(buf + 3U + end)))
623                                 ver = 0U;
624                 }
625         }
626         return ver;
627 }
628
629 static unsigned int
630 _warc_rdtyp(const char *buf, size_t bsz)
631 {
632         static const char _key[] = "\r\nWARC-Type:";
633         const char *val, *eol;
634
635         if ((val = xmemmem(buf, bsz, _key, sizeof(_key) - 1U)) == NULL) {
636                 /* no bother */
637                 return WT_NONE;
638         }
639         val += sizeof(_key) - 1U;
640         if ((eol = _warc_find_eol(val, buf + bsz - val)) == NULL) {
641                 /* no end of line */
642                 return WT_NONE;
643         }
644
645         /* overread whitespace */
646         while (val < eol && isblank((unsigned char)*val))
647                 ++val;
648
649         if (val + 8U == eol) {
650                 if (memcmp(val, "resource", 8U) == 0)
651                         return WT_RSRC;
652                 else if (memcmp(val, "response", 8U) == 0)
653                         return WT_RSP;
654         }
655         return WT_NONE;
656 }
657
658 static warc_string_t
659 _warc_rduri(const char *buf, size_t bsz)
660 {
661         static const char _key[] = "\r\nWARC-Target-URI:";
662         const char *val, *uri, *eol, *p;
663         warc_string_t res = {0U, NULL};
664
665         if ((val = xmemmem(buf, bsz, _key, sizeof(_key) - 1U)) == NULL) {
666                 /* no bother */
667                 return res;
668         }
669         /* overread whitespace */
670         val += sizeof(_key) - 1U;
671         if ((eol = _warc_find_eol(val, buf + bsz - val)) == NULL) {
672                 /* no end of line */
673                 return res;
674         }
675
676         while (val < eol && isblank((unsigned char)*val))
677                 ++val;
678
679         /* overread URL designators */
680         if ((uri = xmemmem(val, eol - val, "://", 3U)) == NULL) {
681                 /* not touching that! */
682                 return res;
683         }
684
685         /* spaces inside uri are not allowed, CRLF should follow */
686         for (p = val; p < eol; p++) {
687                 if (isspace(*p))
688                         return res;
689         }
690
691         /* there must be at least space for ftp */
692         if (uri < (val + 3U))
693                 return res;
694
695         /* move uri to point to after :// */
696         uri += 3U;
697
698         /* now then, inspect the URI */
699         if (memcmp(val, "file", 4U) == 0) {
700                 /* perfect, nothing left to do here */
701
702         } else if (memcmp(val, "http", 4U) == 0 ||
703                    memcmp(val, "ftp", 3U) == 0) {
704                 /* overread domain, and the first / */
705                 while (uri < eol && *uri++ != '/');
706         } else {
707                 /* not sure what to do? best to bugger off */
708                 return res;
709         }
710         res.str = uri;
711         res.len = eol - uri;
712         return res;
713 }
714
715 static ssize_t
716 _warc_rdlen(const char *buf, size_t bsz)
717 {
718         static const char _key[] = "\r\nContent-Length:";
719         const char *val, *eol;
720         char *on = NULL;
721         long int len;
722
723         if ((val = xmemmem(buf, bsz, _key, sizeof(_key) - 1U)) == NULL) {
724                 /* no bother */
725                 return -1;
726         }
727         val += sizeof(_key) - 1U;
728         if ((eol = _warc_find_eol(val, buf + bsz - val)) == NULL) {
729                 /* no end of line */
730                 return -1;
731         }
732
733         /* skip leading whitespace */
734         while (val < eol && isblank(*val))
735                 val++;
736         /* there must be at least one digit */
737         if (!isdigit(*val))
738                 return -1;
739         len = strtol(val, &on, 10);
740         if (on != eol) {
741                 /* line must end here */
742                 return -1;
743         }
744
745         return (size_t)len;
746 }
747
748 static time_t
749 _warc_rdrtm(const char *buf, size_t bsz)
750 {
751         static const char _key[] = "\r\nWARC-Date:";
752         const char *val, *eol;
753         char *on = NULL;
754         time_t res;
755
756         if ((val = xmemmem(buf, bsz, _key, sizeof(_key) - 1U)) == NULL) {
757                 /* no bother */
758                 return (time_t)-1;
759         }
760         val += sizeof(_key) - 1U;
761         if ((eol = _warc_find_eol(val, buf + bsz - val)) == NULL ) {
762                 /* no end of line */
763                 return -1;
764         }
765
766         /* xstrpisotime() kindly overreads whitespace for us, so use that */
767         res = xstrpisotime(val, &on);
768         if (on != eol) {
769                 /* line must end here */
770                 return -1;
771         }
772         return res;
773 }
774
775 static time_t
776 _warc_rdmtm(const char *buf, size_t bsz)
777 {
778         static const char _key[] = "\r\nLast-Modified:";
779         const char *val, *eol;
780         char *on = NULL;
781         time_t res;
782
783         if ((val = xmemmem(buf, bsz, _key, sizeof(_key) - 1U)) == NULL) {
784                 /* no bother */
785                 return (time_t)-1;
786         }
787         val += sizeof(_key) - 1U;
788         if ((eol = _warc_find_eol(val, buf + bsz - val)) == NULL ) {
789                 /* no end of line */
790                 return -1;
791         }
792
793         /* xstrpisotime() kindly overreads whitespace for us, so use that */
794         res = xstrpisotime(val, &on);
795         if (on != eol) {
796                 /* line must end here */
797                 return -1;
798         }
799         return res;
800 }
801
802 static const char*
803 _warc_find_eoh(const char *buf, size_t bsz)
804 {
805         static const char _marker[] = "\r\n\r\n";
806         const char *hit = xmemmem(buf, bsz, _marker, sizeof(_marker) - 1U);
807
808         if (hit != NULL) {
809                 hit += sizeof(_marker) - 1U;
810         }
811         return hit;
812 }
813
814 static const char*
815 _warc_find_eol(const char *buf, size_t bsz)
816 {
817         static const char _marker[] = "\r\n";
818         const char *hit = xmemmem(buf, bsz, _marker, sizeof(_marker) - 1U);
819
820         return hit;
821 }
822 /* archive_read_support_format_warc.c ends here */