// This function will base64 ENCODE theData, // and then Set() that as the string contents. // Additionally it will pack and compress the data! // bool OTASCIIArmor::SetAndPackData(const OTData & theData, bool bLineBreaks/*=true*/) { char * pString = NULL; Release(); if (theData.GetSize() < 1) return true; // -------------------------------------------------------- OTDB::OTPacker * pPacker = OTASCIIArmor::GetPacker(); // No need to check for failure, since this already ASSERTS. No need to cleanup either. // Here I use the default storage context to create the object (the blob.) // I also originally created OTASCIIArmor::GetPacker() using OTDB_DEFAULT_PACKER, // so I know everything is compatible. // OTDB::Blob * pBlob = dynamic_cast<OTDB::Blob *>(OTDB::CreateObject(OTDB::STORED_OBJ_BLOB)); OT_ASSERT(NULL != pBlob); // Beyond this point, responsible to delete pBlob. OTCleanup<OTDB::Blob> theBlobAngel(*pBlob); // make sure memory is cleaned up. // ---------------------------- pBlob->m_memBuffer.assign(static_cast<const unsigned char *>(theData.GetPointer()), static_cast<const unsigned char *>(theData.GetPointer())+theData.GetSize()); OTDB::PackedBuffer * pBuffer = pPacker->Pack(*pBlob); // Now we PACK our data before compressing/encoding it. if (NULL == pBuffer) { OTLog::Error("Failed packing data in OTASCIIArmor::SetAndPackData. \n"); return false; } OTCleanup<OTDB::PackedBuffer> theBufferAngel(*pBuffer); // make sure memory is cleaned up. // -------------------------------------------------------- const uint8_t* pUint = static_cast<const uint8_t*>(pBuffer->GetData()); const size_t theSize = pBuffer->GetSize(); if (NULL != pUint) pString = OTCrypto::It()->Base64Encode(pUint, static_cast<int>(theSize), bLineBreaks); // pString = OT_base64_encode(pUint, static_cast<int> (theSize), (bLineBreaks ? 1 : 0)); else { OTLog::Error("Error while base64_encoding in OTASCIIArmor::SetAndPackData.\n"); return false; } // ------------------------------------- if (NULL != pString) { Set(pString); delete [] pString; pString=NULL; return true; } else { OTLog::Error("Error while base64_encoding in OTASCIIArmor::SetAndPackData.\n"); return false; } }
// This function will base64 DECODE the string contents // and return them as binary in theData // Additionally it will decompress and unpack the data! // bool OTASCIIArmor::GetAndUnpackData(OTData & theData, bool bLineBreaks) const //linebreaks=true { size_t outSize = 0; uint8_t * pData = NULL; theData.Release(); if (GetLength() < 1) return true; // -------------------------------------------------------------- // pData = OT_base64_decode(Get(), &outSize, (bLineBreaks ? 1 : 0)); if (pData) { // -------------------------------------------------------- OTDB::OTPacker * pPacker = OTASCIIArmor::GetPacker(); // No need to check for failure, since this already ASSERTS. No need to cleanup either. OTDB::PackedBuffer * pBuffer = pPacker->CreateBuffer(); // Need to clean this up. OT_ASSERT(NULL != pBuffer); OTCleanup<OTDB::PackedBuffer> theBufferAngel(*pBuffer); // make sure buffer is deleted. pBuffer->SetData(static_cast<const unsigned char*>(pData), outSize); delete [] pData; pData=NULL; // ----------------------------- OTDB::Blob * pBlob = dynamic_cast<OTDB::Blob *>(OTDB::CreateObject(OTDB::STORED_OBJ_BLOB)); OT_ASSERT(NULL != pBlob); OTCleanup<OTDB::Blob> theBlobAngel(*pBlob); // clean up this blob. bool bUnpacked = pPacker->Unpack(*pBuffer, *pBlob); // ---------------------- if (false == bUnpacked) { OTLog::Error("Failed unpacking data in OTASCIIArmor::GetAndUnpackData.\n"); delete [] pData; pData=NULL; return false; } // -------------------------------------------------------- theData.Assign(pBlob->m_memBuffer.data(), pBlob->m_memBuffer.size()); delete [] pData; pData=NULL; return true; } else { OTLog::Error("Error while base64_decoding in OTASCIIArmor::GetAndUnpackData.\n"); return false; } }