index.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.overrideBytesInBuffer = overrideBytesInBuffer;
  6. exports.makeBuffer = makeBuffer;
  7. exports.fromHexdump = fromHexdump;
  8. function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
  9. 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."); }
  10. 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); }
  11. function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
  12. function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
  13. 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; }
  14. function concatUint8Arrays() {
  15. for (var _len = arguments.length, arrays = new Array(_len), _key = 0; _key < _len; _key++) {
  16. arrays[_key] = arguments[_key];
  17. }
  18. var totalLength = arrays.reduce(function (a, b) {
  19. return a + b.length;
  20. }, 0);
  21. var result = new Uint8Array(totalLength);
  22. var offset = 0;
  23. for (var _i = 0, _arrays = arrays; _i < _arrays.length; _i++) {
  24. var arr = _arrays[_i];
  25. if (arr instanceof Uint8Array === false) {
  26. throw new Error("arr must be of type Uint8Array");
  27. }
  28. result.set(arr, offset);
  29. offset += arr.length;
  30. }
  31. return result;
  32. }
  33. function overrideBytesInBuffer(buffer, startLoc, endLoc, newBytes) {
  34. var beforeBytes = buffer.slice(0, startLoc);
  35. var afterBytes = buffer.slice(endLoc, buffer.length); // replacement is empty, we can omit it
  36. if (newBytes.length === 0) {
  37. return concatUint8Arrays(beforeBytes, afterBytes);
  38. }
  39. var replacement = Uint8Array.from(newBytes);
  40. return concatUint8Arrays(beforeBytes, replacement, afterBytes);
  41. }
  42. function makeBuffer() {
  43. for (var _len2 = arguments.length, splitedBytes = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
  44. splitedBytes[_key2] = arguments[_key2];
  45. }
  46. // $FlowIgnore
  47. var bytes = [].concat.apply([], splitedBytes);
  48. return new Uint8Array(bytes).buffer;
  49. }
  50. function fromHexdump(str) {
  51. var lines = str.split("\n"); // remove any leading left whitespace
  52. lines = lines.map(function (line) {
  53. return line.trim();
  54. });
  55. var bytes = lines.reduce(function (acc, line) {
  56. var cols = line.split(" "); // remove the offset, left column
  57. cols.shift();
  58. cols = cols.filter(function (x) {
  59. return x !== "";
  60. });
  61. var bytes = cols.map(function (x) {
  62. return parseInt(x, 16);
  63. });
  64. acc.push.apply(acc, _toConsumableArray(bytes));
  65. return acc;
  66. }, []);
  67. return new Uint8Array(bytes);
  68. }