File: arc4rnd_xs.c

package info (click to toggle)
libbsd-arc4random-perl 1.50-5
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 128 kB
  • sloc: ansic: 627; perl: 148; makefile: 53
file content (239 lines) | stat: -rw-r--r-- 5,774 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*-
 * Copyright (c) 2008, 2009, 2011
 *	Thorsten Glaser <tg@mirbsd.org>
 *
 * Provided that these terms and disclaimer and all copyright notices
 * are retained or reproduced in an accompanying document, permission
 * is granted to deal in this work without restriction, including un-
 * limited rights to use, publicly perform, distribute, sell, modify,
 * merge, give away, or sublicence.
 *
 * This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
 * the utmost extent permitted by applicable law, neither express nor
 * implied; without malicious intent or gross negligence. In no event
 * may a licensor, author or contributor be held liable for indirect,
 * direct, other damage, loss, or other issues arising in any way out
 * of dealing in the work, even if advised of the possibility of such
 * damage or existence of a defect, except proven that it results out
 * of said person's immediate fault when using the work as intended.
 */

#include <sys/types.h>
#include <stdlib.h>

#if defined(HAVE_STDINT_H) && HAVE_STDINT_H
#include <stdint.h>
#elif defined(USE_INTTYPES)
#include <inttypes.h>
#endif

#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"

#if !defined(__attribute__) && (!defined(__GNUC__) || (__GNUC__ < 1) || (__GNUC__ == 2 && __GNUC_MINOR__ < 5))
#define __attribute__(x)		/* nothing */
#endif

#if !defined(__RCSID) || !defined(__IDSTRING)
#undef __RCSID
#undef __IDSTRING
#undef __IDSTRING_CONCAT
#undef __IDSTRING_EXPAND
#define __IDSTRING_CONCAT(l,p)		__LINTED__ ## l ## _ ## p
#define __IDSTRING_EXPAND(l,p)		__IDSTRING_CONCAT(l,p)
#define __IDSTRING(prefix, string)				\
	static const char __IDSTRING_EXPAND(__LINE__,prefix) []	\
	    __attribute__((used)) = "@(""#)" #prefix ": " string
#define __RCSID(x)			__IDSTRING(rcsid,x)
#endif

__RCSID("$MirOS: contrib/hosted/tg/code/BSD::arc4random/arc4rnd_xs.c,v 1.6 2011/06/06 12:20:26 tg Exp $");

#ifdef REDEF_USCORETYPES
#define u_int32_t	uint32_t
#endif

#ifdef NEED_ARC4RANDOM_DECL
u_int32_t arc4random(void);
void arc4random_addrandom(u_char *, int);
void arc4random_stir(void);
#endif

XS(XS_BSD__arc4random_arc4random_xs);
XS(XS_BSD__arc4random_arc4random_xs)
{
	dXSARGS;
	dXSTARG;
	uint32_t rv;

	rv = arc4random();

	XSprePUSH;
	PUSHu((UV)rv);

	XSRETURN(1);
}

XS(XS_BSD__arc4random_stir_xs);
XS(XS_BSD__arc4random_stir_xs)
{
	dXSARGS;

	arc4random_stir();

	XSRETURN_EMPTY;
}

XS(XS_BSD__arc4random_arc4random_addrandom_xs);
XS(XS_BSD__arc4random_arc4random_addrandom_xs)
{
	dXSARGS;
	dXSTARG;
	SV *sv;
	char *buf;
	STRLEN len;
	uint32_t rv;

	sv = ST(0);
	buf = SvPV(sv, len);
	arc4random_addrandom((unsigned char *)buf, (int)len);
	rv = arc4random();
	XSprePUSH;
	PUSHu((UV)rv);

	XSRETURN(1);
}

#ifndef HAVE_ARC4RANDOM_PUSHB
#define HAVE_ARC4RANDOM_PUSHB	1
#endif

#if HAVE_ARC4RANDOM_PUSHB

#ifdef NEED_ARC4RANDOM_DECL
uint32_t arc4random_pushb(const void *, size_t);
#endif

