Open-Transactions  0.93.0-ge03d287
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
opentxs::OTASCIIArmor Class Reference

#include <OTASCIIArmor.hpp>

Inheritance diagram for opentxs::OTASCIIArmor:
Collaboration diagram for opentxs::OTASCIIArmor:

Public Member Functions

EXPORT OTASCIIArmor ()
 
EXPORT OTASCIIArmor (const char *szValue)
 
EXPORT OTASCIIArmor (const OTData &theValue)
 
EXPORT OTASCIIArmor (const OTPayload &theValue)
 
EXPORT OTASCIIArmor (const OTString &strValue)
 
EXPORT OTASCIIArmor (const OTASCIIArmor &strValue)
 
EXPORT OTASCIIArmor (const OTEnvelope &theEnvelope)
 
virtual EXPORT ~OTASCIIArmor ()
 
EXPORT OTASCIIArmoroperator= (const char *szValue)
 
EXPORT OTASCIIArmoroperator= (const OTData &theValue)
 
EXPORT OTASCIIArmoroperator= (const OTString &strValue)
 
EXPORT OTASCIIArmoroperator= (const OTASCIIArmor &strValue)
 
EXPORT bool LoadFromFile (const OTString &foldername, const OTString &filename)
 
EXPORT bool LoadFrom_ifstream (std::ifstream &fin)
 
EXPORT bool SaveTo_ofstream (std::ofstream &fout)
 
EXPORT bool LoadFromExactPath (const std::string &filename)
 
EXPORT bool SaveToExactPath (const std::string &filename)
 
EXPORT bool LoadFromString (OTString &theStr, bool bEscaped=false, const std::string str_override="-----BEGIN")
 
EXPORT bool WriteArmoredString (OTString &strOutput, const std::string str_type, bool bEscaped=false) const
 
EXPORT bool WriteArmoredFile (const OTString &foldername, const OTString &filename, const std::string str_type, bool bEscaped=false) const
 
EXPORT bool GetData (OTData &theData, bool bLineBreaks=true) const
 
EXPORT bool GetAndUnpackData (OTData &theData, bool bLineBreaks=true) const
 
EXPORT bool SetData (const OTData &theData, bool bLineBreaks=true)
 
EXPORT bool SetAndPackData (const OTData &theData, bool bLineBreaks=true)
 
EXPORT bool GetString (OTString &theData, bool bLineBreaks=true) const
 
EXPORT bool GetAndUnpackString (OTString &theData, bool bLineBreaks=true) const
 
EXPORT bool SetString (const OTString &theData, bool bLineBreaks=true)
 
EXPORT bool SetAndPackString (const OTString &theData, bool bLineBreaks=true)
 
EXPORT bool GetStringMap (std::map< std::string, std::string > &the_map, bool bLineBreaks=true) const
 
EXPORT bool GetAndUnpackStringMap (std::map< std::string, std::string > &the_map, bool bLineBreaks=true) const
 
EXPORT bool SetStringMap (const std::map< std::string, std::string > &the_map, bool bLineBreaks=true)
 
EXPORT bool SetAndPackStringMap (const std::map< std::string, std::string > &the_map, bool bLineBreaks=true)
 
- Public Member Functions inherited from opentxs::OTString
EXPORT OTString ()
 
EXPORT OTString (const OTString &value)
 
EXPORT OTString (const OTASCIIArmor &value)
 
 OTString (const OTSignature &value)
 
EXPORT OTString (const OTContract &value)
 
EXPORT OTString (const OTIdentifier &value)
 
 OTString (OTPseudonym &value)
 
EXPORT OTString (const char *value)
 
 OTString (const char *value, size_t size)
 
EXPORT OTString (const std::string &value)
 
virtual EXPORT ~OTString ()
 
virtual EXPORT void Release ()
 
void Initialize ()
 
EXPORT OTStringoperator= (OTString rhs)
 
void swap (OTString &rhs)
 
bool operator> (const OTString &rhs) const
 
bool operator< (const OTString &rhs) const
 
bool operator<= (const OTString &rhs) const
 
bool operator>= (const OTString &rhs) const
 
EXPORT bool operator== (const OTString &rhs) const
 
EXPORT int64_t ToLong () const
 
EXPORT uint64_t ToUlong () const
 
EXPORT bool At (uint32_t index, char &c) const
 
EXPORT bool Exists () const
 
EXPORT bool DecodeIfArmored (bool escapedIsAllowed=true)
 
EXPORT uint32_t GetLength () const
 
EXPORT bool Compare (const char *compare) const
 
EXPORT bool Compare (const OTString &compare) const
 
EXPORT bool Contains (const char *compare) const
 
bool Contains (const OTString &compare) const
 
EXPORT const char * Get () const
 
EXPORT void Set (const char *data, uint32_t enforcedMaxLength=0)
 
EXPORT void Set (const OTString &data)
 
EXPORT bool MemSet (const char *mem, uint32_t size)
 
EXPORT void Concatenate (const char *arg,...)
 
void Concatenate (const OTString &data)
 
void Truncate (uint32_t index)
 
EXPORT void Format (const char *fmt,...)
 
void ConvertToLowerCase () const
 
void ConvertToUpperCase () const
 
EXPORT bool TokenizeIntoKeyValuePairs (Map &map) const
 
EXPORT void OTfgets (std::istream &ofs)
 
bool sgets (char *buffer, uint32_t size)
 
char sgetc ()
 
void sungetc ()
 
void reset ()
 
void WriteToFile (std::ostream &ofs) const
 
void Release_String ()
 
EXPORT void zeroMemory () const
 

Static Public Member Functions

static OTDB::OTPackerGetPacker ()
 
static EXPORT bool LoadFromString (OTASCIIArmor &ascArmor, const OTString &strInput, std::string str_bookend="-----BEGIN")
 
