]> git.saurik.com Git - wxWidgets.git/blobdiff - src/common/datstrm.cpp
Avoid using buffer of already deallocated string in wxHTMLDataObject.
[wxWidgets.git] / src / common / datstrm.cpp
index 67a2e458050716b9e217faccfdafc981671df2b2..c55f2134fbc9b703f3b5df0df9ffe2334d7a79aa 100644 (file)
 #if wxUSE_STREAMS
 
 #include "wx/datstrm.h"
-#include "wx/math.h"
+
+#ifndef WX_PRECOMP
+    #include "wx/math.h"
+#endif //WX_PRECOMP
+
+namespace
+{
+
+// helper unions used to swap bytes of floats and doubles
+union Float32Data
+{
+    wxFloat32 f;
+    wxUint32 i;
+};
+
+union Float64Data
+{
+    wxFloat64 f;
+    wxUint32 i[2];
+};
+
+} // anonymous namespace
+
+// ----------------------------------------------------------------------------
+// wxDataStreamBase
+// ----------------------------------------------------------------------------
+
+wxDataStreamBase::wxDataStreamBase(const wxMBConv& conv)
+#if wxUSE_UNICODE
+    : m_conv(conv.Clone())
+#endif // wxUSE_UNICODE
+{
+    // It is unused in non-Unicode build, so suppress a warning there.
+    wxUnusedVar(conv);
+
+    m_be_order = false;
+
+    // For compatibility with the existing data files, we use extended
+    // precision if it is available, i.e. if wxUSE_APPLE_IEEE is on.
+#if wxUSE_APPLE_IEEE
+    m_useExtendedPrecision = true;
+#endif // wxUSE_APPLE_IEEE
+}
+
+#if wxUSE_UNICODE
+void wxDataStreamBase::SetConv( const wxMBConv &conv )
+{
+    delete m_conv;
+    m_conv = conv.Clone();
+}
+#endif
+
+wxDataStreamBase::~wxDataStreamBase()
+{
+#if wxUSE_UNICODE
+    delete m_conv;
+#endif // wxUSE_UNICODE
+}
 
 // ---------------------------------------------------------------------------
 // wxDataInputStream
 // ---------------------------------------------------------------------------
 
-#if wxUSE_UNICODE
 wxDataInputStream::wxDataInputStream(wxInputStream& s, const wxMBConv& conv)
-  : m_input(&s), m_be_order(false), m_conv(conv)
-#else
-wxDataInputStream::wxDataInputStream(wxInputStream& s)
-  : m_input(&s), m_be_order(false)
-#endif
+  : wxDataStreamBase(conv),
+    m_input(&s)
 {
 }
 
@@ -79,36 +132,72 @@ wxUint8 wxDataInputStream::Read8()
 double wxDataInputStream::ReadDouble()
 {
 #if wxUSE_APPLE_IEEE
-  char buf[10];
+    if ( m_useExtendedPrecision )
+    {
+        char buf[10];
 
-  m_input->Read(buf, 10);
-  return ConvertFromIeeeExtended((const wxInt8 *)buf);
-#else
-  return 0.0;
-#endif
+        m_input->Read(buf, 10);
+        return wxConvertFromIeeeExtended((const wxInt8 *)buf);
+    }
+    else
+#endif // wxUSE_APPLE_IEEE
+    {
+        Float64Data floatData;
+
+        if ( m_be_order == (wxBYTE_ORDER == wxBIG_ENDIAN) )
+        {
+            floatData.i[0] = Read32();
+            floatData.i[1] = Read32();
+        }
+        else
+        {
+            floatData.i[1] = Read32();
+            floatData.i[0] = Read32();
+        }
+
+        return static_cast<double>(floatData.f);
+    }
 }
 
-wxString wxDataInputStream::ReadString()
+float wxDataInputStream::ReadFloat()
 {
-  size_t len;
+#if wxUSE_APPLE_IEEE
+    if ( m_useExtendedPrecision )
+    {
+        return (float)ReadDouble();
+    }
+    else
+#endif // wxUSE_APPLE_IEEE
+    {
+        Float32Data floatData;
 
-  len = Read32();
+        floatData.i = Read32();
+        return static_cast<float>(floatData.f);
+    }
+}
 
-  if (len > 0)
-  {
+wxString wxDataInputStream::ReadString()
+{
+    wxString ret;
+
+    const size_t len = Read32();
+    if ( len > 0 )
+    {
 #if wxUSE_UNICODE
-    wxCharBuffer tmp(len + 1);
-    m_input->Read(tmp.data(), len);
-    tmp.data()[len] = '\0';
-    wxString ret(m_conv.cMB2WX(tmp.data()));
+        wxCharBuffer tmp(len);
+        if ( tmp )
+        {
+            m_input->Read(tmp.data(), len);
+            ret = m_conv->cMB2WX(tmp.data());
+        }
 #else
-    wxString ret;
-    m_input->Read( wxStringBuffer(ret, len), len);
+        wxStringBuffer buf(ret, len);
+        if ( buf )
+            m_input->Read(buf, len);
 #endif
+    }
+
     return ret;
-  }
-  else
-    return wxEmptyString;
 }
 
 #if wxUSE_LONGLONG
