-
Notifications
You must be signed in to change notification settings - Fork 30
/
snappy_decompressor.js
132 lines (122 loc) · 3.63 KB
/
snappy_decompressor.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
// The MIT License (MIT)
//
// Copyright (c) 2016 Zhipeng Jia
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
'use strict'
var WORD_MASK = [0, 0xff, 0xffff, 0xffffff, 0xffffffff]
function copyBytes (fromArray, fromPos, toArray, toPos, length) {
var i
for (i = 0; i < length; i++) {
toArray[toPos + i] = fromArray[fromPos + i]
}
}
function selfCopyBytes (array, pos, offset, length) {
var i
for (i = 0; i < length; i++) {
array[pos + i] = array[pos - offset + i]
}
}
function SnappyDecompressor (compressed) {
this.array = compressed
this.pos = 0
}
SnappyDecompressor.prototype.readUncompressedLength = function () {
var result = 0
var shift = 0
var c, val
while (shift < 32 && this.pos < this.array.length) {
c = this.array[this.pos]
this.pos += 1
val = c & 0x7f
if (((val << shift) >>> shift) !== val) {
return -1
}
result |= val << shift
if (c < 128) {
return result
}
shift += 7
}
return -1
}
SnappyDecompressor.prototype.uncompressToBuffer = function (outBuffer) {
var array = this.array
var arrayLength = array.length
var pos = this.pos
var outPos = 0
var c, len, smallLen
var offset
while (pos < array.length) {
c = array[pos]
pos += 1
if ((c & 0x3) === 0) {
// Literal
len = (c >>> 2) + 1
if (len > 60) {
if (pos + 3 >= arrayLength) {
return false
}
smallLen = len - 60
len = array[pos] + (array[pos + 1] << 8) + (array[pos + 2] << 16) + (array[pos + 3] << 24)
len = (len & WORD_MASK[smallLen]) + 1
pos += smallLen
}
if (pos + len > arrayLength) {
return false
}
copyBytes(array, pos, outBuffer, outPos, len)
pos += len
outPos += len
} else {
switch (c & 0x3) {
case 1:
len = ((c >>> 2) & 0x7) + 4
offset = array[pos] + ((c >>> 5) << 8)
pos += 1
break
case 2:
if (pos + 1 >= arrayLength) {
return false
}
len = (c >>> 2) + 1
offset = array[pos] + (array[pos + 1] << 8)
pos += 2
break
case 3:
if (pos + 3 >= arrayLength) {
return false
}
len = (c >>> 2) + 1
offset = array[pos] + (array[pos + 1] << 8) + (array[pos + 2] << 16) + (array[pos + 3] << 24)
pos += 4
break
default:
break
}
if (offset === 0 || offset > outPos) {
return false
}
selfCopyBytes(outBuffer, outPos, offset, len)
outPos += len
}
}
return true
}
exports.SnappyDecompressor = SnappyDecompressor