[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
RE: Base64 decode of a PNG file
In this post, Scott showed how to set the length of the result (outputData).
http://forums.iprodeveloper.com/forums/aft/148487
After the call to decode, the length of the output is in declen - not %len(OutputData). So you need to use declen as the length.
-----Original Message-----
From: ftpapi-bounces@xxxxxxxxxxxxxxxxxxxxxx [mailto:ftpapi-bounces@xxxxxxxxxxxxxxxxxxxxxx] On Behalf Of Thomas Raddatz
Sent: Monday, February 23, 2015 11:37 AM
To: HTTPAPI and FTPAPI Projects
Subject: Re: Base64 decode of a PNG file
Radu,
As far as I can see, 'OutputData' is a "varying" field:
D OutputData s 32767A varying
Hence you need to add *DATA to the %addr() function:
callp write(fd: %addr(OutputData: *DATA): %len(OutputData));
Thomas.
Am 23.02.2015 um 17:10 schrieb Radu Botescu:
> Hello,
> I call a Fedex webservice in order to retrieve a document in PNG format
> and print it.
> But this document is base64 encoded.
> Thanks to Scott's HTTPAPI I can call the webservice and parse the right
> XML element into a file in IFS.
> So at this time I have a binary file in IFS:
> - /tmp/fedex.bin ==> CCSID 819
> - if I copy/paste it in a online internet decoder I have no problem
> in getting back the PNG
> - but on the AS400 I have no luck:
> - I've tried the openssl (opened QSHELL and put "openssl base64 -d
> -in /tmp/fedex.bin -out /tmp/fedex.png ") ==> the result is a
> fedex.png with 0 bytes;
> - I've tried Scott's base64_decode (reading the IFS and writing the
> result in IFS) ==> same result, a fedex.png with 0 bytes;
> What I'm doing wrong...? :(
> Thank you in advance for your help.
> Here below the program using Scott's base64_decode (sorry in advance if
> my code is a mess...it is only a draft version..proof of concept kind
> of...):
> H DFTACTGRP(*NO) BNDDIR('BASE64':'QC2LE')
>
>
>
> /copy BASE64_H
>
> /copy httpapi_h
>
> /copy ifsio_h
>
>
>
>
>
> D BASE64DATA s 72A DIM(43) CTDATA
> PERRCD(1)
> D ReportError PR
>
>
>
> D IFS_file2Var pr 65535a varying
>
> D pFile 256a value
>
>
>
> D InputData s 32767A varying
>
> D OutputData s 32767A varying
>
> d decdata s 32764a
>
> d decoded s 32764a
>
> d declen s 10i 0
>
> d len s 10i 0
>
> D outputfile s 200A
>
> D outfile s 10I 0
>
> d rc s 10i 0
>
> d i s 10i 0
>
> D fd S 10I 0
>
>
>
> // Declare the files
>
> InputData = IFS_file2Var('/tmp/fedex.bin');
>
> len = %len(InputData);
>
>
>
> outputfile = '/tmp/fedex.png';
>
>
>
> // Prepare the OUT file
>
> fd = open(outputfile
>
> : O_WRONLY+O_CREAT+O_TRUNC+O_CCSID
>
> : S_IRGRP + S_IWGRP + S_IXGRP +
>
> S_IRUSR + S_IWUSR + S_IXUSR
>
> : 819);
>
> callp close(fd);
>
> fd = open(outputfile:O_WRONLY+O_TEXTDATA);
>
>
>
> // decode InputData
>
> declen = base64_decode(%addr(InputData : *DATA )
>
> : len
>
> : %addr(OutputData : *DATA )
>
> : %len(OutputData));
>
>
>
> // write it
>
> callp write(fd: %addr(OutputData): %len(OutputData));
>
> callp close(fd);
>
>
>
> *inlr = *on;
>
>
>
> **** IFS ==> variable pgm
>
> P IFS_file2Var b export
>
> D IFS_file2Var pi 65535a varying
>
> D pFile 256a value
>
>
>
> D open pr 10i 0 extproc('open')
>
> D filename * value
> options(*string)
> D openflags 10i 0 value
>
> D mode 10u 0 value
> options(*nopass)
> D codepage 10u 0 value options(*nopass)
>
> D creatcnvid 10u 0 value options(*nopass)
>
>
>
> D read pr 10i 0 extproc('read')
>
> D handle 10i 0 value
>
> D buffer * value
>
> D bytes 10u 0 value
>
>
>
> D close pr 10i 0 extproc('close')
>
> D handle 10i 0 value
>
> D data s 65535a
>
> D bytesRead s 10i 0
>
> D handle s 10i 0
>
> D rc s 10i 0
>
> D O_RDONLY c 1
>
> D O_TEXTDATA c 16777216
>
>
>
>
>
>
>
> handle = open(%trim(pFile): O_RDONLY+O_TEXTDATA);
>
> if handle < 0;
>
> // something is wrong
>
> return '';
>
> endif;
>
> bytesRead = read(handle: %addr(data): %size(data));
>
> if bytesRead <= 0;
>
> return '';
>
> endIf;
>
> rc = close(handle);
>
> return %trim(data);
>
>
> P e
>
>
>
> ----------------------------------------------------------------------
> - This is the FTPAPI mailing list. To unsubscribe, please go to:
> http://www.scottklement.com/mailman/listinfo/ftpapi
> ----------------------------------------------------------------------
> -
>
-----------------------------------------------------------------------
This is the FTPAPI mailing list. To unsubscribe, please go to:
http://www.scottklement.com/mailman/listinfo/ftpapi
-----------------------------------------------------------------------
-----------------------------------------------------------------------
This is the FTPAPI mailing list. To unsubscribe, please go to:
http://www.scottklement.com/mailman/listinfo/ftpapi
-----------------------------------------------------------------------