[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: BASE64
Scott,
I assume that the problem is that David does not know that size of the
decoded data beforehand. The "5mb" value was just an example.
David, you may either allocate the size of the Base64 encoded data or
roughly calculate the size of the output buffer based on the size of
the input data. For the latter you may use the following procedure:
// ---------------------------------------------------------
// Calculate maximum size of Base64 decoded data.
// (Size may be up to 2 bytes more than required, because
// we do not know the value of the last 2 bytes of
// the Base64 encoded data.)
// ---------------------------------------------------------
P getSizeBased64Decoded...
P b
D pi 10I 0
D sizeEncoded 10I 0 value
/free
return (%int(sizeEncoded / 4) * 3);
/end-free
P e
For the first option, your buffer is always 33% too large, because the
size of the encoded data is roughly 4 * n / 3. But the exact size is
returned by base64_decode().
The counterpart of getSizeBased64Decoded() is:
// ---------------------------------------------------------
// Calculate size of Base64 encoded data.
// See: [1]http://de.wikipedia.org/wiki/Base64
// ---------------------------------------------------------
// Bei einem n Zeichen langen zu kodierenden Text beträgt
// der Platzbedarf für den Base64-kodierten Inhalt (ohne
// Zeilenumbrüche).
// Translated:
// For a given text of n bytes, the length of the Base64
// encoded output bytes is (without CRLF).
// sizeEnc = 4 * (n + 2 - ((n + 2) mod 3)) / 3
// ---------------------------------------------------------
P getSizeBased64Encoded...
P b
D pi 10I 0
D size 10I 0 value
/free
return (4 * (size+2 - (%rem((size+2): 3))) / 3);
/end-free
P e
It returns the exact size of the Base64 encoded data for a given input
size.
For example (where the size of the input data is 13, 14, 15, 16, 17 and
18 bytes):
> call example41 'GermanChars13.txt'
DSPLY Size of encoded data: 20 (expected:20/15)
> call example41 'GermanChars14.txt'
DSPLY Size of encoded data: 20 (expected:20/15)
> call example41 'GermanChars15.txt'
DSPLY Size of encoded data: 20 (expected:20/15)
> call example41 'GermanChars16.txt'
DSPLY Size of encoded data: 24 (expected:24/18)
> call example41 'GermanChars17.txt'
DSPLY Size of encoded data: 24 (expected:24/18)
> call example41 'GermanChars18.txt'
DSPLY Size of encoded data: 24 (expected:24/18)
The results of getSizeBased64Encoded() and getSizeBased64Decoded() are
shown after "expected:".
Thomas.
tpapi-bounces@xxxxxxxxxxxxxxxxxxxxxx schrieb am 30.01.2014 07:28:56:
> Von: sk@xxxxxxxxxxxxxxxx
> An: ftpapi@xxxxxxxxxxxxxxxxxxxxxx,
> Datum: 30.01.2014 07:47
> Betreff: Re: BASE64
> Gesendet von: ftpapi-bounces@xxxxxxxxxxxxxxxxxxxxxx
>
> David,
>
> As you mentioned in your e-mail, 'outsize' is the size of the output
> variable. So if your output variable is 5mb, then pass 5mb in
> outsize. If you allocate memory to a pointer, 'outsize' should be
the
> size of memory you allocated.
>
> You seem to know this already, so I'm not sure what you're asking?
>
> -SK
>
>
> On 1/29/2014 8:09 PM, David Baugh wrote:
> > Hi folks,
> >
> >
> > I'm getting a pdf using SOAP, then sending it through the
BASE64
> > decoder and saving it to file. It works fine as long as the pdf
is
> > small. I have my result variable outbuf defined as a large
character
> > string, which is not ideal but has worked so far since I know
the PDF's
> > I've been importing. Now I need to cater to larger PDF's.
> >
> >
> > I'm setting theBASE64 output size is the size of that variable.
> >
> >
> > From BASE64_H: OutSize = (input) size of area to store output
in
> >
> >
> > In order to cope with large PDF's (i.e. let's go crazy and say
5mb), If
> > I define the output variable as a pointer in my program, how do
I
> > determine Outsize on the call to BASE64?
> >
> >
> > Here's the snippet:
> >
> >
> > outlen = base64_decode( value.buf
> >
> > : value.len
> >
> > : %addr(outbuf)
> >
> > : %size(outbuf) );
> >
> >
> > callp write(fd3: %addr(outbuf): outlen);
> >
> >
> > Thanks,
> >
> >
> > -David.
> >
> >
> >
> >
-----------------------------------------------------------------------
> > This is the FTPAPI mailing list. To unsubscribe, please go to:
> > [2]http://www.scottklement.com/mailman/listinfo/ftpapi
> >
-----------------------------------------------------------------------
>
>
-----------------------------------------------------------------------
> This is the FTPAPI mailing list. To unsubscribe, please go to:
> [3]http://www.scottklement.com/mailman/listinfo/ftpapi
>
-----------------------------------------------------------------------
--
IMPORTANT NOTICE:
This email is confidential, may be legally privileged, and is for the
intended recipient only. Access, disclosure, copying, distribution, or
reliance on any of it by anyone else is prohibited and may be a
criminal
offence. Please delete if obtained in error and email confirmation to
the sender.
References
1. http://de.wikipedia.org/wiki/Base64
2. http://www.scottklement.com/mailman/listinfo/ftpapi
3. 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
-----------------------------------------------------------------------