2013-11-04 17:43:22 +01:00
|
|
|
/*
|
|
|
|
|
This file is part of Pretty Curved Privacy (pcp1).
|
|
|
|
|
|
|
|
|
|
Copyright (C) 2013 T.Linden.
|
|
|
|
|
|
|
|
|
|
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 3 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.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
You can contact me by mail: <tlinden AT cpan DOT org>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
2013-10-28 22:50:05 +01:00
|
|
|
#ifndef _HAVE_PCP_PLATFORM_H
|
|
|
|
|
#define _HAVE_PCP_PLATFORM_H
|
|
|
|
|
|
|
|
|
|
#if defined(CONFIG_H_FILE)
|
|
|
|
|
#include CONFIG_H_FILE
|
|
|
|
|
#elif defined(HAVE_CONFIG_H)
|
|
|
|
|
#include "config.h"
|
|
|
|
|
#else
|
|
|
|
|
#error Need either CONFIG_H_FILE or HAVE_CONFIG_H defined.
|
|
|
|
|
#endif
|
|
|
|
|
|
2013-10-29 22:14:34 +01:00
|
|
|
#ifdef HAVE_ENDIAN_H
|
2013-10-29 22:51:06 +01:00
|
|
|
# include <endian.h>
|
|
|
|
|
#else // no endian.h
|
|
|
|
|
# ifdef HAVE_SYS_ENDIAN_H
|
|
|
|
|
# include <sys/endian.h>
|
|
|
|
|
# ifdef HAVE_BETOH32
|
|
|
|
|
// openbsd, use aliases
|
|
|
|
|
# define be32toh betoh32
|
|
|
|
|
# define htobe32 hto32be
|
|
|
|
|
# endif
|
|
|
|
|
# else // no sys/endian.h
|
|
|
|
|
# if __BYTE_ORDER == __BIG_ENDIAN
|
|
|
|
|
# define be32toh(x) ((void)0)
|
|
|
|
|
# define htobe32(x) ((void)0)
|
|
|
|
|
# else
|
|
|
|
|
# ifdef HAVE_ARPA_INET_H
|
|
|
|
|
# include <arpa/inet.h>
|
|
|
|
|
# else
|
|
|
|
|
# ifdef HAVE_NETINET_IN_H
|
|
|
|
|
# include <netinet/in.h>
|
|
|
|
|
# else
|
|
|
|
|
# error Need either netinet/in.h or arpa/inet.h for ntohl() and htonl()
|
|
|
|
|
# endif
|
|
|
|
|
# endif
|
|
|
|
|
# define be32toh(x) ((u_int32_t)ntohl((u_int32_t)(x)))
|
|
|
|
|
# define htobe32(x) ((u_int32_t)htonl((u_int32_t)(x)))
|
|
|
|
|
# endif
|
|
|
|
|
# endif // HAVE_SYS_ENDIAN_H
|
2013-10-29 22:14:34 +01:00
|
|
|
#endif // HAVE_ENDIAN_H
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef HAVE_ARC4RANDOM_BUF
|
|
|
|
|
// shitty OS. we've got to use other stuff
|
|
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
|
|
static inline FILE *__getranddev() {
|
|
|
|
|
FILE *R;
|
|
|
|
|
if((R = fopen("/dev/urandom", "rb")) == NULL) {
|
|
|
|
|
// not even this is here! what a shame
|
|
|
|
|
if((R = fopen("/dev/random", "rb")) == NULL) {
|
|
|
|
|
// not available or depleted. that's too bad
|
|
|
|
|
fprintf(stderr, "ERROR: /dev/urandom not available, /dev/random is depleted.\n");
|
|
|
|
|
fprintf(stderr, "That's horrible for you but a nightmare for me. I die. Bye.\n");
|
|
|
|
|
exit(2);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return R;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline u_int32_t arc4random() {
|
|
|
|
|
uint32_t x;
|
|
|
|
|
FILE *R = __getranddev();
|
|
|
|
|
fread(&x, sizeof(uint32_t), 1, R);
|
|
|
|
|
fclose(R);
|
|
|
|
|
return x;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline void arc4random_buf(void *buf, size_t nbytes) {
|
|
|
|
|
FILE *R = __getranddev();
|
|
|
|
|
fread(buf, nbytes, 1, R);
|
|
|
|
|
fclose(R);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
2013-10-28 22:50:05 +01:00
|
|
|
#endif /* !_HAVE_PCP_PLATFORM_H */
|
|
|
|
|
|