]>
git.proxmox.com Git - mirror_frr.git/blob - tests/helpers/c/prng.c
2 * Very simple prng to allow for randomized tests with reproducable
5 * Copyright (C) 2012 by Open Source Routing.
6 * Copyright (C) 2012 by Internet Systems Consortium, Inc. ("ISC")
7 * Copyright (C) 2017 Christian Franke
9 * This file is part of Quagga
11 * Quagga is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2, or (at your option) any
16 * Quagga is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public License along
22 * with this program; see the file COPYING; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
38 struct prng
*prng_new(unsigned long long seed
)
40 struct prng
*rv
= calloc(sizeof(*rv
), 1);
49 * This implementation has originally been provided to musl libc by
50 * Szabolcs Nagy <nsz at port70 dot net> in 2013 under the terms of
52 * It is a simple LCG which D.E. Knuth attributes to C.E. Haynes in
55 int prng_rand(struct prng
*prng
)
57 prng
->state
= 6364136223846793005ULL * prng
->state
+ 1;
58 return prng
->state
>> 33;
61 const char *prng_fuzz(struct prng
*prng
, const char *string
,
62 const char *charset
, unsigned int operations
)
65 unsigned int charset_len
;
69 unsigned int character
;
71 assert(strlen(string
) < sizeof(buf
));
73 strncpy(buf
, string
, sizeof(buf
));
74 charset_len
= strlen(charset
);
76 for (i
= 0; i
< operations
; i
++) {
77 offset
= prng_rand(prng
) % strlen(buf
);
78 op
= prng_rand(prng
) % 3;
83 character
= prng_rand(prng
) % charset_len
;
84 buf
[offset
] = charset
[character
];
88 memmove(buf
+ offset
, buf
+ offset
+ 1,
89 strlen(buf
) - offset
);
93 assert(strlen(buf
) + 1 < sizeof(buf
));
95 memmove(buf
+ offset
+ 1, buf
+ offset
,
96 strlen(buf
) + 1 - offset
);
97 character
= prng_rand(prng
) % charset_len
;
98 buf
[offset
] = charset
[character
];
105 void prng_free(struct prng
*prng
)