O Xoves, 26 de Novembro de 2009 ás 09:40:53 Chris Burdess escribiu:
> Jorge Barreiro wrote:
> > I've found what seems to be a bug in the gnu.inet.util.BASE64 class: The
> > encoding function never appends the '=' padding characters. I sent this
> > mail first because there's some code in the class dealing with this, but
> > it's commented, so I don't know if this is intentional. In addition, that
> > library wasn't updated in years. Is it deprecated? Is there any
> > alternative?
> >
> > This class is used in the gnu javax.mail implementation, and is
> > preventing me from authenticating with a server.
> >
> > If you confirm me that the padding characters should be there and this is
> > a bug, I can make and send you a patch.
>
> I'm pretty sure this was an old bug that was fixed a while ago. Check that
> you're using the most up-to-date release.
>
> If it's still not working, please feel free to send me a patch against CVS
> HEAD. --
> Chris Burdess
Hello again,
sorry for the delay. I've been busy, then the holidays...
I've checked the CVS head code and I've found the error: the length of the
target array was wrong. I've checked the history of this class too. It seems
that this issue was already detected, but the fix was wrong and was reverted.
I attach my patch (base64len.patch), which corrects the length of the target
array (so there's no need to truncate, I've removed that code), and
uncommented the code to fill with padding simbols again.
The second patch (base64dec.patch) is for the decode function. That function
works ok, but I detected something odd with the target array length. First, a
buffer with an arbitrary length is created, then it's truncated to the
correct size. But the length of the target array can be known beforehand,
there's no need to truncate the array at the end.
With this patch, the method will throw an arrayoutofindex exception if the
given array length is not divisible by 4, which I think is ok, since the
result would be incorrect.
Please, let me know if you are going to apply the patch (specially the first
one).
Index: source/gnu/inet/util/BASE64.java
===================================================================
RCS file: /sources/classpath/inetlib/source/gnu/inet/util/BASE64.java,v
retrieving revision 1.10
diff -u -r1.10 BASE64.java
--- source/gnu/inet/util/BASE64.java 25 Aug 2005 12:32:03 -0000 1.10
+++ source/gnu/inet/util/BASE64.java 8 Jan 2010 16:48:00 -0000
@@ -82,7 +82,8 @@
public static byte[] encode(byte[] bs)
{
int si = 0, ti = 0; // source/target array indices
- byte[] bt = new byte[((bs.length + 2) * 4) / 3]; // target byte array
+ /* target byte array */
+ byte[] bt = new byte[((bs.length + 2 - ((bs.length + 2) % 3)) * 4) / 3];
for (; si < bs.length; si += 3)
{
int buflen = bs.length - si;
@@ -111,16 +112,10 @@
bt[ti++] = src[b3 & 0x3f];
}
}
- if (ti < bt.length)
- {
- byte[] tmp = new byte[ti];
- System.arraycopy(bt, 0, tmp, 0, ti);
- bt = tmp;
- }
- /*while (ti < bt.length)
+ while (ti < bt.length)
{
bt[ti++] = 0x3d;
- }*/
+ }
return bt;
}
? Makefile
? classes
? config.log
? config.status
? inetlib.jar
Index: source/gnu/inet/util/BASE64.java
===================================================================
RCS file: /sources/classpath/inetlib/source/gnu/inet/util/BASE64.java,v
retrieving revision 1.10
diff -u -r1.10 BASE64.java
--- source/gnu/inet/util/BASE64.java 25 Aug 2005 12:32:03 -0000 1.10
+++ source/gnu/inet/util/BASE64.java 8 Jan 2010 17:45:56 -0000
@@ -131,12 +126,13 @@
*/
public static byte[] decode(byte[] bs)
{
- int srclen = bs.length;
- while (srclen > 0 && bs[srclen - 1] == 0x3d)
+ int padding = 0;
+ while (bs.length - padding > 0 && bs[bs.length - padding - 1] == 0x3d)
{
- srclen--; /* strip padding character */
+ padding++;
}
- byte[] buffer = new byte[srclen];
+ int srclen = bs.length - padding; /* strip padding characters */
+ byte[] buffer = new byte[(bs.length / 4) * 3 - padding]; /* target array */
int buflen = 0;
int si = 0;
int len = srclen - si;
@@ -159,9 +155,7 @@
}
len = srclen - si;
}
- byte[] bt = new byte[buflen];
- System.arraycopy(buffer, 0, bt, 0, buflen);
- return bt;
+ return buffer;
}
public static void main(String[] args)
_______________________________________________
Classpath-inetlib mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/classpath-inetlib