archive_read_support_format_tar.c 106 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599
  1. /*-
  2. * Copyright (c) 2003-2023 Tim Kientzle
  3. * Copyright (c) 2011-2012 Michihiro NAKAJIMA
  4. * Copyright (c) 2016 Martin Matuska
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  18. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  19. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  21. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  25. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "archive_platform.h"
  28. #ifdef HAVE_ERRNO_H
  29. #include <errno.h>
  30. #endif
  31. #include <stddef.h>
  32. #ifdef HAVE_STDLIB_H
  33. #include <stdlib.h>
  34. #endif
  35. #ifdef HAVE_STRING_H
  36. #include <string.h>
  37. #endif
  38. #include "archive.h"
  39. #include "archive_acl_private.h" /* For ACL parsing routines. */
  40. #include "archive_entry.h"
  41. #include "archive_entry_locale.h"
  42. #include "archive_private.h"
  43. #include "archive_read_private.h"
  44. #define tar_min(a,b) ((a) < (b) ? (a) : (b))
  45. /*
  46. * Layout of POSIX 'ustar' tar header.
  47. */
  48. struct archive_entry_header_ustar {
  49. char name[100];
  50. char mode[8];
  51. char uid[8];
  52. char gid[8];
  53. char size[12];
  54. char mtime[12];
  55. char checksum[8];
  56. char typeflag[1];
  57. char linkname[100]; /* "old format" header ends here */
  58. char magic[6]; /* For POSIX: "ustar\0" */
  59. char version[2]; /* For POSIX: "00" */
  60. char uname[32];
  61. char gname[32];
  62. char rdevmajor[8];
  63. char rdevminor[8];
  64. char prefix[155];
  65. };
  66. /*
  67. * Structure of GNU tar header
  68. */
  69. struct gnu_sparse {
  70. char offset[12];
  71. char numbytes[12];
  72. };
  73. struct archive_entry_header_gnutar {
  74. char name[100];
  75. char mode[8];
  76. char uid[8];
  77. char gid[8];
  78. char size[12];
  79. char mtime[12];
  80. char checksum[8];
  81. char typeflag[1];
  82. char linkname[100];
  83. char magic[8]; /* "ustar \0" (note blank/blank/null at end) */
  84. char uname[32];
  85. char gname[32];
  86. char rdevmajor[8];
  87. char rdevminor[8];
  88. char atime[12];
  89. char ctime[12];
  90. char offset[12];
  91. char longnames[4];
  92. char unused[1];
  93. struct gnu_sparse sparse[4];
  94. char isextended[1];
  95. char realsize[12];
  96. /*
  97. * Old GNU format doesn't use POSIX 'prefix' field; they use
  98. * the 'L' (longname) entry instead.
  99. */
  100. };
  101. /*
  102. * Data specific to this format.
  103. */
  104. struct sparse_block {
  105. struct sparse_block *next;
  106. int64_t offset;
  107. int64_t remaining;
  108. int hole;
  109. };
  110. struct tar {
  111. struct archive_string entry_pathname;
  112. /* For "GNU.sparse.name" and other similar path extensions. */
  113. struct archive_string entry_pathname_override;
  114. struct archive_string entry_uname;
  115. struct archive_string entry_gname;
  116. struct archive_string entry_linkpath;
  117. struct archive_string line;
  118. int pax_hdrcharset_utf8;
  119. int64_t entry_bytes_remaining;
  120. int64_t entry_offset;
  121. int64_t entry_padding;
  122. int64_t entry_bytes_unconsumed;
  123. int64_t realsize;
  124. struct sparse_block *sparse_list;
  125. struct sparse_block *sparse_last;
  126. int64_t sparse_offset;
  127. int64_t sparse_numbytes;
  128. int sparse_gnu_major;
  129. int sparse_gnu_minor;
  130. char sparse_gnu_attributes_seen;
  131. char filetype;
  132. struct archive_string localname;
  133. struct archive_string_conv *opt_sconv;
  134. struct archive_string_conv *sconv;
  135. struct archive_string_conv *sconv_acl;
  136. struct archive_string_conv *sconv_default;
  137. int init_default_conversion;
  138. int compat_2x;
  139. int process_mac_extensions;
  140. int read_concatenated_archives;
  141. int realsize_override;
  142. };
  143. static int archive_block_is_null(const char *p);
  144. static char *base64_decode(const char *, size_t, size_t *);
  145. static int gnu_add_sparse_entry(struct archive_read *, struct tar *,
  146. int64_t offset, int64_t remaining);
  147. static void gnu_clear_sparse_list(struct tar *);
  148. static int gnu_sparse_old_read(struct archive_read *, struct tar *,
  149. const struct archive_entry_header_gnutar *header, size_t *);
  150. static int gnu_sparse_old_parse(struct archive_read *, struct tar *,
  151. const struct gnu_sparse *sparse, int length);
  152. static int gnu_sparse_01_parse(struct archive_read *, struct tar *,
  153. const char *, size_t);
  154. static ssize_t gnu_sparse_10_read(struct archive_read *, struct tar *,
  155. size_t *);
  156. static int header_Solaris_ACL(struct archive_read *, struct tar *,
  157. struct archive_entry *, const void *, size_t *);
  158. static int header_common(struct archive_read *, struct tar *,
  159. struct archive_entry *, const void *);
  160. static int header_old_tar(struct archive_read *, struct tar *,
  161. struct archive_entry *, const void *);
  162. static int header_pax_extension(struct archive_read *, struct tar *,
  163. struct archive_entry *, const void *, size_t *);
  164. static int header_pax_global(struct archive_read *, struct tar *,
  165. struct archive_entry *, const void *h, size_t *);
  166. static int header_gnu_longlink(struct archive_read *, struct tar *,
  167. struct archive_entry *, const void *h, size_t *);
  168. static int header_gnu_longname(struct archive_read *, struct tar *,
  169. struct archive_entry *, const void *h, size_t *);
  170. static int is_mac_metadata_entry(struct archive_entry *entry);
  171. static int read_mac_metadata_blob(struct archive_read *,
  172. struct archive_entry *, size_t *);
  173. static int header_volume(struct archive_read *, struct tar *,
  174. struct archive_entry *, const void *h, size_t *);
  175. static int header_ustar(struct archive_read *, struct tar *,
  176. struct archive_entry *, const void *h);
  177. static int header_gnutar(struct archive_read *, struct tar *,
  178. struct archive_entry *, const void *h, size_t *);
  179. static int archive_read_format_tar_bid(struct archive_read *, int);
  180. static int archive_read_format_tar_options(struct archive_read *,
  181. const char *, const char *);
  182. static int archive_read_format_tar_cleanup(struct archive_read *);
  183. static int archive_read_format_tar_read_data(struct archive_read *a,
  184. const void **buff, size_t *size, int64_t *offset);
  185. static int archive_read_format_tar_skip(struct archive_read *a);
  186. static int archive_read_format_tar_read_header(struct archive_read *,
  187. struct archive_entry *);
  188. static int checksum(struct archive_read *, const void *);
  189. static int pax_attribute(struct archive_read *, struct tar *,
  190. struct archive_entry *, const char *key, size_t key_length,
  191. size_t value_length, size_t *unconsumed);
  192. static int pax_attribute_LIBARCHIVE_xattr(struct archive_entry *,
  193. const char *, size_t, const char *, size_t);
  194. static int pax_attribute_SCHILY_acl(struct archive_read *, struct tar *,
  195. struct archive_entry *, size_t, int);
  196. static int pax_attribute_SUN_holesdata(struct archive_read *, struct tar *,
  197. struct archive_entry *, const char *, size_t);
  198. static void pax_time(const char *, size_t, int64_t *sec, long *nanos);
  199. static ssize_t readline(struct archive_read *, struct tar *, const char **,
  200. ssize_t limit, size_t *);
  201. static int read_body_to_string(struct archive_read *, struct tar *,
  202. struct archive_string *, const void *h, size_t *);
  203. static int read_bytes_to_string(struct archive_read *,
  204. struct archive_string *, size_t, size_t *);
  205. static int64_t tar_atol(const char *, size_t);
  206. static int64_t tar_atol10(const char *, size_t);
  207. static int64_t tar_atol256(const char *, size_t);
  208. static int64_t tar_atol8(const char *, size_t);
  209. static int tar_read_header(struct archive_read *, struct tar *,
  210. struct archive_entry *, size_t *);
  211. static int tohex(int c);
  212. static char *url_decode(const char *, size_t);
  213. static void tar_flush_unconsumed(struct archive_read *, size_t *);
  214. /* Sanity limits: These numbers should be low enough to
  215. * prevent a maliciously-crafted archive from forcing us to
  216. * allocate extreme amounts of memory. But of course, they
  217. * need to be high enough for any correct value. These
  218. * will likely need some adjustment as we get more experience. */
  219. static const size_t guname_limit = 65536; /* Longest uname or gname: 64kiB */
  220. static const size_t pathname_limit = 1048576; /* Longest path name: 1MiB */
  221. static const size_t sparse_map_limit = 8 * 1048576; /* Longest sparse map: 8MiB */
  222. static const size_t xattr_limit = 16 * 1048576; /* Longest xattr: 16MiB */
  223. static const size_t fflags_limit = 512; /* Longest fflags */
  224. static const size_t acl_limit = 131072; /* Longest textual ACL: 128kiB */
  225. static const int64_t entry_limit = 0xfffffffffffffffLL; /* 2^60 bytes = 1 ExbiByte */
  226. int
  227. archive_read_support_format_gnutar(struct archive *a)
  228. {
  229. archive_check_magic(a, ARCHIVE_READ_MAGIC,
  230. ARCHIVE_STATE_NEW, "archive_read_support_format_gnutar");
  231. return (archive_read_support_format_tar(a));
  232. }
  233. int
  234. archive_read_support_format_tar(struct archive *_a)
  235. {
  236. struct archive_read *a = (struct archive_read *)_a;
  237. struct tar *tar;
  238. int r;
  239. archive_check_magic(_a, ARCHIVE_READ_MAGIC,
  240. ARCHIVE_STATE_NEW, "archive_read_support_format_tar");
  241. tar = calloc(1, sizeof(*tar));
  242. if (tar == NULL) {
  243. archive_set_error(&a->archive, ENOMEM,
  244. "Can't allocate tar data");
  245. return (ARCHIVE_FATAL);
  246. }
  247. #ifdef HAVE_COPYFILE_H
  248. /* Set this by default on Mac OS. */
  249. tar->process_mac_extensions = 1;
  250. #endif
  251. r = __archive_read_register_format(a, tar, "tar",
  252. archive_read_format_tar_bid,
  253. archive_read_format_tar_options,
  254. archive_read_format_tar_read_header,
  255. archive_read_format_tar_read_data,
  256. archive_read_format_tar_skip,
  257. NULL,
  258. archive_read_format_tar_cleanup,
  259. NULL,
  260. NULL);
  261. if (r != ARCHIVE_OK)
  262. free(tar);
  263. return (ARCHIVE_OK);
  264. }
  265. static int
  266. archive_read_format_tar_cleanup(struct archive_read *a)
  267. {
  268. struct tar *tar;
  269. tar = (struct tar *)(a->format->data);
  270. gnu_clear_sparse_list(tar);
  271. archive_string_free(&tar->entry_pathname);
  272. archive_string_free(&tar->entry_pathname_override);
  273. archive_string_free(&tar->entry_uname);
  274. archive_string_free(&tar->entry_gname);
  275. archive_string_free(&tar->entry_linkpath);
  276. archive_string_free(&tar->line);
  277. archive_string_free(&tar->localname);
  278. free(tar);
  279. (a->format->data) = NULL;
  280. return (ARCHIVE_OK);
  281. }
  282. /*
  283. * Validate number field
  284. *
  285. * This has to be pretty lenient in order to accommodate the enormous
  286. * variety of tar writers in the world:
  287. * = POSIX (IEEE Std 1003.1-1988) ustar requires octal values with leading
  288. * zeros and allows fields to be terminated with space or null characters
  289. * = Many writers use different termination (in particular, libarchive
  290. * omits terminator bytes to squeeze one or two more digits)
  291. * = Many writers pad with space and omit leading zeros
  292. * = GNU tar and star write base-256 values if numbers are too
  293. * big to be represented in octal
  294. *
  295. * Examples of specific tar headers that we should support:
  296. * = Perl Archive::Tar terminates uid, gid, devminor and devmajor with two
  297. * null bytes, pads size with spaces and other numeric fields with zeroes
  298. * = plexus-archiver prior to 2.6.3 (before switching to commons-compress)
  299. * may have uid and gid fields filled with spaces without any octal digits
  300. * at all and pads all numeric fields with spaces
  301. *
  302. * This should tolerate all variants in use. It will reject a field
  303. * where the writer just left garbage after a trailing NUL.
  304. */
  305. static int
  306. validate_number_field(const char* p_field, size_t i_size)
  307. {
  308. unsigned char marker = (unsigned char)p_field[0];
  309. if (marker == 128 || marker == 255 || marker == 0) {
  310. /* Base-256 marker, there's nothing we can check. */
  311. return 1;
  312. } else {
  313. /* Must be octal */
  314. size_t i = 0;
  315. /* Skip any leading spaces */
  316. while (i < i_size && p_field[i] == ' ') {
  317. ++i;
  318. }
  319. /* Skip octal digits. */
  320. while (i < i_size && p_field[i] >= '0' && p_field[i] <= '7') {
  321. ++i;
  322. }
  323. /* Any remaining characters must be space or NUL padding. */
  324. while (i < i_size) {
  325. if (p_field[i] != ' ' && p_field[i] != 0) {
  326. return 0;
  327. }
  328. ++i;
  329. }
  330. return 1;
  331. }
  332. }
  333. static int
  334. archive_read_format_tar_bid(struct archive_read *a, int best_bid)
  335. {
  336. int bid;
  337. const char *h;
  338. const struct archive_entry_header_ustar *header;
  339. (void)best_bid; /* UNUSED */
  340. bid = 0;
  341. /* Now let's look at the actual header and see if it matches. */
  342. h = __archive_read_ahead(a, 512, NULL);
  343. if (h == NULL)
  344. return (-1);
  345. /* If it's an end-of-archive mark, we can handle it. */
  346. if (h[0] == 0 && archive_block_is_null(h)) {
  347. /*
  348. * Usually, I bid the number of bits verified, but
  349. * in this case, 4096 seems excessive so I picked 10 as
  350. * an arbitrary but reasonable-seeming value.
  351. */
  352. return (10);
  353. }
  354. /* If it's not an end-of-archive mark, it must have a valid checksum.*/
  355. if (!checksum(a, h))
  356. return (0);
  357. bid += 48; /* Checksum is usually 6 octal digits. */
  358. header = (const struct archive_entry_header_ustar *)h;
  359. /* Recognize POSIX formats. */
  360. if ((memcmp(header->magic, "ustar\0", 6) == 0)
  361. && (memcmp(header->version, "00", 2) == 0))
  362. bid += 56;
  363. /* Recognize GNU tar format. */
  364. if ((memcmp(header->magic, "ustar ", 6) == 0)
  365. && (memcmp(header->version, " \0", 2) == 0))
  366. bid += 56;
  367. /* Type flag must be null, digit or A-Z, a-z. */
  368. if (header->typeflag[0] != 0 &&
  369. !( header->typeflag[0] >= '0' && header->typeflag[0] <= '9') &&
  370. !( header->typeflag[0] >= 'A' && header->typeflag[0] <= 'Z') &&
  371. !( header->typeflag[0] >= 'a' && header->typeflag[0] <= 'z') )
  372. return (0);
  373. bid += 2; /* 6 bits of variation in an 8-bit field leaves 2 bits. */
  374. /*
  375. * Check format of mode/uid/gid/mtime/size/rdevmajor/rdevminor fields.
  376. */
  377. if (validate_number_field(header->mode, sizeof(header->mode)) == 0
  378. || validate_number_field(header->uid, sizeof(header->uid)) == 0
  379. || validate_number_field(header->gid, sizeof(header->gid)) == 0
  380. || validate_number_field(header->mtime, sizeof(header->mtime)) == 0
  381. || validate_number_field(header->size, sizeof(header->size)) == 0
  382. || validate_number_field(header->rdevmajor, sizeof(header->rdevmajor)) == 0
  383. || validate_number_field(header->rdevminor, sizeof(header->rdevminor)) == 0) {
  384. bid = 0;
  385. }
  386. return (bid);
  387. }
  388. static int
  389. archive_read_format_tar_options(struct archive_read *a,
  390. const char *key, const char *val)
  391. {
  392. struct tar *tar;
  393. int ret = ARCHIVE_FAILED;
  394. tar = (struct tar *)(a->format->data);
  395. if (strcmp(key, "compat-2x") == 0) {
  396. /* Handle UTF-8 filenames as libarchive 2.x */
  397. tar->compat_2x = (val != NULL && val[0] != 0);
  398. tar->init_default_conversion = tar->compat_2x;
  399. return (ARCHIVE_OK);
  400. } else if (strcmp(key, "hdrcharset") == 0) {
  401. if (val == NULL || val[0] == 0)
  402. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  403. "tar: hdrcharset option needs a character-set name");
  404. else {
  405. tar->opt_sconv =
  406. archive_string_conversion_from_charset(
  407. &a->archive, val, 0);
  408. if (tar->opt_sconv != NULL)
  409. ret = ARCHIVE_OK;
  410. else
  411. ret = ARCHIVE_FATAL;
  412. }
  413. return (ret);
  414. } else if (strcmp(key, "mac-ext") == 0) {
  415. tar->process_mac_extensions = (val != NULL && val[0] != 0);
  416. return (ARCHIVE_OK);
  417. } else if (strcmp(key, "read_concatenated_archives") == 0) {
  418. tar->read_concatenated_archives = (val != NULL && val[0] != 0);
  419. return (ARCHIVE_OK);
  420. }
  421. /* Note: The "warn" return is just to inform the options
  422. * supervisor that we didn't handle it. It will generate
  423. * a suitable error if no one used this option. */
  424. return (ARCHIVE_WARN);
  425. }
  426. /* utility function- this exists to centralize the logic of tracking
  427. * how much unconsumed data we have floating around, and to consume
  428. * anything outstanding since we're going to do read_aheads
  429. */
  430. static void
  431. tar_flush_unconsumed(struct archive_read *a, size_t *unconsumed)
  432. {
  433. if (*unconsumed) {
  434. /*
  435. void *data = (void *)__archive_read_ahead(a, *unconsumed, NULL);
  436. * this block of code is to poison claimed unconsumed space, ensuring
  437. * things break if it is in use still.
  438. * currently it WILL break things, so enable it only for debugging this issue
  439. if (data) {
  440. memset(data, 0xff, *unconsumed);
  441. }
  442. */
  443. __archive_read_consume(a, *unconsumed);
  444. *unconsumed = 0;
  445. }
  446. }
  447. /*
  448. * The function invoked by archive_read_next_header(). This
  449. * just sets up a few things and then calls the internal
  450. * tar_read_header() function below.
  451. */
  452. static int
  453. archive_read_format_tar_read_header(struct archive_read *a,
  454. struct archive_entry *entry)
  455. {
  456. /*
  457. * When converting tar archives to cpio archives, it is
  458. * essential that each distinct file have a distinct inode
  459. * number. To simplify this, we keep a static count here to
  460. * assign fake dev/inode numbers to each tar entry. Note that
  461. * pax format archives may overwrite this with something more
  462. * useful.
  463. *
  464. * Ideally, we would track every file read from the archive so
  465. * that we could assign the same dev/ino pair to hardlinks,
  466. * but the memory required to store a complete lookup table is
  467. * probably not worthwhile just to support the relatively
  468. * obscure tar->cpio conversion case.
  469. */
  470. /* TODO: Move this into `struct tar` to avoid conflicts
  471. * when reading multiple archives */
  472. static int default_inode;
  473. static int default_dev;
  474. struct tar *tar;
  475. const char *p;
  476. const wchar_t *wp;
  477. int r;
  478. size_t l, unconsumed = 0;
  479. /* Assign default device/inode values. */
  480. archive_entry_set_dev(entry, 1 + default_dev); /* Don't use zero. */
  481. archive_entry_set_ino(entry, ++default_inode); /* Don't use zero. */
  482. /* Limit generated st_ino number to 16 bits. */
  483. if (default_inode >= 0xffff) {
  484. ++default_dev;
  485. default_inode = 0;
  486. }
  487. tar = (struct tar *)(a->format->data);
  488. tar->entry_offset = 0;
  489. gnu_clear_sparse_list(tar);
  490. tar->realsize = -1; /* Mark this as "unset" */
  491. tar->realsize_override = 0;
  492. /* Setup default string conversion. */
  493. tar->sconv = tar->opt_sconv;
  494. if (tar->sconv == NULL) {
  495. if (!tar->init_default_conversion) {
  496. tar->sconv_default =
  497. archive_string_default_conversion_for_read(&(a->archive));
  498. tar->init_default_conversion = 1;
  499. }
  500. tar->sconv = tar->sconv_default;
  501. }
  502. r = tar_read_header(a, tar, entry, &unconsumed);
  503. tar_flush_unconsumed(a, &unconsumed);
  504. /*
  505. * "non-sparse" files are really just sparse files with
  506. * a single block.
  507. */
  508. if (tar->sparse_list == NULL) {
  509. if (gnu_add_sparse_entry(a, tar, 0, tar->entry_bytes_remaining)
  510. != ARCHIVE_OK)
  511. return (ARCHIVE_FATAL);
  512. } else {
  513. struct sparse_block *sb;
  514. for (sb = tar->sparse_list; sb != NULL; sb = sb->next) {
  515. if (!sb->hole)
  516. archive_entry_sparse_add_entry(entry,
  517. sb->offset, sb->remaining);
  518. }
  519. }
  520. if (r == ARCHIVE_OK && archive_entry_filetype(entry) == AE_IFREG) {
  521. /*
  522. * "Regular" entry with trailing '/' is really
  523. * directory: This is needed for certain old tar
  524. * variants and even for some broken newer ones.
  525. */
  526. if ((wp = archive_entry_pathname_w(entry)) != NULL) {
  527. l = wcslen(wp);
  528. if (l > 0 && wp[l - 1] == L'/') {
  529. archive_entry_set_filetype(entry, AE_IFDIR);
  530. tar->entry_bytes_remaining = 0;
  531. tar->entry_padding = 0;
  532. }
  533. } else if ((p = archive_entry_pathname(entry)) != NULL) {
  534. l = strlen(p);
  535. if (l > 0 && p[l - 1] == '/') {
  536. archive_entry_set_filetype(entry, AE_IFDIR);
  537. tar->entry_bytes_remaining = 0;
  538. tar->entry_padding = 0;
  539. }
  540. }
  541. }
  542. return (r);
  543. }
  544. static int
  545. archive_read_format_tar_read_data(struct archive_read *a,
  546. const void **buff, size_t *size, int64_t *offset)
  547. {
  548. ssize_t bytes_read;
  549. struct tar *tar;
  550. struct sparse_block *p;
  551. tar = (struct tar *)(a->format->data);
  552. for (;;) {
  553. /* Remove exhausted entries from sparse list. */
  554. while (tar->sparse_list != NULL &&
  555. tar->sparse_list->remaining == 0) {
  556. p = tar->sparse_list;
  557. tar->sparse_list = p->next;
  558. free(p);
  559. }
  560. if (tar->entry_bytes_unconsumed) {
  561. __archive_read_consume(a, tar->entry_bytes_unconsumed);
  562. tar->entry_bytes_unconsumed = 0;
  563. }
  564. /* If we're at end of file, return EOF. */
  565. if (tar->sparse_list == NULL ||
  566. tar->entry_bytes_remaining == 0) {
  567. if (__archive_read_consume(a, tar->entry_padding) < 0)
  568. return (ARCHIVE_FATAL);
  569. tar->entry_padding = 0;
  570. *buff = NULL;
  571. *size = 0;
  572. *offset = tar->realsize;
  573. return (ARCHIVE_EOF);
  574. }
  575. *buff = __archive_read_ahead(a, 1, &bytes_read);
  576. if (*buff == NULL) {
  577. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  578. "Truncated tar archive"
  579. " detected while reading data");
  580. return (ARCHIVE_FATAL);
  581. }
  582. if (bytes_read > tar->entry_bytes_remaining)
  583. bytes_read = (ssize_t)tar->entry_bytes_remaining;
  584. /* Don't read more than is available in the
  585. * current sparse block. */
  586. if (tar->sparse_list->remaining < bytes_read)
  587. bytes_read = (ssize_t)tar->sparse_list->remaining;
  588. *size = bytes_read;
  589. *offset = tar->sparse_list->offset;
  590. tar->sparse_list->remaining -= bytes_read;
  591. tar->sparse_list->offset += bytes_read;
  592. tar->entry_bytes_remaining -= bytes_read;
  593. tar->entry_bytes_unconsumed = bytes_read;
  594. if (!tar->sparse_list->hole)
  595. return (ARCHIVE_OK);
  596. /* Current is hole data and skip this. */
  597. }
  598. }
  599. static int
  600. archive_read_format_tar_skip(struct archive_read *a)
  601. {
  602. int64_t bytes_skipped;
  603. int64_t request;
  604. struct sparse_block *p;
  605. struct tar* tar;
  606. tar = (struct tar *)(a->format->data);
  607. /* Do not consume the hole of a sparse file. */
  608. request = 0;
  609. for (p = tar->sparse_list; p != NULL; p = p->next) {
  610. if (!p->hole) {
  611. if (p->remaining >= INT64_MAX - request) {
  612. return ARCHIVE_FATAL;
  613. }
  614. request += p->remaining;
  615. }
  616. }
  617. if (request > tar->entry_bytes_remaining)
  618. request = tar->entry_bytes_remaining;
  619. request += tar->entry_padding + tar->entry_bytes_unconsumed;
  620. bytes_skipped = __archive_read_consume(a, request);
  621. if (bytes_skipped < 0)
  622. return (ARCHIVE_FATAL);
  623. tar->entry_bytes_remaining = 0;
  624. tar->entry_bytes_unconsumed = 0;
  625. tar->entry_padding = 0;
  626. /* Free the sparse list. */
  627. gnu_clear_sparse_list(tar);
  628. return (ARCHIVE_OK);
  629. }
  630. /*
  631. * This function reads and interprets all of the headers associated
  632. * with a single entry.
  633. */
  634. static int
  635. tar_read_header(struct archive_read *a, struct tar *tar,
  636. struct archive_entry *entry, size_t *unconsumed)
  637. {
  638. ssize_t bytes;
  639. int err = ARCHIVE_OK, err2;
  640. int eof_fatal = 0; /* EOF is okay at some points... */
  641. const char *h;
  642. const struct archive_entry_header_ustar *header;
  643. const struct archive_entry_header_gnutar *gnuheader;
  644. /* Bitmask of what header types we've seen. */
  645. int32_t seen_headers = 0;
  646. static const int32_t seen_A_header = 1;
  647. static const int32_t seen_g_header = 2;
  648. static const int32_t seen_K_header = 4;
  649. static const int32_t seen_L_header = 8;
  650. static const int32_t seen_V_header = 16;
  651. static const int32_t seen_x_header = 32; /* Also X */
  652. static const int32_t seen_mac_metadata = 512;
  653. tar->pax_hdrcharset_utf8 = 1;
  654. tar->sparse_gnu_attributes_seen = 0;
  655. archive_string_empty(&(tar->entry_gname));
  656. archive_string_empty(&(tar->entry_pathname));
  657. archive_string_empty(&(tar->entry_pathname_override));
  658. archive_string_empty(&(tar->entry_uname));
  659. archive_string_empty(&tar->entry_linkpath);
  660. /* Ensure format is set. */
  661. if (a->archive.archive_format_name == NULL) {
  662. a->archive.archive_format = ARCHIVE_FORMAT_TAR;
  663. a->archive.archive_format_name = "tar";
  664. }
  665. /*
  666. * TODO: Write global/default pax options into
  667. * 'entry' struct here before overwriting with
  668. * file-specific options.
  669. */
  670. /* Loop over all the headers needed for the next entry */
  671. for (;;) {
  672. /* Find the next valid header record. */
  673. while (1) {
  674. tar_flush_unconsumed(a, unconsumed);
  675. /* Read 512-byte header record */
  676. h = __archive_read_ahead(a, 512, &bytes);
  677. if (bytes == 0) { /* EOF at a block boundary. */
  678. if (eof_fatal) {
  679. /* We've read a special header already;
  680. * if there's no regular header, then this is
  681. * a premature EOF. */
  682. archive_set_error(&a->archive, EINVAL,
  683. "Damaged tar archive");
  684. return (ARCHIVE_FATAL);
  685. } else {
  686. return (ARCHIVE_EOF);
  687. }
  688. }
  689. if (h == NULL) { /* Short block at EOF; this is bad. */
  690. archive_set_error(&a->archive,
  691. ARCHIVE_ERRNO_FILE_FORMAT,
  692. "Truncated tar archive"
  693. " detected while reading next heaader");
  694. return (ARCHIVE_FATAL);
  695. }
  696. *unconsumed += 512;
  697. if (h[0] == 0 && archive_block_is_null(h)) {
  698. /* We found a NULL block which indicates end-of-archive */
  699. if (tar->read_concatenated_archives) {
  700. /* We're ignoring NULL blocks, so keep going. */
  701. continue;
  702. }
  703. /* Try to consume a second all-null record, as well. */
  704. /* If we can't, that's okay. */
  705. tar_flush_unconsumed(a, unconsumed);
  706. h = __archive_read_ahead(a, 512, NULL);
  707. if (h != NULL && h[0] == 0 && archive_block_is_null(h))
  708. __archive_read_consume(a, 512);
  709. archive_clear_error(&a->archive);
  710. return (ARCHIVE_EOF);
  711. }
  712. /* This is NOT a null block, so it must be a valid header. */
  713. if (!checksum(a, h)) {
  714. tar_flush_unconsumed(a, unconsumed);
  715. archive_set_error(&a->archive, EINVAL, "Damaged tar archive");
  716. /* If we've read some critical information (pax headers, etc)
  717. * and _then_ see a bad header, we can't really recover. */
  718. if (eof_fatal) {
  719. return (ARCHIVE_FATAL);
  720. } else {
  721. return (ARCHIVE_RETRY);
  722. }
  723. }
  724. break;
  725. }
  726. /* Determine the format variant. */
  727. header = (const struct archive_entry_header_ustar *)h;
  728. switch(header->typeflag[0]) {
  729. case 'A': /* Solaris tar ACL */
  730. if (seen_headers & seen_A_header) {
  731. return (ARCHIVE_FATAL);
  732. }
  733. seen_headers |= seen_A_header;
  734. a->archive.archive_format = ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE;
  735. a->archive.archive_format_name = "Solaris tar";
  736. err2 = header_Solaris_ACL(a, tar, entry, h, unconsumed);
  737. break;
  738. case 'g': /* POSIX-standard 'g' header. */
  739. if (seen_headers & seen_g_header) {
  740. return (ARCHIVE_FATAL);
  741. }
  742. seen_headers |= seen_g_header;
  743. a->archive.archive_format = ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE;
  744. a->archive.archive_format_name = "POSIX pax interchange format";
  745. err2 = header_pax_global(a, tar, entry, h, unconsumed);
  746. break;
  747. case 'K': /* Long link name (GNU tar, others) */
  748. if (seen_headers & seen_K_header) {
  749. return (ARCHIVE_FATAL);
  750. }
  751. seen_headers |= seen_K_header;
  752. err2 = header_gnu_longlink(a, tar, entry, h, unconsumed);
  753. break;
  754. case 'L': /* Long filename (GNU tar, others) */
  755. if (seen_headers & seen_L_header) {
  756. return (ARCHIVE_FATAL);
  757. }
  758. seen_headers |= seen_L_header;
  759. err2 = header_gnu_longname(a, tar, entry, h, unconsumed);
  760. break;
  761. case 'V': /* GNU volume header */
  762. if (seen_headers & seen_V_header) {
  763. return (ARCHIVE_FATAL);
  764. }
  765. seen_headers |= seen_V_header;
  766. err2 = header_volume(a, tar, entry, h, unconsumed);
  767. break;
  768. case 'X': /* Used by SUN tar; same as 'x'. */
  769. if (seen_headers & seen_x_header) {
  770. return (ARCHIVE_FATAL);
  771. }
  772. seen_headers |= seen_x_header;
  773. a->archive.archive_format = ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE;
  774. a->archive.archive_format_name =
  775. "POSIX pax interchange format (Sun variant)";
  776. err2 = header_pax_extension(a, tar, entry, h, unconsumed);
  777. break;
  778. case 'x': /* POSIX-standard 'x' header. */
  779. if (seen_headers & seen_x_header) {
  780. return (ARCHIVE_FATAL);
  781. }
  782. seen_headers |= seen_x_header;
  783. a->archive.archive_format = ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE;
  784. a->archive.archive_format_name = "POSIX pax interchange format";
  785. err2 = header_pax_extension(a, tar, entry, h, unconsumed);
  786. break;
  787. default: /* Regular header: Legacy tar, GNU tar, or ustar */
  788. gnuheader = (const struct archive_entry_header_gnutar *)h;
  789. if (memcmp(gnuheader->magic, "ustar \0", 8) == 0) {
  790. a->archive.archive_format = ARCHIVE_FORMAT_TAR_GNUTAR;
  791. a->archive.archive_format_name = "GNU tar format";
  792. err2 = header_gnutar(a, tar, entry, h, unconsumed);
  793. } else if (memcmp(header->magic, "ustar", 5) == 0) {
  794. if (a->archive.archive_format != ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE) {
  795. a->archive.archive_format = ARCHIVE_FORMAT_TAR_USTAR;
  796. a->archive.archive_format_name = "POSIX ustar format";
  797. }
  798. err2 = header_ustar(a, tar, entry, h);
  799. } else {
  800. a->archive.archive_format = ARCHIVE_FORMAT_TAR;
  801. a->archive.archive_format_name = "tar (non-POSIX)";
  802. err2 = header_old_tar(a, tar, entry, h);
  803. }
  804. err = err_combine(err, err2);
  805. /* We return warnings or success as-is. Anything else is fatal. */
  806. if (err < ARCHIVE_WARN) {
  807. return (ARCHIVE_FATAL);
  808. }
  809. /* Filename of the form `._filename` is an AppleDouble
  810. * extension entry. The body is the macOS metadata blob;
  811. * this is followed by another entry with the actual
  812. * regular file data.
  813. * This design has two drawbacks:
  814. * = it's brittle; you might just have a file with such a name
  815. * = it duplicates any long pathname extensions
  816. *
  817. * TODO: This probably shouldn't be here at all. Consider
  818. * just returning the contents as a regular entry here and
  819. * then dealing with it when we write data to disk.
  820. */
  821. if (tar->process_mac_extensions
  822. && ((seen_headers & seen_mac_metadata) == 0)
  823. && is_mac_metadata_entry(entry)) {
  824. err2 = read_mac_metadata_blob(a, entry, unconsumed);
  825. if (err2 < ARCHIVE_WARN) {
  826. return (ARCHIVE_FATAL);
  827. }
  828. err = err_combine(err, err2);
  829. /* Note: Other headers can appear again. */
  830. seen_headers = seen_mac_metadata;
  831. break;
  832. }
  833. /* Reconcile GNU sparse attributes */
  834. if (tar->sparse_gnu_attributes_seen) {
  835. /* Only 'S' (GNU sparse) and ustar '0' regular files can be sparse */
  836. if (tar->filetype != 'S' && tar->filetype != '0') {
  837. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  838. "Non-regular file cannot be sparse");
  839. return (ARCHIVE_WARN);
  840. } else if (tar->sparse_gnu_major == 0 &&
  841. tar->sparse_gnu_minor == 0) {
  842. /* Sparse map already parsed from 'x' header */
  843. } else if (tar->sparse_gnu_major == 0 &&
  844. tar->sparse_gnu_minor == 1) {
  845. /* Sparse map already parsed from 'x' header */
  846. } else if (tar->sparse_gnu_major == 1 &&
  847. tar->sparse_gnu_minor == 0) {
  848. /* Sparse map is prepended to file contents */
  849. ssize_t bytes_read;
  850. bytes_read = gnu_sparse_10_read(a, tar, unconsumed);
  851. if (bytes_read < 0)
  852. return ((int)bytes_read);
  853. tar->entry_bytes_remaining -= bytes_read;
  854. } else {
  855. archive_set_error(&a->archive,
  856. ARCHIVE_ERRNO_MISC,
  857. "Unrecognized GNU sparse file format");
  858. return (ARCHIVE_WARN);
  859. }
  860. }
  861. return (err);
  862. }
  863. /* We're between headers ... */
  864. err = err_combine(err, err2);
  865. if (err == ARCHIVE_FATAL)
  866. return (err);
  867. /* The GNU volume header and the pax `g` global header
  868. * are both allowed to be the only header in an
  869. * archive. If we've seen any other header, a
  870. * following EOF is fatal. */
  871. if ((seen_headers & ~seen_V_header & ~seen_g_header) != 0) {
  872. eof_fatal = 1;
  873. }
  874. }
  875. }
  876. /*
  877. * Return true if block checksum is correct.
  878. */
  879. static int
  880. checksum(struct archive_read *a, const void *h)
  881. {
  882. const unsigned char *bytes;
  883. const struct archive_entry_header_ustar *header;
  884. int check, sum;
  885. size_t i;
  886. (void)a; /* UNUSED */
  887. bytes = (const unsigned char *)h;
  888. header = (const struct archive_entry_header_ustar *)h;
  889. /* Checksum field must hold an octal number */
  890. for (i = 0; i < sizeof(header->checksum); ++i) {
  891. char c = header->checksum[i];
  892. if (c != ' ' && c != '\0' && (c < '0' || c > '7'))
  893. return 0;
  894. }
  895. /*
  896. * Test the checksum. Note that POSIX specifies _unsigned_
  897. * bytes for this calculation.
  898. */
  899. sum = (int)tar_atol(header->checksum, sizeof(header->checksum));
  900. check = 0;
  901. for (i = 0; i < 148; i++)
  902. check += (unsigned char)bytes[i];
  903. for (; i < 156; i++)
  904. check += 32;
  905. for (; i < 512; i++)
  906. check += (unsigned char)bytes[i];
  907. if (sum == check)
  908. return (1);
  909. /*
  910. * Repeat test with _signed_ bytes, just in case this archive
  911. * was created by an old BSD, Solaris, or HP-UX tar with a
  912. * broken checksum calculation.
  913. */
  914. check = 0;
  915. for (i = 0; i < 148; i++)
  916. check += (signed char)bytes[i];
  917. for (; i < 156; i++)
  918. check += 32;
  919. for (; i < 512; i++)
  920. check += (signed char)bytes[i];
  921. if (sum == check)
  922. return (1);
  923. #if DONT_FAIL_ON_CRC_ERROR
  924. /* Speed up fuzzing by pretending the checksum is always right. */
  925. return (1);
  926. #else
  927. return (0);
  928. #endif
  929. }
  930. /*
  931. * Return true if this block contains only nulls.
  932. */
  933. static int
  934. archive_block_is_null(const char *p)
  935. {
  936. unsigned i;
  937. for (i = 0; i < 512; i++)
  938. if (*p++)
  939. return (0);
  940. return (1);
  941. }
  942. /*
  943. * Interpret 'A' Solaris ACL header
  944. */
  945. static int
  946. header_Solaris_ACL(struct archive_read *a, struct tar *tar,
  947. struct archive_entry *entry, const void *h, size_t *unconsumed)
  948. {
  949. const struct archive_entry_header_ustar *header;
  950. struct archive_string acl_text;
  951. size_t size;
  952. int err, acl_type;
  953. int64_t type;
  954. char *acl, *p;
  955. header = (const struct archive_entry_header_ustar *)h;
  956. size = (size_t)tar_atol(header->size, sizeof(header->size));
  957. archive_string_init(&acl_text);
  958. err = read_body_to_string(a, tar, &acl_text, h, unconsumed);
  959. if (err != ARCHIVE_OK) {
  960. archive_string_free(&acl_text);
  961. return (err);
  962. }
  963. /* TODO: Examine the first characters to see if this
  964. * is an AIX ACL descriptor. We'll likely never support
  965. * them, but it would be polite to recognize and warn when
  966. * we do see them. */
  967. /* Leading octal number indicates ACL type and number of entries. */
  968. p = acl = acl_text.s;
  969. type = 0;
  970. while (*p != '\0' && p < acl + size) {
  971. if (*p < '0' || *p > '7') {
  972. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  973. "Malformed Solaris ACL attribute (invalid digit)");
  974. archive_string_free(&acl_text);
  975. return(ARCHIVE_WARN);
  976. }
  977. type <<= 3;
  978. type += *p - '0';
  979. if (type > 077777777) {
  980. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  981. "Malformed Solaris ACL attribute (count too large)");
  982. archive_string_free(&acl_text);
  983. return (ARCHIVE_WARN);
  984. }
  985. p++;
  986. }
  987. switch ((int)type & ~0777777) {
  988. case 01000000:
  989. /* POSIX.1e ACL */
  990. acl_type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
  991. break;
  992. case 03000000:
  993. /* NFSv4 ACL */
  994. acl_type = ARCHIVE_ENTRY_ACL_TYPE_NFS4;
  995. break;
  996. default:
  997. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  998. "Malformed Solaris ACL attribute (unsupported type %o)",
  999. (int)type);
  1000. archive_string_free(&acl_text);
  1001. return (ARCHIVE_WARN);
  1002. }
  1003. p++;
  1004. if (p >= acl + size) {
  1005. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1006. "Malformed Solaris ACL attribute (body overflow)");
  1007. archive_string_free(&acl_text);
  1008. return(ARCHIVE_WARN);
  1009. }
  1010. /* ACL text is null-terminated; find the end. */
  1011. size -= (p - acl);
  1012. acl = p;
  1013. while (*p != '\0' && p < acl + size)
  1014. p++;
  1015. if (tar->sconv_acl == NULL) {
  1016. tar->sconv_acl = archive_string_conversion_from_charset(
  1017. &(a->archive), "UTF-8", 1);
  1018. if (tar->sconv_acl == NULL) {
  1019. archive_string_free(&acl_text);
  1020. return (ARCHIVE_FATAL);
  1021. }
  1022. }
  1023. archive_strncpy(&(tar->localname), acl, p - acl);
  1024. err = archive_acl_from_text_l(archive_entry_acl(entry),
  1025. tar->localname.s, acl_type, tar->sconv_acl);
  1026. /* Workaround: Force perm_is_set() to be correct */
  1027. /* If this bit were stored in the ACL, this wouldn't be needed */
  1028. archive_entry_set_perm(entry, archive_entry_perm(entry));
  1029. if (err != ARCHIVE_OK) {
  1030. if (errno == ENOMEM) {
  1031. archive_set_error(&a->archive, ENOMEM,
  1032. "Can't allocate memory for ACL");
  1033. } else
  1034. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1035. "Malformed Solaris ACL attribute (unparsable)");
  1036. }
  1037. archive_string_free(&acl_text);
  1038. return (err);
  1039. }
  1040. /*
  1041. * Interpret 'K' long linkname header.
  1042. */
  1043. static int
  1044. header_gnu_longlink(struct archive_read *a, struct tar *tar,
  1045. struct archive_entry *entry, const void *h, size_t *unconsumed)
  1046. {
  1047. int err;
  1048. struct archive_string linkpath;
  1049. archive_string_init(&linkpath);
  1050. err = read_body_to_string(a, tar, &linkpath, h, unconsumed);
  1051. archive_entry_set_link(entry, linkpath.s);
  1052. archive_string_free(&linkpath);
  1053. return (err);
  1054. }
  1055. static int
  1056. set_conversion_failed_error(struct archive_read *a,
  1057. struct archive_string_conv *sconv, const char *name)
  1058. {
  1059. if (errno == ENOMEM) {
  1060. archive_set_error(&a->archive, ENOMEM,
  1061. "Can't allocate memory for %s", name);
  1062. return (ARCHIVE_FATAL);
  1063. }
  1064. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1065. "%s can't be converted from %s to current locale.",
  1066. name, archive_string_conversion_charset_name(sconv));
  1067. return (ARCHIVE_WARN);
  1068. }
  1069. /*
  1070. * Interpret 'L' long filename header.
  1071. */
  1072. static int
  1073. header_gnu_longname(struct archive_read *a, struct tar *tar,
  1074. struct archive_entry *entry, const void *h, size_t *unconsumed)
  1075. {
  1076. int err;
  1077. struct archive_string longname;
  1078. archive_string_init(&longname);
  1079. err = read_body_to_string(a, tar, &longname, h, unconsumed);
  1080. if (err == ARCHIVE_OK) {
  1081. if (archive_entry_copy_pathname_l(entry, longname.s,
  1082. archive_strlen(&longname), tar->sconv) != 0)
  1083. err = set_conversion_failed_error(a, tar->sconv, "Pathname");
  1084. }
  1085. archive_string_free(&longname);
  1086. return (err);
  1087. }
  1088. /*
  1089. * Interpret 'V' GNU tar volume header.
  1090. */
  1091. static int
  1092. header_volume(struct archive_read *a, struct tar *tar,
  1093. struct archive_entry *entry, const void *h, size_t *unconsumed)
  1094. {
  1095. const struct archive_entry_header_ustar *header;
  1096. int64_t size, to_consume;
  1097. (void)a; /* UNUSED */
  1098. (void)tar; /* UNUSED */
  1099. (void)entry; /* UNUSED */
  1100. header = (const struct archive_entry_header_ustar *)h;
  1101. size = tar_atol(header->size, sizeof(header->size));
  1102. if (size > (int64_t)pathname_limit) {
  1103. return (ARCHIVE_FATAL);
  1104. }
  1105. to_consume = ((size + 511) & ~511);
  1106. *unconsumed += to_consume;
  1107. return (ARCHIVE_OK);
  1108. }
  1109. /*
  1110. * Read the next `size` bytes into the provided string.
  1111. * Null-terminate the string.
  1112. */
  1113. static int
  1114. read_bytes_to_string(struct archive_read *a,
  1115. struct archive_string *as, size_t size,
  1116. size_t *unconsumed) {
  1117. const void *src;
  1118. /* Fail if we can't make our buffer big enough. */
  1119. if (archive_string_ensure(as, (size_t)size+1) == NULL) {
  1120. archive_set_error(&a->archive, ENOMEM,
  1121. "No memory");
  1122. return (ARCHIVE_FATAL);
  1123. }
  1124. tar_flush_unconsumed(a, unconsumed);
  1125. /* Read the body into the string. */
  1126. src = __archive_read_ahead(a, size, NULL);
  1127. if (src == NULL) {
  1128. archive_set_error(&a->archive, EINVAL,
  1129. "Truncated archive"
  1130. " detected while reading metadata");
  1131. *unconsumed = 0;
  1132. return (ARCHIVE_FATAL);
  1133. }
  1134. memcpy(as->s, src, (size_t)size);
  1135. as->s[size] = '\0';
  1136. as->length = (size_t)size;
  1137. *unconsumed += size;
  1138. return (ARCHIVE_OK);
  1139. }
  1140. /*
  1141. * Read body of an archive entry into an archive_string object.
  1142. */
  1143. static int
  1144. read_body_to_string(struct archive_read *a, struct tar *tar,
  1145. struct archive_string *as, const void *h, size_t *unconsumed)
  1146. {
  1147. int64_t size;
  1148. const struct archive_entry_header_ustar *header;
  1149. int r;
  1150. (void)tar; /* UNUSED */
  1151. header = (const struct archive_entry_header_ustar *)h;
  1152. size = tar_atol(header->size, sizeof(header->size));
  1153. if (size > entry_limit) {
  1154. return (ARCHIVE_FATAL);
  1155. }
  1156. if ((size > (int64_t)pathname_limit) || (size < 0)) {
  1157. archive_string_empty(as);
  1158. int64_t to_consume = ((size + 511) & ~511);
  1159. if (to_consume != __archive_read_consume(a, to_consume)) {
  1160. return (ARCHIVE_FATAL);
  1161. }
  1162. archive_set_error(&a->archive, EINVAL,
  1163. "Special header too large: %d > 1MiB",
  1164. (int)size);
  1165. return (ARCHIVE_WARN);
  1166. }
  1167. r = read_bytes_to_string(a, as, size, unconsumed);
  1168. *unconsumed += 0x1ff & (-size);
  1169. return(r);
  1170. }
  1171. /*
  1172. * Parse out common header elements.
  1173. *
  1174. * This would be the same as header_old_tar, except that the
  1175. * filename is handled slightly differently for old and POSIX
  1176. * entries (POSIX entries support a 'prefix'). This factoring
  1177. * allows header_old_tar and header_ustar
  1178. * to handle filenames differently, while still putting most of the
  1179. * common parsing into one place.
  1180. */
  1181. static int
  1182. header_common(struct archive_read *a, struct tar *tar,
  1183. struct archive_entry *entry, const void *h)
  1184. {
  1185. const struct archive_entry_header_ustar *header;
  1186. const char *existing_linkpath;
  1187. const wchar_t *existing_wcs_linkpath;
  1188. int err = ARCHIVE_OK;
  1189. header = (const struct archive_entry_header_ustar *)h;
  1190. /* Parse out the numeric fields (all are octal) */
  1191. /* Split mode handling: Set filetype always, perm only if not already set */
  1192. archive_entry_set_filetype(entry,
  1193. (mode_t)tar_atol(header->mode, sizeof(header->mode)));
  1194. if (!archive_entry_perm_is_set(entry)) {
  1195. archive_entry_set_perm(entry,
  1196. (mode_t)tar_atol(header->mode, sizeof(header->mode)));
  1197. }
  1198. if (!archive_entry_uid_is_set(entry)) {
  1199. archive_entry_set_uid(entry, tar_atol(header->uid, sizeof(header->uid)));
  1200. }
  1201. if (!archive_entry_gid_is_set(entry)) {
  1202. archive_entry_set_gid(entry, tar_atol(header->gid, sizeof(header->gid)));
  1203. }
  1204. tar->entry_bytes_remaining = tar_atol(header->size, sizeof(header->size));
  1205. if (tar->entry_bytes_remaining < 0) {
  1206. tar->entry_bytes_remaining = 0;
  1207. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1208. "Tar entry has negative size");
  1209. return (ARCHIVE_FATAL);
  1210. }
  1211. if (tar->entry_bytes_remaining > entry_limit) {
  1212. tar->entry_bytes_remaining = 0;
  1213. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1214. "Tar entry size overflow");
  1215. return (ARCHIVE_FATAL);
  1216. }
  1217. if (!tar->realsize_override) {
  1218. tar->realsize = tar->entry_bytes_remaining;
  1219. }
  1220. archive_entry_set_size(entry, tar->realsize);
  1221. if (!archive_entry_mtime_is_set(entry)) {
  1222. archive_entry_set_mtime(entry, tar_atol(header->mtime, sizeof(header->mtime)), 0);
  1223. }
  1224. /* Handle the tar type flag appropriately. */
  1225. tar->filetype = header->typeflag[0];
  1226. /*
  1227. * TODO: If the linkpath came from Pax extension header, then
  1228. * we should obey the hdrcharset_utf8 flag when converting these.
  1229. */
  1230. switch (tar->filetype) {
  1231. case '1': /* Hard link */
  1232. archive_entry_set_link_to_hardlink(entry);
  1233. existing_wcs_linkpath = archive_entry_hardlink_w(entry);
  1234. existing_linkpath = archive_entry_hardlink(entry);
  1235. if ((existing_linkpath == NULL || existing_linkpath[0] == '\0')
  1236. && (existing_wcs_linkpath == NULL || existing_wcs_linkpath[0] == '\0')) {
  1237. struct archive_string linkpath;
  1238. archive_string_init(&linkpath);
  1239. archive_strncpy(&linkpath,
  1240. header->linkname, sizeof(header->linkname));
  1241. if (archive_entry_copy_hardlink_l(entry, linkpath.s,
  1242. archive_strlen(&linkpath), tar->sconv) != 0) {
  1243. err = set_conversion_failed_error(a, tar->sconv,
  1244. "Linkname");
  1245. if (err == ARCHIVE_FATAL) {
  1246. archive_string_free(&linkpath);
  1247. return (err);
  1248. }
  1249. }
  1250. archive_string_free(&linkpath);
  1251. }
  1252. /*
  1253. * The following may seem odd, but: Technically, tar
  1254. * does not store the file type for a "hard link"
  1255. * entry, only the fact that it is a hard link. So, I
  1256. * leave the type zero normally. But, pax interchange
  1257. * format allows hard links to have data, which
  1258. * implies that the underlying entry is a regular
  1259. * file.
  1260. */
  1261. if (archive_entry_size(entry) > 0)
  1262. archive_entry_set_filetype(entry, AE_IFREG);
  1263. /*
  1264. * A tricky point: Traditionally, tar readers have
  1265. * ignored the size field when reading hardlink
  1266. * entries, and some writers put non-zero sizes even
  1267. * though the body is empty. POSIX blessed this
  1268. * convention in the 1988 standard, but broke with
  1269. * this tradition in 2001 by permitting hardlink
  1270. * entries to store valid bodies in pax interchange
  1271. * format, but not in ustar format. Since there is no
  1272. * hard and fast way to distinguish pax interchange
  1273. * from earlier archives (the 'x' and 'g' entries are
  1274. * optional, after all), we need a heuristic.
  1275. */
  1276. if (archive_entry_size(entry) == 0) {
  1277. /* If the size is already zero, we're done. */
  1278. } else if (a->archive.archive_format
  1279. == ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE) {
  1280. /* Definitely pax extended; must obey hardlink size. */
  1281. } else if (a->archive.archive_format == ARCHIVE_FORMAT_TAR
  1282. || a->archive.archive_format == ARCHIVE_FORMAT_TAR_GNUTAR)
  1283. {
  1284. /* Old-style or GNU tar: we must ignore the size. */
  1285. archive_entry_set_size(entry, 0);
  1286. tar->entry_bytes_remaining = 0;
  1287. } else if (archive_read_format_tar_bid(a, 50) > 50) {
  1288. /*
  1289. * We don't know if it's pax: If the bid
  1290. * function sees a valid ustar header
  1291. * immediately following, then let's ignore
  1292. * the hardlink size.
  1293. */
  1294. archive_entry_set_size(entry, 0);
  1295. tar->entry_bytes_remaining = 0;
  1296. }
  1297. /*
  1298. * TODO: There are still two cases I'd like to handle:
  1299. * = a ustar non-pax archive with a hardlink entry at
  1300. * end-of-archive. (Look for block of nulls following?)
  1301. * = a pax archive that has not seen any pax headers
  1302. * and has an entry which is a hardlink entry storing
  1303. * a body containing an uncompressed tar archive.
  1304. * The first is worth addressing; I don't see any reliable
  1305. * way to deal with the second possibility.
  1306. */
  1307. break;
  1308. case '2': /* Symlink */
  1309. archive_entry_set_link_to_symlink(entry);
  1310. existing_wcs_linkpath = archive_entry_symlink_w(entry);
  1311. existing_linkpath = archive_entry_symlink(entry);
  1312. if ((existing_linkpath == NULL || existing_linkpath[0] == '\0')
  1313. && (existing_wcs_linkpath == NULL || existing_wcs_linkpath[0] == '\0')) {
  1314. struct archive_string linkpath;
  1315. archive_string_init(&linkpath);
  1316. archive_strncpy(&linkpath,
  1317. header->linkname, sizeof(header->linkname));
  1318. if (archive_entry_copy_symlink_l(entry, linkpath.s,
  1319. archive_strlen(&linkpath), tar->sconv) != 0) {
  1320. err = set_conversion_failed_error(a, tar->sconv,
  1321. "Linkname");
  1322. if (err == ARCHIVE_FATAL) {
  1323. archive_string_free(&linkpath);
  1324. return (err);
  1325. }
  1326. }
  1327. archive_string_free(&linkpath);
  1328. }
  1329. archive_entry_set_filetype(entry, AE_IFLNK);
  1330. archive_entry_set_size(entry, 0);
  1331. tar->entry_bytes_remaining = 0;
  1332. break;
  1333. case '3': /* Character device */
  1334. archive_entry_set_filetype(entry, AE_IFCHR);
  1335. archive_entry_set_size(entry, 0);
  1336. tar->entry_bytes_remaining = 0;
  1337. break;
  1338. case '4': /* Block device */
  1339. archive_entry_set_filetype(entry, AE_IFBLK);
  1340. archive_entry_set_size(entry, 0);
  1341. tar->entry_bytes_remaining = 0;
  1342. break;
  1343. case '5': /* Dir */
  1344. archive_entry_set_filetype(entry, AE_IFDIR);
  1345. archive_entry_set_size(entry, 0);
  1346. tar->entry_bytes_remaining = 0;
  1347. break;
  1348. case '6': /* FIFO device */
  1349. archive_entry_set_filetype(entry, AE_IFIFO);
  1350. archive_entry_set_size(entry, 0);
  1351. tar->entry_bytes_remaining = 0;
  1352. break;
  1353. case 'D': /* GNU incremental directory type */
  1354. /*
  1355. * No special handling is actually required here.
  1356. * It might be nice someday to preprocess the file list and
  1357. * provide it to the client, though.
  1358. */
  1359. archive_entry_set_filetype(entry, AE_IFDIR);
  1360. break;
  1361. case 'M': /* GNU "Multi-volume" (remainder of file from last archive)*/
  1362. /*
  1363. * As far as I can tell, this is just like a regular file
  1364. * entry, except that the contents should be _appended_ to
  1365. * the indicated file at the indicated offset. This may
  1366. * require some API work to fully support.
  1367. */
  1368. break;
  1369. case 'N': /* Old GNU "long filename" entry. */
  1370. /* The body of this entry is a script for renaming
  1371. * previously-extracted entries. Ugh. It will never
  1372. * be supported by libarchive. */
  1373. archive_entry_set_filetype(entry, AE_IFREG);
  1374. break;
  1375. case 'S': /* GNU sparse files */
  1376. /*
  1377. * Sparse files are really just regular files with
  1378. * sparse information in the extended area.
  1379. */
  1380. /* FALLTHROUGH */
  1381. case '0': /* ustar "regular" file */
  1382. /* FALLTHROUGH */
  1383. default: /* Non-standard file types */
  1384. /*
  1385. * Per POSIX: non-recognized types should always be
  1386. * treated as regular files.
  1387. */
  1388. archive_entry_set_filetype(entry, AE_IFREG);
  1389. break;
  1390. }
  1391. return (err);
  1392. }
  1393. /*
  1394. * Parse out header elements for "old-style" tar archives.
  1395. */
  1396. static int
  1397. header_old_tar(struct archive_read *a, struct tar *tar,
  1398. struct archive_entry *entry, const void *h)
  1399. {
  1400. const struct archive_entry_header_ustar *header;
  1401. int err = ARCHIVE_OK, err2;
  1402. /*
  1403. * Copy filename over (to ensure null termination).
  1404. * Skip if pathname was already set e.g. by header_gnu_longname()
  1405. */
  1406. header = (const struct archive_entry_header_ustar *)h;
  1407. const char *existing_pathname = archive_entry_pathname(entry);
  1408. const wchar_t *existing_wcs_pathname = archive_entry_pathname_w(entry);
  1409. if ((existing_pathname == NULL || existing_pathname[0] == '\0')
  1410. && (existing_wcs_pathname == NULL || existing_wcs_pathname[0] == '\0') &&
  1411. archive_entry_copy_pathname_l(entry,
  1412. header->name, sizeof(header->name), tar->sconv) != 0) {
  1413. err = set_conversion_failed_error(a, tar->sconv, "Pathname");
  1414. if (err == ARCHIVE_FATAL)
  1415. return (err);
  1416. }
  1417. /* Grab rest of common fields */
  1418. err2 = header_common(a, tar, entry, h);
  1419. if (err > err2)
  1420. err = err2;
  1421. tar->entry_padding = 0x1ff & (-tar->entry_bytes_remaining);
  1422. return (err);
  1423. }
  1424. /*
  1425. * Is this likely an AppleDouble extension?
  1426. */
  1427. static int
  1428. is_mac_metadata_entry(struct archive_entry *entry) {
  1429. const char *p, *name;
  1430. const wchar_t *wp, *wname;
  1431. wname = wp = archive_entry_pathname_w(entry);
  1432. if (wp != NULL) {
  1433. /* Find the last path element. */
  1434. for (; *wp != L'\0'; ++wp) {
  1435. if (wp[0] == '/' && wp[1] != L'\0')
  1436. wname = wp + 1;
  1437. }
  1438. /*
  1439. * If last path element starts with "._", then
  1440. * this is a Mac extension.
  1441. */
  1442. if (wname[0] == L'.' && wname[1] == L'_' && wname[2] != L'\0')
  1443. return 1;
  1444. } else {
  1445. /* Find the last path element. */
  1446. name = p = archive_entry_pathname(entry);
  1447. if (p == NULL)
  1448. return (ARCHIVE_FAILED);
  1449. for (; *p != '\0'; ++p) {
  1450. if (p[0] == '/' && p[1] != '\0')
  1451. name = p + 1;
  1452. }
  1453. /*
  1454. * If last path element starts with "._", then
  1455. * this is a Mac extension.
  1456. */
  1457. if (name[0] == '.' && name[1] == '_' && name[2] != '\0')
  1458. return 1;
  1459. }
  1460. /* Not a mac extension */
  1461. return 0;
  1462. }
  1463. /*
  1464. * Read a Mac AppleDouble-encoded blob of file metadata,
  1465. * if there is one.
  1466. *
  1467. * TODO: In Libarchive 4, we should consider ripping this
  1468. * out -- instead, return a file starting with `._` as
  1469. * a regular file and let the client (or archive_write logic)
  1470. * handle it.
  1471. */
  1472. static int
  1473. read_mac_metadata_blob(struct archive_read *a,
  1474. struct archive_entry *entry, size_t *unconsumed)
  1475. {
  1476. int64_t size;
  1477. size_t msize;
  1478. const void *data;
  1479. /* Read the body as a Mac OS metadata blob. */
  1480. size = archive_entry_size(entry);
  1481. msize = (size_t)size;
  1482. if (size < 0 || (uintmax_t)msize != (uintmax_t)size) {
  1483. *unconsumed = 0;
  1484. return (ARCHIVE_FATAL);
  1485. }
  1486. /* TODO: Should this merely skip the overlarge entry and
  1487. * WARN? Or is xattr_limit sufficiently large that we can
  1488. * safely assume anything larger is malicious? */
  1489. if (size > (int64_t)xattr_limit) {
  1490. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1491. "Oversized AppleDouble extension has size %llu > %llu",
  1492. (unsigned long long)size,
  1493. (unsigned long long)xattr_limit);
  1494. return (ARCHIVE_FATAL);
  1495. }
  1496. /*
  1497. * TODO: Look beyond the body here to peek at the next header.
  1498. * If it's a regular header (not an extension header)
  1499. * that has the wrong name, just return the current
  1500. * entry as-is, without consuming the body here.
  1501. * That would reduce the risk of us mis-identifying
  1502. * an ordinary file that just happened to have
  1503. * a name starting with "._".
  1504. *
  1505. * Q: Is the above idea really possible? Even
  1506. * when there are GNU or pax extension entries?
  1507. */
  1508. tar_flush_unconsumed(a, unconsumed);
  1509. data = __archive_read_ahead(a, msize, NULL);
  1510. if (data == NULL) {
  1511. archive_set_error(&a->archive, EINVAL,
  1512. "Truncated archive"
  1513. " detected while reading macOS metadata");
  1514. *unconsumed = 0;
  1515. return (ARCHIVE_FATAL);
  1516. }
  1517. archive_entry_clear(entry);
  1518. archive_entry_copy_mac_metadata(entry, data, msize);
  1519. *unconsumed = (msize + 511) & ~ 511;
  1520. return (ARCHIVE_OK);
  1521. }
  1522. /*
  1523. * Parse a file header for a pax extended archive entry.
  1524. */
  1525. static int
  1526. header_pax_global(struct archive_read *a, struct tar *tar,
  1527. struct archive_entry *entry, const void *h, size_t *unconsumed)
  1528. {
  1529. const struct archive_entry_header_ustar *header;
  1530. int64_t size, to_consume;
  1531. (void)a; /* UNUSED */
  1532. (void)tar; /* UNUSED */
  1533. (void)entry; /* UNUSED */
  1534. header = (const struct archive_entry_header_ustar *)h;
  1535. size = tar_atol(header->size, sizeof(header->size));
  1536. if (size > entry_limit) {
  1537. return (ARCHIVE_FATAL);
  1538. }
  1539. to_consume = ((size + 511) & ~511);
  1540. *unconsumed += to_consume;
  1541. return (ARCHIVE_OK);
  1542. }
  1543. /*
  1544. * Parse a file header for a Posix "ustar" archive entry. This also
  1545. * handles "pax" or "extended ustar" entries.
  1546. *
  1547. * In order to correctly handle pax attributes (which precede this),
  1548. * we have to skip parsing any field for which the entry already has
  1549. * contents.
  1550. */
  1551. static int
  1552. header_ustar(struct archive_read *a, struct tar *tar,
  1553. struct archive_entry *entry, const void *h)
  1554. {
  1555. const struct archive_entry_header_ustar *header;
  1556. struct archive_string as;
  1557. int err = ARCHIVE_OK, r;
  1558. header = (const struct archive_entry_header_ustar *)h;
  1559. /* Copy name into an internal buffer to ensure null-termination. */
  1560. const char *existing_pathname = archive_entry_pathname(entry);
  1561. const wchar_t *existing_wcs_pathname = archive_entry_pathname_w(entry);
  1562. if ((existing_pathname == NULL || existing_pathname[0] == '\0')
  1563. && (existing_wcs_pathname == NULL || existing_wcs_pathname[0] == '\0')) {
  1564. archive_string_init(&as);
  1565. if (header->prefix[0]) {
  1566. archive_strncpy(&as, header->prefix, sizeof(header->prefix));
  1567. if (as.s[archive_strlen(&as) - 1] != '/')
  1568. archive_strappend_char(&as, '/');
  1569. archive_strncat(&as, header->name, sizeof(header->name));
  1570. } else {
  1571. archive_strncpy(&as, header->name, sizeof(header->name));
  1572. }
  1573. if (archive_entry_copy_pathname_l(entry, as.s, archive_strlen(&as),
  1574. tar->sconv) != 0) {
  1575. err = set_conversion_failed_error(a, tar->sconv, "Pathname");
  1576. if (err == ARCHIVE_FATAL)
  1577. return (err);
  1578. }
  1579. archive_string_free(&as);
  1580. }
  1581. /* Handle rest of common fields. */
  1582. r = header_common(a, tar, entry, h);
  1583. if (r == ARCHIVE_FATAL)
  1584. return (r);
  1585. if (r < err)
  1586. err = r;
  1587. /* Handle POSIX ustar fields. */
  1588. const char *existing_uname = archive_entry_uname(entry);
  1589. if (existing_uname == NULL || existing_uname[0] == '\0') {
  1590. if (archive_entry_copy_uname_l(entry,
  1591. header->uname, sizeof(header->uname), tar->sconv) != 0) {
  1592. err = set_conversion_failed_error(a, tar->sconv, "Uname");
  1593. if (err == ARCHIVE_FATAL)
  1594. return (err);
  1595. }
  1596. }
  1597. const char *existing_gname = archive_entry_gname(entry);
  1598. if (existing_gname == NULL || existing_gname[0] == '\0') {
  1599. if (archive_entry_copy_gname_l(entry,
  1600. header->gname, sizeof(header->gname), tar->sconv) != 0) {
  1601. err = set_conversion_failed_error(a, tar->sconv, "Gname");
  1602. if (err == ARCHIVE_FATAL)
  1603. return (err);
  1604. }
  1605. }
  1606. /* Parse out device numbers only for char and block specials. */
  1607. if (header->typeflag[0] == '3' || header->typeflag[0] == '4') {
  1608. if (!archive_entry_rdev_is_set(entry)) {
  1609. archive_entry_set_rdevmajor(entry, (dev_t)
  1610. tar_atol(header->rdevmajor, sizeof(header->rdevmajor)));
  1611. archive_entry_set_rdevminor(entry, (dev_t)
  1612. tar_atol(header->rdevminor, sizeof(header->rdevminor)));
  1613. }
  1614. } else {
  1615. archive_entry_set_rdev(entry, 0);
  1616. }
  1617. tar->entry_padding = 0x1ff & (-tar->entry_bytes_remaining);
  1618. return (err);
  1619. }
  1620. static int
  1621. header_pax_extension(struct archive_read *a, struct tar *tar,
  1622. struct archive_entry *entry, const void *h, size_t *unconsumed)
  1623. {
  1624. /* Sanity checks: The largest `x` body I've ever heard of was
  1625. * a little over 4MB. So I doubt there has ever been a
  1626. * well-formed archive with an `x` body over 1GiB. Similarly,
  1627. * it seems plausible that no single attribute has ever been
  1628. * larger than 100MB. So if we see a larger value here, it's
  1629. * almost certainly a sign of a corrupted/malicious archive. */
  1630. /* Maximum sane size for extension body: 1 GiB */
  1631. /* This cannot be raised to larger than 8GiB without
  1632. * exceeding the maximum size for a standard ustar
  1633. * entry. */
  1634. const int64_t ext_size_limit = 1024 * 1024 * (int64_t)1024;
  1635. /* Maximum size for a single line/attr: 100 million characters */
  1636. /* This cannot be raised to more than 2GiB without exceeding
  1637. * a `size_t` on 32-bit platforms. */
  1638. const size_t max_parsed_line_length = 99999999ULL;
  1639. /* Largest attribute prolog: size + name. */
  1640. const size_t max_size_name = 512;
  1641. /* Size and padding of the full extension body */
  1642. int64_t ext_size, ext_padding;
  1643. size_t line_length, value_length, name_length;
  1644. ssize_t to_read, did_read;
  1645. const struct archive_entry_header_ustar *header;
  1646. const char *p, *attr_start, *name_start;
  1647. struct archive_string_conv *sconv;
  1648. struct archive_string *pas = NULL;
  1649. struct archive_string attr_name;
  1650. int err = ARCHIVE_OK, r;
  1651. header = (const struct archive_entry_header_ustar *)h;
  1652. ext_size = tar_atol(header->size, sizeof(header->size));
  1653. if (ext_size > entry_limit) {
  1654. return (ARCHIVE_FATAL);
  1655. }
  1656. if (ext_size < 0) {
  1657. archive_set_error(&a->archive, EINVAL,
  1658. "pax extension header has invalid size: %lld",
  1659. (long long)ext_size);
  1660. return (ARCHIVE_FATAL);
  1661. }
  1662. ext_padding = 0x1ff & (-ext_size);
  1663. if (ext_size > ext_size_limit) {
  1664. /* Consume the pax extension body and return an error */
  1665. if (ext_size + ext_padding != __archive_read_consume(a, ext_size + ext_padding)) {
  1666. return (ARCHIVE_FATAL);
  1667. }
  1668. archive_set_error(&a->archive, EINVAL,
  1669. "Ignoring oversized pax extensions: %d > %d",
  1670. (int)ext_size, (int)ext_size_limit);
  1671. return (ARCHIVE_WARN);
  1672. }
  1673. tar_flush_unconsumed(a, unconsumed);
  1674. /* Parse the size/name of each pax attribute in the body */
  1675. archive_string_init(&attr_name);
  1676. while (ext_size > 0) {
  1677. /* Read enough bytes to parse the size/name of the next attribute */
  1678. to_read = max_size_name;
  1679. if (to_read > ext_size) {
  1680. to_read = ext_size;
  1681. }
  1682. p = __archive_read_ahead(a, to_read, &did_read);
  1683. if (p == NULL) { /* EOF */
  1684. archive_set_error(&a->archive, EINVAL,
  1685. "Truncated tar archive"
  1686. " detected while reading pax attribute name");
  1687. return (ARCHIVE_FATAL);
  1688. }
  1689. if (did_read > ext_size) {
  1690. did_read = ext_size;
  1691. }
  1692. /* Parse size of attribute */
  1693. line_length = 0;
  1694. attr_start = p;
  1695. while (1) {
  1696. if (p >= attr_start + did_read) {
  1697. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1698. "Ignoring malformed pax attributes: overlarge attribute size field");
  1699. *unconsumed += ext_size + ext_padding;
  1700. return (ARCHIVE_WARN);
  1701. }
  1702. if (*p == ' ') {
  1703. p++;
  1704. break;
  1705. }
  1706. if (*p < '0' || *p > '9') {
  1707. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1708. "Ignoring malformed pax attributes: malformed attribute size field");
  1709. *unconsumed += ext_size + ext_padding;
  1710. return (ARCHIVE_WARN);
  1711. }
  1712. line_length *= 10;
  1713. line_length += *p - '0';
  1714. if (line_length > max_parsed_line_length) {
  1715. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1716. "Ignoring malformed pax attribute: size > %lld",
  1717. (long long)max_parsed_line_length);
  1718. *unconsumed += ext_size + ext_padding;
  1719. return (ARCHIVE_WARN);
  1720. }
  1721. p++;
  1722. }
  1723. if ((int64_t)line_length > ext_size) {
  1724. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1725. "Ignoring malformed pax attribute: %lld > %lld",
  1726. (long long)line_length, (long long)ext_size);
  1727. *unconsumed += ext_size + ext_padding;
  1728. return (ARCHIVE_WARN);
  1729. }
  1730. /* Parse name of attribute */
  1731. if (p >= attr_start + did_read
  1732. || p >= attr_start + line_length
  1733. || *p == '=') {
  1734. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1735. "Ignoring malformed pax attributes: empty name found");
  1736. *unconsumed += ext_size + ext_padding;
  1737. return (ARCHIVE_WARN);
  1738. }
  1739. name_start = p;
  1740. while (1) {
  1741. if (p >= attr_start + did_read || p >= attr_start + line_length) {
  1742. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1743. "Ignoring malformed pax attributes: overlarge attribute name");
  1744. *unconsumed += ext_size + ext_padding;
  1745. return (ARCHIVE_WARN);
  1746. }
  1747. if (*p == '=') {
  1748. break;
  1749. }
  1750. p++;
  1751. }
  1752. name_length = p - name_start;
  1753. p++; // Skip '='
  1754. // Save the name before we consume it
  1755. archive_strncpy(&attr_name, name_start, name_length);
  1756. ext_size -= p - attr_start;
  1757. value_length = line_length - (p - attr_start);
  1758. /* Consume size, name, and `=` */
  1759. *unconsumed += p - attr_start;
  1760. tar_flush_unconsumed(a, unconsumed);
  1761. /* pax_attribute will consume value_length - 1 */
  1762. r = pax_attribute(a, tar, entry, attr_name.s, archive_strlen(&attr_name), value_length - 1, unconsumed);
  1763. ext_size -= value_length - 1;
  1764. // Release the allocated attr_name (either here or before every return in this function)
  1765. archive_string_free(&attr_name);
  1766. if (r < ARCHIVE_WARN) {
  1767. *unconsumed += ext_size + ext_padding;
  1768. return (r);
  1769. }
  1770. err = err_combine(err, r);
  1771. /* Consume the `\n` that follows the pax attribute value. */
  1772. tar_flush_unconsumed(a, unconsumed);
  1773. p = __archive_read_ahead(a, 1, &did_read);
  1774. if (p == NULL) {
  1775. archive_set_error(&a->archive, EINVAL,
  1776. "Truncated tar archive"
  1777. " detected while completing pax attribute");
  1778. return (ARCHIVE_FATAL);
  1779. }
  1780. if (p[0] != '\n') {
  1781. archive_set_error(&a->archive, EINVAL,
  1782. "Malformed pax attributes");
  1783. *unconsumed += ext_size + ext_padding;
  1784. return (ARCHIVE_WARN);
  1785. }
  1786. ext_size -= 1;
  1787. *unconsumed += 1;
  1788. tar_flush_unconsumed(a, unconsumed);
  1789. }
  1790. archive_string_free(&attr_name);
  1791. *unconsumed += ext_size + ext_padding;
  1792. /*
  1793. * Some PAX values -- pathname, linkpath, uname, gname --
  1794. * can't be copied into the entry until we know the character
  1795. * set to use:
  1796. */
  1797. if (!tar->pax_hdrcharset_utf8)
  1798. /* PAX specified "BINARY", so use the default charset */
  1799. sconv = tar->opt_sconv;
  1800. else {
  1801. /* PAX default UTF-8 */
  1802. sconv = archive_string_conversion_from_charset(
  1803. &(a->archive), "UTF-8", 1);
  1804. if (sconv == NULL)
  1805. return (ARCHIVE_FATAL);
  1806. if (tar->compat_2x)
  1807. archive_string_conversion_set_opt(sconv,
  1808. SCONV_SET_OPT_UTF8_LIBARCHIVE2X);
  1809. }
  1810. /* Pathname */
  1811. pas = NULL;
  1812. if (archive_strlen(&(tar->entry_pathname_override)) > 0) {
  1813. /* Prefer GNU.sparse.name attribute if present */
  1814. /* GNU sparse files store a fake name under the standard
  1815. * "pathname" key. */
  1816. pas = &(tar->entry_pathname_override);
  1817. } else if (archive_strlen(&(tar->entry_pathname)) > 0) {
  1818. /* Use standard "pathname" PAX extension */
  1819. pas = &(tar->entry_pathname);
  1820. }
  1821. if (pas != NULL) {
  1822. if (archive_entry_copy_pathname_l(entry, pas->s,
  1823. archive_strlen(pas), sconv) != 0) {
  1824. err = set_conversion_failed_error(a, sconv, "Pathname");
  1825. if (err == ARCHIVE_FATAL)
  1826. return (err);
  1827. /* Use raw name without conversion */
  1828. archive_entry_copy_pathname(entry, pas->s);
  1829. }
  1830. }
  1831. /* Uname */
  1832. if (archive_strlen(&(tar->entry_uname)) > 0) {
  1833. if (archive_entry_copy_uname_l(entry, tar->entry_uname.s,
  1834. archive_strlen(&(tar->entry_uname)), sconv) != 0) {
  1835. err = set_conversion_failed_error(a, sconv, "Uname");
  1836. if (err == ARCHIVE_FATAL)
  1837. return (err);
  1838. /* Use raw name without conversion */
  1839. archive_entry_copy_uname(entry, tar->entry_uname.s);
  1840. }
  1841. }
  1842. /* Gname */
  1843. if (archive_strlen(&(tar->entry_gname)) > 0) {
  1844. if (archive_entry_copy_gname_l(entry, tar->entry_gname.s,
  1845. archive_strlen(&(tar->entry_gname)), sconv) != 0) {
  1846. err = set_conversion_failed_error(a, sconv, "Gname");
  1847. if (err == ARCHIVE_FATAL)
  1848. return (err);
  1849. /* Use raw name without conversion */
  1850. archive_entry_copy_gname(entry, tar->entry_gname.s);
  1851. }
  1852. }
  1853. /* Linkpath */
  1854. if (archive_strlen(&(tar->entry_linkpath)) > 0) {
  1855. if (archive_entry_copy_link_l(entry, tar->entry_linkpath.s,
  1856. archive_strlen(&(tar->entry_linkpath)), sconv) != 0) {
  1857. err = set_conversion_failed_error(a, sconv, "Linkpath");
  1858. if (err == ARCHIVE_FATAL)
  1859. return (err);
  1860. /* Use raw name without conversion */
  1861. archive_entry_copy_link(entry, tar->entry_linkpath.s);
  1862. }
  1863. }
  1864. /* Extension may have given us a corrected `entry_bytes_remaining` for
  1865. * the main entry; update the padding appropriately. */
  1866. tar->entry_padding = 0x1ff & (-tar->entry_bytes_remaining);
  1867. return (err);
  1868. }
  1869. static int
  1870. pax_attribute_LIBARCHIVE_xattr(struct archive_entry *entry,
  1871. const char *name, size_t name_length, const char *value, size_t value_length)
  1872. {
  1873. char *name_decoded;
  1874. void *value_decoded;
  1875. size_t value_len;
  1876. if (name_length < 1)
  1877. return 3;
  1878. /* URL-decode name */
  1879. name_decoded = url_decode(name, name_length);
  1880. if (name_decoded == NULL)
  1881. return 2;
  1882. /* Base-64 decode value */
  1883. value_decoded = base64_decode(value, value_length, &value_len);
  1884. if (value_decoded == NULL) {
  1885. free(name_decoded);
  1886. return 1;
  1887. }
  1888. archive_entry_xattr_add_entry(entry, name_decoded,
  1889. value_decoded, value_len);
  1890. free(name_decoded);
  1891. free(value_decoded);
  1892. return 0;
  1893. }
  1894. static int
  1895. pax_attribute_SCHILY_xattr(struct archive_entry *entry,
  1896. const char *name, size_t name_length, const char *value, size_t value_length)
  1897. {
  1898. if (name_length < 1 || name_length > 128) {
  1899. return 1;
  1900. }
  1901. char * null_terminated_name = malloc(name_length + 1);
  1902. if (null_terminated_name != NULL) {
  1903. memcpy(null_terminated_name, name, name_length);
  1904. null_terminated_name[name_length] = '\0';
  1905. archive_entry_xattr_add_entry(entry, null_terminated_name, value, value_length);
  1906. free(null_terminated_name);
  1907. }
  1908. return 0;
  1909. }
  1910. static int
  1911. pax_attribute_RHT_security_selinux(struct archive_entry *entry,
  1912. const char *value, size_t value_length)
  1913. {
  1914. archive_entry_xattr_add_entry(entry, "security.selinux",
  1915. value, value_length);
  1916. return 0;
  1917. }
  1918. static int
  1919. pax_attribute_SCHILY_acl(struct archive_read *a, struct tar *tar,
  1920. struct archive_entry *entry, size_t value_length, int type)
  1921. {
  1922. int r;
  1923. const char *p;
  1924. const char* errstr;
  1925. switch (type) {
  1926. case ARCHIVE_ENTRY_ACL_TYPE_ACCESS:
  1927. errstr = "SCHILY.acl.access";
  1928. break;
  1929. case ARCHIVE_ENTRY_ACL_TYPE_DEFAULT:
  1930. errstr = "SCHILY.acl.default";
  1931. break;
  1932. case ARCHIVE_ENTRY_ACL_TYPE_NFS4:
  1933. errstr = "SCHILY.acl.ace";
  1934. break;
  1935. default:
  1936. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1937. "Unknown ACL type: %d", type);
  1938. return(ARCHIVE_FATAL);
  1939. }
  1940. if (tar->sconv_acl == NULL) {
  1941. tar->sconv_acl =
  1942. archive_string_conversion_from_charset(
  1943. &(a->archive), "UTF-8", 1);
  1944. if (tar->sconv_acl == NULL)
  1945. return (ARCHIVE_FATAL);
  1946. }
  1947. if (value_length > acl_limit) {
  1948. __archive_read_consume(a, value_length);
  1949. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1950. "Unreasonably large ACL: %d > %d",
  1951. (int)value_length, (int)acl_limit);
  1952. return (ARCHIVE_WARN);
  1953. }
  1954. p = __archive_read_ahead(a, value_length, NULL);
  1955. if (p == NULL) {
  1956. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  1957. "Truncated tar archive "
  1958. "detected while reading ACL data");
  1959. return (ARCHIVE_FATAL);
  1960. }
  1961. r = archive_acl_from_text_nl(archive_entry_acl(entry), p, value_length,
  1962. type, tar->sconv_acl);
  1963. __archive_read_consume(a, value_length);
  1964. /* Workaround: Force perm_is_set() to be correct */
  1965. /* If this bit were stored in the ACL, this wouldn't be needed */
  1966. archive_entry_set_perm(entry, archive_entry_perm(entry));
  1967. if (r != ARCHIVE_OK) {
  1968. if (r == ARCHIVE_FATAL) {
  1969. archive_set_error(&a->archive, ENOMEM,
  1970. "%s %s", "Can't allocate memory for ",
  1971. errstr);
  1972. return (r);
  1973. }
  1974. archive_set_error(&a->archive,
  1975. ARCHIVE_ERRNO_MISC, "%s %s", "Parse error: ", errstr);
  1976. }
  1977. return (r);
  1978. }
  1979. static int
  1980. pax_attribute_read_time(struct archive_read *a, size_t value_length, int64_t *ps, long *pn, size_t *unconsumed) {
  1981. struct archive_string as;
  1982. int r;
  1983. if (value_length > 128) {
  1984. __archive_read_consume(a, value_length);
  1985. *ps = 0;
  1986. *pn = 0;
  1987. return (ARCHIVE_FATAL);
  1988. }
  1989. archive_string_init(&as);
  1990. r = read_bytes_to_string(a, &as, value_length, unconsumed);
  1991. if (r < ARCHIVE_OK) {
  1992. archive_string_free(&as);
  1993. return (r);
  1994. }
  1995. pax_time(as.s, archive_strlen(&as), ps, pn);
  1996. archive_string_free(&as);
  1997. if (*ps < 0 || *ps == INT64_MAX) {
  1998. return (ARCHIVE_WARN);
  1999. }
  2000. return (ARCHIVE_OK);
  2001. }
  2002. static int
  2003. pax_attribute_read_number(struct archive_read *a, size_t value_length, int64_t *result) {
  2004. struct archive_string as;
  2005. size_t unconsumed = 0;
  2006. int r;
  2007. if (value_length > 64) {
  2008. __archive_read_consume(a, value_length);
  2009. *result = 0;
  2010. return (ARCHIVE_FATAL);
  2011. }
  2012. archive_string_init(&as);
  2013. r = read_bytes_to_string(a, &as, value_length, &unconsumed);
  2014. tar_flush_unconsumed(a, &unconsumed);
  2015. if (r < ARCHIVE_OK) {
  2016. archive_string_free(&as);
  2017. return (r);
  2018. }
  2019. *result = tar_atol10(as.s, archive_strlen(&as));
  2020. archive_string_free(&as);
  2021. if (*result < 0 || *result == INT64_MAX) {
  2022. *result = INT64_MAX;
  2023. return (ARCHIVE_WARN);
  2024. }
  2025. return (ARCHIVE_OK);
  2026. }
  2027. /*
  2028. * Parse a single key=value attribute.
  2029. *
  2030. * POSIX reserves all-lowercase keywords. Vendor-specific extensions
  2031. * should always have keywords of the form "VENDOR.attribute" In
  2032. * particular, it's quite feasible to support many different vendor
  2033. * extensions here. I'm using "LIBARCHIVE" for extensions unique to
  2034. * this library.
  2035. *
  2036. * TODO: Investigate other vendor-specific extensions and see if
  2037. * any of them look useful.
  2038. */
  2039. static int
  2040. pax_attribute(struct archive_read *a, struct tar *tar, struct archive_entry *entry,
  2041. const char *key, size_t key_length, size_t value_length, size_t *unconsumed)
  2042. {
  2043. int64_t t;
  2044. long n;
  2045. const char *p;
  2046. ssize_t bytes_read;
  2047. int err = ARCHIVE_OK;
  2048. switch (key[0]) {
  2049. case 'G':
  2050. /* GNU.* extensions */
  2051. if (key_length > 4 && memcmp(key, "GNU.", 4) == 0) {
  2052. key += 4;
  2053. key_length -= 4;
  2054. /* GNU.sparse marks the existence of GNU sparse information */
  2055. if (key_length == 6 && memcmp(key, "sparse", 6) == 0) {
  2056. tar->sparse_gnu_attributes_seen = 1;
  2057. }
  2058. /* GNU.sparse.* extensions */
  2059. else if (key_length > 7 && memcmp(key, "sparse.", 7) == 0) {
  2060. tar->sparse_gnu_attributes_seen = 1;
  2061. key += 7;
  2062. key_length -= 7;
  2063. /* GNU "0.0" sparse pax format. */
  2064. if (key_length == 9 && memcmp(key, "numblocks", 9) == 0) {
  2065. /* GNU.sparse.numblocks */
  2066. tar->sparse_offset = -1;
  2067. tar->sparse_numbytes = -1;
  2068. tar->sparse_gnu_major = 0;
  2069. tar->sparse_gnu_minor = 0;
  2070. }
  2071. else if (key_length == 6 && memcmp(key, "offset", 6) == 0) {
  2072. /* GNU.sparse.offset */
  2073. if ((err = pax_attribute_read_number(a, value_length, &t)) == ARCHIVE_OK) {
  2074. tar->sparse_offset = t;
  2075. if (tar->sparse_numbytes != -1) {
  2076. if (gnu_add_sparse_entry(a, tar,
  2077. tar->sparse_offset, tar->sparse_numbytes)
  2078. != ARCHIVE_OK)
  2079. return (ARCHIVE_FATAL);
  2080. tar->sparse_offset = -1;
  2081. tar->sparse_numbytes = -1;
  2082. }
  2083. }
  2084. return (err);
  2085. }
  2086. else if (key_length == 8 && memcmp(key, "numbytes", 8) == 0) {
  2087. /* GNU.sparse.numbytes */
  2088. if ((err = pax_attribute_read_number(a, value_length, &t)) == ARCHIVE_OK) {
  2089. tar->sparse_numbytes = t;
  2090. if (tar->sparse_offset != -1) {
  2091. if (gnu_add_sparse_entry(a, tar,
  2092. tar->sparse_offset, tar->sparse_numbytes)
  2093. != ARCHIVE_OK)
  2094. return (ARCHIVE_FATAL);
  2095. tar->sparse_offset = -1;
  2096. tar->sparse_numbytes = -1;
  2097. }
  2098. }
  2099. return (err);
  2100. }
  2101. else if (key_length == 4 && memcmp(key, "size", 4) == 0) {
  2102. /* GNU.sparse.size */
  2103. if ((err = pax_attribute_read_number(a, value_length, &t)) == ARCHIVE_OK) {
  2104. tar->realsize = t;
  2105. archive_entry_set_size(entry, tar->realsize);
  2106. tar->realsize_override = 1;
  2107. }
  2108. return (err);
  2109. }
  2110. /* GNU "0.1" sparse pax format. */
  2111. else if (key_length == 3 && memcmp(key, "map", 3) == 0) {
  2112. /* GNU.sparse.map */
  2113. tar->sparse_gnu_major = 0;
  2114. tar->sparse_gnu_minor = 1;
  2115. if (value_length > sparse_map_limit) {
  2116. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  2117. "Unreasonably large sparse map: %d > %d",
  2118. (int)value_length, (int)sparse_map_limit);
  2119. err = ARCHIVE_FAILED;
  2120. } else {
  2121. p = __archive_read_ahead(a, value_length, &bytes_read);
  2122. if (p == NULL) {
  2123. archive_set_error(&a->archive, EINVAL,
  2124. "Truncated archive"
  2125. " detected while reading GNU sparse data");
  2126. return (ARCHIVE_FATAL);
  2127. }
  2128. if (gnu_sparse_01_parse(a, tar, p, value_length) != ARCHIVE_OK) {
  2129. err = ARCHIVE_WARN;
  2130. }
  2131. }
  2132. __archive_read_consume(a, value_length);
  2133. return (err);
  2134. }
  2135. /* GNU "1.0" sparse pax format */
  2136. else if (key_length == 5 && memcmp(key, "major", 5) == 0) {
  2137. /* GNU.sparse.major */
  2138. if ((err = pax_attribute_read_number(a, value_length, &t)) == ARCHIVE_OK
  2139. && t >= 0
  2140. && t <= 10) {
  2141. tar->sparse_gnu_major = (int)t;
  2142. }
  2143. return (err);
  2144. }
  2145. else if (key_length == 5 && memcmp(key, "minor", 5) == 0) {
  2146. /* GNU.sparse.minor */
  2147. if ((err = pax_attribute_read_number(a, value_length, &t)) == ARCHIVE_OK
  2148. && t >= 0
  2149. && t <= 10) {
  2150. tar->sparse_gnu_minor = (int)t;
  2151. }
  2152. return (err);
  2153. }
  2154. else if (key_length == 4 && memcmp(key, "name", 4) == 0) {
  2155. /* GNU.sparse.name */
  2156. /*
  2157. * The real filename; when storing sparse
  2158. * files, GNU tar puts a synthesized name into
  2159. * the regular 'path' attribute in an attempt
  2160. * to limit confusion. ;-)
  2161. */
  2162. if (value_length > pathname_limit) {
  2163. *unconsumed += value_length;
  2164. err = ARCHIVE_WARN;
  2165. } else {
  2166. err = read_bytes_to_string(a, &(tar->entry_pathname_override),
  2167. value_length, unconsumed);
  2168. }
  2169. return (err);
  2170. }
  2171. else if (key_length == 8 && memcmp(key, "realsize", 8) == 0) {
  2172. /* GNU.sparse.realsize */
  2173. if ((err = pax_attribute_read_number(a, value_length, &t)) == ARCHIVE_OK) {
  2174. tar->realsize = t;
  2175. archive_entry_set_size(entry, tar->realsize);
  2176. tar->realsize_override = 1;
  2177. }
  2178. return (err);
  2179. }
  2180. }
  2181. }
  2182. break;
  2183. case 'L':
  2184. /* LIBARCHIVE extensions */
  2185. if (key_length > 11 && memcmp(key, "LIBARCHIVE.", 11) == 0) {
  2186. key_length -= 11;
  2187. key += 11;
  2188. /* TODO: Handle arbitrary extended attributes... */
  2189. /*
  2190. if (strcmp(key, "LIBARCHIVE.xxxxxxx") == 0)
  2191. archive_entry_set_xxxxxx(entry, value);
  2192. */
  2193. if (key_length == 12 && memcmp(key, "creationtime", 12) == 0) {
  2194. /* LIBARCHIVE.creationtime */
  2195. if ((err = pax_attribute_read_time(a, value_length, &t, &n, unconsumed)) == ARCHIVE_OK) {
  2196. archive_entry_set_birthtime(entry, t, n);
  2197. }
  2198. return (err);
  2199. }
  2200. else if (key_length == 11 && memcmp(key, "symlinktype", 11) == 0) {
  2201. /* LIBARCHIVE.symlinktype */
  2202. if (value_length < 16) {
  2203. p = __archive_read_ahead(a, value_length, &bytes_read);
  2204. if (p == NULL) {
  2205. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  2206. "Truncated tar archive "
  2207. "detected while reading `symlinktype` attribute");
  2208. return (ARCHIVE_FATAL);
  2209. }
  2210. if (value_length == 4 && memcmp(p, "file", 4) == 0) {
  2211. archive_entry_set_symlink_type(entry,
  2212. AE_SYMLINK_TYPE_FILE);
  2213. } else if (value_length == 3 && memcmp(p, "dir", 3) == 0) {
  2214. archive_entry_set_symlink_type(entry,
  2215. AE_SYMLINK_TYPE_DIRECTORY);
  2216. } else {
  2217. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  2218. "Unrecognized symlink type");
  2219. err = ARCHIVE_WARN;
  2220. }
  2221. } else {
  2222. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  2223. "symlink type is very long"
  2224. "(longest recognized value is 4 bytes, this is %d)",
  2225. (int)value_length);
  2226. err = ARCHIVE_WARN;
  2227. }
  2228. __archive_read_consume(a, value_length);
  2229. return (err);
  2230. }
  2231. else if (key_length > 6 && memcmp(key, "xattr.", 6) == 0) {
  2232. key_length -= 6;
  2233. key += 6;
  2234. if (value_length > xattr_limit) {
  2235. err = ARCHIVE_WARN;
  2236. } else {
  2237. p = __archive_read_ahead(a, value_length, &bytes_read);
  2238. if (p == NULL) {
  2239. archive_set_error(&a->archive, EINVAL,
  2240. "Truncated archive"
  2241. " detected while reading xattr information");
  2242. return (ARCHIVE_FATAL);
  2243. }
  2244. if (pax_attribute_LIBARCHIVE_xattr(entry, key, key_length, p, value_length)) {
  2245. /* TODO: Unable to parse xattr */
  2246. err = ARCHIVE_WARN;
  2247. }
  2248. }
  2249. __archive_read_consume(a, value_length);
  2250. return (err);
  2251. }
  2252. }
  2253. break;
  2254. case 'R':
  2255. /* GNU tar uses RHT.security header to store SELinux xattrs
  2256. * SCHILY.xattr.security.selinux == RHT.security.selinux */
  2257. if (key_length == 20 && memcmp(key, "RHT.security.selinux", 20) == 0) {
  2258. if (value_length > xattr_limit) {
  2259. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  2260. "Ignoring unreasonably large security.selinux attribute:"
  2261. " %d > %d",
  2262. (int)value_length, (int)xattr_limit);
  2263. /* TODO: Should this be FAILED instead? */
  2264. err = ARCHIVE_WARN;
  2265. } else {
  2266. p = __archive_read_ahead(a, value_length, &bytes_read);
  2267. if (p == NULL) {
  2268. archive_set_error(&a->archive, EINVAL,
  2269. "Truncated archive"
  2270. " detected while reading selinux data");
  2271. return (ARCHIVE_FATAL);
  2272. }
  2273. if (pax_attribute_RHT_security_selinux(entry, p, value_length)) {
  2274. /* TODO: Unable to parse xattr */
  2275. err = ARCHIVE_WARN;
  2276. }
  2277. }
  2278. __archive_read_consume(a, value_length);
  2279. return (err);
  2280. }
  2281. break;
  2282. case 'S':
  2283. /* SCHILY.* extensions used by "star" archiver */
  2284. if (key_length > 7 && memcmp(key, "SCHILY.", 7) == 0) {
  2285. key_length -= 7;
  2286. key += 7;
  2287. if (key_length == 10 && memcmp(key, "acl.access", 10) == 0) {
  2288. err = pax_attribute_SCHILY_acl(a, tar, entry, value_length,
  2289. ARCHIVE_ENTRY_ACL_TYPE_ACCESS);
  2290. // TODO: Mark mode as set
  2291. return (err);
  2292. }
  2293. else if (key_length == 11 && memcmp(key, "acl.default", 11) == 0) {
  2294. err = pax_attribute_SCHILY_acl(a, tar, entry, value_length,
  2295. ARCHIVE_ENTRY_ACL_TYPE_DEFAULT);
  2296. return (err);
  2297. }
  2298. else if (key_length == 7 && memcmp(key, "acl.ace", 7) == 0) {
  2299. err = pax_attribute_SCHILY_acl(a, tar, entry, value_length,
  2300. ARCHIVE_ENTRY_ACL_TYPE_NFS4);
  2301. // TODO: Mark mode as set
  2302. return (err);
  2303. }
  2304. else if (key_length == 8 && memcmp(key, "devmajor", 8) == 0) {
  2305. if ((err = pax_attribute_read_number(a, value_length, &t)) == ARCHIVE_OK) {
  2306. archive_entry_set_rdevmajor(entry, (dev_t)t);
  2307. }
  2308. return (err);
  2309. }
  2310. else if (key_length == 8 && memcmp(key, "devminor", 8) == 0) {
  2311. if ((err = pax_attribute_read_number(a, value_length, &t)) == ARCHIVE_OK) {
  2312. archive_entry_set_rdevminor(entry, (dev_t)t);
  2313. }
  2314. return (err);
  2315. }
  2316. else if (key_length == 6 && memcmp(key, "fflags", 6) == 0) {
  2317. if (value_length < fflags_limit) {
  2318. p = __archive_read_ahead(a, value_length, &bytes_read);
  2319. if (p == NULL) {
  2320. /* Truncated archive */
  2321. archive_set_error(&a->archive, EINVAL,
  2322. "Truncated archive"
  2323. " detected while reading SCHILY.fflags");
  2324. return (ARCHIVE_FATAL);
  2325. }
  2326. archive_entry_copy_fflags_text_len(entry, p, value_length);
  2327. err = ARCHIVE_OK;
  2328. } else {
  2329. /* Overlong fflags field */
  2330. err = ARCHIVE_WARN;
  2331. }
  2332. __archive_read_consume(a, value_length);
  2333. return (err);
  2334. }
  2335. else if (key_length == 3 && memcmp(key, "dev", 3) == 0) {
  2336. if ((err = pax_attribute_read_number(a, value_length, &t)) == ARCHIVE_OK) {
  2337. archive_entry_set_dev(entry, (dev_t)t);
  2338. }
  2339. return (err);
  2340. }
  2341. else if (key_length == 3 && memcmp(key, "ino", 3) == 0) {
  2342. if ((err = pax_attribute_read_number(a, value_length, &t)) == ARCHIVE_OK) {
  2343. archive_entry_set_ino(entry, t);
  2344. }
  2345. return (err);
  2346. }
  2347. else if (key_length == 5 && memcmp(key, "nlink", 5) == 0) {
  2348. if ((err = pax_attribute_read_number(a, value_length, &t)) == ARCHIVE_OK) {
  2349. archive_entry_set_nlink(entry, (unsigned int)t);
  2350. }
  2351. return (err);
  2352. }
  2353. else if (key_length == 8 && memcmp(key, "realsize", 8) == 0) {
  2354. if ((err = pax_attribute_read_number(a, value_length, &t)) == ARCHIVE_OK) {
  2355. tar->realsize = t;
  2356. tar->realsize_override = 1;
  2357. archive_entry_set_size(entry, tar->realsize);
  2358. }
  2359. return (err);
  2360. }
  2361. else if (key_length > 6 && memcmp(key, "xattr.", 6) == 0) {
  2362. key_length -= 6;
  2363. key += 6;
  2364. if (value_length < xattr_limit) {
  2365. p = __archive_read_ahead(a, value_length, &bytes_read);
  2366. if (p == NULL) {
  2367. archive_set_error(&a->archive, EINVAL,
  2368. "Truncated archive"
  2369. " detected while reading SCHILY.xattr");
  2370. return (ARCHIVE_FATAL);
  2371. }
  2372. if (pax_attribute_SCHILY_xattr(entry, key, key_length, p, value_length)) {
  2373. /* TODO: Unable to parse xattr */
  2374. err = ARCHIVE_WARN;
  2375. }
  2376. } else {
  2377. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  2378. "Unreasonably large xattr: %d > %d",
  2379. (int)value_length, (int)xattr_limit);
  2380. err = ARCHIVE_WARN;
  2381. }
  2382. __archive_read_consume(a, value_length);
  2383. return (err);
  2384. }
  2385. }
  2386. /* SUN.* extensions from Solaris tar */
  2387. if (key_length > 4 && memcmp(key, "SUN.", 4) == 0) {
  2388. key_length -= 4;
  2389. key += 4;
  2390. if (key_length == 9 && memcmp(key, "holesdata", 9) == 0) {
  2391. /* SUN.holesdata */
  2392. if (value_length < sparse_map_limit) {
  2393. p = __archive_read_ahead(a, value_length, &bytes_read);
  2394. if (p == NULL) {
  2395. archive_set_error(&a->archive, EINVAL,
  2396. "Truncated archive"
  2397. " detected while reading SUN.holesdata");
  2398. return (ARCHIVE_FATAL);
  2399. }
  2400. err = pax_attribute_SUN_holesdata(a, tar, entry, p, value_length);
  2401. if (err < ARCHIVE_OK) {
  2402. archive_set_error(&a->archive,
  2403. ARCHIVE_ERRNO_MISC,
  2404. "Parse error: SUN.holesdata");
  2405. }
  2406. } else {
  2407. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  2408. "Unreasonably large sparse map: %d > %d",
  2409. (int)value_length, (int)sparse_map_limit);
  2410. err = ARCHIVE_FAILED;
  2411. }
  2412. __archive_read_consume(a, value_length);
  2413. return (err);
  2414. }
  2415. }
  2416. break;
  2417. case 'a':
  2418. if (key_length == 5 && memcmp(key, "atime", 5) == 0) {
  2419. if ((err = pax_attribute_read_time(a, value_length, &t, &n, unconsumed)) == ARCHIVE_OK) {
  2420. archive_entry_set_atime(entry, t, n);
  2421. }
  2422. return (err);
  2423. }
  2424. break;
  2425. case 'c':
  2426. if (key_length == 5 && memcmp(key, "ctime", 5) == 0) {
  2427. if ((err = pax_attribute_read_time(a, value_length, &t, &n, unconsumed)) == ARCHIVE_OK) {
  2428. archive_entry_set_ctime(entry, t, n);
  2429. }
  2430. return (err);
  2431. } else if (key_length == 7 && memcmp(key, "charset", 7) == 0) {
  2432. /* TODO: Publish charset information in entry. */
  2433. } else if (key_length == 7 && memcmp(key, "comment", 7) == 0) {
  2434. /* TODO: Publish comment in entry. */
  2435. }
  2436. break;
  2437. case 'g':
  2438. if (key_length == 3 && memcmp(key, "gid", 3) == 0) {
  2439. if ((err = pax_attribute_read_number(a, value_length, &t)) == ARCHIVE_OK) {
  2440. archive_entry_set_gid(entry, t);
  2441. }
  2442. return (err);
  2443. } else if (key_length == 5 && memcmp(key, "gname", 5) == 0) {
  2444. if (value_length > guname_limit) {
  2445. *unconsumed += value_length;
  2446. err = ARCHIVE_WARN;
  2447. } else {
  2448. err = read_bytes_to_string(a, &(tar->entry_gname), value_length, unconsumed);
  2449. }
  2450. return (err);
  2451. }
  2452. break;
  2453. case 'h':
  2454. if (key_length == 10 && memcmp(key, "hdrcharset", 10) == 0) {
  2455. if (value_length < 64) {
  2456. p = __archive_read_ahead(a, value_length, &bytes_read);
  2457. if (p == NULL) {
  2458. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  2459. "Truncated tar archive "
  2460. "detected while reading hdrcharset attribute");
  2461. return (ARCHIVE_FATAL);
  2462. }
  2463. if (value_length == 6
  2464. && memcmp(p, "BINARY", 6) == 0) {
  2465. /* Binary mode. */
  2466. tar->pax_hdrcharset_utf8 = 0;
  2467. err = ARCHIVE_OK;
  2468. } else if (value_length == 23
  2469. && memcmp(p, "ISO-IR 10646 2000 UTF-8", 23) == 0) {
  2470. tar->pax_hdrcharset_utf8 = 1;
  2471. err = ARCHIVE_OK;
  2472. } else {
  2473. /* TODO: Unrecognized character set */
  2474. err = ARCHIVE_WARN;
  2475. }
  2476. } else {
  2477. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  2478. "hdrcharset attribute is unreasonably large (%d bytes)",
  2479. (int)value_length);
  2480. err = ARCHIVE_WARN;
  2481. }
  2482. __archive_read_consume(a, value_length);
  2483. return (err);
  2484. }
  2485. break;
  2486. case 'l':
  2487. /* pax interchange doesn't distinguish hardlink vs. symlink. */
  2488. if (key_length == 8 && memcmp(key, "linkpath", 8) == 0) {
  2489. if (value_length > pathname_limit) {
  2490. *unconsumed += value_length;
  2491. err = ARCHIVE_WARN;
  2492. } else {
  2493. err = read_bytes_to_string(a, &tar->entry_linkpath, value_length, unconsumed);
  2494. }
  2495. return (err);
  2496. }
  2497. break;
  2498. case 'm':
  2499. if (key_length == 5 && memcmp(key, "mtime", 5) == 0) {
  2500. if ((err = pax_attribute_read_time(a, value_length, &t, &n, unconsumed)) == ARCHIVE_OK) {
  2501. archive_entry_set_mtime(entry, t, n);
  2502. }
  2503. return (err);
  2504. }
  2505. break;
  2506. case 'p':
  2507. if (key_length == 4 && memcmp(key, "path", 4) == 0) {
  2508. if (value_length > pathname_limit) {
  2509. *unconsumed += value_length;
  2510. err = ARCHIVE_WARN;
  2511. } else {
  2512. err = read_bytes_to_string(a, &(tar->entry_pathname), value_length, unconsumed);
  2513. }
  2514. return (err);
  2515. }
  2516. break;
  2517. case 'r':
  2518. /* POSIX has reserved 'realtime.*' */
  2519. break;
  2520. case 's':
  2521. /* POSIX has reserved 'security.*' */
  2522. /* Someday: if (strcmp(key, "security.acl") == 0) { ... } */
  2523. if (key_length == 4 && memcmp(key, "size", 4) == 0) {
  2524. /* "size" is the size of the data in the entry. */
  2525. if ((err = pax_attribute_read_number(a, value_length, &t)) == ARCHIVE_OK) {
  2526. tar->entry_bytes_remaining = t;
  2527. /*
  2528. * The "size" pax header keyword always overrides the
  2529. * "size" field in the tar header.
  2530. * GNU.sparse.realsize, GNU.sparse.size and
  2531. * SCHILY.realsize override this value.
  2532. */
  2533. if (!tar->realsize_override) {
  2534. archive_entry_set_size(entry,
  2535. tar->entry_bytes_remaining);
  2536. tar->realsize
  2537. = tar->entry_bytes_remaining;
  2538. }
  2539. }
  2540. else if (t == INT64_MAX) {
  2541. /* Note: pax_attr_read_number returns INT64_MAX on overflow or < 0 */
  2542. tar->entry_bytes_remaining = 0;
  2543. archive_set_error(&a->archive,
  2544. ARCHIVE_ERRNO_MISC,
  2545. "Tar size attribute overflow");
  2546. return (ARCHIVE_FATAL);
  2547. }
  2548. return (err);
  2549. }
  2550. break;
  2551. case 'u':
  2552. if (key_length == 3 && memcmp(key, "uid", 3) == 0) {
  2553. if ((err = pax_attribute_read_number(a, value_length, &t)) == ARCHIVE_OK) {
  2554. archive_entry_set_uid(entry, t);
  2555. }
  2556. return (err);
  2557. } else if (key_length == 5 && memcmp(key, "uname", 5) == 0) {
  2558. if (value_length > guname_limit) {
  2559. *unconsumed += value_length;
  2560. err = ARCHIVE_WARN;
  2561. } else {
  2562. err = read_bytes_to_string(a, &(tar->entry_uname), value_length, unconsumed);
  2563. }
  2564. return (err);
  2565. }
  2566. break;
  2567. }
  2568. /* Unrecognized key, just skip the entire value. */
  2569. __archive_read_consume(a, value_length);
  2570. return (err);
  2571. }
  2572. /*
  2573. * parse a decimal time value, which may include a fractional portion
  2574. */
  2575. static void
  2576. pax_time(const char *p, size_t length, int64_t *ps, long *pn)
  2577. {
  2578. char digit;
  2579. int64_t s;
  2580. unsigned long l;
  2581. int sign;
  2582. int64_t limit, last_digit_limit;
  2583. limit = INT64_MAX / 10;
  2584. last_digit_limit = INT64_MAX % 10;
  2585. if (length <= 0) {
  2586. *ps = 0;
  2587. return;
  2588. }
  2589. s = 0;
  2590. sign = 1;
  2591. if (*p == '-') {
  2592. sign = -1;
  2593. p++;
  2594. length--;
  2595. }
  2596. while (length > 0 && *p >= '0' && *p <= '9') {
  2597. digit = *p - '0';
  2598. if (s > limit ||
  2599. (s == limit && digit > last_digit_limit)) {
  2600. s = INT64_MAX;
  2601. break;
  2602. }
  2603. s = (s * 10) + digit;
  2604. ++p;
  2605. --length;
  2606. }
  2607. *ps = s * sign;
  2608. /* Calculate nanoseconds. */
  2609. *pn = 0;
  2610. if (length <= 0 || *p != '.')
  2611. return;
  2612. l = 100000000UL;
  2613. do {
  2614. ++p;
  2615. --length;
  2616. if (length > 0 && *p >= '0' && *p <= '9')
  2617. *pn += (*p - '0') * l;
  2618. else
  2619. break;
  2620. } while (l /= 10);
  2621. }
  2622. /*
  2623. * Parse GNU tar header
  2624. */
  2625. static int
  2626. header_gnutar(struct archive_read *a, struct tar *tar,
  2627. struct archive_entry *entry, const void *h, size_t *unconsumed)
  2628. {
  2629. const struct archive_entry_header_gnutar *header;
  2630. int64_t t;
  2631. int err = ARCHIVE_OK;
  2632. /*
  2633. * GNU header is like POSIX ustar, except 'prefix' is
  2634. * replaced with some other fields. This also means the
  2635. * filename is stored as in old-style archives.
  2636. */
  2637. /* Grab fields common to all tar variants. */
  2638. err = header_common(a, tar, entry, h);
  2639. if (err == ARCHIVE_FATAL)
  2640. return (err);
  2641. /* Copy filename over (to ensure null termination). */
  2642. header = (const struct archive_entry_header_gnutar *)h;
  2643. const char *existing_pathname = archive_entry_pathname(entry);
  2644. if (existing_pathname == NULL || existing_pathname[0] == '\0') {
  2645. if (archive_entry_copy_pathname_l(entry,
  2646. header->name, sizeof(header->name), tar->sconv) != 0) {
  2647. err = set_conversion_failed_error(a, tar->sconv, "Pathname");
  2648. if (err == ARCHIVE_FATAL)
  2649. return (err);
  2650. }
  2651. }
  2652. /* Fields common to ustar and GNU */
  2653. /* XXX Can the following be factored out since it's common
  2654. * to ustar and gnu tar? Is it okay to move it down into
  2655. * header_common, perhaps? */
  2656. const char *existing_uname = archive_entry_uname(entry);
  2657. if (existing_uname == NULL || existing_uname[0] == '\0') {
  2658. if (archive_entry_copy_uname_l(entry,
  2659. header->uname, sizeof(header->uname), tar->sconv) != 0) {
  2660. err = set_conversion_failed_error(a, tar->sconv, "Uname");
  2661. if (err == ARCHIVE_FATAL)
  2662. return (err);
  2663. }
  2664. }
  2665. const char *existing_gname = archive_entry_gname(entry);
  2666. if (existing_gname == NULL || existing_gname[0] == '\0') {
  2667. if (archive_entry_copy_gname_l(entry,
  2668. header->gname, sizeof(header->gname), tar->sconv) != 0) {
  2669. err = set_conversion_failed_error(a, tar->sconv, "Gname");
  2670. if (err == ARCHIVE_FATAL)
  2671. return (err);
  2672. }
  2673. }
  2674. /* Parse out device numbers only for char and block specials */
  2675. if (header->typeflag[0] == '3' || header->typeflag[0] == '4') {
  2676. if (!archive_entry_rdev_is_set(entry)) {
  2677. archive_entry_set_rdevmajor(entry, (dev_t)
  2678. tar_atol(header->rdevmajor, sizeof(header->rdevmajor)));
  2679. archive_entry_set_rdevminor(entry, (dev_t)
  2680. tar_atol(header->rdevminor, sizeof(header->rdevminor)));
  2681. }
  2682. } else {
  2683. archive_entry_set_rdev(entry, 0);
  2684. }
  2685. tar->entry_padding = 0x1ff & (-tar->entry_bytes_remaining);
  2686. /* Grab GNU-specific fields. */
  2687. if (!archive_entry_atime_is_set(entry)) {
  2688. t = tar_atol(header->atime, sizeof(header->atime));
  2689. if (t > 0)
  2690. archive_entry_set_atime(entry, t, 0);
  2691. }
  2692. if (!archive_entry_ctime_is_set(entry)) {
  2693. t = tar_atol(header->ctime, sizeof(header->ctime));
  2694. if (t > 0)
  2695. archive_entry_set_ctime(entry, t, 0);
  2696. }
  2697. if (header->realsize[0] != 0) {
  2698. tar->realsize
  2699. = tar_atol(header->realsize, sizeof(header->realsize));
  2700. archive_entry_set_size(entry, tar->realsize);
  2701. tar->realsize_override = 1;
  2702. }
  2703. if (header->sparse[0].offset[0] != 0) {
  2704. if (gnu_sparse_old_read(a, tar, header, unconsumed)
  2705. != ARCHIVE_OK)
  2706. return (ARCHIVE_FATAL);
  2707. } else {
  2708. if (header->isextended[0] != 0) {
  2709. /* XXX WTF? XXX */
  2710. }
  2711. }
  2712. return (err);
  2713. }
  2714. static int
  2715. gnu_add_sparse_entry(struct archive_read *a, struct tar *tar,
  2716. int64_t offset, int64_t remaining)
  2717. {
  2718. struct sparse_block *p;
  2719. p = calloc(1, sizeof(*p));
  2720. if (p == NULL) {
  2721. archive_set_error(&a->archive, ENOMEM, "Out of memory");
  2722. return (ARCHIVE_FATAL);
  2723. }
  2724. if (tar->sparse_last != NULL)
  2725. tar->sparse_last->next = p;
  2726. else
  2727. tar->sparse_list = p;
  2728. tar->sparse_last = p;
  2729. if (remaining < 0 || offset < 0 || offset > INT64_MAX - remaining) {
  2730. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, "Malformed sparse map data");
  2731. return (ARCHIVE_FATAL);
  2732. }
  2733. p->offset = offset;
  2734. p->remaining = remaining;
  2735. return (ARCHIVE_OK);
  2736. }
  2737. static void
  2738. gnu_clear_sparse_list(struct tar *tar)
  2739. {
  2740. struct sparse_block *p;
  2741. while (tar->sparse_list != NULL) {
  2742. p = tar->sparse_list;
  2743. tar->sparse_list = p->next;
  2744. free(p);
  2745. }
  2746. tar->sparse_last = NULL;
  2747. }
  2748. /*
  2749. * GNU tar old-format sparse data.
  2750. *
  2751. * GNU old-format sparse data is stored in a fixed-field
  2752. * format. Offset/size values are 11-byte octal fields (same
  2753. * format as 'size' field in ustart header). These are
  2754. * stored in the header, allocating subsequent header blocks
  2755. * as needed. Extending the header in this way is a pretty
  2756. * severe POSIX violation; this design has earned GNU tar a
  2757. * lot of criticism.
  2758. */
  2759. static int
  2760. gnu_sparse_old_read(struct archive_read *a, struct tar *tar,
  2761. const struct archive_entry_header_gnutar *header, size_t *unconsumed)
  2762. {
  2763. ssize_t bytes_read;
  2764. const void *data;
  2765. struct extended {
  2766. struct gnu_sparse sparse[21];
  2767. char isextended[1];
  2768. char padding[7];
  2769. };
  2770. const struct extended *ext;
  2771. if (gnu_sparse_old_parse(a, tar, header->sparse, 4) != ARCHIVE_OK)
  2772. return (ARCHIVE_FATAL);
  2773. if (header->isextended[0] == 0)
  2774. return (ARCHIVE_OK);
  2775. do {
  2776. tar_flush_unconsumed(a, unconsumed);
  2777. data = __archive_read_ahead(a, 512, &bytes_read);
  2778. if (data == NULL) {
  2779. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  2780. "Truncated tar archive "
  2781. "detected while reading sparse file data");
  2782. return (ARCHIVE_FATAL);
  2783. }
  2784. *unconsumed = 512;
  2785. ext = (const struct extended *)data;
  2786. if (gnu_sparse_old_parse(a, tar, ext->sparse, 21) != ARCHIVE_OK)
  2787. return (ARCHIVE_FATAL);
  2788. } while (ext->isextended[0] != 0);
  2789. if (tar->sparse_list != NULL)
  2790. tar->entry_offset = tar->sparse_list->offset;
  2791. return (ARCHIVE_OK);
  2792. }
  2793. static int
  2794. gnu_sparse_old_parse(struct archive_read *a, struct tar *tar,
  2795. const struct gnu_sparse *sparse, int length)
  2796. {
  2797. while (length > 0 && sparse->offset[0] != 0) {
  2798. if (gnu_add_sparse_entry(a, tar,
  2799. tar_atol(sparse->offset, sizeof(sparse->offset)),
  2800. tar_atol(sparse->numbytes, sizeof(sparse->numbytes)))
  2801. != ARCHIVE_OK)
  2802. return (ARCHIVE_FATAL);
  2803. sparse++;
  2804. length--;
  2805. }
  2806. return (ARCHIVE_OK);
  2807. }
  2808. /*
  2809. * GNU tar sparse format 0.0
  2810. *
  2811. * Beginning with GNU tar 1.15, sparse files are stored using
  2812. * information in the pax extended header. The GNU tar maintainers
  2813. * have gone through a number of variations in the process of working
  2814. * out this scheme; fortunately, they're all numbered.
  2815. *
  2816. * Sparse format 0.0 uses attribute GNU.sparse.numblocks to store the
  2817. * number of blocks, and GNU.sparse.offset/GNU.sparse.numbytes to
  2818. * store offset/size for each block. The repeated instances of these
  2819. * latter fields violate the pax specification (which frowns on
  2820. * duplicate keys), so this format was quickly replaced.
  2821. */
  2822. /*
  2823. * GNU tar sparse format 0.1
  2824. *
  2825. * This version replaced the offset/numbytes attributes with
  2826. * a single "map" attribute that stored a list of integers. This
  2827. * format had two problems: First, the "map" attribute could be very
  2828. * long, which caused problems for some implementations. More
  2829. * importantly, the sparse data was lost when extracted by archivers
  2830. * that didn't recognize this extension.
  2831. */
  2832. static int
  2833. gnu_sparse_01_parse(struct archive_read *a, struct tar *tar, const char *p, size_t length)
  2834. {
  2835. const char *e;
  2836. int64_t offset = -1, size = -1;
  2837. for (;;) {
  2838. e = p;
  2839. while (length > 0 && *e != ',') {
  2840. if (*e < '0' || *e > '9')
  2841. return (ARCHIVE_WARN);
  2842. e++;
  2843. length--;
  2844. }
  2845. if (offset < 0) {
  2846. offset = tar_atol10(p, e - p);
  2847. if (offset < 0)
  2848. return (ARCHIVE_WARN);
  2849. } else {
  2850. size = tar_atol10(p, e - p);
  2851. if (size < 0)
  2852. return (ARCHIVE_WARN);
  2853. if (gnu_add_sparse_entry(a, tar, offset, size)
  2854. != ARCHIVE_OK)
  2855. return (ARCHIVE_FATAL);
  2856. offset = -1;
  2857. }
  2858. if (length == 0)
  2859. return (ARCHIVE_OK);
  2860. p = e + 1;
  2861. length--;
  2862. }
  2863. }
  2864. /*
  2865. * GNU tar sparse format 1.0
  2866. *
  2867. * The idea: The offset/size data is stored as a series of base-10
  2868. * ASCII numbers prepended to the file data, so that dearchivers that
  2869. * don't support this format will extract the block map along with the
  2870. * data and a separate post-process can restore the sparseness.
  2871. *
  2872. * Unfortunately, GNU tar 1.16 had a bug that added unnecessary
  2873. * padding to the body of the file when using this format. GNU tar
  2874. * 1.17 corrected this bug without bumping the version number, so
  2875. * it's not possible to support both variants. This code supports
  2876. * the later variant at the expense of not supporting the former.
  2877. *
  2878. * This variant also replaced GNU.sparse.size with GNU.sparse.realsize
  2879. * and introduced the GNU.sparse.major/GNU.sparse.minor attributes.
  2880. */
  2881. /*
  2882. * Read the next line from the input, and parse it as a decimal
  2883. * integer followed by '\n'. Returns positive integer value or
  2884. * negative on error.
  2885. */
  2886. static int64_t
  2887. gnu_sparse_10_atol(struct archive_read *a, struct tar *tar,
  2888. int64_t *remaining, size_t *unconsumed)
  2889. {
  2890. int64_t l, limit, last_digit_limit;
  2891. const char *p;
  2892. ssize_t bytes_read;
  2893. int base, digit;
  2894. base = 10;
  2895. limit = INT64_MAX / base;
  2896. last_digit_limit = INT64_MAX % base;
  2897. /*
  2898. * Skip any lines starting with '#'; GNU tar specs
  2899. * don't require this, but they should.
  2900. */
  2901. do {
  2902. bytes_read = readline(a, tar, &p,
  2903. (ssize_t)tar_min(*remaining, 100), unconsumed);
  2904. if (bytes_read <= 0)
  2905. return (ARCHIVE_FATAL);
  2906. *remaining -= bytes_read;
  2907. } while (p[0] == '#');
  2908. l = 0;
  2909. while (bytes_read > 0) {
  2910. if (*p == '\n')
  2911. return (l);
  2912. if (*p < '0' || *p >= '0' + base)
  2913. return (ARCHIVE_WARN);
  2914. digit = *p - '0';
  2915. if (l > limit || (l == limit && digit > last_digit_limit))
  2916. l = INT64_MAX; /* Truncate on overflow. */
  2917. else
  2918. l = (l * base) + digit;
  2919. p++;
  2920. bytes_read--;
  2921. }
  2922. /* TODO: Error message. */
  2923. return (ARCHIVE_WARN);
  2924. }
  2925. /*
  2926. * Returns length (in bytes) of the sparse data description
  2927. * that was read.
  2928. */
  2929. static ssize_t
  2930. gnu_sparse_10_read(struct archive_read *a, struct tar *tar, size_t *unconsumed)
  2931. {
  2932. ssize_t bytes_read;
  2933. int entries;
  2934. int64_t offset, size, to_skip, remaining;
  2935. /* Clear out the existing sparse list. */
  2936. gnu_clear_sparse_list(tar);
  2937. remaining = tar->entry_bytes_remaining;
  2938. /* Parse entries. */
  2939. entries = (int)gnu_sparse_10_atol(a, tar, &remaining, unconsumed);
  2940. if (entries < 0)
  2941. return (ARCHIVE_FATAL);
  2942. /* Parse the individual entries. */
  2943. while (entries-- > 0) {
  2944. /* Parse offset/size */
  2945. offset = gnu_sparse_10_atol(a, tar, &remaining, unconsumed);
  2946. if (offset < 0)
  2947. return (ARCHIVE_FATAL);
  2948. size = gnu_sparse_10_atol(a, tar, &remaining, unconsumed);
  2949. if (size < 0)
  2950. return (ARCHIVE_FATAL);
  2951. /* Add a new sparse entry. */
  2952. if (gnu_add_sparse_entry(a, tar, offset, size) != ARCHIVE_OK)
  2953. return (ARCHIVE_FATAL);
  2954. }
  2955. /* Skip rest of block... */
  2956. tar_flush_unconsumed(a, unconsumed);
  2957. bytes_read = (ssize_t)(tar->entry_bytes_remaining - remaining);
  2958. to_skip = 0x1ff & -bytes_read;
  2959. /* Fail if tar->entry_bytes_remaing would get negative */
  2960. if (to_skip > remaining)
  2961. return (ARCHIVE_FATAL);
  2962. if (to_skip != __archive_read_consume(a, to_skip))
  2963. return (ARCHIVE_FATAL);
  2964. return ((ssize_t)(bytes_read + to_skip));
  2965. }
  2966. /*
  2967. * Solaris pax extension for a sparse file. This is recorded with the
  2968. * data and hole pairs. The way recording sparse information by Solaris'
  2969. * pax simply indicates where data and sparse are, so the stored contents
  2970. * consist of both data and hole.
  2971. */
  2972. static int
  2973. pax_attribute_SUN_holesdata(struct archive_read *a, struct tar *tar,
  2974. struct archive_entry *entry, const char *p, size_t length)
  2975. {
  2976. const char *e;
  2977. int64_t start, end;
  2978. int hole = 1;
  2979. (void)entry; /* UNUSED */
  2980. end = 0;
  2981. if (length <= 0)
  2982. return (ARCHIVE_WARN);
  2983. if (*p == ' ') {
  2984. p++;
  2985. length--;
  2986. } else {
  2987. return (ARCHIVE_WARN);
  2988. }
  2989. for (;;) {
  2990. e = p;
  2991. while (length > 0 && *e != ' ') {
  2992. if (*e < '0' || *e > '9')
  2993. return (ARCHIVE_WARN);
  2994. e++;
  2995. length--;
  2996. }
  2997. start = end;
  2998. end = tar_atol10(p, e - p);
  2999. if (end < 0)
  3000. return (ARCHIVE_WARN);
  3001. if (start < end) {
  3002. if (gnu_add_sparse_entry(a, tar, start,
  3003. end - start) != ARCHIVE_OK)
  3004. return (ARCHIVE_FATAL);
  3005. tar->sparse_last->hole = hole;
  3006. }
  3007. if (length == 0 || *e == '\n') {
  3008. if (length == 0 && *e == '\n') {
  3009. return (ARCHIVE_OK);
  3010. } else {
  3011. return (ARCHIVE_WARN);
  3012. }
  3013. }
  3014. p = e + 1;
  3015. length--;
  3016. hole = hole == 0;
  3017. }
  3018. }
  3019. /*-
  3020. * Convert text->integer.
  3021. *
  3022. * Traditional tar formats (including POSIX) specify base-8 for
  3023. * all of the standard numeric fields. This is a significant limitation
  3024. * in practice:
  3025. * = file size is limited to 8GB
  3026. * = rdevmajor and rdevminor are limited to 21 bits
  3027. * = uid/gid are limited to 21 bits
  3028. *
  3029. * There are two workarounds for this:
  3030. * = pax extended headers, which use variable-length string fields
  3031. * = GNU tar and STAR both allow either base-8 or base-256 in
  3032. * most fields. The high bit is set to indicate base-256.
  3033. *
  3034. * On read, this implementation supports both extensions.
  3035. */
  3036. static int64_t
  3037. tar_atol(const char *p, size_t char_cnt)
  3038. {
  3039. /*
  3040. * Technically, GNU tar considers a field to be in base-256
  3041. * only if the first byte is 0xff or 0x80.
  3042. */
  3043. if (*p & 0x80)
  3044. return (tar_atol256(p, char_cnt));
  3045. return (tar_atol8(p, char_cnt));
  3046. }
  3047. /*
  3048. * Note that this implementation does not (and should not!) obey
  3049. * locale settings; you cannot simply substitute strtol here, since
  3050. * it does obey locale.
  3051. */
  3052. static int64_t
  3053. tar_atol_base_n(const char *p, size_t char_cnt, int base)
  3054. {
  3055. int64_t l, maxval, limit, last_digit_limit;
  3056. int digit, sign;
  3057. maxval = INT64_MAX;
  3058. limit = INT64_MAX / base;
  3059. last_digit_limit = INT64_MAX % base;
  3060. /* the pointer will not be dereferenced if char_cnt is zero
  3061. * due to the way the && operator is evaluated.
  3062. */
  3063. while (char_cnt != 0 && (*p == ' ' || *p == '\t')) {
  3064. p++;
  3065. char_cnt--;
  3066. }
  3067. sign = 1;
  3068. if (char_cnt != 0 && *p == '-') {
  3069. sign = -1;
  3070. p++;
  3071. char_cnt--;
  3072. maxval = INT64_MIN;
  3073. limit = -(INT64_MIN / base);
  3074. last_digit_limit = -(INT64_MIN % base);
  3075. }
  3076. l = 0;
  3077. if (char_cnt != 0) {
  3078. digit = *p - '0';
  3079. while (digit >= 0 && digit < base && char_cnt != 0) {
  3080. if (l>limit || (l == limit && digit >= last_digit_limit)) {
  3081. return maxval; /* Truncate on overflow. */
  3082. }
  3083. l = (l * base) + digit;
  3084. digit = *++p - '0';
  3085. char_cnt--;
  3086. }
  3087. }
  3088. return (sign < 0) ? -l : l;
  3089. }
  3090. static int64_t
  3091. tar_atol8(const char *p, size_t char_cnt)
  3092. {
  3093. return tar_atol_base_n(p, char_cnt, 8);
  3094. }
  3095. static int64_t
  3096. tar_atol10(const char *p, size_t char_cnt)
  3097. {
  3098. return tar_atol_base_n(p, char_cnt, 10);
  3099. }
  3100. /*
  3101. * Parse a base-256 integer. This is just a variable-length
  3102. * twos-complement signed binary value in big-endian order, except
  3103. * that the high-order bit is ignored. The values here can be up to
  3104. * 12 bytes, so we need to be careful about overflowing 64-bit
  3105. * (8-byte) integers.
  3106. *
  3107. * This code unashamedly assumes that the local machine uses 8-bit
  3108. * bytes and twos-complement arithmetic.
  3109. */
  3110. static int64_t
  3111. tar_atol256(const char *_p, size_t char_cnt)
  3112. {
  3113. uint64_t l;
  3114. const unsigned char *p = (const unsigned char *)_p;
  3115. unsigned char c, neg;
  3116. /* Extend 7-bit 2s-comp to 8-bit 2s-comp, decide sign. */
  3117. c = *p;
  3118. if (c & 0x40) {
  3119. neg = 0xff;
  3120. c |= 0x80;
  3121. l = ~ARCHIVE_LITERAL_ULL(0);
  3122. } else {
  3123. neg = 0;
  3124. c &= 0x7f;
  3125. l = 0;
  3126. }
  3127. /* If more than 8 bytes, check that we can ignore
  3128. * high-order bits without overflow. */
  3129. while (char_cnt > sizeof(int64_t)) {
  3130. --char_cnt;
  3131. if (c != neg)
  3132. return neg ? INT64_MIN : INT64_MAX;
  3133. c = *++p;
  3134. }
  3135. /* c is first byte that fits; if sign mismatch, return overflow */
  3136. if ((c ^ neg) & 0x80) {
  3137. return neg ? INT64_MIN : INT64_MAX;
  3138. }
  3139. /* Accumulate remaining bytes. */
  3140. while (--char_cnt > 0) {
  3141. l = (l << 8) | c;
  3142. c = *++p;
  3143. }
  3144. l = (l << 8) | c;
  3145. /* Return signed twos-complement value. */
  3146. return (int64_t)(l);
  3147. }
  3148. /*
  3149. * Returns length of line (including trailing newline)
  3150. * or negative on error. 'start' argument is updated to
  3151. * point to first character of line. This avoids copying
  3152. * when possible.
  3153. */
  3154. static ssize_t
  3155. readline(struct archive_read *a, struct tar *tar, const char **start,
  3156. ssize_t limit, size_t *unconsumed)
  3157. {
  3158. ssize_t bytes_read;
  3159. ssize_t total_size = 0;
  3160. const void *t;
  3161. const char *s;
  3162. void *p;
  3163. tar_flush_unconsumed(a, unconsumed);
  3164. t = __archive_read_ahead(a, 1, &bytes_read);
  3165. if (bytes_read <= 0 || t == NULL)
  3166. return (ARCHIVE_FATAL);
  3167. s = t; /* Start of line? */
  3168. p = memchr(t, '\n', bytes_read);
  3169. /* If we found '\n' in the read buffer, return pointer to that. */
  3170. if (p != NULL) {
  3171. bytes_read = 1 + ((const char *)p) - s;
  3172. if (bytes_read > limit) {
  3173. archive_set_error(&a->archive,
  3174. ARCHIVE_ERRNO_FILE_FORMAT,
  3175. "Line too long");
  3176. return (ARCHIVE_FATAL);
  3177. }
  3178. *unconsumed = bytes_read;
  3179. *start = s;
  3180. return (bytes_read);
  3181. }
  3182. *unconsumed = bytes_read;
  3183. /* Otherwise, we need to accumulate in a line buffer. */
  3184. for (;;) {
  3185. if (total_size + bytes_read > limit) {
  3186. archive_set_error(&a->archive,
  3187. ARCHIVE_ERRNO_FILE_FORMAT,
  3188. "Line too long");
  3189. return (ARCHIVE_FATAL);
  3190. }
  3191. if (archive_string_ensure(&tar->line, total_size + bytes_read) == NULL) {
  3192. archive_set_error(&a->archive, ENOMEM,
  3193. "Can't allocate working buffer");
  3194. return (ARCHIVE_FATAL);
  3195. }
  3196. memcpy(tar->line.s + total_size, t, bytes_read);
  3197. tar_flush_unconsumed(a, unconsumed);
  3198. total_size += bytes_read;
  3199. /* If we found '\n', clean up and return. */
  3200. if (p != NULL) {
  3201. *start = tar->line.s;
  3202. return (total_size);
  3203. }
  3204. /* Read some more. */
  3205. t = __archive_read_ahead(a, 1, &bytes_read);
  3206. if (bytes_read <= 0 || t == NULL)
  3207. return (ARCHIVE_FATAL);
  3208. s = t; /* Start of line? */
  3209. p = memchr(t, '\n', bytes_read);
  3210. /* If we found '\n', trim the read. */
  3211. if (p != NULL) {
  3212. bytes_read = 1 + ((const char *)p) - s;
  3213. }
  3214. *unconsumed = bytes_read;
  3215. }
  3216. }
  3217. /*
  3218. * base64_decode - Base64 decode
  3219. *
  3220. * This accepts most variations of base-64 encoding, including:
  3221. * * with or without line breaks
  3222. * * with or without the final group padded with '=' or '_' characters
  3223. * (The most economical Base-64 variant does not pad the last group and
  3224. * omits line breaks; RFC1341 used for MIME requires both.)
  3225. */
  3226. static char *
  3227. base64_decode(const char *s, size_t len, size_t *out_len)
  3228. {
  3229. static const unsigned char digits[64] = {
  3230. 'A','B','C','D','E','F','G','H','I','J','K','L','M','N',
  3231. 'O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b',
  3232. 'c','d','e','f','g','h','i','j','k','l','m','n','o','p',
  3233. 'q','r','s','t','u','v','w','x','y','z','0','1','2','3',
  3234. '4','5','6','7','8','9','+','/' };
  3235. static unsigned char decode_table[128];
  3236. char *out, *d;
  3237. const unsigned char *src = (const unsigned char *)s;
  3238. /* If the decode table is not yet initialized, prepare it. */
  3239. if (decode_table[digits[1]] != 1) {
  3240. unsigned i;
  3241. memset(decode_table, 0xff, sizeof(decode_table));
  3242. for (i = 0; i < sizeof(digits); i++)
  3243. decode_table[digits[i]] = i;
  3244. }
  3245. /* Allocate enough space to hold the entire output. */
  3246. /* Note that we may not use all of this... */
  3247. out = malloc(len - len / 4 + 1);
  3248. if (out == NULL) {
  3249. *out_len = 0;
  3250. return (NULL);
  3251. }
  3252. d = out;
  3253. while (len > 0) {
  3254. /* Collect the next group of (up to) four characters. */
  3255. int v = 0;
  3256. int group_size = 0;
  3257. while (group_size < 4 && len > 0) {
  3258. /* '=' or '_' padding indicates final group. */
  3259. if (*src == '=' || *src == '_') {
  3260. len = 0;
  3261. break;
  3262. }
  3263. /* Skip illegal characters (including line breaks) */
  3264. if (*src > 127 || *src < 32
  3265. || decode_table[*src] == 0xff) {
  3266. len--;
  3267. src++;
  3268. continue;
  3269. }
  3270. v <<= 6;
  3271. v |= decode_table[*src++];
  3272. len --;
  3273. group_size++;
  3274. }
  3275. /* Align a short group properly. */
  3276. v <<= 6 * (4 - group_size);
  3277. /* Unpack the group we just collected. */
  3278. switch (group_size) {
  3279. case 4: d[2] = v & 0xff;
  3280. /* FALLTHROUGH */
  3281. case 3: d[1] = (v >> 8) & 0xff;
  3282. /* FALLTHROUGH */
  3283. case 2: d[0] = (v >> 16) & 0xff;
  3284. break;
  3285. case 1: /* this is invalid! */
  3286. break;
  3287. }
  3288. d += group_size * 3 / 4;
  3289. }
  3290. *out_len = d - out;
  3291. return (out);
  3292. }
  3293. static char *
  3294. url_decode(const char *in, size_t length)
  3295. {
  3296. char *out, *d;
  3297. const char *s;
  3298. out = malloc(length + 1);
  3299. if (out == NULL)
  3300. return (NULL);
  3301. for (s = in, d = out; length > 0 && *s != '\0'; ) {
  3302. if (s[0] == '%' && length > 2) {
  3303. /* Try to convert % escape */
  3304. int digit1 = tohex(s[1]);
  3305. int digit2 = tohex(s[2]);
  3306. if (digit1 >= 0 && digit2 >= 0) {
  3307. /* Looks good, consume three chars */
  3308. s += 3;
  3309. length -= 3;
  3310. /* Convert output */
  3311. *d++ = ((digit1 << 4) | digit2);
  3312. continue;
  3313. }
  3314. /* Else fall through and treat '%' as normal char */
  3315. }
  3316. *d++ = *s++;
  3317. --length;
  3318. }
  3319. *d = '\0';
  3320. return (out);
  3321. }
  3322. static int
  3323. tohex(int c)
  3324. {
  3325. if (c >= '0' && c <= '9')
  3326. return (c - '0');
  3327. else if (c >= 'A' && c <= 'F')
  3328. return (c - 'A' + 10);
  3329. else if (c >= 'a' && c <= 'f')
  3330. return (c - 'a' + 10);
  3331. else
  3332. return (-1);
  3333. }