process.c 106 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089
  1. /*
  2. Copyright (c) 1990-2009 Info-ZIP. All rights reserved.
  3. See the accompanying file LICENSE, version 2009-Jan-02 or later
  4. (the contents of which are also included in unzip.h) for terms of use.
  5. If, for some reason, all these files are missing, the Info-ZIP license
  6. also may be found at: ftp://ftp.info-zip.org/pub/infozip/license.html
  7. */
  8. /*---------------------------------------------------------------------------
  9. process.c
  10. This file contains the top-level routines for processing multiple zipfiles.
  11. Contains: process_zipfiles()
  12. free_G_buffers()
  13. do_seekable()
  14. file_size()
  15. rec_find()
  16. find_ecrec64()
  17. find_ecrec()
  18. process_zip_cmmnt()
  19. process_cdir_file_hdr()
  20. get_cdir_ent()
  21. process_local_file_hdr()
  22. getZip64Data()
  23. ef_scan_for_izux()
  24. getRISCOSexfield()
  25. ---------------------------------------------------------------------------*/
  26. #define UNZIP_INTERNAL
  27. #include "unzip.h"
  28. #ifdef WINDLL
  29. # ifdef POCKET_UNZIP
  30. # include "wince/intrface.h"
  31. # else
  32. # include "windll/windll.h"
  33. # endif
  34. #endif
  35. #if defined(DYNALLOC_CRCTAB) || defined(UNICODE_SUPPORT)
  36. # include "crc32.h"
  37. #endif
  38. static int do_seekable OF((__GPRO__ int lastchance));
  39. #ifdef DO_SAFECHECK_2GB
  40. # ifdef USE_STRM_INPUT
  41. static zoff_t file_size OF((FILE *file));
  42. # else
  43. static zoff_t file_size OF((int fh));
  44. # endif
  45. #endif /* DO_SAFECHECK_2GB */
  46. static int rec_find OF((__GPRO__ zoff_t, char *, int));
  47. static int find_ecrec64 OF((__GPRO__ zoff_t searchlen));
  48. static int find_ecrec OF((__GPRO__ zoff_t searchlen));
  49. static int process_zip_cmmnt OF((__GPRO));
  50. static int get_cdir_ent OF((__GPRO));
  51. #ifdef IZ_HAVE_UXUIDGID
  52. static int read_ux3_value OF((ZCONST uch *dbuf, unsigned uidgid_sz,
  53. ulg *p_uidgid));
  54. #endif /* IZ_HAVE_UXUIDGID */
  55. static ZCONST char Far CannotAllocateBuffers[] =
  56. "error: cannot allocate unzip buffers\n";
  57. #ifdef SFX
  58. static ZCONST char Far CannotFindMyself[] =
  59. "unzipsfx: cannot find myself! [%s]\n";
  60. # ifdef CHEAP_SFX_AUTORUN
  61. static ZCONST char Far AutorunPrompt[] =
  62. "\nAuto-run command: %s\nExecute this command? [y/n] ";
  63. static ZCONST char Far NotAutoRunning[] =
  64. "Not executing auto-run command.";
  65. # endif
  66. #else /* !SFX */
  67. /* process_zipfiles() strings */
  68. # if (defined(IZ_CHECK_TZ) && defined(USE_EF_UT_TIME))
  69. static ZCONST char Far WarnInvalidTZ[] =
  70. "Warning: TZ environment variable not found, cannot use UTC times!!\n";
  71. # endif
  72. # if !(defined(UNIX) || defined(AMIGA))
  73. static ZCONST char Far CannotFindWildcardMatch[] =
  74. "%s: cannot find any matches for wildcard specification \"%s\".\n";
  75. # endif /* !(UNIX || AMIGA) */
  76. static ZCONST char Far FilesProcessOK[] =
  77. "%d archive%s successfully processed.\n";
  78. static ZCONST char Far ArchiveWarning[] =
  79. "%d archive%s had warnings but no fatal errors.\n";
  80. static ZCONST char Far ArchiveFatalError[] =
  81. "%d archive%s had fatal errors.\n";
  82. static ZCONST char Far FileHadNoZipfileDir[] =
  83. "%d file%s had no zipfile directory.\n";
  84. static ZCONST char Far ZipfileWasDir[] = "1 \"zipfile\" was a directory.\n";
  85. static ZCONST char Far ManyZipfilesWereDir[] =
  86. "%d \"zipfiles\" were directories.\n";
  87. static ZCONST char Far NoZipfileFound[] = "No zipfiles found.\n";
  88. /* do_seekable() strings */
  89. # ifdef UNIX
  90. static ZCONST char Far CannotFindZipfileDirMsg[] =
  91. "%s: cannot find zipfile directory in one of %s or\n\
  92. %s%s.zip, and cannot find %s, period.\n";
  93. static ZCONST char Far CannotFindEitherZipfile[] =
  94. "%s: cannot find or open %s, %s.zip or %s.\n";
  95. # else /* !UNIX */
  96. static ZCONST char Far CannotFindZipfileDirMsg[] =
  97. "%s: cannot find zipfile directory in %s,\n\
  98. %sand cannot find %s, period.\n";
  99. # ifdef VMS
  100. static ZCONST char Far CannotFindEitherZipfile[] =
  101. "%s: cannot find %s (%s).\n";
  102. # else /* !VMS */
  103. static ZCONST char Far CannotFindEitherZipfile[] =
  104. "%s: cannot find either %s or %s.\n";
  105. # endif /* ?VMS */
  106. # endif /* ?UNIX */
  107. extern ZCONST char Far Zipnfo[]; /* in unzip.c */
  108. #ifndef WINDLL
  109. static ZCONST char Far Unzip[] = "unzip";
  110. #else
  111. static ZCONST char Far Unzip[] = "UnZip DLL";
  112. #endif
  113. #ifdef DO_SAFECHECK_2GB
  114. static ZCONST char Far ZipfileTooBig[] =
  115. "Trying to read large file (> 2 GiB) without large file support\n";
  116. #endif /* DO_SAFECHECK_2GB */
  117. static ZCONST char Far MaybeExe[] =
  118. "note: %s may be a plain executable, not an archive\n";
  119. static ZCONST char Far CentDirNotInZipMsg[] = "\n\
  120. [%s]:\n\
  121. Zipfile is disk %lu of a multi-disk archive, and this is not the disk on\n\
  122. which the central zipfile directory begins (disk %lu).\n";
  123. static ZCONST char Far EndCentDirBogus[] =
  124. "\nwarning [%s]: end-of-central-directory record claims this\n\
  125. is disk %lu but that the central directory starts on disk %lu; this is a\n\
  126. contradiction. Attempting to process anyway.\n";
  127. # ifdef NO_MULTIPART
  128. static ZCONST char Far NoMultiDiskArcSupport[] =
  129. "\nerror [%s]: zipfile is part of multi-disk archive\n\
  130. (sorry, not yet supported).\n";
  131. static ZCONST char Far MaybePakBug[] = "warning [%s]:\
  132. zipfile claims to be 2nd disk of a 2-part archive;\n\
  133. attempting to process anyway. If no further errors occur, this archive\n\
  134. was probably created by PAK v2.51 or earlier. This bug was reported to\n\
  135. NoGate in March 1991 and was supposed to have been fixed by mid-1991; as\n\
  136. of mid-1992 it still hadn't been. (If further errors do occur, archive\n\
  137. was probably created by PKZIP 2.04c or later; UnZip does not yet support\n\
  138. multi-part archives.)\n";
  139. # else
  140. static ZCONST char Far MaybePakBug[] = "warning [%s]:\
  141. zipfile claims to be last disk of a multi-part archive;\n\
  142. attempting to process anyway, assuming all parts have been concatenated\n\
  143. together in order. Expect \"errors\" and warnings...true multi-part support\
  144. \n doesn't exist yet (coming soon).\n";
  145. # endif
  146. static ZCONST char Far ExtraBytesAtStart[] =
  147. "warning [%s]: %s extra byte%s at beginning or within zipfile\n\
  148. (attempting to process anyway)\n";
  149. #endif /* ?SFX */
  150. #if ((!defined(WINDLL) && !defined(SFX)) || !defined(NO_ZIPINFO))
  151. static ZCONST char Far LogInitline[] = "Archive: %s\n";
  152. #endif
  153. static ZCONST char Far MissingBytes[] =
  154. "error [%s]: missing %s bytes in zipfile\n\
  155. (attempting to process anyway)\n";
  156. static ZCONST char Far NullCentDirOffset[] =
  157. "error [%s]: NULL central directory offset\n\
  158. (attempting to process anyway)\n";
  159. static ZCONST char Far ZipfileEmpty[] = "warning [%s]: zipfile is empty\n";
  160. static ZCONST char Far CentDirStartNotFound[] =
  161. "error [%s]: start of central directory not found;\n\
  162. zipfile corrupt.\n%s";
  163. static ZCONST char Far Cent64EndSigSearchErr[] =
  164. "fatal error: read failure while seeking for End-of-centdir-64 signature.\n\
  165. This zipfile is corrupt.\n";
  166. static ZCONST char Far Cent64EndSigSearchOff[] =
  167. "error: End-of-centdir-64 signature not where expected (prepended bytes?)\n\
  168. (attempting to process anyway)\n";
  169. #ifndef SFX
  170. static ZCONST char Far CentDirTooLong[] =
  171. "error [%s]: reported length of central directory is\n\
  172. %s bytes too long (Atari STZip zipfile? J.H.Holm ZIPSPLIT 1.1\n\
  173. zipfile?). Compensating...\n";
  174. static ZCONST char Far CentDirEndSigNotFound[] = "\
  175. End-of-central-directory signature not found. Either this file is not\n\
  176. a zipfile, or it constitutes one disk of a multi-part archive. In the\n\
  177. latter case the central directory and zipfile comment will be found on\n\
  178. the last disk(s) of this archive.\n";
  179. #else /* SFX */
  180. static ZCONST char Far CentDirEndSigNotFound[] =
  181. " End-of-central-directory signature not found.\n";
  182. #endif /* ?SFX */
  183. #ifdef TIMESTAMP
  184. static ZCONST char Far ZipTimeStampFailed[] =
  185. "warning: cannot set time for %s\n";
  186. static ZCONST char Far ZipTimeStampSuccess[] =
  187. "Updated time stamp for %s.\n";
  188. #endif
  189. static ZCONST char Far ZipfileCommTrunc1[] =
  190. "\ncaution: zipfile comment truncated\n";
  191. #ifndef NO_ZIPINFO
  192. static ZCONST char Far NoZipfileComment[] =
  193. "There is no zipfile comment.\n";
  194. static ZCONST char Far ZipfileCommentDesc[] =
  195. "The zipfile comment is %u bytes long and contains the following text:\n";
  196. static ZCONST char Far ZipfileCommBegin[] =
  197. "======================== zipfile comment begins\
  198. ==========================\n";
  199. static ZCONST char Far ZipfileCommEnd[] =
  200. "========================= zipfile comment ends\
  201. ===========================\n";
  202. static ZCONST char Far ZipfileCommTrunc2[] =
  203. "\n The zipfile comment is truncated.\n";
  204. #endif /* !NO_ZIPINFO */
  205. #ifdef UNICODE_SUPPORT
  206. static ZCONST char Far UnicodeVersionError[] =
  207. "\nwarning: Unicode Path version > 1\n";
  208. static ZCONST char Far UnicodeMismatchError[] =
  209. "\nwarning: Unicode Path checksum invalid\n";
  210. #endif
  211. /*******************************/
  212. /* Function process_zipfiles() */
  213. /*******************************/
  214. int process_zipfiles(__G) /* return PK-type error code */
  215. __GDEF
  216. {
  217. #ifndef SFX
  218. char *lastzipfn = (char *)NULL;
  219. int NumWinFiles, NumLoseFiles, NumWarnFiles;
  220. int NumMissDirs, NumMissFiles;
  221. #endif
  222. int error=0, error_in_archive=0;
  223. /*---------------------------------------------------------------------------
  224. Start by allocating buffers and (re)constructing the various PK signature
  225. strings.
  226. ---------------------------------------------------------------------------*/
  227. G.inbuf = (uch *)malloc(INBUFSIZ + 4); /* 4 extra for hold[] (below) */
  228. G.outbuf = (uch *)malloc(OUTBUFSIZ + 1); /* 1 extra for string term. */
  229. if ((G.inbuf == (uch *)NULL) || (G.outbuf == (uch *)NULL)) {
  230. Info(slide, 0x401, ((char *)slide,
  231. LoadFarString(CannotAllocateBuffers)));
  232. return(PK_MEM);
  233. }
  234. G.hold = G.inbuf + INBUFSIZ; /* to check for boundary-spanning sigs */
  235. #ifndef VMS /* VMS uses its own buffer scheme for textmode flush(). */
  236. #ifdef SMALL_MEM
  237. G.outbuf2 = G.outbuf+RAWBUFSIZ; /* never changes */
  238. #endif
  239. #endif /* !VMS */
  240. #if 0 /* CRC_32_TAB has been NULLified by CONSTRUCTGLOBALS !!!! */
  241. /* allocate the CRC table later when we know we can read zipfile data */
  242. CRC_32_TAB = NULL;
  243. #endif /* 0 */
  244. /* finish up initialization of magic signature strings */
  245. local_hdr_sig[0] /* = extd_local_sig[0] */ = /* ASCII 'P', */
  246. central_hdr_sig[0] = end_central_sig[0] = /* not EBCDIC */
  247. end_centloc64_sig[0] = end_central64_sig[0] = 0x50;
  248. local_hdr_sig[1] /* = extd_local_sig[1] */ = /* ASCII 'K', */
  249. central_hdr_sig[1] = end_central_sig[1] = /* not EBCDIC */
  250. end_centloc64_sig[1] = end_central64_sig[1] = 0x4B;
  251. /*---------------------------------------------------------------------------
  252. Make sure timezone info is set correctly; localtime() returns GMT on some
  253. OSes (e.g., Solaris 2.x) if this isn't done first. The ifdefs around
  254. tzset() were initially copied from dos_to_unix_time() in fileio.c. They
  255. may still be too strict; any listed OS that supplies tzset(), regardless
  256. of whether the function does anything, should be removed from the ifdefs.
  257. ---------------------------------------------------------------------------*/
  258. #if (defined(WIN32) && defined(USE_EF_UT_TIME))
  259. /* For the Win32 environment, we may have to "prepare" the environment
  260. prior to the tzset() call, to work around tzset() implementation bugs.
  261. */
  262. iz_w32_prepareTZenv();
  263. #endif
  264. #if (defined(IZ_CHECK_TZ) && defined(USE_EF_UT_TIME))
  265. # ifndef VALID_TIMEZONE
  266. # define VALID_TIMEZONE(tmp) \
  267. (((tmp = getenv("TZ")) != NULL) && (*tmp != '\0'))
  268. # endif
  269. {
  270. char *p;
  271. G.tz_is_valid = VALID_TIMEZONE(p);
  272. # ifndef SFX
  273. if (!G.tz_is_valid) {
  274. Info(slide, 0x401, ((char *)slide, LoadFarString(WarnInvalidTZ)));
  275. error_in_archive = error = PK_WARN;
  276. }
  277. # endif /* !SFX */
  278. }
  279. #endif /* IZ_CHECK_TZ && USE_EF_UT_TIME */
  280. /* For systems that do not have tzset() but supply this function using another
  281. name (_tzset() or something similar), an appropiate "#define tzset ..."
  282. should be added to the system specifc configuration section. */
  283. #if (!defined(T20_VMS) && !defined(MACOS) && !defined(RISCOS) && !defined(QDOS))
  284. #if (!defined(BSD) && !defined(MTS) && !defined(CMS_MVS) && !defined(TANDEM))
  285. tzset();
  286. #endif
  287. #endif
  288. /* Initialize UnZip's built-in pseudo hard-coded "ISO <--> OEM" translation,
  289. depending on the detected codepage setup. */
  290. #ifdef NEED_ISO_OEM_INIT
  291. prepare_ISO_OEM_translat(__G);
  292. #endif
  293. /*---------------------------------------------------------------------------
  294. Initialize the internal flag holding the mode of processing "overwrite
  295. existing file" cases. We do not use the calling interface flags directly
  296. because the overwrite mode may be changed by user interaction while
  297. processing archive files. Such a change should not affect the option
  298. settings as passed through the DLL calling interface.
  299. In case of conflicting options, the 'safer' flag uO.overwrite_none takes
  300. precedence.
  301. ---------------------------------------------------------------------------*/
  302. G.overwrite_mode = (uO.overwrite_none ? OVERWRT_NEVER :
  303. (uO.overwrite_all ? OVERWRT_ALWAYS : OVERWRT_QUERY));
  304. /*---------------------------------------------------------------------------
  305. Match (possible) wildcard zipfile specification with existing files and
  306. attempt to process each. If no hits, try again after appending ".zip"
  307. suffix. If still no luck, give up.
  308. ---------------------------------------------------------------------------*/
  309. #ifdef SFX
  310. if ((error = do_seekable(__G__ 0)) == PK_NOZIP) {
  311. #ifdef EXE_EXTENSION
  312. int len=strlen(G.argv0);
  313. /* append .exe if appropriate; also .sfx? */
  314. if ( (G.zipfn = (char *)malloc(len+sizeof(EXE_EXTENSION))) !=
  315. (char *)NULL ) {
  316. strcpy(G.zipfn, G.argv0);
  317. strcpy(G.zipfn+len, EXE_EXTENSION);
  318. error = do_seekable(__G__ 0);
  319. free(G.zipfn);
  320. G.zipfn = G.argv0; /* for "cannot find myself" message only */
  321. }
  322. #endif /* EXE_EXTENSION */
  323. #ifdef WIN32
  324. G.zipfn = G.argv0; /* for "cannot find myself" message only */
  325. #endif
  326. }
  327. if (error) {
  328. if (error == IZ_DIR)
  329. error_in_archive = PK_NOZIP;
  330. else
  331. error_in_archive = error;
  332. if (error == PK_NOZIP)
  333. Info(slide, 1, ((char *)slide, LoadFarString(CannotFindMyself),
  334. G.zipfn));
  335. }
  336. #ifdef CHEAP_SFX_AUTORUN
  337. if (G.autorun_command[0] && !uO.qflag) { /* NO autorun without prompt! */
  338. Info(slide, 0x81, ((char *)slide, LoadFarString(AutorunPrompt),
  339. FnFilter1(G.autorun_command)));
  340. if (fgets(G.answerbuf, 9, stdin) != (char *)NULL
  341. && toupper(*G.answerbuf) == 'Y')
  342. system(G.autorun_command);
  343. else
  344. Info(slide, 1, ((char *)slide, LoadFarString(NotAutoRunning)));
  345. }
  346. #endif /* CHEAP_SFX_AUTORUN */
  347. #else /* !SFX */
  348. NumWinFiles = NumLoseFiles = NumWarnFiles = 0;
  349. NumMissDirs = NumMissFiles = 0;
  350. while ((G.zipfn = do_wild(__G__ G.wildzipfn)) != (char *)NULL) {
  351. Trace((stderr, "do_wild( %s ) returns %s\n", G.wildzipfn, G.zipfn));
  352. lastzipfn = G.zipfn;
  353. /* print a blank line between the output of different zipfiles */
  354. if (!uO.qflag && error != PK_NOZIP && error != IZ_DIR
  355. #ifdef TIMESTAMP
  356. && (!uO.T_flag || uO.zipinfo_mode)
  357. #endif
  358. && (NumWinFiles+NumLoseFiles+NumWarnFiles+NumMissFiles) > 0)
  359. (*G.message)((zvoid *)&G, (uch *)"\n", 1L, 0);
  360. if ((error = do_seekable(__G__ 0)) == PK_WARN)
  361. ++NumWarnFiles;
  362. else if (error == IZ_DIR)
  363. ++NumMissDirs;
  364. else if (error == PK_NOZIP)
  365. ++NumMissFiles;
  366. else if (error != PK_OK)
  367. ++NumLoseFiles;
  368. else
  369. ++NumWinFiles;
  370. Trace((stderr, "do_seekable(0) returns %d\n", error));
  371. if (error != IZ_DIR && error > error_in_archive)
  372. error_in_archive = error;
  373. #ifdef WINDLL
  374. if (error == IZ_CTRLC) {
  375. free_G_buffers(__G);
  376. return error;
  377. }
  378. #endif
  379. } /* end while-loop (wildcard zipfiles) */
  380. if ((NumWinFiles + NumWarnFiles + NumLoseFiles) == 0 &&
  381. (NumMissDirs + NumMissFiles) == 1 && lastzipfn != (char *)NULL)
  382. {
  383. #if (!defined(UNIX) && !defined(AMIGA)) /* filenames with wildcard characters */
  384. if (iswild(G.wildzipfn)) {
  385. if (iswild(lastzipfn)) {
  386. NumMissDirs = NumMissFiles = 0;
  387. error_in_archive = PK_COOL;
  388. if (uO.qflag < 3)
  389. Info(slide, 0x401, ((char *)slide,
  390. LoadFarString(CannotFindWildcardMatch),
  391. LoadFarStringSmall((uO.zipinfo_mode ? Zipnfo : Unzip)),
  392. G.wildzipfn));
  393. }
  394. } else
  395. #endif
  396. {
  397. #ifndef VMS
  398. /* 2004-11-24 SMS.
  399. * VMS has already tried a default file type of ".zip" in
  400. * do_wild(), so adding ZSUFX here only causes confusion by
  401. * corrupting some valid (though nonexistent) file names.
  402. * Complaining below about "fred;4.zip" is unlikely to be
  403. * helpful to the victim.
  404. */
  405. /* 2005-08-14 Chr. Spieler
  406. * Although we already "know" the failure result, we call
  407. * do_seekable() again with the same zipfile name (and the
  408. * lastchance flag set), just to trigger the error report...
  409. */
  410. #if defined(UNIX) || defined(QDOS)
  411. char *p =
  412. #endif
  413. strcpy(lastzipfn + strlen(lastzipfn), ZSUFX);
  414. #endif /* !VMS */
  415. G.zipfn = lastzipfn;
  416. NumMissDirs = NumMissFiles = 0;
  417. error_in_archive = PK_COOL;
  418. #if defined(UNIX) || defined(QDOS)
  419. /* only Unix has case-sensitive filesystems */
  420. /* Well FlexOS (sometimes) also has them, but support is per media */
  421. /* and a pig to code for, so treat as case insensitive for now */
  422. /* we do this under QDOS to check for .zip as well as _zip */
  423. if ((error = do_seekable(__G__ 0)) == PK_NOZIP || error == IZ_DIR) {
  424. if (error == IZ_DIR)
  425. ++NumMissDirs;
  426. strcpy(p, ALT_ZSUFX);
  427. error = do_seekable(__G__ 1);
  428. }
  429. #else
  430. error = do_seekable(__G__ 1);
  431. #endif
  432. Trace((stderr, "do_seekable(1) returns %d\n", error));
  433. switch (error) {
  434. case PK_WARN:
  435. ++NumWarnFiles;
  436. break;
  437. case IZ_DIR:
  438. ++NumMissDirs;
  439. error = PK_NOZIP;
  440. break;
  441. case PK_NOZIP:
  442. /* increment again => bug:
  443. "1 file had no zipfile directory." */
  444. /* ++NumMissFiles */ ;
  445. break;
  446. default:
  447. if (error)
  448. ++NumLoseFiles;
  449. else
  450. ++NumWinFiles;
  451. break;
  452. }
  453. if (error > error_in_archive)
  454. error_in_archive = error;
  455. #ifdef WINDLL
  456. if (error == IZ_CTRLC) {
  457. free_G_buffers(__G);
  458. return error;
  459. }
  460. #endif
  461. }
  462. }
  463. #endif /* ?SFX */
  464. /*---------------------------------------------------------------------------
  465. Print summary of all zipfiles, assuming zipfile spec was a wildcard (no
  466. need for a summary if just one zipfile).
  467. ---------------------------------------------------------------------------*/
  468. #ifndef SFX
  469. if (iswild(G.wildzipfn) && uO.qflag < 3
  470. #ifdef TIMESTAMP
  471. && !(uO.T_flag && !uO.zipinfo_mode && uO.qflag > 1)
  472. #endif
  473. )
  474. {
  475. if ((NumMissFiles + NumLoseFiles + NumWarnFiles > 0 || NumWinFiles != 1)
  476. #ifdef TIMESTAMP
  477. && !(uO.T_flag && !uO.zipinfo_mode && uO.qflag)
  478. #endif
  479. && !(uO.tflag && uO.qflag > 1))
  480. (*G.message)((zvoid *)&G, (uch *)"\n", 1L, 0x401);
  481. if ((NumWinFiles > 1) ||
  482. (NumWinFiles == 1 &&
  483. NumMissDirs + NumMissFiles + NumLoseFiles + NumWarnFiles > 0))
  484. Info(slide, 0x401, ((char *)slide, LoadFarString(FilesProcessOK),
  485. NumWinFiles, (NumWinFiles == 1)? " was" : "s were"));
  486. if (NumWarnFiles > 0)
  487. Info(slide, 0x401, ((char *)slide, LoadFarString(ArchiveWarning),
  488. NumWarnFiles, (NumWarnFiles == 1)? "" : "s"));
  489. if (NumLoseFiles > 0)
  490. Info(slide, 0x401, ((char *)slide, LoadFarString(ArchiveFatalError),
  491. NumLoseFiles, (NumLoseFiles == 1)? "" : "s"));
  492. if (NumMissFiles > 0)
  493. Info(slide, 0x401, ((char *)slide,
  494. LoadFarString(FileHadNoZipfileDir), NumMissFiles,
  495. (NumMissFiles == 1)? "" : "s"));
  496. if (NumMissDirs == 1)
  497. Info(slide, 0x401, ((char *)slide, LoadFarString(ZipfileWasDir)));
  498. else if (NumMissDirs > 0)
  499. Info(slide, 0x401, ((char *)slide,
  500. LoadFarString(ManyZipfilesWereDir), NumMissDirs));
  501. if (NumWinFiles + NumLoseFiles + NumWarnFiles == 0)
  502. Info(slide, 0x401, ((char *)slide, LoadFarString(NoZipfileFound)));
  503. }
  504. #endif /* !SFX */
  505. /* free allocated memory */
  506. free_G_buffers(__G);
  507. return error_in_archive;
  508. } /* end function process_zipfiles() */
  509. /*****************************/
  510. /* Function free_G_buffers() */
  511. /*****************************/
  512. void free_G_buffers(__G) /* releases all memory allocated in global vars */
  513. __GDEF
  514. {
  515. #ifndef SFX
  516. unsigned i;
  517. #endif
  518. #ifdef SYSTEM_SPECIFIC_DTOR
  519. SYSTEM_SPECIFIC_DTOR(__G);
  520. #endif
  521. inflate_free(__G);
  522. checkdir(__G__ (char *)NULL, END);
  523. #ifdef DYNALLOC_CRCTAB
  524. if (CRC_32_TAB) {
  525. free_crc_table();
  526. CRC_32_TAB = NULL;
  527. }
  528. #endif
  529. if (G.key != (char *)NULL) {
  530. free(G.key);
  531. G.key = (char *)NULL;
  532. }
  533. if (G.extra_field != (uch *)NULL) {
  534. free(G.extra_field);
  535. G.extra_field = (uch *)NULL;
  536. }
  537. #if (!defined(VMS) && !defined(SMALL_MEM))
  538. /* VMS uses its own buffer scheme for textmode flush() */
  539. if (G.outbuf2) {
  540. free(G.outbuf2); /* malloc'd ONLY if unshrink and -a */
  541. G.outbuf2 = (uch *)NULL;
  542. }
  543. #endif
  544. if (G.outbuf)
  545. free(G.outbuf);
  546. if (G.inbuf)
  547. free(G.inbuf);
  548. G.inbuf = G.outbuf = (uch *)NULL;
  549. #ifdef UNICODE_SUPPORT
  550. if (G.filename_full) {
  551. free(G.filename_full);
  552. G.filename_full = (char *)NULL;
  553. G.fnfull_bufsize = 0;
  554. }
  555. #endif /* UNICODE_SUPPORT */
  556. #ifndef SFX
  557. for (i = 0; i < DIR_BLKSIZ; i++) {
  558. if (G.info[i].cfilname != (char Far *)NULL) {
  559. zffree(G.info[i].cfilname);
  560. G.info[i].cfilname = (char Far *)NULL;
  561. }
  562. }
  563. #endif
  564. #ifdef MALLOC_WORK
  565. if (G.area.Slide) {
  566. free(G.area.Slide);
  567. G.area.Slide = (uch *)NULL;
  568. }
  569. #endif
  570. } /* end function free_G_buffers() */
  571. /**************************/
  572. /* Function do_seekable() */
  573. /**************************/
  574. static int do_seekable(__G__ lastchance) /* return PK-type error code */
  575. __GDEF
  576. int lastchance;
  577. {
  578. #ifndef SFX
  579. /* static int no_ecrec = FALSE; SKM: moved to globals.h */
  580. int maybe_exe=FALSE;
  581. int too_weird_to_continue=FALSE;
  582. #ifdef TIMESTAMP
  583. time_t uxstamp;
  584. ulg nmember = 0L;
  585. #endif
  586. #endif
  587. int error=0, error_in_archive;
  588. /*---------------------------------------------------------------------------
  589. Open the zipfile for reading in BINARY mode to prevent CR/LF translation,
  590. which would corrupt the bit streams.
  591. ---------------------------------------------------------------------------*/
  592. if (SSTAT(G.zipfn, &G.statbuf) ||
  593. #ifdef THEOS
  594. (error = S_ISLIB(G.statbuf.st_mode)) != 0 ||
  595. #endif
  596. (error = S_ISDIR(G.statbuf.st_mode)) != 0)
  597. {
  598. #ifndef SFX
  599. if (lastchance && (uO.qflag < 3)) {
  600. #if defined(UNIX) || defined(QDOS)
  601. if (G.no_ecrec)
  602. Info(slide, 1, ((char *)slide,
  603. LoadFarString(CannotFindZipfileDirMsg),
  604. LoadFarStringSmall((uO.zipinfo_mode ? Zipnfo : Unzip)),
  605. G.wildzipfn, uO.zipinfo_mode? " " : "", G.wildzipfn,
  606. G.zipfn));
  607. else
  608. Info(slide, 1, ((char *)slide,
  609. LoadFarString(CannotFindEitherZipfile),
  610. LoadFarStringSmall((uO.zipinfo_mode ? Zipnfo : Unzip)),
  611. G.wildzipfn, G.wildzipfn, G.zipfn));
  612. #else /* !(UNIX || QDOS) */
  613. if (G.no_ecrec)
  614. Info(slide, 0x401, ((char *)slide,
  615. LoadFarString(CannotFindZipfileDirMsg),
  616. LoadFarStringSmall((uO.zipinfo_mode ? Zipnfo : Unzip)),
  617. G.wildzipfn, uO.zipinfo_mode? " " : "", G.zipfn));
  618. else
  619. #ifdef VMS
  620. Info(slide, 0x401, ((char *)slide,
  621. LoadFarString(CannotFindEitherZipfile),
  622. LoadFarStringSmall((uO.zipinfo_mode ? Zipnfo : Unzip)),
  623. G.wildzipfn,
  624. (*G.zipfn ? G.zipfn : vms_msg_text())));
  625. #else /* !VMS */
  626. Info(slide, 0x401, ((char *)slide,
  627. LoadFarString(CannotFindEitherZipfile),
  628. LoadFarStringSmall((uO.zipinfo_mode ? Zipnfo : Unzip)),
  629. G.wildzipfn, G.zipfn));
  630. #endif /* ?VMS */
  631. #endif /* ?(UNIX || QDOS) */
  632. }
  633. #endif /* !SFX */
  634. return error? IZ_DIR : PK_NOZIP;
  635. }
  636. G.ziplen = G.statbuf.st_size;
  637. #ifndef SFX
  638. #if defined(UNIX) || defined(DOS_OS2_W32) || defined(THEOS)
  639. if (G.statbuf.st_mode & S_IEXEC) /* no extension on Unix exes: might */
  640. maybe_exe = TRUE; /* find unzip, not unzip.zip; etc. */
  641. #endif
  642. #endif /* !SFX */
  643. #ifdef VMS
  644. if (check_format(__G)) /* check for variable-length format */
  645. return PK_ERR;
  646. #endif
  647. if (open_input_file(__G)) /* this should never happen, given */
  648. return PK_NOZIP; /* the stat() test above, but... */
  649. #ifdef DO_SAFECHECK_2GB
  650. /* Need more care: Do not trust the size returned by stat() but
  651. determine it by reading beyond the end of the file. */
  652. G.ziplen = file_size(G.zipfd);
  653. if (G.ziplen == EOF) {
  654. Info(slide, 0x401, ((char *)slide, LoadFarString(ZipfileTooBig)));
  655. /*
  656. printf(
  657. " We need a better error message for: 64-bit file, 32-bit program.\n");
  658. */
  659. CLOSE_INFILE();
  660. return IZ_ERRBF;
  661. }
  662. #endif /* DO_SAFECHECK_2GB */
  663. /*---------------------------------------------------------------------------
  664. Find and process the end-of-central-directory header. UnZip need only
  665. check last 65557 bytes of zipfile: comment may be up to 65535, end-of-
  666. central-directory record is 18 bytes, and signature itself is 4 bytes;
  667. add some to allow for appended garbage. Since ZipInfo is often used as
  668. a debugging tool, search the whole zipfile if zipinfo_mode is true.
  669. ---------------------------------------------------------------------------*/
  670. G.cur_zipfile_bufstart = 0;
  671. G.inptr = G.inbuf;
  672. #if ((!defined(WINDLL) && !defined(SFX)) || !defined(NO_ZIPINFO))
  673. # if (!defined(WINDLL) && !defined(SFX))
  674. if ( (!uO.zipinfo_mode && !uO.qflag
  675. # ifdef TIMESTAMP
  676. && !uO.T_flag
  677. # endif
  678. )
  679. # ifndef NO_ZIPINFO
  680. || (uO.zipinfo_mode && uO.hflag)
  681. # endif
  682. )
  683. # else /* not (!WINDLL && !SFX) ==> !NO_ZIPINFO !! */
  684. if (uO.zipinfo_mode && uO.hflag)
  685. # endif /* if..else..: (!WINDLL && !SFX) */
  686. # ifdef WIN32 /* Win32 console may require codepage conversion for G.zipfn */
  687. Info(slide, 0, ((char *)slide, LoadFarString(LogInitline),
  688. FnFilter1(G.zipfn)));
  689. # else
  690. Info(slide, 0, ((char *)slide, LoadFarString(LogInitline), G.zipfn));
  691. # endif
  692. #endif /* (!WINDLL && !SFX) || !NO_ZIPINFO */
  693. if ( (error_in_archive = find_ecrec(__G__
  694. #ifndef NO_ZIPINFO
  695. uO.zipinfo_mode ? G.ziplen :
  696. #endif
  697. MIN(G.ziplen, 66000L)))
  698. > PK_WARN )
  699. {
  700. CLOSE_INFILE();
  701. #ifdef SFX
  702. ++lastchance; /* avoid picky compiler warnings */
  703. return error_in_archive;
  704. #else
  705. if (maybe_exe)
  706. Info(slide, 0x401, ((char *)slide, LoadFarString(MaybeExe),
  707. G.zipfn));
  708. if (lastchance)
  709. return error_in_archive;
  710. else {
  711. G.no_ecrec = TRUE; /* assume we found wrong file: e.g., */
  712. return PK_NOZIP; /* unzip instead of unzip.zip */
  713. }
  714. #endif /* ?SFX */
  715. }
  716. if ((uO.zflag > 0) && !uO.zipinfo_mode) { /* unzip: zflag = comment ONLY */
  717. CLOSE_INFILE();
  718. return error_in_archive;
  719. }
  720. /*---------------------------------------------------------------------------
  721. Test the end-of-central-directory info for incompatibilities (multi-disk
  722. archives) or inconsistencies (missing or extra bytes in zipfile).
  723. ---------------------------------------------------------------------------*/
  724. #ifdef NO_MULTIPART
  725. error = !uO.zipinfo_mode && (G.ecrec.number_this_disk == 1) &&
  726. (G.ecrec.num_disk_start_cdir == 1);
  727. #else
  728. error = !uO.zipinfo_mode && (G.ecrec.number_this_disk != 0);
  729. #endif
  730. #ifndef SFX
  731. if (uO.zipinfo_mode &&
  732. G.ecrec.number_this_disk != G.ecrec.num_disk_start_cdir)
  733. {
  734. if (G.ecrec.number_this_disk > G.ecrec.num_disk_start_cdir) {
  735. Info(slide, 0x401, ((char *)slide,
  736. LoadFarString(CentDirNotInZipMsg), G.zipfn,
  737. (ulg)G.ecrec.number_this_disk,
  738. (ulg)G.ecrec.num_disk_start_cdir));
  739. error_in_archive = PK_FIND;
  740. too_weird_to_continue = TRUE;
  741. } else {
  742. Info(slide, 0x401, ((char *)slide,
  743. LoadFarString(EndCentDirBogus), G.zipfn,
  744. (ulg)G.ecrec.number_this_disk,
  745. (ulg)G.ecrec.num_disk_start_cdir));
  746. error_in_archive = PK_WARN;
  747. }
  748. #ifdef NO_MULTIPART /* concatenation of multiple parts works in some cases */
  749. } else if (!uO.zipinfo_mode && !error && G.ecrec.number_this_disk != 0) {
  750. Info(slide, 0x401, ((char *)slide, LoadFarString(NoMultiDiskArcSupport),
  751. G.zipfn));
  752. error_in_archive = PK_FIND;
  753. too_weird_to_continue = TRUE;
  754. #endif
  755. }
  756. if (!too_weird_to_continue) { /* (relatively) normal zipfile: go for it */
  757. if (error) {
  758. Info(slide, 0x401, ((char *)slide, LoadFarString(MaybePakBug),
  759. G.zipfn));
  760. error_in_archive = PK_WARN;
  761. }
  762. #endif /* !SFX */
  763. if ((G.extra_bytes = G.real_ecrec_offset-G.expect_ecrec_offset) <
  764. (zoff_t)0)
  765. {
  766. Info(slide, 0x401, ((char *)slide, LoadFarString(MissingBytes),
  767. G.zipfn, FmZofft((-G.extra_bytes), NULL, NULL)));
  768. error_in_archive = PK_ERR;
  769. } else if (G.extra_bytes > 0) {
  770. if ((G.ecrec.offset_start_central_directory == 0) &&
  771. (G.ecrec.size_central_directory != 0)) /* zip 1.5 -go bug */
  772. {
  773. Info(slide, 0x401, ((char *)slide,
  774. LoadFarString(NullCentDirOffset), G.zipfn));
  775. G.ecrec.offset_start_central_directory = G.extra_bytes;
  776. G.extra_bytes = 0;
  777. error_in_archive = PK_ERR;
  778. }
  779. #ifndef SFX
  780. else {
  781. Info(slide, 0x401, ((char *)slide,
  782. LoadFarString(ExtraBytesAtStart), G.zipfn,
  783. FmZofft(G.extra_bytes, NULL, NULL),
  784. (G.extra_bytes == 1)? "":"s"));
  785. error_in_archive = PK_WARN;
  786. }
  787. #endif /* !SFX */
  788. }
  789. /*-----------------------------------------------------------------------
  790. Check for empty zipfile and exit now if so.
  791. -----------------------------------------------------------------------*/
  792. if (G.expect_ecrec_offset==0L && G.ecrec.size_central_directory==0) {
  793. if (uO.zipinfo_mode)
  794. Info(slide, 0, ((char *)slide, "%sEmpty zipfile.\n",
  795. uO.lflag>9? "\n " : ""));
  796. else
  797. Info(slide, 0x401, ((char *)slide, LoadFarString(ZipfileEmpty),
  798. G.zipfn));
  799. CLOSE_INFILE();
  800. return (error_in_archive > PK_WARN)? error_in_archive : PK_WARN;
  801. }
  802. /*-----------------------------------------------------------------------
  803. Compensate for missing or extra bytes, and seek to where the start
  804. of central directory should be. If header not found, uncompensate
  805. and try again (necessary for at least some Atari archives created
  806. with STZip, as well as archives created by J.H. Holm's ZIPSPLIT 1.1).
  807. -----------------------------------------------------------------------*/
  808. error = seek_zipf(__G__ G.ecrec.offset_start_central_directory);
  809. if (error == PK_BADERR) {
  810. CLOSE_INFILE();
  811. return PK_BADERR;
  812. }
  813. #ifdef OLD_SEEK_TEST
  814. if (error != PK_OK || readbuf(__G__ G.sig, 4) == 0) {
  815. CLOSE_INFILE();
  816. return PK_ERR; /* file may be locked, or possibly disk error(?) */
  817. }
  818. if (memcmp(G.sig, central_hdr_sig, 4))
  819. #else
  820. if ((error != PK_OK) || (readbuf(__G__ G.sig, 4) == 0) ||
  821. memcmp(G.sig, central_hdr_sig, 4))
  822. #endif
  823. {
  824. #ifndef SFX
  825. zoff_t tmp = G.extra_bytes;
  826. #endif
  827. G.extra_bytes = 0;
  828. error = seek_zipf(__G__ G.ecrec.offset_start_central_directory);
  829. if ((error != PK_OK) || (readbuf(__G__ G.sig, 4) == 0) ||
  830. memcmp(G.sig, central_hdr_sig, 4))
  831. {
  832. if (error != PK_BADERR)
  833. Info(slide, 0x401, ((char *)slide,
  834. LoadFarString(CentDirStartNotFound), G.zipfn,
  835. LoadFarStringSmall(ReportMsg)));
  836. CLOSE_INFILE();
  837. return (error != PK_OK ? error : PK_BADERR);
  838. }
  839. #ifndef SFX
  840. Info(slide, 0x401, ((char *)slide, LoadFarString(CentDirTooLong),
  841. G.zipfn, FmZofft((-tmp), NULL, NULL)));
  842. #endif
  843. error_in_archive = PK_ERR;
  844. }
  845. /*-----------------------------------------------------------------------
  846. Seek to the start of the central directory one last time, since we
  847. have just read the first entry's signature bytes; then list, extract
  848. or test member files as instructed, and close the zipfile.
  849. -----------------------------------------------------------------------*/
  850. error = seek_zipf(__G__ G.ecrec.offset_start_central_directory);
  851. if (error != PK_OK) {
  852. CLOSE_INFILE();
  853. return error;
  854. }
  855. Trace((stderr, "about to extract/list files (error = %d)\n",
  856. error_in_archive));
  857. #ifdef DLL
  858. /* G.fValidate is used only to look at an archive to see if
  859. it appears to be a valid archive. There is no interest
  860. in what the archive contains, nor in validating that the
  861. entries in the archive are in good condition. This is
  862. currently used only in the Windows DLLs for purposes of
  863. checking archives within an archive to determine whether
  864. or not to display the inner archives.
  865. */
  866. if (!G.fValidate)
  867. #endif
  868. {
  869. #ifndef NO_ZIPINFO
  870. if (uO.zipinfo_mode)
  871. error = zipinfo(__G); /* ZIPINFO 'EM */
  872. else
  873. #endif
  874. #ifndef SFX
  875. #ifdef TIMESTAMP
  876. if (uO.T_flag)
  877. error = get_time_stamp(__G__ &uxstamp, &nmember);
  878. else
  879. #endif
  880. if (uO.vflag && !uO.tflag && !uO.cflag)
  881. error = list_files(__G); /* LIST 'EM */
  882. else
  883. #endif /* !SFX */
  884. error = extract_or_test_files(__G); /* EXTRACT OR TEST 'EM */
  885. Trace((stderr, "done with extract/list files (error = %d)\n",
  886. error));
  887. }
  888. if (error > error_in_archive) /* don't overwrite stronger error */
  889. error_in_archive = error; /* with (for example) a warning */
  890. #ifndef SFX
  891. } /* end if (!too_weird_to_continue) */
  892. #endif
  893. CLOSE_INFILE();
  894. #ifdef TIMESTAMP
  895. if (uO.T_flag && !uO.zipinfo_mode && (nmember > 0L)) {
  896. # ifdef WIN32
  897. if (stamp_file(__G__ G.zipfn, uxstamp)) { /* TIME-STAMP 'EM */
  898. # else
  899. if (stamp_file(G.zipfn, uxstamp)) { /* TIME-STAMP 'EM */
  900. # endif
  901. if (uO.qflag < 3)
  902. Info(slide, 0x201, ((char *)slide,
  903. LoadFarString(ZipTimeStampFailed), G.zipfn));
  904. if (error_in_archive < PK_WARN)
  905. error_in_archive = PK_WARN;
  906. } else {
  907. if (!uO.qflag)
  908. Info(slide, 0, ((char *)slide,
  909. LoadFarString(ZipTimeStampSuccess), G.zipfn));
  910. }
  911. }
  912. #endif
  913. return error_in_archive;
  914. } /* end function do_seekable() */
  915. #ifdef DO_SAFECHECK_2GB
  916. /************************/
  917. /* Function file_size() */
  918. /************************/
  919. /* File size determination which does not mislead for large files in a
  920. small-file program. Probably should be somewhere else.
  921. The file has to be opened previously
  922. */
  923. #ifdef USE_STRM_INPUT
  924. static zoff_t file_size(file)
  925. FILE *file;
  926. {
  927. int sts;
  928. size_t siz;
  929. #else /* !USE_STRM_INPUT */
  930. static zoff_t file_size(fh)
  931. int fh;
  932. {
  933. int siz;
  934. #endif /* ?USE_STRM_INPUT */
  935. zoff_t ofs;
  936. char waste[4];
  937. #ifdef USE_STRM_INPUT
  938. /* Seek to actual EOF. */
  939. sts = zfseeko(file, 0, SEEK_END);
  940. if (sts != 0) {
  941. /* fseeko() failed. (Unlikely.) */
  942. ofs = EOF;
  943. } else {
  944. /* Get apparent offset at EOF. */
  945. ofs = zftello(file);
  946. if (ofs < 0) {
  947. /* Offset negative (overflow). File too big. */
  948. ofs = EOF;
  949. } else {
  950. /* Seek to apparent EOF offset.
  951. Won't be at actual EOF if offset was truncated.
  952. */
  953. sts = zfseeko(file, ofs, SEEK_SET);
  954. if (sts != 0) {
  955. /* fseeko() failed. (Unlikely.) */
  956. ofs = EOF;
  957. } else {
  958. /* Read a byte at apparent EOF. Should set EOF flag. */
  959. siz = fread(waste, 1, 1, file);
  960. if (feof(file) == 0) {
  961. /* Not at EOF, but should be. File too big. */
  962. ofs = EOF;
  963. }
  964. }
  965. }
  966. }
  967. #else /* !USE_STRM_INPUT */
  968. /* Seek to actual EOF. */
  969. ofs = zlseek(fh, 0, SEEK_END);
  970. if (ofs == (zoff_t) -1) {
  971. /* zlseek() failed. (Unlikely.) */
  972. ofs = EOF;
  973. } else if (ofs < 0) {
  974. /* Offset negative (overflow). File too big. */
  975. ofs = EOF;
  976. } else {
  977. /* Seek to apparent EOF offset.
  978. Won't be at actual EOF if offset was truncated.
  979. */
  980. ofs = zlseek(fh, ofs, SEEK_SET);
  981. if (ofs == (zoff_t) -1) {
  982. /* zlseek() failed. (Unlikely.) */
  983. ofs = EOF;
  984. } else {
  985. /* Read a byte at apparent EOF. Should set EOF flag. */
  986. siz = read(fh, waste, 1);
  987. if (siz != 0) {
  988. /* Not at EOF, but should be. File too big. */
  989. ofs = EOF;
  990. }
  991. }
  992. }
  993. #endif /* ?USE_STRM_INPUT */
  994. return ofs;
  995. } /* end function file_size() */
  996. #endif /* DO_SAFECHECK_2GB */
  997. /***********************/
  998. /* Function rec_find() */
  999. /***********************/
  1000. static int rec_find(__G__ searchlen, signature, rec_size)
  1001. /* return 0 when rec found, 1 when not found, 2 in case of read error */
  1002. __GDEF
  1003. zoff_t searchlen;
  1004. char* signature;
  1005. int rec_size;
  1006. {
  1007. int i, numblks, found=FALSE;
  1008. zoff_t tail_len;
  1009. /*---------------------------------------------------------------------------
  1010. Zipfile is longer than INBUFSIZ: may need to loop. Start with short
  1011. block at end of zipfile (if not TOO short).
  1012. ---------------------------------------------------------------------------*/
  1013. if ((tail_len = G.ziplen % INBUFSIZ) > rec_size) {
  1014. #ifdef USE_STRM_INPUT
  1015. zfseeko(G.zipfd, G.ziplen-tail_len, SEEK_SET);
  1016. G.cur_zipfile_bufstart = zftello(G.zipfd);
  1017. #else /* !USE_STRM_INPUT */
  1018. G.cur_zipfile_bufstart = zlseek(G.zipfd, G.ziplen-tail_len, SEEK_SET);
  1019. #endif /* ?USE_STRM_INPUT */
  1020. if ((G.incnt = read(G.zipfd, (char *)G.inbuf,
  1021. (unsigned int)tail_len)) != (int)tail_len)
  1022. return 2; /* it's expedient... */
  1023. /* 'P' must be at least (rec_size+4) bytes from end of zipfile */
  1024. for (G.inptr = G.inbuf+(int)tail_len-(rec_size+4);
  1025. G.inptr >= G.inbuf;
  1026. --G.inptr) {
  1027. if ( (*G.inptr == (uch)0x50) && /* ASCII 'P' */
  1028. !memcmp((char *)G.inptr, signature, 4) ) {
  1029. G.incnt -= (int)(G.inptr - G.inbuf);
  1030. found = TRUE;
  1031. break;
  1032. }
  1033. }
  1034. /* sig may span block boundary: */
  1035. memcpy((char *)G.hold, (char *)G.inbuf, 3);
  1036. } else
  1037. G.cur_zipfile_bufstart = G.ziplen - tail_len;
  1038. /*-----------------------------------------------------------------------
  1039. Loop through blocks of zipfile data, starting at the end and going
  1040. toward the beginning. In general, need not check whole zipfile for
  1041. signature, but may want to do so if testing.
  1042. -----------------------------------------------------------------------*/
  1043. numblks = (int)((searchlen - tail_len + (INBUFSIZ-1)) / INBUFSIZ);
  1044. /* ==amount= ==done== ==rounding== =blksiz= */
  1045. for (i = 1; !found && (i <= numblks); ++i) {
  1046. G.cur_zipfile_bufstart -= INBUFSIZ;
  1047. #ifdef USE_STRM_INPUT
  1048. zfseeko(G.zipfd, G.cur_zipfile_bufstart, SEEK_SET);
  1049. #else /* !USE_STRM_INPUT */
  1050. zlseek(G.zipfd, G.cur_zipfile_bufstart, SEEK_SET);
  1051. #endif /* ?USE_STRM_INPUT */
  1052. if ((G.incnt = read(G.zipfd,(char *)G.inbuf,INBUFSIZ))
  1053. != INBUFSIZ)
  1054. return 2; /* read error is fatal failure */
  1055. for (G.inptr = G.inbuf+INBUFSIZ-1; G.inptr >= G.inbuf; --G.inptr)
  1056. if ( (*G.inptr == (uch)0x50) && /* ASCII 'P' */
  1057. !memcmp((char *)G.inptr, signature, 4) ) {
  1058. G.incnt -= (int)(G.inptr - G.inbuf);
  1059. found = TRUE;
  1060. break;
  1061. }
  1062. /* sig may span block boundary: */
  1063. memcpy((char *)G.hold, (char *)G.inbuf, 3);
  1064. }
  1065. return (found ? 0 : 1);
  1066. } /* end function rec_find() */
  1067. #if 0
  1068. /********************************/
  1069. /* Function check_ecrec_zip64() */
  1070. /********************************/
  1071. static int check_ecrec_zip64(__G)
  1072. __GDEF
  1073. {
  1074. return G.ecrec.offset_start_central_directory == 0xFFFFFFFFL
  1075. || G.ecrec.size_central_directory == 0xFFFFFFFFL
  1076. || G.ecrec.total_entries_central_dir == 0xFFFF
  1077. || G.ecrec.num_entries_centrl_dir_ths_disk == 0xFFFF
  1078. || G.ecrec.num_disk_start_cdir == 0xFFFF
  1079. || G.ecrec.number_this_disk == 0xFFFF;
  1080. } /* end function check_ecrec_zip64() */
  1081. #endif /* never */
  1082. /***************************/
  1083. /* Function find_ecrec64() */
  1084. /***************************/
  1085. static int find_ecrec64(__G__ searchlen) /* return PK-class error */
  1086. __GDEF
  1087. zoff_t searchlen;
  1088. {
  1089. ec_byte_rec64 byterec; /* buf for ecrec64 */
  1090. ec_byte_loc64 byterecL; /* buf for ecrec64 locator */
  1091. zoff_t ecloc64_start_offset; /* start offset of ecrec64 locator */
  1092. zusz_t ecrec64_start_offset; /* start offset of ecrec64 */
  1093. zuvl_t ecrec64_start_disk; /* start disk of ecrec64 */
  1094. zuvl_t ecloc64_total_disks; /* total disks */
  1095. zuvl_t ecrec64_disk_cdstart; /* disk number of central dir start */
  1096. zucn_t ecrec64_this_entries; /* entries on disk with ecrec64 */
  1097. zucn_t ecrec64_tot_entries; /* total number of entries */
  1098. zusz_t ecrec64_cdirsize; /* length of central dir */
  1099. zusz_t ecrec64_offs_cdstart; /* offset of central dir start */
  1100. /* First, find the ecrec64 locator. By definition, this must be before
  1101. ecrec with nothing in between. We back up the size of the ecrec64
  1102. locator and check. */
  1103. ecloc64_start_offset = G.real_ecrec_offset - (ECLOC64_SIZE+4);
  1104. if (ecloc64_start_offset < 0)
  1105. /* Seeking would go past beginning, so probably empty archive */
  1106. return PK_COOL;
  1107. #ifdef USE_STRM_INPUT
  1108. zfseeko(G.zipfd, ecloc64_start_offset, SEEK_SET);
  1109. G.cur_zipfile_bufstart = zftello(G.zipfd);
  1110. #else /* !USE_STRM_INPUT */
  1111. G.cur_zipfile_bufstart = zlseek(G.zipfd, ecloc64_start_offset, SEEK_SET);
  1112. #endif /* ?USE_STRM_INPUT */
  1113. if ((G.incnt = read(G.zipfd, (char *)byterecL, ECLOC64_SIZE+4))
  1114. != (ECLOC64_SIZE+4)) {
  1115. if (uO.qflag || uO.zipinfo_mode)
  1116. Info(slide, 0x401, ((char *)slide, "[%s]\n", G.zipfn));
  1117. Info(slide, 0x401, ((char *)slide,
  1118. LoadFarString(Cent64EndSigSearchErr)));
  1119. return PK_ERR;
  1120. }
  1121. if (memcmp((char *)byterecL, end_centloc64_sig, 4) ) {
  1122. /* not found */
  1123. return PK_COOL;
  1124. }
  1125. /* Read the locator. */
  1126. ecrec64_start_disk = (zuvl_t)makelong(&byterecL[NUM_DISK_START_EOCDR64]);
  1127. ecrec64_start_offset = (zusz_t)makeint64(&byterecL[OFFSET_START_EOCDR64]);
  1128. ecloc64_total_disks = (zuvl_t)makelong(&byterecL[NUM_THIS_DISK_LOC64]);
  1129. /* Check for consistency */
  1130. #ifdef TEST
  1131. fprintf(stdout,"\nnumber of disks (ECR) %u, (ECLOC64) %lu\n",
  1132. G.ecrec.number_this_disk, ecloc64_total_disks); fflush(stdout);
  1133. #endif
  1134. if ((G.ecrec.number_this_disk != 0xFFFF) &&
  1135. (G.ecrec.number_this_disk != ecloc64_total_disks - 1)) {
  1136. /* Note: For some unknown reason, the developers at PKWARE decided to
  1137. store the "zip64 total disks" value as a counter starting from 1,
  1138. whereas all other "split/span volume" related fields use 0-based
  1139. volume numbers. Sigh... */
  1140. /* When the total number of disks as found in the traditional ecrec
  1141. is not 0xFFFF, the disk numbers in ecrec and ecloc64 must match.
  1142. When this is not the case, the found ecrec64 locator cannot be valid.
  1143. -> This is not a Zip64 archive.
  1144. */
  1145. Trace((stderr,
  1146. "\ninvalid ECLOC64, differing disk# (ECR %u, ECL64 %lu)\n",
  1147. G.ecrec.number_this_disk, ecloc64_total_disks - 1));
  1148. return PK_COOL;
  1149. }
  1150. /* If found locator, look for ecrec64 where the locator says it is. */
  1151. /* For now assume that ecrec64 is on the same disk as ecloc64 and ecrec,
  1152. which is usually the case and is how Zip writes it. To do this right,
  1153. however, we should allow the ecrec64 to be on another disk since
  1154. the AppNote allows it and the ecrec64 can be large, especially if
  1155. Version 2 is used (AppNote uses 8 bytes for the size of this record). */
  1156. /* FIX BELOW IF ADD SUPPORT FOR MULTIPLE DISKS */
  1157. if (ecrec64_start_offset > (zusz_t)ecloc64_start_offset) {
  1158. /* ecrec64 has to be before ecrec64 locator */
  1159. if (uO.qflag || uO.zipinfo_mode)
  1160. Info(slide, 0x401, ((char *)slide, "[%s]\n", G.zipfn));
  1161. Info(slide, 0x401, ((char *)slide,
  1162. LoadFarString(Cent64EndSigSearchErr)));
  1163. return PK_ERR;
  1164. }
  1165. #ifdef USE_STRM_INPUT
  1166. zfseeko(G.zipfd, ecrec64_start_offset, SEEK_SET);
  1167. G.cur_zipfile_bufstart = zftello(G.zipfd);
  1168. #else /* !USE_STRM_INPUT */
  1169. G.cur_zipfile_bufstart = zlseek(G.zipfd, ecrec64_start_offset, SEEK_SET);
  1170. #endif /* ?USE_STRM_INPUT */
  1171. if ((G.incnt = read(G.zipfd, (char *)byterec, ECREC64_SIZE+4))
  1172. != (ECREC64_SIZE+4)) {
  1173. if (uO.qflag || uO.zipinfo_mode)
  1174. Info(slide, 0x401, ((char *)slide, "[%s]\n", G.zipfn));
  1175. Info(slide, 0x401, ((char *)slide,
  1176. LoadFarString(Cent64EndSigSearchErr)));
  1177. return PK_ERR;
  1178. }
  1179. if (memcmp((char *)byterec, end_central64_sig, 4) ) {
  1180. /* Zip64 EOCD Record not found */
  1181. /* Since we already have seen the Zip64 EOCD Locator, it's
  1182. possible we got here because there are bytes prepended
  1183. to the archive, like the sfx prefix. */
  1184. /* Make a guess as to where the Zip64 EOCD Record might be */
  1185. ecrec64_start_offset = ecloc64_start_offset - ECREC64_SIZE - 4;
  1186. #ifdef USE_STRM_INPUT
  1187. zfseeko(G.zipfd, ecrec64_start_offset, SEEK_SET);
  1188. G.cur_zipfile_bufstart = zftello(G.zipfd);
  1189. #else /* !USE_STRM_INPUT */
  1190. G.cur_zipfile_bufstart = zlseek(G.zipfd, ecrec64_start_offset, SEEK_SET);
  1191. #endif /* ?USE_STRM_INPUT */
  1192. if ((G.incnt = read(G.zipfd, (char *)byterec, ECREC64_SIZE+4))
  1193. != (ECREC64_SIZE+4)) {
  1194. if (uO.qflag || uO.zipinfo_mode)
  1195. Info(slide, 0x401, ((char *)slide, "[%s]\n", G.zipfn));
  1196. Info(slide, 0x401, ((char *)slide,
  1197. LoadFarString(Cent64EndSigSearchErr)));
  1198. return PK_ERR;
  1199. }
  1200. if (memcmp((char *)byterec, end_central64_sig, 4) ) {
  1201. /* Zip64 EOCD Record not found */
  1202. /* Probably something not so easy to handle so exit */
  1203. if (uO.qflag || uO.zipinfo_mode)
  1204. Info(slide, 0x401, ((char *)slide, "[%s]\n", G.zipfn));
  1205. Info(slide, 0x401, ((char *)slide,
  1206. LoadFarString(Cent64EndSigSearchErr)));
  1207. return PK_ERR;
  1208. }
  1209. if (uO.qflag || uO.zipinfo_mode)
  1210. Info(slide, 0x401, ((char *)slide, "[%s]\n", G.zipfn));
  1211. Info(slide, 0x401, ((char *)slide,
  1212. LoadFarString(Cent64EndSigSearchOff)));
  1213. }
  1214. /* Check consistency of found ecrec64 with ecloc64 (and ecrec): */
  1215. if ( (zuvl_t)makelong(&byterec[NUMBER_THIS_DSK_REC64])
  1216. != ecrec64_start_disk )
  1217. /* found ecrec64 does not match ecloc64 info -> no Zip64 archive */
  1218. return PK_COOL;
  1219. /* Read all relevant ecrec64 fields and compare them to the corresponding
  1220. ecrec fields unless those are set to "all-ones".
  1221. */
  1222. ecrec64_disk_cdstart =
  1223. (zuvl_t)makelong(&byterec[NUM_DISK_START_CEN_DIR64]);
  1224. if ( (G.ecrec.num_disk_start_cdir != 0xFFFF) &&
  1225. (G.ecrec.num_disk_start_cdir != ecrec64_disk_cdstart) )
  1226. return PK_COOL;
  1227. ecrec64_this_entries
  1228. = makeint64(&byterec[NUM_ENTRIES_CEN_DIR_THS_DISK64]);
  1229. if ( (G.ecrec.num_entries_centrl_dir_ths_disk != 0xFFFF) &&
  1230. (G.ecrec.num_entries_centrl_dir_ths_disk != ecrec64_this_entries) )
  1231. return PK_COOL;
  1232. ecrec64_tot_entries
  1233. = makeint64(&byterec[TOTAL_ENTRIES_CENTRAL_DIR64]);
  1234. if ( (G.ecrec.total_entries_central_dir != 0xFFFF) &&
  1235. (G.ecrec.total_entries_central_dir != ecrec64_tot_entries) )
  1236. return PK_COOL;
  1237. ecrec64_cdirsize
  1238. = makeint64(&byterec[SIZE_CENTRAL_DIRECTORY64]);
  1239. if ( (G.ecrec.size_central_directory != 0xFFFFFFFFL) &&
  1240. (G.ecrec.size_central_directory != ecrec64_cdirsize) )
  1241. return PK_COOL;
  1242. ecrec64_offs_cdstart
  1243. = makeint64(&byterec[OFFSET_START_CENTRAL_DIRECT64]);
  1244. if ( (G.ecrec.offset_start_central_directory != 0xFFFFFFFFL) &&
  1245. (G.ecrec.offset_start_central_directory != ecrec64_offs_cdstart) )
  1246. return PK_COOL;
  1247. /* Now, we are (almost) sure that we have a Zip64 archive. */
  1248. G.ecrec.have_ecr64 = 1;
  1249. /* Update the "end-of-central-dir offset" for later checks. */
  1250. G.real_ecrec_offset = ecrec64_start_offset;
  1251. /* Update all ecdir_rec data that are flagged to be invalid
  1252. in Zip64 mode. Set the ecrec64-mandatory flag when such a
  1253. case is found. */
  1254. if (G.ecrec.number_this_disk == 0xFFFF) {
  1255. G.ecrec.number_this_disk = ecrec64_start_disk;
  1256. if (ecrec64_start_disk != 0xFFFF) G.ecrec.is_zip64_archive = TRUE;
  1257. }
  1258. if (G.ecrec.num_disk_start_cdir == 0xFFFF) {
  1259. G.ecrec.num_disk_start_cdir = ecrec64_disk_cdstart;
  1260. if (ecrec64_disk_cdstart != 0xFFFF) G.ecrec.is_zip64_archive = TRUE;
  1261. }
  1262. if (G.ecrec.num_entries_centrl_dir_ths_disk == 0xFFFF) {
  1263. G.ecrec.num_entries_centrl_dir_ths_disk = ecrec64_this_entries;
  1264. if (ecrec64_this_entries != 0xFFFF) G.ecrec.is_zip64_archive = TRUE;
  1265. }
  1266. if (G.ecrec.total_entries_central_dir == 0xFFFF) {
  1267. G.ecrec.total_entries_central_dir = ecrec64_tot_entries;
  1268. if (ecrec64_tot_entries != 0xFFFF) G.ecrec.is_zip64_archive = TRUE;
  1269. }
  1270. if (G.ecrec.size_central_directory == 0xFFFFFFFFL) {
  1271. G.ecrec.size_central_directory = ecrec64_cdirsize;
  1272. if (ecrec64_cdirsize != 0xFFFFFFFF) G.ecrec.is_zip64_archive = TRUE;
  1273. }
  1274. if (G.ecrec.offset_start_central_directory == 0xFFFFFFFFL) {
  1275. G.ecrec.offset_start_central_directory = ecrec64_offs_cdstart;
  1276. if (ecrec64_offs_cdstart != 0xFFFFFFFF) G.ecrec.is_zip64_archive = TRUE;
  1277. }
  1278. return PK_COOL;
  1279. } /* end function find_ecrec64() */
  1280. /*************************/
  1281. /* Function find_ecrec() */
  1282. /*************************/
  1283. static int find_ecrec(__G__ searchlen) /* return PK-class error */
  1284. __GDEF
  1285. zoff_t searchlen;
  1286. {
  1287. int found = FALSE;
  1288. int error_in_archive;
  1289. int result;
  1290. ec_byte_rec byterec;
  1291. /*---------------------------------------------------------------------------
  1292. Treat case of short zipfile separately.
  1293. ---------------------------------------------------------------------------*/
  1294. if (G.ziplen <= INBUFSIZ) {
  1295. #ifdef USE_STRM_INPUT
  1296. zfseeko(G.zipfd, 0L, SEEK_SET);
  1297. #else /* !USE_STRM_INPUT */
  1298. zlseek(G.zipfd, 0L, SEEK_SET);
  1299. #endif /* ?USE_STRM_INPUT */
  1300. if ((G.incnt = read(G.zipfd,(char *)G.inbuf,(unsigned int)G.ziplen))
  1301. == (int)G.ziplen)
  1302. /* 'P' must be at least (ECREC_SIZE+4) bytes from end of zipfile */
  1303. for (G.inptr = G.inbuf+(int)G.ziplen-(ECREC_SIZE+4);
  1304. G.inptr >= G.inbuf;
  1305. --G.inptr) {
  1306. if ( (*G.inptr == (uch)0x50) && /* ASCII 'P' */
  1307. !memcmp((char *)G.inptr, end_central_sig, 4)) {
  1308. G.incnt -= (int)(G.inptr - G.inbuf);
  1309. found = TRUE;
  1310. break;
  1311. }
  1312. }
  1313. /*---------------------------------------------------------------------------
  1314. Zipfile is longer than INBUFSIZ:
  1315. MB - this next block of code moved to rec_find so that same code can be
  1316. used to look for zip64 ec record. No need to include code above since
  1317. a zip64 ec record will only be looked for if it is a BIG file.
  1318. ---------------------------------------------------------------------------*/
  1319. } else {
  1320. found =
  1321. (rec_find(__G__ searchlen, end_central_sig, ECREC_SIZE) == 0
  1322. ? TRUE : FALSE);
  1323. } /* end if (ziplen > INBUFSIZ) */
  1324. /*---------------------------------------------------------------------------
  1325. Searched through whole region where signature should be without finding
  1326. it. Print informational message and die a horrible death.
  1327. ---------------------------------------------------------------------------*/
  1328. if (!found) {
  1329. if (uO.qflag || uO.zipinfo_mode)
  1330. Info(slide, 0x401, ((char *)slide, "[%s]\n", G.zipfn));
  1331. Info(slide, 0x401, ((char *)slide,
  1332. LoadFarString(CentDirEndSigNotFound)));
  1333. return PK_ERR; /* failed */
  1334. }
  1335. /*---------------------------------------------------------------------------
  1336. Found the signature, so get the end-central data before returning. Do
  1337. any necessary machine-type conversions (byte ordering, structure padding
  1338. compensation) by reading data into character array and copying to struct.
  1339. ---------------------------------------------------------------------------*/
  1340. G.real_ecrec_offset = G.cur_zipfile_bufstart + (G.inptr-G.inbuf);
  1341. #ifdef TEST
  1342. printf("\n found end-of-central-dir signature at offset %s (%sh)\n",
  1343. FmZofft(G.real_ecrec_offset, NULL, NULL),
  1344. FmZofft(G.real_ecrec_offset, FZOFFT_HEX_DOT_WID, "X"));
  1345. printf(" from beginning of file; offset %d (%.4Xh) within block\n",
  1346. G.inptr-G.inbuf, G.inptr-G.inbuf);
  1347. #endif
  1348. if (readbuf(__G__ (char *)byterec, ECREC_SIZE+4) == 0)
  1349. return PK_EOF;
  1350. G.ecrec.number_this_disk =
  1351. makeword(&byterec[NUMBER_THIS_DISK]);
  1352. G.ecrec.num_disk_start_cdir =
  1353. makeword(&byterec[NUM_DISK_WITH_START_CEN_DIR]);
  1354. G.ecrec.num_entries_centrl_dir_ths_disk =
  1355. makeword(&byterec[NUM_ENTRIES_CEN_DIR_THS_DISK]);
  1356. G.ecrec.total_entries_central_dir =
  1357. makeword(&byterec[TOTAL_ENTRIES_CENTRAL_DIR]);
  1358. G.ecrec.size_central_directory =
  1359. makelong(&byterec[SIZE_CENTRAL_DIRECTORY]);
  1360. G.ecrec.offset_start_central_directory =
  1361. makelong(&byterec[OFFSET_START_CENTRAL_DIRECTORY]);
  1362. G.ecrec.zipfile_comment_length =
  1363. makeword(&byterec[ZIPFILE_COMMENT_LENGTH]);
  1364. /* Now, we have to read the archive comment, BEFORE the file pointer
  1365. is moved away backwards to seek for a Zip64 ECLOC64 structure.
  1366. */
  1367. if ( (error_in_archive = process_zip_cmmnt(__G)) > PK_WARN )
  1368. return error_in_archive;
  1369. /* Next: Check for existence of Zip64 end-of-cent-dir locator
  1370. ECLOC64. This structure must reside on the same volume as the
  1371. classic ECREC, at exactly (ECLOC64_SIZE+4) bytes in front
  1372. of the ECREC.
  1373. The ECLOC64 structure directs to the longer ECREC64 structure
  1374. A ECREC64 will ALWAYS exist for a proper Zip64 archive, as
  1375. the "Version Needed To Extract" field is required to be set
  1376. to 4.5 or higher whenever any Zip64 features are used anywhere
  1377. in the archive, so just check for that to see if this is a
  1378. Zip64 archive.
  1379. */
  1380. result = find_ecrec64(__G__ searchlen+76);
  1381. /* 76 bytes for zip64ec & zip64 locator */
  1382. if (result != PK_COOL) {
  1383. if (error_in_archive < result)
  1384. error_in_archive = result;
  1385. return error_in_archive;
  1386. }
  1387. G.expect_ecrec_offset = G.ecrec.offset_start_central_directory +
  1388. G.ecrec.size_central_directory;
  1389. #ifndef NO_ZIPINFO
  1390. if (uO.zipinfo_mode) {
  1391. /* In ZipInfo mode, additional info about the data found in the
  1392. end-of-central-directory areas is printed out.
  1393. */
  1394. zi_end_central(__G);
  1395. }
  1396. #endif
  1397. return error_in_archive;
  1398. } /* end function find_ecrec() */
  1399. /********************************/
  1400. /* Function process_zip_cmmnt() */
  1401. /********************************/
  1402. static int process_zip_cmmnt(__G) /* return PK-type error code */
  1403. __GDEF
  1404. {
  1405. int error = PK_COOL;
  1406. /*---------------------------------------------------------------------------
  1407. Get the zipfile comment (up to 64KB long), if any, and print it out.
  1408. ---------------------------------------------------------------------------*/
  1409. #ifdef WINDLL
  1410. /* for comment button: */
  1411. if ((!G.fValidate) && (G.lpUserFunctions != NULL))
  1412. G.lpUserFunctions->cchComment = G.ecrec.zipfile_comment_length;
  1413. #endif /* WINDLL */
  1414. #ifndef NO_ZIPINFO
  1415. /* ZipInfo, verbose format */
  1416. if (uO.zipinfo_mode && uO.lflag > 9) {
  1417. /*-------------------------------------------------------------------
  1418. Get the zipfile comment, if any, and print it out.
  1419. (Comment may be up to 64KB long. May the fleas of a thousand
  1420. camels infest the arm-pits of anyone who actually takes advantage
  1421. of this fact.)
  1422. -------------------------------------------------------------------*/
  1423. if (!G.ecrec.zipfile_comment_length)
  1424. Info(slide, 0, ((char *)slide, LoadFarString(NoZipfileComment)));
  1425. else {
  1426. Info(slide, 0, ((char *)slide, LoadFarString(ZipfileCommentDesc),
  1427. G.ecrec.zipfile_comment_length));
  1428. Info(slide, 0, ((char *)slide, LoadFarString(ZipfileCommBegin)));
  1429. if (do_string(__G__ G.ecrec.zipfile_comment_length, DISPLAY))
  1430. error = PK_WARN;
  1431. Info(slide, 0, ((char *)slide, LoadFarString(ZipfileCommEnd)));
  1432. if (error)
  1433. Info(slide, 0, ((char *)slide,
  1434. LoadFarString(ZipfileCommTrunc2)));
  1435. } /* endif (comment exists) */
  1436. /* ZipInfo, non-verbose mode: print zipfile comment only if requested */
  1437. } else if (G.ecrec.zipfile_comment_length &&
  1438. (uO.zflag > 0) && uO.zipinfo_mode) {
  1439. if (do_string(__G__ G.ecrec.zipfile_comment_length, DISPLAY)) {
  1440. Info(slide, 0x401, ((char *)slide,
  1441. LoadFarString(ZipfileCommTrunc1)));
  1442. error = PK_WARN;
  1443. }
  1444. } else
  1445. #endif /* !NO_ZIPINFO */
  1446. if ( G.ecrec.zipfile_comment_length &&
  1447. (uO.zflag > 0
  1448. #ifndef WINDLL
  1449. || (uO.zflag == 0
  1450. # ifndef NO_ZIPINFO
  1451. && !uO.zipinfo_mode
  1452. # endif
  1453. # ifdef TIMESTAMP
  1454. && !uO.T_flag
  1455. # endif
  1456. && !uO.qflag)
  1457. #endif /* !WINDLL */
  1458. ) )
  1459. {
  1460. if (do_string(__G__ G.ecrec.zipfile_comment_length,
  1461. #if (defined(SFX) && defined(CHEAP_SFX_AUTORUN))
  1462. # ifndef NO_ZIPINFO
  1463. (oU.zipinfo_mode ? DISPLAY : CHECK_AUTORUN)
  1464. # else
  1465. CHECK_AUTORUN
  1466. # endif
  1467. #else
  1468. DISPLAY
  1469. #endif
  1470. ))
  1471. {
  1472. Info(slide, 0x401, ((char *)slide,
  1473. LoadFarString(ZipfileCommTrunc1)));
  1474. error = PK_WARN;
  1475. }
  1476. }
  1477. #if (defined(SFX) && defined(CHEAP_SFX_AUTORUN))
  1478. else if (G.ecrec.zipfile_comment_length) {
  1479. if (do_string(__G__ G.ecrec.zipfile_comment_length, CHECK_AUTORUN_Q))
  1480. {
  1481. Info(slide, 0x401, ((char *)slide,
  1482. LoadFarString(ZipfileCommTrunc1)));
  1483. error = PK_WARN;
  1484. }
  1485. }
  1486. #endif
  1487. return error;
  1488. } /* end function process_zip_cmmnt() */
  1489. /************************************/
  1490. /* Function process_cdir_file_hdr() */
  1491. /************************************/
  1492. int process_cdir_file_hdr(__G) /* return PK-type error code */
  1493. __GDEF
  1494. {
  1495. int error;
  1496. /*---------------------------------------------------------------------------
  1497. Get central directory info, save host and method numbers, and set flag
  1498. for lowercase conversion of filename, depending on the OS from which the
  1499. file is coming.
  1500. ---------------------------------------------------------------------------*/
  1501. if ((error = get_cdir_ent(__G)) != 0)
  1502. return error;
  1503. G.pInfo->hostver = G.crec.version_made_by[0];
  1504. G.pInfo->hostnum = MIN(G.crec.version_made_by[1], NUM_HOSTS);
  1505. /* extnum = MIN(crec.version_needed_to_extract[1], NUM_HOSTS); */
  1506. G.pInfo->lcflag = 0;
  1507. if (uO.L_flag == 1) /* name conversion for monocase systems */
  1508. switch (G.pInfo->hostnum) {
  1509. case FS_FAT_: /* PKZIP and zip -k store in uppercase */
  1510. case CPM_: /* like MS-DOS, right? */
  1511. case VM_CMS_: /* all caps? */
  1512. case MVS_: /* all caps? */
  1513. case TANDEM_:
  1514. case TOPS20_:
  1515. case VMS_: /* our Zip uses lowercase, but ASi's doesn't */
  1516. /* case Z_SYSTEM_: ? */
  1517. /* case QDOS_: ? */
  1518. G.pInfo->lcflag = 1; /* convert filename to lowercase */
  1519. break;
  1520. default: /* AMIGA_, FS_HPFS_, FS_NTFS_, MAC_, UNIX_, ATARI_, */
  1521. break; /* FS_VFAT_, ATHEOS_, BEOS_ (Z_SYSTEM_), THEOS_: */
  1522. /* no conversion */
  1523. }
  1524. else if (uO.L_flag > 1) /* let -LL force lower case for all names */
  1525. G.pInfo->lcflag = 1;
  1526. /* do Amigas (AMIGA_) also have volume labels? */
  1527. if (IS_VOLID(G.crec.external_file_attributes) &&
  1528. (G.pInfo->hostnum == FS_FAT_ || G.pInfo->hostnum == FS_HPFS_ ||
  1529. G.pInfo->hostnum == FS_NTFS_ || G.pInfo->hostnum == ATARI_))
  1530. {
  1531. G.pInfo->vollabel = TRUE;
  1532. G.pInfo->lcflag = 0; /* preserve case of volume labels */
  1533. } else
  1534. G.pInfo->vollabel = FALSE;
  1535. /* this flag is needed to detect archives made by "PKZIP for Unix" when
  1536. deciding which kind of codepage conversion has to be applied to
  1537. strings (see do_string() function in fileio.c) */
  1538. G.pInfo->HasUxAtt = (G.crec.external_file_attributes & 0xffff0000L) != 0L;
  1539. #ifdef UNICODE_SUPPORT
  1540. /* remember the state of GPB11 (General Purpuse Bit 11) which indicates
  1541. that the standard path and comment are UTF-8. */
  1542. G.pInfo->GPFIsUTF8
  1543. = (G.crec.general_purpose_bit_flag & (1 << 11)) == (1 << 11);
  1544. #endif
  1545. return PK_COOL;
  1546. } /* end function process_cdir_file_hdr() */
  1547. /***************************/
  1548. /* Function get_cdir_ent() */
  1549. /***************************/
  1550. static int get_cdir_ent(__G) /* return PK-type error code */
  1551. __GDEF
  1552. {
  1553. cdir_byte_hdr byterec;
  1554. /*---------------------------------------------------------------------------
  1555. Read the next central directory entry and do any necessary machine-type
  1556. conversions (byte ordering, structure padding compensation--do so by
  1557. copying the data from the array into which it was read (byterec) to the
  1558. usable struct (crec)).
  1559. ---------------------------------------------------------------------------*/
  1560. if (readbuf(__G__ (char *)byterec, CREC_SIZE) == 0)
  1561. return PK_EOF;
  1562. G.crec.version_made_by[0] = byterec[C_VERSION_MADE_BY_0];
  1563. G.crec.version_made_by[1] = byterec[C_VERSION_MADE_BY_1];
  1564. G.crec.version_needed_to_extract[0] =
  1565. byterec[C_VERSION_NEEDED_TO_EXTRACT_0];
  1566. G.crec.version_needed_to_extract[1] =
  1567. byterec[C_VERSION_NEEDED_TO_EXTRACT_1];
  1568. G.crec.general_purpose_bit_flag =
  1569. makeword(&byterec[C_GENERAL_PURPOSE_BIT_FLAG]);
  1570. G.crec.compression_method =
  1571. makeword(&byterec[C_COMPRESSION_METHOD]);
  1572. G.crec.last_mod_dos_datetime =
  1573. makelong(&byterec[C_LAST_MOD_DOS_DATETIME]);
  1574. G.crec.crc32 =
  1575. makelong(&byterec[C_CRC32]);
  1576. G.crec.csize =
  1577. makelong(&byterec[C_COMPRESSED_SIZE]);
  1578. G.crec.ucsize =
  1579. makelong(&byterec[C_UNCOMPRESSED_SIZE]);
  1580. G.crec.filename_length =
  1581. makeword(&byterec[C_FILENAME_LENGTH]);
  1582. G.crec.extra_field_length =
  1583. makeword(&byterec[C_EXTRA_FIELD_LENGTH]);
  1584. G.crec.file_comment_length =
  1585. makeword(&byterec[C_FILE_COMMENT_LENGTH]);
  1586. G.crec.disk_number_start =
  1587. makeword(&byterec[C_DISK_NUMBER_START]);
  1588. G.crec.internal_file_attributes =
  1589. makeword(&byterec[C_INTERNAL_FILE_ATTRIBUTES]);
  1590. G.crec.external_file_attributes =
  1591. makelong(&byterec[C_EXTERNAL_FILE_ATTRIBUTES]); /* LONG, not word! */
  1592. G.crec.relative_offset_local_header =
  1593. makelong(&byterec[C_RELATIVE_OFFSET_LOCAL_HEADER]);
  1594. return PK_COOL;
  1595. } /* end function get_cdir_ent() */
  1596. /*************************************/
  1597. /* Function process_local_file_hdr() */
  1598. /*************************************/
  1599. int process_local_file_hdr(__G) /* return PK-type error code */
  1600. __GDEF
  1601. {
  1602. local_byte_hdr byterec;
  1603. /*---------------------------------------------------------------------------
  1604. Read the next local file header and do any necessary machine-type con-
  1605. versions (byte ordering, structure padding compensation--do so by copy-
  1606. ing the data from the array into which it was read (byterec) to the
  1607. usable struct (lrec)).
  1608. ---------------------------------------------------------------------------*/
  1609. if (readbuf(__G__ (char *)byterec, LREC_SIZE) == 0)
  1610. return PK_EOF;
  1611. G.lrec.version_needed_to_extract[0] =
  1612. byterec[L_VERSION_NEEDED_TO_EXTRACT_0];
  1613. G.lrec.version_needed_to_extract[1] =
  1614. byterec[L_VERSION_NEEDED_TO_EXTRACT_1];
  1615. G.lrec.general_purpose_bit_flag =
  1616. makeword(&byterec[L_GENERAL_PURPOSE_BIT_FLAG]);
  1617. G.lrec.compression_method = makeword(&byterec[L_COMPRESSION_METHOD]);
  1618. G.lrec.last_mod_dos_datetime = makelong(&byterec[L_LAST_MOD_DOS_DATETIME]);
  1619. G.lrec.crc32 = makelong(&byterec[L_CRC32]);
  1620. G.lrec.csize = makelong(&byterec[L_COMPRESSED_SIZE]);
  1621. G.lrec.ucsize = makelong(&byterec[L_UNCOMPRESSED_SIZE]);
  1622. G.lrec.filename_length = makeword(&byterec[L_FILENAME_LENGTH]);
  1623. G.lrec.extra_field_length = makeword(&byterec[L_EXTRA_FIELD_LENGTH]);
  1624. if ((G.lrec.general_purpose_bit_flag & 8) != 0) {
  1625. /* can't trust local header, use central directory: */
  1626. G.lrec.crc32 = G.pInfo->crc;
  1627. G.lrec.csize = G.pInfo->compr_size;
  1628. G.lrec.ucsize = G.pInfo->uncompr_size;
  1629. }
  1630. G.csize = G.lrec.csize;
  1631. return PK_COOL;
  1632. } /* end function process_local_file_hdr() */
  1633. /*******************************/
  1634. /* Function getZip64Data() */
  1635. /*******************************/
  1636. int getZip64Data(__G__ ef_buf, ef_len)
  1637. __GDEF
  1638. ZCONST uch *ef_buf; /* buffer containing extra field */
  1639. unsigned ef_len; /* total length of extra field */
  1640. {
  1641. unsigned eb_id;
  1642. unsigned eb_len;
  1643. /*---------------------------------------------------------------------------
  1644. This function scans the extra field for zip64 information, ie 8-byte
  1645. versions of compressed file size, uncompressed file size, relative offset
  1646. and a 4-byte version of disk start number.
  1647. Sets both local header and central header fields. Not terribly clever,
  1648. but it means that this procedure is only called in one place.
  1649. ---------------------------------------------------------------------------*/
  1650. if (ef_len == 0 || ef_buf == NULL)
  1651. return PK_COOL;
  1652. Trace((stderr,"\ngetZip64Data: scanning extra field of length %u\n",
  1653. ef_len));
  1654. while (ef_len >= EB_HEADSIZE) {
  1655. eb_id = makeword(EB_ID + ef_buf);
  1656. eb_len = makeword(EB_LEN + ef_buf);
  1657. if (eb_len > (ef_len - EB_HEADSIZE)) {
  1658. /* discovered some extra field inconsistency! */
  1659. Trace((stderr,
  1660. "getZip64Data: block length %u > rest ef_size %u\n", eb_len,
  1661. ef_len - EB_HEADSIZE));
  1662. break;
  1663. }
  1664. if (eb_id == EF_PKSZ64) {
  1665. int offset = EB_HEADSIZE;
  1666. if (G.crec.ucsize == 0xffffffff || G.lrec.ucsize == 0xffffffff){
  1667. G.lrec.ucsize = G.crec.ucsize = makeint64(offset + ef_buf);
  1668. offset += sizeof(G.crec.ucsize);
  1669. }
  1670. if (G.crec.csize == 0xffffffff || G.lrec.csize == 0xffffffff){
  1671. G.csize = G.lrec.csize = G.crec.csize = makeint64(offset + ef_buf);
  1672. offset += sizeof(G.crec.csize);
  1673. }
  1674. if (G.crec.relative_offset_local_header == 0xffffffff){
  1675. G.crec.relative_offset_local_header = makeint64(offset + ef_buf);
  1676. offset += sizeof(G.crec.relative_offset_local_header);
  1677. }
  1678. if (G.crec.disk_number_start == 0xffff){
  1679. G.crec.disk_number_start = (zuvl_t)makelong(offset + ef_buf);
  1680. offset += sizeof(G.crec.disk_number_start);
  1681. }
  1682. }
  1683. /* Skip this extra field block */
  1684. ef_buf += (eb_len + EB_HEADSIZE);
  1685. ef_len -= (eb_len + EB_HEADSIZE);
  1686. }
  1687. return PK_COOL;
  1688. } /* end function getZip64Data() */
  1689. #ifdef UNICODE_SUPPORT
  1690. /*******************************/
  1691. /* Function getUnicodeData() */
  1692. /*******************************/
  1693. int getUnicodeData(__G__ ef_buf, ef_len)
  1694. __GDEF
  1695. ZCONST uch *ef_buf; /* buffer containing extra field */
  1696. unsigned ef_len; /* total length of extra field */
  1697. {
  1698. unsigned eb_id;
  1699. unsigned eb_len;
  1700. /*---------------------------------------------------------------------------
  1701. This function scans the extra field for Unicode information, ie UTF-8
  1702. path extra fields.
  1703. On return, G.unipath_filename =
  1704. NULL, if no Unicode path extra field or error
  1705. "", if the standard path is UTF-8 (free when done)
  1706. null-terminated UTF-8 path (free when done)
  1707. Return PK_COOL if no error.
  1708. ---------------------------------------------------------------------------*/
  1709. G.unipath_filename = NULL;
  1710. if (ef_len == 0 || ef_buf == NULL)
  1711. return PK_COOL;
  1712. Trace((stderr,"\ngetUnicodeData: scanning extra field of length %u\n",
  1713. ef_len));
  1714. while (ef_len >= EB_HEADSIZE) {
  1715. eb_id = makeword(EB_ID + ef_buf);
  1716. eb_len = makeword(EB_LEN + ef_buf);
  1717. if (eb_len > (ef_len - EB_HEADSIZE)) {
  1718. /* discovered some extra field inconsistency! */
  1719. Trace((stderr,
  1720. "getUnicodeData: block length %u > rest ef_size %u\n", eb_len,
  1721. ef_len - EB_HEADSIZE));
  1722. break;
  1723. }
  1724. if (eb_id == EF_UNIPATH) {
  1725. int offset = EB_HEADSIZE;
  1726. ush ULen = eb_len - 5;
  1727. ulg chksum = CRCVAL_INITIAL;
  1728. /* version */
  1729. G.unipath_version = (uch) *(offset + ef_buf);
  1730. offset += 1;
  1731. if (G.unipath_version > 1) {
  1732. /* can do only version 1 */
  1733. Info(slide, 0x401, ((char *)slide,
  1734. LoadFarString(UnicodeVersionError)));
  1735. return PK_ERR;
  1736. }
  1737. /* filename CRC */
  1738. G.unipath_checksum = makelong(offset + ef_buf);
  1739. offset += 4;
  1740. /*
  1741. * Compute 32-bit crc
  1742. */
  1743. chksum = crc32(chksum, (uch *)(G.filename_full),
  1744. strlen(G.filename_full));
  1745. /* If the checksums's don't match then likely filename has been
  1746. * modified and the Unicode Path is no longer valid.
  1747. */
  1748. if (chksum != G.unipath_checksum) {
  1749. Info(slide, 0x401, ((char *)slide,
  1750. LoadFarString(UnicodeMismatchError)));
  1751. if (G.unicode_mismatch == 1) {
  1752. /* warn and continue */
  1753. } else if (G.unicode_mismatch == 2) {
  1754. /* ignore and continue */
  1755. } else if (G.unicode_mismatch == 0) {
  1756. }
  1757. return PK_ERR;
  1758. }
  1759. /* UTF-8 Path */
  1760. if ((G.unipath_filename = malloc(ULen + 1)) == NULL) {
  1761. return PK_ERR;
  1762. }
  1763. if (ULen == 0) {
  1764. /* standard path is UTF-8 so use that */
  1765. G.unipath_filename[0] = '\0';
  1766. } else {
  1767. /* UTF-8 path */
  1768. strncpy(G.unipath_filename,
  1769. (ZCONST char *)(offset + ef_buf), ULen);
  1770. G.unipath_filename[ULen] = '\0';
  1771. }
  1772. }
  1773. /* Skip this extra field block */
  1774. ef_buf += (eb_len + EB_HEADSIZE);
  1775. ef_len -= (eb_len + EB_HEADSIZE);
  1776. }
  1777. return PK_COOL;
  1778. } /* end function getUnicodeData() */
  1779. #ifdef UNICODE_WCHAR
  1780. /*---------------------------------------------
  1781. * Unicode conversion functions
  1782. *
  1783. * Based on functions provided by Paul Kienitz
  1784. *
  1785. *---------------------------------------------
  1786. */
  1787. /*
  1788. NOTES APPLICABLE TO ALL STRING FUNCTIONS:
  1789. All of the x_to_y functions take parameters for an output buffer and
  1790. its available length, and return an int. The value returned is the
  1791. length of the string that the input produces, which may be larger than
  1792. the provided buffer length. If the returned value is less than the
  1793. buffer length, then the contents of the buffer will be null-terminated;
  1794. otherwise, it will not be terminated and may be invalid, possibly
  1795. stopping in the middle of a multibyte sequence.
  1796. In all cases you may pass NULL as the buffer and/or 0 as the length, if
  1797. you just want to learn how much space the string is going to require.
  1798. The functions will return -1 if the input is invalid UTF-8 or cannot be
  1799. encoded as UTF-8.
  1800. */
  1801. static int utf8_char_bytes OF((ZCONST char *utf8));
  1802. static ulg ucs4_char_from_utf8 OF((ZCONST char **utf8));
  1803. static int utf8_to_ucs4_string OF((ZCONST char *utf8, ulg *ucs4buf,
  1804. int buflen));
  1805. /* utility functions for managing UTF-8 and UCS-4 strings */
  1806. /* utf8_char_bytes
  1807. *
  1808. * Returns the number of bytes used by the first character in a UTF-8
  1809. * string, or -1 if the UTF-8 is invalid or null.
  1810. */
  1811. static int utf8_char_bytes(utf8)
  1812. ZCONST char *utf8;
  1813. {
  1814. int t, r;
  1815. unsigned lead;
  1816. if (!utf8)
  1817. return -1; /* no input */
  1818. lead = (unsigned char) *utf8;
  1819. if (lead < 0x80)
  1820. r = 1; /* an ascii-7 character */
  1821. else if (lead < 0xC0)
  1822. return -1; /* error: trailing byte without lead byte */
  1823. else if (lead < 0xE0)
  1824. r = 2; /* an 11 bit character */
  1825. else if (lead < 0xF0)
  1826. r = 3; /* a 16 bit character */
  1827. else if (lead < 0xF8)
  1828. r = 4; /* a 21 bit character (the most currently used) */
  1829. else if (lead < 0xFC)
  1830. r = 5; /* a 26 bit character (shouldn't happen) */
  1831. else if (lead < 0xFE)
  1832. r = 6; /* a 31 bit character (shouldn't happen) */
  1833. else
  1834. return -1; /* error: invalid lead byte */
  1835. for (t = 1; t < r; t++)
  1836. if ((unsigned char) utf8[t] < 0x80 || (unsigned char) utf8[t] >= 0xC0)
  1837. return -1; /* error: not enough valid trailing bytes */
  1838. return r;
  1839. }
  1840. /* ucs4_char_from_utf8
  1841. *
  1842. * Given a reference to a pointer into a UTF-8 string, returns the next
  1843. * UCS-4 character and advances the pointer to the next character sequence.
  1844. * Returns ~0 (= -1 in twos-complement notation) and does not advance the
  1845. * pointer when input is ill-formed.
  1846. */
  1847. static ulg ucs4_char_from_utf8(utf8)
  1848. ZCONST char **utf8;
  1849. {
  1850. ulg ret;
  1851. int t, bytes;
  1852. if (!utf8)
  1853. return ~0L; /* no input */
  1854. bytes = utf8_char_bytes(*utf8);
  1855. if (bytes <= 0)
  1856. return ~0L; /* invalid input */
  1857. if (bytes == 1)
  1858. ret = **utf8; /* ascii-7 */
  1859. else
  1860. ret = **utf8 & (0x7F >> bytes); /* lead byte of a multibyte sequence */
  1861. (*utf8)++;
  1862. for (t = 1; t < bytes; t++) /* consume trailing bytes */
  1863. ret = (ret << 6) | (*((*utf8)++) & 0x3F);
  1864. return (zwchar) ret;
  1865. }
  1866. #if 0 /* currently unused */
  1867. /* utf8_from_ucs4_char - Convert UCS char to UTF-8
  1868. *
  1869. * Returns the number of bytes put into utf8buf to represent ch, from 1 to 6,
  1870. * or -1 if ch is too large to represent. utf8buf must have room for 6 bytes.
  1871. */
  1872. static int utf8_from_ucs4_char(utf8buf, ch)
  1873. char *utf8buf;
  1874. ulg ch;
  1875. {
  1876. int trailing = 0;
  1877. int leadmask = 0x80;
  1878. int leadbits = 0x3F;
  1879. int tch = ch;
  1880. int ret;
  1881. if (ch > 0x7FFFFFFFL)
  1882. return -1; /* UTF-8 can represent 31 bits */
  1883. if (ch < 0x7F)
  1884. {
  1885. *utf8buf++ = (char) ch; /* ascii-7 */
  1886. return 1;
  1887. }
  1888. do {
  1889. trailing++;
  1890. leadmask = (leadmask >> 1) | 0x80;
  1891. leadbits >>= 1;
  1892. tch >>= 6;
  1893. } while (tch & ~leadbits);
  1894. ret = trailing + 1;
  1895. /* produce lead byte */
  1896. *utf8buf++ = (char) (leadmask | (ch >> (6 * trailing)));
  1897. while (--trailing >= 0)
  1898. /* produce trailing bytes */
  1899. *utf8buf++ = (char) (0x80 | ((ch >> (6 * trailing)) & 0x3F));
  1900. return ret;
  1901. }
  1902. #endif /* unused */
  1903. /*===================================================================*/
  1904. /* utf8_to_ucs4_string - convert UTF-8 string to UCS string
  1905. *
  1906. * Return UCS count. Now returns int so can return -1.
  1907. */
  1908. static int utf8_to_ucs4_string(utf8, ucs4buf, buflen)
  1909. ZCONST char *utf8;
  1910. ulg *ucs4buf;
  1911. int buflen;
  1912. {
  1913. int count = 0;
  1914. for (;;)
  1915. {
  1916. ulg ch = ucs4_char_from_utf8(&utf8);
  1917. if (ch == ~0L)
  1918. return -1;
  1919. else
  1920. {
  1921. if (ucs4buf && count < buflen)
  1922. ucs4buf[count] = ch;
  1923. if (ch == 0)
  1924. return count;
  1925. count++;
  1926. }
  1927. }
  1928. }
  1929. #if 0 /* currently unused */
  1930. /* ucs4_string_to_utf8
  1931. *
  1932. *
  1933. */
  1934. static int ucs4_string_to_utf8(ucs4, utf8buf, buflen)
  1935. ZCONST ulg *ucs4;
  1936. char *utf8buf;
  1937. int buflen;
  1938. {
  1939. char mb[6];
  1940. int count = 0;
  1941. if (!ucs4)
  1942. return -1;
  1943. for (;;)
  1944. {
  1945. int mbl = utf8_from_ucs4_char(mb, *ucs4++);
  1946. int c;
  1947. if (mbl <= 0)
  1948. return -1;
  1949. /* We could optimize this a bit by passing utf8buf + count */
  1950. /* directly to utf8_from_ucs4_char when buflen >= count + 6... */
  1951. c = buflen - count;
  1952. if (mbl < c)
  1953. c = mbl;
  1954. if (utf8buf && count < buflen)
  1955. strncpy(utf8buf + count, mb, c);
  1956. if (mbl == 1 && !mb[0])
  1957. return count; /* terminating nul */
  1958. count += mbl;
  1959. }
  1960. }
  1961. /* utf8_chars
  1962. *
  1963. * Wrapper: counts the actual unicode characters in a UTF-8 string.
  1964. */
  1965. static int utf8_chars(utf8)
  1966. ZCONST char *utf8;
  1967. {
  1968. return utf8_to_ucs4_string(utf8, NULL, 0);
  1969. }
  1970. #endif /* unused */
  1971. /* --------------------------------------------------- */
  1972. /* Unicode Support
  1973. *
  1974. * These functions common for all Unicode ports.
  1975. *
  1976. * These functions should allocate and return strings that can be
  1977. * freed with free().
  1978. *
  1979. * 8/27/05 EG
  1980. *
  1981. * Use zwchar for wide char which is unsigned long
  1982. * in zip.h and 32 bits. This avoids problems with
  1983. * different sizes of wchar_t.
  1984. */
  1985. #if 0 /* currently unused */
  1986. /* is_ascii_string
  1987. * Checks if a string is all ascii
  1988. */
  1989. int is_ascii_string(mbstring)
  1990. ZCONST char *mbstring;
  1991. {
  1992. char *p;
  1993. uch c;
  1994. for (p = mbstring; c = (uch)*p; p++) {
  1995. if (c > 0x7F) {
  1996. return 0;
  1997. }
  1998. }
  1999. return 1;
  2000. }
  2001. /* local to UTF-8 */
  2002. char *local_to_utf8_string(local_string)
  2003. ZCONST char *local_string;
  2004. {
  2005. return wide_to_utf8_string(local_to_wide_string(local_string));
  2006. }
  2007. # endif /* unused */
  2008. /* wide_to_escape_string
  2009. provides a string that represents a wide char not in local char set
  2010. An initial try at an algorithm. Suggestions welcome.
  2011. According to the standard, Unicode character points are restricted to
  2012. the number range from 0 to 0x10FFFF, respective 21 bits.
  2013. For a hexadecimal notation, 2 octets are sufficient for the mostly
  2014. used characters from the "Basic Multilingual Plane", all other
  2015. Unicode characters can be represented by 3 octets (= 6 hex digits).
  2016. The Unicode standard suggests to write Unicode character points
  2017. as 4 resp. 6 hex digits, preprended by "U+".
  2018. (e.g.: U+10FFFF for the highest character point, or U+0030 for the ASCII
  2019. digit "0")
  2020. However, for the purpose of escaping non-ASCII chars in an ASCII character
  2021. stream, the "U" is not a very good escape initializer. Therefore, we
  2022. use the following convention within our Info-ZIP code:
  2023. If not an ASCII char probably need 2 bytes at least. So if
  2024. a 2-byte wide encode it as 4 hex digits with a leading #U. If
  2025. needs 3 bytes then prefix the string with #L. So
  2026. #U1234
  2027. is a 2-byte wide character with bytes 0x12 and 0x34 while
  2028. #L123456
  2029. is a 3-byte wide character with bytes 0x12, 0x34, 0x56.
  2030. On Windows, wide that need two wide characters need to be converted
  2031. to a single number.
  2032. */
  2033. /* set this to the max bytes an escape can be */
  2034. #define MAX_ESCAPE_BYTES 8
  2035. char *wide_to_escape_string(wide_char)
  2036. zwchar wide_char;
  2037. {
  2038. int i;
  2039. zwchar w = wide_char;
  2040. uch b[sizeof(zwchar)];
  2041. char d[3];
  2042. char e[11];
  2043. int len;
  2044. char *r;
  2045. /* fill byte array with zeros */
  2046. memzero(b, sizeof(zwchar));
  2047. /* get bytes in right to left order */
  2048. for (len = 0; w; len++) {
  2049. b[len] = (char)(w % 0x100);
  2050. w /= 0x100;
  2051. }
  2052. strcpy(e, "#");
  2053. /* either 2 bytes or 3 bytes */
  2054. if (len <= 2) {
  2055. len = 2;
  2056. strcat(e, "U");
  2057. } else {
  2058. strcat(e, "L");
  2059. }
  2060. for (i = len - 1; i >= 0; i--) {
  2061. sprintf(d, "%02x", b[i]);
  2062. strcat(e, d);
  2063. }
  2064. if ((r = malloc(strlen(e) + 1)) == NULL) {
  2065. return NULL;
  2066. }
  2067. strcpy(r, e);
  2068. return r;
  2069. }
  2070. #if 0 /* currently unused */
  2071. /* returns the wide character represented by the escape string */
  2072. zwchar escape_string_to_wide(escape_string)
  2073. ZCONST char *escape_string;
  2074. {
  2075. int i;
  2076. zwchar w;
  2077. char c;
  2078. int len;
  2079. ZCONST char *e = escape_string;
  2080. if (e == NULL) {
  2081. return 0;
  2082. }
  2083. if (e[0] != '#') {
  2084. /* no leading # */
  2085. return 0;
  2086. }
  2087. len = strlen(e);
  2088. /* either #U1234 or #L123456 format */
  2089. if (len != 6 && len != 8) {
  2090. return 0;
  2091. }
  2092. w = 0;
  2093. if (e[1] == 'L') {
  2094. if (len != 8) {
  2095. return 0;
  2096. }
  2097. /* 3 bytes */
  2098. for (i = 2; i < 8; i++) {
  2099. c = e[i];
  2100. if (c < '0' || c > '9') {
  2101. return 0;
  2102. }
  2103. w = w * 0x10 + (zwchar)(c - '0');
  2104. }
  2105. } else if (e[1] == 'U') {
  2106. /* 2 bytes */
  2107. for (i = 2; i < 6; i++) {
  2108. c = e[i];
  2109. if (c < '0' || c > '9') {
  2110. return 0;
  2111. }
  2112. w = w * 0x10 + (zwchar)(c - '0');
  2113. }
  2114. }
  2115. return w;
  2116. }
  2117. #endif /* unused */
  2118. #ifndef WIN32 /* WIN32 supplies a special variant of this function */
  2119. /* convert wide character string to multi-byte character string */
  2120. char *wide_to_local_string(wide_string, escape_all)
  2121. ZCONST zwchar *wide_string;
  2122. int escape_all;
  2123. {
  2124. int i;
  2125. wchar_t wc;
  2126. int b;
  2127. int state_dependent;
  2128. int wsize = 0;
  2129. int max_bytes = MB_CUR_MAX;
  2130. char buf[9];
  2131. char *buffer = NULL;
  2132. char *local_string = NULL;
  2133. for (wsize = 0; wide_string[wsize]; wsize++) ;
  2134. if (max_bytes < MAX_ESCAPE_BYTES)
  2135. max_bytes = MAX_ESCAPE_BYTES;
  2136. if ((buffer = (char *)malloc(wsize * max_bytes + 1)) == NULL) {
  2137. return NULL;
  2138. }
  2139. /* convert it */
  2140. buffer[0] = '\0';
  2141. /* set initial state if state-dependent encoding */
  2142. wc = (wchar_t)'a';
  2143. b = wctomb(NULL, wc);
  2144. if (b == 0)
  2145. state_dependent = 0;
  2146. else
  2147. state_dependent = 1;
  2148. for (i = 0; i < wsize; i++) {
  2149. if (sizeof(wchar_t) < 4 && wide_string[i] > 0xFFFF) {
  2150. /* wchar_t probably 2 bytes */
  2151. /* could do surrogates if state_dependent and wctomb can do */
  2152. wc = zwchar_to_wchar_t_default_char;
  2153. } else {
  2154. wc = (wchar_t)wide_string[i];
  2155. }
  2156. b = wctomb(buf, wc);
  2157. if (escape_all) {
  2158. if (b == 1 && (uch)buf[0] <= 0x7f) {
  2159. /* ASCII */
  2160. strncat(buffer, buf, b);
  2161. } else {
  2162. /* use escape for wide character */
  2163. char *escape_string = wide_to_escape_string(wide_string[i]);
  2164. strcat(buffer, escape_string);
  2165. free(escape_string);
  2166. }
  2167. } else if (b > 0) {
  2168. /* multi-byte char */
  2169. strncat(buffer, buf, b);
  2170. } else {
  2171. /* no MB for this wide */
  2172. /* use escape for wide character */
  2173. char *escape_string = wide_to_escape_string(wide_string[i]);
  2174. strcat(buffer, escape_string);
  2175. free(escape_string);
  2176. }
  2177. }
  2178. if ((local_string = (char *)malloc(strlen(buffer) + 1)) != NULL) {
  2179. strcpy(local_string, buffer);
  2180. }
  2181. free(buffer);
  2182. return local_string;
  2183. }
  2184. #endif /* !WIN32 */
  2185. #if 0 /* currently unused */
  2186. /* convert local string to display character set string */
  2187. char *local_to_display_string(local_string)
  2188. ZCONST char *local_string;
  2189. {
  2190. char *display_string;
  2191. /* For Windows, OEM string should never be bigger than ANSI string, says
  2192. CharToOem description.
  2193. For all other ports, just make a copy of local_string.
  2194. */
  2195. if ((display_string = (char *)malloc(strlen(local_string) + 1)) == NULL) {
  2196. return NULL;
  2197. }
  2198. strcpy(display_string, local_string);
  2199. #ifdef EBCDIC
  2200. {
  2201. char *ebc;
  2202. if ((ebc = malloc(strlen(display_string) + 1)) == NULL) {
  2203. return NULL;
  2204. }
  2205. strtoebc(ebc, display_string);
  2206. free(display_string);
  2207. display_string = ebc;
  2208. }
  2209. #endif
  2210. return display_string;
  2211. }
  2212. #endif /* unused */
  2213. /* UTF-8 to local */
  2214. char *utf8_to_local_string(utf8_string, escape_all)
  2215. ZCONST char *utf8_string;
  2216. int escape_all;
  2217. {
  2218. zwchar *wide = utf8_to_wide_string(utf8_string);
  2219. char *loc = wide_to_local_string(wide, escape_all);
  2220. free(wide);
  2221. return loc;
  2222. }
  2223. #if 0 /* currently unused */
  2224. /* convert multi-byte character string to wide character string */
  2225. zwchar *local_to_wide_string(local_string)
  2226. ZCONST char *local_string;
  2227. {
  2228. int wsize;
  2229. wchar_t *wc_string;
  2230. zwchar *wide_string;
  2231. /* for now try to convert as string - fails if a bad char in string */
  2232. wsize = mbstowcs(NULL, local_string, strlen(local_string) + 1);
  2233. if (wsize == (size_t)-1) {
  2234. /* could not convert */
  2235. return NULL;
  2236. }
  2237. /* convert it */
  2238. if ((wc_string = (wchar_t *)malloc((wsize + 1) * sizeof(wchar_t))) == NULL) {
  2239. return NULL;
  2240. }
  2241. wsize = mbstowcs(wc_string, local_string, strlen(local_string) + 1);
  2242. wc_string[wsize] = (wchar_t) 0;
  2243. /* in case wchar_t is not zwchar */
  2244. if ((wide_string = (zwchar *)malloc((wsize + 1) * sizeof(zwchar))) == NULL) {
  2245. return NULL;
  2246. }
  2247. for (wsize = 0; wide_string[wsize] = (zwchar)wc_string[wsize]; wsize++) ;
  2248. wide_string[wsize] = (zwchar) 0;
  2249. free(wc_string);
  2250. return wide_string;
  2251. }
  2252. /* convert wide string to UTF-8 */
  2253. char *wide_to_utf8_string(wide_string)
  2254. ZCONST zwchar *wide_string;
  2255. {
  2256. int mbcount;
  2257. char *utf8_string;
  2258. /* get size of utf8 string */
  2259. mbcount = ucs4_string_to_utf8(wide_string, NULL, 0);
  2260. if (mbcount == -1)
  2261. return NULL;
  2262. if ((utf8_string = (char *) malloc(mbcount + 1)) == NULL) {
  2263. return NULL;
  2264. }
  2265. mbcount = ucs4_string_to_utf8(wide_string, utf8_string, mbcount + 1);
  2266. if (mbcount == -1)
  2267. return NULL;
  2268. return utf8_string;
  2269. }
  2270. #endif /* unused */
  2271. /* convert UTF-8 string to wide string */
  2272. zwchar *utf8_to_wide_string(utf8_string)
  2273. ZCONST char *utf8_string;
  2274. {
  2275. int wcount;
  2276. zwchar *wide_string;
  2277. wcount = utf8_to_ucs4_string(utf8_string, NULL, 0);
  2278. if (wcount == -1)
  2279. return NULL;
  2280. if ((wide_string = (zwchar *) malloc((wcount + 1) * sizeof(zwchar)))
  2281. == NULL) {
  2282. return NULL;
  2283. }
  2284. wcount = utf8_to_ucs4_string(utf8_string, wide_string, wcount + 1);
  2285. return wide_string;
  2286. }
  2287. #endif /* UNICODE_WCHAR */
  2288. #endif /* UNICODE_SUPPORT */
  2289. #ifdef USE_EF_UT_TIME
  2290. #ifdef IZ_HAVE_UXUIDGID
  2291. static int read_ux3_value(dbuf, uidgid_sz, p_uidgid)
  2292. ZCONST uch *dbuf; /* buffer a uid or gid value */
  2293. unsigned uidgid_sz; /* size of uid/gid value */
  2294. ulg *p_uidgid; /* return storage: uid or gid value */
  2295. {
  2296. zusz_t uidgid64;
  2297. switch (uidgid_sz) {
  2298. case 2:
  2299. *p_uidgid = (ulg)makeword(dbuf);
  2300. break;
  2301. case 4:
  2302. *p_uidgid = (ulg)makelong(dbuf);
  2303. break;
  2304. case 8:
  2305. uidgid64 = makeint64(dbuf);
  2306. #ifndef LARGE_FILE_SUPPORT
  2307. if (uidgid64 == (zusz_t)0xffffffffL)
  2308. return FALSE;
  2309. #endif
  2310. *p_uidgid = (ulg)uidgid64;
  2311. if ((zusz_t)(*p_uidgid) != uidgid64)
  2312. return FALSE;
  2313. break;
  2314. }
  2315. return TRUE;
  2316. }
  2317. #endif /* IZ_HAVE_UXUIDGID */
  2318. /*******************************/
  2319. /* Function ef_scan_for_izux() */
  2320. /*******************************/
  2321. unsigned ef_scan_for_izux(ef_buf, ef_len, ef_is_c, dos_mdatetime,
  2322. z_utim, z_uidgid)
  2323. ZCONST uch *ef_buf; /* buffer containing extra field */
  2324. unsigned ef_len; /* total length of extra field */
  2325. int ef_is_c; /* flag indicating "is central extra field" */
  2326. ulg dos_mdatetime; /* last_mod_file_date_time in DOS format */
  2327. iztimes *z_utim; /* return storage: atime, mtime, ctime */
  2328. ulg *z_uidgid; /* return storage: uid and gid */
  2329. {
  2330. unsigned flags = 0;
  2331. unsigned eb_id;
  2332. unsigned eb_len;
  2333. int have_new_type_eb = 0;
  2334. long i_time; /* buffer for Unix style 32-bit integer time value */
  2335. #ifdef TIME_T_TYPE_DOUBLE
  2336. int ut_in_archive_sgn = 0;
  2337. #else
  2338. int ut_zip_unzip_compatible = FALSE;
  2339. #endif
  2340. /*---------------------------------------------------------------------------
  2341. This function scans the extra field for EF_TIME, EF_IZUNIX2, EF_IZUNIX, or
  2342. EF_PKUNIX blocks containing Unix-style time_t (GMT) values for the entry's
  2343. access, creation, and modification time.
  2344. If a valid block is found, the time stamps are copied to the iztimes
  2345. structure (provided the z_utim pointer is not NULL).
  2346. If a IZUNIX2 block is found or the IZUNIX block contains UID/GID fields,
  2347. and the z_uidgid array pointer is valid (!= NULL), the owner info is
  2348. transfered as well.
  2349. The presence of an EF_TIME or EF_IZUNIX2 block results in ignoring all
  2350. data from probably present obsolete EF_IZUNIX blocks.
  2351. If multiple blocks of the same type are found, only the information from
  2352. the last block is used.
  2353. The return value is a combination of the EF_TIME Flags field with an
  2354. additional flag bit indicating the presence of valid UID/GID info,
  2355. or 0 in case of failure.
  2356. ---------------------------------------------------------------------------*/
  2357. if (ef_len == 0 || ef_buf == NULL || (z_utim == 0 && z_uidgid == NULL))
  2358. return 0;
  2359. TTrace((stderr,"\nef_scan_for_izux: scanning extra field of length %u\n",
  2360. ef_len));
  2361. while (ef_len >= EB_HEADSIZE) {
  2362. eb_id = makeword(EB_ID + ef_buf);
  2363. eb_len = makeword(EB_LEN + ef_buf);
  2364. if (eb_len > (ef_len - EB_HEADSIZE)) {
  2365. /* discovered some extra field inconsistency! */
  2366. TTrace((stderr,
  2367. "ef_scan_for_izux: block length %u > rest ef_size %u\n", eb_len,
  2368. ef_len - EB_HEADSIZE));
  2369. break;
  2370. }
  2371. switch (eb_id) {
  2372. case EF_TIME:
  2373. flags &= ~0x0ff; /* ignore previous IZUNIX or EF_TIME fields */
  2374. have_new_type_eb = 1;
  2375. if ( eb_len >= EB_UT_MINLEN && z_utim != NULL) {
  2376. unsigned eb_idx = EB_UT_TIME1;
  2377. TTrace((stderr,"ef_scan_for_izux: found TIME extra field\n"));
  2378. flags |= (ef_buf[EB_HEADSIZE+EB_UT_FLAGS] & 0x0ff);
  2379. if ((flags & EB_UT_FL_MTIME)) {
  2380. if ((eb_idx+4) <= eb_len) {
  2381. i_time = (long)makelong((EB_HEADSIZE+eb_idx) + ef_buf);
  2382. eb_idx += 4;
  2383. TTrace((stderr," UT e.f. modification time = %ld\n",
  2384. i_time));
  2385. #ifdef TIME_T_TYPE_DOUBLE
  2386. if ((ulg)(i_time) & (ulg)(0x80000000L)) {
  2387. if (dos_mdatetime == DOSTIME_MINIMUM) {
  2388. ut_in_archive_sgn = -1;
  2389. z_utim->mtime =
  2390. (time_t)((long)i_time | (~(long)0x7fffffffL));
  2391. } else if (dos_mdatetime >= DOSTIME_2038_01_18) {
  2392. ut_in_archive_sgn = 1;
  2393. z_utim->mtime =
  2394. (time_t)((ulg)i_time & (ulg)0xffffffffL);
  2395. } else {
  2396. ut_in_archive_sgn = 0;
  2397. /* cannot determine sign of mtime;
  2398. without modtime: ignore complete UT field */
  2399. flags &= ~0x0ff; /* no time_t times available */
  2400. TTrace((stderr,
  2401. " UT modtime range error; ignore e.f.!\n"));
  2402. break; /* stop scanning this field */
  2403. }
  2404. } else {
  2405. /* cannot determine, safe assumption is FALSE */
  2406. ut_in_archive_sgn = 0;
  2407. z_utim->mtime = (time_t)i_time;
  2408. }
  2409. #else /* !TIME_T_TYPE_DOUBLE */
  2410. if ((ulg)(i_time) & (ulg)(0x80000000L)) {
  2411. ut_zip_unzip_compatible =
  2412. ((time_t)0x80000000L < (time_t)0L)
  2413. ? (dos_mdatetime == DOSTIME_MINIMUM)
  2414. : (dos_mdatetime >= DOSTIME_2038_01_18);
  2415. if (!ut_zip_unzip_compatible) {
  2416. /* UnZip interprets mtime differently than Zip;
  2417. without modtime: ignore complete UT field */
  2418. flags &= ~0x0ff; /* no time_t times available */
  2419. TTrace((stderr,
  2420. " UT modtime range error; ignore e.f.!\n"));
  2421. break; /* stop scanning this field */
  2422. }
  2423. } else {
  2424. /* cannot determine, safe assumption is FALSE */
  2425. ut_zip_unzip_compatible = FALSE;
  2426. }
  2427. z_utim->mtime = (time_t)i_time;
  2428. #endif /* ?TIME_T_TYPE_DOUBLE */
  2429. } else {
  2430. flags &= ~EB_UT_FL_MTIME;
  2431. TTrace((stderr," UT e.f. truncated; no modtime\n"));
  2432. }
  2433. }
  2434. if (ef_is_c) {
  2435. break; /* central version of TIME field ends here */
  2436. }
  2437. if (flags & EB_UT_FL_ATIME) {
  2438. if ((eb_idx+4) <= eb_len) {
  2439. i_time = (long)makelong((EB_HEADSIZE+eb_idx) + ef_buf);
  2440. eb_idx += 4;
  2441. TTrace((stderr," UT e.f. access time = %ld\n",
  2442. i_time));
  2443. #ifdef TIME_T_TYPE_DOUBLE
  2444. if ((ulg)(i_time) & (ulg)(0x80000000L)) {
  2445. if (ut_in_archive_sgn == -1)
  2446. z_utim->atime =
  2447. (time_t)((long)i_time | (~(long)0x7fffffffL));
  2448. } else if (ut_in_archive_sgn == 1) {
  2449. z_utim->atime =
  2450. (time_t)((ulg)i_time & (ulg)0xffffffffL);
  2451. } else {
  2452. /* sign of 32-bit time is unknown -> ignore it */
  2453. flags &= ~EB_UT_FL_ATIME;
  2454. TTrace((stderr,
  2455. " UT access time range error: skip time!\n"));
  2456. }
  2457. } else {
  2458. z_utim->atime = (time_t)i_time;
  2459. }
  2460. #else /* !TIME_T_TYPE_DOUBLE */
  2461. if (((ulg)(i_time) & (ulg)(0x80000000L)) &&
  2462. !ut_zip_unzip_compatible) {
  2463. flags &= ~EB_UT_FL_ATIME;
  2464. TTrace((stderr,
  2465. " UT access time range error: skip time!\n"));
  2466. } else {
  2467. z_utim->atime = (time_t)i_time;
  2468. }
  2469. #endif /* ?TIME_T_TYPE_DOUBLE */
  2470. } else {
  2471. flags &= ~EB_UT_FL_ATIME;
  2472. }
  2473. }
  2474. if (flags & EB_UT_FL_CTIME) {
  2475. if ((eb_idx+4) <= eb_len) {
  2476. i_time = (long)makelong((EB_HEADSIZE+eb_idx) + ef_buf);
  2477. TTrace((stderr," UT e.f. creation time = %ld\n",
  2478. i_time));
  2479. #ifdef TIME_T_TYPE_DOUBLE
  2480. if ((ulg)(i_time) & (ulg)(0x80000000L)) {
  2481. if (ut_in_archive_sgn == -1)
  2482. z_utim->ctime =
  2483. (time_t)((long)i_time | (~(long)0x7fffffffL));
  2484. } else if (ut_in_archive_sgn == 1) {
  2485. z_utim->ctime =
  2486. (time_t)((ulg)i_time & (ulg)0xffffffffL);
  2487. } else {
  2488. /* sign of 32-bit time is unknown -> ignore it */
  2489. flags &= ~EB_UT_FL_CTIME;
  2490. TTrace((stderr,
  2491. " UT creation time range error: skip time!\n"));
  2492. }
  2493. } else {
  2494. z_utim->ctime = (time_t)i_time;
  2495. }
  2496. #else /* !TIME_T_TYPE_DOUBLE */
  2497. if (((ulg)(i_time) & (ulg)(0x80000000L)) &&
  2498. !ut_zip_unzip_compatible) {
  2499. flags &= ~EB_UT_FL_CTIME;
  2500. TTrace((stderr,
  2501. " UT creation time range error: skip time!\n"));
  2502. } else {
  2503. z_utim->ctime = (time_t)i_time;
  2504. }
  2505. #endif /* ?TIME_T_TYPE_DOUBLE */
  2506. } else {
  2507. flags &= ~EB_UT_FL_CTIME;
  2508. }
  2509. }
  2510. }
  2511. break;
  2512. case EF_IZUNIX2:
  2513. if (have_new_type_eb == 0) {
  2514. flags &= ~0x0ff; /* ignore any previous IZUNIX field */
  2515. have_new_type_eb = 1;
  2516. }
  2517. #ifdef IZ_HAVE_UXUIDGID
  2518. if (have_new_type_eb > 1)
  2519. break; /* IZUNIX3 overrides IZUNIX2 e.f. block ! */
  2520. if (eb_len == EB_UX2_MINLEN && z_uidgid != NULL) {
  2521. z_uidgid[0] = (ulg)makeword((EB_HEADSIZE+EB_UX2_UID) + ef_buf);
  2522. z_uidgid[1] = (ulg)makeword((EB_HEADSIZE+EB_UX2_GID) + ef_buf);
  2523. flags |= EB_UX2_VALID; /* signal success */
  2524. }
  2525. #endif
  2526. break;
  2527. case EF_IZUNIX3:
  2528. /* new 3rd generation Unix ef */
  2529. have_new_type_eb = 2;
  2530. /*
  2531. Version 1 byte version of this extra field, currently 1
  2532. UIDSize 1 byte Size of UID field
  2533. UID Variable UID for this entry
  2534. GIDSize 1 byte Size of GID field
  2535. GID Variable GID for this entry
  2536. */
  2537. #ifdef IZ_HAVE_UXUIDGID
  2538. if (eb_len >= EB_UX3_MINLEN
  2539. && z_uidgid != NULL
  2540. && (*((EB_HEADSIZE + 0) + ef_buf) == 1)
  2541. /* only know about version 1 */
  2542. {
  2543. uch uid_size;
  2544. uch gid_size;
  2545. uid_size = *((EB_HEADSIZE + 1) + ef_buf);
  2546. gid_size = *((EB_HEADSIZE + uid_size + 2) + ef_buf);
  2547. flags &= ~0x0ff; /* ignore any previous UNIX field */
  2548. if ( read_ux3_value((EB_HEADSIZE + 2) + ef_buf,
  2549. uid_size, z_uidgid[0])
  2550. &&
  2551. read_ux3_value((EB_HEADSIZE + uid_size + 3) + ef_buf,
  2552. gid_size, z_uidgid[1]) )
  2553. {
  2554. flags |= EB_UX2_VALID; /* signal success */
  2555. }
  2556. }
  2557. #endif /* IZ_HAVE_UXUIDGID */
  2558. break;
  2559. case EF_IZUNIX:
  2560. case EF_PKUNIX: /* PKUNIX e.f. layout is identical to IZUNIX */
  2561. if (eb_len >= EB_UX_MINLEN) {
  2562. TTrace((stderr,"ef_scan_for_izux: found %s extra field\n",
  2563. (eb_id == EF_IZUNIX ? "IZUNIX" : "PKUNIX")));
  2564. if (have_new_type_eb > 0) {
  2565. break; /* Ignore IZUNIX extra field block ! */
  2566. }
  2567. if (z_utim != NULL) {
  2568. flags |= (EB_UT_FL_MTIME | EB_UT_FL_ATIME);
  2569. i_time = (long)makelong((EB_HEADSIZE+EB_UX_MTIME)+ef_buf);
  2570. TTrace((stderr," Unix EF modtime = %ld\n", i_time));
  2571. #ifdef TIME_T_TYPE_DOUBLE
  2572. if ((ulg)(i_time) & (ulg)(0x80000000L)) {
  2573. if (dos_mdatetime == DOSTIME_MINIMUM) {
  2574. ut_in_archive_sgn = -1;
  2575. z_utim->mtime =
  2576. (time_t)((long)i_time | (~(long)0x7fffffffL));
  2577. } else if (dos_mdatetime >= DOSTIME_2038_01_18) {
  2578. ut_in_archive_sgn = 1;
  2579. z_utim->mtime =
  2580. (time_t)((ulg)i_time & (ulg)0xffffffffL);
  2581. } else {
  2582. ut_in_archive_sgn = 0;
  2583. /* cannot determine sign of mtime;
  2584. without modtime: ignore complete UT field */
  2585. flags &= ~0x0ff; /* no time_t times available */
  2586. TTrace((stderr,
  2587. " UX modtime range error: ignore e.f.!\n"));
  2588. }
  2589. } else {
  2590. /* cannot determine, safe assumption is FALSE */
  2591. ut_in_archive_sgn = 0;
  2592. z_utim->mtime = (time_t)i_time;
  2593. }
  2594. #else /* !TIME_T_TYPE_DOUBLE */
  2595. if ((ulg)(i_time) & (ulg)(0x80000000L)) {
  2596. ut_zip_unzip_compatible =
  2597. ((time_t)0x80000000L < (time_t)0L)
  2598. ? (dos_mdatetime == DOSTIME_MINIMUM)
  2599. : (dos_mdatetime >= DOSTIME_2038_01_18);
  2600. if (!ut_zip_unzip_compatible) {
  2601. /* UnZip interpretes mtime differently than Zip;
  2602. without modtime: ignore complete UT field */
  2603. flags &= ~0x0ff; /* no time_t times available */
  2604. TTrace((stderr,
  2605. " UX modtime range error: ignore e.f.!\n"));
  2606. }
  2607. } else {
  2608. /* cannot determine, safe assumption is FALSE */
  2609. ut_zip_unzip_compatible = FALSE;
  2610. }
  2611. z_utim->mtime = (time_t)i_time;
  2612. #endif /* ?TIME_T_TYPE_DOUBLE */
  2613. i_time = (long)makelong((EB_HEADSIZE+EB_UX_ATIME)+ef_buf);
  2614. TTrace((stderr," Unix EF actime = %ld\n", i_time));
  2615. #ifdef TIME_T_TYPE_DOUBLE
  2616. if ((ulg)(i_time) & (ulg)(0x80000000L)) {
  2617. if (ut_in_archive_sgn == -1)
  2618. z_utim->atime =
  2619. (time_t)((long)i_time | (~(long)0x7fffffffL));
  2620. } else if (ut_in_archive_sgn == 1) {
  2621. z_utim->atime =
  2622. (time_t)((ulg)i_time & (ulg)0xffffffffL);
  2623. } else if (flags & 0x0ff) {
  2624. /* sign of 32-bit time is unknown -> ignore it */
  2625. flags &= ~EB_UT_FL_ATIME;
  2626. TTrace((stderr,
  2627. " UX access time range error: skip time!\n"));
  2628. }
  2629. } else {
  2630. z_utim->atime = (time_t)i_time;
  2631. }
  2632. #else /* !TIME_T_TYPE_DOUBLE */
  2633. if (((ulg)(i_time) & (ulg)(0x80000000L)) &&
  2634. !ut_zip_unzip_compatible && (flags & 0x0ff)) {
  2635. /* atime not in range of UnZip's time_t */
  2636. flags &= ~EB_UT_FL_ATIME;
  2637. TTrace((stderr,
  2638. " UX access time range error: skip time!\n"));
  2639. } else {
  2640. z_utim->atime = (time_t)i_time;
  2641. }
  2642. #endif /* ?TIME_T_TYPE_DOUBLE */
  2643. }
  2644. #ifdef IZ_HAVE_UXUIDGID
  2645. if (eb_len >= EB_UX_FULLSIZE && z_uidgid != NULL) {
  2646. z_uidgid[0] = makeword((EB_HEADSIZE+EB_UX_UID) + ef_buf);
  2647. z_uidgid[1] = makeword((EB_HEADSIZE+EB_UX_GID) + ef_buf);
  2648. flags |= EB_UX2_VALID;
  2649. }
  2650. #endif /* IZ_HAVE_UXUIDGID */
  2651. }
  2652. break;
  2653. default:
  2654. break;
  2655. }
  2656. /* Skip this extra field block */
  2657. ef_buf += (eb_len + EB_HEADSIZE);
  2658. ef_len -= (eb_len + EB_HEADSIZE);
  2659. }
  2660. return flags;
  2661. }
  2662. #endif /* USE_EF_UT_TIME */
  2663. #if (defined(RISCOS) || defined(ACORN_FTYPE_NFS))
  2664. #define SPARKID_2 0x30435241 /* = "ARC0" */
  2665. /*******************************/
  2666. /* Function getRISCOSexfield() */
  2667. /*******************************/
  2668. zvoid *getRISCOSexfield(ef_buf, ef_len)
  2669. ZCONST uch *ef_buf; /* buffer containing extra field */
  2670. unsigned ef_len; /* total length of extra field */
  2671. {
  2672. unsigned eb_id;
  2673. unsigned eb_len;
  2674. /*---------------------------------------------------------------------------
  2675. This function scans the extra field for a Acorn SPARK filetype ef-block.
  2676. If a valid block is found, the function returns a pointer to the start
  2677. of the SPARK_EF block in the extra field buffer. Otherwise, a NULL
  2678. pointer is returned.
  2679. ---------------------------------------------------------------------------*/
  2680. if (ef_len == 0 || ef_buf == NULL)
  2681. return NULL;
  2682. Trace((stderr,"\ngetRISCOSexfield: scanning extra field of length %u\n",
  2683. ef_len));
  2684. while (ef_len >= EB_HEADSIZE) {
  2685. eb_id = makeword(EB_ID + ef_buf);
  2686. eb_len = makeword(EB_LEN + ef_buf);
  2687. if (eb_len > (ef_len - EB_HEADSIZE)) {
  2688. /* discovered some extra field inconsistency! */
  2689. Trace((stderr,
  2690. "getRISCOSexfield: block length %u > rest ef_size %u\n", eb_len,
  2691. ef_len - EB_HEADSIZE));
  2692. break;
  2693. }
  2694. if (eb_id == EF_SPARK && (eb_len == 24 || eb_len == 20)) {
  2695. if (makelong(EB_HEADSIZE + ef_buf) == SPARKID_2) {
  2696. /* Return a pointer to the valid SPARK filetype ef block */
  2697. return (zvoid *)ef_buf;
  2698. }
  2699. }
  2700. /* Skip this extra field block */
  2701. ef_buf += (eb_len + EB_HEADSIZE);
  2702. ef_len -= (eb_len + EB_HEADSIZE);
  2703. }
  2704. return NULL;
  2705. }
  2706. #endif /* (RISCOS || ACORN_FTYPE_NFS) */