XS(XS_BSD__arc4random_arc4random_pushb_xs);
XS(XS_BSD__arc4random_arc4random_pushb_xs)
{
	dXSARGS;
	dXSTARG;
	SV *sv;
	char *buf;
	STRLEN len;
	uint32_t rv;

	sv = ST(0);
	buf = SvPV(sv, len);
	rv = arc4random_pushb((void *)buf, (size_t)len);
	XSprePUSH;
	PUSHu((UV)rv);

	XSRETURN(1);
}
#elif defined(arc4random_pushk)
#define XS_BSD__arc4random_arc4random_pushb_xs \
	XS_BSD__arc4random_arc4random_pushk_xs
#else
#define XS_BSD__arc4random_arc4random_pushb_xs \
	XS_BSD__arc4random_arc4random_addrandom_xs
#endif

#if defined(arc4random_pushk)
XS(XS_BSD__arc4random_arc4random_pushk_xs);
XS(XS_BSD__arc4random_arc4random_pushk_xs)
{
	dXSARGS;
	dXSTARG;
	SV *sv;
	char *buf;
	STRLEN len;
	uint32_t rv;

	sv = ST(0);
	buf = SvPV(sv, len);
	rv = arc4random_pushk((void *)buf, (size_t)len);
	XSprePUSH;
	PUSHu((UV)rv);

	XSRETURN(1);
}
#elif HAVE_ARC4RANDOM_PUSHB
#define XS_BSD__arc4random_arc4random_pushk_xs \
	XS_BSD__arc4random_arc4random_pushb_xs
#else
#define XS_BSD__arc4random_arc4random_pushk_xs \
	XS_BSD__arc4random_arc4random_addrandom_xs
#endif

#undef HAVE_ARC4RANDOM_KINTF
#if HAVE_ARC4RANDOM_PUSHB || defined(arc4random_pushk)
#define HAVE_ARC4RANDOM_KINTF	1
#else
#define HAVE_ARC4RANDOM_KINTF	0
#endif


/*
 * These may be needed because praeprocessor commands inside a
 * macro's argument list may not work
 */

#if HAVE_ARC4RANDOM_PUSHB
#define IDT_ARC4RANDOM_PUSHB	" arc4random_pushb"
#else
#define IDT_ARC4RANDOM_PUSHB	""
#endif

#if defined(arc4random_pushk)
#define IDT_arc4random_pushk	" arc4random_pushk"
#else
#define IDT_arc4random_pushk	""
#endif

#if HAVE_ARC4RANDOM_KINTF
#define IDT_ARC4RANDOM_KINTF	" have_kintf:=1"
#else
#define IDT_ARC4RANDOM_KINTF	" have_kintf:=0"
#endif

__IDSTRING(api_text, "BSD::arc4random " XS_VERSION " with {"
    " arc4random"
    " arc4random_addrandom"
    IDT_ARC4RANDOM_PUSHB
    IDT_arc4random_pushk
    IDT_ARC4RANDOM_KINTF
    " }");


/* the Perl API is not const clean */
static char file[] = __FILE__;
static char func_a4r[] = "BSD::arc4random::arc4random_xs";
static char func_a4add[] = "BSD::arc4random::arc4random_addrandom_xs";
static char func_a4rpb[] = "BSD::arc4random::arc4random_pushb_xs";
static char func_a4rpk[] = "BSD::arc4random::arc4random_pushk_xs";
static char func_astir[] = "BSD::arc4random::arc4random_stir_xs";
static char func_kintf[] = "BSD::arc4random::have_kintf";

#ifdef __cplusplus
extern "C"
#endif
XS(boot_BSD__arc4random);
XS(boot_BSD__arc4random)
{
	dXSARGS;

	XS_VERSION_BOOTCHECK;

	newXS(func_a4r, XS_BSD__arc4random_arc4random_xs, file);
	newXS(func_a4add, XS_BSD__arc4random_arc4random_addrandom_xs, file);
	newXS(func_a4rpb, XS_BSD__arc4random_arc4random_pushb_xs, file);
	newXS(func_a4rpk, XS_BSD__arc4random_arc4random_pushk_xs, file);
	newXS(func_astir, XS_BSD__arc4random_stir_xs, file);

	newCONSTSUB(NULL, func_kintf, newSViv(HAVE_ARC4RANDOM_KINTF));

	XSRETURN_YES;
}