archive_read_support_format_warc.c revision 313570
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: stable/11/contrib/libarchive/libarchive/archive_read_support_format_warc.c 313570 2017-02-11 00:54:16Z mm $");
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
75typedef 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
97typedef struct {
98	size_t len;
99	const char *str;
100} warc_string_t;
101
102typedef struct {
103	size_t len;
104	char *str;
105} warc_strbuf_t;
106
107struct 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
123static int _warc_bid(struct archive_read *a, int);
124static int _warc_cleanup(struct archive_read *a);
125static int _warc_read(struct archive_read*, const void**, size_t*, int64_t*);
126static int _warc_skip(struct archive_read *a);
127static int _warc_rdhdr(struct archive_read *a, struct archive_entry *e);
128
129/* private routines */
130static unsigned int _warc_rdver(const char buf[10], size_t bsz);
131static unsigned int _warc_rdtyp(const char *buf, size_t bsz);
132static warc_string_t _warc_rduri(const char *buf, size_t bsz);
133static ssize_t _warc_rdlen(const char *buf, size_t bsz);
134static time_t _warc_rdrtm(const char *buf, size_t bsz);
135static time_t _warc_rdmtm(const char *buf, size_t bsz);
136static const char *_warc_find_eoh(const char *buf, size_t bsz);
137
138
139int
140archive_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
167static 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
181static 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 == 0U || ver > 10000U) {
202		/* oh oh oh, best not to wager ... */
203		return -1;
204	}
205
206	/* otherwise be confident */
207	return (64);
208}
209
210static 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
230start_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	} else if ((ver = _warc_rdver(buf, eoh - buf)) > 10000U) {
258		/* nawww, I wish they promised backward compatibility
259		 * anyhoo, in their infinite wisdom the 28500 guys might
260		 * come up with something we can't possibly handle so
261		 * best end things here */
262		archive_set_error(
263			&a->archive, ARCHIVE_ERRNO_MISC,
264			"Unsupported record version");
265		return (ARCHIVE_FATAL);
266	} else if ((cntlen = _warc_rdlen(buf, eoh - buf)) < 0) {
267		/* nightmare!  the specs say content-length is mandatory
268		 * so I don't feel overly bad stopping the reader here */
269		archive_set_error(
270			&a->archive, EINVAL,
271			"Bad content length");
272		return (ARCHIVE_FATAL);
273	} else if ((rtime = _warc_rdrtm(buf, eoh - buf)) == (time_t)-1) {
274		/* record time is mandatory as per WARC/1.0,
275		 * so just barf here, fast and loud */
276		archive_set_error(
277			&a->archive, EINVAL,
278			"Bad record time");
279		return (ARCHIVE_FATAL);
280	}
281
282	/* let the world know we're a WARC archive */
283	a->archive.archive_format = ARCHIVE_FORMAT_WARC;
284	if (ver != w->pver) {
285		/* stringify this entry's version */
286		archive_string_sprintf(&w->sver,
287			"WARC/%u.%u", ver / 10000, ver % 10000);
288		/* remember the version */
289		w->pver = ver;
290	}
291	/* start off with the type */
292	ftyp = _warc_rdtyp(buf, eoh - buf);
293	/* and let future calls know about the content */
294	w->cntlen = cntlen;
295	w->cntoff = 0U;
296	mtime = 0;/* Avoid compiling error on some platform. */
297
298	switch (ftyp) {
299	case WT_RSRC:
300	case WT_RSP:
301		/* only try and read the filename in the cases that are
302		 * guaranteed to have one */
303		fnam = _warc_rduri(buf, eoh - buf);
304		/* check the last character in the URI to avoid creating
305		 * directory endpoints as files, see Todo above */
306		if (fnam.len == 0 || fnam.str[fnam.len - 1] == '/') {
307			/* break here for now */
308			fnam.len = 0U;
309			fnam.str = NULL;
310			break;
311		}
312		/* bang to our string pool, so we save a
313		 * malloc()+free() roundtrip */
314		if (fnam.len + 1U > w->pool.len) {
315			w->pool.len = ((fnam.len + 64U) / 64U) * 64U;
316			w->pool.str = realloc(w->pool.str, w->pool.len);
317		}
318		memcpy(w->pool.str, fnam.str, fnam.len);
319		w->pool.str[fnam.len] = '\0';
320		/* let no one else know about the pool, it's a secret, shhh */
321		fnam.str = w->pool.str;
322
323		/* snarf mtime or deduce from rtime
324		 * this is a custom header added by our writer, it's quite
325		 * hard to believe anyone else would go through with it
326		 * (apart from being part of some http responses of course) */
327		if ((mtime = _warc_rdmtm(buf, eoh - buf)) == (time_t)-1) {
328			mtime = rtime;
329		}
330		break;
331	default:
332		fnam.len = 0U;
333		fnam.str = NULL;
334		break;
335	}
336
337	/* now eat some of those delicious buffer bits */
338	__archive_read_consume(a, eoh - buf);
339
340	switch (ftyp) {
341	case WT_RSRC:
342	case WT_RSP:
343		if (fnam.len > 0U) {
344			/* populate entry object */
345			archive_entry_set_filetype(entry, AE_IFREG);
346			archive_entry_copy_pathname(entry, fnam.str);
347			archive_entry_set_size(entry, cntlen);
348			archive_entry_set_perm(entry, 0644);
349			/* rtime is the new ctime, mtime stays mtime */
350			archive_entry_set_ctime(entry, rtime, 0L);
351			archive_entry_set_mtime(entry, mtime, 0L);
352			break;
353		}
354		/* FALLTHROUGH */
355	default:
356		/* consume the content and start over */
357		_warc_skip(a);
358		goto start_over;
359	}
360	return (ARCHIVE_OK);
361}
362
363static int
364_warc_read(struct archive_read *a, const void **buf, size_t *bsz, int64_t *off)
365{
366	struct warc_s *w = a->format->data;
367	const char *rab;
368	ssize_t nrd;
369
370	if (w->cntoff >= w->cntlen) {
371	eof:
372		/* it's our lucky day, no work, we can leave early */
373		*buf = NULL;
374		*bsz = 0U;
375		*off = w->cntoff + 4U/*for \r\n\r\n separator*/;
376		w->unconsumed = 0U;
377		return (ARCHIVE_EOF);
378	}
379
380	rab = __archive_read_ahead(a, 1U, &nrd);
381	if (nrd < 0) {
382		*bsz = 0U;
383		/* big catastrophe */
384		return (int)nrd;
385	} else if (nrd == 0) {
386		goto eof;
387	} else if ((size_t)nrd > w->cntlen - w->cntoff) {
388		/* clamp to content-length */
389		nrd = w->cntlen - w->cntoff;
390	}
391	*off = w->cntoff;
392	*bsz = nrd;
393	*buf = rab;
394
395	w->cntoff += nrd;
396	w->unconsumed = (size_t)nrd;
397	return (ARCHIVE_OK);
398}
399
400static int
401_warc_skip(struct archive_read *a)
402{
403	struct warc_s *w = a->format->data;
404
405	__archive_read_consume(a, w->cntlen + 4U/*\r\n\r\n separator*/);
406	w->cntlen = 0U;
407	w->cntoff = 0U;
408	return (ARCHIVE_OK);
409}
410
411
412/* private routines */
413static void*
414deconst(const void *c)
415{
416	return (char *)0x1 + (((const char *)c) - (const char *)0x1);
417}
418
419static char*
420xmemmem(const char *hay, const size_t haysize,
421	const char *needle, const size_t needlesize)
422{
423	const char *const eoh = hay + haysize;
424	const char *const eon = needle + needlesize;
425	const char *hp;
426	const char *np;
427	const char *cand;
428	unsigned int hsum;
429	unsigned int nsum;
430	unsigned int eqp;
431
432	/* trivial checks first
433         * a 0-sized needle is defined to be found anywhere in haystack
434         * then run strchr() to find a candidate in HAYSTACK (i.e. a portion
435         * that happens to begin with *NEEDLE) */
436	if (needlesize == 0UL) {
437		return deconst(hay);
438	} else if ((hay = memchr(hay, *needle, haysize)) == NULL) {
439		/* trivial */
440		return NULL;
441	}
442
443	/* First characters of haystack and needle are the same now. Both are
444	 * guaranteed to be at least one character long.  Now computes the sum
445	 * of characters values of needle together with the sum of the first
446	 * needle_len characters of haystack. */
447	for (hp = hay + 1U, np = needle + 1U, hsum = *hay, nsum = *hay, eqp = 1U;
448	     hp < eoh && np < eon;
449	     hsum ^= *hp, nsum ^= *np, eqp &= *hp == *np, hp++, np++);
450
451	/* HP now references the (NEEDLESIZE + 1)-th character. */
452	if (np < eon) {
453		/* haystack is smaller than needle, :O */
454		return NULL;
455	} else if (eqp) {
456		/* found a match */
457		return deconst(hay);
458	}
459
460	/* now loop through the rest of haystack,
461	 * updating the sum iteratively */
462	for (cand = hay; hp < eoh; hp++) {
463		hsum ^= *cand++;
464		hsum ^= *hp;
465
466		/* Since the sum of the characters is already known to be
467		 * equal at that point, it is enough to check just NEEDLESIZE - 1
468		 * characters for equality,
469		 * also CAND is by design < HP, so no need for range checks */
470		if (hsum == nsum && memcmp(cand, needle, needlesize - 1U) == 0) {
471			return deconst(cand);
472		}
473	}
474	return NULL;
475}
476
477static int
478strtoi_lim(const char *str, const char **ep, int llim, int ulim)
479{
480	int res = 0;
481	const char *sp;
482	/* we keep track of the number of digits via rulim */
483	int rulim;
484
485	for (sp = str, rulim = ulim > 10 ? ulim : 10;
486	     res * 10 <= ulim && rulim && *sp >= '0' && *sp <= '9';
487	     sp++, rulim /= 10) {
488		res *= 10;
489		res += *sp - '0';
490	}
491	if (sp == str) {
492		res = -1;
493	} else if (res < llim || res > ulim) {
494		res = -2;
495	}
496	*ep = (const char*)sp;
497	return res;
498}
499
500static time_t
501time_from_tm(struct tm *t)
502{
503#if HAVE_TIMEGM
504        /* Use platform timegm() if available. */
505        return (timegm(t));
506#elif HAVE__MKGMTIME64
507        return (_mkgmtime64(t));
508#else
509        /* Else use direct calculation using POSIX assumptions. */
510        /* First, fix up tm_yday based on the year/month/day. */
511        if (mktime(t) == (time_t)-1)
512                return ((time_t)-1);
513        /* Then we can compute timegm() from first principles. */
514        return (t->tm_sec
515            + t->tm_min * 60
516            + t->tm_hour * 3600
517            + t->tm_yday * 86400
518            + (t->tm_year - 70) * 31536000
519            + ((t->tm_year - 69) / 4) * 86400
520            - ((t->tm_year - 1) / 100) * 86400
521            + ((t->tm_year + 299) / 400) * 86400);
522#endif
523}
524
525static time_t
526xstrpisotime(const char *s, char **endptr)
527{
528/** like strptime() but strictly for ISO 8601 Zulu strings */
529	struct tm tm;
530	time_t res = (time_t)-1;
531
532	/* make sure tm is clean */
533	memset(&tm, 0, sizeof(tm));
534
535	/* as a courtesy to our callers, and since this is a non-standard
536	 * routine, we skip leading whitespace */
537	while (isblank((unsigned char)*s))
538		++s;
539
540	/* read year */
541	if ((tm.tm_year = strtoi_lim(s, &s, 1583, 4095)) < 0 || *s++ != '-') {
542		goto out;
543	}
544	/* read month */
545	if ((tm.tm_mon = strtoi_lim(s, &s, 1, 12)) < 0 || *s++ != '-') {
546		goto out;
547	}
548	/* read day-of-month */
549	if ((tm.tm_mday = strtoi_lim(s, &s, 1, 31)) < 0 || *s++ != 'T') {
550		goto out;
551	}
552	/* read hour */
553	if ((tm.tm_hour = strtoi_lim(s, &s, 0, 23)) < 0 || *s++ != ':') {
554		goto out;
555	}
556	/* read minute */
557	if ((tm.tm_min = strtoi_lim(s, &s, 0, 59)) < 0 || *s++ != ':') {
558		goto out;
559	}
560	/* read second */
561	if ((tm.tm_sec = strtoi_lim(s, &s, 0, 60)) < 0 || *s++ != 'Z') {
562		goto out;
563	}
564
565	/* massage TM to fulfill some of POSIX' constraints */
566	tm.tm_year -= 1900;
567	tm.tm_mon--;
568
569	/* now convert our custom tm struct to a unix stamp using UTC */
570	res = time_from_tm(&tm);
571
572out:
573	if (endptr != NULL) {
574		*endptr = deconst(s);
575	}
576	return res;
577}
578
579static unsigned int
580_warc_rdver(const char buf[10], size_t bsz)
581{
582	static const char magic[] = "WARC/";
583	unsigned int ver;
584
585	(void)bsz; /* UNUSED */
586
587	if (memcmp(buf, magic, sizeof(magic) - 1U) != 0) {
588		/* nope */
589		return 99999U;
590	}
591	/* looks good so far, read the version number for a laugh */
592	buf += sizeof(magic) - 1U;
593	/* most common case gets a quick-check here */
594	if (memcmp(buf, "1.0\r\n", 5U) == 0) {
595		ver = 10000U;
596	} else {
597		switch (*buf) {
598		case '0':
599		case '1':
600		case '2':
601		case '3':
602		case '4':
603		case '5':
604		case '6':
605		case '7':
606		case '8':
607			if (buf[1U] == '.') {
608				char *on;
609
610				/* set up major version */
611				ver = (buf[0U] - '0') * 10000U;
612				/* minor version, anyone? */
613				ver += (strtol(buf + 2U, &on, 10)) * 100U;
614				/* don't parse anything else */
615				if (on > buf + 2U) {
616					break;
617				}
618			}
619			/* FALLTHROUGH */
620		case '9':
621		default:
622			/* just make the version ridiculously high */
623			ver = 999999U;
624			break;
625		}
626	}
627	return ver;
628}
629
630static unsigned int
631_warc_rdtyp(const char *buf, size_t bsz)
632{
633	static const char _key[] = "\r\nWARC-Type:";
634	const char *const eob = buf + bsz;
635	const char *val;
636
637	if ((val = xmemmem(buf, bsz, _key, sizeof(_key) - 1U)) == NULL) {
638		/* no bother */
639		return WT_NONE;
640	}
641	/* overread whitespace */
642	val += sizeof(_key) - 1U;
643	while (val < eob && isspace((unsigned char)*val))
644		++val;
645
646	if (val + 8U > eob) {
647		;
648	} else if (memcmp(val, "resource", 8U) == 0) {
649		return WT_RSRC;
650	} else if (memcmp(val, "warcinfo", 8U) == 0) {
651		return WT_INFO;
652	} else if (memcmp(val, "metadata", 8U) == 0) {
653		return WT_META;
654	} else if (memcmp(val, "request", 7U) == 0) {
655		return WT_REQ;
656	} else if (memcmp(val, "response", 8U) == 0) {
657		return WT_RSP;
658	} else if (memcmp(val, "conversi", 8U) == 0) {
659		return WT_CONV;
660	} else if (memcmp(val, "continua", 8U) == 0) {
661		return WT_CONT;
662	}
663	return WT_NONE;
664}
665
666static warc_string_t
667_warc_rduri(const char *buf, size_t bsz)
668{
669	static const char _key[] = "\r\nWARC-Target-URI:";
670	const char *const eob = buf + bsz;
671	const char *val;
672	const char *uri;
673	const char *eol;
674	warc_string_t res = {0U, NULL};
675
676	if ((val = xmemmem(buf, bsz, _key, sizeof(_key) - 1U)) == NULL) {
677		/* no bother */
678		return res;
679	}
680	/* overread whitespace */
681	val += sizeof(_key) - 1U;
682	while (val < eob && isspace((unsigned char)*val))
683		++val;
684
685	/* overread URL designators */
686	if ((uri = xmemmem(val, eob - val, "://", 3U)) == NULL) {
687		/* not touching that! */
688		return res;
689	} else if ((eol = memchr(uri, '\n', eob - uri)) == NULL) {
690		/* no end of line? :O */
691		return res;
692	}
693
694	/* massage uri to point to after :// */
695	uri += 3U;
696	/* also massage eol to point to the first whitespace
697	 * after the last non-whitespace character before
698	 * the end of the line */
699	while (eol > uri && isspace((unsigned char)eol[-1]))
700		--eol;
701
702	/* now then, inspect the URI */
703	if (memcmp(val, "file", 4U) == 0) {
704		/* perfect, nothing left to do here */
705
706	} else if (memcmp(val, "http", 4U) == 0 ||
707		   memcmp(val, "ftp", 3U) == 0) {
708		/* overread domain, and the first / */
709		while (uri < eol && *uri++ != '/');
710	} else {
711		/* not sure what to do? best to bugger off */
712		return res;
713	}
714	res.str = uri;
715	res.len = eol - uri;
716	return res;
717}
718
719static ssize_t
720_warc_rdlen(const char *buf, size_t bsz)
721{
722	static const char _key[] = "\r\nContent-Length:";
723	const char *val;
724	char *on = NULL;
725	long int len;
726
727	if ((val = xmemmem(buf, bsz, _key, sizeof(_key) - 1U)) == NULL) {
728		/* no bother */
729		return -1;
730	}
731
732	/* strtol kindly overreads whitespace for us, so use that */
733	val += sizeof(_key) - 1U;
734	len = strtol(val, &on, 10);
735	if (on == NULL || !isspace((unsigned char)*on)) {
736		/* hm, can we trust that number?  Best not. */
737		return -1;
738	}
739	return (size_t)len;
740}
741
742static time_t
743_warc_rdrtm(const char *buf, size_t bsz)
744{
745	static const char _key[] = "\r\nWARC-Date:";
746	const char *val;
747	char *on = NULL;
748	time_t res;
749
750	if ((val = xmemmem(buf, bsz, _key, sizeof(_key) - 1U)) == NULL) {
751		/* no bother */
752		return (time_t)-1;
753	}
754
755	/* xstrpisotime() kindly overreads whitespace for us, so use that */
756	val += sizeof(_key) - 1U;
757	res = xstrpisotime(val, &on);
758	if (on == NULL || !isspace((unsigned char)*on)) {
759		/* hm, can we trust that number?  Best not. */
760		return (time_t)-1;
761	}
762	return res;
763}
764
765static time_t
766_warc_rdmtm(const char *buf, size_t bsz)
767{
768	static const char _key[] = "\r\nLast-Modified:";
769	const char *val;
770	char *on = NULL;
771	time_t res;
772
773	if ((val = xmemmem(buf, bsz, _key, sizeof(_key) - 1U)) == NULL) {
774		/* no bother */
775		return (time_t)-1;
776	}
777
778	/* xstrpisotime() kindly overreads whitespace for us, so use that */
779	val += sizeof(_key) - 1U;
780	res = xstrpisotime(val, &on);
781	if (on == NULL || !isspace((unsigned char)*on)) {
782		/* hm, can we trust that number?  Best not. */
783		return (time_t)-1;
784	}
785	return res;
786}
787
788static const char*
789_warc_find_eoh(const char *buf, size_t bsz)
790{
791	static const char _marker[] = "\r\n\r\n";
792	const char *hit = xmemmem(buf, bsz, _marker, sizeof(_marker) - 1U);
793
794	if (hit != NULL) {
795		hit += sizeof(_marker) - 1U;
796	}
797	return hit;
798}
799
800/* archive_read_support_format_warc.c ends here */
801