|
| 1 | +export function h$wrapBuffer(buf, unalignedOk, offset, length) { |
| 2 | + if (!unalignedOk && offset && offset % 8 !== 0) { |
| 3 | + throw "h$wrapBuffer: offset not aligned:" + offset; |
| 4 | + } |
| 5 | + if (!buf || !(buf instanceof ArrayBuffer)) |
| 6 | + throw "h$wrapBuffer: not an ArrayBuffer"; |
| 7 | + if (!offset) { |
| 8 | + offset = 0; |
| 9 | + } |
| 10 | + if (!length || length < 0) { |
| 11 | + length = buf.byteLength - offset; |
| 12 | + } |
| 13 | + return { |
| 14 | + buf: buf, |
| 15 | + len: length, |
| 16 | + i3: offset % 4 ? null : new Int32Array(buf, offset, length >> 2), |
| 17 | + u8: new Uint8Array(buf, offset, length), |
| 18 | + u1: offset % 2 ? null : new Uint16Array(buf, offset, length >> 1), |
| 19 | + f3: offset % 4 ? null : new Float32Array(buf, offset, length >> 2), |
| 20 | + f6: offset % 8 ? null : new Float64Array(buf, offset, length >> 3), |
| 21 | + dv: new DataView(buf, offset, length), |
| 22 | + }; |
| 23 | +} |
| 24 | + |
| 25 | +export function h$byteArrayToBase64String(off, len, ba) { |
| 26 | + var bin = ""; |
| 27 | + var u8 = ba.u8; |
| 28 | + var end = off + len; |
| 29 | + for (var i = off; i < end; i++) { |
| 30 | + bin += String.fromCharCode(u8[i]); |
| 31 | + } |
| 32 | + return window.btoa(bin); |
| 33 | +} |
| 34 | + |
| 35 | +export function h$newByteArrayFromBase64String(base64) { |
| 36 | + var bin = window.atob(base64); |
| 37 | + var ba = h$newByteArray(bin.length); |
| 38 | + var u8 = ba.u8; |
| 39 | + for (var i = 0; i < bin.length; i++) { |
| 40 | + u8[i] = bin.charCodeAt(i); |
| 41 | + } |
| 42 | + return ba; |
| 43 | +} |
| 44 | + |
| 45 | +export function h$newByteArray(len) { |
| 46 | + var len0 = Math.max(h$roundUpToMultipleOf(len, 8), 8); |
| 47 | + var buf = new ArrayBuffer(len0); |
| 48 | + return { |
| 49 | + buf: buf, |
| 50 | + len: len, |
| 51 | + i3: new Int32Array(buf), |
| 52 | + u8: new Uint8Array(buf), |
| 53 | + u1: new Uint16Array(buf), |
| 54 | + f3: new Float32Array(buf), |
| 55 | + f6: new Float64Array(buf), |
| 56 | + dv: new DataView(buf), |
| 57 | + }; |
| 58 | +} |
| 59 | + |
| 60 | +export function h$roundUpToMultipleOf(n, m) { |
| 61 | + var rem = n % m; |
| 62 | + return rem === 0 ? n : n - rem + m; |
| 63 | +} |
| 64 | + |
| 65 | +[ |
| 66 | + h$wrapBuffer, |
| 67 | + h$byteArrayToBase64String, |
| 68 | + h$newByteArrayFromBase64String, |
| 69 | + h$newByteArray, |
| 70 | + h$roundUpToMultipleOf, |
| 71 | +].forEach(function (f) { |
| 72 | + if (!globalThis[f.name]) { |
| 73 | + globalThis[f.name] = f; |
| 74 | + } |
| 75 | +}); |
0 commit comments