bool STObject::setType (const SOTemplate& type)
{
    boost::ptr_vector<SerializedType> newData (type.peek ().size ());
    bool valid = true;

    mType = &type;

    BOOST_FOREACH (const SOElement * elem, type.peek ())
    {
        bool match = false;

        for (boost::ptr_vector<SerializedType>::iterator it = mData.begin (); it != mData.end (); ++it)
            if (it->getFName () == elem->e_field)
            {
                // matching entry, move to new vector
                match = true;

                if ((elem->flags == SOE_DEFAULT) && it->isDefault ())
                {
                    WriteLog (lsWARNING, STObject) << "setType( " << getFName ().getName () << ") invalid default "
                                                   << elem->e_field.fieldName;
                    valid = false;
                }

                newData.push_back (mData.release (it).release ()); // CAUTION: This renders 'it' invalid
                break;
            }

        if (!match)
        {
            // no match found
            if (elem->flags == SOE_REQUIRED)
            {
                WriteLog (lsWARNING, STObject) << "setType( " << getFName ().getName () << ") invalid missing "
                                               << elem->e_field.fieldName;
                valid = false;
            }

            newData.push_back (makeNonPresentObject (elem->e_field).release ());
        }
    }

    BOOST_FOREACH (const SerializedType & t, mData)
    {
        // Anything left over must be discardable
        if (!t.getFName ().isDiscardable ())
        {
            WriteLog (lsWARNING, STObject) << "setType( " << getFName ().getName () << ") invalid leftover "
                                           << t.getFName ().getName ();
            valid = false;
        }
    }

    mData.swap (newData);
    return valid;
}
Ejemplo n.º 2
0
bool STObject::setType (const SOTemplate& type)
{
    bool valid = true;
    mType = &type;
    decltype(v_) v;
    v.reserve(type.size());
    for (auto const& e : type.all())
    {
        auto const iter = std::find_if(
            v_.begin(), v_.end(), [&](detail::STVar const& b)
                { return b.get().getFName() == e->e_field; });
        if (iter != v_.end())
        {
            if ((e->flags == SOE_DEFAULT) && iter->get().isDefault())
            {
                WriteLog (lsWARNING, STObject) <<
                    "setType( " << getFName ().getName () <<
                    " ) invalid default " << e->e_field.fieldName;
                valid = false;
            }
            v.emplace_back(std::move(*iter));
            v_.erase(iter);
        }
        else
        {
            if (e->flags == SOE_REQUIRED)
            {
                WriteLog (lsWARNING, STObject) <<
                    "setType( " << getFName ().getName () <<
                    " ) invalid missing " << e->e_field.fieldName;
                valid = false;
            }
            v.emplace_back(detail::nonPresentObject, e->e_field);
        }
    }
    for (auto const& e : v_)
    {
        // Anything left over in the object must be discardable
        if (! e->getFName().isDiscardable())
        {
            WriteLog (lsWARNING, STObject) <<
                "setType( " << getFName ().getName () <<
                " ) invalid leftover " << e->getFName ().getName ();
            valid = false;
        }
    }
    // Swap the template matching data in for the old data,
    // freeing any leftover junk
    v_.swap(v);
    return valid;
}
Ejemplo n.º 3
0
Json::Value STUInt16::getJson(int) const
{
	if (getFName() == sfLedgerEntryType)
	{
		LedgerEntryFormat *f = LedgerEntryFormat::getLgrFormat(value);
		if (f != NULL)
			return f->t_name;
	}
	if (getFName() == sfTransactionType)
	{
		TransactionFormat *f = TransactionFormat::getTxnFormat(value);
		if (f != NULL)
			return f->t_name;
	}
	return value;
}
Ejemplo n.º 4
0
std::string STUInt16::getText() const
{
	if (getFName() == sfLedgerEntryType)
	{
		LedgerEntryFormat *f = LedgerEntryFormat::getLgrFormat(value);
		if (f != NULL)
			return f->t_name;
	}
	if (getFName() == sfTransactionType)
	{
		TransactionFormat *f = TransactionFormat::getTxnFormat(value);
		if (f != NULL)
			return f->t_name;
	}
	return boost::lexical_cast<std::string>(value);
}
Ejemplo n.º 5
0
void STObject::add (Serializer& s, bool withSigningFields) const
{
    std::map<int, STBase const*> fields;
    for (auto const& e : v_)
    {
        // pick out the fields and sort them
        if ((e->getSType() != STI_NOTPRESENT) &&
            e->getFName().shouldInclude (withSigningFields))
        {
            fields.insert (std::make_pair (
                e->getFName().fieldCode, &e.get()));
        }
    }

    // insert sorted
    for (auto const& e : fields)
    {
        auto const field = e.second;

        // When we serialize an object inside another object,
        // the type associated by rule with this field name
        // must be OBJECT, or the object cannot be deserialized
        assert ((field->getSType() != STI_OBJECT) ||
            (field->getFName().fieldType == STI_OBJECT));
        field->addFieldID (s);
        field->add (s);
        if (dynamic_cast<const STArray*> (field) != nullptr)
            s.addFieldID (STI_ARRAY, 1);
        else if (dynamic_cast<const STObject*> (field) != nullptr)
            s.addFieldID (STI_OBJECT, 1);
    }
}
Ejemplo n.º 6
0
int cmd(int fd, char signal, char * fname){
	char * pathless;
	write(fd, &signal, sizeof(char));
	if(fname != NULL){
		pathless = getFName(fname);
		write(fd, pathless, strlen(pathless));
		write(fd, &signal, sizeof(char));
	}
}
Ejemplo n.º 7
0
std::string STUInt8::getText() const
{
	if (getFName() == sfTransactionResult)
	{
		std::string token, human;
		if (transResultInfo(static_cast<TER>(value), token, human))
			return human;
	}
	return boost::lexical_cast<std::string>(value);
}
Ejemplo n.º 8
0
Json::Value STUInt8::getJson(int) const
{
	if (getFName() == sfTransactionResult)
	{
		std::string token, human;
		if (transResultInfo(static_cast<TER>(value), token, human))
			return token;
		else
			cLog(lsWARNING) << "Unknown result code in metadata: " << value;
	}
	return value;
}
Ejemplo n.º 9
0
std::string STUInt16::getText () const
{
    if (getFName () == sfLedgerEntryType)
    {
        auto item = LedgerFormats::getInstance ().findByType (
            static_cast <LedgerEntryType> (value_));

        if (item != nullptr)
            return item->getName ();
    }

    if (getFName () == sfTransactionType)
    {
        TxFormats::Item const* const item =
            TxFormats::getInstance().findByType (static_cast <TxType> (value_));

        if (item != nullptr)
            return item->getName ();
    }

    return beast::lexicalCastThrow <std::string> (value_);
}
Ejemplo n.º 10
0
Json::Value STUInt16::getJson (int) const
{
    if (getFName () == sfLedgerEntryType)
    {
        LedgerFormats::Item const* const item =
            LedgerFormats::getInstance ().findByType (static_cast <LedgerEntryType> (value_));

        if (item != nullptr)
            return item->getName ();
    }

    if (getFName () == sfTransactionType)
    {
        TxFormats::Item const* const item =
            TxFormats::getInstance().findByType (static_cast <TxType> (value_));

        if (item != nullptr)
            return item->getName ();
    }

    return value_;
}
Ejemplo n.º 11
0
int broadcastCmd(struct wrkrTrack workers, char signal, char * fname){
	int i, fd;
	char * pathless;
	if(fname != NULL)
		pathless = getFName(fname);
	for(i = 0; i < workers.workerCount; i++){
		fd = workers.worker_fd[i];
		write(fd, &signal, sizeof(char));
		if(fname != NULL){
			write(fd, pathless, strlen(pathless));
			write(fd, &signal, sizeof(char));
		}
	}
}
Ejemplo n.º 12
0
static
bool
isMemoOkay (STObject const& st, std::string& reason)
{
    if (!st.isFieldPresent (sfMemos))
        return true;

    auto const& memos = st.getFieldArray (sfMemos);

    // The number 2048 is a preallocation hint, not a hard limit
    // to avoid allocate/copy/free's
    Serializer s (2048);
    memos.add (s);

    // FIXME move the memo limit into a config tunable
    if (s.getDataLength () > 1024)
    {
        reason = "The memo exceeds the maximum allowed size.";
        return false;
    }

    for (auto const& memo : memos)
    {
        auto memoObj = dynamic_cast <STObject const*> (&memo);

        if (!memoObj || (memoObj->getFName() != sfMemo))
        {
            reason = "A memo array may contain only Memo objects.";
            return false;
        }

        for (auto const& memoElement : *memoObj)
        {
            auto const& name = memoElement.getFName();

            if (name != sfMemoType &&
                name != sfMemoData &&
                name != sfMemoFormat)
            {
                reason = "A memo may contain only MemoType, MemoData or "
                         "MemoFormat fields.";
                return false;
            }

            // The raw data is stored as hex-octets, which we want to decode.
            auto data = strUnHex (memoElement.getText ());

            if (!data.second)
            {
                reason = "The MemoType, MemoData and MemoFormat fields may "
                         "only contain hex-encoded data.";
                return false;
            }

            if (name == sfMemoData)
                continue;

            // The only allowed characters for MemoType and MemoFormat are the
            // characters allowed in URLs per RFC 3986: alphanumerics and the
            // following symbols: -._~:/?#[]@!$&'()*+,;=%
            static std::array<char, 256> const allowedSymbols = []
            {
                std::array<char, 256> a;
                a.fill(0);

                std::string symbols (
                    "0123456789"
                    "-._~:/?#[]@!$&'()*+,;=%"
                    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
                    "abcdefghijklmnopqrstuvwxyz");

                for(char c : symbols)
                    a[c] = 1;
                return a;
            }();

            for (auto c : data.first)
            {
                if (!allowedSymbols[c])
                {
                    reason = "The MemoType and MemoFormat fields may only "
                             "contain characters that are allowed in URLs "
                             "under RFC 3986.";
                    return false;
                }
            }
        }
    }

    return true;
}