CS 32 Solution Project 4

CS 32 Solution Project 4 - / ips.cpp #include "ips.h"...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
// ips.cpp #include "ips.h" #include <string> #include <iostream> #include <sstream> // for istringstream #include <list> #include <hash_set> #include <algorithm> #include <cctype> #include <cstdlib> using namespace std; typedef unsigned int Soffset; // offset in data stream const size_t MAX_SIGNATURE_LENGTH = 96; const size_t NONWILD_PREFIX_LENGTH = 6; const size_t MAX_STAR_WILDCARD_LENGTH = 32; const size_t BUFFER_SIZE = 3 * MAX_PACKET_SIZE; // Spec- mandated limit const unsigned int HASH_TABLE_SIZE = 199999; /////////////////////////////////////////////////////////// /////////////////// // DataStream /////////////////////////////////////////////////////////// /////////////////// class DataStream { public: DataStream() : m_windowBegin(0), m_windowEnd(0) {} void reset() { m_windowBegin = 0; m_windowEnd = 0;
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
m_soffsetMap.clear(); } Soffset end() const { return m_windowEnd; } const char* soffsetToPtr(Soffset so) const { return m_buffer + (so - m_windowBegin); } char* soffsetToPtr(Soffset so) { return m_buffer + (so - m_windowBegin); } void soffsetToPacket(Soffset so, PacketNumberType& pn, { list<SoffsetPacketNumberPair>::const_iterator p = m_soffsetMap.end(); while (p != m_soffsetMap.begin()) { p--; if (so >= p->soffset) { pn = p->packetNumber; po = so - p->soffset; return; } } cerr << "*** REACHED UNREACHABLE CODE IN soffsetToPacket! ***" << endl; exit(1); } Soffset addPacketToStream(const Packet& p, Soffset earliestToRetain) { unsigned int packetLength; const char* packetData = p.getData(packetLength);
Background image of page 2
if (m_windowEnd - m_windowBegin + packetLength > BUFFER_SIZE) { copy(soffsetToPtr(earliestToRetain), soffsetToPtr(m_windowEnd), soffsetToPtr(m_windowBegin)); m_windowBegin = earliestToRetain; list<SoffsetPacketNumberPair>::iterator p = m_soffsetMap.begin(); // list is never empty, so p++ will not increment end(). do { p++; } while (p != m_soffsetMap.end() && m_windowBegin >= p->soffset); p--; m_soffsetMap.erase(m_soffsetMap.begin(), p); } Soffset packetStart = m_windowEnd; m_soffsetMap.push_back(SoffsetPacketNumberPair(pack etStart, p.g etPacketNumber())); copy(packetData, packetData + packetLength, soffsetToPtr(packetStart)); m_windowEnd += packetLength; return packetStart; } private: struct SoffsetPacketNumberPair { SoffsetPacketNumberPair(Soffset so, PacketNumberType pn) : soffset(so), packetNumber(pn) {} Soffset soffset; PacketNumberType packetNumber; };
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
char m_buffer[BUFFER_SIZE]; Soffset m_windowBegin; Soffset m_windowEnd; list<SoffsetPacketNumberPair> m_soffsetMap; }; /////////////////////////////////////////////////////////// /////////////////// // SignatureInfo /////////////////////////////////////////////////////////// /////////////////// struct SignatureInfo { : id(i), sig(s), maxMatch(0) { for (unsigned int k = 0; k < sig.size(); k++) maxMatch += (sig[k] == '*' ? MAX_STAR_WILDCARD_LENGTH : 1);
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/07/2009 for the course CS 32 taught by Professor Davidsmallberg during the Winter '08 term at UCLA.

Page1 / 18

CS 32 Solution Project 4 - / ips.cpp #include "ips.h"...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online