/*
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  Author: g0tsu
 *  Email:  g0tsu at dnmx.0rg
 */

#include <assert.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/syscall.h>

#define SUCCESS 1
#define FAILURE 0

int lc_random_bytes(void *bytes, size_t size) {
  size_t read_bytes = 0;
  ssize_t n;
  /* Linux getrandom(2) syscall or FreeBSD getrandom(2) function*/
  /* Keep reading until we get enough entropy */
  while (read_bytes < size) {
  /* Below, (bytes + read_bytes)  is pointer arithmetic.

       bytes   read_bytes  size
         |      |           |
        [#######=============] (we're going to write over the = region)
                 \\\\\\\\\\\\\
                  amount_to_read
  */

  size_t amount_to_read = size - read_bytes;
  n = syscall(SYS_getrandom, bytes + read_bytes, amount_to_read, 0);

  if (n == -1) {
    if (errno == ENOSYS) {
      /* This can happen if PHP was compiled against a newer kernel where getrandom()
       * is available, but then runs on an older kernel without getrandom(). If this
       * happens we simply fall back to reading from /dev/urandom. */
      assert(read_bytes == 0);
      break;
    } else if (errno == EINTR || errno == EAGAIN) {
      /* Try again */
      continue;
    } else {
        /* If the syscall fails, fall back to reading from /dev/urandom */
      break;
    }
  }

    read_bytes += (size_t) n;
  }

  if (read_bytes < size) {
    int    fd;
    struct stat st;

  if (fd < 0) {

#if HAVE_DEV_URANDOM
  fd = open("/dev/urandom", O_RDONLY);
#else
  fd = open("/dev/random", O_RDONLY);
#endif
    if (fd < 0) {
      return FAILURE;
    }
    /* Does the file exist and is it a character device? */
    if (fstat(fd, &st) != 0 ||
# ifdef S_ISNAM
        !(S_ISNAM(st.st_mode) || S_ISCHR(st.st_mode))
# else
        !S_ISCHR(st.st_mode)
# endif
      ) {
        close(fd);
        return FAILURE;
      }
    }

    for (read_bytes = 0; read_bytes < size; read_bytes += (size_t) n) {
      n = read(fd, bytes + read_bytes, size - read_bytes);
      if (n <= 0) {
        break;
      }
    }

    if (read_bytes < size) {
      return FAILURE;
    }
  }

  return SUCCESS;
}
