This is the mail archive of the
java@gcc.gnu.org
mailing list for the Java project.
BigInteger(byte[]) constructor bugfix.
- To: <java at gcc dot gnu dot org>
- Subject: BigInteger(byte[]) constructor bugfix.
- From: "Mark J. Roberts" <mjr at statesmean dot com>
- Date: Sun, 1 Apr 2001 01:06:03 -0500 (EST)
BigInteger(byte[]) currently returns bogus data, due to a bug in the
byteArrayToIntArray method -- it appears to be simply wrong.
Here's a test case illustrating the problem:
public class Test {
public static void main(String[] args)
{
byte val[] = {73, -106, 2, -46}; // aka "1234567890"
int words[] = byteArrayToIntArray(val, val[0] < 0 ? -1 : 0);
System.out.print(words.length+" words:");
for (int i = 0 ; i < words.length ; i++) System.out.print(" "+words[i]);
System.out.println("");
}
private static int[] byteArrayToIntArray(byte[] bytes, int sign)
{
// Determine number of words needed.
int[] words = new int[(bytes.length + 3) / 4 + 1];
int nwords = words.length;
// For simplicity, tack on an extra word of sign at the front,
// it will be canonicalized out later. */
if ((bytes.length % 4) != 0) words[--nwords] = sign;
// Create a int out of modulo 4 high order bytes.
int bptr = 0;
int word = sign;
for (int i = bytes.length % 4; i > 0; --i, bptr++)
word = (word << 8) | (((int) bytes[bptr]) & 0xff);
words[--nwords] = word;
// Elements remaining in byte[] are a multiple of 4.
while (nwords > 0)
words[--nwords] = bytes[bptr++] << 24 |
(((int) bytes[bptr++]) & 0xff) << 16 |
(((int) bytes[bptr++]) & 0xff) << 8 |
(((int) bytes[bptr++]) & 0xff);
return words;
}
}
Anyway, this patch fixed it (Freenet crypto works now):
Index: libjava/java/math/BigInteger.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/math/BigInteger.java,v
retrieving revision 1.10.4.1
diff -r1.10.4.1 BigInteger.java
223c223
< int[] words = new int[(bytes.length + 3) / 4 + 1];
---
> int[] words = new int[bytes.length/4 + 1];
225,228d224
<
< // For simplicity, tack on an extra word of sign at the front,
< // it will be canonicalized out later. */
< words[--nwords] = sign;
--
Satan, oscillate my metallic sonatas!
Mark Roberts | mjr@statesmean.com