payload valuation and gcc warning fixes

This commit is contained in:
Peter Thorson
2012-01-08 20:20:17 -06:00
parent 7ef6ea4ee3
commit 1ecf0a2e6a
2 changed files with 30 additions and 6 deletions

View File

@@ -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<uint32_t>((unsigned char)(c)))) {
if (m_opcode == frame::opcode::TEXT &&
!m_validator.consume(static_cast<uint32_t>((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<int32_t*>(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);
}
}
}

View File

@@ -38,7 +38,7 @@
#include <boost/utility.hpp>
#include <algorithm>
#include <istream>
#include <iostream>
#include <queue>
#include <vector>
@@ -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