- Static Public Member Functions inherited from opentxs::OTString
static bool vformat (const char *fmt, std::va_list *pvl, std::string &s)
 
static EXPORT std::string & trim (std::string &str)
 
static EXPORT std::string replace_chars (const std::string &str, const std::string &charsFrom, const char &charTo)
 
static EXPORT bool safe_strcpy (char *dest, const char *src, size_t destSize, bool zeroSource=false)
 
static size_t safe_strlen (const char *s, size_t max)
 
static EXPORT int64_t StringToLong (const std::string &number)
 
static EXPORT uint64_t StringToUlong (const std::string &number)
 

Additional Inherited Members

- Public Types inherited from opentxs::OTString
typedef std::list< std::string > List
 
typedef std::map< std::string,
std::string > 
Map
 
- Protected Attributes inherited from opentxs::OTString
uint32_t length_
 
uint32_t position_
 
char * data_
 

Detailed Description

Definition at line 170 of file OTASCIIArmor.hpp.

Constructor & Destructor Documentation

opentxs::OTASCIIArmor::OTASCIIArmor ( )

Definition at line 220 of file OTASCIIArmor.cpp.

221  : OTString()
222 {
223 }
EXPORT OTString()
Definition: OTString.cpp:786
opentxs::OTASCIIArmor::OTASCIIArmor ( const char *  szValue)

Definition at line 261 of file OTASCIIArmor.cpp.

262  : OTString(szValue)
263 {
264 }
EXPORT OTString()
Definition: OTString.cpp:786
opentxs::OTASCIIArmor::OTASCIIArmor ( const OTData theValue)

Definition at line 240 of file OTASCIIArmor.cpp.

241  : OTString()
242 {
243  SetData(theValue);
244 }
EXPORT OTString()
Definition: OTString.cpp:786
EXPORT bool SetData(const OTData &theData, bool bLineBreaks=true)
opentxs::OTASCIIArmor::OTASCIIArmor ( const OTPayload theValue)

Definition at line 233 of file OTASCIIArmor.cpp.

234  : OTString()
235 {
236  SetData(theValue);
237 }
EXPORT OTString()
Definition: OTString.cpp:786
EXPORT bool SetData(const OTData &theData, bool bLineBreaks=true)
opentxs::OTASCIIArmor::OTASCIIArmor ( const OTString strValue)

Definition at line 226 of file OTASCIIArmor.cpp.

227  : OTString(/*Don't pass here, since we're encoding.*/)
228 {
229  SetString(strValue);
230 }
EXPORT OTString()
Definition: OTString.cpp:786
EXPORT bool SetString(const OTString &theData, bool bLineBreaks=true)
opentxs::OTASCIIArmor::OTASCIIArmor ( const OTASCIIArmor strValue)

Definition at line 247 of file OTASCIIArmor.cpp.

248  : OTString(dynamic_cast<const OTString&>(strValue))
249 {
250 }
EXPORT OTString()
Definition: OTString.cpp:786
opentxs::OTASCIIArmor::OTASCIIArmor ( const OTEnvelope theEnvelope)

Definition at line 254 of file OTASCIIArmor.cpp.

255  : OTString()
256 {
257  theEnvelope.GetAsciiArmoredData(*this);
258 }
EXPORT OTString()
Definition: OTString.cpp:786
opentxs::OTASCIIArmor::~OTASCIIArmor ( )
virtual

Definition at line 1151 of file OTASCIIArmor.cpp.

1152 {
1153  // ~OTString called automatically, which calls Release().
1154 }

Member Function Documentation

bool opentxs::OTASCIIArmor::GetAndUnpackData ( OTData theData,
bool  bLineBreaks = true 
) const

Definition at line 633 of file OTASCIIArmor.cpp.

