+/**
+ * @brief Returns length of XHTML entity by parsing the text. It also determines if it is XHTML entity or not.
+ *
+ * @param[in] markupStringBuffer The mark-up string buffer. It's a const iterator pointing the current character.
+ * @param[in] markupStringEndBuffer Pointing to end of mark-up string buffer.
+ *
+ * @return Length of markupText in case of XHTML entity otherwise return 0.
+ */
+unsigned int GetXHTMLEntityLength( const char*& markupStringBuffer,
+ const char* const markupStringEndBuffer )
+{
+ char character = *markupStringBuffer;
+ if( AMPERSAND == character ) // '&'
+ {
+ // if the iterator is pointing to a '&' character, then check for ';' to find end to XHTML entity.
+ ++markupStringBuffer;
+ if( markupStringBuffer < markupStringEndBuffer )
+ {
+ unsigned int len = 1u;
+ for( ; markupStringBuffer < markupStringEndBuffer ; ++markupStringBuffer )
+ {
+ character = *markupStringBuffer;
+ ++len;
+ if( SEMI_COLON == character ) // ';'
+ {
+ // found end of XHTML entity
+ ++markupStringBuffer;
+ return len;
+ }
+ else if( ( AMPERSAND == character ) || ( BACK_SLASH == character ) || ( LESS_THAN == character ))
+ {
+ return 0;
+ }
+ }
+ }
+ }
+ return 0;
+}
+
+/**
+ * @brief It parses a XHTML string which has hex/decimal entity and fill its corresponging utf-8 string.
+ *
+ * @param[in] markupText The mark-up text buffer.
+ * @param[out] utf-8 text Corresponding to markup Text
+ *
+ * @return true if string is successfully parsed otherwise false
+ */
+bool XHTMLNumericEntityToUtf8 ( const char* markupText, char* utf8 )
+{
+ bool result = false;
+
+ if( NULL != markupText )
+ {
+ bool isHex = false;
+
+ // check if hex or decimal entity
+ if( ( CHAR_ARRAY_END != *markupText ) && ( HEX_CODE == *markupText ) )
+ {
+ isHex = true;
+ ++markupText;
+ }
+
+ char* end = NULL;
+ unsigned long l = strtoul( markupText, &end, ( isHex ? 16 : 10 ) ); // l contains UTF-32 code in case of correct XHTML entity
+
+ // check for valid XHTML numeric entities (between '#' or "#x" and ';')
+ if( ( l > 0 ) && ( l < ULONG_MAX ) && ( *end == SEMI_COLON ) ) // in case wrong XHTML entity is set eg. "abcdefs;" in that case *end will be 'a'
+ {
+ /* characters XML 1.1 permits */
+ if( ( ( XHTML_DECIMAL_ENTITY_RANGE[0] < l ) && ( l <= XHTML_DECIMAL_ENTITY_RANGE[1] ) ) ||
+ ( ( XHTML_DECIMAL_ENTITY_RANGE[2] <= l ) && ( l <= XHTML_DECIMAL_ENTITY_RANGE[3] ) ) ||
+ ( ( XHTML_DECIMAL_ENTITY_RANGE[4] <= l ) && ( l <= XHTML_DECIMAL_ENTITY_RANGE[5] ) ) )
+ {
+ // Convert UTF32 code to UTF8
+ Utf32ToUtf8( reinterpret_cast<const uint32_t* const>( &l ), 1, reinterpret_cast<uint8_t*>( utf8 ) );
+ result = true;
+ }
+ }
+ }
+ return result;
+}
+