Make PerlIO_getpos() to behave like fgetpos() on return.
[p5sagit/p5-mst-13.2.git] / utf8.c
diff --git a/utf8.c b/utf8.c
index c077182..6ddf42b 100644 (file)
--- a/utf8.c
+++ b/utf8.c
@@ -104,27 +104,41 @@ Perl_uv_to_utf8(pTHX_ U8 *d, UV uv)
 /* Tests if some arbitrary number of bytes begins in a valid UTF-8 character.
  * The actual number of bytes in the UTF-8 character will be returned if it
  * is valid, otherwise 0. */
-int
+STRLEN
 Perl_is_utf8_char(pTHX_ U8 *s)
 {
     U8 u = *s;
-    int slen, len;
+    STRLEN slen, len;
+    UV uv, ouv;
 
-    if (!(u & 0x80))
+    if (u <= 0x7f)
        return 1;
 
-    if (!(u & 0x40))
+    if (u >= 0x80 && u <= 0xbf)
        return 0;
 
-    len = UTF8SKIP(u);
+    len = UTF8SKIP(s);
+
+    if (len < 2 || (u >= 0xc0 && u <= 0xfd && s[1] < 0x80))
+       return 0;
 
     slen = len - 1;
     s++;
+    uv = u;
+    ouv = uv;
     while (slen--) {
        if ((*s & 0xc0) != 0x80)
            return 0;
+       uv = (uv << 6) | (*s & 0x3f);
+       if (uv < ouv)
+           return 0;
+       ouv = uv;
        s++;
     }
+
+    if (UNISKIP(uv) < len)
+       return 0;
+
     return len;
 }
 
@@ -140,109 +154,192 @@ string, false otherwise.
 bool
 Perl_is_utf8_string(pTHX_ U8 *s, STRLEN len)
 {
-    U8* x=s;
-    U8* send=s+len;
-    int c;
+    U8* x = s;
+    U8* send = s + len;
+    STRLEN c;
+
     while (x < send) {
         c = is_utf8_char(x);
+       if (!c)
+           return FALSE;
         x += c;
-        if (!c || x > send)
-            return 0;
+        if (x > send)
+            return FALSE;
     }
-    return 1;
+
+    return TRUE;
 }
 
 /*
-=for apidoc Am|U8* s|utf8_to_uv_chk|I32 *retlen|I32 checking
+=for apidoc Am|U8* s|utf8_to_uv|STRLEN curlen|I32 *retlen|U32 flags
 
 Returns the character value of the first character in the string C<s>
-which is assumed to be in UTF8 encoding; C<retlen> will be set to the
-length, in bytes, of that character, and the pointer C<s> will be
-advanced to the end of the character.
+which is assumed to be in UTF8 encoding and no longer than C<curlen>;
+C<retlen> will be set to the length, in bytes, of that character,
+and the pointer C<s> will be advanced to the end of the character.
 
 If C<s> does not point to a well-formed UTF8 character, the behaviour
-is dependent on the value of C<checking>: if this is true, it is
-assumed that the caller will raise a warning, and this function will
-set C<retlen> to C<-1> and return. If C<checking> is not true, an optional UTF8
-warning is produced.
+is dependent on the value of C<flags>: if it contains UTF8_CHECK_ONLY,
+it is assumed that the caller will raise a warning, and this function
+will set C<retlen> to C<-1> and return.  The C<flags> can also contain
+various flags to allow deviations from the strict UTF-8 encoding.
 
-=cut
-*/
+=cut */
 
 UV