635 {
636  size_t outSize = 0;
637  uint8_t* pData = nullptr;
638 
639  theData.Release();
640 
641  if (GetLength() < 1) return true;
642 
643  pData = OTCrypto::It()->Base64Decode(Get(), &outSize, bLineBreaks);
644  // pData = OT_base64_decode(Get(), &outSize, (bLineBreaks ? 1 : 0));
645 
646  if (pData) {
647 
648  OTDB::OTPacker* pPacker =
649  OTASCIIArmor::GetPacker(); // No need to check for failure, since
650  // this already ASSERTS. No need to
651  // cleanup either.
652 
653  std::unique_ptr<OTDB::PackedBuffer> pBuffer(pPacker->CreateBuffer());
654  OT_ASSERT(nullptr != pBuffer);
655 
656  pBuffer->SetData(static_cast<const uint8_t*>(pData), outSize);
657  delete[] pData;
658  pData = nullptr;
659 
660  std::unique_ptr<OTDB::Blob> pBlob(dynamic_cast<OTDB::Blob*>(
662  OT_ASSERT(nullptr != pBlob);
663 
664  bool bUnpacked = pPacker->Unpack(*pBuffer, *pBlob);
665 
666  if (!bUnpacked) {
667  otErr
668  << "Failed unpacking data in OTASCIIArmor::GetAndUnpackData.\n";
669  delete[] pData;
670  pData = nullptr;
671  return false;
672  }
673 
674  theData.Assign(pBlob->m_memBuffer.data(),
675  static_cast<uint32_t>(pBlob->m_memBuffer.size()));
676  delete[] pData;
677  pData = nullptr;
678  return true;
679  }
680  else {
681  otErr << "Error while base64_decoding in "
682  "OTASCIIArmor::GetAndUnpackData.\n";
683  return false;
684  }
685 }
static EXPORT OTCrypto * It()
Definition: OTCrypto.cpp:630
EXPORT Storable * CreateObject(StoredObjectType eType)
Definition: OTStorage.cpp:530
EXPORT uint32_t GetLength() const
Definition: OTString.cpp:1040
#define OT_ASSERT(x)
Definition: Assert.hpp:150
static OTDB::OTPacker * GetPacker()
EXPORT const char * Get() const
Definition: OTString.cpp:1045
OTLOG_IMPORT OTLogStream otErr
virtual bool Base64Decode(const OTString &strInput, OTData &theOutput, bool bLineBreaks=true) const
Definition: OTCrypto.cpp:740
bool opentxs::OTASCIIArmor::GetAndUnpackString ( OTString strData,
bool  bLineBreaks = true 
) const

if we pack, compress, encode on the way in, that means, therefore, we need to decode, uncompress, then unpack on our way out. Right?

This function will base64-DECODE the string contents, then uncompress them using zlib, and then unpack the result using whatever is the default packer (MsgPack, Protobuf, etc).

I originally added compression because message sizes were too big. Now I'm adding packing, to solve any issues of binary compatibility across various platforms.

Definition at line 398 of file OTASCIIArmor.cpp.

400 {
401  size_t outSize = 0;
402  uint8_t* pData = nullptr;
403 
404  strData.Release();
405 
406  if (GetLength() < 1) {
407  return true;
408  }
409 
410  pData = OTCrypto::It()->Base64Decode(Get(), &outSize, bLineBreaks);
411  // pData = OT_base64_decode(Get(), &outSize, (bLineBreaks ? 1 : 0));
412 
413  if (pData) {
414 
415  std::string str_decoded(pData, pData + outSize);
416 
417  delete[] pData;
418  pData = nullptr;
419 
420  std::string str_uncompressed = "";
421  try {
422  str_uncompressed = decompress_string(str_decoded);
423  }
424  catch (const std::runtime_error&) {
425  otErr << "Failed decompressing string in "
426  "OTASCIIArmor::GetAndUnpackString.\n";
427  return false;
428  }
429 
430  // PUT THE PACKED BUFFER HERE, AND UNPACK INTO strData
431 
432  OTDB::OTPacker* pPacker =
433  OTASCIIArmor::GetPacker(); // No need to check for failure, since
434  // this already ASSERTS. No need to
435  // cleanup either.
436 
437  std::unique_ptr<OTDB::PackedBuffer> pBuffer(pPacker->CreateBuffer());
438  OT_ASSERT(nullptr != pBuffer);
439 
440  pBuffer->SetData(
441  reinterpret_cast<const uint8_t*>(str_uncompressed.data()),
442  str_uncompressed.size());
443 
444  std::unique_ptr<OTDB::OTDBString> pOTDBString(
445  dynamic_cast<OTDB::OTDBString*>(
447  OT_ASSERT(nullptr != pOTDBString);
448 
449  bool bUnpacked = pPacker->Unpack(*pBuffer, *pOTDBString);
450 
451  if (!bUnpacked) {
452  otErr << "Failed unpacking string in "
453  "OTASCIIArmor::GetAndUnpackString.\n";
454 
455  return false;
456  }
457 
458  // This enforces the null termination. (using the 2nd parameter as
459  // nEnforcedMaxLength)
460  strData.Set(pOTDBString->m_string.c_str(),
461  static_cast<uint32_t>(pOTDBString->m_string.length()));
462 
463  return true;
464  }
465  else {
466  otErr << "OTASCIIArmor::GetAndUnpackString: nullptr pData while "
467  "base64-decoding pData.\n";
468  return false;
469  }
470 }
static EXPORT OTCrypto * It()
Definition: OTCrypto.cpp:630
EXPORT Storable * CreateObject(StoredObjectType eType)
Definition: OTStorage.cpp:530
EXPORT uint32_t GetLength() const
Definition: OTString.cpp:1040
std::string decompress_string(const std::string &str)
#define OT_ASSERT(x)
Definition: Assert.hpp:150
static OTDB::OTPacker * GetPacker()
EXPORT const char * Get() const
Definition: OTString.cpp:1045
OTLOG_IMPORT OTLogStream otErr
virtual bool Base64Decode(const OTString &strInput, OTData &theOutput, bool bLineBreaks=true) const
Definition: OTCrypto.cpp:740
bool opentxs::OTASCIIArmor::GetAndUnpackStringMap ( std::map< std::string, std::string > &  the_map,
bool  bLineBreaks = true 
) const

Definition at line 496 of file OTASCIIArmor.cpp.

498 {
499  size_t outSize = 0;
500  uint8_t* pData = nullptr;
501 
502  the_map.clear();
503 
504  if (GetLength() < 1) return true;
505 
506  pData = OTCrypto::It()->Base64Decode(Get(), &outSize, bLineBreaks);
507 
508  if (pData) {
509 
510  OTDB::OTPacker* pPacker =
511  OTASCIIArmor::GetPacker(); // No need to check for failure, since
512  // this already ASSERTS. No need to
513  // cleanup either.
514 
515  std::unique_ptr<OTDB::PackedBuffer> pBuffer(
516  pPacker->CreateBuffer()); // Need to clean this up.
517  OT_ASSERT(nullptr != pBuffer);
518 
519  pBuffer->SetData(static_cast<const uint8_t*>(pData), outSize);
520  delete[] pData;
521  pData = nullptr;
522 
523  std::unique_ptr<OTDB::StringMap> pStringMap(
524  dynamic_cast<OTDB::StringMap*>(
526  OT_ASSERT(nullptr != pStringMap);
527 
528  bool bUnpacked = pPacker->Unpack(*pBuffer, *pStringMap);
529 
530  if (!bUnpacked) {
531  otErr << "Failed unpacking data in "
532  "OTASCIIArmor::GetAndUnpackStringMap.\n";
533  delete[] pData;
534  pData = nullptr;
535  return false;
536  }
537 
538  the_map = pStringMap->the_map;
539 
540  delete[] pData;
541  pData = nullptr;
542  return true;
543  }
544  else {
545  otErr << "Error while base64_decoding in "
546  "OTASCIIArmor::GetAndUnpackStringMap.\n";
547  return false;
548  }
549 }
static EXPORT OTCrypto * It()
Definition: OTCrypto.cpp:630
EXPORT Storable * CreateObject(StoredObjectType eType)
Definition: OTStorage.cpp:530
EXPORT uint32_t GetLength() const
Definition: OTString.cpp:1040
#define OT_ASSERT(x)
Definition: Assert.hpp:150
static OTDB::OTPacker * GetPacker()
EXPORT const char * Get() const
Definition: OTString.cpp:1045
OTLOG_IMPORT OTLogStream otErr
virtual bool Base64Decode(const OTString &strInput, OTData &theOutput, bool bLineBreaks=true) const
Definition: OTCrypto.cpp:740
bool opentxs::OTASCIIArmor::GetData ( OTData theData,
bool  bLineBreaks = true 
) const

Definition at line 623 of file OTASCIIArmor.cpp.

625 {
626  return GetAndUnpackData(theData, bLineBreaks);
627 }
EXPORT bool GetAndUnpackData(OTData &theData, bool bLineBreaks=true) const
OTDB::OTPacker * opentxs::OTASCIIArmor::GetPacker ( )
static

Definition at line 159 of file OTASCIIArmor.cpp.

160 {
161  if (nullptr ==
162  s_pPacker) { // WARNING: Do not change OTDB_DEFAULT_PACKER below
163  // unless you also change SetAndPackData() since it
164  // ASSUMES this.
165  s_pPacker.reset(OTDB::OTPacker::Create(
166  OTDB_DEFAULT_PACKER)); // Protobuf is the only one that works on all
167  // platforms right now.
168  OT_ASSERT(nullptr != s_pPacker);
169  }
170 
171  return s_pPacker.get();
172 }
#define OTDB_DEFAULT_PACKER
Definition: OTStorage.hpp:167
#define OT_ASSERT(x)
Definition: Assert.hpp:150
static OTPacker * Create(PackType ePackType)
Definition: OTStorage.cpp:889
bool opentxs::OTASCIIArmor::GetString ( OTString theData,
bool  bLineBreaks = true 
) const

Definition at line 484 of file OTASCIIArmor.cpp.

486 {
487  return GetAndUnpackString(strData, bLineBreaks);
488 }
EXPORT bool GetAndUnpackString(OTString &theData, bool bLineBreaks=true) const
bool opentxs::OTASCIIArmor::GetStringMap ( std::map< std::string, std::string > &  the_map,
bool  bLineBreaks = true 
) const

Definition at line 490 of file OTASCIIArmor.cpp.

492 {
493  return GetAndUnpackStringMap(the_map, bLineBreaks);
494 }
EXPORT bool GetAndUnpackStringMap(std::map< std::string, std::string > &the_map, bool bLineBreaks=true) const
bool opentxs::OTASCIIArmor::LoadFrom_ifstream ( std::ifstream &  fin)

Definition at line 894 of file OTASCIIArmor.cpp.

895 {
896  std::stringstream buffer;
897  buffer << fin.rdbuf();
898 
899  std::string contents(buffer.str());
900 
901  OTString theString;
902  theString.Set(contents.c_str());
903 
904  return LoadFromString(theString);
905 }
EXPORT OTString()
Definition: OTString.cpp:786
static EXPORT bool LoadFromString(OTASCIIArmor &ascArmor, const OTString &strInput, std::string str_bookend="-----BEGIN")
bool opentxs::OTASCIIArmor::LoadFromExactPath ( const std::string &  filename)

Definition at line 879 of file OTASCIIArmor.cpp.

880 {
881  std::ifstream fin(filename.c_str(), std::ios::binary);
882 
883  if (!fin.is_open()) {
884  otWarn << "OTASCIIArmor::LoadFromExactPath: Failed opening file: "
885  << filename << "\n";
886  return false;
887  }
888 
889  return LoadFrom_ifstream(fin);
890 }
EXPORT bool LoadFrom_ifstream(std::ifstream &fin)
OTLOG_IMPORT OTLogStream otWarn
bool opentxs::OTASCIIArmor::LoadFromFile ( const OTString foldername,
const OTString filename 
)

Definition at line 854 of file OTASCIIArmor.cpp.

856 {
857  OT_ASSERT(foldername.Exists());
858  OT_ASSERT(filename.Exists());
859 
860  if (!OTDB::Exists(foldername.Get(), filename.Get())) {
861  otErr << "OTASCIIArmor::LoadFromFile: File does not exist: "
862  << foldername << "" << OTLog::PathSeparator() << "" << filename
863  << "\n";
864  return false;
865  }
866 
867  OTString strFileContents(OTDB::QueryPlainString(
868  foldername.Get(), filename.Get())); // <=== LOADING FROM DATA STORE.
869 
870  if (strFileContents.GetLength() < 2) {
871  otErr << "OTASCIIArmor::LoadFromFile: Error reading file: "
872  << foldername << OTLog::PathSeparator() << filename << "\n";
873  return false;
874  }
875 
876  return LoadFromString(strFileContents);
877 }
static EXPORT const char * PathSeparator()
Definition: OTLog.cpp:408
EXPORT OTString()
Definition: OTString.cpp:786
static EXPORT bool LoadFromString(OTASCIIArmor &ascArmor, const OTString &strInput, std::string str_bookend="-----BEGIN")
EXPORT std::string QueryPlainString(std::string strFolder, std::string oneStr="", std::string twoStr="", std::string threeStr="")
Definition: OTStorage.cpp:728
#define OT_ASSERT(x)
Definition: Assert.hpp:150
OTLOG_IMPORT OTLogStream otErr
EXPORT bool Exists(std::string strFolder, std::string oneStr="", std::string twoStr="", std::string threeStr="")
Definition: OTStorage.cpp:584
bool opentxs::OTASCIIArmor::LoadFromString ( OTASCIIArmor ascArmor,
const OTString strInput,
std::string  str_bookend = "-----BEGIN" 
)
static

Definition at line 189 of file OTASCIIArmor.cpp.

192 {
193 
194  if (strInput.Contains(str_bookend)) // YES there are bookends around this.
195  {
196  const std::string str_escaped("- " + str_bookend);
197 
198  const bool bEscaped = strInput.Contains(str_escaped);
199 
200  OTString strLoadFrom(strInput);
201 
202  if (!ascArmor.LoadFromString(strLoadFrom, bEscaped)) // removes the
203  // bookends so we
204  // have JUST the
205  // coded part.
206  {
207  // otErr << "%s: Failure loading string into OTASCIIArmor
208  // object:\n\n%s\n\n",
209  // __FUNCTION__, strInput.Get());
210  return false;
211  }
212  }
213  else
214  ascArmor.Set(strInput.Get());
215 
216  return true;
217 }
EXPORT OTString()
Definition: OTString.cpp:786
bool opentxs::OTASCIIArmor::LoadFromString ( OTString theStr,
bool  bEscaped = false,
const std::string  str_override = "-----BEGIN" 
)

Definition at line 1025 of file OTASCIIArmor.cpp.

1031 {
1032  // Should never be 0 size, as default is "-----BEGIN"
1033  // But if you want to load a private key, try "-----BEGIN ENCRYPTED PRIVATE"
1034  // instead.
1035  // *smile*
1036  const std::string str_end_line =
1037  "-----END"; // Someday maybe allow parameterized option for this.
1038 
1039  const int32_t nBufSize = 2100; // todo: hardcoding
1040  const int32_t nBufSize2 = 2048; // todo: hardcoding
1041 
1042  char buffer1[2100]; // todo: hardcoding
1043 
1044  std::fill(&buffer1[0], &buffer1[(nBufSize - 1)], 0); // Initializing to 0.
1045 
1046  bool bContentMode = false; // "Currently IN content mode."
1047  bool bHaveEnteredContentMode =
1048  false; // "Have NOT YET entered content mode."
1049 
1050  // Clear out whatever string might have been in there before.
1051  Release();
1052 
1053  // Load up the string from theStr,
1054  // (bookended by "-----BEGIN ... -----" and "END-----" messages)
1055  bool bIsEOF = false;
1056  theStr.reset(); // So we can call theStr.sgets(). Making sure position is at
1057  // start of string.
1058 
1059  do {
1060  bIsEOF = !(theStr.sgets(buffer1, nBufSize2)); // 2048
1061 
1062  std::string line = buffer1;
1063  const char* pConstBuf = line.c_str();
1064  char* pBuf = (char*)pConstBuf;
1065 
1066  // It's not a blank line.
1067  if (line.length() < 2) {
1068  continue;
1069  }
1070 
1071  // if we're on a dashed line...
1072  else if (line.at(0) == '-' && line.at(2) == '-' && line.at(3) == '-' &&
1073  (bEscaped ? (line.at(1) == ' ') : (line.at(1) == '-'))) {
1074  // If I just hit a dash, that means there are only two options:
1075 
1076  // a. I have not yet entered content mode, and potentially just now
1077  // entering it for the first time.
1078  if (!bHaveEnteredContentMode) {
1079  // str_override defaults to: "-----BEGIN" (If you want to load
1080  // a private key instead,
1081  // Try passing "-----BEGIN ENCRYPTED PRIVATE" instead of going
1082  // with the default.)
1083  //
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) == '-'))) {
1088  // otErr << "Reading ascii-armored
1089  // contents...";
1090  bHaveEnteredContentMode = true;
1091  bContentMode = true;
1092  continue;
1093  }
1094  else {
1095  continue;
1096  }
1097  }
1098 
1099  // b. I am now LEAVING content mode!
1100  else if (bContentMode &&
1101  // str_end_line is "-----END"
1102  (line.find(str_end_line) != std::string::npos)) {
1103  // otErr << "Finished reading ascii-armored
1104  // contents.\n";
1105  // otErr << "Finished reading ascii-armored
1106  // contents:\n%s(END DATA)\n", Get());
1107  bContentMode = false;
1108  continue;
1109  }
1110  }
1111 
1112  // Else we're on a normal line, not a dashed line.
1113  else {
1114  if (bHaveEnteredContentMode && bContentMode) {
1115  if (line.compare(0, 8, "Version:") == 0) {
1116  // otErr << "Skipping version line...\n";
1117  continue;
1118  }
1119  if (line.compare(0, 8, "Comment:") == 0) {
1120  // otErr << "Skipping comment line...\n";
1121  continue;
1122  }
1123  }
1124  }
1125 
1126  // Here we save the line to member variables, if appropriate
1127  if (bContentMode) {
1128  Concatenate("%s\n", pBuf);
1129  }
1130  } while (!bIsEOF && (bContentMode || !bHaveEnteredContentMode));
1131 
1132  // reset the string position back to 0
1133  theStr.reset();
1134 
1135  if (!bHaveEnteredContentMode) {
1136  otErr << "Error in OTASCIIArmor::LoadFromString: EOF before "
1137  "ascii-armored "
1138  "content found, in:\n\n" << theStr << "\n\n";
1139  return false;
1140  }
1141  else if (bContentMode) {
1142  otErr
1143  << "Error in OTASCIIArmor::LoadFromString: EOF while still reading "
1144  "content, in:\n\n" << theStr << "\n\n";
1145  return false;
1146  }
1147  else
1148  return true;
1149 }
EXPORT void Concatenate(const char *arg,...)
Definition: OTString.cpp:1334
OTLOG_IMPORT OTLogStream otErr
virtual EXPORT void Release()
Definition: OTString.cpp:765
OTASCIIArmor & opentxs::OTASCIIArmor::operator= ( const char *  szValue)

Definition at line 267 of file OTASCIIArmor.cpp.

268 {
269  Set(szValue);
270  return *this;
271 }
EXPORT void Set(const char *data, uint32_t enforcedMaxLength=0)
Definition: OTString.cpp:1055
OTASCIIArmor & opentxs::OTASCIIArmor::operator= ( const OTData theValue)

Definition at line 283 of file OTASCIIArmor.cpp.

284 {
285  SetData(theValue);
286  return *this;
287 }
EXPORT bool SetData(const OTData &theData, bool bLineBreaks=true)
OTASCIIArmor & opentxs::OTASCIIArmor::operator= ( const OTString strValue)

Definition at line 274 of file OTASCIIArmor.cpp.

275 {
276  if ((&strValue) != (&(dynamic_cast<const OTString&>(*this)))) {
277  SetString(strValue);
278  }
279  return *this;
280 }
EXPORT bool SetString(const OTString &theData, bool bLineBreaks=true)
OTASCIIArmor & opentxs::OTASCIIArmor::operator= ( const OTASCIIArmor strValue)

Definition at line 290 of file OTASCIIArmor.cpp.

291 {
292  if ((&strValue) != this) // prevent self-assignment
293  {
294  OTString::operator=(dynamic_cast<const OTString&>(strValue));
295  }
296  return *this;
297 }
EXPORT OTString & operator=(OTString rhs)
Definition: OTString.cpp:1011
bool opentxs::OTASCIIArmor::SaveTo_ofstream ( std::ofstream &  fout)

Definition at line 920 of file OTASCIIArmor.cpp.

921 {
922  OTString strOutput;
923  std::string str_type("DATA"); // -----BEGIN OT ARMORED DATA-----
924 
925  if (WriteArmoredString(strOutput, str_type) && strOutput.Exists()) {
926  // WRITE IT TO THE FILE
927  //
928  fout << strOutput;
929 
930  if (fout.fail()) {
931  otErr << __FUNCTION__ << ": Failed saving to file.\n Contents:\n\n"
932  << strOutput << "\n\n";
933  return false;
934  }
935 
936  return true;
937  }
938 
939  return false;
940 }
EXPORT OTString()
Definition: OTString.cpp:786
EXPORT bool WriteArmoredString(OTString &strOutput, const std::string str_type, bool bEscaped=false) const
OTLOG_IMPORT OTLogStream otErr
bool opentxs::OTASCIIArmor::SaveToExactPath ( const std::string &  filename)

Definition at line 907 of file OTASCIIArmor.cpp.

908 {
909  std::ofstream fout(filename.c_str(), std::ios::out | std::ios::binary);
910 
911  if (!fout.is_open()) {
912  otWarn << "OTASCIIArmor::SaveToExactPath: Failed opening file: "
913  << filename << "\n";
914  return false;
915  }
916 
917  return SaveTo_ofstream(fout);
918 }
EXPORT bool SaveTo_ofstream(std::ofstream &fout)
OTLOG_IMPORT OTLogStream otWarn
bool opentxs::OTASCIIArmor::SetAndPackData ( const OTData theData,
bool  bLineBreaks = true 
)

Definition at line 698 of file OTASCIIArmor.cpp.

699 {
700  char* pString = nullptr;
701 
702  Release();
703 
704  if (theData.GetSize() < 1) return true;
705 
706  OTDB::OTPacker* pPacker =
707  OTASCIIArmor::GetPacker(); // No need to check for failure, since this
708  // already ASSERTS. No need to cleanup
709  // either.
710 
711  // Here I use the default storage context to create the object (the blob.)
712  // I also originally created OTASCIIArmor::GetPacker() using
713  // OTDB_DEFAULT_PACKER,
714  // so I know everything is compatible.
715  //
716  std::unique_ptr<OTDB::Blob> pBlob(
717  dynamic_cast<OTDB::Blob*>(OTDB::CreateObject(OTDB::STORED_OBJ_BLOB)));
718 
719  OT_ASSERT(nullptr !=
720  pBlob); // Beyond this point, responsible to delete pBlob.
721 
722  pBlob->m_memBuffer.assign(
723  static_cast<const uint8_t*>(theData.GetPointer()),
724  static_cast<const uint8_t*>(theData.GetPointer()) + theData.GetSize());
725 
726  std::unique_ptr<OTDB::PackedBuffer> pBuffer(pPacker->Pack(
727  *pBlob)); // Now we PACK our data before compressing/encoding it.
728 
729  if (nullptr == pBuffer) {
730  otErr << "Failed packing data in OTASCIIArmor::SetAndPackData. \n";
731  return false;
732  }
733 
734  const uint8_t* pUint = static_cast<const uint8_t*>(pBuffer->GetData());
735  const size_t theSize = pBuffer->GetSize();
736 
737  if (nullptr != pUint)
738  pString = OTCrypto::It()->Base64Encode(
739  pUint, static_cast<int32_t>(theSize), bLineBreaks);
740  // pString = OT_base64_encode(pUint, static_cast<int32_t> (theSize),
741  // (bLineBreaks ? 1 : 0));
742  else {
743  otErr
744  << "Error while base64_encoding in OTASCIIArmor::SetAndPackData.\n";
745  return false;
746  }
747 
748  if (nullptr != pString) {
749  Set(pString);
750  delete[] pString;
751  pString = nullptr;
752  return true;
753  }
754  else {
755  otErr
756  << "Error while base64_encoding in OTASCIIArmor::SetAndPackData.\n";
757  return false;
758  }
759 }
static EXPORT OTCrypto * It()
Definition: OTCrypto.cpp:630
EXPORT Storable * CreateObject(StoredObjectType eType)
Definition: OTStorage.cpp:530
EXPORT void Set(const char *data, uint32_t enforcedMaxLength=0)
Definition: OTString.cpp:1055
#define OT_ASSERT(x)
Definition: Assert.hpp:150
virtual bool Base64Encode(const OTData &theInput, OTString &strOutput, bool bLineBreaks=true) const
Definition: OTCrypto.cpp:710
static OTDB::OTPacker * GetPacker()
OTLOG_IMPORT OTLogStream otErr
virtual EXPORT void Release()
Definition: OTString.cpp:765
bool opentxs::OTASCIIArmor::SetAndPackString ( const OTString strData,
bool  bLineBreaks = true 
)

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 int64_t, so I started compressing them. Then they were not binary compatible across various platforms, so I added the packing.

Definition at line 772 of file OTASCIIArmor.cpp.

774 {
775  Release();
776 
777  if (strData.GetLength() < 1) return true;
778 
779  OTDB::OTPacker* pPacker =
780  OTASCIIArmor::GetPacker(); // No need to check for failure, since this
781  // already ASSERTS. No need to cleanup
782  // either.
783 
784  // Here I use the default storage context to create the object (the blob.)
785  // I also originally created OTASCIIArmor::GetPacker() using
786  // OTDB_DEFAULT_PACKER,
787  // so I know everything is compatible.
788  //
789  std::unique_ptr<OTDB::OTDBString> pOTDBString(
790  dynamic_cast<OTDB::OTDBString*>(
792 
793  OT_ASSERT(nullptr != pOTDBString);
794 
795  const uint32_t theStringSize32 = strData.GetLength();
796  const size_t theStringSize =
797  theStringSize32; // might need a cast here. // todo make sure this will
798  // handle sizes as big as I need.
799 
800  pOTDBString->m_string.assign(strData.Get(), // const char *
801  theStringSize);
802 
803  std::unique_ptr<OTDB::PackedBuffer> pBuffer(
804  pPacker->Pack(*pOTDBString)); // Now we PACK our string before
805  // compressing/encoding it.
806 
807  if (nullptr == pBuffer) {
808  otErr << "Failed packing string in OTASCIIArmor::SetAndPackString. \n";
809  return false;
810  }
811 
812  std::string str_packed(reinterpret_cast<const char*>(pBuffer->GetData()),
813  pBuffer->GetSize());
814 
815  std::string str_compressed = compress_string(str_packed);
816 
817  // Success
818  if (str_compressed.size()) {
819  // Now let's base-64 encode it...
820  // TODO: remove static cast, add check for longer than 'int32_t' length?
821  // (da2ce7)
822  char* pString = OTCrypto::It()->Base64Encode(
823  (const uint8_t*)(str_compressed.data()),
824  static_cast<int32_t>(str_compressed.size()), bLineBreaks);
825 
826  if (pString) {
827  Set(pString);
828  delete[] pString;
829  pString = nullptr;
830  return true;
831  }
832  else {
833  otErr << "OTASCIIArmor::" << __FUNCTION__ << ": pString nullptr.\n";
834  }
835  }
836  else {
837  otErr << "OTASCIIArmor::" << __FUNCTION__ << ": nDestLen 0.\n";
838  }
839 
840  return false;
841 }
static EXPORT OTCrypto * It()
Definition: OTCrypto.cpp:630
EXPORT Storable * CreateObject(StoredObjectType eType)
Definition: OTStorage.cpp:530
std::string compress_string(const std::string &str, int32_t compressionlevel=Z_BEST_COMPRESSION)
EXPORT void Set(const char *data, uint32_t enforcedMaxLength=0)
Definition: OTString.cpp:1055
#define OT_ASSERT(x)
Definition: Assert.hpp:150
virtual bool Base64Encode(const OTData &theInput, OTString &strOutput, bool bLineBreaks=true) const
Definition: OTCrypto.cpp:710
static OTDB::OTPacker * GetPacker()
OTLOG_IMPORT OTLogStream otErr
virtual EXPORT void Release()
Definition: OTString.cpp:765
bool opentxs::OTASCIIArmor::SetAndPackStringMap ( const std::map< std::string, std::string > &  the_map,
bool  bLineBreaks = true 
)

Definition at line 557 of file OTASCIIArmor.cpp.

559 {
560  char* pString = nullptr;
561 
562  Release();
563 
564  if (the_map.empty()) return true;
565 
566  OTDB::OTPacker* pPacker =
567  OTASCIIArmor::GetPacker(); // No need to check for failure, since this
568  // already ASSERTS. No need to cleanup
569  // either.
570 
571  // Here I use the default storage context to create the object (the string
572  // map.)
573  // I also originally created OTASCIIArmor::GetPacker() using
574  // OTDB_DEFAULT_PACKER,
575  // so I know everything is compatible.
576  //
577  std::unique_ptr<OTDB::StringMap> pStringMap(dynamic_cast<OTDB::StringMap*>(
579 
580  OT_ASSERT(nullptr !=
581  pStringMap); // Beyond this point, responsible to delete
582  // pStringMap.
583 
584  pStringMap->the_map = the_map;
585 
586  std::unique_ptr<OTDB::PackedBuffer> pBuffer(pPacker->Pack(
587  *pStringMap)); // Now we PACK our data before compressing/encoding it.
588 
589  if (nullptr == pBuffer) {
590  otErr << "Failed packing data in OTASCIIArmor::SetAndPackStringMap. \n";
591  return false;
592  }
593 
594  const uint8_t* pUint = static_cast<const uint8_t*>(pBuffer->GetData());
595  const size_t theSize = pBuffer->GetSize();
596 
597  if (nullptr != pUint)
598  pString = OTCrypto::It()->Base64Encode(
599  pUint, static_cast<int32_t>(theSize), bLineBreaks);
600  // pString = OT_base64_encode(pUint, static_cast<int32_t> (theSize),
601  // (bLineBreaks ? 1 : 0));
602  else {
603  otErr << "Error while base64_encoding in "
604  "OTASCIIArmor::SetAndPackStringMap.\n";
605  return false;
606  }
607 
608  if (nullptr != pString) {
609  Set(pString);
610  delete[] pString;
611  pString = nullptr;
612  return true;
613  }
614  else {
615  otErr << "Error while base64_encoding in "
616  "OTASCIIArmor::SetAndPackStringMap.\n";
617  return false;
618  }
619 }
static EXPORT OTCrypto * It()
Definition: OTCrypto.cpp:630
EXPORT Storable * CreateObject(StoredObjectType eType)
Definition: OTStorage.cpp:530
EXPORT void Set(const char *data, uint32_t enforcedMaxLength=0)
Definition: OTString.cpp:1055
#define OT_ASSERT(x)
Definition: Assert.hpp:150
virtual bool Base64Encode(const OTData &theInput, OTString &strOutput, bool bLineBreaks=true) const
Definition: OTCrypto.cpp:710
static OTDB::OTPacker * GetPacker()
OTLOG_IMPORT OTLogStream otErr
virtual EXPORT void Release()
Definition: OTString.cpp:765
bool opentxs::OTASCIIArmor::SetData ( const OTData theData,
bool  bLineBreaks = true 
)

Definition at line 689 of file OTASCIIArmor.cpp.

690 {
691  return SetAndPackData(theData, bLineBreaks);
692 }
EXPORT bool SetAndPackData(const OTData &theData, bool bLineBreaks=true)
bool opentxs::OTASCIIArmor::SetString ( const OTString theData,
bool  bLineBreaks = true 
)

Definition at line 846 of file OTASCIIArmor.cpp.

847 {
848 
849  return SetAndPackString(strData, bLineBreaks);
850 }
EXPORT bool SetAndPackString(const OTString &theData, bool bLineBreaks=true)
bool opentxs::OTASCIIArmor::SetStringMap ( const std::map< std::string, std::string > &  the_map,
bool  bLineBreaks = true 
)

Definition at line 551 of file OTASCIIArmor.cpp.

553 {
554  return SetAndPackStringMap(the_map, bLineBreaks);
555 }
EXPORT bool SetAndPackStringMap(const std::map< std::string, std::string > &the_map, bool bLineBreaks=true)
bool opentxs::OTASCIIArmor::WriteArmoredFile ( const OTString foldername,
const OTString filename,
const std::string  str_type,
bool  bEscaped = false 
) const

Definition at line 945 of file OTASCIIArmor.cpp.

951 {
952  OT_ASSERT(foldername.Exists());
953  OT_ASSERT(filename.Exists());
954 
955  OTString strOutput;
956 
957  if (WriteArmoredString(strOutput, str_type, bEscaped) &&
958  strOutput.Exists()) {
959  // WRITE IT TO THE FILE
960  // StorePlainString will attempt to create all the folders leading up to
961  // the path
962  // for the output file.
963  //
964  bool bSaved = OTDB::StorePlainString(strOutput.Get(), foldername.Get(),
965  filename.Get());
966 
967  if (!bSaved) {
968  otErr << "OTASCIIArmor::WriteArmoredFile"
969  << ": Failed saving to file: %s%s%s\n\n Contents:\n\n"
970  << strOutput << "\n\n",
971  foldername.Get(), OTLog::PathSeparator(), filename.Get();
972  return false;
973  }
974 
975  return true;
976  }
977 
978  return false;
979 }
EXPORT bool StorePlainString(std::string strContents, std::string strFolder, std::string oneStr="", std::string twoStr="", std::string threeStr="")
Definition: OTStorage.cpp:698
static EXPORT const char * PathSeparator()
Definition: OTLog.cpp:408
EXPORT OTString()
Definition: OTString.cpp:786
EXPORT bool WriteArmoredString(OTString &strOutput, const std::string str_type, bool bEscaped=false) const
#define OT_ASSERT(x)
Definition: Assert.hpp:150
OTLOG_IMPORT OTLogStream otErr
bool opentxs::OTASCIIArmor::WriteArmoredString ( OTString strOutput,
const std::string  str_type,
bool  bEscaped = false 
) const

Definition at line 984 of file OTASCIIArmor.cpp.

990 {
991  const char* szEscape = "- ";
992 
993  OTString strTemp;
994  strTemp.Format(
995  "%s%s %s-----\n" // "%s-----BEGIN OT ARMORED %s-----\n"
996  "Version: Open Transactions %s\n"
997  "Comment: "
998  "http://github.com/FellowTraveler/Open-Transactions/wiki\n\n" // todo
999  // hardcoding.
1000  "%s" // Should already have a newline at the bottom.
1001  "%s%s %s-----\n\n", // "%s-----END OT ARMORED %s-----\n"
1002  bEscaped ? szEscape : "",
1003  OT_BEGIN_ARMORED, str_type.c_str(), // "%s%s %s-----\n"
1004  OTLog::Version(), // "Version: Open Transactions %s\n"
1005  /* No variable */ // "Comment:
1006  // http://github.com/FellowTraveler/Open-Transactions/wiki\n\n",
1007  Get(), // "%s" <==== CONTENTS OF THIS OBJECT BEING
1008  // WRITTEN...
1009  bEscaped ? szEscape : "", OT_END_ARMORED,
1010  str_type.c_str()); // "%s%s %s-----\n"
1011 
1012  strOutput.Concatenate("%s", strTemp.Get());
1013 
1014  return true;
1015 }
const char * OT_END_ARMORED
EXPORT OTString()
Definition: OTString.cpp:786
static EXPORT const char * Version()
Definition: OTLog.cpp:399
EXPORT const char * Get() const
Definition: OTString.cpp:1045
const char * OT_BEGIN_ARMORED

The documentation for this class was generated from the following files: