prng.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 <zebra.h>
  26. #include <assert.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include "prng.h"
  30. struct prng
  31. {
  32. unsigned long long state1;
  33. unsigned long long state2;
  34. };
  35. static char
  36. prng_bit(struct prng *prng)
  37. {
  38. prng->state1 *= 2416;
  39. prng->state1 += 374441;
  40. prng->state1 %= 1771875;
  41. if (prng->state1 % 2)
  42. {
  43. prng->state2 *= 84589;
  44. prng->state2 += 45989;
  45. prng->state2 %= 217728;
  46. }
  47. return prng->state2 % 2;
  48. }
  49. struct prng*
  50. prng_new(unsigned long long seed)
  51. {
  52. struct prng *rv = calloc(sizeof(*rv), 1);
  53. assert(rv);
  54. rv->state1 = rv->state2 = seed;
  55. return rv;
  56. }
  57. unsigned int
  58. prng_rand(struct prng *prng)
  59. {
  60. unsigned int i, rv = 0;
  61. for (i = 0; i < 32; i++)
  62. {
  63. rv |= prng_bit(prng);
  64. rv <<= 1;
  65. }
  66. return rv;
  67. }
  68. const char *
  69. prng_fuzz(struct prng *prng,
  70. const char *string,
  71. const char *charset,
  72. unsigned int operations)
  73. {
  74. static char buf[256];
  75. unsigned int charset_len;
  76. unsigned int i;
  77. unsigned int offset;
  78. unsigned int op;
  79. unsigned int character;
  80. assert(strlen(string) < sizeof(buf));
  81. strncpy(buf, string, sizeof(buf));
  82. charset_len = strlen(charset);
  83. for (i = 0; i < operations; i++)
  84. {
  85. offset = prng_rand(prng) % strlen(buf);
  86. op = prng_rand(prng) % 3;
  87. switch (op)
  88. {
  89. case 0:
  90. /* replace */
  91. character = prng_rand(prng) % charset_len;
  92. buf[offset] = charset[character];
  93. break;
  94. case 1:
  95. /* remove */
  96. memmove(buf + offset, buf + offset + 1, strlen(buf) - offset);
  97. break;
  98. case 2:
  99. /* insert */
  100. assert(strlen(buf) + 1 < sizeof(buf));
  101. memmove(buf + offset + 1, buf + offset, strlen(buf) + 1 - offset);
  102. character = prng_rand(prng) % charset_len;
  103. buf[offset] = charset[character];
  104. break;
  105. }
  106. }
  107. return buf;
  108. }
  109. void
  110. prng_free(struct prng *prng)
  111. {
  112. free(prng);
  113. }