From 635629d0271dfb0128af61a307642ed02eea4a17 Mon Sep 17 00:00:00 2001 From: Endi Sukma Dewata Date: Tue, 13 Dec 2011 16:51:00 -0600 Subject: Renamed byte-to-char & char-to-byte converters. The byte-to-char and char-to-byte converters have been renamed in preparation for converting them into charset encoders and decoders. Ticket #3 --- .../netscape/security/util/ASN1CharStrConvMap.java | 10 +- .../security/util/ByteToCharIA5String.java | 69 -------- .../security/util/ByteToCharPrintable.java | 88 ---------- .../netscape/security/util/ByteToCharUnicode.java | 187 --------------------- .../security/util/ByteToCharUniversalString.java | 99 ----------- .../security/util/CharToByteIA5String.java | 88 ---------- .../security/util/CharToBytePrintable.java | 122 -------------- .../security/util/CharToByteUniversalString.java | 85 ---------- .../netscape/security/util/IA5CharsetDecoder.java | 69 ++++++++ .../netscape/security/util/IA5CharsetEncoder.java | 88 ++++++++++ .../security/util/PrintableCharsetDecoder.java | 88 ++++++++++ .../security/util/PrintableCharsetEncoder.java | 122 ++++++++++++++ .../security/util/UniversalCharsetDecoder.java | 99 +++++++++++ .../security/util/UniversalCharsetEncoder.java | 85 ++++++++++ 14 files changed, 556 insertions(+), 743 deletions(-) delete mode 100644 pki/base/util/src/netscape/security/util/ByteToCharIA5String.java delete mode 100644 pki/base/util/src/netscape/security/util/ByteToCharPrintable.java delete mode 100644 pki/base/util/src/netscape/security/util/ByteToCharUnicode.java delete mode 100644 pki/base/util/src/netscape/security/util/ByteToCharUniversalString.java delete mode 100644 pki/base/util/src/netscape/security/util/CharToByteIA5String.java delete mode 100644 pki/base/util/src/netscape/security/util/CharToBytePrintable.java delete mode 100644 pki/base/util/src/netscape/security/util/CharToByteUniversalString.java create mode 100644 pki/base/util/src/netscape/security/util/IA5CharsetDecoder.java create mode 100644 pki/base/util/src/netscape/security/util/IA5CharsetEncoder.java create mode 100644 pki/base/util/src/netscape/security/util/PrintableCharsetDecoder.java create mode 100644 pki/base/util/src/netscape/security/util/PrintableCharsetEncoder.java create mode 100644 pki/base/util/src/netscape/security/util/UniversalCharsetDecoder.java create mode 100644 pki/base/util/src/netscape/security/util/UniversalCharsetEncoder.java (limited to 'pki') diff --git a/pki/base/util/src/netscape/security/util/ASN1CharStrConvMap.java b/pki/base/util/src/netscape/security/util/ASN1CharStrConvMap.java index da0fd45c5..4fa7b4ea4 100644 --- a/pki/base/util/src/netscape/security/util/ASN1CharStrConvMap.java +++ b/pki/base/util/src/netscape/security/util/ASN1CharStrConvMap.java @@ -184,11 +184,11 @@ public class ASN1CharStrConvMap static { defaultMap = new ASN1CharStrConvMap(); defaultMap.addEntry(DerValue.tag_PrintableString, - (Class)CharToBytePrintable.class, (Class)ByteToCharPrintable.class); + PrintableCharsetEncoder.class, PrintableCharsetDecoder.class); defaultMap.addEntry(DerValue.tag_VisibleString, - CharToBytePrintable.class, ByteToCharPrintable.class); + PrintableCharsetEncoder.class, PrintableCharsetDecoder.class); defaultMap.addEntry(DerValue.tag_IA5String, - CharToByteIA5String.class, ByteToCharIA5String.class); + IA5CharsetEncoder.class, IA5CharsetDecoder.class); defaultMap.addEntry(DerValue.tag_BMPString, // Changed by bskim //sun.io.CharToByteUnicode.class, @@ -197,8 +197,8 @@ public class ASN1CharStrConvMap sun.io.ByteToCharUnicodeBig.class); // Change end defaultMap.addEntry(DerValue.tag_UniversalString, - CharToByteUniversalString.class, - ByteToCharUniversalString.class); + UniversalCharsetEncoder.class, + UniversalCharsetDecoder.class); // XXX this is an oversimplified implementation of T.61 strings, it // doesn't handle all cases defaultMap.addEntry(DerValue.tag_T61String, diff --git a/pki/base/util/src/netscape/security/util/ByteToCharIA5String.java b/pki/base/util/src/netscape/security/util/ByteToCharIA5String.java deleted file mode 100644 index 69fab22a7..000000000 --- a/pki/base/util/src/netscape/security/util/ByteToCharIA5String.java +++ /dev/null @@ -1,69 +0,0 @@ -// --- BEGIN COPYRIGHT BLOCK --- -// This program is free software; you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation; version 2 of the License. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License along -// with this program; if not, write to the Free Software Foundation, Inc., -// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -// -// (C) 2007 Red Hat, Inc. -// All rights reserved. -// --- END COPYRIGHT BLOCK --- -package netscape.security.util; - -import sun.io.ByteToCharConverter; -import sun.io.ConversionBufferFullException; -import sun.io.UnknownCharacterException; - -/** - * Converts bytes in ASN.1 IA5String character set to unicode - * characters. - * - * @author Lily Hsiao - * @author Slava Galperin - */ - -public class ByteToCharIA5String extends ByteToCharConverter -{ - public String getCharacterEncoding() { - return "ASN.1 IA5String"; - } - - public int convert(byte[] input, int inStart, int inEnd, - char[] output, int outStart, int outEnd) - throws ConversionBufferFullException, - UnknownCharacterException - { - int j = outStart; - for (int i = inStart; i < inEnd; i++, j++) { - if (j >= outEnd) { - byteOff = i; - charOff = j; - throw new ConversionBufferFullException(); - } - if (!subMode && (input[i] & 0x80) != 0) { - byteOff = i; - charOff = j; - badInputLength = 1; - throw new UnknownCharacterException(); - } - output[j] = (char) (input[i] & 0x7f); - } - byteOff = inEnd; - charOff = j; - return j - outStart; - } - - public int flush(char[] output, int outStart, int outEnd) { - return 0; - } - - public void reset() { } - -} diff --git a/pki/base/util/src/netscape/security/util/ByteToCharPrintable.java b/pki/base/util/src/netscape/security/util/ByteToCharPrintable.java deleted file mode 100644 index 0607ad2e8..000000000 --- a/pki/base/util/src/netscape/security/util/ByteToCharPrintable.java +++ /dev/null @@ -1,88 +0,0 @@ -// --- BEGIN COPYRIGHT BLOCK --- -// This program is free software; you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation; version 2 of the License. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License along -// with this program; if not, write to the Free Software Foundation, Inc., -// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -// -// (C) 2007 Red Hat, Inc. -// All rights reserved. -// --- END COPYRIGHT BLOCK --- -package netscape.security.util; - -import sun.io.ByteToCharConverter; -import sun.io.ConversionBufferFullException; -import sun.io.MalformedInputException; -import sun.io.UnknownCharacterException; - -/** - * Converts bytes in ASN.1 Printable String character set to unicode - * characters. - * - * @author Lily Hsiao - * @author Slava Galperin - */ - -public class ByteToCharPrintable extends ByteToCharConverter -{ - - public String getCharacterEncoding() - { - return "ASN.1 Printable"; - } - - public int convert(byte[] input, int inStart, int inEnd, - char[] output, int outStart, int outEnd) - throws MalformedInputException, - UnknownCharacterException, - ConversionBufferFullException - { - int j = outStart; - boolean hasNonPrintableChar = false; - - for (int i = inStart; i < inEnd; i++, j++) { - if (j >= outEnd) { - byteOff = i; - charOff = j; - throw new ConversionBufferFullException(); - } - if (!subMode && - !CharToBytePrintable.isPrintableChar((char) (input[i] & 0x7f))) { - /* "bug" fix for 359010 - byteOff = i; - charOff = j; - badInputLength = 1; - throw new UnknownCharacterException(); - */ - j--; - hasNonPrintableChar = true; - } else - output[j] = (char) (input[i] & 0x7f); - } - - if (hasNonPrintableChar == true) { - // - } - - byteOff = inEnd; - charOff = j; - return j - outStart; - } - - public int flush( char[] output, int outStart, int outEnd ) - throws MalformedInputException, ConversionBufferFullException - { - return 0; - } - - public void reset() { } - - -} diff --git a/pki/base/util/src/netscape/security/util/ByteToCharUnicode.java b/pki/base/util/src/netscape/security/util/ByteToCharUnicode.java deleted file mode 100644 index 312b8a22b..000000000 --- a/pki/base/util/src/netscape/security/util/ByteToCharUnicode.java +++ /dev/null @@ -1,187 +0,0 @@ -// --- BEGIN COPYRIGHT BLOCK --- -// This program is free software; you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation; version 2 of the License. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License along -// with this program; if not, write to the Free Software Foundation, Inc., -// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -// -// (C) 2007 Red Hat, Inc. -// All rights reserved. -// --- END COPYRIGHT BLOCK --- -package netscape.security.util; -import sun.io.ByteToCharUnicodeBig; -import sun.io.ByteToCharUnicodeLittle; -import sun.io.ConversionBufferFullException; -import sun.io.MalformedInputException; - -/** - * Convert byte arrays containing Unicode characters into arrays of actual - * Unicode characters, sensing the byte order automatically. To force a - * particular byte order, use either the "UnicodeBig" or the "UnicodeLittle" - * encoding. - * - * If the first character is a byte order mark, it will be interpreted and - * discarded. Otherwise, the byte order is assumed to be BigEndian. - * Either way, the byte order is decided by the first character. Later - * byte order marks will be passed through as characters (if they indicate - * the same byte order) or will cause an error (if they indicate the other - * byte order). - * - * @see ByteToCharUnicodeLittle - * @see ByteToCharUnicodeBig - * - * @version 1.3, 96/11/23 - * @author Mark Reinhold - */ - -public class ByteToCharUnicode extends sun.io.ByteToCharConverter { - - static final char BYTE_ORDER_MARK = (char) 0xfeff; - static final char REVERSED_MARK = (char) 0xfffe; - - static final int AUTO = 0; - static final int BIG = 1; - static final int LITTLE = 2; - - int byteOrder; - - public ByteToCharUnicode() { - byteOrder = AUTO; - } - - public String getCharacterEncoding() { - switch (byteOrder) { - case BIG: return "UnicodeBig"; - case LITTLE: return "UnicodeLittle"; - default: return "Unicode"; - } - } - - boolean started = false; - int leftOverByte; - boolean leftOver = false; - - public int convert(byte[] in, int inOff, int inEnd, - char[] out, int outOff, int outEnd) - throws ConversionBufferFullException, MalformedInputException - { - byteOff = inOff; - charOff = outOff; - - if (inOff >= inEnd) - return 0; - - int b1, b2; - int bc = 0; - int inI = inOff, outI = outOff; - - if (leftOver) { - b1 = leftOverByte & 0xff; - leftOver = false; - } - else - b1 = in[inI++] & 0xff; - bc = 1; - - if (!started) { /* Read possible initial byte-order mark */ - if (inI < inEnd) { - b2 = in[inI++] & 0xff; - bc = 2; - - char c = (char) ((b1 << 8) | b2); - int bo = AUTO; - - if (c == BYTE_ORDER_MARK) - bo = BIG; - else if (c == REVERSED_MARK) - bo = LITTLE; - - if (byteOrder == AUTO) { - if (bo == AUTO) { - bo = BIG; // BigEndian by default - } - byteOrder = bo; - if (inI < inEnd) { - b1 = in[inI++] & 0xff; - bc = 1; - } - } - else if (bo == AUTO) { - inI--; - bc = 1; - } - else if (byteOrder == bo) { - if (inI < inEnd) { - b1 = in[inI++] & 0xff; - bc = 1; - } - } - else { - badInputLength = bc; - throw new - MalformedInputException("Incorrect byte-order mark"); - } - - started = true; - } - } - - /* Loop invariant: (b1 contains the next input byte) && (bc == 1) */ - while (inI < inEnd) { - b2 = in[inI++] & 0xff; - bc = 2; - - char c; - if (byteOrder == BIG) - c = (char) ((b1 << 8) | b2); - else - c = (char) ((b2 << 8) | b1); - - if (c == REVERSED_MARK) - throw new - MalformedInputException("Reversed byte-order mark"); - - if (outI >= outEnd) - throw new ConversionBufferFullException(); - out[outI++] = c; - byteOff = inI; - charOff = outI; - - if (inI < inEnd) { - b1 = in[inI++] & 0xff; - bc = 1; - } - } - - if (bc == 1) { - leftOverByte = b1; - leftOver = true; - } - - return outI - outOff; - } - - public void reset() { - leftOver = false; - byteOff = charOff = 0; - } - - public int flush(char buf[], int off, int len) - throws MalformedInputException - { - if (leftOver) { - reset(); - throw new MalformedInputException(); - } - byteOff = charOff = 0; - return 0; - } - -} diff --git a/pki/base/util/src/netscape/security/util/ByteToCharUniversalString.java b/pki/base/util/src/netscape/security/util/ByteToCharUniversalString.java deleted file mode 100644 index 77165b7fe..000000000 --- a/pki/base/util/src/netscape/security/util/ByteToCharUniversalString.java +++ /dev/null @@ -1,99 +0,0 @@ -// --- BEGIN COPYRIGHT BLOCK --- -// This program is free software; you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation; version 2 of the License. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License along -// with this program; if not, write to the Free Software Foundation, Inc., -// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -// -// (C) 2007 Red Hat, Inc. -// All rights reserved. -// --- END COPYRIGHT BLOCK --- -package netscape.security.util; - -import sun.io.ByteToCharConverter; -import sun.io.ConversionBufferFullException; -import sun.io.UnknownCharacterException; - -/** - * Converts bytes in ASN.1 UniversalString character set to unicode - * characters. - * - * @author Lily Hsiao - * @author Slava Galperin - */ - -public class ByteToCharUniversalString extends ByteToCharConverter -{ - public String getCharacterEncoding() { - return "ASN.1 UniversalString"; - } - - public int convert(byte[] input, int inStart, int inEnd, - char[] output, int outStart, int outEnd) - throws ConversionBufferFullException, - UnknownCharacterException - { - int j = outStart; - - - int i = inStart; - while(i < inEnd) { - // XXX we do not know what to do with truly UCS-4 characters here - // we also assumed network byte order - - if ( i+3 >= inEnd || - (!((input[i] == 0 && input[i+1] == 0) || - (input[i+2] == 0 && input[i+3] == 0)))) { - byteOff = i; - charOff = j; - throw new UnknownCharacterException(); - } - if (input[i+2] == 0 && input[i+3] == 0) { - // Try to be a bit forgiving. If the byte order is - // reversed, we still try handle it. - - // Sample Date Set (1): - // 0000000 f 0 \0 \0 213 0 \0 \0 S 0 \0 \0 - // 0000014 - - // Sample Date Set (2): - // 0000000 w \0 \0 \0 w \0 \0 \0 w \0 \0 \0 . \0 \0 \0 - // 0000020 ( \0 \0 \0 t \0 \0 \0 o \0 \0 \0 b \0 \0 \0 - // 0000040 e \0 \0 \0 | \0 \0 \0 n \0 \0 \0 o \0 \0 \0 - // 0000060 t \0 \0 \0 t \0 \0 \0 o \0 \0 \0 b \0 \0 \0 - // 0000100 e \0 \0 \0 ) \0 \0 \0 . \0 \0 \0 c \0 \0 \0 - // 0000120 o \0 \0 \0 m \0 \0 \0 - // 0000130 - output[j] = (char)(((input[i+1] << 8)& 0xff00) + (input[i] & 0x00ff)); - } else { - // This should be the right order. - // - // 0000000 0000 00c4 0000 0064 0000 006d 0000 0069 - // 0000020 0000 006e 0000 0020 0000 0051 0000 0041 - // 0000040 - - // (input[i] == 0 && input[i+1] == 0) - output[j] = (char)(((input[i+2] << 8)& 0xff00) + (input[i+3] & 0x00ff)); - } - j++; - i += 4; - } - byteOff = inEnd; - charOff = j; - return j - outStart; - } - - public int flush(char[] output, int outStart, int outEnd) { - return 0; - } - - public void reset() { } - -} diff --git a/pki/base/util/src/netscape/security/util/CharToByteIA5String.java b/pki/base/util/src/netscape/security/util/CharToByteIA5String.java deleted file mode 100644 index f7c0d1e2d..000000000 --- a/pki/base/util/src/netscape/security/util/CharToByteIA5String.java +++ /dev/null @@ -1,88 +0,0 @@ -// --- BEGIN COPYRIGHT BLOCK --- -// This program is free software; you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation; version 2 of the License. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License along -// with this program; if not, write to the Free Software Foundation, Inc., -// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -// -// (C) 2007 Red Hat, Inc. -// All rights reserved. -// --- END COPYRIGHT BLOCK --- -package netscape.security.util; - -import sun.io.CharToByteConverter; -import sun.io.ConversionBufferFullException; -import sun.io.UnknownCharacterException; - -/** - * Converts a string of ASN.1 IA5String characters to IA5String bytes. - * - * @author Lily Hsiao - * @author Slava Galperin - */ - -public class CharToByteIA5String extends CharToByteConverter -{ - /* - * Returns the character set id for the conversion. - * @return the character set id. - */ - public String getCharacterEncoding () { - return "ASN.1 IA5String"; - } - - /* - * Converts an array of Unicode characters into an array of IA5String - * bytes and returns the total number of characters converted. - * If conversion cannot be done, UnknownCharacterException is - * thrown. The character and byte offset will be set to the point - * of the unknown character. - * @param input character array to convert. - * @param inStart offset from which to start the conversion. - * @param inEnd where to end the conversion. - * @param output byte array to store converted bytes. - * @param outStart starting offset in the output byte array. - * @param outEnd ending offset in the output byte array. - * @return the number of characters converted. - */ - public int convert(char[] input, int inStart, int inEnd, - byte[] output, int outStart, int outEnd) - throws ConversionBufferFullException, - UnknownCharacterException - { - int j = outStart; - for (int i = inStart; i < inEnd; i++, j++) { - if (j >= outEnd) { - charOff = i; - byteOff = j; - throw new ConversionBufferFullException(); - } - if (!subMode && (input[i] & 0xFF80) != 0) { - charOff = i; - byteOff = j; - badInputLength = 1; - throw new UnknownCharacterException(); - } - - output[j] = (byte) (input[i] & 0x7f); - } - return j - outStart; - } - - public int flush(byte[] output, int outStart, int outEnd) { - return 0; - } - - public void reset() { } - - public int getMaxBytesPerChar() { - return 1; - } -} diff --git a/pki/base/util/src/netscape/security/util/CharToBytePrintable.java b/pki/base/util/src/netscape/security/util/CharToBytePrintable.java deleted file mode 100644 index 970f7782f..000000000 --- a/pki/base/util/src/netscape/security/util/CharToBytePrintable.java +++ /dev/null @@ -1,122 +0,0 @@ -// --- BEGIN COPYRIGHT BLOCK --- -// This program is free software; you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation; version 2 of the License. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License along -// with this program; if not, write to the Free Software Foundation, Inc., -// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -// -// (C) 2007 Red Hat, Inc. -// All rights reserved. -// --- END COPYRIGHT BLOCK --- -package netscape.security.util; - -import sun.io.CharToByteConverter; -import sun.io.ConversionBufferFullException; -import sun.io.MalformedInputException; -import sun.io.UnknownCharacterException; - -/** - * Converts a string of ASN.1 PrintableString characters to PrintableString - * bytes. - * - * @author Lily Hsiao - * @author Slava Galperin - */ - -public class CharToBytePrintable extends CharToByteConverter -{ - /* - * returns the character set id for the conversion. - * @return the character set id. - */ - public String getCharacterEncoding() - { - return "ASN.1 Printable"; - } - - public static boolean isPrintableChar( char c ) - { - if ((c < 'A' || c > 'Z') && - (c < 'a' || c > 'z') && - (c < '0' || c > '9') && - (c != ' ') && - (c != '\'') && - (c != '(') && - (c != ')') && - (c != '+') && - (c != ',') && - (c != '-') && - (c != '.') && - (c != '/') && - (c != ':') && - (c != '=') && - (c != '?')) - { - return false; - } else { - return true; - } - } - - /* - * Converts an array of Unicode characters into an array of Printable - * String bytes and returns the total number of characters converted. - * If conversion cannot be done, UnknownCharacterException is - * thrown. The character and byte offset will be set to the point - * of the unknown character. - * @param input character array to convert. - * @param inStart offset from which to start the conversion. - * @param inEnd where to end the conversion. - * @param output byte array to store converted bytes. - * @param outStart starting offset in the output byte array. - * @param outEnd ending offset in the output byte array. - * @return the number of characters converted. - */ - public int convert(char[] input, int inStart, int inEnd, - byte[] output, int outStart, int outEnd) - throws MalformedInputException, UnknownCharacterException, - ConversionBufferFullException - { - int j = outStart; - int i; - for (i = inStart; i < inEnd ; i++, j++) - { - if (j >= outEnd) { - charOff = i; - byteOff = j; - throw new ConversionBufferFullException(); - } - if (!subMode && !isPrintableChar(input[i])) { - charOff = i; - byteOff = j; - badInputLength = 1; - throw new UnknownCharacterException(); - } - output[j] = (byte) (input[i] & 0x7f); - } - charOff = i; - byteOff = j; - return j - outStart; - } - - public int flush(byte[] output, int outStart, int outEnd) - throws MalformedInputException, ConversionBufferFullException - { - return 0; - } - - public void reset() { } - - public int getMaxBytesPerChar() - { - return 1; - } - -} diff --git a/pki/base/util/src/netscape/security/util/CharToByteUniversalString.java b/pki/base/util/src/netscape/security/util/CharToByteUniversalString.java deleted file mode 100644 index 0d566d539..000000000 --- a/pki/base/util/src/netscape/security/util/CharToByteUniversalString.java +++ /dev/null @@ -1,85 +0,0 @@ -// --- BEGIN COPYRIGHT BLOCK --- -// This program is free software; you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation; version 2 of the License. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License along -// with this program; if not, write to the Free Software Foundation, Inc., -// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -// -// (C) 2007 Red Hat, Inc. -// All rights reserved. -// --- END COPYRIGHT BLOCK --- -package netscape.security.util; - -import sun.io.CharToByteConverter; -import sun.io.ConversionBufferFullException; -import sun.io.UnknownCharacterException; - -/** - * Converts a string of ASN.1 IA5String characters to IA5String bytes. - * - * @author Lily Hsiao - * @author Slava Galperin - */ - -public class CharToByteUniversalString extends CharToByteConverter -{ - /* - * Returns the character set id for the conversion. - * @return the character set id. - */ - public String getCharacterEncoding () { - return "ASN.1 UniversalString"; - } - - /* - * Converts an array of Unicode characters into an array of UniversalString - * bytes and returns the total number of characters converted. - * If conversion cannot be done, UnknownCharacterException is - * thrown. The character and byte offset will be set to the point - * of the unknown character. - * @param input character array to convert. - * @param inStart offset from which to start the conversion. - * @param inEnd where to end the conversion. - * @param output byte array to store converted bytes. - * @param outStart starting offset in the output byte array. - * @param outEnd ending offset in the output byte array. - * @return the number of characters converted. - */ - public int convert(char[] input, int inStart, int inEnd, - byte[] output, int outStart, int outEnd) - throws ConversionBufferFullException, - UnknownCharacterException - { - int j = outStart; - for (int i = inStart; i < inEnd; i++) { - if (j+3 >= outEnd) { - charOff = i; - byteOff = j; - throw new ConversionBufferFullException(); - } - output[j++] = 0; - output[j++] = 0; - output[j++] = (byte) ((input[i] >> 8) & 0xff); - output[j++] = (byte) (input[i] & 0xff); - } - - return j - outStart; - } - - public int flush(byte[] output, int outStart, int outEnd) { - return 0; - } - - public void reset() { } - - public int getMaxBytesPerChar() { - return 4; - } -} diff --git a/pki/base/util/src/netscape/security/util/IA5CharsetDecoder.java b/pki/base/util/src/netscape/security/util/IA5CharsetDecoder.java new file mode 100644 index 000000000..d884b9e8c --- /dev/null +++ b/pki/base/util/src/netscape/security/util/IA5CharsetDecoder.java @@ -0,0 +1,69 @@ +// --- BEGIN COPYRIGHT BLOCK --- +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; version 2 of the License. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// (C) 2007 Red Hat, Inc. +// All rights reserved. +// --- END COPYRIGHT BLOCK --- +package netscape.security.util; + +import sun.io.ByteToCharConverter; +import sun.io.ConversionBufferFullException; +import sun.io.UnknownCharacterException; + +/** + * Converts bytes in ASN.1 IA5String character set to unicode + * characters. + * + * @author Lily Hsiao + * @author Slava Galperin + */ + +public class IA5CharsetDecoder extends ByteToCharConverter +{ + public String getCharacterEncoding() { + return "ASN.1 IA5String"; + } + + public int convert(byte[] input, int inStart, int inEnd, + char[] output, int outStart, int outEnd) + throws ConversionBufferFullException, + UnknownCharacterException + { + int j = outStart; + for (int i = inStart; i < inEnd; i++, j++) { + if (j >= outEnd) { + byteOff = i; + charOff = j; + throw new ConversionBufferFullException(); + } + if (!subMode && (input[i] & 0x80) != 0) { + byteOff = i; + charOff = j; + badInputLength = 1; + throw new UnknownCharacterException(); + } + output[j] = (char) (input[i] & 0x7f); + } + byteOff = inEnd; + charOff = j; + return j - outStart; + } + + public int flush(char[] output, int outStart, int outEnd) { + return 0; + } + + public void reset() { } + +} diff --git a/pki/base/util/src/netscape/security/util/IA5CharsetEncoder.java b/pki/base/util/src/netscape/security/util/IA5CharsetEncoder.java new file mode 100644 index 000000000..3901ec4ec --- /dev/null +++ b/pki/base/util/src/netscape/security/util/IA5CharsetEncoder.java @@ -0,0 +1,88 @@ +// --- BEGIN COPYRIGHT BLOCK --- +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; version 2 of the License. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// (C) 2007 Red Hat, Inc. +// All rights reserved. +// --- END COPYRIGHT BLOCK --- +package netscape.security.util; + +import sun.io.CharToByteConverter; +import sun.io.ConversionBufferFullException; +import sun.io.UnknownCharacterException; + +/** + * Converts a string of ASN.1 IA5String characters to IA5String bytes. + * + * @author Lily Hsiao + * @author Slava Galperin + */ + +public class IA5CharsetEncoder extends CharToByteConverter +{ + /* + * Returns the character set id for the conversion. + * @return the character set id. + */ + public String getCharacterEncoding () { + return "ASN.1 IA5String"; + } + + /* + * Converts an array of Unicode characters into an array of IA5String + * bytes and returns the total number of characters converted. + * If conversion cannot be done, UnknownCharacterException is + * thrown. The character and byte offset will be set to the point + * of the unknown character. + * @param input character array to convert. + * @param inStart offset from which to start the conversion. + * @param inEnd where to end the conversion. + * @param output byte array to store converted bytes. + * @param outStart starting offset in the output byte array. + * @param outEnd ending offset in the output byte array. + * @return the number of characters converted. + */ + public int convert(char[] input, int inStart, int inEnd, + byte[] output, int outStart, int outEnd) + throws ConversionBufferFullException, + UnknownCharacterException + { + int j = outStart; + for (int i = inStart; i < inEnd; i++, j++) { + if (j >= outEnd) { + charOff = i; + byteOff = j; + throw new ConversionBufferFullException(); + } + if (!subMode && (input[i] & 0xFF80) != 0) { + charOff = i; + byteOff = j; + badInputLength = 1; + throw new UnknownCharacterException(); + } + + output[j] = (byte) (input[i] & 0x7f); + } + return j - outStart; + } + + public int flush(byte[] output, int outStart, int outEnd) { + return 0; + } + + public void reset() { } + + public int getMaxBytesPerChar() { + return 1; + } +} diff --git a/pki/base/util/src/netscape/security/util/PrintableCharsetDecoder.java b/pki/base/util/src/netscape/security/util/PrintableCharsetDecoder.java new file mode 100644 index 000000000..7ffd9cdc6 --- /dev/null +++ b/pki/base/util/src/netscape/security/util/PrintableCharsetDecoder.java @@ -0,0 +1,88 @@ +// --- BEGIN COPYRIGHT BLOCK --- +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; version 2 of the License. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// (C) 2007 Red Hat, Inc. +// All rights reserved. +// --- END COPYRIGHT BLOCK --- +package netscape.security.util; + +import sun.io.ByteToCharConverter; +import sun.io.ConversionBufferFullException; +import sun.io.MalformedInputException; +import sun.io.UnknownCharacterException; + +/** + * Converts bytes in ASN.1 Printable String character set to unicode + * characters. + * + * @author Lily Hsiao + * @author Slava Galperin + */ + +public class PrintableCharsetDecoder extends ByteToCharConverter +{ + + public String getCharacterEncoding() + { + return "ASN.1 Printable"; + } + + public int convert(byte[] input, int inStart, int inEnd, + char[] output, int outStart, int outEnd) + throws MalformedInputException, + UnknownCharacterException, + ConversionBufferFullException + { + int j = outStart; + boolean hasNonPrintableChar = false; + + for (int i = inStart; i < inEnd; i++, j++) { + if (j >= outEnd) { + byteOff = i; + charOff = j; + throw new ConversionBufferFullException(); + } + if (!subMode && + !PrintableCharsetEncoder.isPrintableChar((char) (input[i] & 0x7f))) { + /* "bug" fix for 359010 + byteOff = i; + charOff = j; + badInputLength = 1; + throw new UnknownCharacterException(); + */ + j--; + hasNonPrintableChar = true; + } else + output[j] = (char) (input[i] & 0x7f); + } + + if (hasNonPrintableChar == true) { + // + } + + byteOff = inEnd; + charOff = j; + return j - outStart; + } + + public int flush( char[] output, int outStart, int outEnd ) + throws MalformedInputException, ConversionBufferFullException + { + return 0; + } + + public void reset() { } + + +} diff --git a/pki/base/util/src/netscape/security/util/PrintableCharsetEncoder.java b/pki/base/util/src/netscape/security/util/PrintableCharsetEncoder.java new file mode 100644 index 000000000..351e0668e --- /dev/null +++ b/pki/base/util/src/netscape/security/util/PrintableCharsetEncoder.java @@ -0,0 +1,122 @@ +// --- BEGIN COPYRIGHT BLOCK --- +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; version 2 of the License. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// (C) 2007 Red Hat, Inc. +// All rights reserved. +// --- END COPYRIGHT BLOCK --- +package netscape.security.util; + +import sun.io.CharToByteConverter; +import sun.io.ConversionBufferFullException; +import sun.io.MalformedInputException; +import sun.io.UnknownCharacterException; + +/** + * Converts a string of ASN.1 PrintableString characters to PrintableString + * bytes. + * + * @author Lily Hsiao + * @author Slava Galperin + */ + +public class PrintableCharsetEncoder extends CharToByteConverter +{ + /* + * returns the character set id for the conversion. + * @return the character set id. + */ + public String getCharacterEncoding() + { + return "ASN.1 Printable"; + } + + public static boolean isPrintableChar( char c ) + { + if ((c < 'A' || c > 'Z') && + (c < 'a' || c > 'z') && + (c < '0' || c > '9') && + (c != ' ') && + (c != '\'') && + (c != '(') && + (c != ')') && + (c != '+') && + (c != ',') && + (c != '-') && + (c != '.') && + (c != '/') && + (c != ':') && + (c != '=') && + (c != '?')) + { + return false; + } else { + return true; + } + } + + /* + * Converts an array of Unicode characters into an array of Printable + * String bytes and returns the total number of characters converted. + * If conversion cannot be done, UnknownCharacterException is + * thrown. The character and byte offset will be set to the point + * of the unknown character. + * @param input character array to convert. + * @param inStart offset from which to start the conversion. + * @param inEnd where to end the conversion. + * @param output byte array to store converted bytes. + * @param outStart starting offset in the output byte array. + * @param outEnd ending offset in the output byte array. + * @return the number of characters converted. + */ + public int convert(char[] input, int inStart, int inEnd, + byte[] output, int outStart, int outEnd) + throws MalformedInputException, UnknownCharacterException, + ConversionBufferFullException + { + int j = outStart; + int i; + for (i = inStart; i < inEnd ; i++, j++) + { + if (j >= outEnd) { + charOff = i; + byteOff = j; + throw new ConversionBufferFullException(); + } + if (!subMode && !isPrintableChar(input[i])) { + charOff = i; + byteOff = j; + badInputLength = 1; + throw new UnknownCharacterException(); + } + output[j] = (byte) (input[i] & 0x7f); + } + charOff = i; + byteOff = j; + return j - outStart; + } + + public int flush(byte[] output, int outStart, int outEnd) + throws MalformedInputException, ConversionBufferFullException + { + return 0; + } + + public void reset() { } + + public int getMaxBytesPerChar() + { + return 1; + } + +} diff --git a/pki/base/util/src/netscape/security/util/UniversalCharsetDecoder.java b/pki/base/util/src/netscape/security/util/UniversalCharsetDecoder.java new file mode 100644 index 000000000..6a997af99 --- /dev/null +++ b/pki/base/util/src/netscape/security/util/UniversalCharsetDecoder.java @@ -0,0 +1,99 @@ +// --- BEGIN COPYRIGHT BLOCK --- +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; version 2 of the License. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// (C) 2007 Red Hat, Inc. +// All rights reserved. +// --- END COPYRIGHT BLOCK --- +package netscape.security.util; + +import sun.io.ByteToCharConverter; +import sun.io.ConversionBufferFullException; +import sun.io.UnknownCharacterException; + +/** + * Converts bytes in ASN.1 UniversalString character set to unicode + * characters. + * + * @author Lily Hsiao + * @author Slava Galperin + */ + +public class UniversalCharsetDecoder extends ByteToCharConverter +{ + public String getCharacterEncoding() { + return "ASN.1 UniversalString"; + } + + public int convert(byte[] input, int inStart, int inEnd, + char[] output, int outStart, int outEnd) + throws ConversionBufferFullException, + UnknownCharacterException + { + int j = outStart; + + + int i = inStart; + while(i < inEnd) { + // XXX we do not know what to do with truly UCS-4 characters here + // we also assumed network byte order + + if ( i+3 >= inEnd || + (!((input[i] == 0 && input[i+1] == 0) || + (input[i+2] == 0 && input[i+3] == 0)))) { + byteOff = i; + charOff = j; + throw new UnknownCharacterException(); + } + if (input[i+2] == 0 && input[i+3] == 0) { + // Try to be a bit forgiving. If the byte order is + // reversed, we still try handle it. + + // Sample Date Set (1): + // 0000000 f 0 \0 \0 213 0 \0 \0 S 0 \0 \0 + // 0000014 + + // Sample Date Set (2): + // 0000000 w \0 \0 \0 w \0 \0 \0 w \0 \0 \0 . \0 \0 \0 + // 0000020 ( \0 \0 \0 t \0 \0 \0 o \0 \0 \0 b \0 \0 \0 + // 0000040 e \0 \0 \0 | \0 \0 \0 n \0 \0 \0 o \0 \0 \0 + // 0000060 t \0 \0 \0 t \0 \0 \0 o \0 \0 \0 b \0 \0 \0 + // 0000100 e \0 \0 \0 ) \0 \0 \0 . \0 \0 \0 c \0 \0 \0 + // 0000120 o \0 \0 \0 m \0 \0 \0 + // 0000130 + output[j] = (char)(((input[i+1] << 8)& 0xff00) + (input[i] & 0x00ff)); + } else { + // This should be the right order. + // + // 0000000 0000 00c4 0000 0064 0000 006d 0000 0069 + // 0000020 0000 006e 0000 0020 0000 0051 0000 0041 + // 0000040 + + // (input[i] == 0 && input[i+1] == 0) + output[j] = (char)(((input[i+2] << 8)& 0xff00) + (input[i+3] & 0x00ff)); + } + j++; + i += 4; + } + byteOff = inEnd; + charOff = j; + return j - outStart; + } + + public int flush(char[] output, int outStart, int outEnd) { + return 0; + } + + public void reset() { } + +} diff --git a/pki/base/util/src/netscape/security/util/UniversalCharsetEncoder.java b/pki/base/util/src/netscape/security/util/UniversalCharsetEncoder.java new file mode 100644 index 000000000..89c2a98b9 --- /dev/null +++ b/pki/base/util/src/netscape/security/util/UniversalCharsetEncoder.java @@ -0,0 +1,85 @@ +// --- BEGIN COPYRIGHT BLOCK --- +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; version 2 of the License. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// (C) 2007 Red Hat, Inc. +// All rights reserved. +// --- END COPYRIGHT BLOCK --- +package netscape.security.util; + +import sun.io.CharToByteConverter; +import sun.io.ConversionBufferFullException; +import sun.io.UnknownCharacterException; + +/** + * Converts a string of ASN.1 IA5String characters to IA5String bytes. + * + * @author Lily Hsiao + * @author Slava Galperin + */ + +public class UniversalCharsetEncoder extends CharToByteConverter +{ + /* + * Returns the character set id for the conversion. + * @return the character set id. + */ + public String getCharacterEncoding () { + return "ASN.1 UniversalString"; + } + + /* + * Converts an array of Unicode characters into an array of UniversalString + * bytes and returns the total number of characters converted. + * If conversion cannot be done, UnknownCharacterException is + * thrown. The character and byte offset will be set to the point + * of the unknown character. + * @param input character array to convert. + * @param inStart offset from which to start the conversion. + * @param inEnd where to end the conversion. + * @param output byte array to store converted bytes. + * @param outStart starting offset in the output byte array. + * @param outEnd ending offset in the output byte array. + * @return the number of characters converted. + */ + public int convert(char[] input, int inStart, int inEnd, + byte[] output, int outStart, int outEnd) + throws ConversionBufferFullException, + UnknownCharacterException + { + int j = outStart; + for (int i = inStart; i < inEnd; i++) { + if (j+3 >= outEnd) { + charOff = i; + byteOff = j; + throw new ConversionBufferFullException(); + } + output[j++] = 0; + output[j++] = 0; + output[j++] = (byte) ((input[i] >> 8) & 0xff); + output[j++] = (byte) (input[i] & 0xff); + } + + return j - outStart; + } + + public int flush(byte[] output, int outStart, int outEnd) { + return 0; + } + + public void reset() { } + + public int getMaxBytesPerChar() { + return 4; + } +} -- cgit