archive_write_set_format_7zip.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328
  1. /*-
  2. * Copyright (c) 2011-2012 Michihiro NAKAJIMA
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  16. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  17. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  18. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  19. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  21. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "archive_platform.h"
  26. __FBSDID("$FreeBSD$");
  27. #ifdef HAVE_ERRNO_H
  28. #include <errno.h>
  29. #endif
  30. #include <stdlib.h>
  31. #ifdef HAVE_BZLIB_H
  32. #include <cm_bzlib.h>
  33. #endif
  34. #if HAVE_LZMA_H
  35. #include <cm_lzma.h>
  36. #endif
  37. #ifdef HAVE_ZLIB_H
  38. #include <cm_zlib.h>
  39. #endif
  40. #include "archive.h"
  41. #ifndef HAVE_ZLIB_H
  42. #include "archive_crc32.h"
  43. #endif
  44. #include "archive_endian.h"
  45. #include "archive_entry.h"
  46. #include "archive_entry_locale.h"
  47. #include "archive_ppmd7_private.h"
  48. #include "archive_private.h"
  49. #include "archive_rb.h"
  50. #include "archive_string.h"
  51. #include "archive_write_private.h"
  52. /*
  53. * Codec ID
  54. */
  55. #define _7Z_COPY 0
  56. #define _7Z_LZMA1 0x030101
  57. #define _7Z_LZMA2 0x21
  58. #define _7Z_DEFLATE 0x040108
  59. #define _7Z_BZIP2 0x040202
  60. #define _7Z_PPMD 0x030401
  61. /*
  62. * 7-Zip header property IDs.
  63. */
  64. #define kEnd 0x00
  65. #define kHeader 0x01
  66. #define kArchiveProperties 0x02
  67. #define kAdditionalStreamsInfo 0x03
  68. #define kMainStreamsInfo 0x04
  69. #define kFilesInfo 0x05
  70. #define kPackInfo 0x06
  71. #define kUnPackInfo 0x07
  72. #define kSubStreamsInfo 0x08
  73. #define kSize 0x09
  74. #define kCRC 0x0A
  75. #define kFolder 0x0B
  76. #define kCodersUnPackSize 0x0C
  77. #define kNumUnPackStream 0x0D
  78. #define kEmptyStream 0x0E
  79. #define kEmptyFile 0x0F
  80. #define kAnti 0x10
  81. #define kName 0x11
  82. #define kCTime 0x12
  83. #define kATime 0x13
  84. #define kMTime 0x14
  85. #define kAttributes 0x15
  86. #define kEncodedHeader 0x17
  87. enum la_zaction {
  88. ARCHIVE_Z_FINISH,
  89. ARCHIVE_Z_RUN
  90. };
  91. /*
  92. * A stream object of universal compressor.
  93. */
  94. struct la_zstream {
  95. const uint8_t *next_in;
  96. size_t avail_in;
  97. uint64_t total_in;
  98. uint8_t *next_out;
  99. size_t avail_out;
  100. uint64_t total_out;
  101. uint32_t prop_size;
  102. uint8_t *props;
  103. int valid;
  104. void *real_stream;
  105. int (*code) (struct archive *a,
  106. struct la_zstream *lastrm,
  107. enum la_zaction action);
  108. int (*end)(struct archive *a,
  109. struct la_zstream *lastrm);
  110. };
  111. #define PPMD7_DEFAULT_ORDER 6
  112. #define PPMD7_DEFAULT_MEM_SIZE (1 << 24)
  113. struct ppmd_stream {
  114. int stat;
  115. CPpmd7 ppmd7_context;
  116. CPpmd7z_RangeEnc range_enc;
  117. IByteOut byteout;
  118. uint8_t *buff;
  119. uint8_t *buff_ptr;
  120. uint8_t *buff_end;
  121. size_t buff_bytes;
  122. };
  123. struct coder {
  124. unsigned codec;
  125. size_t prop_size;
  126. uint8_t *props;
  127. };
  128. struct file {
  129. struct archive_rb_node rbnode;
  130. struct file *next;
  131. unsigned name_len;
  132. uint8_t *utf16name;/* UTF16-LE name. */
  133. uint64_t size;
  134. unsigned flg;
  135. #define MTIME_IS_SET (1<<0)
  136. #define ATIME_IS_SET (1<<1)
  137. #define CTIME_IS_SET (1<<2)
  138. #define CRC32_IS_SET (1<<3)
  139. #define HAS_STREAM (1<<4)
  140. struct {
  141. time_t time;
  142. long time_ns;
  143. } times[3];
  144. #define MTIME 0
  145. #define ATIME 1
  146. #define CTIME 2
  147. mode_t mode;
  148. uint32_t crc32;
  149. int dir:1;
  150. };
  151. struct _7zip {
  152. int temp_fd;
  153. uint64_t temp_offset;
  154. struct file *cur_file;
  155. size_t total_number_entry;
  156. size_t total_number_nonempty_entry;
  157. size_t total_number_empty_entry;
  158. size_t total_number_dir_entry;
  159. size_t total_bytes_entry_name;
  160. size_t total_number_time_defined[3];
  161. uint64_t total_bytes_compressed;
  162. uint64_t total_bytes_uncompressed;
  163. uint64_t entry_bytes_remaining;
  164. uint32_t entry_crc32;
  165. uint32_t precode_crc32;
  166. uint32_t encoded_crc32;
  167. int crc32flg;
  168. #define PRECODE_CRC32 1
  169. #define ENCODED_CRC32 2
  170. unsigned opt_compression;
  171. int opt_compression_level;
  172. struct la_zstream stream;
  173. struct coder coder;
  174. struct archive_string_conv *sconv;
  175. /*
  176. * Compressed data buffer.
  177. */
  178. unsigned char wbuff[512 * 20 * 6];
  179. size_t wbuff_remaining;
  180. /*
  181. * The list of the file entries which has its contents is used to
  182. * manage struct file objects.
  183. * We use 'next' (a member of struct file) to chain.
  184. */
  185. struct {
  186. struct file *first;
  187. struct file **last;
  188. } file_list, empty_list;
  189. struct archive_rb_tree rbtree;/* for empty files */
  190. };
  191. static int _7z_options(struct archive_write *,
  192. const char *, const char *);
  193. static int _7z_write_header(struct archive_write *,
  194. struct archive_entry *);
  195. static ssize_t _7z_write_data(struct archive_write *,
  196. const void *, size_t);
  197. static int _7z_finish_entry(struct archive_write *);
  198. static int _7z_close(struct archive_write *);
  199. static int _7z_free(struct archive_write *);
  200. static int file_cmp_node(const struct archive_rb_node *,
  201. const struct archive_rb_node *);
  202. static int file_cmp_key(const struct archive_rb_node *, const void *);
  203. static int file_new(struct archive_write *a, struct archive_entry *,
  204. struct file **);
  205. static void file_free(struct file *);
  206. static void file_register(struct _7zip *, struct file *);
  207. static void file_register_empty(struct _7zip *, struct file *);
  208. static void file_init_register(struct _7zip *);
  209. static void file_init_register_empty(struct _7zip *);
  210. static void file_free_register(struct _7zip *);
  211. static ssize_t compress_out(struct archive_write *, const void *, size_t ,
  212. enum la_zaction);
  213. static int compression_init_encoder_copy(struct archive *,
  214. struct la_zstream *);
  215. static int compression_code_copy(struct archive *,
  216. struct la_zstream *, enum la_zaction);
  217. static int compression_end_copy(struct archive *, struct la_zstream *);
  218. static int compression_init_encoder_deflate(struct archive *,
  219. struct la_zstream *, int, int);
  220. #ifdef HAVE_ZLIB_H
  221. static int compression_code_deflate(struct archive *,
  222. struct la_zstream *, enum la_zaction);
  223. static int compression_end_deflate(struct archive *, struct la_zstream *);
  224. #endif
  225. static int compression_init_encoder_bzip2(struct archive *,
  226. struct la_zstream *, int);
  227. #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
  228. static int compression_code_bzip2(struct archive *,
  229. struct la_zstream *, enum la_zaction);
  230. static int compression_end_bzip2(struct archive *, struct la_zstream *);
  231. #endif
  232. static int compression_init_encoder_lzma1(struct archive *,
  233. struct la_zstream *, int);
  234. static int compression_init_encoder_lzma2(struct archive *,
  235. struct la_zstream *, int);
  236. #if defined(HAVE_LZMA_H)
  237. static int compression_code_lzma(struct archive *,
  238. struct la_zstream *, enum la_zaction);
  239. static int compression_end_lzma(struct archive *, struct la_zstream *);
  240. #endif
  241. static int compression_init_encoder_ppmd(struct archive *,
  242. struct la_zstream *, unsigned, uint32_t);
  243. static int compression_code_ppmd(struct archive *,
  244. struct la_zstream *, enum la_zaction);
  245. static int compression_end_ppmd(struct archive *, struct la_zstream *);
  246. static int _7z_compression_init_encoder(struct archive_write *, unsigned,
  247. int);
  248. static int compression_code(struct archive *,
  249. struct la_zstream *, enum la_zaction);
  250. static int compression_end(struct archive *,
  251. struct la_zstream *);
  252. static int enc_uint64(struct archive_write *, uint64_t);
  253. static int make_header(struct archive_write *, uint64_t, uint64_t,
  254. uint64_t, int, struct coder *);
  255. static int make_streamsInfo(struct archive_write *, uint64_t, uint64_t,
  256. uint64_t, int, struct coder *, int, uint32_t);
  257. int
  258. archive_write_set_format_7zip(struct archive *_a)
  259. {
  260. static const struct archive_rb_tree_ops rb_ops = {
  261. file_cmp_node, file_cmp_key
  262. };
  263. struct archive_write *a = (struct archive_write *)_a;
  264. struct _7zip *zip;
  265. archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
  266. ARCHIVE_STATE_NEW, "archive_write_set_format_7zip");
  267. /* If another format was already registered, unregister it. */
  268. if (a->format_free != NULL)
  269. (a->format_free)(a);
  270. zip = calloc(1, sizeof(*zip));
  271. if (zip == NULL) {
  272. archive_set_error(&a->archive, ENOMEM,
  273. "Can't allocate 7-Zip data");
  274. return (ARCHIVE_FATAL);
  275. }
  276. zip->temp_fd = -1;
  277. __archive_rb_tree_init(&(zip->rbtree), &rb_ops);
  278. file_init_register(zip);
  279. file_init_register_empty(zip);
  280. /* Set default compression type and its level. */
  281. #if HAVE_LZMA_H
  282. zip->opt_compression = _7Z_LZMA1;
  283. #elif defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
  284. zip->opt_compression = _7Z_BZIP2;
  285. #elif defined(HAVE_ZLIB_H)
  286. zip->opt_compression = _7Z_DEFLATE;
  287. #else
  288. zip->opt_compression = _7Z_COPY;
  289. #endif
  290. zip->opt_compression_level = 6;
  291. a->format_data = zip;
  292. a->format_name = "7zip";
  293. a->format_options = _7z_options;
  294. a->format_write_header = _7z_write_header;
  295. a->format_write_data = _7z_write_data;
  296. a->format_finish_entry = _7z_finish_entry;
  297. a->format_close = _7z_close;
  298. a->format_free = _7z_free;
  299. a->archive.archive_format = ARCHIVE_FORMAT_7ZIP;
  300. a->archive.archive_format_name = "7zip";
  301. return (ARCHIVE_OK);
  302. }
  303. static int
  304. _7z_options(struct archive_write *a, const char *key, const char *value)
  305. {
  306. struct _7zip *zip;
  307. zip = (struct _7zip *)a->format_data;
  308. if (strcmp(key, "compression") == 0) {
  309. const char *name = NULL;
  310. if (value == NULL || strcmp(value, "copy") == 0 ||
  311. strcmp(value, "COPY") == 0 ||
  312. strcmp(value, "store") == 0 ||
  313. strcmp(value, "STORE") == 0)
  314. zip->opt_compression = _7Z_COPY;
  315. else if (strcmp(value, "deflate") == 0 ||
  316. strcmp(value, "DEFLATE") == 0)
  317. #if HAVE_ZLIB_H
  318. zip->opt_compression = _7Z_DEFLATE;
  319. #else
  320. name = "deflate";
  321. #endif
  322. else if (strcmp(value, "bzip2") == 0 ||
  323. strcmp(value, "BZIP2") == 0)
  324. #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
  325. zip->opt_compression = _7Z_BZIP2;
  326. #else
  327. name = "bzip2";
  328. #endif
  329. else if (strcmp(value, "lzma1") == 0 ||
  330. strcmp(value, "LZMA1") == 0)
  331. #if HAVE_LZMA_H
  332. zip->opt_compression = _7Z_LZMA1;
  333. #else
  334. name = "lzma1";
  335. #endif
  336. else if (strcmp(value, "lzma2") == 0 ||
  337. strcmp(value, "LZMA2") == 0)
  338. #if HAVE_LZMA_H
  339. zip->opt_compression = _7Z_LZMA2;
  340. #else
  341. name = "lzma2";
  342. #endif
  343. else if (strcmp(value, "ppmd") == 0 ||
  344. strcmp(value, "PPMD") == 0 ||
  345. strcmp(value, "PPMd") == 0)
  346. zip->opt_compression = _7Z_PPMD;
  347. else {
  348. archive_set_error(&(a->archive),
  349. ARCHIVE_ERRNO_MISC,
  350. "Unknown compression name: `%s'",
  351. value);
  352. return (ARCHIVE_FAILED);
  353. }
  354. if (name != NULL) {
  355. archive_set_error(&(a->archive),
  356. ARCHIVE_ERRNO_MISC,
  357. "`%s' compression not supported "
  358. "on this platform",
  359. name);
  360. return (ARCHIVE_FAILED);
  361. }
  362. return (ARCHIVE_OK);
  363. }
  364. if (strcmp(key, "compression-level") == 0) {
  365. if (value == NULL ||
  366. !(value[0] >= '0' && value[0] <= '9') ||
  367. value[1] != '\0') {
  368. archive_set_error(&(a->archive),
  369. ARCHIVE_ERRNO_MISC,
  370. "Illegal value `%s'",
  371. value);
  372. return (ARCHIVE_FAILED);
  373. }
  374. zip->opt_compression_level = value[0] - '0';
  375. return (ARCHIVE_OK);
  376. }
  377. /* Note: The "warn" return is just to inform the options
  378. * supervisor that we didn't handle it. It will generate
  379. * a suitable error if no one used this option. */
  380. return (ARCHIVE_WARN);
  381. }
  382. static int
  383. _7z_write_header(struct archive_write *a, struct archive_entry *entry)
  384. {
  385. struct _7zip *zip;
  386. struct file *file;
  387. int r;
  388. zip = (struct _7zip *)a->format_data;
  389. zip->cur_file = NULL;
  390. zip->entry_bytes_remaining = 0;
  391. if (zip->sconv == NULL) {
  392. zip->sconv = archive_string_conversion_to_charset(
  393. &a->archive, "UTF-16LE", 1);
  394. if (zip->sconv == NULL)
  395. return (ARCHIVE_FATAL);
  396. }
  397. r = file_new(a, entry, &file);
  398. if (r < ARCHIVE_WARN) {
  399. file_free(file);
  400. return (r);
  401. }
  402. if (file->size == 0 && file->dir) {
  403. if (!__archive_rb_tree_insert_node(&(zip->rbtree),
  404. (struct archive_rb_node *)file)) {
  405. /* We have already had the same file. */
  406. file_free(file);
  407. return (ARCHIVE_OK);
  408. }
  409. }
  410. if (file->flg & MTIME_IS_SET)
  411. zip->total_number_time_defined[MTIME]++;
  412. if (file->flg & CTIME_IS_SET)
  413. zip->total_number_time_defined[CTIME]++;
  414. if (file->flg & ATIME_IS_SET)
  415. zip->total_number_time_defined[ATIME]++;
  416. zip->total_number_entry++;
  417. zip->total_bytes_entry_name += file->name_len + 2;
  418. if (file->size == 0) {
  419. /* Count up the number of empty files. */
  420. zip->total_number_empty_entry++;
  421. if (file->dir)
  422. zip->total_number_dir_entry++;
  423. else
  424. file_register_empty(zip, file);
  425. return (r);
  426. }
  427. /*
  428. * Init compression.
  429. */
  430. if ((zip->total_number_entry - zip->total_number_empty_entry) == 1) {
  431. r = _7z_compression_init_encoder(a, zip->opt_compression,
  432. zip->opt_compression_level);
  433. if (r < 0) {
  434. file_free(file);
  435. return (ARCHIVE_FATAL);
  436. }
  437. }
  438. /* Register a non-empty file. */
  439. file_register(zip, file);
  440. /*
  441. * Set the current file to cur_file to read its contents.
  442. */
  443. zip->cur_file = file;
  444. /* Save a offset of current file in temporary file. */
  445. zip->entry_bytes_remaining = file->size;
  446. zip->entry_crc32 = 0;
  447. /*
  448. * Store a symbolic link name as file contents.
  449. */
  450. if (archive_entry_filetype(entry) == AE_IFLNK) {
  451. ssize_t bytes;
  452. const void *p = (const void *)archive_entry_symlink(entry);
  453. bytes = compress_out(a, p, (size_t)file->size, ARCHIVE_Z_RUN);
  454. if (bytes < 0)
  455. return ((int)bytes);
  456. zip->entry_crc32 = crc32(zip->entry_crc32, p, (unsigned)bytes);
  457. zip->entry_bytes_remaining -= bytes;
  458. }
  459. return (r);
  460. }
  461. /*
  462. * Write data to a temporary file.
  463. */
  464. static int
  465. write_to_temp(struct archive_write *a, const void *buff, size_t s)
  466. {
  467. struct _7zip *zip;
  468. const unsigned char *p;
  469. ssize_t ws;
  470. zip = (struct _7zip *)a->format_data;
  471. /*
  472. * Open a temporary file.
  473. */
  474. if (zip->temp_fd == -1) {
  475. zip->temp_offset = 0;
  476. zip->temp_fd = __archive_mktemp(NULL);
  477. if (zip->temp_fd < 0) {
  478. archive_set_error(&a->archive, errno,
  479. "Couldn't create temporary file");
  480. return (ARCHIVE_FATAL);
  481. }
  482. }
  483. p = (const unsigned char *)buff;
  484. while (s) {
  485. ws = write(zip->temp_fd, p, s);
  486. if (ws < 0) {
  487. archive_set_error(&(a->archive), errno,
  488. "fwrite function failed");
  489. return (ARCHIVE_FATAL);
  490. }
  491. s -= ws;
  492. p += ws;
  493. zip->temp_offset += ws;
  494. }
  495. return (ARCHIVE_OK);
  496. }
  497. static ssize_t
  498. compress_out(struct archive_write *a, const void *buff, size_t s,
  499. enum la_zaction run)
  500. {
  501. struct _7zip *zip = (struct _7zip *)a->format_data;
  502. int r;
  503. if (run == ARCHIVE_Z_FINISH && zip->stream.total_in == 0 && s == 0)
  504. return (0);
  505. if ((zip->crc32flg & PRECODE_CRC32) && s)
  506. zip->precode_crc32 = crc32(zip->precode_crc32, buff,
  507. (unsigned)s);
  508. zip->stream.next_in = (const unsigned char *)buff;
  509. zip->stream.avail_in = s;
  510. for (;;) {
  511. /* Compress file data. */
  512. r = compression_code(&(a->archive), &(zip->stream), run);
  513. if (r != ARCHIVE_OK && r != ARCHIVE_EOF)
  514. return (ARCHIVE_FATAL);
  515. if (zip->stream.avail_out == 0) {
  516. if (write_to_temp(a, zip->wbuff, sizeof(zip->wbuff))
  517. != ARCHIVE_OK)
  518. return (ARCHIVE_FATAL);
  519. zip->stream.next_out = zip->wbuff;
  520. zip->stream.avail_out = sizeof(zip->wbuff);
  521. if (zip->crc32flg & ENCODED_CRC32)
  522. zip->encoded_crc32 = crc32(zip->encoded_crc32,
  523. zip->wbuff, sizeof(zip->wbuff));
  524. if (run == ARCHIVE_Z_FINISH && r != ARCHIVE_EOF)
  525. continue;
  526. }
  527. if (zip->stream.avail_in == 0)
  528. break;
  529. }
  530. if (run == ARCHIVE_Z_FINISH) {
  531. uint64_t bytes = sizeof(zip->wbuff) - zip->stream.avail_out;
  532. if (write_to_temp(a, zip->wbuff, (size_t)bytes) != ARCHIVE_OK)
  533. return (ARCHIVE_FATAL);
  534. if ((zip->crc32flg & ENCODED_CRC32) && bytes)
  535. zip->encoded_crc32 = crc32(zip->encoded_crc32,
  536. zip->wbuff, (unsigned)bytes);
  537. }
  538. return (s);
  539. }
  540. static ssize_t
  541. _7z_write_data(struct archive_write *a, const void *buff, size_t s)
  542. {
  543. struct _7zip *zip;
  544. ssize_t bytes;
  545. zip = (struct _7zip *)a->format_data;
  546. if (s > zip->entry_bytes_remaining)
  547. s = (size_t)zip->entry_bytes_remaining;
  548. if (s == 0 || zip->cur_file == NULL)
  549. return (0);
  550. bytes = compress_out(a, buff, s, ARCHIVE_Z_RUN);
  551. if (bytes < 0)
  552. return (bytes);
  553. zip->entry_crc32 = crc32(zip->entry_crc32, buff, (unsigned)bytes);
  554. zip->entry_bytes_remaining -= bytes;
  555. return (bytes);
  556. }
  557. static int
  558. _7z_finish_entry(struct archive_write *a)
  559. {
  560. struct _7zip *zip;
  561. size_t s;
  562. ssize_t r;
  563. zip = (struct _7zip *)a->format_data;
  564. if (zip->cur_file == NULL)
  565. return (ARCHIVE_OK);
  566. while (zip->entry_bytes_remaining > 0) {
  567. s = (size_t)zip->entry_bytes_remaining;
  568. if (s > a->null_length)
  569. s = a->null_length;
  570. r = _7z_write_data(a, a->nulls, s);
  571. if (r < 0)
  572. return ((int)r);
  573. }
  574. zip->total_bytes_compressed += zip->stream.total_in;
  575. zip->total_bytes_uncompressed += zip->stream.total_out;
  576. zip->cur_file->crc32 = zip->entry_crc32;
  577. zip->cur_file = NULL;
  578. return (ARCHIVE_OK);
  579. }
  580. static int
  581. flush_wbuff(struct archive_write *a)
  582. {
  583. struct _7zip *zip;
  584. int r;
  585. size_t s;
  586. zip = (struct _7zip *)a->format_data;
  587. s = sizeof(zip->wbuff) - zip->wbuff_remaining;
  588. r = __archive_write_output(a, zip->wbuff, s);
  589. if (r != ARCHIVE_OK)
  590. return (r);
  591. zip->wbuff_remaining = sizeof(zip->wbuff);
  592. return (r);
  593. }
  594. static int
  595. copy_out(struct archive_write *a, uint64_t offset, uint64_t length)
  596. {
  597. struct _7zip *zip;
  598. int r;
  599. zip = (struct _7zip *)a->format_data;
  600. if (zip->temp_offset > 0 &&
  601. lseek(zip->temp_fd, offset, SEEK_SET) < 0) {
  602. archive_set_error(&(a->archive), errno, "lseek failed");
  603. return (ARCHIVE_FATAL);
  604. }
  605. while (length) {
  606. size_t rsize;
  607. ssize_t rs;
  608. unsigned char *wb;
  609. if (length > zip->wbuff_remaining)
  610. rsize = zip->wbuff_remaining;
  611. else
  612. rsize = (size_t)length;
  613. wb = zip->wbuff + (sizeof(zip->wbuff) - zip->wbuff_remaining);
  614. rs = read(zip->temp_fd, wb, rsize);
  615. if (rs < 0) {
  616. archive_set_error(&(a->archive), errno,
  617. "Can't read temporary file(%jd)",
  618. (intmax_t)rs);
  619. return (ARCHIVE_FATAL);
  620. }
  621. if (rs == 0) {
  622. archive_set_error(&(a->archive), 0,
  623. "Truncated 7-Zip archive");
  624. return (ARCHIVE_FATAL);
  625. }
  626. zip->wbuff_remaining -= rs;
  627. length -= rs;
  628. if (zip->wbuff_remaining == 0) {
  629. r = flush_wbuff(a);
  630. if (r != ARCHIVE_OK)
  631. return (r);
  632. }
  633. }
  634. return (ARCHIVE_OK);
  635. }
  636. static int
  637. _7z_close(struct archive_write *a)
  638. {
  639. struct _7zip *zip;
  640. unsigned char *wb;
  641. uint64_t header_offset, header_size, header_unpacksize;
  642. uint64_t length;
  643. uint32_t header_crc32;
  644. int r;
  645. zip = (struct _7zip *)a->format_data;
  646. if (zip->total_number_entry > 0) {
  647. struct archive_rb_node *n;
  648. uint64_t data_offset, data_size, data_unpacksize;
  649. unsigned header_compression;
  650. r = (int)compress_out(a, NULL, 0, ARCHIVE_Z_FINISH);
  651. if (r < 0)
  652. return (r);
  653. data_offset = 0;
  654. data_size = zip->stream.total_out;
  655. data_unpacksize = zip->stream.total_in;
  656. zip->coder.codec = zip->opt_compression;
  657. zip->coder.prop_size = zip->stream.prop_size;
  658. zip->coder.props = zip->stream.props;
  659. zip->stream.prop_size = 0;
  660. zip->stream.props = NULL;
  661. zip->total_number_nonempty_entry =
  662. zip->total_number_entry - zip->total_number_empty_entry;
  663. /* Connect an empty file list. */
  664. if (zip->empty_list.first != NULL) {
  665. *zip->file_list.last = zip->empty_list.first;
  666. zip->file_list.last = zip->empty_list.last;
  667. }
  668. /* Connect a directory file list. */
  669. ARCHIVE_RB_TREE_FOREACH(n, &(zip->rbtree)) {
  670. file_register(zip, (struct file *)n);
  671. }
  672. /*
  673. * NOTE: 7z command supports just LZMA1, LZMA2 and COPY for
  674. * the compression type for encoding the header.
  675. */
  676. #if HAVE_LZMA_H
  677. header_compression = _7Z_LZMA1;
  678. /* If the stored file is only one, do not encode the header.
  679. * This is the same way 7z command does. */
  680. if (zip->total_number_entry == 1)
  681. header_compression = _7Z_COPY;
  682. #else
  683. header_compression = _7Z_COPY;
  684. #endif
  685. r = _7z_compression_init_encoder(a, header_compression, 6);
  686. if (r < 0)
  687. return (r);
  688. zip->crc32flg = PRECODE_CRC32;
  689. zip->precode_crc32 = 0;
  690. r = make_header(a, data_offset, data_size, data_unpacksize,
  691. 1, &(zip->coder));
  692. if (r < 0)
  693. return (r);
  694. r = (int)compress_out(a, NULL, 0, ARCHIVE_Z_FINISH);
  695. if (r < 0)
  696. return (r);
  697. header_offset = data_offset + data_size;
  698. header_size = zip->stream.total_out;
  699. header_crc32 = zip->precode_crc32;
  700. header_unpacksize = zip->stream.total_in;
  701. if (header_compression != _7Z_COPY) {
  702. /*
  703. * Encode the header in order to reduce the size
  704. * of the archive.
  705. */
  706. free(zip->coder.props);
  707. zip->coder.codec = header_compression;
  708. zip->coder.prop_size = zip->stream.prop_size;
  709. zip->coder.props = zip->stream.props;
  710. zip->stream.prop_size = 0;
  711. zip->stream.props = NULL;
  712. r = _7z_compression_init_encoder(a, _7Z_COPY, 0);
  713. if (r < 0)
  714. return (r);
  715. zip->crc32flg = ENCODED_CRC32;
  716. zip->encoded_crc32 = 0;
  717. /*
  718. * Make EncodedHeader.
  719. */
  720. r = enc_uint64(a, kEncodedHeader);
  721. if (r < 0)
  722. return (r);
  723. r = make_streamsInfo(a, header_offset, header_size,
  724. header_unpacksize, 1, &(zip->coder), 0,
  725. header_crc32);
  726. if (r < 0)
  727. return (r);
  728. r = (int)compress_out(a, NULL, 0, ARCHIVE_Z_FINISH);
  729. if (r < 0)
  730. return (r);
  731. header_offset = header_offset + header_size;
  732. header_size = zip->stream.total_out;
  733. header_crc32 = zip->encoded_crc32;
  734. }
  735. zip->crc32flg = 0;
  736. } else {
  737. header_offset = header_size = 0;
  738. header_crc32 = 0;
  739. }
  740. length = zip->temp_offset;
  741. /*
  742. * Make the zip header on wbuff(write buffer).
  743. */
  744. wb = zip->wbuff;
  745. zip->wbuff_remaining = sizeof(zip->wbuff);
  746. memcpy(&wb[0], "7z\xBC\xAF\x27\x1C", 6);
  747. wb[6] = 0;/* Major version. */
  748. wb[7] = 3;/* Minor version. */
  749. archive_le64enc(&wb[12], header_offset);/* Next Header Offset */
  750. archive_le64enc(&wb[20], header_size);/* Next Header Size */
  751. archive_le32enc(&wb[28], header_crc32);/* Next Header CRC */
  752. archive_le32enc(&wb[8], crc32(0, &wb[12], 20));/* Start Header CRC */
  753. zip->wbuff_remaining -= 32;
  754. /*
  755. * Read all file contents and an encoded header from the temporary
  756. * file and write out it.
  757. */
  758. r = copy_out(a, 0, length);
  759. if (r != ARCHIVE_OK)
  760. return (r);
  761. r = flush_wbuff(a);
  762. return (r);
  763. }
  764. /*
  765. * Encode 64 bits value into 7-Zip's encoded UINT64 value.
  766. */
  767. static int
  768. enc_uint64(struct archive_write *a, uint64_t val)
  769. {
  770. unsigned mask = 0x80;
  771. uint8_t numdata[9];
  772. int i;
  773. numdata[0] = 0;
  774. for (i = 1; i < (int)sizeof(numdata); i++) {
  775. if (val < mask) {
  776. numdata[0] |= (uint8_t)val;
  777. break;
  778. }
  779. numdata[i] = (uint8_t)val;
  780. val >>= 8;
  781. numdata[0] |= mask;
  782. mask >>= 1;
  783. }
  784. return ((int)compress_out(a, numdata, i, ARCHIVE_Z_RUN));
  785. }
  786. static int
  787. make_substreamsInfo(struct archive_write *a, struct coder *coders)
  788. {
  789. struct _7zip *zip = (struct _7zip *)a->format_data;
  790. struct file *file;
  791. int r;
  792. /*
  793. * Make SubStreamsInfo.
  794. */
  795. r = enc_uint64(a, kSubStreamsInfo);
  796. if (r < 0)
  797. return (r);
  798. if (zip->total_number_nonempty_entry > 1 && coders->codec != _7Z_COPY) {
  799. /*
  800. * Make NumUnPackStream.
  801. */
  802. r = enc_uint64(a, kNumUnPackStream);
  803. if (r < 0)
  804. return (r);
  805. /* Write numUnpackStreams */
  806. r = enc_uint64(a, zip->total_number_nonempty_entry);
  807. if (r < 0)
  808. return (r);
  809. /*
  810. * Make kSize.
  811. */
  812. r = enc_uint64(a, kSize);
  813. if (r < 0)
  814. return (r);
  815. file = zip->file_list.first;
  816. for (;file != NULL; file = file->next) {
  817. if (file->next == NULL ||
  818. file->next->size == 0)
  819. break;
  820. r = enc_uint64(a, file->size);
  821. if (r < 0)
  822. return (r);
  823. }
  824. }
  825. /*
  826. * Make CRC.
  827. */
  828. r = enc_uint64(a, kCRC);
  829. if (r < 0)
  830. return (r);
  831. /* All are defined */
  832. r = enc_uint64(a, 1);
  833. if (r < 0)
  834. return (r);
  835. file = zip->file_list.first;
  836. for (;file != NULL; file = file->next) {
  837. uint8_t crc[4];
  838. if (file->size == 0)
  839. break;
  840. archive_le32enc(crc, file->crc32);
  841. r = (int)compress_out(a, crc, 4, ARCHIVE_Z_RUN);
  842. if (r < 0)
  843. return (r);
  844. }
  845. /* Write End. */
  846. r = enc_uint64(a, kEnd);
  847. if (r < 0)
  848. return (r);
  849. return (ARCHIVE_OK);
  850. }
  851. static int
  852. make_streamsInfo(struct archive_write *a, uint64_t offset, uint64_t pack_size,
  853. uint64_t unpack_size, int num_coder, struct coder *coders, int substrm,
  854. uint32_t header_crc)
  855. {
  856. struct _7zip *zip = (struct _7zip *)a->format_data;
  857. uint8_t codec_buff[8];
  858. int numFolders, fi;
  859. int codec_size;
  860. int i, r;
  861. if (coders->codec == _7Z_COPY)
  862. numFolders = (int)zip->total_number_nonempty_entry;
  863. else
  864. numFolders = 1;
  865. /*
  866. * Make PackInfo.
  867. */
  868. r = enc_uint64(a, kPackInfo);
  869. if (r < 0)
  870. return (r);
  871. /* Write PackPos. */
  872. r = enc_uint64(a, offset);
  873. if (r < 0)
  874. return (r);
  875. /* Write NumPackStreams. */
  876. r = enc_uint64(a, numFolders);
  877. if (r < 0)
  878. return (r);
  879. /* Make Size. */
  880. r = enc_uint64(a, kSize);
  881. if (r < 0)
  882. return (r);
  883. if (numFolders > 1) {
  884. struct file *file = zip->file_list.first;
  885. for (;file != NULL; file = file->next) {
  886. if (file->size == 0)
  887. break;
  888. r = enc_uint64(a, file->size);
  889. if (r < 0)
  890. return (r);
  891. }
  892. } else {
  893. /* Write size. */
  894. r = enc_uint64(a, pack_size);
  895. if (r < 0)
  896. return (r);
  897. }
  898. r = enc_uint64(a, kEnd);
  899. if (r < 0)
  900. return (r);
  901. /*
  902. * Make UnPackInfo.
  903. */
  904. r = enc_uint64(a, kUnPackInfo);
  905. if (r < 0)
  906. return (r);
  907. /*
  908. * Make Folder.
  909. */
  910. r = enc_uint64(a, kFolder);
  911. if (r < 0)
  912. return (r);
  913. /* Write NumFolders. */
  914. r = enc_uint64(a, numFolders);
  915. if (r < 0)
  916. return (r);
  917. /* Write External. */
  918. r = enc_uint64(a, 0);
  919. if (r < 0)
  920. return (r);
  921. for (fi = 0; fi < numFolders; fi++) {
  922. /* Write NumCoders. */
  923. r = enc_uint64(a, num_coder);
  924. if (r < 0)
  925. return (r);
  926. for (i = 0; i < num_coder; i++) {
  927. unsigned codec_id = coders[i].codec;
  928. /* Write Codec flag. */
  929. archive_be64enc(codec_buff, codec_id);
  930. for (codec_size = 8; codec_size > 0; codec_size--) {
  931. if (codec_buff[8 - codec_size])
  932. break;
  933. }
  934. if (codec_size == 0)
  935. codec_size = 1;
  936. if (coders[i].prop_size)
  937. r = enc_uint64(a, codec_size | 0x20);
  938. else
  939. r = enc_uint64(a, codec_size);
  940. if (r < 0)
  941. return (r);
  942. /* Write Codec ID. */
  943. codec_size &= 0x0f;
  944. r = (int)compress_out(a, &codec_buff[8-codec_size],
  945. codec_size, ARCHIVE_Z_RUN);
  946. if (r < 0)
  947. return (r);
  948. if (coders[i].prop_size) {
  949. /* Write Codec property size. */
  950. r = enc_uint64(a, coders[i].prop_size);
  951. if (r < 0)
  952. return (r);
  953. /* Write Codec properties. */
  954. r = (int)compress_out(a, coders[i].props,
  955. coders[i].prop_size, ARCHIVE_Z_RUN);
  956. if (r < 0)
  957. return (r);
  958. }
  959. }
  960. }
  961. /*
  962. * Make CodersUnPackSize.
  963. */
  964. r = enc_uint64(a, kCodersUnPackSize);
  965. if (r < 0)
  966. return (r);
  967. if (numFolders > 1) {
  968. struct file *file = zip->file_list.first;
  969. for (;file != NULL; file = file->next) {
  970. if (file->size == 0)
  971. break;
  972. r = enc_uint64(a, file->size);
  973. if (r < 0)
  974. return (r);
  975. }
  976. } else {
  977. /* Write UnPackSize. */
  978. r = enc_uint64(a, unpack_size);
  979. if (r < 0)
  980. return (r);
  981. }
  982. if (!substrm) {
  983. uint8_t crc[4];
  984. /*
  985. * Make CRC.
  986. */
  987. r = enc_uint64(a, kCRC);
  988. if (r < 0)
  989. return (r);
  990. /* All are defined */
  991. r = enc_uint64(a, 1);
  992. if (r < 0)
  993. return (r);
  994. archive_le32enc(crc, header_crc);
  995. r = (int)compress_out(a, crc, 4, ARCHIVE_Z_RUN);
  996. if (r < 0)
  997. return (r);
  998. }
  999. /* Write End. */
  1000. r = enc_uint64(a, kEnd);
  1001. if (r < 0)
  1002. return (r);
  1003. if (substrm) {
  1004. /*
  1005. * Make SubStreamsInfo.
  1006. */
  1007. r = make_substreamsInfo(a, coders);
  1008. if (r < 0)
  1009. return (r);
  1010. }
  1011. /* Write End. */
  1012. r = enc_uint64(a, kEnd);
  1013. if (r < 0)
  1014. return (r);
  1015. return (ARCHIVE_OK);
  1016. }
  1017. #define EPOC_TIME ARCHIVE_LITERAL_ULL(116444736000000000)
  1018. static uint64_t
  1019. utcToFiletime(time_t t, long ns)
  1020. {
  1021. uint64_t fileTime;
  1022. fileTime = t;
  1023. fileTime *= 10000000;
  1024. fileTime += ns / 100;
  1025. fileTime += EPOC_TIME;
  1026. return (fileTime);
  1027. }
  1028. static int
  1029. make_time(struct archive_write *a, uint8_t type, unsigned flg, int ti)
  1030. {
  1031. uint8_t filetime[8];
  1032. struct _7zip *zip = (struct _7zip *)a->format_data;
  1033. struct file *file;
  1034. int r;
  1035. uint8_t b, mask;
  1036. /*
  1037. * Make Time Bools.
  1038. */
  1039. if (zip->total_number_time_defined[ti] == zip->total_number_entry) {
  1040. /* Write Time Type. */
  1041. r = enc_uint64(a, type);
  1042. if (r < 0)
  1043. return (r);
  1044. /* Write EmptyStream Size. */
  1045. r = enc_uint64(a, 2 + zip->total_number_entry * 8);
  1046. if (r < 0)
  1047. return (r);
  1048. /* All are defined. */
  1049. r = enc_uint64(a, 1);
  1050. if (r < 0)
  1051. return (r);
  1052. } else {
  1053. if (zip->total_number_time_defined[ti] == 0)
  1054. return (ARCHIVE_OK);
  1055. /* Write Time Type. */
  1056. r = enc_uint64(a, type);
  1057. if (r < 0)
  1058. return (r);
  1059. /* Write EmptyStream Size. */
  1060. r = enc_uint64(a, 2 + ((zip->total_number_entry + 7) >> 3)
  1061. + zip->total_number_time_defined[ti] * 8);
  1062. if (r < 0)
  1063. return (r);
  1064. /* All are not defined. */
  1065. r = enc_uint64(a, 0);
  1066. if (r < 0)
  1067. return (r);
  1068. b = 0;
  1069. mask = 0x80;
  1070. file = zip->file_list.first;
  1071. for (;file != NULL; file = file->next) {
  1072. if (file->flg & flg)
  1073. b |= mask;
  1074. mask >>= 1;
  1075. if (mask == 0) {
  1076. r = (int)compress_out(a, &b, 1, ARCHIVE_Z_RUN);
  1077. if (r < 0)
  1078. return (r);
  1079. mask = 0x80;
  1080. b = 0;
  1081. }
  1082. }
  1083. if (mask != 0x80) {
  1084. r = (int)compress_out(a, &b, 1, ARCHIVE_Z_RUN);
  1085. if (r < 0)
  1086. return (r);
  1087. }
  1088. }
  1089. /* External. */
  1090. r = enc_uint64(a, 0);
  1091. if (r < 0)
  1092. return (r);
  1093. /*
  1094. * Make Times.
  1095. */
  1096. file = zip->file_list.first;
  1097. for (;file != NULL; file = file->next) {
  1098. if ((file->flg & flg) == 0)
  1099. continue;
  1100. archive_le64enc(filetime, utcToFiletime(file->times[ti].time,
  1101. file->times[ti].time_ns));
  1102. r = (int)compress_out(a, filetime, 8, ARCHIVE_Z_RUN);
  1103. if (r < 0)
  1104. return (r);
  1105. }
  1106. return (ARCHIVE_OK);
  1107. }
  1108. static int
  1109. make_header(struct archive_write *a, uint64_t offset, uint64_t pack_size,
  1110. uint64_t unpack_size, int codernum, struct coder *coders)
  1111. {
  1112. struct _7zip *zip = (struct _7zip *)a->format_data;
  1113. struct file *file;
  1114. int r;
  1115. uint8_t b, mask;
  1116. /*
  1117. * Make FilesInfo.
  1118. */
  1119. r = enc_uint64(a, kHeader);
  1120. if (r < 0)
  1121. return (r);
  1122. /*
  1123. * If there are empty files only, do not write MainStreamInfo.
  1124. */
  1125. if (zip->total_number_nonempty_entry) {
  1126. /*
  1127. * Make MainStreamInfo.
  1128. */
  1129. r = enc_uint64(a, kMainStreamsInfo);
  1130. if (r < 0)
  1131. return (r);
  1132. r = make_streamsInfo(a, offset, pack_size, unpack_size,
  1133. codernum, coders, 1, 0);
  1134. if (r < 0)
  1135. return (r);
  1136. }
  1137. /*
  1138. * Make FilesInfo.
  1139. */
  1140. r = enc_uint64(a, kFilesInfo);
  1141. if (r < 0)
  1142. return (r);
  1143. /* Write numFiles. */
  1144. r = enc_uint64(a, zip->total_number_entry);
  1145. if (r < 0)
  1146. return (r);
  1147. if (zip->total_number_empty_entry > 0) {
  1148. /* Make EmptyStream. */
  1149. r = enc_uint64(a, kEmptyStream);
  1150. if (r < 0)
  1151. return (r);
  1152. /* Write EmptyStream Size. */
  1153. r = enc_uint64(a, (zip->total_number_entry+7)>>3);
  1154. if (r < 0)
  1155. return (r);
  1156. b = 0;
  1157. mask = 0x80;
  1158. file = zip->file_list.first;
  1159. for (;file != NULL; file = file->next) {
  1160. if (file->size == 0)
  1161. b |= mask;
  1162. mask >>= 1;
  1163. if (mask == 0) {
  1164. r = (int)compress_out(a, &b, 1, ARCHIVE_Z_RUN);
  1165. if (r < 0)
  1166. return (r);
  1167. mask = 0x80;
  1168. b = 0;
  1169. }
  1170. }
  1171. if (mask != 0x80) {
  1172. r = (int)compress_out(a, &b, 1, ARCHIVE_Z_RUN);
  1173. if (r < 0)
  1174. return (r);
  1175. }
  1176. }
  1177. if (zip->total_number_empty_entry > zip->total_number_dir_entry) {
  1178. /* Make EmptyFile. */
  1179. r = enc_uint64(a, kEmptyFile);
  1180. if (r < 0)
  1181. return (r);
  1182. /* Write EmptyFile Size. */
  1183. r = enc_uint64(a, (zip->total_number_empty_entry + 7) >> 3);
  1184. if (r < 0)
  1185. return (r);
  1186. b = 0;
  1187. mask = 0x80;
  1188. file = zip->file_list.first;
  1189. for (;file != NULL; file = file->next) {
  1190. if (file->size)
  1191. continue;
  1192. if (!file->dir)
  1193. b |= mask;
  1194. mask >>= 1;
  1195. if (mask == 0) {
  1196. r = (int)compress_out(a, &b, 1, ARCHIVE_Z_RUN);
  1197. if (r < 0)
  1198. return (r);
  1199. mask = 0x80;
  1200. b = 0;
  1201. }
  1202. }
  1203. if (mask != 0x80) {
  1204. r = (int)compress_out(a, &b, 1, ARCHIVE_Z_RUN);
  1205. if (r < 0)
  1206. return (r);
  1207. }
  1208. }
  1209. /* Make Name. */
  1210. r = enc_uint64(a, kName);
  1211. if (r < 0)
  1212. return (r);
  1213. /* Write Name size. */
  1214. r = enc_uint64(a, zip->total_bytes_entry_name+1);
  1215. if (r < 0)
  1216. return (r);
  1217. /* Write dmy byte. */
  1218. r = enc_uint64(a, 0);
  1219. if (r < 0)
  1220. return (r);
  1221. file = zip->file_list.first;
  1222. for (;file != NULL; file = file->next) {
  1223. r = (int)compress_out(a, file->utf16name, file->name_len+2,
  1224. ARCHIVE_Z_RUN);
  1225. if (r < 0)
  1226. return (r);
  1227. }
  1228. /* Make MTime. */
  1229. r = make_time(a, kMTime, MTIME_IS_SET, MTIME);
  1230. if (r < 0)
  1231. return (r);
  1232. /* Make CTime. */
  1233. r = make_time(a, kCTime, CTIME_IS_SET, CTIME);
  1234. if (r < 0)
  1235. return (r);
  1236. /* Make ATime. */
  1237. r = make_time(a, kATime, ATIME_IS_SET, ATIME);
  1238. if (r < 0)
  1239. return (r);
  1240. /* Make Attributes. */
  1241. r = enc_uint64(a, kAttributes);
  1242. if (r < 0)
  1243. return (r);
  1244. /* Write Attributes size. */
  1245. r = enc_uint64(a, 2 + zip->total_number_entry * 4);
  1246. if (r < 0)
  1247. return (r);
  1248. /* Write "All Are Defined". */
  1249. r = enc_uint64(a, 1);
  1250. if (r < 0)
  1251. return (r);
  1252. /* Write dmy byte. */
  1253. r = enc_uint64(a, 0);
  1254. if (r < 0)
  1255. return (r);
  1256. file = zip->file_list.first;
  1257. for (;file != NULL; file = file->next) {
  1258. /*
  1259. * High 16bits is unix mode.
  1260. * Low 16bits is Windows attributes.
  1261. */
  1262. uint32_t encattr, attr;
  1263. if (file->dir)
  1264. attr = 0x8010;
  1265. else
  1266. attr = 0x8020;
  1267. if ((file->mode & 0222) == 0)
  1268. attr |= 1;/* Read Only. */
  1269. attr |= ((uint32_t)file->mode) << 16;
  1270. archive_le32enc(&encattr, attr);
  1271. r = (int)compress_out(a, &encattr, 4, ARCHIVE_Z_RUN);
  1272. if (r < 0)
  1273. return (r);
  1274. }
  1275. /* Write End. */
  1276. r = enc_uint64(a, kEnd);
  1277. if (r < 0)
  1278. return (r);
  1279. /* Write End. */
  1280. r = enc_uint64(a, kEnd);
  1281. if (r < 0)
  1282. return (r);
  1283. return (ARCHIVE_OK);
  1284. }
  1285. static int
  1286. _7z_free(struct archive_write *a)
  1287. {
  1288. struct _7zip *zip = (struct _7zip *)a->format_data;
  1289. /* Close the temporary file. */
  1290. if (zip->temp_fd >= 0)
  1291. close(zip->temp_fd);
  1292. file_free_register(zip);
  1293. compression_end(&(a->archive), &(zip->stream));
  1294. free(zip->coder.props);
  1295. free(zip);
  1296. return (ARCHIVE_OK);
  1297. }
  1298. static int
  1299. file_cmp_node(const struct archive_rb_node *n1,
  1300. const struct archive_rb_node *n2)
  1301. {
  1302. const struct file *f1 = (const struct file *)n1;
  1303. const struct file *f2 = (const struct file *)n2;
  1304. if (f1->name_len == f2->name_len)
  1305. return (memcmp(f1->utf16name, f2->utf16name, f1->name_len));
  1306. return (f1->name_len > f2->name_len)?1:-1;
  1307. }
  1308. static int
  1309. file_cmp_key(const struct archive_rb_node *n, const void *key)
  1310. {
  1311. const struct file *f = (const struct file *)n;
  1312. return (f->name_len - *(const char *)key);
  1313. }
  1314. static int
  1315. file_new(struct archive_write *a, struct archive_entry *entry,
  1316. struct file **newfile)
  1317. {
  1318. struct _7zip *zip;
  1319. struct file *file;
  1320. const char *u16;
  1321. size_t u16len;
  1322. int ret = ARCHIVE_OK;
  1323. zip = (struct _7zip *)a->format_data;
  1324. *newfile = NULL;
  1325. file = calloc(1, sizeof(*file));
  1326. if (file == NULL) {
  1327. archive_set_error(&a->archive, ENOMEM,
  1328. "Can't allocate memory");
  1329. return (ARCHIVE_FATAL);
  1330. }
  1331. if (0 > archive_entry_pathname_l(entry, &u16, &u16len, zip->sconv)) {
  1332. if (errno == ENOMEM) {
  1333. free(file);
  1334. archive_set_error(&a->archive, ENOMEM,
  1335. "Can't allocate memory for UTF-16LE");
  1336. return (ARCHIVE_FATAL);
  1337. }
  1338. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1339. "A filename cannot be converted to UTF-16LE;"
  1340. "You should disable making Joliet extension");
  1341. ret = ARCHIVE_WARN;
  1342. }
  1343. file->utf16name = malloc(u16len + 2);
  1344. if (file->utf16name == NULL) {
  1345. free(file);
  1346. archive_set_error(&a->archive, ENOMEM,
  1347. "Can't allocate memory for Name");
  1348. return (ARCHIVE_FATAL);
  1349. }
  1350. memcpy(file->utf16name, u16, u16len);
  1351. file->utf16name[u16len+0] = 0;
  1352. file->utf16name[u16len+1] = 0;
  1353. file->name_len = (unsigned)u16len;
  1354. file->mode = archive_entry_mode(entry);
  1355. if (archive_entry_filetype(entry) == AE_IFREG)
  1356. file->size = archive_entry_size(entry);
  1357. else
  1358. archive_entry_set_size(entry, 0);
  1359. if (archive_entry_filetype(entry) == AE_IFDIR)
  1360. file->dir = 1;
  1361. else if (archive_entry_filetype(entry) == AE_IFLNK)
  1362. file->size = strlen(archive_entry_symlink(entry));
  1363. if (archive_entry_mtime_is_set(entry)) {
  1364. file->flg |= MTIME_IS_SET;
  1365. file->times[MTIME].time = archive_entry_mtime(entry);
  1366. file->times[MTIME].time_ns = archive_entry_mtime_nsec(entry);
  1367. }
  1368. if (archive_entry_atime_is_set(entry)) {
  1369. file->flg |= ATIME_IS_SET;
  1370. file->times[ATIME].time = archive_entry_atime(entry);
  1371. file->times[ATIME].time_ns = archive_entry_atime_nsec(entry);
  1372. }
  1373. if (archive_entry_ctime_is_set(entry)) {
  1374. file->flg |= CTIME_IS_SET;
  1375. file->times[CTIME].time = archive_entry_ctime(entry);
  1376. file->times[CTIME].time_ns = archive_entry_ctime_nsec(entry);
  1377. }
  1378. *newfile = file;
  1379. return (ret);
  1380. }
  1381. static void
  1382. file_free(struct file *file)
  1383. {
  1384. free(file->utf16name);
  1385. free(file);
  1386. }
  1387. static void
  1388. file_register(struct _7zip *zip, struct file *file)
  1389. {
  1390. file->next = NULL;
  1391. *zip->file_list.last = file;
  1392. zip->file_list.last = &(file->next);
  1393. }
  1394. static void
  1395. file_init_register(struct _7zip *zip)
  1396. {
  1397. zip->file_list.first = NULL;
  1398. zip->file_list.last = &(zip->file_list.first);
  1399. }
  1400. static void
  1401. file_free_register(struct _7zip *zip)
  1402. {
  1403. struct file *file, *file_next;
  1404. file = zip->file_list.first;
  1405. while (file != NULL) {
  1406. file_next = file->next;
  1407. file_free(file);
  1408. file = file_next;
  1409. }
  1410. }
  1411. static void
  1412. file_register_empty(struct _7zip *zip, struct file *file)
  1413. {
  1414. file->next = NULL;
  1415. *zip->empty_list.last = file;
  1416. zip->empty_list.last = &(file->next);
  1417. }
  1418. static void
  1419. file_init_register_empty(struct _7zip *zip)
  1420. {
  1421. zip->empty_list.first = NULL;
  1422. zip->empty_list.last = &(zip->empty_list.first);
  1423. }
  1424. #if !defined(HAVE_ZLIB_H) || !defined(HAVE_BZLIB_H) ||\
  1425. !defined(BZ_CONFIG_ERROR) || !defined(HAVE_LZMA_H)
  1426. static int
  1427. compression_unsupported_encoder(struct archive *a,
  1428. struct la_zstream *lastrm, const char *name)
  1429. {
  1430. archive_set_error(a, ARCHIVE_ERRNO_MISC,
  1431. "%s compression not supported on this platform", name);
  1432. lastrm->valid = 0;
  1433. lastrm->real_stream = NULL;
  1434. return (ARCHIVE_FAILED);
  1435. }
  1436. #endif
  1437. /*
  1438. * _7_COPY compressor.
  1439. */
  1440. static int
  1441. compression_init_encoder_copy(struct archive *a, struct la_zstream *lastrm)
  1442. {
  1443. if (lastrm->valid)
  1444. compression_end(a, lastrm);
  1445. lastrm->valid = 1;
  1446. lastrm->code = compression_code_copy;
  1447. lastrm->end = compression_end_copy;
  1448. return (ARCHIVE_OK);
  1449. }
  1450. static int
  1451. compression_code_copy(struct archive *a,
  1452. struct la_zstream *lastrm, enum la_zaction action)
  1453. {
  1454. size_t bytes;
  1455. (void)a; /* UNUSED */
  1456. if (lastrm->avail_out > lastrm->avail_in)
  1457. bytes = lastrm->avail_in;
  1458. else
  1459. bytes = lastrm->avail_out;
  1460. if (bytes) {
  1461. memcpy(lastrm->next_out, lastrm->next_in, bytes);
  1462. lastrm->next_in += bytes;
  1463. lastrm->avail_in -= bytes;
  1464. lastrm->total_in += bytes;
  1465. lastrm->next_out += bytes;
  1466. lastrm->avail_out -= bytes;
  1467. lastrm->total_out += bytes;
  1468. }
  1469. if (action == ARCHIVE_Z_FINISH && lastrm->avail_in == 0)
  1470. return (ARCHIVE_EOF);
  1471. return (ARCHIVE_OK);
  1472. }
  1473. static int
  1474. compression_end_copy(struct archive *a, struct la_zstream *lastrm)
  1475. {
  1476. (void)a; /* UNUSED */
  1477. lastrm->valid = 0;
  1478. return (ARCHIVE_OK);
  1479. }
  1480. /*
  1481. * _7_DEFLATE compressor.
  1482. */
  1483. #ifdef HAVE_ZLIB_H
  1484. static int
  1485. compression_init_encoder_deflate(struct archive *a,
  1486. struct la_zstream *lastrm, int level, int withheader)
  1487. {
  1488. z_stream *strm;
  1489. if (lastrm->valid)
  1490. compression_end(a, lastrm);
  1491. strm = calloc(1, sizeof(*strm));
  1492. if (strm == NULL) {
  1493. archive_set_error(a, ENOMEM,
  1494. "Can't allocate memory for gzip stream");
  1495. return (ARCHIVE_FATAL);
  1496. }
  1497. /* zlib.h is not const-correct, so we need this one bit
  1498. * of ugly hackery to convert a const * pointer to
  1499. * a non-const pointer. */
  1500. strm->next_in = (Bytef *)(uintptr_t)(const void *)lastrm->next_in;
  1501. strm->avail_in = (uInt)lastrm->avail_in;
  1502. strm->total_in = (uLong)lastrm->total_in;
  1503. strm->next_out = lastrm->next_out;
  1504. strm->avail_out = (uInt)lastrm->avail_out;
  1505. strm->total_out = (uLong)lastrm->total_out;
  1506. if (deflateInit2(strm, level, Z_DEFLATED,
  1507. (withheader)?15:-15,
  1508. 8, Z_DEFAULT_STRATEGY) != Z_OK) {
  1509. free(strm);
  1510. lastrm->real_stream = NULL;
  1511. archive_set_error(a, ARCHIVE_ERRNO_MISC,
  1512. "Internal error initializing compression library");
  1513. return (ARCHIVE_FATAL);
  1514. }
  1515. lastrm->real_stream = strm;
  1516. lastrm->valid = 1;
  1517. lastrm->code = compression_code_deflate;
  1518. lastrm->end = compression_end_deflate;
  1519. return (ARCHIVE_OK);
  1520. }
  1521. static int
  1522. compression_code_deflate(struct archive *a,
  1523. struct la_zstream *lastrm, enum la_zaction action)
  1524. {
  1525. z_stream *strm;
  1526. int r;
  1527. strm = (z_stream *)lastrm->real_stream;
  1528. /* zlib.h is not const-correct, so we need this one bit
  1529. * of ugly hackery to convert a const * pointer to
  1530. * a non-const pointer. */
  1531. strm->next_in = (Bytef *)(uintptr_t)(const void *)lastrm->next_in;
  1532. strm->avail_in = (uInt)lastrm->avail_in;
  1533. strm->total_in = (uLong)lastrm->total_in;
  1534. strm->next_out = lastrm->next_out;
  1535. strm->avail_out = (uInt)lastrm->avail_out;
  1536. strm->total_out = (uLong)lastrm->total_out;
  1537. r = deflate(strm,
  1538. (action == ARCHIVE_Z_FINISH)? Z_FINISH: Z_NO_FLUSH);
  1539. lastrm->next_in = strm->next_in;
  1540. lastrm->avail_in = strm->avail_in;
  1541. lastrm->total_in = strm->total_in;
  1542. lastrm->next_out = strm->next_out;
  1543. lastrm->avail_out = strm->avail_out;
  1544. lastrm->total_out = strm->total_out;
  1545. switch (r) {
  1546. case Z_OK:
  1547. return (ARCHIVE_OK);
  1548. case Z_STREAM_END:
  1549. return (ARCHIVE_EOF);
  1550. default:
  1551. archive_set_error(a, ARCHIVE_ERRNO_MISC,
  1552. "GZip compression failed:"
  1553. " deflate() call returned status %d", r);
  1554. return (ARCHIVE_FATAL);
  1555. }
  1556. }
  1557. static int
  1558. compression_end_deflate(struct archive *a, struct la_zstream *lastrm)
  1559. {
  1560. z_stream *strm;
  1561. int r;
  1562. strm = (z_stream *)lastrm->real_stream;
  1563. r = deflateEnd(strm);
  1564. free(strm);
  1565. lastrm->real_stream = NULL;
  1566. lastrm->valid = 0;
  1567. if (r != Z_OK) {
  1568. archive_set_error(a, ARCHIVE_ERRNO_MISC,
  1569. "Failed to clean up compressor");
  1570. return (ARCHIVE_FATAL);
  1571. }
  1572. return (ARCHIVE_OK);
  1573. }
  1574. #else
  1575. static int
  1576. compression_init_encoder_deflate(struct archive *a,
  1577. struct la_zstream *lastrm, int level, int withheader)
  1578. {
  1579. (void) level; /* UNUSED */
  1580. (void) withheader; /* UNUSED */
  1581. if (lastrm->valid)
  1582. compression_end(a, lastrm);
  1583. return (compression_unsupported_encoder(a, lastrm, "deflate"));
  1584. }
  1585. #endif
  1586. /*
  1587. * _7_BZIP2 compressor.
  1588. */
  1589. #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
  1590. static int
  1591. compression_init_encoder_bzip2(struct archive *a,
  1592. struct la_zstream *lastrm, int level)
  1593. {
  1594. bz_stream *strm;
  1595. if (lastrm->valid)
  1596. compression_end(a, lastrm);
  1597. strm = calloc(1, sizeof(*strm));
  1598. if (strm == NULL) {
  1599. archive_set_error(a, ENOMEM,
  1600. "Can't allocate memory for bzip2 stream");
  1601. return (ARCHIVE_FATAL);
  1602. }
  1603. /* bzlib.h is not const-correct, so we need this one bit
  1604. * of ugly hackery to convert a const * pointer to
  1605. * a non-const pointer. */
  1606. strm->next_in = (char *)(uintptr_t)(const void *)lastrm->next_in;
  1607. strm->avail_in = lastrm->avail_in;
  1608. strm->total_in_lo32 = (uint32_t)(lastrm->total_in & 0xffffffff);
  1609. strm->total_in_hi32 = (uint32_t)(lastrm->total_in >> 32);
  1610. strm->next_out = (char *)lastrm->next_out;
  1611. strm->avail_out = lastrm->avail_out;
  1612. strm->total_out_lo32 = (uint32_t)(lastrm->total_out & 0xffffffff);
  1613. strm->total_out_hi32 = (uint32_t)(lastrm->total_out >> 32);
  1614. if (BZ2_bzCompressInit(strm, level, 0, 30) != BZ_OK) {
  1615. free(strm);
  1616. lastrm->real_stream = NULL;
  1617. archive_set_error(a, ARCHIVE_ERRNO_MISC,
  1618. "Internal error initializing compression library");
  1619. return (ARCHIVE_FATAL);
  1620. }
  1621. lastrm->real_stream = strm;
  1622. lastrm->valid = 1;
  1623. lastrm->code = compression_code_bzip2;
  1624. lastrm->end = compression_end_bzip2;
  1625. return (ARCHIVE_OK);
  1626. }
  1627. static int
  1628. compression_code_bzip2(struct archive *a,
  1629. struct la_zstream *lastrm, enum la_zaction action)
  1630. {
  1631. bz_stream *strm;
  1632. int r;
  1633. strm = (bz_stream *)lastrm->real_stream;
  1634. /* bzlib.h is not const-correct, so we need this one bit
  1635. * of ugly hackery to convert a const * pointer to
  1636. * a non-const pointer. */
  1637. strm->next_in = (char *)(uintptr_t)(const void *)lastrm->next_in;
  1638. strm->avail_in = lastrm->avail_in;
  1639. strm->total_in_lo32 = (uint32_t)(lastrm->total_in & 0xffffffff);
  1640. strm->total_in_hi32 = (uint32_t)(lastrm->total_in >> 32);
  1641. strm->next_out = (char *)lastrm->next_out;
  1642. strm->avail_out = lastrm->avail_out;
  1643. strm->total_out_lo32 = (uint32_t)(lastrm->total_out & 0xffffffff);
  1644. strm->total_out_hi32 = (uint32_t)(lastrm->total_out >> 32);
  1645. r = BZ2_bzCompress(strm,
  1646. (action == ARCHIVE_Z_FINISH)? BZ_FINISH: BZ_RUN);
  1647. lastrm->next_in = (const unsigned char *)strm->next_in;
  1648. lastrm->avail_in = strm->avail_in;
  1649. lastrm->total_in =
  1650. (((uint64_t)(uint32_t)strm->total_in_hi32) << 32)
  1651. + (uint64_t)(uint32_t)strm->total_in_lo32;
  1652. lastrm->next_out = (unsigned char *)strm->next_out;
  1653. lastrm->avail_out = strm->avail_out;
  1654. lastrm->total_out =
  1655. (((uint64_t)(uint32_t)strm->total_out_hi32) << 32)
  1656. + (uint64_t)(uint32_t)strm->total_out_lo32;
  1657. switch (r) {
  1658. case BZ_RUN_OK: /* Non-finishing */
  1659. case BZ_FINISH_OK: /* Finishing: There's more work to do */
  1660. return (ARCHIVE_OK);
  1661. case BZ_STREAM_END: /* Finishing: all done */
  1662. /* Only occurs in finishing case */
  1663. return (ARCHIVE_EOF);
  1664. default:
  1665. /* Any other return value indicates an error */
  1666. archive_set_error(a, ARCHIVE_ERRNO_MISC,
  1667. "Bzip2 compression failed:"
  1668. " BZ2_bzCompress() call returned status %d", r);
  1669. return (ARCHIVE_FATAL);
  1670. }
  1671. }
  1672. static int
  1673. compression_end_bzip2(struct archive *a, struct la_zstream *lastrm)
  1674. {
  1675. bz_stream *strm;
  1676. int r;
  1677. strm = (bz_stream *)lastrm->real_stream;
  1678. r = BZ2_bzCompressEnd(strm);
  1679. free(strm);
  1680. lastrm->real_stream = NULL;
  1681. lastrm->valid = 0;
  1682. if (r != BZ_OK) {
  1683. archive_set_error(a, ARCHIVE_ERRNO_MISC,
  1684. "Failed to clean up compressor");
  1685. return (ARCHIVE_FATAL);
  1686. }
  1687. return (ARCHIVE_OK);
  1688. }
  1689. #else
  1690. static int
  1691. compression_init_encoder_bzip2(struct archive *a,
  1692. struct la_zstream *lastrm, int level)
  1693. {
  1694. (void) level; /* UNUSED */
  1695. if (lastrm->valid)
  1696. compression_end(a, lastrm);
  1697. return (compression_unsupported_encoder(a, lastrm, "bzip2"));
  1698. }
  1699. #endif
  1700. /*
  1701. * _7_LZMA1, _7_LZMA2 compressor.
  1702. */
  1703. #if defined(HAVE_LZMA_H)
  1704. static int
  1705. compression_init_encoder_lzma(struct archive *a,
  1706. struct la_zstream *lastrm, int level, uint64_t filter_id)
  1707. {
  1708. static const lzma_stream lzma_init_data = LZMA_STREAM_INIT;
  1709. lzma_stream *strm;
  1710. lzma_filter *lzmafilters;
  1711. lzma_options_lzma lzma_opt;
  1712. int r;
  1713. if (lastrm->valid)
  1714. compression_end(a, lastrm);
  1715. strm = calloc(1, sizeof(*strm) + sizeof(*lzmafilters) * 2);
  1716. if (strm == NULL) {
  1717. archive_set_error(a, ENOMEM,
  1718. "Can't allocate memory for lzma stream");
  1719. return (ARCHIVE_FATAL);
  1720. }
  1721. lzmafilters = (lzma_filter *)(strm+1);
  1722. if (level > 6)
  1723. level = 6;
  1724. if (lzma_lzma_preset(&lzma_opt, level)) {
  1725. free(strm);
  1726. lastrm->real_stream = NULL;
  1727. archive_set_error(a, ENOMEM,
  1728. "Internal error initializing compression library");
  1729. return (ARCHIVE_FATAL);
  1730. }
  1731. lzmafilters[0].id = filter_id;
  1732. lzmafilters[0].options = &lzma_opt;
  1733. lzmafilters[1].id = LZMA_VLI_UNKNOWN;/* Terminate */
  1734. r = lzma_properties_size(&(lastrm->prop_size), lzmafilters);
  1735. if (r != LZMA_OK) {
  1736. free(strm);
  1737. lastrm->real_stream = NULL;
  1738. archive_set_error(a, ARCHIVE_ERRNO_MISC,
  1739. "lzma_properties_size failed");
  1740. return (ARCHIVE_FATAL);
  1741. }
  1742. if (lastrm->prop_size) {
  1743. lastrm->props = malloc(lastrm->prop_size);
  1744. if (lastrm->props == NULL) {
  1745. free(strm);
  1746. lastrm->real_stream = NULL;
  1747. archive_set_error(a, ENOMEM,
  1748. "Cannot allocate memory");
  1749. return (ARCHIVE_FATAL);
  1750. }
  1751. r = lzma_properties_encode(lzmafilters, lastrm->props);
  1752. if (r != LZMA_OK) {
  1753. free(strm);
  1754. lastrm->real_stream = NULL;
  1755. archive_set_error(a, ARCHIVE_ERRNO_MISC,
  1756. "lzma_properties_encode failed");
  1757. return (ARCHIVE_FATAL);
  1758. }
  1759. }
  1760. *strm = lzma_init_data;
  1761. r = lzma_raw_encoder(strm, lzmafilters);
  1762. switch (r) {
  1763. case LZMA_OK:
  1764. lastrm->real_stream = strm;
  1765. lastrm->valid = 1;
  1766. lastrm->code = compression_code_lzma;
  1767. lastrm->end = compression_end_lzma;
  1768. r = ARCHIVE_OK;
  1769. break;
  1770. case LZMA_MEM_ERROR:
  1771. free(strm);
  1772. lastrm->real_stream = NULL;
  1773. archive_set_error(a, ENOMEM,
  1774. "Internal error initializing compression library: "
  1775. "Cannot allocate memory");
  1776. r = ARCHIVE_FATAL;
  1777. break;
  1778. default:
  1779. free(strm);
  1780. lastrm->real_stream = NULL;
  1781. archive_set_error(a, ARCHIVE_ERRNO_MISC,
  1782. "Internal error initializing compression library: "
  1783. "It's a bug in liblzma");
  1784. r = ARCHIVE_FATAL;
  1785. break;
  1786. }
  1787. return (r);
  1788. }
  1789. static int
  1790. compression_init_encoder_lzma1(struct archive *a,
  1791. struct la_zstream *lastrm, int level)
  1792. {
  1793. return compression_init_encoder_lzma(a, lastrm, level,
  1794. LZMA_FILTER_LZMA1);
  1795. }
  1796. static int
  1797. compression_init_encoder_lzma2(struct archive *a,
  1798. struct la_zstream *lastrm, int level)
  1799. {
  1800. return compression_init_encoder_lzma(a, lastrm, level,
  1801. LZMA_FILTER_LZMA2);
  1802. }
  1803. static int
  1804. compression_code_lzma(struct archive *a,
  1805. struct la_zstream *lastrm, enum la_zaction action)
  1806. {
  1807. lzma_stream *strm;
  1808. int r;
  1809. strm = (lzma_stream *)lastrm->real_stream;
  1810. strm->next_in = lastrm->next_in;
  1811. strm->avail_in = lastrm->avail_in;
  1812. strm->total_in = lastrm->total_in;
  1813. strm->next_out = lastrm->next_out;
  1814. strm->avail_out = lastrm->avail_out;
  1815. strm->total_out = lastrm->total_out;
  1816. r = lzma_code(strm,
  1817. (action == ARCHIVE_Z_FINISH)? LZMA_FINISH: LZMA_RUN);
  1818. lastrm->next_in = strm->next_in;
  1819. lastrm->avail_in = strm->avail_in;
  1820. lastrm->total_in = strm->total_in;
  1821. lastrm->next_out = strm->next_out;
  1822. lastrm->avail_out = strm->avail_out;
  1823. lastrm->total_out = strm->total_out;
  1824. switch (r) {
  1825. case LZMA_OK:
  1826. /* Non-finishing case */
  1827. return (ARCHIVE_OK);
  1828. case LZMA_STREAM_END:
  1829. /* This return can only occur in finishing case. */
  1830. return (ARCHIVE_EOF);
  1831. case LZMA_MEMLIMIT_ERROR:
  1832. archive_set_error(a, ENOMEM,
  1833. "lzma compression error:"
  1834. " %ju MiB would have been needed",
  1835. (uintmax_t)((lzma_memusage(strm) + 1024 * 1024 -1)
  1836. / (1024 * 1024)));
  1837. return (ARCHIVE_FATAL);
  1838. default:
  1839. /* Any other return value indicates an error */
  1840. archive_set_error(a, ARCHIVE_ERRNO_MISC,
  1841. "lzma compression failed:"
  1842. " lzma_code() call returned status %d", r);
  1843. return (ARCHIVE_FATAL);
  1844. }
  1845. }
  1846. static int
  1847. compression_end_lzma(struct archive *a, struct la_zstream *lastrm)
  1848. {
  1849. lzma_stream *strm;
  1850. (void)a; /* UNUSED */
  1851. strm = (lzma_stream *)lastrm->real_stream;
  1852. lzma_end(strm);
  1853. free(strm);
  1854. lastrm->valid = 0;
  1855. lastrm->real_stream = NULL;
  1856. return (ARCHIVE_OK);
  1857. }
  1858. #else
  1859. static int
  1860. compression_init_encoder_lzma1(struct archive *a,
  1861. struct la_zstream *lastrm, int level)
  1862. {
  1863. (void) level; /* UNUSED */
  1864. if (lastrm->valid)
  1865. compression_end(a, lastrm);
  1866. return (compression_unsupported_encoder(a, lastrm, "lzma"));
  1867. }
  1868. static int
  1869. compression_init_encoder_lzma2(struct archive *a,
  1870. struct la_zstream *lastrm, int level)
  1871. {
  1872. (void) level; /* UNUSED */
  1873. if (lastrm->valid)
  1874. compression_end(a, lastrm);
  1875. return (compression_unsupported_encoder(a, lastrm, "lzma"));
  1876. }
  1877. #endif
  1878. /*
  1879. * _7_PPMD compressor.
  1880. */
  1881. static void *
  1882. ppmd_alloc(void *p, size_t size)
  1883. {
  1884. (void)p;
  1885. return malloc(size);
  1886. }
  1887. static void
  1888. ppmd_free(void *p, void *address)
  1889. {
  1890. (void)p;
  1891. free(address);
  1892. }
  1893. static ISzAlloc g_szalloc = { ppmd_alloc, ppmd_free };
  1894. static void
  1895. ppmd_write(void *p, Byte b)
  1896. {
  1897. struct archive_write *a = ((IByteOut *)p)->a;
  1898. struct _7zip *zip = (struct _7zip *)(a->format_data);
  1899. struct la_zstream *lastrm = &(zip->stream);
  1900. struct ppmd_stream *strm;
  1901. if (lastrm->avail_out) {
  1902. *lastrm->next_out++ = b;
  1903. lastrm->avail_out--;
  1904. lastrm->total_out++;
  1905. return;
  1906. }
  1907. strm = (struct ppmd_stream *)lastrm->real_stream;
  1908. if (strm->buff_ptr < strm->buff_end) {
  1909. *strm->buff_ptr++ = b;
  1910. strm->buff_bytes++;
  1911. }
  1912. }
  1913. static int
  1914. compression_init_encoder_ppmd(struct archive *a,
  1915. struct la_zstream *lastrm, unsigned maxOrder, uint32_t msize)
  1916. {
  1917. struct ppmd_stream *strm;
  1918. uint8_t *props;
  1919. int r;
  1920. if (lastrm->valid)
  1921. compression_end(a, lastrm);
  1922. strm = calloc(1, sizeof(*strm));
  1923. if (strm == NULL) {
  1924. archive_set_error(a, ENOMEM,
  1925. "Can't allocate memory for PPMd");
  1926. return (ARCHIVE_FATAL);
  1927. }
  1928. strm->buff = malloc(32);
  1929. if (strm->buff == NULL) {
  1930. free(strm);
  1931. archive_set_error(a, ENOMEM,
  1932. "Can't allocate memory for PPMd");
  1933. return (ARCHIVE_FATAL);
  1934. }
  1935. strm->buff_ptr = strm->buff;
  1936. strm->buff_end = strm->buff + 32;
  1937. props = malloc(1+4);
  1938. if (props == NULL) {
  1939. free(strm->buff);
  1940. free(strm);
  1941. archive_set_error(a, ENOMEM,
  1942. "Coludn't allocate memory for PPMd");
  1943. return (ARCHIVE_FATAL);
  1944. }
  1945. props[0] = maxOrder;
  1946. archive_le32enc(props+1, msize);
  1947. __archive_ppmd7_functions.Ppmd7_Construct(&strm->ppmd7_context);
  1948. r = __archive_ppmd7_functions.Ppmd7_Alloc(
  1949. &strm->ppmd7_context, msize, &g_szalloc);
  1950. if (r == 0) {
  1951. free(strm->buff);
  1952. free(strm);
  1953. free(props);
  1954. archive_set_error(a, ENOMEM,
  1955. "Coludn't allocate memory for PPMd");
  1956. return (ARCHIVE_FATAL);
  1957. }
  1958. __archive_ppmd7_functions.Ppmd7_Init(&(strm->ppmd7_context), maxOrder);
  1959. strm->byteout.a = (struct archive_write *)a;
  1960. strm->byteout.Write = ppmd_write;
  1961. strm->range_enc.Stream = &(strm->byteout);
  1962. __archive_ppmd7_functions.Ppmd7z_RangeEnc_Init(&(strm->range_enc));
  1963. strm->stat = 0;
  1964. lastrm->real_stream = strm;
  1965. lastrm->valid = 1;
  1966. lastrm->code = compression_code_ppmd;
  1967. lastrm->end = compression_end_ppmd;
  1968. lastrm->prop_size = 5;
  1969. lastrm->props = props;
  1970. return (ARCHIVE_OK);
  1971. }
  1972. static int
  1973. compression_code_ppmd(struct archive *a,
  1974. struct la_zstream *lastrm, enum la_zaction action)
  1975. {
  1976. struct ppmd_stream *strm;
  1977. (void)a; /* UNUSED */
  1978. strm = (struct ppmd_stream *)lastrm->real_stream;
  1979. /* Copy encoded data if there are remaining bytes from previous call. */
  1980. if (strm->buff_bytes) {
  1981. uint8_t *p = strm->buff_ptr - strm->buff_bytes;
  1982. while (lastrm->avail_out && strm->buff_bytes) {
  1983. *lastrm->next_out++ = *p++;
  1984. lastrm->avail_out--;
  1985. lastrm->total_out++;
  1986. strm->buff_bytes--;
  1987. }
  1988. if (strm->buff_bytes)
  1989. return (ARCHIVE_OK);
  1990. if (strm->stat == 1)
  1991. return (ARCHIVE_EOF);
  1992. strm->buff_ptr = strm->buff;
  1993. }
  1994. while (lastrm->avail_in && lastrm->avail_out) {
  1995. __archive_ppmd7_functions.Ppmd7_EncodeSymbol(
  1996. &(strm->ppmd7_context), &(strm->range_enc),
  1997. *lastrm->next_in++);
  1998. lastrm->avail_in--;
  1999. lastrm->total_in++;
  2000. }
  2001. if (lastrm->avail_in == 0 && action == ARCHIVE_Z_FINISH) {
  2002. __archive_ppmd7_functions.Ppmd7z_RangeEnc_FlushData(
  2003. &(strm->range_enc));
  2004. strm->stat = 1;
  2005. /* Return EOF if there are no remaining bytes. */
  2006. if (strm->buff_bytes == 0)
  2007. return (ARCHIVE_EOF);
  2008. }
  2009. return (ARCHIVE_OK);
  2010. }
  2011. static int
  2012. compression_end_ppmd(struct archive *a, struct la_zstream *lastrm)
  2013. {
  2014. struct ppmd_stream *strm;
  2015. (void)a; /* UNUSED */
  2016. strm = (struct ppmd_stream *)lastrm->real_stream;
  2017. __archive_ppmd7_functions.Ppmd7_Free(&strm->ppmd7_context, &g_szalloc);
  2018. free(strm->buff);
  2019. free(strm);
  2020. lastrm->real_stream = NULL;
  2021. lastrm->valid = 0;
  2022. return (ARCHIVE_OK);
  2023. }
  2024. /*
  2025. * Universal compressor initializer.
  2026. */
  2027. static int
  2028. _7z_compression_init_encoder(struct archive_write *a, unsigned compression,
  2029. int compression_level)
  2030. {
  2031. struct _7zip *zip;
  2032. int r;
  2033. zip = (struct _7zip *)a->format_data;
  2034. switch (compression) {
  2035. case _7Z_DEFLATE:
  2036. r = compression_init_encoder_deflate(
  2037. &(a->archive), &(zip->stream),
  2038. compression_level, 0);
  2039. break;
  2040. case _7Z_BZIP2:
  2041. r = compression_init_encoder_bzip2(
  2042. &(a->archive), &(zip->stream),
  2043. compression_level);
  2044. break;
  2045. case _7Z_LZMA1:
  2046. r = compression_init_encoder_lzma1(
  2047. &(a->archive), &(zip->stream),
  2048. compression_level);
  2049. break;
  2050. case _7Z_LZMA2:
  2051. r = compression_init_encoder_lzma2(
  2052. &(a->archive), &(zip->stream),
  2053. compression_level);
  2054. break;
  2055. case _7Z_PPMD:
  2056. r = compression_init_encoder_ppmd(
  2057. &(a->archive), &(zip->stream),
  2058. PPMD7_DEFAULT_ORDER, PPMD7_DEFAULT_MEM_SIZE);
  2059. break;
  2060. case _7Z_COPY:
  2061. default:
  2062. r = compression_init_encoder_copy(
  2063. &(a->archive), &(zip->stream));
  2064. break;
  2065. }
  2066. if (r == ARCHIVE_OK) {
  2067. zip->stream.total_in = 0;
  2068. zip->stream.next_out = zip->wbuff;
  2069. zip->stream.avail_out = sizeof(zip->wbuff);
  2070. zip->stream.total_out = 0;
  2071. }
  2072. return (r);
  2073. }
  2074. static int
  2075. compression_code(struct archive *a, struct la_zstream *lastrm,
  2076. enum la_zaction action)
  2077. {
  2078. if (lastrm->valid)
  2079. return (lastrm->code(a, lastrm, action));
  2080. return (ARCHIVE_OK);
  2081. }
  2082. static int
  2083. compression_end(struct archive *a, struct la_zstream *lastrm)
  2084. {
  2085. if (lastrm->valid) {
  2086. lastrm->prop_size = 0;
  2087. free(lastrm->props);
  2088. lastrm->props = NULL;
  2089. return (lastrm->end(a, lastrm));
  2090. }
  2091. return (ARCHIVE_OK);
  2092. }