[Service] Integrate DeviceHome and SignalingServer
[platform/framework/web/wrtjs.git] / device_home / node_modules / qrcode / lib / utils / typedarray-buffer.js
1 /**
2  * Implementation of a subset of node.js Buffer methods for the browser.
3  * Based on https://github.com/feross/buffer
4  */
5
6 /* eslint-disable no-proto */
7
8 'use strict'
9
10 var isArray = require('isarray')
11
12 function typedArraySupport () {
13   // Can typed array instances be augmented?
14   try {
15     var arr = new Uint8Array(1)
16     arr.__proto__ = {__proto__: Uint8Array.prototype, foo: function () { return 42 }}
17     return arr.foo() === 42
18   } catch (e) {
19     return false
20   }
21 }
22
23 Buffer.TYPED_ARRAY_SUPPORT = typedArraySupport()
24
25 var K_MAX_LENGTH = Buffer.TYPED_ARRAY_SUPPORT
26     ? 0x7fffffff
27     : 0x3fffffff
28
29 function Buffer (arg, offset, length) {
30   if (!Buffer.TYPED_ARRAY_SUPPORT && !(this instanceof Buffer)) {
31     return new Buffer(arg, offset, length)
32   }
33
34   if (typeof arg === 'number') {
35     return allocUnsafe(this, arg)
36   }
37
38   return from(this, arg, offset, length)
39 }
40
41 if (Buffer.TYPED_ARRAY_SUPPORT) {
42   Buffer.prototype.__proto__ = Uint8Array.prototype
43   Buffer.__proto__ = Uint8Array
44
45   // Fix subarray() in ES2016. See: https://github.com/feross/buffer/pull/97
46   if (typeof Symbol !== 'undefined' && Symbol.species &&
47       Buffer[Symbol.species] === Buffer) {
48     Object.defineProperty(Buffer, Symbol.species, {
49       value: null,
50       configurable: true,
51       enumerable: false,
52       writable: false
53     })
54   }
55 }
56
57 function checked (length) {
58   // Note: cannot use `length < K_MAX_LENGTH` here because that fails when
59   // length is NaN (which is otherwise coerced to zero.)
60   if (length >= K_MAX_LENGTH) {
61     throw new RangeError('Attempt to allocate Buffer larger than maximum ' +
62                          'size: 0x' + K_MAX_LENGTH.toString(16) + ' bytes')
63   }
64   return length | 0
65 }
66
67 function isnan (val) {
68   return val !== val // eslint-disable-line no-self-compare
69 }
70
71 function createBuffer (that, length) {
72   var buf
73   if (Buffer.TYPED_ARRAY_SUPPORT) {
74     buf = new Uint8Array(length)
75     buf.__proto__ = Buffer.prototype
76   } else {
77     // Fallback: Return an object instance of the Buffer class
78     buf = that
79     if (buf === null) {
80       buf = new Buffer(length)
81     }
82     buf.length = length
83   }
84
85   return buf
86 }
87
88 function allocUnsafe (that, size) {
89   var buf = createBuffer(that, size < 0 ? 0 : checked(size) | 0)
90
91   if (!Buffer.TYPED_ARRAY_SUPPORT) {
92     for (var i = 0; i < size; ++i) {
93       buf[i] = 0
94     }
95   }
96
97   return buf
98 }
99
100 function fromString (that, string) {
101   var length = byteLength(string) | 0
102   var buf = createBuffer(that, length)
103
104   var actual = buf.write(string)
105
106   if (actual !== length) {
107     // Writing a hex string, for example, that contains invalid characters will
108     // cause everything after the first invalid character to be ignored. (e.g.
109     // 'abxxcd' will be treated as 'ab')
110     buf = buf.slice(0, actual)
111   }
112
113   return buf
114 }
115
116 function fromArrayLike (that, array) {
117   var length = array.length < 0 ? 0 : checked(array.length) | 0
118   var buf = createBuffer(that, length)
119   for (var i = 0; i < length; i += 1) {
120     buf[i] = array[i] & 255
121   }
122   return buf
123 }
124
125 function fromArrayBuffer (that, array, byteOffset, length) {
126   if (byteOffset < 0 || array.byteLength < byteOffset) {
127     throw new RangeError('\'offset\' is out of bounds')
128   }
129
130   if (array.byteLength < byteOffset + (length || 0)) {
131     throw new RangeError('\'length\' is out of bounds')
132   }
133
134   var buf
135   if (byteOffset === undefined && length === undefined) {
136     buf = new Uint8Array(array)
137   } else if (length === undefined) {
138     buf = new Uint8Array(array, byteOffset)
139   } else {
140     buf = new Uint8Array(array, byteOffset, length)
141   }
142
143   if (Buffer.TYPED_ARRAY_SUPPORT) {
144     // Return an augmented `Uint8Array` instance, for best performance
145     buf.__proto__ = Buffer.prototype
146   } else {
147     // Fallback: Return an object instance of the Buffer class
148     buf = fromArrayLike(that, buf)
149   }
150
151   return buf
152 }
153
154 function fromObject (that, obj) {
155   if (Buffer.isBuffer(obj)) {
156     var len = checked(obj.length) | 0
157     var buf = createBuffer(that, len)
158
159     if (buf.length === 0) {
160       return buf
161     }
162
163     obj.copy(buf, 0, 0, len)
164     return buf
165   }
166
167   if (obj) {
168     if ((typeof ArrayBuffer !== 'undefined' &&
169         obj.buffer instanceof ArrayBuffer) || 'length' in obj) {
170       if (typeof obj.length !== 'number' || isnan(obj.length)) {
171         return createBuffer(that, 0)
172       }
173       return fromArrayLike(that, obj)
174     }
175
176     if (obj.type === 'Buffer' && Array.isArray(obj.data)) {
177       return fromArrayLike(that, obj.data)
178     }
179   }
180
181   throw new TypeError('First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object.')
182 }
183
184 function utf8ToBytes (string, units) {
185   units = units || Infinity
186   var codePoint
187   var length = string.length
188   var leadSurrogate = null
189   var bytes = []
190
191   for (var i = 0; i < length; ++i) {
192     codePoint = string.charCodeAt(i)
193
194     // is surrogate component
195     if (codePoint > 0xD7FF && codePoint < 0xE000) {
196       // last char was a lead
197       if (!leadSurrogate) {
198         // no lead yet
199         if (codePoint > 0xDBFF) {
200           // unexpected trail
201           if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
202           continue
203         } else if (i + 1 === length) {
204           // unpaired lead
205           if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
206           continue
207         }
208
209         // valid lead
210         leadSurrogate = codePoint
211
212         continue
213       }
214
215       // 2 leads in a row
216       if (codePoint < 0xDC00) {
217         if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
218         leadSurrogate = codePoint
219         continue
220       }
221
222       // valid surrogate pair
223       codePoint = (leadSurrogate - 0xD800 << 10 | codePoint - 0xDC00) + 0x10000
224     } else if (leadSurrogate) {
225       // valid bmp char, but last char was a lead
226       if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
227     }
228
229     leadSurrogate = null
230
231     // encode utf8
232     if (codePoint < 0x80) {
233       if ((units -= 1) < 0) break
234       bytes.push(codePoint)
235     } else if (codePoint < 0x800) {
236       if ((units -= 2) < 0) break
237       bytes.push(
238         codePoint >> 0x6 | 0xC0,
239         codePoint & 0x3F | 0x80
240       )
241     } else if (codePoint < 0x10000) {
242       if ((units -= 3) < 0) break
243       bytes.push(
244         codePoint >> 0xC | 0xE0,
245         codePoint >> 0x6 & 0x3F | 0x80,
246         codePoint & 0x3F | 0x80
247       )
248     } else if (codePoint < 0x110000) {
249       if ((units -= 4) < 0) break
250       bytes.push(
251         codePoint >> 0x12 | 0xF0,
252         codePoint >> 0xC & 0x3F | 0x80,
253         codePoint >> 0x6 & 0x3F | 0x80,
254         codePoint & 0x3F | 0x80
255       )
256     } else {
257       throw new Error('Invalid code point')
258     }
259   }
260
261   return bytes
262 }
263
264 function byteLength (string) {
265   if (Buffer.isBuffer(string)) {
266     return string.length
267   }
268   if (typeof ArrayBuffer !== 'undefined' && typeof ArrayBuffer.isView === 'function' &&
269       (ArrayBuffer.isView(string) || string instanceof ArrayBuffer)) {
270     return string.byteLength
271   }
272   if (typeof string !== 'string') {
273     string = '' + string
274   }
275
276   var len = string.length
277   if (len === 0) return 0
278
279   return utf8ToBytes(string).length
280 }
281
282 function blitBuffer (src, dst, offset, length) {
283   for (var i = 0; i < length; ++i) {
284     if ((i + offset >= dst.length) || (i >= src.length)) break
285     dst[i + offset] = src[i]
286   }
287   return i
288 }
289
290 function utf8Write (buf, string, offset, length) {
291   return blitBuffer(utf8ToBytes(string, buf.length - offset), buf, offset, length)
292 }
293
294 function from (that, value, offset, length) {
295   if (typeof value === 'number') {
296     throw new TypeError('"value" argument must not be a number')
297   }
298
299   if (typeof ArrayBuffer !== 'undefined' && value instanceof ArrayBuffer) {
300     return fromArrayBuffer(that, value, offset, length)
301   }
302
303   if (typeof value === 'string') {
304     return fromString(that, value, offset)
305   }
306
307   return fromObject(that, value)
308 }
309
310 Buffer.prototype.write = function write (string, offset, length) {
311   // Buffer#write(string)
312   if (offset === undefined) {
313     length = this.length
314     offset = 0
315   // Buffer#write(string, encoding)
316   } else if (length === undefined && typeof offset === 'string') {
317     length = this.length
318     offset = 0
319   // Buffer#write(string, offset[, length])
320   } else if (isFinite(offset)) {
321     offset = offset | 0
322     if (isFinite(length)) {
323       length = length | 0
324     } else {
325       length = undefined
326     }
327   }
328
329   var remaining = this.length - offset
330   if (length === undefined || length > remaining) length = remaining
331
332   if ((string.length > 0 && (length < 0 || offset < 0)) || offset > this.length) {
333     throw new RangeError('Attempt to write outside buffer bounds')
334   }
335
336   return utf8Write(this, string, offset, length)
337 }
338
339 Buffer.prototype.slice = function slice (start, end) {
340   var len = this.length
341   start = ~~start
342   end = end === undefined ? len : ~~end
343
344   if (start < 0) {
345     start += len
346     if (start < 0) start = 0
347   } else if (start > len) {
348     start = len
349   }
350
351   if (end < 0) {
352     end += len
353     if (end < 0) end = 0
354   } else if (end > len) {
355     end = len
356   }
357
358   if (end < start) end = start
359
360   var newBuf
361   if (Buffer.TYPED_ARRAY_SUPPORT) {
362     newBuf = this.subarray(start, end)
363     // Return an augmented `Uint8Array` instance
364     newBuf.__proto__ = Buffer.prototype
365   } else {
366     var sliceLen = end - start
367     newBuf = new Buffer(sliceLen, undefined)
368     for (var i = 0; i < sliceLen; ++i) {
369       newBuf[i] = this[i + start]
370     }
371   }
372
373   return newBuf
374 }
375
376 Buffer.prototype.copy = function copy (target, targetStart, start, end) {
377   if (!start) start = 0
378   if (!end && end !== 0) end = this.length
379   if (targetStart >= target.length) targetStart = target.length
380   if (!targetStart) targetStart = 0
381   if (end > 0 && end < start) end = start
382
383   // Copy 0 bytes; we're done
384   if (end === start) return 0
385   if (target.length === 0 || this.length === 0) return 0
386
387   // Fatal error conditions
388   if (targetStart < 0) {
389     throw new RangeError('targetStart out of bounds')
390   }
391   if (start < 0 || start >= this.length) throw new RangeError('sourceStart out of bounds')
392   if (end < 0) throw new RangeError('sourceEnd out of bounds')
393
394   // Are we oob?
395   if (end > this.length) end = this.length
396   if (target.length - targetStart < end - start) {
397     end = target.length - targetStart + start
398   }
399
400   var len = end - start
401   var i
402
403   if (this === target && start < targetStart && targetStart < end) {
404     // descending copy from end
405     for (i = len - 1; i >= 0; --i) {
406       target[i + targetStart] = this[i + start]
407     }
408   } else if (len < 1000 || !Buffer.TYPED_ARRAY_SUPPORT) {
409     // ascending copy from start
410     for (i = 0; i < len; ++i) {
411       target[i + targetStart] = this[i + start]
412     }
413   } else {
414     Uint8Array.prototype.set.call(
415       target,
416       this.subarray(start, start + len),
417       targetStart
418     )
419   }
420
421   return len
422 }
423
424 Buffer.prototype.fill = function fill (val, start, end) {
425   // Handle string cases:
426   if (typeof val === 'string') {
427     if (typeof start === 'string') {
428       start = 0
429       end = this.length
430     } else if (typeof end === 'string') {
431       end = this.length
432     }
433     if (val.length === 1) {
434       var code = val.charCodeAt(0)
435       if (code < 256) {
436         val = code
437       }
438     }
439   } else if (typeof val === 'number') {
440     val = val & 255
441   }
442
443   // Invalid ranges are not set to a default, so can range check early.
444   if (start < 0 || this.length < start || this.length < end) {
445     throw new RangeError('Out of range index')
446   }
447
448   if (end <= start) {
449     return this
450   }
451
452   start = start >>> 0
453   end = end === undefined ? this.length : end >>> 0
454
455   if (!val) val = 0
456
457   var i
458   if (typeof val === 'number') {
459     for (i = start; i < end; ++i) {
460       this[i] = val
461     }
462   } else {
463     var bytes = Buffer.isBuffer(val)
464       ? val
465       : new Buffer(val)
466     var len = bytes.length
467     for (i = 0; i < end - start; ++i) {
468       this[i + start] = bytes[i % len]
469     }
470   }
471
472   return this
473 }
474
475 Buffer.concat = function concat (list, length) {
476   if (!isArray(list)) {
477     throw new TypeError('"list" argument must be an Array of Buffers')
478   }
479
480   if (list.length === 0) {
481     return createBuffer(null, 0)
482   }
483
484   var i
485   if (length === undefined) {
486     length = 0
487     for (i = 0; i < list.length; ++i) {
488       length += list[i].length
489     }
490   }
491
492   var buffer = allocUnsafe(null, length)
493   var pos = 0
494   for (i = 0; i < list.length; ++i) {
495     var buf = list[i]
496     if (!Buffer.isBuffer(buf)) {
497       throw new TypeError('"list" argument must be an Array of Buffers')
498     }
499     buf.copy(buffer, pos)
500     pos += buf.length
501   }
502   return buffer
503 }
504
505 Buffer.byteLength = byteLength
506
507 Buffer.prototype._isBuffer = true
508 Buffer.isBuffer = function isBuffer (b) {
509   return !!(b != null && b._isBuffer)
510 }
511
512 module.exports.alloc = function (size) {
513   var buffer = new Buffer(size)
514   buffer.fill(0)
515   return buffer
516 }
517
518 module.exports.from = function (data) {
519   return new Buffer(data)
520 }