Encode/decode base64 example

Other open source tools published on ScottKlement.com
Post Reply
aa73
Posts: 1
Joined: Wed Dec 08, 2021 10:20 am

Encode/decode base64 example

Post by aa73 »

Hello,

I have successfully installed the program on my machine.
The problem is, I don't know how to use it.
Do you have sample programs for encoding and decoding PDF files?

Thank you

Regards
Scott Klement
Site Admin
Posts: 796
Joined: Sun Jul 04, 2021 5:12 am

Re: Encode/decode base64 example

Post by Scott Klement »

Can you be more specific about what sort of example you're looking for? What you have for input? Do you have data in a file, or is it data from a REST API or just a string in a program? What is it?

Is it formatted as an XML? JSON? MIME? Or, what sort of document is the base64 encoded data in? Or, is it simply a big base64-string?
sLucke1015
Posts: 18
Joined: Tue Mar 07, 2023 3:09 pm

Re: Encode/decode base64 example

Post by sLucke1015 »

I would like to hijack this thread since it's been unused for about 18 months, if I may?

I'm not sure what "aa73" was looking for, but I can answer your questions as far as my situation, Scott.

The input will be from a field from a complex DS which is the result of a REST APIs JSON reply. (document varchar(8000000) inz)

The encodingType is "B64Encode".
The mimetype is supposed to be "TIFF".

Does this help?
Can I add anything?

Thank you,
Scott Klement
Site Admin
Posts: 796
Joined: Sun Jul 04, 2021 5:12 am

Re: Encode/decode base64 example

Post by Scott Klement »

about the only thing you told us is that it's a field in a data structure... What is in that field? What do you want to do with it?
sLucke1015
Posts: 18
Joined: Tue Mar 07, 2023 3:09 pm

Re: Encode/decode base64 example

Post by sLucke1015 »

I'm sorry...

It is supposed to be an embedded TIFF document (may be a single page or multiple pages) and it is b64 encoded.

Currently the RPGLE program is successfully using the following HTTPAPI to retrieve the JSON and save it to the IFS:
http_setOption ( 'network-ccsid' : '1208' );
http_setAuth( HTTP_AUTH_BASIC : apiKey : ILR_httpAuth() );
http_req( 'GET' : url : myStmf1 );

I need to decode it and save it to the IFS.

This is what is being tried now and it (base64_decode) is bombing immediately on statement 243.00 "if (base64r(B1)=x'FF');" because B1 is zero.

dcl-s docIn varchar(8000000) inz;
dcl-s docOut varchar(8000000) inz;

docIn = bla.bla.bla;

base64_decode( %addr(docIn) : %len(%trimr(docIn)) : %addr(docOut) : %size(docOut) );

Also tried without success the SQL statement:
EXEC SQL VALUES qsys2.base64_decode( :docIn ) INTO :docOut;

Thank you,
Scott Klement
Site Admin
Posts: 796
Joined: Sun Jul 04, 2021 5:12 am

Re: Encode/decode base64 example

Post by Scott Klement »

I still don't know what is in the docIn field...

It sounds like part of it may be base64 encoded, and part of it may not? What does it look like?
sLucke1015
Posts: 18
Joined: Tue Mar 07, 2023 3:09 pm

Re: Encode/decode base64 example

Post by sLucke1015 »

Sorry again, here is the contents of DOCIN:

Code: Select all

"TU0AKgAAAAgAEAEAAAQAAAABAAAJwAEBAAQAAAABAAAMxgECAAMAAAABAAEAAAEDAAMAAAABAAQAAAEGAAMAAAABAAAAAAEKAAMAAAABAAEAAAERAAQ
... lots of data removed by scott...
///////////////////////////////////8AEAEA\u003d"
Scott Klement
Site Admin
Posts: 796
Joined: Sun Jul 04, 2021 5:12 am

Re: Encode/decode base64 example

Post by Scott Klement »

Ok, this appears to be a JSON string.

The quotes at the start/end are not valid in base64, nor is the \u003d at the end of the string. (And presumably it's possible that other documents may also have JSON escape sequences like that.)

You should decode the JSON so that all you have left is the base64 data, then feed that into base64_decode.
Scott Klement
Site Admin
Posts: 796
Joined: Sun Jul 04, 2021 5:12 am

Re: Encode/decode base64 example

Post by Scott Klement »

Here's an example using YAJLINTO (with DATA-INTO) to decode the JSON.

Code: Select all

**free
ctl-opt bnddir('BASE64') option(*srcstmt);
/if defined(*CRTBNDRPG)
ctl-opt dftactgrp(*no) actgrp(*NEW);
/endif

/copy ifsio_h
/copy base64_h

dcl-pr getSysErrno pointer extproc('__errno');
end-pr;

dcl-c mystmf   '/path/to/mystmf.json';
dcl-c tiffstmf '/path/to/result.tif';

dcl-s docin varchar(8000000) inz('');
dcl-s docout varchar(8000000) inz('');
dcl-s fd int(10);
dcl-s len int(10);
dcl-s err int(10) based(p_err);
dcl-s msgid char(7);

data-into docIn %data(mystmf:'doc=file') %parser('YAJLINTO');

%len(docOut) = %len(docOut:*MAX);

len = base64_decode( %addr(docIn:*data): %len(docIn)
                   : %addr(docOut:*data): %len(docOut:*MAX));

fd = open( tiffstmf
         : O_CREAT + O_WRONLY + O_EXCL + O_INHERITMODE
         : 0
         : 0 );

if fd = -1;
  // handle error
  p_err = getSysErrno();
  msgid = 'CPE' + %editc(%dec(err:4:0):'X');
  snd-msg *escape %msg(msgid: '*LIBL/QCPFMSG');
endif;

callp write(fd: %addr(docOut:*data): %len(docOut));
callp close(fd);

*inlr = *on;
sLucke1015
Posts: 18
Joined: Tue Mar 07, 2023 3:09 pm

Re: Encode/decode base64 example

Post by sLucke1015 »

Awesome! Thank you very much, Sir!
Post Reply