gnash-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Gnash-commit] gnash ChangeLog server/vm/ASHandlers.cpp server...


From: Benjamin Wolsey
Subject: [Gnash-commit] gnash ChangeLog server/vm/ASHandlers.cpp server...
Date: Thu, 27 Mar 2008 14:32:03 +0000

CVSROOT:        /sources/gnash
Module name:    gnash
Changes by:     Benjamin Wolsey <bwy>   08/03/27 14:32:03

Modified files:
        .              : ChangeLog 
        server/vm      : ASHandlers.cpp ASHandlers.h 

Log message:
                * server/vm/ASHandlers.{cpp,h}: const correct guessEncoding,
                  don't try to access vector out of bounds in ActionMbSubString.
                  use at() for runtime checking.
        
        Overhead or not, I think it's useful in this case, and mbsubstring is 
unlikely
        to be used often enough to cause much slow-down.
        
        I'm not convinced guessEncoding is working for UTF-8. Using the utf8
        methods might be more successful.

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/gnash/ChangeLog?cvsroot=gnash&r1=1.6045&r2=1.6046
http://cvs.savannah.gnu.org/viewcvs/gnash/server/vm/ASHandlers.cpp?cvsroot=gnash&r1=1.220&r2=1.221
http://cvs.savannah.gnu.org/viewcvs/gnash/server/vm/ASHandlers.h?cvsroot=gnash&r1=1.14&r2=1.15

Patches:
Index: ChangeLog
===================================================================
RCS file: /sources/gnash/gnash/ChangeLog,v
retrieving revision 1.6045
retrieving revision 1.6046
diff -u -b -r1.6045 -r1.6046
--- ChangeLog   27 Mar 2008 13:41:25 -0000      1.6045
+++ ChangeLog   27 Mar 2008 14:32:02 -0000      1.6046
@@ -1,5 +1,11 @@
 2008-03-27 Benjamin Wolsey <address@hidden>
 
+       * server/vm/ASHandlers.{cpp,h}: const correct guessEncoding,
+         don't try to access vector out of bounds in ActionMbSubString.
+         Use at() for run time checking.
+       
+2008-03-27 Benjamin Wolsey <address@hidden>
+
        * server/parser/action_buffer.{cpp,h}: fix very annoying
          g++4.3 compiler warnings about parentheses, use at() to
          access vectors, don't cast away constness in hexify call.

Index: server/vm/ASHandlers.cpp
===================================================================
RCS file: /sources/gnash/gnash/server/vm/ASHandlers.cpp,v
retrieving revision 1.220
retrieving revision 1.221
diff -u -b -r1.220 -r1.221
--- server/vm/ASHandlers.cpp    19 Mar 2008 20:50:56 -0000      1.220
+++ server/vm/ASHandlers.cpp    27 Mar 2008 14:32:03 -0000      1.221
@@ -1531,61 +1531,67 @@
 }
 
 as_encoding_guess_t
