-
Notifications
You must be signed in to change notification settings - Fork 0
/
XS.xs
50 lines (37 loc) · 1.05 KB
/
XS.xs
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
/*
* Copyright (C) 2017 by Tomasz Konojacki
*
* This library is free software; you can redistribute it and/or modify
* it under the same terms as Perl itself, either Perl version 5.24.0 or,
* at your option, any later version of Perl 5 you may have available.
*
*/
#define PERL_NO_GET_CONTEXT
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
MODULE = Bytes::Random::XS PACKAGE = Bytes::Random::XS
PROTOTYPES: DISABLE
SV *
random_bytes(count)
IV count
CODE:
IV i = 0;
char *str;
count = count > 0 ? count : 0;
RETVAL = newSV(count ? count : 1);
SvPOK_on(RETVAL);
SvCUR_set(RETVAL, count);
str = SvPVX(RETVAL);
if (count) {
if (!PL_srand_called) {
seedDrand01((Rand_seed_t)seed());
PL_srand_called = TRUE;
}
for (; count > i; ++i)
str[i] = (char)(256 * Drand01());
}
/* no point in doing that, but it doesn't hurt */
str[i] = 0;
OUTPUT:
RETVAL