| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698 |
- /*-
- * Copyright (c) 2010-2012 Michihiro NAKAJIMA
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
- #include "archive_platform.h"
- #ifdef HAVE_ERRNO_H
- #include <errno.h>
- #endif
- #ifdef HAVE_LIMITS_H
- #include <limits.h>
- #endif
- #include <stdlib.h>
- #if HAVE_LIBXML_XMLWRITER_H
- #include <libxml/xmlwriter.h>
- #if defined(LIBXML_VERSION) && LIBXML_VERSION >= 20703
- #define XAR_WRITER_HAS_XML
- #endif /* LIBXML_VERSION */
- #elif HAVE_XMLLITE_H
- #include <objidl.h>
- #include <initguid.h>
- #include <xmllite.h>
- #define XAR_WRITER_HAS_XML
- #endif
- #ifdef HAVE_BZLIB_H
- #include <cm3p/bzlib.h>
- #endif
- #if HAVE_LZMA_H
- #include <cm3p/lzma.h>
- #endif
- #ifdef HAVE_ZLIB_H
- #include <cm3p/zlib.h>
- #endif
- #include "archive.h"
- #include "archive_digest_private.h"
- #include "archive_endian.h"
- #include "archive_entry.h"
- #include "archive_entry_locale.h"
- #include "archive_private.h"
- #include "archive_rb.h"
- #include "archive_string.h"
- #include "archive_write_private.h"
- /*
- * Differences to xar utility.
- * - Subdocument is not supported yet.
- * - ACL is not supported yet.
- * - When writing an XML element <link type="<file-type>">, <file-type>
- * which is a file type a symbolic link is referencing is always marked
- * as "broken". Xar utility uses stat(2) to get the file type, but, in
- * libarchive format writer, we should not use it; if it is needed, we
- * should get about it at archive_read_disk.c.
- * - It is possible to appear both <flags> and <ext2> elements.
- * Xar utility generates <flags> on BSD platform and <ext2> on Linux
- * platform.
- *
- */
- #if !defined(XAR_WRITER_HAS_XML) ||\
- !defined(HAVE_ZLIB_H) || \
- !defined(ARCHIVE_HAS_MD5) || !defined(ARCHIVE_HAS_SHA1)
- /*
- * xar needs several external libraries.
- * o libxml2 or xmllite (on Windows)
- * o openssl or MD5/SHA1 hash function
- * o zlib
- * o bzlib2 (option)
- * o liblzma (option)
- */
- int
- archive_write_set_format_xar(struct archive *_a)
- {
- struct archive_write *a = (struct archive_write *)_a;
- archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
- "Xar not supported on this platform");
- return (ARCHIVE_WARN);
- }
- #else /* Support xar format */
- struct xml_writer;
- static int xml_writer_create(struct xml_writer **pctx);
- static int xml_writer_start_document(struct xml_writer *ctx);
- static int xml_writer_end_document(struct xml_writer *ctx);
- static int xml_writer_set_indent(struct xml_writer *ctx, unsigned int indent);
- static int xml_writer_start_element(struct xml_writer *ctx,
- const char *localName);
- static int xml_writer_write_attribute(struct xml_writer *ctx, const char *key,
- const char *value);
- static int xml_writer_write_attributef(struct xml_writer *ctx, const char *key,
- const char *format, ...);
- static int xml_writer_write_string(struct xml_writer *ctx, const char *string);
- static int xml_writer_write_base64(struct xml_writer* ctx,
- const char *data, size_t start, size_t len);
- static int xml_writer_end_element(struct xml_writer *ctx);
- static int xml_writer_get_final_content_and_length(struct xml_writer *ctx,
- const char **out, size_t *size);
- static int xml_writer_destroy(struct xml_writer *ctx);
- /*#define DEBUG_PRINT_TOC 1 */
- #define HEADER_MAGIC 0x78617221
- #define HEADER_SIZE 28
- #define HEADER_VERSION 1
- enum sumalg {
- CKSUM_NONE = 0,
- CKSUM_SHA1 = 1,
- CKSUM_MD5 = 2
- };
- #define MD5_SIZE 16
- #define SHA1_SIZE 20
- #define MAX_SUM_SIZE 20
- #define MD5_NAME "md5"
- #define SHA1_NAME "sha1"
- enum enctype {
- NONE,
- GZIP,
- BZIP2,
- LZMA,
- XZ,
- };
- struct chksumwork {
- enum sumalg alg;
- #ifdef ARCHIVE_HAS_MD5
- archive_md5_ctx md5ctx;
- #endif
- #ifdef ARCHIVE_HAS_SHA1
- archive_sha1_ctx sha1ctx;
- #endif
- };
- enum la_zaction {
- ARCHIVE_Z_FINISH,
- ARCHIVE_Z_RUN
- };
- /*
- * Universal zstream.
- */
- struct la_zstream {
- const unsigned char *next_in;
- size_t avail_in;
- uint64_t total_in;
- unsigned char *next_out;
- size_t avail_out;
- uint64_t total_out;
- int valid;
- void *real_stream;
- int (*code) (struct archive *a,
- struct la_zstream *lastrm,
- enum la_zaction action);
- int (*end)(struct archive *a,
- struct la_zstream *lastrm);
- };
- struct chksumval {
- enum sumalg alg;
- size_t len;
- unsigned char val[MAX_SUM_SIZE];
- };
- struct heap_data {
- int id;
- struct heap_data *next;
- uint64_t temp_offset;
- uint64_t length; /* archived size. */
- uint64_t size; /* extracted size. */
- enum enctype compression;
- struct chksumval a_sum; /* archived checksum. */
- struct chksumval e_sum; /* extracted checksum. */
- };
- struct file {
- struct archive_rb_node rbnode;
- int id;
- struct archive_entry *entry;
- struct archive_rb_tree rbtree;
- struct file *next;
- struct file *chnext;
- struct file *hlnext;
- /* For hardlinked files.
- * Use only when archive_entry_nlink() > 1 */
- struct file *hardlink_target;
- struct file *parent; /* parent directory entry */
- /*
- * To manage sub directory files.
- * We use 'chnext' (a member of struct file) to chain.
- */
- struct {
- struct file *first;
- struct file **last;
- } children;
- /* For making a directory tree. */
- struct archive_string parentdir;
- struct archive_string basename;
- struct archive_string symlink;
- int ea_idx;
- struct {
- struct heap_data *first;
- struct heap_data **last;
- } xattr;
- struct heap_data data;
- struct archive_string script;
- unsigned int virtual:1;
- unsigned int dir:1;
- };
- struct hardlink {
- struct archive_rb_node rbnode;
- int nlink;
- struct {
- struct file *first;
- struct file **last;
- } file_list;
- };
- struct xar {
- int temp_fd;
- uint64_t temp_offset;
- int file_idx;
- struct file *root;
- struct file *cur_dirent;
- struct archive_string cur_dirstr;
- struct file *cur_file;
- uint64_t bytes_remaining;
- struct archive_string tstr;
- struct archive_string vstr;
- enum sumalg opt_toc_sumalg;
- enum sumalg opt_sumalg;
- enum enctype opt_compression;
- int opt_compression_level;
- uint32_t opt_threads;
- struct chksumwork a_sumwrk; /* archived checksum. */
- struct chksumwork e_sumwrk; /* extracted checksum. */
- struct la_zstream stream;
- struct archive_string_conv *sconv;
- /*
- * Compressed data buffer.
- */
- unsigned char wbuff[1024 * 64];
- size_t wbuff_remaining;
- struct heap_data toc;
- /*
- * The list of all file entries is used to manage struct file
- * objects.
- * We use 'next' (a member of struct file) to chain.
- */
- struct {
- struct file *first;
- struct file **last;
- } file_list;
- /*
- * The list of hard-linked file entries.
- * We use 'hlnext' (a member of struct file) to chain.
- */
- struct archive_rb_tree hardlink_rbtree;
- };
- static int xar_options(struct archive_write *,
- const char *, const char *);
- static int xar_write_header(struct archive_write *,
- struct archive_entry *);
- static ssize_t xar_write_data(struct archive_write *,
- const void *, size_t);
- static int xar_finish_entry(struct archive_write *);
- static int xar_close(struct archive_write *);
- static int xar_free(struct archive_write *);
- static struct file *file_new(struct archive_write *a, struct archive_entry *);
- static void file_free(struct file *);
- static struct file *file_create_virtual_dir(struct archive_write *a, struct xar *,
- const char *);
- static int file_add_child_tail(struct file *, struct file *);
- static struct file *file_find_child(struct file *, const char *);
- static int file_gen_utility_names(struct archive_write *,
- struct file *);
- static int get_path_component(char *, int, const char *);
- static int file_tree(struct archive_write *, struct file **);
- static void file_register(struct xar *, struct file *);
- static void file_init_register(struct xar *);
- static void file_free_register(struct xar *);
- static int file_register_hardlink(struct archive_write *,
- struct file *);
- static void file_connect_hardlink_files(struct xar *);
- static void file_init_hardlinks(struct xar *);
- static void file_free_hardlinks(struct xar *);
- static void checksum_init(struct chksumwork *, enum sumalg);
- static void checksum_update(struct chksumwork *, const void *, size_t);
- static void checksum_final(struct chksumwork *, struct chksumval *);
- static int compression_init_encoder_gzip(struct archive *,
- struct la_zstream *, int, int);
- static int compression_code_gzip(struct archive *,
- struct la_zstream *, enum la_zaction);
- static int compression_end_gzip(struct archive *, struct la_zstream *);
- static int compression_init_encoder_bzip2(struct archive *,
- struct la_zstream *, int);
- #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
- static int compression_code_bzip2(struct archive *,
- struct la_zstream *, enum la_zaction);
- static int compression_end_bzip2(struct archive *, struct la_zstream *);
- #endif
- static int compression_init_encoder_lzma(struct archive *,
- struct la_zstream *, int);
- static int compression_init_encoder_xz(struct archive *,
- struct la_zstream *, int, int);
- #if defined(HAVE_LZMA_H)
- static int compression_code_lzma(struct archive *,
- struct la_zstream *, enum la_zaction);
- static int compression_end_lzma(struct archive *, struct la_zstream *);
- #endif
- static int xar_compression_init_encoder(struct archive_write *);
- static int compression_code(struct archive *,
- struct la_zstream *, enum la_zaction);
- static int compression_end(struct archive *,
- struct la_zstream *);
- static int save_xattrs(struct archive_write *, struct file *);
- static int getalgsize(enum sumalg);
- static const char *getalgname(enum sumalg);
- int
- archive_write_set_format_xar(struct archive *_a)
- {
- struct archive_write *a = (struct archive_write *)_a;
- struct xar *xar;
- archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
- ARCHIVE_STATE_NEW, "archive_write_set_format_xar");
- /* If another format was already registered, unregister it. */
- if (a->format_free != NULL)
- (a->format_free)(a);
- xar = calloc(1, sizeof(*xar));
- if (xar == NULL) {
- archive_set_error(&a->archive, ENOMEM,
- "Can't allocate xar data");
- return (ARCHIVE_FATAL);
- }
- xar->temp_fd = -1;
- file_init_register(xar);
- file_init_hardlinks(xar);
- archive_string_init(&(xar->tstr));
- archive_string_init(&(xar->vstr));
- /*
- * Create the root directory.
- */
- xar->root = file_create_virtual_dir(a, xar, "");
- if (xar->root == NULL) {
- free(xar);
- archive_set_error(&a->archive, ENOMEM,
- "Can't allocate xar data");
- return (ARCHIVE_FATAL);
- }
- xar->root->parent = xar->root;
- file_register(xar, xar->root);
- xar->cur_dirent = xar->root;
- archive_string_init(&(xar->cur_dirstr));
- archive_string_ensure(&(xar->cur_dirstr), 1);
- xar->cur_dirstr.s[0] = 0;
- /*
- * Initialize option.
- */
- /* Set default checksum type. */
- xar->opt_toc_sumalg = CKSUM_SHA1;
- xar->opt_sumalg = CKSUM_SHA1;
- /* Set default compression type, level, and number of threads. */
- xar->opt_compression = GZIP;
- xar->opt_compression_level = 6;
- xar->opt_threads = 1;
- a->format_data = xar;
- a->format_name = "xar";
- a->format_options = xar_options;
- a->format_write_header = xar_write_header;
- a->format_write_data = xar_write_data;
- a->format_finish_entry = xar_finish_entry;
- a->format_close = xar_close;
- a->format_free = xar_free;
- a->archive.archive_format = ARCHIVE_FORMAT_XAR;
- a->archive.archive_format_name = "xar";
- return (ARCHIVE_OK);
- }
- static int
- xar_options(struct archive_write *a, const char *key, const char *value)
- {
- struct xar *xar;
- xar = (struct xar *)a->format_data;
- if (strcmp(key, "checksum") == 0) {
- if (value == NULL)
- xar->opt_sumalg = CKSUM_NONE;
- else if (strcmp(value, "none") == 0)
- xar->opt_sumalg = CKSUM_NONE;
- else if (strcmp(value, "sha1") == 0)
- xar->opt_sumalg = CKSUM_SHA1;
- else if (strcmp(value, "md5") == 0)
- xar->opt_sumalg = CKSUM_MD5;
- else {
- archive_set_error(&(a->archive),
- ARCHIVE_ERRNO_MISC,
- "Unknown checksum name: `%s'",
- value);
- return (ARCHIVE_FAILED);
- }
- return (ARCHIVE_OK);
- }
- if (strcmp(key, "compression") == 0) {
- const char *name = NULL;
- if (value == NULL)
- xar->opt_compression = NONE;
- else if (strcmp(value, "none") == 0)
- xar->opt_compression = NONE;
- else if (strcmp(value, "gzip") == 0)
- xar->opt_compression = GZIP;
- else if (strcmp(value, "bzip2") == 0)
- #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
- xar->opt_compression = BZIP2;
- #else
- name = "bzip2";
- #endif
- else if (strcmp(value, "lzma") == 0)
- #if HAVE_LZMA_H
- xar->opt_compression = LZMA;
- #else
- name = "lzma";
- #endif
- else if (strcmp(value, "xz") == 0)
- #if HAVE_LZMA_H
- xar->opt_compression = XZ;
- #else
- name = "xz";
- #endif
- else {
- archive_set_error(&(a->archive),
- ARCHIVE_ERRNO_MISC,
- "Unknown compression name: `%s'",
- value);
- return (ARCHIVE_FAILED);
- }
- if (name != NULL) {
- archive_set_error(&(a->archive),
- ARCHIVE_ERRNO_MISC,
- "`%s' compression not supported "
- "on this platform",
- name);
- return (ARCHIVE_FAILED);
- }
- return (ARCHIVE_OK);
- }
- if (strcmp(key, "compression-level") == 0) {
- if (value == NULL ||
- !(value[0] >= '0' && value[0] <= '9') ||
- value[1] != '\0') {
- archive_set_error(&(a->archive),
- ARCHIVE_ERRNO_MISC,
- "Illegal value `%s'",
- value);
- return (ARCHIVE_FAILED);
- }
- xar->opt_compression_level = value[0] - '0';
- return (ARCHIVE_OK);
- }
- if (strcmp(key, "toc-checksum") == 0) {
- if (value == NULL)
- xar->opt_toc_sumalg = CKSUM_NONE;
- else if (strcmp(value, "none") == 0)
- xar->opt_toc_sumalg = CKSUM_NONE;
- else if (strcmp(value, "sha1") == 0)
- xar->opt_toc_sumalg = CKSUM_SHA1;
- else if (strcmp(value, "md5") == 0)
- xar->opt_toc_sumalg = CKSUM_MD5;
- else {
- archive_set_error(&(a->archive),
- ARCHIVE_ERRNO_MISC,
- "Unknown checksum name: `%s'",
- value);
- return (ARCHIVE_FAILED);
- }
- return (ARCHIVE_OK);
- }
- if (strcmp(key, "threads") == 0) {
- char *endptr;
- if (value == NULL)
- return (ARCHIVE_FAILED);
- errno = 0;
- xar->opt_threads = (int)strtoul(value, &endptr, 10);
- if (errno != 0 || *endptr != '\0') {
- xar->opt_threads = 1;
- archive_set_error(&(a->archive),
- ARCHIVE_ERRNO_MISC,
- "Illegal value `%s'",
- value);
- return (ARCHIVE_FAILED);
- }
- if (xar->opt_threads == 0) {
- #ifdef HAVE_LZMA_STREAM_ENCODER_MT
- xar->opt_threads = lzma_cputhreads();
- #else
- xar->opt_threads = 1;
- #endif
- }
- }
- /* Note: The "warn" return is just to inform the options
- * supervisor that we didn't handle it. It will generate
- * a suitable error if no one used this option. */
- return (ARCHIVE_WARN);
- }
- static int
- xar_write_header(struct archive_write *a, struct archive_entry *entry)
- {
- struct xar *xar;
- struct file *file;
- struct archive_entry *file_entry;
- int r, r2;
- xar = (struct xar *)a->format_data;
- xar->cur_file = NULL;
- xar->bytes_remaining = 0;
- if (xar->sconv == NULL) {
- xar->sconv = archive_string_conversion_to_charset(
- &a->archive, "UTF-8", 1);
- if (xar->sconv == NULL)
- return (ARCHIVE_FATAL);
- }
- file = file_new(a, entry);
- if (file == NULL) {
- archive_set_error(&a->archive, ENOMEM,
- "Can't allocate data");
- return (ARCHIVE_FATAL);
- }
- r2 = file_gen_utility_names(a, file);
- if (r2 < ARCHIVE_WARN)
- return (r2);
- /*
- * Ignore a path which looks like the top of directory name
- * since we have already made the root directory of an Xar archive.
- */
- if (archive_strlen(&(file->parentdir)) == 0 &&
- archive_strlen(&(file->basename)) == 0) {
- file_free(file);
- return (r2);
- }
- /* Add entry into tree */
- file_entry = file->entry;
- r = file_tree(a, &file);
- if (r != ARCHIVE_OK)
- return (r);
- /* There is the same file in tree and
- * the current file is older than the file in tree.
- * So we don't need the current file data anymore. */
- if (file->entry != file_entry)
- return (r2);
- if (file->id == 0)
- file_register(xar, file);
- /* A virtual file, which is a directory, does not have
- * any contents and we won't store it into a archive
- * file other than its name. */
- if (file->virtual)
- return (r2);
- /*
- * Prepare to save the contents of the file.
- */
- if (xar->temp_fd == -1) {
- int algsize;
- xar->temp_offset = 0;
- xar->temp_fd = __archive_mktemp(NULL);
- if (xar->temp_fd < 0) {
- archive_set_error(&a->archive, errno,
- "Couldn't create temporary file");
- return (ARCHIVE_FATAL);
- }
- algsize = getalgsize(xar->opt_toc_sumalg);
- if (algsize > 0) {
- if (lseek(xar->temp_fd, algsize, SEEK_SET) < 0) {
- archive_set_error(&(a->archive), errno,
- "lseek failed");
- return (ARCHIVE_FATAL);
- }
- xar->temp_offset = algsize;
- }
- }
- if (archive_entry_hardlink(file->entry) == NULL) {
- r = save_xattrs(a, file);
- if (r != ARCHIVE_OK)
- return (ARCHIVE_FATAL);
- }
- /* Non regular files contents are unneeded to be saved to
- * a temporary file. */
- if (archive_entry_filetype(file->entry) != AE_IFREG)
- return (r2);
- /*
- * Set the current file to cur_file to read its contents.
- */
- xar->cur_file = file;
- if (archive_entry_nlink(file->entry) > 1) {
- r = file_register_hardlink(a, file);
- if (r != ARCHIVE_OK)
- return (r);
- if (archive_entry_hardlink(file->entry) != NULL) {
- archive_entry_unset_size(file->entry);
- return (r2);
- }
- }
- /* Save a offset of current file in temporary file. */
- file->data.temp_offset = xar->temp_offset;
- file->data.size = archive_entry_size(file->entry);
- file->data.compression = xar->opt_compression;
- xar->bytes_remaining = archive_entry_size(file->entry);
- checksum_init(&(xar->a_sumwrk), xar->opt_sumalg);
- checksum_init(&(xar->e_sumwrk), xar->opt_sumalg);
- r = xar_compression_init_encoder(a);
- if (r != ARCHIVE_OK)
- return (r);
- else
- return (r2);
- }
- static int
- write_to_temp(struct archive_write *a, const void *buff, size_t s)
- {
- struct xar *xar;
- const unsigned char *p;
- ssize_t ws;
- xar = (struct xar *)a->format_data;
- p = (const unsigned char *)buff;
- while (s) {
- ws = write(xar->temp_fd, p, s);
- if (ws < 0) {
- archive_set_error(&(a->archive), errno,
- "fwrite function failed");
- return (ARCHIVE_FATAL);
- }
- s -= ws;
- p += ws;
- xar->temp_offset += ws;
- }
- return (ARCHIVE_OK);
- }
- static ssize_t
- xar_write_data(struct archive_write *a, const void *buff, size_t s)
- {
- struct xar *xar;
- enum la_zaction run;
- size_t size = 0;
- size_t rsize;
- int r;
- xar = (struct xar *)a->format_data;
- if (s > xar->bytes_remaining)
- s = (size_t)xar->bytes_remaining;
- if (s == 0 || xar->cur_file == NULL)
- return (0);
- if (xar->cur_file->data.compression == NONE) {
- checksum_update(&(xar->e_sumwrk), buff, s);
- checksum_update(&(xar->a_sumwrk), buff, s);
- size = rsize = s;
- } else {
- xar->stream.next_in = (const unsigned char *)buff;
- xar->stream.avail_in = s;
- if (xar->bytes_remaining > s)
- run = ARCHIVE_Z_RUN;
- else
- run = ARCHIVE_Z_FINISH;
- /* Compress file data. */
- for (;;) {
- r = compression_code(&(a->archive), &(xar->stream),
- run);
- if (r != ARCHIVE_OK && r != ARCHIVE_EOF)
- return (ARCHIVE_FATAL);
- if (xar->stream.avail_out == 0 ||
- run == ARCHIVE_Z_FINISH) {
- size = sizeof(xar->wbuff) -
- xar->stream.avail_out;
- checksum_update(&(xar->a_sumwrk), xar->wbuff,
- size);
- xar->cur_file->data.length += size;
- if (write_to_temp(a, xar->wbuff,
- size) != ARCHIVE_OK)
- return (ARCHIVE_FATAL);
- if (r == ARCHIVE_OK) {
- /* Output buffer was full */
- xar->stream.next_out = xar->wbuff;
- xar->stream.avail_out =
- sizeof(xar->wbuff);
- } else {
- /* ARCHIVE_EOF - We are done */
- break;
- }
- } else {
- /* Compressor wants more input */
- break;
- }
- }
- rsize = s - xar->stream.avail_in;
- checksum_update(&(xar->e_sumwrk), buff, rsize);
- }
- #if !defined(_WIN32) || defined(__CYGWIN__)
- if (xar->bytes_remaining ==
- (uint64_t)archive_entry_size(xar->cur_file->entry)) {
- /*
- * Get the path of a shell script if so.
- */
- const unsigned char *b = (const unsigned char *)buff;
- archive_string_empty(&(xar->cur_file->script));
- if (rsize > 2 && b[0] == '#' && b[1] == '!') {
- size_t i, end, off;
- off = 2;
- if (b[off] == ' ')
- off++;
- #ifdef PATH_MAX
- if ((rsize - off) > PATH_MAX)
- end = off + PATH_MAX;
- else
- #endif
- end = rsize;
- /* Find the end of a script path. */
- for (i = off; i < end && b[i] != '\0' &&
- b[i] != '\n' && b[i] != '\r' &&
- b[i] != ' ' && b[i] != '\t'; i++)
- ;
- archive_strncpy(&(xar->cur_file->script), b + off,
- i - off);
- }
- }
- #endif
- if (xar->cur_file->data.compression == NONE) {
- if (write_to_temp(a, buff, size) != ARCHIVE_OK)
- return (ARCHIVE_FATAL);
- xar->cur_file->data.length += size;
- }
- xar->bytes_remaining -= rsize;
- return (rsize);
- }
- static int
- xar_finish_entry(struct archive_write *a)
- {
- struct xar *xar;
- struct file *file;
- size_t s;
- ssize_t w;
- xar = (struct xar *)a->format_data;
- if (xar->cur_file == NULL)
- return (ARCHIVE_OK);
- while (xar->bytes_remaining > 0) {
- s = (size_t)xar->bytes_remaining;
- if (s > a->null_length)
- s = a->null_length;
- w = xar_write_data(a, a->nulls, s);
- if (w > 0)
- xar->bytes_remaining -= w;
- else
- return ((int)w);
- }
- file = xar->cur_file;
- checksum_final(&(xar->e_sumwrk), &(file->data.e_sum));
- checksum_final(&(xar->a_sumwrk), &(file->data.a_sum));
- xar->cur_file = NULL;
- return (ARCHIVE_OK);
- }
- static int
- xmlwrite_string_attr(struct archive_write *a, struct xml_writer *writer,
- const char *key, const char *value,
- const char *attrkey, const char *attrvalue)
- {
- int r;
- r = xml_writer_start_element(writer, key);
- if (r < 0) {
- archive_set_error(&a->archive,
- ARCHIVE_ERRNO_MISC,
- "xml_writer_start_element() failed: %d", r);
- return (ARCHIVE_FATAL);
- }
- if (attrkey != NULL && attrvalue != NULL) {
- r = xml_writer_write_attribute(writer, attrkey, attrvalue);
- if (r < 0) {
- archive_set_error(&a->archive,
- ARCHIVE_ERRNO_MISC,
- "xml_writer_write_attribute() failed: %d", r);
- return (ARCHIVE_FATAL);
- }
- }
- if (value != NULL) {
- r = xml_writer_write_string(writer, value);
- if (r < 0) {
- archive_set_error(&a->archive,
- ARCHIVE_ERRNO_MISC,
- "xml_writer_write_string() failed: %d", r);
- return (ARCHIVE_FATAL);
- }
- }
- r = xml_writer_end_element(writer);
- if (r < 0) {
- archive_set_error(&a->archive,
- ARCHIVE_ERRNO_MISC,
- "xml_writer_end_element() failed: %d", r);
- return (ARCHIVE_FATAL);
- }
- return (ARCHIVE_OK);
- }
- static int
- xmlwrite_string(struct archive_write *a, struct xml_writer *writer,
- const char *key, const char *value)
- {
- int r;
- if (value == NULL)
- return (ARCHIVE_OK);
- r = xml_writer_start_element(writer, key);
- if (r < 0) {
- archive_set_error(&a->archive,
- ARCHIVE_ERRNO_MISC,
- "xml_writer_start_element() failed: %d", r);
- return (ARCHIVE_FATAL);
- }
- if (value != NULL) {
- r = xml_writer_write_string(writer, value);
- if (r < 0) {
- archive_set_error(&a->archive,
- ARCHIVE_ERRNO_MISC,
- "xml_writer_write_string() failed: %d", r);
- return (ARCHIVE_FATAL);
- }
- }
- r = xml_writer_end_element(writer);
- if (r < 0) {
- archive_set_error(&a->archive,
- ARCHIVE_ERRNO_MISC,
- "xml_writer_end_element() failed: %d", r);
- return (ARCHIVE_FATAL);
- }
- return (ARCHIVE_OK);
- }
- static int
- xmlwrite_fstring(struct archive_write *a, struct xml_writer *writer,
- const char *key, const char *fmt, ...)
- {
- struct xar *xar;
- va_list ap;
- xar = (struct xar *)a->format_data;
- va_start(ap, fmt);
- archive_string_empty(&xar->vstr);
- archive_string_vsprintf(&xar->vstr, fmt, ap);
- va_end(ap);
- return (xmlwrite_string(a, writer, key, xar->vstr.s));
- }
- static int
- xmlwrite_time(struct archive_write *a, struct xml_writer *writer,
- const char *key, time_t t, int z)
- {
- char timestr[100];
- struct tm tm;
- #if defined(HAVE_GMTIME_S)
- gmtime_s(&tm, &t);
- #elif defined(HAVE_GMTIME_R)
- gmtime_r(&t, &tm);
- #else
- memcpy(&tm, gmtime(&t), sizeof(tm));
- #endif
- memset(×tr, 0, sizeof(timestr));
- /* Do not use %F and %T for portability. */
- strftime(timestr, sizeof(timestr), "%Y-%m-%dT%H:%M:%S", &tm);
- if (z)
- strcat(timestr, "Z");
- return (xmlwrite_string(a, writer, key, timestr));
- }
- static int
- xmlwrite_mode(struct archive_write *a, struct xml_writer *writer,
- const char *key, mode_t mode)
- {
- char ms[5];
- ms[0] = '0';
- ms[1] = '0' + ((mode >> 6) & 07);
- ms[2] = '0' + ((mode >> 3) & 07);
- ms[3] = '0' + (mode & 07);
- ms[4] = '\0';
- return (xmlwrite_string(a, writer, key, ms));
- }
- static int
- xmlwrite_sum(struct archive_write *a, struct xml_writer *writer,
- const char *key, struct chksumval *sum)
- {
- const char *algname;
- int algsize;
- char buff[MAX_SUM_SIZE*2 + 1];
- char *p;
- unsigned char *s;
- int i, r;
- if (sum->len > 0) {
- algname = getalgname(sum->alg);
- algsize = getalgsize(sum->alg);
- if (algname != NULL) {
- const char *hex = "0123456789abcdef";
- p = buff;
- s = sum->val;
- for (i = 0; i < algsize; i++) {
- *p++ = hex[(*s >> 4)];
- *p++ = hex[(*s & 0x0f)];
- s++;
- }
- *p = '\0';
- r = xmlwrite_string_attr(a, writer,
- key, buff,
- "style", algname);
- if (r < 0)
- return (ARCHIVE_FATAL);
- }
- }
- return (ARCHIVE_OK);
- }
- static int
- xmlwrite_heap(struct archive_write *a, struct xml_writer *writer,
- struct heap_data *heap)
- {
- const char *encname;
- int r;
- r = xmlwrite_fstring(a, writer, "length", "%ju", heap->length);
- if (r < 0)
- return (ARCHIVE_FATAL);
- r = xmlwrite_fstring(a, writer, "offset", "%ju", heap->temp_offset);
- if (r < 0)
- return (ARCHIVE_FATAL);
- r = xmlwrite_fstring(a, writer, "size", "%ju", heap->size);
- if (r < 0)
- return (ARCHIVE_FATAL);
- switch (heap->compression) {
- case GZIP:
- encname = "application/x-gzip"; break;
- case BZIP2:
- encname = "application/x-bzip2"; break;
- case LZMA:
- encname = "application/x-lzma"; break;
- case XZ:
- encname = "application/x-xz"; break;
- default:
- encname = "application/octet-stream"; break;
- }
- r = xmlwrite_string_attr(a, writer, "encoding", NULL,
- "style", encname);
- if (r < 0)
- return (ARCHIVE_FATAL);
- r = xmlwrite_sum(a, writer, "archived-checksum", &(heap->a_sum));
- if (r < 0)
- return (ARCHIVE_FATAL);
- r = xmlwrite_sum(a, writer, "extracted-checksum", &(heap->e_sum));
- if (r < 0)
- return (ARCHIVE_FATAL);
- return (ARCHIVE_OK);
- }
- /*
- * xar utility records fflags as following xml elements:
- * <flags>
- * <UserNoDump/>
- * .....
- * </flags>
- * or
- * <ext2>
- * <NoDump/>
- * .....
- * </ext2>
- * If xar is running on BSD platform, records <flags>..</flags>;
- * if xar is running on linux platform, records <ext2>..</ext2>;
- * otherwise does not record.
- *
- * Our implements records both <flags> and <ext2> if it's necessary.
- */
- static int
- make_fflags_entry(struct archive_write *a, struct xml_writer *writer,
- const char *element, const char *fflags_text)
- {
- static const struct flagentry {
- const char *name;
- const char *xarname;
- }
- flagbsd[] = {
- { "sappnd", "SystemAppend"},
- { "sappend", "SystemAppend"},
- { "arch", "SystemArchived"},
- { "archived", "SystemArchived"},
- { "schg", "SystemImmutable"},
- { "schange", "SystemImmutable"},
- { "simmutable", "SystemImmutable"},
- { "nosunlnk", "SystemNoUnlink"},
- { "nosunlink", "SystemNoUnlink"},
- { "snapshot", "SystemSnapshot"},
- { "uappnd", "UserAppend"},
- { "uappend", "UserAppend"},
- { "uchg", "UserImmutable"},
- { "uchange", "UserImmutable"},
- { "uimmutable", "UserImmutable"},
- { "nodump", "UserNoDump"},
- { "noopaque", "UserOpaque"},
- { "nouunlnk", "UserNoUnlink"},
- { "nouunlink", "UserNoUnlink"},
- { NULL, NULL}
- },
- flagext2[] = {
- { "sappnd", "AppendOnly"},
- { "sappend", "AppendOnly"},
- { "schg", "Immutable"},
- { "schange", "Immutable"},
- { "simmutable", "Immutable"},
- { "nodump", "NoDump"},
- { "nouunlnk", "Undelete"},
- { "nouunlink", "Undelete"},
- { "btree", "BTree"},
- { "comperr", "CompError"},
- { "compress", "Compress"},
- { "noatime", "NoAtime"},
- { "compdirty", "CompDirty"},
- { "comprblk", "CompBlock"},
- { "dirsync", "DirSync"},
- { "hashidx", "HashIndexed"},
- { "imagic", "iMagic"},
- { "journal", "Journaled"},
- { "securedeletion", "SecureDeletion"},
- { "sync", "Synchronous"},
- { "notail", "NoTail"},
- { "topdir", "TopDir"},
- { "reserved", "Reserved"},
- { NULL, NULL}
- };
- const struct flagentry *fe, *flagentry;
- #define FLAGENTRY_MAXSIZE ((sizeof(flagbsd)+sizeof(flagext2))/sizeof(flagbsd))
- const struct flagentry *avail[FLAGENTRY_MAXSIZE];
- const char *p;
- int i, n, r;
- if (strcmp(element, "ext2") == 0)
- flagentry = flagext2;
- else
- flagentry = flagbsd;
- n = 0;
- p = fflags_text;
- do {
- const char *cp;
- cp = strchr(p, ',');
- if (cp == NULL)
- cp = p + strlen(p);
- for (fe = flagentry; fe->name != NULL; fe++) {
- if (fe->name[cp - p] != '\0'
- || p[0] != fe->name[0])
- continue;
- if (strncmp(p, fe->name, cp - p) == 0) {
- avail[n++] = fe;
- break;
- }
- }
- if (*cp == ',')
- p = cp + 1;
- else
- p = NULL;
- } while (p != NULL);
- if (n > 0) {
- r = xml_writer_start_element(writer, element);
- if (r < 0) {
- archive_set_error(&a->archive,
- ARCHIVE_ERRNO_MISC,
- "xml_writer_start_element() failed: %d", r);
- return (ARCHIVE_FATAL);
- }
- for (i = 0; i < n; i++) {
- r = xmlwrite_string(a, writer,
- avail[i]->xarname, NULL);
- if (r != ARCHIVE_OK)
- return (r);
- }
- r = xml_writer_end_element(writer);
- if (r < 0) {
- archive_set_error(&a->archive,
- ARCHIVE_ERRNO_MISC,
- "xml_writer_end_element() failed: %d", r);
- return (ARCHIVE_FATAL);
- }
- }
- return (ARCHIVE_OK);
- }
- /*
- * This function determines whether a UTF-8 string contains
- * only codepoints that are convertible to Latin-1. Strings
- * beyond Latin-1 are stored base64-encoded in the XAR TOC.
- */
- static int
- is_u8_zstring_latin1(const char *in)
- {
- unsigned int c;
- while (*in) {
- c = *in++;
- if (c < 0x80) continue;
- /*
- * Filter out non-continuation, any continuation of 2-3
- * bytes, and any continuation of 1 byte whose high 3 bits
- * are non-zero. Recall, 1-byte continuations can store 11
- * bits whereas Latin-1 codepoints are only 8 bits wide.
- */
- if ((c & 0xFC) != 0xC0)
- return (0);
- c = *in++;
- /*
- * If we get any non-continuation byte (including 0x00!),
- * the string is not valid UTF-8.
- */
- if ((c & 0xC0) != 0x80)
- return (0); /* invalid unicode */
- }
- return (1);
- }
- static int
- make_file_entry(struct archive_write *a, struct xml_writer *writer,
- struct file *file)
- {
- struct xar *xar;
- const char *filetype, *filelink, *fflags;
- struct archive_string linkto;
- struct heap_data *heap;
- const char *p;
- size_t len;
- int r, r2;
- xar = (struct xar *)a->format_data;
- r2 = ARCHIVE_OK;
- /*
- * Make a file name entry, "<name>".
- */
- if (!is_u8_zstring_latin1(file->basename.s)) {
- r = xml_writer_start_element(writer, "name");
- if (r < 0) {
- archive_set_error(&a->archive,
- ARCHIVE_ERRNO_MISC,
- "xml_writer_start_element() failed: %d", r);
- return (ARCHIVE_FATAL);
- }
- r = xml_writer_write_attribute(writer,
- "enctype", "base64");
- if (r < 0) {
- archive_set_error(&a->archive,
- ARCHIVE_ERRNO_MISC,
- "xml_writer_write_attribute() failed: %d", r);
- return (ARCHIVE_FATAL);
- }
- r = xml_writer_write_base64(writer, file->basename.s,
- 0, (int)archive_strlen(&(file->basename)));
- if (r < 0) {
- archive_set_error(&a->archive,
- ARCHIVE_ERRNO_MISC,
- "xml_writer_write_base64() failed: %d", r);
- return (ARCHIVE_FATAL);
- }
- r = xml_writer_end_element(writer);
- if (r < 0) {
- archive_set_error(&a->archive,
- ARCHIVE_ERRNO_MISC,
- "xml_writer_end_element() failed: %d", r);
- return (ARCHIVE_FATAL);
- }
- } else {
- r = xmlwrite_string(a, writer, "name", file->basename.s);
- if (r < 0)
- return (ARCHIVE_FATAL);
- }
- /*
- * Make a file type entry, "<type>".
- */
- filelink = NULL;
- archive_string_init(&linkto);
- switch (archive_entry_filetype(file->entry)) {
- case AE_IFDIR:
- filetype = "directory"; break;
- case AE_IFLNK:
- filetype = "symlink"; break;
- case AE_IFCHR:
- filetype = "character special"; break;
- case AE_IFBLK:
- filetype = "block special"; break;
- case AE_IFSOCK:
- filetype = "socket"; break;
- case AE_IFIFO:
- filetype = "fifo"; break;
- case AE_IFREG:
- default:
- if (file->hardlink_target != NULL) {
- filetype = "hardlink";
- filelink = "link";
- if (file->hardlink_target == file)
- archive_strcpy(&linkto, "original");
- else
- archive_string_sprintf(&linkto, "%d",
- file->hardlink_target->id);
- } else
- filetype = "file";
- break;
- }
- r = xmlwrite_string_attr(a, writer, "type", filetype,
- filelink, linkto.s);
- archive_string_free(&linkto);
- if (r < 0)
- return (ARCHIVE_FATAL);
- /*
- * On a virtual directory, we record "name" and "type" only.
- */
- if (file->virtual)
- return (ARCHIVE_OK);
- switch (archive_entry_filetype(file->entry)) {
- case AE_IFLNK:
- /*
- * xar utility has checked a file type, which
- * a symbolic-link file has referenced.
- * For example:
- * <link type="directory">../ref/</link>
- * The symlink target file is "../ref/" and its
- * file type is a directory.
- *
- * <link type="file">../f</link>
- * The symlink target file is "../f" and its
- * file type is a regular file.
- *
- * But our implementation cannot do it, and then we
- * always record that a attribute "type" is "broken",
- * for example:
- * <link type="broken">foo/bar</link>
- * It means "foo/bar" is not reachable.
- */
- r = xmlwrite_string_attr(a, writer, "link",
- file->symlink.s,
- "type", "broken");
- if (r < 0)
- return (ARCHIVE_FATAL);
- break;
- case AE_IFCHR:
- case AE_IFBLK:
- r = xml_writer_start_element(writer, "device");
- if (r < 0) {
- archive_set_error(&a->archive,
- ARCHIVE_ERRNO_MISC,
- "xml_writer_start_element() failed: %d", r);
- return (ARCHIVE_FATAL);
- }
- r = xmlwrite_fstring(a, writer, "major",
- "%d", archive_entry_rdevmajor(file->entry));
- if (r < 0)
- return (ARCHIVE_FATAL);
- r = xmlwrite_fstring(a, writer, "minor",
- "%d", archive_entry_rdevminor(file->entry));
- if (r < 0)
- return (ARCHIVE_FATAL);
- r = xml_writer_end_element(writer);
- if (r < 0) {
- archive_set_error(&a->archive,
- ARCHIVE_ERRNO_MISC,
- "xml_writer_end_element() failed: %d", r);
- return (ARCHIVE_FATAL);
- }
- break;
- default:
- break;
- }
- /*
- * Make a inode entry, "<inode>".
- */
- r = xmlwrite_fstring(a, writer, "inode",
- "%jd", archive_entry_ino64(file->entry));
- if (r < 0)
- return (ARCHIVE_FATAL);
- if (archive_entry_dev(file->entry) != 0) {
- r = xmlwrite_fstring(a, writer, "deviceno",
- "%d", archive_entry_dev(file->entry));
- if (r < 0)
- return (ARCHIVE_FATAL);
- }
- /*
- * Make a file mode entry, "<mode>".
- */
- r = xmlwrite_mode(a, writer, "mode",
- archive_entry_mode(file->entry));
- if (r < 0)
- return (ARCHIVE_FATAL);
- /*
- * Make a user entry, "<uid>" and "<user>.
- */
- r = xmlwrite_fstring(a, writer, "uid",
- "%d", archive_entry_uid(file->entry));
- if (r < 0)
- return (ARCHIVE_FATAL);
- r = archive_entry_uname_l(file->entry, &p, &len, xar->sconv);
- if (r != 0) {
- if (errno == ENOMEM) {
- archive_set_error(&a->archive, ENOMEM,
- "Can't allocate memory for Uname");
- return (ARCHIVE_FATAL);
- }
- archive_set_error(&a->archive,
- ARCHIVE_ERRNO_FILE_FORMAT,
- "Can't translate uname '%s' to UTF-8",
- archive_entry_uname(file->entry));
- r2 = ARCHIVE_WARN;
- }
- if (len > 0) {
- r = xmlwrite_string(a, writer, "user", p);
- if (r < 0)
- return (ARCHIVE_FATAL);
- }
- /*
- * Make a group entry, "<gid>" and "<group>.
- */
- r = xmlwrite_fstring(a, writer, "gid",
- "%d", archive_entry_gid(file->entry));
- if (r < 0)
- return (ARCHIVE_FATAL);
- r = archive_entry_gname_l(file->entry, &p, &len, xar->sconv);
- if (r != 0) {
- if (errno == ENOMEM) {
- archive_set_error(&a->archive, ENOMEM,
- "Can't allocate memory for Gname");
- return (ARCHIVE_FATAL);
- }
- archive_set_error(&a->archive,
- ARCHIVE_ERRNO_FILE_FORMAT,
- "Can't translate gname '%s' to UTF-8",
- archive_entry_gname(file->entry));
- r2 = ARCHIVE_WARN;
- }
- if (len > 0) {
- r = xmlwrite_string(a, writer, "group", p);
- if (r < 0)
- return (ARCHIVE_FATAL);
- }
- /*
- * Make a ctime entry, "<ctime>".
- */
- if (archive_entry_ctime_is_set(file->entry)) {
- r = xmlwrite_time(a, writer, "ctime",
- archive_entry_ctime(file->entry), 1);
- if (r < 0)
- return (ARCHIVE_FATAL);
- }
- /*
- * Make a mtime entry, "<mtime>".
- */
- if (archive_entry_mtime_is_set(file->entry)) {
- r = xmlwrite_time(a, writer, "mtime",
- archive_entry_mtime(file->entry), 1);
- if (r < 0)
- return (ARCHIVE_FATAL);
- }
- /*
- * Make a atime entry, "<atime>".
- */
- if (archive_entry_atime_is_set(file->entry)) {
- r = xmlwrite_time(a, writer, "atime",
- archive_entry_atime(file->entry), 1);
- if (r < 0)
- return (ARCHIVE_FATAL);
- }
- /*
- * Make fflags entries, "<flags>" and "<ext2>".
- */
- fflags = archive_entry_fflags_text(file->entry);
- if (fflags != NULL) {
- r = make_fflags_entry(a, writer, "flags", fflags);
- if (r < 0)
- return (r);
- r = make_fflags_entry(a, writer, "ext2", fflags);
- if (r < 0)
- return (r);
- }
- /*
- * Make extended attribute entries, "<ea>".
- */
- archive_entry_xattr_reset(file->entry);
- for (heap = file->xattr.first; heap != NULL; heap = heap->next) {
- const char *name;
- const void *value;
- size_t size;
- archive_entry_xattr_next(file->entry,
- &name, &value, &size);
- r = xml_writer_start_element(writer, "ea");
- if (r < 0) {
- archive_set_error(&a->archive,
- ARCHIVE_ERRNO_MISC,
- "xml_writer_start_element() failed: %d", r);
- return (ARCHIVE_FATAL);
- }
- r = xml_writer_write_attributef(writer,
- "id", "%d", heap->id);
- if (r < 0) {
- archive_set_error(&a->archive,
- ARCHIVE_ERRNO_MISC,
- "xml_writer_write_attributef() failed: %d", r);
- return (ARCHIVE_FATAL);
- }
- r = xmlwrite_heap(a, writer, heap);
- if (r < 0)
- return (ARCHIVE_FATAL);
- r = xmlwrite_string(a, writer, "name", name);
- if (r < 0)
- return (ARCHIVE_FATAL);
- r = xml_writer_end_element(writer);
- if (r < 0) {
- archive_set_error(&a->archive,
- ARCHIVE_ERRNO_MISC,
- "xml_writer_end_element() failed: %d", r);
- return (ARCHIVE_FATAL);
- }
- }
- /*
- * Make a file data entry, "<data>".
- */
- if (file->data.length > 0) {
- r = xml_writer_start_element(writer, "data");
- if (r < 0) {
- archive_set_error(&a->archive,
- ARCHIVE_ERRNO_MISC,
- "xml_writer_start_element() failed: %d", r);
- return (ARCHIVE_FATAL);
- }
- r = xmlwrite_heap(a, writer, &(file->data));
- if (r < 0)
- return (ARCHIVE_FATAL);
- r = xml_writer_end_element(writer);
- if (r < 0) {
- archive_set_error(&a->archive,
- ARCHIVE_ERRNO_MISC,
- "xml_writer_end_element() failed: %d", r);
- return (ARCHIVE_FATAL);
- }
- }
- if (archive_strlen(&file->script) > 0) {
- r = xml_writer_start_element(writer, "content");
- if (r < 0) {
- archive_set_error(&a->archive,
- ARCHIVE_ERRNO_MISC,
- "xml_writer_start_element() failed: %d", r);
- return (ARCHIVE_FATAL);
- }
- r = xmlwrite_string(a, writer,
- "interpreter", file->script.s);
- if (r < 0)
- return (ARCHIVE_FATAL);
- r = xmlwrite_string(a, writer, "type", "script");
- if (r < 0)
- return (ARCHIVE_FATAL);
- r = xml_writer_end_element(writer);
- if (r < 0) {
- archive_set_error(&a->archive,
- ARCHIVE_ERRNO_MISC,
- "xml_writer_end_element() failed: %d", r);
- return (ARCHIVE_FATAL);
- }
- }
- return (r2);
- }
- /*
- * Make the TOC
- */
- static int
- make_toc(struct archive_write *a)
- {
- struct xar *xar;
- struct file *np;
- struct xml_writer *writer;
- const char* content;
- size_t use;
- int algsize;
- int r, ret;
- xar = (struct xar *)a->format_data;
- ret = ARCHIVE_FATAL;
- /*
- * Initialize xml writer.
- */
- writer = NULL;
- r = xml_writer_create(&writer);
- if (r < 0) {
- archive_set_error(&a->archive,
- ARCHIVE_ERRNO_MISC,
- "xml_writer_create() failed: %d", r);
- goto exit_toc;
- }
- r = xml_writer_set_indent(writer, 4);
- if (r < 0) {
- archive_set_error(&a->archive,
- ARCHIVE_ERRNO_MISC,
- "xml_writer_set_indent() failed: %d", r);
- goto exit_toc;
- }
- r = xml_writer_start_document(writer);
- if (r < 0) {
- archive_set_error(&a->archive,
- ARCHIVE_ERRNO_MISC,
- "xml_writer_start_document() failed: %d", r);
- goto exit_toc;
- }
- /*
- * Start recording TOC
- */
- r = xml_writer_start_element(writer, "xar");
- if (r < 0) {
- archive_set_error(&a->archive,
- ARCHIVE_ERRNO_MISC,
- "xml_writer_start_element() failed: %d", r);
- goto exit_toc;
- }
- r = xml_writer_start_element(writer, "toc");
- if (r < 0) {
- archive_set_error(&a->archive,
- ARCHIVE_ERRNO_MISC,
- "xml_writer_start_element() failed: %d", r);
- goto exit_toc;
- }
- /*
- * Record the creation time of the archive file.
- */
- r = xmlwrite_time(a, writer, "creation-time", time(NULL), 0);
- if (r < 0)
- goto exit_toc;
- /*
- * Record the checksum value of TOC
- */
- algsize = getalgsize(xar->opt_toc_sumalg);
- if (algsize) {
- /*
- * Record TOC checksum
- */
- r = xml_writer_start_element(writer, "checksum");
- if (r < 0) {
- archive_set_error(&a->archive,
- ARCHIVE_ERRNO_MISC,
- "xml_writer_start_element() failed: %d", r);
- goto exit_toc;
- }
- r = xml_writer_write_attribute(writer, "style",
- getalgname(xar->opt_toc_sumalg));
- if (r < 0) {
- archive_set_error(&a->archive,
- ARCHIVE_ERRNO_MISC,
- "xml_writer_write_attribute() failed: %d", r);
- goto exit_toc;
- }
- /*
- * Record the offset of the value of checksum of TOC
- */
- r = xmlwrite_string(a, writer, "offset", "0");
- if (r < 0)
- goto exit_toc;
- /*
- * Record the size of the value of checksum of TOC
- */
- r = xmlwrite_fstring(a, writer, "size", "%d", algsize);
- if (r < 0)
- goto exit_toc;
- r = xml_writer_end_element(writer);
- if (r < 0) {
- archive_set_error(&a->archive,
- ARCHIVE_ERRNO_MISC,
- "xml_writer_end_element() failed: %d", r);
- goto exit_toc;
- }
- }
- np = xar->root;
- do {
- if (np != np->parent) {
- r = make_file_entry(a, writer, np);
- if (r != ARCHIVE_OK)
- goto exit_toc;
- }
- if (np->dir && np->children.first != NULL) {
- /* Enter to sub directories. */
- np = np->children.first;
- r = xml_writer_start_element(writer,
- "file");
- if (r < 0) {
- archive_set_error(&a->archive,
- ARCHIVE_ERRNO_MISC,
- "xml_writer_start_element() "
- "failed: %d", r);
- goto exit_toc;
- }
- r = xml_writer_write_attributef(
- writer, "id", "%d", np->id);
- if (r < 0) {
- archive_set_error(&a->archive,
- ARCHIVE_ERRNO_MISC,
- "xml_writer_write_attributef() "
- "failed: %d", r);
- goto exit_toc;
- }
- continue;
- }
- while (np != np->parent) {
- r = xml_writer_end_element(writer);
- if (r < 0) {
- archive_set_error(&a->archive,
- ARCHIVE_ERRNO_MISC,
- "xml_writer_end_element() "
- "failed: %d", r);
- goto exit_toc;
- }
- if (np->chnext == NULL) {
- /* Return to the parent directory. */
- np = np->parent;
- } else {
- np = np->chnext;
- r = xml_writer_start_element(writer,
- "file");
- if (r < 0) {
- archive_set_error(&a->archive,
- ARCHIVE_ERRNO_MISC,
- "xml_writer_start_element() "
- "failed: %d", r);
- goto exit_toc;
- }
- r = xml_writer_write_attributef(
- writer, "id", "%d", np->id);
- if (r < 0) {
- archive_set_error(&a->archive,
- ARCHIVE_ERRNO_MISC,
- "xml_writer_write_attributef() "
- "failed: %d", r);
- goto exit_toc;
- }
- break;
- }
- }
- } while (np != np->parent);
- r = xml_writer_end_document(writer);
- if (r < 0) {
- archive_set_error(&a->archive,
- ARCHIVE_ERRNO_MISC,
- "xml_writer_end_document() failed: %d", r);
- goto exit_toc;
- }
- r = xml_writer_get_final_content_and_length(writer, &content, &use);
- if (r < 0) {
- archive_set_error(&a->archive,
- ARCHIVE_ERRNO_MISC,
- "xml_writer_get_final_content_and_length() failed: %d", r);
- goto exit_toc;
- }
- #if DEBUG_PRINT_TOC
- fprintf(stderr, "\n---TOC-- %d bytes --\n%s\n",
- (int)strlen(content), content);
- #endif
- /*
- * Compress the TOC and calculate the sum of the TOC.
- */
- xar->toc.temp_offset = xar->temp_offset;
- xar->toc.size = (uint64_t)use;
- checksum_init(&(xar->a_sumwrk), xar->opt_toc_sumalg);
- r = compression_init_encoder_gzip(&(a->archive),
- &(xar->stream), 6, 1);
- if (r != ARCHIVE_OK)
- goto exit_toc;
- xar->stream.next_in = (const unsigned char *)content;
- xar->stream.avail_in = use;
- xar->stream.total_in = 0;
- xar->stream.next_out = xar->wbuff;
- xar->stream.avail_out = sizeof(xar->wbuff);
- xar->stream.total_out = 0;
- for (;;) {
- size_t size;
- r = compression_code(&(a->archive),
- &(xar->stream), ARCHIVE_Z_FINISH);
- if (r != ARCHIVE_OK && r != ARCHIVE_EOF)
- goto exit_toc;
- size = sizeof(xar->wbuff) - xar->stream.avail_out;
- checksum_update(&(xar->a_sumwrk), xar->wbuff, size);
- if (write_to_temp(a, xar->wbuff, size) != ARCHIVE_OK)
- goto exit_toc;
- if (r == ARCHIVE_EOF)
- break;
- xar->stream.next_out = xar->wbuff;
- xar->stream.avail_out = sizeof(xar->wbuff);
- }
- r = compression_end(&(a->archive), &(xar->stream));
- if (r != ARCHIVE_OK)
- goto exit_toc;
- xar->toc.length = xar->stream.total_out;
- xar->toc.compression = GZIP;
- checksum_final(&(xar->a_sumwrk), &(xar->toc.a_sum));
- ret = ARCHIVE_OK;
- exit_toc:
- if (writer)
- xml_writer_destroy(writer);
- return (ret);
- }
- static int
- flush_wbuff(struct archive_write *a)
- {
- struct xar *xar;
- int r;
- size_t s;
- xar = (struct xar *)a->format_data;
- s = sizeof(xar->wbuff) - xar->wbuff_remaining;
- r = __archive_write_output(a, xar->wbuff, s);
- if (r != ARCHIVE_OK)
- return (r);
- xar->wbuff_remaining = sizeof(xar->wbuff);
- return (r);
- }
- static int
- copy_out(struct archive_write *a, uint64_t offset, uint64_t length)
- {
- struct xar *xar;
- int r;
- xar = (struct xar *)a->format_data;
- if (lseek(xar->temp_fd, offset, SEEK_SET) < 0) {
- archive_set_error(&(a->archive), errno, "lseek failed");
- return (ARCHIVE_FATAL);
- }
- while (length) {
- size_t rsize;
- ssize_t rs;
- unsigned char *wb;
- if (length > xar->wbuff_remaining)
- rsize = xar->wbuff_remaining;
- else
- rsize = (size_t)length;
- wb = xar->wbuff + (sizeof(xar->wbuff) - xar->wbuff_remaining);
- rs = read(xar->temp_fd, wb, rsize);
- if (rs < 0) {
- archive_set_error(&(a->archive), errno,
- "Can't read temporary file(%jd)",
- (intmax_t)rs);
- return (ARCHIVE_FATAL);
- }
- if (rs == 0) {
- archive_set_error(&(a->archive), 0,
- "Truncated xar archive");
- return (ARCHIVE_FATAL);
- }
- xar->wbuff_remaining -= rs;
- length -= rs;
- if (xar->wbuff_remaining == 0) {
- r = flush_wbuff(a);
- if (r != ARCHIVE_OK)
- return (r);
- }
- }
- return (ARCHIVE_OK);
- }
- static int
- xar_close(struct archive_write *a)
- {
- struct xar *xar;
- unsigned char *wb;
- uint64_t length;
- int r;
- xar = (struct xar *)a->format_data;
- /* Empty! */
- if (xar->root->children.first == NULL)
- return (ARCHIVE_OK);
- /* Save the length of all file extended attributes and contents. */
- length = xar->temp_offset;
- /* Connect hardlinked files */
- file_connect_hardlink_files(xar);
- /* Make the TOC */
- r = make_toc(a);
- if (r != ARCHIVE_OK)
- return (r);
- /*
- * Make the xar header on wbuff(write buffer).
- */
- wb = xar->wbuff;
- xar->wbuff_remaining = sizeof(xar->wbuff);
- archive_be32enc(&wb[0], HEADER_MAGIC);
- archive_be16enc(&wb[4], HEADER_SIZE);
- archive_be16enc(&wb[6], HEADER_VERSION);
- archive_be64enc(&wb[8], xar->toc.length);
- archive_be64enc(&wb[16], xar->toc.size);
- archive_be32enc(&wb[24], xar->toc.a_sum.alg);
- xar->wbuff_remaining -= HEADER_SIZE;
- /*
- * Write the TOC
- */
- r = copy_out(a, xar->toc.temp_offset, xar->toc.length);
- if (r != ARCHIVE_OK)
- return (r);
- /* Write the checksum value of the TOC. */
- if (xar->toc.a_sum.len) {
- if (xar->wbuff_remaining < xar->toc.a_sum.len) {
- r = flush_wbuff(a);
- if (r != ARCHIVE_OK)
- return (r);
- }
- wb = xar->wbuff + (sizeof(xar->wbuff) - xar->wbuff_remaining);
- memcpy(wb, xar->toc.a_sum.val, xar->toc.a_sum.len);
- xar->wbuff_remaining -= xar->toc.a_sum.len;
- }
- /*
- * Write all file extended attributes and contents.
- */
- r = copy_out(a, xar->toc.a_sum.len, length);
- if (r != ARCHIVE_OK)
- return (r);
- r = flush_wbuff(a);
- return (r);
- }
- static int
- xar_free(struct archive_write *a)
- {
- struct xar *xar;
- xar = (struct xar *)a->format_data;
- /* Close the temporary file. */
- if (xar->temp_fd >= 0)
- close(xar->temp_fd);
- archive_string_free(&(xar->cur_dirstr));
- archive_string_free(&(xar->tstr));
- archive_string_free(&(xar->vstr));
- file_free_hardlinks(xar);
- file_free_register(xar);
- compression_end(&(a->archive), &(xar->stream));
- free(xar);
- return (ARCHIVE_OK);
- }
- static int
- file_cmp_node(const struct archive_rb_node *n1,
- const struct archive_rb_node *n2)
- {
- const struct file *f1 = (const struct file *)n1;
- const struct file *f2 = (const struct file *)n2;
- return (strcmp(f1->basename.s, f2->basename.s));
- }
- static int
- file_cmp_key(const struct archive_rb_node *n, const void *key)
- {
- const struct file *f = (const struct file *)n;
- return (strcmp(f->basename.s, (const char *)key));
- }
- static struct file *
- file_new(struct archive_write *a, struct archive_entry *entry)
- {
- struct file *file;
- static const struct archive_rb_tree_ops rb_ops = {
- file_cmp_node, file_cmp_key
- };
- file = calloc(1, sizeof(*file));
- if (file == NULL)
- return (NULL);
- if (entry != NULL)
- file->entry = archive_entry_clone(entry);
- else
- file->entry = archive_entry_new2(&a->archive);
- if (file->entry == NULL) {
- free(file);
- return (NULL);
- }
- __archive_rb_tree_init(&(file->rbtree), &rb_ops);
- file->children.first = NULL;
- file->children.last = &(file->children.first);
- file->xattr.first = NULL;
- file->xattr.last = &(file->xattr.first);
- archive_string_init(&(file->parentdir));
- archive_string_init(&(file->basename));
- archive_string_init(&(file->symlink));
- archive_string_init(&(file->script));
- if (entry != NULL && archive_entry_filetype(entry) == AE_IFDIR)
- file->dir = 1;
- return (file);
- }
- static void
- file_free(struct file *file)
- {
- struct heap_data *heap, *next_heap;
- heap = file->xattr.first;
- while (heap != NULL) {
- next_heap = heap->next;
- free(heap);
- heap = next_heap;
- }
- archive_string_free(&(file->parentdir));
- archive_string_free(&(file->basename));
- archive_string_free(&(file->symlink));
- archive_string_free(&(file->script));
- archive_entry_free(file->entry);
- free(file);
- }
- static struct file *
- file_create_virtual_dir(struct archive_write *a, struct xar *xar,
- const char *pathname)
- {
- struct file *file;
- (void)xar; /* UNUSED */
- file = file_new(a, NULL);
- if (file == NULL)
- return (NULL);
- archive_entry_set_pathname(file->entry, pathname);
- archive_entry_set_mode(file->entry, 0555 | AE_IFDIR);
- file->dir = 1;
- file->virtual = 1;
- return (file);
- }
- static int
- file_add_child_tail(struct file *parent, struct file *child)
- {
- if (!__archive_rb_tree_insert_node(
- &(parent->rbtree), (struct archive_rb_node *)child))
- return (0);
- child->chnext = NULL;
- *parent->children.last = child;
- parent->children.last = &(child->chnext);
- child->parent = parent;
- return (1);
- }
- /*
- * Find a entry from `parent'
- */
- static struct file *
- file_find_child(struct file *parent, const char *child_name)
- {
- struct file *np;
- np = (struct file *)__archive_rb_tree_find_node(
- &(parent->rbtree), child_name);
- return (np);
- }
- #if defined(_WIN32) || defined(__CYGWIN__)
- static void
- cleanup_backslash(char *utf8, size_t len)
- {
- /* Convert a path-separator from '\' to '/' */
- while (*utf8 != '\0' && len) {
- if (*utf8 == '\\')
- *utf8 = '/';
- ++utf8;
- --len;
- }
- }
- #else
- #define cleanup_backslash(p, len) /* nop */
- #endif
- /*
- * Generate a parent directory name and a base name from a pathname.
- */
- static int
- file_gen_utility_names(struct archive_write *a, struct file *file)
- {
- struct xar *xar;
- const char *pp;
- char *p, *dirname, *slash;
- size_t len;
- int r = ARCHIVE_OK;
- xar = (struct xar *)a->format_data;
- archive_string_empty(&(file->parentdir));
- archive_string_empty(&(file->basename));
- archive_string_empty(&(file->symlink));
- if (file->parent == file)/* virtual root */
- return (ARCHIVE_OK);
- if (archive_entry_pathname_l(file->entry, &pp, &len, xar->sconv)
- != 0) {
- if (errno == ENOMEM) {
- archive_set_error(&a->archive, ENOMEM,
- "Can't allocate memory for Pathname");
- return (ARCHIVE_FATAL);
- }
- archive_set_error(&a->archive,
- ARCHIVE_ERRNO_FILE_FORMAT,
- "Can't translate pathname '%s' to UTF-8",
- archive_entry_pathname(file->entry));
- r = ARCHIVE_WARN;
- }
- archive_strncpy(&(file->parentdir), pp, len);
- len = file->parentdir.length;
- p = dirname = file->parentdir.s;
- /*
- * Convert a path-separator from '\' to '/'
- */
- cleanup_backslash(p, len);
- /*
- * Remove leading '/', '../' and './' elements
- */
- while (*p) {
- if (p[0] == '/') {
- p++;
- len--;
- } else if (p[0] != '.')
- break;
- else if (p[1] == '.' && p[2] == '/') {
- p += 3;
- len -= 3;
- } else if (p[1] == '/' || (p[1] == '.' && p[2] == '\0')) {
- p += 2;
- len -= 2;
- } else if (p[1] == '\0') {
- p++;
- len--;
- } else
- break;
- }
- if (p != dirname) {
- memmove(dirname, p, len+1);
- p = dirname;
- }
- /*
- * Remove "/","/." and "/.." elements from tail.
- */
- while (len > 0) {
- size_t ll = len;
- if (p[len-1] == '/') {
- p[len-1] = '\0';
- len--;
- }
- if (len > 1 && p[len-2] == '/' && p[len-1] == '.') {
- p[len-2] = '\0';
- len -= 2;
- }
- if (len > 2 && p[len-3] == '/' && p[len-2] == '.' &&
- p[len-1] == '.') {
- p[len-3] = '\0';
- len -= 3;
- }
- if (ll == len)
- break;
- }
- while (*p) {
- if (p[0] == '/') {
- if (p[1] == '/')
- /* Convert '//' --> '/' */
- memmove(p, p+1, strlen(p+1) + 1);
- else if (p[1] == '.' && p[2] == '/')
- /* Convert '/./' --> '/' */
- memmove(p, p+2, strlen(p+2) + 1);
- else if (p[1] == '.' && p[2] == '.' && p[3] == '/') {
- /* Convert 'dir/dir1/../dir2/'
- * --> 'dir/dir2/'
- */
- char *rp = p -1;
- while (rp >= dirname) {
- if (*rp == '/')
- break;
- --rp;
- }
- if (rp > dirname) {
- strcpy(rp, p+3);
- p = rp;
- } else {
- strcpy(dirname, p+4);
- p = dirname;
- }
- } else
- p++;
- } else
- p++;
- }
- p = dirname;
- len = strlen(p);
- if (archive_entry_filetype(file->entry) == AE_IFLNK) {
- size_t len2;
- /* Convert symlink name too. */
- if (archive_entry_symlink_l(file->entry, &pp, &len2,
- xar->sconv) != 0) {
- if (errno == ENOMEM) {
- archive_set_error(&a->archive, ENOMEM,
- "Can't allocate memory for Linkname");
- return (ARCHIVE_FATAL);
- }
- archive_set_error(&a->archive,
- ARCHIVE_ERRNO_FILE_FORMAT,
- "Can't translate symlink '%s' to UTF-8",
- archive_entry_symlink(file->entry));
- r = ARCHIVE_WARN;
- }
- archive_strncpy(&(file->symlink), pp, len2);
- cleanup_backslash(file->symlink.s, file->symlink.length);
- }
- /*
- * - Count up directory elements.
- * - Find out the position which points the last position of
- * path separator('/').
- */
- slash = NULL;
- for (; *p != '\0'; p++)
- if (*p == '/')
- slash = p;
- if (slash == NULL) {
- /* The pathname doesn't have a parent directory. */
- file->parentdir.length = len;
- archive_string_copy(&(file->basename), &(file->parentdir));
- archive_string_empty(&(file->parentdir));
- *file->parentdir.s = '\0';
- return (r);
- }
- /* Make a basename from dirname and slash */
- *slash = '\0';
- file->parentdir.length = slash - dirname;
- archive_strcpy(&(file->basename), slash + 1);
- return (r);
- }
- static int
- get_path_component(char *name, int n, const char *fn)
- {
- char *p;
- int l;
- p = strchr(fn, '/');
- if (p == NULL) {
- if ((l = (int)strlen(fn)) == 0)
- return (0);
- } else
- l = (int)(p - fn);
- if (l > n -1)
- return (-1);
- memcpy(name, fn, l);
- name[l] = '\0';
- return (l);
- }
- /*
- * Add a new entry into the tree.
- */
- static int
- file_tree(struct archive_write *a, struct file **filepp)
- {
- #if defined(_WIN32) && !defined(__CYGWIN__)
- char name[_MAX_FNAME];/* Included null terminator size. */
- #elif defined(NAME_MAX) && NAME_MAX >= 255
- char name[NAME_MAX+1];
- #else
- char name[256];
- #endif
- struct xar *xar = (struct xar *)a->format_data;
- struct file *dent, *file, *np;
- struct archive_entry *ent;
- const char *fn, *p;
- int l;
- file = *filepp;
- dent = xar->root;
- if (file->parentdir.length > 0)
- fn = p = file->parentdir.s;
- else
- fn = p = "";
- /*
- * If the path of the parent directory of `file' entry is
- * the same as the path of `cur_dirent', add isoent to
- * `cur_dirent'.
- */
- if (archive_strlen(&(xar->cur_dirstr))
- == archive_strlen(&(file->parentdir)) &&
- strcmp(xar->cur_dirstr.s, fn) == 0) {
- if (!file_add_child_tail(xar->cur_dirent, file)) {
- np = (struct file *)__archive_rb_tree_find_node(
- &(xar->cur_dirent->rbtree),
- file->basename.s);
- goto same_entry;
- }
- return (ARCHIVE_OK);
- }
- for (;;) {
- l = get_path_component(name, sizeof(name), fn);
- if (l == 0) {
- np = NULL;
- break;
- }
- if (l < 0) {
- archive_set_error(&a->archive,
- ARCHIVE_ERRNO_MISC,
- "A name buffer is too small");
- file_free(file);
- *filepp = NULL;
- return (ARCHIVE_FATAL);
- }
- np = file_find_child(dent, name);
- if (np == NULL || fn[0] == '\0')
- break;
- /* Find next subdirectory. */
- if (!np->dir) {
- /* NOT Directory! */
- archive_set_error(&a->archive,
- ARCHIVE_ERRNO_MISC,
- "`%s' is not directory, we cannot insert `%s' ",
- archive_entry_pathname(np->entry),
- archive_entry_pathname(file->entry));
- file_free(file);
- *filepp = NULL;
- return (ARCHIVE_FAILED);
- }
- fn += l;
- if (fn[0] == '/')
- fn++;
- dent = np;
- }
- if (np == NULL) {
- /*
- * Create virtual parent directories.
- */
- while (fn[0] != '\0') {
- struct file *vp;
- struct archive_string as;
- archive_string_init(&as);
- archive_strncat(&as, p, fn - p + l);
- if (as.s[as.length-1] == '/') {
- as.s[as.length-1] = '\0';
- as.length--;
- }
- vp = file_create_virtual_dir(a, xar, as.s);
- if (vp == NULL) {
- archive_string_free(&as);
- archive_set_error(&a->archive, ENOMEM,
- "Can't allocate memory");
- file_free(file);
- *filepp = NULL;
- return (ARCHIVE_FATAL);
- }
- archive_string_free(&as);
- if (file_gen_utility_names(a, vp) <= ARCHIVE_FAILED)
- return (ARCHIVE_FATAL);
- file_add_child_tail(dent, vp);
- file_register(xar, vp);
- np = vp;
- fn += l;
- if (fn[0] == '/')
- fn++;
- l = get_path_component(name, sizeof(name), fn);
- if (l < 0) {
- archive_string_free(&as);
- archive_set_error(&a->archive,
- ARCHIVE_ERRNO_MISC,
- "A name buffer is too small");
- file_free(file);
- *filepp = NULL;
- return (ARCHIVE_FATAL);
- }
- dent = np;
- }
- /* Found out the parent directory where isoent can be
- * inserted. */
- xar->cur_dirent = dent;
- archive_string_empty(&(xar->cur_dirstr));
- archive_string_ensure(&(xar->cur_dirstr),
- archive_strlen(&(dent->parentdir)) +
- archive_strlen(&(dent->basename)) + 2);
- if (archive_strlen(&(dent->parentdir)) +
- archive_strlen(&(dent->basename)) == 0)
- xar->cur_dirstr.s[0] = 0;
- else {
- if (archive_strlen(&(dent->parentdir)) > 0) {
- archive_string_copy(&(xar->cur_dirstr),
- &(dent->parentdir));
- archive_strappend_char(&(xar->cur_dirstr), '/');
- }
- archive_string_concat(&(xar->cur_dirstr),
- &(dent->basename));
- }
- if (!file_add_child_tail(dent, file)) {
- np = (struct file *)__archive_rb_tree_find_node(
- &(dent->rbtree), file->basename.s);
- goto same_entry;
- }
- return (ARCHIVE_OK);
- }
- same_entry:
- /*
- * We have already has the entry the filename of which is
- * the same.
- */
- if (archive_entry_filetype(np->entry) !=
- archive_entry_filetype(file->entry)) {
- archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
- "Found duplicate entries `%s' and its file type is "
- "different",
- archive_entry_pathname(np->entry));
- file_free(file);
- *filepp = NULL;
- return (ARCHIVE_FAILED);
- }
- /* Swap files. */
- ent = np->entry;
- np->entry = file->entry;
- file->entry = ent;
- np->virtual = 0;
- file_free(file);
- *filepp = np;
- return (ARCHIVE_OK);
- }
- static void
- file_register(struct xar *xar, struct file *file)
- {
- file->id = xar->file_idx++;
- file->next = NULL;
- *xar->file_list.last = file;
- xar->file_list.last = &(file->next);
- }
- static void
- file_init_register(struct xar *xar)
- {
- xar->file_list.first = NULL;
- xar->file_list.last = &(xar->file_list.first);
- }
- static void
- file_free_register(struct xar *xar)
- {
- struct file *file, *file_next;
- file = xar->file_list.first;
- while (file != NULL) {
- file_next = file->next;
- file_free(file);
- file = file_next;
- }
- }
- /*
- * Register entry to get a hardlink target.
- */
- static int
- file_register_hardlink(struct archive_write *a, struct file *file)
- {
- struct xar *xar = (struct xar *)a->format_data;
- struct hardlink *hl;
- const char *pathname;
- archive_entry_set_nlink(file->entry, 1);
- pathname = archive_entry_hardlink(file->entry);
- if (pathname == NULL) {
- /* This `file` is a hardlink target. */
- hl = malloc(sizeof(*hl));
- if (hl == NULL) {
- archive_set_error(&a->archive, ENOMEM,
- "Can't allocate memory");
- return (ARCHIVE_FATAL);
- }
- hl->nlink = 1;
- /* A hardlink target must be the first position. */
- file->hlnext = NULL;
- hl->file_list.first = file;
- hl->file_list.last = &(file->hlnext);
- __archive_rb_tree_insert_node(&(xar->hardlink_rbtree),
- (struct archive_rb_node *)hl);
- } else {
- hl = (struct hardlink *)__archive_rb_tree_find_node(
- &(xar->hardlink_rbtree), pathname);
- if (hl != NULL) {
- /* Insert `file` entry into the tail. */
- file->hlnext = NULL;
- *hl->file_list.last = file;
- hl->file_list.last = &(file->hlnext);
- hl->nlink++;
- }
- archive_entry_unset_size(file->entry);
- }
- return (ARCHIVE_OK);
- }
- /*
- * Hardlinked files have to have the same location of extent.
- * We have to find out hardlink target entries for entries which
- * have a hardlink target name.
- */
- static void
- file_connect_hardlink_files(struct xar *xar)
- {
- struct archive_rb_node *n;
- struct hardlink *hl;
- struct file *target, *nf;
- ARCHIVE_RB_TREE_FOREACH(n, &(xar->hardlink_rbtree)) {
- hl = (struct hardlink *)n;
- /* The first entry must be a hardlink target. */
- target = hl->file_list.first;
- archive_entry_set_nlink(target->entry, hl->nlink);
- if (hl->nlink > 1)
- /* It means this file is a hardlink
- * target itself. */
- target->hardlink_target = target;
- for (nf = target->hlnext;
- nf != NULL; nf = nf->hlnext) {
- nf->hardlink_target = target;
- archive_entry_set_nlink(nf->entry, hl->nlink);
- }
- }
- }
- static int
- file_hd_cmp_node(const struct archive_rb_node *n1,
- const struct archive_rb_node *n2)
- {
- const struct hardlink *h1 = (const struct hardlink *)n1;
- const struct hardlink *h2 = (const struct hardlink *)n2;
- return (strcmp(archive_entry_pathname(h1->file_list.first->entry),
- archive_entry_pathname(h2->file_list.first->entry)));
- }
- static int
- file_hd_cmp_key(const struct archive_rb_node *n, const void *key)
- {
- const struct hardlink *h = (const struct hardlink *)n;
- return (strcmp(archive_entry_pathname(h->file_list.first->entry),
- (const char *)key));
- }
- static void
- file_init_hardlinks(struct xar *xar)
- {
- static const struct archive_rb_tree_ops rb_ops = {
- file_hd_cmp_node, file_hd_cmp_key,
- };
- __archive_rb_tree_init(&(xar->hardlink_rbtree), &rb_ops);
- }
- static void
- file_free_hardlinks(struct xar *xar)
- {
- struct archive_rb_node *n, *tmp;
- ARCHIVE_RB_TREE_FOREACH_SAFE(n, &(xar->hardlink_rbtree), tmp) {
- __archive_rb_tree_remove_node(&(xar->hardlink_rbtree), n);
- free(n);
- }
- }
- static void
- checksum_init(struct chksumwork *sumwrk, enum sumalg sum_alg)
- {
- sumwrk->alg = sum_alg;
- switch (sum_alg) {
- case CKSUM_NONE:
- break;
- case CKSUM_SHA1:
- archive_sha1_init(&(sumwrk->sha1ctx));
- break;
- case CKSUM_MD5:
- archive_md5_init(&(sumwrk->md5ctx));
- break;
- }
- }
- static void
- checksum_update(struct chksumwork *sumwrk, const void *buff, size_t size)
- {
- switch (sumwrk->alg) {
- case CKSUM_NONE:
- break;
- case CKSUM_SHA1:
- archive_sha1_update(&(sumwrk->sha1ctx), buff, size);
- break;
- case CKSUM_MD5:
- archive_md5_update(&(sumwrk->md5ctx), buff, size);
- break;
- }
- }
- static void
- checksum_final(struct chksumwork *sumwrk, struct chksumval *sumval)
- {
- switch (sumwrk->alg) {
- case CKSUM_NONE:
- sumval->len = 0;
- break;
- case CKSUM_SHA1:
- archive_sha1_final(&(sumwrk->sha1ctx), sumval->val);
- sumval->len = SHA1_SIZE;
- break;
- case CKSUM_MD5:
- archive_md5_final(&(sumwrk->md5ctx), sumval->val);
- sumval->len = MD5_SIZE;
- break;
- }
- sumval->alg = sumwrk->alg;
- }
- #if !defined(HAVE_BZLIB_H) || !defined(BZ_CONFIG_ERROR) || !defined(HAVE_LZMA_H)
- static int
- compression_unsupported_encoder(struct archive *a,
- struct la_zstream *lastrm, const char *name)
- {
- archive_set_error(a, ARCHIVE_ERRNO_MISC,
- "%s compression not supported on this platform", name);
- lastrm->valid = 0;
- lastrm->real_stream = NULL;
- return (ARCHIVE_FAILED);
- }
- #endif
- static int
- compression_init_encoder_gzip(struct archive *a,
- struct la_zstream *lastrm, int level, int withheader)
- {
- z_stream *strm;
- if (lastrm->valid)
- compression_end(a, lastrm);
- strm = calloc(1, sizeof(*strm));
- if (strm == NULL) {
- archive_set_error(a, ENOMEM,
- "Can't allocate memory for gzip stream");
- return (ARCHIVE_FATAL);
- }
- /* zlib.h is not const-correct, so we need this one bit
- * of ugly hackery to convert a const * pointer to
- * a non-const pointer. */
- strm->next_in = (Bytef *)(uintptr_t)(const void *)lastrm->next_in;
- strm->avail_in = (uInt)lastrm->avail_in;
- strm->total_in = (uLong)lastrm->total_in;
- strm->next_out = lastrm->next_out;
- strm->avail_out = (uInt)lastrm->avail_out;
- strm->total_out = (uLong)lastrm->total_out;
- if (deflateInit2(strm, level, Z_DEFLATED,
- (withheader)?15:-15,
- 8, Z_DEFAULT_STRATEGY) != Z_OK) {
- free(strm);
- lastrm->real_stream = NULL;
- archive_set_error(a, ARCHIVE_ERRNO_MISC,
- "Internal error initializing compression library");
- return (ARCHIVE_FATAL);
- }
- lastrm->real_stream = strm;
- lastrm->valid = 1;
- lastrm->code = compression_code_gzip;
- lastrm->end = compression_end_gzip;
- return (ARCHIVE_OK);
- }
- static int
- compression_code_gzip(struct archive *a,
- struct la_zstream *lastrm, enum la_zaction action)
- {
- z_stream *strm;
- int r;
- strm = (z_stream *)lastrm->real_stream;
- /* zlib.h is not const-correct, so we need this one bit
- * of ugly hackery to convert a const * pointer to
- * a non-const pointer. */
- strm->next_in = (Bytef *)(uintptr_t)(const void *)lastrm->next_in;
- strm->avail_in = (uInt)lastrm->avail_in;
- strm->total_in = (uLong)lastrm->total_in;
- strm->next_out = lastrm->next_out;
- strm->avail_out = (uInt)lastrm->avail_out;
- strm->total_out = (uLong)lastrm->total_out;
- r = deflate(strm,
- (action == ARCHIVE_Z_FINISH)? Z_FINISH: Z_NO_FLUSH);
- lastrm->next_in = strm->next_in;
- lastrm->avail_in = strm->avail_in;
- lastrm->total_in = strm->total_in;
- lastrm->next_out = strm->next_out;
- lastrm->avail_out = strm->avail_out;
- lastrm->total_out = strm->total_out;
- switch (r) {
- case Z_OK:
- return (ARCHIVE_OK);
- case Z_STREAM_END:
- return (ARCHIVE_EOF);
- default:
- archive_set_error(a, ARCHIVE_ERRNO_MISC,
- "GZip compression failed:"
- " deflate() call returned status %d", r);
- return (ARCHIVE_FATAL);
- }
- }
- static int
- compression_end_gzip(struct archive *a, struct la_zstream *lastrm)
- {
- z_stream *strm;
- int r;
- strm = (z_stream *)lastrm->real_stream;
- r = deflateEnd(strm);
- free(strm);
- lastrm->real_stream = NULL;
- lastrm->valid = 0;
- if (r != Z_OK) {
- archive_set_error(a, ARCHIVE_ERRNO_MISC,
- "Failed to clean up compressor");
- return (ARCHIVE_FATAL);
- }
- return (ARCHIVE_OK);
- }
- #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
- static int
- compression_init_encoder_bzip2(struct archive *a,
- struct la_zstream *lastrm, int level)
- {
- bz_stream *strm;
- if (lastrm->valid)
- compression_end(a, lastrm);
- strm = calloc(1, sizeof(*strm));
- if (strm == NULL) {
- archive_set_error(a, ENOMEM,
- "Can't allocate memory for bzip2 stream");
- return (ARCHIVE_FATAL);
- }
- /* bzlib.h is not const-correct, so we need this one bit
- * of ugly hackery to convert a const * pointer to
- * a non-const pointer. */
- strm->next_in = (char *)(uintptr_t)(const void *)lastrm->next_in;
- strm->avail_in = (unsigned int)lastrm->avail_in;
- strm->total_in_lo32 = (uint32_t)(lastrm->total_in & 0xffffffff);
- strm->total_in_hi32 = (uint32_t)(lastrm->total_in >> 32);
- strm->next_out = (char *)lastrm->next_out;
- strm->avail_out = (unsigned int)lastrm->avail_out;
- strm->total_out_lo32 = (uint32_t)(lastrm->total_out & 0xffffffff);
- strm->total_out_hi32 = (uint32_t)(lastrm->total_out >> 32);
- if (BZ2_bzCompressInit(strm, level, 0, 30) != BZ_OK) {
- free(strm);
- lastrm->real_stream = NULL;
- archive_set_error(a, ARCHIVE_ERRNO_MISC,
- "Internal error initializing compression library");
- return (ARCHIVE_FATAL);
- }
- lastrm->real_stream = strm;
- lastrm->valid = 1;
- lastrm->code = compression_code_bzip2;
- lastrm->end = compression_end_bzip2;
- return (ARCHIVE_OK);
- }
- static int
- compression_code_bzip2(struct archive *a,
- struct la_zstream *lastrm, enum la_zaction action)
- {
- bz_stream *strm;
- int r;
- strm = (bz_stream *)lastrm->real_stream;
- /* bzlib.h is not const-correct, so we need this one bit
- * of ugly hackery to convert a const * pointer to
- * a non-const pointer. */
- strm->next_in = (char *)(uintptr_t)(const void *)lastrm->next_in;
- strm->avail_in = (unsigned int)lastrm->avail_in;
- strm->total_in_lo32 = (uint32_t)(lastrm->total_in & 0xffffffff);
- strm->total_in_hi32 = (uint32_t)(lastrm->total_in >> 32);
- strm->next_out = (char *)lastrm->next_out;
- strm->avail_out = (unsigned int)lastrm->avail_out;
- strm->total_out_lo32 = (uint32_t)(lastrm->total_out & 0xffffffff);
- strm->total_out_hi32 = (uint32_t)(lastrm->total_out >> 32);
- r = BZ2_bzCompress(strm,
- (action == ARCHIVE_Z_FINISH)? BZ_FINISH: BZ_RUN);
- lastrm->next_in = (const unsigned char *)strm->next_in;
- lastrm->avail_in = strm->avail_in;
- lastrm->total_in =
- (((uint64_t)(uint32_t)strm->total_in_hi32) << 32)
- + (uint64_t)(uint32_t)strm->total_in_lo32;
- lastrm->next_out = (unsigned char *)strm->next_out;
- lastrm->avail_out = strm->avail_out;
- lastrm->total_out =
- (((uint64_t)(uint32_t)strm->total_out_hi32) << 32)
- + (uint64_t)(uint32_t)strm->total_out_lo32;
- switch (r) {
- case BZ_RUN_OK: /* Non-finishing */
- case BZ_FINISH_OK: /* Finishing: There's more work to do */
- return (ARCHIVE_OK);
- case BZ_STREAM_END: /* Finishing: all done */
- /* Only occurs in finishing case */
- return (ARCHIVE_EOF);
- default:
- /* Any other return value indicates an error */
- archive_set_error(a, ARCHIVE_ERRNO_MISC,
- "Bzip2 compression failed:"
- " BZ2_bzCompress() call returned status %d", r);
- return (ARCHIVE_FATAL);
- }
- }
- static int
- compression_end_bzip2(struct archive *a, struct la_zstream *lastrm)
- {
- bz_stream *strm;
- int r;
- strm = (bz_stream *)lastrm->real_stream;
- r = BZ2_bzCompressEnd(strm);
- free(strm);
- lastrm->real_stream = NULL;
- lastrm->valid = 0;
- if (r != BZ_OK) {
- archive_set_error(a, ARCHIVE_ERRNO_MISC,
- "Failed to clean up compressor");
- return (ARCHIVE_FATAL);
- }
- return (ARCHIVE_OK);
- }
- #else
- static int
- compression_init_encoder_bzip2(struct archive *a,
- struct la_zstream *lastrm, int level)
- {
- (void) level; /* UNUSED */
- if (lastrm->valid)
- compression_end(a, lastrm);
- return (compression_unsupported_encoder(a, lastrm, "bzip2"));
- }
- #endif
- #if defined(HAVE_LZMA_H)
- static int
- compression_init_encoder_lzma(struct archive *a,
- struct la_zstream *lastrm, int level)
- {
- static const lzma_stream lzma_init_data = LZMA_STREAM_INIT;
- lzma_stream *strm;
- lzma_options_lzma lzma_opt;
- int r;
- if (lastrm->valid)
- compression_end(a, lastrm);
- if (lzma_lzma_preset(&lzma_opt, level)) {
- lastrm->real_stream = NULL;
- archive_set_error(a, ENOMEM,
- "Internal error initializing compression library");
- return (ARCHIVE_FATAL);
- }
- strm = calloc(1, sizeof(*strm));
- if (strm == NULL) {
- archive_set_error(a, ENOMEM,
- "Can't allocate memory for lzma stream");
- return (ARCHIVE_FATAL);
- }
- *strm = lzma_init_data;
- r = lzma_alone_encoder(strm, &lzma_opt);
- switch (r) {
- case LZMA_OK:
- lastrm->real_stream = strm;
- lastrm->valid = 1;
- lastrm->code = compression_code_lzma;
- lastrm->end = compression_end_lzma;
- r = ARCHIVE_OK;
- break;
- case LZMA_MEM_ERROR:
- free(strm);
- lastrm->real_stream = NULL;
- archive_set_error(a, ENOMEM,
- "Internal error initializing compression library: "
- "Cannot allocate memory");
- r = ARCHIVE_FATAL;
- break;
- default:
- free(strm);
- lastrm->real_stream = NULL;
- archive_set_error(a, ARCHIVE_ERRNO_MISC,
- "Internal error initializing compression library: "
- "It's a bug in liblzma");
- r = ARCHIVE_FATAL;
- break;
- }
- return (r);
- }
- static int
- compression_init_encoder_xz(struct archive *a,
- struct la_zstream *lastrm, int level, int threads)
- {
- static const lzma_stream lzma_init_data = LZMA_STREAM_INIT;
- lzma_stream *strm;
- lzma_filter *lzmafilters;
- lzma_options_lzma lzma_opt;
- int r;
- #ifdef HAVE_LZMA_STREAM_ENCODER_MT
- lzma_mt mt_options;
- #endif
- (void)threads; /* UNUSED (if multi-threaded LZMA library not avail) */
- if (lastrm->valid)
- compression_end(a, lastrm);
- strm = calloc(1, sizeof(*strm) + sizeof(*lzmafilters) * 2);
- if (strm == NULL) {
- archive_set_error(a, ENOMEM,
- "Can't allocate memory for xz stream");
- return (ARCHIVE_FATAL);
- }
- lzmafilters = (lzma_filter *)(strm+1);
- if (level > 9)
- level = 9;
- if (lzma_lzma_preset(&lzma_opt, level)) {
- free(strm);
- lastrm->real_stream = NULL;
- archive_set_error(a, ENOMEM,
- "Internal error initializing compression library");
- return (ARCHIVE_FATAL);
- }
- lzmafilters[0].id = LZMA_FILTER_LZMA2;
- lzmafilters[0].options = &lzma_opt;
- lzmafilters[1].id = LZMA_VLI_UNKNOWN;/* Terminate */
- *strm = lzma_init_data;
- #ifdef HAVE_LZMA_STREAM_ENCODER_MT
- if (threads > 1) {
- memset(&mt_options, 0, sizeof(mt_options));
- mt_options.threads = threads;
- mt_options.timeout = 300;
- mt_options.filters = lzmafilters;
- mt_options.check = LZMA_CHECK_CRC64;
- r = lzma_stream_encoder_mt(strm, &mt_options);
- } else
- #endif
- r = lzma_stream_encoder(strm, lzmafilters, LZMA_CHECK_CRC64);
- switch (r) {
- case LZMA_OK:
- lastrm->real_stream = strm;
- lastrm->valid = 1;
- lastrm->code = compression_code_lzma;
- lastrm->end = compression_end_lzma;
- r = ARCHIVE_OK;
- break;
- case LZMA_MEM_ERROR:
- free(strm);
- lastrm->real_stream = NULL;
- archive_set_error(a, ENOMEM,
- "Internal error initializing compression library: "
- "Cannot allocate memory");
- r = ARCHIVE_FATAL;
- break;
- default:
- free(strm);
- lastrm->real_stream = NULL;
- archive_set_error(a, ARCHIVE_ERRNO_MISC,
- "Internal error initializing compression library: "
- "It's a bug in liblzma");
- r = ARCHIVE_FATAL;
- break;
- }
- return (r);
- }
- static int
- compression_code_lzma(struct archive *a,
- struct la_zstream *lastrm, enum la_zaction action)
- {
- lzma_stream *strm;
- int r;
- strm = (lzma_stream *)lastrm->real_stream;
- strm->next_in = lastrm->next_in;
- strm->avail_in = lastrm->avail_in;
- strm->total_in = lastrm->total_in;
- strm->next_out = lastrm->next_out;
- strm->avail_out = lastrm->avail_out;
- strm->total_out = lastrm->total_out;
- r = lzma_code(strm,
- (action == ARCHIVE_Z_FINISH)? LZMA_FINISH: LZMA_RUN);
- lastrm->next_in = strm->next_in;
- lastrm->avail_in = strm->avail_in;
- lastrm->total_in = strm->total_in;
- lastrm->next_out = strm->next_out;
- lastrm->avail_out = strm->avail_out;
- lastrm->total_out = strm->total_out;
- switch (r) {
- case LZMA_OK:
- /* Non-finishing case */
- return (ARCHIVE_OK);
- case LZMA_STREAM_END:
- /* This return can only occur in finishing case. */
- return (ARCHIVE_EOF);
- case LZMA_MEMLIMIT_ERROR:
- archive_set_error(a, ENOMEM,
- "lzma compression error:"
- " %ju MiB would have been needed",
- (uintmax_t)((lzma_memusage(strm) + 1024 * 1024 -1)
- / (1024 * 1024)));
- return (ARCHIVE_FATAL);
- default:
- /* Any other return value indicates an error */
- archive_set_error(a, ARCHIVE_ERRNO_MISC,
- "lzma compression failed:"
- " lzma_code() call returned status %d", r);
- return (ARCHIVE_FATAL);
- }
- }
- static int
- compression_end_lzma(struct archive *a, struct la_zstream *lastrm)
- {
- lzma_stream *strm;
- (void)a; /* UNUSED */
- strm = (lzma_stream *)lastrm->real_stream;
- lzma_end(strm);
- free(strm);
- lastrm->valid = 0;
- lastrm->real_stream = NULL;
- return (ARCHIVE_OK);
- }
- #else
- static int
- compression_init_encoder_lzma(struct archive *a,
- struct la_zstream *lastrm, int level)
- {
- (void) level; /* UNUSED */
- if (lastrm->valid)
- compression_end(a, lastrm);
- return (compression_unsupported_encoder(a, lastrm, "lzma"));
- }
- static int
- compression_init_encoder_xz(struct archive *a,
- struct la_zstream *lastrm, int level, int threads)
- {
- (void) level; /* UNUSED */
- (void) threads; /* UNUSED */
- if (lastrm->valid)
- compression_end(a, lastrm);
- return (compression_unsupported_encoder(a, lastrm, "xz"));
- }
- #endif
- static int
- xar_compression_init_encoder(struct archive_write *a)
- {
- struct xar *xar;
- int r;
- xar = (struct xar *)a->format_data;
- switch (xar->opt_compression) {
- case GZIP:
- r = compression_init_encoder_gzip(
- &(a->archive), &(xar->stream),
- xar->opt_compression_level, 1);
- break;
- case BZIP2:
- r = compression_init_encoder_bzip2(
- &(a->archive), &(xar->stream),
- xar->opt_compression_level);
- break;
- case LZMA:
- r = compression_init_encoder_lzma(
- &(a->archive), &(xar->stream),
- xar->opt_compression_level);
- break;
- case XZ:
- r = compression_init_encoder_xz(
- &(a->archive), &(xar->stream),
- xar->opt_compression_level, xar->opt_threads);
- break;
- default:
- r = ARCHIVE_OK;
- break;
- }
- if (r == ARCHIVE_OK) {
- xar->stream.total_in = 0;
- xar->stream.next_out = xar->wbuff;
- xar->stream.avail_out = sizeof(xar->wbuff);
- xar->stream.total_out = 0;
- }
- return (r);
- }
- static int
- compression_code(struct archive *a, struct la_zstream *lastrm,
- enum la_zaction action)
- {
- if (lastrm->valid)
- return (lastrm->code(a, lastrm, action));
- return (ARCHIVE_OK);
- }
- static int
- compression_end(struct archive *a, struct la_zstream *lastrm)
- {
- if (lastrm->valid)
- return (lastrm->end(a, lastrm));
- return (ARCHIVE_OK);
- }
- static int
- save_xattrs(struct archive_write *a, struct file *file)
- {
- struct xar *xar;
- const char *name;
- const void *value;
- struct heap_data *heap;
- size_t size;
- int count, r;
- xar = (struct xar *)a->format_data;
- count = archive_entry_xattr_reset(file->entry);
- if (count == 0)
- return (ARCHIVE_OK);
- while (count--) {
- archive_entry_xattr_next(file->entry,
- &name, &value, &size);
- checksum_init(&(xar->a_sumwrk), xar->opt_sumalg);
- checksum_init(&(xar->e_sumwrk), xar->opt_sumalg);
- heap = calloc(1, sizeof(*heap));
- if (heap == NULL) {
- archive_set_error(&a->archive, ENOMEM,
- "Can't allocate memory for xattr");
- return (ARCHIVE_FATAL);
- }
- heap->id = file->ea_idx++;
- heap->temp_offset = xar->temp_offset;
- heap->size = size;/* save a extracted size */
- heap->compression = xar->opt_compression;
- /* Get a extracted sumcheck value. */
- checksum_update(&(xar->e_sumwrk), value, size);
- checksum_final(&(xar->e_sumwrk), &(heap->e_sum));
- /*
- * Not compression to xattr is simple way.
- */
- if (heap->compression == NONE) {
- checksum_update(&(xar->a_sumwrk), value, size);
- checksum_final(&(xar->a_sumwrk), &(heap->a_sum));
- if (write_to_temp(a, value, size)
- != ARCHIVE_OK) {
- free(heap);
- return (ARCHIVE_FATAL);
- }
- heap->length = size;
- /* Add heap to the tail of file->xattr. */
- heap->next = NULL;
- *file->xattr.last = heap;
- file->xattr.last = &(heap->next);
- /* Next xattr */
- continue;
- }
- /*
- * Init compression library.
- */
- r = xar_compression_init_encoder(a);
- if (r != ARCHIVE_OK) {
- free(heap);
- return (ARCHIVE_FATAL);
- }
- xar->stream.next_in = (const unsigned char *)value;
- xar->stream.avail_in = size;
- for (;;) {
- r = compression_code(&(a->archive),
- &(xar->stream), ARCHIVE_Z_FINISH);
- if (r != ARCHIVE_OK && r != ARCHIVE_EOF) {
- free(heap);
- return (ARCHIVE_FATAL);
- }
- size = sizeof(xar->wbuff) - xar->stream.avail_out;
- checksum_update(&(xar->a_sumwrk),
- xar->wbuff, size);
- if (write_to_temp(a, xar->wbuff, size)
- != ARCHIVE_OK) {
- free(heap);
- return (ARCHIVE_FATAL);
- }
- if (r == ARCHIVE_OK) {
- xar->stream.next_out = xar->wbuff;
- xar->stream.avail_out = sizeof(xar->wbuff);
- } else {
- checksum_final(&(xar->a_sumwrk),
- &(heap->a_sum));
- heap->length = xar->stream.total_out;
- /* Add heap to the tail of file->xattr. */
- heap->next = NULL;
- *file->xattr.last = heap;
- file->xattr.last = &(heap->next);
- break;
- }
- }
- /* Clean up compression library. */
- r = compression_end(&(a->archive), &(xar->stream));
- if (r != ARCHIVE_OK)
- return (ARCHIVE_FATAL);
- }
- return (ARCHIVE_OK);
- }
- static int
- getalgsize(enum sumalg sumalg)
- {
- switch (sumalg) {
- default:
- case CKSUM_NONE:
- return (0);
- case CKSUM_SHA1:
- return (SHA1_SIZE);
- case CKSUM_MD5:
- return (MD5_SIZE);
- }
- }
- static const char *
- getalgname(enum sumalg sumalg)
- {
- switch (sumalg) {
- default:
- case CKSUM_NONE:
- return (NULL);
- case CKSUM_SHA1:
- return (SHA1_NAME);
- case CKSUM_MD5:
- return (MD5_NAME);
- }
- }
- #if HAVE_LIBXML_XMLWRITER_H
- #define BAD_CAST_CONST (const xmlChar *)
- struct xml_writer {
- xmlTextWriterPtr writer;
- xmlBufferPtr bp;
- unsigned int indent;
- };
- static int
- xml_writer_create(struct xml_writer **pctx)
- {
- struct xml_writer *ctx = calloc(1, sizeof(struct xml_writer));
- if (ctx == NULL) {
- return (-1);
- }
- ctx->bp = xmlBufferCreate();
- if (ctx->bp == NULL) {
- free(ctx);
- return (-1);
- }
- ctx->writer = xmlNewTextWriterMemory(ctx->bp, 0);
- if (ctx->writer == NULL) {
- xmlBufferFree(ctx->bp);
- free(ctx);
- return (-1);
- }
- *pctx = ctx;
- return (0);
- }
- static int
- xml_writer_destroy(struct xml_writer *ctx)
- {
- xmlFreeTextWriter(ctx->writer);
- xmlBufferFree(ctx->bp);
- free(ctx);
- return (0);
- }
- static int
- xml_writer_start_document(struct xml_writer *ctx)
- {
- int r;
- r = xmlTextWriterStartDocument(ctx->writer, "1.0", "UTF-8", NULL);
- if (r < 0) {
- return (r);
- }
- r = xmlTextWriterSetIndent(ctx->writer, (int)ctx->indent);
- return (r);
- }
- static int
- xml_writer_end_document(struct xml_writer *ctx)
- {
- return (xmlTextWriterEndDocument(ctx->writer));
- }
- static int
- xml_writer_set_indent(struct xml_writer *ctx, unsigned int indent)
- {
- /* libxml2 only lets you set the indent after starting the document */
- ctx->indent = indent;
- return (0);
- }
- static int
- xml_writer_start_element(struct xml_writer *ctx, const char *localName)
- {
- return (xmlTextWriterStartElement(ctx->writer,
- BAD_CAST_CONST(localName)));
- }
- static int
- xml_writer_write_attribute(struct xml_writer *ctx,
- const char *key, const char *value)
- {
- return (xmlTextWriterWriteAttribute(ctx->writer,
- BAD_CAST_CONST(key), BAD_CAST_CONST(value)));
- }
- static int
- xml_writer_write_attributef(struct xml_writer *ctx,
- const char *key, const char *format, ...)
- {
- va_list ap;
- int ret;
- va_start(ap, format);
- ret = xmlTextWriterWriteVFormatAttribute(ctx->writer,
- BAD_CAST_CONST(key), format, ap);
- va_end(ap);
- return (ret);
- }
- static int
- xml_writer_write_string(struct xml_writer *ctx, const char *string)
- {
- return (xmlTextWriterWriteString(ctx->writer, BAD_CAST_CONST(string)));
- }
- static int
- xml_writer_write_base64(struct xml_writer* ctx,
- const char *data, size_t start, size_t len)
- {
- return (xmlTextWriterWriteBase64(ctx->writer, data,
- (int)start, (int)len));
- }
- static int
- xml_writer_end_element(struct xml_writer *ctx)
- {
- return (xmlTextWriterEndElement(ctx->writer));
- }
- static int
- xml_writer_get_final_content_and_length(struct xml_writer *ctx,
- const char **out, size_t *size)
- {
- *out = (const char*)ctx->bp->content;
- *size = (size_t)ctx->bp->use;
- return (0);
- }
- #elif HAVE_XMLLITE_H
- struct xml_writer {
- IXmlWriter *writer;
- IStream *stream;
- HGLOBAL global;
- };
- static int
- xml_writer_create(struct xml_writer **pctx)
- {
- struct xml_writer *ctx;
- HRESULT hr;
- ctx = calloc(1, sizeof(struct xml_writer));
- if (ctx == NULL) {
- return (E_OUTOFMEMORY);
- }
- hr = CreateStreamOnHGlobal(NULL, TRUE, &ctx->stream);
- if (FAILED(hr)) {
- free(ctx);
- return (hr);
- }
- hr = CreateXmlWriter(&IID_IXmlWriter, (void **)&ctx->writer, NULL);
- if (FAILED(hr)) {
- ctx->stream->lpVtbl->Release(ctx->stream);
- free(ctx);
- return (hr);
- }
- hr = ctx->writer->lpVtbl->SetOutput(ctx->writer,
- (IUnknown *)ctx->stream);
- if (FAILED(hr)) {
- ctx->writer->lpVtbl->Release(ctx->writer);
- ctx->stream->lpVtbl->Release(ctx->stream);
- free(ctx);
- return (hr);
- }
- *pctx = ctx;
- return (S_OK);
- }
- static int
- xml_writer_destroy(struct xml_writer *ctx)
- {
- if (ctx->global)
- GlobalUnlock(ctx->global);
- ctx->writer->lpVtbl->Release(ctx->writer); /* Destroys only writer */
- ctx->stream->lpVtbl->Release(ctx->stream); /* Destroys stream, global */
- free(ctx);
- return (S_OK);
- }
- static int
- xml_writer_start_document(struct xml_writer *ctx)
- {
- return ctx->writer->lpVtbl->WriteStartDocument(ctx->writer,
- XmlStandalone_Omit);
- }
- static int
- xml_writer_end_document(struct xml_writer *ctx)
- {
- return ctx->writer->lpVtbl->WriteEndDocument(ctx->writer);
- }
- static int
- xml_writer_set_indent(struct xml_writer *ctx, unsigned int indent)
- {
- /* Windows' xmllite does not support indent sizes; will always be 2 */
- (void)indent;
- return ctx->writer->lpVtbl->SetProperty(ctx->writer,
- XmlWriterProperty_Indent, (LONG_PTR)TRUE);
- }
- static int
- xml_writer_start_element(struct xml_writer *ctx, const char *localName)
- {
- struct archive_wstring as;
- HRESULT hr;
- archive_string_init(&as);
- if (archive_wstring_append_from_mbs(&as, localName,
- strlen(localName))) {
- hr = E_OUTOFMEMORY;
- goto exit_hr;
- }
- hr = ctx->writer->lpVtbl->WriteStartElement(ctx->writer, NULL,
- as.s, NULL);
- exit_hr:
- archive_wstring_free(&as);
- return hr;
- }
- static int
- xml_writer_write_attribute(struct xml_writer *ctx,
- const char *key, const char *value)
- {
- struct archive_wstring ask, asv;
- HRESULT hr;
- archive_string_init(&ask);
- archive_string_init(&asv);
- if (archive_wstring_append_from_mbs(&ask, key, strlen(key))) {
- hr = E_OUTOFMEMORY;
- goto exit_hr;
- }
- if (archive_wstring_append_from_mbs(&asv, value, strlen(value))) {
- hr = E_OUTOFMEMORY;
- goto exit_hr;
- }
- hr = ctx->writer->lpVtbl->WriteAttributeString(ctx->writer, NULL,
- ask.s, NULL, asv.s);
- exit_hr:
- archive_wstring_free(&asv);
- archive_wstring_free(&ask);
- return hr;
- }
- static int
- xml_writer_write_attributef(struct xml_writer *ctx,
- const char *key, const char *format, ...)
- {
- struct archive_wstring ask, asv;
- struct archive_string asf;
- HRESULT hr;
- va_list ap;
- va_start(ap, format);
- archive_string_init(&ask);
- archive_string_init(&asv);
- archive_string_init(&asf);
- if (archive_wstring_append_from_mbs(&ask, key, strlen(key))) {
- hr = E_OUTOFMEMORY;
- goto exit_hr;
- }
- archive_string_vsprintf(&asf, format, ap);
- if (archive_wstring_append_from_mbs(&asv, asf.s, asf.length)) {
- hr = E_OUTOFMEMORY;
- goto exit_hr;
- }
- hr = ctx->writer->lpVtbl->WriteAttributeString(ctx->writer, NULL,
- ask.s, NULL, asv.s);
- exit_hr:
- archive_string_free(&asf);
- archive_wstring_free(&asv);
- archive_wstring_free(&ask);
- va_end(ap);
- return hr;
- }
- static int
- xml_writer_write_string(struct xml_writer *ctx, const char *string)
- {
- struct archive_wstring as;
- HRESULT hr;
- archive_string_init(&as);
- if (archive_wstring_append_from_mbs(&as, string, strlen(string))) {
- hr = E_OUTOFMEMORY;
- goto exit_hr;
- }
- hr = ctx->writer->lpVtbl->WriteString(ctx->writer, as.s);
- exit_hr:
- archive_wstring_free(&as);
- return hr;
- }
- static const wchar_t base64[] = {
- L'A', L'B', L'C', L'D', L'E', L'F', L'G', L'H',
- L'I', L'J', L'K', L'L', L'M', L'N', L'O', L'P',
- L'Q', L'R', L'S', L'T', L'U', L'V', L'W', L'X',
- L'Y', L'Z', L'a', L'b', L'c', L'd', L'e', L'f',
- L'g', L'h', L'i', L'j', L'k', L'l', L'm', L'n',
- L'o', L'p', L'q', L'r', L's', L't', L'u', L'v',
- L'w', L'x', L'y', L'z', L'0', L'1', L'2', L'3',
- L'4', L'5', L'6', L'7', L'8', L'9', L'+', L'/'
- };
- static void
- la_b64_wencode(struct archive_wstring *as, const unsigned char *p, size_t len)
- {
- int c;
- for (; len >= 3; p += 3, len -= 3) {
- c = p[0] >> 2;
- archive_wstrappend_wchar(as, base64[c]);
- c = ((p[0] & 0x03) << 4) | ((p[1] & 0xf0) >> 4);
- archive_wstrappend_wchar(as, base64[c]);
- c = ((p[1] & 0x0f) << 2) | ((p[2] & 0xc0) >> 6);
- archive_wstrappend_wchar(as, base64[c]);
- c = p[2] & 0x3f;
- archive_wstrappend_wchar(as, base64[c]);
- }
- if (len > 0) {
- c = p[0] >> 2;
- archive_wstrappend_wchar(as, base64[c]);
- c = (p[0] & 0x03) << 4;
- if (len == 1) {
- archive_wstrappend_wchar(as, base64[c]);
- archive_wstrappend_wchar(as, '=');
- archive_wstrappend_wchar(as, '=');
- } else {
- c |= (p[1] & 0xf0) >> 4;
- archive_wstrappend_wchar(as, base64[c]);
- c = (p[1] & 0x0f) << 2;
- archive_wstrappend_wchar(as, base64[c]);
- archive_wstrappend_wchar(as, '=');
- }
- }
- }
- static int
- xml_writer_write_base64(struct xml_writer* ctx,
- const char *data, size_t start, size_t len)
- {
- struct archive_wstring as;
- HRESULT hr;
- archive_string_init(&as);
- la_b64_wencode(&as, (const unsigned char *)data + start, len - start);
- hr = ctx->writer->lpVtbl->WriteString(ctx->writer, as.s);
- archive_wstring_free(&as);
- return hr;
- }
- static int
- xml_writer_end_element(struct xml_writer *ctx)
- {
- return ctx->writer->lpVtbl->WriteEndElement(ctx->writer);
- }
- static int
- xml_writer_get_final_content_and_length(struct xml_writer *ctx,
- const char **out, size_t *size)
- {
- HGLOBAL gbl;
- HRESULT hr;
- hr = ctx->writer->lpVtbl->Flush(ctx->writer);
- if (FAILED(hr)) {
- return (hr);
- }
- hr = GetHGlobalFromStream(ctx->stream, &gbl);
- if (FAILED(hr)) {
- return (hr);
- }
- *out = (const char *)GlobalLock(gbl);
- if (*out == NULL) {
- hr = HRESULT_FROM_WIN32(GetLastError());
- return (hr);
- }
- /* GlobalUnlock is called in
- * xml_writer_destroy.
- */
- *size = (size_t)GlobalSize(gbl);
- ctx->global = gbl;
- return (hr);
- }
- #endif /* HAVE_LIBXML_XMLWRITER_H */
- #endif /* Support xar format */
|