diff options
author | Ashod Nakashian <ashod.nakashian@collabora.co.uk> | 2017-07-13 21:34:41 -0400 |
---|---|---|
committer | Ashod Nakashian <ashnakash@gmail.com> | 2017-07-16 18:25:25 +0200 |
commit | 2c4c08320532d2edfbd53d1bb0c45e7448caf004 (patch) | |
tree | 3b6d4be4f7842504067c71280d349bdc4620ba38 /sax | |
parent | 392c3b9093fdc8d64a2db2c98d070c2b0fd833aa (diff) |
comphelper: move base64 encode/decode helpers from sax
Change-Id: I02e33793736003f36976bc8b518c389a31082dbe
Reviewed-on: https://gerrit.libreoffice.org/39991
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: Ashod Nakashian <ashnakash@gmail.com>
Diffstat (limited to 'sax')
-rw-r--r-- | sax/source/tools/converter.cxx | 160 |
1 files changed, 4 insertions, 156 deletions
diff --git a/sax/source/tools/converter.cxx b/sax/source/tools/converter.cxx index b1e7344ebf74..0c560865a7b6 100644 --- a/sax/source/tools/converter.cxx +++ b/sax/source/tools/converter.cxx @@ -33,6 +33,7 @@ #include <sal/log.hxx> #include <osl/time.h> #include <osl/diagnose.h> +#include <comphelper/base64.hxx> #include <algorithm> @@ -1915,174 +1916,21 @@ sal_Int32 Converter::indexOfComma( const OUString& rStr, return -1; } -const - sal_Char aBase64EncodeTable[] = - { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', - 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', - 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', - 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', - '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' }; - -const - sal_uInt8 aBase64DecodeTable[] = - { 62,255,255,255, 63, // 43-47 -// + / - - 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,255,255,255, 0,255,255, // 48-63 -// 0 1 2 3 4 5 6 7 8 9 = - - 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, // 64-79 -// A B C D E F G H I J K L M N O - - 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,255,255,255,255,255, // 80-95 -// P Q R S T U V W X Y Z - - 0, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, // 96-111 -// a b c d e f g h i j k l m n o - - 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51 }; // 112-123 -// p q r s t u v w x y z - - -void ThreeByteToFourByte (const sal_Int8* pBuffer, const sal_Int32 nStart, const sal_Int32 nFullLen, OUStringBuffer& sBuffer) -{ - sal_Int32 nLen(nFullLen - nStart); - if (nLen > 3) - nLen = 3; - if (nLen == 0) - { - return; - } - - sal_Int32 nBinaer; - switch (nLen) - { - case 1: - { - nBinaer = ((sal_uInt8)pBuffer[nStart + 0]) << 16; - } - break; - case 2: - { - nBinaer = (((sal_uInt8)pBuffer[nStart + 0]) << 16) + - (((sal_uInt8)pBuffer[nStart + 1]) << 8); - } - break; - default: - { - nBinaer = (((sal_uInt8)pBuffer[nStart + 0]) << 16) + - (((sal_uInt8)pBuffer[nStart + 1]) << 8) + - ((sal_uInt8)pBuffer[nStart + 2]); - } - break; - } - - sal_Unicode buf[] = { '=', '=', '=', '=' }; - - sal_uInt8 nIndex (static_cast<sal_uInt8>((nBinaer & 0xFC0000) >> 18)); - buf[0] = aBase64EncodeTable [nIndex]; - - nIndex = static_cast<sal_uInt8>((nBinaer & 0x3F000) >> 12); - buf[1] = aBase64EncodeTable [nIndex]; - if (nLen > 1) - { - nIndex = static_cast<sal_uInt8>((nBinaer & 0xFC0) >> 6); - buf[2] = aBase64EncodeTable [nIndex]; - if (nLen > 2) - { - nIndex = static_cast<sal_uInt8>((nBinaer & 0x3F)); - buf[3] = aBase64EncodeTable [nIndex]; - } - } - sBuffer.append(buf, SAL_N_ELEMENTS(buf)); -} - void Converter::encodeBase64(OUStringBuffer& aStrBuffer, const uno::Sequence<sal_Int8>& aPass) { - sal_Int32 i(0); - sal_Int32 nBufferLength(aPass.getLength()); - const sal_Int8* pBuffer = aPass.getConstArray(); - while (i < nBufferLength) - { - ThreeByteToFourByte (pBuffer, i, nBufferLength, aStrBuffer); - i += 3; - } + comphelper::Base64::encode(aStrBuffer, aPass); } void Converter::decodeBase64(uno::Sequence<sal_Int8>& aBuffer, const OUString& sBuffer) { - sal_Int32 nCharsDecoded = decodeBase64SomeChars( aBuffer, sBuffer ); - OSL_ENSURE( nCharsDecoded == sBuffer.getLength(), "some bytes left in base64 decoding!" ); + comphelper::Base64::decode(aBuffer, sBuffer); } sal_Int32 Converter::decodeBase64SomeChars( uno::Sequence<sal_Int8>& rOutBuffer, const OUString& rInBuffer) { - sal_Int32 nInBufferLen = rInBuffer.getLength(); - sal_Int32 nMinOutBufferLen = (nInBufferLen / 4) * 3; - if( rOutBuffer.getLength() < nMinOutBufferLen ) - rOutBuffer.realloc( nMinOutBufferLen ); - - const sal_Unicode *pInBuffer = rInBuffer.getStr(); - sal_Int8 *pOutBuffer = rOutBuffer.getArray(); - sal_Int8 *pOutBufferStart = pOutBuffer; - sal_Int32 nCharsDecoded = 0; - - sal_uInt8 aDecodeBuffer[4]; - sal_Int32 nBytesToDecode = 0; - sal_Int32 nBytesGotFromDecoding = 3; - sal_Int32 nInBufferPos= 0; - while( nInBufferPos < nInBufferLen ) - { - sal_Unicode cChar = *pInBuffer; - if( cChar >= '+' && cChar <= 'z' ) - { - sal_uInt8 nByte = aBase64DecodeTable[cChar-'+']; - if( nByte != 255 ) - { - // We have found a valid character! - aDecodeBuffer[nBytesToDecode++] = nByte; - - // One '=' character at the end means 2 out bytes - // Two '=' characters at the end mean 1 out bytes - if( '=' == cChar && nBytesToDecode > 2 ) - nBytesGotFromDecoding--; - if( 4 == nBytesToDecode ) - { - // Four characters found, so we may convert now! - sal_uInt32 nOut = (aDecodeBuffer[0] << 18) + - (aDecodeBuffer[1] << 12) + - (aDecodeBuffer[2] << 6) + - aDecodeBuffer[3]; - - *pOutBuffer++ = (sal_Int8)((nOut & 0xff0000) >> 16); - if( nBytesGotFromDecoding > 1 ) - *pOutBuffer++ = (sal_Int8)((nOut & 0xff00) >> 8); - if( nBytesGotFromDecoding > 2 ) - *pOutBuffer++ = (sal_Int8)(nOut & 0xff); - nCharsDecoded = nInBufferPos + 1; - nBytesToDecode = 0; - nBytesGotFromDecoding = 3; - } - } - else - { - nCharsDecoded++; - } - } - else - { - nCharsDecoded++; - } - - nInBufferPos++; - pInBuffer++; - } - if( (pOutBuffer - pOutBufferStart) != rOutBuffer.getLength() ) - rOutBuffer.realloc( pOutBuffer - pOutBufferStart ); - - return nCharsDecoded; + return comphelper::Base64::decodeSomeChars(rOutBuffer, rInBuffer); } double Converter::GetConversionFactor(OUStringBuffer& rUnit, sal_Int16 nSourceUnit, sal_Int16 nTargetUnit) |