Encode/decode base64 example
Encode/decode base64 example
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
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
-
- Site Admin
- Posts: 796
- Joined: Sun Jul 04, 2021 5:12 am
Re: Encode/decode base64 example
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?
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?
-
- Posts: 18
- Joined: Tue Mar 07, 2023 3:09 pm
Re: Encode/decode base64 example
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,
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,
-
- Site Admin
- Posts: 796
- Joined: Sun Jul 04, 2021 5:12 am
Re: Encode/decode base64 example
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?
-
- Posts: 18
- Joined: Tue Mar 07, 2023 3:09 pm
Re: Encode/decode base64 example
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,
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,
-
- Site Admin
- Posts: 796
- Joined: Sun Jul 04, 2021 5:12 am
Re: Encode/decode base64 example
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?
It sounds like part of it may be base64 encoded, and part of it may not? What does it look like?
-
- Posts: 18
- Joined: Tue Mar 07, 2023 3:09 pm
Re: Encode/decode base64 example
Sorry again, here is the contents of DOCIN:
Code: Select all
"TU0AKgAAAAgAEAEAAAQAAAABAAAJwAEBAAQAAAABAAAMxgECAAMAAAABAAEAAAEDAAMAAAABAAQAAAEGAAMAAAABAAAAAAEKAAMAAAABAAEAAAERAAQ
... lots of data removed by scott...
///////////////////////////////////8AEAEA\u003d"
-
- Site Admin
- Posts: 796
- Joined: Sun Jul 04, 2021 5:12 am
Re: Encode/decode base64 example
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.
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.
-
- Site Admin
- Posts: 796
- Joined: Sun Jul 04, 2021 5:12 am
Re: Encode/decode base64 example
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;
-
- Posts: 18
- Joined: Tue Mar 07, 2023 3:09 pm
Re: Encode/decode base64 example
Awesome! Thank you very much, Sir!