]> git.saurik.com Git - wxWidgets.git/blobdiff - src/common/strconv.cpp
avoid an unnecessary and unused (resulting in release build warning) variable in...
[wxWidgets.git] / src / common / strconv.cpp
index 79ba45b9a11a7c948288e5462c893ea8a29456c8..0cdae0151a051943df4ed1c46481f8ef1898bc31 100644 (file)
@@ -565,7 +565,7 @@ static const unsigned char utf7unb64[] =
     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
-    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff 
+    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
 };
 
 size_t wxMBConvUTF7::ToWChar(wchar_t *dst, size_t dstLen,
@@ -609,11 +609,19 @@ size_t wxMBConvUTF7::ToWChar(wchar_t *dst, size_t dstLen,
             const unsigned char dc = utf7unb64[cc];
             if ( dc == 0xff )
             {
-                // end of encoded part, check that nothing was left: the bit
-                // field cycles through 0,6,4,2 sequence so check that we're at
-                // the end of it
-                if ( state.bit != 2 )
+                // end of encoded part, check that nothing was left: there can
+                // be up to 4 bits of 0 padding but nothing else (we also need
+                // to check isLSB as we count bits modulo 8 while a valid UTF-7
+                // encoded sequence must contain an integral number of UTF-16
+                // characters)
+                if ( state.isLSB || state.bit > 4 ||
+                        (state.accum & ((1 << state.bit) - 1)) )
+                {
+                    if ( !len )
+                        state = stateOrig;
+
                     return wxCONV_FAILED;
+                }
 
                 state.ToDirect();
 
@@ -666,7 +674,15 @@ size_t wxMBConvUTF7::ToWChar(wchar_t *dst, size_t dstLen,
                     len++;
                     src++;
                 }
-                else
+                else if ( utf7unb64[(unsigned)*src] == 0xff )
+                {
+                    // empty encoded chunks are not allowed
+                    if ( !len )
+                        state = stateOrig;
+
+                    return wxCONV_FAILED;
+                }
+                else // base-64 encoded chunk follows
                 {
                     state.ToShifted();
                 }
@@ -2284,7 +2300,7 @@ wxMBConv_iconv::ToWChar(wchar_t *dst, size_t dstLen,
     else // no destination buffer
     {
         // convert using temp buffer to calculate the size of the buffer needed
-        wchar_t tbuf[8];
+        wchar_t tbuf[256];
         res = 0;
 
         do
@@ -2331,13 +2347,12 @@ size_t wxMBConv_iconv::FromWChar(char *dst, size_t dstLen,
     if (ms_wcNeedsSwap)
     {
         // need to copy to temp buffer to switch endianness
-        // (doing WC_BSWAP twice on the original buffer won't help, as it
+        // (doing WC_BSWAP twice on the original buffer won't work, as it
         //  could be in read-only memory, or be accessed in some other thread)
-        tmpbuf = (wchar_t *)malloc(inbuflen + SIZEOF_WCHAR_T);
+        tmpbuf = (wchar_t *)malloc(inbuflen);
         for ( size_t i = 0; i < srcLen; i++ )
             tmpbuf[i] = WC_BSWAP(src[i]);
 
-        tmpbuf[srcLen] = L'\0';
         src = tmpbuf;
     }
 
@@ -2352,16 +2367,16 @@ size_t wxMBConv_iconv::FromWChar(char *dst, size_t dstLen,
     else // no destination buffer
     {
         // convert using temp buffer to calculate the size of the buffer needed
-        char tbuf[16];
+        char tbuf[256];
         res = 0;
         do
         {
             dst = tbuf;
-            outbuflen = 16;
+            outbuflen = WXSIZEOF(tbuf);
 
             cres = iconv(w2m, ICONV_CHAR_CAST(&inbuf), &inbuflen, &dst, &outbuflen);
 
-            res += 16 - outbuflen;
+            res += WXSIZEOF(tbuf) - outbuflen;
         }
         while ((cres == (size_t)-1) && (errno == E2BIG));
     }
@@ -2874,6 +2889,16 @@ wxCSConv::wxCSConv(const wxString& charset)
 
 #if wxUSE_FONTMAP
     m_encoding = wxFontMapperBase::GetEncodingFromName(charset);
+    if ( m_encoding == wxFONTENCODING_MAX )
+    {
+        // set to unknown/invalid value
+        m_encoding = wxFONTENCODING_SYSTEM;
+    }
+    else if ( m_encoding == wxFONTENCODING_DEFAULT )
+    {
+        // wxFONTENCODING_DEFAULT is same as US-ASCII in this context
+        m_encoding = wxFONTENCODING_ISO8859_1;
+    }
 #else
     m_encoding = wxFONTENCODING_SYSTEM;
 #endif