Custom Base64Decode Function
KevMar 4 2011 — edited Mar 7 2011Hi,
I'm having trouble converting a base64 image to a blob.
I have checked the length of the blob before its encoded and it is 53924.
When i run my function to decode the blob the result is 53923. Because i'm missing some data, the image does not render.
What's strange is the smaller the substr, the more characters that are lost.
Is there something wrong with this function that is causing it to fail:
I have added both the encode and decode function.
I would appreciate any help
Thanks
Kevin
ENCODE
create or replace function base64encode( p_blob in blob )
return clob
is
t_rv clob;
t_step pls_integer := 24573; make sure you set a multiple of 3 not higher than 24573
t_step pls_integer := 474;
begin
for i in 0 .. trunc( ( dbms_lob.getlength( p_blob ) - 1 ) / t_step )
loop
t_rv := t_rv || utl_raw.cast_to_varchar2( utl_encode.base64_encode( dbms_lob.substr( p_blob, t_step, i * t_step + 1 ) ) );
end loop;
return t_rv;
end;
DECODE
create or replace function base64decode( p_clob in clob )
return blob
is
t_rv blob;
v_raw_temp raw(31984);
t_step pls_integer := 24573; make sure you set a multiple of 3 not higher than 24573
t_step pls_integer := 31984;
ln_num number;
begin
dbms_lob.createtemporary(t_rv, true);
for i in 0 .. trunc( ( dbms_lob.getlength( p_clob ) -1 ) / t_step)
loop
v_raw_temp := utl_encode.base64_decode( utl_raw.cast_to_raw(substr( p_clob, i * t_step + 1,t_step ) ) );
dbms_output.put_line(utl_raw.length(v_raw_temp));
dbms_lob.writeappend(t_rv, utl_raw.length(v_raw_temp),v_raw_temp);
ln_num := i;
end loop;
-- dbms_output.put_line(ln_num);
return t_rv;
end;