forked from emscripten-core/emscripten
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibrary_lz4.js
More file actions
181 lines (180 loc) · 6.82 KB
/
library_lz4.js
File metadata and controls
181 lines (180 loc) · 6.82 KB
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
#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('mini-lz4.js') }}};
return MiniLZ4;
})();
LZ4.CHUNK_SIZE = LZ4.codec.CHUNK_SIZE;
},
loadPackage: function (pack) {
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);
}
console.log('loading package');
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,
});
});
},
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: undefined,
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(ERRNO_CODES.ENOENT);
},
mknod: function (parent, name, mode, dev) {
throw new FS.ErrnoError(ERRNO_CODES.EPERM);
},
rename: function (oldNode, newDir, newName) {
throw new FS.ErrnoError(ERRNO_CODES.EPERM);
},
unlink: function(parent, name) {
throw new FS.ErrnoError(ERRNO_CODES.EPERM);
},
rmdir: function(parent, name) {
throw new FS.ErrnoError(ERRNO_CODES.EPERM);
},
readdir: function(node) {
throw new FS.ErrnoError(ERRNO_CODES.EPERM);
},
symlink: function(parent, newName, oldPath) {
throw new FS.ErrnoError(ERRNO_CODES.EPERM);
},
readlink: function(node) {
throw new FS.ErrnoError(ERRNO_CODES.EPERM);
},
},
stream_ops: {
read: function (stream, buffer, offset, length, position) {
//console.log('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;
//console.log('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) {
console.log('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);
//console.log('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(ERRNO_CODES.EIO);
},
llseek: function (stream, offset, whence) {
var position = offset;
if (whence === 1) { // SEEK_CUR.
position += stream.position;
} else if (whence === 2) { // SEEK_END.
if (FS.isFile(stream.node.mode)) {
position += stream.node.size;
}
}
if (position < 0) {
throw new FS.ErrnoError(ERRNO_CODES.EINVAL);
}
return position;
},
},
},
});
LibraryManager.library['$FS__deps'].push('$LZ4'); // LZ4=1, so auto-include us
#endif