1 /* Invisible Vector Library
2 * coded by Ketmar // Invisible Vector <ketmar@ketmar.no-ip.org>
3 * Understanding is not required. Only obedience.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 module iv
.a85ct
/*is aliced*/;
23 // very slow and enifficient ascii85 decoder
24 string
decodeAscii85(T
) (const(T
)[] src
) {
25 static immutable uint[5] pow85
= [85*85*85*85u, 85*85*85u, 85*85u, 85u, 1u];
26 auto data
= cast(const(ubyte)[])src
;
31 void decodeTuple (int bytes
) @safe nothrow {
33 res
~= (tuple
>>24)&0xff;
38 foreach (immutable b
; data
) {
39 if (b
<= 32 || b
> 126) continue; // skip blanks
42 if (count
!= 0) return null; // alas
45 if (b
< '!' || b
> 'u') return null; // alas
46 tuple
+= (b
-'!')*pow85
[count
++];
54 // write last (possibly incomplete) tuple
56 tuple
+= pow85
[--count
];
63 string
encodeAscii85(T
) (const(T
)[] src
, int width
=76) {
64 auto data
= cast(const(ubyte)[])src
;
66 int count
= 0, pos
= 0;
69 void encodeTuple () @safe nothrow {
74 buf
[bpos
++] = tuple
%85;
79 if (width
> 0 && pos
>= width
) { res
~= '\n'; pos
= 0; }
80 res
~= cast(char)(buf
[--bpos
]+'!');
85 foreach (immutable b
; data
) {
87 case 0: tuple |
= b
<<24; break;
88 case 1: tuple |
= b
<<16; break;
89 case 2: tuple |
= b
<<8; break;
94 if (width
> 0 && pos
>= width
) { res
~= '\n'; pos
= 0; }
106 if (count
> 0) encodeTuple();
112 template a85enc(string s
) {
113 private static enum qstr(string s
) = s
.stringof
;
114 enum a85enc
= qstr
!(encodeAscii85(s
, 0));
119 template a85dec(string s
) {
120 private static enum qstr(string s
) = s
.stringof
;
121 enum a85dec
= qstr
!(decodeAscii85(s
));
126 enum e
= encodeAscii85("One, two, Freddy's coming for you");
127 enum s
= decodeAscii85(`:Ms_p+EVgG/0IE&ARo=s-Z^D?Df'3+B-:f)EZfXGFT`);
132 writeln(decodeAscii85(e
) == s
);