xref: /freebsd/contrib/expat/lib/random_getentropy.c (revision e3935639d8d8b6556cad18e1c90e419a65f26b40)
1*e3935639SPhilip Paeps /*
2*e3935639SPhilip Paeps                             __  __            _
3*e3935639SPhilip Paeps                          ___\ \/ /_ __   __ _| |_
4*e3935639SPhilip Paeps                         / _ \\  /| '_ \ / _` | __|
5*e3935639SPhilip Paeps                        |  __//  \| |_) | (_| | |_
6*e3935639SPhilip Paeps                         \___/_/\_\ .__/ \__,_|\__|
7*e3935639SPhilip Paeps                                  |_| XML parser
8*e3935639SPhilip Paeps 
9*e3935639SPhilip Paeps    Copyright (c) 2026 Sebastian Pipping <sebastian@pipping.org>
10*e3935639SPhilip Paeps    Licensed under the MIT license:
11*e3935639SPhilip Paeps 
12*e3935639SPhilip Paeps    Permission is  hereby granted,  free of charge,  to any  person obtaining
13*e3935639SPhilip Paeps    a  copy  of  this  software   and  associated  documentation  files  (the
14*e3935639SPhilip Paeps    "Software"),  to  deal in  the  Software  without restriction,  including
15*e3935639SPhilip Paeps    without  limitation the  rights  to use,  copy,  modify, merge,  publish,
16*e3935639SPhilip Paeps    distribute, sublicense, and/or sell copies of the Software, and to permit
17*e3935639SPhilip Paeps    persons  to whom  the Software  is  furnished to  do so,  subject to  the
18*e3935639SPhilip Paeps    following conditions:
19*e3935639SPhilip Paeps 
20*e3935639SPhilip Paeps    The above copyright  notice and this permission notice  shall be included
21*e3935639SPhilip Paeps    in all copies or substantial portions of the Software.
22*e3935639SPhilip Paeps 
23*e3935639SPhilip Paeps    THE  SOFTWARE  IS  PROVIDED  "AS  IS",  WITHOUT  WARRANTY  OF  ANY  KIND,
24*e3935639SPhilip Paeps    EXPRESS  OR IMPLIED,  INCLUDING  BUT  NOT LIMITED  TO  THE WARRANTIES  OF
25*e3935639SPhilip Paeps    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
26*e3935639SPhilip Paeps    NO EVENT SHALL THE AUTHORS OR  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
27*e3935639SPhilip Paeps    DAMAGES OR  OTHER LIABILITY, WHETHER  IN AN  ACTION OF CONTRACT,  TORT OR
28*e3935639SPhilip Paeps    OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
29*e3935639SPhilip Paeps    USE OR OTHER DEALINGS IN THE SOFTWARE.
30*e3935639SPhilip Paeps */
31*e3935639SPhilip Paeps 
32*e3935639SPhilip Paeps #include "random_getentropy.h"
33*e3935639SPhilip Paeps 
34*e3935639SPhilip Paeps // NOTE: Please keep this block in sync with its two siblings in files
35*e3935639SPhilip Paeps //       `configure.ac` and `ConfigureChecks.cmake`!
36*e3935639SPhilip Paeps #if defined(__APPLE__)
37*e3935639SPhilip Paeps #  include <sys/random.h>
38*e3935639SPhilip Paeps #else
39*e3935639SPhilip Paeps #  if defined(__GLIBC__) && ! defined(_DEFAULT_SOURCE)
40*e3935639SPhilip Paeps #    define _DEFAULT_SOURCE 1
41*e3935639SPhilip Paeps #  endif
42*e3935639SPhilip Paeps #  if ! defined(_GNU_SOURCE)
43*e3935639SPhilip Paeps #    define _GNU_SOURCE 1 /* for musl */
44*e3935639SPhilip Paeps #  endif
45*e3935639SPhilip Paeps #  include <unistd.h>
46*e3935639SPhilip Paeps #endif // ! defined(__APPLE__)
47*e3935639SPhilip Paeps 
48*e3935639SPhilip Paeps #include <errno.h>
49*e3935639SPhilip Paeps 
50*e3935639SPhilip Paeps bool
writeRandomBytes_getentropy(void * target,size_t count)51*e3935639SPhilip Paeps writeRandomBytes_getentropy(void *target, size_t count) {
52*e3935639SPhilip Paeps   errno = 0;
53*e3935639SPhilip Paeps   return getentropy(target, count) == 0;
54*e3935639SPhilip Paeps }
55