-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuffer.js
More file actions
189 lines (163 loc) · 5.06 KB
/
Copy pathbuffer.js
File metadata and controls
189 lines (163 loc) · 5.06 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
181
182
183
184
185
186
187
188
189
/* eslint-disable no-proto */
import ieee754 from './_private/ieee754'
import { checkIEEE754, checkInt, checkOffset, checkSize } from './_private/assert'
import { utf8ToBytes, utf8Slice, blitBuffer } from './_private/utf8'
export default function Buffer () {}
Buffer.from = function (array) {
if ((array instanceof Uint8Array) === false) {
throw new Error('Buffer.from() requires a Uint8Array')
}
const buffer = array
buffer.__proto__ = Buffer.prototype
return buffer
}
Buffer.alloc = function (size = 0) {
checkSize(size)
const buffer = new Uint8Array(size)
buffer.__proto__ = Buffer.prototype
return buffer
}
Buffer.byteLength = function (string) {
if (typeof string !== 'string') {
string = '' + string
}
var len = string.length
if (len === 0) return 0
return utf8ToBytes(string).length
}
Buffer.prototype.__proto__ = Uint8Array.prototype
Buffer.prototype.toString = function () {
const length = this.length || 0
if (length === 0) return ''
return utf8Slice(this, 0, length)
}
Buffer.prototype.indexOf = function (value, offset = 0) {
// Empty buffer means no match
if (this.length === 0) return -1
// Normalize byteOffset
if (offset > 0x7fffffff) {
offset = 0x7fffffff
} else if (offset < -0x80000000) {
offset = -0x80000000
}
offset = +offset // Coerce to Number.
if (isNaN(offset)) {
// offset: it it's undefined, null, NaN, "foo", etc, search whole this
offset = this.length - 1
}
// Normalize offset: negative offsets start from the end of the this
if (offset < 0) offset = this.length + offset
if (offset >= this.length) {
offset = this.length - 1
} else if (offset < 0) {
return -1
}
// Special case: looking for empty string/this always fails
if (value.length === 0) {
return -1
}
value = value & 0xFF // Search for a byte value [0-255]
return Uint8Array.prototype.indexOf.call(this, value, offset)
}
Buffer.prototype.slice = function (start, end) {
var len = this.length
start = ~~start
end = end === undefined ? len : ~~end
if (start < 0) {
start += len
if (start < 0) start = 0
} else if (start > len) {
start = len
}
if (end < 0) {
end += len
if (end < 0) end = 0
} else if (end > len) {
end = len
}
if (end < start) end = start
const newBuf = this.subarray(start, end)
newBuf.__proto__ = Buffer.prototype
return newBuf
}
Buffer.prototype.write = function (string, offset) {
var length = this.length - offset
if ((string.length > 0 && (length < 0 || offset < 0)) || offset > this.length) {
throw new RangeError('Attempt to write outside buffer bounds')
}
return blitBuffer(utf8ToBytes(string, this.length - offset), this, offset, length)
}
Buffer.prototype.writeInt8 = function (value, offset) {
value = +value
offset = offset | 0
checkInt(this, value, offset, 1, 0x7f, -0x80)
if (value < 0) value = 0xff + value + 1
this[offset] = (value & 0xff)
return offset + 1
}
Buffer.prototype.writeUInt8 = function (value, offset) {
value = +value
offset = offset | 0
checkInt(this, value, offset, 1, 0xff, 0)
this[offset] = (value & 0xff)
return offset + 1
}
Buffer.prototype.writeFloatLE = function (value, offset) {
checkIEEE754(this, value, offset, 4, 3.4028234663852886e+38, -3.4028234663852886e+38)
ieee754.write(this, value, offset, 23, 4)
return offset + 4
}
Buffer.prototype.writeDoubleLE = function (value, offset) {
checkIEEE754(this, value, offset, 8, 1.7976931348623157E+308, -1.7976931348623157E+308)
ieee754.write(this, value, offset, 52, 8)
return offset + 8
}
Buffer.prototype.writeInt16LE = function (value, offset) {
value = +value
offset = offset | 0
checkInt(this, value, offset, 2, 0x7fff, -0x8000)
this[offset] = (value & 0xff)
this[offset + 1] = (value >>> 8)
return offset + 2
}
Buffer.prototype.writeInt32LE = function (value, offset) {
value = +value
offset = offset | 0
checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000)
this[offset] = (value & 0xff)
this[offset + 1] = (value >>> 8)
this[offset + 2] = (value >>> 16)
this[offset + 3] = (value >>> 24)
return offset + 4
}
Buffer.prototype.readInt8 = function (offset) {
checkOffset(offset, 1, this.length)
if (!(this[offset] & 0x80)) return (this[offset])
return ((0xff - this[offset] + 1) * -1)
}
Buffer.prototype.readUInt8 = function (offset) {
checkOffset(offset, 1, this.length)
return this[offset]
}
Buffer.prototype.readFloatLE = function (offset) {
checkOffset(offset, 4, this.length)
return ieee754.read(this, offset, 23, 4)
}
Buffer.prototype.readDoubleLE = function (offset) {
checkOffset(offset, 8, this.length)
return ieee754.read(this, offset, 52, 8)
}
Buffer.prototype.readInt16LE = function (offset) {
offset = offset >>> 0
checkOffset(offset, 2, this.length)
const val = this[offset] | (this[offset + 1] << 8)
return (val & 0x8000) ? val | 0xFFFF0000 : val
}
Buffer.prototype.readInt32LE = function (offset) {
offset = offset >>> 0
checkOffset(offset, 4, this.length)
return (this[offset]) |
(this[offset + 1] << 8) |
(this[offset + 2] << 16) |
(this[offset + 3] << 24)
}