UNPKG

4.39 kBJavaScriptView Raw
1'use strict';
2
3// lightweight Buffer shim for pbf browser build
4// based on code from github.com/feross/buffer (MIT-licensed)
5
6module.exports = Buffer;
7
8var ieee754 = require('ieee754');
9
10var BufferMethods;
11
12function Buffer(length) {
13 var arr;
14 if (length && length.length) {
15 arr = length;
16 length = arr.length;
17 }
18 var buf = new Uint8Array(length || 0);
19 if (arr) buf.set(arr);
20
21 buf.readUInt32LE = BufferMethods.readUInt32LE;
22 buf.writeUInt32LE = BufferMethods.writeUInt32LE;
23 buf.readInt32LE = BufferMethods.readInt32LE;
24 buf.writeInt32LE = BufferMethods.writeInt32LE;
25 buf.readFloatLE = BufferMethods.readFloatLE;
26 buf.writeFloatLE = BufferMethods.writeFloatLE;
27 buf.readDoubleLE = BufferMethods.readDoubleLE;
28 buf.writeDoubleLE = BufferMethods.writeDoubleLE;
29 buf.toString = BufferMethods.toString;
30 buf.write = BufferMethods.write;
31 buf.slice = BufferMethods.slice;
32 buf.copy = BufferMethods.copy;
33
34 buf._isBuffer = true;
35 return buf;
36}
37
38var lastStr, lastStrEncoded;
39
40BufferMethods = {
41 readUInt32LE: function(pos) {
42 return ((this[pos]) |
43 (this[pos + 1] << 8) |
44 (this[pos + 2] << 16)) +
45 (this[pos + 3] * 0x1000000);
46 },
47
48 writeUInt32LE: function(val, pos) {
49 this[pos] = val;
50 this[pos + 1] = (val >>> 8);
51 this[pos + 2] = (val >>> 16);
52 this[pos + 3] = (val >>> 24);
53 },
54
55 readInt32LE: function(pos) {
56 return ((this[pos]) |
57 (this[pos + 1] << 8) |
58 (this[pos + 2] << 16)) +
59 (this[pos + 3] << 24);
60 },
61
62 readFloatLE: function(pos) { return ieee754.read(this, pos, true, 23, 4); },
63 readDoubleLE: function(pos) { return ieee754.read(this, pos, true, 52, 8); },
64
65 writeFloatLE: function(val, pos) { return ieee754.write(this, val, pos, true, 23, 4); },
66 writeDoubleLE: function(val, pos) { return ieee754.write(this, val, pos, true, 52, 8); },
67
68 toString: function(encoding, start, end) {
69 var str = '',
70 tmp = '';
71
72 start = start || 0;
73 end = Math.min(this.length, end || this.length);
74
75 for (var i = start; i < end; i++) {
76 var ch = this[i];
77 if (ch <= 0x7F) {
78 str += decodeURIComponent(tmp) + String.fromCharCode(ch);
79 tmp = '';
80 } else {
81 tmp += '%' + ch.toString(16);
82 }
83 }
84
85 str += decodeURIComponent(tmp);
86
87 return str;
88 },
89
90 write: function(str, pos) {
91 var bytes = str === lastStr ? lastStrEncoded : encodeString(str);
92 for (var i = 0; i < bytes.length; i++) {
93 this[pos + i] = bytes[i];
94 }
95 },
96
97 slice: function(start, end) {
98 return this.subarray(start, end);
99 },
100
101 copy: function(buf, pos) {
102 pos = pos || 0;
103 for (var i = 0; i < this.length; i++) {
104 buf[pos + i] = this[i];
105 }
106 }
107};
108
109BufferMethods.writeInt32LE = BufferMethods.writeUInt32LE;
110
111Buffer.byteLength = function(str) {
112 lastStr = str;
113 lastStrEncoded = encodeString(str);
114 return lastStrEncoded.length;
115};
116
117Buffer.isBuffer = function(buf) {
118 return !!(buf && buf._isBuffer);
119};
120
121function encodeString(str) {
122 var length = str.length,
123 bytes = [];
124
125 for (var i = 0, c, lead; i < length; i++) {
126 c = str.charCodeAt(i); // code point
127
128 if (c > 0xD7FF && c < 0xE000) {
129
130 if (lead) {
131 if (c < 0xDC00) {
132 bytes.push(0xEF, 0xBF, 0xBD);
133 lead = c;
134 continue;
135
136 } else {
137 c = lead - 0xD800 << 10 | c - 0xDC00 | 0x10000;
138 lead = null;
139 }
140
141 } else {
142 if (c > 0xDBFF || (i + 1 === length)) bytes.push(0xEF, 0xBF, 0xBD);
143 else lead = c;
144
145 continue;
146 }
147
148 } else if (lead) {
149 bytes.push(0xEF, 0xBF, 0xBD);
150 lead = null;
151 }
152
153 if (c < 0x80) bytes.push(c);
154 else if (c < 0x800) bytes.push(c >> 0x6 | 0xC0, c & 0x3F | 0x80);
155 else if (c < 0x10000) bytes.push(c >> 0xC | 0xE0, c >> 0x6 & 0x3F | 0x80, c & 0x3F | 0x80);
156 else bytes.push(c >> 0x12 | 0xF0, c >> 0xC & 0x3F | 0x80, c >> 0x6 & 0x3F | 0x80, c & 0x3F | 0x80);
157 }
158 return bytes;
159}