mirror of
https://github.com/openharmony/third_party_FreeBSD.git
synced 2026-07-01 08:11:39 -04:00
Add new libc-ndk interface
Issue: https://gitee.com/openharmony/interface_sdk_c/issues/I869YN Test:Build & Boot devices & xts Signed-off-by: zzulilyw <378305181@qq.com>
This commit is contained in:
@@ -101,6 +101,19 @@ please copy it to your project root dir and modify it refer to OpenHarmony/tools
|
||||
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
||||
" desc=""/>
|
||||
<licensetext name="
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
" desc=""/>
|
||||
</licensematcher>
|
||||
<licensematcher name="MIT" desc="">
|
||||
<licensetext name="The author of this software is David M. Gay." desc=""/>
|
||||
|
||||
@@ -0,0 +1,217 @@
|
||||
/* $NetBSD: backtrace.c,v 1.3 2013/08/29 14:58:56 christos Exp $ */
|
||||
|
||||
/*-
|
||||
* Copyright (c) 2012 The NetBSD Foundation, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to The NetBSD Foundation
|
||||
* by Christos Zoulas.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
||||
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
||||
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
||||
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#define _BSD_SOURCE
|
||||
#include <sys/cdefs.h>
|
||||
|
||||
#include <sys/param.h>
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <dlfcn.h>
|
||||
|
||||
#include "execinfo.h"
|
||||
|
||||
|
||||
static int rasprintf(char **buf, size_t *bufsiz, size_t offs, const char *fmt, ...)
|
||||
{
|
||||
for (;;) {
|
||||
size_t nbufsiz;
|
||||
char *nbuf;
|
||||
|
||||
if (*buf && offs < *bufsiz) {
|
||||
va_list ap;
|
||||
int len;
|
||||
|
||||
va_start(ap, fmt);
|
||||
len = vsnprintf(*buf + offs, *bufsiz - offs, fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
if (len < 0 || (size_t)len + 1 < *bufsiz - offs)
|
||||
return len;
|
||||
nbufsiz = MAX(*bufsiz + 512, (size_t)len + 1);
|
||||
} else
|
||||
nbufsiz = MAX(offs, *bufsiz) + 512;
|
||||
|
||||
nbuf = realloc(*buf, nbufsiz);
|
||||
if (nbuf == NULL)
|
||||
return -1;
|
||||
*buf = nbuf;
|
||||
*bufsiz = nbufsiz;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* format specifiers:
|
||||
* %a = address
|
||||
* %n = symbol_name
|
||||
* %d = symbol_address - address
|
||||
* %D = if symbol_address == address "" else +%d
|
||||
* %f = filename
|
||||
*/
|
||||
static ssize_t format_string(char **buf, size_t *bufsiz, size_t offs, const char *fmt,
|
||||
Dl_info *dli, const void *addr)
|
||||
{
|
||||
ptrdiff_t diff = (const char *)addr - (const char *)dli->dli_saddr;
|
||||
size_t o = offs;
|
||||
int len;
|
||||
|
||||
for (; *fmt; fmt++) {
|
||||
if (*fmt != '%')
|
||||
goto printone;
|
||||
switch (*++fmt) {
|
||||
case 'a':
|
||||
len = rasprintf(buf, bufsiz, o, "%p", addr);
|
||||
break;
|
||||
case 'n':
|
||||
len = rasprintf(buf, bufsiz, o, "%s", dli->dli_sname);
|
||||
break;
|
||||
case 'D':
|
||||
if (diff)
|
||||
len = rasprintf(buf, bufsiz, o, "+0x%tx", diff);
|
||||
else
|
||||
len = 0;
|
||||
break;
|
||||
case 'd':
|
||||
len = rasprintf(buf, bufsiz, o, "0x%tx", diff);
|
||||
break;
|
||||
case 'f':
|
||||
len = rasprintf(buf, bufsiz, o, "%s", dli->dli_fname);
|
||||
break;
|
||||
default:
|
||||
printone:
|
||||
len = rasprintf(buf, bufsiz, o, "%c", *fmt);
|
||||
break;
|
||||
}
|
||||
if (len == -1)
|
||||
return -1;
|
||||
o += len;
|
||||
}
|
||||
return o - offs;
|
||||
}
|
||||
|
||||
static ssize_t format_address(char **buf, size_t *bufsiz, size_t offs,
|
||||
const char *fmt, const void *addr)
|
||||
{
|
||||
Dl_info dli;
|
||||
|
||||
memset(&dli, 0, sizeof(dli));
|
||||
(void)dladdr(addr, &dli);
|
||||
|
||||
if (dli.dli_sname == NULL)
|
||||
dli.dli_sname = "???";
|
||||
if (dli.dli_fname == NULL)
|
||||
dli.dli_fname = "???";
|
||||
if (dli.dli_saddr == NULL)
|
||||
dli.dli_saddr = (void *)(intptr_t)addr;
|
||||
|
||||
return format_string(buf, bufsiz, offs, fmt, &dli, addr);
|
||||
}
|
||||
|
||||
char **backtrace_symbols_fmt(void *const *trace, size_t len, const char *fmt)
|
||||
{
|
||||
|
||||
static const size_t slen = sizeof(char *) + 64; /* estimate */
|
||||
char *ptr;
|
||||
|
||||
if ((ptr = calloc(len, slen)) == NULL)
|
||||
goto out;
|
||||
|
||||
size_t psize = len * slen;
|
||||
size_t offs = len * sizeof(char *);
|
||||
|
||||
/* We store only offsets in the first pass because of realloc */
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
ssize_t x;
|
||||
((char **)(void *)ptr)[i] = (void *)offs;
|
||||
x = format_address(&ptr, &psize, offs, fmt, trace[i]);
|
||||
if (x == -1) {
|
||||
free(ptr);
|
||||
ptr = NULL;
|
||||
goto out;
|
||||
}
|
||||
offs += x;
|
||||
ptr[offs++] = '\0';
|
||||
assert(offs < psize);
|
||||
}
|
||||
|
||||
/* Change offsets to pointers */
|
||||
for (size_t j = 0; j < len; j++)
|
||||
((char **)(void *)ptr)[j] += (intptr_t)ptr;
|
||||
|
||||
out:
|
||||
return (void *)ptr;
|
||||
}
|
||||
|
||||
int backtrace_symbols_fd_fmt(void *const *trace, size_t len, int fd,
|
||||
const char *fmt)
|
||||
{
|
||||
char **s = backtrace_symbols_fmt(trace, len, fmt);
|
||||
if (s == NULL)
|
||||
return -1;
|
||||
for (size_t i = 0; i < len; i++)
|
||||
if (dprintf(fd, "%s\n", s[i]) < 0)
|
||||
break;
|
||||
free(s);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const char fmt[] = "%a <%n%D> at %f";
|
||||
|
||||
char **backtrace_symbols(void *const *trace, size_t len)
|
||||
{
|
||||
return backtrace_symbols_fmt(trace, len, fmt);
|
||||
}
|
||||
|
||||
int backtrace_symbols_fd(void *const *trace, size_t len, int fd)
|
||||
{
|
||||
return backtrace_symbols_fd_fmt(trace, len, fd, fmt);
|
||||
}
|
||||
|
||||
void print_backtrace() {
|
||||
void *bt[10];
|
||||
int size = backtrace(bt, 10);
|
||||
|
||||
for (int i = 0; i < size; i++) {
|
||||
Dl_info info;
|
||||
dladdr(bt[i], &info);
|
||||
if (info.dli_sname) {
|
||||
printf("Address: %p, Symbol: %s\n", bt[i], info.dli_sname);
|
||||
} else {
|
||||
printf("Address: %p\n", bt[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
/* $NetBSD: execinfo.h,v 1.2 2012/06/09 21:22:17 christos Exp $ */
|
||||
/* $FreeBSD$ */
|
||||
|
||||
/*-
|
||||
* Copyright (c) 2012 The NetBSD Foundation, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to The NetBSD Foundation
|
||||
* by Christos Zoulas.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
||||
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
||||
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
||||
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#ifndef _EXECINFO_H_
|
||||
#define _EXECINFO_H_
|
||||
|
||||
#include <stddef.h>
|
||||
#include <sys/cdefs.h>
|
||||
|
||||
__BEGIN_DECLS
|
||||
size_t backtrace(void **, size_t);
|
||||
char **backtrace_symbols(void *const *, size_t);
|
||||
int backtrace_symbols_fd(void *const *, size_t, int);
|
||||
char **backtrace_symbols_fmt(void *const *, size_t, const char *);
|
||||
int backtrace_symbols_fd_fmt(void *const *, size_t, int, const char *);
|
||||
__END_DECLS
|
||||
|
||||
#endif /* _EXECINFO_H_ */
|
||||
@@ -0,0 +1,103 @@
|
||||
/* $NetBSD: unwind.c,v 1.3 2019/01/30 22:46:49 mrg Exp $ */
|
||||
|
||||
/*-
|
||||
* Copyright (c) 2012 The NetBSD Foundation, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to The NetBSD Foundation
|
||||
* by Christos Zoulas.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
||||
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
||||
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
||||
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#include <sys/cdefs.h>
|
||||
#include <sys/types.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "execinfo.h"
|
||||
|
||||
struct tracer_context {
|
||||
void **arr;
|
||||
size_t len;
|
||||
size_t n;
|
||||
};
|
||||
|
||||
typedef enum {
|
||||
_URC_NO_REASON = 0,
|
||||
#if defined(__arm__) && !defined(__USING_SJLJ_EXCEPTIONS__) && \
|
||||
!defined(__ARM_DWARF_EH__) && !defined(__SEH__)
|
||||
_URC_OK = 0, /* used by ARM EHABI */
|
||||
#endif
|
||||
_URC_FOREIGN_EXCEPTION_CAUGHT = 1,
|
||||
|
||||
_URC_FATAL_PHASE2_ERROR = 2,
|
||||
_URC_FATAL_PHASE1_ERROR = 3,
|
||||
_URC_NORMAL_STOP = 4,
|
||||
|
||||
_URC_END_OF_STACK = 5,
|
||||
_URC_HANDLER_FOUND = 6,
|
||||
_URC_INSTALL_CONTEXT = 7,
|
||||
_URC_CONTINUE_UNWIND = 8,
|
||||
#if defined(__arm__) && !defined(__USING_SJLJ_EXCEPTIONS__) && \
|
||||
!defined(__ARM_DWARF_EH__) && !defined(__SEH__)
|
||||
_URC_FAILURE = 9 /* used by ARM EHABI */
|
||||
#endif
|
||||
} _Unwind_Reason_Code;
|
||||
|
||||
struct _Unwind_Context;
|
||||
typedef unsigned int _Unwind_Word __attribute__((__mode__(__unwind_word__)));
|
||||
|
||||
_Unwind_Word _Unwind_GetGR(struct _Unwind_Context *, int);
|
||||
_Unwind_Word _Unwind_GetIP(struct _Unwind_Context *);
|
||||
typedef _Unwind_Reason_Code (*_Unwind_Trace_Fn)(struct _Unwind_Context *,
|
||||
void *);
|
||||
extern _Unwind_Reason_Code _Unwind_Backtrace (_Unwind_Trace_Fn, void *);
|
||||
static _Unwind_Reason_Code
|
||||
tracer(struct _Unwind_Context *ctx, void *arg)
|
||||
{
|
||||
struct tracer_context *t = arg;
|
||||
if (t->n == (size_t)~0) {
|
||||
/* Skip backtrace frame */
|
||||
t->n = 0;
|
||||
return 0;
|
||||
}
|
||||
if (t->n < t->len)
|
||||
t->arr[t->n++] = (void *)_Unwind_GetIP(ctx);
|
||||
else
|
||||
return _URC_END_OF_STACK;
|
||||
return _URC_NO_REASON;
|
||||
}
|
||||
|
||||
size_t
|
||||
backtrace(void **arr, size_t len)
|
||||
{
|
||||
struct tracer_context ctx;
|
||||
|
||||
ctx.arr = arr;
|
||||
ctx.len = len;
|
||||
ctx.n = (size_t)~0;
|
||||
|
||||
_Unwind_Backtrace(tracer, &ctx);
|
||||
if (ctx.n == (size_t)~0)
|
||||
ctx.n = 0;
|
||||
|
||||
return ctx.n;
|
||||
}
|
||||
@@ -0,0 +1,226 @@
|
||||
/* OPENBSD ORIGINAL: lib/libc/crypt/chacha_private.h */
|
||||
|
||||
/*
|
||||
chacha-merged.c version 20080118
|
||||
D. J. Bernstein
|
||||
Public domain.
|
||||
*/
|
||||
|
||||
/* $OpenBSD: chacha_private.h,v 1.3 2022/02/28 21:56:29 dtucker Exp $ */
|
||||
|
||||
typedef unsigned char u8;
|
||||
typedef unsigned int u32;
|
||||
typedef unsigned int u_int;
|
||||
typedef unsigned char u_char;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
u32 input[16]; /* could be compressed */
|
||||
} chacha_ctx;
|
||||
|
||||
#define U8C(v) (v##U)
|
||||
#define U32C(v) (v##U)
|
||||
|
||||
#define U8V(v) ((u8)(v) & U8C(0xFF))
|
||||
#define U32V(v) ((u32)(v) & U32C(0xFFFFFFFF))
|
||||
|
||||
#define ROTL32(v, n) \
|
||||
(U32V((v) << (n)) | ((v) >> (32 - (n))))
|
||||
|
||||
#define U8TO32_LITTLE(p) \
|
||||
(((u32)((p)[0]) ) | \
|
||||
((u32)((p)[1]) << 8) | \
|
||||
((u32)((p)[2]) << 16) | \
|
||||
((u32)((p)[3]) << 24))
|
||||
|
||||
#define U32TO8_LITTLE(p, v) \
|
||||
do { \
|
||||
(p)[0] = U8V((v) ); \
|
||||
(p)[1] = U8V((v) >> 8); \
|
||||
(p)[2] = U8V((v) >> 16); \
|
||||
(p)[3] = U8V((v) >> 24); \
|
||||
} while (0)
|
||||
|
||||
#define ROTATE(v,c) (ROTL32(v,c))
|
||||
#define XOR(v,w) ((v) ^ (w))
|
||||
#define PLUS(v,w) (U32V((v) + (w)))
|
||||
#define PLUSONE(v) (PLUS((v),1))
|
||||
|
||||
#define QUARTERROUND(a,b,c,d) \
|
||||
a = PLUS(a,b); d = ROTATE(XOR(d,a),16); \
|
||||
c = PLUS(c,d); b = ROTATE(XOR(b,c),12); \
|
||||
a = PLUS(a,b); d = ROTATE(XOR(d,a), 8); \
|
||||
c = PLUS(c,d); b = ROTATE(XOR(b,c), 7);
|
||||
|
||||
static const char sigma[16] = "expand 32-byte k";
|
||||
static const char tau[16] = "expand 16-byte k";
|
||||
|
||||
static void
|
||||
chacha_keysetup(chacha_ctx *x,const u8 *k,u32 kbits)
|
||||
{
|
||||
const char *constants;
|
||||
|
||||
x->input[4] = U8TO32_LITTLE(k + 0);
|
||||
x->input[5] = U8TO32_LITTLE(k + 4);
|
||||
x->input[6] = U8TO32_LITTLE(k + 8);
|
||||
x->input[7] = U8TO32_LITTLE(k + 12);
|
||||
if (kbits == 256) { /* recommended */
|
||||
k += 16;
|
||||
constants = sigma;
|
||||
} else { /* kbits == 128 */
|
||||
constants = tau;
|
||||
}
|
||||
x->input[8] = U8TO32_LITTLE(k + 0);
|
||||
x->input[9] = U8TO32_LITTLE(k + 4);
|
||||
x->input[10] = U8TO32_LITTLE(k + 8);
|
||||
x->input[11] = U8TO32_LITTLE(k + 12);
|
||||
x->input[0] = U8TO32_LITTLE(constants + 0);
|
||||
x->input[1] = U8TO32_LITTLE(constants + 4);
|
||||
x->input[2] = U8TO32_LITTLE(constants + 8);
|
||||
x->input[3] = U8TO32_LITTLE(constants + 12);
|
||||
}
|
||||
|
||||
static void
|
||||
chacha_ivsetup(chacha_ctx *x,const u8 *iv)
|
||||
{
|
||||
x->input[12] = 0;
|
||||
x->input[13] = 0;
|
||||
x->input[14] = U8TO32_LITTLE(iv + 0);
|
||||
x->input[15] = U8TO32_LITTLE(iv + 4);
|
||||
}
|
||||
|
||||
static void
|
||||
chacha_encrypt_bytes(chacha_ctx *x,const u8 *m,u8 *c,u32 bytes)
|
||||
{
|
||||
u32 x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15;
|
||||
u32 j0, j1, j2, j3, j4, j5, j6, j7, j8, j9, j10, j11, j12, j13, j14, j15;
|
||||
u8 *ctarget = NULL;
|
||||
u8 tmp[64];
|
||||
u_int i;
|
||||
|
||||
if (!bytes) return;
|
||||
|
||||
j0 = x->input[0];
|
||||
j1 = x->input[1];
|
||||
j2 = x->input[2];
|
||||
j3 = x->input[3];
|
||||
j4 = x->input[4];
|
||||
j5 = x->input[5];
|
||||
j6 = x->input[6];
|
||||
j7 = x->input[7];
|
||||
j8 = x->input[8];
|
||||
j9 = x->input[9];
|
||||
j10 = x->input[10];
|
||||
j11 = x->input[11];
|
||||
j12 = x->input[12];
|
||||
j13 = x->input[13];
|
||||
j14 = x->input[14];
|
||||
j15 = x->input[15];
|
||||
|
||||
for (;;) {
|
||||
if (bytes < 64) {
|
||||
for (i = 0;i < bytes;++i) tmp[i] = m[i];
|
||||
m = tmp;
|
||||
ctarget = c;
|
||||
c = tmp;
|
||||
}
|
||||
x0 = j0;
|
||||
x1 = j1;
|
||||
x2 = j2;
|
||||
x3 = j3;
|
||||
x4 = j4;
|
||||
x5 = j5;
|
||||
x6 = j6;
|
||||
x7 = j7;
|
||||
x8 = j8;
|
||||
x9 = j9;
|
||||
x10 = j10;
|
||||
x11 = j11;
|
||||
x12 = j12;
|
||||
x13 = j13;
|
||||
x14 = j14;
|
||||
x15 = j15;
|
||||
for (i = 20;i > 0;i -= 2) {
|
||||
QUARTERROUND( x0, x4, x8,x12)
|
||||
QUARTERROUND( x1, x5, x9,x13)
|
||||
QUARTERROUND( x2, x6,x10,x14)
|
||||
QUARTERROUND( x3, x7,x11,x15)
|
||||
QUARTERROUND( x0, x5,x10,x15)
|
||||
QUARTERROUND( x1, x6,x11,x12)
|
||||
QUARTERROUND( x2, x7, x8,x13)
|
||||
QUARTERROUND( x3, x4, x9,x14)
|
||||
}
|
||||
x0 = PLUS(x0,j0);
|
||||
x1 = PLUS(x1,j1);
|
||||
x2 = PLUS(x2,j2);
|
||||
x3 = PLUS(x3,j3);
|
||||
x4 = PLUS(x4,j4);
|
||||
x5 = PLUS(x5,j5);
|
||||
x6 = PLUS(x6,j6);
|
||||
x7 = PLUS(x7,j7);
|
||||
x8 = PLUS(x8,j8);
|
||||
x9 = PLUS(x9,j9);
|
||||
x10 = PLUS(x10,j10);
|
||||
x11 = PLUS(x11,j11);
|
||||
x12 = PLUS(x12,j12);
|
||||
x13 = PLUS(x13,j13);
|
||||
x14 = PLUS(x14,j14);
|
||||
x15 = PLUS(x15,j15);
|
||||
|
||||
#ifndef KEYSTREAM_ONLY
|
||||
x0 = XOR(x0,U8TO32_LITTLE(m + 0));
|
||||
x1 = XOR(x1,U8TO32_LITTLE(m + 4));
|
||||
x2 = XOR(x2,U8TO32_LITTLE(m + 8));
|
||||
x3 = XOR(x3,U8TO32_LITTLE(m + 12));
|
||||
x4 = XOR(x4,U8TO32_LITTLE(m + 16));
|
||||
x5 = XOR(x5,U8TO32_LITTLE(m + 20));
|
||||
x6 = XOR(x6,U8TO32_LITTLE(m + 24));
|
||||
x7 = XOR(x7,U8TO32_LITTLE(m + 28));
|
||||
x8 = XOR(x8,U8TO32_LITTLE(m + 32));
|
||||
x9 = XOR(x9,U8TO32_LITTLE(m + 36));
|
||||
x10 = XOR(x10,U8TO32_LITTLE(m + 40));
|
||||
x11 = XOR(x11,U8TO32_LITTLE(m + 44));
|
||||
x12 = XOR(x12,U8TO32_LITTLE(m + 48));
|
||||
x13 = XOR(x13,U8TO32_LITTLE(m + 52));
|
||||
x14 = XOR(x14,U8TO32_LITTLE(m + 56));
|
||||
x15 = XOR(x15,U8TO32_LITTLE(m + 60));
|
||||
#endif
|
||||
|
||||
j12 = PLUSONE(j12);
|
||||
if (!j12) {
|
||||
j13 = PLUSONE(j13);
|
||||
/* stopping at 2^70 bytes per nonce is user's responsibility */
|
||||
}
|
||||
|
||||
U32TO8_LITTLE(c + 0,x0);
|
||||
U32TO8_LITTLE(c + 4,x1);
|
||||
U32TO8_LITTLE(c + 8,x2);
|
||||
U32TO8_LITTLE(c + 12,x3);
|
||||
U32TO8_LITTLE(c + 16,x4);
|
||||
U32TO8_LITTLE(c + 20,x5);
|
||||
U32TO8_LITTLE(c + 24,x6);
|
||||
U32TO8_LITTLE(c + 28,x7);
|
||||
U32TO8_LITTLE(c + 32,x8);
|
||||
U32TO8_LITTLE(c + 36,x9);
|
||||
U32TO8_LITTLE(c + 40,x10);
|
||||
U32TO8_LITTLE(c + 44,x11);
|
||||
U32TO8_LITTLE(c + 48,x12);
|
||||
U32TO8_LITTLE(c + 52,x13);
|
||||
U32TO8_LITTLE(c + 56,x14);
|
||||
U32TO8_LITTLE(c + 60,x15);
|
||||
|
||||
if (bytes <= 64) {
|
||||
if (bytes < 64) {
|
||||
for (i = 0;i < bytes;++i) ctarget[i] = c[i];
|
||||
}
|
||||
x->input[12] = j12;
|
||||
x->input[13] = j13;
|
||||
return;
|
||||
}
|
||||
bytes -= 64;
|
||||
c += 64;
|
||||
#ifndef KEYSTREAM_ONLY
|
||||
m += 64;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,255 @@
|
||||
/*
|
||||
* Copyright (c) 1996, David Mazieres <dm@uun.org>
|
||||
* Copyright (c) 2008, Damien Miller <djm@openbsd.org>
|
||||
* Copyright (c) 2013, Markus Friedl <markus@openbsd.org>
|
||||
* Copyright (c) 2014, Theo de Raadt <deraadt@openbsd.org>
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
/*
|
||||
* ChaCha based random number generator for OpenBSD.
|
||||
*/
|
||||
#define _BSD_SOURCE
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
#if defined(__FreeBSD__)
|
||||
#include <assert.h>
|
||||
#endif
|
||||
#include <fcntl.h>
|
||||
#include <limits.h>
|
||||
#include <pthread.h>
|
||||
#include <signal.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
/*
|
||||
* Unfortunately, pthread_atfork() is broken on FreeBSD (at least 9 and 10) if
|
||||
* a program does not link to -lthr. Callbacks registered with pthread_atfork()
|
||||
* appear to fail silently. So, it is not always possible to detect a PID
|
||||
* wraparound.
|
||||
*/
|
||||
#define _ARC4_ATFORK(f) pthread_atfork(NULL, NULL, (f))
|
||||
|
||||
#define CHACHA_EMBED
|
||||
#define KEYSTREAM_ONLY
|
||||
#if defined(__FreeBSD__)
|
||||
#define ARC4RANDOM_FXRNG 1
|
||||
#else
|
||||
#define ARC4RANDOM_FXRNG 0
|
||||
#endif
|
||||
#include "chacha_private.h"
|
||||
|
||||
#define minimum(a, b) ((a) < (b) ? (a) : (b))
|
||||
|
||||
#if defined(__GNUC__) || defined(_MSC_VER)
|
||||
#define inline __inline
|
||||
#else /* __GNUC__ || _MSC_VER */
|
||||
#define inline
|
||||
#endif /* !__GNUC__ && !_MSC_VER */
|
||||
|
||||
#define KEYSZ 32
|
||||
#define IVSZ 8
|
||||
#define BLOCKSZ 64
|
||||
#define RSBUFSZ (16*BLOCKSZ)
|
||||
|
||||
#define REKEY_BASE (1024*1024) /* NB. should be a power of 2 */
|
||||
|
||||
/* Marked INHERIT_ZERO, so zero'd out in fork children. */
|
||||
static struct _rs {
|
||||
size_t rs_have; /* valid bytes at end of rs_buf */
|
||||
size_t rs_count; /* bytes till reseed */
|
||||
} *rs;
|
||||
|
||||
/* Maybe be preserved in fork children, if _rs_allocate() decides. */
|
||||
static struct _rsx {
|
||||
chacha_ctx rs_chacha; /* chacha context for random keystream */
|
||||
u_char rs_buf[RSBUFSZ]; /* keystream blocks */
|
||||
#ifdef __FreeBSD__
|
||||
uint32_t rs_seed_generation; /* 32-bit userspace RNG version */
|
||||
#endif
|
||||
} *rsx;
|
||||
|
||||
static inline int _rs_allocate(struct _rs **, struct _rsx **);
|
||||
static inline void _rs_forkdetect(void);
|
||||
#include "arc4random.h"
|
||||
|
||||
static inline void _rs_rekey(u_char *dat, size_t datlen);
|
||||
|
||||
static inline void
|
||||
_rs_init(u_char *buf, size_t n)
|
||||
{
|
||||
if (n < KEYSZ + IVSZ)
|
||||
return;
|
||||
|
||||
if (rs == NULL) {
|
||||
if (_rs_allocate(&rs, &rsx) == -1)
|
||||
_exit(1);
|
||||
}
|
||||
|
||||
chacha_keysetup(&rsx->rs_chacha, buf, KEYSZ * 8);
|
||||
chacha_ivsetup(&rsx->rs_chacha, buf + KEYSZ);
|
||||
}
|
||||
|
||||
static void
|
||||
_rs_stir(void)
|
||||
{
|
||||
u_char rnd[KEYSZ + IVSZ];
|
||||
uint32_t rekey_fuzz = 0;
|
||||
|
||||
#if defined(__FreeBSD__)
|
||||
bool need_init;
|
||||
|
||||
/*
|
||||
* De-couple allocation (which locates the vdso_fxrngp pointer in
|
||||
* auxinfo) from initialization. This allows us to read the root seed
|
||||
* version before we fetch system entropy, maintaining the invariant
|
||||
* that the PRF was seeded with entropy from rs_seed_generation or a
|
||||
* later generation. But never seeded from an earlier generation.
|
||||
* This invariant prevents us from missing a root reseed event.
|
||||
*/
|
||||
need_init = false;
|
||||
if (rs == NULL) {
|
||||
if (_rs_allocate(&rs, &rsx) == -1)
|
||||
abort();
|
||||
need_init = true;
|
||||
}
|
||||
/*
|
||||
* Transition period: new userspace on old kernel. This should become
|
||||
* a hard error at some point, if the scheme is adopted.
|
||||
*/
|
||||
if (vdso_fxrngp != NULL)
|
||||
rsx->rs_seed_generation =
|
||||
fxrng_load_acq_generation(&vdso_fxrngp->fx_generation32);
|
||||
#endif
|
||||
|
||||
if (getentropy(rnd, sizeof rnd) == -1)
|
||||
_getentropy_fail();
|
||||
|
||||
#if !defined(__FreeBSD__)
|
||||
if (!rs)
|
||||
_rs_init(rnd, sizeof(rnd));
|
||||
#else /* __FreeBSD__ */
|
||||
assert(rs != NULL);
|
||||
if (need_init)
|
||||
_rs_init(rnd, sizeof(rnd));
|
||||
#endif
|
||||
else
|
||||
_rs_rekey(rnd, sizeof(rnd));
|
||||
explicit_bzero(rnd, sizeof(rnd)); /* discard source seed */
|
||||
|
||||
/* invalidate rs_buf */
|
||||
rs->rs_have = 0;
|
||||
memset(rsx->rs_buf, 0, sizeof(rsx->rs_buf));
|
||||
|
||||
/* rekey interval should not be predictable */
|
||||
chacha_encrypt_bytes(&rsx->rs_chacha, (uint8_t *)&rekey_fuzz,
|
||||
(uint8_t *)&rekey_fuzz, sizeof(rekey_fuzz));
|
||||
rs->rs_count = REKEY_BASE + (rekey_fuzz % REKEY_BASE);
|
||||
}
|
||||
|
||||
static inline void
|
||||
_rs_stir_if_needed(size_t len)
|
||||
{
|
||||
_rs_forkdetect();
|
||||
if (!rs || rs->rs_count <= len)
|
||||
_rs_stir();
|
||||
if (rs->rs_count <= len)
|
||||
rs->rs_count = 0;
|
||||
else
|
||||
rs->rs_count -= len;
|
||||
}
|
||||
|
||||
static inline void
|
||||
_rs_rekey(u_char *dat, size_t datlen)
|
||||
{
|
||||
#ifndef KEYSTREAM_ONLY
|
||||
memset(rsx->rs_buf, 0, sizeof(rsx->rs_buf));
|
||||
#endif
|
||||
/* fill rs_buf with the keystream */
|
||||
chacha_encrypt_bytes(&rsx->rs_chacha, rsx->rs_buf,
|
||||
rsx->rs_buf, sizeof(rsx->rs_buf));
|
||||
/* mix in optional user provided data */
|
||||
if (dat) {
|
||||
size_t i, m;
|
||||
|
||||
m = minimum(datlen, KEYSZ + IVSZ);
|
||||
for (i = 0; i < m; i++)
|
||||
rsx->rs_buf[i] ^= dat[i];
|
||||
}
|
||||
/* immediately reinit for backtracking resistance */
|
||||
_rs_init(rsx->rs_buf, KEYSZ + IVSZ);
|
||||
memset(rsx->rs_buf, 0, KEYSZ + IVSZ);
|
||||
rs->rs_have = sizeof(rsx->rs_buf) - KEYSZ - IVSZ;
|
||||
}
|
||||
|
||||
static inline void
|
||||
_rs_random_buf(void *_buf, size_t n)
|
||||
{
|
||||
u_char *buf = (u_char *)_buf;
|
||||
u_char *keystream;
|
||||
size_t m;
|
||||
|
||||
_rs_stir_if_needed(n);
|
||||
while (n > 0) {
|
||||
if (rs->rs_have > 0) {
|
||||
m = minimum(n, rs->rs_have);
|
||||
keystream = rsx->rs_buf + sizeof(rsx->rs_buf)
|
||||
- rs->rs_have;
|
||||
memcpy(buf, keystream, m);
|
||||
memset(keystream, 0, m);
|
||||
buf += m;
|
||||
n -= m;
|
||||
rs->rs_have -= m;
|
||||
}
|
||||
if (rs->rs_have == 0)
|
||||
_rs_rekey(NULL, 0);
|
||||
}
|
||||
}
|
||||
|
||||
static inline void
|
||||
_rs_random_u32(uint32_t *val)
|
||||
{
|
||||
u_char *keystream;
|
||||
|
||||
_rs_stir_if_needed(sizeof(*val));
|
||||
if (rs->rs_have < sizeof(*val))
|
||||
_rs_rekey(NULL, 0);
|
||||
keystream = rsx->rs_buf + sizeof(rsx->rs_buf) - rs->rs_have;
|
||||
memcpy(val, keystream, sizeof(*val));
|
||||
memset(keystream, 0, sizeof(*val));
|
||||
rs->rs_have -= sizeof(*val);
|
||||
}
|
||||
|
||||
uint32_t
|
||||
arc4random(void)
|
||||
{
|
||||
uint32_t val;
|
||||
|
||||
_ARC4_LOCK();
|
||||
_rs_random_u32(&val);
|
||||
_ARC4_UNLOCK();
|
||||
return val;
|
||||
}
|
||||
|
||||
void
|
||||
arc4random_buf(void *buf, size_t n)
|
||||
{
|
||||
_ARC4_LOCK();
|
||||
_rs_random_buf(buf, n);
|
||||
_ARC4_UNLOCK();
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
* Copyright (c) 1996, David Mazieres <dm@uun.org>
|
||||
* Copyright (c) 2008, Damien Miller <djm@openbsd.org>
|
||||
* Copyright (c) 2013, Markus Friedl <markus@openbsd.org>
|
||||
* Copyright (c) 2014, Theo de Raadt <deraadt@openbsd.org>
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Stub functions for portability.
|
||||
*/
|
||||
#ifndef ARC4RANDOM_H
|
||||
#define ARC4RANDOM_H
|
||||
|
||||
#include <sys/mman.h>
|
||||
|
||||
#include <pthread.h>
|
||||
#include <signal.h>
|
||||
|
||||
static pthread_mutex_t arc4random_mtx = PTHREAD_MUTEX_INITIALIZER;
|
||||
#define _ARC4_LOCK() pthread_mutex_lock(&arc4random_mtx)
|
||||
#define _ARC4_UNLOCK() pthread_mutex_unlock(&arc4random_mtx)
|
||||
|
||||
/*
|
||||
* Unfortunately, pthread_atfork() is broken on FreeBSD (at least 9 and 10) if
|
||||
* a program does not link to -lthr. Callbacks registered with pthread_atfork()
|
||||
* appear to fail silently. So, it is not always possible to detect a PID
|
||||
* wraparound.
|
||||
*/
|
||||
#define _ARC4_ATFORK(f) pthread_atfork(NULL, NULL, (f))
|
||||
|
||||
static inline void
|
||||
_getentropy_fail(void)
|
||||
{
|
||||
raise(SIGKILL);
|
||||
}
|
||||
|
||||
static volatile sig_atomic_t _rs_forked;
|
||||
|
||||
static inline void
|
||||
_rs_forkhandler(void)
|
||||
{
|
||||
_rs_forked = 1;
|
||||
}
|
||||
|
||||
static inline void
|
||||
_rs_forkdetect(void)
|
||||
{
|
||||
static pid_t _rs_pid = 0;
|
||||
pid_t pid = getpid();
|
||||
|
||||
if (_rs_pid == 0 || _rs_pid != pid || _rs_forked) {
|
||||
_rs_pid = pid;
|
||||
_rs_forked = 0;
|
||||
if (rs)
|
||||
memset(rs, 0, sizeof(*rs));
|
||||
}
|
||||
}
|
||||
|
||||
static inline int
|
||||
_rs_allocate(struct _rs **rsp, struct _rsx **rsxp)
|
||||
{
|
||||
if ((*rsp = mmap(NULL, sizeof(**rsp), PROT_READ|PROT_WRITE,
|
||||
MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED)
|
||||
return (-1);
|
||||
|
||||
if ((*rsxp = mmap(NULL, sizeof(**rsxp), PROT_READ|PROT_WRITE,
|
||||
MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED) {
|
||||
munmap(*rsp, sizeof(**rsp));
|
||||
*rsp = NULL;
|
||||
return (-1);
|
||||
}
|
||||
|
||||
_ARC4_ATFORK(_rs_forkhandler);
|
||||
return (0);
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright (c) 2008, Damien Miller <djm@openbsd.org>
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/*
|
||||
* Calculate a uniformly distributed random number less than upper_bound
|
||||
* avoiding "modulo bias".
|
||||
*
|
||||
* Uniformity is achieved by generating new random numbers until the one
|
||||
* returned is outside the range [0, 2**32 % upper_bound). This
|
||||
* guarantees the selected random number will be inside
|
||||
* [2**32 % upper_bound, 2**32) which maps back to [0, upper_bound)
|
||||
* after reduction modulo upper_bound.
|
||||
*/
|
||||
uint32_t
|
||||
arc4random_uniform(uint32_t upper_bound)
|
||||
{
|
||||
uint32_t r, min;
|
||||
|
||||
if (upper_bound < 2)
|
||||
return 0;
|
||||
|
||||
/* 2**32 % x == (2**32 - x) % x */
|
||||
min = -upper_bound % upper_bound;
|
||||
|
||||
/*
|
||||
* This could theoretically loop forever but each retry has
|
||||
* p > 0.5 (worst case, usually far better) of selecting a
|
||||
* number inside the range we need, so it should rarely need
|
||||
* to re-roll.
|
||||
*/
|
||||
for (;;) {
|
||||
r = arc4random();
|
||||
if (r >= min)
|
||||
break;
|
||||
}
|
||||
|
||||
return r % upper_bound;
|
||||
}
|
||||
@@ -0,0 +1,273 @@
|
||||
/*-
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*
|
||||
* Copyright (c) 2001 Daniel Eischen <deischen@FreeBSD.org>.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef _NAMESPACE_H_
|
||||
#define _NAMESPACE_H_
|
||||
|
||||
/*
|
||||
* Adjust names so that headers declare "hidden" names.
|
||||
*
|
||||
* README: When modifying this file don't forget to make the appropriate
|
||||
* changes in un-namespace.h!!!
|
||||
*/
|
||||
|
||||
/*
|
||||
* ISO C (C90) section. Most names in libc aren't in ISO C, so they
|
||||
* should be here. Most aren't here...
|
||||
*/
|
||||
#define err _err
|
||||
#define warn _warn
|
||||
#define nsdispatch _nsdispatch
|
||||
|
||||
/*
|
||||
* Prototypes for syscalls/functions that need to be overridden
|
||||
* in libc_r/libpthread.
|
||||
*/
|
||||
#define accept _accept
|
||||
#define __acl_aclcheck_fd ___acl_aclcheck_fd
|
||||
#define __acl_delete_fd ___acl_delete_fd
|
||||
#define __acl_get_fd ___acl_get_fd
|
||||
#define __acl_set_fd ___acl_set_fd
|
||||
#define bind _bind
|
||||
#define __cap_get_fd ___cap_get_fd
|
||||
#define __cap_set_fd ___cap_set_fd
|
||||
#define clock_nanosleep _clock_nanosleep
|
||||
#define close _close
|
||||
#define connect _connect
|
||||
#define dup _dup
|
||||
#define dup2 _dup2
|
||||
#define execve _execve
|
||||
#define fcntl _fcntl
|
||||
/*#define flock _flock */
|
||||
#define flockfile _flockfile
|
||||
#define fpathconf _fpathconf
|
||||
#define fstat _fstat
|
||||
#define fstatfs _fstatfs
|
||||
#define fsync _fsync
|
||||
#define funlockfile _funlockfile
|
||||
#define getdirentries _getdirentries
|
||||
#define getlogin _getlogin
|
||||
#define getpeername _getpeername
|
||||
#define getprogname _getprogname
|
||||
#define getsockname _getsockname
|
||||
#define getsockopt _getsockopt
|
||||
#define ioctl _ioctl
|
||||
/* #define kevent _kevent */
|
||||
#define listen _listen
|
||||
#define nanosleep _nanosleep
|
||||
#define open _open
|
||||
#define openat _openat
|
||||
#define poll _poll
|
||||
#define pthread_atfork _pthread_atfork
|
||||
#define pthread_attr_destroy _pthread_attr_destroy
|
||||
#define pthread_attr_get_np _pthread_attr_get_np
|
||||
#define pthread_attr_getaffinity_np _pthread_attr_getaffinity_np
|
||||
#define pthread_attr_getdetachstate _pthread_attr_getdetachstate
|
||||
#define pthread_attr_getguardsize _pthread_attr_getguardsize
|
||||
#define pthread_attr_getinheritsched _pthread_attr_getinheritsched
|
||||
#define pthread_attr_getschedparam _pthread_attr_getschedparam
|
||||
#define pthread_attr_getschedpolicy _pthread_attr_getschedpolicy
|
||||
#define pthread_attr_getscope _pthread_attr_getscope
|
||||
#define pthread_attr_getstack _pthread_attr_getstack
|
||||
#define pthread_attr_getstackaddr _pthread_attr_getstackaddr
|
||||
#define pthread_attr_getstacksize _pthread_attr_getstacksize
|
||||
#define pthread_attr_init _pthread_attr_init
|
||||
#define pthread_attr_setaffinity_np _pthread_attr_setaffinity_np
|
||||
#define pthread_attr_setcreatesuspend_np _pthread_attr_setcreatesuspend_np
|
||||
#define pthread_attr_setdetachstate _pthread_attr_setdetachstate
|
||||
#define pthread_attr_setguardsize _pthread_attr_setguardsize
|
||||
#define pthread_attr_setinheritsched _pthread_attr_setinheritsched
|
||||
#define pthread_attr_setschedparam _pthread_attr_setschedparam
|
||||
#define pthread_attr_setschedpolicy _pthread_attr_setschedpolicy
|
||||
#define pthread_attr_setscope _pthread_attr_setscope
|
||||
#define pthread_attr_setstack _pthread_attr_setstack
|
||||
#define pthread_attr_setstackaddr _pthread_attr_setstackaddr
|
||||
#define pthread_attr_setstacksize _pthread_attr_setstacksize
|
||||
#define pthread_barrier_destroy _pthread_barrier_destroy
|
||||
#define pthread_barrier_init _pthread_barrier_init
|
||||
#define pthread_barrier_wait _pthread_barrier_wait
|
||||
#define pthread_barrierattr_destroy _pthread_barrierattr_destroy
|
||||
#define pthread_barrierattr_getpshared _pthread_barrierattr_getpshared
|
||||
#define pthread_barrierattr_init _pthread_barrierattr_init
|
||||
#define pthread_barrierattr_setpshared _pthread_barrierattr_setpshared
|
||||
#define pthread_cancel _pthread_cancel
|
||||
#define pthread_cond_broadcast _pthread_cond_broadcast
|
||||
#define pthread_cond_destroy _pthread_cond_destroy
|
||||
#define pthread_cond_init _pthread_cond_init
|
||||
#define pthread_cond_signal _pthread_cond_signal
|
||||
#define pthread_cond_timedwait _pthread_cond_timedwait
|
||||
#define pthread_cond_wait _pthread_cond_wait
|
||||
#define pthread_condattr_destroy _pthread_condattr_destroy
|
||||
#define pthread_condattr_getclock _pthread_condattr_getclock
|
||||
#define pthread_condattr_getpshared _pthread_condattr_getpshared
|
||||
#define pthread_condattr_init _pthread_condattr_init
|
||||
#define pthread_condattr_setclock _pthread_condattr_setclock
|
||||
#define pthread_condattr_setpshared _pthread_condattr_setpshared
|
||||
#define pthread_create _pthread_create
|
||||
#define pthread_detach _pthread_detach
|
||||
#define pthread_equal _pthread_equal
|
||||
#define pthread_exit _pthread_exit
|
||||
#define pthread_get_name_np _pthread_get_name_np
|
||||
#define pthread_getaffinity_np _pthread_getaffinity_np
|
||||
#define pthread_getconcurrency _pthread_getconcurrency
|
||||
#define pthread_getcpuclockid _pthread_getcpuclockid
|
||||
#define pthread_getname_np _pthread_getname_np
|
||||
#define pthread_getprio _pthread_getprio
|
||||
#define pthread_getschedparam _pthread_getschedparam
|
||||
#define pthread_getspecific _pthread_getspecific
|
||||
#define pthread_getthreadid_np _pthread_getthreadid_np
|
||||
#define pthread_join _pthread_join
|
||||
#define pthread_key_create _pthread_key_create
|
||||
#define pthread_key_delete _pthread_key_delete
|
||||
#define pthread_kill _pthread_kill
|
||||
#define pthread_main_np _pthread_main_np
|
||||
#define pthread_multi_np _pthread_multi_np
|
||||
#define pthread_mutex_destroy _pthread_mutex_destroy
|
||||
#define pthread_mutex_getprioceiling _pthread_mutex_getprioceiling
|
||||
#define pthread_mutex_init _pthread_mutex_init
|
||||
#define pthread_mutex_isowned_np _pthread_mutex_isowned_np
|
||||
#define pthread_mutex_lock _pthread_mutex_lock
|
||||
#define pthread_mutex_setprioceiling _pthread_mutex_setprioceiling
|
||||
#define pthread_mutex_timedlock _pthread_mutex_timedlock
|
||||
#define pthread_mutex_trylock _pthread_mutex_trylock
|
||||
#define pthread_mutex_unlock _pthread_mutex_unlock
|
||||
#define pthread_mutexattr_destroy _pthread_mutexattr_destroy
|
||||
#define pthread_mutexattr_getkind_np _pthread_mutexattr_getkind_np
|
||||
#define pthread_mutexattr_getprioceiling _pthread_mutexattr_getprioceiling
|
||||
#define pthread_mutexattr_getprotocol _pthread_mutexattr_getprotocol
|
||||
#define pthread_mutexattr_getpshared _pthread_mutexattr_getpshared
|
||||
#define pthread_mutexattr_gettype _pthread_mutexattr_gettype
|
||||
#define pthread_mutexattr_init _pthread_mutexattr_init
|
||||
#define pthread_mutexattr_setkind_np _pthread_mutexattr_setkind_np
|
||||
#define pthread_mutexattr_setprioceiling _pthread_mutexattr_setprioceiling
|
||||
#define pthread_mutexattr_setprotocol _pthread_mutexattr_setprotocol
|
||||
#define pthread_mutexattr_setpshared _pthread_mutexattr_setpshared
|
||||
#define pthread_mutexattr_settype _pthread_mutexattr_settype
|
||||
#define pthread_once _pthread_once
|
||||
#define pthread_resume_all_np _pthread_resume_all_np
|
||||
#define pthread_resume_np _pthread_resume_np
|
||||
#define pthread_rwlock_destroy _pthread_rwlock_destroy
|
||||
#define pthread_rwlock_init _pthread_rwlock_init
|
||||
#define pthread_rwlock_rdlock _pthread_rwlock_rdlock
|
||||
#define pthread_rwlock_timedrdlock _pthread_rwlock_timedrdlock
|
||||
#define pthread_rwlock_timedwrlock _pthread_rwlock_timedwrlock
|
||||
#define pthread_rwlock_tryrdlock _pthread_rwlock_tryrdlock
|
||||
#define pthread_rwlock_trywrlock _pthread_rwlock_trywrlock
|
||||
#define pthread_rwlock_unlock _pthread_rwlock_unlock
|
||||
#define pthread_rwlock_wrlock _pthread_rwlock_wrlock
|
||||
#define pthread_rwlockattr_destroy _pthread_rwlockattr_destroy
|
||||
#define pthread_rwlockattr_getpshared _pthread_rwlockattr_getpshared
|
||||
#define pthread_rwlockattr_init _pthread_rwlockattr_init
|
||||
#define pthread_rwlockattr_setpshared _pthread_rwlockattr_setpshared
|
||||
#define pthread_self _pthread_self
|
||||
#define pthread_set_name_np _pthread_set_name_np
|
||||
#define pthread_setaffinity_np _pthread_setaffinity_np
|
||||
#define pthread_setcancelstate _pthread_setcancelstate
|
||||
#define pthread_setcanceltype _pthread_setcanceltype
|
||||
#define pthread_setconcurrency _pthread_setconcurrency
|
||||
#define pthread_setname_np _pthread_setname_np
|
||||
#define pthread_setprio _pthread_setprio
|
||||
#define pthread_setschedparam _pthread_setschedparam
|
||||
#define pthread_setspecific _pthread_setspecific
|
||||
#define pthread_sigmask _pthread_sigmask
|
||||
#define pthread_single_np _pthread_single_np
|
||||
#define pthread_spin_destroy _pthread_spin_destroy
|
||||
#define pthread_spin_init _pthread_spin_init
|
||||
#define pthread_spin_lock _pthread_spin_lock
|
||||
#define pthread_spin_trylock _pthread_spin_trylock
|
||||
#define pthread_spin_unlock _pthread_spin_unlock
|
||||
#define pthread_suspend_all_np _pthread_suspend_all_np
|
||||
#define pthread_suspend_np _pthread_suspend_np
|
||||
#define pthread_switch_add_np _pthread_switch_add_np
|
||||
#define pthread_switch_delete_np _pthread_switch_delete_np
|
||||
#define pthread_testcancel _pthread_testcancel
|
||||
#define pthread_timedjoin_np _pthread_timedjoin_np
|
||||
#define pthread_yield _pthread_yield
|
||||
#define read _read
|
||||
#define readv _readv
|
||||
#define recvfrom _recvfrom
|
||||
#define recvmsg _recvmsg
|
||||
#define recvmmsg _recvmmsg
|
||||
#define select _select
|
||||
#define sem_close _sem_close
|
||||
#define sem_destroy _sem_destroy
|
||||
#define sem_getvalue _sem_getvalue
|
||||
#define sem_init _sem_init
|
||||
#define sem_open _sem_open
|
||||
#define sem_post _sem_post
|
||||
#define sem_timedwait _sem_timedwait
|
||||
#define sem_clockwait_np _sem_clockwait_np
|
||||
#define sem_trywait _sem_trywait
|
||||
#define sem_unlink _sem_unlink
|
||||
#define sem_wait _sem_wait
|
||||
#define sendmsg _sendmsg
|
||||
#define sendmmsg _sendmmsg
|
||||
#define sendto _sendto
|
||||
#define setsockopt _setsockopt
|
||||
/*#define sigaction _sigaction*/
|
||||
#define sigprocmask _sigprocmask
|
||||
#define sigsuspend _sigsuspend
|
||||
#define socket _socket
|
||||
#define socketpair _socketpair
|
||||
#define usleep _usleep
|
||||
#define wait4 _wait4
|
||||
#define wait6 _wait6
|
||||
#define waitpid _waitpid
|
||||
#define write _write
|
||||
#define writev _writev
|
||||
|
||||
|
||||
/*
|
||||
* Other hidden syscalls/functions that libc_r needs to override
|
||||
* but are not used internally by libc.
|
||||
*
|
||||
* XXX - When modifying libc to use one of the following, remove
|
||||
* the prototype from below and place it in the list above.
|
||||
*/
|
||||
#if 0
|
||||
#define creat _creat
|
||||
#define fchflags _fchflags
|
||||
#define fchmod _fchmod
|
||||
#define ftrylockfile _ftrylockfile
|
||||
#define msync _msync
|
||||
#define nfssvc _nfssvc
|
||||
#define pause _pause
|
||||
#define sched_yield _sched_yield
|
||||
#define sendfile _sendfile
|
||||
#define shutdown _shutdown
|
||||
#define sigaltstack _sigaltstack
|
||||
#define sigpending _sigpending
|
||||
#define sigreturn _sigreturn
|
||||
#define sigsetmask _sigsetmask
|
||||
#define sleep _sleep
|
||||
#define system _system
|
||||
#define tcdrain _tcdrain
|
||||
#define wait _wait
|
||||
#endif
|
||||
|
||||
#endif /* _NAMESPACE_H_ */
|
||||
Reference in New Issue
Block a user