/* Set @string to the value of the given @node, however, with strings * compressed and entity references 'expanded'. */ static void set_enhanced_dom_node_value(struct dom_string *string, struct dom_node *node) { struct dom_string *value; assert(node); memset(string, 0, sizeof(*string)); switch (node->type) { case DOM_NODE_ENTITY_REFERENCE: /* FIXME: Set to the entity value. */ string->string = null_or_stracpy(string->string); break; default: value = get_dom_node_value(node); if (!value) { set_dom_string(string, NULL, 0); return; } string->string = compress_string(value->string, value->length); } string->length = string->string ? strlen((const char *)string->string) : 0; }
// Compress and Base64-encode bool OTASCIIArmor::SetString(const String& strData, bool bLineBreaks) //=true { Release(); if (strData.GetLength() < 1) return true; std::string stdstring = std::string(strData.Get()); std::string str_compressed = compress_string(stdstring); // "Success" if (str_compressed.size() == 0) { otErr << "OTASCIIArmor::" << __FUNCTION__ << ": compression fail 0.\n"; return false; } char* pString = App::Me().Crypto().Util().Base64Encode( reinterpret_cast<const uint8_t*>((str_compressed.data())), static_cast<int32_t>(str_compressed.size()), bLineBreaks); if (!pString) { otErr << "OTASCIIArmor::" << __FUNCTION__ << ": Base64Encode fail.\n"; return false; } Set(pString); delete[] pString; return true; }
/* * serializeNode - * This is used on the query dispatcher to serialize Plan and Query Trees for * dispatching to qExecs. * The returned string is palloc'ed in the current memory context. */ char * serializeNode(Node *node, int *size, int *uncompressed_size_out) { char *pszNode; char *sNode; int uncompressed_size; Assert(node != NULL); Assert(size != NULL); START_MEMORY_ACCOUNT(MemoryAccounting_CreateAccount(0, MEMORY_OWNER_TYPE_Serializer)); { pszNode = nodeToBinaryStringFast(node, &uncompressed_size); Assert(pszNode != NULL); if (NULL != uncompressed_size_out) { *uncompressed_size_out = uncompressed_size; } sNode = compress_string(pszNode, uncompressed_size, size); pfree(pszNode); if (DEBUG5 >= log_min_messages) { Node * newnode = NULL; PG_TRY(); { newnode = deserializeNode(sNode, *size); } PG_CATCH(); { elog_node_display(DEBUG5, "Before serialization", node, true); PG_RE_THROW(); } PG_END_TRY(); /* Some plans guarantee these differences (see serialization * of plan nodes -- they avoid sending QD-only info out) */ if (strcmp(nodeToString(node), nodeToString(newnode)) != 0) { elog_node_display(DEBUG5, "Before serialization", node, true); elog_node_display(DEBUG5, "After deserialization", newnode, true); } } } END_MEMORY_ACCOUNT(); return sNode; }
void transmit(const char* transmission, const char* jsonStr, const char* kind, const char* key, const char* syshost, char* resultFn) { char * cmdline; char * p_dbg = getenv("XALT_TRACING"); int xalt_tracing = (p_dbg && (strcmp(p_dbg,"yes") == 0 || strcmp(p_dbg,"run") == 0 )); if ((strcasecmp(transmission,"file") != 0 ) && (strcasecmp(transmission,"syslog") != 0 ) && (strcasecmp(transmission,"none") != 0 ) && (strcasecmp(transmission,"syslogv1") != 0 )) transmission = "file"; if (strcasecmp(transmission, "file") == 0 || strcasecmp(transmission, "file_separate_dirs") == 0 ) { if (resultFn == NULL) { DEBUG0(stderr," resultFn is NULL, $HOME might be undefined -> No XALT output\n"); return; } int err = mkpath(resultFn, 0700); if (err) { if (xalt_tracing) { perror("Error: "); fprintf(stderr," unable to mkpath(%s) -> No XALT output\n", resultFn); } return; } FILE* fp = fopen(resultFn,"w"); if (fp == NULL && xalt_tracing) fprintf(stderr," Unable to open: %s -> No XALT output\n", resultFn); else { fprintf(fp, "%s\n", jsonStr); fclose(fp); DEBUG2(stderr," Wrote json %s file : %s\n",kind, resultFn); } } else if (strcasecmp(transmission, "syslogv1") == 0) { int zslen; int b64len; char* zs = compress_string(jsonStr,&zslen); char* b64 = base64_encode(zs, zslen, &b64len); asprintf(&cmdline, "PATH=%s logger -t XALT_LOGGING_%s \"%s:%s\"\n",XALT_SYSTEM_PATH, syshost, kind, b64); system(cmdline); free(zs); free(b64); free(cmdline); } else if (strcasecmp(transmission, "syslog") == 0) { int sz; int zslen; char* zs = compress_string(jsonStr, &zslen); char* b64 = base64_encode(zs, zslen, &sz); int blkSz = (sz < syslog_msg_sz) ? sz : syslog_msg_sz; int nBlks = (sz - 1)/blkSz + 1; int istrt = 0; int iend = blkSz; int i; for (i = 0; i < nBlks; i++) { asprintf(&cmdline, "PATH=%s logger -t XALT_LOGGING_%s V:2 kind:%s idx:%d nb:%d syshost:%s key:%s value:%.*s\n", XALT_SYSTEM_PATH, syshost, kind, i, nBlks, syshost, key, iend-istrt, &b64[istrt]); system(cmdline); free(cmdline); istrt = iend; iend = istrt + blkSz; if (iend > sz) iend = sz; } free(b64); } }
/// 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. // 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. // -------------------------------------------------------- std::string str_packed(reinterpret_cast<const char *>(pBuffer->GetData()), pBuffer->GetSize()); std::string str_compressed = compress_string( str_packed ); // Success if (str_compressed.size()) { // Now let's base-64 encode it... // TODO: remove static cast, add check for longer than 'int32_t' length? (da2ce7) char * pString = OTCrypto::It()->Base64Encode((const uint8_t*)(str_compressed.data()), static_cast<int32_t>(str_compressed.size()), bLineBreaks); if (pString) { Set(pString); delete [] pString; pString=NULL; return true; } else { OTLog::vError("OTASCIIArmor::%s: pString NULL.\n", __FUNCTION__); } } else { OTLog::vError("OTASCIIArmor::%s: nDestLen 0.\n", __FUNCTION__); } return false; }