archive_read.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749
  1. /*-
  2. * Copyright (c) 2003-2011 Tim Kientzle
  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. * This file contains the "essential" portions of the read API, that
  27. * is, stuff that will probably always be used by any client that
  28. * actually needs to read an archive. Optional pieces have been, as
  29. * far as possible, separated out into separate files to avoid
  30. * needlessly bloating statically-linked clients.
  31. */
  32. #include "archive_platform.h"
  33. #ifdef HAVE_ERRNO_H
  34. #include <errno.h>
  35. #endif
  36. #include <stdio.h>
  37. #ifdef HAVE_STDLIB_H
  38. #include <stdlib.h>
  39. #endif
  40. #ifdef HAVE_STRING_H
  41. #include <string.h>
  42. #endif
  43. #ifdef HAVE_UNISTD_H
  44. #include <unistd.h>
  45. #endif
  46. #include "archive.h"
  47. #include "archive_entry.h"
  48. #include "archive_private.h"
  49. #include "archive_read_private.h"
  50. #define minimum(a, b) (a < b ? a : b)
  51. static int choose_filters(struct archive_read *);
  52. static int choose_format(struct archive_read *);
  53. static int close_filters(struct archive_read *);
  54. static int64_t _archive_filter_bytes(struct archive *, int);
  55. static int _archive_filter_code(struct archive *, int);
  56. static const char *_archive_filter_name(struct archive *, int);
  57. static int _archive_filter_count(struct archive *);
  58. static int _archive_read_close(struct archive *);
  59. static int _archive_read_data_block(struct archive *,
  60. const void **, size_t *, int64_t *);
  61. static int _archive_read_free(struct archive *);
  62. static int _archive_read_next_header(struct archive *,
  63. struct archive_entry **);
  64. static int _archive_read_next_header2(struct archive *,
  65. struct archive_entry *);
  66. static int64_t advance_file_pointer(struct archive_read_filter *, int64_t);
  67. static const struct archive_vtable
  68. archive_read_vtable = {
  69. .archive_filter_bytes = _archive_filter_bytes,
  70. .archive_filter_code = _archive_filter_code,
  71. .archive_filter_name = _archive_filter_name,
  72. .archive_filter_count = _archive_filter_count,
  73. .archive_read_data_block = _archive_read_data_block,
  74. .archive_read_next_header = _archive_read_next_header,
  75. .archive_read_next_header2 = _archive_read_next_header2,
  76. .archive_free = _archive_read_free,
  77. .archive_close = _archive_read_close,
  78. };
  79. /*
  80. * Allocate, initialize and return a struct archive object.
  81. */
  82. struct archive *
  83. archive_read_new(void)
  84. {
  85. struct archive_read *a;
  86. a = calloc(1, sizeof(*a));
  87. if (a == NULL)
  88. return (NULL);
  89. a->archive.magic = ARCHIVE_READ_MAGIC;
  90. a->archive.state = ARCHIVE_STATE_NEW;
  91. a->entry = archive_entry_new2(&a->archive);
  92. a->archive.vtable = &archive_read_vtable;
  93. a->passphrases.last = &a->passphrases.first;
  94. return (&a->archive);
  95. }
  96. /*
  97. * Record the do-not-extract-to file. This belongs in archive_read_extract.c.
  98. */
  99. void
  100. archive_read_extract_set_skip_file(struct archive *_a, la_int64_t d,
  101. la_int64_t i)
  102. {
  103. struct archive_read *a = (struct archive_read *)_a;
  104. if (ARCHIVE_OK != __archive_check_magic(_a, ARCHIVE_READ_MAGIC,
  105. ARCHIVE_STATE_ANY, "archive_read_extract_set_skip_file"))
  106. return;
  107. a->skip_file_set = 1;
  108. a->skip_file_dev = d;
  109. a->skip_file_ino = i;
  110. }
  111. /*
  112. * Open the archive
  113. */
  114. int
  115. archive_read_open(struct archive *a, void *client_data,
  116. archive_open_callback *client_opener, archive_read_callback *client_reader,
  117. archive_close_callback *client_closer)
  118. {
  119. /* Old archive_read_open() is just a thin shell around
  120. * archive_read_open1. */
  121. archive_read_set_open_callback(a, client_opener);
  122. archive_read_set_read_callback(a, client_reader);
  123. archive_read_set_close_callback(a, client_closer);
  124. archive_read_set_callback_data(a, client_data);
  125. return archive_read_open1(a);
  126. }
  127. int
  128. archive_read_open2(struct archive *a, void *client_data,
  129. archive_open_callback *client_opener,
  130. archive_read_callback *client_reader,
  131. archive_skip_callback *client_skipper,
  132. archive_close_callback *client_closer)
  133. {
  134. /* Old archive_read_open2() is just a thin shell around
  135. * archive_read_open1. */
  136. archive_read_set_callback_data(a, client_data);
  137. archive_read_set_open_callback(a, client_opener);
  138. archive_read_set_read_callback(a, client_reader);
  139. archive_read_set_skip_callback(a, client_skipper);
  140. archive_read_set_close_callback(a, client_closer);
  141. return archive_read_open1(a);
  142. }
  143. static ssize_t
  144. client_read_proxy(struct archive_read_filter *self, const void **buff)
  145. {
  146. ssize_t r;
  147. r = (self->archive->client.reader)(&self->archive->archive,
  148. self->data, buff);
  149. return (r);
  150. }
  151. static int64_t
  152. client_skip_proxy(struct archive_read_filter *self, int64_t request)
  153. {
  154. if (request < 0)
  155. __archive_errx(1, "Negative skip requested.");
  156. if (request == 0)
  157. return 0;
  158. if (self->archive->client.skipper != NULL) {
  159. int64_t total = 0;
  160. for (;;) {
  161. int64_t get, ask = request;
  162. get = (self->archive->client.skipper)
  163. (&self->archive->archive, self->data, ask);
  164. total += get;
  165. if (get == 0 || get == request)
  166. return (total);
  167. if (get > request)
  168. return ARCHIVE_FATAL;
  169. request -= get;
  170. }
  171. } else if (self->archive->client.seeker != NULL
  172. && request > 64 * 1024) {
  173. /* If the client provided a seeker but not a skipper,
  174. * we can use the seeker to skip forward.
  175. *
  176. * Note: This isn't always a good idea. The client
  177. * skipper is allowed to skip by less than requested
  178. * if it needs to maintain block alignment. The
  179. * seeker is not allowed to play such games, so using
  180. * the seeker here may be a performance loss compared
  181. * to just reading and discarding. That's why we
  182. * only do this for skips of over 64k.
  183. */
  184. int64_t before = self->position;
  185. int64_t after = (self->archive->client.seeker)
  186. (&self->archive->archive, self->data, request, SEEK_CUR);
  187. if (after != before + request)
  188. return ARCHIVE_FATAL;
  189. return after - before;
  190. }
  191. return 0;
  192. }
  193. static int64_t
  194. client_seek_proxy(struct archive_read_filter *self, int64_t offset, int whence)
  195. {
  196. /* DO NOT use the skipper here! If we transparently handled
  197. * forward seek here by using the skipper, that will break
  198. * other libarchive code that assumes a successful forward
  199. * seek means it can also seek backwards.
  200. */
  201. if (self->archive->client.seeker == NULL) {
  202. archive_set_error(&self->archive->archive, ARCHIVE_ERRNO_MISC,
  203. "Current client reader does not support seeking a device");
  204. return (ARCHIVE_FAILED);
  205. }
  206. return (self->archive->client.seeker)(&self->archive->archive,
  207. self->data, offset, whence);
  208. }
  209. static int
  210. read_client_close_proxy(struct archive_read *a)
  211. {
  212. int r = ARCHIVE_OK, r2;
  213. unsigned int i;
  214. if (a->client.closer == NULL)
  215. return (r);
  216. for (i = 0; i < a->client.nodes; i++)
  217. {
  218. r2 = (a->client.closer)
  219. ((struct archive *)a, a->client.dataset[i].data);
  220. if (r > r2)
  221. r = r2;
  222. }
  223. return (r);
  224. }
  225. static int
  226. client_close_proxy(struct archive_read_filter *self)
  227. {
  228. return read_client_close_proxy(self->archive);
  229. }
  230. static int
  231. client_open_proxy(struct archive_read_filter *self)
  232. {
  233. int r = ARCHIVE_OK;
  234. if (self->archive->client.opener != NULL)
  235. r = (self->archive->client.opener)(
  236. (struct archive *)self->archive, self->data);
  237. return (r);
  238. }
  239. static int
  240. client_switch_proxy(struct archive_read_filter *self, unsigned int iindex)
  241. {
  242. int r1 = ARCHIVE_OK, r2 = ARCHIVE_OK;
  243. void *data2 = NULL;
  244. /* Don't do anything if already in the specified data node */
  245. if (self->archive->client.cursor == iindex)
  246. return (ARCHIVE_OK);
  247. self->archive->client.cursor = iindex;
  248. data2 = self->archive->client.dataset[self->archive->client.cursor].data;
  249. if (self->archive->client.switcher != NULL)
  250. {
  251. r1 = r2 = (self->archive->client.switcher)
  252. ((struct archive *)self->archive, self->data, data2);
  253. self->data = data2;
  254. }
  255. else
  256. {
  257. /* Attempt to call close and open instead */
  258. if (self->archive->client.closer != NULL)
  259. r1 = (self->archive->client.closer)
  260. ((struct archive *)self->archive, self->data);
  261. self->data = data2;
  262. r2 = client_open_proxy(self);
  263. }
  264. return (r1 < r2) ? r1 : r2;
  265. }
  266. int
  267. archive_read_set_open_callback(struct archive *_a,
  268. archive_open_callback *client_opener)
  269. {
  270. struct archive_read *a = (struct archive_read *)_a;
  271. archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
  272. "archive_read_set_open_callback");
  273. a->client.opener = client_opener;
  274. return ARCHIVE_OK;
  275. }
  276. int
  277. archive_read_set_read_callback(struct archive *_a,
  278. archive_read_callback *client_reader)
  279. {
  280. struct archive_read *a = (struct archive_read *)_a;
  281. archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
  282. "archive_read_set_read_callback");
  283. a->client.reader = client_reader;
  284. return ARCHIVE_OK;
  285. }
  286. int
  287. archive_read_set_skip_callback(struct archive *_a,
  288. archive_skip_callback *client_skipper)
  289. {
  290. struct archive_read *a = (struct archive_read *)_a;
  291. archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
  292. "archive_read_set_skip_callback");
  293. a->client.skipper = client_skipper;
  294. return ARCHIVE_OK;
  295. }
  296. int
  297. archive_read_set_seek_callback(struct archive *_a,
  298. archive_seek_callback *client_seeker)
  299. {
  300. struct archive_read *a = (struct archive_read *)_a;
  301. archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
  302. "archive_read_set_seek_callback");
  303. a->client.seeker = client_seeker;
  304. return ARCHIVE_OK;
  305. }
  306. int
  307. archive_read_set_close_callback(struct archive *_a,
  308. archive_close_callback *client_closer)
  309. {
  310. struct archive_read *a = (struct archive_read *)_a;
  311. archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
  312. "archive_read_set_close_callback");
  313. a->client.closer = client_closer;
  314. return ARCHIVE_OK;
  315. }
  316. int
  317. archive_read_set_switch_callback(struct archive *_a,
  318. archive_switch_callback *client_switcher)
  319. {
  320. struct archive_read *a = (struct archive_read *)_a;
  321. archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
  322. "archive_read_set_switch_callback");
  323. a->client.switcher = client_switcher;
  324. return ARCHIVE_OK;
  325. }
  326. int
  327. archive_read_set_callback_data(struct archive *_a, void *client_data)
  328. {
  329. return archive_read_set_callback_data2(_a, client_data, 0);
  330. }
  331. int
  332. archive_read_set_callback_data2(struct archive *_a, void *client_data,
  333. unsigned int iindex)
  334. {
  335. struct archive_read *a = (struct archive_read *)_a;
  336. archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
  337. "archive_read_set_callback_data2");
  338. if (a->client.nodes == 0)
  339. {
  340. a->client.dataset = (struct archive_read_data_node *)
  341. calloc(1, sizeof(*a->client.dataset));
  342. if (a->client.dataset == NULL)
  343. {
  344. archive_set_error(&a->archive, ENOMEM,
  345. "No memory.");
  346. return ARCHIVE_FATAL;
  347. }
  348. a->client.nodes = 1;
  349. }
  350. if (iindex > a->client.nodes - 1)
  351. {
  352. archive_set_error(&a->archive, EINVAL,
  353. "Invalid index specified.");
  354. return ARCHIVE_FATAL;
  355. }
  356. a->client.dataset[iindex].data = client_data;
  357. a->client.dataset[iindex].begin_position = -1;
  358. a->client.dataset[iindex].total_size = -1;
  359. return ARCHIVE_OK;
  360. }
  361. int
  362. archive_read_add_callback_data(struct archive *_a, void *client_data,
  363. unsigned int iindex)
  364. {
  365. struct archive_read *a = (struct archive_read *)_a;
  366. void *p;
  367. unsigned int i;
  368. archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
  369. "archive_read_add_callback_data");
  370. if (iindex > a->client.nodes) {
  371. archive_set_error(&a->archive, EINVAL,
  372. "Invalid index specified.");
  373. return ARCHIVE_FATAL;
  374. }
  375. p = realloc(a->client.dataset, sizeof(*a->client.dataset)
  376. * (++(a->client.nodes)));
  377. if (p == NULL) {
  378. archive_set_error(&a->archive, ENOMEM,
  379. "No memory.");
  380. return ARCHIVE_FATAL;
  381. }
  382. a->client.dataset = (struct archive_read_data_node *)p;
  383. for (i = a->client.nodes - 1; i > iindex; i--) {
  384. a->client.dataset[i].data = a->client.dataset[i-1].data;
  385. a->client.dataset[i].begin_position = -1;
  386. a->client.dataset[i].total_size = -1;
  387. }
  388. a->client.dataset[iindex].data = client_data;
  389. a->client.dataset[iindex].begin_position = -1;
  390. a->client.dataset[iindex].total_size = -1;
  391. return ARCHIVE_OK;
  392. }
  393. int
  394. archive_read_append_callback_data(struct archive *_a, void *client_data)
  395. {
  396. struct archive_read *a = (struct archive_read *)_a;
  397. return archive_read_add_callback_data(_a, client_data, a->client.nodes);
  398. }
  399. int
  400. archive_read_prepend_callback_data(struct archive *_a, void *client_data)
  401. {
  402. return archive_read_add_callback_data(_a, client_data, 0);
  403. }
  404. static const struct archive_read_filter_vtable
  405. none_reader_vtable = {
  406. .read = client_read_proxy,
  407. .close = client_close_proxy,
  408. };
  409. int
  410. archive_read_open1(struct archive *_a)
  411. {
  412. struct archive_read *a = (struct archive_read *)_a;
  413. struct archive_read_filter *filter, *tmp;
  414. int slot, e = ARCHIVE_OK;
  415. archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
  416. "archive_read_open");
  417. archive_clear_error(&a->archive);
  418. if (a->client.reader == NULL) {
  419. archive_set_error(&a->archive, EINVAL,
  420. "No reader function provided to archive_read_open");
  421. a->archive.state = ARCHIVE_STATE_FATAL;
  422. return (ARCHIVE_FATAL);
  423. }
  424. /* Open data source. */
  425. if (a->client.opener != NULL) {
  426. e = (a->client.opener)(&a->archive, a->client.dataset[0].data);
  427. if (e != 0) {
  428. /* If the open failed, call the closer to clean up. */
  429. read_client_close_proxy(a);
  430. return (e);
  431. }
  432. }
  433. filter = calloc(1, sizeof(*filter));
  434. if (filter == NULL)
  435. return (ARCHIVE_FATAL);
  436. filter->bidder = NULL;
  437. filter->upstream = NULL;
  438. filter->archive = a;
  439. filter->data = a->client.dataset[0].data;
  440. filter->vtable = &none_reader_vtable;
  441. filter->name = "none";
  442. filter->code = ARCHIVE_FILTER_NONE;
  443. filter->can_skip = 1;
  444. filter->can_seek = 1;
  445. a->client.dataset[0].begin_position = 0;
  446. if (!a->filter || !a->bypass_filter_bidding)
  447. {
  448. a->filter = filter;
  449. /* Build out the input pipeline. */
  450. e = choose_filters(a);
  451. if (e < ARCHIVE_WARN) {
  452. a->archive.state = ARCHIVE_STATE_FATAL;
  453. return (ARCHIVE_FATAL);
  454. }
  455. }
  456. else
  457. {
  458. /* Need to add "NONE" type filter at the end of the filter chain */
  459. tmp = a->filter;
  460. while (tmp->upstream)
  461. tmp = tmp->upstream;
  462. tmp->upstream = filter;
  463. }
  464. if (!a->format)
  465. {
  466. slot = choose_format(a);
  467. if (slot < 0) {
  468. close_filters(a);
  469. a->archive.state = ARCHIVE_STATE_FATAL;
  470. return (ARCHIVE_FATAL);
  471. }
  472. a->format = &(a->formats[slot]);
  473. }
  474. a->archive.state = ARCHIVE_STATE_HEADER;
  475. /* Ensure libarchive starts from the first node in a multivolume set */
  476. client_switch_proxy(a->filter, 0);
  477. return (e);
  478. }
  479. /*
  480. * Allow each registered stream transform to bid on whether
  481. * it wants to handle this stream. Repeat until we've finished
  482. * building the pipeline.
  483. */
  484. /* We won't build a filter pipeline with more stages than this. */
  485. #define MAX_NUMBER_FILTERS 25
  486. static int
  487. choose_filters(struct archive_read *a)
  488. {
  489. int number_bidders, i, bid, best_bid, number_filters;
  490. struct archive_read_filter_bidder *bidder, *best_bidder;
  491. struct archive_read_filter *filter;
  492. ssize_t avail;
  493. int r;
  494. for (number_filters = 0; number_filters < MAX_NUMBER_FILTERS; ++number_filters) {
  495. number_bidders = sizeof(a->bidders) / sizeof(a->bidders[0]);
  496. best_bid = 0;
  497. best_bidder = NULL;
  498. bidder = a->bidders;
  499. for (i = 0; i < number_bidders; i++, bidder++) {
  500. if (bidder->vtable == NULL)
  501. continue;
  502. bid = (bidder->vtable->bid)(bidder, a->filter);
  503. if (bid > best_bid) {
  504. best_bid = bid;
  505. best_bidder = bidder;
  506. }
  507. }
  508. /* If no bidder, we're done. */
  509. if (best_bidder == NULL) {
  510. /* Verify the filter by asking it for some data. */
  511. __archive_read_filter_ahead(a->filter, 1, &avail);
  512. if (avail < 0) {
  513. __archive_read_free_filters(a);
  514. return (ARCHIVE_FATAL);
  515. }
  516. return (ARCHIVE_OK);
  517. }
  518. filter
  519. = calloc(1, sizeof(*filter));
  520. if (filter == NULL)
  521. return (ARCHIVE_FATAL);
  522. filter->bidder = best_bidder;
  523. filter->archive = a;
  524. filter->upstream = a->filter;
  525. a->filter = filter;
  526. r = (best_bidder->vtable->init)(a->filter);
  527. if (r != ARCHIVE_OK) {
  528. __archive_read_free_filters(a);
  529. return (ARCHIVE_FATAL);
  530. }
  531. }
  532. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  533. "Input requires too many filters for decoding");
  534. return (ARCHIVE_FATAL);
  535. }
  536. int
  537. __archive_read_header(struct archive_read *a, struct archive_entry *entry)
  538. {
  539. if (!a->filter->vtable->read_header)
  540. return (ARCHIVE_OK);
  541. return a->filter->vtable->read_header(a->filter, entry);
  542. }
  543. /*
  544. * Read header of next entry.
  545. */
  546. static int
  547. _archive_read_next_header2(struct archive *_a, struct archive_entry *entry)
  548. {
  549. struct archive_read *a = (struct archive_read *)_a;
  550. int r1 = ARCHIVE_OK, r2;
  551. archive_check_magic(_a, ARCHIVE_READ_MAGIC,
  552. ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
  553. "archive_read_next_header");
  554. archive_entry_clear(entry);
  555. archive_clear_error(&a->archive);
  556. /*
  557. * If client didn't consume entire data, skip any remainder
  558. * (This is especially important for GNU incremental directories.)
  559. */
  560. if (a->archive.state == ARCHIVE_STATE_DATA) {
  561. r1 = archive_read_data_skip(&a->archive);
  562. if (r1 == ARCHIVE_EOF)
  563. archive_set_error(&a->archive, EIO,
  564. "Premature end-of-file.");
  565. if (r1 == ARCHIVE_EOF || r1 == ARCHIVE_FATAL) {
  566. a->archive.state = ARCHIVE_STATE_FATAL;
  567. return (ARCHIVE_FATAL);
  568. }
  569. }
  570. /* Record start-of-header offset in uncompressed stream. */
  571. a->header_position = a->filter->position;
  572. ++_a->file_count;
  573. r2 = (a->format->read_header)(a, entry);
  574. /*
  575. * EOF and FATAL are persistent at this layer. By
  576. * modifying the state, we guarantee that future calls to
  577. * read a header or read data will fail.
  578. */
  579. switch (r2) {
  580. case ARCHIVE_EOF:
  581. a->archive.state = ARCHIVE_STATE_EOF;
  582. --_a->file_count;/* Revert a file counter. */
  583. break;
  584. case ARCHIVE_OK:
  585. a->archive.state = ARCHIVE_STATE_DATA;
  586. break;
  587. case ARCHIVE_WARN:
  588. a->archive.state = ARCHIVE_STATE_DATA;
  589. break;
  590. case ARCHIVE_RETRY:
  591. break;
  592. case ARCHIVE_FATAL:
  593. a->archive.state = ARCHIVE_STATE_FATAL;
  594. break;
  595. }
  596. __archive_reset_read_data(&a->archive);
  597. a->data_start_node = a->client.cursor;
  598. /* EOF always wins; otherwise return the worst error. */
  599. return (r2 < r1 || r2 == ARCHIVE_EOF) ? r2 : r1;
  600. }
  601. static int
  602. _archive_read_next_header(struct archive *_a, struct archive_entry **entryp)
  603. {
  604. int ret;
  605. struct archive_read *a = (struct archive_read *)_a;
  606. *entryp = NULL;
  607. ret = _archive_read_next_header2(_a, a->entry);
  608. *entryp = a->entry;
  609. return ret;
  610. }
  611. /*
  612. * Allow each registered format to bid on whether it wants to handle
  613. * the next entry. Return index of winning bidder.
  614. */
  615. static int
  616. choose_format(struct archive_read *a)
  617. {
  618. int slots;
  619. int i;
  620. int bid, best_bid;
  621. int best_bid_slot;
  622. slots = sizeof(a->formats) / sizeof(a->formats[0]);
  623. best_bid = -1;
  624. best_bid_slot = -1;
  625. /* Set up a->format for convenience of bidders. */
  626. a->format = &(a->formats[0]);
  627. for (i = 0; i < slots; i++, a->format++) {
  628. if (a->format->bid) {
  629. bid = (a->format->bid)(a, best_bid);
  630. if (bid == ARCHIVE_FATAL)
  631. return (ARCHIVE_FATAL);
  632. if (a->filter->position != 0)
  633. __archive_read_seek(a, 0, SEEK_SET);
  634. if ((bid > best_bid) || (best_bid_slot < 0)) {
  635. best_bid = bid;
  636. best_bid_slot = i;
  637. }
  638. }
  639. }
  640. /*
  641. * There were no bidders; this is a serious programmer error
  642. * and demands a quick and definitive abort.
  643. */
  644. if (best_bid_slot < 0) {
  645. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  646. "No formats registered");
  647. return (ARCHIVE_FATAL);
  648. }
  649. /*
  650. * There were bidders, but no non-zero bids; this means we
  651. * can't support this stream.
  652. */
  653. if (best_bid < 1) {
  654. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  655. "Unrecognized archive format");
  656. return (ARCHIVE_FATAL);
  657. }
  658. return (best_bid_slot);
  659. }
  660. /*
  661. * Return the file offset (within the uncompressed data stream) where
  662. * the last header started.
  663. */
  664. la_int64_t
  665. archive_read_header_position(struct archive *_a)
  666. {
  667. struct archive_read *a = (struct archive_read *)_a;
  668. archive_check_magic(_a, ARCHIVE_READ_MAGIC,
  669. ARCHIVE_STATE_ANY, "archive_read_header_position");
  670. return (a->header_position);
  671. }
  672. /*
  673. * Returns 1 if the archive contains at least one encrypted entry.
  674. * If the archive format not support encryption at all
  675. * ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED is returned.
  676. * If for any other reason (e.g. not enough data read so far)
  677. * we cannot say whether there are encrypted entries, then
  678. * ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW is returned.
  679. * In general, this function will return values below zero when the
  680. * reader is uncertain or totally incapable of encryption support.
  681. * When this function returns 0 you can be sure that the reader
  682. * supports encryption detection but no encrypted entries have
  683. * been found yet.
  684. *
  685. * NOTE: If the metadata/header of an archive is also encrypted, you
  686. * cannot rely on the number of encrypted entries. That is why this
  687. * function does not return the number of encrypted entries but#
  688. * just shows that there are some.
  689. */
  690. int
  691. archive_read_has_encrypted_entries(struct archive *_a)
  692. {
  693. struct archive_read *a = (struct archive_read *)_a;
  694. int format_supports_encryption = archive_read_format_capabilities(_a)
  695. & (ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_DATA | ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_METADATA);
  696. if (!_a || !format_supports_encryption) {
  697. /* Format in general doesn't support encryption */
  698. return ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED;
  699. }
  700. /* A reader potentially has read enough data now. */
  701. if (a->format && a->format->has_encrypted_entries) {
  702. return (a->format->has_encrypted_entries)(a);
  703. }
  704. /* For any other reason we cannot say how many entries are there. */
  705. return ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW;
  706. }
  707. /*
  708. * Returns a bitmask of capabilities that are supported by the archive format reader.
  709. * If the reader has no special capabilities, ARCHIVE_READ_FORMAT_CAPS_NONE is returned.
  710. */
  711. int
  712. archive_read_format_capabilities(struct archive *_a)
  713. {
  714. struct archive_read *a = (struct archive_read *)_a;
  715. if (a && a->format && a->format->format_capabilties) {
  716. return (a->format->format_capabilties)(a);
  717. }
  718. return ARCHIVE_READ_FORMAT_CAPS_NONE;
  719. }
  720. /*
  721. * Read data from an archive entry, using a read(2)-style interface.
  722. * This is a convenience routine that just calls
  723. * archive_read_data_block and copies the results into the client
  724. * buffer, filling any gaps with zero bytes. Clients using this
  725. * API can be completely ignorant of sparse-file issues; sparse files
  726. * will simply be padded with nulls.
  727. *
  728. * DO NOT intermingle calls to this function and archive_read_data_block
  729. * to read a single entry body.
  730. */
  731. la_ssize_t
  732. archive_read_data(struct archive *_a, void *buff, size_t s)
  733. {
  734. struct archive *a = (struct archive *)_a;
  735. char *dest;
  736. const void *read_buf;
  737. size_t bytes_read;
  738. size_t len;
  739. int r;
  740. bytes_read = 0;
  741. dest = (char *)buff;
  742. while (s > 0) {
  743. if (a->read_data_offset == a->read_data_output_offset &&
  744. a->read_data_remaining == 0) {
  745. read_buf = a->read_data_block;
  746. a->read_data_is_posix_read = 1;
  747. a->read_data_requested = s;
  748. r = archive_read_data_block(a, &read_buf,
  749. &a->read_data_remaining, &a->read_data_offset);
  750. a->read_data_block = read_buf;
  751. if (r == ARCHIVE_EOF)
  752. return (bytes_read);
  753. /*
  754. * Error codes are all negative, so the status
  755. * return here cannot be confused with a valid
  756. * byte count. (ARCHIVE_OK is zero.)
  757. */
  758. if (r < ARCHIVE_OK)
  759. return (r);
  760. }
  761. if (a->read_data_offset < a->read_data_output_offset) {
  762. archive_set_error(a, ARCHIVE_ERRNO_FILE_FORMAT,
  763. "Encountered out-of-order sparse blocks");
  764. return (ARCHIVE_RETRY);
  765. }
  766. /* Compute the amount of zero padding needed. */
  767. if (a->read_data_output_offset + (int64_t)s <
  768. a->read_data_offset) {
  769. len = s;
  770. } else if (a->read_data_output_offset <
  771. a->read_data_offset) {
  772. len = (size_t)(a->read_data_offset -
  773. a->read_data_output_offset);
  774. } else
  775. len = 0;
  776. /* Add zeroes. */
  777. memset(dest, 0, len);
  778. s -= len;
  779. a->read_data_output_offset += len;
  780. dest += len;
  781. bytes_read += len;
  782. /* Copy data if there is any space left. */
  783. if (s > 0) {
  784. len = a->read_data_remaining;
  785. if (len > s)
  786. len = s;
  787. if (len) {
  788. memcpy(dest, a->read_data_block, len);
  789. s -= len;
  790. a->read_data_block += len;
  791. a->read_data_remaining -= len;
  792. a->read_data_output_offset += len;
  793. a->read_data_offset += len;
  794. dest += len;
  795. bytes_read += len;
  796. }
  797. }
  798. }
  799. a->read_data_is_posix_read = 0;
  800. a->read_data_requested = 0;
  801. return (bytes_read);
  802. }
  803. /*
  804. * Reset the read_data_* variables, used for starting a new entry.
  805. */
  806. void __archive_reset_read_data(struct archive * a)
  807. {
  808. a->read_data_output_offset = 0;
  809. a->read_data_remaining = 0;
  810. a->read_data_is_posix_read = 0;
  811. a->read_data_requested = 0;
  812. /* extra resets, from rar.c */
  813. a->read_data_block = NULL;
  814. a->read_data_offset = 0;
  815. }
  816. /*
  817. * Skip over all remaining data in this entry.
  818. */
  819. int
  820. archive_read_data_skip(struct archive *_a)
  821. {
  822. struct archive_read *a = (struct archive_read *)_a;
  823. int r;
  824. const void *buff;
  825. size_t size;
  826. int64_t offset;
  827. archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_DATA,
  828. "archive_read_data_skip");
  829. if (a->format->read_data_skip != NULL)
  830. r = (a->format->read_data_skip)(a);
  831. else {
  832. while ((r = archive_read_data_block(&a->archive,
  833. &buff, &size, &offset))
  834. == ARCHIVE_OK)
  835. ;
  836. }
  837. if (r == ARCHIVE_EOF)
  838. r = ARCHIVE_OK;
  839. a->archive.state = ARCHIVE_STATE_HEADER;
  840. return (r);
  841. }
  842. la_int64_t
  843. archive_seek_data(struct archive *_a, int64_t offset, int whence)
  844. {
  845. struct archive_read *a = (struct archive_read *)_a;
  846. archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_DATA,
  847. "archive_seek_data_block");
  848. if (a->format->seek_data == NULL) {
  849. archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
  850. "Internal error: "
  851. "No format_seek_data_block function registered");
  852. return (ARCHIVE_FATAL);
  853. }
  854. return (a->format->seek_data)(a, offset, whence);
  855. }
  856. /*
  857. * Read the next block of entry data from the archive.
  858. * This is a zero-copy interface; the client receives a pointer,
  859. * size, and file offset of the next available block of data.
  860. *
  861. * Returns ARCHIVE_OK if the operation is successful, ARCHIVE_EOF if
  862. * the end of entry is encountered.
  863. */
  864. static int
  865. _archive_read_data_block(struct archive *_a,
  866. const void **buff, size_t *size, int64_t *offset)
  867. {
  868. struct archive_read *a = (struct archive_read *)_a;
  869. archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_DATA,
  870. "archive_read_data_block");
  871. if (a->format->read_data == NULL) {
  872. archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
  873. "Internal error: "
  874. "No format->read_data function registered");
  875. return (ARCHIVE_FATAL);
  876. }
  877. return (a->format->read_data)(a, buff, size, offset);
  878. }
  879. static int
  880. close_filters(struct archive_read *a)
  881. {
  882. struct archive_read_filter *f = a->filter;
  883. int r = ARCHIVE_OK;
  884. /* Close each filter in the pipeline. */
  885. while (f != NULL) {
  886. struct archive_read_filter *t = f->upstream;
  887. if (!f->closed && f->vtable != NULL) {
  888. int r1 = (f->vtable->close)(f);
  889. f->closed = 1;
  890. if (r1 < r)
  891. r = r1;
  892. }
  893. free(f->buffer);
  894. f->buffer = NULL;
  895. f = t;
  896. }
  897. return r;
  898. }
  899. void
  900. __archive_read_free_filters(struct archive_read *a)
  901. {
  902. /* Make sure filters are closed and their buffers are freed */
  903. close_filters(a);
  904. while (a->filter != NULL) {
  905. struct archive_read_filter *t = a->filter->upstream;
  906. free(a->filter);
  907. a->filter = t;
  908. }
  909. }
  910. /*
  911. * return the count of # of filters in use
  912. */
  913. static int
  914. _archive_filter_count(struct archive *_a)
  915. {
  916. struct archive_read *a = (struct archive_read *)_a;
  917. struct archive_read_filter *p = a->filter;
  918. int count = 0;
  919. while(p) {
  920. count++;
  921. p = p->upstream;
  922. }
  923. return count;
  924. }
  925. /*
  926. * Close the file and all I/O.
  927. */
  928. static int
  929. _archive_read_close(struct archive *_a)
  930. {
  931. struct archive_read *a = (struct archive_read *)_a;
  932. int r = ARCHIVE_OK, r1 = ARCHIVE_OK;
  933. archive_check_magic(&a->archive, ARCHIVE_READ_MAGIC,
  934. ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_read_close");
  935. if (a->archive.state == ARCHIVE_STATE_CLOSED)
  936. return (ARCHIVE_OK);
  937. archive_clear_error(&a->archive);
  938. a->archive.state = ARCHIVE_STATE_CLOSED;
  939. /* TODO: Clean up the formatters. */
  940. /* Release the filter objects. */
  941. r1 = close_filters(a);
  942. if (r1 < r)
  943. r = r1;
  944. return (r);
  945. }
  946. /*
  947. * Release memory and other resources.
  948. */
  949. static int
  950. _archive_read_free(struct archive *_a)
  951. {
  952. struct archive_read *a = (struct archive_read *)_a;
  953. struct archive_read_passphrase *p;
  954. int i, n;
  955. int slots;
  956. int r = ARCHIVE_OK;
  957. if (_a == NULL)
  958. return (ARCHIVE_OK);
  959. archive_check_magic(_a, ARCHIVE_READ_MAGIC,
  960. ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_read_free");
  961. if (a->archive.state != ARCHIVE_STATE_CLOSED
  962. && a->archive.state != ARCHIVE_STATE_FATAL)
  963. r = archive_read_close(&a->archive);
  964. /* Call cleanup functions registered by optional components. */
  965. if (a->cleanup_archive_extract != NULL)
  966. r = (a->cleanup_archive_extract)(a);
  967. /* Cleanup format-specific data. */
  968. slots = sizeof(a->formats) / sizeof(a->formats[0]);
  969. for (i = 0; i < slots; i++) {
  970. a->format = &(a->formats[i]);
  971. if (a->formats[i].cleanup)
  972. (a->formats[i].cleanup)(a);
  973. }
  974. /* Free the filters */
  975. __archive_read_free_filters(a);
  976. /* Release the bidder objects. */
  977. n = sizeof(a->bidders)/sizeof(a->bidders[0]);
  978. for (i = 0; i < n; i++) {
  979. if (a->bidders[i].vtable == NULL ||
  980. a->bidders[i].vtable->free == NULL)
  981. continue;
  982. (a->bidders[i].vtable->free)(&a->bidders[i]);
  983. }
  984. /* Release passphrase list. */
  985. p = a->passphrases.first;
  986. while (p != NULL) {
  987. struct archive_read_passphrase *np = p->next;
  988. /* A passphrase should be cleaned. */
  989. memset(p->passphrase, 0, strlen(p->passphrase));
  990. free(p->passphrase);
  991. free(p);
  992. p = np;
  993. }
  994. archive_string_free(&a->archive.error_string);
  995. archive_entry_free(a->entry);
  996. a->archive.magic = 0;
  997. __archive_clean(&a->archive);
  998. free(a->client.dataset);
  999. free(a);
  1000. return (r);
  1001. }
  1002. static struct archive_read_filter *
  1003. get_filter(struct archive *_a, int n)
  1004. {
  1005. struct archive_read *a = (struct archive_read *)_a;
  1006. struct archive_read_filter *f = a->filter;
  1007. /* We use n == -1 for 'the last filter', which is always the
  1008. * client proxy. */
  1009. if (n == -1 && f != NULL) {
  1010. struct archive_read_filter *last = f;
  1011. f = f->upstream;
  1012. while (f != NULL) {
  1013. last = f;
  1014. f = f->upstream;
  1015. }
  1016. return (last);
  1017. }
  1018. if (n < 0)
  1019. return NULL;
  1020. while (n > 0 && f != NULL) {
  1021. f = f->upstream;
  1022. --n;
  1023. }
  1024. return (f);
  1025. }
  1026. static int
  1027. _archive_filter_code(struct archive *_a, int n)
  1028. {
  1029. struct archive_read_filter *f = get_filter(_a, n);
  1030. return f == NULL ? -1 : f->code;
  1031. }
  1032. static const char *
  1033. _archive_filter_name(struct archive *_a, int n)
  1034. {
  1035. struct archive_read_filter *f = get_filter(_a, n);
  1036. return f != NULL ? f->name : NULL;
  1037. }
  1038. static int64_t
  1039. _archive_filter_bytes(struct archive *_a, int n)
  1040. {
  1041. struct archive_read_filter *f = get_filter(_a, n);
  1042. return f == NULL ? -1 : f->position;
  1043. }
  1044. /*
  1045. * Used internally by read format handlers to register their bid and
  1046. * initialization functions.
  1047. */
  1048. int
  1049. __archive_read_register_format(struct archive_read *a,
  1050. void *format_data,
  1051. const char *name,
  1052. int (*bid)(struct archive_read *, int),
  1053. int (*options)(struct archive_read *, const char *, const char *),
  1054. int (*read_header)(struct archive_read *, struct archive_entry *),
  1055. int (*read_data)(struct archive_read *, const void **, size_t *, int64_t *),
  1056. int (*read_data_skip)(struct archive_read *),
  1057. int64_t (*seek_data)(struct archive_read *, int64_t, int),
  1058. int (*cleanup)(struct archive_read *),
  1059. int (*format_capabilities)(struct archive_read *),
  1060. int (*has_encrypted_entries)(struct archive_read *))
  1061. {
  1062. int i, number_slots;
  1063. archive_check_magic(&a->archive,
  1064. ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
  1065. "__archive_read_register_format");
  1066. number_slots = sizeof(a->formats) / sizeof(a->formats[0]);
  1067. for (i = 0; i < number_slots; i++) {
  1068. if (a->formats[i].bid == bid)
  1069. return (ARCHIVE_WARN); /* We've already installed */
  1070. if (a->formats[i].bid == NULL) {
  1071. a->formats[i].bid = bid;
  1072. a->formats[i].options = options;
  1073. a->formats[i].read_header = read_header;
  1074. a->formats[i].read_data = read_data;
  1075. a->formats[i].read_data_skip = read_data_skip;
  1076. a->formats[i].seek_data = seek_data;
  1077. a->formats[i].cleanup = cleanup;
  1078. a->formats[i].data = format_data;
  1079. a->formats[i].name = name;
  1080. a->formats[i].format_capabilties = format_capabilities;
  1081. a->formats[i].has_encrypted_entries = has_encrypted_entries;
  1082. return (ARCHIVE_OK);
  1083. }
  1084. }
  1085. archive_set_error(&a->archive, ENOMEM,
  1086. "Not enough slots for format registration");
  1087. return (ARCHIVE_FATAL);
  1088. }
  1089. /*
  1090. * Used internally by decompression routines to register their bid and
  1091. * initialization functions.
  1092. */
  1093. int
  1094. __archive_read_register_bidder(struct archive_read *a,
  1095. void *bidder_data,
  1096. const char *name,
  1097. const struct archive_read_filter_bidder_vtable *vtable)
  1098. {
  1099. struct archive_read_filter_bidder *bidder;
  1100. int i, number_slots;
  1101. archive_check_magic(&a->archive, ARCHIVE_READ_MAGIC,
  1102. ARCHIVE_STATE_NEW, "__archive_read_register_bidder");
  1103. number_slots = sizeof(a->bidders) / sizeof(a->bidders[0]);
  1104. for (i = 0; i < number_slots; i++) {
  1105. if (a->bidders[i].vtable != NULL)
  1106. continue;
  1107. memset(a->bidders + i, 0, sizeof(a->bidders[0]));
  1108. bidder = (a->bidders + i);
  1109. bidder->data = bidder_data;
  1110. bidder->name = name;
  1111. bidder->vtable = vtable;
  1112. if (bidder->vtable->bid == NULL || bidder->vtable->init == NULL) {
  1113. archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
  1114. "Internal error: "
  1115. "no bid/init for filter bidder");
  1116. return (ARCHIVE_FATAL);
  1117. }
  1118. return (ARCHIVE_OK);
  1119. }
  1120. archive_set_error(&a->archive, ENOMEM,
  1121. "Not enough slots for filter registration");
  1122. return (ARCHIVE_FATAL);
  1123. }
  1124. /*
  1125. * The next section implements the peek/consume internal I/O
  1126. * system used by archive readers. This system allows simple
  1127. * read-ahead for consumers while preserving zero-copy operation
  1128. * most of the time.
  1129. *
  1130. * The two key operations:
  1131. * * The read-ahead function returns a pointer to a block of data
  1132. * that satisfies a minimum request.
  1133. * * The consume function advances the file pointer.
  1134. *
  1135. * In the ideal case, filters generate blocks of data
  1136. * and __archive_read_ahead() just returns pointers directly into
  1137. * those blocks. Then __archive_read_consume() just bumps those
  1138. * pointers. Only if your request would span blocks does the I/O
  1139. * layer use a copy buffer to provide you with a contiguous block of
  1140. * data.
  1141. *
  1142. * A couple of useful idioms:
  1143. * * "I just want some data." Ask for 1 byte and pay attention to
  1144. * the "number of bytes available" from __archive_read_ahead().
  1145. * Consume whatever you actually use.
  1146. * * "I want to output a large block of data." As above, ask for 1 byte,
  1147. * emit all that's available (up to whatever limit you have), consume
  1148. * it all, then repeat until you're done. This effectively means that
  1149. * you're passing along the blocks that came from your provider.
  1150. * * "I want to peek ahead by a large amount." Ask for 4k or so, then
  1151. * double and repeat until you get an error or have enough. Note
  1152. * that the I/O layer will likely end up expanding its copy buffer
  1153. * to fit your request, so use this technique cautiously. This
  1154. * technique is used, for example, by some of the format tasting
  1155. * code that has uncertain look-ahead needs.
  1156. */
  1157. /*
  1158. * Looks ahead in the input stream:
  1159. * * If 'avail' pointer is provided, that returns number of bytes available
  1160. * in the current buffer, which may be much larger than requested.
  1161. * * If end-of-file, *avail gets set to zero.
  1162. * * If error, *avail gets error code.
  1163. * * If request can be met, returns pointer to data.
  1164. * * If minimum request cannot be met, returns NULL.
  1165. *
  1166. * Note: If you just want "some data", ask for 1 byte and pay attention
  1167. * to *avail, which will have the actual amount available. If you
  1168. * know exactly how many bytes you need, just ask for that and treat
  1169. * a NULL return as an error.
  1170. *
  1171. * Important: This does NOT move the file pointer. See
  1172. * __archive_read_consume() below.
  1173. */
  1174. const void *
  1175. __archive_read_ahead(struct archive_read *a, size_t min, ssize_t *avail)
  1176. {
  1177. return (__archive_read_filter_ahead(a->filter, min, avail));
  1178. }
  1179. const void *
  1180. __archive_read_filter_ahead(struct archive_read_filter *filter,
  1181. size_t min, ssize_t *avail)
  1182. {
  1183. ssize_t bytes_read;
  1184. size_t tocopy;
  1185. if (filter->fatal) {
  1186. if (avail)
  1187. *avail = ARCHIVE_FATAL;
  1188. return (NULL);
  1189. }
  1190. /*
  1191. * Keep pulling more data until we can satisfy the request.
  1192. */
  1193. for (;;) {
  1194. /*
  1195. * If we can satisfy from the copy buffer (and the
  1196. * copy buffer isn't empty), we're done. In particular,
  1197. * note that min == 0 is a perfectly well-defined
  1198. * request.
  1199. */
  1200. if (filter->avail >= min && filter->avail > 0) {
  1201. if (avail != NULL)
  1202. *avail = filter->avail;
  1203. return (filter->next);
  1204. }
  1205. /*
  1206. * We can satisfy directly from client buffer if everything
  1207. * currently in the copy buffer is still in the client buffer.
  1208. */
  1209. if (filter->client_total >= filter->client_avail + filter->avail
  1210. && filter->client_avail + filter->avail >= min) {
  1211. /* "Roll back" to client buffer. */
  1212. filter->client_avail += filter->avail;
  1213. filter->client_next -= filter->avail;
  1214. /* Copy buffer is now empty. */
  1215. filter->avail = 0;
  1216. filter->next = filter->buffer;
  1217. /* Return data from client buffer. */
  1218. if (avail != NULL)
  1219. *avail = filter->client_avail;
  1220. return (filter->client_next);
  1221. }
  1222. /* Move data forward in copy buffer if necessary. */
  1223. if (filter->next > filter->buffer &&
  1224. filter->next + min > filter->buffer + filter->buffer_size) {
  1225. if (filter->avail > 0)
  1226. memmove(filter->buffer, filter->next,
  1227. filter->avail);
  1228. filter->next = filter->buffer;
  1229. }
  1230. /* If we've used up the client data, get more. */
  1231. if (filter->client_avail <= 0) {
  1232. if (filter->end_of_file) {
  1233. if (avail != NULL)
  1234. *avail = filter->avail;
  1235. return (NULL);
  1236. }
  1237. bytes_read = (filter->vtable->read)(filter,
  1238. &filter->client_buff);
  1239. if (bytes_read < 0) { /* Read error. */
  1240. filter->client_total = filter->client_avail = 0;
  1241. filter->client_next =
  1242. filter->client_buff = NULL;
  1243. filter->fatal = 1;
  1244. if (avail != NULL)
  1245. *avail = ARCHIVE_FATAL;
  1246. return (NULL);
  1247. }
  1248. if (bytes_read == 0) {
  1249. /* Check for another client object first */
  1250. if (filter->archive->client.cursor !=
  1251. filter->archive->client.nodes - 1) {
  1252. if (client_switch_proxy(filter,
  1253. filter->archive->client.cursor + 1)
  1254. == ARCHIVE_OK)
  1255. continue;
  1256. }
  1257. /* Premature end-of-file. */
  1258. filter->client_total = filter->client_avail = 0;
  1259. filter->client_next =
  1260. filter->client_buff = NULL;
  1261. filter->end_of_file = 1;
  1262. /* Return whatever we do have. */
  1263. if (avail != NULL)
  1264. *avail = filter->avail;
  1265. return (NULL);
  1266. }
  1267. filter->client_total = bytes_read;
  1268. filter->client_avail = filter->client_total;
  1269. filter->client_next = filter->client_buff;
  1270. } else {
  1271. /*
  1272. * We can't satisfy the request from the copy
  1273. * buffer or the existing client data, so we
  1274. * need to copy more client data over to the
  1275. * copy buffer.
  1276. */
  1277. /* Ensure the buffer is big enough. */
  1278. if (min > filter->buffer_size) {
  1279. size_t s, t;
  1280. char *p;
  1281. /* Double the buffer; watch for overflow. */
  1282. s = t = filter->buffer_size;
  1283. if (s == 0)
  1284. s = min;
  1285. while (s < min) {
  1286. t *= 2;
  1287. if (t <= s) { /* Integer overflow! */
  1288. archive_set_error(
  1289. &filter->archive->archive,
  1290. ENOMEM,
  1291. "Unable to allocate copy"
  1292. " buffer");
  1293. filter->fatal = 1;
  1294. if (avail != NULL)
  1295. *avail = ARCHIVE_FATAL;
  1296. return (NULL);
  1297. }
  1298. s = t;
  1299. }
  1300. /* Now s >= min, so allocate a new buffer. */
  1301. p = malloc(s);
  1302. if (p == NULL) {
  1303. archive_set_error(
  1304. &filter->archive->archive,
  1305. ENOMEM,
  1306. "Unable to allocate copy buffer");
  1307. filter->fatal = 1;
  1308. if (avail != NULL)
  1309. *avail = ARCHIVE_FATAL;
  1310. return (NULL);
  1311. }
  1312. /* Move data into newly-enlarged buffer. */
  1313. if (filter->avail > 0)
  1314. memmove(p, filter->next, filter->avail);
  1315. free(filter->buffer);
  1316. filter->next = filter->buffer = p;
  1317. filter->buffer_size = s;
  1318. }
  1319. /* We can add client data to copy buffer. */
  1320. /* First estimate: copy to fill rest of buffer. */
  1321. tocopy = (filter->buffer + filter->buffer_size)
  1322. - (filter->next + filter->avail);
  1323. /* Don't waste time buffering more than we need to. */
  1324. if (tocopy + filter->avail > min)
  1325. tocopy = min - filter->avail;
  1326. /* Don't copy more than is available. */
  1327. if (tocopy > filter->client_avail)
  1328. tocopy = filter->client_avail;
  1329. memcpy(filter->next + filter->avail,
  1330. filter->client_next, tocopy);
  1331. /* Remove this data from client buffer. */
  1332. filter->client_next += tocopy;
  1333. filter->client_avail -= tocopy;
  1334. /* add it to copy buffer. */
  1335. filter->avail += tocopy;
  1336. }
  1337. }
  1338. }
  1339. /*
  1340. * Move the file pointer forward.
  1341. */
  1342. int64_t
  1343. __archive_read_consume(struct archive_read *a, int64_t request)
  1344. {
  1345. return (__archive_read_filter_consume(a->filter, request));
  1346. }
  1347. int64_t
  1348. __archive_read_filter_consume(struct archive_read_filter * filter,
  1349. int64_t request)
  1350. {
  1351. int64_t skipped;
  1352. if (request < 0)
  1353. return ARCHIVE_FATAL;
  1354. if (request == 0)
  1355. return 0;
  1356. skipped = advance_file_pointer(filter, request);
  1357. if (skipped == request)
  1358. return (skipped);
  1359. /* We hit EOF before we satisfied the skip request. */
  1360. if (skipped < 0) /* Map error code to 0 for error message below. */
  1361. skipped = 0;
  1362. archive_set_error(&filter->archive->archive,
  1363. ARCHIVE_ERRNO_MISC,
  1364. "Truncated input file (needed %jd bytes, only %jd available)",
  1365. (intmax_t)request, (intmax_t)skipped);
  1366. return (ARCHIVE_FATAL);
  1367. }
  1368. /*
  1369. * Advance the file pointer by the amount requested.
  1370. * Returns the amount actually advanced, which may be less than the
  1371. * request if EOF is encountered first.
  1372. * Returns a negative value if there's an I/O error.
  1373. */
  1374. static int64_t
  1375. advance_file_pointer(struct archive_read_filter *filter, int64_t request)
  1376. {
  1377. int64_t bytes_skipped, total_bytes_skipped = 0;
  1378. ssize_t bytes_read;
  1379. size_t min;
  1380. if (filter->fatal)
  1381. return (-1);
  1382. /* Use up the copy buffer first. */
  1383. if (filter->avail > 0) {
  1384. min = (size_t)minimum(request, (int64_t)filter->avail);
  1385. filter->next += min;
  1386. filter->avail -= min;
  1387. request -= min;
  1388. filter->position += min;
  1389. total_bytes_skipped += min;
  1390. }
  1391. /* Then use up the client buffer. */
  1392. if (filter->client_avail > 0) {
  1393. min = (size_t)minimum(request, (int64_t)filter->client_avail);
  1394. filter->client_next += min;
  1395. filter->client_avail -= min;
  1396. request -= min;
  1397. filter->position += min;
  1398. total_bytes_skipped += min;
  1399. }
  1400. if (request == 0)
  1401. return (total_bytes_skipped);
  1402. /* If there's an optimized skip function, use it. */
  1403. if (filter->can_skip != 0) {
  1404. bytes_skipped = client_skip_proxy(filter, request);
  1405. if (bytes_skipped < 0) { /* error */
  1406. filter->fatal = 1;
  1407. return (bytes_skipped);
  1408. }
  1409. filter->position += bytes_skipped;
  1410. total_bytes_skipped += bytes_skipped;
  1411. request -= bytes_skipped;
  1412. if (request == 0)
  1413. return (total_bytes_skipped);
  1414. }
  1415. /* Use ordinary reads as necessary to complete the request. */
  1416. for (;;) {
  1417. bytes_read = (filter->vtable->read)(filter, &filter->client_buff);
  1418. if (bytes_read < 0) {
  1419. filter->client_buff = NULL;
  1420. filter->fatal = 1;
  1421. return (bytes_read);
  1422. }
  1423. if (bytes_read == 0) {
  1424. if (filter->archive->client.cursor !=
  1425. filter->archive->client.nodes - 1) {
  1426. if (client_switch_proxy(filter,
  1427. filter->archive->client.cursor + 1)
  1428. == ARCHIVE_OK)
  1429. continue;
  1430. }
  1431. filter->client_buff = NULL;
  1432. filter->end_of_file = 1;
  1433. return (total_bytes_skipped);
  1434. }
  1435. if (bytes_read >= request) {
  1436. filter->client_next =
  1437. ((const char *)filter->client_buff) + request;
  1438. filter->client_avail = (size_t)(bytes_read - request);
  1439. filter->client_total = bytes_read;
  1440. total_bytes_skipped += request;
  1441. filter->position += request;
  1442. return (total_bytes_skipped);
  1443. }
  1444. filter->position += bytes_read;
  1445. total_bytes_skipped += bytes_read;
  1446. request -= bytes_read;
  1447. }
  1448. }
  1449. /**
  1450. * Returns ARCHIVE_FAILED if seeking isn't supported.
  1451. */
  1452. int64_t
  1453. __archive_read_seek(struct archive_read *a, int64_t offset, int whence)
  1454. {
  1455. return __archive_read_filter_seek(a->filter, offset, whence);
  1456. }
  1457. int64_t
  1458. __archive_read_filter_seek(struct archive_read_filter *filter, int64_t offset,
  1459. int whence)
  1460. {
  1461. struct archive_read_client *client;
  1462. int64_t r;
  1463. unsigned int cursor;
  1464. if (filter->closed || filter->fatal)
  1465. return (ARCHIVE_FATAL);
  1466. if (filter->can_seek == 0)
  1467. return (ARCHIVE_FAILED);
  1468. client = &(filter->archive->client);
  1469. switch (whence) {
  1470. case SEEK_CUR:
  1471. /* Adjust the offset and use SEEK_SET instead */
  1472. offset += filter->position;
  1473. __LA_FALLTHROUGH;
  1474. case SEEK_SET:
  1475. cursor = 0;
  1476. while (1)
  1477. {
  1478. if (client->dataset[cursor].begin_position < 0 ||
  1479. client->dataset[cursor].total_size < 0 ||
  1480. client->dataset[cursor].begin_position +
  1481. client->dataset[cursor].total_size - 1 > offset ||
  1482. cursor + 1 >= client->nodes)
  1483. break;
  1484. r = client->dataset[cursor].begin_position +
  1485. client->dataset[cursor].total_size;
  1486. client->dataset[++cursor].begin_position = r;
  1487. }
  1488. while (1) {
  1489. r = client_switch_proxy(filter, cursor);
  1490. if (r != ARCHIVE_OK)
  1491. return r;
  1492. if ((r = client_seek_proxy(filter, 0, SEEK_END)) < 0)
  1493. return r;
  1494. client->dataset[cursor].total_size = r;
  1495. if (client->dataset[cursor].begin_position +
  1496. client->dataset[cursor].total_size - 1 > offset ||
  1497. cursor + 1 >= client->nodes)
  1498. break;
  1499. r = client->dataset[cursor].begin_position +
  1500. client->dataset[cursor].total_size;
  1501. client->dataset[++cursor].begin_position = r;
  1502. }
  1503. offset -= client->dataset[cursor].begin_position;
  1504. if (offset < 0
  1505. || offset > client->dataset[cursor].total_size)
  1506. return ARCHIVE_FATAL;
  1507. if ((r = client_seek_proxy(filter, offset, SEEK_SET)) < 0)
  1508. return r;
  1509. break;
  1510. case SEEK_END:
  1511. cursor = 0;
  1512. while (1) {
  1513. if (client->dataset[cursor].begin_position < 0 ||
  1514. client->dataset[cursor].total_size < 0 ||
  1515. cursor + 1 >= client->nodes)
  1516. break;
  1517. r = client->dataset[cursor].begin_position +
  1518. client->dataset[cursor].total_size;
  1519. client->dataset[++cursor].begin_position = r;
  1520. }
  1521. while (1) {
  1522. r = client_switch_proxy(filter, cursor);
  1523. if (r != ARCHIVE_OK)
  1524. return r;
  1525. if ((r = client_seek_proxy(filter, 0, SEEK_END)) < 0)
  1526. return r;
  1527. client->dataset[cursor].total_size = r;
  1528. r = client->dataset[cursor].begin_position +
  1529. client->dataset[cursor].total_size;
  1530. if (cursor + 1 >= client->nodes)
  1531. break;
  1532. client->dataset[++cursor].begin_position = r;
  1533. }
  1534. while (1) {
  1535. if (r + offset >=
  1536. client->dataset[cursor].begin_position)
  1537. break;
  1538. offset += client->dataset[cursor].total_size;
  1539. if (cursor == 0)
  1540. break;
  1541. cursor--;
  1542. r = client->dataset[cursor].begin_position +
  1543. client->dataset[cursor].total_size;
  1544. }
  1545. offset = (r + offset) - client->dataset[cursor].begin_position;
  1546. if ((r = client_switch_proxy(filter, cursor)) != ARCHIVE_OK)
  1547. return r;
  1548. r = client_seek_proxy(filter, offset, SEEK_SET);
  1549. if (r < ARCHIVE_OK)
  1550. return r;
  1551. break;
  1552. default:
  1553. return (ARCHIVE_FATAL);
  1554. }
  1555. r += client->dataset[cursor].begin_position;
  1556. if (r >= 0) {
  1557. /*
  1558. * Ouch. Clearing the buffer like this hurts, especially
  1559. * at bid time. A lot of our efficiency at bid time comes
  1560. * from having bidders reuse the data we've already read.
  1561. *
  1562. * TODO: If the seek request is in data we already
  1563. * have, then don't call the seek callback.
  1564. *
  1565. * TODO: Zip seeks to end-of-file at bid time. If
  1566. * other formats also start doing this, we may need to
  1567. * find a way for clients to fudge the seek offset to
  1568. * a block boundary.
  1569. *
  1570. * Hmmm... If whence was SEEK_END, we know the file
  1571. * size is (r - offset). Can we use that to simplify
  1572. * the TODO items above?
  1573. */
  1574. filter->avail = filter->client_avail = 0;
  1575. filter->next = filter->buffer;
  1576. filter->position = r;
  1577. filter->end_of_file = 0;
  1578. }
  1579. return r;
  1580. }