archive_read.c 48 KB

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