157 std::unique_ptr<OTDB::OTPacker> OTASCIIArmor::s_pPacker;
171 return s_pPacker.get();
191 std::string str_bookend)
196 const std::string str_escaped(
"- " + str_bookend);
198 const bool bEscaped = strInput.
Contains(str_escaped);
214 ascArmor.
Set(strInput.
Get());
276 if ((&strValue) != (&(dynamic_cast<const OTString&>(*
this)))) {
292 if ((&strValue) !=
this)
304 int32_t compressionlevel = Z_BEST_COMPRESSION)
307 memset(&zs, 0,
sizeof(zs));
309 if (deflateInit(&zs, compressionlevel) != Z_OK)
310 throw(std::runtime_error(
"deflateInit failed while compressing."));
312 zs.next_in = (Bytef*)str.data();
313 zs.avail_in =
static_cast<uInt
>(str.size());
316 char outbuffer[32768];
317 std::string outstring;
321 zs.next_out =
reinterpret_cast<Bytef*
>(outbuffer);
322 zs.avail_out =
sizeof(outbuffer);
324 ret = deflate(&zs, Z_FINISH);
326 if (outstring.size() < zs.total_out) {
328 outstring.append(outbuffer, zs.total_out - outstring.size());
330 }
while (ret == Z_OK);
334 if (ret != Z_STREAM_END) {
335 std::ostringstream oss;
336 oss <<
"Exception during zlib compression: (" << ret <<
") " << zs.msg;
337 throw(std::runtime_error(oss.str()));
347 memset(&zs, 0,
sizeof(zs));
349 if (inflateInit(&zs) != Z_OK)
350 throw(std::runtime_error(
"inflateInit failed while decompressing."));
352 zs.next_in = (Bytef*)str.data();
353 zs.avail_in =
static_cast<uInt
>(str.size());
356 char outbuffer[32768];
357 std::string outstring;
361 zs.next_out =
reinterpret_cast<Bytef*
>(outbuffer);
362 zs.avail_out =
sizeof(outbuffer);
364 ret = inflate(&zs, 0);
366 if (outstring.size() < zs.total_out) {
367 outstring.append(outbuffer, zs.total_out - outstring.size());
370 }
while (ret == Z_OK);
374 if (ret != Z_STREAM_END) {
375 std::ostringstream oss;
376 oss <<
"Exception during zlib decompression: (" << ret <<
")";
377 if (zs.msg !=
nullptr) {
378 oss <<
" " << zs.msg;
380 throw(std::runtime_error(oss.str()));
399 OTString& strData,
bool bLineBreaks)
const
402 uint8_t* pData =
nullptr;
406 if (GetLength() < 1) {
415 std::string str_decoded(pData, pData + outSize);
420 std::string str_uncompressed =
"";
424 catch (
const std::runtime_error&) {
425 otErr <<
"Failed decompressing string in "
426 "OTASCIIArmor::GetAndUnpackString.\n";
437 std::unique_ptr<OTDB::PackedBuffer> pBuffer(pPacker->
CreateBuffer());
441 reinterpret_cast<const uint8_t*>(str_uncompressed.data()),
442 str_uncompressed.size());
444 std::unique_ptr<OTDB::OTDBString> pOTDBString(
445 dynamic_cast<OTDB::OTDBString*>(
449 bool bUnpacked = pPacker->
Unpack(*pBuffer, *pOTDBString);
452 otErr <<
"Failed unpacking string in "
453 "OTASCIIArmor::GetAndUnpackString.\n";
460 strData.Set(pOTDBString->
m_string.c_str(),
461 static_cast<uint32_t
>(pOTDBString->
m_string.length()));
466 otErr <<
"OTASCIIArmor::GetAndUnpackString: nullptr pData while "
467 "base64-decoding pData.\n";
485 bool bLineBreaks)
const
487 return GetAndUnpackString(strData, bLineBreaks);
491 bool bLineBreaks)
const
497 std::map<std::string, std::string>& the_map,
bool bLineBreaks)
const
500 uint8_t* pData =
nullptr;
515 std::unique_ptr<OTDB::PackedBuffer> pBuffer(
519 pBuffer->SetData(static_cast<const uint8_t*>(pData), outSize);
523 std::unique_ptr<OTDB::StringMap> pStringMap(
524 dynamic_cast<OTDB::StringMap*>(
528 bool bUnpacked = pPacker->
Unpack(*pBuffer, *pStringMap);
531 otErr <<
"Failed unpacking data in "
532 "OTASCIIArmor::GetAndUnpackStringMap.\n";
538 the_map = pStringMap->the_map;
545 otErr <<
"Error while base64_decoding in "
546 "OTASCIIArmor::GetAndUnpackStringMap.\n";
552 const std::map<std::string, std::string>& the_map,
bool bLineBreaks)
558 const std::map<std::string, std::string>& the_map,
bool bLineBreaks)
560 char* pString =
nullptr;
564 if (the_map.empty())
return true;
577 std::unique_ptr<OTDB::StringMap> pStringMap(dynamic_cast<OTDB::StringMap*>(
584 pStringMap->the_map = the_map;
586 std::unique_ptr<OTDB::PackedBuffer> pBuffer(pPacker->
Pack(
589 if (
nullptr == pBuffer) {
590 otErr <<
"Failed packing data in OTASCIIArmor::SetAndPackStringMap. \n";
594 const uint8_t* pUint =
static_cast<const uint8_t*
>(pBuffer->GetData());
595 const size_t theSize = pBuffer->GetSize();
597 if (
nullptr != pUint)
599 pUint, static_cast<int32_t>(theSize), bLineBreaks);
603 otErr <<
"Error while base64_encoding in "
604 "OTASCIIArmor::SetAndPackStringMap.\n";
608 if (
nullptr != pString) {
615 otErr <<
"Error while base64_encoding in "
616 "OTASCIIArmor::SetAndPackStringMap.\n";
624 bool bLineBreaks)
const
626 return GetAndUnpackData(theData, bLineBreaks);
634 bool bLineBreaks)
const
637 uint8_t* pData =
nullptr;
641 if (GetLength() < 1)
return true;
653 std::unique_ptr<OTDB::PackedBuffer> pBuffer(pPacker->
CreateBuffer());
656 pBuffer->SetData(static_cast<const uint8_t*>(pData), outSize);
660 std::unique_ptr<OTDB::Blob> pBlob(dynamic_cast<OTDB::Blob*>(
664 bool bUnpacked = pPacker->
Unpack(*pBuffer, *pBlob);
668 <<
"Failed unpacking data in OTASCIIArmor::GetAndUnpackData.\n";
681 otErr <<
"Error while base64_decoding in "
682 "OTASCIIArmor::GetAndUnpackData.\n";
700 char* pString =
nullptr;
704 if (theData.
GetSize() < 1)
return true;
716 std::unique_ptr<OTDB::Blob> pBlob(
722 pBlob->m_memBuffer.assign(
723 static_cast<const uint8_t*>(theData.
GetPointer()),
726 std::unique_ptr<OTDB::PackedBuffer> pBuffer(pPacker->
Pack(
729 if (
nullptr == pBuffer) {
730 otErr <<
"Failed packing data in OTASCIIArmor::SetAndPackData. \n";
734 const uint8_t* pUint =
static_cast<const uint8_t*
>(pBuffer->GetData());
735 const size_t theSize = pBuffer->GetSize();
737 if (
nullptr != pUint)
739 pUint, static_cast<int32_t>(theSize), bLineBreaks);
744 <<
"Error while base64_encoding in OTASCIIArmor::SetAndPackData.\n";
748 if (
nullptr != pString) {
756 <<
"Error while base64_encoding in OTASCIIArmor::SetAndPackData.\n";
777 if (strData.
GetLength() < 1)
return true;
789 std::unique_ptr<OTDB::OTDBString> pOTDBString(
790 dynamic_cast<OTDB::OTDBString*>(
795 const uint32_t theStringSize32 = strData.
GetLength();
796 const size_t theStringSize =
800 pOTDBString->m_string.assign(strData.
Get(),
803 std::unique_ptr<OTDB::PackedBuffer> pBuffer(
804 pPacker->
Pack(*pOTDBString));
807 if (
nullptr == pBuffer) {
808 otErr <<
"Failed packing string in OTASCIIArmor::SetAndPackString. \n";
812 std::string str_packed(reinterpret_cast<const char*>(pBuffer->GetData()),
818 if (str_compressed.size()) {
823 (
const uint8_t*)(str_compressed.data()),
824 static_cast<int32_t>(str_compressed.size()), bLineBreaks);
833 otErr <<
"OTASCIIArmor::" << __FUNCTION__ <<
": pString nullptr.\n";
837 otErr <<
"OTASCIIArmor::" << __FUNCTION__ <<
": nDestLen 0.\n";
861 otErr <<
"OTASCIIArmor::LoadFromFile: File does not exist: "
868 foldername.
Get(), filename.
Get()));
871 otErr <<
"OTASCIIArmor::LoadFromFile: Error reading file: "
881 std::ifstream fin(filename.c_str(), std::ios::binary);
883 if (!fin.is_open()) {
884 otWarn <<
"OTASCIIArmor::LoadFromExactPath: Failed opening file: "
896 std::stringstream buffer;
897 buffer << fin.rdbuf();
899 std::string contents(buffer.str());
902 theString.
Set(contents.c_str());
909 std::ofstream fout(filename.c_str(), std::ios::out | std::ios::binary);
911 if (!fout.is_open()) {
912 otWarn <<
"OTASCIIArmor::SaveToExactPath: Failed opening file: "
923 std::string str_type(
"DATA");
931 otErr << __FUNCTION__ <<
": Failed saving to file.\n Contents:\n\n"
932 << strOutput <<
"\n\n";
948 std::string str_type,
958 strOutput.Exists()) {
968 otErr <<
"OTASCIIArmor::WriteArmoredFile"
969 <<
": Failed saving to file: %s%s%s\n\n Contents:\n\n"
970 << strOutput <<
"\n\n",
987 std::string str_type,
991 const char* szEscape =
"- ";
996 "Version: Open Transactions %s\n"
998 "http://github.com/FellowTraveler/Open-Transactions/wiki\n\n"
1002 bEscaped ? szEscape :
"",
1029 std::string str_override)
1036 const std::string str_end_line =
1039 const int32_t nBufSize = 2100;
1040 const int32_t nBufSize2 = 2048;
1044 std::fill(&buffer1[0], &buffer1[(nBufSize - 1)], 0);
1046 bool bContentMode =
false;
1047 bool bHaveEnteredContentMode =
1055 bool bIsEOF =
false;
1060 bIsEOF = !(theStr.
sgets(buffer1, nBufSize2));
1062 std::string line = buffer1;
1063 const char* pConstBuf = line.c_str();
1064 char* pBuf = (
char*)pConstBuf;
1067 if (line.length() < 2) {
1072 else if (line.at(0) ==
'-' && line.at(2) ==
'-' && line.at(3) ==
'-' &&
1073 (bEscaped ? (line.at(1) ==
' ') : (line.at(1) ==
'-'))) {
1078 if (!bHaveEnteredContentMode) {
1084 if (line.find(str_override) != std::string::npos &&
1085 line.at(0) ==
'-' && line.at(2) ==
'-' &&
1086 line.at(3) ==
'-' &&
1087 (bEscaped ? (line.at(1) ==
' ') : (line.at(1) ==
'-'))) {
1090 bHaveEnteredContentMode =
true;
1091 bContentMode =
true;
1100 else if (bContentMode &&
1102 (line.find(str_end_line) != std::string::npos)) {
1107 bContentMode =
false;
1114 if (bHaveEnteredContentMode && bContentMode) {
1115 if (line.compare(0, 8,
"Version:") == 0) {
1119 if (line.compare(0, 8,
"Comment:") == 0) {
1130 }
while (!bIsEOF && (bContentMode || !bHaveEnteredContentMode));
1135 if (!bHaveEnteredContentMode) {
1136 otErr <<
"Error in OTASCIIArmor::LoadFromString: EOF before "
1138 "content found, in:\n\n" << theStr <<
"\n\n";
1141 else if (bContentMode) {
1143 <<
"Error in OTASCIIArmor::LoadFromString: EOF while still reading "
1144 "content, in:\n\n" << theStr <<
"\n\n";
static EXPORT OTCrypto * It()
EXPORT OTString & operator=(OTString rhs)
EXPORT bool StorePlainString(std::string strContents, std::string strFolder, std::string oneStr="", std::string twoStr="", std::string threeStr="")
EXPORT Storable * CreateObject(StoredObjectType eType)
EXPORT bool LoadFromExactPath(const std::string &filename)
EXPORT bool Unpack(PackedBuffer &inBuf, Storable &outObj)
EXPORT bool SetAndPackString(const OTString &theData, bool bLineBreaks=true)
const char * OT_BEGIN_ARMORED_escaped
PackedBuffer * Pack(Storable &inObj)
const char * OT_BEGIN_SIGNED_escaped
std::vector< uint8_t > m_memBuffer
EXPORT bool GetAndUnpackStringMap(std::map< std::string, std::string > &the_map, bool bLineBreaks=true) const
virtual EXPORT ~OTASCIIArmor()
#define OTDB_DEFAULT_PACKER
const char * OT_END_ARMORED
EXPORT bool GetStringMap(std::map< std::string, std::string > &the_map, bool bLineBreaks=true) const
static EXPORT const char * PathSeparator()
std::string compress_string(const std::string &str, int32_t compressionlevel=Z_BEST_COMPRESSION)
const char * OT_END_ARMORED_escaped
EXPORT uint32_t GetLength() const
EXPORT void Concatenate(const char *arg,...)
EXPORT bool WriteArmoredString(OTString &strOutput, const std::string str_type, bool bEscaped=false) const
EXPORT bool Exists() const
EXPORT bool SetString(const OTString &theData, bool bLineBreaks=true)
static EXPORT bool LoadFromString(OTASCIIArmor &ascArmor, const OTString &strInput, std::string str_bookend="-----BEGIN")
EXPORT bool LoadFromFile(const OTString &foldername, const OTString &filename)
EXPORT void Format(const char *fmt,...)
EXPORT bool SaveToExactPath(const std::string &filename)
EXPORT bool SetAndPackData(const OTData &theData, bool bLineBreaks=true)
static EXPORT const char * Version()
std::string decompress_string(const std::string &str)
EXPORT void Set(const char *data, uint32_t enforcedMaxLength=0)
EXPORT OTASCIIArmor & operator=(const char *szValue)
EXPORT std::string QueryPlainString(std::string strFolder, std::string oneStr="", std::string twoStr="", std::string threeStr="")
EXPORT bool SaveTo_ofstream(std::ofstream &fout)
EXPORT bool GetAsciiArmoredData(OTASCIIArmor &theArmoredText, bool bLineBreaks=true) const
bool sgets(char *buffer, uint32_t size)
EXPORT bool GetAndUnpackData(OTData &theData, bool bLineBreaks=true) const
const char * OT_BEGIN_SIGNED
virtual bool Base64Encode(const OTData &theInput, OTString &strOutput, bool bLineBreaks=true) const
static OTPacker * Create(PackType ePackType)
EXPORT bool LoadFrom_ifstream(std::ifstream &fin)
static OTDB::OTPacker * GetPacker()
EXPORT bool GetData(OTData &theData, bool bLineBreaks=true) const
OTLOG_IMPORT OTLogStream otWarn
EXPORT bool WriteArmoredFile(const OTString &foldername, const OTString &filename, const std::string str_type, bool bEscaped=false) const
EXPORT const char * Get() const
OTLOG_IMPORT OTLogStream otErr
const void * GetPointer() const
virtual PackedBuffer * CreateBuffer()=0
EXPORT bool Exists(std::string strFolder, std::string oneStr="", std::string twoStr="", std::string threeStr="")
virtual bool Base64Decode(const OTString &strInput, OTData &theOutput, bool bLineBreaks=true) const
EXPORT bool Contains(const char *compare) const
EXPORT bool SetData(const OTData &theData, bool bLineBreaks=true)
EXPORT bool GetString(OTString &theData, bool bLineBreaks=true) const
EXPORT bool SetStringMap(const std::map< std::string, std::string > &the_map, bool bLineBreaks=true)
virtual EXPORT void Release()
EXPORT bool GetAndUnpackString(OTString &theData, bool bLineBreaks=true) const
const char * OT_BEGIN_ARMORED
EXPORT bool SetAndPackStringMap(const std::map< std::string, std::string > &the_map, bool bLineBreaks=true)