summaryrefslogtreecommitdiffstats
path: root/src/lib/lz4/lz4-block-codec-js.js
blob: 809da66adea41946d9f1972259f26163dd9a3f6f (plain)
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*******************************************************************************

    lz4-block-codec-js.js
        A javascript wrapper around a pure javascript implementation of
        LZ4 block format codec.
    Copyright (C) 2018 Raymond Hill

    BSD-2-Clause License (http://www.opensource.org/licenses/bsd-license.php)

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions are
    met:

    1. Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer.

    2. Redistributions in binary form must reproduce the above
    copyright notice, this list of conditions and the following disclaimer
    in the documentation and/or other materials provided with the
    distribution.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

    Home: https://github.com/gorhill/lz4-wasm

    I used the same license as the one picked by creator of LZ4 out of respect
    for his creation, see https://lz4.github.io/lz4/

*/

'use strict';

/******************************************************************************/

(function(context) {                    // >>>> Start of private namespace

/******************************************************************************/

const growOutputBuffer = function(instance, size) {
    if (
        instance.outputBuffer === undefined ||
        instance.outputBuffer.byteLength < size
    ) {
        instance.outputBuffer = new ArrayBuffer(size + 0xFFFF & 0x7FFF0000);
    }
    return instance.outputBuffer;
};

const encodeBound = function(size) {
    return size > 0x7E000000 ?
        0 :
        size + (size / 255 | 0) + 16;
};

const encodeBlock = function(instance, iBuf, oOffset) {
    let iLen = iBuf.byteLength;
    if ( iLen >= 0x7E000000 ) { throw new RangeError(); }

    // "The last match must start at least 12 bytes before end of block"
    let lastMatchPos = iLen - 12;

    // "The last 5 bytes are always literals"
    let lastLiteralPos = iLen - 5;

    if ( instance.hashTable === undefined ) {
        instance.hashTable = new Int32Array(65536);
    }
    instance.hashTable.fill(-65536);

    if ( iBuf instanceof ArrayBuffer ) {
        iBuf = new Uint8Array(iBuf);
    }

    let oLen = oOffset + encodeBound(iLen);
    let oBuf = new Uint8Array(growOutputBuffer(instance, oLen), 0, oLen);
    let iPos = 0;
    let oPos = oOffset;
    let anchorPos = 0;

    // sequence-finding loop
    for (;;) {
        let refPos;
        let mOffset;
        let sequence = iBuf[iPos] << 8 | iBuf[iPos+1] << 16 | iBuf[iPos+2] << 24;

        // match-finding loop
        while ( iPos <= lastMatchPos ) {
            sequence = sequence >>> 8 | iBuf[iPos+3] << 24;
            let hash = (sequence * 0x9E37 & 0xFFFF) + (sequence * 0x79B1 >>> 16) & 0xFFFF;
            refPos = instance.hashTable[hash];
            instance.hashTable[hash] = iPos;
            mOffset = iPos - refPos;
            if (
                mOffset < 65536 &&
                iBuf[refPos+0] === ((sequence       ) & 0xFF) &&
                iBuf[refPos+1] === ((sequence >>>  8) & 0xFF) &&
                iBuf[refPos+2] === ((sequence >>> 16) & 0xFF) &&
                iBuf[refPos+3] === ((sequence >>> 24) & 0xFF)
            ) {
                break;
            }
            iPos += 1;
        }

        // no match found
        if ( iPos > lastMatchPos ) { break; }

        // match found
        let lLen = iPos - anchorPos;
        let mLen = iPos;
        iPos += 4; refPos += 4;
        while ( iPos < lastLiteralPos && iBuf[iPos] === iBuf[refPos] ) {
            iPos += 1; refPos += 1;
        }
        mLen = iPos - mLen;
        let token = mLen < 19 ? mLen - 4 : 15;

        // write token, length of literals if needed
        if ( lLen >= 15 ) {
            oBuf[oPos++] = 0xF0 | token;
            let l = lLen - 15;
            while ( l >= 255 ) {
                oBuf[oPos++] = 255;
                l -= 255;
            }
            oBuf[oPos++] = l;
        } else {
            oBuf[oPos++] = (lLen << 4) | token;
        }

        // write literals
        while ( lLen-- ) {
            oBuf[oPos++] = iBuf[anchorPos++];
        }

        if ( mLen === 0 ) { break; }

        // write offset of match
        oBuf[oPos+0] = mOffset;
        oBuf[oPos+1] = mOffset >>> 8;
        oPos += 2;

        // write length of match if needed
        if ( mLen >= 19 ) {
            let l = mLen - 19;
            while ( l >= 255 ) {
                oBuf[oPos++] = 255;
                l -= 255;
            }
            oBuf[oPos++] = l;
        }

        anchorPos = iPos;
    }

    // last sequence is literals only
    let lLen = iLen - anchorPos;
    if ( lLen >= 15 ) {
        oBuf[oPos++] = 0xF0;
        let l = lLen - 15;
        while ( l >= 255 ) {
            oBuf[oPos++] = 255;
            l -= 255;
        }
        oBuf[oPos++] = l;
    } else {
        oBuf[oPos++] = lLen << 4;
    }
    while ( lLen-- ) {
        oBuf[oPos++] = iBuf[anchorPos++];
    }

    return new Uint8Array(oBuf.buffer, 0, oPos);
};

const decodeBlock = function(instance, iBuf, iOffset, oLen) {
    let iLen = iBuf.byteLength;
    let oBuf = new Uint8Array(growOutputBuffer(instance, oLen), 0, oLen);
    let iPos = iOffset, oPos = 0;

    while ( iPos < iLen ) {
        let token = iBuf[iPos++];

        // literals
        let clen = token >>> 4;

        // length of literals
        if ( clen !== 0 ) {
            if ( clen === 15 ) {
                let l;
                for (;;) {
                    l = iBuf[iPos++];
                    if ( l !== 255 ) { break; }
                    clen += 255;
                }
                clen += l;
            }

            // copy literals
            let end = iPos + clen;
            while ( iPos < end ) {
                oBuf[oPos++] = iBuf[iPos++];
            }
            if ( iPos === iLen ) { break; }
        }

        // match
        let mOffset = iBuf[iPos+0] | (iBuf[iPos+1] << 8);
        if ( mOffset === 0 || mOffset > oPos ) { return; }
        iPos += 2;

        // length of match
        clen = (token & 0x0F) + 4;
        if ( clen === 19 ) {
            let l;
            for (;;) {
                l = iBuf[iPos++];
                if ( l !== 255 ) { break; }
                clen += 255;
            }
            clen += l;
        }

        // copy match
        let mPos = oPos - mOffset;
        let end = oPos + clen;
        while ( oPos < end ) {
            oBuf[oPos++] = oBuf[mPos++];
        }
    }

    return oBuf;
};

/******************************************************************************/

context.LZ4BlockJS = function() {
    this.hashTable = undefined;
    this.outputBuffer = undefined;
};

context.LZ4BlockJS.prototype = {
    flavor: 'js',
    init: function() {
        return Promise.resolve(true);
    },

    reset: function() {
        this.hashTable = undefined;
        this.outputBuffer = undefined;
    },

    bytesInUse: function() {
        let bytesInUse = 0;
        if ( this.hashTable !== undefined ) {
            bytesInUse += this.hashTable.byteLength;
        }
        if ( this.outputBuffer !== undefined ) {
            bytesInUse += this.outputBuffer.byteLength;
        }
        return bytesInUse;
    },

    encodeBlock: function(input, outputOffset) {
        if ( input instanceof ArrayBuffer ) {
            input = new Uint8Array(input);
        } else if ( input instanceof Uint8Array === false ) {
            throw new TypeError();
        }
        return encodeBlock(this, input, outputOffset);
    },

    decodeBlock: function(input, inputOffset, outputSize) {
        if ( input instanceof ArrayBuffer ) {
            input = new Uint8Array(input);
        } else if ( input instanceof Uint8Array === false ) {
            throw new TypeError();
        }
        return decodeBlock(this, input, inputOffset, outputSize);
    }
};

/******************************************************************************/

})(this || self);                       // <<<< End of private namespace

/******************************************************************************/