comphelper/Library_comphelper.mk  |    1 
 comphelper/source/misc/base64.cxx |  216 ++++++++++++++++++++++++++++++++++++++
 include/comphelper/base64.hxx     |   52 +++++++++
 sax/source/tools/converter.cxx    |  160 ----------------------------
 4 files changed, 273 insertions(+), 156 deletions(-)

New commits:
commit 2c4c08320532d2edfbd53d1bb0c45e7448caf004
Author: Ashod Nakashian <ashod.nakash...@collabora.co.uk>
Date:   Thu Jul 13 21:34:41 2017 -0400

    comphelper: move base64 encode/decode helpers from sax
    
    Change-Id: I02e33793736003f36976bc8b518c389a31082dbe
    Reviewed-on: https://gerrit.libreoffice.org/39991
    Tested-by: Jenkins <c...@libreoffice.org>
    Reviewed-by: Ashod Nakashian <ashnak...@gmail.com>

diff --git a/comphelper/Library_comphelper.mk b/comphelper/Library_comphelper.mk
index 9d2b6c5961f4..ec5ac9d3240c 100644
--- a/comphelper/Library_comphelper.mk
+++ b/comphelper/Library_comphelper.mk
@@ -91,6 +91,7 @@ $(eval $(call gb_Library_add_exception_objects,comphelper,\
     comphelper/source/misc/anytostring \
     comphelper/source/misc/asyncnotification \
     comphelper/source/misc/backupfilehelper \
+    comphelper/source/misc/base64 \
     comphelper/source/misc/comphelper_module \
     comphelper/source/misc/comphelper_services \
     comphelper/source/misc/componentbase \
diff --git a/comphelper/source/misc/base64.cxx 
b/comphelper/source/misc/base64.cxx
new file mode 100644
index 000000000000..502c8d2d7d2c
--- /dev/null
+++ b/comphelper/source/misc/base64.cxx
@@ -0,0 +1,216 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * This file incorporates work covered by the following license notice:
+ *
+ *   Licensed to the Apache Software Foundation (ASF) under one or more
+ *   contributor license agreements. See the NOTICE file distributed
+ *   with this work for additional information regarding copyright
+ *   ownership. The ASF licenses this file to you under the Apache
+ *   License, Version 2.0 (the "License"); you may not use this file
+ *   except in compliance with the License. You may obtain a copy of
+ *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
+ */
+
+#include <comphelper/base64.hxx>
+
+#include <com/sun/star/i18n/UnicodeType.hpp>
+#include <com/sun/star/util/DateTime.hpp>
+#include <com/sun/star/util/Date.hpp>
+#include <com/sun/star/util/DateTimeWithTimezone.hpp>
+#include <com/sun/star/util/DateWithTimezone.hpp>
+#include <com/sun/star/util/Duration.hpp>
+#include <com/sun/star/util/Time.hpp>
+#include <com/sun/star/uno/Sequence.hxx>
+
+#include <rtl/ustrbuf.hxx>
+#include <rtl/math.hxx>
+#include <sal/log.hxx>
+#include <osl/time.h>
+#include <osl/diagnose.h>
+
+#include <algorithm>
+
+using namespace com::sun::star;
+using namespace com::sun::star::uno;
+using namespace com::sun::star::util;
+using namespace ::com::sun::star::i18n;
+
+namespace comphelper {
+
+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 Base64::encode(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;
+    }
+}
+
+void Base64::decode(uno::Sequence<sal_Int8>& aBuffer, const OUString& sBuffer)
+{
+    sal_Int32 nCharsDecoded = decodeSomeChars( aBuffer, sBuffer );
+    OSL_ENSURE( nCharsDecoded == sBuffer.getLength(), "some bytes left in 
base64 decoding!" );
+}
+
+sal_Int32 Base64::decodeSomeChars(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;
+}
+
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/include/comphelper/base64.hxx b/include/comphelper/base64.hxx
new file mode 100644
index 000000000000..302b0ab51d09
--- /dev/null
+++ b/include/comphelper/base64.hxx
@@ -0,0 +1,52 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * This file incorporates work covered by the following license notice:
+ *
+ *   Licensed to the Apache Software Foundation (ASF) under one or more
+ *   contributor license agreements. See the NOTICE file distributed
+ *   with this work for additional information regarding copyright
+ *   ownership. The ASF licenses this file to you under the Apache
+ *   License, Version 2.0 (the "License"); you may not use this file
+ *   except in compliance with the License. You may obtain a copy of
+ *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
+ */
+
+#ifndef INCLUDED_COMPHELPER_BASE64_HXX
+#define INCLUDED_COMPHELPER_BASE64_HXX
+
+#include <comphelper/comphelperdllapi.h>
+
+#include <sal/types.h>
+#include <rtl/ustring.hxx>
+#include <rtl/ustrbuf.hxx>
+#include <com/sun/star/uno/Sequence.h>
+
+namespace comphelper {
+
+class COMPHELPER_DLLPUBLIC Base64
+{
+public:
+    /** encodes the given byte sequence into Base64 */
+    static void encode(OUStringBuffer& aStrBuffer, const 
css::uno::Sequence<sal_Int8>& aPass);
+
+    // Decode a base 64 encoded string into a sequence of bytes. The first
+    // version can be used for attribute values only, because it does not
+    // return any chars left from conversion.
+    // For text submitted throgh the SAX characters call, the later method
+    // must be used!
+    static void decode(css::uno::Sequence<sal_Int8>& aPass, const OUString& 
sBuffer);
+
+    static sal_Int32 decodeSomeChars(css::uno::Sequence<sal_Int8>& aPass, 
const OUString& sBuffer);
+};
+
+}
+
+#endif // INCLUDED_COMPHELPER_BASE64_HXX
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
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)
_______________________________________________
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits

Reply via email to