]>
git.proxmox.com Git - mirror_frr.git/blob - tests/helpers/c/prng.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Very simple prng to allow for randomized tests with reproducable
6 * Copyright (C) 2012 by Open Source Routing.
7 * Copyright (C) 2012 by Internet Systems Consortium, Inc. ("ISC")
8 * Copyright (C) 2017 Christian Franke
10 * This file is part of Quagga
25 struct prng
*prng_new(unsigned long long seed
)
27 struct prng
*rv
= calloc(sizeof(*rv
), 1);
36 * This implementation has originally been provided to musl libc by
37 * Szabolcs Nagy <nsz at port70 dot net> in 2013 under the terms of
39 * It is a simple LCG which D.E. Knuth attributes to C.E. Haynes in
42 int prng_rand(struct prng
*prng
)
44 prng
->state
= 6364136223846793005ULL * prng
->state
+ 1;
45 return prng
->state
>> 33;
48 const char *prng_fuzz(struct prng
*prng
, const char *string
,
49 const char *charset
, unsigned int operations
)
52 unsigned int charset_len
;
56 unsigned int character
;
58 assert(strlen(string
) < sizeof(buf
));
60 strncpy(buf
, string
, sizeof(buf
));
61 charset_len
= strlen(charset
);
63 for (i
= 0; i
< operations
; i
++) {
64 offset
= prng_rand(prng
) % strlen(buf
);
65 op
= prng_rand(prng
) % 3;
70 character
= prng_rand(prng
) % charset_len
;
71 buf
[offset
] = charset
[character
];
75 memmove(buf
+ offset
, buf
+ offset
+ 1,
76 strlen(buf
) - offset
);
80 assert(strlen(buf
) + 1 < sizeof(buf
));
82 memmove(buf
+ offset
+ 1, buf
+ offset
,
83 strlen(buf
) + 1 - offset
);
84 character
= prng_rand(prng
) % charset_len
;
85 buf
[offset
] = charset
[character
];
92 void prng_free(struct prng
*prng
)