From b8d2426e1bcc860c2c743e2baebbd9c57f117bb9 Mon Sep 17 00:00:00 2001 From: "jan.nijtmans" Date: Fri, 7 Sep 2018 11:56:40 +0000 Subject: Use GetFileSizeEx() in stead of GetFileSize(), to get the file size on Windows. Minor improvement: attach empty .zip file to tclsh.exe (both for Windows and UNIX), zo "zip -A" can be used to modify its zip contents. --- .fossil-settings/binary-glob | 1 + generic/tclZipfs.c | 29 +++++++++++++++-------------- tools/empty.zip | Bin 0 -> 22 bytes unix/Makefile.in | 1 + win/Makefile.in | 1 + 5 files changed, 18 insertions(+), 14 deletions(-) create mode 100644 tools/empty.zip diff --git a/.fossil-settings/binary-glob b/.fossil-settings/binary-glob index ec574be..5eebf07 100644 --- a/.fossil-settings/binary-glob +++ b/.fossil-settings/binary-glob @@ -4,6 +4,7 @@ compat/zlib/win64/zdll.lib compat/zlib/win64/zlib1.dll compat/zlib/win64/libz.dll.a compat/zlib/zlib.3.pdf +tools/empty.zip *.bmp *.gif *.png \ No newline at end of file diff --git a/generic/tclZipfs.c b/generic/tclZipfs.c index 0ab60e7..4d716df 100644 --- a/generic/tclZipfs.c +++ b/generic/tclZipfs.c @@ -172,9 +172,9 @@ typedef struct ZipFile { char is_membuf; /* When true, not a file but a memory buffer */ Tcl_Channel chan; /* Channel handle or NULL */ unsigned char *data; /* Memory mapped or malloc'ed file */ - size_t length; /* Length of memory mapped file */ + Tcl_WideUInt length; /* Length of memory mapped file */ unsigned char *tofree; /* Non-NULL if malloc'ed file */ - size_t nfiles; /* Number of files in archive */ + size_t nfiles; /* Number of files in archive */ size_t baseoffs; /* Archive start */ size_t baseoffsp; /* Password start */ size_t centoffs; /* Archive directory start */ @@ -1039,7 +1039,7 @@ ZipFSOpenArchive(Tcl_Interp *interp, const char *zipname, int needZip, ZipFile * goto error; } zf->length = Tcl_Seek(zf->chan, 0, SEEK_END); - if ((zf->length <= 0) || (zf->length > 64 * 1024 * 1024)) { + if ((zf->length < ZIP_CENTRAL_END_LEN) || (zf->length > 64 * 1024 * 1024)) { ZIPFS_ERROR(interp,"illegal file size"); goto error; } @@ -1058,9 +1058,9 @@ ZipFSOpenArchive(Tcl_Interp *interp, const char *zipname, int needZip, ZipFile * zf->chan = NULL; } else { #if defined(_WIN32) || defined(_WIN64) - zf->length = GetFileSize((HANDLE) handle, 0); + i = GetFileSizeEx((HANDLE) handle, (PLARGE_INTEGER)&zf->length); if ( - (zf->length == (size_t)INVALID_FILE_SIZE) || + (i == 0) || (zf->length < ZIP_CENTRAL_END_LEN) ) { ZIPFS_ERROR(interp,"invalid file size"); @@ -1078,15 +1078,15 @@ ZipFSOpenArchive(Tcl_Interp *interp, const char *zipname, int needZip, ZipFile * goto error; } #else - zf->length = lseek((int) (long) handle, 0, SEEK_END); + zf->length = lseek(PTR2INT(handle), 0, SEEK_END); if ((zf->length == (size_t)-1) || (zf->length < ZIP_CENTRAL_END_LEN)) { ZIPFS_ERROR(interp,"invalid file size"); goto error; } - lseek((int) (long) handle, 0, SEEK_SET); + lseek(PTR2INT(handle), 0, SEEK_SET); zf->data = (unsigned char *) mmap(0, zf->length, PROT_READ, MAP_FILE | MAP_PRIVATE, - (int) (long) handle, 0); + PTR2INT(handle), 0); if (zf->data == MAP_FAILED) { ZIPFS_ERROR(interp,"file mapping failed"); goto error; @@ -2103,7 +2103,7 @@ wrerr: * Compressed file larger than input, * write it again uncompressed. */ - if ((int) Tcl_Seek(in, 0, SEEK_SET) != 0) { + if (Tcl_Seek(in, 0, SEEK_SET) != 0) { goto seekErr; } if ((int) Tcl_Seek(out, pos[2], SEEK_SET) != pos[2]) { @@ -4311,14 +4311,15 @@ TclZipfs_Init(Tcl_Interp *interp) Unlock(); if(interp != NULL) { static const EnsembleImplMap initMap[] = { - {"mount", ZipFSMountObjCmd, NULL, NULL, NULL, 0}, - {"mount_data", ZipFSMountBufferObjCmd, NULL, NULL, NULL, 0}, - {"unmount", ZipFSUnmountObjCmd, NULL, NULL, NULL, 0}, - {"mkkey", ZipFSMkKeyObjCmd, NULL, NULL, NULL, 0}, {"mkimg", ZipFSMkImgObjCmd, NULL, NULL, NULL, 0}, {"mkzip", ZipFSMkZipObjCmd, NULL, NULL, NULL, 0}, {"lmkimg", ZipFSLMkImgObjCmd, NULL, NULL, NULL, 0}, {"lmkzip", ZipFSLMkZipObjCmd, NULL, NULL, NULL, 0}, + /* The 4 entries above are not available in safe interpreters */ + {"mount", ZipFSMountObjCmd, NULL, NULL, NULL, 0}, + {"mount_data", ZipFSMountBufferObjCmd, NULL, NULL, NULL, 0}, + {"unmount", ZipFSUnmountObjCmd, NULL, NULL, NULL, 0}, + {"mkkey", ZipFSMkKeyObjCmd, NULL, NULL, NULL, 0}, {"exists", ZipFSExistsObjCmd, NULL, NULL, NULL, 1}, {"info", ZipFSInfoObjCmd, NULL, NULL, NULL, 1}, {"list", ZipFSListObjCmd, NULL, NULL, NULL, 1}, @@ -4349,7 +4350,7 @@ TclZipfs_Init(Tcl_Interp *interp) "}\n"; Tcl_EvalEx(interp, findproc, -1, TCL_EVAL_GLOBAL); Tcl_LinkVar(interp, "::tcl::zipfs::wrmax", (char *) &ZipFS.wrmax,TCL_LINK_INT); - TclMakeEnsemble(interp, "zipfs", initMap); + TclMakeEnsemble(interp, "zipfs", Tcl_IsSafe(interp) ? (initMap+4) : initMap); Tcl_PkgProvide(interp, "zipfs", "2.0"); } return TCL_OK; diff --git a/tools/empty.zip b/tools/empty.zip new file mode 100644 index 0000000..15cb0ec Binary files /dev/null and b/tools/empty.zip differ diff --git a/unix/Makefile.in b/unix/Makefile.in index 5569b44..2c005b6 100644 --- a/unix/Makefile.in +++ b/unix/Makefile.in @@ -711,6 +711,7 @@ ${TCL_EXE}: ${TCLSH_OBJS} ${TCL_LIB_FILE} ${TCL_STUB_LIB_FILE} ${CC} ${CFLAGS} ${LDFLAGS} ${TCLSH_OBJS} \ @TCL_BUILD_LIB_SPEC@ ${TCL_STUB_LIB_FILE} ${LIBS} @EXTRA_TCLSH_LIBS@ \ ${CC_SEARCH_FLAGS} -o ${TCL_EXE} + cat ${TOOL_DIR}/empty.zip >> ${TCL_EXE} # Must be empty so it doesn't conflict with rule for ${TCL_EXE} above ${NATIVE_TCLSH}: diff --git a/win/Makefile.in b/win/Makefile.in index a7be485..1d9c522 100644 --- a/win/Makefile.in +++ b/win/Makefile.in @@ -492,6 +492,7 @@ $(TCLSH): $(TCLSH_OBJS) @LIBRARIES@ $(TCL_STUB_LIB_FILE) tclsh.$(RES) $(CC) $(CFLAGS) $(TCLSH_OBJS) $(TCL_LIB_FILE) $(TCL_STUB_LIB_FILE) $(LIBS) \ tclsh.$(RES) $(CC_EXENAME) $(LDFLAGS_CONSOLE) @VC_MANIFEST_EMBED_EXE@ + cat ${TOOL_DIR}/empty.zip >> ${TCLSH} cat32.$(OBJEXT): cat.c $(CC) -c $(CC_SWITCHES) @DEPARG@ $(CC_OBJNAME) -- cgit v0.12