-SWFHandlers::GuessEncoding(std::string &str, int &length, std::vector<int>& 
offsets)
+SWFHandlers::guessEncoding(const std::string &str, int &length, 
std::vector<int>& offsets)
 {
-    const char *cstr = str.c_str();
-    const char *i = cstr;
     int width = 0; // The remaining width, not the total.
     bool is_sought = true;
-    int j;
-    int index = 0;
 
+    std::string::const_iterator it = str.begin();
     length = 0;
+    int index = 0;
+    
     // First, assume it's UTF8 and try to be wrong.
-    for (index = 0; is_sought && *i != '\0'; ++i, ++index)
+    while (it != str.end() && is_sought)
     {
-        j = static_cast<int> (*i);
+        int c = static_cast<int>(*it);
 
         if (width)
         {
             --width;
-            if ((j & 0xB0) != 0x80)
+            if ((c & 0xB0) != 0x80)
+            {
                 is_sought = false;
+            }
             continue;
         }
         ++length;
         offsets.push_back(index); //[length - 1] = index;
 
-        if ((j & 0xC0) == 0x80)
-            continue; // A 1 byte character.
-        else if ((j & 0xE0) == 0xC0)
-            width = 1;
-        else if ((j & 0xF0) == 0xE0)
-            width = 2;
-        else if ((j & 0xF8) == 0xF0)
-            width = 3;
-        else if (j & 0x80)
-            is_sought = false;
+        if ((c & 0xC0) == 0x80) continue; // A 1 byte character.
+        else if ((c & 0xE0) == 0xC0) width = 1;
+        else if ((c & 0xF0) == 0xE0) width = 2;
+        else if ((c & 0xF8) == 0xF0) width = 3;
+        else if (c & 0x80) is_sought = false;
+            
+        ++it;
+        ++index;
     }
+
     offsets.push_back(index); // [length - 1] = index;
-    if (!width && is_sought) // No width left, so it's almost certainly UTF8.
+
+    if (!width && is_sought)
+    {
+        // No width left, so it's almost certainly UTF8.
         return ENCGUESS_UNICODE;
+    }
 
+    it = str.begin();
+    index = 0;
     is_sought = true;
     width = 0;
     length = 0;
     bool was_odd = true;
     bool was_even = true;
     // Now, assume it's SHIFT_JIS and try to be wrong.
-    for (index = 0, i = cstr; is_sought && (*i != '\0'); ++i, ++index)
+    while (it != str.end() && is_sought)
     {
-        j = static_cast<int> (*i);
+        int c = static_cast<int> (*it);
 
         if (width)
         {
             --width;
-            if ((j < 0x40) || ((j < 0x9F) && was_even) ||
-                ((j > 0x9E) && was_odd) || (j == 0x7F))
+            if ((c < 0x40) || ((c < 0x9F) && was_even) ||
+                ((c > 0x9E) && was_odd) || (c == 0x7F))
             {
                 is_sought = false;
             }
@@ -1595,28 +1601,36 @@
         ++length;
         offsets.push_back(index); // [length - 1] = index;
 
-        if ((j == 0x80) || (j == 0xA0) || (j >= 0xF0))
+        if ((c == 0x80) || (c == 0xA0) || (c >= 0xF0))
         {
             is_sought = false;
             break;
         }
 
-        if (((j >= 0x81) && (j <= 0x9F)) || ((j >= 0xE0) && (j <= 0xEF)))
+        if (((c >= 0x81) && (c <= 0x9F)) || ((c >= 0xE0) && (c <= 0xEF)))
         {
             width = 1;
-            was_odd = j & 0x01;
+            was_odd = c & 0x01;
             was_even = !was_odd;
         }
         
+        it++;
+        index++;    
     }
     offsets.push_back(index); // [length - 1] = index;
-    if (!width && is_sought) // No width left, so it's probably SHIFT_JIS.
+    
+    if (!width && is_sought)
+    {
+        // No width left, so it's probably SHIFT_JIS.
         return ENCGUESS_JIS;
+    }
 
     // It's something else.
-    length = mbstowcs(NULL, cstr, 0);
+    length = mbstowcs(NULL, str.c_str(), 0);
     if (length == -1)
-        length = strlen(cstr);
+    {
+        length = str.length();
+    }
     return ENCGUESS_OTHER;
 }
 
@@ -1638,7 +1652,7 @@
         int length;
         std::vector<int> unused;
            unused.resize(str.length()+1);
-        (void) GuessEncoding(str, length, unused);
+        (void) guessEncoding(str, length, unused);
         env.top(0).set_int(length);
     }
 }
@@ -1747,9 +1761,8 @@
     string str = string_val.to_string();
     int length = 0;
     std::vector<int> offsets;
-    //offsets.resize(str.length() + 1);
 
-    as_encoding_guess_t encoding = GuessEncoding(str, length, offsets);
+    as_encoding_guess_t encoding = guessEncoding(str, length, offsets);
 
     //log_debug("Guessed encoding for %s: %d - len:%d, offsets.size:%d", 
str.c_str(), encoding, length, offsets.size());
     //for (int i=0; i<offsets.size(); ++i) log_debug("  offsets[%d]: %d", i, 
offsets[i]);
@@ -1772,7 +1785,7 @@
         start = 1;
     }
 
-    else if ( start > length )
+    else if ( start > length)
     {
        IF_VERBOSE_ASCODING_ERRORS (
        log_aserror(_("base goes beyond input string in ActionMbSubString, "
@@ -1785,11 +1798,11 @@
     // Adjust the start for our own use.
     --start;
 
-    if (size + start - 1 > length)
+    if (size + start > length)
     {
         IF_VERBOSE_ASCODING_ERRORS(
         log_aserror(_("base+size goes beyond input string in 
ActionMbSubString, "
-            "adjusting size based on length:%d and start:%d"), length,start);
+            "adjusting size based on length:%d and start:%d"), length, start);
         );
         size = length - start;
     }
@@ -1802,7 +1815,8 @@
     }
     else
     {
-        env.top(0).set_string(str.substr(offsets[start], offsets[start+size] - 
offsets[start]));
+        env.top(0).set_string(str.substr(offsets.at(start),
+                            offsets.at(start + size) - offsets.at(start)));
     }
     return;
 }

Index: server/vm/ASHandlers.h
===================================================================
RCS file: /sources/gnash/gnash/server/vm/ASHandlers.h,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -b -r1.14 -r1.15
--- server/vm/ASHandlers.h      21 Jan 2008 20:56:03 -0000      1.14
+++ server/vm/ASHandlers.h      27 Mar 2008 14:32:03 -0000      1.15
@@ -162,7 +162,7 @@
         // and the offsets to the characters in offsets, if offsets is not 
NULL.
         // If not NULL, offsets should be at least s.length().
         // offsets are not accurate if the return value is GUESSENC_OTHER
-        static as_encoding_guess_t GuessEncoding(std::string& s, int& length,
+    static as_encoding_guess_t guessEncoding(const std::string& s, int& length,
             std::vector<int>& offsets);
 
        static void ActionEnd(ActionExec& thread);




reply via email to

[Prev in Thread] Current Thread [Next in Thread]