1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-29 03:19:54 +00:00

Add support for systems missing arc4random_buf

This commit is contained in:
Jason Pepas 2020-06-26 14:34:13 -05:00
parent 977b0c3c0c
commit 597d84e263
2 changed files with 15 additions and 0 deletions

View File

@ -58,6 +58,7 @@
/* #define JANET_NO_REALPATH */ /* #define JANET_NO_REALPATH */
/* #define JANET_NO_SYMLINKS */ /* #define JANET_NO_SYMLINKS */
/* #define JANET_NO_UMASK */ /* #define JANET_NO_UMASK */
/* #define JANET_NO_ARC4RANDOM_BUF */
/* #define JANET_OUT_OF_MEMORY do { printf("janet out of memory\n"); exit(1); } while (0) */ /* #define JANET_OUT_OF_MEMORY do { printf("janet out of memory\n"); exit(1); } while (0) */
/* #define JANET_EXIT(msg) do { printf("C assert failed executing janet: %s\n", msg); exit(1); } while (0) */ /* #define JANET_EXIT(msg) do { printf("C assert failed executing janet: %s\n", msg); exit(1); } while (0) */
/* #define JANET_TOP_LEVEL_SIGNAL(msg) call_my_function((msg), stderr) */ /* #define JANET_TOP_LEVEL_SIGNAL(msg) call_my_function((msg), stderr) */

View File

@ -70,6 +70,20 @@ extern char **environ;
void arc4random_buf(void *buf, size_t nbytes); void arc4random_buf(void *buf, size_t nbytes);
#endif #endif
/* arc4random_buf wasn't available in OS X until 10.7. */
#ifdef JANET_NO_ARC4RANDOM_BUF
/* Based on https://stackoverflow.com/a/12956868/558735 */
uint32_t arc4random(void);
void arc4random_buf(void *buf, size_t nbytes) {
size_t entropy_len = (nbytes/4)+1;
uint32_t entropy[entropy_len];
for (size_t i = 0; i < entropy_len; i++) {
entropy[i] = arc4random();
}
memcpy(buf, entropy, nbytes);
}
#endif
/* Not POSIX, but all Unixes but Solaris have this function. */ /* Not POSIX, but all Unixes but Solaris have this function. */
#if defined(JANET_POSIX) && !defined(__sun) #if defined(JANET_POSIX) && !defined(__sun)
time_t timegm(struct tm *tm); time_t timegm(struct tm *tm);