void appendHeaderWithEmulation(Header& h) { Header::iterator s = h.begin(); Header::iterator e; uint8_t zeros[] = {0, 0}; uint8_t emulation[] = {0, 0, 3}; do { e = std::search(s, h.end(), zeros, zeros + N_ELEMENTS(zeros)); m_headers.insert(m_headers.end(), s, e); if (e == h.end()) break; s = e + N_ELEMENTS(zeros); /* only when bitstream contains 0x000000/0x000001/0x000002/0x000003 need to insert emulation prevention byte 0x03 */ if (*s <= 3) m_headers.insert(m_headers.end(), emulation, emulation + N_ELEMENTS(emulation)); else m_headers.insert(m_headers.end(), zeros, zeros + N_ELEMENTS(zeros)); } while (1); }
int Control::process(Header &header) { size_ = header.payload_size() + 2; Utilities::Utf8::Byte utf8; uint8_t mask_offset = 0; uint8_t *buffer = &(*buffer_.get())[0]; buffer[0] = header.begin()[0]; buffer[1] = header.begin()[1]; Utilities::Print::hex(&buffer[0],header.payload_size()+2); std::cout << std::endl; if( (buffer[0] & 0x0F) == 0x08) { if(header.payload_size() >= 2) { Utilities::Websocket::applyMask(&buffer[2] , 2 , &header.mask(), mask_offset , false , utf8); Utilities::Websocket::applyMask(&buffer[4] , header.payload_size() -2 , &header.mask(), mask_offset , true , utf8); if(!utf8.complete()) { std::cout << "invalid utf in control" << std::endl; return 1007; } } state_ = ControlState::KILL; std::cout << "control kill" << std::endl; buffer[1] &= 0x7F; uint16_t code = buffer[2] << 8 | buffer[3] << 0; if( code < 1000 ||(code < 3000 && code != 1000 && code != 1001 && code != 1002 && code != 1003 && code != 1007 && code != 1008 && code != 1009 && code != 1010 && code != 1011) || code > 4999 ) { buffer[2] = 0x03; buffer[3] = 0xEA; } } else if ( (buffer[0] & 0x0F) == 0x09 ) { Utilities::Websocket::applyMask(&buffer[2] , header.payload_size() , &header.mask(), mask_offset , false , utf8); if(!utf8.complete()) { std::cout << "invalid utf in control" << std::endl; return 1007; } state_ = ControlState::PING; buffer[0] = ( buffer[0] & 0xF0 ) | 0x0A; buffer[1] &= 0x7F; std::cout << "control pong"; Utilities::Print::hex(&buffer[0],2);std::cout <<std::endl; } else if ( (buffer[0] & 0x0F) == 0x0A ) { state_ = ControlState::PONG; std::cout << "pong recieved" << std::endl; } else { state_ = ControlState::INVALID; } return 0; }