You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xerces.apache.org by el...@apache.org on 2001/04/09 22:59:01 UTC
cvs commit: xml-xerces/java/src/org/apache/xerces/utils Base64.java HexBin.java
elena 01/04/09 13:59:01
Modified: java/src/org/apache/xerces/utils Base64.java HexBin.java
Log:
Applied patches from Sandy Gao (Schema PR changes)
Revision Changes Path
1.5 +180 -61 xml-xerces/java/src/org/apache/xerces/utils/Base64.java
Index: Base64.java
===================================================================
RCS file: /home/cvs/xml-xerces/java/src/org/apache/xerces/utils/Base64.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- Base64.java 2000/08/02 16:36:20 1.4
+++ Base64.java 2001/04/09 20:59:01 1.5
@@ -62,18 +62,18 @@
/**
* This class provides encode/decode for RFC 2045 Base64 as
- * defined by RFC 2045, N. Freed and N. Borenstein.
+ * defined by RFC 2045, N. Freed and N. Borenstein.
* RFC 2045: Multipurpose Internet Mail Extensions (MIME)
* Part One: Format of Internet Message Bodies. Reference
- * 1996 Available at: http://www.ietf.org/rfc/rfc2045.txt
+ * 1996 Available at: http://www.ietf.org/rfc/rfc2045.txt
* This class is used by XML Schema binary format validation
*
* @author Jeffrey Rodriguez
- * @version $Id: Base64.java,v 1.4 2000/08/02 16:36:20 jeffreyr Exp $
+ * @version $Id: Base64.java,v 1.5 2001/04/09 20:59:01 elena Exp $
*/
public final class Base64 {
- static private final int BASELENGTH = 255;
+ static private final int BASELENGTH = 255;
static private final int LOOKUPLENGTH = 64;
static private final int TWENTYFOURBITGROUP = 24;
static private final int EIGHTBIT = 8;
@@ -83,14 +83,14 @@
static private final int SIGN = -128;
static private final byte PAD = ( byte ) '=';
static private final boolean fDebug = false;
- static private byte [] base64Alphabet = new byte[BASELENGTH];
+ static private byte [] base64Alphabet = new byte[BASELENGTH];
static private byte [] lookUpBase64Alphabet = new byte[LOOKUPLENGTH];
static {
for (int i = 0; i<BASELENGTH; i++ ) {
- base64Alphabet[i] = -1;
+ base64Alphabet[i] = -1;
}
for ( int i = 'Z'; i >= 'A'; i-- ) {
base64Alphabet[i] = (byte) (i-'A');
@@ -103,7 +103,7 @@
base64Alphabet[i] = (byte) (i-'0' + 52);
}
- base64Alphabet['+'] = 62;
+ base64Alphabet['+'] = 62;
base64Alphabet['/'] = 63;
for (int i = 0; i<=25; i++ )
@@ -119,6 +119,18 @@
}
+ protected static boolean isWS (byte octect) {
+ return (octect == 0x20 || octect == 0xd || octect == 0xa || octect == 0x9);
+ }
+
+ protected static boolean isPad (byte octect) {
+ return (octect == PAD);
+ }
+
+ protected static boolean isData (byte octect) {
+ return (base64Alphabet[octect] != -1);
+ }
+
public static boolean isBase64( String isValidString ){
return( isArrayByteBase64( isValidString.getBytes()));
}
@@ -126,30 +138,38 @@
public static boolean isBase64( byte octect ) {
//shall we ignore white space? JEFF??
- return(octect == PAD || base64Alphabet[octect] != -1 );
+ return ( isWS(octect) || isPad(octect) || isData(octect));
}
- public static boolean isArrayByteBase64( byte[] arrayOctect ) {
- int length = arrayOctect.length;
- if ( length == 0 )
- return false;
- for ( int i=0; i < length; i++ ) {
- if ( Base64.isBase64( arrayOctect[i] ) == false)
- return false;
+ protected static int removeWS (byte[] arrayOctect) {
+ if (arrayOctect == null || arrayOctect.length == 0)
+ return 0;
+
+ int len = arrayOctect.length, i=0, j=0;
+ for (i=0,j=0; i<len; i++) {
+ if (!isWS(arrayOctect[i])) {
+ if (i>j)
+ arrayOctect[j] = arrayOctect[i];
+ j++;
+ }
}
- return true;
+ return j;
+ }
+
+ public static boolean isArrayByteBase64( byte[] arrayOctect ) {
+ return (getDataLength(arrayOctect) > 0);
}
/**
* Encodes hex octects into Base64
- *
+ *
* @param binaryData Array containing binaryData
* @return Encoded Base64 array
*/
public static byte[] encode( byte[] binaryData ) {
int lengthDataBits = binaryData.length*EIGHTBIT;
- int fewerThan24bits = lengthDataBits%TWENTYFOURBITGROUP;
+ int fewerThan24bits = lengthDataBits%TWENTYFOURBITGROUP;
int numberTriplets = lengthDataBits/TWENTYFOURBITGROUP;
byte encodedData[] = null;
@@ -170,7 +190,7 @@
for ( i = 0; i<numberTriplets; i++ ) {
dataIndex = i*3;
- b1 = binaryData[dataIndex];
+ b1 = binaryData[dataIndex];
b2 = binaryData[dataIndex + 1];
b3 = binaryData[dataIndex + 2];
@@ -184,10 +204,10 @@
encodedIndex = i*4;
byte val1 = ((b1 & SIGN)==0)?(byte)(b1>>2):(byte)((b1)>>2^0xc0);
- byte val2 = ((b2 & SIGN)==0)?(byte)(b2>>4):(byte)((b2)>>4^0xf0);
+ byte val2 = ((b2 & SIGN)==0)?(byte)(b2>>4):(byte)((b2)>>4^0xf0);
byte val3 = ((b3 & SIGN)==0)?(byte)(b3>>6):(byte)((b3)>>6^0xfc);
- encodedData[encodedIndex] = lookUpBase64Alphabet[ val1 ];
+ encodedData[encodedIndex] = lookUpBase64Alphabet[ val1 ];
if (fDebug) {
System.out.println( "val2 = " + val2 );
System.out.println( "k4 = " + (k<<4));
@@ -195,7 +215,7 @@
}
encodedData[encodedIndex+1] = lookUpBase64Alphabet[ val2 | ( k<<4 )];
- encodedData[encodedIndex+2] = lookUpBase64Alphabet[ (l <<2 ) | val3 ];
+ encodedData[encodedIndex+2] = lookUpBase64Alphabet[ (l <<2 ) | val3 ];
encodedData[encodedIndex+3] = lookUpBase64Alphabet[ b3 & 0x3f ];
}
@@ -211,7 +231,7 @@
}
byte val1 = ((b1 & SIGN)==0)?(byte)(b1>>2):(byte)((b1)>>2^0xc0);
encodedData[encodedIndex] = lookUpBase64Alphabet[ val1 ];
- encodedData[encodedIndex + 1] = lookUpBase64Alphabet[ k<<4 ];
+ encodedData[encodedIndex + 1] = lookUpBase64Alphabet[ k<<4 ];
encodedData[encodedIndex + 2] = PAD;
encodedData[encodedIndex + 3] = PAD;
} else if ( fewerThan24bits == SIXTEENBIT ) {
@@ -222,11 +242,11 @@
k = ( byte ) ( b1 &0x03 );
byte val1 = ((b1 & SIGN)==0)?(byte)(b1>>2):(byte)((b1)>>2^0xc0);
- byte val2 = ((b2 & SIGN)==0)?(byte)(b2>>4):(byte)((b2)>>4^0xf0);
+ byte val2 = ((b2 & SIGN)==0)?(byte)(b2>>4):(byte)((b2)>>4^0xf0);
encodedData[encodedIndex] = lookUpBase64Alphabet[ val1 ];
encodedData[encodedIndex + 1] = lookUpBase64Alphabet[ val2 | ( k<<4 )];
- encodedData[encodedIndex + 2] = lookUpBase64Alphabet[ l<<2 ];
+ encodedData[encodedIndex + 2] = lookUpBase64Alphabet[ l<<2 ];
encodedData[encodedIndex + 3] = PAD;
}
return encodedData;
@@ -235,51 +255,150 @@
/**
* Decodes Base64 data into octects
- *
+ *
* @param binaryData Byte array containing Base64 data
* @return Array containind decoded data.
*/
public static byte[] decode( byte[] base64Data ) {
- int numberQuadruple = base64Data.length/FOURBYTE;
- byte decodedData[] = null;
- byte b1=0,b2=0,b3=0, b4=0, marker0=0, marker1=0;
+ if (base64Data == null)
+ return null;
- // Throw away anything not in base64Data
- // Adjust size
+ int length = base64Data.length;
+ if ( length == 0 )
+ return null;
- int encodedIndex = 0;
- int dataIndex = 0;
- decodedData = new byte[ numberQuadruple*3 + 1 ];
+ // remove all whitespaces
+ byte arrayOctect[] = new byte[length];
+ System.arraycopy(base64Data,0,arrayOctect,0,length);
+ length = removeWS(arrayOctect);
+ if (length == 0 || length % FOURBYTE != 0)
+ return null;
+
+ // calculate the decoded value length
+ int numberQuadruple = length/FOURBYTE*3;
+ if (isPad(arrayOctect[length-2]))
+ numberQuadruple -= 2;
+ else if (isPad(arrayOctect[length-1]))
+ numberQuadruple--;
+
+ byte decodedData[] = new byte[ numberQuadruple];
+ byte b1=0,b2=0,b3=0,b4=0, d1=0,d2=0,d3=0,d4=0;
+
+ int decodedIndex = 0;
+
+ boolean b3isp = false, b4isp = false;
+ int i = 0;
+
+ while (i < length) {
+ // the first byte must be real data
+ d1 = arrayOctect[i];
+ if (!isData(d1))
+ return null;
+ i++;
+ // the second byte must be real data
+ d2 = arrayOctect[i];
+ if (!isData(d2))
+ return null;
+ i++;
+ // the third byte must be real data or pad
+ d3 = arrayOctect[i];
+ b3isp = isPad(d3);
+ if (!isData(d3) && !b3isp)
+ return null;
+ i++;
+ if (b3isp && (d2&0xf) != 0)
+ return null;
+ // the forth byte must be real data or pad
+ // and if the third is pad, the forth must be pad
+ d4 = arrayOctect[i];
+ b4isp = isPad(d4);
+ if (!isData(d4) && !b4isp || b3isp && !b4isp)
+ return null;
+ i++;
+ if (!b3isp && b4isp && (d3&0x3) != 0)
+ return null;
+
+ // if the forth byte is pad, it should be the end of the input
+ if (b4isp && i < length)
+ return null;
+
+ b1 = base64Alphabet[d1];
+ b2 = base64Alphabet[d2];
+
+ if ( !b4isp ) { //No PAD e.g 3cQl
+ b3 = base64Alphabet[d3];
+ b4 = base64Alphabet[d4];
+
+ decodedData[decodedIndex++] = (byte)( b1 <<2 | b2>>4 ) ;
+ decodedData[decodedIndex++] = (byte)(((b2 & 0xf)<<4 ) |( (b3>>2) & 0xf) );
+ decodedData[decodedIndex++] = (byte)( b3<<6 | b4 );
+ } else if ( b3isp ) { //Two PAD e.g. 3c[Pad][Pad]
+ decodedData[decodedIndex++] = (byte)( b1 <<2 | b2>>4 ) ;
+ } else if ( b4isp ) { //One PAD e.g. 3cQ[Pad]
+ b3 = base64Alphabet[d3];
- for (int i = 0; i<numberQuadruple; i++ ) {
- dataIndex = i*4;
- marker0 = base64Data[dataIndex +2];
- marker1 = base64Data[dataIndex +3];
-
- b1 = base64Alphabet[base64Data[dataIndex]];
- b2 = base64Alphabet[base64Data[dataIndex +1]];
-
- if ( marker0 != PAD && marker1 != PAD ) { //No PAD e.g 3cQl
- b3 = base64Alphabet[ marker0 ];
- b4 = base64Alphabet[ marker1 ];
-
- decodedData[encodedIndex] = (byte)( b1 <<2 | b2>>4 ) ;
- decodedData[encodedIndex+1] = (byte)(((b2 & 0xf)<<4 ) |( (b3>>2) & 0xf) );
- decodedData[encodedIndex+2] = (byte)( b3<<6 | b4 );
- } else if ( marker0 == PAD ) { //Two PAD e.g. 3c[Pad][Pad]
- decodedData[encodedIndex] = (byte)( b1 <<2 | b2>>4 ) ;
- decodedData[encodedIndex+1] = (byte)((b2 & 0xf)<<4 );
- decodedData[encodedIndex+2] = (byte) 0;
- } else if ( marker1 == PAD ) { //One PAD e.g. 3cQ[Pad]
- b3 = base64Alphabet[ marker0 ];
-
- decodedData[encodedIndex] = (byte)( b1 <<2 | b2>>4 );
- decodedData[encodedIndex+1] = (byte)(((b2 & 0xf)<<4 ) |( (b3>>2) & 0xf) );
- decodedData[encodedIndex+2] = (byte)( b3<<6);
+ decodedData[decodedIndex++] = (byte)( b1 <<2 | b2>>4 );
+ decodedData[decodedIndex++] = (byte)(((b2 & 0xf)<<4 ) |( (b3>>2) & 0xf) );
}
- encodedIndex += 3;
}
+
return decodedData;
+ }
+
+ static public int getDataLength (byte[] base64Data) {
+ if (base64Data == null)
+ return 0;
+
+ int length = base64Data.length;
+ if ( length == 0 )
+ return 0;
+
+ // remove all whitespaces
+ byte arrayOctect[] = new byte[length];
+ System.arraycopy(base64Data,0,arrayOctect,0,length);
+ length = removeWS(arrayOctect);
+ if ( length == 0 )
+ return 0;
+ if (length % FOURBYTE != 0)
+ return -1;
+
+ byte d2=0, d3=0;
+ boolean b3isp = false, b4isp = false;
+ int i = 0;
+
+ while (i < length) {
+ // the first byte must be real data
+ if (!isData(arrayOctect[i]))
+ return -1;
+ i++;
+ // the second byte must be real data
+ d2 = arrayOctect[i];
+ if (!isData(d2))
+ return -1;
+ i++;
+ // the third byte must be real data or pad
+ d3 = arrayOctect[i];
+ b3isp = isPad(d3);
+ if (!isData(d3) && !b3isp)
+ return -1;
+ if (b3isp && (d2&0xf) != 0)
+ return -1;
+ i++;
+ // the forth byte must be real data or pad
+ // and if the third is pad, the forth must be pad
+ b4isp = isPad(arrayOctect[i]);
+ if (!isData(arrayOctect[i]) && !b4isp || b3isp && !b4isp)
+ return -1;
+ i++;
+ if (!b3isp && b4isp && (d3&0x3) != 0)
+ return -1;
+
+ // if the forth byte is pad, it should be the end of the input
+ if (b4isp && i < length)
+ return -1;
+ }
+ // adjust number according to pad
+ return length/FOURBYTE*3 - (b3isp?2:(b4isp?1:0));
}
-}
+}
1.5 +45 -37 xml-xerces/java/src/org/apache/xerces/utils/HexBin.java
Index: HexBin.java
===================================================================
RCS file: /home/cvs/xml-xerces/java/src/org/apache/xerces/utils/HexBin.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- HexBin.java 2000/11/29 00:22:23 1.4
+++ HexBin.java 2001/04/09 20:59:01 1.5
@@ -65,19 +65,19 @@
*
* This class encodes/decodes hexadecimal data
* @author Jeffrey Rodriguez
- * @version $Id: HexBin.java,v 1.4 2000/11/29 00:22:23 jeffreyr Exp $
+ * @version $Id: HexBin.java,v 1.5 2001/04/09 20:59:01 elena Exp $
*/
public final class HexBin {
- static private final int BASELENGTH = 255;
+ static private final int BASELENGTH = 255;
static private final int LOOKUPLENGTH = 16;
- static private byte [] hexNumberTable = new byte[BASELENGTH];
+ static private byte [] hexNumberTable = new byte[BASELENGTH];
static private byte [] lookUpHexAlphabet = new byte[LOOKUPLENGTH];
static {
- for (int i = 0; i<BASELENGTH; i++ ) {
- hexNumberTable[i] = -1;
+ for (int i = 0; i<BASELENGTH; i++ ) {
+ hexNumberTable[i] = -1;
}
for ( int i = '9'; i >= '0'; i--) {
hexNumberTable[i] = (byte) (i-'0');
@@ -97,31 +97,31 @@
/**
* byte to be tested if it is Base64 alphabet
- *
+ *
* @param octect
- * @return
+ * @return
*/
static boolean isHex( byte octect ) {
//shall we ignore white space? JEFF??
- return(hexNumberTable[octect] != -1 );
+ return( hexNumberTable[octect] != -1 );
}
/**
- * Array of bytes to check against Hex Table
- *
+ * Array of bytes to check against Hex Table
+ *
* @param arrayOctect
- * @return
+ * @return
*/
static boolean isArrayByteHex( byte[] arrayOctect ) {
- int length = arrayOctect.length;
- if( length == 0 )
- return false;
- for( int i=0; i < length; i++ ){
-
- if( HexBin.isHex( arrayOctect[i] ) == false)
- return false;
-
+ if (arrayOctect == null)
+ return false;
+ int length = arrayOctect.length;
+ if( length == 0 || length % 2 != 0)
+ return false;
+ for( int i=0; i < length; i++ ){
+ if( HexBin.isHex( arrayOctect[i] ) == false)
+ return false;
}
return true;
}
@@ -130,32 +130,40 @@
return( isArrayByteHex( isValidString.getBytes()));
}
-
-
/**
* array of byte to encode
- *
+ *
* @param binaryData
* @return return encode binary array
*/
static public byte[] encode( byte[] binaryData ) {
- int lengthData = binaryData.length;
- int lengthEncode = lengthData;
- byte[] encodedData = new byte[lengthData];
- for( int i = 0; i<lengthData; i++ ){
- encodedData[i] = lookUpHexAlphabet[ binaryData[i] ];
- }
- return encodedData;
+ int lengthData = binaryData.length;
+ int lengthEncode = lengthData * 2;
+ byte[] encodedData = new byte[lengthEncode];
+ for( int i = 0; i<lengthData; i++ ){
+ encodedData[i*2] = lookUpHexAlphabet[ binaryData[i] >> 4];
+ encodedData[i*2+1] = lookUpHexAlphabet[ binaryData[i] & 0xf];
+ }
+ return encodedData;
}
-
static public byte[] decode ( byte[] binaryData ) {
- int lengthData = binaryData.length;
- int lengthEncode = lengthData;
- byte[] decodedData = new byte[lengthData];
- for( int i = 0; i<lengthData; i++ ){
- decodedData[i] = hexNumberTable[binaryData[i]];
- }
- return decodedData;
+ int lengthData = binaryData.length;
+ if( lengthData == 0 || lengthData % 2 != 0)
+ return null;
+
+ int lengthDecode = lengthData / 2;
+ byte[] decodedData = new byte[lengthDecode];
+ for( int i = 0; i<lengthDecode; i++ ){
+ decodedData[i] = (byte)((hexNumberTable[binaryData[i*2]] << 4) | hexNumberTable[binaryData[i*2+1]]);
+ }
+ return decodedData;
+ }
+
+ static public int getDataLength (byte[] hexData) {
+ if (!isArrayByteHex(hexData))
+ return -1;
+
+ return hexData.length / 2;
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-cvs-help@xml.apache.org