archive_read.c 48 KB

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