-
-
Notifications
You must be signed in to change notification settings - Fork 297
Expand file tree
/
Copy pathbrowser-buffer.js
More file actions
206 lines (179 loc) · 5.58 KB
/
browser-buffer.js
File metadata and controls
206 lines (179 loc) · 5.58 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
"use strict"
// Minimal Buffer shim for iconv-lite browser usage.
// Implements only the subset of Buffer API used by iconv-lite codecs.
// Extends Uint8Array so instanceof checks and standard web APIs work.
var textEncoder = new TextEncoder()
var textDecoder = new TextDecoder()
class BufferShim extends Uint8Array {
// Prevent inherited methods (like map, filter) from creating BufferShim instances.
static get [Symbol.species] () { return Uint8Array }
static alloc (size, fill) {
var buf = new BufferShim(size)
if (fill !== undefined && fill !== 0) {
buf.fill(fill)
}
return buf
}
static from (arg, encoding) {
if (typeof arg === "string") {
return BufferShim._fromString(arg, encoding)
}
// Uint8Array, Array, or other array-like
var result = new BufferShim(arg.length)
result.set(arg)
return result
}
static _fromString (str, encoding) {
if (!encoding || encoding === "utf8" || encoding === "utf-8") {
var encoded = textEncoder.encode(str)
var buf = new BufferShim(encoded.length)
buf.set(encoded)
return buf
}
if (encoding === "ucs2" || encoding === "ucs-2" ||
encoding === "utf16le" || encoding === "utf-16le") {
var buf = new BufferShim(str.length * 2)
for (var i = 0; i < str.length; i++) {
var code = str.charCodeAt(i)
buf[i * 2] = code & 0xFF
buf[i * 2 + 1] = (code >> 8) & 0xFF
}
return buf
}
if (encoding === "binary" || encoding === "latin1") {
var buf = new BufferShim(str.length)
for (var i = 0; i < str.length; i++) {
buf[i] = str.charCodeAt(i) & 0xFF
}
return buf
}
if (encoding === "base64") {
// Node.js is lenient with base64 - strip invalid chars and add padding
var cleaned = str.replace(/[^A-Za-z0-9+/]/g, "")
while (cleaned.length % 4 !== 0) cleaned += "="
var binaryStr
try { binaryStr = atob(cleaned) } catch (e) { binaryStr = "" }
var buf = new BufferShim(binaryStr.length)
for (var i = 0; i < binaryStr.length; i++) {
buf[i] = binaryStr.charCodeAt(i)
}
return buf
}
if (encoding === "hex") {
var len = str.length >> 1
var buf = new BufferShim(len)
for (var i = 0; i < len; i++) {
buf[i] = parseInt(str.substr(i * 2, 2), 16)
}
return buf
}
if (encoding === "ascii") {
var buf = new BufferShim(str.length)
for (var i = 0; i < str.length; i++) {
buf[i] = str.charCodeAt(i) & 0x7F
}
return buf
}
// Fallback to utf8
return BufferShim._fromString(str, "utf8")
}
static concat (list) {
var totalLength = 0
for (var i = 0; i < list.length; i++) {
totalLength += list[i].length
}
var result = new BufferShim(totalLength)
var offset = 0
for (var i = 0; i < list.length; i++) {
result.set(list[i], offset)
offset += list[i].length
}
return result
}
static isBuffer (obj) {
return obj instanceof BufferShim
}
toString (encoding) {
if (!encoding || encoding === "utf8" || encoding === "utf-8") {
return textDecoder.decode(this)
}
if (encoding === "ucs2" || encoding === "ucs-2" ||
encoding === "utf16le" || encoding === "utf-16le") {
var result = ""
var CHUNK = 8192
var count = this.length >> 1
for (var start = 0; start < count; start += CHUNK) {
var end = Math.min(start + CHUNK, count)
var codes = new Array(end - start)
for (var i = start; i < end; i++) {
codes[i - start] = this[i * 2] | (this[i * 2 + 1] << 8)
}
result += String.fromCharCode.apply(null, codes)
}
return result
}
if (encoding === "base64") {
var binaryStr = ""
for (var i = 0; i < this.length; i++) {
binaryStr += String.fromCharCode(this[i])
}
return btoa(binaryStr)
}
if (encoding === "binary" || encoding === "latin1") {
var result = ""
for (var i = 0; i < this.length; i++) {
result += String.fromCharCode(this[i])
}
return result
}
if (encoding === "hex") {
var result = ""
for (var i = 0; i < this.length; i++) {
result += (this[i] < 16 ? "0" : "") + this[i].toString(16)
}
return result
}
if (encoding === "ascii") {
var result = ""
for (var i = 0; i < this.length; i++) {
result += String.fromCharCode(this[i] & 0x7F)
}
return result
}
// Fallback to utf8
return textDecoder.decode(this)
}
slice (start, end) {
var sliced = Uint8Array.prototype.slice.call(this, start, end)
Object.setPrototypeOf(sliced, BufferShim.prototype)
return sliced
}
write (str, offset) {
// Only used in utf7.js for writing ASCII strings at an offset.
// Returns number of bytes written.
if (offset === undefined) offset = 0
var len = Math.min(str.length, this.length - offset)
for (var i = 0; i < len; i++) {
this[offset + i] = str.charCodeAt(i) & 0xFF
}
return len
}
readUInt16LE (offset) {
return this[offset] | (this[offset + 1] << 8)
}
writeUInt32LE (value, offset) {
this[offset] = value & 0xFF
this[offset + 1] = (value >>> 8) & 0xFF
this[offset + 2] = (value >>> 16) & 0xFF
this[offset + 3] = (value >>> 24) & 0xFF
return offset + 4
}
writeUInt32BE (value, offset) {
this[offset] = (value >>> 24) & 0xFF
this[offset + 1] = (value >>> 16) & 0xFF
this[offset + 2] = (value >>> 8) & 0xFF
this[offset + 3] = value & 0xFF
return offset + 4
}
}
module.exports = { Buffer: BufferShim }