Exemplo n.º 1
0
void CObjectsSniffer::Probe(CObjectIStream& input)
{
    _ASSERT(m_Candidates.size());
    vector<CRef<COffsetReadHook> > hooks;  // list of all hooks we set

    //
    // create hooks for all candidates
    //

    TCandidates::const_iterator it;

    for (it = m_Candidates.begin(); it < m_Candidates.end(); ++it) {
        CRef<COffsetReadHook> h(new COffsetReadHook(this, it->event_mode));
        
        it->type_info.SetLocalReadHook(input, &(*h));
        hooks.push_back(h);

    } // for

    m_TopLevelMap.clear();


    if (input.GetDataFormat() == eSerial_AsnText
        || input.GetDataFormat() == eSerial_Xml) {
        ProbeText(input);
    } else {
        ProbeASN1_Bin(input);
    }

    //
    // Reset(clean) the hooks
    //

    _ASSERT(hooks.size() == m_Candidates.size());
    for (it = m_Candidates.begin(); it < m_Candidates.end(); ++it) {
        it->type_info.ResetLocalReadHook(input);
    } // for
}
Exemplo n.º 2
0
void CObjectsSniffer::ProbeText(CObjectIStream& input)
{
    TCandidates::const_iterator last_cand = m_Candidates.end();

    string format_name;  // for LOG_POST messages
    if (input.GetDataFormat() == eSerial_AsnText) {
        format_name = "ASN.1 text";
    } else {
        format_name = "XML";
    }

    try {
        while (true) {
            m_StreamPos = input.GetStreamPos();
            string header = input.ReadFileHeader();

            if ( last_cand != m_Candidates.end() ) {
                // Check the previously found candidate first
                // (performance optimization)
                if (header == last_cand->type_info.GetTypeInfo()->GetName()) {
                    TCandidates::const_iterator it = last_cand;
                    CObjectInfo object_info(it->type_info.GetTypeInfo());
                    input.Read(object_info, CObjectIStream::eNoFileHeader);
                    m_TopLevelMap.push_back(
                        SObjectDescription(it->type_info, m_StreamPos));

                    _TRACE("Same type "
                           << format_name << " top level object found:" 
                           << it->type_info.GetTypeInfo()->GetName());
                    continue;
                }
            }

            bool found = false;
            // Scan through all candidates
            ITERATE ( TCandidates, it, m_Candidates ) {
                if ( header == it->type_info.GetTypeInfo()->GetName() ) {
                    CObjectInfo object_info(it->type_info.GetTypeInfo());
                    input.Read(object_info, CObjectIStream::eNoFileHeader);
                    found = true;
                    last_cand = it;
                    m_TopLevelMap.push_back(
                        SObjectDescription(it->type_info, m_StreamPos));

                    LOG_POST_X(2, Info 
                                << format_name << " top level object found:" 
                                << it->type_info.GetTypeInfo()->GetName());
                    break;
                }
            } // for
            if ( !found ) {
                input.SetStreamPos(m_StreamPos);
                return;
            }
        } // while
    }
    catch (CEofException& /*ignored*/) {
    }
    catch (exception& e) {
        LOG_POST_X(3, Info << "Exception reading "
                   << format_name << " " << e.what());
        input.SetStreamPos(m_StreamPos);
    }
}