Exemplo n.º 1
0
TEST(TestEndianSwap, Endian_SwapBE64)
{
  uint64_t ref, var;
  ref = UINT64_C(0x00FF00FF00FF00FF);
  var = Endian_SwapBE64(UINT64_C(0xFF00FF00FF00FF00));
  EXPECT_EQ(ref, var);
}
Exemplo n.º 2
0
CWebSocketFrame::CWebSocketFrame(const char* data, uint64_t length)
{
  reset();

  if (data == NULL || length < LENGTH_MIN)
    return;

  m_free = false;
  m_data = data;
  m_lengthFrame = length;

  // Get the FIN flag
  m_final = ((m_data[0] & MASK_FIN) == MASK_FIN);
  // Get the RSV1 - RSV3 flags
  m_extension |= m_data[0] & MASK_RSV1;
  m_extension |= (m_data[0] & MASK_RSV2) << 1;
  m_extension |= (m_data[0] & MASK_RSV3) << 2;
  // Get the opcode
  m_opcode = (WebSocketFrameOpcode)(m_data[0] & MASK_OPCODE);
  if (m_opcode >= WebSocketUnknownFrame)
  {
    CLog::Log(LOGINFO, "WebSocket: Frame with invalid opcode %2X received", m_opcode);
    reset();
    return;
  }
  if ((m_opcode & CONTROL_FRAME) == CONTROL_FRAME && !m_final)
  {
    CLog::Log(LOGINFO, "WebSocket: Fragmented control frame (opcode %2X) received", m_opcode);
    reset();
    return;
  }

  // Get the MASK flag
  m_masked = ((m_data[1] & MASK_MASK) == MASK_MASK);

  // Get the payload length
  m_length = (uint64_t)(m_data[1] & MASK_LENGTH);
  if ((m_length <= 125 && m_lengthFrame  < m_length + LENGTH_MIN) ||
      (m_length == 126 && m_lengthFrame < LENGTH_MIN + 2) ||
      (m_length == 127 && m_lengthFrame < LENGTH_MIN + 8))
  {
    CLog::Log(LOGINFO, "WebSocket: Frame with invalid length received");
    reset();
    return;
  }

  if (IsControlFrame() && (m_length > 125 || !m_final))
  {
    CLog::Log(LOGWARNING, "WebSocket: Invalid control frame received");
    reset();
    return;
  }

  int offset = 0;
  if (m_length == 126)
  {
    m_length = (uint64_t)Endian_SwapBE16(*(uint16_t *)(m_data + 2));
    offset = 2;
  }
  else if (m_length == 127)
  {
    m_length = Endian_SwapBE64(*(uint64_t *)(m_data + 2));
    offset = 8;
  }

  if (m_lengthFrame < LENGTH_MIN + offset + m_length)
  {
    CLog::Log(LOGINFO, "WebSocket: Frame with invalid length received");
    reset();
    return;
  }

  // Get the mask
  if (m_masked)
  {
    m_mask = *(uint32_t *)(m_data + LENGTH_MIN + offset);
    offset += 4;
  }

  if (m_lengthFrame != LENGTH_MIN + offset + m_length)
    m_lengthFrame = LENGTH_MIN + offset + m_length;

  // Get application data
  if (m_length > 0)
    m_applicationData = (char *)(m_data + LENGTH_MIN + offset);
  else
    m_applicationData = NULL;

  // Unmask the application data if necessary
  if (m_masked)
  {
    for (uint64_t index = 0; index < m_length; index++)
      m_applicationData[index] = m_applicationData[index] ^ ((char *)(&m_mask))[index % 4];
  }

  m_valid = true;
}