void WriteFPRData(const char* replayPath, FPR_DATA* data, int nElements) { REPLAY_FOOTER footer; footer.version = 1; footer.magic1 = MAGIC_1; footer.magic2 = MAGIC_2; footer.magic3 = MAGIC_3; // compress the data long compressedSize = EZ_COMPRESSMAXDESTLENGTH(nElements * sizeof(FPR_DATA)); unsigned char* pCompressedData = (unsigned char*)malloc(compressedSize); ezcompress(pCompressedData, &compressedSize, (unsigned char*)data, nElements * sizeof(FPR_DATA)); // write the data to the file footer.sizeOfData = compressedSize; FILE* fpReplay = fopen(replayPath, "ab"); fwrite(pCompressedData, footer.sizeOfData, 1, fpReplay); fwrite(&footer, sizeof(REPLAY_FOOTER), 1, fpReplay); fclose(fpReplay); free(pCompressedData); }
/// This function first Packs the incoming string, using whatever is the default packer. (MsgPack or Protobuf). /// Then it Compresses the packed binary data using zlib. (ezcompress.) /// Then it Base64-Encodes the compressed binary and sets it as a string on THIS OBJECT. /// /// I added these pieces 1-by-1 over time. At first the messages were too long, so I started compressing them. /// Then they were not binary compatible across various platforms, so I added the packing. // bool OTASCIIArmor::SetAndPackString(const OTString & strData, bool bLineBreaks) //=true { Release(); if (strData.GetLength() < 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::OTDBString * pOTDBString = dynamic_cast<OTDB::OTDBString *>(OTDB::CreateObject(OTDB::STORED_OBJ_STRING)); OT_ASSERT(NULL != pOTDBString); // Beyond this point, responsible to delete pString. OTCleanup<OTDB::OTDBString> theStringAngel(*pOTDBString); // make sure memory is cleaned up. // ----------------------------- const uint32_t theStringSize32 = strData.GetLength(); const size_t theStringSize = theStringSize32; // might need a cast here. // todo make sure this will handle sizes as big as I need. pOTDBString->m_string.assign(strData.Get(), // const char * theStringSize); OTDB::PackedBuffer * pBuffer = pPacker->Pack(*pOTDBString); // Now we PACK our string before compressing/encoding it. if (NULL == pBuffer) { OTLog::Error("Failed packing string in OTASCIIArmor::SetAndPackString. \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(); // -------------------------------------------------------- char * pString = NULL; // Set up source buffer and destination buffer long nDestLen = DEFAULT_BUFFER_SIZE_EASYZLIB; // todo stop hardcoding numbers (but this one is OK I think.) const long lSourcelen = static_cast<long> (theSize); unsigned char* pSource = new unsigned char[lSourcelen+10]; // for safety unsigned char* pDest = new unsigned char[nDestLen +10]; // for safety OT_ASSERT(NULL != pSource); OT_ASSERT(NULL != pDest); OTPassword::zeroMemory(pSource, lSourcelen+10); OTPassword::zeroMemory(pDest, nDestLen +10); // void * OTPassword::safe_memcpy(void * dest, // uint32_t dest_size, // const // void * src, // uint32_t src_length, // bool bZeroSource/*=false*/) // if true, sets the source buffer to zero after copying is done. OTPassword::safe_memcpy(pSource, lSourcelen, pUint, static_cast<uint32_t>(theSize)); // Now we are compressing first before base64-encoding (for strings, anyway) int nErr = ezcompress( pDest, &nDestLen, pSource, lSourcelen ); // If the destination buffer wasn't the right size the first time around, // then we re-allocate it to the right size (which we now know) and try again... if ( nErr == EZ_BUF_ERROR ) { delete [] pDest; pDest = new unsigned char [nDestLen+10]; // enough room now OT_ASSERT(NULL != pDest); OTPassword::zeroMemory(pDest, nDestLen+10); nErr = ezcompress( pDest, &nDestLen, pSource, lSourcelen ); } // Clean this up... delete [] pSource; pSource = NULL; // Still errors? if ( nErr == EZ_BUF_ERROR ) { delete [] pDest; pDest = NULL; OT_FAIL_MSG("Error allocating memory in OTASCIIArmor::SetAndPackString\n"); } else if ( nErr == EZ_STREAM_ERROR ) { delete [] pDest; pDest = NULL; OT_FAIL_MSG( "pDest is NULL in OTASCIIArmor::SetAndPackString\n"); } else if ( nErr == EZ_DATA_ERROR ) { delete [] pDest; pDest = NULL; OT_FAIL_MSG("corrupted pSrc passed to ezuncompress OTASCIIArmor::SetAndPackString\n"); } else if ( nErr == EZ_MEM_ERROR ) { delete [] pDest; pDest = NULL; OT_FAIL_MSG("Out of memory in OTASCIIArmor::SetAndPackString\n"); } OT_ASSERT_MSG(pDest != NULL, "pDest NULL in OTASCIIArmor::SetAndPackString\n"); // Success if (0 < nDestLen) { // Now let's base-64 encode it... pString = OTCrypto::It()->Base64Encode((const uint8_t*)pDest, nDestLen, bLineBreaks); // pString = OT_base64_encode((const uint8_t*)pDest, nDestLen, (bLineBreaks ? 1 : 0)); delete [] pDest; pDest = NULL; if (pString) { Set(pString); delete [] pString; pString=NULL; return true; } else { OTLog::Error("pString NULL in OTASCIIArmor::SetAndPackString\n"); } } else { OTLog::Error("nDestLen 0 in OTASCIIArmor::SetAndPackString\n"); } if (pDest) delete [] pDest; pDest = NULL; return false; }
// This version is fully functional, and performs compression in addition to base64-encoding. // bool OTASCIIArmor::SetString(const OTString & theData, bool bLineBreaks) //=true { return SetAndPackString(theData, bLineBreaks); // --------------------------------------------------------------- // OTLog::vError("DEBUGGING OTASCIIARMOR::SETSTRING, INPUT: --------->%s<---------", theData.Get()); Release(); if (theData.GetLength() < 1) return true; char * pString = NULL; // Set up source buffer and destination buffer long nDestLen = DEFAULT_BUFFER_SIZE_EASYZLIB; // todo stop hardcoding numbers (but this one is OK I think.) const long lSourcelen = sizeof(unsigned char)*theData.GetLength()+1;// for null terminator unsigned char* pSource = new unsigned char[lSourcelen+10]; // for safety unsigned char* pDest = new unsigned char[nDestLen+10]; // for safety OT_ASSERT(NULL != pSource); OT_ASSERT(NULL != pDest); memcpy(pSource, (const unsigned char*)theData.Get(), lSourcelen ); // Now we are compressing first before base64-encoding (for strings, anyway) int nErr = ezcompress( pDest, &nDestLen, pSource, lSourcelen ); // If the destination buffer wasn't the right size the first time around, // then we re-allocate it to the right size (which we now know) and try again... if ( nErr == EZ_BUF_ERROR ) { delete [] pDest; pDest = new unsigned char [nDestLen]; // enough room now OT_ASSERT(NULL != pDest); nErr = ezcompress( pDest, &nDestLen, pSource, lSourcelen ); } // Clean this up... delete [] pSource; pSource = NULL; // Still errors? if ( nErr == EZ_BUF_ERROR ) { delete [] pDest; pDest = NULL; OT_ASSERT_MSG(false, "Error allocating memory in OTASCIIArmor::SetString\n"); return false; // not really necessary but just making sure. } else if ( nErr == EZ_STREAM_ERROR ) { delete [] pDest; pDest = NULL; OT_ASSERT_MSG(false, "pDest is NULL in OTASCIIArmor::SetString\n"); return false; // not really necessary but just making sure. } else if ( nErr == EZ_DATA_ERROR ) { delete [] pDest; pDest = NULL; OT_ASSERT_MSG(false, "corrupted pSrc passed to ezuncompress OTASCIIArmor::SetString\n"); return false; // not really necessary but just making sure. } else if ( nErr == EZ_MEM_ERROR ) { delete [] pDest; pDest = NULL; OT_ASSERT_MSG(false, "Out of memory in OTASCIIArmor::SetString\n"); return false; // not really necessary but just making sure. } OT_ASSERT_MSG(pDest != NULL, "pDest NULL in OTASCIIArmor::SetString\n"); // Success if (0 < nDestLen) { // Now let's base-64 encode it... pString = OT_base64_encode((const uint8_t*)pDest, nDestLen, (bLineBreaks ? 1 : 0)); // pString = OT_base64_encode((const uint8_t*)theData.Get(), theData.GetLength()+1, (bLineBreaks ? 1 : 0)); // this was before we used compression. delete [] pDest; pDest = NULL; if (pString) { Set(pString); delete [] pString; pString=NULL; return true; } else { OTLog::Error("pString NULL in OTASCIIArmor::SetString\n"); } } else { OTLog::Error("nDestLen 0 in OTASCIIArmor::SetString\n"); } if (pDest) delete [] pDest; pDest = NULL; return false; }