-
Notifications
You must be signed in to change notification settings - Fork 0
/
PwGen.xs
92 lines (74 loc) · 1.41 KB
/
PwGen.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
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
/*
* Copyright (C) 2014 Bastian Friedrich <[email protected]>
*/
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include "pwgen.h"
#include <unistd.h>
#define MY_CXT_KEY "Crypt::PwGen::_guts" XS_VERSION
int (*pw_number)(int max_num);
typedef struct {
int gen;
} my_cxt_t;
START_MY_CXT
MODULE = Crypt::PwGen PACKAGE = Crypt::PwGen
PROTOTYPES: ENABLE
BOOT:
/*
* Set generator to "phonemes", number generator to "random number"
*/
{
MY_CXT_INIT;
MY_CXT.gen = 1;
}
pw_number = pw_random_number;
void
pwgen_phonemes()
PREINIT:
dMY_CXT;
CODE:
MY_CXT.gen = 1;
void
pwgen_rand()
PREINIT:
dMY_CXT;
CODE:
MY_CXT.gen = 2;
void
pwgen_random_number()
CODE:
pw_number = pw_random_number;
void
pwgen_sha1_number(sha1)
char *sha1
CODE:
pw_sha1_init(sha1);
pw_number = pw_sha1_number;
SV *
pwgen(len, flags = PW_DIGITS | PW_UPPERS, remove = "")
int len
int flags
char *remove
PREINIT:
dMY_CXT;
char *buf;
void (*pwgen)(char *inbuf, int size, int pw_flags, char *remove);
CODE:
if (len < 1) {
Perl_croak(aTHX_ "len must be positive");
} else if (len > 1023) {
Perl_croak(aTHX_ "maximum len of 1023 exceeded");
}
printf("Remove is %s\n", remove);
if (MY_CXT.gen == 1) {
pwgen = pw_phonemes;
} else {
pwgen = pw_rand;
}
buf = (char *)malloc(len + 1);
pwgen(buf, len, flags, remove);
RETVAL = newSVpv(buf, 0);
free(buf);
OUTPUT:
RETVAL