mirror of
https://github.com/ONLYOFFICE/core.git
synced 2026-04-07 13:55:33 +08:00
Merge remote-tracking branch 'origin/release/v7.2.0' into feature/pdfwriter
# Conflicts: # Common/3dParty/html/css/src/CCompiledStyle.cpp # Common/3dParty/html/css/src/ConstValues.h # Common/3dParty/html/css/src/xhtml/CDocumentStyle.cpp # HtmlFile2/htmlfile2.cpp
This commit is contained in:
@ -1,107 +0,0 @@
|
||||
import sys
|
||||
sys.path.append("../../../build_tools/scripts")
|
||||
import base
|
||||
import os
|
||||
|
||||
base.configure_common_apps()
|
||||
|
||||
# remove previous version
|
||||
if base.is_dir("./deploy"):
|
||||
base.delete_dir("./deploy")
|
||||
base.create_dir("./deploy")
|
||||
|
||||
# fetch emsdk
|
||||
command_prefix = "" if ("windows" == base.host_platform()) else "./"
|
||||
if not base.is_dir("emsdk"):
|
||||
base.cmd("git", ["clone", "https://github.com/emscripten-core/emsdk.git"])
|
||||
os.chdir("emsdk")
|
||||
base.cmd(command_prefix + "emsdk", ["install", "latest"])
|
||||
base.cmd(command_prefix + "emsdk", ["activate", "latest"])
|
||||
os.chdir("../")
|
||||
|
||||
|
||||
# compile
|
||||
compiler_flags = ["-o zlib.js",
|
||||
"-O3",
|
||||
"-fno-rtti",
|
||||
"-s WASM=1",
|
||||
"-s ALLOW_MEMORY_GROWTH=1",
|
||||
"-s FILESYSTEM=0",
|
||||
"-s ENVIRONMENT='web,worker'"]
|
||||
#"-s LLD_REPORT_UNDEFINED"]
|
||||
|
||||
exported_functions = ["_malloc",
|
||||
"_free",
|
||||
"_Zlib_Malloc",
|
||||
"_Zlib_Free",
|
||||
"_Zlib_Create",
|
||||
"_Zlib_Open",
|
||||
"_Zlib_Close",
|
||||
"_Zlib_AddFile",
|
||||
"_Zlib_RemoveFile",
|
||||
"_Zlib_GetPaths",
|
||||
"_Zlib_GetFile",
|
||||
"_Zlib_Save"]
|
||||
|
||||
libZlib_src_path = "../src/zlib-1.2.11"
|
||||
input_zlib_sources = ["inflate.c", "zutil.c", "adler32.c", "crc32.c", "inftrees.c",
|
||||
"inffast.c", "deflate.c", "trees.c"]
|
||||
|
||||
libMinizip_src_path = "../src/zlib-1.2.11/contrib/minizip"
|
||||
input_minizip_sources = ["unzip.c", "ioapi.c", "zip.c", "ioapibuf.c"]
|
||||
|
||||
sources = []
|
||||
for item in input_zlib_sources:
|
||||
sources.append(libZlib_src_path + '/' + item)
|
||||
for item in input_minizip_sources:
|
||||
sources.append(libMinizip_src_path + '/' + item)
|
||||
sources.append("../src/ZipBuffer.cpp")
|
||||
sources.append("wasm/src/base.cpp")
|
||||
|
||||
compiler_flags.append("-I../src/zlib-1.2.11")
|
||||
compiler_flags.append("-D__linux__")
|
||||
|
||||
# arguments
|
||||
arguments = ""
|
||||
for item in compiler_flags:
|
||||
arguments += (item + " ")
|
||||
|
||||
arguments += "-s EXPORTED_FUNCTIONS=\"["
|
||||
for item in exported_functions:
|
||||
arguments += ("'" + item + "',")
|
||||
arguments = arguments[:-1]
|
||||
arguments += "]\" "
|
||||
for item in sources:
|
||||
arguments += (item + " ")
|
||||
|
||||
# command
|
||||
windows_bat = []
|
||||
if base.host_platform() == "windows":
|
||||
windows_bat.append("call emsdk/emsdk_env.bat")
|
||||
windows_bat.append("call emcc " + arguments)
|
||||
else:
|
||||
windows_bat.append("#!/bin/bash")
|
||||
windows_bat.append("source ./emsdk/emsdk_env.sh")
|
||||
windows_bat.append("emcc " + arguments)
|
||||
base.run_as_bat(windows_bat)
|
||||
|
||||
# finalize
|
||||
base.replaceInFile("./zlib.js", "__ATPOSTRUN__=[];", "__ATPOSTRUN__=[function(){self.onEngineInit();}];")
|
||||
base.replaceInFile("./zlib.js", "function getBinaryPromise(){", "function getBinaryPromise2(){")
|
||||
|
||||
zlib_js_content = base.readFile("./zlib.js")
|
||||
desktop_fetch_content = base.readFile("./../../Common/js/desktop_fetch.js")
|
||||
string_utf8_content = base.readFile("./../../Common/js/string_utf8.js")
|
||||
engine_base_js_content = base.readFile("./wasm/js/zlib_new.js")
|
||||
engine_js_content = engine_base_js_content.replace("//desktop_fetch", desktop_fetch_content)
|
||||
engine_js_content = engine_js_content.replace("//string_utf8", string_utf8_content)
|
||||
engine_js_content = engine_js_content.replace("//module", zlib_js_content)
|
||||
|
||||
# write new version
|
||||
base.writeFile("./deploy/zlib.js", engine_js_content)
|
||||
base.copy_file("./zlib.wasm", "./deploy/zlib.wasm")
|
||||
base.copy_file("./wasm/js/index.html", "./deploy/index.html")
|
||||
base.copy_file("./wasm/js/code.js", "./deploy/code.js")
|
||||
|
||||
base.delete_file("zlib.js")
|
||||
base.delete_file("zlib.wasm")
|
||||
@ -55,12 +55,12 @@ window.onload = function()
|
||||
var archive = window.nativeZlibEngine.save();
|
||||
window.nativeZlibEngine.close();
|
||||
|
||||
window.nativeZlibEngine.open(archive2);
|
||||
var files3 = window.nativeZlibEngine.files;
|
||||
for (var _path in files3)
|
||||
window.nativeZlibEngine.open(archive);
|
||||
var files2 = window.nativeZlibEngine.files;
|
||||
for (var _path in files2)
|
||||
{
|
||||
window.nativeZlibEngine.getFile(_path);
|
||||
window.writeFile(_path, files3[_path]);
|
||||
window.writeFile(_path, files2[_path]);
|
||||
}
|
||||
window.nativeZlibEngine.close();
|
||||
};
|
||||
@ -74,5 +74,5 @@ window.writeFile = function(path, file)
|
||||
{
|
||||
if (!file) return;
|
||||
var dst = document.getElementById("main");
|
||||
dst.innerHTML += path + ' ' + file.length + '\n';
|
||||
dst.innerHTML += ("<p>" + path + ' ' + file.length + "</p>");
|
||||
};
|
||||
|
||||
@ -1,409 +1,324 @@
|
||||
/*
|
||||
* (c) Copyright Ascensio System SIA 2010-2019
|
||||
*
|
||||
* This program is a free software product. You can redistribute it and/or
|
||||
* modify it under the terms of the GNU Affero General Public License (AGPL)
|
||||
* version 3 as published by the Free Software Foundation. In accordance with
|
||||
* Section 7(a) of the GNU AGPL its Section 15 shall be amended to the effect
|
||||
* that Ascensio System SIA expressly excludes the warranty of non-infringement
|
||||
* of any third-party rights.
|
||||
*
|
||||
* This program is distributed WITHOUT ANY WARRANTY; without even the implied
|
||||
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For
|
||||
* details, see the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
|
||||
*
|
||||
* You can contact Ascensio System SIA at 20A-12 Ernesta Birznieka-Upisha
|
||||
* street, Riga, Latvia, EU, LV-1050.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of the Program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU AGPL version 3.
|
||||
*
|
||||
* Pursuant to Section 7(b) of the License you must retain the original Product
|
||||
* logo when distributing the program. Pursuant to Section 7(e) we decline to
|
||||
* grant you any rights under trademark law for use of our trademarks.
|
||||
*
|
||||
* All the Product's GUI elements, including illustrations and icon sets, as
|
||||
* well as technical writing content are licensed under the terms of the
|
||||
* Creative Commons Attribution-ShareAlike 4.0 International. See the License
|
||||
* terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
|
||||
*
|
||||
*/
|
||||
|
||||
(function(window, undefined){
|
||||
|
||||
var printErr = undefined;
|
||||
var print = undefined;
|
||||
//desktop_fetch
|
||||
|
||||
var fetch = self.fetch;
|
||||
var getBinaryPromise = null;
|
||||
if (self.AscDesktopEditor && document.currentScript && 0 == document.currentScript.src.indexOf("file:///"))
|
||||
{
|
||||
fetch = undefined; // fetch not support file:/// scheme
|
||||
getBinaryPromise = function()
|
||||
//string_utf8
|
||||
|
||||
//module
|
||||
|
||||
/**
|
||||
* Class representing a zip archive creator/reader.
|
||||
* @constructor
|
||||
*/
|
||||
function ZLib()
|
||||
{
|
||||
var wasmPath = "ascdesktop://zlib/" + wasmBinaryFile.substr(8);
|
||||
return new Promise(function (resolve, reject)
|
||||
{
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.open('GET', wasmPath, true);
|
||||
xhr.responseType = 'arraybuffer';
|
||||
|
||||
if (xhr.overrideMimeType)
|
||||
xhr.overrideMimeType('text/plain; charset=x-user-defined');
|
||||
else
|
||||
xhr.setRequestHeader('Accept-Charset', 'x-user-defined');
|
||||
|
||||
xhr.onload = function ()
|
||||
{
|
||||
if (this.status == 200)
|
||||
resolve(new Uint8Array(this.response));
|
||||
};
|
||||
xhr.send(null);
|
||||
});
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
getBinaryPromise = function() { return getBinaryPromise2(); }
|
||||
}
|
||||
|
||||
//module
|
||||
|
||||
var readFromUtf8 = function(buffer, start, len)
|
||||
{
|
||||
var result = "";
|
||||
var index = start;
|
||||
var end = start + len;
|
||||
while (index < end)
|
||||
{
|
||||
var u0 = buffer[index++];
|
||||
if (!(u0 & 128))
|
||||
{
|
||||
result += String.fromCharCode(u0);
|
||||
continue;
|
||||
}
|
||||
var u1 = buffer[index++] & 63;
|
||||
if ((u0 & 224) == 192)
|
||||
{
|
||||
result += String.fromCharCode((u0 & 31) << 6 | u1);
|
||||
continue;
|
||||
}
|
||||
var u2 = buffer[index++] & 63;
|
||||
if ((u0 & 240) == 224)
|
||||
u0 = (u0 & 15) << 12 | u1 << 6 | u2;
|
||||
else
|
||||
u0 = (u0 & 7) << 18 | u1 << 12 | u2 << 6 | buffer[index++] & 63;
|
||||
if (u0 < 65536)
|
||||
result += String.fromCharCode(u0);
|
||||
else
|
||||
{
|
||||
var ch = u0 - 65536;
|
||||
result += String.fromCharCode(55296 | ch >> 10, 56320 | ch & 1023);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
var allocString = function(string)
|
||||
{
|
||||
var inputLen = string.length;
|
||||
var testLen = 6 * inputLen + 1;
|
||||
var tmpStrings = new ArrayBuffer(testLen);
|
||||
|
||||
var code = 0;
|
||||
var index = 0;
|
||||
|
||||
var outputIndex = 0;
|
||||
var outputDataTmp = new Uint8Array(tmpStrings);
|
||||
var outputData = outputDataTmp;
|
||||
|
||||
while (index < inputLen)
|
||||
{
|
||||
code = string.charCodeAt(index++);
|
||||
if (code >= 0xD800 && code <= 0xDFFF && index < inputLen)
|
||||
code = 0x10000 + (((code & 0x3FF) << 10) | (0x03FF & string.charCodeAt(index++)));
|
||||
|
||||
if (code < 0x80)
|
||||
outputData[outputIndex++] = code;
|
||||
else if (code < 0x0800)
|
||||
{
|
||||
outputData[outputIndex++] = 0xC0 | (code >> 6);
|
||||
outputData[outputIndex++] = 0x80 | (code & 0x3F);
|
||||
}
|
||||
else if (code < 0x10000)
|
||||
{
|
||||
outputData[outputIndex++] = 0xE0 | (code >> 12);
|
||||
outputData[outputIndex++] = 0x80 | ((code >> 6) & 0x3F);
|
||||
outputData[outputIndex++] = 0x80 | (code & 0x3F);
|
||||
}
|
||||
else if (code < 0x1FFFFF)
|
||||
{
|
||||
outputData[outputIndex++] = 0xF0 | (code >> 18);
|
||||
outputData[outputIndex++] = 0x80 | ((code >> 12) & 0x3F);
|
||||
outputData[outputIndex++] = 0x80 | ((code >> 6) & 0x3F);
|
||||
outputData[outputIndex++] = 0x80 | (code & 0x3F);
|
||||
}
|
||||
else if (code < 0x3FFFFFF)
|
||||
{
|
||||
outputData[outputIndex++] = 0xF8 | (code >> 24);
|
||||
outputData[outputIndex++] = 0x80 | ((code >> 18) & 0x3F);
|
||||
outputData[outputIndex++] = 0x80 | ((code >> 12) & 0x3F);
|
||||
outputData[outputIndex++] = 0x80 | ((code >> 6) & 0x3F);
|
||||
outputData[outputIndex++] = 0x80 | (code & 0x3F);
|
||||
}
|
||||
else if (code < 0x7FFFFFFF)
|
||||
{
|
||||
outputData[outputIndex++] = 0xFC | (code >> 30);
|
||||
outputData[outputIndex++] = 0x80 | ((code >> 24) & 0x3F);
|
||||
outputData[outputIndex++] = 0x80 | ((code >> 18) & 0x3F);
|
||||
outputData[outputIndex++] = 0x80 | ((code >> 12) & 0x3F);
|
||||
outputData[outputIndex++] = 0x80 | ((code >> 6) & 0x3F);
|
||||
outputData[outputIndex++] = 0x80 | (code & 0x3F);
|
||||
}
|
||||
}
|
||||
|
||||
outputData[outputIndex++] = 0;
|
||||
|
||||
var tmpBuffer = new Uint8Array(tmpStrings, 0, outputIndex);
|
||||
return { length : outputIndex, buf : tmpBuffer };
|
||||
};
|
||||
|
||||
function Zlib()
|
||||
{
|
||||
this.zipFile = null;
|
||||
this.memory = null;
|
||||
this.isInit = false;
|
||||
this.files = [];
|
||||
|
||||
this.GetZip = function()
|
||||
{
|
||||
if (!this.isInit) return null;
|
||||
if (!this.zipFile) return null;
|
||||
this.GetFilesInZip();
|
||||
|
||||
// вычисление длины дерева
|
||||
var _paths = [];
|
||||
var nLength = 4;
|
||||
for (var i = 0; i < this.files.length; i++)
|
||||
{
|
||||
_paths.push(allocString(this.files[i].path));
|
||||
nLength += 4;
|
||||
nLength += _paths[i].length;
|
||||
|
||||
nLength += 4;
|
||||
nLength += this.files[i].length;
|
||||
}
|
||||
// создание дерева файлов
|
||||
var tmpBuffer = new Uint8Array(nLength);
|
||||
tmpBuffer.set([
|
||||
(nLength >> 0) & 0xFF,
|
||||
(nLength >> 8) & 0xFF,
|
||||
(nLength >> 16) & 0xFF,
|
||||
(nLength >> 24) & 0xFF
|
||||
], 0);
|
||||
var index = 4;
|
||||
for (var i = 0; i < this.files.length; i++)
|
||||
{
|
||||
tmpBuffer.set([
|
||||
(_paths[i].length >> 0) & 0xFF,
|
||||
(_paths[i].length >> 8) & 0xFF,
|
||||
(_paths[i].length >> 16) & 0xFF,
|
||||
(_paths[i].length >> 24) & 0xFF
|
||||
], index);
|
||||
index += 4;
|
||||
tmpBuffer.set(_paths[i].buf, index);
|
||||
index += _paths[i].length;
|
||||
|
||||
tmpBuffer.set([
|
||||
(this.files[i].length >> 0) & 0xFF,
|
||||
(this.files[i].length >> 8) & 0xFF,
|
||||
(this.files[i].length >> 16) & 0xFF,
|
||||
(this.files[i].length >> 24) & 0xFF
|
||||
], index);
|
||||
index += 4;
|
||||
tmpBuffer.set(this.files[i].file, index);
|
||||
index += this.files[i].length;
|
||||
}
|
||||
|
||||
var pointer = Module["_Zlib_Malloc"](tmpBuffer.length);
|
||||
if (!pointer) return null;
|
||||
Module["HEAP8"].set(tmpBuffer, pointer);
|
||||
var pointerZip = Module["_Zlib_CompressFiles"](this.zipFile, pointer);
|
||||
if (!pointerZip) return null;
|
||||
var _lenFile = new Int32Array(Module["HEAP8"].buffer, pointerZip, 4);
|
||||
var len = _lenFile[0];
|
||||
var zip = new Uint8Array(Module["HEAP8"].buffer, pointerZip + 4, len);
|
||||
Module["_Zlib_Free"](pointer);
|
||||
return zip;
|
||||
}
|
||||
|
||||
this.CreateZipFromFiles = function(_files)
|
||||
{
|
||||
if (!this.isInit) return null;
|
||||
if (this.zipFile) this.CloseZip();
|
||||
this.zipFile = Module["_Zlib_Create"]();
|
||||
for (var i = 0; i < _files.length; i++)
|
||||
this.files.push({
|
||||
path : _files[i].path,
|
||||
length : _files[i].length,
|
||||
file : _files[i].file
|
||||
});
|
||||
this.engine = 0; // указатель на нативный класс Zlib
|
||||
this.files = {};
|
||||
}
|
||||
|
||||
this.AddFileInZip = function(_file)
|
||||
{
|
||||
if (!this.isInit) return false;
|
||||
if (!this.zipFile) return false;
|
||||
if (!this.files.length) this.GetFilesInZip();
|
||||
else if (!this.files[0].length) this.GetFilesInZip();
|
||||
|
||||
var findFile = this.files.find(o => o.path == _file.path);
|
||||
if (findFile && findFile.length != _file.length)
|
||||
{
|
||||
this.DeleteFileInZip(findFile.path)
|
||||
this.files.push(_file);
|
||||
}
|
||||
else if (!findFile)
|
||||
this.files.push(_file);
|
||||
return true;
|
||||
}
|
||||
|
||||
this.DeleteFileInZip = function(_path)
|
||||
{
|
||||
if (!this.isInit) return false;
|
||||
if (!this.zipFile) return false;
|
||||
if (!this.files.length) this.GetFilesInZip();
|
||||
else if (!this.files[0].length) this.GetFilesInZip();
|
||||
|
||||
for (var i = 0; i < this.files.length; i++)
|
||||
{
|
||||
if (this.files[i].path == _path)
|
||||
{
|
||||
this.files.splice(i, 1);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
/**
|
||||
* Check loaded wasm/asmjs module
|
||||
*/
|
||||
ZLib.prototype.isModuleInit = false;
|
||||
|
||||
this.GetPathsInZip = function()
|
||||
{
|
||||
if (!this.isInit || !this.zipFile) return null;
|
||||
if (this.files.length)
|
||||
/**
|
||||
* Open archive from bytes
|
||||
* @param {Uint8Array | ArrayBuffer} buf
|
||||
* @returns {boolean} success or not
|
||||
*/
|
||||
ZLib.prototype.open = function(buf)
|
||||
{
|
||||
if (!this.isModuleInit)
|
||||
return false;
|
||||
|
||||
if (this.engine)
|
||||
this.close();
|
||||
|
||||
if (!buf)
|
||||
return false;
|
||||
|
||||
var arrayBuffer = (undefined !== buf.byteLength) ? new Uint8Array(buf) : buf;
|
||||
|
||||
// TODO: открыли архив, и заполнили this.files
|
||||
// объектами { path : null }
|
||||
|
||||
// копируем память в память webasm
|
||||
var FileRawDataSize = arrayBuffer.length;
|
||||
var FileRawData = Module["_Zlib_Malloc"](FileRawDataSize);
|
||||
if (0 == FileRawData)
|
||||
return false;
|
||||
Module["HEAP8"].set(arrayBuffer, FileRawData);
|
||||
|
||||
// грузим данные
|
||||
this.engine = Module["_Zlib_Open"](FileRawData, FileRawDataSize);
|
||||
if (0 == this.engine)
|
||||
{
|
||||
var _paths = [];
|
||||
for (var i = 0; i < this.files.length; i++)
|
||||
_paths.push(this.files[i].path);
|
||||
return _paths;
|
||||
Module["_Zlib_Free"](FileRawData);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
// получаем пути в архиве
|
||||
var pointer = Module["_Zlib_GetPathsInArchive"](this.zipFile);
|
||||
if (!pointer)
|
||||
{
|
||||
Module["_Zlib_Destroy"](this.zipFile);
|
||||
Module["_Zlib_Free"](this.memory);
|
||||
return null;
|
||||
}
|
||||
var lenArray = new Int32Array(Module["HEAP8"].buffer, pointer, 4);
|
||||
var len = lenArray[0];
|
||||
len -= 4;
|
||||
|
||||
var buffer = new Uint8Array(Module["HEAP8"].buffer, pointer + 4, len);
|
||||
var index = 0;
|
||||
var _paths = [];
|
||||
while (index < len)
|
||||
{
|
||||
var lenRec = buffer[index] | buffer[index + 1] << 8 | buffer[index + 2] << 16 | buffer[index + 3] << 24;
|
||||
index += 4;
|
||||
var _path = readFromUtf8(buffer, index, lenRec);
|
||||
var findPath = this.files.find(o => o.path === _path);
|
||||
if (!findPath)
|
||||
{
|
||||
this.files.push({
|
||||
path : _path,
|
||||
length : 0,
|
||||
file : null
|
||||
});
|
||||
_paths.push(_path);
|
||||
}
|
||||
var pointer = Module["_Zlib_GetPaths"](this.engine);
|
||||
if (0 == pointer)
|
||||
{
|
||||
Module["_Zlib_Close"](this.engine);
|
||||
Module["_Zlib_Free"](FileRawData);
|
||||
return false;
|
||||
}
|
||||
var lenArray = new Int32Array(Module["HEAP8"].buffer, pointer, 4);
|
||||
var len = lenArray[0];
|
||||
len -= 4;
|
||||
|
||||
var buffer = new Uint8Array(Module["HEAP8"].buffer, pointer + 4, len);
|
||||
var index = 0;
|
||||
while (index < len)
|
||||
{
|
||||
var lenRec = buffer[index] | buffer[index + 1] << 8 | buffer[index + 2] << 16 | buffer[index + 3] << 24;
|
||||
index += 4;
|
||||
var _path = "".fromUtf8(buffer, index, lenRec);
|
||||
this.files[_path] = null;
|
||||
index += lenRec;
|
||||
}
|
||||
return _paths;
|
||||
}
|
||||
|
||||
this.GetFilesInZip = function()
|
||||
}
|
||||
Module["_Zlib_Free"](FileRawData);
|
||||
Module["_Zlib_Free"](pointer);
|
||||
return true;
|
||||
};
|
||||
|
||||
/**
|
||||
* Create new archive
|
||||
* @returns {boolean} success or not
|
||||
*/
|
||||
ZLib.prototype.create = function()
|
||||
{
|
||||
if (!this.isInit) return [];
|
||||
if (!this.zipFile) return [];
|
||||
if (!this.isModuleInit)
|
||||
return false;
|
||||
|
||||
if (this.engine)
|
||||
this.close();
|
||||
|
||||
this.engine = Module["_Zlib_Create"]();
|
||||
return !!this.engine;
|
||||
};
|
||||
|
||||
/**
|
||||
* Save archive from current files
|
||||
* @returns {Uint8Array | null} zip-archive bytes, or null if error
|
||||
*/
|
||||
ZLib.prototype.save = function()
|
||||
{
|
||||
if (!this.isModuleInit || !this.engine)
|
||||
return null;
|
||||
|
||||
var pointerZip = Module["_Zlib_Save"](this.engine);
|
||||
if (0 == pointerZip)
|
||||
return null;
|
||||
|
||||
var _lenFile = new Int32Array(Module["HEAP8"].buffer, pointerZip, 4);
|
||||
var len = _lenFile[0];
|
||||
var zip = new Uint8Array(Module["HEAP8"].buffer, pointerZip + 4, len);
|
||||
return zip;
|
||||
};
|
||||
|
||||
/**
|
||||
* Get all file paths in archive
|
||||
* @returns {Array}
|
||||
*/
|
||||
ZLib.prototype.getPaths = function()
|
||||
{
|
||||
var retFiles = [];
|
||||
if (!this.files)
|
||||
return retFiles;
|
||||
|
||||
for (var path in this.files)
|
||||
{
|
||||
if (this.files.hasOwnProperty(path))
|
||||
retFiles.push(path);
|
||||
}
|
||||
return retFiles;
|
||||
};
|
||||
|
||||
/**
|
||||
* Get uncomressed file from archive
|
||||
* @param {string} path
|
||||
* @returns {Uint8Array | null} bytes of uncompressed data, or null if error
|
||||
*/
|
||||
ZLib.prototype.getFile = function(path)
|
||||
{
|
||||
if (!this.isModuleInit || !this.engine)
|
||||
return null;
|
||||
|
||||
// проверяем - есть ли файл вообще?
|
||||
if (undefined === this.files[path])
|
||||
return null;
|
||||
|
||||
// проверяем - может мы уже его разжимали?
|
||||
if (null !== this.files[path])
|
||||
{
|
||||
if (this.files[path].l > 0)
|
||||
{
|
||||
return new Uint8Array(Module["HEAP8"].buffer, this.files[path].p, this.files[path].l);
|
||||
}
|
||||
else
|
||||
{
|
||||
var _lenFile = new Int32Array(Module["HEAP8"].buffer, this.files[path].p, 4);
|
||||
var len = _lenFile[0];
|
||||
return new Uint8Array(Module["HEAP8"].buffer, this.files[path].p + 4, len);
|
||||
}
|
||||
}
|
||||
|
||||
var tmp = path.toUtf8();
|
||||
var pointer = Module["_Zlib_Malloc"](tmp.length);
|
||||
if (0 == pointer)
|
||||
return null;
|
||||
Module["HEAP8"].set(tmp, pointer);
|
||||
|
||||
var pointerFile = Module["_Zlib_GetFile"](this.engine, pointer);
|
||||
if (0 == pointerFile)
|
||||
{
|
||||
Module["_Zlib_Free"](pointer);
|
||||
return null;
|
||||
}
|
||||
|
||||
var _lenFile = new Int32Array(Module["HEAP8"].buffer, pointerFile, 4);
|
||||
var len = _lenFile[0];
|
||||
|
||||
Module["_Zlib_Free"](pointer);
|
||||
this.files[path] = { p : pointerFile, l : 0};
|
||||
return new Uint8Array(Module["HEAP8"].buffer, pointerFile + 4, len);
|
||||
};
|
||||
|
||||
/**
|
||||
* Add uncomressed file to archive
|
||||
* @param {string} path
|
||||
* @param {Uint8Array} new file in archive
|
||||
* @returns {boolean} success or not
|
||||
*/
|
||||
ZLib.prototype.addFile = function(path, data)
|
||||
{
|
||||
if (!this.isModuleInit || !this.engine)
|
||||
return false;
|
||||
|
||||
if (!data)
|
||||
return false;
|
||||
|
||||
// проверяем - может такой файл уже есть? тогда его надо сначала удалить?
|
||||
if (undefined !== this.files[path])
|
||||
this.removeFile(path);
|
||||
|
||||
var tmp = path.toUtf8();
|
||||
var pointer = Module["_Zlib_Malloc"](tmp.length);
|
||||
if (0 == pointer)
|
||||
return false;
|
||||
Module["HEAP8"].set(tmp, pointer);
|
||||
|
||||
var arrayBuffer = (undefined !== data.byteLength) ? new Uint8Array(data) : data;
|
||||
|
||||
var FileRawDataSize = arrayBuffer.length;
|
||||
var FileRawData = Module["_Zlib_Malloc"](FileRawDataSize);
|
||||
if (0 == FileRawData)
|
||||
{
|
||||
Module["_Zlib_Free"](pointer);
|
||||
return false;
|
||||
}
|
||||
Module["HEAP8"].set(arrayBuffer, FileRawData);
|
||||
|
||||
var _paths = this.GetPathsInZip();
|
||||
var _files = [];
|
||||
for (var i = 0; i < _paths.length; i++)
|
||||
_files.push(this.GetFileInZip(_paths[i]));
|
||||
return _files;
|
||||
}
|
||||
|
||||
this.GetFileInZip = function(_path)
|
||||
{
|
||||
if (!this.isInit) return null;
|
||||
if (!this.zipFile) return null;
|
||||
|
||||
var findFile = this.files.find(o => o.path === _path);
|
||||
if (findFile && findFile.file) return findFile;
|
||||
|
||||
var tmp = allocString(_path);
|
||||
var pointer = Module["_Zlib_Malloc"](tmp.length);
|
||||
if (!pointer) return null;
|
||||
Module["HEAP8"].set(tmp.buf, pointer);
|
||||
|
||||
var pointerFile = Module["_Zlib_GetFileFromArchive"](this.zipFile, pointer);
|
||||
if (!pointerFile)
|
||||
{
|
||||
Module["_Zlib_Free"](pointer);
|
||||
return null;
|
||||
}
|
||||
|
||||
var _lenFile = new Int32Array(Module["HEAP8"].buffer, pointerFile, 4);
|
||||
var len = _lenFile[0];
|
||||
|
||||
var buffer = new Uint8Array(Module["HEAP8"].buffer, pointerFile + 4, len);
|
||||
if (findFile)
|
||||
Module["_Zlib_AddFile"](this.engine, pointer, FileRawData, FileRawDataSize);
|
||||
|
||||
this.files[path] = { p : FileRawData, l : FileRawDataSize};
|
||||
Module["_Zlib_Free"](pointer);
|
||||
return true;
|
||||
};
|
||||
|
||||
/**
|
||||
* Remove file from archive
|
||||
* @param {string} path
|
||||
* @returns {boolean} success or not
|
||||
*/
|
||||
ZLib.prototype.removeFile = function(path)
|
||||
{
|
||||
if (!this.isModuleInit || !this.engine)
|
||||
return false;
|
||||
|
||||
// проверяем - может такого файла и нет?
|
||||
if (undefined === this.files[path])
|
||||
return false;
|
||||
|
||||
var tmp = path.toUtf8();
|
||||
var pointer = Module["_Zlib_Malloc"](tmp.length);
|
||||
if (0 == pointer)
|
||||
return false;
|
||||
Module["HEAP8"].set(tmp, pointer);
|
||||
|
||||
Module["_Zlib_RemoveFile"](this.engine, pointer);
|
||||
|
||||
if (this.files[path] && this.files[path].p)
|
||||
{
|
||||
findFile.length = len;
|
||||
findFile.file = buffer;
|
||||
findFile = {
|
||||
path : _path,
|
||||
length : len,
|
||||
file : buffer
|
||||
};
|
||||
Module["_Zlib_Free"](this.files[path].p);
|
||||
delete this.files[path];
|
||||
}
|
||||
else
|
||||
Module["_Zlib_Free"](pointer);
|
||||
return true;
|
||||
};
|
||||
|
||||
/**
|
||||
* Close & remove all used memory in archive
|
||||
* @returns {undefined}
|
||||
*/
|
||||
ZLib.prototype.close = function()
|
||||
{
|
||||
if (!this.isModuleInit || !this.engine)
|
||||
return;
|
||||
|
||||
for (var i in this.files)
|
||||
{
|
||||
findFile = {
|
||||
path : _path,
|
||||
length : len,
|
||||
file : buffer
|
||||
};
|
||||
this.files.push(findFile);
|
||||
if (this.files[i] && this.files[i].p)
|
||||
Module["_Zlib_Free"](this.files[i].p);
|
||||
}
|
||||
|
||||
Module["_Zlib_Free"](pointer);
|
||||
return findFile;
|
||||
}
|
||||
|
||||
this.OpenZipFromBuffer = function(dataBuffer)
|
||||
{
|
||||
var uint8DataBuffer = new Uint8Array(dataBuffer);
|
||||
return this.OpenZipFromUint8Array(uint8DataBuffer);
|
||||
}
|
||||
|
||||
this.OpenZipFromUint8Array = function(dataBuffer)
|
||||
{
|
||||
if (!this.isInit) return null;
|
||||
if (this.zipFile) this.CloseZip();
|
||||
this.files = {};
|
||||
if (this.engine)
|
||||
Module["_Zlib_Free"](this.engine);
|
||||
this.engine = 0;
|
||||
};
|
||||
|
||||
// копируем память в память webasm
|
||||
var FileRawDataSize = dataBuffer.length;
|
||||
this.memory = Module["_Zlib_Malloc"](FileRawDataSize);
|
||||
if (!this.memory) return null;
|
||||
Module["HEAP8"].set(dataBuffer, this.memory);
|
||||
|
||||
// грузим данные
|
||||
this.zipFile = Module["_Zlib_Load"](this.memory, FileRawDataSize);
|
||||
if (!this.zipFile)
|
||||
{
|
||||
Module["_Zlib_Free"](this.memory);
|
||||
return null;
|
||||
}
|
||||
this.GetPathsInZip();
|
||||
return this;
|
||||
}
|
||||
|
||||
this.CloseZip = function()
|
||||
{
|
||||
if (!this.isInit) return;
|
||||
if (this.zipFile) Module["_Zlib_Destroy"](this.zipFile);
|
||||
if (this.memory) Module["_Zlib_Free"](this.memory);
|
||||
this.zipFile = null;
|
||||
this.memory = null;
|
||||
this.files = [];
|
||||
}
|
||||
}
|
||||
|
||||
window.nativeZlibEngine = new Zlib();
|
||||
window.onEngineInit = function()
|
||||
window.nativeZlibEngine = new ZLib();
|
||||
window.onZlibEngineInit = function()
|
||||
{
|
||||
window.nativeZlibEngine.isInit = true;
|
||||
window.nativeZlibEngine.isModuleInit = true;
|
||||
window["ZLibModule_onLoad"] && window["ZLibModule_onLoad"]();
|
||||
};
|
||||
|
||||
})(window, undefined);
|
||||
|
||||
|
||||
@ -1,272 +0,0 @@
|
||||
(function(window, undefined){
|
||||
|
||||
//desktop_fetch
|
||||
|
||||
//string_utf8
|
||||
|
||||
//module
|
||||
|
||||
/**
|
||||
* Class representing a zip archive creator/reader.
|
||||
* @constructor
|
||||
*/
|
||||
function ZLib()
|
||||
{
|
||||
this.engine = 0; // указатель на нативный класс Zlib
|
||||
this.files = {};
|
||||
}
|
||||
|
||||
/**
|
||||
* Check loaded wasm/asmjs module
|
||||
*/
|
||||
ZLib.prototype.isModuleInit = false;
|
||||
|
||||
/**
|
||||
* Open archive from bytes
|
||||
* @param {Uint8Array | ArrayBuffer} buf
|
||||
* @returns {boolean} success or not
|
||||
*/
|
||||
ZLib.prototype.open = function(buf)
|
||||
{
|
||||
if (!this.isModuleInit)
|
||||
return false;
|
||||
|
||||
if (this.engine)
|
||||
this.close();
|
||||
|
||||
if (!buf)
|
||||
return false;
|
||||
|
||||
var arrayBuffer = (undefined !== buf.byteLength) ? new Uint8Array(buf) : buf;
|
||||
|
||||
// TODO: открыли архив, и заполнили this.files
|
||||
// объектами { path : null }
|
||||
|
||||
// копируем память в память webasm
|
||||
var FileRawDataSize = arrayBuffer.length;
|
||||
var FileRawData = Module["_Zlib_Malloc"](FileRawDataSize);
|
||||
if (0 == FileRawData)
|
||||
return false;
|
||||
Module["HEAP8"].set(arrayBuffer, FileRawData);
|
||||
|
||||
// грузим данные
|
||||
this.engine = Module["_Zlib_Open"](FileRawData, FileRawDataSize);
|
||||
if (0 == this.engine)
|
||||
{
|
||||
Module["_Zlib_Free"](FileRawData);
|
||||
return false;
|
||||
}
|
||||
|
||||
// получаем пути в архиве
|
||||
var pointer = Module["_Zlib_GetPaths"](this.engine);
|
||||
if (0 == pointer)
|
||||
{
|
||||
Module["_Zlib_Close"](this.engine);
|
||||
Module["_Zlib_Free"](FileRawData);
|
||||
return false;
|
||||
}
|
||||
var lenArray = new Int32Array(Module["HEAP8"].buffer, pointer, 4);
|
||||
var len = lenArray[0];
|
||||
len -= 4;
|
||||
|
||||
var buffer = new Uint8Array(Module["HEAP8"].buffer, pointer + 4, len);
|
||||
var index = 0;
|
||||
while (index < len)
|
||||
{
|
||||
var lenRec = buffer[index] | buffer[index + 1] << 8 | buffer[index + 2] << 16 | buffer[index + 3] << 24;
|
||||
index += 4;
|
||||
var _path = "".fromUtf8(buffer, index, lenRec);
|
||||
this.files[_path] = null;
|
||||
index += lenRec;
|
||||
}
|
||||
Module["_Zlib_Free"](FileRawData);
|
||||
Module["_Zlib_Free"](pointer);
|
||||
return true;
|
||||
};
|
||||
|
||||
/**
|
||||
* Create new archive
|
||||
* @returns {boolean} success or not
|
||||
*/
|
||||
ZLib.prototype.create = function()
|
||||
{
|
||||
if (!this.isModuleInit)
|
||||
return false;
|
||||
|
||||
if (this.engine)
|
||||
this.close();
|
||||
|
||||
this.engine = Module["_Zlib_Create"]();
|
||||
return !!this.engine;
|
||||
};
|
||||
|
||||
/**
|
||||
* Save archive from current files
|
||||
* @returns {Uint8Array | null} zip-archive bytes, or null if error
|
||||
*/
|
||||
ZLib.prototype.save = function()
|
||||
{
|
||||
if (!this.isModuleInit || !this.engine)
|
||||
return null;
|
||||
|
||||
var pointerZip = Module["_Zlib_Save"](this.engine);
|
||||
if (0 == pointerZip)
|
||||
return null;
|
||||
|
||||
var _lenFile = new Int32Array(Module["HEAP8"].buffer, pointerZip, 4);
|
||||
var len = _lenFile[0];
|
||||
var zip = new Uint8Array(Module["HEAP8"].buffer, pointerZip + 4, len);
|
||||
return zip;
|
||||
};
|
||||
|
||||
/**
|
||||
* Get uncomressed file from archive
|
||||
* @param {string} path
|
||||
* @returns {Uint8Array | null} bytes of uncompressed data, or null if error
|
||||
*/
|
||||
ZLib.prototype.getFile = function(path)
|
||||
{
|
||||
if (!this.isModuleInit || !this.engine)
|
||||
return null;
|
||||
|
||||
// проверяем - есть ли файл вообще?
|
||||
if (undefined === this.files[path])
|
||||
return null;
|
||||
|
||||
// проверяем - может мы уже его разжимали?
|
||||
if (null !== this.files[path])
|
||||
{
|
||||
if (this.files[path].l > 0)
|
||||
{
|
||||
return new Uint8Array(Module["HEAP8"].buffer, this.files[path].p, this.files[path].l);
|
||||
}
|
||||
else
|
||||
{
|
||||
var _lenFile = new Int32Array(Module["HEAP8"].buffer, this.files[path].p, 4);
|
||||
var len = _lenFile[0];
|
||||
return new Uint8Array(Module["HEAP8"].buffer, this.files[path].p + 4, len);
|
||||
}
|
||||
}
|
||||
|
||||
var tmp = path.toUtf8();
|
||||
var pointer = Module["_Zlib_Malloc"](tmp.length);
|
||||
if (0 == pointer)
|
||||
return null;
|
||||
Module["HEAP8"].set(tmp, pointer);
|
||||
|
||||
var pointerFile = Module["_Zlib_GetFile"](this.engine, pointer);
|
||||
if (0 == pointerFile)
|
||||
{
|
||||
Module["_Zlib_Free"](pointer);
|
||||
return null;
|
||||
}
|
||||
|
||||
var _lenFile = new Int32Array(Module["HEAP8"].buffer, pointerFile, 4);
|
||||
var len = _lenFile[0];
|
||||
|
||||
Module["_Zlib_Free"](pointer);
|
||||
this.files[path] = { p : pointerFile, l : 0};
|
||||
return new Uint8Array(Module["HEAP8"].buffer, pointerFile + 4, len);
|
||||
};
|
||||
|
||||
/**
|
||||
* Add uncomressed file to archive
|
||||
* @param {string} path
|
||||
* @param {Uint8Array} new file in archive
|
||||
* @returns {boolean} success or not
|
||||
*/
|
||||
ZLib.prototype.addFile = function(path, data)
|
||||
{
|
||||
if (!this.isModuleInit || !this.engine)
|
||||
return false;
|
||||
|
||||
if (!data)
|
||||
return false;
|
||||
|
||||
// проверяем - может такой файл уже есть? тогда его надо сначала удалить?
|
||||
if (undefined !== this.files[path])
|
||||
this.removeFile(path);
|
||||
|
||||
var tmp = path.toUtf8();
|
||||
var pointer = Module["_Zlib_Malloc"](tmp.length);
|
||||
if (0 == pointer)
|
||||
return false;
|
||||
Module["HEAP8"].set(tmp, pointer);
|
||||
|
||||
var arrayBuffer = (undefined !== data.byteLength) ? new Uint8Array(data) : data;
|
||||
|
||||
var FileRawDataSize = arrayBuffer.length;
|
||||
var FileRawData = Module["_Zlib_Malloc"](FileRawDataSize);
|
||||
if (0 == FileRawData)
|
||||
{
|
||||
Module["_Zlib_Free"](pointer);
|
||||
return false;
|
||||
}
|
||||
Module["HEAP8"].set(arrayBuffer, FileRawData);
|
||||
|
||||
Module["_Zlib_AddFile"](this.engine, pointer, FileRawData, FileRawDataSize);
|
||||
|
||||
this.files[path] = { p : FileRawData, l : FileRawDataSize};
|
||||
Module["_Zlib_Free"](pointer);
|
||||
return true;
|
||||
};
|
||||
|
||||
/**
|
||||
* Remove file from archive
|
||||
* @param {string} path
|
||||
* @returns {boolean} success or not
|
||||
*/
|
||||
ZLib.prototype.removeFile = function(path)
|
||||
{
|
||||
if (!this.isModuleInit || !this.engine)
|
||||
return false;
|
||||
|
||||
// проверяем - может такого файла и нет?
|
||||
if (undefined === this.files[path])
|
||||
return false;
|
||||
|
||||
var tmp = path.toUtf8();
|
||||
var pointer = Module["_Zlib_Malloc"](tmp.length);
|
||||
if (0 == pointer)
|
||||
return false;
|
||||
Module["HEAP8"].set(tmp, pointer);
|
||||
|
||||
Module["_Zlib_RemoveFile"](this.engine, pointer);
|
||||
|
||||
if (this.files[path] && this.files[path].p)
|
||||
{
|
||||
Module["_Zlib_Free"](this.files[path].p);
|
||||
delete this.files[path];
|
||||
}
|
||||
Module["_Zlib_Free"](pointer);
|
||||
return true;
|
||||
};
|
||||
|
||||
/**
|
||||
* Close & remove all used memory in archive
|
||||
* @returns {undefined}
|
||||
*/
|
||||
ZLib.prototype.close = function()
|
||||
{
|
||||
if (!this.isModuleInit || !this.engine)
|
||||
return;
|
||||
|
||||
for (var i in this.files)
|
||||
{
|
||||
if (this.files[i] && this.files[i].p)
|
||||
Module["_Zlib_Free"](this.files[i].p);
|
||||
}
|
||||
this.files = {};
|
||||
if (this.engine)
|
||||
Module["_Zlib_Free"](this.engine);
|
||||
this.engine = 0;
|
||||
};
|
||||
|
||||
window.nativeZlibEngine = new ZLib();
|
||||
window.onEngineInit = function()
|
||||
{
|
||||
window.nativeZlibEngine.isModuleInit = true;
|
||||
};
|
||||
|
||||
})(window, undefined);
|
||||
|
||||
@ -2,10 +2,10 @@
|
||||
"name": "zlib",
|
||||
"res_folder": "./deploy",
|
||||
"wasm": true,
|
||||
"asm": false,
|
||||
"asm": true,
|
||||
"run_before": null,
|
||||
"run_after": "import sys;sys.path.append(\"../../../build_tools/scripts\");import base;base.configure_common_apps();base.replaceInFile(\"./deploy/zlib.js\", \"__ATPOSTRUN__=[];\", \"__ATPOSTRUN__=[function(){self.onEngineInit();}];\");base.replaceInFile(\"./deploy/zlib.js\", \"function getBinaryPromise(){\", \"function getBinaryPromise2(){\");base.copy_file(\"./wasm/js/index.html\", \"./deploy/index.html\");base.copy_file(\"./wasm/js/code.js\", \"./deploy/code.js\")",
|
||||
"base_js_content": "./wasm/js/zlib_new.js",
|
||||
"run_after": "import sys;sys.path.append(\"../../../build_tools/scripts\");import base;base.configure_common_apps();base.replaceInFile(\"./deploy/zlib.js\", \"__ATPOSTRUN__=[];\", \"__ATPOSTRUN__=[function(){self.onZlibEngineInit();}];\");base.replaceInFile(\"./deploy/zlib.js\", \"function getBinaryPromise(){\", \"function getBinaryPromise2(){\");base.replaceInFile(\"./deploy/zlib_ie.js\", \"__ATPOSTRUN__=[];\", \"__ATPOSTRUN__=[function(){self.onZlibEngineInit();}];\");base.replaceInFile(\"./deploy/zlib_ie.js\", \"function getBinaryPromise(){\", \"function getBinaryPromise2(){\");base.copy_file(\"./wasm/js/index.html\", \"./deploy/index.html\");base.copy_file(\"./wasm/js/code.js\", \"./deploy/code.js\")",
|
||||
"base_js_content": "./wasm/js/zlib.js",
|
||||
|
||||
"compiler_flags": [
|
||||
"-O3",
|
||||
@ -13,6 +13,7 @@
|
||||
"-Wno-unused-command-line-argument",
|
||||
"-s ALLOW_MEMORY_GROWTH=1",
|
||||
"-s FILESYSTEM=0",
|
||||
"--memory-init-file 0",
|
||||
"-s ENVIRONMENT='web,worker'"
|
||||
],
|
||||
"exported_functions": [
|
||||
|
||||
@ -369,6 +369,10 @@ protected:
|
||||
|
||||
public:
|
||||
// Открывает архив, переданные данные необходимо освободить после использования класса
|
||||
CZipFolderMemory()
|
||||
{
|
||||
m_zlib = new CZipBuffer();
|
||||
}
|
||||
CZipFolderMemory(BYTE* data, DWORD length)
|
||||
{
|
||||
m_zlib = new CZipBuffer(data, length);
|
||||
|
||||
Reference in New Issue
Block a user