-Perl_utf8_to_uv_chk(pTHX_ U8* s, I32* retlen, bool checking)
+Perl_utf8_to_uv(pTHX_ U8* s, STRLEN curlen, STRLEN* retlen, U32 flags)
 {
-    UV uv = *s;
-    int len;
-    if (!(uv & 0x80)) {
+    dTHR;
+    UV uv = *s, ouv;
+    STRLEN len = 1;
+    bool dowarn = ckWARN_d(WARN_UTF8);
+    STRLEN expectlen = 0;
+    
+    if (uv <= 0x7f) { /* Pure ASCII. */
        if (retlen)
            *retlen = 1;
        return *s;
     }
-    if (!(uv & 0x40)) {
-        dTHR;
-       if (checking && retlen) {
-           *retlen = -1;
-           return 0;
-       }
 
-       if (ckWARN_d(WARN_UTF8))
-           Perl_warner(aTHX_ WARN_UTF8, "Malformed UTF-8 character");
-       if (retlen)
-           *retlen = 1;
-       return *s;
+    if ((uv >= 0x80 && uv <= 0xbf) &&
+       !(flags & UTF8_ALLOW_CONTINUATION)) {
+       if (dowarn)
+           Perl_warner(aTHX_ WARN_UTF8,
+                       "Malformed UTF-8 character (unexpected continuation byte 0x%02"UVxf")",
+                       uv);
+       goto malformed;
     }
 
-    if      (!(uv & 0x20))     { len = 2; uv &= 0x1f; }
-    else if (!(uv & 0x10))     { len = 3; uv &= 0x0f; }
-    else if (!(uv & 0x08))     { len = 4; uv &= 0x07; }
-    else if (!(uv & 0x04))     { len = 5; uv &= 0x03; }
-    else if (!(uv & 0x02))     { len = 6; uv &= 0x01; }
-    else if (!(uv & 0x01))     { len = 7;  uv = 0; }
+    if ((uv >= 0xc0 && uv <= 0xfd && curlen >1 && s[1] < 0x80) &&
+       !(flags & UTF8_ALLOW_NON_CONTINUATION)) {
+       if (dowarn)
+           Perl_warner(aTHX_ WARN_UTF8,
+                       "Malformed UTF-8 character (unexpected non-continuation byte 0x%02"UVxf" after byte 0x%02"UVxf")",
+                       (UV)s[1], uv);
+       goto malformed;
+    }
+    
+    if ((uv == 0xfe || uv == 0xff) &&
+       !(flags & UTF8_ALLOW_FE_FF)) {
+       if (dowarn)
+           Perl_warner(aTHX_ WARN_UTF8,
+                       "Malformed UTF-8 character (byte 0x%02"UVxf")",
+                       uv);
+       goto malformed;
+    }
+       
+    if      (!(uv & 0x20))     { len =  2; uv &= 0x1f; }
+    else if (!(uv & 0x10))     { len =  3; uv &= 0x0f; }
+    else if (!(uv & 0x08))     { len =  4; uv &= 0x07; }
+    else if (!(uv & 0x04))     { len =  5; uv &= 0x03; }
+    else if (!(uv & 0x02))     { len =  6; uv &= 0x01; }
+    else if (!(uv & 0x01))     { len =  7; uv = 0; }
     else                       { len = 13; uv = 0; } /* whoa! */
-
+       
     if (retlen)
        *retlen = len;
-    --len;
+    
+    expectlen = len;
+
+    if ((curlen < expectlen) &&
+       !(flags & UTF8_ALLOW_SHORT)) {
+       if (dowarn)
+           Perl_warner(aTHX_ WARN_UTF8,
+                       "Malformed UTF-8 character (%d byte%s, need %d)",
+                       curlen, curlen > 1 ? "s" : "", expectlen);
+       goto malformed;
+    }
+
+    len--;
     s++;
+    ouv = uv;
+
     while (len--) {
        if ((*s & 0xc0) != 0x80) {
-            dTHR;
-           if (checking && retlen) {
-               *retlen = -1;
-               return 0;
-            }
-
-           if (ckWARN_d(WARN_UTF8))
-               Perl_warner(aTHX_ WARN_UTF8, "Malformed UTF-8 character");
-           if (retlen)
-               *retlen -= len + 1;
-           return 0xfffd;
+           if (dowarn)
+               Perl_warner(aTHX_ WARN_UTF8,
+                           "Malformed UTF-8 character (unexpected continuation byte 0x%02x)",
+                           *s);
+           goto malformed;
        }
        else
-           uv = (uv << 6) | (*s++ & 0x3f);
+           uv = (uv << 6) | (*s & 0x3f);
+       if (uv < ouv) {
+           /* This cannot be allowed. */
+           if (dowarn)
+               Perl_warner(aTHX_ WARN_UTF8,
+                           "Malformed UTF-8 character (overflow at 0x%"UVxf", byte 0x%02x)",
+                           ouv, *s);
+           goto malformed;
+       }
+       s++;
+       ouv = uv;
     }
+
+    if ((uv >= 0xd800 && uv <= 0xdfff) &&
+       !(flags & UTF8_ALLOW_SURROGATE)) {
+       if (dowarn)
+           Perl_warner(aTHX_ WARN_UTF8,
+                       "Malformed UTF-8 character (UTF-16 surrogate 0x%04"UVxf")",
+                       uv);
+       goto malformed;
+    } else if ((uv == 0xfffe) &&
+              !(flags & UTF8_ALLOW_BOM)) {
+       if (dowarn)
+           Perl_warner(aTHX_ WARN_UTF8,
+                       "Malformed UTF-8 character (byte order mark 0x%04"UVxf")",
+                       uv);
+       goto malformed;
+    } else if ((uv == 0xffff) &&
+              !(flags & UTF8_ALLOW_FFFF)) {
+       if (dowarn)
+           Perl_warner(aTHX_ WARN_UTF8,
+                       "Malformed UTF-8 character (character 0x%04"UVxf")",
+                       uv);
+       goto malformed;
+    } else if ((expectlen > UNISKIP(uv)) &&
+              !(flags & UTF8_ALLOW_LONG)) {
+       if (dowarn)
+           Perl_warner(aTHX_ WARN_UTF8,
+                       "Malformed UTF-8 character (%d byte%s, need %d)",
+                       expectlen, expectlen > 1 ? "s": "", UNISKIP(uv));
+       goto malformed;
+    }
+
     return uv;
+
+malformed:
+
+    if (flags & UTF8_CHECK_ONLY) {
+       if (retlen)
+           *retlen = len;
+       return 0;
+    }
+
+    if (retlen)
+       *retlen = -1;
+
+    return UNICODE_REPLACEMENT_CHARACTER;
 }
 
 /*
-=for apidoc Am|U8* s|utf8_to_uv|I32 *retlen
+=for apidoc Am|U8* s|utf8_to_uv_simple|STRLEN *retlen
 
 Returns the character value of the first character in the string C<s>
 which is assumed to be in UTF8 encoding; C<retlen> will be set to the
 length, in bytes, of that character, and the pointer C<s> will be
 advanced to the end of the character.
 
-If C<s> does not point to a well-formed UTF8 character, an optional UTF8
-warning is produced.
+If C<s> does not point to a well-formed UTF8 character, zero is
+returned and retlen is set, if possible, to -1.
 
 =cut
 */
 
 UV
-Perl_utf8_to_uv(pTHX_ U8* s, I32* retlen)
+Perl_utf8_to_uv_simple(pTHX_ U8* s, STRLEN* retlen)
 {
- return Perl_utf8_to_uv_chk(aTHX_ s, retlen, 0);
+    return Perl_utf8_to_uv(aTHX_ s, (STRLEN)-1, retlen, 0);
 }
 
 /* utf8_distance(a,b) returns the number of UTF8 characters between
@@ -302,30 +399,30 @@ Returns zero on failure, setting C<len> to -1.
 U8 *
 Perl_utf8_to_bytes(pTHX_ U8* s, STRLEN *len)
 {
-    dTHR;
     U8 *send;
     U8 *d;
-    U8 *save;
-
-    send = s + *len;
-    d = save = s;
+    U8 *save = s;
 
     /* ensure valid UTF8 and chars < 256 before updating string */
-    while (s < send) {
-       U8 c = *s++;
+    for (send = s + *len; s < send; ) {
+        U8 c = *s++;
+
         if (c >= 0x80 &&
-           ( (s >= send) || ((*s++ & 0xc0) != 0x80) || ((c & 0xfe) != 0xc2))) {
-           *len = -1;
-           return 0;
-       }
+            ((s >= send) ||
+            ((*s++ & 0xc0) != 0x80) || ((c & 0xfe) != 0xc2))) {
+            *len = -1;
+            return 0;
+        }
     }
-    s = save;
+
+    d = s = save;
     while (s < send) {
-        if (*s < 0x80)
-            *d++ = *s++;
+        if (*s < 0x80) {
+           *d++ = *s++;
+       }
         else {
-            I32 ulen;
-            *d++ = (U8)utf8_to_uv(s, &ulen);
+            STRLEN ulen;
+            *d++ = (U8)utf8_to_uv_simple(s, &ulen);
             s += ulen;
         }
     }
@@ -853,7 +950,7 @@ Perl_to_utf8_upper(pTHX_ U8 *p)
     if (!PL_utf8_toupper)
        PL_utf8_toupper = swash_init("utf8", "ToUpper", &PL_sv_undef, 4, 0);
     uv = swash_fetch(PL_utf8_toupper, p);
-    return uv ? uv : utf8_to_uv_chk(p,0,0);
+    return uv ? uv : utf8_to_uv(p,STRLEN_MAX,0,0);
 }
 
 UV
@@ -864,7 +961,7 @@ Perl_to_utf8_title(pTHX_ U8 *p)
     if (!PL_utf8_totitle)
        PL_utf8_totitle = swash_init("utf8", "ToTitle", &PL_sv_undef, 4, 0);
     uv = swash_fetch(PL_utf8_totitle, p);
-    return uv ? uv : utf8_to_uv_chk(p,0,0);
+    return uv ? uv : utf8_to_uv(p,STRLEN_MAX,0,0);
 }
 
 UV
