prng.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Very simple prng to allow for randomized tests with reproducable
  3. * results.
  4. *
  5. * Copyright (C) 2012 by Open Source Routing.
  6. * Copyright (C) 2012 by Internet Systems Consortium, Inc. ("ISC")
  7. *
  8. * This file is part of Quagga
  9. *
  10. * Quagga is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the
  12. * Free Software Foundation; either version 2, or (at your option) any
  13. * later version.
  14. *
  15. * Quagga is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with Quagga; see the file COPYING. If not, write to the Free
  22. * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  23. * 02111-1307, USA.
  24. */
  25. #include <assert.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include "prng.h"
  29. struct prng
  30. {
  31. unsigned long long state1;
  32. unsigned long long state2;
  33. };
  34. static char
  35. prng_bit(struct prng *prng)
  36. {
  37. prng->state1 *= 2416;
  38. prng->state1 += 374441;
  39. prng->state1 %= 1771875;
  40. if (prng->state1 % 2)
  41. {
  42. prng->state2 *= 84589;
  43. prng->state2 += 45989;
  44. prng->state2 %= 217728;
  45. }
  46. return prng->state2 % 2;
  47. }
  48. struct prng*
  49. prng_new(unsigned long long seed)
  50. {
  51. struct prng *rv = calloc(sizeof(*rv), 1);
  52. assert(rv);
  53. rv->state1 = rv->state2 = seed;
  54. return rv;
  55. }
  56. unsigned int
  57. prng_rand(struct prng *prng)
  58. {
  59. unsigned int i, rv = 0;
  60. for (i = 0; i < 32; i++)
  61. {
  62. rv |= prng_bit(prng);
  63. rv <<= 1;
  64. }
  65. return rv;
  66. }
  67. const char *
  68. prng_fuzz(struct prng *prng,
  69. const char *string,
  70. const char *charset,
  71. unsigned int operations)
  72. {
  73. static char buf[256];
  74. unsigned int charset_len;
  75. unsigned int i;
  76. unsigned int offset;
  77. unsigned int op;
  78. unsigned int character;
  79. assert(strlen(string) < sizeof(buf));
  80. strncpy(buf, string, sizeof(buf));
  81. charset_len = strlen(charset);
  82. for (i = 0; i < operations; i++)
  83. {
  84. offset = prng_rand(prng) % strlen(buf);
  85. op = prng_rand(prng) % 3;
  86. switch (op)
  87. {
  88. case 0:
  89. /* replace */
  90. character = prng_rand(prng) % charset_len;
  91. buf[offset] = charset[character];
  92. break;
  93. case 1:
  94. /* remove */
  95. memmove(buf + offset, buf + offset + 1, strlen(buf) - offset);
  96. break;
  97. case 2:
  98. /* insert */
  99. assert(strlen(buf) + 1 < sizeof(buf));
  100. memmove(buf + offset + 1, buf + offset, strlen(buf) + 1 - offset);
  101. character = prng_rand(prng) % charset_len;
  102. buf[offset] = charset[character];
  103. break;
  104. }
  105. }
  106. return buf;
  107. }
  108. void
  109. prng_free(struct prng *prng)
  110. {
  111. free(prng);
  112. }