archive_write_set_format_7zip.c 55 KB

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