archive_write_set_format_7zip.c 56 KB

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