| 1 |
|
| /*- |
| 2 |
|
| * Copyright (c) 2008, 2009 |
| 3 |
|
| * Thorsten Glaser <tg@mirbsd.org> |
| 4 |
|
| * |
| 5 |
|
| * Provided that these terms and disclaimer and all copyright notices |
| 6 |
|
| * are retained or reproduced in an accompanying document, permission |
| 7 |
|
| * is granted to deal in this work without restriction, including un- |
| 8 |
|
| * limited rights to use, publicly perform, distribute, sell, modify, |
| 9 |
|
| * merge, give away, or sublicence. |
| 10 |
|
| * |
| 11 |
|
| * This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to |
| 12 |
|
| * the utmost extent permitted by applicable law, neither express nor |
| 13 |
|
| * implied; without malicious intent or gross negligence. In no event |
| 14 |
|
| * may a licensor, author or contributor be held liable for indirect, |
| 15 |
|
| * direct, other damage, loss, or other issues arising in any way out |
| 16 |
|
| * of dealing in the work, even if advised of the possibility of such |
| 17 |
|
| * damage or existence of a defect, except proven that it results out |
| 18 |
|
| * of said person's immediate fault when using the work as intended. |
| 19 |
|
| */ |
| 20 |
|
| |
| 21 |
|
| #include <sys/types.h> |
| 22 |
|
| #include <stdlib.h> |
| 23 |
|
| |
| 24 |
|
| #if defined(HAVE_STDINT_H) && HAVE_STDINT_H |
| 25 |
|
| #include <stdint.h> |
| 26 |
|
| #elif defined(USE_INTTYPES) |
| 27 |
|
| #include <inttypes.h> |
| 28 |
|
| #endif |
| 29 |
|
| |
| 30 |
|
| #include "EXTERN.h" |
| 31 |
|
| #include "perl.h" |
| 32 |
|
| #include "XSUB.h" |
| 33 |
|
| |
| 34 |
|
| #if !defined(__attribute__) && (!defined(__GNUC__) || (__GNUC__ < 1) || (__GNUC__ == 2 && __GNUC_MINOR__ < 5)) |
| 35 |
|
| #define __attribute__(x) /* nothing */ |
| 36 |
|
| #endif |
| 37 |
|
| |
| 38 |
|
| #if !defined(__RCSID) || !defined(__IDSTRING) |
| 39 |
|
| #undef __RCSID |
| 40 |
|
| #undef __IDSTRING |
| 41 |
|
| #undef __IDSTRING_CONCAT |
| 42 |
|
| #undef __IDSTRING_EXPAND |
| 43 |
|
| #define __IDSTRING_CONCAT(l,p) __LINTED__ ## l ## _ ## p |
| 44 |
|
| #define __IDSTRING_EXPAND(l,p) __IDSTRING_CONCAT(l,p) |
| 45 |
|
| #define __IDSTRING(prefix, string) \ |
| 46 |
|
| static const char __IDSTRING_EXPAND(__LINE__,prefix) [] \ |
| 47 |
|
| __attribute__((used)) = "@(""#)" #prefix ": " string |
| 48 |
|
| #define __RCSID(x) __IDSTRING(rcsid,x) |
| 49 |
|
| #endif |
| 50 |
|
| |
| 51 |
|
| __RCSID("$MirOS: contrib/hosted/tg/code/BSD::arc4random/arc4rnd_xs.c,v 1.5 2009/10/10 22:43:53 tg Exp $"); |
| 52 |
|
| |
| 53 |
|
| #ifdef REDEF_USCORETYPES |
| 54 |
|
| #define u_int32_t uint32_t |
| 55 |
|
| #endif |
| 56 |
|
| |
| 57 |
|
| #ifdef NEED_ARC4RANDOM_DECL |
| 58 |
|
| u_int32_t arc4random(void); |
| 59 |
|
| void arc4random_addrandom(u_char *, int); |
| 60 |
|
| #endif |
| 61 |
|
| |
| 62 |
|
| XS(XS_BSD__arc4random_arc4random_xs); |
| 63 |
9 |
| XS(XS_BSD__arc4random_arc4random_xs) |
| 64 |
|
| { |
| 65 |
9 |
| dXSARGS; |
| 66 |
9 |
▶ | dXSTARG; |
Branch: 1 branch
| Branch | True | False | Total |
| dXSTARG; |
9 |
0 |
9 |
|
| 67 |
|
| uint32_t rv; |
| 68 |
|
| |
| 69 |
9 |
| rv = arc4random(); |
| 70 |
|
| |
| 71 |
9 |
| XSprePUSH; |
| 72 |
9 |
▶ | PUSHu((UV)rv); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHu((UV)rv); |
8 |
1 |
9 |
|
| 73 |
|
| |
| 74 |
9 |
| XSRETURN(1); |
| 75 |
|
| } |
| 76 |
|
| |
| 77 |
|
| XS(XS_BSD__arc4random_stir_xs); |
| 78 |
0 |
| XS(XS_BSD__arc4random_stir_xs) |
| 79 |
|
| { |
| 80 |
0 |
| dXSARGS; |
| 81 |
|
| |
| 82 |
0 |
| arc4random_stir(); |
| 83 |
|
| |
| 84 |
0 |
| XSRETURN_EMPTY; |
| 85 |
|
| } |
| 86 |
|
| |
| 87 |
|
| XS(XS_BSD__arc4random_arc4random_addrandom_xs); |
| 88 |
3 |
| XS(XS_BSD__arc4random_arc4random_addrandom_xs) |
| 89 |
|
| { |
| 90 |
3 |
| dXSARGS; |
| 91 |
3 |
▶ | dXSTARG; |
Branch: 1 branch
| Branch | True | False | Total |
| dXSTARG; |
3 |
0 |
3 |
|
| 92 |
|
| SV *sv; |
| 93 |
|
| char *buf; |
| 94 |
|
| STRLEN len; |
| 95 |
|
| uint32_t rv; |
| 96 |
|
| |
| 97 |
3 |
| sv = ST(0); |
| 98 |
3 |
| buf = SvPV(sv, len); |
| 99 |
3 |
| arc4random_addrandom((unsigned char *)buf, (int)len); |
| 100 |
3 |
| rv = arc4random(); |
| 101 |
3 |
| XSprePUSH; |
| 102 |
3 |
▶ | PUSHu((UV)rv); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHu((UV)rv); |
2 |
1 |
3 |
|
| 103 |
|
| |
| 104 |
3 |
| XSRETURN(1); |
| 105 |
|
| } |
| 106 |
|
| |
| 107 |
|
| #ifndef HAVE_ARC4RANDOM_PUSHB |
| 108 |
|
| #define HAVE_ARC4RANDOM_PUSHB 1 |
| 109 |
|
| #endif |
| 110 |
|
| |
| 111 |
|
| #if HAVE_ARC4RANDOM_PUSHB |
| 112 |
|
| XS(XS_BSD__arc4random_arc4random_pushb_xs); |
| 113 |
|
| XS(XS_BSD__arc4random_arc4random_pushb_xs) |
| 114 |
|
| { |
| 115 |
|
| dXSARGS; |
| 116 |
|
| dXSTARG; |
| 117 |
|
| SV *sv; |
| 118 |
|
| char *buf; |
| 119 |
|
| STRLEN len; |
| 120 |
|
| uint32_t rv; |
| 121 |
|
| |
| 122 |
|
| sv = ST(0); |
| 123 |
|
| buf = SvPV(sv, len); |
| 124 |
|
| rv = arc4random_pushb((void *)buf, (size_t)len); |
| 125 |
|
| XSprePUSH; |
| 126 |
|
| PUSHu((UV)rv); |
| 127 |
|
| |
| 128 |
|
| XSRETURN(1); |
| 129 |
|
| } |
| 130 |
|
| #elif defined(arc4random_pushk) |
| 131 |
|
| #define XS_BSD__arc4random_arc4random_pushb_xs \ |
| 132 |
|
| XS_BSD__arc4random_arc4random_pushk_xs |
| 133 |
|
| #else |
| 134 |
|
| #define XS_BSD__arc4random_arc4random_pushb_xs \ |
| 135 |
|
| XS_BSD__arc4random_arc4random_addrandom_xs |
| 136 |
|
| #endif |
| 137 |
|
| |
| 138 |
|
| #if defined(arc4random_pushk) |
| 139 |
|
| XS(XS_BSD__arc4random_arc4random_pushk_xs); |
| 140 |
|
| XS(XS_BSD__arc4random_arc4random_pushk_xs) |
| 141 |
|
| { |
| 142 |
|
| dXSARGS; |
| 143 |
|
| dXSTARG; |
| 144 |
|
| SV *sv; |
| 145 |
|
| char *buf; |
| 146 |
|
| STRLEN len; |
| 147 |
|
| uint32_t rv; |
| 148 |
|
| |
| 149 |
|
| sv = ST(0); |
| 150 |
|
| buf = SvPV(sv, len); |
| 151 |
|
| rv = arc4random_pushk((void *)buf, (size_t)len); |
| 152 |
|
| XSprePUSH; |
| 153 |
|
| PUSHu((UV)rv); |
| 154 |
|
| |
| 155 |
|
| XSRETURN(1); |
| 156 |
|
| } |
| 157 |
|
| #elif HAVE_ARC4RANDOM_PUSHB |
| 158 |
|
| #define XS_BSD__arc4random_arc4random_pushk_xs \ |
| 159 |
|
| XS_BSD__arc4random_arc4random_pushb_xs |
| 160 |
|
| #else |
| 161 |
|
| #define XS_BSD__arc4random_arc4random_pushk_xs \ |
| 162 |
|
| XS_BSD__arc4random_arc4random_addrandom_xs |
| 163 |
|
| #endif |
| 164 |
|
| |
| 165 |
|
| #undef HAVE_ARC4RANDOM_KINTF |
| 166 |
|
| #if HAVE_ARC4RANDOM_PUSHB || defined(arc4random_pushk) |
| 167 |
|
| #define HAVE_ARC4RANDOM_KINTF 1 |
| 168 |
|
| #else |
| 169 |
|
| #define HAVE_ARC4RANDOM_KINTF 0 |
| 170 |
|
| #endif |
| 171 |
|
| |
| 172 |
|
| |
| 173 |
|
| /* |
| 174 |
|
| * These may be needed because praeprocessor commands inside a |
| 175 |
|
| * macro's argument list may not work |
| 176 |
|
| */ |
| 177 |
|
| |
| 178 |
|
| #if HAVE_ARC4RANDOM_PUSHB |
| 179 |
|
| #define IDT_ARC4RANDOM_PUSHB " arc4random_pushb" |
| 180 |
|
| #else |
| 181 |
|
| #define IDT_ARC4RANDOM_PUSHB "" |
| 182 |
|
| #endif |
| 183 |
|
| |
| 184 |
|
| #if defined(arc4random_pushk) |
| 185 |
|
| #define IDT_arc4random_pushk " arc4random_pushk" |
| 186 |
|
| #else |
| 187 |
|
| #define IDT_arc4random_pushk "" |
| 188 |
|
| #endif |
| 189 |
|
| |
| 190 |
|
| #if HAVE_ARC4RANDOM_KINTF |
| 191 |
|
| #define IDT_ARC4RANDOM_KINTF " have_kintf:=1" |
| 192 |
|
| #else |
| 193 |
|
| #define IDT_ARC4RANDOM_KINTF " have_kintf:=0" |
| 194 |
|
| #endif |
| 195 |
|
| |
| 196 |
|
| __IDSTRING(api_text, "BSD::arc4random " XS_VERSION " with {" |
| 197 |
|
| " arc4random" |
| 198 |
|
| " arc4random_addrandom" |
| 199 |
|
| IDT_ARC4RANDOM_PUSHB |
| 200 |
|
| IDT_arc4random_pushk |
| 201 |
|
| IDT_ARC4RANDOM_KINTF |
| 202 |
|
| " }"); |
| 203 |
|
| |
| 204 |
|
| |
| 205 |
|
| /* the Perl API is not const clean */ |
| 206 |
|
| static char file[] = __FILE__; |
| 207 |
|
| static char func_a4r[] = "BSD::arc4random::arc4random_xs"; |
| 208 |
|
| static char func_a4add[] = "BSD::arc4random::arc4random_addrandom_xs"; |
| 209 |
|
| static char func_a4rpb[] = "BSD::arc4random::arc4random_pushb_xs"; |
| 210 |
|
| static char func_a4rpk[] = "BSD::arc4random::arc4random_pushk_xs"; |
| 211 |
|
| static char func_astir[] = "BSD::arc4random::arc4random_stir_xs"; |
| 212 |
|
| static char func_kintf[] = "BSD::arc4random::have_kintf"; |
| 213 |
|
| |
| 214 |
|
| #ifdef __cplusplus |
| 215 |
|
| extern "C" |
| 216 |
|
| #endif |
| 217 |
|
| XS(boot_BSD__arc4random); |
| 218 |
1 |
| XS(boot_BSD__arc4random) |
| 219 |
|
| { |
| 220 |
1 |
| dXSARGS; |
| 221 |
|
| |
| 222 |
1 |
| XS_VERSION_BOOTCHECK; |
| 223 |
|
| |
| 224 |
1 |
| newXS(func_a4r, XS_BSD__arc4random_arc4random_xs, file); |
| 225 |
1 |
| newXS(func_a4add, XS_BSD__arc4random_arc4random_addrandom_xs, file); |
| 226 |
1 |
| newXS(func_a4rpb, XS_BSD__arc4random_arc4random_pushb_xs, file); |
| 227 |
1 |
| newXS(func_a4rpk, XS_BSD__arc4random_arc4random_pushk_xs, file); |
| 228 |
1 |
| newXS(func_astir, XS_BSD__arc4random_stir_xs, file); |
| 229 |
|
| |
| 230 |
1 |
| newCONSTSUB(NULL, func_kintf, newSViv(HAVE_ARC4RANDOM_KINTF)); |
| 231 |
|
| |
| 232 |
1 |
| XSRETURN_YES; |
| 233 |
|
| } |