@@ -875,7 +972,7 @@ Perl_to_utf8_lower(pTHX_ U8 *p)
     if (!PL_utf8_tolower)
        PL_utf8_tolower = swash_init("utf8", "ToLower", &PL_sv_undef, 4, 0);
     uv = swash_fetch(PL_utf8_tolower, p);
-    return uv ? uv : utf8_to_uv_chk(p,0,0);
+    return uv ? uv : utf8_to_uv(p,STRLEN_MAX,0,0);
 }
 
 /* a "swash" is a swatch hash */
@@ -965,7 +1062,7 @@ Perl_swash_fetch(pTHX_ SV *sv, U8 *ptr)
            PUSHMARK(SP);
            EXTEND(SP,3);
            PUSHs((SV*)sv);
-           PUSHs(sv_2mortal(newSViv(utf8_to_uv_chk(ptr, 0, 0) & ~(needents - 1))));
+           PUSHs(sv_2mortal(newSViv(utf8_to_uv(ptr, STRLEN_MAX, 0, 0) & ~(needents - 1))));
            PUSHs(sv_2mortal(newSViv(needents)));
            PUTBACK;
            if (call_method("SWASHGET", G_SCALAR))
@@ -992,7 +1089,7 @@ Perl_swash_fetch(pTHX_ SV *sv, U8 *ptr)
            Copy(ptr, PL_last_swash_key, klen, U8);
     }
 
-    switch ((slen << 3) / needents) {
+    switch ((int)((slen << 3) / needents)) {
     case 1:
        bit = 1 << (off & 7);
        off >>= 3;