bug-gnulib
[Top][All Lists]
Advanced

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

[bug-gnulib] Re: base64


From: Simon Josefsson
Subject: [bug-gnulib] Re: base64
Date: Sat, 27 Nov 2004 16:54:21 +0100
User-agent: Gnus/5.110003 (No Gnus v0.3) Emacs/21.3.50 (gnu/linux)

Stepan Kasal <address@hidden> writes:

> Hi,
>
> On Sat, Nov 27, 2004 at 01:42:38PM +0100, Simon Josefsson wrote:
>> The goal is to compute:
>> 
>> #define BASE64_LENGTH(inlen) ((((inlen) + 2) / 3) * 4)
>
> I suggest the following:
>
> base64_encode_alloc (const char *in, size_t inlen, char **out)
> {
>   size_t outlen = ((inlen + 2) / 3) * 4;

That's good, then I can use BASE64_LENGTH directly.

>   if (inlen > outlen)
>       /* overflow! */
>
> Proof:
> If there is no overflow, outlen >= inlen.
>
> If the operation (inlen + 2) overflows then it yields at most +1, so
> outlen is 0.
>
> If the multiplication overflows, we lose at least half of the correct
> value, so the result is < ((inlen + 2) / 3) * 2, which is less than
> (inlen + 2) * 0.66667, which is less than inlen as soon as (inlen > 4).
> \qed
>
> You can put the proof in a comment above the check, if you please.

If there is no objection, I'll use this.  Btw, does your proof require
that SIZE_MAX is a power of two?  (For the "we lost at least half of
the correct value".)

Thanks.





reply via email to

[Prev in Thread] Current Thread [Next in Thread]