@@ -358,6 +447,14 @@ void wxDataInputStream::ReadDouble(double *buffer, size_t size)
   }
 }
 
+void wxDataInputStream::ReadFloat(float *buffer, size_t size)
+{
+  for (wxUint32 i=0; i<size; i++)
+  {
+    *(buffer++) = ReadFloat();
+  }
+}
+
 wxDataInputStream& wxDataInputStream::operator>>(wxString& s)
 {
   s = ReadString();
@@ -428,15 +525,15 @@ wxDataInputStream& wxDataInputStream::operator>>(wxLongLong& i)
 }
 #endif // wxLongLong_t
 
-wxDataInputStream& wxDataInputStream::operator>>(double& i)
+wxDataInputStream& wxDataInputStream::operator>>(double& d)
 {
-  i = ReadDouble();
+  d = ReadDouble();
   return *this;
 }
 
 wxDataInputStream& wxDataInputStream::operator>>(float& f)
 {
-  f = (float)ReadDouble();
+  f = ReadFloat();
   return *this;
 }
 
@@ -444,13 +541,9 @@ wxDataInputStream& wxDataInputStream::operator>>(float& f)
 // wxDataOutputStream
 // ---------------------------------------------------------------------------
 
-#if wxUSE_UNICODE
 wxDataOutputStream::wxDataOutputStream(wxOutputStream& s, const wxMBConv& conv)
-  : m_output(&s), m_be_order(false), m_conv(conv)
-#else
-wxDataOutputStream::wxDataOutputStream(wxOutputStream& s)
-  : m_output(&s), m_be_order(false)
-#endif
+  : wxDataStreamBase(conv),
+    m_output(&s)
 {
 }
 
@@ -497,7 +590,7 @@ void wxDataOutputStream::Write8(wxUint8 i)
 void wxDataOutputStream::WriteString(const wxString& string)
 {
 #if wxUSE_UNICODE
-  const wxWX2MBbuf buf = string.mb_str(m_conv);
+  const wxWX2MBbuf buf = string.mb_str(*m_conv);
 #else
   const wxWX2MBbuf buf = string.mb_str();
 #endif
@@ -509,17 +602,49 @@ void wxDataOutputStream::WriteString(const wxString& string)
 
 void wxDataOutputStream::WriteDouble(double d)
 {
-  char buf[10];
+#if wxUSE_APPLE_IEEE
+    if ( m_useExtendedPrecision )
+    {
+        char buf[10];
+
+        wxConvertToIeeeExtended(d, (wxInt8 *)buf);
+        m_output->Write(buf, 10);
+    }
+    else
+#endif // wxUSE_APPLE_IEEE
+    {
+        Float64Data floatData;
+
+        floatData.f = (wxFloat64)d;
 
+        if ( m_be_order == (wxBYTE_ORDER == wxBIG_ENDIAN) )
+        {
+            Write32(floatData.i[0]);
+            Write32(floatData.i[1]);
+        }
+        else
+        {
+            Write32(floatData.i[1]);
+            Write32(floatData.i[0]);
+        }
+    }
+}
+
+void wxDataOutputStream::WriteFloat(float f)
+{
 #if wxUSE_APPLE_IEEE
-  ConvertToIeeeExtended(d, (wxInt8 *)buf);
-#else
-#if !defined(__VMS__) && !defined(__GNUG__)
-# pragma warning "wxDataOutputStream::WriteDouble() not using IeeeExtended - will not work!"
-#endif
-   buf[0] = '\0';
-#endif
-  m_output->Write(buf, 10);
+    if ( m_useExtendedPrecision )
+    {
+        WriteDouble((double)f);
+    }
+    else
+#endif // wxUSE_APPLE_IEEE
+    {
+        Float32Data floatData;
+
+        floatData.f = (wxFloat32)f;
+        Write32(floatData.i);
+    }
 }
 
 #if wxHAS_INT64
@@ -633,11 +758,12 @@ void wxDataOutputStream::WriteDouble(const double *buffer, size_t size)
   }
 }
 
-wxDataOutputStream& wxDataOutputStream::operator<<(const wxChar *string)
+void wxDataOutputStream::WriteFloat(const float *buffer, size_t size)
 {
-  Write32(wxStrlen(string));
-  m_output->Write((const char *)string, wxStrlen(string)*sizeof(wxChar));
-  return *this;
+  for (wxUint32 i=0; i<size; i++)
+  {
+    WriteFloat(*(buffer++));
+  }
 }
 
 wxDataOutputStream& wxDataOutputStream::operator<<(const wxString& string)
@@ -710,15 +836,15 @@ wxDataOutputStream& wxDataOutputStream::operator<<(const wxLongLong &i)
 }
 #endif // wxLongLong_t
 
-wxDataOutputStream& wxDataOutputStream::operator<<(double f)
+wxDataOutputStream& wxDataOutputStream::operator<<(double d)
 {
-  WriteDouble(f);
+  WriteDouble(d);
   return *this;
 }
 
 wxDataOutputStream& wxDataOutputStream::operator<<(float f)
 {
-  WriteDouble((double)f);
+  WriteFloat(f);
   return *this;
 }