-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlz4_post.js
53 lines (44 loc) · 1.53 KB
/
lz4_post.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
;(function() {
var bufferLen = 8388608,
srcBuffer = Module['allocate'](bufferLen,'i8',Module['ALLOC_STATIC']),
dstBuffer = Module['allocate'](bufferLen,'i8',Module['ALLOC_STATIC'])
function _compress(src) {
var i,
srcLength = src.length,
heap = Module['HEAPU8'],
rsltLen,
rslt
for(i=0;i<srcLength;i++) heap[srcBuffer+i] = src[i]
rsltLen = Module['_LZ4_compress'](srcBuffer, dstBuffer, srcLength)
rslt = new src.constructor(rsltLen)
for(i=0;i<rsltLen;i++) rslt[i] = heap[dstBuffer+i]
return rslt
}
exports['compress'] = function(src) {
if (src.length > bufferLen) throw new Error('source length out of bounds')
return _compress(src)
}
function _decompress(src) {
var i,
srcLength = src.length,
heap = Module['HEAPU8'],
rsltLen,
rslt
for(i=0;i<srcLength;i++) heap[srcBuffer+i] = src[i]
rsltLen = Module['_LZ4_uncompress_unknownOutputSize'](srcBuffer, dstBuffer, srcLength, bufferLen)
if (rsltLen > 0) {
rslt = new src.constructor(rsltLen)
for(i=0;i<rsltLen;i++) rslt[i] = heap[dstBuffer+i]
}
return rslt
}
exports['uncompress'] = function(src) {
var rslt
if (src.length > bufferLen) throw new Error('source length out of bounds')
rslt = _decompress(src)
if (!rslt) throw new Error('decompression error')
return rslt
}
})()
})(typeof exports === 'undefined' ? this['lz4']={} : exports)
;