archive_read_support_format_warc.c 20 KB

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