zipup.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922
  1. /*
  2. zipup.c - Zip 3
  3. Copyright (c) 1990-2008 Info-ZIP. All rights reserved.
  4. See the accompanying file LICENSE, version 2007-Mar-4 or later
  5. (the contents of which are also included in zip.h) for terms of use.
  6. If, for some reason, all these files are missing, the Info-ZIP license
  7. also may be found at: ftp://ftp.info-zip.org/pub/infozip/license.html
  8. */
  9. /*
  10. * zipup.c by Mark Adler and Jean-loup Gailly.
  11. */
  12. #define __ZIPUP_C
  13. /* Found that for at least unix port zip.h has to be first or ctype.h will
  14. define off_t and when using 64-bit file environment off_t in other files
  15. is 8 bytes while off_t here is 4 bytes, and this makes the zlist struct
  16. different sizes and needless to say leads to segmentation faults. Putting
  17. zip.h first seems to fix this. 8/14/04 EG */
  18. #include "zip.h"
  19. #include <ctype.h>
  20. #include <errno.h>
  21. #ifndef UTIL /* This module contains no code for Zip Utilities */
  22. #include "revision.h"
  23. #include "crc32.h"
  24. #include "crypt.h"
  25. #ifdef USE_ZLIB
  26. # include "zlib.h"
  27. #endif
  28. #ifdef BZIP2_SUPPORT
  29. # ifdef BZIP2_USEBZIP2DIR
  30. # include "bzip2/bzlib.h"
  31. # else
  32. # include "bzlib.h"
  33. # endif
  34. #endif
  35. #ifdef OS2
  36. # include "os2/os2zip.h"
  37. #endif
  38. #if defined(MMAP)
  39. # include <sys/mman.h>
  40. # ifndef PAGESIZE /* used to be SYSV, what about pagesize on SVR3 ? */
  41. # define PAGESIZE getpagesize()
  42. # endif
  43. # if defined(NO_VALLOC) && !defined(valloc)
  44. # define valloc malloc
  45. # endif
  46. #endif
  47. /* Use the raw functions for MSDOS and Unix to save on buffer space.
  48. They're not used for VMS since it doesn't work (raw is weird on VMS).
  49. */
  50. #ifdef AMIGA
  51. # include "amiga/zipup.h"
  52. #endif /* AMIGA */
  53. #ifdef AOSVS
  54. # include "aosvs/zipup.h"
  55. #endif /* AOSVS */
  56. #ifdef ATARI
  57. # include "atari/zipup.h"
  58. #endif
  59. #ifdef __BEOS__
  60. # include "beos/zipup.h"
  61. #endif
  62. #ifdef __ATHEOS__
  63. # include "atheos/zipup.h"
  64. #endif /* __ATHEOS__ */
  65. #ifdef __human68k__
  66. # include "human68k/zipup.h"
  67. #endif /* __human68k__ */
  68. #ifdef MACOS
  69. # include "macos/zipup.h"
  70. #endif
  71. #ifdef DOS
  72. # include "msdos/zipup.h"
  73. #endif /* DOS */
  74. #ifdef NLM
  75. # include "novell/zipup.h"
  76. # include <nwfattr.h>
  77. #endif
  78. #ifdef OS2
  79. # include "os2/zipup.h"
  80. #endif /* OS2 */
  81. #ifdef RISCOS
  82. # include "acorn/zipup.h"
  83. #endif
  84. #ifdef TOPS20
  85. # include "tops20/zipup.h"
  86. #endif
  87. #ifdef UNIX
  88. # include "unix/zipup.h"
  89. #endif
  90. #ifdef CMS_MVS
  91. # include "zipup.h"
  92. #endif /* CMS_MVS */
  93. #ifdef TANDEM
  94. # include "zipup.h"
  95. #endif /* TANDEM */
  96. #ifdef VMS
  97. # include "vms/zipup.h"
  98. #endif /* VMS */
  99. #ifdef QDOS
  100. # include "qdos/zipup.h"
  101. #endif /* QDOS */
  102. #ifdef WIN32
  103. # include "win32/zipup.h"
  104. #endif
  105. #ifdef THEOS
  106. # include "theos/zipup.h"
  107. #endif
  108. /* Local functions */
  109. #ifndef RISCOS
  110. local int suffixes OF((char *, char *));
  111. #else
  112. local int filetypes OF((char *, char *));
  113. #endif
  114. local unsigned file_read OF((char *buf, unsigned size));
  115. #ifdef USE_ZLIB
  116. local int zl_deflate_init OF((int pack_level));
  117. #else /* !USE_ZLIB */
  118. # ifdef ZP_NEED_MEMCOMPR
  119. local unsigned mem_read OF((char *buf, unsigned size));
  120. # endif
  121. #endif /* ?USE_ZLIB */
  122. /* zip64 support 08/29/2003 R.Nausedat */
  123. local zoff_t filecompress OF((struct zlist far *z_entry, int *cmpr_method));
  124. #ifdef BZIP2_SUPPORT
  125. local zoff_t bzfilecompress OF((struct zlist far *z_entry, int *cmpr_method));
  126. #endif
  127. /* Deflate "internal" global data (currently not in zip.h) */
  128. #if defined(MMAP) || defined(BIG_MEM)
  129. # ifdef USE_ZLIB
  130. local uch *window = NULL; /* Used to read all input file at once */
  131. local ulg window_size; /* size of said window */
  132. # else /* !USE_ZLIB */
  133. extern uch *window; /* Used to read all input file at once */
  134. #endif /* ?USE_ZLIB */
  135. #endif /* MMAP || BIG_MEM */
  136. #ifndef USE_ZLIB
  137. extern ulg window_size; /* size of said window */
  138. unsigned (*read_buf) OF((char *buf, unsigned size)) = file_read;
  139. /* Current input function. Set to mem_read for in-memory compression */
  140. #endif /* !USE_ZLIB */
  141. /* Local data */
  142. local ulg crc; /* crc on uncompressed file data */
  143. local ftype ifile; /* file to compress */
  144. #if defined(MMAP) || defined(BIG_MEM)
  145. local ulg remain;
  146. /* window bytes not yet processed.
  147. * special value "(ulg)-1L" reserved to signal normal reads.
  148. */
  149. #endif /* MMAP || BIG_MEM */
  150. #ifdef USE_ZLIB
  151. local int deflInit = FALSE; /* flag: zlib deflate is initialized */
  152. local z_stream zstrm; /* zlib's data interface structure */
  153. local char *f_ibuf = NULL;
  154. local char *f_obuf = NULL;
  155. #else /* !USE_ZLIB */
  156. local char file_outbuf[1024]; /* output buffer for compression to file */
  157. # ifdef ZP_NEED_MEMCOMPR
  158. local char *in_buf;
  159. /* Current input buffer, in_buf is used only for in-memory compression. */
  160. local unsigned in_offset;
  161. /* Current offset in input buffer. in_offset is used only for in-memory
  162. * compression. On 16 bit machines, the buffer is limited to 64K.
  163. */
  164. local unsigned in_size; /* size of current input buffer */
  165. # endif /* ZP_NEED_MEMCOMPR */
  166. #endif /* ?USE_ZLIB */
  167. #ifdef BZIP2_SUPPORT
  168. local int bzipInit; /* flag: bzip2lib is initialized */
  169. local bz_stream bstrm; /* zlib's data interface structure */
  170. # if !defined(USE_ZLIB)
  171. local char *f_ibuf = NULL;
  172. local char *f_obuf = NULL;
  173. # endif /* !USE_ZLIB */
  174. #endif /* BZIP2_SUPPORT */
  175. #ifdef DEBUG
  176. zoff_t isize; /* input file size. global only for debugging */
  177. #else /* !DEBUG */
  178. local zoff_t isize; /* input file size. global only for debugging */
  179. #endif /* ?DEBUG */
  180. /* If file_read detects binary it sets this flag - 12/16/04 EG */
  181. local int file_binary = 0; /* first buf */
  182. local int file_binary_final = 0; /* for bzip2 for entire file. assume text until find binary */
  183. /* moved check to function 3/14/05 EG */
  184. int is_seekable(y)
  185. FILE *y;
  186. {
  187. zoff_t pos;
  188. #ifdef BROKEN_FSEEK
  189. if (!fseekable(y)) {
  190. return 0;
  191. }
  192. #endif
  193. pos = zftello(y);
  194. if (zfseeko(y, pos, SEEK_SET)) {
  195. return 0;
  196. }
  197. return 1;
  198. }
  199. int percent(n, m)
  200. uzoff_t n;
  201. uzoff_t m; /* n is the original size, m is the new size */
  202. /* Return the percentage compression from n to m using only integer
  203. operations */
  204. {
  205. zoff_t p;
  206. #if 0
  207. if (n > 0xffffffL) /* If n >= 16M */
  208. { /* then divide n and m by 256 */
  209. n += 0x80; n >>= 8;
  210. m += 0x80; m >>= 8;
  211. }
  212. return n > m ? (int)(1 + (200 * (n - m)/n)) / 2 : 0;
  213. #endif
  214. /* 2004-12-01 SMS.
  215. * Changed to do big-n test only for small zoff_t.
  216. * Changed big-n arithmetic to accomodate apparently negative values
  217. * when a small zoff_t value exceeds 2G.
  218. * Increased the reduction divisor from 256 to 512 to avoid the sign bit
  219. * in a reduced intermediate, allowing signed arithmetic for the final
  220. * result (which is no longer artificially limited to non-negative
  221. * values).
  222. * Note that right shifts must be on unsigned values to avoid undesired
  223. * sign extension.
  224. */
  225. /* Handle n = 0 case and account for int maybe being 16-bit. 12/28/2004 EG
  226. */
  227. #define PC_MAX_SAFE 0x007fffffL /* 9 clear bits at high end. */
  228. #define PC_MAX_RND 0xffffff00L /* 8 clear bits at low end. */
  229. if (sizeof(uzoff_t) < 8) /* Don't fiddle with big zoff_t. */
  230. {
  231. if ((ulg)n > PC_MAX_SAFE) /* Reduce large values. (n > m) */
  232. {
  233. if ((ulg)n < PC_MAX_RND) /* Divide n by 512 with rounding, */
  234. n = ((ulg)n + 0x100) >> 9; /* if boost won't overflow. */
  235. else /* Otherwise, use max value. */
  236. n = PC_MAX_SAFE;
  237. if ((ulg)m < PC_MAX_RND) /* Divide m by 512 with rounding, */
  238. m = ((ulg)m + 0x100) >> 9; /* if boost won't overflow. */
  239. else /* Otherwise, use max value. */
  240. m = PC_MAX_SAFE;
  241. }
  242. }
  243. if (n != 0)
  244. p = ((200 * ((zoff_t)n - (zoff_t)m) / (zoff_t)n) + 1) / 2;
  245. else
  246. p = 0;
  247. return (int)p; /* Return (rounded) % reduction. */
  248. }
  249. #ifndef RISCOS
  250. local int suffixes(a, s)
  251. char *a; /* name to check suffix of */
  252. char *s; /* list of suffixes separated by : or ; */
  253. /* Return true if a ends in any of the suffixes in the list s. */
  254. {
  255. int m; /* true if suffix matches so far */
  256. char *p; /* pointer into special */
  257. char *q; /* pointer into name a */
  258. #ifdef QDOS
  259. short dlen = devlen(a);
  260. a = a + dlen;
  261. #endif
  262. m = 1;
  263. #ifdef VMS
  264. if( (q = strrchr(a,';')) != NULL ) /* Cut out VMS file version */
  265. --q;
  266. else
  267. q = a + strlen(a) - 1;
  268. #else /* !VMS */
  269. q = a + strlen(a) - 1;
  270. #endif /* ?VMS */
  271. for (p = s + strlen(s) - 1; p >= s; p--)
  272. if (*p == ':' || *p == ';')
  273. {
  274. if (m)
  275. return 1;
  276. else
  277. {
  278. m = 1;
  279. #ifdef VMS
  280. if( (q = strrchr(a,';')) != NULL ) /* Cut out VMS file version */
  281. --q;
  282. else
  283. q = a + strlen(a) - 1;
  284. #else /* !VMS */
  285. q = a + strlen(a) - 1;
  286. #endif /* ?VMS */
  287. }
  288. }
  289. else
  290. {
  291. m = m && q >= a && case_map(*p) == case_map(*q);
  292. q--;
  293. }
  294. return m;
  295. }
  296. #else /* RISCOS */
  297. local int filetypes(a, s)
  298. char *a; /* extra field of file to check filetype of */
  299. char *s; /* list of filetypes separated by : or ; */
  300. /* Return true if a is any of the filetypes in the list s. */
  301. {
  302. char *p; /* pointer into special */
  303. char typestr[4]; /* filetype hex string taken from a */
  304. if ((((unsigned*)a)[2] & 0xFFF00000) != 0xFFF00000) {
  305. /* The file is not filestamped, always try to compress it */
  306. return 0;
  307. }
  308. sprintf(typestr,"%.3X",(((unsigned*)a)[2] & 0x000FFF00) >> 8);
  309. for (p=s;p<=s+strlen(s)-3;p+=3) { /* p+=3 to skip 3 hex type */
  310. while (*p==':' || *p==';')
  311. p++;
  312. if (typestr[0] == toupper(p[0]) &&
  313. typestr[1] == toupper(p[1]) &&
  314. typestr[2] == toupper(p[2]))
  315. return 1;
  316. }
  317. return 0;
  318. }
  319. #endif /* ?RISCOS */
  320. /* Note: a zip "entry" includes a local header (which includes the file
  321. name), an encryption header if encrypting, the compressed data
  322. and possibly an extended local header. */
  323. int zipup(z)
  324. struct zlist far *z; /* zip entry to compress */
  325. /* Compress the file z->name into the zip entry described by *z and write
  326. it to the file *y. Encrypt if requested. Return an error code in the
  327. ZE_ class. Also, update tempzn by the number of bytes written. */
  328. /* y is now global */
  329. {
  330. iztimes f_utim; /* UNIX GMT timestamps, filled by filetime() */
  331. ulg tim; /* time returned by filetime() */
  332. ulg a = 0L; /* attributes returned by filetime() */
  333. char *b; /* malloc'ed file buffer */
  334. extent k = 0; /* result of zread */
  335. int l = 0; /* true if this file is a symbolic link */
  336. int m; /* method for this entry */
  337. zoff_t o = 0, p; /* offsets in zip file */
  338. zoff_t q = (zoff_t) -3; /* size returned by filetime */
  339. uzoff_t uq; /* unsigned q */
  340. zoff_t s = 0; /* size of compressed data */
  341. int r; /* temporary variable */
  342. int isdir; /* set for a directory name */
  343. int set_type = 0; /* set if file type (ascii/binary) unknown */
  344. zoff_t last_o; /* used to detect wrap around */
  345. ush tempext = 0; /* temp copies of extra fields */
  346. ush tempcext = 0;
  347. char *tempextra = NULL;
  348. char *tempcextra = NULL;
  349. #ifdef WINDLL
  350. # ifdef ZIP64_SUPPORT
  351. extern _int64 filesize64;
  352. extern unsigned long low;
  353. extern unsigned long high;
  354. # endif
  355. #endif
  356. z->nam = strlen(z->iname);
  357. isdir = z->iname[z->nam-1] == (char)0x2f; /* ascii[(unsigned)('/')] */
  358. file_binary = -1; /* not set, set after first read */
  359. file_binary_final = 0; /* not set, set after first read */
  360. #if defined(UNICODE_SUPPORT) && defined(WIN32)
  361. if (!no_win32_wide)
  362. tim = filetimew(z->namew, &a, &q, &f_utim);
  363. else
  364. tim = filetime(z->name, &a, &q, &f_utim);
  365. #else
  366. tim = filetime(z->name, &a, &q, &f_utim);
  367. #endif
  368. if (tim == 0 || q == (zoff_t) -3)
  369. return ZE_OPEN;
  370. /* q is set to -1 if the input file is a device, -2 for a volume label */
  371. if (q == (zoff_t) -2) {
  372. isdir = 1;
  373. q = 0;
  374. } else if (isdir != ((a & MSDOS_DIR_ATTR) != 0)) {
  375. /* don't overwrite a directory with a file and vice-versa */
  376. return ZE_MISS;
  377. }
  378. /* reset dot_count for each file */
  379. if (!display_globaldots)
  380. dot_count = -1;
  381. /* display uncompressed size */
  382. uq = ((uzoff_t) q > (uzoff_t) -3) ? 0 : (uzoff_t) q;
  383. if (noisy && display_usize) {
  384. fprintf(mesg, " (");
  385. DisplayNumString( mesg, uq );
  386. fprintf(mesg, ")");
  387. mesg_line_started = 1;
  388. fflush(mesg);
  389. }
  390. if (logall && display_usize) {
  391. fprintf(logfile, " (");
  392. DisplayNumString( logfile, uq );
  393. fprintf(logfile, ")");
  394. logfile_line_started = 1;
  395. fflush(logfile);
  396. }
  397. /* initial z->len so if error later have something */
  398. z->len = uq;
  399. z->att = (ush)UNKNOWN; /* will be changed later */
  400. z->atx = 0; /* may be changed by set_extra_field() */
  401. /* Free the old extra fields which are probably obsolete */
  402. /* Should probably read these and keep any we don't update. 12/30/04 EG */
  403. if (extra_fields == 2) {
  404. /* If keeping extra fields, make copy before clearing for set_extra_field()
  405. A better approach is to modify the port code, but maybe later */
  406. if (z->ext) {
  407. if ((tempextra = malloc(z->ext)) == NULL) {
  408. ZIPERR(ZE_MEM, "extra fields copy");
  409. }
  410. memcpy(tempextra, z->extra, z->ext);
  411. tempext = z->ext;
  412. }
  413. if (z->cext) {
  414. if ((tempcextra = malloc(z->cext)) == NULL) {
  415. ZIPERR(ZE_MEM, "extra fields copy");
  416. }
  417. memcpy(tempcextra, z->cextra, z->cext);
  418. tempcext = z->cext;
  419. }
  420. }
  421. if (z->ext) {
  422. free((zvoid *)(z->extra));
  423. }
  424. if (z->cext && z->extra != z->cextra) {
  425. free((zvoid *)(z->cextra));
  426. }
  427. z->extra = z->cextra = NULL;
  428. z->ext = z->cext = 0;
  429. #if defined(MMAP) || defined(BIG_MEM)
  430. remain = (ulg)-1L; /* changed only for MMAP or BIG_MEM */
  431. #endif /* MMAP || BIG_MEM */
  432. #if (!defined(USE_ZLIB) || defined(MMAP) || defined(BIG_MEM))
  433. window_size = 0L;
  434. #endif /* !USE_ZLIB || MMAP || BIG_MEM */
  435. /* Select method based on the suffix and the global method */
  436. #ifndef RISCOS
  437. m = special != NULL && suffixes(z->name, special) ? STORE : method;
  438. #else /* RISCOS must set m after setting extra field */
  439. m = method;
  440. #endif /* ?RISCOS */
  441. /* For now force deflate if using descriptors. Instead zip and unzip
  442. could check bytes read against compressed size in each data descriptor
  443. found and skip over any that don't match. This is how at least one
  444. other zipper does it. To be added later. Until then it
  445. probably doesn't hurt to force deflation when streaming. 12/30/04 EG
  446. */
  447. /* Now is a good time. For now allow storing for testing. 12/16/05 EG */
  448. /* By release need to force deflation based on reports some inflate
  449. streamed data to find the end of the data */
  450. /* Need to handle bzip2 */
  451. #ifdef NO_STREAMING_STORE
  452. if (use_descriptors && m == STORE)
  453. {
  454. m = DEFLATE;
  455. }
  456. #endif
  457. /* Open file to zip up unless it is stdin */
  458. if (strcmp(z->name, "-") == 0)
  459. {
  460. ifile = (ftype)zstdin;
  461. #if defined(MSDOS) || defined(__human68k__)
  462. if (isatty(zstdin) == 0) /* keep default mode if stdin is a terminal */
  463. setmode(zstdin, O_BINARY);
  464. #endif
  465. z->tim = tim;
  466. }
  467. else
  468. {
  469. #if !(defined(VMS) && defined(VMS_PK_EXTRA))
  470. if (extra_fields) {
  471. /* create extra field and change z->att and z->atx if desired */
  472. set_extra_field(z, &f_utim);
  473. # ifdef QLZIP
  474. if(qlflag)
  475. a |= (S_IXUSR) << 16; /* Cross compilers don't set this */
  476. # endif
  477. # ifdef RISCOS
  478. m = special != NULL && filetypes(z->extra, special) ? STORE : method;
  479. # endif /* RISCOS */
  480. /* For now allow store for testing */
  481. #ifdef NO_STREAMING_STORE
  482. /* For now force deflation if using data descriptors. */
  483. if (use_descriptors && m == STORE)
  484. {
  485. m = DEFLATE;
  486. }
  487. #endif
  488. }
  489. #endif /* !(VMS && VMS_PK_EXTRA) */
  490. l = issymlnk(a);
  491. if (l) {
  492. ifile = fbad;
  493. m = STORE;
  494. }
  495. else if (isdir) { /* directory */
  496. ifile = fbad;
  497. m = STORE;
  498. q = 0;
  499. }
  500. #ifdef THEOS
  501. else if (((a >> 16) & S_IFMT) == S_IFLIB) { /* library */
  502. ifile = fbad;
  503. m = STORE;
  504. q = 0;
  505. }
  506. #endif
  507. else {
  508. #ifdef CMS_MVS
  509. if (bflag) {
  510. if ((ifile = zopen(z->name, fhowb)) == fbad)
  511. return ZE_OPEN;
  512. }
  513. else
  514. #endif /* CMS_MVS */
  515. #if defined(UNICODE_SUPPORT) && defined(WIN32)
  516. if (!no_win32_wide) {
  517. if ((ifile = zwopen(z->namew, fhow)) == fbad)
  518. return ZE_OPEN;
  519. } else {
  520. if ((ifile = zopen(z->name, fhow)) == fbad)
  521. return ZE_OPEN;
  522. }
  523. #else
  524. if ((ifile = zopen(z->name, fhow)) == fbad)
  525. return ZE_OPEN;
  526. #endif
  527. }
  528. z->tim = tim;
  529. #if defined(VMS) && defined(VMS_PK_EXTRA)
  530. /* vms_get_attributes must be called after vms_open() */
  531. if (extra_fields) {
  532. /* create extra field and change z->att and z->atx if desired */
  533. vms_get_attributes(ifile, z, &f_utim);
  534. }
  535. #endif /* VMS && VMS_PK_EXTRA */
  536. #if defined(MMAP) || defined(BIG_MEM)
  537. /* Map ordinary files but not devices. This code should go in fileio.c */
  538. if (!translate_eol && m != STORE && q != -1L && (ulg)q > 0 &&
  539. (ulg)q + MIN_LOOKAHEAD > (ulg)q) {
  540. # ifdef MMAP
  541. /* Map the whole input file in memory */
  542. if (window != NULL)
  543. free(window); /* window can't be a mapped file here */
  544. window_size = (ulg)q + MIN_LOOKAHEAD;
  545. remain = window_size & (PAGESIZE-1);
  546. /* If we can't touch the page beyond the end of file, we must
  547. * allocate an extra page.
  548. */
  549. if (remain > MIN_LOOKAHEAD) {
  550. window = (uch*)mmap(0, window_size, PROT_READ, MAP_PRIVATE, ifile, 0);
  551. } else {
  552. window = (uch*)valloc(window_size - remain + PAGESIZE);
  553. if (window != NULL) {
  554. window = (uch*)mmap((char*)window, window_size - remain, PROT_READ,
  555. MAP_PRIVATE | MAP_FIXED, ifile, 0);
  556. } else {
  557. window = (uch*)(-1);
  558. }
  559. }
  560. if (window == (uch*)(-1)) {
  561. Trace((mesg, " mmap failure on %s\n", z->name));
  562. window = NULL;
  563. window_size = 0L;
  564. remain = (ulg)-1L;
  565. } else {
  566. remain = (ulg)q;
  567. }
  568. # else /* !MMAP, must be BIG_MEM */
  569. /* Read the whole input file at once */
  570. window_size = (ulg)q + MIN_LOOKAHEAD;
  571. window = window ? (uch*) realloc(window, (unsigned)window_size)
  572. : (uch*) malloc((unsigned)window_size);
  573. /* Just use normal code if big malloc or realloc fails: */
  574. if (window != NULL) {
  575. remain = (ulg)zread(ifile, (char*)window, q+1);
  576. if (remain != (ulg)q) {
  577. fprintf(mesg, " q=%lu, remain=%lu ", (ulg)q, remain);
  578. error("can't read whole file at once");
  579. }
  580. } else {
  581. window_size = 0L;
  582. }
  583. # endif /* ?MMAP */
  584. }
  585. #endif /* MMAP || BIG_MEM */
  586. } /* strcmp(z->name, "-") == 0 */
  587. if (extra_fields == 2) {
  588. unsigned len;
  589. char *p;
  590. /* step through old extra fields and copy over any not already
  591. in new extra fields */
  592. p = copy_nondup_extra_fields(tempextra, tempext, z->extra, z->ext, &len);
  593. free(z->extra);
  594. z->ext = len;
  595. z->extra = p;
  596. p = copy_nondup_extra_fields(tempcextra, tempcext, z->cextra, z->cext, &len);
  597. free(z->cextra);
  598. z->cext = len;
  599. z->cextra = p;
  600. if (tempext)
  601. free(tempextra);
  602. if (tempcext)
  603. free(tempcextra);
  604. }
  605. if (q == 0)
  606. m = STORE;
  607. if (m == BEST)
  608. m = DEFLATE;
  609. /* Do not create STORED files with extended local headers if the
  610. * input size is not known, because such files could not be extracted.
  611. * So if the zip file is not seekable and the input file is not
  612. * on disk, obey the -0 option by forcing deflation with stored block.
  613. * Note however that using "zip -0" as filter is not very useful...
  614. * ??? to be done.
  615. */
  616. /* An alternative used by others is to allow storing but on reading do
  617. * a second check when a signature is found. This is simply to check
  618. * the compressed size to the bytes read since the start of the file data.
  619. * If this is the right signature then the compressed size should match
  620. * the size of the compressed data to that point. If not look for the
  621. * next signature. We should do this. 12/31/04 EG
  622. *
  623. * For reading and testing we should do this, but should not write
  624. * stored streamed data unless for testing as finding the end of
  625. * streamed deflated data can be done by inflating. 6/26/06 EG
  626. */
  627. /* Fill in header information and write local header to zip file.
  628. * This header will later be re-written since compressed length and
  629. * crc are not yet known.
  630. */
  631. /* (Assume ext, cext, com, and zname already filled in.) */
  632. #if defined(OS2) || defined(WIN32)
  633. # ifdef WIN32_OEM
  634. /* When creating OEM-coded names on Win32, the entries must always be marked
  635. as "created on MSDOS" (OS_CODE = 0), because UnZip needs to handle archive
  636. entry names just like those created by Zip's MSDOS port.
  637. */
  638. z->vem = (ush)(dosify ? 20 : 0 + Z_MAJORVER * 10 + Z_MINORVER);
  639. # else
  640. z->vem = (ush)(z->dosflag ? (dosify ? 20 : /* Made under MSDOS by PKZIP 2.0 */
  641. (0 + Z_MAJORVER * 10 + Z_MINORVER))
  642. : OS_CODE + Z_MAJORVER * 10 + Z_MINORVER);
  643. /* For a plain old (8+3) FAT file system, we cheat and pretend that the file
  644. * was not made on OS2/WIN32 but under DOS. unzip is confused otherwise.
  645. */
  646. # endif
  647. #else /* !(OS2 || WIN32) */
  648. z->vem = (ush)(dosify ? 20 : OS_CODE + Z_MAJORVER * 10 + Z_MINORVER);
  649. #endif /* ?(OS2 || WIN32) */
  650. z->ver = (ush)(m == STORE ? 10 : 20); /* Need PKUNZIP 2.0 except for store */
  651. #ifdef BZIP2_SUPPORT
  652. if (method == BZIP2)
  653. z->ver = (ush)(m == STORE ? 10 : 46);
  654. #endif
  655. z->crc = 0; /* to be updated later */
  656. /* Assume first that we will need an extended local header: */
  657. if (isdir)
  658. /* If dir then q = 0 and extended header not needed */
  659. z->flg = 0;
  660. else
  661. z->flg = 8; /* to be updated later */
  662. #if CRYPT
  663. if (!isdir && key != NULL) {
  664. z->flg |= 1;
  665. /* Since we do not yet know the crc here, we pretend that the crc
  666. * is the modification time:
  667. */
  668. z->crc = z->tim << 16;
  669. /* More than pretend. File is encrypted using crypt header with that. */
  670. }
  671. #endif /* CRYPT */
  672. z->lflg = z->flg;
  673. z->how = (ush)m; /* may be changed later */
  674. z->siz = (zoff_t)(m == STORE && q >= 0 ? q : 0); /* will be changed later */
  675. z->len = (zoff_t)(q != -1L ? q : 0); /* may be changed later */
  676. if (z->att == (ush)UNKNOWN) {
  677. z->att = BINARY; /* set sensible value in header */
  678. set_type = 1;
  679. }
  680. /* Attributes from filetime(), flag bits from set_extra_field(): */
  681. #if defined(DOS) || defined(OS2) || defined(WIN32)
  682. z->atx = z->dosflag ? a & 0xff : a | (z->atx & 0x0000ff00);
  683. #else
  684. z->atx = dosify ? a & 0xff : a | (z->atx & 0x0000ff00);
  685. #endif /* DOS || OS2 || WIN32 */
  686. if ((r = putlocal(z, PUTLOCAL_WRITE)) != ZE_OK) {
  687. if (ifile != fbad)
  688. zclose(ifile);
  689. return r;
  690. }
  691. /* now get split information set by bfwrite() */
  692. z->off = current_local_offset;
  693. /* disk local header was written to */
  694. z->dsk = current_local_disk;
  695. tempzn += 4 + LOCHEAD + z->nam + z->ext;
  696. #if CRYPT
  697. if (!isdir && key != NULL) {
  698. crypthead(key, z->crc);
  699. z->siz += RAND_HEAD_LEN; /* to be updated later */
  700. tempzn += RAND_HEAD_LEN;
  701. }
  702. #endif /* CRYPT */
  703. if (ferror(y)) {
  704. if (ifile != fbad)
  705. zclose(ifile);
  706. ZIPERR(ZE_WRITE, "unexpected error on zip file");
  707. }
  708. last_o = o;
  709. o = zftello(y); /* for debugging only, ftell can fail on pipes */
  710. if (ferror(y))
  711. clearerr(y);
  712. if (o != -1 && last_o > o) {
  713. fprintf(mesg, "last %s o %s\n", zip_fzofft(last_o, NULL, NULL),
  714. zip_fzofft(o, NULL, NULL));
  715. ZIPERR(ZE_BIG, "seek wrap - zip file too big to write");
  716. }
  717. /* Write stored or deflated file to zip file */
  718. isize = 0L;
  719. crc = CRCVAL_INITIAL;
  720. if (isdir) {
  721. /* nothing to write */
  722. }
  723. else if (m != STORE) {
  724. if (set_type) z->att = (ush)UNKNOWN;
  725. /* ... is finally set in file compression routine */
  726. #ifdef BZIP2_SUPPORT
  727. if (m == BZIP2) {
  728. s = bzfilecompress(z, &m);
  729. }
  730. else
  731. #endif /* BZIP2_SUPPORT */
  732. {
  733. s = filecompress(z, &m);
  734. }
  735. #ifndef PGP
  736. if (z->att == (ush)BINARY && translate_eol && file_binary) {
  737. if (translate_eol == 1)
  738. zipwarn("has binary so -l ignored", "");
  739. else
  740. zipwarn("has binary so -ll ignored", "");
  741. }
  742. else if (z->att == (ush)BINARY && translate_eol) {
  743. if (translate_eol == 1)
  744. zipwarn("-l used on binary file - corrupted?", "");
  745. else
  746. zipwarn("-ll used on binary file - corrupted?", "");
  747. }
  748. #endif
  749. }
  750. else
  751. {
  752. if ((b = malloc(SBSZ)) == NULL)
  753. return ZE_MEM;
  754. if (l) {
  755. k = rdsymlnk(z->name, b, SBSZ);
  756. /*
  757. * compute crc first because zfwrite will alter the buffer b points to !!
  758. */
  759. crc = crc32(crc, (uch *) b, k);
  760. if (zfwrite(b, 1, k) != k)
  761. {
  762. free((zvoid *)b);
  763. return ZE_TEMP;
  764. }
  765. isize = k;
  766. #ifdef MINIX
  767. q = k;
  768. #endif /* MINIX */
  769. }
  770. else
  771. {
  772. while ((k = file_read(b, SBSZ)) > 0 && k != (extent) EOF)
  773. {
  774. if (zfwrite(b, 1, k) != k)
  775. {
  776. if (ifile != fbad)
  777. zclose(ifile);
  778. free((zvoid *)b);
  779. return ZE_TEMP;
  780. }
  781. if (!display_globaldots) {
  782. if (dot_size > 0) {
  783. /* initial space */
  784. if (noisy && dot_count == -1) {
  785. #ifndef WINDLL
  786. putc(' ', mesg);
  787. fflush(mesg);
  788. #else
  789. fprintf(stdout,"%c",' ');
  790. #endif
  791. dot_count++;
  792. }
  793. dot_count++;
  794. if (dot_size <= (dot_count + 1) * SBSZ) dot_count = 0;
  795. }
  796. if ((verbose || noisy) && dot_size && !dot_count) {
  797. #ifndef WINDLL
  798. putc('.', mesg);
  799. fflush(mesg);
  800. #else
  801. fprintf(stdout,"%c",'.');
  802. #endif
  803. mesg_line_started = 1;
  804. }
  805. }
  806. }
  807. }
  808. free((zvoid *)b);
  809. s = isize;
  810. }
  811. if (ifile != fbad && zerr(ifile)) {
  812. perror("\nzip warning");
  813. if (logfile)
  814. fprintf(logfile, "\nzip warning: %s\n", strerror(errno));
  815. zipwarn("could not read input file: ", z->oname);
  816. }
  817. if (ifile != fbad)
  818. zclose(ifile);
  819. #ifdef MMAP
  820. if (remain != (ulg)-1L) {
  821. munmap((caddr_t) window, window_size);
  822. window = NULL;
  823. }
  824. #endif /*MMAP */
  825. tempzn += s;
  826. p = tempzn; /* save for future fseek() */
  827. #if (!defined(MSDOS) || defined(OS2))
  828. #if !defined(VMS) && !defined(CMS_MVS) && !defined(__mpexl)
  829. /* Check input size (but not in VMS -- variable record lengths mess it up)
  830. * and not on MSDOS -- diet in TSR mode reports an incorrect file size)
  831. */
  832. #ifndef TANDEM /* Tandem EOF does not match byte count unless Unstructured */
  833. if (!translate_eol && q != -1L && isize != q)
  834. {
  835. Trace((mesg, " i=%lu, q=%lu ", isize, q));
  836. zipwarn(" file size changed while zipping ", z->name);
  837. }
  838. #endif /* !TANDEM */
  839. #endif /* !VMS && !CMS_MVS && !__mpexl */
  840. #endif /* (!MSDOS || OS2) */
  841. if (isdir)
  842. {
  843. /* A directory */
  844. z->siz = 0;
  845. z->len = 0;
  846. z->how = STORE;
  847. z->ver = 10;
  848. /* never encrypt directory so don't need extended local header */
  849. z->flg &= ~8;
  850. z->lflg &= ~8;
  851. }
  852. else
  853. {
  854. /* Try to rewrite the local header with correct information */
  855. z->crc = crc;
  856. z->siz = s;
  857. #if CRYPT
  858. if (!isdir && key != NULL)
  859. z->siz += RAND_HEAD_LEN;
  860. #endif /* CRYPT */
  861. z->len = isize;
  862. /* if can seek back to local header */
  863. #ifdef BROKEN_FSEEK
  864. if (use_descriptors || !fseekable(y) || zfseeko(y, z->off, SEEK_SET))
  865. #else
  866. if (use_descriptors || zfseeko(y, z->off, SEEK_SET))
  867. #endif
  868. {
  869. if (z->how != (ush) m)
  870. error("can't rewrite method");
  871. if (m == STORE && q < 0)
  872. ZIPERR(ZE_PARMS, "zip -0 not supported for I/O on pipes or devices");
  873. if ((r = putextended(z)) != ZE_OK)
  874. return r;
  875. /* if Zip64 and not seekable then Zip64 data descriptor */
  876. #ifdef ZIP64_SUPPORT
  877. tempzn += (zip64_entry ? 24L : 16L);
  878. #else
  879. tempzn += 16L;
  880. #endif
  881. z->flg = z->lflg; /* if z->flg modified by deflate */
  882. } else {
  883. /* ftell() not as useful across splits */
  884. if (bytes_this_entry != (uzoff_t)(key ? s + 12 : s)) {
  885. fprintf(mesg, " s=%s, actual=%s ",
  886. zip_fzofft(s, NULL, NULL), zip_fzofft(bytes_this_entry, NULL, NULL));
  887. error("incorrect compressed size");
  888. }
  889. #if 0
  890. /* seek ok, ftell() should work, check compressed size */
  891. # if !defined(VMS) && !defined(CMS_MVS)
  892. if (p - o != s) {
  893. fprintf(mesg, " s=%s, actual=%s ",
  894. zip_fzofft(s, NULL, NULL), zip_fzofft(p-o, NULL, NULL));
  895. error("incorrect compressed size");
  896. }
  897. # endif /* !VMS && !CMS_MVS */
  898. #endif /* 0 */
  899. z->how = (ush)m;
  900. switch (m)
  901. {
  902. case STORE:
  903. z->ver = 10; break;
  904. /* Need PKUNZIP 2.0 for DEFLATE */
  905. case DEFLATE:
  906. z->ver = 20; break;
  907. #ifdef BZIP2_SUPPORT
  908. case BZIP2:
  909. z->ver = 46; break;
  910. #endif
  911. }
  912. /*
  913. * The encryption header needs the crc, but we don't have it
  914. * for a new file. The file time is used instead and the encryption
  915. * header then used to encrypt the data. The AppNote standard only
  916. * can be applied to a file that the crc is known, so that means
  917. * either an existing entry in an archive or get the crc before
  918. * creating the encryption header and then encrypt the data.
  919. */
  920. if ((z->flg & 1) == 0) {
  921. /* not encrypting so don't need extended local header */
  922. z->flg &= ~8;
  923. }
  924. /* deflate may have set compression level bit markers in z->flg,
  925. and we can't think of any reason central and local flags should
  926. be different. */
  927. z->lflg = z->flg;
  928. /* If not using descriptors, back up and rewrite local header. */
  929. if (split_method == 1 && current_local_file != y) {
  930. if (zfseeko(current_local_file, z->off, SEEK_SET))
  931. return ZE_READ;
  932. }
  933. /* if local header in another split, putlocal will close it */
  934. if ((r = putlocal(z, PUTLOCAL_REWRITE)) != ZE_OK)
  935. return r;
  936. if (zfseeko(y, bytes_this_split, SEEK_SET))
  937. return ZE_READ;
  938. if ((z->flg & 1) != 0) {
  939. /* encrypted file, extended header still required */
  940. if ((r = putextended(z)) != ZE_OK)
  941. return r;
  942. #ifdef ZIP64_SUPPORT
  943. if (zip64_entry)
  944. tempzn += 24L;
  945. else
  946. tempzn += 16L;
  947. #else
  948. tempzn += 16L;
  949. #endif
  950. }
  951. }
  952. } /* isdir */
  953. /* Free the local extra field which is no longer needed */
  954. if (z->ext) {
  955. if (z->extra != z->cextra) {
  956. free((zvoid *)(z->extra));
  957. z->extra = NULL;
  958. }
  959. z->ext = 0;
  960. }
  961. /* Display statistics */
  962. if (noisy)
  963. {
  964. if (verbose) {
  965. fprintf( mesg, "\t(in=%s) (out=%s)",
  966. zip_fzofft(isize, NULL, "u"), zip_fzofft(s, NULL, "u"));
  967. }
  968. #ifdef BZIP2_SUPPORT
  969. if (m == BZIP2)
  970. fprintf(mesg, " (bzipped %d%%)\n", percent(isize, s));
  971. else
  972. #endif
  973. if (m == DEFLATE)
  974. fprintf(mesg, " (deflated %d%%)\n", percent(isize, s));
  975. else
  976. fprintf(mesg, " (stored 0%%)\n");
  977. mesg_line_started = 0;
  978. fflush(mesg);
  979. }
  980. if (logall)
  981. {
  982. #ifdef BZIP2_SUPPORT
  983. if (m == BZIP2)
  984. fprintf(logfile, " (bzipped %d%%)\n", percent(isize, s));
  985. else
  986. #endif
  987. if (m == DEFLATE)
  988. fprintf(logfile, " (deflated %d%%)\n", percent(isize, s));
  989. else
  990. fprintf(logfile, " (stored 0%%)\n");
  991. logfile_line_started = 0;
  992. fflush(logfile);
  993. }
  994. #ifdef WINDLL
  995. # ifdef ZIP64_SUPPORT
  996. /* The DLL api has been updated and uses a different
  997. interface. 7/24/04 EG */
  998. if (lpZipUserFunctions->ServiceApplication64 != NULL)
  999. {
  1000. if ((*lpZipUserFunctions->ServiceApplication64)(z->zname, isize))
  1001. ZIPERR(ZE_ABORT, "User terminated operation");
  1002. }
  1003. else
  1004. {
  1005. filesize64 = isize;
  1006. low = (unsigned long)(filesize64 & 0x00000000FFFFFFFF);
  1007. high = (unsigned long)((filesize64 >> 32) & 0x00000000FFFFFFFF);
  1008. if (lpZipUserFunctions->ServiceApplication64_No_Int64 != NULL) {
  1009. if ((*lpZipUserFunctions->ServiceApplication64_No_Int64)(z->zname, low, high))
  1010. ZIPERR(ZE_ABORT, "User terminated operation");
  1011. }
  1012. }
  1013. # else
  1014. if (lpZipUserFunctions->ServiceApplication != NULL)
  1015. {
  1016. if ((*lpZipUserFunctions->ServiceApplication)(z->zname, isize))
  1017. {
  1018. ZIPERR(ZE_ABORT, "User terminated operation");
  1019. }
  1020. }
  1021. # endif
  1022. #endif
  1023. return ZE_OK;
  1024. }
  1025. local unsigned file_read(buf, size)
  1026. char *buf;
  1027. unsigned size;
  1028. /* Read a new buffer from the current input file, perform end-of-line
  1029. * translation, and update the crc and input file size.
  1030. * IN assertion: size >= 2 (for end-of-line translation)
  1031. */
  1032. {
  1033. unsigned len;
  1034. char *b;
  1035. zoff_t isize_prev; /* Previous isize. Used for overflow check. */
  1036. #if defined(MMAP) || defined(BIG_MEM)
  1037. if (remain == 0L) {
  1038. return 0;
  1039. } else if (remain != (ulg)-1L) {
  1040. /* The window data is already in place. We still compute the crc
  1041. * by 32K blocks instead of once on whole file to keep a certain
  1042. * locality of reference.
  1043. */
  1044. Assert(buf == (char*)window + isize, "are you lost?");
  1045. if ((ulg)size > remain) size = (unsigned)remain;
  1046. if (size > WSIZE) size = WSIZE; /* don't touch all pages at once */
  1047. remain -= (ulg)size;
  1048. len = size;
  1049. } else
  1050. #endif /* MMAP || BIG_MEM */
  1051. if (translate_eol == 0) {
  1052. len = zread(ifile, buf, size);
  1053. if (len == (unsigned)EOF || len == 0) return len;
  1054. #ifdef OS390
  1055. b = buf;
  1056. if (aflag == ASCII) {
  1057. while (*b != '\0') {
  1058. *b = (char)ascii[(uch)*b];
  1059. b++;
  1060. }
  1061. }
  1062. #endif
  1063. } else if (translate_eol == 1) {
  1064. /* translate_eol == 1 */
  1065. /* Transform LF to CR LF */
  1066. size >>= 1;
  1067. b = buf+size;
  1068. size = len = zread(ifile, b, size);
  1069. if (len == (unsigned)EOF || len == 0) return len;
  1070. /* check buf for binary - 12/16/04 */
  1071. if (file_binary == -1) {
  1072. /* first read */
  1073. file_binary = is_text_buf(b, size) ? 0 : 1;
  1074. }
  1075. if (file_binary != 1) {
  1076. #ifdef EBCDIC
  1077. if (aflag == ASCII)
  1078. {
  1079. do {
  1080. char c;
  1081. if ((c = *b++) == '\n') {
  1082. *buf++ = CR; *buf++ = LF; len++;
  1083. } else {
  1084. *buf++ = (char)ascii[(uch)c];
  1085. }
  1086. } while (--size != 0);
  1087. }
  1088. else
  1089. #endif /* EBCDIC */
  1090. {
  1091. do {
  1092. if ((*buf++ = *b++) == '\n') *(buf-1) = CR, *buf++ = LF, len++;
  1093. } while (--size != 0);
  1094. }
  1095. buf -= len;
  1096. } else { /* do not translate binary */
  1097. memcpy(buf, b, size);
  1098. }
  1099. } else {
  1100. /* translate_eol == 2 */
  1101. /* Transform CR LF to LF and suppress final ^Z */
  1102. b = buf;
  1103. size = len = zread(ifile, buf, size-1);
  1104. if (len == (unsigned)EOF || len == 0) return len;
  1105. /* check buf for binary - 12/16/04 */
  1106. if (file_binary == -1) {
  1107. /* first read */
  1108. file_binary = is_text_buf(b, size) ? 0 : 1;
  1109. }
  1110. if (file_binary != 1) {
  1111. buf[len] = '\n'; /* I should check if next char is really a \n */
  1112. #ifdef EBCDIC
  1113. if (aflag == ASCII)
  1114. {
  1115. do {
  1116. char c;
  1117. if ((c = *b++) == '\r' && *b == '\n') {
  1118. len--;
  1119. } else {
  1120. *buf++ = (char)(c == '\n' ? LF : ascii[(uch)c]);
  1121. }
  1122. } while (--size != 0);
  1123. }
  1124. else
  1125. #endif /* EBCDIC */
  1126. {
  1127. do {
  1128. if (( *buf++ = *b++) == CR && *b == LF) buf--, len--;
  1129. } while (--size != 0);
  1130. }
  1131. if (len == 0) {
  1132. zread(ifile, buf, 1); len = 1; /* keep single \r if EOF */
  1133. #ifdef EBCDIC
  1134. if (aflag == ASCII) {
  1135. *buf = (char)(*buf == '\n' ? LF : ascii[(uch)(*buf)]);
  1136. }
  1137. #endif
  1138. } else {
  1139. buf -= len;
  1140. if (buf[len-1] == CTRLZ) len--; /* suppress final ^Z */
  1141. }
  1142. }
  1143. }
  1144. crc = crc32(crc, (uch *) buf, len);
  1145. /* 2005-05-23 SMS.
  1146. Increment file size. A small-file program reading a large file may
  1147. cause isize to overflow, so complain (and abort) if it goes
  1148. negative or wraps around. Awful things happen later otherwise.
  1149. */
  1150. isize_prev = isize;
  1151. isize += (ulg)len;
  1152. if (isize < isize_prev) {
  1153. ZIPERR(ZE_BIG, "overflow in byte count");
  1154. }
  1155. return len;
  1156. }
  1157. #ifdef USE_ZLIB
  1158. local int zl_deflate_init(pack_level)
  1159. int pack_level;
  1160. {
  1161. unsigned i;
  1162. int windowBits;
  1163. int err = Z_OK;
  1164. int zp_err = ZE_OK;
  1165. if (zlib_version[0] != ZLIB_VERSION[0]) {
  1166. sprintf(errbuf, "incompatible zlib version (expected %s, found %s)",
  1167. ZLIB_VERSION, zlib_version);
  1168. zp_err = ZE_LOGIC;
  1169. } else if (strcmp(zlib_version, ZLIB_VERSION) != 0) {
  1170. fprintf(mesg,
  1171. "\twarning: different zlib version (expected %s, using %s)\n",
  1172. ZLIB_VERSION, zlib_version);
  1173. }
  1174. /* windowBits = log2(WSIZE) */
  1175. for (i = ((unsigned)WSIZE), windowBits = 0; i != 1; i >>= 1, ++windowBits);
  1176. zstrm.zalloc = (alloc_func)Z_NULL;
  1177. zstrm.zfree = (free_func)Z_NULL;
  1178. Trace((stderr, "initializing deflate()\n"));
  1179. err = deflateInit2(&zstrm, pack_level, Z_DEFLATED, -windowBits, 8, 0);
  1180. if (err == Z_MEM_ERROR) {
  1181. sprintf(errbuf, "cannot initialize zlib deflate");
  1182. zp_err = ZE_MEM;
  1183. } else if (err != Z_OK) {
  1184. sprintf(errbuf, "zlib deflateInit failure (%d)", err);
  1185. zp_err = ZE_LOGIC;
  1186. }
  1187. deflInit = TRUE;
  1188. return zp_err;
  1189. }
  1190. void zl_deflate_free()
  1191. {
  1192. int err;
  1193. if (f_obuf != NULL) {
  1194. free(f_obuf);
  1195. f_obuf = NULL;
  1196. }
  1197. if (f_ibuf != NULL) {
  1198. free(f_ibuf);
  1199. f_ibuf = NULL;
  1200. }
  1201. if (deflInit) {
  1202. err = deflateEnd(&zstrm);
  1203. if (err != Z_OK && err !=Z_DATA_ERROR) {
  1204. ziperr(ZE_LOGIC, "zlib deflateEnd failed");
  1205. }
  1206. deflInit = FALSE;
  1207. }
  1208. }
  1209. #else /* !USE_ZLIB */
  1210. # ifdef ZP_NEED_MEMCOMPR
  1211. /* ===========================================================================
  1212. * In-memory read function. As opposed to file_read(), this function
  1213. * does not perform end-of-line translation, and does not update the
  1214. * crc and input size.
  1215. * Note that the size of the entire input buffer is an unsigned long,
  1216. * but the size used in mem_read() is only an unsigned int. This makes a
  1217. * difference on 16 bit machines. mem_read() may be called several
  1218. * times for an in-memory compression.
  1219. */
  1220. local unsigned mem_read(b, bsize)
  1221. char *b;
  1222. unsigned bsize;
  1223. {
  1224. if (in_offset < in_size) {
  1225. ulg block_size = in_size - in_offset;
  1226. if (block_size > (ulg)bsize) block_size = (ulg)bsize;
  1227. memcpy(b, in_buf + in_offset, (unsigned)block_size);
  1228. in_offset += (unsigned)block_size;
  1229. return (unsigned)block_size;
  1230. } else {
  1231. return 0; /* end of input */
  1232. }
  1233. }
  1234. # endif /* ZP_NEED_MEMCOMPR */
  1235. /* ===========================================================================
  1236. * Flush the current output buffer.
  1237. */
  1238. void flush_outbuf(o_buf, o_idx)
  1239. char *o_buf;
  1240. unsigned *o_idx;
  1241. {
  1242. if (y == NULL) {
  1243. error("output buffer too small for in-memory compression");
  1244. }
  1245. /* Encrypt and write the output buffer: */
  1246. if (*o_idx != 0) {
  1247. zfwrite(o_buf, 1, (extent)*o_idx);
  1248. if (ferror(y)) ziperr(ZE_WRITE, "write error on zip file");
  1249. }
  1250. *o_idx = 0;
  1251. }
  1252. /* ===========================================================================
  1253. * Return true if the zip file can be seeked. This is used to check if
  1254. * the local header can be re-rewritten. This function always returns
  1255. * true for in-memory compression.
  1256. * IN assertion: the local header has already been written (ftell() > 0).
  1257. */
  1258. int seekable()
  1259. {
  1260. return fseekable(y);
  1261. }
  1262. #endif /* ?USE_ZLIB */
  1263. /* ===========================================================================
  1264. * Compression to archive file.
  1265. */
  1266. local zoff_t filecompress(z_entry, cmpr_method)
  1267. struct zlist far *z_entry;
  1268. int *cmpr_method;
  1269. {
  1270. #ifdef USE_ZLIB
  1271. int err = Z_OK;
  1272. unsigned mrk_cnt = 1;
  1273. int maybe_stored = FALSE;
  1274. ulg cmpr_size;
  1275. #if defined(MMAP) || defined(BIG_MEM)
  1276. unsigned ibuf_sz = (unsigned)SBSZ;
  1277. #else
  1278. # define ibuf_sz ((unsigned)SBSZ)
  1279. #endif
  1280. #ifndef OBUF_SZ
  1281. # define OBUF_SZ ZBSZ
  1282. #endif
  1283. unsigned u;
  1284. #if defined(MMAP) || defined(BIG_MEM)
  1285. if (remain == (ulg)-1L && f_ibuf == NULL)
  1286. #else /* !(MMAP || BIG_MEM */
  1287. if (f_ibuf == NULL)
  1288. #endif /* MMAP || BIG_MEM */
  1289. f_ibuf = (char *)malloc(SBSZ);
  1290. if (f_obuf == NULL)
  1291. f_obuf = (char *)malloc(OBUF_SZ);
  1292. #if defined(MMAP) || defined(BIG_MEM)
  1293. if ((remain == (ulg)-1L && f_ibuf == NULL) || f_obuf == NULL)
  1294. #else /* !(MMAP || BIG_MEM */
  1295. if (f_ibuf == NULL || f_obuf == NULL)
  1296. #endif /* MMAP || BIG_MEM */
  1297. ziperr(ZE_MEM, "allocating zlib file-I/O buffers");
  1298. if (!deflInit) {
  1299. err = zl_deflate_init(level);
  1300. if (err != ZE_OK)
  1301. ziperr(err, errbuf);
  1302. }
  1303. if (level <= 2) {
  1304. z_entry->flg |= 4;
  1305. } else if (level >= 8) {
  1306. z_entry->flg |= 2;
  1307. }
  1308. #if defined(MMAP) || defined(BIG_MEM)
  1309. if (remain != (ulg)-1L) {
  1310. zstrm.next_in = (Bytef *)window;
  1311. ibuf_sz = (unsigned)WSIZE;
  1312. } else
  1313. #endif /* MMAP || BIG_MEM */
  1314. {
  1315. zstrm.next_in = (Bytef *)f_ibuf;
  1316. }
  1317. zstrm.avail_in = file_read(zstrm.next_in, ibuf_sz);
  1318. if (zstrm.avail_in < ibuf_sz) {
  1319. unsigned more = file_read(zstrm.next_in + zstrm.avail_in,
  1320. (ibuf_sz - zstrm.avail_in));
  1321. if (more == EOF || more == 0) {
  1322. maybe_stored = TRUE;
  1323. } else {
  1324. zstrm.avail_in += more;
  1325. }
  1326. }
  1327. zstrm.next_out = (Bytef *)f_obuf;
  1328. zstrm.avail_out = OBUF_SZ;
  1329. if (!maybe_stored) while (zstrm.avail_in != 0 && zstrm.avail_in != EOF) {
  1330. err = deflate(&zstrm, Z_NO_FLUSH);
  1331. if (err != Z_OK && err != Z_STREAM_END) {
  1332. sprintf(errbuf, "unexpected zlib deflate error %d", err);
  1333. ziperr(ZE_LOGIC, errbuf);
  1334. }
  1335. if (zstrm.avail_out == 0) {
  1336. if (zfwrite(f_obuf, 1, OBUF_SZ) != OBUF_SZ) {
  1337. ziperr(ZE_TEMP, "error writing to zipfile");
  1338. }
  1339. zstrm.next_out = (Bytef *)f_obuf;
  1340. zstrm.avail_out = OBUF_SZ;
  1341. }
  1342. if (zstrm.avail_in == 0) {
  1343. if (verbose || noisy)
  1344. while((unsigned)(zstrm.total_in / (uLong)WSIZE) > mrk_cnt) {
  1345. mrk_cnt++;
  1346. if (!display_globaldots) {
  1347. if (dot_size > 0) {
  1348. /* initial space */
  1349. if (noisy && dot_count == -1) {
  1350. #ifndef WINDLL
  1351. putc(' ', mesg);
  1352. fflush(mesg);
  1353. #else
  1354. fprintf(stdout,"%c",' ');
  1355. #endif
  1356. dot_count++;
  1357. }
  1358. dot_count++;
  1359. if (dot_size <= (dot_count + 1) * WSIZE) dot_count = 0;
  1360. }
  1361. if (noisy && dot_size && !dot_count) {
  1362. #ifndef WINDLL
  1363. putc('.', mesg);
  1364. fflush(mesg);
  1365. #else
  1366. fprintf(stdout,"%c",'.');
  1367. #endif
  1368. mesg_line_started = 1;
  1369. }
  1370. }
  1371. }
  1372. #if defined(MMAP) || defined(BIG_MEM)
  1373. if (remain == (ulg)-1L)
  1374. zstrm.next_in = (Bytef *)f_ibuf;
  1375. #else
  1376. zstrm.next_in = (Bytef *)f_ibuf;
  1377. #endif
  1378. zstrm.avail_in = file_read(zstrm.next_in, ibuf_sz);
  1379. }
  1380. }
  1381. do {
  1382. err = deflate(&zstrm, Z_FINISH);
  1383. if (maybe_stored) {
  1384. if (err == Z_STREAM_END && zstrm.total_out >= zstrm.total_in &&
  1385. fseekable(zipfile)) {
  1386. /* deflation does not reduce size, switch to STORE method */
  1387. unsigned len_out = (unsigned)zstrm.total_in;
  1388. if (zfwrite(f_ibuf, 1, len_out) != len_out) {
  1389. ziperr(ZE_TEMP, "error writing to zipfile");
  1390. }
  1391. zstrm.total_out = (uLong)len_out;
  1392. *cmpr_method = STORE;
  1393. break;
  1394. } else {
  1395. maybe_stored = FALSE;
  1396. }
  1397. }
  1398. if (zstrm.avail_out < OBUF_SZ) {
  1399. unsigned len_out = OBUF_SZ - zstrm.avail_out;
  1400. if (zfwrite(f_obuf, 1, len_out) != len_out) {
  1401. ziperr(ZE_TEMP, "error writing to zipfile");
  1402. }
  1403. zstrm.next_out = (Bytef *)f_obuf;
  1404. zstrm.avail_out = OBUF_SZ;
  1405. }
  1406. } while (err == Z_OK);
  1407. if (err != Z_STREAM_END) {
  1408. sprintf(errbuf, "unexpected zlib deflate error %d", err);
  1409. ziperr(ZE_LOGIC, errbuf);
  1410. }
  1411. if (z_entry->att == (ush)UNKNOWN)
  1412. z_entry->att = (ush)(zstrm.data_type == Z_ASCII ? ASCII : BINARY);
  1413. cmpr_size = (ulg)zstrm.total_out;
  1414. if ((err = deflateReset(&zstrm)) != Z_OK)
  1415. ziperr(ZE_LOGIC, "zlib deflateReset failed");
  1416. return cmpr_size;
  1417. #else /* !USE_ZLIB */
  1418. /* Set the defaults for file compression. */
  1419. read_buf = file_read;
  1420. /* Initialize deflate's internals and execute file compression. */
  1421. bi_init(file_outbuf, sizeof(file_outbuf), TRUE);
  1422. ct_init(&z_entry->att, cmpr_method);
  1423. lm_init(level, &z_entry->flg);
  1424. return deflate();
  1425. #endif /* ?USE_ZLIB */
  1426. }
  1427. #ifdef ZP_NEED_MEMCOMPR
  1428. /* ===========================================================================
  1429. * In-memory compression. This version can be used only if the entire input
  1430. * fits in one memory buffer. The compression is then done in a single
  1431. * call of memcompress(). (An extension to allow repeated calls would be
  1432. * possible but is not needed here.)
  1433. * The first two bytes of the compressed output are set to a short with the
  1434. * method used (DEFLATE or STORE). The following four bytes contain the CRC.
  1435. * The values are stored in little-endian order on all machines.
  1436. * This function returns the byte size of the compressed output, including
  1437. * the first six bytes (method and crc).
  1438. */
  1439. ulg memcompress(tgt, tgtsize, src, srcsize)
  1440. char *tgt, *src; /* target and source buffers */
  1441. ulg tgtsize, srcsize; /* target and source sizes */
  1442. {
  1443. ulg crc;
  1444. unsigned out_total;
  1445. int method = DEFLATE;
  1446. #ifdef USE_ZLIB
  1447. int err = Z_OK;
  1448. #else
  1449. ush att = (ush)UNKNOWN;
  1450. ush flags = 0;
  1451. #endif
  1452. if (tgtsize <= (ulg)6L) error("target buffer too small");
  1453. out_total = 2 + 4;
  1454. #ifdef USE_ZLIB
  1455. if (!deflInit) {
  1456. err = zl_deflate_init(level);
  1457. if (err != ZE_OK)
  1458. ziperr(err, errbuf);
  1459. }
  1460. zstrm.next_in = (Bytef *)src;
  1461. zstrm.avail_in = (uInt)srcsize;
  1462. zstrm.next_out = (Bytef *)(tgt + out_total);
  1463. zstrm.avail_out = (uInt)tgtsize - (uInt)out_total;
  1464. err = deflate(&zstrm, Z_FINISH);
  1465. if (err != Z_STREAM_END)
  1466. error("output buffer too small for in-memory compression");
  1467. out_total += (unsigned)zstrm.total_out;
  1468. if ((err = deflateReset(&zstrm)) != Z_OK)
  1469. error("zlib deflateReset failed");
  1470. #else /* !USE_ZLIB */
  1471. read_buf = mem_read;
  1472. in_buf = src;
  1473. in_size = (unsigned)srcsize;
  1474. in_offset = 0;
  1475. window_size = 0L;
  1476. bi_init(tgt + (2 + 4), (unsigned)(tgtsize - (2 + 4)), FALSE);
  1477. ct_init(&att, &method);
  1478. lm_init((level != 0 ? level : 1), &flags);
  1479. out_total += (unsigned)deflate();
  1480. window_size = 0L; /* was updated by lm_init() */
  1481. #endif /* ?USE_ZLIB */
  1482. crc = CRCVAL_INITIAL;
  1483. crc = crc32(crc, (uch *)src, (extent)srcsize);
  1484. /* For portability, force little-endian order on all machines: */
  1485. tgt[0] = (char)(method & 0xff);
  1486. tgt[1] = (char)((method >> 8) & 0xff);
  1487. tgt[2] = (char)(crc & 0xff);
  1488. tgt[3] = (char)((crc >> 8) & 0xff);
  1489. tgt[4] = (char)((crc >> 16) & 0xff);
  1490. tgt[5] = (char)((crc >> 24) & 0xff);
  1491. return (ulg)out_total;
  1492. }
  1493. #endif /* ZP_NEED_MEMCOMPR */
  1494. #ifdef BZIP2_SUPPORT
  1495. local int bz_compress_init(pack_level)
  1496. int pack_level;
  1497. {
  1498. int err = BZ_OK;
  1499. int zp_err = ZE_OK;
  1500. const char *bzlibVer;
  1501. bzlibVer = BZ2_bzlibVersion();
  1502. /* $TODO - Check BZIP2 LIB version? */
  1503. bstrm.bzalloc = NULL;
  1504. bstrm.bzfree = NULL;
  1505. bstrm.opaque = NULL;
  1506. Trace((stderr, "initializing bzlib compress()\n"));
  1507. err = BZ2_bzCompressInit(&bstrm, pack_level, 0, 30);
  1508. if (err == BZ_MEM_ERROR) {
  1509. sprintf(errbuf, "cannot initialize bzlib compress");
  1510. zp_err = ZE_MEM;
  1511. } else if (err != BZ_OK) {
  1512. sprintf(errbuf, "bzlib bzCompressInit failure (%d)", err);
  1513. zp_err = ZE_LOGIC;
  1514. }
  1515. bzipInit = TRUE;
  1516. return zp_err;
  1517. }
  1518. void bz_compress_free()
  1519. {
  1520. int err;
  1521. if (f_obuf != NULL) {
  1522. free(f_obuf);
  1523. f_obuf = NULL;
  1524. }
  1525. if (f_ibuf != NULL) {
  1526. free(f_ibuf);
  1527. f_ibuf = NULL;
  1528. }
  1529. if (bzipInit) {
  1530. err = BZ2_bzCompressEnd(&bstrm);
  1531. if (err != BZ_OK && err != BZ_DATA_ERROR) {
  1532. ziperr(ZE_LOGIC, "bzlib bzCompressEnd failed");
  1533. }
  1534. bzipInit = FALSE;
  1535. }
  1536. }
  1537. /* ===========================================================================
  1538. * BZIP2 Compression to archive file.
  1539. */
  1540. local zoff_t bzfilecompress(z_entry, cmpr_method)
  1541. struct zlist far *z_entry;
  1542. int *cmpr_method;
  1543. {
  1544. FILE *zipfile = y;
  1545. int err = BZ_OK;
  1546. unsigned mrk_cnt = 1;
  1547. int maybe_stored = FALSE;
  1548. zoff_t cmpr_size;
  1549. #if defined(MMAP) || defined(BIG_MEM)
  1550. unsigned ibuf_sz = (unsigned)SBSZ;
  1551. #else
  1552. # define ibuf_sz ((unsigned)SBSZ)
  1553. #endif
  1554. #ifndef OBUF_SZ
  1555. # define OBUF_SZ ZBSZ
  1556. #endif
  1557. #if defined(MMAP) || defined(BIG_MEM)
  1558. if (remain == (ulg)-1L && f_ibuf == NULL)
  1559. #else /* !(MMAP || BIG_MEM */
  1560. if (f_ibuf == NULL)
  1561. #endif /* MMAP || BIG_MEM */
  1562. f_ibuf = (char *)malloc(SBSZ);
  1563. if (f_obuf == NULL)
  1564. f_obuf = (char *)malloc(OBUF_SZ);
  1565. #if defined(MMAP) || defined(BIG_MEM)
  1566. if ((remain == (ulg)-1L && f_ibuf == NULL) || f_obuf == NULL)
  1567. #else /* !(MMAP || BIG_MEM */
  1568. if (f_ibuf == NULL || f_obuf == NULL)
  1569. #endif /* MMAP || BIG_MEM */
  1570. ziperr(ZE_MEM, "allocating zlib/bzlib file-I/O buffers");
  1571. if (!bzipInit) {
  1572. err = bz_compress_init(level);
  1573. if (err != ZE_OK)
  1574. ziperr(err, errbuf);
  1575. }
  1576. #if defined(MMAP) || defined(BIG_MEM)
  1577. if (remain != (ulg)-1L) {
  1578. bstrm.next_in = (Bytef *)window;
  1579. ibuf_sz = (unsigned)WSIZE;
  1580. } else
  1581. #endif /* MMAP || BIG_MEM */
  1582. {
  1583. bstrm.next_in = (char *)f_ibuf;
  1584. }
  1585. bstrm.avail_in = file_read(bstrm.next_in, ibuf_sz);
  1586. if (file_binary_final == 0) {
  1587. /* check for binary as library does not */
  1588. if (!is_text_buf(bstrm.next_in, ibuf_sz))
  1589. file_binary_final = 1;
  1590. }
  1591. if (bstrm.avail_in < ibuf_sz) {
  1592. unsigned more = file_read(bstrm.next_in + bstrm.avail_in,
  1593. (ibuf_sz - bstrm.avail_in));
  1594. if (more == (unsigned) EOF || more == 0) {
  1595. maybe_stored = TRUE;
  1596. } else {
  1597. bstrm.avail_in += more;
  1598. }
  1599. }
  1600. bstrm.next_out = (char *)f_obuf;
  1601. bstrm.avail_out = OBUF_SZ;
  1602. if (!maybe_stored) {
  1603. while (bstrm.avail_in != 0 && bstrm.avail_in != (unsigned) EOF) {
  1604. err = BZ2_bzCompress(&bstrm, BZ_RUN);
  1605. if (err != BZ_RUN_OK && err != BZ_STREAM_END) {
  1606. sprintf(errbuf, "unexpected bzlib compress error %d", err);
  1607. ziperr(ZE_LOGIC, errbuf);
  1608. }
  1609. if (bstrm.avail_out == 0) {
  1610. if (zfwrite(f_obuf, 1, OBUF_SZ) != OBUF_SZ) {
  1611. ziperr(ZE_TEMP, "error writing to zipfile");
  1612. }
  1613. bstrm.next_out = (char *)f_obuf;
  1614. bstrm.avail_out = OBUF_SZ;
  1615. }
  1616. /* $TODO what about high 32-bits of total-in??? */
  1617. if (bstrm.avail_in == 0) {
  1618. if (verbose || noisy)
  1619. #ifdef LARGE_FILE_SUPPORT
  1620. while((unsigned)((bstrm.total_in_lo32
  1621. + (((zoff_t)bstrm.total_in_hi32) << 32))
  1622. / (zoff_t)(ulg)WSIZE) > mrk_cnt) {
  1623. #else
  1624. while((unsigned)(bstrm.total_in_lo32 / (ulg)WSIZE) > mrk_cnt) {
  1625. #endif
  1626. mrk_cnt++;
  1627. if (!display_globaldots) {
  1628. if (dot_size > 0) {
  1629. /* initial space */
  1630. if (noisy && dot_count == -1) {
  1631. #ifndef WINDLL
  1632. putc(' ', mesg);
  1633. fflush(mesg);
  1634. #else
  1635. fprintf(stdout,"%c",' ');
  1636. #endif
  1637. dot_count++;
  1638. }
  1639. dot_count++;
  1640. if (dot_size <= (dot_count + 1) * WSIZE) dot_count = 0;
  1641. }
  1642. if (noisy && dot_size && !dot_count) {
  1643. #ifndef WINDLL
  1644. putc('.', mesg);
  1645. fflush(mesg);
  1646. #else
  1647. fprintf(stdout,"%c",'.');
  1648. #endif
  1649. mesg_line_started = 1;
  1650. }
  1651. }
  1652. }
  1653. #if defined(MMAP) || defined(BIG_MEM)
  1654. if (remain == (ulg)-1L)
  1655. bstrm.next_in = (char *)f_ibuf;
  1656. #else
  1657. bstrm.next_in = (char *)f_ibuf;
  1658. #endif
  1659. bstrm.avail_in = file_read(bstrm.next_in, ibuf_sz);
  1660. if (file_binary_final == 0) {
  1661. /* check for binary as library does not */
  1662. if (!is_text_buf(bstrm.next_in, ibuf_sz))
  1663. file_binary_final = 1;
  1664. }
  1665. }
  1666. }
  1667. }
  1668. /* binary or text */
  1669. if (file_binary_final)
  1670. /* found binary in file */
  1671. z_entry->att = (ush)BINARY;
  1672. else
  1673. /* text file */
  1674. z_entry->att = (ush)ASCII;
  1675. do {
  1676. err = BZ2_bzCompress(&bstrm, BZ_FINISH);
  1677. if (maybe_stored) {
  1678. /* This code is only executed when the complete data stream fits
  1679. into the input buffer (see above where maybe_stored gets set).
  1680. So, it is safe to assume that total_in_hi32 (and total_out_hi32)
  1681. are 0, because the input buffer size is well below the 32-bit
  1682. limit.
  1683. */
  1684. if (err == BZ_STREAM_END
  1685. && bstrm.total_out_lo32 >= bstrm.total_in_lo32
  1686. && fseekable(zipfile)) {
  1687. /* BZIP2 compress does not reduce size,
  1688. switch to STORE method */
  1689. unsigned len_out = (unsigned)bstrm.total_in_lo32;
  1690. if (zfwrite(f_ibuf, 1, len_out) != len_out) {
  1691. ziperr(ZE_TEMP, "error writing to zipfile");
  1692. }
  1693. bstrm.total_out_lo32 = (ulg)len_out;
  1694. *cmpr_method = STORE;
  1695. break;
  1696. } else {
  1697. maybe_stored = FALSE;
  1698. }
  1699. }
  1700. if (bstrm.avail_out < OBUF_SZ) {
  1701. unsigned len_out = OBUF_SZ - bstrm.avail_out;
  1702. if (zfwrite(f_obuf, 1, len_out) != len_out) {
  1703. ziperr(ZE_TEMP, "error writing to zipfile");
  1704. }
  1705. bstrm.next_out = (char *)f_obuf;
  1706. bstrm.avail_out = OBUF_SZ;
  1707. }
  1708. } while (err == BZ_FINISH_OK);
  1709. if (err < BZ_OK) {
  1710. sprintf(errbuf, "unexpected bzlib compress error %d", err);
  1711. ziperr(ZE_LOGIC, errbuf);
  1712. }
  1713. if (z_entry->att == (ush)UNKNOWN)
  1714. z_entry->att = (ush)BINARY;
  1715. #ifdef LARGE_FILE_SUPPORT
  1716. cmpr_size = (zoff_t)bstrm.total_out_lo32
  1717. + (((zoff_t)bstrm.total_out_hi32) << 32);
  1718. #else
  1719. cmpr_size = (zoff_t)bstrm.total_out_lo32;
  1720. #endif
  1721. if ((err = BZ2_bzCompressEnd(&bstrm)) != BZ_OK)
  1722. ziperr(ZE_LOGIC, "zlib deflateReset failed");
  1723. bzipInit = FALSE;
  1724. return cmpr_size;
  1725. }
  1726. #endif /* BZIP2_SUPPORT */
  1727. #endif /* !UTIL */