The class FSD_Base64Decoder has been renamed to FSD_Base64.
The new method FSD_Base64::Encode has been added.
FILES
-FSD_Base64Decoder.cxx
-FSD_Base64Decoder.hxx
+FSD_Base64.cxx
+FSD_Base64.hxx
FSD_BinaryFile.cxx
FSD_BinaryFile.hxx
FSD_BStream.hxx
--- /dev/null
+// Copyright (c) 2016-2019 OPEN CASCADE SAS
+//
+// This file is part of Open CASCADE Technology software library.
+//
+// This library is free software; you can redistribute it and/or modify it under
+// the terms of the GNU Lesser General Public License version 2.1 as published
+// by the Free Software Foundation, with special exception defined in the file
+// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
+// distribution for complete text of the license and disclaimer of any warranty.
+//
+// Alternatively, this file may be used under the terms of Open CASCADE
+// commercial license or contractual agreement.
+
+// This code is taken from the snippet provided by the user polfosol
+// in the discussion https://stackoverflow.com/questions/180947/base64-decode-snippet-in-c
+
+#include <FSD_Base64.hxx>
+#include <Message.hxx>
+#include <Message_Messenger.hxx>
+
+// =======================================================================
+// function : Encode
+// =======================================================================
+Standard_Size FSD_Base64::Encode (char* theEncodedStr,
+ const Standard_Byte* theData,
+ const Standard_Size theDataLen)
+{
+ static const char* B64chars =
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
+
+ if (theDataLen == 0)
+ {
+ return 0;
+ }
+
+ Standard_Size aPad = theDataLen % 3;
+ const Standard_Size aSize64 = 4 * (int(aPad > 0) + theDataLen / 3);
+ if (!theEncodedStr)
+ {
+ return aSize64;
+ }
+
+ Standard_Size iStr = 0;
+ for (Standard_Size i = 0; i < theDataLen - aPad; i += 3)
+ {
+ int aWord = int(theData[i]) << 16 | int(theData[i + 1]) << 8 | theData[i + 2];
+ theEncodedStr[iStr++] = static_cast<char> (B64chars[aWord >> 18]);
+ theEncodedStr[iStr++] = static_cast<char> (B64chars[aWord >> 12 & 0x3F]);
+ theEncodedStr[iStr++] = static_cast<char> (B64chars[aWord >> 6 & 0x3F]);
+ theEncodedStr[iStr++] = static_cast<char> (B64chars[aWord & 0x3F]);
+ }
+ if (aPad--) /// padding
+ {
+ int aWord = aPad ? int(theData[theDataLen - 2]) << 8 | theData[theDataLen - 1]
+ : theData[theDataLen - 1];
+ theEncodedStr[iStr++] = static_cast<char> (aPad ? B64chars[aWord >> 10] : B64chars[aWord >> 2]);
+ theEncodedStr[iStr++] = static_cast<char> (aPad ? B64chars[aWord >> 4 & 0x03F] : B64chars[(aWord & 3) << 4]);
+ theEncodedStr[iStr++] = static_cast<char> (aPad ? B64chars[(aWord & 0xF) << 2] : '=');
+ }
+ while (iStr < aSize64)
+ {
+ theEncodedStr[iStr++] = '=';
+ }
+ return aSize64;
+}
+
+// =======================================================================
+// function : Decode
+// =======================================================================
+Standard_Size FSD_Base64::Decode (Standard_Byte* theDecodedData,
+ Standard_CString theEncodedStr,
+ const Standard_Size theStrLen)
+{
+ static const Standard_Byte B64index[256] = {
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 62, 63, 62, 62, 63, 52, 53, 54, 55,
+ 56, 57, 58, 59, 60, 61, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6,
+ 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0,
+ 0, 0, 0, 63, 0, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
+ 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51 };
+
+ if (theStrLen == 0)
+ {
+ return 0;
+ }
+
+ // Calculate pad bytes and number of bytes without pad
+ Standard_Integer aPad = theStrLen > 0 && (theStrLen % 4 || theEncodedStr[theStrLen - 1] == '=');
+ const Standard_Size aNbIter = ((theStrLen + 3) / 4 - aPad) * 4;
+ if (theStrLen > aNbIter + 2 && theEncodedStr[aNbIter + 2] != '=')
+ {
+ aPad++;
+ }
+
+ // Calculate new size and allocate the buffer
+ const Standard_Size aDecodedSize = aNbIter / 4 * 3 + aPad;
+ if (!theDecodedData)
+ {
+ return aDecodedSize;
+ }
+
+ // Decoding loop
+ for (Standard_Size i = 0; i < aNbIter; i += 4)
+ {
+ Standard_Integer aWord = B64index[Standard_Size (theEncodedStr[i])] << 18 |
+ B64index[Standard_Size (theEncodedStr[i + 1])] << 12 |
+ B64index[Standard_Size (theEncodedStr[i + 2])] << 6 |
+ B64index[Standard_Size (theEncodedStr[i + 3])];
+ *theDecodedData++ = static_cast<Standard_Byte> (aWord >> 16);
+ *theDecodedData++ = static_cast<Standard_Byte> (aWord >> 8 & 0xFF);
+ *theDecodedData++ = static_cast<Standard_Byte> (aWord & 0xFF);
+ }
+
+ // Decoding pad bytes
+ if (aPad > 0)
+ {
+ Standard_Integer aWord = B64index[Standard_Size (theEncodedStr[aNbIter])] << 18 |
+ B64index[Standard_Size (theEncodedStr[aNbIter + 1])] << 12;
+ *theDecodedData++ = static_cast<Standard_Byte> (aWord >> 16);
+
+ if (aPad > 1)
+ {
+ aWord |= B64index[Standard_Size (theEncodedStr[aNbIter + 2])] << 6;
+ *theDecodedData++ = static_cast<Standard_Byte> (aWord >> 8 & 0xFF);
+ }
+ }
+ return aDecodedSize;
+}
+
+// =======================================================================
+// function : Decode
+// =======================================================================
+Handle(NCollection_Buffer) FSD_Base64::Decode (Standard_CString theEncodedStr,
+ const Standard_Size theStrLen)
+{
+ Standard_Size aDataSize = Decode (NULL, theEncodedStr, theStrLen);
+ Handle(NCollection_Buffer) aBuf =
+ new NCollection_Buffer (NCollection_BaseAllocator::CommonBaseAllocator());
+ if (!aBuf->Allocate (aDataSize))
+ {
+ Message::DefaultMessenger()->Send ("Fail to allocate memory.", Message_Fail);
+ return NULL;
+ }
+ Decode (aBuf->ChangeData(), theEncodedStr, theStrLen);
+ return aBuf;
+}
--- /dev/null
+// Copyright (c) 2016-2019 OPEN CASCADE SAS
+//
+// This file is part of Open CASCADE Technology software library.
+//
+// This library is free software; you can redistribute it and/or modify it under
+// the terms of the GNU Lesser General Public License version 2.1 as published
+// by the Free Software Foundation, with special exception defined in the file
+// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
+// distribution for complete text of the license and disclaimer of any warranty.
+//
+// Alternatively, this file may be used under the terms of Open CASCADE
+// commercial license or contractual agreement.
+
+#ifndef _FSD_Base64_HeaderFile
+#define _FSD_Base64_HeaderFile
+
+#include <NCollection_Buffer.hxx>
+
+//! Tool for encoding/decoding base64 stream.
+class FSD_Base64
+{
+public:
+
+ //! Function encoding a buffer to base64.
+ //! @param theEncodedStr [out] the place for encoded string. Terminating null is not put.
+ //! If it is NULL just return the needed size.
+ //! @param theData [in] the input binary data.
+ //! @param theDataLen [in] the length of input data in bytes.
+ //! @return the length of the encoded string not including terminating null.
+ Standard_EXPORT static Standard_Size Encode (char* theEncodedStr,
+ const Standard_Byte* theData,
+ const Standard_Size theDataLen);
+
+ //! Function decoding base64 string.
+ //! @param theDecodedData [out] the place for decoded data.
+ //! If it is NULL just return the needed size.
+ //! @param theEncodedStr [in] the input encoded string.
+ //! @param theStrLen [in] the length of input encoded string.
+ //! @return the length of the decoded data in bytes.
+ Standard_EXPORT static Standard_Size Decode (Standard_Byte* theDecodedData,
+ Standard_CString theEncodedStr,
+ const Standard_Size theStrLen);
+
+ //! Function decoding base64 string.
+ //! @param theEncodedStr [in] the input encoded string.
+ //! @param theStrLen [in] the length of input encoded string.
+ //! @return null handle if theLen is 0 or in case of out of memory condition.
+ Standard_EXPORT static Handle(NCollection_Buffer) Decode (Standard_CString theStr,
+ const Standard_Size theLen);
+};
+
+#endif // _FSD_Base64_HeaderFile
+++ /dev/null
-// Author: Kirill Gavrilov
-// Copyright (c) 2016-2019 OPEN CASCADE SAS
-//
-// This file is part of Open CASCADE Technology software library.
-//
-// This library is free software; you can redistribute it and/or modify it under
-// the terms of the GNU Lesser General Public License version 2.1 as published
-// by the Free Software Foundation, with special exception defined in the file
-// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
-// distribution for complete text of the license and disclaimer of any warranty.
-//
-// Alternatively, this file may be used under the terms of Open CASCADE
-// commercial license or contractual agreement.
-
-#include <FSD_Base64Decoder.hxx>
-
-#include <Message.hxx>
-#include <Message_Messenger.hxx>
-
-// =======================================================================
-// function : Decode
-// purpose :
-// =======================================================================
-Handle(NCollection_Buffer) FSD_Base64Decoder::Decode (const Standard_Byte* theStr,
- const Standard_Size theLen)
-{
- //! Look-up table for decoding base64 stream.
- static const Standard_Byte THE_BASE64_FROM[128] =
- {
- 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
- 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
- 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 62, 255, 62, 255, 63,
- 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255, 0, 255, 255, 255,
- 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
- 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 255, 255, 255, 255, 63,
- 255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
- 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 255, 255, 255, 255, 255
- };
-
- Handle(NCollection_Buffer) aData = new NCollection_Buffer (NCollection_BaseAllocator::CommonBaseAllocator());
- if (!aData->Allocate (3 * theLen / 4))
- {
- Message::DefaultMessenger()->Send ("Fail to allocate memory.", Message_Fail);
- return Handle(NCollection_Buffer)();
- }
-
- Standard_Byte* aDataPtr = aData->ChangeData();
- const Standard_Byte* anEnd = theStr + theLen;
- for (const Standard_Byte* aByteIter = theStr; aByteIter < anEnd; aByteIter += 4)
- {
- // get values for each group of four base 64 characters
- const Standard_Byte b4[4] =
- {
- aByteIter + 0 < anEnd && aByteIter[0] <= 'z' ? THE_BASE64_FROM[aByteIter[0]] : Standard_Byte(0xFF),
- aByteIter + 1 < anEnd && aByteIter[1] <= 'z' ? THE_BASE64_FROM[aByteIter[1]] : Standard_Byte(0xFF),
- aByteIter + 2 < anEnd && aByteIter[2] <= 'z' ? THE_BASE64_FROM[aByteIter[2]] : Standard_Byte(0xFF),
- aByteIter + 3 < anEnd && aByteIter[3] <= 'z' ? THE_BASE64_FROM[aByteIter[3]] : Standard_Byte(0xFF)
- };
-
- // transform into a group of three bytes
- const Standard_Byte b3[3] =
- {
- Standard_Byte(((b4[0] & 0x3F) << 2) + ((b4[1] & 0x30) >> 4)),
- Standard_Byte(((b4[1] & 0x0F) << 4) + ((b4[2] & 0x3C) >> 2)),
- Standard_Byte(((b4[2] & 0x03) << 6) + ((b4[3] & 0x3F) >> 0))
- };
-
- // add the byte to the return value if it isn't part of an '=' character (indicated by 0xFF)
- if (b4[1] != 0xFF)
- {
- *aDataPtr = b3[0];
- ++aDataPtr;
- }
- if (b4[2] != 0xFF)
- {
- *aDataPtr = b3[1];
- ++aDataPtr;
- }
- if (b4[3] != 0xFF)
- {
- *aDataPtr = b3[2];
- ++aDataPtr;
- }
- }
-
- return aData;
-}
+++ /dev/null
-// Author: Kirill Gavrilov
-// Copyright (c) 2016-2019 OPEN CASCADE SAS
-//
-// This file is part of Open CASCADE Technology software library.
-//
-// This library is free software; you can redistribute it and/or modify it under
-// the terms of the GNU Lesser General Public License version 2.1 as published
-// by the Free Software Foundation, with special exception defined in the file
-// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
-// distribution for complete text of the license and disclaimer of any warranty.
-//
-// Alternatively, this file may be used under the terms of Open CASCADE
-// commercial license or contractual agreement.
-
-#ifndef _FSD_Base64Decoder_HeaderFile
-#define _FSD_Base64Decoder_HeaderFile
-
-#include <NCollection_Buffer.hxx>
-
-//! Tool decoding base64 stream.
-class FSD_Base64Decoder
-{
-public:
-
- //! Function decoding base64 stream.
- Standard_EXPORT static Handle(NCollection_Buffer) Decode (const Standard_Byte* theStr,
- const Standard_Size theLen);
-};
-
-#endif // _FSD_Base64Decoder_HeaderFile
#include <OSD_Path.hxx>
#include <OSD_ThreadPool.hxx>
#include <Precision.hxx>
-#include <FSD_Base64Decoder.hxx>
+#include <FSD_Base64.hxx>
#include <TopExp_Explorer.hxx>
#include <TopoDS.hxx>
#include <TopoDS_Iterator.hxx>
const char* aBase64Data = aDataIter + 8;
const size_t aBase64Len = size_t(aBase64End - aBase64Data);
//const TCollection_AsciiString aMime (aDataStart, aDataIter - aDataStart);
- Handle(NCollection_Buffer) aData = FSD_Base64Decoder::Decode ((const Standard_Byte* )aBase64Data, aBase64Len);
+ Handle(NCollection_Buffer) aData = FSD_Base64::Decode (aBase64Data, aBase64Len);
theTexture = new Image_Texture (aData, myFilePath + "@" + getKeyString (*aSrcVal));
return true;
}
if (!myDecodedBuffers.Find (theName, aData.StreamData))
{
// it is better decoding in multiple threads
- aData.StreamData = FSD_Base64Decoder::Decode ((const Standard_Byte* )anUriData + 37, anUriVal->GetStringLength() - 37);
+ aData.StreamData = FSD_Base64::Decode (anUriData + 37, anUriVal->GetStringLength() - 37);
myDecodedBuffers.Bind (theName, aData.StreamData);
}
return true;