1
0
mirror of https://git.dev.opencascade.org/repos/occt.git synced 2025-04-04 18:06:22 +03:00

0031514: Foundation Classes - Add Base64 encoding function

The class FSD_Base64Decoder has been renamed to FSD_Base64.
The new method FSD_Base64::Encode has been added.
The method Decode has been improved for performance.
This commit is contained in:
msv 2020-04-23 13:13:11 +03:00 committed by bugmaster
parent da80ff68f1
commit 45d498ef1f
6 changed files with 263 additions and 142 deletions

View File

@ -1,6 +1,6 @@
FILES
FSD_Base64Decoder.cxx
FSD_Base64Decoder.hxx
FSD_Base64.cxx
FSD_Base64.hxx
FSD_BinaryFile.cxx
FSD_BinaryFile.hxx
FSD_BStream.hxx

177
src/FSD/FSD_Base64.cxx Normal file
View File

@ -0,0 +1,177 @@
// 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_Base64.hxx>
// =======================================================================
// function : Encode
// =======================================================================
Standard_Size FSD_Base64::Encode (char* theEncodedStr,
const Standard_Size theStrLen,
const Standard_Byte* theData,
const Standard_Size theDataLen)
{
static const char aBase64Chars[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
if (theDataLen == 0)
{
return 0;
}
Standard_Size aPad = theDataLen % 3;
const Standard_Size aSize64 = 4 * ((theDataLen + 2) / 3);
if (theEncodedStr == NULL)
{
return aSize64;
}
if (aSize64 > theStrLen)
{
return 0;
}
Standard_Size iStr = 0;
for (Standard_Size i = 0; i < theDataLen - aPad; i += 3)
{
uint32_t aWord = (uint32_t(theData[i]) << 16) +
(uint32_t(theData[i + 1]) << 8) +
theData[i + 2];
theEncodedStr[iStr++] = aBase64Chars[aWord >> 18];
theEncodedStr[iStr++] = aBase64Chars[aWord >> 12 & 0x3F];
theEncodedStr[iStr++] = aBase64Chars[aWord >> 6 & 0x3F];
theEncodedStr[iStr++] = aBase64Chars[aWord & 0x3F];
}
if (aPad-- != 0)
{
if (aPad != 0)
{
uint32_t aWord = uint32_t(theData[theDataLen - 2]) << 8 | theData[theDataLen - 1];
theEncodedStr[iStr++] = aBase64Chars[aWord >> 10];
theEncodedStr[iStr++] = aBase64Chars[aWord >> 4 & 0x03F];
theEncodedStr[iStr++] = aBase64Chars[(aWord & 0xF) << 2];
}
else
{
uint32_t aWord = theData[theDataLen - 1];
theEncodedStr[iStr++] = aBase64Chars[aWord >> 2];
theEncodedStr[iStr++] = aBase64Chars[(aWord & 3) << 4];
theEncodedStr[iStr++] = '=';
}
}
while (iStr < aSize64)
{
theEncodedStr[iStr++] = '=';
}
return aSize64;
}
// =======================================================================
// function : Encode
// =======================================================================
TCollection_AsciiString FSD_Base64::Encode(const Standard_Byte* theData,
const Standard_Size theDataLen)
{
Standard_Size aStrLen = Encode (NULL, 0, theData, theDataLen);
TCollection_AsciiString aStr ((Standard_Integer)aStrLen, 0);
Encode (const_cast<char*>(aStr.ToCString()), aStrLen, theData, theDataLen);
return aStr;
}
// =======================================================================
// function : Decode
// =======================================================================
Standard_Size FSD_Base64::Decode (Standard_Byte* theDecodedData,
const Standard_Size theDataLen,
Standard_CString theEncodedStr,
const Standard_Size theStrLen)
{
static const Standard_Byte aBase64Codes[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
uint32_t aPad (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
const Standard_Size aDecodedSize = aNbIter / 4 * 3 + aPad;
if (theDecodedData == NULL)
{
return aDecodedSize;
}
if (aDecodedSize > theDataLen)
{
return 0;
}
// Decoding loop
for (Standard_Size i = 0; i < aNbIter; i += 4)
{
unsigned aWord = (aBase64Codes[unsigned (theEncodedStr[i])] << 18) +
(aBase64Codes[unsigned (theEncodedStr[i + 1])] << 12) +
(aBase64Codes[unsigned (theEncodedStr[i + 2])] << 6) +
aBase64Codes[unsigned (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)
{
unsigned aWord = (aBase64Codes[unsigned (theEncodedStr[aNbIter])] << 18) +
(aBase64Codes[unsigned (theEncodedStr[aNbIter + 1])] << 12);
*theDecodedData++ = static_cast<Standard_Byte> (aWord >> 16);
if (aPad > 1)
{
aWord += (aBase64Codes[unsigned (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)
{
const Standard_Size aDataSize = Decode (NULL, 0, theEncodedStr, theStrLen);
Handle(NCollection_Buffer) aBuf = new NCollection_Buffer (NCollection_BaseAllocator::CommonBaseAllocator());
if (aDataSize == 0)
{
return aBuf;
}
if (!aBuf->Allocate (aDataSize))
{
return Handle(NCollection_Buffer)();
}
Decode (aBuf->ChangeData(), aDataSize, theEncodedStr, theStrLen);
return aBuf;
}

68
src/FSD/FSD_Base64.hxx Normal file
View File

@ -0,0 +1,68 @@
// 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>
#include <TCollection_AsciiString.hxx>
//! Tool for encoding/decoding base64 stream.
class FSD_Base64
{
public:
//! Function encoding a buffer to base64 string.
//! @param[out] theEncodedStr the place for encoded string. Terminating null is not put.
//! If it is NULL just return the needed size.
//! @param[in] theStrLen the length of the buffer theEncodedStr in bytes.
//! This value must not be less than value returned when theEncodedStr is NULL.
//! @param[in] theData the input binary data.
//! @param[in] theDataLen the length of input data in bytes.
//! @return the length of the encoded string not including terminating null.
//! If theStrLen is not enough for storing all data nothing is written and 0 is returned.
Standard_EXPORT static Standard_Size Encode (char* theEncodedStr,
const Standard_Size theStrLen,
const Standard_Byte* theData,
const Standard_Size theDataLen);
//! Function encoding a buffer to base64 string.
//! @param[in] theData the input binary data
//! @param[in] theDataLen the length of input data in bytes
//! @return Base64 encoded string.
Standard_EXPORT static TCollection_AsciiString Encode(const Standard_Byte* theData,
const Standard_Size theDataLen);
//! Function decoding base64 string.
//! @param[out] theDecodedData the place for decoded data.
//! If it is NULL just return the needed size.
//! @param[in] theDataLen the length of the buffer theDecodedData in bytes.
//! This value must not be less than value returned when theDecodedData is NULL.
//! @param[in] theEncodedStr the input encoded string.
//! @param[in] theStrLen the length of input encoded string.
//! @return the length of the decoded data in bytes. If theDataLen is not enough
//! for storing all data nothing is written and 0 is returned.
Standard_EXPORT static Standard_Size Decode (Standard_Byte* theDecodedData,
const Standard_Size theDataLen,
Standard_CString theEncodedStr,
const Standard_Size theStrLen);
//! Function decoding base64 string.
//! @param[in] theStr the input encoded string
//! @param[in] theLen the length of input encoded string
//! @return null handle 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

View File

@ -1,106 +0,0 @@
// 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>
//! Buffer with decoded data.
class FSD_Base64DecoderBuffer : public NCollection_Buffer
{
public:
//! Empty constructor.
FSD_Base64DecoderBuffer() : NCollection_Buffer (NCollection_BaseAllocator::CommonBaseAllocator()) {}
//! Shrink data size.
void ShrinkSize (Standard_Size theSize)
{
if (theSize < mySize)
{
mySize = theSize;
}
}
};
// =======================================================================
// 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(FSD_Base64DecoderBuffer) aData = new FSD_Base64DecoderBuffer();
if (!aData->Allocate (3 * theLen / 4))
{
Message::SendFail ("Fail to allocate memory.");
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;
}
}
// shrink buffer size to actual length
const Standard_Size aFinalLen = aDataPtr - aData->ChangeData();
aData->ShrinkSize (aFinalLen);
return aData;
}

View File

@ -1,30 +0,0 @@
// 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

View File

@ -24,7 +24,7 @@
#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>
@ -808,7 +808,11 @@ bool RWGltf_GltfJsonParser::gltfParseTexture (Handle(Image_Texture)& theTexture,
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);
if (aData.IsNull())
{
Message::SendFail ("Fail to allocate memory.");
}
theTexture = new Image_Texture (aData, myFilePath + "@" + getKeyString (*aSrcVal));
return true;
}
@ -933,7 +937,11 @@ bool RWGltf_GltfJsonParser::gltfParseTextureInBufferView (Handle(Image_Texture)&
Handle(NCollection_Buffer) aBaseBuffer;
if (!myDecodedBuffers.Find (aBufferId, aBaseBuffer))
{
aBaseBuffer = FSD_Base64Decoder::Decode ((const Standard_Byte* )anUriData + 37, anUriVal->GetStringLength() - 37);
aBaseBuffer = FSD_Base64::Decode (anUriData + 37, anUriVal->GetStringLength() - 37);
if (aBaseBuffer.IsNull())
{
Message::SendFail ("Fail to allocate memory.");
}
myDecodedBuffers.Bind (aBufferId, aBaseBuffer);
}
@ -1805,7 +1813,11 @@ bool RWGltf_GltfJsonParser::gltfParseBuffer (const Handle(RWGltf_GltfLatePrimiti
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);
if (aData.StreamData.IsNull())
{
Message::SendFail ("Fail to allocate memory.");
}
myDecodedBuffers.Bind (theName, aData.StreamData);
}
return true;