mirror of
https://github.com/janet-lang/janet
synced 2025-01-24 22:26:52 +00:00
eliminate large stack allocation from arc4random_buf bodge
This commit is contained in:
parent
597d84e263
commit
f5d208d5d6
@ -72,15 +72,20 @@ void arc4random_buf(void *buf, size_t nbytes);
|
|||||||
|
|
||||||
/* arc4random_buf wasn't available in OS X until 10.7. */
|
/* arc4random_buf wasn't available in OS X until 10.7. */
|
||||||
#ifdef JANET_NO_ARC4RANDOM_BUF
|
#ifdef JANET_NO_ARC4RANDOM_BUF
|
||||||
/* Based on https://stackoverflow.com/a/12956868/558735 */
|
|
||||||
uint32_t arc4random(void);
|
uint32_t arc4random(void);
|
||||||
void arc4random_buf(void *buf, size_t nbytes) {
|
void arc4random_buf(void *buf, size_t nbytes) {
|
||||||
size_t entropy_len = (nbytes/4)+1;
|
uint32_t *buf_as_words = (uint32_t*)buf;
|
||||||
uint32_t entropy[entropy_len];
|
size_t nwords = nbytes / 4;
|
||||||
for (size_t i = 0; i < entropy_len; i++) {
|
for (size_t i=0; i < nwords; i++) {
|
||||||
entropy[i] = arc4random();
|
buf_as_words[i] = arc4random();
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t tail_len = nbytes % 4;
|
||||||
|
if (tail_len) {
|
||||||
|
uint8_t *tail = buf + nbytes - tail_len;
|
||||||
|
uint32_t rand = arc4random();
|
||||||
|
memcpy(tail, &rand, tail_len);
|
||||||
}
|
}
|
||||||
memcpy(buf, entropy, nbytes);
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user