📄 ectag.cpp
字号:
if (m_state == bsLength || m_state == bsLengthChld) { ec_taglen_t tagLen; if (!socket.ReadNumber(&tagLen, sizeof(ec_taglen_t))) { return false; } else { m_dataLen = tagLen; if (m_state == bsLength) { m_state = bsData1; } else { m_state = bsChildCnt; } } } if (m_state == bsChildCnt || m_state == bsChildren) { if (!ReadChildren(socket)) { return false; } } if (m_state == bsData1) { unsigned int tmp_len = m_dataLen; m_dataLen = 0; m_dataLen = tmp_len - GetTagLen(); if (m_dataLen > 0) { m_tagData = malloc(m_dataLen); m_state = bsData2; } else { m_tagData = NULL; m_state = bsFinished; } } if (m_state == bsData2) { if (m_tagData != NULL) { if (!socket.ReadBuffer((void *)m_tagData, m_dataLen)) { return false; } else { m_state = bsFinished; } } else { m_error = 1; return false; } } return true;}bool CECTag::WriteTag(CECSocket& socket) const{ ec_tagname_t tmp_tagName = (m_tagName << 1) | (m_tagList.empty() ? 0 : 1); ec_tagtype_t type = m_dataType; ec_taglen_t tagLen = GetTagLen(); wxASSERT(type != EC_TAGTYPE_UNKNOWN); if (!socket.WriteNumber(&tmp_tagName, sizeof(ec_tagname_t))) return false; if (!socket.WriteNumber(&type, sizeof(ec_tagtype_t))) return false; if (!socket.WriteNumber(&tagLen, sizeof(ec_taglen_t))) return false; if (!m_tagList.empty()) { if (!WriteChildren(socket)) return false; } if (m_dataLen > 0) { if (m_tagData != NULL) { // This is here only to make sure everything, it should not be NULL at this point if (!socket.WriteBuffer(m_tagData, m_dataLen)) return false; } } return true;}bool CECTag::ReadChildren(CECSocket& socket){ if (m_state == bsChildCnt) { uint16 tmp_tagCount; if (!socket.ReadNumber(&tmp_tagCount, sizeof(uint16))) { return false; } else { m_tagList.clear(); if (tmp_tagCount > 0) { m_tagList.reserve(tmp_tagCount); for (int i=0; i<tmp_tagCount; i++) { m_tagList.push_back(CECTag(socket)); } m_haschildren = true; m_state = bsChildren; } else { m_state = bsData1; } } } if (m_state == bsChildren) { for (unsigned int i=0; i<m_tagList.size(); i++) { CECTag& tag = m_tagList[i]; if (!tag.IsOk()) { if (!tag.ReadFromSocket(socket)) { if (tag.m_error != 0) { m_error = tag.m_error; } return false; } } } m_state = bsData1; } return true;}bool CECTag::WriteChildren(CECSocket& socket) const{ wxASSERT(m_tagList.size() < 0xFFFF); uint16 tmp = (uint16)m_tagList.size(); if (!socket.WriteNumber(&tmp, sizeof(tmp))) return false; if (!m_tagList.empty()) { for (TagList::size_type i=0; i<m_tagList.size(); i++) { if (!m_tagList[i].WriteTag(socket)) return false; } } return true;}/** * Finds the (first) child tag with given name. * * @param name TAG name to look for. * @return the tag found, or NULL. */const CECTag* CECTag::GetTagByName(ec_tagname_t name) const{ for (TagList::size_type i=0; i<m_tagList.size(); i++) if (m_tagList[i].m_tagName == name) return &m_tagList[i]; return NULL;}/** * Finds the (first) child tag with given name. * * @param name TAG name to look for. * @return the tag found, or NULL. */CECTag* CECTag::GetTagByName(ec_tagname_t name){ for (TagList::size_type i=0; i<m_tagList.size(); i++) if (m_tagList[i].m_tagName == name) return &m_tagList[i]; return NULL;}/** * Finds the (first) child tag with given name. * * @param name TAG name to look for. * @return the tag found, or a special null-valued tag otherwise. * * @see s_theNullTag */const CECTag* CECTag::GetTagByNameSafe(ec_tagname_t name) const{ const CECTag* result = GetTagByName(name); if (result == NULL) result = &s_theNullTag; return result;}/** * Query TAG length that is suitable for the TAGLEN field (i.e.\ * without it's own header size). * * @return Tag length, containing its childs' length. */uint32 CECTag::GetTagLen(void) const{ uint32 length = m_dataLen; for (TagList::size_type i=0; i<m_tagList.size(); i++) { length += m_tagList[i].GetTagLen(); length += sizeof(ec_tagname_t) + sizeof(ec_tagtype_t) + sizeof(ec_taglen_t) + ((m_tagList[i].GetTagCount() > 0) ? 2 : 0); } return length;}uint64_t CECTag::GetInt() const{ if (m_tagData == NULL) { // Empty tag - This is NOT an error. EC_ASSERT(m_dataType == EC_TAGTYPE_UNKNOWN); return 0; } switch (m_dataType) { case EC_TAGTYPE_UINT8: return PeekUInt8(m_tagData); case EC_TAGTYPE_UINT16: return ENDIAN_NTOHS( RawPeekUInt16( m_tagData ) ); case EC_TAGTYPE_UINT32: return ENDIAN_NTOHL( RawPeekUInt32( m_tagData ) ); case EC_TAGTYPE_UINT64: return ENDIAN_NTOHLL( RawPeekUInt64( m_tagData ) ); case EC_TAGTYPE_UNKNOWN: // Empty tag - This is NOT an error. return 0; default: EC_ASSERT(0); return 0; }}std::string CECTag::GetStringDataSTL() const{ if (m_dataType != EC_TAGTYPE_STRING) { EC_ASSERT(m_dataType == EC_TAGTYPE_UNKNOWN); return std::string(); } else if (m_tagData == NULL) { EC_ASSERT(false); return std::string(); } return std::string((const char*)m_tagData);}#ifdef USE_WX_EXTENSIONSwxString CECTag::GetStringData() const{ return UTF82unicode(GetStringDataSTL().c_str());}#endifCMD4Hash CECTag::GetMD4Data() const{ if (m_dataType != EC_TAGTYPE_HASH16) { EC_ASSERT(m_dataType == EC_TAGTYPE_UNKNOWN); return CMD4Hash(); } EC_ASSERT(m_tagData != NULL); // Doesn't matter if m_tagData is NULL in CMD4Hash(), // that'll just result in an empty hash. return CMD4Hash((const unsigned char *)m_tagData); }/** * Returns an EC_IPv4_t class. * * This function takes care of the enadianness of the port number. * * @return EC_IPv4_t class. * * @see CECTag(ec_tagname_t, const EC_IPv4_t&) */EC_IPv4_t CECTag::GetIPv4Data() const{ EC_IPv4_t p(0, 0); if (m_tagData == NULL) { EC_ASSERT(false); } else if (m_dataType != EC_TAGTYPE_IPV4) { EC_ASSERT(false); } else { RawPokeUInt32( p.m_ip, RawPeekUInt32( ((EC_IPv4_t *)m_tagData)->m_ip ) ); p.m_port = ENDIAN_NTOHS(((EC_IPv4_t *)m_tagData)->m_port); } return p;}/** * Returns a double value. * * @note The returned value is what we get by converting the string form * of the number to a double. * * @return The double value of the tag. * * @see CECTag(ec_tagname_t, double) */double CECTag::GetDoubleData(void) const{ if (m_dataType != EC_TAGTYPE_DOUBLE) { EC_ASSERT(m_dataType == EC_TAGTYPE_UNKNOWN); return 0; } else if (m_tagData == NULL) { EC_ASSERT(false); return 0; } std::istringstream double_str((const char*)m_tagData); double data; double_str >> data; return data;}void CECTag::ConstructStringTag(ec_tagname_t /*name*/, const std::string& data){ m_dataLen = (ec_taglen_t)strlen(data.c_str()) + 1; m_tagData = malloc(m_dataLen); if (m_tagData != NULL) { memcpy((void *)m_tagData, data.c_str(), m_dataLen); m_error = 0; m_dataType = EC_TAGTYPE_STRING; } else { m_error = 1; } }/*! * \fn CMD4Hash CECTag::GetMD4Data(void) const * * \brief Returns a CMD4Hash class. * * This function takes care of converting from MSB to LSB as necessary. * * \return CMD4Hash class. * * \sa CECTag(ec_tagname_t, const CMD4Hash&) *//*! * \fn CECTag *CECTag::GetTagByIndex(size_t index) const * * \brief Finds the indexth child tag. * * \param index 0-based index, 0 <= \e index < GetTagCount() * * \return The child tag, or NULL if index out of range. *//*! * \fn CECTag *CECTag::GetTagByIndexSafe(size_t index) const * * \brief Finds the indexth child tag. * * \param index 0-based index, 0 <= \e index < GetTagCount() * * \return The child tag, or a special null-valued tag if index out of range. *//*! * \fn size_t CECTag::GetTagCount(void) const * * \brief Returns the number of child tags. * * \return The number of child tags. *//*! * \fn const void *CECTag::GetTagData(void) const * * \brief Returns a pointer to the TAG DATA. * * \return A pointer to the TAG DATA. (As specified with the data field of the constructor.)*//*! * \fn uint16 CECTag::GetTagDataLen(void) const * * \brief Returns the length of the data buffer. * * \return The length of the data buffer. *//*! * \fn ec_tagname_t CECTag::GetTagName(void) const * * \brief Returns TAGNAME. * * \return The name of the tag. *//*! * \fn wxString CECTag::GetStringData(void) const * * \brief Returns the string data of the tag. * * Returns a wxString created from TAGDATA. It is automatically * converted from UTF-8 to the internal application encoding. * Should be used with care (only on tags created with the * CECTag(ec_tagname_t, const wxString&) constructor), * becuse it does not perform any check to see if the tag really contains a * string object. * * \return The string data of the tag. * * \sa CECTag(ec_tagname_t, const wxString&) *//*! * \fn uint8 CECTag::GetInt(void) const * * \brief Returns the uint8 data of the tag. * * This function takes care of the endianness problems with numbers. * * \return The uint8 data of the tag. * * \sa CECTag(ec_tagname_t, uint8) *//*! * \fn uint16 CECTag::GetInt(void) const * * \brief Returns the uint16 data of the tag. * * This function takes care of the endianness problems with numbers. * * \return The uint16 data of the tag. * * \sa CECTag(ec_tagname_t, uint16) *//*! * \fn uint32 CECTag::GetInt(void) const * * \brief Returns the uint32 data of the tag. * * This function takes care of the endianness problems with numbers. * * \return The uint32 data of the tag. * * \sa CECTag(ec_tagname_t, uint32) *//*! * \fn uint64 CECTag::GetInt(void) const * * \brief Returns the uint64 data of the tag. * * This function takes care of the endianness problems with numbers. * * \return The uint64 data of the tag. * * \sa CECTag(ec_tagname_t, uint64) */// File_checked_for_headers
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -