0

I might be taking the wrong approach or not understanding correctly the base64 algorithm.

I have a REST service that asks for a JSON with various parameters,

{
 "credentials":{...}
 , "content":"BASE64"
 , ...more
}

The content key contains a string which represents a base64 encoded of an invoice.

In love with the simplicity of php I only do this

$xml_string = '<?xml version="1.0" encoding="UTF-8"?>...more xml stuff';
$content = base64_encode($xml_string);

Et Voilà I have my content encoded plain an simple, ready to be sent to the third party.

Migrating to Oracle I did this

declare
  xml_string varchar2(4000):= '<?xml version="1.0" encoding="UTF-8"?>...more xml stuff';
  b64 varchar2(4000);
begin
  b64 := utl_raw.cast_to_varchar2(utl_encode.base64_encode(utl_raw.cast_to_raw(xml_string)));
end;

And it works, easy right? Well no with oracle nothing is easy, it has to be a pain in the ass.

The invoice XML may have a lot of concepts and can grow quite big so it's reasonable to assume that varchar2(4000) will not be enough eventually.

So the xml_string now is a clob to accommodate bigger strings, easy no?

again no I did this without success:

declare
  xml_string clob:= '<?xml version="1.0" encoding="UTF-8"?>...more xml stuff';
      b64 clob;
    begin
      b64:=to_clob(utl_encode.base64_encode(utl_raw.cast_to_raw(xml_string));
    end;

but it only gives me a garbage string once decoded.

Is there a function (can be user made) that makes a simple base64encode of a CLOB string.

I have only found functions for blobs (images and binary files). And this one that receives a CLOB and gives a CLOB (perfection ehh?) well does not work http://bitwiseeloquence.blogspot.mx/2011/07/converting-to-and-from-base-64-clob-in.html

Neto Yo
  • 450
  • 1
  • 5
  • 17
  • 1
    Note: Every 3 bytes of input to base64 will result in 4 bytes of output, with the last batch always being 4 bytes regardless of input. So whatever your largest input data is the output will be `ceil(size/3)*4`. You may find this detail useful in your planning. – Sammitch Oct 05 '17 at 00:12
  • 1
    Also, so long as you feed base64 blobs of data that have sizes that are mutliples of 3 you can safely concatenate the resulting encoded strings. eg: `base64_encode("abcde") === base64_encode("abc") . base64_encode("de")` – Sammitch Oct 05 '17 at 00:18
  • Have a look at this solution: https://stackoverflow.com/questions/3804279/base64-encoding-and-decoding-in-oracle/3806265#40852152 – Wernfried Domscheit Oct 05 '17 at 06:48

0 Answers0