index.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
  2. function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
  3. function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
  4. function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
  5. function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
  6. function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
  7. function concatUint8Arrays() {
  8. for (var _len = arguments.length, arrays = new Array(_len), _key = 0; _key < _len; _key++) {
  9. arrays[_key] = arguments[_key];
  10. }
  11. var totalLength = arrays.reduce(function (a, b) {
  12. return a + b.length;
  13. }, 0);
  14. var result = new Uint8Array(totalLength);
  15. var offset = 0;
  16. for (var _i = 0, _arrays = arrays; _i < _arrays.length; _i++) {
  17. var arr = _arrays[_i];
  18. if (arr instanceof Uint8Array === false) {
  19. throw new Error("arr must be of type Uint8Array");
  20. }
  21. result.set(arr, offset);
  22. offset += arr.length;
  23. }
  24. return result;
  25. }
  26. export function overrideBytesInBuffer(buffer, startLoc, endLoc, newBytes) {
  27. var beforeBytes = buffer.slice(0, startLoc);
  28. var afterBytes = buffer.slice(endLoc, buffer.length); // replacement is empty, we can omit it
  29. if (newBytes.length === 0) {
  30. return concatUint8Arrays(beforeBytes, afterBytes);
  31. }
  32. var replacement = Uint8Array.from(newBytes);
  33. return concatUint8Arrays(beforeBytes, replacement, afterBytes);
  34. }
  35. export function makeBuffer() {
  36. for (var _len2 = arguments.length, splitedBytes = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
  37. splitedBytes[_key2] = arguments[_key2];
  38. }
  39. // $FlowIgnore
  40. var bytes = [].concat.apply([], splitedBytes);
  41. return new Uint8Array(bytes).buffer;
  42. }
  43. export function fromHexdump(str) {
  44. var lines = str.split("\n"); // remove any leading left whitespace
  45. lines = lines.map(function (line) {
  46. return line.trim();
  47. });
  48. var bytes = lines.reduce(function (acc, line) {
  49. var cols = line.split(" "); // remove the offset, left column
  50. cols.shift();
  51. cols = cols.filter(function (x) {
  52. return x !== "";
  53. });
  54. var bytes = cols.map(function (x) {
  55. return parseInt(x, 16);
  56. });
  57. acc.push.apply(acc, _toConsumableArray(bytes));
  58. return acc;
  59. }, []);
  60. return new Uint8Array(bytes);
  61. }