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.

66 lines
2.0 KiB

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