]> git.tdb.fi Git - libs/core.git/blobdiff - source/utf8.cpp
Fix the UTF-8 decoder
[libs/core.git] / source / utf8.cpp
index 2d0458f3254b96c69ff827c0f2e86f5a44ab220b..dd01150803186dc68a4687668026a94e23b3dee4 100644 (file)
@@ -34,7 +34,7 @@ void Utf8::Encoder::encode_char(UnicodeChar ch, string &buf)
                utf[0]=0xFF<<(8-bytes) | ch>>(bytes*6-6);
                for(unsigned j=bytes-1; j>0; --j)
                {
-                       utf[j]=0x80 | ch&0x3F;
+                       utf[j]=0x80 | (ch&0x3F);
                        ch>>=6;
                }
 
@@ -71,22 +71,14 @@ UnicodeChar Utf8::Decoder::decode_char(const string &str, string::const_iterator
                UnicodeChar result=(*j++)&(mask-1);
 
                unsigned k;
-               for(k=1; (k<bytes && j!=str.end()); ++k)
-               {
-                       if((*j&0xC0)!=0x80)
-                       {
-                               result=error("Incomplete UTF-8 character");
-                               i=j;
-                               return result;
-                       }
-                       result=result<<6 | (*j++)&0x3F;
-               }
+               for(k=1; (k<bytes && j!=str.end() && (*j&0xC0)==0x80); ++k)
+                       result=(result<<6) | ((*j++)&0x3F);
 
                if(k<bytes)
-                       result=error("Incomplete UTF-8 character at end of input");
-               else if(!result>>(bytes*6-6))
+                       result=error("Incomplete UTF-8 character");
+               else if(!(result>>(bytes*5-4)) || !(result>>7))
                        result=error("Denormalized UTF-8 multibyte sequence");
-               else if(result>0x10FFFF)
+               else if(result>0x10FFFF || (result>=0xD800 && result<=0xDFFF))
                        result=error("Invalid Unicode code point");
 
                i=j;