Permalink
Browse files

unzip, bugfix: fix isZipFile error if filename is in unicode (#373)

  • Loading branch information...
asionius authored and xicilion committed Nov 7, 2017
1 parent 0f4adf5 commit e5bb49aa2072fbcd2e4c3886e5a8d44c2c9b9650
Showing with 106 additions and 7 deletions.
  1. +4 −3 fibjs/src/base/fibjs.cpp
  2. +23 −4 fibjs/src/zip/ZipFile.cpp
  3. +70 −0 test/selfzip_test.js
  4. +9 −0 test/unzip_test.js
View
@@ -31,6 +31,7 @@ void init_fs();
void init_fiber();
void init_signal();
void options(int32_t& pos, char* argv[]);
result_t ifZipFile(exlib::string filename, bool& retVal);
exlib::string s_root;
@@ -94,9 +95,9 @@ void main(int32_t argc, char* argv[])
process_base::get_execPath(exePath);
unzFile unz;
if ((unz = unzOpen64(exePath.c_str())) != NULL) {
unzClose(unz);
bool bZip;
ifZipFile(exePath, bZip);
if (bZip) {
exePath.append(1, '$');
ptrArg.resize(argc + 1);
View
@@ -144,13 +144,24 @@ const char* zip_error(int32_t err)
return zError(err);
}
result_t zip_base::isZipFile(exlib::string filename, bool& retVal, AsyncEvent* ac)
#ifdef _WIN32
static voidpf ZCALLBACK fopen64_file_func(voidpf opaque, const void* filename, int mode)
{
if (ac->isSync())
return CHECK_ERROR(CALL_E_NOSYNC);
return (voidpf)::_wfopen(UTF8_W((const char*)filename), UTF8_W((const char*)"rb"));
}
#endif
result_t ifZipFile(exlib::string filename, bool& retVal)
{
unzFile unz;
if ((unz = unzOpen64(filename.c_str())) == NULL)
#ifndef _WIN32
if ((unz = unzOpen2_64(filename.c_str(), NULL)) == NULL)
#else
zlib_filefunc64_def io;
fill_fopen64_filefunc(&io);
io.zopen64_file = fopen64_file_func;
if ((unz = unzOpen2_64(filename.c_str(), &io)) == NULL)
#endif
retVal = false;
else {
retVal = true;
@@ -160,6 +171,14 @@ result_t zip_base::isZipFile(exlib::string filename, bool& retVal, AsyncEvent* a
return 0;
}
result_t zip_base::isZipFile(exlib::string filename, bool& retVal, AsyncEvent* ac)
{
if (ac->isSync())
return CHECK_ERROR(CALL_E_NOSYNC);
return ifZipFile(filename, retVal);
}
result_t zip_base::open(exlib::string path, exlib::string mod, int32_t compress_type,
obj_ptr<ZipFile_base>& retVal, AsyncEvent* ac)
{
View
@@ -11,15 +11,47 @@ var coroutine = require('coroutine');
var execPath = process.execPath;
var testPath = path.join(__dirname, path.basename(execPath));
var unicodeTestPath = path.join(__dirname, "测试" + path.basename(execPath));
describe("selfzip", () => {
after(() => {
try {
fs.unlink(testPath);
fs.unlink(unicodeTestPath);
} catch (e) {}
});
describe("js", () => {
function test_selfzip_with_unicodename(script, argv) {
var ms = new io.MemoryStream();
var zf = zip.open(ms, 'w');
zf.write(new Buffer(script), 'index.js');
zf.close();
ms.rewind();
fs.copy(execPath, unicodeTestPath);
fs.appendFile(unicodeTestPath, ms.readAll());
if (process.platform !== 'win32')
fs.chmod(unicodeTestPath, 511);
var r = process.run(unicodeTestPath, argv);
for (var i = 0; i < 100; i++) {
try {
fs.unlink(unicodeTestPath);
} catch (e) {}
if (!fs.exists(unicodeTestPath))
break;
coroutine.sleep(100);
}
return r;
}
function test_selfzip(script, argv) {
var ms = new io.MemoryStream();
var zf = zip.open(ms, 'w');
@@ -52,22 +84,56 @@ describe("selfzip", () => {
it("zip", () => {
assert.equal(test_selfzip('process.exit(65);', []), 65);
assert.equal(test_selfzip_with_unicodename('process.exit(65);', []), 65);
});
it("argv", () => {
assert.equal(test_selfzip('process.exit(process.argv[1] === process.argv[0] + "$");', []), 1);
assert.equal(test_selfzip_with_unicodename('process.exit(process.argv[1] === process.argv[0] + "$");', []), 1);
});
it("argv size", () => {
assert.equal(test_selfzip('process.exit(process.argv.length);', [1, 2, 3, 4, 5]), 7);
assert.equal(test_selfzip_with_unicodename('process.exit(process.argv.length);', [1, 2, 3, 4, 5]), 7);
});
it("custom argv", () => {
assert.equal(test_selfzip('process.exit(process.argv[2]);', [94]), 94);
assert.equal(test_selfzip_with_unicodename('process.exit(process.argv[2]);', [94]), 94);
});
});
describe("jsc", () => {
function test_selfzip_with_unicodename(script, argv) {
var ms = new io.MemoryStream();
var zf = zip.open(ms, 'w');
zf.write(util.compile('test.js', script, 1), 'index.jsc');
zf.close();
ms.rewind();
fs.copy(execPath, unicodeTestPath);
fs.appendFile(unicodeTestPath, ms.readAll());
if (process.platform !== 'win32')
fs.chmod(unicodeTestPath, 511);
var r = process.run(unicodeTestPath, argv);
for (var i = 0; i < 100; i++) {
try {
fs.unlink(unicodeTestPath);
} catch (e) {}
if (!fs.exists(unicodeTestPath))
break;
coroutine.sleep(100);
}
return r;
}
function test_selfzip(script, argv) {
var ms = new io.MemoryStream();
var zf = zip.open(ms, 'w');
@@ -100,18 +166,22 @@ describe("selfzip", () => {
it("zip", () => {
assert.equal(test_selfzip('process.exit(65);', []), 65);
assert.equal(test_selfzip_with_unicodename('process.exit(65);', []), 65);
});
it("argv", () => {
assert.equal(test_selfzip('process.exit(process.argv[1] === process.argv[0] + "$");', []), 1);
assert.equal(test_selfzip_with_unicodename('process.exit(process.argv[1] === process.argv[0] + "$");', []), 1);
});
it("argv size", () => {
assert.equal(test_selfzip('process.exit(process.argv.length);', [1, 2, 3, 4, 5]), 7);
assert.equal(test_selfzip_with_unicodename('process.exit(process.argv.length);', [1, 2, 3, 4, 5]), 7);
});
it("custom argv", () => {
assert.equal(test_selfzip('process.exit(process.argv[2]);', [94]), 94);
assert.equal(test_selfzip_with_unicodename('process.exit(process.argv[2]);', [94]), 94);
});
});
});
View
@@ -69,6 +69,15 @@ describe("zip", () => {
assert.equal(zip.isZipFile(path.join(__dirname, 'unzip_test.zip' + vmid)), true);
});
it("zip filename in unicode", () => {
var fname = "测试.zip" + vmid;
var f = zip.open(path.join(__dirname, fname), "w");
f.write(path.join(__dirname, 'unzip_test.js'), 'unzip_test.js');
f.close();
assert.equal(zip.isZipFile(path.join(__dirname, fname)), true);
fs.unlink(path.join(__dirname, fname));
})
it("namelist & infolist & getinfo", () => {
zipfile = zip.open(path.join(__dirname, 'unzip_test.zip' + vmid));
var namelist = zipfile.namelist();

0 comments on commit e5bb49a

Please sign in to comment.