Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

base64 decoder in sql procedure or function, anyone?

463 views
Skip to first unread message

Lennart

unread,
Jan 24, 2009, 1:23:12 PM1/24/09
to

Before I rush of and start implementing one, I just want to check
whether someone already did and is willing to share their code? For
reasons that are beyond my control I need an sql implementation (i.e.
no java or c allowed).


Thanx
/Lennart

Lennart

unread,
Jan 28, 2009, 3:22:44 PM1/28/09
to

Probably useless for everyone else, but I'll post a partial solution
anyhow. It *almost* works :-) I haven't had the time to figure out why
it is bailing out after a number of characters.

create function base64.decode_char ( c varchar(1) )
returns char
begin atomic
declare x int;
set x = ascii(c);
return
case
when x between 65 and 90 then chr( x - ascii('A') )
when x between 97 and 122 then chr( x - ascii('a') + 26 )
when x between 48 and 57 then chr( x - ascii('0') + 52 )
when x = 43 then chr(62)
else chr(63)
end;
end @

create function base64.decode_str ( s varchar(4000) )
returns varchar(4000)
begin atomic
declare n int;
declare len int;
declare res varchar(4000);

declare b1 char;
declare b2 char;
declare b3 char;
declare b4 char;

declare s1 char;
declare s2 char;
declare s3 char;

set n = 1;
set len = length(rtrim(s));
set res = '';

while n <= len do
set b1 = base64.decode_char( substr(s,n,1) );
set b2 = base64.decode_char( substr(s,n+1,1) );
set b3 = base64.decode_char( substr(s,n+2,1) );
set b4 = base64.decode_char( substr(s,n+3,1) );

set s1 = case when n+1 <= len then
chr( bitor(ascii(b1)*power(2,2),
ascii(b2)/power(2,4)
))
else ''
end;
set s2 = case when n+2 <= len then
chr( bitor( bitand(ascii(b2),15)*power(2,4),
ascii(b3)/power(2,2)
))
else ''
end;
set s3 = case when n+3 <= len then
chr( bitor( bitand(ascii(b3),3)*power(2,6),
ascii(b4)
))
else ''
end;
set res = rtrim(res) || s1 || s2 || s3;
set n = n + 4;
end while;
return res;
end @

Hmm, fft, huffman, ... ;-)


/Lennart

0 new messages