archive_write_set_format_7zip.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303
  1. /*-
  2. * Copyright (c) 2011 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 <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[1024 * 64];
  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 menber 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. "Illeagal value `%s'",
  371. value);
  372. return (ARCHIVE_FAILED);
  373. }
  374. zip->opt_compression_level = value[0] - '0';
  375. return (ARCHIVE_OK);
  376. }
  377. return (ARCHIVE_FAILED);
  378. }
  379. static int
  380. _7z_write_header(struct archive_write *a, struct archive_entry *entry)
  381. {
  382. struct _7zip *zip;
  383. struct file *file;
  384. int r;
  385. zip = (struct _7zip *)a->format_data;
  386. zip->cur_file = NULL;
  387. zip->entry_bytes_remaining = 0;
  388. if (zip->sconv == NULL) {
  389. zip->sconv = archive_string_conversion_to_charset(
  390. &a->archive, "UTF-16LE", 1);
  391. if (zip->sconv == NULL)
  392. return (ARCHIVE_FATAL);
  393. }
  394. r = file_new(a, entry, &file);
  395. if (r < ARCHIVE_WARN) {
  396. file_free(file);
  397. return (r);
  398. }
  399. if (file->flg & MTIME_IS_SET)
  400. zip->total_number_time_defined[MTIME]++;
  401. if (file->flg & CTIME_IS_SET)
  402. zip->total_number_time_defined[CTIME]++;
  403. if (file->flg & ATIME_IS_SET)
  404. zip->total_number_time_defined[ATIME]++;
  405. if (file->size == 0 && file->dir) {
  406. if (!__archive_rb_tree_insert_node(&(zip->rbtree),
  407. (struct archive_rb_node *)file))
  408. file_free(file);
  409. }
  410. zip->total_number_entry++;
  411. zip->total_bytes_entry_name += file->name_len + 2;
  412. if (file->size == 0) {
  413. /* Count up the number of empty files. */
  414. zip->total_number_empty_entry++;
  415. if (file->dir)
  416. zip->total_number_dir_entry++;
  417. else
  418. file_register_empty(zip, file);
  419. return (r);
  420. }
  421. /*
  422. * Init compression.
  423. */
  424. if ((zip->total_number_entry - zip->total_number_empty_entry) == 1) {
  425. r = _7z_compression_init_encoder(a, zip->opt_compression,
  426. zip->opt_compression_level);
  427. if (r < 0) {
  428. file_free(file);
  429. return (ARCHIVE_FATAL);
  430. }
  431. }
  432. /* Register a non-empty file. */
  433. file_register(zip, file);
  434. /*
  435. * Set the current file to cur_file to read its contents.
  436. */
  437. zip->cur_file = file;
  438. /* Save a offset of current file in temporary file. */
  439. zip->entry_bytes_remaining = file->size;
  440. zip->entry_crc32 = 0;
  441. /*
  442. * Store a symbolic link name as file contents.
  443. */
  444. if (archive_entry_filetype(entry) == AE_IFLNK) {
  445. ssize_t bytes;
  446. const void *p = (const void *)archive_entry_symlink(entry);
  447. bytes = compress_out(a, p, file->size, ARCHIVE_Z_RUN);
  448. if (bytes < 0)
  449. return ((int)bytes);
  450. zip->entry_crc32 = crc32(zip->entry_crc32, p, bytes);
  451. zip->entry_bytes_remaining -= bytes;
  452. }
  453. return (r);
  454. }
  455. /*
  456. * Write data to a temporary file.
  457. */
  458. static int
  459. write_to_temp(struct archive_write *a, const void *buff, size_t s)
  460. {
  461. struct _7zip *zip;
  462. unsigned char *p;
  463. ssize_t ws;
  464. zip = (struct _7zip *)a->format_data;
  465. /*
  466. * Open a temporary file.
  467. */
  468. if (zip->temp_fd == -1) {
  469. zip->temp_offset = 0;
  470. zip->temp_fd = __archive_mktemp(NULL);
  471. if (zip->temp_fd < 0) {
  472. archive_set_error(&a->archive, errno,
  473. "Couldn't create temporary file");
  474. return (ARCHIVE_FATAL);
  475. }
  476. }
  477. p = (unsigned char *)buff;
  478. while (s) {
  479. ws = write(zip->temp_fd, p, s);
  480. if (ws < 0) {
  481. archive_set_error(&(a->archive), errno,
  482. "fwrite function failed");
  483. return (ARCHIVE_FATAL);
  484. }
  485. s -= ws;
  486. p += ws;
  487. zip->temp_offset += ws;
  488. }
  489. return (ARCHIVE_OK);
  490. }
  491. static ssize_t
  492. compress_out(struct archive_write *a, const void *buff, size_t s,
  493. enum la_zaction run)
  494. {
  495. struct _7zip *zip = (struct _7zip *)a->format_data;
  496. int r;
  497. if (run == ARCHIVE_Z_FINISH && zip->stream.total_in == 0 && s == 0)
  498. return (0);
  499. if ((zip->crc32flg & PRECODE_CRC32) && s)
  500. zip->precode_crc32 = crc32(zip->precode_crc32, buff, s);
  501. zip->stream.next_in = (const unsigned char *)buff;
  502. zip->stream.avail_in = s;
  503. do {
  504. /* Compress file data. */
  505. r = compression_code(&(a->archive), &(zip->stream), run);
  506. if (r != ARCHIVE_OK && r != ARCHIVE_EOF)
  507. return (ARCHIVE_FATAL);
  508. if (zip->stream.avail_out == 0) {
  509. if (write_to_temp(a, zip->wbuff, sizeof(zip->wbuff))
  510. != ARCHIVE_OK)
  511. return (ARCHIVE_FATAL);
  512. zip->stream.next_out = zip->wbuff;
  513. zip->stream.avail_out = sizeof(zip->wbuff);
  514. if (zip->crc32flg & ENCODED_CRC32)
  515. zip->encoded_crc32 = crc32(zip->encoded_crc32,
  516. zip->wbuff, sizeof(zip->wbuff));
  517. }
  518. } while (zip->stream.avail_in);
  519. if (run == ARCHIVE_Z_FINISH) {
  520. uint64_t bytes = sizeof(zip->wbuff) - zip->stream.avail_out;
  521. if (write_to_temp(a, zip->wbuff, bytes) != ARCHIVE_OK)
  522. return (ARCHIVE_FATAL);
  523. if ((zip->crc32flg & ENCODED_CRC32) && bytes)
  524. zip->encoded_crc32 = crc32(zip->encoded_crc32,
  525. zip->wbuff, bytes);
  526. }
  527. return (s);
  528. }
  529. static ssize_t
  530. _7z_write_data(struct archive_write *a, const void *buff, size_t s)
  531. {
  532. struct _7zip *zip;
  533. ssize_t bytes;
  534. zip = (struct _7zip *)a->format_data;
  535. if (s > zip->entry_bytes_remaining)
  536. s = zip->entry_bytes_remaining;
  537. if (s == 0 || zip->cur_file == NULL)
  538. return (0);
  539. bytes = compress_out(a, buff, s, ARCHIVE_Z_RUN);
  540. if (bytes < 0)
  541. return (bytes);
  542. zip->entry_crc32 = crc32(zip->entry_crc32, buff, bytes);
  543. zip->entry_bytes_remaining -= bytes;
  544. return (bytes);
  545. }
  546. static int
  547. _7z_finish_entry(struct archive_write *a)
  548. {
  549. struct _7zip *zip;
  550. size_t s;
  551. ssize_t r;
  552. zip = (struct _7zip *)a->format_data;
  553. if (zip->cur_file == NULL)
  554. return (ARCHIVE_OK);
  555. while (zip->entry_bytes_remaining > 0) {
  556. s = zip->entry_bytes_remaining;
  557. if (s > a->null_length)
  558. s = a->null_length;
  559. r = _7z_write_data(a, a->nulls, s);
  560. if (r < 0)
  561. return (r);
  562. }
  563. zip->total_bytes_compressed += zip->stream.total_in;
  564. zip->total_bytes_uncompressed += zip->stream.total_out;
  565. zip->cur_file->crc32 = zip->entry_crc32;
  566. zip->cur_file = NULL;
  567. return (ARCHIVE_OK);
  568. }
  569. static int
  570. flush_wbuff(struct archive_write *a)
  571. {
  572. struct _7zip *zip;
  573. int r;
  574. size_t s;
  575. zip = (struct _7zip *)a->format_data;
  576. s = sizeof(zip->wbuff) - zip->wbuff_remaining;
  577. r = __archive_write_output(a, zip->wbuff, s);
  578. if (r != ARCHIVE_OK)
  579. return (r);
  580. zip->wbuff_remaining = sizeof(zip->wbuff);
  581. return (r);
  582. }
  583. static int
  584. copy_out(struct archive_write *a, uint64_t offset, uint64_t length)
  585. {
  586. struct _7zip *zip;
  587. int r;
  588. zip = (struct _7zip *)a->format_data;
  589. if (zip->temp_offset > 0 &&
  590. lseek(zip->temp_fd, offset, SEEK_SET) < 0) {
  591. archive_set_error(&(a->archive), errno, "lseek failed");
  592. return (ARCHIVE_FATAL);
  593. }
  594. while (length) {
  595. size_t rsize;
  596. ssize_t rs;
  597. unsigned char *wb;
  598. if (length > zip->wbuff_remaining)
  599. rsize = zip->wbuff_remaining;
  600. else
  601. rsize = (size_t)length;
  602. wb = zip->wbuff + (sizeof(zip->wbuff) - zip->wbuff_remaining);
  603. rs = read(zip->temp_fd, wb, rsize);
  604. if (rs < 0) {
  605. archive_set_error(&(a->archive), errno,
  606. "Can't read temporary file(%jd)",
  607. (intmax_t)rs);
  608. return (ARCHIVE_FATAL);
  609. }
  610. if (rs == 0) {
  611. archive_set_error(&(a->archive), 0,
  612. "Truncated 7-Zip archive");
  613. return (ARCHIVE_FATAL);
  614. }
  615. zip->wbuff_remaining -= rs;
  616. length -= rs;
  617. if (zip->wbuff_remaining == 0) {
  618. r = flush_wbuff(a);
  619. if (r != ARCHIVE_OK)
  620. return (r);
  621. }
  622. }
  623. return (ARCHIVE_OK);
  624. }
  625. static int
  626. _7z_close(struct archive_write *a)
  627. {
  628. struct _7zip *zip;
  629. unsigned char *wb;
  630. uint64_t header_offset, header_size, header_unpacksize;
  631. uint64_t length;
  632. uint32_t header_crc32;
  633. int r;
  634. zip = (struct _7zip *)a->format_data;
  635. if (zip->total_number_entry > 0) {
  636. struct archive_rb_node *n;
  637. uint64_t data_offset, data_size, data_unpacksize;
  638. unsigned header_compression;
  639. r = (int)compress_out(a, NULL, 0, ARCHIVE_Z_FINISH);
  640. if (r < 0)
  641. return (r);
  642. data_offset = 0;
  643. data_size = zip->stream.total_out;
  644. data_unpacksize = zip->stream.total_in;
  645. zip->coder.codec = zip->opt_compression;
  646. zip->coder.prop_size = zip->stream.prop_size;
  647. zip->coder.props = zip->stream.props;
  648. zip->stream.prop_size = 0;
  649. zip->stream.props = NULL;
  650. zip->total_number_nonempty_entry =
  651. zip->total_number_entry - zip->total_number_empty_entry;
  652. /* Connect an empty file list. */
  653. *zip->file_list.last = zip->empty_list.first;
  654. zip->file_list.last = zip->empty_list.last;
  655. /* Connect a directory file list. */
  656. ARCHIVE_RB_TREE_FOREACH(n, &(zip->rbtree)) {
  657. file_register(zip, (struct file *)n);
  658. }
  659. /*
  660. * NOTE: 7z command supports just LZMA1, LZMA2 and COPY for
  661. * the compression type for encoding the header.
  662. */
  663. #if HAVE_LZMA_H
  664. header_compression = _7Z_LZMA1;
  665. /* If the stored file is only one, do not encode the header.
  666. * This is the same way 7z command does. */
  667. if (zip->total_number_entry == 1)
  668. header_compression = _7Z_COPY;
  669. #else
  670. header_compression = _7Z_COPY;
  671. #endif
  672. r = _7z_compression_init_encoder(a, header_compression, 6);
  673. if (r < 0)
  674. return (r);
  675. zip->crc32flg = PRECODE_CRC32;
  676. zip->precode_crc32 = 0;
  677. r = make_header(a, data_offset, data_size, data_unpacksize,
  678. 1, &(zip->coder));
  679. if (r < 0)
  680. return (r);
  681. r = (int)compress_out(a, NULL, 0, ARCHIVE_Z_FINISH);
  682. if (r < 0)
  683. return (r);
  684. header_offset = data_offset + data_size;
  685. header_size = zip->stream.total_out;
  686. header_crc32 = zip->precode_crc32;
  687. header_unpacksize = zip->stream.total_in;
  688. if (header_compression != _7Z_COPY) {
  689. /*
  690. * Encode the header in order to reduce the size
  691. * of the archive.
  692. */
  693. free(zip->coder.props);
  694. zip->coder.codec = header_compression;
  695. zip->coder.prop_size = zip->stream.prop_size;
  696. zip->coder.props = zip->stream.props;
  697. zip->stream.prop_size = 0;
  698. zip->stream.props = NULL;
  699. r = _7z_compression_init_encoder(a, _7Z_COPY, 0);
  700. if (r < 0)
  701. return (r);
  702. zip->crc32flg = ENCODED_CRC32;
  703. zip->encoded_crc32 = 0;
  704. /*
  705. * Make EncodedHeader.
  706. */
  707. r = enc_uint64(a, kEncodedHeader);
  708. if (r < 0)
  709. return (r);
  710. r = make_streamsInfo(a, header_offset, header_size,
  711. header_unpacksize, 1, &(zip->coder), 0,
  712. header_crc32);
  713. if (r < 0)
  714. return (r);
  715. r = (int)compress_out(a, NULL, 0, ARCHIVE_Z_FINISH);
  716. if (r < 0)
  717. return (r);
  718. header_offset = header_offset + header_size;
  719. header_size = zip->stream.total_out;
  720. header_crc32 = zip->encoded_crc32;
  721. }
  722. zip->crc32flg = 0;
  723. } else {
  724. header_offset = header_size = 0;
  725. header_crc32 = 0;
  726. }
  727. length = zip->temp_offset;
  728. /*
  729. * Make the zip header on wbuff(write buffer).
  730. */
  731. wb = zip->wbuff;
  732. zip->wbuff_remaining = sizeof(zip->wbuff);
  733. memcpy(&wb[0], "7z\xBC\xAF\x27\x1C", 6);
  734. wb[6] = 0;/* Major version. */
  735. wb[7] = 3;/* Minor version. */
  736. archive_le64enc(&wb[12], header_offset);/* Next Header Offset */
  737. archive_le64enc(&wb[20], header_size);/* Next Header Size */
  738. archive_le32enc(&wb[28], header_crc32);/* Next Header CRC */
  739. archive_le32enc(&wb[8], crc32(0, &wb[12], 20));/* Start Header CRC */
  740. zip->wbuff_remaining -= 32;
  741. /*
  742. * Read all file contents and an encoded header from the temporary
  743. * file and write out it.
  744. */
  745. r = copy_out(a, 0, length);
  746. if (r != ARCHIVE_OK)
  747. return (r);
  748. r = flush_wbuff(a);
  749. return (r);
  750. }
  751. /*
  752. * Encode 64 bits value into 7-Zip's encoded UINT64 value.
  753. */
  754. static int
  755. enc_uint64(struct archive_write *a, uint64_t val)
  756. {
  757. unsigned mask = 0x80;
  758. uint8_t numdata[9];
  759. int i;
  760. numdata[0] = 0;
  761. for (i = 1; i < sizeof(numdata); i++) {
  762. if (val < mask) {
  763. numdata[0] |= (uint8_t)val;
  764. break;
  765. }
  766. numdata[i] = (uint8_t)val;
  767. val >>= 8;
  768. numdata[0] |= mask;
  769. mask >>= 1;
  770. }
  771. return (compress_out(a, numdata, i, ARCHIVE_Z_RUN));
  772. }
  773. static int
  774. make_substreamsInfo(struct archive_write *a, struct coder *coders)
  775. {
  776. struct _7zip *zip = (struct _7zip *)a->format_data;
  777. struct file *file;
  778. int r;
  779. /*
  780. * Make SubStreamsInfo.
  781. */
  782. r = enc_uint64(a, kSubStreamsInfo);
  783. if (r < 0)
  784. return (r);
  785. if (zip->total_number_nonempty_entry > 1 && coders->codec != _7Z_COPY) {
  786. /*
  787. * Make NumUnPackStream.
  788. */
  789. r = enc_uint64(a, kNumUnPackStream);
  790. if (r < 0)
  791. return (r);
  792. /* Write numUnpackStreams */
  793. r = enc_uint64(a, zip->total_number_nonempty_entry);
  794. if (r < 0)
  795. return (r);
  796. /*
  797. * Make kSize.
  798. */
  799. r = enc_uint64(a, kSize);
  800. if (r < 0)
  801. return (r);
  802. file = zip->file_list.first;
  803. for (;file != NULL; file = file->next) {
  804. if (file->next == NULL ||
  805. file->next->size == 0)
  806. break;
  807. r = enc_uint64(a, file->size);
  808. if (r < 0)
  809. return (r);
  810. }
  811. }
  812. /*
  813. * Make CRC.
  814. */
  815. r = enc_uint64(a, kCRC);
  816. if (r < 0)
  817. return (r);
  818. /* All are defined */
  819. r = enc_uint64(a, 1);
  820. if (r < 0)
  821. return (r);
  822. file = zip->file_list.first;
  823. for (;file != NULL; file = file->next) {
  824. uint8_t crc[4];
  825. if (file->size == 0)
  826. break;
  827. archive_le32enc(crc, file->crc32);
  828. r = compress_out(a, crc, 4, ARCHIVE_Z_RUN);
  829. if (r < 0)
  830. return (r);
  831. }
  832. /* Write End. */
  833. r = enc_uint64(a, kEnd);
  834. if (r < 0)
  835. return (r);
  836. return (ARCHIVE_OK);
  837. }
  838. static int
  839. make_streamsInfo(struct archive_write *a, uint64_t offset, uint64_t pack_size,
  840. uint64_t unpack_size, int num_coder, struct coder *coders, int substrm,
  841. uint32_t header_crc)
  842. {
  843. struct _7zip *zip = (struct _7zip *)a->format_data;
  844. uint8_t codec_buff[8];
  845. int numFolders, fi;
  846. int codec_size;
  847. int i, r;
  848. if (coders->codec == _7Z_COPY)
  849. numFolders = zip->total_number_nonempty_entry;
  850. else
  851. numFolders = 1;
  852. /*
  853. * Make PackInfo.
  854. */
  855. r = enc_uint64(a, kPackInfo);
  856. if (r < 0)
  857. return (r);
  858. /* Write PackPos. */
  859. r = enc_uint64(a, offset);
  860. if (r < 0)
  861. return (r);
  862. /* Write NumPackStreams. */
  863. r = enc_uint64(a, numFolders);
  864. if (r < 0)
  865. return (r);
  866. /* Make Size. */
  867. r = enc_uint64(a, kSize);
  868. if (r < 0)
  869. return (r);
  870. if (numFolders > 1) {
  871. struct file *file = zip->file_list.first;
  872. for (;file != NULL; file = file->next) {
  873. if (file->size == 0)
  874. break;
  875. r = enc_uint64(a, file->size);
  876. if (r < 0)
  877. return (r);
  878. }
  879. } else {
  880. /* Write size. */
  881. r = enc_uint64(a, pack_size);
  882. if (r < 0)
  883. return (r);
  884. }
  885. r = enc_uint64(a, kEnd);
  886. if (r < 0)
  887. return (r);
  888. /*
  889. * Make UnPackInfo.
  890. */
  891. r = enc_uint64(a, kUnPackInfo);
  892. if (r < 0)
  893. return (r);
  894. /*
  895. * Make Folder.
  896. */
  897. r = enc_uint64(a, kFolder);
  898. if (r < 0)
  899. return (r);
  900. /* Write NumFolders. */
  901. r = enc_uint64(a, numFolders);
  902. if (r < 0)
  903. return (r);
  904. /* Write External. */
  905. r = enc_uint64(a, 0);
  906. if (r < 0)
  907. return (r);
  908. for (fi = 0; fi < numFolders; fi++) {
  909. /* Write NumCoders. */
  910. r = enc_uint64(a, num_coder);
  911. if (r < 0)
  912. return (r);
  913. for (i = 0; i < num_coder; i++) {
  914. unsigned codec_id = coders[i].codec;
  915. /* Write Codec flag. */
  916. archive_be64enc(codec_buff, codec_id);
  917. for (codec_size = 8; codec_size > 0; codec_size--) {
  918. if (codec_buff[8 - codec_size])
  919. break;
  920. }
  921. if (codec_size == 0)
  922. codec_size = 1;
  923. if (coders[i].prop_size)
  924. r = enc_uint64(a, codec_size | 0x20);
  925. else
  926. r = enc_uint64(a, codec_size);
  927. if (r < 0)
  928. return (r);
  929. /* Write Codec ID. */
  930. codec_size &= 0x0f;
  931. r = compress_out(a, &codec_buff[8-codec_size],
  932. codec_size, ARCHIVE_Z_RUN);
  933. if (r < 0)
  934. return (r);
  935. if (coders[i].prop_size) {
  936. /* Write Codec property size. */
  937. r = enc_uint64(a, coders[i].prop_size);
  938. if (r < 0)
  939. return (r);
  940. /* Write Codec properties. */
  941. r = compress_out(a, coders[i].props,
  942. coders[i].prop_size, ARCHIVE_Z_RUN);
  943. if (r < 0)
  944. return (r);
  945. }
  946. }
  947. }
  948. /*
  949. * Make CodersUnPackSize.
  950. */
  951. r = enc_uint64(a, kCodersUnPackSize);
  952. if (r < 0)
  953. return (r);
  954. if (numFolders > 1) {
  955. struct file *file = zip->file_list.first;
  956. for (;file != NULL; file = file->next) {
  957. if (file->size == 0)
  958. break;
  959. r = enc_uint64(a, file->size);
  960. if (r < 0)
  961. return (r);
  962. }
  963. } else {
  964. /* Write UnPackSize. */
  965. r = enc_uint64(a, unpack_size);
  966. if (r < 0)
  967. return (r);
  968. }
  969. if (!substrm) {
  970. uint8_t crc[4];
  971. /*
  972. * Make CRC.
  973. */
  974. r = enc_uint64(a, kCRC);
  975. if (r < 0)
  976. return (r);
  977. /* All are defined */
  978. r = enc_uint64(a, 1);
  979. if (r < 0)
  980. return (r);
  981. archive_le32enc(crc, header_crc);
  982. r = compress_out(a, crc, 4, ARCHIVE_Z_RUN);
  983. if (r < 0)
  984. return (r);
  985. }
  986. /* Write End. */
  987. r = enc_uint64(a, kEnd);
  988. if (r < 0)
  989. return (r);
  990. if (substrm) {
  991. /*
  992. * Make SubStreamsInfo.
  993. */
  994. r = make_substreamsInfo(a, coders);
  995. if (r < 0)
  996. return (r);
  997. }
  998. /* Write End. */
  999. r = enc_uint64(a, kEnd);
  1000. if (r < 0)
  1001. return (r);
  1002. return (ARCHIVE_OK);
  1003. }
  1004. #define EPOC_TIME ARCHIVE_LITERAL_ULL(116444736000000000)
  1005. static uint64_t
  1006. utcToFiletime(time_t time, long ns)
  1007. {
  1008. uint64_t fileTime;
  1009. fileTime = time;
  1010. fileTime *= 10000000;
  1011. fileTime += ns / 100;
  1012. fileTime += EPOC_TIME;
  1013. return (fileTime);
  1014. }
  1015. static int
  1016. make_time(struct archive_write *a, uint8_t type, unsigned flg, int ti)
  1017. {
  1018. uint8_t filetime[8];
  1019. struct _7zip *zip = (struct _7zip *)a->format_data;
  1020. struct file *file;
  1021. int r;
  1022. uint8_t mask, byte;
  1023. /*
  1024. * Make Time Bools.
  1025. */
  1026. if (zip->total_number_time_defined[ti] == zip->total_number_entry) {
  1027. /* Write Time Type. */
  1028. r = enc_uint64(a, type);
  1029. if (r < 0)
  1030. return (r);
  1031. /* Write EmptyStream Size. */
  1032. r = enc_uint64(a, 2 + zip->total_number_entry * 8);
  1033. if (r < 0)
  1034. return (r);
  1035. /* All are defined. */
  1036. r = enc_uint64(a, 1);
  1037. if (r < 0)
  1038. return (r);
  1039. } else {
  1040. if (zip->total_number_time_defined[ti] == 0)
  1041. return (ARCHIVE_OK);
  1042. /* Write Time Type. */
  1043. r = enc_uint64(a, type);
  1044. if (r < 0)
  1045. return (r);
  1046. /* Write EmptyStream Size. */
  1047. r = enc_uint64(a, 2 + ((zip->total_number_entry + 7) >> 3)
  1048. + zip->total_number_time_defined[ti] * 8);
  1049. if (r < 0)
  1050. return (r);
  1051. /* All are not defined. */
  1052. r = enc_uint64(a, 0);
  1053. if (r < 0)
  1054. return (r);
  1055. byte = 0;
  1056. mask = 0x80;
  1057. file = zip->file_list.first;
  1058. for (;file != NULL; file = file->next) {
  1059. if (file->flg & flg)
  1060. byte |= mask;
  1061. mask >>= 1;
  1062. if (mask == 0) {
  1063. r = compress_out(a, &byte, 1, ARCHIVE_Z_RUN);
  1064. if (r < 0)
  1065. return (r);
  1066. mask = 0x80;
  1067. byte = 0;
  1068. }
  1069. }
  1070. if (mask != 0x80) {
  1071. r = compress_out(a, &byte, 1, ARCHIVE_Z_RUN);
  1072. if (r < 0)
  1073. return (r);
  1074. }
  1075. }
  1076. /* External. */
  1077. r = enc_uint64(a, 0);
  1078. if (r < 0)
  1079. return (r);
  1080. /*
  1081. * Make Times.
  1082. */
  1083. file = zip->file_list.first;
  1084. for (;file != NULL; file = file->next) {
  1085. if ((file->flg & flg) == 0)
  1086. continue;
  1087. archive_le64enc(filetime, utcToFiletime(file->times[ti].time,
  1088. file->times[ti].time_ns));
  1089. r = compress_out(a, filetime, 8, ARCHIVE_Z_RUN);
  1090. if (r < 0)
  1091. return (r);
  1092. }
  1093. return (ARCHIVE_OK);
  1094. }
  1095. static int
  1096. make_header(struct archive_write *a, uint64_t offset, uint64_t pack_size,
  1097. uint64_t unpack_size, int codernum, struct coder *coders)
  1098. {
  1099. struct _7zip *zip = (struct _7zip *)a->format_data;
  1100. struct file *file;
  1101. int r;
  1102. uint8_t mask, byte;
  1103. /*
  1104. * Make FilesInfo.
  1105. */
  1106. r = enc_uint64(a, kHeader);
  1107. if (r < 0)
  1108. return (r);
  1109. /*
  1110. * If there are empty files only, do not write MainStreamInfo.
  1111. */
  1112. if (zip->total_number_nonempty_entry) {
  1113. /*
  1114. * Make MainStreamInfo.
  1115. */
  1116. r = enc_uint64(a, kMainStreamsInfo);
  1117. if (r < 0)
  1118. return (r);
  1119. r = make_streamsInfo(a, offset, pack_size, unpack_size,
  1120. codernum, coders, 1, 0);
  1121. if (r < 0)
  1122. return (r);
  1123. }
  1124. /*
  1125. * Make FilesInfo.
  1126. */
  1127. r = enc_uint64(a, kFilesInfo);
  1128. if (r < 0)
  1129. return (r);
  1130. /* Write numFiles. */
  1131. r = enc_uint64(a, zip->total_number_entry);
  1132. if (r < 0)
  1133. return (r);
  1134. if (zip->total_number_empty_entry > 0) {
  1135. /* Make EmptyStream. */
  1136. r = enc_uint64(a, kEmptyStream);
  1137. if (r < 0)
  1138. return (r);
  1139. /* Write EmptyStream Size. */
  1140. r = enc_uint64(a, (zip->total_number_entry+7)>>3);
  1141. if (r < 0)
  1142. return (r);
  1143. byte = 0;
  1144. mask = 0x80;
  1145. file = zip->file_list.first;
  1146. for (;file != NULL; file = file->next) {
  1147. if (file->size == 0)
  1148. byte |= mask;
  1149. mask >>= 1;
  1150. if (mask == 0) {
  1151. r = compress_out(a, &byte, 1, ARCHIVE_Z_RUN);
  1152. if (r < 0)
  1153. return (r);
  1154. mask = 0x80;
  1155. byte = 0;
  1156. }
  1157. }
  1158. if (mask != 0x80) {
  1159. r = compress_out(a, &byte, 1, ARCHIVE_Z_RUN);
  1160. if (r < 0)
  1161. return (r);
  1162. }
  1163. }
  1164. if (zip->total_number_empty_entry > zip->total_number_dir_entry) {
  1165. /* Make EmptyFile. */
  1166. r = enc_uint64(a, kEmptyFile);
  1167. if (r < 0)
  1168. return (r);
  1169. /* Write EmptyFile Size. */
  1170. r = enc_uint64(a, (zip->total_number_empty_entry + 7) >> 3);
  1171. if (r < 0)
  1172. return (r);
  1173. byte = 0;
  1174. mask = 0x80;
  1175. file = zip->file_list.first;
  1176. for (;file != NULL; file = file->next) {
  1177. if (file->size)
  1178. continue;
  1179. if (!file->dir)
  1180. byte |= mask;
  1181. mask >>= 1;
  1182. if (mask == 0) {
  1183. r = compress_out(a, &byte, 1, ARCHIVE_Z_RUN);
  1184. if (r < 0)
  1185. return (r);
  1186. mask = 0x80;
  1187. byte = 0;
  1188. }
  1189. }
  1190. if (mask != 0x80) {
  1191. r = compress_out(a, &byte, 1, ARCHIVE_Z_RUN);
  1192. if (r < 0)
  1193. return (r);
  1194. }
  1195. }
  1196. /* Make Name. */
  1197. r = enc_uint64(a, kName);
  1198. if (r < 0)
  1199. return (r);
  1200. /* Write Nume size. */
  1201. r = enc_uint64(a, zip->total_bytes_entry_name+1);
  1202. if (r < 0)
  1203. return (r);
  1204. /* Write dmy byte. */
  1205. r = enc_uint64(a, 0);
  1206. if (r < 0)
  1207. return (r);
  1208. file = zip->file_list.first;
  1209. for (;file != NULL; file = file->next) {
  1210. r = compress_out(a, file->utf16name, file->name_len+2,
  1211. ARCHIVE_Z_RUN);
  1212. if (r < 0)
  1213. return (r);
  1214. }
  1215. /* Make MTime. */
  1216. r = make_time(a, kMTime, MTIME_IS_SET, MTIME);
  1217. if (r < 0)
  1218. return (r);
  1219. /* Make CTime. */
  1220. r = make_time(a, kCTime, CTIME_IS_SET, CTIME);
  1221. if (r < 0)
  1222. return (r);
  1223. /* Make ATime. */
  1224. r = make_time(a, kATime, ATIME_IS_SET, ATIME);
  1225. if (r < 0)
  1226. return (r);
  1227. /* Make Attributes. */
  1228. r = enc_uint64(a, kAttributes);
  1229. if (r < 0)
  1230. return (r);
  1231. /* Write Attributes size. */
  1232. r = enc_uint64(a, 2 + zip->total_number_entry * 4);
  1233. if (r < 0)
  1234. return (r);
  1235. /* Write "All Are Defined". */
  1236. r = enc_uint64(a, 1);
  1237. if (r < 0)
  1238. return (r);
  1239. /* Write dmy byte. */
  1240. r = enc_uint64(a, 0);
  1241. if (r < 0)
  1242. return (r);
  1243. file = zip->file_list.first;
  1244. for (;file != NULL; file = file->next) {
  1245. /*
  1246. * High 16bits is unix mode.
  1247. * Low 16bits is Windows attributes.
  1248. */
  1249. uint32_t encattr, attr;
  1250. if (file->dir)
  1251. attr = 0x8010;
  1252. else
  1253. attr = 0x8020;
  1254. if ((file->mode & 0222) == 0)
  1255. attr |= 1;/* Read Only. */
  1256. attr |= ((uint32_t)file->mode) << 16;
  1257. archive_le32enc(&encattr, attr);
  1258. r = compress_out(a, &encattr, 4, ARCHIVE_Z_RUN);
  1259. if (r < 0)
  1260. return (r);
  1261. }
  1262. /* Write End. */
  1263. r = enc_uint64(a, kEnd);
  1264. if (r < 0)
  1265. return (r);
  1266. /* Write End. */
  1267. r = enc_uint64(a, kEnd);
  1268. if (r < 0)
  1269. return (r);
  1270. return (ARCHIVE_OK);
  1271. }
  1272. static int
  1273. _7z_free(struct archive_write *a)
  1274. {
  1275. struct _7zip *zip = (struct _7zip *)a->format_data;
  1276. file_free_register(zip);
  1277. compression_end(&(a->archive), &(zip->stream));
  1278. free(zip->coder.props);
  1279. free(zip);
  1280. return (ARCHIVE_OK);
  1281. }
  1282. static int
  1283. file_cmp_node(const struct archive_rb_node *n1,
  1284. const struct archive_rb_node *n2)
  1285. {
  1286. struct file *f1 = (struct file *)n1;
  1287. struct file *f2 = (struct file *)n2;
  1288. if (f1->name_len == f2->name_len)
  1289. return (memcmp(f1->utf16name, f2->utf16name, f1->name_len));
  1290. return (f1->name_len > f2->name_len)?1:-1;
  1291. }
  1292. static int
  1293. file_cmp_key(const struct archive_rb_node *n, const void *key)
  1294. {
  1295. struct file *f = (struct file *)n;
  1296. return (f->name_len - *(const char *)key);
  1297. }
  1298. static int
  1299. file_new(struct archive_write *a, struct archive_entry *entry,
  1300. struct file **newfile)
  1301. {
  1302. struct _7zip *zip;
  1303. struct file *file;
  1304. const char *u16;
  1305. size_t u16len;
  1306. int ret = ARCHIVE_OK;
  1307. zip = (struct _7zip *)a->format_data;
  1308. *newfile = NULL;
  1309. file = calloc(1, sizeof(*file));
  1310. if (file == NULL) {
  1311. archive_set_error(&a->archive, ENOMEM,
  1312. "Can't allocate memory");
  1313. return (ARCHIVE_FATAL);
  1314. }
  1315. if (0 > archive_entry_pathname_l(entry, &u16, &u16len, zip->sconv)) {
  1316. if (errno == ENOMEM) {
  1317. archive_set_error(&a->archive, ENOMEM,
  1318. "Can't allocate memory for UTF-16LE");
  1319. return (ARCHIVE_FATAL);
  1320. }
  1321. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  1322. "A filename cannot be converted to UTF-16LE;"
  1323. "You should disable making Joliet extension");
  1324. ret = ARCHIVE_WARN;
  1325. }
  1326. file->utf16name = malloc(u16len + 2);
  1327. if (file->utf16name == NULL) {
  1328. archive_set_error(&a->archive, ENOMEM,
  1329. "Can't allocate memory for Name");
  1330. return (ARCHIVE_FATAL);
  1331. }
  1332. memcpy(file->utf16name, u16, u16len);
  1333. file->utf16name[u16len+0] = 0;
  1334. file->utf16name[u16len+1] = 0;
  1335. file->name_len = u16len;
  1336. file->mode = archive_entry_mode(entry);
  1337. if (archive_entry_filetype(entry) == AE_IFREG)
  1338. file->size = archive_entry_size(entry);
  1339. else
  1340. archive_entry_set_size(entry, 0);
  1341. if (archive_entry_filetype(entry) == AE_IFDIR)
  1342. file->dir = 1;
  1343. else if (archive_entry_filetype(entry) == AE_IFLNK)
  1344. file->size = strlen(archive_entry_symlink(entry));
  1345. if (archive_entry_mtime_is_set(entry)) {
  1346. file->flg |= MTIME_IS_SET;
  1347. file->times[MTIME].time = archive_entry_mtime(entry);
  1348. file->times[MTIME].time_ns = archive_entry_mtime_nsec(entry);
  1349. }
  1350. if (archive_entry_atime_is_set(entry)) {
  1351. file->flg |= ATIME_IS_SET;
  1352. file->times[ATIME].time = archive_entry_atime(entry);
  1353. file->times[ATIME].time_ns = archive_entry_atime_nsec(entry);
  1354. }
  1355. if (archive_entry_ctime_is_set(entry)) {
  1356. file->flg |= CTIME_IS_SET;
  1357. file->times[CTIME].time = archive_entry_ctime(entry);
  1358. file->times[CTIME].time_ns = archive_entry_ctime_nsec(entry);
  1359. }
  1360. *newfile = file;
  1361. return (ret);
  1362. }
  1363. static void
  1364. file_free(struct file *file)
  1365. {
  1366. free(file->utf16name);
  1367. free(file);
  1368. }
  1369. static void
  1370. file_register(struct _7zip *zip, struct file *file)
  1371. {
  1372. file->next = NULL;
  1373. *zip->file_list.last = file;
  1374. zip->file_list.last = &(file->next);
  1375. }
  1376. static void
  1377. file_init_register(struct _7zip *zip)
  1378. {
  1379. zip->file_list.first = NULL;
  1380. zip->file_list.last = &(zip->file_list.first);
  1381. }
  1382. static void
  1383. file_free_register(struct _7zip *zip)
  1384. {
  1385. struct file *file, *file_next;
  1386. file = zip->file_list.first;
  1387. while (file != NULL) {
  1388. file_next = file->next;
  1389. file_free(file);
  1390. file = file_next;
  1391. }
  1392. }
  1393. static void
  1394. file_register_empty(struct _7zip *zip, struct file *file)
  1395. {
  1396. file->next = NULL;
  1397. *zip->empty_list.last = file;
  1398. zip->empty_list.last = &(file->next);
  1399. }
  1400. static void
  1401. file_init_register_empty(struct _7zip *zip)
  1402. {
  1403. zip->empty_list.first = NULL;
  1404. zip->empty_list.last = &(zip->empty_list.first);
  1405. }
  1406. #if !defined(HAVE_BZLIB_H) || !defined(BZ_CONFIG_ERROR) || !defined(HAVE_LZMA_H)
  1407. static int
  1408. compression_unsupported_encoder(struct archive *a,
  1409. struct la_zstream *lastrm, const char *name)
  1410. {
  1411. archive_set_error(a, ARCHIVE_ERRNO_MISC,
  1412. "%s compression not supported on this platform", name);
  1413. lastrm->valid = 0;
  1414. lastrm->real_stream = NULL;
  1415. return (ARCHIVE_FAILED);
  1416. }
  1417. #endif
  1418. /*
  1419. * _7_COPY compressor.
  1420. */
  1421. static int
  1422. compression_init_encoder_copy(struct archive *a, struct la_zstream *lastrm)
  1423. {
  1424. if (lastrm->valid)
  1425. compression_end(a, lastrm);
  1426. lastrm->valid = 1;
  1427. lastrm->code = compression_code_copy;
  1428. lastrm->end = compression_end_copy;
  1429. return (ARCHIVE_OK);
  1430. }
  1431. static int
  1432. compression_code_copy(struct archive *a,
  1433. struct la_zstream *lastrm, enum la_zaction action)
  1434. {
  1435. size_t bytes;
  1436. (void)a; /* UNUSED */
  1437. if (lastrm->avail_out > lastrm->avail_in)
  1438. bytes = lastrm->avail_in;
  1439. else
  1440. bytes = lastrm->avail_out;
  1441. if (bytes) {
  1442. memcpy(lastrm->next_out, lastrm->next_in, bytes);
  1443. lastrm->next_in += bytes;
  1444. lastrm->avail_in -= bytes;
  1445. lastrm->total_in += bytes;
  1446. lastrm->next_out += bytes;
  1447. lastrm->avail_out -= bytes;
  1448. lastrm->total_out += bytes;
  1449. }
  1450. if (action == ARCHIVE_Z_FINISH && lastrm->avail_in == 0)
  1451. return (ARCHIVE_EOF);
  1452. return (ARCHIVE_OK);
  1453. }
  1454. static int
  1455. compression_end_copy(struct archive *a, struct la_zstream *lastrm)
  1456. {
  1457. (void)a; /* UNUSED */
  1458. lastrm->valid = 0;
  1459. return (ARCHIVE_OK);
  1460. }
  1461. /*
  1462. * _7_DEFLATE compressor.
  1463. */
  1464. #ifdef HAVE_ZLIB_H
  1465. static int
  1466. compression_init_encoder_deflate(struct archive *a,
  1467. struct la_zstream *lastrm, int level, int withheader)
  1468. {
  1469. z_stream *strm;
  1470. if (lastrm->valid)
  1471. compression_end(a, lastrm);
  1472. strm = calloc(1, sizeof(*strm));
  1473. if (strm == NULL) {
  1474. archive_set_error(a, ENOMEM,
  1475. "Can't allocate memory for gzip stream");
  1476. return (ARCHIVE_FATAL);
  1477. }
  1478. /* zlib.h is not const-correct, so we need this one bit
  1479. * of ugly hackery to convert a const * pointer to
  1480. * a non-const pointer. */
  1481. strm->next_in = (Bytef *)(uintptr_t)(const void *)lastrm->next_in;
  1482. strm->avail_in = lastrm->avail_in;
  1483. strm->total_in = lastrm->total_in;
  1484. strm->next_out = lastrm->next_out;
  1485. strm->avail_out = lastrm->avail_out;
  1486. strm->total_out = lastrm->total_out;
  1487. if (deflateInit2(strm, level, Z_DEFLATED,
  1488. (withheader)?15:-15,
  1489. 8, Z_DEFAULT_STRATEGY) != Z_OK) {
  1490. free(strm);
  1491. lastrm->real_stream = NULL;
  1492. archive_set_error(a, ARCHIVE_ERRNO_MISC,
  1493. "Internal error initializing compression library");
  1494. return (ARCHIVE_FATAL);
  1495. }
  1496. lastrm->real_stream = strm;
  1497. lastrm->valid = 1;
  1498. lastrm->code = compression_code_deflate;
  1499. lastrm->end = compression_end_deflate;
  1500. return (ARCHIVE_OK);
  1501. }
  1502. static int
  1503. compression_code_deflate(struct archive *a,
  1504. struct la_zstream *lastrm, enum la_zaction action)
  1505. {
  1506. z_stream *strm;
  1507. int r;
  1508. strm = (z_stream *)lastrm->real_stream;
  1509. /* zlib.h is not const-correct, so we need this one bit
  1510. * of ugly hackery to convert a const * pointer to
  1511. * a non-const pointer. */
  1512. strm->next_in = (Bytef *)(uintptr_t)(const void *)lastrm->next_in;
  1513. strm->avail_in = lastrm->avail_in;
  1514. strm->total_in = lastrm->total_in;
  1515. strm->next_out = lastrm->next_out;
  1516. strm->avail_out = lastrm->avail_out;
  1517. strm->total_out = lastrm->total_out;
  1518. r = deflate(strm,
  1519. (action == ARCHIVE_Z_FINISH)? Z_FINISH: Z_NO_FLUSH);
  1520. lastrm->next_in = strm->next_in;
  1521. lastrm->avail_in = strm->avail_in;
  1522. lastrm->total_in = strm->total_in;
  1523. lastrm->next_out = strm->next_out;
  1524. lastrm->avail_out = strm->avail_out;
  1525. lastrm->total_out = strm->total_out;
  1526. switch (r) {
  1527. case Z_OK:
  1528. return (ARCHIVE_OK);
  1529. case Z_STREAM_END:
  1530. return (ARCHIVE_EOF);
  1531. default:
  1532. archive_set_error(a, ARCHIVE_ERRNO_MISC,
  1533. "GZip compression failed:"
  1534. " deflate() call returned status %d", r);
  1535. return (ARCHIVE_FATAL);
  1536. }
  1537. }
  1538. static int
  1539. compression_end_deflate(struct archive *a, struct la_zstream *lastrm)
  1540. {
  1541. z_stream *strm;
  1542. int r;
  1543. strm = (z_stream *)lastrm->real_stream;
  1544. r = deflateEnd(strm);
  1545. free(strm);
  1546. lastrm->real_stream = NULL;
  1547. lastrm->valid = 0;
  1548. if (r != Z_OK) {
  1549. archive_set_error(a, ARCHIVE_ERRNO_MISC,
  1550. "Failed to clean up compressor");
  1551. return (ARCHIVE_FATAL);
  1552. }
  1553. return (ARCHIVE_OK);
  1554. }
  1555. #else
  1556. static int
  1557. compression_init_encoder_deflate(struct archive *a,
  1558. struct la_zstream *lastrm, int level, int withheader)
  1559. {
  1560. (void) level; /* UNUSED */
  1561. (void) withheader; /* UNUSED */
  1562. if (lastrm->valid)
  1563. compression_end(a, lastrm);
  1564. return (compression_unsupported_encoder(a, lastrm, "deflate"));
  1565. }
  1566. #endif
  1567. /*
  1568. * _7_BZIP2 compressor.
  1569. */
  1570. #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
  1571. static int
  1572. compression_init_encoder_bzip2(struct archive *a,
  1573. struct la_zstream *lastrm, int level)
  1574. {
  1575. bz_stream *strm;
  1576. if (lastrm->valid)
  1577. compression_end(a, lastrm);
  1578. strm = calloc(1, sizeof(*strm));
  1579. if (strm == NULL) {
  1580. archive_set_error(a, ENOMEM,
  1581. "Can't allocate memory for bzip2 stream");
  1582. return (ARCHIVE_FATAL);
  1583. }
  1584. /* bzlib.h is not const-correct, so we need this one bit
  1585. * of ugly hackery to convert a const * pointer to
  1586. * a non-const pointer. */
  1587. strm->next_in = (char *)(uintptr_t)(const void *)lastrm->next_in;
  1588. strm->avail_in = lastrm->avail_in;
  1589. strm->total_in_lo32 = (uint32_t)(lastrm->total_in & 0xffffffff);
  1590. strm->total_in_hi32 = (uint32_t)(lastrm->total_in >> 32);
  1591. strm->next_out = (char *)lastrm->next_out;
  1592. strm->avail_out = lastrm->avail_out;
  1593. strm->total_out_lo32 = (uint32_t)(lastrm->total_out & 0xffffffff);
  1594. strm->total_out_hi32 = (uint32_t)(lastrm->total_out >> 32);
  1595. if (BZ2_bzCompressInit(strm, level, 0, 30) != BZ_OK) {
  1596. free(strm);
  1597. lastrm->real_stream = NULL;
  1598. archive_set_error(a, ARCHIVE_ERRNO_MISC,
  1599. "Internal error initializing compression library");
  1600. return (ARCHIVE_FATAL);
  1601. }
  1602. lastrm->real_stream = strm;
  1603. lastrm->valid = 1;
  1604. lastrm->code = compression_code_bzip2;
  1605. lastrm->end = compression_end_bzip2;
  1606. return (ARCHIVE_OK);
  1607. }
  1608. static int
  1609. compression_code_bzip2(struct archive *a,
  1610. struct la_zstream *lastrm, enum la_zaction action)
  1611. {
  1612. bz_stream *strm;
  1613. int r;
  1614. strm = (bz_stream *)lastrm->real_stream;
  1615. /* bzlib.h is not const-correct, so we need this one bit
  1616. * of ugly hackery to convert a const * pointer to
  1617. * a non-const pointer. */
  1618. strm->next_in = (char *)(uintptr_t)(const void *)lastrm->next_in;
  1619. strm->avail_in = lastrm->avail_in;
  1620. strm->total_in_lo32 = (uint32_t)(lastrm->total_in & 0xffffffff);
  1621. strm->total_in_hi32 = (uint32_t)(lastrm->total_in >> 32);
  1622. strm->next_out = (char *)lastrm->next_out;
  1623. strm->avail_out = lastrm->avail_out;
  1624. strm->total_out_lo32 = (uint32_t)(lastrm->total_out & 0xffffffff);
  1625. strm->total_out_hi32 = (uint32_t)(lastrm->total_out >> 32);
  1626. r = BZ2_bzCompress(strm,
  1627. (action == ARCHIVE_Z_FINISH)? BZ_FINISH: BZ_RUN);
  1628. lastrm->next_in = (const unsigned char *)strm->next_in;
  1629. lastrm->avail_in = strm->avail_in;
  1630. lastrm->total_in =
  1631. (((uint64_t)(uint32_t)strm->total_in_hi32) << 32)
  1632. + (uint64_t)(uint32_t)strm->total_in_lo32;
  1633. lastrm->next_out = (unsigned char *)strm->next_out;
  1634. lastrm->avail_out = strm->avail_out;
  1635. lastrm->total_out =
  1636. (((uint64_t)(uint32_t)strm->total_out_hi32) << 32)
  1637. + (uint64_t)(uint32_t)strm->total_out_lo32;
  1638. switch (r) {
  1639. case BZ_RUN_OK: /* Non-finishing */
  1640. case BZ_FINISH_OK: /* Finishing: There's more work to do */
  1641. return (ARCHIVE_OK);
  1642. case BZ_STREAM_END: /* Finishing: all done */
  1643. /* Only occurs in finishing case */
  1644. return (ARCHIVE_EOF);
  1645. default:
  1646. /* Any other return value indicates an error */
  1647. archive_set_error(a, ARCHIVE_ERRNO_MISC,
  1648. "Bzip2 compression failed:"
  1649. " BZ2_bzCompress() call returned status %d", r);
  1650. return (ARCHIVE_FATAL);
  1651. }
  1652. }
  1653. static int
  1654. compression_end_bzip2(struct archive *a, struct la_zstream *lastrm)
  1655. {
  1656. bz_stream *strm;
  1657. int r;
  1658. strm = (bz_stream *)lastrm->real_stream;
  1659. r = BZ2_bzCompressEnd(strm);
  1660. free(strm);
  1661. lastrm->real_stream = NULL;
  1662. lastrm->valid = 0;
  1663. if (r != BZ_OK) {
  1664. archive_set_error(a, ARCHIVE_ERRNO_MISC,
  1665. "Failed to clean up compressor");
  1666. return (ARCHIVE_FATAL);
  1667. }
  1668. return (ARCHIVE_OK);
  1669. }
  1670. #else
  1671. static int
  1672. compression_init_encoder_bzip2(struct archive *a,
  1673. struct la_zstream *lastrm, int level)
  1674. {
  1675. (void) level; /* UNUSED */
  1676. if (lastrm->valid)
  1677. compression_end(a, lastrm);
  1678. return (compression_unsupported_encoder(a, lastrm, "bzip2"));
  1679. }
  1680. #endif
  1681. /*
  1682. * _7_LZMA1, _7_LZMA2 compressor.
  1683. */
  1684. #if defined(HAVE_LZMA_H)
  1685. static int
  1686. compression_init_encoder_lzma(struct archive *a,
  1687. struct la_zstream *lastrm, int level, uint64_t filter_id)
  1688. {
  1689. static const lzma_stream lzma_init_data = LZMA_STREAM_INIT;
  1690. lzma_stream *strm;
  1691. lzma_filter *lzmafilters;
  1692. lzma_options_lzma lzma_opt;
  1693. int r;
  1694. if (lastrm->valid)
  1695. compression_end(a, lastrm);
  1696. strm = calloc(1, sizeof(*strm) + sizeof(*lzmafilters) * 2);
  1697. if (strm == NULL) {
  1698. archive_set_error(a, ENOMEM,
  1699. "Can't allocate memory for lzma stream");
  1700. return (ARCHIVE_FATAL);
  1701. }
  1702. lzmafilters = (lzma_filter *)(strm+1);
  1703. if (level > 6)
  1704. level = 6;
  1705. if (lzma_lzma_preset(&lzma_opt, level)) {
  1706. lastrm->real_stream = NULL;
  1707. archive_set_error(a, ENOMEM,
  1708. "Internal error initializing compression library");
  1709. return (ARCHIVE_FATAL);
  1710. }
  1711. lzmafilters[0].id = filter_id;
  1712. lzmafilters[0].options = &lzma_opt;
  1713. lzmafilters[1].id = LZMA_VLI_UNKNOWN;/* Terminate */
  1714. r = lzma_properties_size(&(lastrm->prop_size), lzmafilters);
  1715. if (r != LZMA_OK) {
  1716. free(strm);
  1717. lastrm->real_stream = NULL;
  1718. archive_set_error(a, ARCHIVE_ERRNO_MISC,
  1719. "lzma_properties_size failed");
  1720. return (ARCHIVE_FATAL);
  1721. }
  1722. if (lastrm->prop_size) {
  1723. lastrm->props = malloc(lastrm->prop_size);
  1724. if (lastrm->props == NULL) {
  1725. free(strm);
  1726. lastrm->real_stream = NULL;
  1727. archive_set_error(a, ENOMEM,
  1728. "Cannot allocate memory");
  1729. return (ARCHIVE_FATAL);
  1730. }
  1731. r = lzma_properties_encode(lzmafilters, lastrm->props);
  1732. if (r != LZMA_OK) {
  1733. free(strm);
  1734. lastrm->real_stream = NULL;
  1735. archive_set_error(a, ARCHIVE_ERRNO_MISC,
  1736. "lzma_properties_encode failed");
  1737. return (ARCHIVE_FATAL);
  1738. }
  1739. }
  1740. *strm = lzma_init_data;
  1741. r = lzma_raw_encoder(strm, lzmafilters);
  1742. switch (r) {
  1743. case LZMA_OK:
  1744. lastrm->real_stream = strm;
  1745. lastrm->valid = 1;
  1746. lastrm->code = compression_code_lzma;
  1747. lastrm->end = compression_end_lzma;
  1748. r = ARCHIVE_OK;
  1749. break;
  1750. case LZMA_MEM_ERROR:
  1751. free(strm);
  1752. lastrm->real_stream = NULL;
  1753. archive_set_error(a, ENOMEM,
  1754. "Internal error initializing compression library: "
  1755. "Cannot allocate memory");
  1756. r = ARCHIVE_FATAL;
  1757. break;
  1758. default:
  1759. free(strm);
  1760. lastrm->real_stream = NULL;
  1761. archive_set_error(a, ARCHIVE_ERRNO_MISC,
  1762. "Internal error initializing compression library: "
  1763. "It's a bug in liblzma");
  1764. r = ARCHIVE_FATAL;
  1765. break;
  1766. }
  1767. return (r);
  1768. }
  1769. static int
  1770. compression_init_encoder_lzma1(struct archive *a,
  1771. struct la_zstream *lastrm, int level)
  1772. {
  1773. return compression_init_encoder_lzma(a, lastrm, level,
  1774. LZMA_FILTER_LZMA1);
  1775. }
  1776. static int
  1777. compression_init_encoder_lzma2(struct archive *a,
  1778. struct la_zstream *lastrm, int level)
  1779. {
  1780. return compression_init_encoder_lzma(a, lastrm, level,
  1781. LZMA_FILTER_LZMA2);
  1782. }
  1783. static int
  1784. compression_code_lzma(struct archive *a,
  1785. struct la_zstream *lastrm, enum la_zaction action)
  1786. {
  1787. lzma_stream *strm;
  1788. int r;
  1789. strm = (lzma_stream *)lastrm->real_stream;
  1790. strm->next_in = lastrm->next_in;
  1791. strm->avail_in = lastrm->avail_in;
  1792. strm->total_in = lastrm->total_in;
  1793. strm->next_out = lastrm->next_out;
  1794. strm->avail_out = lastrm->avail_out;
  1795. strm->total_out = lastrm->total_out;
  1796. r = lzma_code(strm,
  1797. (action == ARCHIVE_Z_FINISH)? LZMA_FINISH: LZMA_RUN);
  1798. lastrm->next_in = strm->next_in;
  1799. lastrm->avail_in = strm->avail_in;
  1800. lastrm->total_in = strm->total_in;
  1801. lastrm->next_out = strm->next_out;
  1802. lastrm->avail_out = strm->avail_out;
  1803. lastrm->total_out = strm->total_out;
  1804. switch (r) {
  1805. case LZMA_OK:
  1806. /* Non-finishing case */
  1807. return (ARCHIVE_OK);
  1808. case LZMA_STREAM_END:
  1809. /* This return can only occur in finishing case. */
  1810. return (ARCHIVE_EOF);
  1811. case LZMA_MEMLIMIT_ERROR:
  1812. archive_set_error(a, ENOMEM,
  1813. "lzma compression error:"
  1814. " %ju MiB would have been needed",
  1815. (uintmax_t)((lzma_memusage(strm) + 1024 * 1024 -1)
  1816. / (1024 * 1024)));
  1817. return (ARCHIVE_FATAL);
  1818. default:
  1819. /* Any other return value indicates an error */
  1820. archive_set_error(a, ARCHIVE_ERRNO_MISC,
  1821. "lzma compression failed:"
  1822. " lzma_code() call returned status %d", r);
  1823. return (ARCHIVE_FATAL);
  1824. }
  1825. }
  1826. static int
  1827. compression_end_lzma(struct archive *a, struct la_zstream *lastrm)
  1828. {
  1829. lzma_stream *strm;
  1830. (void)a; /* UNUSED */
  1831. strm = (lzma_stream *)lastrm->real_stream;
  1832. lzma_end(strm);
  1833. free(strm);
  1834. lastrm->valid = 0;
  1835. lastrm->real_stream = NULL;
  1836. return (ARCHIVE_OK);
  1837. }
  1838. #else
  1839. static int
  1840. compression_init_encoder_lzma1(struct archive *a,
  1841. struct la_zstream *lastrm, int level)
  1842. {
  1843. (void) level; /* UNUSED */
  1844. if (lastrm->valid)
  1845. compression_end(a, lastrm);
  1846. return (compression_unsupported_encoder(a, lastrm, "lzma"));
  1847. }
  1848. static int
  1849. compression_init_encoder_lzma2(struct archive *a,
  1850. struct la_zstream *lastrm, int level)
  1851. {
  1852. (void) level; /* UNUSED */
  1853. if (lastrm->valid)
  1854. compression_end(a, lastrm);
  1855. return (compression_unsupported_encoder(a, lastrm, "lzma"));
  1856. }
  1857. #endif
  1858. /*
  1859. * _7_PPMD compressor.
  1860. */
  1861. static void *
  1862. ppmd_alloc(void *p, size_t size)
  1863. {
  1864. (void)p;
  1865. return malloc(size);
  1866. }
  1867. static void
  1868. ppmd_free(void *p, void *address)
  1869. {
  1870. (void)p;
  1871. free(address);
  1872. }
  1873. static ISzAlloc g_szalloc = { ppmd_alloc, ppmd_free };
  1874. static void
  1875. ppmd_write(void *p, Byte b)
  1876. {
  1877. struct archive_write *a = ((IByteOut *)p)->a;
  1878. struct _7zip *zip = (struct _7zip *)(a->format_data);
  1879. struct la_zstream *lastrm = &(zip->stream);
  1880. struct ppmd_stream *strm;
  1881. if (lastrm->avail_out) {
  1882. *lastrm->next_out++ = b;
  1883. lastrm->avail_out--;
  1884. lastrm->total_out++;
  1885. return;
  1886. }
  1887. strm = (struct ppmd_stream *)lastrm->real_stream;
  1888. if (strm->buff_ptr < strm->buff_end) {
  1889. *strm->buff_ptr++ = b;
  1890. strm->buff_bytes++;
  1891. }
  1892. }
  1893. static int
  1894. compression_init_encoder_ppmd(struct archive *a,
  1895. struct la_zstream *lastrm, unsigned maxOrder, uint32_t msize)
  1896. {
  1897. struct ppmd_stream *strm;
  1898. uint8_t *props;
  1899. int r;
  1900. if (lastrm->valid)
  1901. compression_end(a, lastrm);
  1902. strm = calloc(1, sizeof(*strm));
  1903. if (strm == NULL) {
  1904. archive_set_error(a, ENOMEM,
  1905. "Can't allocate memory for PPMd");
  1906. return (ARCHIVE_FATAL);
  1907. }
  1908. strm->buff = malloc(32);
  1909. if (strm->buff == NULL) {
  1910. free(strm);
  1911. archive_set_error(a, ENOMEM,
  1912. "Can't allocate memory for PPMd");
  1913. return (ARCHIVE_FATAL);
  1914. }
  1915. strm->buff_ptr = strm->buff;
  1916. strm->buff_end = strm->buff + 32;
  1917. props = malloc(1+4);
  1918. if (props == NULL) {
  1919. free(strm->buff);
  1920. free(strm);
  1921. archive_set_error(a, ENOMEM,
  1922. "Coludn't allocate memory for PPMd");
  1923. return (ARCHIVE_FATAL);
  1924. }
  1925. props[0] = maxOrder;
  1926. archive_le32enc(props+1, msize);
  1927. __archive_ppmd7_functions.Ppmd7_Construct(&strm->ppmd7_context);
  1928. r = __archive_ppmd7_functions.Ppmd7_Alloc(
  1929. &strm->ppmd7_context, msize, &g_szalloc);
  1930. if (r == 0) {
  1931. free(strm->buff);
  1932. free(strm);
  1933. free(props);
  1934. archive_set_error(a, ENOMEM,
  1935. "Coludn't allocate memory for PPMd");
  1936. return (ARCHIVE_FATAL);
  1937. }
  1938. __archive_ppmd7_functions.Ppmd7_Init(&(strm->ppmd7_context), maxOrder);
  1939. strm->byteout.a = (struct archive_write *)a;
  1940. strm->byteout.Write = ppmd_write;
  1941. strm->range_enc.Stream = &(strm->byteout);
  1942. __archive_ppmd7_functions.Ppmd7z_RangeEnc_Init(&(strm->range_enc));
  1943. strm->stat = 0;
  1944. lastrm->real_stream = strm;
  1945. lastrm->valid = 1;
  1946. lastrm->code = compression_code_ppmd;
  1947. lastrm->end = compression_end_ppmd;
  1948. lastrm->prop_size = 5;
  1949. lastrm->props = props;
  1950. return (ARCHIVE_OK);
  1951. }
  1952. static int
  1953. compression_code_ppmd(struct archive *a,
  1954. struct la_zstream *lastrm, enum la_zaction action)
  1955. {
  1956. struct ppmd_stream *strm;
  1957. strm = (struct ppmd_stream *)lastrm->real_stream;
  1958. /* Copy encoded data if there are remaining bytes from previous call. */
  1959. if (strm->buff_bytes) {
  1960. uint8_t *p = strm->buff_ptr - strm->buff_bytes;
  1961. while (lastrm->avail_out && strm->buff_bytes) {
  1962. *lastrm->next_out++ = *p++;
  1963. lastrm->avail_out--;
  1964. lastrm->total_out++;
  1965. strm->buff_bytes--;
  1966. }
  1967. if (strm->buff_bytes)
  1968. return (ARCHIVE_OK);
  1969. if (strm->stat == 1)
  1970. return (ARCHIVE_EOF);
  1971. strm->buff_ptr = strm->buff;
  1972. }
  1973. while (lastrm->avail_in && lastrm->avail_out) {
  1974. __archive_ppmd7_functions.Ppmd7_EncodeSymbol(
  1975. &(strm->ppmd7_context), &(strm->range_enc),
  1976. *lastrm->next_in++);
  1977. lastrm->avail_in--;
  1978. lastrm->total_in++;
  1979. }
  1980. if (lastrm->avail_in == 0 && action == ARCHIVE_Z_FINISH) {
  1981. __archive_ppmd7_functions.Ppmd7z_RangeEnc_FlushData(
  1982. &(strm->range_enc));
  1983. strm->stat = 1;
  1984. /* Return EOF if there are no remaining bytes. */
  1985. if (strm->buff_bytes == 0)
  1986. return (ARCHIVE_EOF);
  1987. }
  1988. return (ARCHIVE_OK);
  1989. }
  1990. static int
  1991. compression_end_ppmd(struct archive *a, struct la_zstream *lastrm)
  1992. {
  1993. struct ppmd_stream *strm;
  1994. strm = (struct ppmd_stream *)lastrm->real_stream;
  1995. __archive_ppmd7_functions.Ppmd7_Free(&strm->ppmd7_context, &g_szalloc);
  1996. free(strm->buff);
  1997. free(strm);
  1998. lastrm->real_stream = NULL;
  1999. lastrm->valid = 0;
  2000. return (ARCHIVE_OK);
  2001. }
  2002. /*
  2003. * Universal compressor initializer.
  2004. */
  2005. static int
  2006. _7z_compression_init_encoder(struct archive_write *a, unsigned compression,
  2007. int compression_level)
  2008. {
  2009. struct _7zip *zip;
  2010. int r;
  2011. zip = (struct _7zip *)a->format_data;
  2012. switch (compression) {
  2013. case _7Z_DEFLATE:
  2014. r = compression_init_encoder_deflate(
  2015. &(a->archive), &(zip->stream),
  2016. compression_level, 0);
  2017. break;
  2018. case _7Z_BZIP2:
  2019. r = compression_init_encoder_bzip2(
  2020. &(a->archive), &(zip->stream),
  2021. compression_level);
  2022. break;
  2023. case _7Z_LZMA1:
  2024. r = compression_init_encoder_lzma1(
  2025. &(a->archive), &(zip->stream),
  2026. compression_level);
  2027. break;
  2028. case _7Z_LZMA2:
  2029. r = compression_init_encoder_lzma2(
  2030. &(a->archive), &(zip->stream),
  2031. compression_level);
  2032. break;
  2033. case _7Z_PPMD:
  2034. r = compression_init_encoder_ppmd(
  2035. &(a->archive), &(zip->stream),
  2036. PPMD7_DEFAULT_ORDER, PPMD7_DEFAULT_MEM_SIZE);
  2037. break;
  2038. case _7Z_COPY:
  2039. default:
  2040. r = compression_init_encoder_copy(
  2041. &(a->archive), &(zip->stream));
  2042. break;
  2043. }
  2044. if (r == ARCHIVE_OK) {
  2045. zip->stream.total_in = 0;
  2046. zip->stream.next_out = zip->wbuff;
  2047. zip->stream.avail_out = sizeof(zip->wbuff);
  2048. zip->stream.total_out = 0;
  2049. }
  2050. return (r);
  2051. }
  2052. static int
  2053. compression_code(struct archive *a, struct la_zstream *lastrm,
  2054. enum la_zaction action)
  2055. {
  2056. if (lastrm->valid)
  2057. return (lastrm->code(a, lastrm, action));
  2058. return (ARCHIVE_OK);
  2059. }
  2060. static int
  2061. compression_end(struct archive *a, struct la_zstream *lastrm)
  2062. {
  2063. if (lastrm->valid) {
  2064. lastrm->prop_size = 0;
  2065. free(lastrm->props);
  2066. lastrm->props = NULL;
  2067. return (lastrm->end(a, lastrm));
  2068. }
  2069. return (ARCHIVE_OK);
  2070. }