[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: BASE64



Thanks guys,

Yes you hit the nail on the head Thomas. I have a variety of different files of varying sizes. Some may be 15k, others may be 5mb. If I set a constant of 5mb, Murphy's law states that I'll get one that's bigger (too bad) and I'd prefer to avoid  excessive  allocation (5mb for one of the little guys). Therefore looking for method of approximating size of the space needed by the base64 routine on the fly.

Thanks for the help!

-David.

-----Original Message-----
From: ftpapi-bounces@xxxxxxxxxxxxxxxxxxxxxx [mailto:ftpapi-bounces@xxxxxxxxxxxxxxxxxxxxxx] On Behalf Of thomas.raddatz@xxxxxx
Sent: Thursday, January 30, 2014 12:22 AM
To: ftpapi@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: BASE64


Scott,

I assume that the problem is that David does not know that size of thedecoded data beforehand. The "5mb" value was just an example.

David, you may either allocate the size of the Base64 encoded data orroughly calculate the size of the output buffer based on the size of theinput 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 thesize of the encoded data is roughly 4 * n / 3. But the exact size isreturned by base64_decode().

The counterpart of getSizeBased64Decoded() is:

  // ---------------------------------------------------------
  //   Calculate size of Base64 encoded data.
  //   See: 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 inputsize.

For example (where the size of the input data is 13, 14, 15, 16, 17 and 18bytes):

> 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() areshown 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 largecharacter
> >     string, which is not ideal but has worked so far since I know thePDF'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 say5mb), 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:
> > 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
> ----------------------------------------------------------------------
> -


--
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.
-----------------------------------------------------------------------
This is the FTPAPI mailing list.  To unsubscribe, please go to:
http://www.scottklement.com/mailman/listinfo/ftpapi
-----------------------------------------------------------------------