Fuzzer.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Fuzzer = void 0;
  4. const crypto_1 = require("crypto");
  5. function xoshiro128ss(a, b, c, d) {
  6. return () => {
  7. const t = b << 9;
  8. let r = b * 5;
  9. r = ((r << 7) | (r >>> 25)) * 9;
  10. c ^= a;
  11. d ^= b;
  12. b ^= c;
  13. a ^= d;
  14. c ^= t;
  15. d = (d << 11) | (d >>> 21);
  16. return (r >>> 0) / 4294967296;
  17. };
  18. }
  19. class Fuzzer {
  20. static randomInt(min, max) {
  21. return Math.floor(Math.random() * (max - min + 1)) + min;
  22. }
  23. static randomInt2([min, max]) {
  24. return Math.floor(Math.random() * (max - min + 1)) + min;
  25. }
  26. static pick(elements) {
  27. return elements[Math.floor(Math.random() * elements.length)];
  28. }
  29. static repeat(times, callback) {
  30. const result = [];
  31. for (let i = 0; i < times; i++)
  32. result.push(callback());
  33. return result;
  34. }
  35. constructor(seed) {
  36. this.randomInt = (min, max) => {
  37. return Math.floor(Math.random() * (max - min + 1)) + min;
  38. };
  39. this.pick = (elements) => {
  40. return elements[Math.floor(Math.random() * elements.length)];
  41. };
  42. this.repeat = (times, callback) => {
  43. const result = [];
  44. for (let i = 0; i < times; i++)
  45. result.push(callback());
  46. return result;
  47. };
  48. this.seed = seed = seed || (0, crypto_1.randomBytes)(4 * 4);
  49. let i = 0;
  50. const a = (seed[i++] << 24) | (seed[i++] << 16) | (seed[i++] << 8) | seed[i++];
  51. const b = (seed[i++] << 24) | (seed[i++] << 16) | (seed[i++] << 8) | seed[i++];
  52. const c = (seed[i++] << 24) | (seed[i++] << 16) | (seed[i++] << 8) | seed[i++];
  53. const d = (seed[i++] << 24) | (seed[i++] << 16) | (seed[i++] << 8) | seed[i++];
  54. this.random = xoshiro128ss(a, b, c, d);
  55. Math.random = this.random;
  56. }
  57. }
  58. exports.Fuzzer = Fuzzer;
  59. //# sourceMappingURL=Fuzzer.js.map