XZip.cpp 116 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211
  1. // XZip.cpp Version 1.3
  2. //
  3. // Authors: Mark Adler et al. (see below)
  4. //
  5. // Modified by: Lucian Wischik
  6. // lu@wischik.com
  7. //
  8. // Version 1.0 - Turned C files into just a single CPP file
  9. // - Made them compile cleanly as C++ files
  10. // - Gave them simpler APIs
  11. // - Added the ability to zip/unzip directly in memory without
  12. // any intermediate files
  13. //
  14. // Modified by: Hans Dietrich
  15. // hdietrich@gmail.com
  16. //
  17. // Version 1.3: - Fixed UTC problem
  18. //
  19. // Version 1.2: - Many bug fixes. See CodeProject article for list.
  20. //
  21. // Version 1.1: - Added Unicode support to CreateZip() and ZipAdd()
  22. // - Changed file names to avoid conflicts with Lucian's files
  23. //
  24. ///////////////////////////////////////////////////////////////////////////////
  25. //
  26. // Lucian Wischik's comments:
  27. // --------------------------
  28. // THIS FILE is almost entirely based upon code by Info-ZIP.
  29. // It has been modified by Lucian Wischik.
  30. // The original code may be found at http://www.info-zip.org
  31. // The original copyright text follows.
  32. //
  33. ///////////////////////////////////////////////////////////////////////////////
  34. //
  35. // Original authors' comments:
  36. // ---------------------------
  37. // This is version 2002-Feb-16 of the Info-ZIP copyright and license. The
  38. // definitive version of this document should be available at
  39. // ftp://ftp.info-zip.org/pub/infozip/license.html indefinitely.
  40. //
  41. // Copyright (c) 1990-2002 Info-ZIP. All rights reserved.
  42. //
  43. // For the purposes of this copyright and license, "Info-ZIP" is defined as
  44. // the following set of individuals:
  45. //
  46. // Mark Adler, John Bush, Karl Davis, Harald Denker, Jean-Michel Dubois,
  47. // Jean-loup Gailly, Hunter Goatley, Ian Gorman, Chris Herborth, Dirk Haase,
  48. // Greg Hartwig, Robert Heath, Jonathan Hudson, Paul Kienitz,
  49. // David Kirschbaum, Johnny Lee, Onno van der Linden, Igor Mandrichenko,
  50. // Steve P. Miller, Sergio Monesi, Keith Owens, George Petrov, Greg Roelofs,
  51. // Kai Uwe Rommel, Steve Salisbury, Dave Smith, Christian Spieler,
  52. // Antoine Verheijen, Paul von Behren, Rich Wales, Mike White
  53. //
  54. // This software is provided "as is", without warranty of any kind, express
  55. // or implied. In no event shall Info-ZIP or its contributors be held liable
  56. // for any direct, indirect, incidental, special or consequential damages
  57. // arising out of the use of or inability to use this software.
  58. //
  59. // Permission is granted to anyone to use this software for any purpose,
  60. // including commercial applications, and to alter it and redistribute it
  61. // freely, subject to the following restrictions:
  62. //
  63. // 1. Redistributions of source code must retain the above copyright notice,
  64. // definition, disclaimer, and this list of conditions.
  65. //
  66. // 2. Redistributions in binary form (compiled executables) must reproduce
  67. // the above copyright notice, definition, disclaimer, and this list of
  68. // conditions in documentation and/or other materials provided with the
  69. // distribution. The sole exception to this condition is redistribution
  70. // of a standard UnZipSFX binary as part of a self-extracting archive;
  71. // that is permitted without inclusion of this license, as long as the
  72. // normal UnZipSFX banner has not been removed from the binary or disabled.
  73. //
  74. // 3. Altered versions--including, but not limited to, ports to new
  75. // operating systems, existing ports with new graphical interfaces, and
  76. // dynamic, shared, or static library versions--must be plainly marked
  77. // as such and must not be misrepresented as being the original source.
  78. // Such altered versions also must not be misrepresented as being
  79. // Info-ZIP releases--including, but not limited to, labeling of the
  80. // altered versions with the names "Info-ZIP" (or any variation thereof,
  81. // including, but not limited to, different capitalizations),
  82. // "Pocket UnZip", "WiZ" or "MacZip" without the explicit permission of
  83. // Info-ZIP. Such altered versions are further prohibited from
  84. // misrepresentative use of the Zip-Bugs or Info-ZIP e-mail addresses or
  85. // of the Info-ZIP URL(s).
  86. //
  87. // 4. Info-ZIP retains the right to use the names "Info-ZIP", "Zip", "UnZip",
  88. // "UnZipSFX", "WiZ", "Pocket UnZip", "Pocket Zip", and "MacZip" for its
  89. // own source and binary releases.
  90. //
  91. ///////////////////////////////////////////////////////////////////////////////
  92. #define _USE_32BIT_TIME_T //+++1.2
  93. #define STRICT
  94. #ifndef WIN32_LEAN_AND_MEAN
  95. #define WIN32_LEAN_AND_MEAN
  96. #endif //WIN32_LEAN_AND_MEAN
  97. #include <windows.h>
  98. #include <tchar.h>
  99. #include <time.h>
  100. #include "xzip.h"
  101. #pragma warning(disable : 4996) // disable bogus deprecation warning
  102. typedef unsigned char uch; // unsigned 8-bit value
  103. typedef unsigned short ush; // unsigned 16-bit value
  104. typedef unsigned long ulg; // unsigned 32-bit value
  105. typedef size_t extent; // file size
  106. typedef unsigned Pos; // must be at least 32 bits
  107. typedef unsigned IPos; // A Pos is an index in the character window. Pos is used only for parameter passing
  108. #ifndef EOF
  109. #define EOF (-1)
  110. #endif
  111. // Error return values. The values 0..4 and 12..18 follow the conventions
  112. // of PKZIP. The values 4..10 are all assigned to "insufficient memory"
  113. // by PKZIP, so the codes 5..10 are used here for other purposes.
  114. #define ZE_MISS -1 // used by procname(), zipbare()
  115. #define ZE_OK 0 // success
  116. #define ZE_EOF 2 // unexpected end of zip file
  117. #define ZE_FORM 3 // zip file structure error
  118. #define ZE_MEM 4 // out of memory
  119. #define ZE_LOGIC 5 // internal logic error
  120. #define ZE_BIG 6 // entry too large to split
  121. #define ZE_NOTE 7 // invalid comment format
  122. #define ZE_TEST 8 // zip test (-T) failed or out of memory
  123. #define ZE_ABORT 9 // user interrupt or termination
  124. #define ZE_TEMP 10 // error using a temp file
  125. #define ZE_READ 11 // read or seek error
  126. #define ZE_NONE 12 // nothing to do
  127. #define ZE_NAME 13 // missing or empty zip file
  128. #define ZE_WRITE 14 // error writing to a file
  129. #define ZE_CREAT 15 // couldn't open to write
  130. #define ZE_PARMS 16 // bad command line
  131. #define ZE_OPEN 18 // could not open a specified file to read
  132. #define ZE_MAXERR 18 // the highest error number
  133. // internal file attribute
  134. #define UNKNOWN (-1)
  135. #define BINARY 0
  136. #define ASCII 1
  137. #define BEST -1 // Use best method (deflation or store)
  138. #define STORE 0 // Store method
  139. #define DEFLATE 8 // Deflation method
  140. #define CRCVAL_INITIAL 0L
  141. // MSDOS file or directory attributes
  142. #define MSDOS_HIDDEN_ATTR 0x02
  143. #define MSDOS_DIR_ATTR 0x10
  144. // Lengths of headers after signatures in bytes
  145. #define LOCHEAD 26
  146. #define CENHEAD 42
  147. #define ENDHEAD 18
  148. // Definitions for extra field handling:
  149. #define EB_HEADSIZE 4 /* length of a extra field block header */
  150. #define EB_LEN 2 /* offset of data length field in header */
  151. #define EB_UT_MINLEN 1 /* minimal UT field contains Flags byte */
  152. #define EB_UT_FLAGS 0 /* byte offset of Flags field */
  153. #define EB_UT_TIME1 1 /* byte offset of 1st time value */
  154. #define EB_UT_FL_MTIME (1 << 0) /* mtime present */
  155. #define EB_UT_FL_ATIME (1 << 1) /* atime present */
  156. #define EB_UT_FL_CTIME (1 << 2) /* ctime present */
  157. #define EB_UT_LEN(n) (EB_UT_MINLEN + 4 * (n))
  158. #define EB_L_UT_SIZE (EB_HEADSIZE + EB_UT_LEN(3))
  159. #define EB_C_UT_SIZE (EB_HEADSIZE + EB_UT_LEN(1))
  160. // Macros for writing machine integers to little-endian format
  161. #define PUTSH(a,f) {char _putsh_c=(char)((a)&0xff); wfunc(param,&_putsh_c,1); _putsh_c=(char)((a)>>8); wfunc(param,&_putsh_c,1);}
  162. #define PUTLG(a,f) {PUTSH((a) & 0xffff,(f)) PUTSH((a) >> 16,(f))}
  163. // -- Structure of a ZIP file --
  164. // Signatures for zip file information headers
  165. #define LOCSIG 0x04034b50L
  166. #define CENSIG 0x02014b50L
  167. #define ENDSIG 0x06054b50L
  168. #define EXTLOCSIG 0x08074b50L
  169. #define MIN_MATCH 3
  170. #define MAX_MATCH 258
  171. // The minimum and maximum match lengths
  172. #define WSIZE (0x8000)
  173. // Maximum window size = 32K. If you are really short of memory, compile
  174. // with a smaller WSIZE but this reduces the compression ratio for files
  175. // of size > WSIZE. WSIZE must be a power of two in the current implementation.
  176. //
  177. #define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
  178. // Minimum amount of lookahead, except at the end of the input file.
  179. // See deflate.c for comments about the MIN_MATCH+1.
  180. //
  181. #define MAX_DIST (WSIZE-MIN_LOOKAHEAD)
  182. // In order to simplify the code, particularly on 16 bit machines, match
  183. // distances are limited to MAX_DIST instead of WSIZE.
  184. //
  185. // ===========================================================================
  186. // Constants
  187. //
  188. #define MAX_BITS 15
  189. // All codes must not exceed MAX_BITS bits
  190. #define MAX_BL_BITS 7
  191. // Bit length codes must not exceed MAX_BL_BITS bits
  192. #define LENGTH_CODES 29
  193. // number of length codes, not counting the special END_BLOCK code
  194. #define LITERALS 256
  195. // number of literal bytes 0..255
  196. #define END_BLOCK 256
  197. // end of block literal code
  198. #define L_CODES (LITERALS+1+LENGTH_CODES)
  199. // number of Literal or Length codes, including the END_BLOCK code
  200. #define D_CODES 30
  201. // number of distance codes
  202. #define BL_CODES 19
  203. // number of codes used to transfer the bit lengths
  204. #define STORED_BLOCK 0
  205. #define STATIC_TREES 1
  206. #define DYN_TREES 2
  207. // The three kinds of block type
  208. #define LIT_BUFSIZE 0x8000
  209. #define DIST_BUFSIZE LIT_BUFSIZE
  210. // Sizes of match buffers for literals/lengths and distances. There are
  211. // 4 reasons for limiting LIT_BUFSIZE to 64K:
  212. // - frequencies can be kept in 16 bit counters
  213. // - if compression is not successful for the first block, all input data is
  214. // still in the window so we can still emit a stored block even when input
  215. // comes from standard input. (This can also be done for all blocks if
  216. // LIT_BUFSIZE is not greater than 32K.)
  217. // - if compression is not successful for a file smaller than 64K, we can
  218. // even emit a stored file instead of a stored block (saving 5 bytes).
  219. // - creating new Huffman trees less frequently may not provide fast
  220. // adaptation to changes in the input data statistics. (Take for
  221. // example a binary file with poorly compressible code followed by
  222. // a highly compressible string table.) Smaller buffer sizes give
  223. // fast adaptation but have of course the overhead of transmitting trees
  224. // more frequently.
  225. // - I can't count above 4
  226. // The current code is general and allows DIST_BUFSIZE < LIT_BUFSIZE (to save
  227. // memory at the expense of compression). Some optimizations would be possible
  228. // if we rely on DIST_BUFSIZE == LIT_BUFSIZE.
  229. //
  230. #define REP_3_6 16
  231. // repeat previous bit length 3-6 times (2 bits of repeat count)
  232. #define REPZ_3_10 17
  233. // repeat a zero length 3-10 times (3 bits of repeat count)
  234. #define REPZ_11_138 18
  235. // repeat a zero length 11-138 times (7 bits of repeat count)
  236. #define HEAP_SIZE (2*L_CODES+1)
  237. // maximum heap size
  238. // ===========================================================================
  239. // Local data used by the "bit string" routines.
  240. //
  241. #define Buf_size (8 * 2*sizeof(char))
  242. // Number of bits used within bi_buf. (bi_buf may be implemented on
  243. // more than 16 bits on some systems.)
  244. // Output a 16 bit value to the bit stream, lower (oldest) byte first
  245. #if 0 // -----------------------------------------------------------
  246. #define PUTSHORT(state,w) \
  247. { \
  248. if (state.bs.out_offset >= state.bs.out_size-1) \
  249. state.flush_outbuf(state.param,state.bs.out_buf, &state.bs.out_offset); \
  250. state.bs.out_buf[state.bs.out_offset++] = (char) ((w) & 0xff); \
  251. state.bs.out_buf[state.bs.out_offset++] = (char) ((ush)(w) >> 8); \
  252. }
  253. #endif // -----------------------------------------------------------
  254. //+++1.2
  255. #define PUTSHORT(state,w) \
  256. { \
  257. if (state.bs.out_offset >= state.bs.out_size-1) \
  258. state.flush_outbuf(state.param,state.bs.out_buf, &state.bs.out_offset); \
  259. if (state.bs.out_offset < state.bs.out_size-1) \
  260. { \
  261. state.bs.out_buf[state.bs.out_offset++] = (char) ((w) & 0xff); \
  262. state.bs.out_buf[state.bs.out_offset++] = (char) ((ush)(w) >> 8); \
  263. }\
  264. }
  265. #if 0 // -----------------------------------------------------------
  266. #define PUTBYTE(state,b) \
  267. { \
  268. if (state.bs.out_offset >= state.bs.out_size) \
  269. state.flush_outbuf(state.param,state.bs.out_buf, &state.bs.out_offset); \
  270. state.bs.out_buf[state.bs.out_offset++] = (char) (b); \
  271. }
  272. #endif // -----------------------------------------------------------
  273. //+++1.2
  274. #define PUTBYTE(state,b) \
  275. { \
  276. if (state.bs.out_offset >= state.bs.out_size) \
  277. state.flush_outbuf(state.param,state.bs.out_buf, &state.bs.out_offset); \
  278. if (state.bs.out_offset < state.bs.out_size) \
  279. state.bs.out_buf[state.bs.out_offset++] = (char) (b); \
  280. }
  281. // DEFLATE.CPP HEADER
  282. #define HASH_BITS 15
  283. // For portability to 16 bit machines, do not use values above 15.
  284. #define HASH_SIZE (unsigned)(1<<HASH_BITS)
  285. #define HASH_MASK (HASH_SIZE-1)
  286. #define WMASK (WSIZE-1)
  287. // HASH_SIZE and WSIZE must be powers of two
  288. #define NIL 0
  289. // Tail of hash chains
  290. #define FAST 4
  291. #define SLOW 2
  292. // speed options for the general purpose bit flag
  293. #define TOO_FAR 4096
  294. // Matches of length 3 are discarded if their distance exceeds TOO_FAR
  295. #define EQUAL 0
  296. // result of memcmp for equal strings
  297. // ===========================================================================
  298. // Local data used by the "longest match" routines.
  299. #define H_SHIFT ((HASH_BITS+MIN_MATCH-1)/MIN_MATCH)
  300. // Number of bits by which ins_h and del_h must be shifted at each
  301. // input step. It must be such that after MIN_MATCH steps, the oldest
  302. // byte no longer takes part in the hash key, that is:
  303. // H_SHIFT * MIN_MATCH >= HASH_BITS
  304. #define max_insert_length max_lazy_match
  305. // Insert new strings in the hash table only if the match length
  306. // is not greater than this length. This saves time but degrades compression.
  307. // max_insert_length is used only for compression levels <= 3.
  308. const int extra_lbits[LENGTH_CODES] // extra bits for each length code
  309. = {0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0};
  310. const int extra_dbits[D_CODES] // extra bits for each distance code
  311. = {0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13};
  312. const int extra_blbits[BL_CODES]// extra bits for each bit length code
  313. = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7};
  314. const uch bl_order[BL_CODES] = {16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15};
  315. // The lengths of the bit length codes are sent in order of decreasing
  316. // probability, to avoid transmitting the lengths for unused bit length codes.
  317. typedef struct config {
  318. ush good_length; // reduce lazy search above this match length
  319. ush max_lazy; // do not perform lazy search above this match length
  320. ush nice_length; // quit search above this match length
  321. ush max_chain;
  322. } config;
  323. // Values for max_lazy_match, good_match, nice_match and max_chain_length,
  324. // depending on the desired pack level (0..9). The values given below have
  325. // been tuned to exclude worst case performance for pathological files.
  326. // Better values may be found for specific files.
  327. //
  328. const config configuration_table[10] = {
  329. // good lazy nice chain
  330. {0, 0, 0, 0}, // 0 store only
  331. {4, 4, 8, 4}, // 1 maximum speed, no lazy matches
  332. {4, 5, 16, 8}, // 2
  333. {4, 6, 32, 32}, // 3
  334. {4, 4, 16, 16}, // 4 lazy matches */
  335. {8, 16, 32, 32}, // 5
  336. {8, 16, 128, 128}, // 6
  337. {8, 32, 128, 256}, // 7
  338. {32, 128, 258, 1024}, // 8
  339. {32, 258, 258, 4096}};// 9 maximum compression */
  340. // Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4
  341. // For deflate_fast() (levels <= 3) good is ignored and lazy has a different meaning.
  342. // Data structure describing a single value and its code string.
  343. typedef struct ct_data {
  344. union {
  345. ush freq; // frequency count
  346. ush code; // bit string
  347. } fc;
  348. union {
  349. ush dad; // father node in Huffman tree
  350. ush len; // length of bit string
  351. } dl;
  352. } ct_data;
  353. typedef struct tree_desc
  354. {
  355. ct_data *dyn_tree; // the dynamic tree
  356. ct_data *static_tree; // corresponding static tree or NULL
  357. const int *extra_bits; // extra bits for each code or NULL
  358. int extra_base; // base index for extra_bits
  359. int elems; // max number of elements in the tree
  360. int max_length; // max bit length for the codes
  361. int max_code; // largest code with non zero frequency
  362. } tree_desc;
  363. class TTreeState
  364. {
  365. public:
  366. TTreeState();
  367. ct_data dyn_ltree[HEAP_SIZE]; // literal and length tree
  368. ct_data dyn_dtree[2*D_CODES+1]; // distance tree
  369. ct_data static_ltree[L_CODES+2]; // the static literal tree...
  370. // ... Since the bit lengths are imposed, there is no need for the L_CODES
  371. // extra codes used during heap construction. However the codes 286 and 287
  372. // are needed to build a canonical tree (see ct_init below).
  373. ct_data static_dtree[D_CODES]; // the static distance tree...
  374. // ... (Actually a trivial tree since all codes use 5 bits.)
  375. ct_data bl_tree[2*BL_CODES+1]; // Huffman tree for the bit lengths
  376. tree_desc l_desc;
  377. tree_desc d_desc;
  378. tree_desc bl_desc;
  379. ush bl_count[MAX_BITS+1]; // number of codes at each bit length for an optimal tree
  380. int heap[2*L_CODES+1]; // heap used to build the Huffman trees
  381. int heap_len; // number of elements in the heap
  382. int heap_max; // element of largest frequency
  383. // The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used.
  384. // The same heap array is used to build all trees.
  385. uch depth[2*L_CODES+1];
  386. // Depth of each subtree used as tie breaker for trees of equal frequency
  387. uch length_code[MAX_MATCH-MIN_MATCH+1];
  388. // length code for each normalized match length (0 == MIN_MATCH)
  389. uch dist_code[512];
  390. // distance codes. The first 256 values correspond to the distances
  391. // 3 .. 258, the last 256 values correspond to the top 8 bits of
  392. // the 15 bit distances.
  393. int base_length[LENGTH_CODES];
  394. // First normalized length for each code (0 = MIN_MATCH)
  395. int base_dist[D_CODES];
  396. // First normalized distance for each code (0 = distance of 1)
  397. uch far l_buf[LIT_BUFSIZE]; // buffer for literals/lengths
  398. ush far d_buf[DIST_BUFSIZE]; // buffer for distances
  399. uch flag_buf[(LIT_BUFSIZE/8)];
  400. // flag_buf is a bit array distinguishing literals from lengths in
  401. // l_buf, and thus indicating the presence or absence of a distance.
  402. unsigned last_lit; // running index in l_buf
  403. unsigned last_dist; // running index in d_buf
  404. unsigned last_flags; // running index in flag_buf
  405. uch flags; // current flags not yet saved in flag_buf
  406. uch flag_bit; // current bit used in flags
  407. // bits are filled in flags starting at bit 0 (least significant).
  408. // Note: these flags are overkill in the current code since we don't
  409. // take advantage of DIST_BUFSIZE == LIT_BUFSIZE.
  410. ulg opt_len; // bit length of current block with optimal trees
  411. ulg static_len; // bit length of current block with static trees
  412. ulg cmpr_bytelen; // total byte length of compressed file
  413. ulg cmpr_len_bits; // number of bits past 'cmpr_bytelen'
  414. ulg input_len; // total byte length of input file
  415. // input_len is for debugging only since we can get it by other means.
  416. ush *file_type; // pointer to UNKNOWN, BINARY or ASCII
  417. // int *file_method; // pointer to DEFLATE or STORE
  418. };
  419. TTreeState::TTreeState()
  420. {
  421. tree_desc a = {dyn_ltree, static_ltree, extra_lbits, LITERALS+1, L_CODES, MAX_BITS, 0}; l_desc = a;
  422. tree_desc b = {dyn_dtree, static_dtree, extra_dbits, 0, D_CODES, MAX_BITS, 0}; d_desc = b;
  423. tree_desc c = {bl_tree, NULL, extra_blbits, 0, BL_CODES, MAX_BL_BITS, 0}; bl_desc = c;
  424. last_lit = 0;
  425. last_dist = 0;
  426. last_flags = 0;
  427. memset(dyn_ltree, 0, sizeof(dyn_ltree));
  428. memset(dyn_dtree, 0, sizeof(dyn_dtree));
  429. memset(static_ltree, 0, sizeof(static_ltree));
  430. memset(static_dtree, 0, sizeof(static_dtree));
  431. memset(bl_tree, 0, sizeof(bl_tree));
  432. memset(bl_count, 0, sizeof(bl_count));
  433. memset(heap, 0, sizeof(heap));
  434. heap_len = 0;
  435. heap_max = 0;
  436. memset(depth, 0, sizeof(depth));
  437. memset(length_code, 0, sizeof(length_code));
  438. memset(dist_code, 0, sizeof(dist_code));
  439. memset(base_length, 0, sizeof(base_length));
  440. memset(base_dist, 0, sizeof(base_dist));
  441. memset(l_buf, 0, sizeof(l_buf));
  442. memset(d_buf, 0, sizeof(d_buf));
  443. memset(flag_buf, 0, sizeof(flag_buf));
  444. last_lit = 0;
  445. last_dist = 0;
  446. last_flags = 0;
  447. flags = 0;
  448. flag_bit = 0;
  449. opt_len = 0;
  450. static_len = 0;
  451. cmpr_bytelen = 0;
  452. cmpr_len_bits = 0;
  453. input_len = 0;
  454. file_type = 0;
  455. }
  456. class TBitState
  457. {
  458. public:
  459. TBitState()
  460. {
  461. flush_flg = 0;
  462. bi_buf = 0;
  463. bi_valid = 0;
  464. out_buf = 0;
  465. out_offset = 0;
  466. out_size = 0;
  467. bits_sent = 0;
  468. }
  469. int flush_flg;
  470. //
  471. unsigned bi_buf;
  472. // Output buffer. bits are inserted starting at the bottom (least significant
  473. // bits). The width of bi_buf must be at least 16 bits.
  474. int bi_valid;
  475. // Number of valid bits in bi_buf. All bits above the last valid bit
  476. // are always zero.
  477. char *out_buf;
  478. // Current output buffer.
  479. unsigned out_offset;
  480. // Current offset in output buffer.
  481. // On 16 bit machines, the buffer is limited to 64K.
  482. unsigned out_size;
  483. // Size of current output buffer
  484. ulg bits_sent; // bit length of the compressed data only needed for debugging???
  485. };
  486. class TDeflateState
  487. {
  488. public:
  489. TDeflateState()
  490. {
  491. memset(window, 0, sizeof(window));
  492. memset(prev, 0, sizeof(prev));
  493. memset(head, 0, sizeof(head));
  494. window_size = 0;
  495. block_start = 0;
  496. sliding = 0;
  497. ins_h = 0;
  498. prev_length = 0;
  499. strstart = 0;
  500. match_start = 0;
  501. eofile = 0;
  502. lookahead = 0;
  503. max_chain_length = 0;
  504. max_lazy_match = 0;
  505. good_match = 0;
  506. nice_match = 0;
  507. }
  508. uch window[2L*WSIZE];
  509. // Sliding window. Input bytes are read into the second half of the window,
  510. // and move to the first half later to keep a dictionary of at least WSIZE
  511. // bytes. With this organization, matches are limited to a distance of
  512. // WSIZE-MAX_MATCH bytes, but this ensures that IO is always
  513. // performed with a length multiple of the block size. Also, it limits
  514. // the window size to 64K, which is quite useful on MSDOS.
  515. // To do: limit the window size to WSIZE+CBSZ if SMALL_MEM (the code would
  516. // be less efficient since the data would have to be copied WSIZE/CBSZ times)
  517. Pos prev[WSIZE];
  518. // Link to older string with same hash index. To limit the size of this
  519. // array to 64K, this link is maintained only for the last 32K strings.
  520. // An index in this array is thus a window index modulo 32K.
  521. Pos head[HASH_SIZE];
  522. // Heads of the hash chains or NIL. If your compiler thinks that
  523. // HASH_SIZE is a dynamic value, recompile with -DDYN_ALLOC.
  524. ulg window_size;
  525. // window size, 2*WSIZE except for MMAP or BIG_MEM, where it is the
  526. // input file length plus MIN_LOOKAHEAD.
  527. long block_start;
  528. // window position at the beginning of the current output block. Gets
  529. // negative when the window is moved backwards.
  530. int sliding;
  531. // Set to false when the input file is already in memory
  532. unsigned ins_h; // hash index of string to be inserted
  533. unsigned int prev_length;
  534. // Length of the best match at previous step. Matches not greater than this
  535. // are discarded. This is used in the lazy match evaluation.
  536. unsigned strstart; // start of string to insert
  537. unsigned match_start; // start of matching string
  538. int eofile; // flag set at end of input file
  539. unsigned lookahead; // number of valid bytes ahead in window
  540. unsigned max_chain_length;
  541. // To speed up deflation, hash chains are never searched beyond this length.
  542. // A higher limit improves compression ratio but degrades the speed.
  543. unsigned int max_lazy_match;
  544. // Attempt to find a better match only when the current match is strictly
  545. // smaller than this value. This mechanism is used only for compression
  546. // levels >= 4.
  547. unsigned good_match;
  548. // Use a faster search when the previous match is longer than this
  549. int nice_match; // Stop searching when current match exceeds this
  550. };
  551. typedef struct iztimes {
  552. time_t atime,mtime,ctime;
  553. } iztimes; // access, modify, create times
  554. typedef struct zlist {
  555. ush vem, ver, flg, how; // See central header in zipfile.c for what vem..off are
  556. ulg tim, crc, siz, len;
  557. extent nam, ext, cext, com; // offset of ext must be >= LOCHEAD
  558. ush dsk, att, lflg; // offset of lflg must be >= LOCHEAD
  559. ulg atx, off;
  560. char name[MAX_PATH]; // File name in zip file
  561. char *extra; // Extra field (set only if ext != 0)
  562. char *cextra; // Extra in central (set only if cext != 0)
  563. char *comment; // Comment (set only if com != 0)
  564. char iname[MAX_PATH]; // Internal file name after cleanup
  565. char zname[MAX_PATH]; // External version of internal name
  566. int mark; // Marker for files to operate on
  567. int trash; // Marker for files to delete
  568. int dosflag; // Set to force MSDOS file attributes
  569. struct zlist far *nxt; // Pointer to next header in list
  570. } TZipFileInfo;
  571. class TState;
  572. typedef unsigned (*READFUNC)(TState &state, char *buf,unsigned size);
  573. typedef unsigned (*FLUSHFUNC)(void *param, const char *buf, unsigned *size);
  574. typedef unsigned (*WRITEFUNC)(void *param, const char *buf, unsigned size);
  575. class TState
  576. {
  577. public:
  578. TState() //+++1.2
  579. {
  580. param = 0;
  581. level = 0;
  582. seekable = FALSE;
  583. readfunc = 0;
  584. flush_outbuf = 0;
  585. err = 0;
  586. }
  587. void *param;
  588. int level;
  589. bool seekable;
  590. READFUNC readfunc;
  591. FLUSHFUNC flush_outbuf;
  592. TTreeState ts;
  593. TBitState bs;
  594. TDeflateState ds;
  595. const char *err;
  596. };
  597. void Assert(TState &state,bool cond, const char *msg)
  598. { if (cond) return;
  599. state.err=msg;
  600. }
  601. void __cdecl Trace(const char *x, ...) {va_list paramList; va_start(paramList, x); paramList; va_end(paramList);}
  602. void __cdecl Tracec(bool ,const char *x, ...) {va_list paramList; va_start(paramList, x); paramList; va_end(paramList);}
  603. // ===========================================================================
  604. // Local (static) routines in this file.
  605. //
  606. void init_block (TState &);
  607. void pqdownheap (TState &,ct_data *tree, int k);
  608. void gen_bitlen (TState &,tree_desc *desc);
  609. void gen_codes (TState &state,ct_data *tree, int max_code);
  610. void build_tree (TState &,tree_desc *desc);
  611. void scan_tree (TState &,ct_data *tree, int max_code);
  612. void send_tree (TState &state,ct_data *tree, int max_code);
  613. int build_bl_tree (TState &);
  614. void send_all_trees (TState &state,int lcodes, int dcodes, int blcodes);
  615. void compress_block (TState &state,ct_data *ltree, ct_data *dtree);
  616. void set_file_type (TState &);
  617. void send_bits (TState &state, int value, int length);
  618. unsigned bi_reverse (unsigned code, int len);
  619. void bi_windup (TState &state);
  620. void copy_block (TState &state,char *buf, unsigned len, int header);
  621. #define send_code(state, c, tree) send_bits(state, tree[c].fc.code, tree[c].dl.len)
  622. // Send a code of the given tree. c and tree must not have side effects
  623. // alternatively...
  624. //#define send_code(state, c, tree)
  625. // { if (state.verbose>1) fprintf(stderr,"\ncd %3d ",(c));
  626. // send_bits(state, tree[c].fc.code, tree[c].dl.len); }
  627. #define d_code(dist) ((dist) < 256 ? state.ts.dist_code[dist] : state.ts.dist_code[256+((dist)>>7)])
  628. // Mapping from a distance to a distance code. dist is the distance - 1 and
  629. // must not have side effects. dist_code[256] and dist_code[257] are never used.
  630. #define Max(a,b) (a >= b ? a : b)
  631. /* the arguments must not have side effects */
  632. /* ===========================================================================
  633. * Allocate the match buffer, initialize the various tables and save the
  634. * location of the internal file attribute (ascii/binary) and method
  635. * (DEFLATE/STORE).
  636. */
  637. void ct_init(TState &state, ush *attr)
  638. {
  639. int n; /* iterates over tree elements */
  640. int bits; /* bit counter */
  641. int length; /* length value */
  642. int code; /* code value */
  643. int dist; /* distance index */
  644. state.ts.file_type = attr;
  645. //state.ts.file_method = method;
  646. state.ts.cmpr_bytelen = state.ts.cmpr_len_bits = 0L;
  647. state.ts.input_len = 0L;
  648. if (state.ts.static_dtree[0].dl.len != 0) return; /* ct_init already called */
  649. /* Initialize the mapping length (0..255) -> length code (0..28) */
  650. length = 0;
  651. for (code = 0; code < LENGTH_CODES-1; code++) {
  652. state.ts.base_length[code] = length;
  653. for (n = 0; n < (1<<extra_lbits[code]); n++) {
  654. state.ts.length_code[length++] = (uch)code;
  655. }
  656. }
  657. Assert(state,length == 256, "ct_init: length != 256");
  658. /* Note that the length 255 (match length 258) can be represented
  659. * in two different ways: code 284 + 5 bits or code 285, so we
  660. * overwrite length_code[255] to use the best encoding:
  661. */
  662. state.ts.length_code[length-1] = (uch)code;
  663. /* Initialize the mapping dist (0..32K) -> dist code (0..29) */
  664. dist = 0;
  665. for (code = 0 ; code < 16; code++) {
  666. state.ts.base_dist[code] = dist;
  667. for (n = 0; n < (1<<extra_dbits[code]); n++) {
  668. state.ts.dist_code[dist++] = (uch)code;
  669. }
  670. }
  671. Assert(state,dist == 256, "ct_init: dist != 256");
  672. dist >>= 7; /* from now on, all distances are divided by 128 */
  673. for ( ; code < D_CODES; code++) {
  674. state.ts.base_dist[code] = dist << 7;
  675. for (n = 0; n < (1<<(extra_dbits[code]-7)); n++) {
  676. state.ts.dist_code[256 + dist++] = (uch)code;
  677. }
  678. }
  679. Assert(state,dist == 256, "ct_init: 256+dist != 512");
  680. /* Construct the codes of the static literal tree */
  681. for (bits = 0; bits <= MAX_BITS; bits++) state.ts.bl_count[bits] = 0;
  682. n = 0;
  683. while (n <= 143) state.ts.static_ltree[n++].dl.len = 8, state.ts.bl_count[8]++;
  684. while (n <= 255) state.ts.static_ltree[n++].dl.len = 9, state.ts.bl_count[9]++;
  685. while (n <= 279) state.ts.static_ltree[n++].dl.len = 7, state.ts.bl_count[7]++;
  686. while (n <= 287) state.ts.static_ltree[n++].dl.len = 8, state.ts.bl_count[8]++;
  687. /* fc.codes 286 and 287 do not exist, but we must include them in the
  688. * tree construction to get a canonical Huffman tree (longest code
  689. * all ones)
  690. */
  691. gen_codes(state,(ct_data *)state.ts.static_ltree, L_CODES+1);
  692. /* The static distance tree is trivial: */
  693. for (n = 0; n < D_CODES; n++) {
  694. state.ts.static_dtree[n].dl.len = 5;
  695. state.ts.static_dtree[n].fc.code = (ush)bi_reverse(n, 5);
  696. }
  697. /* Initialize the first block of the first file: */
  698. init_block(state);
  699. }
  700. /* ===========================================================================
  701. * Initialize a new block.
  702. */
  703. void init_block(TState &state)
  704. {
  705. int n; /* iterates over tree elements */
  706. /* Initialize the trees. */
  707. for (n = 0; n < L_CODES; n++) state.ts.dyn_ltree[n].fc.freq = 0;
  708. for (n = 0; n < D_CODES; n++) state.ts.dyn_dtree[n].fc.freq = 0;
  709. for (n = 0; n < BL_CODES; n++) state.ts.bl_tree[n].fc.freq = 0;
  710. state.ts.dyn_ltree[END_BLOCK].fc.freq = 1;
  711. state.ts.opt_len = state.ts.static_len = 0L;
  712. state.ts.last_lit = state.ts.last_dist = state.ts.last_flags = 0;
  713. state.ts.flags = 0; state.ts.flag_bit = 1;
  714. }
  715. #define SMALLEST 1
  716. /* Index within the heap array of least frequent node in the Huffman tree */
  717. /* ===========================================================================
  718. * Remove the smallest element from the heap and recreate the heap with
  719. * one less element. Updates heap and heap_len.
  720. */
  721. #define pqremove(tree, top) \
  722. {\
  723. top = state.ts.heap[SMALLEST]; \
  724. state.ts.heap[SMALLEST] = state.ts.heap[state.ts.heap_len--]; \
  725. pqdownheap(state,tree, SMALLEST); \
  726. }
  727. /* ===========================================================================
  728. * Compares to subtrees, using the tree depth as tie breaker when
  729. * the subtrees have equal frequency. This minimizes the worst case length.
  730. */
  731. #define smaller(tree, n, m) \
  732. (tree[n].fc.freq < tree[m].fc.freq || \
  733. (tree[n].fc.freq == tree[m].fc.freq && state.ts.depth[n] <= state.ts.depth[m]))
  734. /* ===========================================================================
  735. * Restore the heap property by moving down the tree starting at node k,
  736. * exchanging a node with the smallest of its two sons if necessary, stopping
  737. * when the heap property is re-established (each father smaller than its
  738. * two sons).
  739. */
  740. void pqdownheap(TState &state,ct_data *tree, int k)
  741. {
  742. int v = state.ts.heap[k];
  743. int j = k << 1; /* left son of k */
  744. int htemp; /* required because of bug in SASC compiler */
  745. while (j <= state.ts.heap_len) {
  746. /* Set j to the smallest of the two sons: */
  747. if (j < state.ts.heap_len && smaller(tree, state.ts.heap[j+1], state.ts.heap[j])) j++;
  748. /* Exit if v is smaller than both sons */
  749. htemp = state.ts.heap[j];
  750. if (smaller(tree, v, htemp)) break;
  751. /* Exchange v with the smallest son */
  752. state.ts.heap[k] = htemp;
  753. k = j;
  754. /* And continue down the tree, setting j to the left son of k */
  755. j <<= 1;
  756. }
  757. state.ts.heap[k] = v;
  758. }
  759. /* ===========================================================================
  760. * Compute the optimal bit lengths for a tree and update the total bit length
  761. * for the current block.
  762. * IN assertion: the fields freq and dad are set, heap[heap_max] and
  763. * above are the tree nodes sorted by increasing frequency.
  764. * OUT assertions: the field len is set to the optimal bit length, the
  765. * array bl_count contains the frequencies for each bit length.
  766. * The length opt_len is updated; static_len is also updated if stree is
  767. * not null.
  768. */
  769. void gen_bitlen(TState &state,tree_desc *desc)
  770. {
  771. ct_data *tree = desc->dyn_tree;
  772. const int *extra = desc->extra_bits;
  773. int base = desc->extra_base;
  774. int max_code = desc->max_code;
  775. int max_length = desc->max_length;
  776. ct_data *stree = desc->static_tree;
  777. int h; /* heap index */
  778. int n, m; /* iterate over the tree elements */
  779. int bits; /* bit length */
  780. int xbits; /* extra bits */
  781. ush f; /* frequency */
  782. int overflow = 0; /* number of elements with bit length too large */
  783. for (bits = 0; bits <= MAX_BITS; bits++) state.ts.bl_count[bits] = 0;
  784. /* In a first pass, compute the optimal bit lengths (which may
  785. * overflow in the case of the bit length tree).
  786. */
  787. tree[state.ts.heap[state.ts.heap_max]].dl.len = 0; /* root of the heap */
  788. for (h = state.ts.heap_max+1; h < HEAP_SIZE; h++) {
  789. n = state.ts.heap[h];
  790. bits = tree[tree[n].dl.dad].dl.len + 1;
  791. if (bits > max_length) bits = max_length, overflow++;
  792. tree[n].dl.len = (ush)bits;
  793. /* We overwrite tree[n].dl.dad which is no longer needed */
  794. if (n > max_code) continue; /* not a leaf node */
  795. state.ts.bl_count[bits]++;
  796. xbits = 0;
  797. if (n >= base) xbits = extra[n-base];
  798. f = tree[n].fc.freq;
  799. state.ts.opt_len += (ulg)f * (bits + xbits);
  800. if (stree) state.ts.static_len += (ulg)f * (stree[n].dl.len + xbits);
  801. }
  802. if (overflow == 0) return;
  803. Trace("\nbit length overflow\n");
  804. /* This happens for example on obj2 and pic of the Calgary corpus */
  805. /* Find the first bit length which could increase: */
  806. do {
  807. bits = max_length-1;
  808. while (state.ts.bl_count[bits] == 0) bits--;
  809. state.ts.bl_count[bits]--; /* move one leaf down the tree */
  810. state.ts.bl_count[bits+1] += (ush)2; /* move one overflow item as its brother */
  811. state.ts.bl_count[max_length]--;
  812. /* The brother of the overflow item also moves one step up,
  813. * but this does not affect bl_count[max_length]
  814. */
  815. overflow -= 2;
  816. } while (overflow > 0);
  817. /* Now recompute all bit lengths, scanning in increasing frequency.
  818. * h is still equal to HEAP_SIZE. (It is simpler to reconstruct all
  819. * lengths instead of fixing only the wrong ones. This idea is taken
  820. * from 'ar' written by Haruhiko Okumura.)
  821. */
  822. for (bits = max_length; bits != 0; bits--) {
  823. n = state.ts.bl_count[bits];
  824. while (n != 0) {
  825. m = state.ts.heap[--h];
  826. if (m > max_code) continue;
  827. if (tree[m].dl.len != (ush)bits) {
  828. Trace("code %d bits %d->%d\n", m, tree[m].dl.len, bits);
  829. state.ts.opt_len += ((long)bits-(long)tree[m].dl.len)*(long)tree[m].fc.freq;
  830. tree[m].dl.len = (ush)bits;
  831. }
  832. n--;
  833. }
  834. }
  835. }
  836. /* ===========================================================================
  837. * Generate the codes for a given tree and bit counts (which need not be
  838. * optimal).
  839. * IN assertion: the array bl_count contains the bit length statistics for
  840. * the given tree and the field len is set for all tree elements.
  841. * OUT assertion: the field code is set for all tree elements of non
  842. * zero code length.
  843. */
  844. void gen_codes (TState &state, ct_data *tree, int max_code)
  845. {
  846. ush next_code[MAX_BITS+1]; /* next code value for each bit length */
  847. ush code = 0; /* running code value */
  848. int bits; /* bit index */
  849. int n; /* code index */
  850. /* The distribution counts are first used to generate the code values
  851. * without bit reversal.
  852. */
  853. for (bits = 1; bits <= MAX_BITS; bits++) {
  854. next_code[bits] = code = (ush)((code + state.ts.bl_count[bits-1]) << 1);
  855. }
  856. /* Check that the bit counts in bl_count are consistent. The last code
  857. * must be all ones.
  858. */
  859. Assert(state,code + state.ts.bl_count[MAX_BITS]-1 == (1<< ((ush) MAX_BITS)) - 1,
  860. "inconsistent bit counts");
  861. Trace("\ngen_codes: max_code %d ", max_code);
  862. for (n = 0; n <= max_code; n++) {
  863. int len = tree[n].dl.len;
  864. if (len == 0) continue;
  865. /* Now reverse the bits */
  866. tree[n].fc.code = (ush)bi_reverse(next_code[len]++, len);
  867. //Tracec(tree != state.ts.static_ltree, "\nn %3d %c l %2d c %4x (%x) ", n, (isgraph(n) ? n : ' '), len, tree[n].fc.code, next_code[len]-1);
  868. }
  869. }
  870. /* ===========================================================================
  871. * Construct one Huffman tree and assigns the code bit strings and lengths.
  872. * Update the total bit length for the current block.
  873. * IN assertion: the field freq is set for all tree elements.
  874. * OUT assertions: the fields len and code are set to the optimal bit length
  875. * and corresponding code. The length opt_len is updated; static_len is
  876. * also updated if stree is not null. The field max_code is set.
  877. */
  878. void build_tree(TState &state,tree_desc *desc)
  879. {
  880. ct_data *tree = desc->dyn_tree;
  881. ct_data *stree = desc->static_tree;
  882. int elems = desc->elems;
  883. int n, m; /* iterate over heap elements */
  884. int max_code = -1; /* largest code with non zero frequency */
  885. int node = elems; /* next internal node of the tree */
  886. /* Construct the initial heap, with least frequent element in
  887. * heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n+1].
  888. * heap[0] is not used.
  889. */
  890. state.ts.heap_len = 0, state.ts.heap_max = HEAP_SIZE;
  891. for (n = 0; n < elems; n++) {
  892. if (tree[n].fc.freq != 0) {
  893. state.ts.heap[++state.ts.heap_len] = max_code = n;
  894. state.ts.depth[n] = 0;
  895. } else {
  896. tree[n].dl.len = 0;
  897. }
  898. }
  899. /* The pkzip format requires that at least one distance code exists,
  900. * and that at least one bit should be sent even if there is only one
  901. * possible code. So to avoid special checks later on we force at least
  902. * two codes of non zero frequency.
  903. */
  904. while (state.ts.heap_len < 2) {
  905. int newcp = state.ts.heap[++state.ts.heap_len] = (max_code < 2 ? ++max_code : 0);
  906. tree[newcp].fc.freq = 1;
  907. state.ts.depth[newcp] = 0;
  908. state.ts.opt_len--; if (stree) state.ts.static_len -= stree[newcp].dl.len;
  909. /* new is 0 or 1 so it does not have extra bits */
  910. }
  911. desc->max_code = max_code;
  912. /* The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree,
  913. * establish sub-heaps of increasing lengths:
  914. */
  915. for (n = state.ts.heap_len/2; n >= 1; n--) pqdownheap(state,tree, n);
  916. /* Construct the Huffman tree by repeatedly combining the least two
  917. * frequent nodes.
  918. */
  919. do {
  920. pqremove(tree, n); /* n = node of least frequency */
  921. m = state.ts.heap[SMALLEST]; /* m = node of next least frequency */
  922. state.ts.heap[--state.ts.heap_max] = n; /* keep the nodes sorted by frequency */
  923. state.ts.heap[--state.ts.heap_max] = m;
  924. /* Create a new node father of n and m */
  925. tree[node].fc.freq = (ush)(tree[n].fc.freq + tree[m].fc.freq);
  926. state.ts.depth[node] = (uch) (Max(state.ts.depth[n], state.ts.depth[m]) + 1);
  927. tree[n].dl.dad = tree[m].dl.dad = (ush)node;
  928. /* and insert the new node in the heap */
  929. state.ts.heap[SMALLEST] = node++;
  930. pqdownheap(state,tree, SMALLEST);
  931. } while (state.ts.heap_len >= 2);
  932. state.ts.heap[--state.ts.heap_max] = state.ts.heap[SMALLEST];
  933. /* At this point, the fields freq and dad are set. We can now
  934. * generate the bit lengths.
  935. */
  936. gen_bitlen(state,(tree_desc *)desc);
  937. /* The field len is now set, we can generate the bit codes */
  938. gen_codes (state,(ct_data *)tree, max_code);
  939. }
  940. /* ===========================================================================
  941. * Scan a literal or distance tree to determine the frequencies of the codes
  942. * in the bit length tree. Updates opt_len to take into account the repeat
  943. * counts. (The contribution of the bit length codes will be added later
  944. * during the construction of bl_tree.)
  945. */
  946. void scan_tree (TState &state,ct_data *tree, int max_code)
  947. {
  948. int n; /* iterates over all tree elements */
  949. int prevlen = -1; /* last emitted length */
  950. int curlen; /* length of current code */
  951. int nextlen = tree[0].dl.len; /* length of next code */
  952. int count = 0; /* repeat count of the current code */
  953. int max_count = 7; /* max repeat count */
  954. int min_count = 4; /* min repeat count */
  955. if (nextlen == 0) max_count = 138, min_count = 3;
  956. tree[max_code+1].dl.len = (ush)-1; /* guard */
  957. for (n = 0; n <= max_code; n++) {
  958. curlen = nextlen; nextlen = tree[n+1].dl.len;
  959. if (++count < max_count && curlen == nextlen) {
  960. continue;
  961. } else if (count < min_count) {
  962. state.ts.bl_tree[curlen].fc.freq = (ush)(state.ts.bl_tree[curlen].fc.freq + count);
  963. } else if (curlen != 0) {
  964. if (curlen != prevlen) state.ts.bl_tree[curlen].fc.freq++;
  965. state.ts.bl_tree[REP_3_6].fc.freq++;
  966. } else if (count <= 10) {
  967. state.ts.bl_tree[REPZ_3_10].fc.freq++;
  968. } else {
  969. state.ts.bl_tree[REPZ_11_138].fc.freq++;
  970. }
  971. count = 0; prevlen = curlen;
  972. if (nextlen == 0) {
  973. max_count = 138, min_count = 3;
  974. } else if (curlen == nextlen) {
  975. max_count = 6, min_count = 3;
  976. } else {
  977. max_count = 7, min_count = 4;
  978. }
  979. }
  980. }
  981. /* ===========================================================================
  982. * Send a literal or distance tree in compressed form, using the codes in
  983. * bl_tree.
  984. */
  985. void send_tree (TState &state, ct_data *tree, int max_code)
  986. {
  987. int n; /* iterates over all tree elements */
  988. int prevlen = -1; /* last emitted length */
  989. int curlen; /* length of current code */
  990. int nextlen = tree[0].dl.len; /* length of next code */
  991. int count = 0; /* repeat count of the current code */
  992. int max_count = 7; /* max repeat count */
  993. int min_count = 4; /* min repeat count */
  994. /* tree[max_code+1].dl.len = -1; */ /* guard already set */
  995. if (nextlen == 0) max_count = 138, min_count = 3;
  996. for (n = 0; n <= max_code; n++) {
  997. curlen = nextlen; nextlen = tree[n+1].dl.len;
  998. if (++count < max_count && curlen == nextlen) {
  999. continue;
  1000. } else if (count < min_count) {
  1001. do { send_code(state, curlen, state.ts.bl_tree); } while (--count != 0);
  1002. } else if (curlen != 0) {
  1003. if (curlen != prevlen) {
  1004. send_code(state, curlen, state.ts.bl_tree); count--;
  1005. }
  1006. Assert(state,count >= 3 && count <= 6, " 3_6?");
  1007. send_code(state,REP_3_6, state.ts.bl_tree); send_bits(state,count-3, 2);
  1008. } else if (count <= 10) {
  1009. send_code(state,REPZ_3_10, state.ts.bl_tree); send_bits(state,count-3, 3);
  1010. } else {
  1011. send_code(state,REPZ_11_138, state.ts.bl_tree); send_bits(state,count-11, 7);
  1012. }
  1013. count = 0; prevlen = curlen;
  1014. if (nextlen == 0) {
  1015. max_count = 138, min_count = 3;
  1016. } else if (curlen == nextlen) {
  1017. max_count = 6, min_count = 3;
  1018. } else {
  1019. max_count = 7, min_count = 4;
  1020. }
  1021. }
  1022. }
  1023. /* ===========================================================================
  1024. * Construct the Huffman tree for the bit lengths and return the index in
  1025. * bl_order of the last bit length code to send.
  1026. */
  1027. int build_bl_tree(TState &state)
  1028. {
  1029. int max_blindex; /* index of last bit length code of non zero freq */
  1030. /* Determine the bit length frequencies for literal and distance trees */
  1031. scan_tree(state,(ct_data *)state.ts.dyn_ltree, state.ts.l_desc.max_code);
  1032. scan_tree(state,(ct_data *)state.ts.dyn_dtree, state.ts.d_desc.max_code);
  1033. /* Build the bit length tree: */
  1034. build_tree(state,(tree_desc *)(&state.ts.bl_desc));
  1035. /* opt_len now includes the length of the tree representations, except
  1036. * the lengths of the bit lengths codes and the 5+5+4 bits for the counts.
  1037. */
  1038. /* Determine the number of bit length codes to send. The pkzip format
  1039. * requires that at least 4 bit length codes be sent. (appnote.txt says
  1040. * 3 but the actual value used is 4.)
  1041. */
  1042. for (max_blindex = BL_CODES-1; max_blindex >= 3; max_blindex--) {
  1043. if (state.ts.bl_tree[bl_order[max_blindex]].dl.len != 0) break;
  1044. }
  1045. /* Update opt_len to include the bit length tree and counts */
  1046. state.ts.opt_len += 3*(max_blindex+1) + 5+5+4;
  1047. Trace("\ndyn trees: dyn %ld, stat %ld", state.ts.opt_len, state.ts.static_len);
  1048. return max_blindex;
  1049. }
  1050. /* ===========================================================================
  1051. * Send the header for a block using dynamic Huffman trees: the counts, the
  1052. * lengths of the bit length codes, the literal tree and the distance tree.
  1053. * IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4.
  1054. */
  1055. void send_all_trees(TState &state,int lcodes, int dcodes, int blcodes)
  1056. {
  1057. int rank; /* index in bl_order */
  1058. Assert(state,lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes");
  1059. Assert(state,lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_CODES,
  1060. "too many codes");
  1061. Trace("\nbl counts: ");
  1062. send_bits(state,lcodes-257, 5);
  1063. /* not +255 as stated in appnote.txt 1.93a or -256 in 2.04c */
  1064. send_bits(state,dcodes-1, 5);
  1065. send_bits(state,blcodes-4, 4); /* not -3 as stated in appnote.txt */
  1066. for (rank = 0; rank < blcodes; rank++) {
  1067. Trace("\nbl code %2d ", bl_order[rank]);
  1068. send_bits(state,state.ts.bl_tree[bl_order[rank]].dl.len, 3);
  1069. }
  1070. Trace("\nbl tree: sent %ld", state.bs.bits_sent);
  1071. send_tree(state,(ct_data *)state.ts.dyn_ltree, lcodes-1); /* send the literal tree */
  1072. Trace("\nlit tree: sent %ld", state.bs.bits_sent);
  1073. send_tree(state,(ct_data *)state.ts.dyn_dtree, dcodes-1); /* send the distance tree */
  1074. Trace("\ndist tree: sent %ld", state.bs.bits_sent);
  1075. }
  1076. /* ===========================================================================
  1077. * Determine the best encoding for the current block: dynamic trees, static
  1078. * trees or store, and output the encoded block to the zip file. This function
  1079. * returns the total compressed length (in bytes) for the file so far.
  1080. */
  1081. ulg flush_block(TState &state,char *buf, ulg stored_len, int eof)
  1082. {
  1083. ulg opt_lenb, static_lenb; /* opt_len and static_len in bytes */
  1084. int max_blindex; /* index of last bit length code of non zero freq */
  1085. state.ts.flag_buf[state.ts.last_flags] = state.ts.flags; /* Save the flags for the last 8 items */
  1086. /* Check if the file is ascii or binary */
  1087. if (*state.ts.file_type == (ush)UNKNOWN) set_file_type(state);
  1088. /* Construct the literal and distance trees */
  1089. build_tree(state,(tree_desc *)(&state.ts.l_desc));
  1090. Trace("\nlit data: dyn %ld, stat %ld", state.ts.opt_len, state.ts.static_len);
  1091. build_tree(state,(tree_desc *)(&state.ts.d_desc));
  1092. Trace("\ndist data: dyn %ld, stat %ld", state.ts.opt_len, state.ts.static_len);
  1093. /* At this point, opt_len and static_len are the total bit lengths of
  1094. * the compressed block data, excluding the tree representations.
  1095. */
  1096. /* Build the bit length tree for the above two trees, and get the index
  1097. * in bl_order of the last bit length code to send.
  1098. */
  1099. max_blindex = build_bl_tree(state);
  1100. /* Determine the best encoding. Compute first the block length in bytes */
  1101. opt_lenb = (state.ts.opt_len+3+7)>>3;
  1102. static_lenb = (state.ts.static_len+3+7)>>3;
  1103. state.ts.input_len += stored_len; /* for debugging only */
  1104. Trace("\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u dist %u ",
  1105. opt_lenb, state.ts.opt_len, static_lenb, state.ts.static_len, stored_len,
  1106. state.ts.last_lit, state.ts.last_dist);
  1107. if (static_lenb <= opt_lenb) opt_lenb = static_lenb;
  1108. // Originally, zip allowed the file to be transformed from a compressed
  1109. // into a stored file in the case where compression failed, there
  1110. // was only one block, and it was allowed to change. I've removed this
  1111. // possibility since the code's cleaner if no changes are allowed.
  1112. //if (stored_len <= opt_lenb && eof && state.ts.cmpr_bytelen == 0L
  1113. // && state.ts.cmpr_len_bits == 0L && state.seekable)
  1114. //{ // && state.ts.file_method != NULL
  1115. // // Since LIT_BUFSIZE <= 2*WSIZE, the input data must be there:
  1116. // Assert(state,buf!=NULL,"block vanished");
  1117. // copy_block(state,buf, (unsigned)stored_len, 0); // without header
  1118. // state.ts.cmpr_bytelen = stored_len;
  1119. // Assert(state,false,"unimplemented *state.ts.file_method = STORE;");
  1120. // //*state.ts.file_method = STORE;
  1121. //}
  1122. //else
  1123. if (stored_len+4 <= opt_lenb && buf != (char*)NULL) {
  1124. /* 4: two words for the lengths */
  1125. /* The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE.
  1126. * Otherwise we can't have processed more than WSIZE input bytes since
  1127. * the last block flush, because compression would have been
  1128. * successful. If LIT_BUFSIZE <= WSIZE, it is never too late to
  1129. * transform a block into a stored block.
  1130. */
  1131. send_bits(state,(STORED_BLOCK<<1)+eof, 3); /* send block type */
  1132. state.ts.cmpr_bytelen += ((state.ts.cmpr_len_bits + 3 + 7) >> 3) + stored_len + 4;
  1133. state.ts.cmpr_len_bits = 0L;
  1134. copy_block(state,buf, (unsigned)stored_len, 1); /* with header */
  1135. }
  1136. else if (static_lenb == opt_lenb) {
  1137. send_bits(state,(STATIC_TREES<<1)+eof, 3);
  1138. compress_block(state,(ct_data *)state.ts.static_ltree, (ct_data *)state.ts.static_dtree);
  1139. state.ts.cmpr_len_bits += 3 + state.ts.static_len;
  1140. state.ts.cmpr_bytelen += state.ts.cmpr_len_bits >> 3;
  1141. state.ts.cmpr_len_bits &= 7L;
  1142. }
  1143. else {
  1144. send_bits(state,(DYN_TREES<<1)+eof, 3);
  1145. send_all_trees(state,state.ts.l_desc.max_code+1, state.ts.d_desc.max_code+1, max_blindex+1);
  1146. compress_block(state,(ct_data *)state.ts.dyn_ltree, (ct_data *)state.ts.dyn_dtree);
  1147. state.ts.cmpr_len_bits += 3 + state.ts.opt_len;
  1148. state.ts.cmpr_bytelen += state.ts.cmpr_len_bits >> 3;
  1149. state.ts.cmpr_len_bits &= 7L;
  1150. }
  1151. Assert(state,((state.ts.cmpr_bytelen << 3) + state.ts.cmpr_len_bits) == state.bs.bits_sent, "bad compressed size");
  1152. init_block(state);
  1153. if (eof) {
  1154. // Assert(state,input_len == isize, "bad input size");
  1155. bi_windup(state);
  1156. state.ts.cmpr_len_bits += 7; /* align on byte boundary */
  1157. }
  1158. Trace("\n");
  1159. return state.ts.cmpr_bytelen + (state.ts.cmpr_len_bits >> 3);
  1160. }
  1161. /* ===========================================================================
  1162. * Save the match info and tally the frequency counts. Return true if
  1163. * the current block must be flushed.
  1164. */
  1165. int ct_tally (TState &state,int dist, int lc)
  1166. {
  1167. state.ts.l_buf[state.ts.last_lit++] = (uch)lc;
  1168. if (dist == 0) {
  1169. /* lc is the unmatched char */
  1170. state.ts.dyn_ltree[lc].fc.freq++;
  1171. } else {
  1172. /* Here, lc is the match length - MIN_MATCH */
  1173. dist--; /* dist = match distance - 1 */
  1174. Assert(state,(ush)dist < (ush)MAX_DIST &&
  1175. (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) &&
  1176. (ush)d_code(dist) < (ush)D_CODES, "ct_tally: bad match");
  1177. state.ts.dyn_ltree[state.ts.length_code[lc]+LITERALS+1].fc.freq++;
  1178. state.ts.dyn_dtree[d_code(dist)].fc.freq++;
  1179. state.ts.d_buf[state.ts.last_dist++] = (ush)dist;
  1180. state.ts.flags |= state.ts.flag_bit;
  1181. }
  1182. state.ts.flag_bit <<= 1;
  1183. /* Output the flags if they fill a byte: */
  1184. if ((state.ts.last_lit & 7) == 0) {
  1185. state.ts.flag_buf[state.ts.last_flags++] = state.ts.flags;
  1186. state.ts.flags = 0, state.ts.flag_bit = 1;
  1187. }
  1188. /* Try to guess if it is profitable to stop the current block here */
  1189. if (state.level > 2 && (state.ts.last_lit & 0xfff) == 0) {
  1190. /* Compute an upper bound for the compressed length */
  1191. ulg out_length = (ulg)state.ts.last_lit*8L;
  1192. ulg in_length = (ulg)state.ds.strstart-state.ds.block_start;
  1193. int dcode;
  1194. for (dcode = 0; dcode < D_CODES; dcode++) {
  1195. out_length += (ulg)state.ts.dyn_dtree[dcode].fc.freq*(5L+extra_dbits[dcode]);
  1196. }
  1197. out_length >>= 3;
  1198. Trace("\nlast_lit %u, last_dist %u, in %ld, out ~%ld(%ld%%) ",
  1199. state.ts.last_lit, state.ts.last_dist, in_length, out_length,
  1200. 100L - out_length*100L/in_length);
  1201. if (state.ts.last_dist < state.ts.last_lit/2 && out_length < in_length/2) return 1;
  1202. }
  1203. return (state.ts.last_lit == LIT_BUFSIZE-1 || state.ts.last_dist == DIST_BUFSIZE);
  1204. /* We avoid equality with LIT_BUFSIZE because of wraparound at 64K
  1205. * on 16 bit machines and because stored blocks are restricted to
  1206. * 64K-1 bytes.
  1207. */
  1208. }
  1209. /* ===========================================================================
  1210. * Send the block data compressed using the given Huffman trees
  1211. */
  1212. void compress_block(TState &state,ct_data *ltree, ct_data *dtree)
  1213. {
  1214. unsigned dist; /* distance of matched string */
  1215. int lc; /* match length or unmatched char (if dist == 0) */
  1216. unsigned lx = 0; /* running index in l_buf */
  1217. unsigned dx = 0; /* running index in d_buf */
  1218. unsigned fx = 0; /* running index in flag_buf */
  1219. uch flag = 0; /* current flags */
  1220. unsigned code; /* the code to send */
  1221. int extra; /* number of extra bits to send */
  1222. if (state.ts.last_lit != 0) do {
  1223. if ((lx & 7) == 0) flag = state.ts.flag_buf[fx++];
  1224. lc = state.ts.l_buf[lx++];
  1225. if ((flag & 1) == 0) {
  1226. send_code(state,lc, ltree); /* send a literal byte */
  1227. } else {
  1228. /* Here, lc is the match length - MIN_MATCH */
  1229. code = state.ts.length_code[lc];
  1230. send_code(state,code+LITERALS+1, ltree); /* send the length code */
  1231. extra = extra_lbits[code];
  1232. if (extra != 0) {
  1233. lc -= state.ts.base_length[code];
  1234. send_bits(state,lc, extra); /* send the extra length bits */
  1235. }
  1236. dist = state.ts.d_buf[dx++];
  1237. /* Here, dist is the match distance - 1 */
  1238. code = d_code(dist);
  1239. Assert(state,code < D_CODES, "bad d_code");
  1240. send_code(state,code, dtree); /* send the distance code */
  1241. extra = extra_dbits[code];
  1242. if (extra != 0) {
  1243. dist -= state.ts.base_dist[code];
  1244. send_bits(state,dist, extra); /* send the extra distance bits */
  1245. }
  1246. } /* literal or match pair ? */
  1247. flag >>= 1;
  1248. } while (lx < state.ts.last_lit);
  1249. send_code(state,END_BLOCK, ltree);
  1250. }
  1251. /* ===========================================================================
  1252. * Set the file type to ASCII or BINARY, using a crude approximation:
  1253. * binary if more than 20% of the bytes are <= 6 or >= 128, ascii otherwise.
  1254. * IN assertion: the fields freq of dyn_ltree are set and the total of all
  1255. * frequencies does not exceed 64K (to fit in an int on 16 bit machines).
  1256. */
  1257. void set_file_type(TState &state)
  1258. {
  1259. int n = 0;
  1260. unsigned ascii_freq = 0;
  1261. unsigned bin_freq = 0;
  1262. while (n < 7) bin_freq += state.ts.dyn_ltree[n++].fc.freq;
  1263. while (n < 128) ascii_freq += state.ts.dyn_ltree[n++].fc.freq;
  1264. while (n < LITERALS) bin_freq += state.ts.dyn_ltree[n++].fc.freq;
  1265. *state.ts.file_type = (ush)(bin_freq > (ascii_freq >> 2) ? BINARY : ASCII);
  1266. }
  1267. /* ===========================================================================
  1268. * Initialize the bit string routines.
  1269. */
  1270. void bi_init (TState &state,char *tgt_buf, unsigned tgt_size, int flsh_allowed)
  1271. {
  1272. state.bs.out_buf = tgt_buf;
  1273. state.bs.out_size = tgt_size;
  1274. state.bs.out_offset = 0;
  1275. state.bs.flush_flg = flsh_allowed;
  1276. state.bs.bi_buf = 0;
  1277. state.bs.bi_valid = 0;
  1278. state.bs.bits_sent = 0L;
  1279. }
  1280. /* ===========================================================================
  1281. * Send a value on a given number of bits.
  1282. * IN assertion: length <= 16 and value fits in length bits.
  1283. */
  1284. void send_bits(TState &state,int value, int length)
  1285. {
  1286. Assert(state,length > 0 && length <= 15, "invalid length");
  1287. state.bs.bits_sent += (ulg)length;
  1288. /* If not enough room in bi_buf, use (bi_valid) bits from bi_buf and
  1289. * (Buf_size - bi_valid) bits from value to flush the filled bi_buf,
  1290. * then fill in the rest of (value), leaving (length - (Buf_size-bi_valid))
  1291. * unused bits in bi_buf.
  1292. */
  1293. state.bs.bi_buf |= (value << state.bs.bi_valid);
  1294. state.bs.bi_valid += length;
  1295. if (state.bs.bi_valid > (int)Buf_size) {
  1296. PUTSHORT(state,state.bs.bi_buf);
  1297. state.bs.bi_valid -= Buf_size;
  1298. state.bs.bi_buf = (unsigned)value >> (length - state.bs.bi_valid);
  1299. }
  1300. }
  1301. /* ===========================================================================
  1302. * Reverse the first len bits of a code, using straightforward code (a faster
  1303. * method would use a table)
  1304. * IN assertion: 1 <= len <= 15
  1305. */
  1306. unsigned bi_reverse(unsigned code, int len)
  1307. {
  1308. register unsigned res = 0;
  1309. do {
  1310. res |= code & 1;
  1311. code >>= 1, res <<= 1;
  1312. } while (--len > 0);
  1313. return res >> 1;
  1314. }
  1315. /* ===========================================================================
  1316. * Write out any remaining bits in an incomplete byte.
  1317. */
  1318. void bi_windup(TState &state)
  1319. {
  1320. if (state.bs.bi_valid > 8) {
  1321. PUTSHORT(state,state.bs.bi_buf);
  1322. } else if (state.bs.bi_valid > 0) {
  1323. PUTBYTE(state,state.bs.bi_buf);
  1324. }
  1325. if (state.bs.flush_flg) {
  1326. state.flush_outbuf(state.param,state.bs.out_buf, &state.bs.out_offset);
  1327. }
  1328. state.bs.bi_buf = 0;
  1329. state.bs.bi_valid = 0;
  1330. state.bs.bits_sent = (state.bs.bits_sent+7) & ~7;
  1331. }
  1332. /* ===========================================================================
  1333. * Copy a stored block to the zip file, storing first the length and its
  1334. * one's complement if requested.
  1335. */
  1336. void copy_block(TState &state, char *block, unsigned len, int header)
  1337. {
  1338. bi_windup(state); /* align on byte boundary */
  1339. if (header) {
  1340. PUTSHORT(state,(ush)len);
  1341. PUTSHORT(state,(ush)~len);
  1342. state.bs.bits_sent += 2*16;
  1343. }
  1344. if (state.bs.flush_flg) {
  1345. state.flush_outbuf(state.param,state.bs.out_buf, &state.bs.out_offset);
  1346. state.bs.out_offset = len;
  1347. state.flush_outbuf(state.param,block, &state.bs.out_offset);
  1348. } else if (state.bs.out_offset + len > state.bs.out_size) {
  1349. Assert(state,false,"output buffer too small for in-memory compression");
  1350. } else {
  1351. memcpy(state.bs.out_buf + state.bs.out_offset, block, len);
  1352. state.bs.out_offset += len;
  1353. }
  1354. state.bs.bits_sent += (ulg)len<<3;
  1355. }
  1356. /* ===========================================================================
  1357. * Prototypes for functions.
  1358. */
  1359. void fill_window (TState &state);
  1360. ulg deflate_fast (TState &state);
  1361. int longest_match (TState &state,IPos cur_match);
  1362. /* ===========================================================================
  1363. * Update a hash value with the given input byte
  1364. * IN assertion: all calls to to UPDATE_HASH are made with consecutive
  1365. * input characters, so that a running hash key can be computed from the
  1366. * previous key instead of complete recalculation each time.
  1367. */
  1368. #define UPDATE_HASH(h,c) (h = (((h)<<H_SHIFT) ^ (c)) & HASH_MASK)
  1369. /* ===========================================================================
  1370. * Insert string s in the dictionary and set match_head to the previous head
  1371. * of the hash chain (the most recent string with same hash key). Return
  1372. * the previous length of the hash chain.
  1373. * IN assertion: all calls to to INSERT_STRING are made with consecutive
  1374. * input characters and the first MIN_MATCH bytes of s are valid
  1375. * (except for the last MIN_MATCH-1 bytes of the input file).
  1376. */
  1377. #define INSERT_STRING(s, match_head) \
  1378. (UPDATE_HASH(state.ds.ins_h, state.ds.window[(s) + (MIN_MATCH-1)]), \
  1379. state.ds.prev[(s) & WMASK] = match_head = state.ds.head[state.ds.ins_h], \
  1380. state.ds.head[state.ds.ins_h] = (s))
  1381. /* ===========================================================================
  1382. * Initialize the "longest match" routines for a new file
  1383. *
  1384. * IN assertion: window_size is > 0 if the input file is already read or
  1385. * mmap'ed in the window[] array, 0 otherwise. In the first case,
  1386. * window_size is sufficient to contain the whole input file plus
  1387. * MIN_LOOKAHEAD bytes (to avoid referencing memory beyond the end
  1388. * of window[] when looking for matches towards the end).
  1389. */
  1390. void lm_init (TState &state, int pack_level, ush *flags)
  1391. {
  1392. register unsigned j;
  1393. Assert(state,pack_level>=1 && pack_level<=8,"bad pack level");
  1394. /* Do not slide the window if the whole input is already in memory
  1395. * (window_size > 0)
  1396. */
  1397. state.ds.sliding = 0;
  1398. if (state.ds.window_size == 0L) {
  1399. state.ds.sliding = 1;
  1400. state.ds.window_size = (ulg)2L*WSIZE;
  1401. }
  1402. /* Initialize the hash table (avoiding 64K overflow for 16 bit systems).
  1403. * prev[] will be initialized on the fly.
  1404. */
  1405. state.ds.head[HASH_SIZE-1] = NIL;
  1406. memset((char*)state.ds.head, NIL, (unsigned)(HASH_SIZE-1)*sizeof(*state.ds.head));
  1407. /* Set the default configuration parameters:
  1408. */
  1409. state.ds.max_lazy_match = configuration_table[pack_level].max_lazy;
  1410. state.ds.good_match = configuration_table[pack_level].good_length;
  1411. state.ds.nice_match = configuration_table[pack_level].nice_length;
  1412. state.ds.max_chain_length = configuration_table[pack_level].max_chain;
  1413. if (pack_level <= 2) {
  1414. *flags |= FAST;
  1415. } else if (pack_level >= 8) {
  1416. *flags |= SLOW;
  1417. }
  1418. /* ??? reduce max_chain_length for binary files */
  1419. state.ds.strstart = 0;
  1420. state.ds.block_start = 0L;
  1421. j = WSIZE;
  1422. j <<= 1; // Can read 64K in one step
  1423. state.ds.lookahead = state.readfunc(state, (char*)state.ds.window, j);
  1424. if (state.ds.lookahead == 0 || state.ds.lookahead == (unsigned)EOF) {
  1425. state.ds.eofile = 1, state.ds.lookahead = 0;
  1426. return;
  1427. }
  1428. state.ds.eofile = 0;
  1429. /* Make sure that we always have enough lookahead. This is important
  1430. * if input comes from a device such as a tty.
  1431. */
  1432. if (state.ds.lookahead < MIN_LOOKAHEAD) fill_window(state);
  1433. state.ds.ins_h = 0;
  1434. for (j=0; j<MIN_MATCH-1; j++) UPDATE_HASH(state.ds.ins_h, state.ds.window[j]);
  1435. /* If lookahead < MIN_MATCH, ins_h is garbage, but this is
  1436. * not important since only literal bytes will be emitted.
  1437. */
  1438. }
  1439. /* ===========================================================================
  1440. * Set match_start to the longest match starting at the given string and
  1441. * return its length. Matches shorter or equal to prev_length are discarded,
  1442. * in which case the result is equal to prev_length and match_start is
  1443. * garbage.
  1444. * IN assertions: cur_match is the head of the hash chain for the current
  1445. * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
  1446. */
  1447. // For 80x86 and 680x0 and ARM, an optimized version is in match.asm or
  1448. // match.S. The code is functionally equivalent, so you can use the C version
  1449. // if desired. Which I do so desire!
  1450. int longest_match(TState &state,IPos cur_match)
  1451. {
  1452. unsigned chain_length = state.ds.max_chain_length; /* max hash chain length */
  1453. register uch far *scan = state.ds.window + state.ds.strstart; /* current string */
  1454. register uch far *match; /* matched string */
  1455. register int len; /* length of current match */
  1456. int best_len = state.ds.prev_length; /* best match length so far */
  1457. IPos limit = state.ds.strstart > (IPos)MAX_DIST ? state.ds.strstart - (IPos)MAX_DIST : NIL;
  1458. /* Stop when cur_match becomes <= limit. To simplify the code,
  1459. * we prevent matches with the string of window index 0.
  1460. */
  1461. // The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
  1462. // It is easy to get rid of this optimization if necessary.
  1463. Assert(state,HASH_BITS>=8 && MAX_MATCH==258,"Code too clever");
  1464. register uch far *strend = state.ds.window + state.ds.strstart + MAX_MATCH;
  1465. register uch scan_end1 = scan[best_len-1];
  1466. register uch scan_end = scan[best_len];
  1467. /* Do not waste too much time if we already have a good match: */
  1468. if (state.ds.prev_length >= state.ds.good_match) {
  1469. chain_length >>= 2;
  1470. }
  1471. Assert(state,state.ds.strstart <= state.ds.window_size-MIN_LOOKAHEAD, "insufficient lookahead");
  1472. do {
  1473. Assert(state,cur_match < state.ds.strstart, "no future");
  1474. match = state.ds.window + cur_match;
  1475. /* Skip to next match if the match length cannot increase
  1476. * or if the match length is less than 2:
  1477. */
  1478. if (match[best_len] != scan_end ||
  1479. match[best_len-1] != scan_end1 ||
  1480. *match != *scan ||
  1481. *++match != scan[1]) continue;
  1482. /* The check at best_len-1 can be removed because it will be made
  1483. * again later. (This heuristic is not always a win.)
  1484. * It is not necessary to compare scan[2] and match[2] since they
  1485. * are always equal when the other bytes match, given that
  1486. * the hash keys are equal and that HASH_BITS >= 8.
  1487. */
  1488. scan += 2, match++;
  1489. /* We check for insufficient lookahead only every 8th comparison;
  1490. * the 256th check will be made at strstart+258.
  1491. */
  1492. do {
  1493. } while (*++scan == *++match && *++scan == *++match &&
  1494. *++scan == *++match && *++scan == *++match &&
  1495. *++scan == *++match && *++scan == *++match &&
  1496. *++scan == *++match && *++scan == *++match &&
  1497. scan < strend);
  1498. Assert(state,scan <= state.ds.window+(unsigned)(state.ds.window_size-1), "wild scan");
  1499. len = MAX_MATCH - (int)(strend - scan);
  1500. scan = strend - MAX_MATCH;
  1501. if (len > best_len) {
  1502. state.ds.match_start = cur_match;
  1503. best_len = len;
  1504. if (len >= state.ds.nice_match) break;
  1505. scan_end1 = scan[best_len-1];
  1506. scan_end = scan[best_len];
  1507. }
  1508. } while ((cur_match = state.ds.prev[cur_match & WMASK]) > limit
  1509. && --chain_length != 0);
  1510. return best_len;
  1511. }
  1512. #define check_match(state,start, match, length)
  1513. // or alternatively...
  1514. //void check_match(TState &state,IPos start, IPos match, int length)
  1515. //{ // check that the match is indeed a match
  1516. // if (memcmp((char*)state.ds.window + match,
  1517. // (char*)state.ds.window + start, length) != EQUAL) {
  1518. // fprintf(stderr,
  1519. // " start %d, match %d, length %d\n",
  1520. // start, match, length);
  1521. // error("invalid match");
  1522. // }
  1523. // if (state.verbose > 1) {
  1524. // fprintf(stderr,"\\[%d,%d]", start-match, length);
  1525. // do { fprintf(stdout,"%c",state.ds.window[start++]); } while (--length != 0);
  1526. // }
  1527. //}
  1528. /* ===========================================================================
  1529. * Fill the window when the lookahead becomes insufficient.
  1530. * Updates strstart and lookahead, and sets eofile if end of input file.
  1531. *
  1532. * IN assertion: lookahead < MIN_LOOKAHEAD && strstart + lookahead > 0
  1533. * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD
  1534. * At least one byte has been read, or eofile is set; file reads are
  1535. * performed for at least two bytes (required for the translate_eol option).
  1536. */
  1537. void fill_window(TState &state)
  1538. {
  1539. register unsigned n, m;
  1540. unsigned more; /* Amount of free space at the end of the window. */
  1541. do {
  1542. more = (unsigned)(state.ds.window_size - (ulg)state.ds.lookahead - (ulg)state.ds.strstart);
  1543. /* If the window is almost full and there is insufficient lookahead,
  1544. * move the upper half to the lower one to make room in the upper half.
  1545. */
  1546. if (more == (unsigned)EOF) {
  1547. /* Very unlikely, but possible on 16 bit machine if strstart == 0
  1548. * and lookahead == 1 (input done one byte at time)
  1549. */
  1550. more--;
  1551. /* For MMAP or BIG_MEM, the whole input file is already in memory so
  1552. * we must not perform sliding. We must however call (*read_buf)() in
  1553. * order to compute the crc, update lookahead and possibly set eofile.
  1554. */
  1555. } else if (state.ds.strstart >= WSIZE+MAX_DIST && state.ds.sliding) {
  1556. /* By the IN assertion, the window is not empty so we can't confuse
  1557. * more == 0 with more == 64K on a 16 bit machine.
  1558. */
  1559. memcpy((char*)state.ds.window, (char*)state.ds.window+WSIZE, (unsigned)WSIZE);
  1560. state.ds.match_start -= WSIZE;
  1561. state.ds.strstart -= WSIZE; /* we now have strstart >= MAX_DIST: */
  1562. state.ds.block_start -= (long) WSIZE;
  1563. for (n = 0; n < HASH_SIZE; n++) {
  1564. m = state.ds.head[n];
  1565. state.ds.head[n] = (Pos)(m >= WSIZE ? m-WSIZE : NIL);
  1566. }
  1567. for (n = 0; n < WSIZE; n++) {
  1568. m = state.ds.prev[n];
  1569. state.ds.prev[n] = (Pos)(m >= WSIZE ? m-WSIZE : NIL);
  1570. /* If n is not on any hash chain, prev[n] is garbage but
  1571. * its value will never be used.
  1572. */
  1573. }
  1574. more += WSIZE;
  1575. }
  1576. if (state.ds.eofile) return;
  1577. /* If there was no sliding:
  1578. * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
  1579. * more == window_size - lookahead - strstart
  1580. * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
  1581. * => more >= window_size - 2*WSIZE + 2
  1582. * In the MMAP or BIG_MEM case (not yet supported in gzip),
  1583. * window_size == input_size + MIN_LOOKAHEAD &&
  1584. * strstart + lookahead <= input_size => more >= MIN_LOOKAHEAD.
  1585. * Otherwise, window_size == 2*WSIZE so more >= 2.
  1586. * If there was sliding, more >= WSIZE. So in all cases, more >= 2.
  1587. */
  1588. Assert(state,more >= 2, "more < 2");
  1589. n = state.readfunc(state, (char*)state.ds.window+state.ds.strstart+state.ds.lookahead, more);
  1590. if (n == 0 || n == (unsigned)EOF) {
  1591. state.ds.eofile = 1;
  1592. } else {
  1593. state.ds.lookahead += n;
  1594. }
  1595. } while (state.ds.lookahead < MIN_LOOKAHEAD && !state.ds.eofile);
  1596. }
  1597. /* ===========================================================================
  1598. * Flush the current block, with given end-of-file flag.
  1599. * IN assertion: strstart is set to the end of the current match.
  1600. */
  1601. #define FLUSH_BLOCK(state,eof) \
  1602. flush_block(state,state.ds.block_start >= 0L ? (char*)&state.ds.window[(unsigned)state.ds.block_start] : \
  1603. (char*)NULL, (long)state.ds.strstart - state.ds.block_start, (eof))
  1604. /* ===========================================================================
  1605. * Processes a new input file and return its compressed length. This
  1606. * function does not perform lazy evaluation of matches and inserts
  1607. * new strings in the dictionary only for unmatched strings or for short
  1608. * matches. It is used only for the fast compression options.
  1609. */
  1610. ulg deflate_fast(TState &state)
  1611. {
  1612. IPos hash_head = NIL; /* head of the hash chain */
  1613. int flush; /* set if current block must be flushed */
  1614. unsigned match_length = 0; /* length of best match */
  1615. state.ds.prev_length = MIN_MATCH-1;
  1616. while (state.ds.lookahead != 0) {
  1617. /* Insert the string window[strstart .. strstart+2] in the
  1618. * dictionary, and set hash_head to the head of the hash chain:
  1619. */
  1620. if (state.ds.lookahead >= MIN_MATCH)
  1621. INSERT_STRING(state.ds.strstart, hash_head);
  1622. /* Find the longest match, discarding those <= prev_length.
  1623. * At this point we have always match_length < MIN_MATCH
  1624. */
  1625. if (hash_head != NIL && state.ds.strstart - hash_head <= MAX_DIST) {
  1626. /* To simplify the code, we prevent matches with the string
  1627. * of window index 0 (in particular we have to avoid a match
  1628. * of the string with itself at the start of the input file).
  1629. */
  1630. /* Do not look for matches beyond the end of the input.
  1631. * This is necessary to make deflate deterministic.
  1632. */
  1633. if ((unsigned)state.ds.nice_match > state.ds.lookahead) state.ds.nice_match = (int)state.ds.lookahead;
  1634. match_length = longest_match (state,hash_head);
  1635. /* longest_match() sets match_start */
  1636. if (match_length > state.ds.lookahead) match_length = state.ds.lookahead;
  1637. }
  1638. if (match_length >= MIN_MATCH) {
  1639. check_match(state,state.ds.strstart, state.ds.match_start, match_length);
  1640. flush = ct_tally(state,state.ds.strstart-state.ds.match_start, match_length - MIN_MATCH);
  1641. state.ds.lookahead -= match_length;
  1642. /* Insert new strings in the hash table only if the match length
  1643. * is not too large. This saves time but degrades compression.
  1644. */
  1645. if (match_length <= state.ds.max_insert_length
  1646. && state.ds.lookahead >= MIN_MATCH) {
  1647. match_length--; /* string at strstart already in hash table */
  1648. do {
  1649. state.ds.strstart++;
  1650. INSERT_STRING(state.ds.strstart, hash_head);
  1651. /* strstart never exceeds WSIZE-MAX_MATCH, so there are
  1652. * always MIN_MATCH bytes ahead.
  1653. */
  1654. } while (--match_length != 0);
  1655. state.ds.strstart++;
  1656. } else {
  1657. state.ds.strstart += match_length;
  1658. match_length = 0;
  1659. state.ds.ins_h = state.ds.window[state.ds.strstart];
  1660. UPDATE_HASH(state.ds.ins_h, state.ds.window[state.ds.strstart+1]);
  1661. Assert(state,MIN_MATCH==3,"Call UPDATE_HASH() MIN_MATCH-3 more times");
  1662. }
  1663. } else {
  1664. /* No match, output a literal byte */
  1665. flush = ct_tally (state,0, state.ds.window[state.ds.strstart]);
  1666. state.ds.lookahead--;
  1667. state.ds.strstart++;
  1668. }
  1669. if (flush) FLUSH_BLOCK(state,0), state.ds.block_start = state.ds.strstart;
  1670. /* Make sure that we always have enough lookahead, except
  1671. * at the end of the input file. We need MAX_MATCH bytes
  1672. * for the next match, plus MIN_MATCH bytes to insert the
  1673. * string following the next match.
  1674. */
  1675. if (state.ds.lookahead < MIN_LOOKAHEAD) fill_window(state);
  1676. }
  1677. return FLUSH_BLOCK(state,1); /* eof */
  1678. }
  1679. /* ===========================================================================
  1680. * Same as above, but achieves better compression. We use a lazy
  1681. * evaluation for matches: a match is finally adopted only if there is
  1682. * no better match at the next window position.
  1683. */
  1684. ulg deflate(TState &state)
  1685. {
  1686. IPos hash_head = NIL; /* head of hash chain */
  1687. IPos prev_match; /* previous match */
  1688. int flush; /* set if current block must be flushed */
  1689. int match_available = 0; /* set if previous match exists */
  1690. register unsigned match_length = MIN_MATCH-1; /* length of best match */
  1691. if (state.level <= 3) return deflate_fast(state); /* optimized for speed */
  1692. /* Process the input block. */
  1693. while (state.ds.lookahead != 0) {
  1694. /* Insert the string window[strstart .. strstart+2] in the
  1695. * dictionary, and set hash_head to the head of the hash chain:
  1696. */
  1697. if (state.ds.lookahead >= MIN_MATCH)
  1698. INSERT_STRING(state.ds.strstart, hash_head);
  1699. /* Find the longest match, discarding those <= prev_length.
  1700. */
  1701. state.ds.prev_length = match_length, prev_match = state.ds.match_start;
  1702. match_length = MIN_MATCH-1;
  1703. if (hash_head != NIL && state.ds.prev_length < state.ds.max_lazy_match &&
  1704. state.ds.strstart - hash_head <= MAX_DIST) {
  1705. /* To simplify the code, we prevent matches with the string
  1706. * of window index 0 (in particular we have to avoid a match
  1707. * of the string with itself at the start of the input file).
  1708. */
  1709. /* Do not look for matches beyond the end of the input.
  1710. * This is necessary to make deflate deterministic.
  1711. */
  1712. if ((unsigned)state.ds.nice_match > state.ds.lookahead) state.ds.nice_match = (int)state.ds.lookahead;
  1713. match_length = longest_match (state,hash_head);
  1714. /* longest_match() sets match_start */
  1715. if (match_length > state.ds.lookahead) match_length = state.ds.lookahead;
  1716. /* Ignore a length 3 match if it is too distant: */
  1717. if (match_length == MIN_MATCH && state.ds.strstart-state.ds.match_start > TOO_FAR){
  1718. /* If prev_match is also MIN_MATCH, match_start is garbage
  1719. * but we will ignore the current match anyway.
  1720. */
  1721. match_length = MIN_MATCH-1;
  1722. }
  1723. }
  1724. /* If there was a match at the previous step and the current
  1725. * match is not better, output the previous match:
  1726. */
  1727. if (state.ds.prev_length >= MIN_MATCH && match_length <= state.ds.prev_length) {
  1728. unsigned max_insert = state.ds.strstart + state.ds.lookahead - MIN_MATCH;
  1729. check_match(state,state.ds.strstart-1, prev_match, state.ds.prev_length);
  1730. flush = ct_tally(state,state.ds.strstart-1-prev_match, state.ds.prev_length - MIN_MATCH);
  1731. /* Insert in hash table all strings up to the end of the match.
  1732. * strstart-1 and strstart are already inserted.
  1733. */
  1734. state.ds.lookahead -= state.ds.prev_length-1;
  1735. state.ds.prev_length -= 2;
  1736. do {
  1737. if (++state.ds.strstart <= max_insert) {
  1738. INSERT_STRING(state.ds.strstart, hash_head);
  1739. /* strstart never exceeds WSIZE-MAX_MATCH, so there are
  1740. * always MIN_MATCH bytes ahead.
  1741. */
  1742. }
  1743. } while (--state.ds.prev_length != 0);
  1744. state.ds.strstart++;
  1745. match_available = 0;
  1746. match_length = MIN_MATCH-1;
  1747. if (flush) FLUSH_BLOCK(state,0), state.ds.block_start = state.ds.strstart;
  1748. } else if (match_available) {
  1749. /* If there was no match at the previous position, output a
  1750. * single literal. If there was a match but the current match
  1751. * is longer, truncate the previous match to a single literal.
  1752. */
  1753. if (ct_tally (state,0, state.ds.window[state.ds.strstart-1])) {
  1754. FLUSH_BLOCK(state,0), state.ds.block_start = state.ds.strstart;
  1755. }
  1756. state.ds.strstart++;
  1757. state.ds.lookahead--;
  1758. } else {
  1759. /* There is no previous match to compare with, wait for
  1760. * the next step to decide.
  1761. */
  1762. match_available = 1;
  1763. state.ds.strstart++;
  1764. state.ds.lookahead--;
  1765. }
  1766. // Assert(state,strstart <= isize && lookahead <= isize, "a bit too far");
  1767. /* Make sure that we always have enough lookahead, except
  1768. * at the end of the input file. We need MAX_MATCH bytes
  1769. * for the next match, plus MIN_MATCH bytes to insert the
  1770. * string following the next match.
  1771. */
  1772. if (state.ds.lookahead < MIN_LOOKAHEAD) fill_window(state);
  1773. }
  1774. if (match_available) ct_tally (state,0, state.ds.window[state.ds.strstart-1]);
  1775. return FLUSH_BLOCK(state,1); /* eof */
  1776. }
  1777. int putlocal(struct zlist far *z, WRITEFUNC wfunc,void *param)
  1778. { // Write a local header described by *z to file *f. Return a ZE_ error code.
  1779. PUTLG(LOCSIG, f);
  1780. PUTSH(z->ver, f);
  1781. PUTSH(z->lflg, f);
  1782. PUTSH(z->how, f);
  1783. PUTLG(z->tim, f);
  1784. PUTLG(z->crc, f);
  1785. PUTLG(z->siz, f);
  1786. PUTLG(z->len, f);
  1787. PUTSH(z->nam, f);
  1788. PUTSH(z->ext, f);
  1789. size_t res = (size_t)wfunc(param, z->iname, (unsigned int)z->nam);
  1790. if (res!=z->nam) return ZE_TEMP;
  1791. if (z->ext)
  1792. { res = (size_t)wfunc(param, z->extra, (unsigned int)z->ext);
  1793. if (res!=z->ext) return ZE_TEMP;
  1794. }
  1795. return ZE_OK;
  1796. }
  1797. int putextended(struct zlist far *z, WRITEFUNC wfunc, void *param)
  1798. { // Write an extended local header described by *z to file *f. Returns a ZE_ code
  1799. PUTLG(EXTLOCSIG, f);
  1800. PUTLG(z->crc, f);
  1801. PUTLG(z->siz, f);
  1802. PUTLG(z->len, f);
  1803. return ZE_OK;
  1804. }
  1805. int putcentral(struct zlist far *z, WRITEFUNC wfunc, void *param)
  1806. { // Write a central header entry of *z to file *f. Returns a ZE_ code.
  1807. PUTLG(CENSIG, f);
  1808. PUTSH(z->vem, f);
  1809. PUTSH(z->ver, f);
  1810. PUTSH(z->flg, f);
  1811. PUTSH(z->how, f);
  1812. PUTLG(z->tim, f);
  1813. PUTLG(z->crc, f);
  1814. PUTLG(z->siz, f);
  1815. PUTLG(z->len, f);
  1816. PUTSH(z->nam, f);
  1817. PUTSH(z->cext, f);
  1818. PUTSH(z->com, f);
  1819. PUTSH(z->dsk, f);
  1820. PUTSH(z->att, f);
  1821. PUTLG(z->atx, f);
  1822. PUTLG(z->off, f);
  1823. if ((size_t)wfunc(param, z->iname, (unsigned int)z->nam) != z->nam ||
  1824. (z->cext && (size_t)wfunc(param, z->cextra, (unsigned int)z->cext) != z->cext) ||
  1825. (z->com && (size_t)wfunc(param, z->comment, (unsigned int)z->com) != z->com))
  1826. return ZE_TEMP;
  1827. return ZE_OK;
  1828. }
  1829. int putend(int n, ulg s, ulg c, extent m, char *z, WRITEFUNC wfunc, void *param)
  1830. { // write the end of the central-directory-data to file *f.
  1831. PUTLG(ENDSIG, f);
  1832. PUTSH(0, f);
  1833. PUTSH(0, f);
  1834. PUTSH(n, f);
  1835. PUTSH(n, f);
  1836. PUTLG(s, f);
  1837. PUTLG(c, f);
  1838. PUTSH(m, f);
  1839. // Write the comment, if any
  1840. if (m && wfunc(param, z, (unsigned int)m) != m) return ZE_TEMP;
  1841. return ZE_OK;
  1842. }
  1843. const ulg crc_table[256] = {
  1844. 0x00000000L, 0x77073096L, 0xee0e612cL, 0x990951baL, 0x076dc419L,
  1845. 0x706af48fL, 0xe963a535L, 0x9e6495a3L, 0x0edb8832L, 0x79dcb8a4L,
  1846. 0xe0d5e91eL, 0x97d2d988L, 0x09b64c2bL, 0x7eb17cbdL, 0xe7b82d07L,
  1847. 0x90bf1d91L, 0x1db71064L, 0x6ab020f2L, 0xf3b97148L, 0x84be41deL,
  1848. 0x1adad47dL, 0x6ddde4ebL, 0xf4d4b551L, 0x83d385c7L, 0x136c9856L,
  1849. 0x646ba8c0L, 0xfd62f97aL, 0x8a65c9ecL, 0x14015c4fL, 0x63066cd9L,
  1850. 0xfa0f3d63L, 0x8d080df5L, 0x3b6e20c8L, 0x4c69105eL, 0xd56041e4L,
  1851. 0xa2677172L, 0x3c03e4d1L, 0x4b04d447L, 0xd20d85fdL, 0xa50ab56bL,
  1852. 0x35b5a8faL, 0x42b2986cL, 0xdbbbc9d6L, 0xacbcf940L, 0x32d86ce3L,
  1853. 0x45df5c75L, 0xdcd60dcfL, 0xabd13d59L, 0x26d930acL, 0x51de003aL,
  1854. 0xc8d75180L, 0xbfd06116L, 0x21b4f4b5L, 0x56b3c423L, 0xcfba9599L,
  1855. 0xb8bda50fL, 0x2802b89eL, 0x5f058808L, 0xc60cd9b2L, 0xb10be924L,
  1856. 0x2f6f7c87L, 0x58684c11L, 0xc1611dabL, 0xb6662d3dL, 0x76dc4190L,
  1857. 0x01db7106L, 0x98d220bcL, 0xefd5102aL, 0x71b18589L, 0x06b6b51fL,
  1858. 0x9fbfe4a5L, 0xe8b8d433L, 0x7807c9a2L, 0x0f00f934L, 0x9609a88eL,
  1859. 0xe10e9818L, 0x7f6a0dbbL, 0x086d3d2dL, 0x91646c97L, 0xe6635c01L,
  1860. 0x6b6b51f4L, 0x1c6c6162L, 0x856530d8L, 0xf262004eL, 0x6c0695edL,
  1861. 0x1b01a57bL, 0x8208f4c1L, 0xf50fc457L, 0x65b0d9c6L, 0x12b7e950L,
  1862. 0x8bbeb8eaL, 0xfcb9887cL, 0x62dd1ddfL, 0x15da2d49L, 0x8cd37cf3L,
  1863. 0xfbd44c65L, 0x4db26158L, 0x3ab551ceL, 0xa3bc0074L, 0xd4bb30e2L,
  1864. 0x4adfa541L, 0x3dd895d7L, 0xa4d1c46dL, 0xd3d6f4fbL, 0x4369e96aL,
  1865. 0x346ed9fcL, 0xad678846L, 0xda60b8d0L, 0x44042d73L, 0x33031de5L,
  1866. 0xaa0a4c5fL, 0xdd0d7cc9L, 0x5005713cL, 0x270241aaL, 0xbe0b1010L,
  1867. 0xc90c2086L, 0x5768b525L, 0x206f85b3L, 0xb966d409L, 0xce61e49fL,
  1868. 0x5edef90eL, 0x29d9c998L, 0xb0d09822L, 0xc7d7a8b4L, 0x59b33d17L,
  1869. 0x2eb40d81L, 0xb7bd5c3bL, 0xc0ba6cadL, 0xedb88320L, 0x9abfb3b6L,
  1870. 0x03b6e20cL, 0x74b1d29aL, 0xead54739L, 0x9dd277afL, 0x04db2615L,
  1871. 0x73dc1683L, 0xe3630b12L, 0x94643b84L, 0x0d6d6a3eL, 0x7a6a5aa8L,
  1872. 0xe40ecf0bL, 0x9309ff9dL, 0x0a00ae27L, 0x7d079eb1L, 0xf00f9344L,
  1873. 0x8708a3d2L, 0x1e01f268L, 0x6906c2feL, 0xf762575dL, 0x806567cbL,
  1874. 0x196c3671L, 0x6e6b06e7L, 0xfed41b76L, 0x89d32be0L, 0x10da7a5aL,
  1875. 0x67dd4accL, 0xf9b9df6fL, 0x8ebeeff9L, 0x17b7be43L, 0x60b08ed5L,
  1876. 0xd6d6a3e8L, 0xa1d1937eL, 0x38d8c2c4L, 0x4fdff252L, 0xd1bb67f1L,
  1877. 0xa6bc5767L, 0x3fb506ddL, 0x48b2364bL, 0xd80d2bdaL, 0xaf0a1b4cL,
  1878. 0x36034af6L, 0x41047a60L, 0xdf60efc3L, 0xa867df55L, 0x316e8eefL,
  1879. 0x4669be79L, 0xcb61b38cL, 0xbc66831aL, 0x256fd2a0L, 0x5268e236L,
  1880. 0xcc0c7795L, 0xbb0b4703L, 0x220216b9L, 0x5505262fL, 0xc5ba3bbeL,
  1881. 0xb2bd0b28L, 0x2bb45a92L, 0x5cb36a04L, 0xc2d7ffa7L, 0xb5d0cf31L,
  1882. 0x2cd99e8bL, 0x5bdeae1dL, 0x9b64c2b0L, 0xec63f226L, 0x756aa39cL,
  1883. 0x026d930aL, 0x9c0906a9L, 0xeb0e363fL, 0x72076785L, 0x05005713L,
  1884. 0x95bf4a82L, 0xe2b87a14L, 0x7bb12baeL, 0x0cb61b38L, 0x92d28e9bL,
  1885. 0xe5d5be0dL, 0x7cdcefb7L, 0x0bdbdf21L, 0x86d3d2d4L, 0xf1d4e242L,
  1886. 0x68ddb3f8L, 0x1fda836eL, 0x81be16cdL, 0xf6b9265bL, 0x6fb077e1L,
  1887. 0x18b74777L, 0x88085ae6L, 0xff0f6a70L, 0x66063bcaL, 0x11010b5cL,
  1888. 0x8f659effL, 0xf862ae69L, 0x616bffd3L, 0x166ccf45L, 0xa00ae278L,
  1889. 0xd70dd2eeL, 0x4e048354L, 0x3903b3c2L, 0xa7672661L, 0xd06016f7L,
  1890. 0x4969474dL, 0x3e6e77dbL, 0xaed16a4aL, 0xd9d65adcL, 0x40df0b66L,
  1891. 0x37d83bf0L, 0xa9bcae53L, 0xdebb9ec5L, 0x47b2cf7fL, 0x30b5ffe9L,
  1892. 0xbdbdf21cL, 0xcabac28aL, 0x53b39330L, 0x24b4a3a6L, 0xbad03605L,
  1893. 0xcdd70693L, 0x54de5729L, 0x23d967bfL, 0xb3667a2eL, 0xc4614ab8L,
  1894. 0x5d681b02L, 0x2a6f2b94L, 0xb40bbe37L, 0xc30c8ea1L, 0x5a05df1bL,
  1895. 0x2d02ef8dL
  1896. };
  1897. #define CRC32(c, b) (crc_table[((int)(c) ^ (b)) & 0xff] ^ ((c) >> 8))
  1898. #define DO1(buf) crc = CRC32(crc, *buf++)
  1899. #define DO2(buf) DO1(buf); DO1(buf)
  1900. #define DO4(buf) DO2(buf); DO2(buf)
  1901. #define DO8(buf) DO4(buf); DO4(buf)
  1902. ulg crc32(ulg crc, const uch *buf, extent len)
  1903. { if (buf==NULL) return 0L;
  1904. crc = crc ^ 0xffffffffL;
  1905. while (len >= 8) {DO8(buf); len -= 8;}
  1906. if (len) do {DO1(buf);} while (--len);
  1907. return crc ^ 0xffffffffL; // (instead of ~c for 64-bit machines)
  1908. }
  1909. bool HasZipSuffix(const char *fn)
  1910. { const char *ext = fn+strlen(fn);
  1911. while (ext>fn && *ext!='.') ext--;
  1912. if (ext==fn && *ext!='.') return false;
  1913. if (stricmp(ext,".Z")==0) return true;
  1914. if (stricmp(ext,".zip")==0) return true;
  1915. if (stricmp(ext,".zoo")==0) return true;
  1916. if (stricmp(ext,".arc")==0) return true;
  1917. if (stricmp(ext,".lzh")==0) return true;
  1918. if (stricmp(ext,".arj")==0) return true;
  1919. if (stricmp(ext,".gz")==0) return true;
  1920. if (stricmp(ext,".tgz")==0) return true;
  1921. return false;
  1922. }
  1923. time_t filetime2timet(const FILETIME ft)
  1924. { SYSTEMTIME st; FileTimeToSystemTime(&ft,&st);
  1925. if (st.wYear<1970) {st.wYear=1970; st.wMonth=1; st.wDay=1;}
  1926. if (st.wYear>=2038) {st.wYear=2037; st.wMonth=12; st.wDay=31;}
  1927. struct tm tm;
  1928. tm.tm_sec = st.wSecond;
  1929. tm.tm_min = st.wMinute;
  1930. tm.tm_hour = st.wHour;
  1931. tm.tm_mday = st.wDay;
  1932. tm.tm_mon = st.wMonth-1;
  1933. tm.tm_year = st.wYear-1900;
  1934. tm.tm_isdst = 0;
  1935. time_t t = mktime(&tm);
  1936. return t;
  1937. }
  1938. ZRESULT GetFileInfo(HANDLE hf, ulg *attr, long *size, iztimes *times, ulg *timestamp)
  1939. {
  1940. DWORD type=GetFileType(hf);
  1941. if (type!=FILE_TYPE_DISK)
  1942. return ZR_NOTINITED;
  1943. // The handle must be a handle to a file
  1944. // The date and time is returned in a long with the date most significant to allow
  1945. // unsigned integer comparison of absolute times. The attributes have two
  1946. // high bytes unix attr, and two low bytes a mapping of that to DOS attr.
  1947. //struct stat s; int res=stat(fn,&s); if (res!=0) return false;
  1948. // translate windows file attributes into zip ones.
  1949. BY_HANDLE_FILE_INFORMATION bhi;
  1950. BOOL res=GetFileInformationByHandle(hf,&bhi);
  1951. if (!res)
  1952. return ZR_NOFILE;
  1953. // +++1.3
  1954. /// Convert times from UTC to local time. MSDN says that FILETIME is local
  1955. /// for FAT file system and UTC for NTFS system, but tests show that both FAT and NTFS
  1956. /// return UTC time.
  1957. {
  1958. // Get time zone difference
  1959. SYSTEMTIME stUTC, stLocal;
  1960. GetSystemTime(&stUTC);
  1961. GetLocalTime(&stLocal); // could be a few milliseconds difference, but should we care?
  1962. FILETIME ftUTC, ftLocal;
  1963. SystemTimeToFileTime(&stUTC, &ftUTC);
  1964. SystemTimeToFileTime(&stLocal, &ftLocal);
  1965. LONG64 uiUTC, uiLocal;
  1966. memcpy (&uiUTC, &ftUTC, min(sizeof(LONG64), sizeof(FILETIME))); // use 'min' as safeguard, however both sizes should be the same: 64-bit
  1967. memcpy (&uiLocal, &ftLocal, min(sizeof(LONG64), sizeof(FILETIME)));
  1968. LONG64 uiTimeDiff = uiUTC - uiLocal;
  1969. // apply difference
  1970. FILETIME* pFileTimes[3] = { &bhi.ftLastWriteTime, &bhi.ftLastAccessTime, &bhi.ftCreationTime };
  1971. for (int i=0; i<3; i++){
  1972. LONG64 uiUTC_file;
  1973. memcpy (&uiUTC_file, pFileTimes[i], min(sizeof(LONG64), sizeof(FILETIME)));
  1974. LONG64 uiLocal_file = uiUTC_file - uiTimeDiff;
  1975. memcpy (pFileTimes[i], &uiLocal_file, min(sizeof(LONG64), sizeof(FILETIME)));
  1976. }
  1977. }
  1978. DWORD fa=bhi.dwFileAttributes;
  1979. ulg a=0;
  1980. // Zip uses the lower word for its interpretation of windows stuff
  1981. if (fa&FILE_ATTRIBUTE_READONLY) a|=0x01;
  1982. if (fa&FILE_ATTRIBUTE_HIDDEN) a|=0x02;
  1983. if (fa&FILE_ATTRIBUTE_SYSTEM) a|=0x04;
  1984. if (fa&FILE_ATTRIBUTE_DIRECTORY)a|=0x10;
  1985. if (fa&FILE_ATTRIBUTE_ARCHIVE) a|=0x20;
  1986. // It uses the upper word for standard unix attr, which we must manually construct
  1987. if (fa&FILE_ATTRIBUTE_DIRECTORY)a|=0x40000000; // directory
  1988. else a|=0x80000000; // normal file
  1989. a|=0x01000000; // readable
  1990. if (fa&FILE_ATTRIBUTE_READONLY) {}
  1991. else a|=0x00800000; // writeable
  1992. // now just a small heuristic to check if it's an executable:
  1993. DWORD red, hsize=GetFileSize(hf,NULL); if (hsize>40)
  1994. { SetFilePointer(hf,0,NULL,FILE_BEGIN); unsigned short magic; ReadFile(hf,&magic,sizeof(magic),&red,NULL);
  1995. SetFilePointer(hf,36,NULL,FILE_BEGIN); unsigned long hpos; ReadFile(hf,&hpos,sizeof(hpos),&red,NULL);
  1996. if (magic==0x54AD && hsize>hpos+4+20+28)
  1997. { SetFilePointer(hf,hpos,NULL,FILE_BEGIN); unsigned long signature; ReadFile(hf,&signature,sizeof(signature),&red,NULL);
  1998. if (signature==IMAGE_DOS_SIGNATURE || signature==IMAGE_OS2_SIGNATURE
  1999. || signature==IMAGE_OS2_SIGNATURE_LE || signature==IMAGE_NT_SIGNATURE)
  2000. { a |= 0x00400000; // executable
  2001. }
  2002. }
  2003. }
  2004. //
  2005. if (attr!=NULL) *attr = a;
  2006. if (size!=NULL) *size = hsize;
  2007. if (times!=NULL)
  2008. { // time_t is 32bit number of seconds elapsed since 0:0:0GMT, Jan1, 1970.
  2009. // but FILETIME is 64bit number of 100-nanosecs since Jan1, 1601
  2010. times->atime = filetime2timet(bhi.ftLastAccessTime);
  2011. times->mtime = filetime2timet(bhi.ftLastWriteTime);
  2012. times->ctime = filetime2timet(bhi.ftCreationTime);
  2013. }
  2014. if (timestamp!=NULL)
  2015. { WORD dosdate,dostime;
  2016. FileTimeToDosDateTime(&bhi.ftLastWriteTime,&dosdate,&dostime);
  2017. *timestamp = (WORD)dostime | (((DWORD)dosdate)<<16);
  2018. }
  2019. return ZR_OK;
  2020. }
  2021. ///////////////////////////////////////////////////////////////////////////////
  2022. ///////////////////////////////////////////////////////////////////////////////
  2023. ///////////////////////////////////////////////////////////////////////////////
  2024. class TZip
  2025. { public:
  2026. TZip() : hfout(0),hmapout(0),zfis(0),obuf(0),hfin(0),writ(0),oerr(false),hasputcen(false),ooffset(0) {}
  2027. ~TZip() {}
  2028. // These variables say about the file we're writing into
  2029. // We can write to pipe, file-by-handle, file-by-name, memory-to-memmapfile
  2030. HANDLE hfout; // if valid, we'll write here (for files or pipes)
  2031. HANDLE hmapout; // otherwise, we'll write here (for memmap)
  2032. unsigned ooffset; // for hfout, this is where the pointer was initially
  2033. ZRESULT oerr; // did a write operation give rise to an error?
  2034. unsigned writ; // how far have we written. This is maintained by Add, not write(), to avoid confusion over seeks
  2035. bool ocanseek; // can we seek?
  2036. char *obuf; // this is where we've locked mmap to view.
  2037. unsigned int opos; // current pos in the mmap
  2038. unsigned int mapsize; // the size of the map we created
  2039. bool hasputcen; // have we yet placed the central directory?
  2040. //
  2041. TZipFileInfo *zfis; // each file gets added onto this list, for writing the table at the end
  2042. ZRESULT Create(void *z,unsigned int len,DWORD flags);
  2043. static unsigned sflush(void *param,const char *buf, unsigned *size);
  2044. static unsigned swrite(void *param,const char *buf, unsigned size);
  2045. unsigned int write(const char *buf,unsigned int size);
  2046. bool oseek(unsigned int pos);
  2047. ZRESULT GetMemory(void **pbuf, unsigned long *plen);
  2048. ZRESULT Close();
  2049. // some variables to do with the file currently being read:
  2050. // I haven't done it object-orientedly here, just put them all
  2051. // together, since OO didn't seem to make the design any clearer.
  2052. ulg attr; iztimes times; ulg timestamp; // all open_* methods set these
  2053. bool iseekable; long isize,ired; // size is not set until close() on pips
  2054. ulg crc; // crc is not set until close(). iwrit is cumulative
  2055. HANDLE hfin; bool selfclosehf; // for input files and pipes
  2056. const char *bufin; unsigned int lenin,posin; // for memory
  2057. // and a variable for what we've done with the input: (i.e. compressed it!)
  2058. ulg csize; // compressed size, set by the compression routines
  2059. // and this is used by some of the compression routines
  2060. char buf[16384];
  2061. ZRESULT open_file(const TCHAR *fn);
  2062. ZRESULT open_handle(HANDLE hf,unsigned int len);
  2063. ZRESULT open_mem(void *src,unsigned int len);
  2064. ZRESULT open_dir();
  2065. static unsigned sread(TState &s,char *buf,unsigned size);
  2066. unsigned read(char *buf, unsigned size);
  2067. ZRESULT iclose();
  2068. ZRESULT ideflate(TZipFileInfo *zfi);
  2069. ZRESULT istore();
  2070. ZRESULT Add(const char *odstzn, void *src,unsigned int len, DWORD flags);
  2071. ZRESULT AddCentral();
  2072. };
  2073. ZRESULT TZip::Create(void *z,unsigned int len,DWORD flags)
  2074. {
  2075. if (hfout!=0 || hmapout!=0 || obuf!=0 || writ!=0 || oerr!=ZR_OK || hasputcen)
  2076. return ZR_NOTINITED;
  2077. //
  2078. if (flags==ZIP_HANDLE)
  2079. {
  2080. HANDLE hf = (HANDLE)z;
  2081. BOOL res = DuplicateHandle(GetCurrentProcess(),hf,GetCurrentProcess(),&hfout,0,FALSE,DUPLICATE_SAME_ACCESS);
  2082. if (!res)
  2083. return ZR_NODUPH;
  2084. // now we have our own hfout, which we must close. And the caller will close hf
  2085. DWORD type = GetFileType(hfout);
  2086. ocanseek = (type==FILE_TYPE_DISK);
  2087. if (type==FILE_TYPE_DISK)
  2088. ooffset=SetFilePointer(hfout,0,NULL,FILE_CURRENT);
  2089. else
  2090. ooffset=0;
  2091. return ZR_OK;
  2092. }
  2093. else if (flags==ZIP_FILENAME)
  2094. {
  2095. #ifdef _UNICODE
  2096. const TCHAR *fn = (const TCHAR*)z;
  2097. hfout = CreateFileW(fn,GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
  2098. #else
  2099. const char *fn = (const char*)z;
  2100. hfout = CreateFileA(fn,GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
  2101. #endif
  2102. if (hfout==INVALID_HANDLE_VALUE)
  2103. {
  2104. hfout=0;
  2105. return ZR_NOFILE;
  2106. }
  2107. ocanseek=true;
  2108. ooffset=0;
  2109. return ZR_OK;
  2110. }
  2111. else if (flags==ZIP_MEMORY)
  2112. {
  2113. unsigned int size = len;
  2114. if (size==0)
  2115. return ZR_MEMSIZE;
  2116. if (z!=0)
  2117. obuf=(char*)z;
  2118. else
  2119. {
  2120. hmapout = CreateFileMapping(INVALID_HANDLE_VALUE,NULL,PAGE_READWRITE,0,size,NULL);
  2121. if (hmapout==NULL)
  2122. return ZR_NOALLOC;
  2123. obuf = (char*)MapViewOfFile(hmapout,FILE_MAP_ALL_ACCESS,0,0,size);
  2124. if (obuf==0)
  2125. {
  2126. CloseHandle(hmapout);
  2127. hmapout=0;
  2128. return ZR_NOALLOC;
  2129. }
  2130. }
  2131. ocanseek=true;
  2132. opos=0;
  2133. mapsize=size;
  2134. return ZR_OK;
  2135. }
  2136. else
  2137. return ZR_ARGS;
  2138. }
  2139. unsigned TZip::sflush(void *param,const char *buf, unsigned *size)
  2140. { // static
  2141. if (*size==0) return 0;
  2142. TZip *zip = (TZip*)param;
  2143. unsigned int writ = zip->write(buf,*size);
  2144. if (writ!=0) *size=0;
  2145. return writ;
  2146. }
  2147. unsigned TZip::swrite(void *param,const char *buf, unsigned size)
  2148. { // static
  2149. if (size==0) return 0;
  2150. TZip *zip=(TZip*)param; return zip->write(buf,size);
  2151. }
  2152. #if 0 // -----------------------------------------------------------
  2153. unsigned int TZip::write(const char *buf,unsigned int size)
  2154. { if (obuf!=0)
  2155. { if (opos+size>=mapsize) {oerr=ZR_MEMSIZE; return 0;}
  2156. memcpy(obuf+opos, buf, size);
  2157. opos+=size;
  2158. return size;
  2159. }
  2160. else if (hfout!=0)
  2161. { DWORD writ; WriteFile(hfout,buf,size,&writ,NULL);
  2162. return writ;
  2163. }
  2164. oerr=ZR_NOTINITED; return 0;
  2165. }
  2166. #endif // -----------------------------------------------------------
  2167. //+++1.2
  2168. unsigned int TZip::write(const char *buf, unsigned int size)
  2169. {
  2170. if (obuf != 0)
  2171. {
  2172. if (opos+size >= mapsize)
  2173. {
  2174. int newmapsize = 2*mapsize>opos+size?2*mapsize:opos+size;
  2175. HANDLE hmapout2 = CreateFileMapping(INVALID_HANDLE_VALUE,NULL,PAGE_READWRITE,0,newmapsize,NULL);
  2176. if (hmapout2 == NULL)
  2177. return ZR_NOALLOC;
  2178. char *obuf2 = NULL; // this is where we've locked mmap to view.
  2179. obuf2 = (char*)MapViewOfFile(hmapout2,FILE_MAP_ALL_ACCESS,0,0,newmapsize);
  2180. if (obuf2 == 0)
  2181. {
  2182. CloseHandle(hmapout2);
  2183. hmapout2 = 0;
  2184. return ZR_NOALLOC;
  2185. }
  2186. memcpy(obuf2, obuf, mapsize);
  2187. UnmapViewOfFile(obuf);
  2188. CloseHandle(hmapout);
  2189. mapsize = newmapsize;
  2190. obuf = obuf2;
  2191. hmapout = hmapout2;
  2192. }
  2193. memcpy(obuf+opos, buf, size);
  2194. opos += size;
  2195. return size;
  2196. }
  2197. else if (hfout!=0)
  2198. {
  2199. DWORD writ = 0;
  2200. WriteFile(hfout,buf,size,&writ,NULL);
  2201. return writ;
  2202. }
  2203. oerr = ZR_NOTINITED;
  2204. return 0;
  2205. }
  2206. bool TZip::oseek(unsigned int pos)
  2207. { if (!ocanseek) {oerr=ZR_SEEK; return false;}
  2208. if (obuf!=0)
  2209. { if (pos>=mapsize) {oerr=ZR_MEMSIZE; return false;}
  2210. opos=pos;
  2211. return true;
  2212. }
  2213. else if (hfout!=0)
  2214. { SetFilePointer(hfout,pos+ooffset,NULL,FILE_BEGIN);
  2215. return true;
  2216. }
  2217. oerr=ZR_NOTINITED; return 0;
  2218. }
  2219. ZRESULT TZip::GetMemory(void **pbuf, unsigned long *plen)
  2220. { // When the user calls GetMemory, they're presumably at the end
  2221. // of all their adding. In any case, we have to add the central
  2222. // directory now, otherwise the memory we tell them won't be complete.
  2223. if (!hasputcen) AddCentral(); hasputcen=true;
  2224. if (pbuf!=NULL) *pbuf=(void*)obuf;
  2225. if (plen!=NULL) *plen=writ;
  2226. if (obuf==NULL) return ZR_NOTMMAP;
  2227. return ZR_OK;
  2228. }
  2229. ZRESULT TZip::Close()
  2230. { // if the directory hadn't already been added through a call to GetMemory,
  2231. // then we do it now
  2232. ZRESULT res=ZR_OK; if (!hasputcen) res=AddCentral(); hasputcen=true;
  2233. if (obuf!=0 && hmapout!=0) UnmapViewOfFile(obuf); obuf=0;
  2234. if (hmapout!=0) CloseHandle(hmapout); hmapout=0;
  2235. if (hfout!=0) CloseHandle(hfout); hfout=0;
  2236. return res;
  2237. }
  2238. ZRESULT TZip::open_file(const TCHAR *fn)
  2239. { hfin=0; bufin=0; selfclosehf=false; crc=CRCVAL_INITIAL; isize=0; csize=0; ired=0;
  2240. if (fn==0) return ZR_ARGS;
  2241. HANDLE hf = CreateFile(fn,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,0,NULL);
  2242. if (hf==INVALID_HANDLE_VALUE) return ZR_NOFILE;
  2243. ZRESULT res = open_handle(hf,0);
  2244. if (res!=ZR_OK) {CloseHandle(hf); return res;}
  2245. selfclosehf=true;
  2246. return ZR_OK;
  2247. }
  2248. ZRESULT TZip::open_handle(HANDLE hf,unsigned int len)
  2249. { hfin=0; bufin=0; selfclosehf=false; crc=CRCVAL_INITIAL; isize=0; csize=0; ired=0;
  2250. if (hf==0 || hf==INVALID_HANDLE_VALUE) return ZR_ARGS;
  2251. DWORD type = GetFileType(hf);
  2252. if (type==FILE_TYPE_DISK)
  2253. { ZRESULT res = GetFileInfo(hf,&attr,&isize,&times,&timestamp);
  2254. if (res!=ZR_OK) return res;
  2255. SetFilePointer(hf,0,NULL,FILE_BEGIN); // because GetFileInfo will have screwed it up
  2256. iseekable=true; hfin=hf;
  2257. return ZR_OK;
  2258. }
  2259. else
  2260. { attr= 0x80000000; // just a normal file
  2261. isize = -1; // can't know size until at the end
  2262. if (len!=0) isize=len; // unless we were told explicitly!
  2263. iseekable=false;
  2264. SYSTEMTIME st; GetLocalTime(&st);
  2265. FILETIME ft; SystemTimeToFileTime(&st,&ft);
  2266. WORD dosdate,dostime; FileTimeToDosDateTime(&ft,&dosdate,&dostime);
  2267. times.atime = filetime2timet(ft);
  2268. times.mtime = times.atime;
  2269. times.ctime = times.atime;
  2270. timestamp = (WORD)dostime | (((DWORD)dosdate)<<16);
  2271. hfin=hf;
  2272. return ZR_OK;
  2273. }
  2274. }
  2275. ZRESULT TZip::open_mem(void *src,unsigned int len)
  2276. { hfin=0; bufin=(const char*)src; selfclosehf=false; crc=CRCVAL_INITIAL; ired=0; csize=0; ired=0;
  2277. lenin=len; posin=0;
  2278. if (src==0 || len==0) return ZR_ARGS;
  2279. attr= 0x80000000; // just a normal file
  2280. isize = len;
  2281. iseekable=true;
  2282. SYSTEMTIME st; GetLocalTime(&st);
  2283. FILETIME ft; SystemTimeToFileTime(&st,&ft);
  2284. WORD dosdate,dostime; FileTimeToDosDateTime(&ft,&dosdate,&dostime);
  2285. times.atime = filetime2timet(ft);
  2286. times.mtime = times.atime;
  2287. times.ctime = times.atime;
  2288. timestamp = (WORD)dostime | (((DWORD)dosdate)<<16);
  2289. return ZR_OK;
  2290. }
  2291. ZRESULT TZip::open_dir()
  2292. { hfin=0; bufin=0; selfclosehf=false; crc=CRCVAL_INITIAL; isize=0; csize=0; ired=0;
  2293. attr= 0x41C00010; // a readable writable directory, and again directory
  2294. isize = 0;
  2295. iseekable=false;
  2296. SYSTEMTIME st; GetLocalTime(&st);
  2297. FILETIME ft; SystemTimeToFileTime(&st,&ft);
  2298. WORD dosdate,dostime; FileTimeToDosDateTime(&ft,&dosdate,&dostime);
  2299. times.atime = filetime2timet(ft);
  2300. times.mtime = times.atime;
  2301. times.ctime = times.atime;
  2302. timestamp = (WORD)dostime | (((DWORD)dosdate)<<16);
  2303. return ZR_OK;
  2304. }
  2305. unsigned TZip::sread(TState &s,char *buf,unsigned size)
  2306. { // static
  2307. TZip *zip = (TZip*)s.param;
  2308. return zip->read(buf,size);
  2309. }
  2310. unsigned TZip::read(char *buf, unsigned size)
  2311. { if (bufin!=0)
  2312. { if (posin>=lenin) return 0; // end of input
  2313. ulg red = lenin-posin;
  2314. if (red>size) red=size;
  2315. memcpy(buf, bufin+posin, red);
  2316. posin += red;
  2317. ired += red;
  2318. crc = crc32(crc, (uch*)buf, red);
  2319. return red;
  2320. }
  2321. else if (hfin!=0)
  2322. { DWORD red;
  2323. BOOL ok = ReadFile(hfin,buf,size,&red,NULL);
  2324. if (!ok) return 0;
  2325. ired += red;
  2326. crc = crc32(crc, (uch*)buf, red);
  2327. return red;
  2328. }
  2329. else {oerr=ZR_NOTINITED; return 0;}
  2330. }
  2331. ZRESULT TZip::iclose()
  2332. { if (selfclosehf && hfin!=0) CloseHandle(hfin); hfin=0;
  2333. bool mismatch = (isize!=-1 && isize!=ired);
  2334. isize=ired; // and crc has been being updated anyway
  2335. if (mismatch) return ZR_MISSIZE;
  2336. else return ZR_OK;
  2337. }
  2338. #if 0 // -----------------------------------------------------------
  2339. ZRESULT TZip::ideflate(TZipFileInfo *zfi)
  2340. { TState state;
  2341. state.readfunc=sread; state.flush_outbuf=sflush;
  2342. state.param=this; state.level=8; state.seekable=iseekable; state.err=NULL;
  2343. // the following line will make ct_init realise it has to perform the init
  2344. state.ts.static_dtree[0].dl.len = 0;
  2345. // It would be nicer if I could figure out precisely which data had to
  2346. // be initted each time, and which didn't, but that's kind of difficult.
  2347. // Maybe for the next version...
  2348. //
  2349. bi_init(state,buf, sizeof(buf), TRUE); // it used to be just 1024-size, not 16384 as here
  2350. ct_init(state,&zfi->att);
  2351. lm_init(state,state.level, &zfi->flg);
  2352. ulg sz = deflate(state);
  2353. csize=sz;
  2354. if (state.err!=NULL) return ZR_FLATE;
  2355. else return ZR_OK;
  2356. }
  2357. #endif // -----------------------------------------------------------
  2358. //+++1.2
  2359. // create state object on heap
  2360. ZRESULT TZip::ideflate(TZipFileInfo *zfi)
  2361. {
  2362. ZRESULT zr = ZR_OK;
  2363. TState* state=new TState();
  2364. (*state).readfunc=sread; (*state).flush_outbuf=sflush;
  2365. (*state).param=this; (*state).level=8; (*state).seekable=iseekable; (*state).err=NULL;
  2366. // the following line will make ct_init realise it has to perform the init
  2367. (*state).ts.static_dtree[0].dl.len = 0;
  2368. // It would be nicer if I could figure out precisely which data had to
  2369. // be initted each time, and which didn't, but that's kind of difficult.
  2370. // Maybe for the next version...
  2371. //
  2372. bi_init(*state,buf, sizeof(buf), TRUE); // it used to be just 1024-size, not 16384 as here
  2373. ct_init(*state,&zfi->att);
  2374. lm_init(*state,(*state).level, &zfi->flg);
  2375. ulg sz = deflate(*state);
  2376. csize=sz;
  2377. if ((*state).err!=NULL)
  2378. {
  2379. zr = ZR_FLATE;
  2380. }
  2381. delete state;
  2382. return zr;
  2383. }
  2384. ZRESULT TZip::istore()
  2385. { ulg size=0;
  2386. for (;;)
  2387. { unsigned int cin=read(buf,16384); if (cin<=0 || cin==(unsigned int)EOF) break;
  2388. unsigned int cout = write(buf,cin); if (cout!=cin) return ZR_MISSIZE;
  2389. size += cin;
  2390. }
  2391. csize=size;
  2392. return ZR_OK;
  2393. }
  2394. ZRESULT TZip::Add(const char *odstzn, void *src,unsigned int len, DWORD flags)
  2395. {
  2396. if (oerr)
  2397. return ZR_FAILED;
  2398. if (hasputcen)
  2399. return ZR_ENDED;
  2400. // zip has its own notion of what its names should look like: i.e. dir/file.stuff
  2401. char dstzn[MAX_PATH];
  2402. strcpy(dstzn, odstzn);
  2403. if (*dstzn == 0)
  2404. return ZR_ARGS;
  2405. char *d=dstzn;
  2406. while (*d != 0)
  2407. {
  2408. if (*d == '\\')
  2409. *d = '/'; d++;
  2410. }
  2411. bool isdir = (flags==ZIP_FOLDER);
  2412. bool needs_trailing_slash = (isdir && dstzn[strlen(dstzn)-1]!='/');
  2413. int method=DEFLATE;
  2414. if (isdir || HasZipSuffix(dstzn))
  2415. method=STORE;
  2416. // now open whatever was our input source:
  2417. ZRESULT openres;
  2418. if (flags==ZIP_FILENAME)
  2419. openres=open_file((const TCHAR*)src);
  2420. else if (flags==ZIP_HANDLE)
  2421. openres=open_handle((HANDLE)src,len);
  2422. else if (flags==ZIP_MEMORY)
  2423. openres=open_mem(src,len);
  2424. else if (flags==ZIP_FOLDER)
  2425. openres=open_dir();
  2426. else return ZR_ARGS;
  2427. if (openres!=ZR_OK)
  2428. return openres;
  2429. // A zip "entry" consists of a local header (which includes the file name),
  2430. // then the compressed data, and possibly an extended local header.
  2431. // Initialize the local header
  2432. TZipFileInfo zfi; zfi.nxt=NULL;
  2433. strcpy(zfi.name,"");
  2434. strcpy(zfi.iname,dstzn);
  2435. zfi.nam=strlen(zfi.iname);
  2436. if (needs_trailing_slash)
  2437. {
  2438. strcat(zfi.iname,"/");
  2439. zfi.nam++;
  2440. }
  2441. strcpy(zfi.zname,"");
  2442. zfi.extra=NULL; zfi.ext=0; // extra header to go after this compressed data, and its length
  2443. zfi.cextra=NULL; zfi.cext=0; // extra header to go in the central end-of-zip directory, and its length
  2444. zfi.comment=NULL; zfi.com=0; // comment, and its length
  2445. zfi.mark = 1;
  2446. zfi.dosflag = 0;
  2447. zfi.att = (ush)BINARY;
  2448. zfi.vem = (ush)0xB17; // 0xB00 is win32 os-code. 0x17 is 23 in decimal: zip 2.3
  2449. zfi.ver = (ush)20; // Needs PKUNZIP 2.0 to unzip it
  2450. zfi.tim = timestamp;
  2451. // Even though we write the header now, it will have to be rewritten, since we don't know compressed size or crc.
  2452. zfi.crc = 0; // to be updated later
  2453. zfi.flg = 8; // 8 means 'there is an extra header'. Assume for the moment that we need it.
  2454. zfi.lflg = zfi.flg; // to be updated later
  2455. zfi.how = (ush)method; // to be updated later
  2456. zfi.siz = (ulg)(method==STORE && isize>=0 ? isize : 0); // to be updated later
  2457. zfi.len = (ulg)(isize); // to be updated later
  2458. zfi.dsk = 0;
  2459. zfi.atx = attr;
  2460. zfi.off = writ+ooffset; // offset within file of the start of this local record
  2461. // stuff the 'times' structure into zfi.extra
  2462. char xloc[EB_L_UT_SIZE];
  2463. zfi.extra=xloc;
  2464. zfi.ext=EB_L_UT_SIZE;
  2465. char xcen[EB_C_UT_SIZE];
  2466. zfi.cextra=xcen;
  2467. zfi.cext=EB_C_UT_SIZE;
  2468. xloc[0] = 'U';
  2469. xloc[1] = 'T';
  2470. xloc[2] = EB_UT_LEN(3); // length of data part of e.f.
  2471. xloc[3] = 0;
  2472. xloc[4] = EB_UT_FL_MTIME | EB_UT_FL_ATIME | EB_UT_FL_CTIME;
  2473. xloc[5] = (char)(times.mtime);
  2474. xloc[6] = (char)(times.mtime >> 8);
  2475. xloc[7] = (char)(times.mtime >> 16);
  2476. xloc[8] = (char)(times.mtime >> 24);
  2477. xloc[9] = (char)(times.atime);
  2478. xloc[10] = (char)(times.atime >> 8);
  2479. xloc[11] = (char)(times.atime >> 16);
  2480. xloc[12] = (char)(times.atime >> 24);
  2481. xloc[13] = (char)(times.ctime);
  2482. xloc[14] = (char)(times.ctime >> 8);
  2483. xloc[15] = (char)(times.ctime >> 16);
  2484. xloc[16] = (char)(times.ctime >> 24);
  2485. memcpy(zfi.cextra,zfi.extra,EB_C_UT_SIZE);
  2486. zfi.cextra[EB_LEN] = EB_UT_LEN(1);
  2487. // (1) Start by writing the local header:
  2488. int r = putlocal(&zfi,swrite,this);
  2489. if (r!=ZE_OK)
  2490. {
  2491. iclose();
  2492. return ZR_WRITE;
  2493. }
  2494. writ += 4 + LOCHEAD + (unsigned int)zfi.nam + (unsigned int)zfi.ext;
  2495. if (oerr!=ZR_OK)
  2496. {
  2497. iclose();
  2498. return oerr;
  2499. }
  2500. //(2) Write deflated/stored file to zip file
  2501. ZRESULT writeres=ZR_OK;
  2502. if (!isdir && method==DEFLATE)
  2503. writeres=ideflate(&zfi);
  2504. else if (!isdir && method==STORE)
  2505. writeres=istore();
  2506. else if (isdir)
  2507. csize=0;
  2508. iclose();
  2509. writ += csize;
  2510. if (oerr!=ZR_OK)
  2511. return oerr;
  2512. if (writeres!=ZR_OK)
  2513. return ZR_WRITE;
  2514. // (3) Either rewrite the local header with correct information...
  2515. bool first_header_has_size_right = (zfi.siz==csize);
  2516. zfi.crc = crc;
  2517. zfi.siz = csize;
  2518. zfi.len = isize;
  2519. if (ocanseek)
  2520. {
  2521. zfi.how = (ush)method;
  2522. if ((zfi.flg & 1) == 0)
  2523. zfi.flg &= ~8; // clear the extended local header flag
  2524. zfi.lflg = zfi.flg;
  2525. // rewrite the local header:
  2526. if (!oseek(zfi.off-ooffset))
  2527. return ZR_SEEK;
  2528. if ((r = putlocal(&zfi, swrite,this)) != ZE_OK)
  2529. return ZR_WRITE;
  2530. if (!oseek(writ))
  2531. return ZR_SEEK;
  2532. }
  2533. else
  2534. {
  2535. // (4) ... or put an updated header at the end
  2536. if (zfi.how != (ush) method)
  2537. return ZR_NOCHANGE;
  2538. if (method==STORE && !first_header_has_size_right)
  2539. return ZR_NOCHANGE;
  2540. if ((r = putextended(&zfi, swrite,this)) != ZE_OK)
  2541. return ZR_WRITE;
  2542. writ += 16L;
  2543. zfi.flg = zfi.lflg; // if flg modified by inflate, for the central index
  2544. }
  2545. if (oerr!=ZR_OK)
  2546. return oerr;
  2547. // Keep a copy of the zipfileinfo, for our end-of-zip directory
  2548. char *cextra = new char[zfi.cext];
  2549. memcpy(cextra,zfi.cextra,zfi.cext); zfi.cextra=cextra;
  2550. TZipFileInfo *pzfi = new TZipFileInfo;
  2551. memcpy(pzfi,&zfi,sizeof(zfi));
  2552. if (zfis==NULL)
  2553. zfis=pzfi;
  2554. else
  2555. {
  2556. TZipFileInfo *z=zfis;
  2557. while (z->nxt!=NULL)
  2558. z=z->nxt;
  2559. z->nxt=pzfi;
  2560. }
  2561. return ZR_OK;
  2562. }
  2563. ZRESULT TZip::AddCentral()
  2564. { // write central directory
  2565. int numentries = 0;
  2566. ulg pos_at_start_of_central = writ;
  2567. //ulg tot_unc_size=0, tot_compressed_size=0;
  2568. bool okay=true;
  2569. for (TZipFileInfo *zfi=zfis; zfi!=NULL; )
  2570. { if (okay)
  2571. { int res = putcentral(zfi, swrite,this);
  2572. if (res!=ZE_OK) okay=false;
  2573. }
  2574. writ += 4 + CENHEAD + (unsigned int)zfi->nam + (unsigned int)zfi->cext + (unsigned int)zfi->com;
  2575. //tot_unc_size += zfi->len;
  2576. //tot_compressed_size += zfi->siz;
  2577. numentries++;
  2578. //
  2579. TZipFileInfo *zfinext = zfi->nxt;
  2580. if (zfi->cextra!=0) delete[] zfi->cextra;
  2581. delete zfi;
  2582. zfi = zfinext;
  2583. }
  2584. ulg center_size = writ - pos_at_start_of_central;
  2585. if (okay)
  2586. { int res = putend(numentries, center_size, pos_at_start_of_central+ooffset, 0, NULL, swrite,this);
  2587. if (res!=ZE_OK) okay=false;
  2588. writ += 4 + ENDHEAD + 0;
  2589. }
  2590. if (!okay) return ZR_WRITE;
  2591. return ZR_OK;
  2592. }
  2593. ZRESULT lasterrorZ=ZR_OK;
  2594. unsigned int FormatZipMessageZ(ZRESULT code, char *buf,unsigned int len)
  2595. { if (code==ZR_RECENT) code=lasterrorZ;
  2596. const char *msg="unknown zip result code";
  2597. switch (code)
  2598. { case ZR_OK: msg="Success"; break;
  2599. case ZR_NODUPH: msg="Culdn't duplicate handle"; break;
  2600. case ZR_NOFILE: msg="Couldn't create/open file"; break;
  2601. case ZR_NOALLOC: msg="Failed to allocate memory"; break;
  2602. case ZR_WRITE: msg="Error writing to file"; break;
  2603. case ZR_NOTFOUND: msg="File not found in the zipfile"; break;
  2604. case ZR_MORE: msg="Still more data to unzip"; break;
  2605. case ZR_CORRUPT: msg="Zipfile is corrupt or not a zipfile"; break;
  2606. case ZR_READ: msg="Error reading file"; break;
  2607. case ZR_ARGS: msg="Caller: faulty arguments"; break;
  2608. case ZR_PARTIALUNZ: msg="Caller: the file had already been partially unzipped"; break;
  2609. case ZR_NOTMMAP: msg="Caller: can only get memory of a memory zipfile"; break;
  2610. case ZR_MEMSIZE: msg="Caller: not enough space allocated for memory zipfile"; break;
  2611. case ZR_FAILED: msg="Caller: there was a previous error"; break;
  2612. case ZR_ENDED: msg="Caller: additions to the zip have already been ended"; break;
  2613. case ZR_ZMODE: msg="Caller: mixing creation and opening of zip"; break;
  2614. case ZR_NOTINITED: msg="Zip-bug: internal initialisation not completed"; break;
  2615. case ZR_SEEK: msg="Zip-bug: trying to seek the unseekable"; break;
  2616. case ZR_MISSIZE: msg="Zip-bug: the anticipated size turned out wrong"; break;
  2617. case ZR_NOCHANGE: msg="Zip-bug: tried to change mind, but not allowed"; break;
  2618. case ZR_FLATE: msg="Zip-bug: an internal error during flation"; break;
  2619. }
  2620. unsigned int mlen=(unsigned int)strlen(msg);
  2621. if (buf==0 || len==0) return mlen;
  2622. unsigned int n=mlen; if (n+1>len) n=len-1;
  2623. strncpy(buf,msg,n); buf[n]=0;
  2624. return mlen;
  2625. }
  2626. typedef struct
  2627. { DWORD flag;
  2628. TZip *zip;
  2629. } TZipHandleData;
  2630. HZIP CreateZipZ(void *z,unsigned int len,DWORD flags)
  2631. {
  2632. tzset();
  2633. TZip *zip = new TZip();
  2634. lasterrorZ = zip->Create(z,len,flags);
  2635. if (lasterrorZ != ZR_OK)
  2636. {
  2637. delete zip;
  2638. return 0;
  2639. }
  2640. TZipHandleData *han = new TZipHandleData;
  2641. han->flag = 2;
  2642. han->zip = zip;
  2643. return (HZIP)han;
  2644. }
  2645. ZRESULT ZipAdd(HZIP hz, const TCHAR *dstzn, void *src, unsigned int len, DWORD flags)
  2646. {
  2647. if (hz == 0)
  2648. {
  2649. lasterrorZ = ZR_ARGS;
  2650. return ZR_ARGS;
  2651. }
  2652. TZipHandleData *han = (TZipHandleData*)hz;
  2653. if (han->flag != 2)
  2654. {
  2655. lasterrorZ = ZR_ZMODE;
  2656. return ZR_ZMODE;
  2657. }
  2658. TZip *zip = han->zip;
  2659. if (flags == ZIP_FILENAME)
  2660. {
  2661. char szDest[MAX_PATH*2];
  2662. memset(szDest, 0, sizeof(szDest));
  2663. #ifdef _UNICODE
  2664. // need to convert Unicode dest to ANSI
  2665. int nActualChars = WideCharToMultiByte(CP_ACP, // code page
  2666. 0, // performance and mapping flags
  2667. (LPCWSTR) dstzn, // wide-character string
  2668. -1, // number of chars in string
  2669. szDest, // buffer for new string
  2670. MAX_PATH*2-2, // size of buffer
  2671. NULL, // default for unmappable chars
  2672. NULL); // set when default char used
  2673. if (nActualChars == 0)
  2674. return ZR_ARGS;
  2675. #else
  2676. strcpy(szDest, dstzn);
  2677. #endif
  2678. lasterrorZ = zip->Add(szDest, src, len, flags);
  2679. }
  2680. else
  2681. {
  2682. lasterrorZ = zip->Add((char *)dstzn, src, len, flags);
  2683. }
  2684. return lasterrorZ;
  2685. }
  2686. ZRESULT ZipGetMemory(HZIP hz, void **buf, unsigned long *len)
  2687. { if (hz==0) {if (buf!=0) *buf=0; if (len!=0) *len=0; lasterrorZ=ZR_ARGS;return ZR_ARGS;}
  2688. TZipHandleData *han = (TZipHandleData*)hz;
  2689. if (han->flag!=2) {lasterrorZ=ZR_ZMODE;return ZR_ZMODE;}
  2690. TZip *zip = han->zip;
  2691. lasterrorZ = zip->GetMemory(buf,len);
  2692. return lasterrorZ;
  2693. }
  2694. ZRESULT CloseZipZ(HZIP hz)
  2695. { if (hz==0) {lasterrorZ=ZR_ARGS;return ZR_ARGS;}
  2696. TZipHandleData *han = (TZipHandleData*)hz;
  2697. if (han->flag!=2) {lasterrorZ=ZR_ZMODE;return ZR_ZMODE;}
  2698. TZip *zip = han->zip;
  2699. lasterrorZ = zip->Close();
  2700. delete zip;
  2701. delete han;
  2702. return lasterrorZ;
  2703. }
  2704. bool IsZipHandleZ(HZIP hz)
  2705. { if (hz==0) return true;
  2706. TZipHandleData *han = (TZipHandleData*)hz;
  2707. return (han->flag==2);
  2708. }
  2709. //+++1.2
  2710. /**
  2711. * Added by Renaud Deysine. This fonctionnality was missing in API
  2712. * @brief Add a folder to the zip file. Empty folders will also be added.
  2713. * This method add recursively the content of a directory
  2714. * @param AbsolutePath like "C:\\Windows" or "C:\\Windows\"
  2715. * @param DirToAdd like "System32"
  2716. *
  2717. */
  2718. BOOL AddFolderContent(HZIP hZip, TCHAR* AbsolutePath, TCHAR* DirToAdd)
  2719. {
  2720. HANDLE hFind; // file handle
  2721. WIN32_FIND_DATA FindFileData;
  2722. TCHAR PathToSearchInto [MAX_PATH] = {0};
  2723. if (NULL != DirToAdd)
  2724. {
  2725. ZipAdd(hZip, DirToAdd, 0, 0, ZIP_FOLDER);
  2726. }
  2727. // Construct the path to search into "C:\\Windows\\System32\\*"
  2728. _tcscpy(PathToSearchInto, AbsolutePath);
  2729. _tcscat(PathToSearchInto, _T("\\"));
  2730. _tcscat(PathToSearchInto, DirToAdd);
  2731. _tcscat(PathToSearchInto, _T("\\*"));
  2732. hFind = FindFirstFile(PathToSearchInto,&FindFileData); // find the first file
  2733. if(hFind == INVALID_HANDLE_VALUE)
  2734. {
  2735. return FALSE;
  2736. }
  2737. bool bSearch = true;
  2738. while(bSearch) // until we finds an entry
  2739. {
  2740. if(FindNextFile(hFind,&FindFileData))
  2741. {
  2742. // Don't care about . and ..
  2743. //if(IsDots(FindFileData.cFileName))
  2744. if ((_tcscmp(FindFileData.cFileName, _T(".")) == 0) ||
  2745. (_tcscmp(FindFileData.cFileName, _T("..")) == 0))
  2746. continue;
  2747. // We have found a directory
  2748. if((FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
  2749. {
  2750. TCHAR RelativePathNewDirFound[MAX_PATH] = {0};
  2751. _tcscat(RelativePathNewDirFound, DirToAdd);
  2752. _tcscat(RelativePathNewDirFound, _T("\\"));
  2753. _tcscat(RelativePathNewDirFound, FindFileData.cFileName);
  2754. // Recursive call with the new directory found
  2755. if (AddFolderContent(hZip, AbsolutePath, RelativePathNewDirFound)== FALSE)
  2756. {
  2757. return FALSE ;
  2758. }
  2759. }
  2760. // We have found a file
  2761. else
  2762. {
  2763. // Add the found file to the zip file
  2764. TCHAR RelativePathNewFileFound[MAX_PATH] = {0};
  2765. _tcscpy(RelativePathNewFileFound, DirToAdd);
  2766. _tcscat(RelativePathNewFileFound, _T("\\"));
  2767. _tcscat(RelativePathNewFileFound, FindFileData.cFileName);
  2768. if (ZipAdd(hZip, RelativePathNewFileFound, RelativePathNewFileFound, 0, ZIP_FILENAME) != ZR_OK)
  2769. {
  2770. return FALSE;
  2771. }
  2772. }
  2773. }//FindNextFile
  2774. else
  2775. {
  2776. if(GetLastError() == ERROR_NO_MORE_FILES) // no more files there
  2777. bSearch = false;
  2778. else {
  2779. // some error occured, close the handle and return FALSE
  2780. FindClose(hFind);
  2781. return FALSE;
  2782. }
  2783. }
  2784. }//while
  2785. FindClose(hFind); // closing file handle
  2786. return true;
  2787. }