]>
Commit | Line | Data |
---|---|---|
1a1c8cd6 MG |
1 | #define PERL_NO_GET_CONTEXT |
2 | #include "EXTERN.h" | |
3 | #include "perl.h" | |
4 | #include "XSUB.h" | |
5 | ||
6 | #include "ppport.h" | |
7 | ||
30d6e88f | 8 | #include <base91-0.6.0/base91.c> |
1a1c8cd6 MG |
9 | |
10 | typedef struct base91 { | |
11 | struct basE91 b91; | |
12 | SV* output; | |
13 | }* Convert__Base91; | |
14 | ||
22bf75b3 | 15 | Convert__Base91 new(pTHX_ SV* class) { |
1a1c8cd6 MG |
16 | Convert__Base91 self; |
17 | Newxc(self, 1, struct base91, struct base91); | |
18 | basE91_init(&self->b91); | |
19 | self->output = newSVpvs(""); | |
20 | return self; | |
21 | } | |
22 | ||
22bf75b3 | 23 | void encode(pTHX_ Convert__Base91 self, SV* input) { |
1a1c8cd6 MG |
24 | void *o; |
25 | char* i; | |
26 | size_t len, max_out_len, ret; | |
27 | ||
28 | i = SvPVbyte(input, len); | |
29 | max_out_len = len + (len / 4) + 1; /* technically ceil(len * 16 / 13) */ | |
30 | Newx(o, max_out_len, char); | |
31 | ||
32 | ret = basE91_encode(&self->b91, i, len, o); | |
33 | sv_catpvn_nomg(self->output, o, ret); | |
34 | Safefree(o); | |
35 | } | |
36 | ||
22bf75b3 | 37 | SV* encode_end(pTHX_ Convert__Base91 self) { |
1a1c8cd6 MG |
38 | char o[2]; |
39 | size_t ret; | |
40 | SV* out; | |
41 | ret = basE91_encode_end(&self->b91, o); | |
42 | sv_catpvn_nomg(self->output, o, ret); | |
43 | ||
44 | out = self->output; | |
45 | self->output = newSVpvs(""); | |
46 | return out; | |
47 | } | |
48 | ||
22bf75b3 | 49 | void decode(pTHX_ Convert__Base91 self, SV* input) { |
1a1c8cd6 MG |
50 | void *o; |
51 | char* i; | |
52 | size_t len, max_out_len, ret; | |
53 | ||
54 | i = SvPVbyte(input, len); | |
55 | Newx(o, len, char); | |
56 | ||
57 | ret = basE91_decode(&self->b91, i, len, o); | |
58 | sv_catpvn_nomg(self->output, o, ret); | |
59 | Safefree(o); | |
60 | } | |
61 | ||
22bf75b3 | 62 | SV* decode_end(pTHX_ Convert__Base91 self) { |
1a1c8cd6 MG |
63 | char o; |
64 | size_t ret; | |
65 | SV* out; | |
66 | ret = basE91_decode_end(&self->b91, &o); | |
67 | sv_catpvn_nomg(self->output, &o, ret); | |
68 | ||
69 | out = self->output; | |
70 | self->output = newSVpvs(""); | |
71 | return out; | |
72 | } | |
73 | ||
22bf75b3 | 74 | void DESTROY(pTHX_ Convert__Base91 self) { |
1a1c8cd6 MG |
75 | sv_2mortal(self->output); |
76 | Safefree(self); | |
77 | } | |
78 | ||
79 | MODULE = Convert::Base91 PACKAGE = Convert::Base91 | |
80 | PROTOTYPES: ENABLE | |
81 | ||
82 | Convert::Base91 new(SV* class) | |
22bf75b3 | 83 | C_ARGS: aTHX_ class |
1a1c8cd6 MG |
84 | |
85 | void encode(Convert::Base91 self, SV* input) | |
22bf75b3 | 86 | C_ARGS: aTHX_ self, input |
1a1c8cd6 MG |
87 | |
88 | SV* encode_end(Convert::Base91 self) | |
22bf75b3 | 89 | C_ARGS: aTHX_ self |
1a1c8cd6 MG |
90 | |
91 | void decode(Convert::Base91 self, SV* input) | |
22bf75b3 | 92 | C_ARGS: aTHX_ self, input |
1a1c8cd6 MG |
93 | |
94 | SV* decode_end(Convert::Base91 self) | |
22bf75b3 | 95 | C_ARGS: aTHX_ self |
1a1c8cd6 MG |
96 | |
97 | void DESTROY(Convert::Base91 self) | |
22bf75b3 | 98 | C_ARGS: aTHX_ self |