forked from emscripten-core/emscripten
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibrary_lz4.js
215 lines (213 loc) · 8.18 KB
/
library_lz4.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/**
* @license
* Copyright 2015 The Emscripten Authors
* SPDX-License-Identifier: MIT
*/
#if LZ4
mergeInto(LibraryManager.library, {
$LZ4__deps: ['$FS'],
$LZ4: {
DIR_MODE: {{{ cDefine('S_IFDIR') }}} | 511 /* 0777 */,
FILE_MODE: {{{ cDefine('S_IFREG') }}} | 511 /* 0777 */,
CHUNK_SIZE: -1,
codec: null,
init: function() {
if (LZ4.codec) return;
LZ4.codec = (function() {
{{{ read('../third_party/mini-lz4.js') }}};
return MiniLZ4;
})();
LZ4.CHUNK_SIZE = LZ4.codec.CHUNK_SIZE;
},
loadPackage: function (pack, preloadPlugin) {
LZ4.init();
var compressedData = pack['compressedData'];
if (!compressedData) compressedData = LZ4.codec.compressPackage(pack['data']);
assert(compressedData['cachedIndexes'].length === compressedData['cachedChunks'].length);
for (var i = 0; i < compressedData['cachedIndexes'].length; i++) {
compressedData['cachedIndexes'][i] = -1;
compressedData['cachedChunks'][i] = compressedData['data'].subarray(compressedData['cachedOffset'] + i*LZ4.CHUNK_SIZE,
compressedData['cachedOffset'] + (i+1)*LZ4.CHUNK_SIZE);
assert(compressedData['cachedChunks'][i].length === LZ4.CHUNK_SIZE);
}
pack['metadata'].files.forEach(function(file) {
var dir = PATH.dirname(file.filename);
var name = PATH.basename(file.filename);
FS.createPath('', dir, true, true);
var parent = FS.analyzePath(dir).object;
LZ4.createNode(parent, name, LZ4.FILE_MODE, 0, {
compressedData: compressedData,
start: file.start,
end: file.end,
});
});
// Preload files if necessary. This code is largely similar to
// createPreloadedFile in library_fs.js. However, a main difference here
// is that we only decompress the file if it can be preloaded.
// Abstracting out the common parts seems to be more effort than it is
// worth.
if (preloadPlugin) {
Browser.init();
pack['metadata'].files.forEach(function(file) {
var handled = false;
var fullname = file.filename;
Module['preloadPlugins'].forEach(function(plugin) {
if (handled) return;
if (plugin['canHandle'](fullname)) {
var dep = getUniqueRunDependency('fp ' + fullname);
addRunDependency(dep);
var finish = function() {
removeRunDependency(dep);
}
var byteArray = FS.readFile(fullname);
plugin['handle'](byteArray, fullname, finish, finish);
handled = true;
}
});
});
}
},
createNode: function (parent, name, mode, dev, contents, mtime) {
var node = FS.createNode(parent, name, mode);
node.mode = mode;
node.node_ops = LZ4.node_ops;
node.stream_ops = LZ4.stream_ops;
node.timestamp = (mtime || new Date).getTime();
assert(LZ4.FILE_MODE !== LZ4.DIR_MODE);
if (mode === LZ4.FILE_MODE) {
node.size = contents.end - contents.start;
node.contents = contents;
} else {
node.size = 4096;
node.contents = {};
}
if (parent) {
parent.contents[name] = node;
}
return node;
},
node_ops: {
getattr: function(node) {
return {
dev: 1,
ino: node.id,
mode: node.mode,
nlink: 1,
uid: 0,
gid: 0,
rdev: undefined,
size: node.size,
atime: new Date(node.timestamp),
mtime: new Date(node.timestamp),
ctime: new Date(node.timestamp),
blksize: 4096,
blocks: Math.ceil(node.size / 4096),
};
},
setattr: function(node, attr) {
if (attr.mode !== undefined) {
node.mode = attr.mode;
}
if (attr.timestamp !== undefined) {
node.timestamp = attr.timestamp;
}
},
lookup: function(parent, name) {
throw new FS.ErrnoError({{{ cDefine('ENOENT') }}});
},
mknod: function (parent, name, mode, dev) {
throw new FS.ErrnoError({{{ cDefine('EPERM') }}});
},
rename: function (oldNode, newDir, newName) {
throw new FS.ErrnoError({{{ cDefine('EPERM') }}});
},
unlink: function(parent, name) {
throw new FS.ErrnoError({{{ cDefine('EPERM') }}});
},
rmdir: function(parent, name) {
throw new FS.ErrnoError({{{ cDefine('EPERM') }}});
},
readdir: function(node) {
throw new FS.ErrnoError({{{ cDefine('EPERM') }}});
},
symlink: function(parent, newName, oldPath) {
throw new FS.ErrnoError({{{ cDefine('EPERM') }}});
},
readlink: function(node) {
throw new FS.ErrnoError({{{ cDefine('EPERM') }}});
},
},
stream_ops: {
read: function (stream, buffer, offset, length, position) {
//out('LZ4 read ' + [offset, length, position]);
length = Math.min(length, stream.node.size - position);
if (length <= 0) return 0;
var contents = stream.node.contents;
var compressedData = contents.compressedData;
var written = 0;
while (written < length) {
var start = contents.start + position + written; // start index in uncompressed data
var desired = length - written;
//out('current read: ' + ['start', start, 'desired', desired]);
var chunkIndex = Math.floor(start / LZ4.CHUNK_SIZE);
var compressedStart = compressedData['offsets'][chunkIndex];
var compressedSize = compressedData['sizes'][chunkIndex];
var currChunk;
if (compressedData['successes'][chunkIndex]) {
var found = compressedData['cachedIndexes'].indexOf(chunkIndex);
if (found >= 0) {
currChunk = compressedData['cachedChunks'][found];
} else {
// decompress the chunk
compressedData['cachedIndexes'].pop();
compressedData['cachedIndexes'].unshift(chunkIndex);
currChunk = compressedData['cachedChunks'].pop();
compressedData['cachedChunks'].unshift(currChunk);
if (compressedData['debug']) {
out('decompressing chunk ' + chunkIndex);
Module['decompressedChunks'] = (Module['decompressedChunks'] || 0) + 1;
}
var compressed = compressedData['data'].subarray(compressedStart, compressedStart + compressedSize);
//var t = Date.now();
var originalSize = LZ4.codec.uncompress(compressed, currChunk);
//out('decompress time: ' + (Date.now() - t));
if (chunkIndex < compressedData['successes'].length-1) assert(originalSize === LZ4.CHUNK_SIZE); // all but the last chunk must be full-size
}
} else {
// uncompressed
currChunk = compressedData['data'].subarray(compressedStart, compressedStart + LZ4.CHUNK_SIZE);
}
var startInChunk = start % LZ4.CHUNK_SIZE;
var endInChunk = Math.min(startInChunk + desired, LZ4.CHUNK_SIZE);
buffer.set(currChunk.subarray(startInChunk, endInChunk), offset + written);
var currWritten = endInChunk - startInChunk;
written += currWritten;
}
return written;
},
write: function (stream, buffer, offset, length, position) {
throw new FS.ErrnoError({{{ cDefine('EIO') }}});
},
llseek: function (stream, offset, whence) {
var position = offset;
if (whence === {{{ cDefine('SEEK_CUR') }}}) {
position += stream.position;
} else if (whence === {{{ cDefine('SEEK_END') }}}) {
if (FS.isFile(stream.node.mode)) {
position += stream.node.size;
}
}
if (position < 0) {
throw new FS.ErrnoError({{{ cDefine('EINVAL') }}});
}
return position;
},
},
},
});
if (LibraryManager.library['$FS__deps']) {
LibraryManager.library['$FS__deps'].push('$LZ4'); // LZ4=1, so auto-include us
} else {
warn('FS does not seem to be in use (no preloaded files etc.), LZ4 will not do anything');
}
#endif