You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
2.2 KiB

4 years ago
  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) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
  9. function concatUint8Arrays() {
  10. for (var _len = arguments.length, arrays = new Array(_len), _key = 0; _key < _len; _key++) {
  11. arrays[_key] = arguments[_key];
  12. }
  13. var totalLength = arrays.reduce(function (a, b) {
  14. return a + b.length;
  15. }, 0);
  16. var result = new Uint8Array(totalLength);
  17. var offset = 0;
  18. for (var _i = 0; _i < arrays.length; _i++) {
  19. var arr = arrays[_i];
  20. if (arr instanceof Uint8Array === false) {
  21. throw new Error("arr must be of type Uint8Array");
  22. }
  23. result.set(arr, offset);
  24. offset += arr.length;
  25. }
  26. return result;
  27. }
  28. function overrideBytesInBuffer(buffer, startLoc, endLoc, newBytes) {
  29. var beforeBytes = buffer.slice(0, startLoc);
  30. var afterBytes = buffer.slice(endLoc, buffer.length); // replacement is empty, we can omit it
  31. if (newBytes.length === 0) {
  32. return concatUint8Arrays(beforeBytes, afterBytes);
  33. }
  34. var replacement = Uint8Array.from(newBytes);
  35. return concatUint8Arrays(beforeBytes, replacement, afterBytes);
  36. }
  37. function makeBuffer() {
  38. for (var _len2 = arguments.length, splitedBytes = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
  39. splitedBytes[_key2] = arguments[_key2];
  40. }
  41. var bytes = [].concat.apply([], splitedBytes);
  42. return new Uint8Array(bytes).buffer;
  43. }
  44. function fromHexdump(str) {
  45. var lines = str.split("\n"); // remove any leading left whitespace
  46. lines = lines.map(function (line) {
  47. return line.trim();
  48. });
  49. var bytes = lines.reduce(function (acc, line) {
  50. var cols = line.split(" "); // remove the offset, left column
  51. cols.shift();
  52. cols = cols.filter(function (x) {
  53. return x !== "";
  54. });
  55. var bytes = cols.map(function (x) {
  56. return parseInt(x, 16);
  57. });
  58. acc.push.apply(acc, _toConsumableArray(bytes));
  59. return acc;
  60. }, []);
  61. return Buffer.from(bytes);
  62. }