Commit bd8a2e96 by Casey Marshall Committed by Andreas Tobler

Author e-mail updated for all files.

2004-08-30  Casey Marshall  <csm@gnu.org>

	Author e-mail updated for all files.
	* gnu/java/security/OID.java (equals): Test if the aurgment is an
	instance of OID.
	(compareTo): Use `equals'.
	* gnu/java/security/der/BitString.java (equals): Test if the
	argument is an instance of BitString.
	* gnu/java/security/der/DERReader.java: Removed NIO imports.  Made
	class final. Made fields private.
	(<init>): New constructor.
	(skip): New method.
	(makeString): Made static; don't use NIO.
	(fromIso88591, fromUtf16Be, fromUtf8): New methods.
	* gnu/java/security/der/DERWriter.java: Fixed imports.
	(writeString): Don't use NIO.
	(toIso88591, toUtf16Be, toUtf8): New methods.
	* gnu/java/security/der/DERValue.java: Formatting changes only.
	* gnu/java/security/der/DER.java: Likewise.

From-SVN: r86765
parent 39880d37
2004-08-30 Casey Marshall <csm@gnu.org>
Author e-mail updated for all files.
* gnu/java/security/OID.java (equals): Test if the aurgment is an
instance of OID.
(compareTo): Use `equals'.
* gnu/java/security/der/BitString.java (equals): Test if the
argument is an instance of BitString.
* gnu/java/security/der/DERReader.java: Removed NIO imports. Made
class final. Made fields private.
(<init>): New constructor.
(skip): New method.
(makeString): Made static; don't use NIO.
(fromIso88591, fromUtf16Be, fromUtf8): New methods.
* gnu/java/security/der/DERWriter.java: Fixed imports.
(writeString): Don't use NIO.
(toIso88591, toUtf16Be, toUtf8): New methods.
* gnu/java/security/der/DERValue.java: Formatting changes only.
* gnu/java/security/der/DER.java: Likewise.
2004-08-30 Tom Tromey <tromey@redhat.com> 2004-08-30 Tom Tromey <tromey@redhat.com>
* java/nio/CharBuffer.java (put): Fix typo. * java/nio/CharBuffer.java (put): Fix typo.
......
...@@ -7,7 +7,7 @@ GNU Classpath is free software; you can redistribute it and/or modify ...@@ -7,7 +7,7 @@ GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option) the Free Software Foundation; either version 2, or (at your option)
any later version. any later version.
GNU Classpath is distributed in the hope that it will be useful, but GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
...@@ -65,7 +65,7 @@ import gnu.java.security.der.DEREncodingException; ...@@ -65,7 +65,7 @@ import gnu.java.security.der.DEREncodingException;
* <p>OIDs may be relative, in which case the first two elements of the * <p>OIDs may be relative, in which case the first two elements of the
* OID are omitted. * OID are omitted.
* *
* @author Casey Marshall (rsdio@metastatic.org) * @author Casey Marshall (csm@gnu.org)
*/ */
public class OID implements Cloneable, Comparable, java.io.Serializable public class OID implements Cloneable, Comparable, java.io.Serializable
{ {
...@@ -336,7 +336,7 @@ public class OID implements Cloneable, Comparable, java.io.Serializable ...@@ -336,7 +336,7 @@ public class OID implements Cloneable, Comparable, java.io.Serializable
/* Nice idea, but possibly too expensive for whatever benefit it /* Nice idea, but possibly too expensive for whatever benefit it
* provides. * provides.
public String getShortName() public String getShortName()
{ {
return OIDTable.getShortName(this); return OIDTable.getShortName(this);
...@@ -391,8 +391,8 @@ public class OID implements Cloneable, Comparable, java.io.Serializable ...@@ -391,8 +391,8 @@ public class OID implements Cloneable, Comparable, java.io.Serializable
*/ */
public boolean equals(Object o) public boolean equals(Object o)
{ {
if (this == o) if (!(o instanceof OID))
return true; return false;
return java.util.Arrays.equals(components, ((OID) o).components); return java.util.Arrays.equals(components, ((OID) o).components);
} }
...@@ -411,7 +411,7 @@ public class OID implements Cloneable, Comparable, java.io.Serializable ...@@ -411,7 +411,7 @@ public class OID implements Cloneable, Comparable, java.io.Serializable
*/ */
public int compareTo(Object o) public int compareTo(Object o)
{ {
if (o == this) if (equals(o))
return 0; return 0;
int[] components2 = ((OID) o).components; int[] components2 = ((OID) o).components;
int len = Math.min(components.length, components2.length); int len = Math.min(components.length, components2.length);
......
...@@ -7,7 +7,7 @@ GNU Classpath is free software; you can redistribute it and/or modify ...@@ -7,7 +7,7 @@ GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option) the Free Software Foundation; either version 2, or (at your option)
any later version. any later version.
GNU Classpath is distributed in the hope that it will be useful, but GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
...@@ -51,9 +51,9 @@ import java.util.Arrays; ...@@ -51,9 +51,9 @@ import java.util.Arrays;
* <p>Where the "xxx" represents three bits that should be ignored, and * <p>Where the "xxx" represents three bits that should be ignored, and
* can have any value. * can have any value.
* *
* @author Casey Marshall (rsdio@metastatic.org) * @author Casey Marshall (csm@gnu.org)
*/ */
public class BitString implements Cloneable, Comparable, java.io.Serializable public class BitString implements Cloneable, Comparable
{ {
// Fields. // Fields.
...@@ -288,8 +288,8 @@ public class BitString implements Cloneable, Comparable, java.io.Serializable ...@@ -288,8 +288,8 @@ public class BitString implements Cloneable, Comparable, java.io.Serializable
public boolean equals(Object o) public boolean equals(Object o)
{ {
if (this == o) if (!(o instanceof BitString))
return true; return false;
BitString that = (BitString) o; BitString that = (BitString) o;
// True for cloned instances. // True for cloned instances.
if (this.bytes == that.bytes && this.ignoredBits == that.ignoredBits) if (this.bytes == that.bytes && this.ignoredBits == that.ignoredBits)
......
...@@ -7,7 +7,7 @@ GNU Classpath is free software; you can redistribute it and/or modify ...@@ -7,7 +7,7 @@ GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option) the Free Software Foundation; either version 2, or (at your option)
any later version. any later version.
GNU Classpath is distributed in the hope that it will be useful, but GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
...@@ -41,7 +41,7 @@ package gnu.java.security.der; ...@@ -41,7 +41,7 @@ package gnu.java.security.der;
/** /**
* The set of tags for DER types. * The set of tags for DER types.
* *
* @author Casey Marshall (rsdio@metastatic.org) * @author Casey Marshall (csm@gnu.org)
*/ */
public interface DER public interface DER
{ {
......
...@@ -7,7 +7,7 @@ GNU Classpath is free software; you can redistribute it and/or modify ...@@ -7,7 +7,7 @@ GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option) the Free Software Foundation; either version 2, or (at your option)
any later version. any later version.
GNU Classpath is distributed in the hope that it will be useful, but GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
...@@ -47,11 +47,6 @@ import java.io.IOException; ...@@ -47,11 +47,6 @@ import java.io.IOException;
import java.math.BigInteger; import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.TimeZone; import java.util.TimeZone;
...@@ -65,17 +60,17 @@ import gnu.java.security.OID; ...@@ -65,17 +60,17 @@ import gnu.java.security.OID;
* to the calling application to determine if the data are structured * to the calling application to determine if the data are structured
* properly by inspecting the {@link DERValue} that is returned. * properly by inspecting the {@link DERValue} that is returned.
* *
* @author Casey Marshall (rsdio@metastatic.org) * @author Casey Marshall (csm@gnu.org)
*/ */
public class DERReader implements DER public final class DERReader implements DER
{ {
// Fields. // Fields.
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
protected InputStream in; private InputStream in;
protected final ByteArrayOutputStream encBuf; private final ByteArrayOutputStream encBuf;
// Constructor. // Constructor.
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
...@@ -90,6 +85,11 @@ public class DERReader implements DER ...@@ -90,6 +85,11 @@ public class DERReader implements DER
this(new ByteArrayInputStream(in)); this(new ByteArrayInputStream(in));
} }
public DERReader (byte[] in, int off, int len)
{
this (new ByteArrayInputStream (in, off, len));
}
/** /**
* Create a new DER readed from an input stream. * Create a new DER readed from an input stream.
* *
...@@ -123,6 +123,11 @@ public class DERReader implements DER ...@@ -123,6 +123,11 @@ public class DERReader implements DER
// Instance methods. // Instance methods.
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
public void skip (int bytes) throws IOException
{
in.skip (bytes);
}
/** /**
* Decode a single value from the input stream, returning it in a new * Decode a single value from the input stream, returning it in a new
* {@link DERValue}. By "single value" we mean any single type in its * {@link DERValue}. By "single value" we mean any single type in its
...@@ -251,10 +256,9 @@ public class DERReader implements DER ...@@ -251,10 +256,9 @@ public class DERReader implements DER
throw new DEREncodingException(); throw new DEREncodingException();
} }
private String makeString(int tag, byte[] value) private static String makeString(int tag, byte[] value)
throws IOException throws IOException
{ {
Charset charset = null;
switch (tag & 0x1F) switch (tag & 0x1F)
{ {
case NUMERIC_STRING: case NUMERIC_STRING:
...@@ -265,28 +269,81 @@ public class DERReader implements DER ...@@ -265,28 +269,81 @@ public class DERReader implements DER
case GRAPHIC_STRING: case GRAPHIC_STRING:
case ISO646_STRING: case ISO646_STRING:
case GENERAL_STRING: case GENERAL_STRING:
charset = Charset.forName("ISO-8859-1"); return fromIso88591(value);
break;
case UNIVERSAL_STRING: case UNIVERSAL_STRING:
// XXX The docs say UniversalString is encoded in four bytes // XXX The docs say UniversalString is encoded in four bytes
// per character, but Java has no support (yet) for UTF-32. // per character, but Java has no support (yet) for UTF-32.
//return new String(buf, "UTF-32"); //return new String(buf, "UTF-32");
case BMP_STRING: case BMP_STRING:
charset = Charset.forName("UTF-16BE"); return fromUtf16Be(value);
break;
case UTF8_STRING: case UTF8_STRING:
charset = Charset.forName("UTF-8"); return fromUtf8(value);
break;
default: default:
throw new DEREncodingException("unknown string tag"); throw new DEREncodingException("unknown string tag");
} }
if (charset == null) }
throw new DEREncodingException("no decoder");
CharsetDecoder decoder = charset.newDecoder(); private static String fromIso88591(byte[] bytes)
CharBuffer result = decoder.decode(ByteBuffer.wrap(value)); {
char[] buf = new char[result.remaining()]; StringBuffer str = new StringBuffer(bytes.length);
result.get(buf); for (int i = 0; i < bytes.length; i++)
return new String(buf); str.append((char) (bytes[i] & 0xFF));
return str.toString();
}
private static String fromUtf16Be(byte[] bytes) throws IOException
{
if ((bytes.length & 0x01) != 0)
throw new IOException("UTF-16 bytes are odd in length");
StringBuffer str = new StringBuffer(bytes.length / 2);
for (int i = 0; i < bytes.length; i += 2)
{
char c = (char) ((bytes[i] << 8) & 0xFF);
c |= (char) (bytes[i+1] & 0xFF);
str.append(c);
}
return str.toString();
}
private static String fromUtf8(byte[] bytes) throws IOException
{
StringBuffer str = new StringBuffer((int)(bytes.length / 1.5));
for (int i = 0; i < bytes.length; )
{
char c = 0;
if ((bytes[i] & 0xE0) == 0xE0)
{
if ((i + 2) >= bytes.length)
throw new IOException("short UTF-8 input");
c = (char) ((bytes[i++] & 0x0F) << 12);
if ((bytes[i] & 0x80) != 0x80)
throw new IOException("malformed UTF-8 input");
c |= (char) ((bytes[i++] & 0x3F) << 6);
if ((bytes[i] & 0x80) != 0x80)
throw new IOException("malformed UTF-8 input");
c |= (char) (bytes[i++] & 0x3F);
}
else if ((bytes[i] & 0xC0) == 0xC0)
{
if ((i + 1) >= bytes.length)
throw new IOException("short input");
c = (char) ((bytes[i++] & 0x1F) << 6);
if ((bytes[i] & 0x80) != 0x80)
throw new IOException("malformed UTF-8 input");
c |= (char) (bytes[i++] & 0x3F);
}
else if ((bytes[i] & 0xFF) < 0x80)
{
c = (char) (bytes[i++] & 0xFF);
}
else
throw new IOException("badly formed UTF-8 sequence");
str.append(c);
}
return str.toString();
} }
private Date makeTime(int tag, byte[] value) throws IOException private Date makeTime(int tag, byte[] value) throws IOException
......
...@@ -7,7 +7,7 @@ GNU Classpath is free software; you can redistribute it and/or modify ...@@ -7,7 +7,7 @@ GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option) the Free Software Foundation; either version 2, or (at your option)
any later version. any later version.
GNU Classpath is distributed in the hope that it will be useful, but GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
...@@ -48,15 +48,10 @@ public class DERValue implements DER ...@@ -48,15 +48,10 @@ public class DERValue implements DER
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
private final int tagClass; private final int tagClass;
private final boolean constructed; private final boolean constructed;
private final int tag; private final int tag;
private int length; private int length;
private final Object value; private final Object value;
private byte[] encoded; private byte[] encoded;
// Constructor. // Constructor.
......
...@@ -7,7 +7,7 @@ GNU Classpath is free software; you can redistribute it and/or modify ...@@ -7,7 +7,7 @@ GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option) the Free Software Foundation; either version 2, or (at your option)
any later version. any later version.
GNU Classpath is distributed in the hope that it will be useful, but GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
...@@ -43,12 +43,12 @@ import gnu.java.security.OID; ...@@ -43,12 +43,12 @@ import gnu.java.security.OID;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.math.BigInteger; import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.BitSet;
import java.util.Date; import java.util.Date;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
...@@ -66,7 +66,7 @@ import java.util.TimeZone; ...@@ -66,7 +66,7 @@ import java.util.TimeZone;
* <p>This class only defines static methods; there are no instance * <p>This class only defines static methods; there are no instance
* variables needed. * variables needed.
* *
* @author Casey Marshall (rsdio@metastatic.org) * @author Casey Marshall (csm@gnu.org)
*/ */
public class DERWriter implements DER public class DERWriter implements DER
{ {
...@@ -82,7 +82,7 @@ public class DERWriter implements DER ...@@ -82,7 +82,7 @@ public class DERWriter implements DER
// Class methods. // Class methods.
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
public static int write(OutputStream out, DERValue object) public static int write(OutputStream out, DERValue object)
throws IOException throws IOException
{ {
out.write(object.getExternalTag()); out.write(object.getExternalTag());
...@@ -226,7 +226,6 @@ public class DERWriter implements DER ...@@ -226,7 +226,6 @@ public class DERWriter implements DER
private static int writeString(OutputStream out, int tag, String str) private static int writeString(OutputStream out, int tag, String str)
throws IOException throws IOException
{ {
Charset charset = null;
byte[] b = null; byte[] b = null;
switch (tag & 0x1F) switch (tag & 0x1F)
{ {
...@@ -238,33 +237,65 @@ public class DERWriter implements DER ...@@ -238,33 +237,65 @@ public class DERWriter implements DER
case GRAPHIC_STRING: case GRAPHIC_STRING:
case ISO646_STRING: case ISO646_STRING:
case GENERAL_STRING: case GENERAL_STRING:
charset = Charset.forName("ISO-8859-1"); b = toIso88591(str);
break; break;
case UNIVERSAL_STRING: case UNIVERSAL_STRING:
case BMP_STRING: case BMP_STRING:
charset = Charset.forName("UTF-16BE"); b = toUtf16Be(str);
break; break;
case UTF8_STRING: case UTF8_STRING:
default: default:
charset = Charset.forName("UTF-8"); b = toUtf8(str);
break; break;
} }
if (charset == null) writeLength(out, b.length);
throw new DEREncodingException("no charset"); out.write(b);
CharsetEncoder encoder = charset.newEncoder(); return b.length;
ByteBuffer result = encoder.encode(CharBuffer.wrap(str)); }
if (result.hasArray())
private static byte[] toIso88591(String string)
{
byte[] result = new byte[string.length()];
for (int i = 0; i < string.length(); i++)
result[i] = (byte) string.charAt(i);
return result;
}
private static byte[] toUtf16Be(String string)
{
byte[] result = new byte[string.length() * 2];
for (int i = 0; i < string.length(); i++)
{ {
b = result.array(); result[i*2 ] = (byte) ((string.charAt(i) >>> 8) & 0xFF);
result[i*2+1] = (byte) (string.charAt(i) & 0xFF);
} }
else return result;
}
private static byte[] toUtf8(String string)
{
ByteArrayOutputStream buf =
new ByteArrayOutputStream((int)(string.length() * 1.5));
for (int i = 0; i < string.length(); i++)
{ {
b = new byte[result.remaining()]; char c = string.charAt(i);
result.get(b); if (c < 0x0080)
buf.write(c & 0xFF);
else if (c < 0x0800)
{
buf.write(0xC0 | ((c >>> 6) & 0x3F));
buf.write(0x80 | (c & 0x3F));
}
else
{
buf.write(0xE0 | ((c >>> 12) & 0x0F));
buf.write(0x80 | ((c >>> 6) & 0x3F));
buf.write(0x80 | (c & 0x3F));
}
} }
writeLength(out, b.length); return buf.toByteArray();
out.write(b);
return b.length;
} }
private static int writeDate(OutputStream out, int tag, Date date) private static int writeDate(OutputStream out, int tag, Date date)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment