You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xerces.apache.org by pe...@apache.org on 2002/02/15 22:36:56 UTC
cvs commit: xml-xerces/c/src/xercesc/util Base64.hpp Base64.cpp
peiyongz 02/02/15 13:36:56
Modified: c/src/xercesc/util Base64.hpp Base64.cpp
Log:
Interface redefined for conversion in XMLByte
Revision Changes Path
1.3 +58 -51 xml-xerces/c/src/xercesc/util/Base64.hpp
Index: Base64.hpp
===================================================================
RCS file: /home/cvs/xml-xerces/c/src/xercesc/util/Base64.hpp,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- Base64.hpp 11 Feb 2002 21:28:24 -0000 1.2
+++ Base64.hpp 15 Feb 2002 21:36:56 -0000 1.3
@@ -55,7 +55,7 @@
*/
/*
- * $Id: Base64.hpp,v 1.2 2002/02/11 21:28:24 peiyongz Exp $
+ * $Id: Base64.hpp,v 1.3 2002/02/15 21:36:56 peiyongz Exp $
*/
#ifndef BASE64_HPP
@@ -82,42 +82,49 @@
/**
* Encodes octets into Base64 data
*
- * @param inputData Byte array containing binary data.
- * @param inputLength Length of the input array.
- * @param outputLength Pointer to variable, where will be
- * stored length of returned data.
- * @return Byte array containing encoded Base64 data,
- * or NULL if input data can not be encoded.
+ * @param inputData Binary data in XMLByte stream.
+ * @param inputLength Length of the XMLByte stream.
+ * @param outputLength Length of the encoded Base64 byte stream.
+ * @return Encoded Base64 data in XMLByte stream,
+ * or NULL if input data can not be encoded.
*/
- static XMLCh* encode (
- const XMLCh* const inputData,
- const int inputLength,
- int *outputLength = 0 );
+ static XMLByte* encode(const XMLByte* const inputData,
+ const unsigned int inputLength,
+ unsigned int* outputLength);
/**
- * Get data length
- * returns length of decoded data given an array
- * containing encoded data.
- *
- * @param inputData Byte array containing Base64 data.
- * @return Length of decoded data, or -1 if input data
- * can not be decoded.
+ * Decodes Base64 data into octets
+ *
+ * @param inputData Base64 data in XMLByte stream.
+ * @param outputLength Length of decoded XMLByte stream.
+ * @return Decoded binary data in XMLByte stream,
+ * or NULL if input data can not be decoded.
*/
- static int getDataLength(
- const XMLCh* const inputData );
+ static XMLByte* decode(const XMLByte* const inputData,
+ unsigned int* outputLength);
/**
- * Decodes Base64 data into octets
+ * Decodes Base64 data into XMLCh
*
- * @param inputData Byte array containing Base64 data.
- * @param outputLength Reference to variable, where will be
- * stored length of returned data.
- * @return Byte array containing decoded binary data, or
- * NULL if input data can not be decoded.
+ * @param inputData Base64 data in XMLCh stream.
+ * @param outputLength Length of decoded XMLCh stream
+ * @return Decoded binary data in XMLCh stream,
+ * or NULL if input data can not be decoded.
+ */
+ static XMLCh* decode(const XMLCh* const inputData,
+ unsigned int* outputLength);
+
+ /**
+ * Get data length
+ *
+ * Returns length of decoded data given an array
+ * containing encoded data.
+ *
+ * @param inputData Base64 data in XMLCh stream.
+ * @return Length of decoded data,
+ * or -1 if input data can not be decoded.
*/
- static XMLCh* decode(
- const XMLCh* const inputData,
- int& outputLength );
+ static int getDataLength(const XMLCh* const inputData );
//@}
@@ -129,16 +136,16 @@
static void init();
- static bool isData(const XMLCh& octet);
- static bool isPad(const XMLCh& octet);
+ static bool isData(const XMLByte& octet);
+ static bool isPad(const XMLByte& octet);
- static XMLCh set1stOctet(const XMLCh&, const XMLCh&);
- static XMLCh set2ndOctet(const XMLCh&, const XMLCh&);
- static XMLCh set3rdOctet(const XMLCh&, const XMLCh&);
-
- static void split1stOctet(const XMLCh&, XMLCh&, XMLCh&);
- static void split2ndOctet(const XMLCh&, XMLCh&, XMLCh&);
- static void split3rdOctet(const XMLCh&, XMLCh&, XMLCh&);
+ static XMLByte set1stOctet(const XMLByte&, const XMLByte&);
+ static XMLByte set2ndOctet(const XMLByte&, const XMLByte&);
+ static XMLByte set3rdOctet(const XMLByte&, const XMLByte&);
+
+ static void split1stOctet(const XMLByte&, XMLByte&, XMLByte&);
+ static void split2ndOctet(const XMLByte&, XMLByte&, XMLByte&);
+ static void split3rdOctet(const XMLByte&, XMLByte&, XMLByte&);
// -----------------------------------------------------------------------
// Unimplemented constructors and operators
@@ -168,10 +175,10 @@
//
// -----------------------------------------------------------------------
- static const XMLCh base64Alphabet[];
- static const XMLCh base64Padding;
+ static const XMLByte base64Alphabet[];
+ static const XMLByte base64Padding;
- static XMLCh base64Inverse[];
+ static XMLByte base64Inverse[];
static bool isInitialized;
static const unsigned int quadsPerLine;
@@ -180,38 +187,38 @@
// -----------------------------------------------------------------------
// Helper methods
// -----------------------------------------------------------------------
-inline bool Base64::isPad(const XMLCh& octet)
+inline bool Base64::isPad(const XMLByte& octet)
{
return ( octet == base64Padding );
}
-inline XMLCh Base64::set1stOctet(const XMLCh& b1, const XMLCh& b2)
+inline XMLByte Base64::set1stOctet(const XMLByte& b1, const XMLByte& b2)
{
return (( b1 << 2 ) | ( b2 >> 4 ));
}
-inline XMLCh Base64::set2ndOctet(const XMLCh& b2, const XMLCh& b3)
+inline XMLByte Base64::set2ndOctet(const XMLByte& b2, const XMLByte& b3)
{
return (( b2 << 4 ) | ( b3 >> 2 ));
}
-inline XMLCh Base64::set3rdOctet(const XMLCh& b3, const XMLCh& b4)
+inline XMLByte Base64::set3rdOctet(const XMLByte& b3, const XMLByte& b4)
{
return (( b3 << 6 ) | b4 );
}
-inline void Base64::split1stOctet(const XMLCh& ch, XMLCh& b1, XMLCh& b2) {
- b1 = ( ch & 0xff ) >> 2;
+inline void Base64::split1stOctet(const XMLByte& ch, XMLByte& b1, XMLByte& b2) {
+ b1 = ch >> 2;
b2 = ( ch & 0x3 ) << 4;
}
-inline void Base64::split2ndOctet(const XMLCh& ch, XMLCh& b2, XMLCh& b3) {
- b2 |= ( ch & 0xff ) >> 4; // combine with previous value
+inline void Base64::split2ndOctet(const XMLByte& ch, XMLByte& b2, XMLByte& b3) {
+ b2 |= ch >> 4; // combine with previous value
b3 = ( ch & 0xf ) << 2;
}
-inline void Base64::split3rdOctet(const XMLCh& ch, XMLCh& b3, XMLCh& b4) {
- b3 |= ( ch & 0xff) >> 6; // combine with previous value
+inline void Base64::split3rdOctet(const XMLByte& ch, XMLByte& b3, XMLByte& b4) {
+ b3 |= ch >> 6; // combine with previous value
b4 = ( ch & 0x3f );
}
1.2 +77 -41 xml-xerces/c/src/xercesc/util/Base64.cpp
Index: Base64.cpp
===================================================================
RCS file: /home/cvs/xml-xerces/c/src/xercesc/util/Base64.cpp,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- Base64.cpp 1 Feb 2002 22:22:09 -0000 1.1
+++ Base64.cpp 15 Feb 2002 21:36:56 -0000 1.2
@@ -56,8 +56,11 @@
/*
* $Log: Base64.cpp,v $
- * Revision 1.1 2002/02/01 22:22:09 peiyongz
- * Initial revision
+ * Revision 1.2 2002/02/15 21:36:56 peiyongz
+ * Interface redefined for conversion in XMLByte
+ *
+ * Revision 1.1.1.1 2002/02/01 22:22:09 peiyongz
+ * sane_include
*
* Revision 1.6 2001/10/15 19:42:16 knoaman
* Null-terminate base64Alphabet.
@@ -99,38 +102,39 @@
// ---------------------------------------------------------------------------
// the base64 alphabet according to definition in RFC 2045
-const XMLCh Base64::base64Alphabet[] = {
+const XMLByte Base64::base64Alphabet[] = {
chLatin_A, chLatin_B, chLatin_C, chLatin_D, chLatin_E,
chLatin_F, chLatin_G, chLatin_H, chLatin_I, chLatin_J,
chLatin_K, chLatin_L, chLatin_M, chLatin_N, chLatin_O,
chLatin_P, chLatin_Q, chLatin_R, chLatin_S, chLatin_T,
- chLatin_U, chLatin_V, chLatin_W, chLatin_X, chLatin_Y,
- chLatin_Z, chLatin_a, chLatin_b, chLatin_c, chLatin_d,
- chLatin_e, chLatin_f, chLatin_g, chLatin_h, chLatin_i,
- chLatin_j, chLatin_k, chLatin_l, chLatin_m, chLatin_n,
- chLatin_o, chLatin_p, chLatin_q, chLatin_r, chLatin_s,
- chLatin_t, chLatin_u, chLatin_v, chLatin_w, chLatin_x,
- chLatin_y, chLatin_z, chDigit_0, chDigit_1, chDigit_2,
- chDigit_3, chDigit_4, chDigit_5, chDigit_6, chDigit_7,
- chDigit_8, chDigit_9, chPlus, chForwardSlash, chNull
+ chLatin_U, chLatin_V, chLatin_W, chLatin_X, chLatin_Y, chLatin_Z,
+ chLatin_a, chLatin_b, chLatin_c, chLatin_d, chLatin_e,
+ chLatin_f, chLatin_g, chLatin_h, chLatin_i, chLatin_j,
+ chLatin_k, chLatin_l, chLatin_m, chLatin_n, chLatin_o,
+ chLatin_p, chLatin_q, chLatin_r, chLatin_s, chLatin_t,
+ chLatin_u, chLatin_v, chLatin_w, chLatin_x, chLatin_y, chLatin_z,
+ chDigit_0, chDigit_1, chDigit_2, chDigit_3, chDigit_4,
+ chDigit_5, chDigit_6, chDigit_7, chDigit_8, chDigit_9,
+ chPlus, chForwardSlash, chNull
};
-const XMLCh Base64::base64Padding = chEqual;
-XMLCh Base64::base64Inverse[ BASELENGTH ];
+XMLByte Base64::base64Inverse[BASELENGTH];
+
+const XMLByte Base64::base64Padding = chEqual;
+
bool Base64::isInitialized = false;
// number of quadruplets per one line ( must be >1 and <19 )
const unsigned int Base64::quadsPerLine = 15;
-XMLCh* Base64::encode(
- const XMLCh* const inputData,
- const int inputLength,
- int *outputLength )
+XMLByte* Base64::encode(const XMLByte* const inputData,
+ const unsigned int inputLength,
+ unsigned int* outputLength)
{
if (!isInitialized)
init();
- if ( inputData == 0 )
+ if (!inputData)
return 0;
int quadrupletCount = ( inputLength + 2 ) / 3;
@@ -143,12 +147,11 @@
//
// convert the triplet(s) to quadruplet(s)
//
- XMLCh b1, b2, b3, b4; // base64 binary codes ( 0..63 )
+ XMLByte b1, b2, b3, b4; // base64 binary codes ( 0..63 )
- int inputIndex = 0;
- int outputIndex = 0;
- XMLCh *encodedData =
- new XMLCh[ quadrupletCount*FOURBYTE + lineCount + 1 ];
+ unsigned int inputIndex = 0;
+ unsigned int outputIndex = 0;
+ XMLByte *encodedData = new XMLByte[ quadrupletCount*FOURBYTE + lineCount + 1 ];
//
// Process all quadruplet(s) except the last
@@ -234,8 +237,8 @@
//
int Base64::getDataLength( const XMLCh* const inputData )
{
- int retLen = 0;
- XMLCh* decodedData = decode( inputData, retLen );
+ unsigned int retLen = 0;
+ XMLCh* decodedData = decode(inputData, &retLen);
if ( !decodedData )
return -1;
@@ -254,20 +257,22 @@
//
// temporary data, rawInputData, is ALWAYS released by this function.
//
-XMLCh* Base64::decode( const XMLCh* const inputData, int& outputLength )
+XMLByte* Base64::decode(const XMLByte* const inputData,
+ unsigned int* outputLength)
{
if (!isInitialized)
init();
- if (( inputData == 0 ) || ( *inputData == 0 ))
+ if ((!inputData) || (!*inputData))
return 0;
//
// remove all whitespaces from the base64Data
//
- int inputLength = XMLString::stringLen( inputData );
- XMLCh* rawInputData = new XMLCh[ inputLength + 1 ];
- ArrayJanitor<XMLCh> jan(rawInputData);
+
+ int inputLength = XMLString::stringLen( (const char* const)inputData );
+ XMLByte* rawInputData = new XMLByte[ inputLength + 1 ];
+ ArrayJanitor<XMLByte> jan(rawInputData);
int inputIndex = 0;
int rawInputLength = 0;
@@ -292,12 +297,12 @@
//
// convert the quadruplet(s) to triplet(s)
//
- XMLCh d1, d2, d3, d4; // base64 characters
- XMLCh b1, b2, b3, b4; // base64 binary codes ( 0..64 )
+ XMLByte d1, d2, d3, d4; // base64 characters
+ XMLByte b1, b2, b3, b4; // base64 binary codes ( 0..64 )
int rawInputIndex = 0;
int outputIndex = 0;
- XMLCh *decodedData = new XMLCh[ quadrupletCount*3 + 1 ];
+ XMLByte *decodedData = new XMLByte[ quadrupletCount*3 + 1 ];
//
// Process all quadruplet(s) except the last
@@ -392,11 +397,46 @@
// write out the end of string
decodedData[ outputIndex ] = 0;
- outputLength = outputIndex;
+ *outputLength = outputIndex;
return decodedData;
}
+XMLCh* Base64::decode(const XMLCh* const inputData,
+ unsigned int* outputLength)
+{
+ if (!inputData)
+ return 0;
+
+ unsigned int srcLen = XMLString::stringLen(inputData);
+ XMLByte *toFill = new XMLByte[srcLen+1];
+ ArrayJanitor<XMLByte> janFill(toFill);
+
+ for (unsigned int i = 0; i < srcLen; i++)
+ toFill[i] = (XMLByte)inputData[i];
+
+ toFill[srcLen] = 0;
+
+ unsigned int decodedLen = 0;
+ XMLByte *DecodedBuf = decode(toFill, &decodedLen);
+
+ if (!DecodedBuf)
+ return 0;
+
+ XMLCh *toRet = new XMLCh[decodedLen+1];
+
+ for (unsigned int i = 0; i < decodedLen; i++)
+ toRet[i] = (XMLCh)DecodedBuf[i];
+
+ toRet[decodedLen] = 0;
+
+ *outputLength = decodedLen;
+ delete[] DecodedBuf;
+
+ return toRet;
+
+}
+
// -----------------------------------------------------------------------
// Helper methods
// -----------------------------------------------------------------------
@@ -414,18 +454,14 @@
int i;
// set all fields to -1
for ( i = 0; i < BASELENGTH; i++ )
- base64Inverse[i] = (XMLCh)-1;
+ base64Inverse[i] = (XMLByte)-1;
// compute inverse table
for ( i = 0; i < 64; i++ )
- base64Inverse[ base64Alphabet[i] ] = (XMLCh)i;
+ base64Inverse[ base64Alphabet[i] ] = (XMLByte)i;
}
-bool Base64::isData(const XMLCh& octet)
+bool Base64::isData(const XMLByte& octet)
{
- // sanity check to avoid out-of-bound index
- if (( octet >= BASELENGTH ) || ( octet < 0 ))
- return false;
-
- return( base64Inverse[octet] != (XMLCh) -1);
+ return (base64Inverse[octet]!=(XMLByte)-1);
}
---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-cvs-help@xml.apache.org