cURL / Mailing Lists / curl-library / Single Mail

curl-library

Curl_base64_decode not mod 3 issue

From: matthew b <mb7oo_at_hotmail.com>
Date: Thu, 19 Dec 2002 07:16:26 +0000

Hi. I needed a base64 decoder, and since I was using curl anyway I thought
I'd try base64.c in anticipation of it being part of the exported library.
(I say passing in a pre-existing buffer is OK for a base64 encode and decode
interface since the size is deterministic, but don't have the feel of the
curl interface enough to say if it's consistent.)

Anyway, the version on my machine (base64.c,v 1.14 2002/09/03) doesn't seem
to propertly handle the case when the size of the enencoded message is not a
multiple of 3. I made two small tweaks to fix it, and it seems to work now.

--Matthew
(not subscribed to this list, monitoring the web archive.)

--- /tmp/base64.c.orig Wed Dec 18 22:49:34 2002
+++ /tmp/base64.c Wed Dec 18 22:51:52 2002
@@ -61,6 +61,8 @@
       x = (x << 6) + 62;
     else if(src[i] == '/')
       x = (x << 6) + 63;
+ else if(src[i] == '=')
+ x = (x << 6);
   }

   dest[2] = (unsigned char)(x & 255); x >>= 8;
@@ -78,6 +80,7 @@
   int length = 0;
   int equalsTerm = 0;
   int i;
+ int numQuantums;
   unsigned char lastQuantum[3];

   while((src[length] != '=') && src[length])
@@ -85,10 +88,11 @@
   while(src[length+equalsTerm] == '=')
     equalsTerm++;

+ numQuantums = (length + equalsTerm) / 4;
   if(rawLength)
- *rawLength = (length * 3 / 4) - equalsTerm;
+ *rawLength = (numQuantums * 3) - equalsTerm;

- for(i = 0; i < length/4 - 1; i++) {
+ for(i = 0; i < numQuantums - 1; i++) {
     decodeQuantum(dest, src);
     dest += 3; src += 4;
   }

_________________________________________________________________
Add photos to your messages with MSN 8. Get 2 months FREE*.
http://join.msn.com/?page=features/featuredemail

-------------------------------------------------------
This SF.NET email is sponsored by: Order your Holiday Geek Presents Now!
Green Lasers, Hip Geek T-Shirts, Remote Control Tanks, Caffeinated Soap,
MP3 Players, XBox Games, Flying Saucers, WebCams, Smart Putty.
T H I N K G E E K . C O M http://www.thinkgeek.com/sf/
Received on 2002-12-19