| 1 | #define PERL_NO_GET_CONTEXT |
| 2 | #include "EXTERN.h" |
| 3 | #include "perl.h" |
| 4 | #include "XSUB.h" |
| 5 | |
| 6 | #include "ppport.h" |
| 7 | |
| 8 | #include <base91.c> |
| 9 | |
| 10 | typedef struct base91 { |
| 11 | struct basE91 b91; |
| 12 | SV* output; |
| 13 | }* Convert__Base91; |
| 14 | |
| 15 | Convert__Base91 new(pTHX_ SV* class) { |
| 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 | |
| 23 | void encode(pTHX_ Convert__Base91 self, SV* input) { |
| 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 | |
| 37 | SV* encode_end(pTHX_ Convert__Base91 self) { |
| 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 | |
| 49 | void decode(pTHX_ Convert__Base91 self, SV* input) { |
| 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 | |
| 62 | SV* decode_end(pTHX_ Convert__Base91 self) { |
| 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 | |
| 74 | void DESTROY(pTHX_ Convert__Base91 self) { |
| 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) |
| 83 | C_ARGS: aTHX_ class |
| 84 | |
| 85 | void encode(Convert::Base91 self, SV* input) |
| 86 | C_ARGS: aTHX_ self, input |
| 87 | |
| 88 | SV* encode_end(Convert::Base91 self) |
| 89 | C_ARGS: aTHX_ self |
| 90 | |
| 91 | void decode(Convert::Base91 self, SV* input) |
| 92 | C_ARGS: aTHX_ self, input |
| 93 | |
| 94 | SV* decode_end(Convert::Base91 self) |
| 95 | C_ARGS: aTHX_ self |
| 96 | |
| 97 | void DESTROY(Convert::Base91 self) |
| 98 | C_ARGS: aTHX_ self |