From 1ecf0a2e6aa3d1c701e7707dab553c5de438c98d Mon Sep 17 00:00:00 2001 From: Peter Thorson Date: Sun, 8 Jan 2012 20:20:17 -0600 Subject: [PATCH] payload valuation and gcc warning fixes --- src/messages/data.cpp | 24 ++++++++++++++++++++---- src/messages/data.hpp | 12 ++++++++++-- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/src/messages/data.cpp b/src/messages/data.cpp index c92759552e..4df1674288 100644 --- a/src/messages/data.cpp +++ b/src/messages/data.cpp @@ -87,10 +87,13 @@ uint64_t data::process_payload(std::istream& input,uint64_t size) { void data::process_character(unsigned char c) { if (m_masking_index >= 0) { - c = c ^ m_masking_key[(m_masking_index++)%4]; + c = c ^ m_masking_key[m_masking_index]; + m_masking_index = index_value((m_masking_index+1)%4); } - if (m_opcode == frame::opcode::TEXT && !m_validator.consume(static_cast((unsigned char)(c)))) { + if (m_opcode == frame::opcode::TEXT && + !m_validator.consume(static_cast((unsigned char)(c)))) + { throw processor::exception("Invalid UTF8 data",processor::error::PAYLOAD_VIOLATION); } @@ -113,7 +116,19 @@ void data::complete() { } } } - + +void data::validate_payload() { + if (m_opcode == frame::opcode::TEXT) { + if (!m_validator.decode(m_payload.begin(), m_payload.end())) { + throw exception("Invalid UTF8 data",error::PAYLOAD_VIOLATION); + } + + if (!m_validator.complete()) { + throw exception("Invalid UTF8 data",error::PAYLOAD_VIOLATION); + } + } +} + void data::set_masking_key(int32_t key) { *reinterpret_cast(m_masking_key) = key; // -2 indicates a masked frame whose key is zero. @@ -141,7 +156,8 @@ void data::append_payload(const std::string& payload) { void data::mask() { if (m_masking_index >= 0) { for (std::string::iterator it = m_payload.begin(); it != m_payload.end(); it++) { - (*it) = *it ^ m_masking_key[(m_masking_index++)%4]; + (*it) = *it ^ m_masking_key[m_masking_index]; + m_masking_index = index_value((m_masking_index+1)%4); } } } diff --git a/src/messages/data.hpp b/src/messages/data.hpp index 1aca002687..b131e066d2 100644 --- a/src/messages/data.hpp +++ b/src/messages/data.hpp @@ -38,7 +38,7 @@ #include #include -#include +#include #include #include @@ -110,7 +110,7 @@ public: } void recycle(element_ptr p) { if (p->get_index()+1 > m_used.size() || m_used[p->get_index()] != p) { - std::cout << "error tried to recycle a pointer we don't control" << std::endl; + //std::cout << "error tried to recycle a pointer we don't control" << std::endl; // error tried to recycle a pointer we don't control return; } @@ -118,6 +118,13 @@ public: m_avaliable.push(p); m_used[p->get_index()] = element_ptr(); + /*std::cout << "message recycled (" + << m_cur_elements-m_avaliable.size() + << "/" + << m_cur_elements + << ")" + << std::endl;*/ + if (m_callback && m_avaliable.size() == 1) { m_callback(); } @@ -162,6 +169,7 @@ public: uint64_t process_payload(std::istream& input,uint64_t size); void process_character(unsigned char c); void complete(); + void validate_payload(); // ##writing## // sets the payload to payload. Performs max size and UTF8 validation